2009-03-03 19:32:27 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-07-30 13:37:37 -07:00
|
|
|
package com.android.launcher2;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-10-28 15:36:06 -07:00
|
|
|
import com.android.launcher.R;
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2010-10-10 11:26:02 -07:00
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.animation.AnimatorListenerAdapter;
|
2010-10-14 09:01:34 -07:00
|
|
|
import android.animation.AnimatorSet;
|
|
|
|
|
import android.animation.ObjectAnimator;
|
2010-10-07 11:13:10 -07:00
|
|
|
import android.animation.TimeInterpolator;
|
2010-09-27 11:15:43 -07:00
|
|
|
import android.animation.ValueAnimator;
|
|
|
|
|
import android.animation.ValueAnimator.AnimatorUpdateListener;
|
2010-06-11 17:34:16 -07:00
|
|
|
import android.app.WallpaperManager;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.content.Context;
|
2009-09-21 15:23:04 -04:00
|
|
|
import android.content.res.Resources;
|
2010-06-11 17:34:16 -07:00
|
|
|
import android.content.res.TypedArray;
|
2010-10-10 11:26:02 -07:00
|
|
|
import android.graphics.Bitmap;
|
2010-06-11 17:34:16 -07:00
|
|
|
import android.graphics.Canvas;
|
2010-10-10 11:26:02 -07:00
|
|
|
import android.graphics.Paint;
|
2010-09-27 11:15:43 -07:00
|
|
|
import android.graphics.Point;
|
|
|
|
|
import android.graphics.PointF;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.graphics.RectF;
|
2010-10-14 09:01:34 -07:00
|
|
|
import android.graphics.Region;
|
2010-07-12 14:25:18 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.util.AttributeSet;
|
2010-10-10 11:26:02 -07:00
|
|
|
import android.util.Log;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.view.ContextMenu;
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewDebug;
|
|
|
|
|
import android.view.ViewGroup;
|
2010-06-11 17:34:16 -07:00
|
|
|
import android.view.animation.Animation;
|
2010-09-29 17:14:26 -07:00
|
|
|
import android.view.animation.DecelerateInterpolator;
|
2010-06-11 17:34:16 -07:00
|
|
|
import android.view.animation.LayoutAnimationController;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-10-28 15:36:06 -07:00
|
|
|
import java.util.Arrays;
|
2010-10-14 13:21:48 -07:00
|
|
|
|
2010-09-30 14:11:56 -07:00
|
|
|
public class CellLayout extends ViewGroup implements Dimmable {
|
2010-06-11 17:34:16 -07:00
|
|
|
static final String TAG = "CellLayout";
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
private int mCellWidth;
|
|
|
|
|
private int mCellHeight;
|
2010-06-11 17:34:16 -07:00
|
|
|
|
|
|
|
|
private int mLeftPadding;
|
|
|
|
|
private int mRightPadding;
|
|
|
|
|
private int mTopPadding;
|
|
|
|
|
private int mBottomPadding;
|
|
|
|
|
|
2010-07-26 22:02:18 -07:00
|
|
|
private int mCountX;
|
|
|
|
|
private int mCountY;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
private int mWidthGap;
|
|
|
|
|
private int mHeightGap;
|
|
|
|
|
|
|
|
|
|
private final Rect mRect = new Rect();
|
2010-07-16 13:55:32 -07:00
|
|
|
private final RectF mRectF = new RectF();
|
2009-03-03 19:32:27 -08:00
|
|
|
private final CellInfo mCellInfo = new CellInfo();
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2010-09-27 11:15:43 -07:00
|
|
|
// These are temporary variables to prevent having to allocate a new object just to
|
|
|
|
|
// return an (x, y) value from helper functions. Do NOT use them to maintain other state.
|
2010-07-12 14:25:18 -07:00
|
|
|
private final int[] mTmpCellXY = new int[2];
|
2010-09-27 11:15:43 -07:00
|
|
|
private final int[] mTmpPoint = new int[2];
|
|
|
|
|
private final PointF mTmpPointF = new PointF();
|
2010-07-12 14:25:18 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
boolean[][] mOccupied;
|
|
|
|
|
|
2010-07-27 10:01:56 -07:00
|
|
|
private OnTouchListener mInterceptTouchListener;
|
|
|
|
|
|
2010-09-03 14:15:02 -07:00
|
|
|
private float mBackgroundAlpha;
|
2010-10-28 11:11:18 -07:00
|
|
|
private float mBackgroundAlphaMultiplier = 1.0f;
|
2010-09-30 14:11:56 -07:00
|
|
|
|
2010-09-03 14:15:02 -07:00
|
|
|
private Drawable mBackground;
|
2010-09-30 14:11:56 -07:00
|
|
|
private Drawable mBackgroundMini;
|
|
|
|
|
private Drawable mBackgroundMiniHover;
|
2010-10-06 15:49:50 -07:00
|
|
|
private Drawable mBackgroundHover;
|
2010-10-02 16:01:03 -07:00
|
|
|
private Drawable mBackgroundMiniAcceptsDrops;
|
2010-10-14 09:01:34 -07:00
|
|
|
private Rect mBackgroundRect;
|
|
|
|
|
private Rect mHoverRect;
|
|
|
|
|
private float mHoverScale;
|
|
|
|
|
private float mHoverAlpha;
|
2010-10-02 16:01:03 -07:00
|
|
|
private boolean mAcceptsDrops;
|
2010-10-06 15:49:50 -07:00
|
|
|
|
|
|
|
|
// If we're actively dragging something over this screen, mHover is true
|
2010-08-19 13:52:27 -07:00
|
|
|
private boolean mHover = false;
|
2010-07-27 10:01:56 -07:00
|
|
|
|
2010-09-27 11:15:43 -07:00
|
|
|
private final Point mDragCenter = new Point();
|
2010-07-12 14:25:18 -07:00
|
|
|
|
2010-09-29 17:14:26 -07:00
|
|
|
// These arrays are used to implement the drag visualization on x-large screens.
|
2010-10-10 11:26:02 -07:00
|
|
|
// They are used as circular arrays, indexed by mDragOutlineCurrent.
|
|
|
|
|
private Point[] mDragOutlines = new Point[8];
|
2010-10-14 07:02:04 -07:00
|
|
|
private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
|
2010-10-10 11:26:02 -07:00
|
|
|
private InterruptibleInOutAnimator[] mDragOutlineAnims =
|
|
|
|
|
new InterruptibleInOutAnimator[mDragOutlines.length];
|
2010-09-29 17:14:26 -07:00
|
|
|
|
|
|
|
|
// Used as an index into the above 3 arrays; indicates which is the most current value.
|
2010-10-10 11:26:02 -07:00
|
|
|
private int mDragOutlineCurrent = 0;
|
2010-10-14 13:21:48 -07:00
|
|
|
private final Paint mDragOutlinePaint = new Paint();
|
2010-09-29 17:14:26 -07:00
|
|
|
|
2010-09-27 11:15:43 -07:00
|
|
|
private Drawable mCrosshairsDrawable = null;
|
2010-10-08 15:33:52 -07:00
|
|
|
private InterruptibleInOutAnimator mCrosshairsAnimator = null;
|
2010-09-27 11:15:43 -07:00
|
|
|
private float mCrosshairsVisibility = 0.0f;
|
|
|
|
|
|
2010-07-12 14:25:18 -07:00
|
|
|
// When a drag operation is in progress, holds the nearest cell to the touch point
|
|
|
|
|
private final int[] mDragCell = new int[2];
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-06-11 17:34:16 -07:00
|
|
|
private final WallpaperManager mWallpaperManager;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-10-10 11:26:02 -07:00
|
|
|
private boolean mDragging = false;
|
|
|
|
|
|
2010-10-19 10:34:32 -07:00
|
|
|
private TimeInterpolator mEaseOutInterpolator;
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
public CellLayout(Context context) {
|
|
|
|
|
this(context, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CellLayout(Context context, AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CellLayout(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
|
super(context, attrs, defStyle);
|
2010-07-12 14:25:18 -07:00
|
|
|
|
|
|
|
|
// A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
|
|
|
|
|
// the user where a dragged item will land when dropped.
|
|
|
|
|
setWillNotDraw(false);
|
2010-08-19 13:52:27 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
|
|
|
|
|
|
|
|
|
|
mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
|
|
|
|
|
mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
|
2010-10-22 14:54:12 -07:00
|
|
|
mWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, -1);
|
|
|
|
|
mHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, -1);
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2010-07-26 22:02:18 -07:00
|
|
|
mLeftPadding =
|
|
|
|
|
a.getDimensionPixelSize(R.styleable.CellLayout_xAxisStartPadding, 10);
|
|
|
|
|
mRightPadding =
|
|
|
|
|
a.getDimensionPixelSize(R.styleable.CellLayout_xAxisEndPadding, 10);
|
|
|
|
|
mTopPadding =
|
|
|
|
|
a.getDimensionPixelSize(R.styleable.CellLayout_yAxisStartPadding, 10);
|
|
|
|
|
mBottomPadding =
|
|
|
|
|
a.getDimensionPixelSize(R.styleable.CellLayout_yAxisEndPadding, 10);
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2010-07-26 22:02:18 -07:00
|
|
|
mCountX = LauncherModel.getCellCountX();
|
|
|
|
|
mCountY = LauncherModel.getCellCountY();
|
2010-09-17 15:00:07 -07:00
|
|
|
mOccupied = new boolean[mCountX][mCountY];
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
a.recycle();
|
|
|
|
|
|
|
|
|
|
setAlwaysDrawnWithCacheEnabled(false);
|
|
|
|
|
|
2010-09-27 11:15:43 -07:00
|
|
|
mWallpaperManager = WallpaperManager.getInstance(context);
|
|
|
|
|
|
2010-10-06 12:14:43 -07:00
|
|
|
final Resources res = getResources();
|
2010-09-27 11:15:43 -07:00
|
|
|
|
2010-10-06 12:14:43 -07:00
|
|
|
if (LauncherApplication.isScreenXLarge()) {
|
2010-09-29 17:14:26 -07:00
|
|
|
mBackgroundMini = res.getDrawable(R.drawable.mini_home_screen_bg);
|
2010-09-30 14:11:56 -07:00
|
|
|
mBackgroundMini.setFilterBitmap(true);
|
2010-09-29 17:14:26 -07:00
|
|
|
mBackground = res.getDrawable(R.drawable.home_screen_bg);
|
2010-09-27 11:15:43 -07:00
|
|
|
mBackground.setFilterBitmap(true);
|
2010-09-29 17:14:26 -07:00
|
|
|
mBackgroundMiniHover = res.getDrawable(R.drawable.mini_home_screen_bg_hover);
|
2010-09-30 14:11:56 -07:00
|
|
|
mBackgroundMiniHover.setFilterBitmap(true);
|
2010-10-06 15:49:50 -07:00
|
|
|
mBackgroundHover = res.getDrawable(R.drawable.home_screen_bg_hover);
|
|
|
|
|
mBackgroundHover.setFilterBitmap(true);
|
2010-10-02 16:01:03 -07:00
|
|
|
mBackgroundMiniAcceptsDrops = res.getDrawable(
|
|
|
|
|
R.drawable.mini_home_screen_bg_accepts_drops);
|
|
|
|
|
mBackgroundMiniAcceptsDrops.setFilterBitmap(true);
|
2010-10-06 12:14:43 -07:00
|
|
|
}
|
2010-09-27 11:15:43 -07:00
|
|
|
|
2010-10-06 12:14:43 -07:00
|
|
|
// Initialize the data structures used for the drag visualization.
|
2010-09-29 17:14:26 -07:00
|
|
|
|
2010-10-06 12:14:43 -07:00
|
|
|
mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
|
2010-10-19 10:34:32 -07:00
|
|
|
mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
|
2010-09-27 11:15:43 -07:00
|
|
|
|
2010-10-06 12:14:43 -07:00
|
|
|
// Set up the animation for fading the crosshairs in and out
|
|
|
|
|
int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
|
2010-10-08 15:33:52 -07:00
|
|
|
mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
|
2010-10-14 07:02:04 -07:00
|
|
|
mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
|
2010-10-06 12:14:43 -07:00
|
|
|
public void onAnimationUpdate(ValueAnimator animation) {
|
|
|
|
|
mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
|
2010-10-14 13:21:48 -07:00
|
|
|
invalidate();
|
2010-10-06 12:14:43 -07:00
|
|
|
}
|
|
|
|
|
});
|
2010-10-19 10:34:32 -07:00
|
|
|
mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
|
2010-10-06 12:14:43 -07:00
|
|
|
|
2010-10-10 11:26:02 -07:00
|
|
|
for (int i = 0; i < mDragOutlines.length; i++) {
|
|
|
|
|
mDragOutlines[i] = new Point(-1, -1);
|
2010-10-06 12:14:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// When dragging things around the home screens, we show a green outline of
|
|
|
|
|
// where the item will land. The outlines gradually fade out, leaving a trail
|
|
|
|
|
// behind the drag path.
|
|
|
|
|
// Set up all the animations that are used to implement this fading.
|
|
|
|
|
final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
|
2010-10-14 07:02:04 -07:00
|
|
|
final float fromAlphaValue = 0;
|
|
|
|
|
final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
|
2010-10-10 11:26:02 -07:00
|
|
|
|
2010-10-14 13:21:48 -07:00
|
|
|
Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
|
2010-10-10 11:26:02 -07:00
|
|
|
|
|
|
|
|
for (int i = 0; i < mDragOutlineAnims.length; i++) {
|
2010-10-06 12:14:43 -07:00
|
|
|
final InterruptibleInOutAnimator anim =
|
|
|
|
|
new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
|
2010-10-19 10:34:32 -07:00
|
|
|
anim.getAnimator().setInterpolator(mEaseOutInterpolator);
|
2010-10-06 12:14:43 -07:00
|
|
|
final int thisIndex = i;
|
2010-10-14 07:02:04 -07:00
|
|
|
anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
|
2010-09-27 11:15:43 -07:00
|
|
|
public void onAnimationUpdate(ValueAnimator animation) {
|
2010-10-10 11:26:02 -07:00
|
|
|
final Bitmap outline = (Bitmap)anim.getTag();
|
|
|
|
|
|
|
|
|
|
// If an animation is started and then stopped very quickly, we can still
|
|
|
|
|
// get spurious updates we've cleared the tag. Guard against this.
|
|
|
|
|
if (outline == null) {
|
2010-10-13 17:32:10 -07:00
|
|
|
if (false) {
|
|
|
|
|
Object val = animation.getAnimatedValue();
|
|
|
|
|
Log.d(TAG, "anim " + thisIndex + " update: " + val +
|
|
|
|
|
", isStopped " + anim.isStopped());
|
|
|
|
|
}
|
2010-10-10 11:26:02 -07:00
|
|
|
// Try to prevent it from continuing to run
|
|
|
|
|
animation.cancel();
|
|
|
|
|
} else {
|
2010-10-14 07:02:04 -07:00
|
|
|
mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
|
2010-10-10 11:26:02 -07:00
|
|
|
final int left = mDragOutlines[thisIndex].x;
|
|
|
|
|
final int top = mDragOutlines[thisIndex].y;
|
|
|
|
|
CellLayout.this.invalidate(left, top,
|
|
|
|
|
left + outline.getWidth(), top + outline.getHeight());
|
|
|
|
|
}
|
2010-09-27 11:15:43 -07:00
|
|
|
}
|
|
|
|
|
});
|
2010-10-10 11:26:02 -07:00
|
|
|
// The animation holds a reference to the drag outline bitmap as long is it's
|
|
|
|
|
// running. This way the bitmap can be GCed when the animations are complete.
|
2010-10-14 07:02:04 -07:00
|
|
|
anim.getAnimator().addListener(new AnimatorListenerAdapter() {
|
2010-10-28 15:36:06 -07:00
|
|
|
@Override
|
2010-10-10 11:26:02 -07:00
|
|
|
public void onAnimationEnd(Animator animation) {
|
2010-10-14 07:02:04 -07:00
|
|
|
if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
|
2010-10-10 11:26:02 -07:00
|
|
|
anim.setTag(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
mDragOutlineAnims[i] = anim;
|
2010-09-27 11:15:43 -07:00
|
|
|
}
|
2010-10-19 10:34:32 -07:00
|
|
|
|
2010-10-14 09:01:34 -07:00
|
|
|
mBackgroundRect = new Rect();
|
|
|
|
|
mHoverRect = new Rect();
|
|
|
|
|
setHoverScale(1.0f);
|
|
|
|
|
setHoverAlpha(1.0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateHoverRect() {
|
|
|
|
|
float marginFraction = (mHoverScale - 1.0f) / 2.0f;
|
|
|
|
|
int marginX = (int) (marginFraction * (mBackgroundRect.right - mBackgroundRect.left));
|
|
|
|
|
int marginY = (int) (marginFraction * (mBackgroundRect.bottom - mBackgroundRect.top));
|
|
|
|
|
mHoverRect.set(mBackgroundRect.left - marginX, mBackgroundRect.top - marginY,
|
|
|
|
|
mBackgroundRect.right + marginX, mBackgroundRect.bottom + marginY);
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setHoverScale(float scaleFactor) {
|
|
|
|
|
if (scaleFactor != mHoverScale) {
|
|
|
|
|
mHoverScale = scaleFactor;
|
|
|
|
|
updateHoverRect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float getHoverScale() {
|
|
|
|
|
return mHoverScale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float getHoverAlpha() {
|
|
|
|
|
return mHoverAlpha;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setHoverAlpha(float alpha) {
|
|
|
|
|
mHoverAlpha = alpha;
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void animateDrop() {
|
|
|
|
|
if (LauncherApplication.isScreenXLarge()) {
|
|
|
|
|
Resources res = getResources();
|
|
|
|
|
float onDropScale = res.getInteger(R.integer.config_screenOnDropScalePercent) / 100.0f;
|
|
|
|
|
ObjectAnimator scaleUp = ObjectAnimator.ofFloat(this, "hoverScale", onDropScale);
|
|
|
|
|
scaleUp.setDuration(res.getInteger(R.integer.config_screenOnDropScaleUpDuration));
|
|
|
|
|
ObjectAnimator scaleDown = ObjectAnimator.ofFloat(this, "hoverScale", 1.0f);
|
|
|
|
|
scaleDown.setDuration(res.getInteger(R.integer.config_screenOnDropScaleDownDuration));
|
|
|
|
|
ObjectAnimator alphaFadeOut = ObjectAnimator.ofFloat(this, "hoverAlpha", 0.0f);
|
|
|
|
|
|
|
|
|
|
alphaFadeOut.setStartDelay(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
|
|
|
|
|
alphaFadeOut.setDuration(res.getInteger(R.integer.config_screenOnDropAlphaFadeDelay));
|
|
|
|
|
|
|
|
|
|
AnimatorSet bouncer = new AnimatorSet();
|
|
|
|
|
bouncer.play(scaleUp).before(scaleDown);
|
|
|
|
|
bouncer.play(scaleUp).with(alphaFadeOut);
|
2010-10-28 15:36:06 -07:00
|
|
|
bouncer.addListener(new LauncherAnimatorListenerAdapter() {
|
|
|
|
|
@Override
|
2010-10-14 09:01:34 -07:00
|
|
|
public void onAnimationStart(Animator animation) {
|
|
|
|
|
setHover(true);
|
|
|
|
|
}
|
2010-10-28 15:36:06 -07:00
|
|
|
@Override
|
|
|
|
|
public void onAnimationEndOrCancel(Animator animation) {
|
2010-10-14 09:01:34 -07:00
|
|
|
setHover(false);
|
|
|
|
|
setHoverScale(1.0f);
|
|
|
|
|
setHoverAlpha(1.0f);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
bouncer.start();
|
|
|
|
|
}
|
2009-11-10 02:54:41 -08:00
|
|
|
}
|
|
|
|
|
|
2010-08-19 13:52:27 -07:00
|
|
|
public void setHover(boolean value) {
|
|
|
|
|
if (mHover != value) {
|
2010-10-02 16:01:03 -07:00
|
|
|
mHover = value;
|
2010-08-19 13:52:27 -07:00
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-03 22:12:02 -07:00
|
|
|
public boolean getHover() {
|
|
|
|
|
return mHover;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-06 15:49:50 -07:00
|
|
|
public void drawChildren(Canvas canvas) {
|
|
|
|
|
super.dispatchDraw(canvas);
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-10 02:54:41 -08:00
|
|
|
@Override
|
2010-10-06 15:49:50 -07:00
|
|
|
protected void onDraw(Canvas canvas) {
|
2010-10-02 16:01:03 -07:00
|
|
|
// When we're large, we are either drawn in a "hover" state (ie when dragging an item to
|
|
|
|
|
// a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
|
|
|
|
|
// When we're small, we are either drawn normally or in the "accepts drops" state (during
|
|
|
|
|
// a drag). However, we also drag the mini hover background *over* one of those two
|
|
|
|
|
// backgrounds
|
2010-09-03 14:15:02 -07:00
|
|
|
if (mBackgroundAlpha > 0.0f) {
|
2010-09-30 14:11:56 -07:00
|
|
|
Drawable bg;
|
2010-10-06 15:49:50 -07:00
|
|
|
if (getScaleX() < 0.5f) {
|
2010-10-02 16:01:03 -07:00
|
|
|
bg = mAcceptsDrops ? mBackgroundMiniAcceptsDrops : mBackgroundMini;
|
2010-09-30 14:11:56 -07:00
|
|
|
} else {
|
2010-10-06 15:49:50 -07:00
|
|
|
bg = mHover ? mBackgroundHover : mBackground;
|
2010-09-30 14:11:56 -07:00
|
|
|
}
|
2010-10-05 12:27:22 -07:00
|
|
|
if (bg != null) {
|
2010-10-28 11:11:18 -07:00
|
|
|
bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
|
2010-10-14 09:01:34 -07:00
|
|
|
bg.setBounds(mBackgroundRect);
|
2010-10-05 12:27:22 -07:00
|
|
|
bg.draw(canvas);
|
|
|
|
|
}
|
2010-10-02 16:01:03 -07:00
|
|
|
if (mHover && getScaleX() < 0.5f) {
|
2010-10-14 09:01:34 -07:00
|
|
|
boolean modifiedClipRect = false;
|
|
|
|
|
if (mHoverScale > 1.0f) {
|
|
|
|
|
// If the hover background's scale is greater than 1, we'll be drawing outside
|
|
|
|
|
// the bounds of this CellLayout. Get around that by temporarily increasing the
|
|
|
|
|
// size of the clip rect
|
|
|
|
|
float marginFraction = (mHoverScale - 1.0f) / 2.0f;
|
|
|
|
|
Rect clipRect = canvas.getClipBounds();
|
|
|
|
|
int marginX = (int) (marginFraction * (clipRect.right - clipRect.left));
|
|
|
|
|
int marginY = (int) (marginFraction * (clipRect.bottom - clipRect.top));
|
|
|
|
|
canvas.save(Canvas.CLIP_SAVE_FLAG);
|
|
|
|
|
canvas.clipRect(-marginX, -marginY,
|
|
|
|
|
getWidth() + marginX, getHeight() + marginY, Region.Op.REPLACE);
|
|
|
|
|
modifiedClipRect = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mBackgroundMiniHover.setAlpha((int) (mBackgroundAlpha * mHoverAlpha * 255));
|
|
|
|
|
mBackgroundMiniHover.setBounds(mHoverRect);
|
2010-10-02 16:01:03 -07:00
|
|
|
mBackgroundMiniHover.draw(canvas);
|
2010-10-14 09:01:34 -07:00
|
|
|
if (modifiedClipRect) {
|
|
|
|
|
canvas.restore();
|
|
|
|
|
}
|
2010-10-02 16:01:03 -07:00
|
|
|
}
|
2010-08-19 13:52:27 -07:00
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-09-27 11:15:43 -07:00
|
|
|
if (mCrosshairsVisibility > 0.0f) {
|
|
|
|
|
final int countX = mCountX;
|
|
|
|
|
final int countY = mCountY;
|
|
|
|
|
|
|
|
|
|
final float MAX_ALPHA = 0.4f;
|
|
|
|
|
final int MAX_VISIBLE_DISTANCE = 600;
|
|
|
|
|
final float DISTANCE_MULTIPLIER = 0.002f;
|
|
|
|
|
|
|
|
|
|
final Drawable d = mCrosshairsDrawable;
|
|
|
|
|
final int width = d.getIntrinsicWidth();
|
|
|
|
|
final int height = d.getIntrinsicHeight();
|
|
|
|
|
|
|
|
|
|
int x = getLeftPadding() - (mWidthGap / 2) - (width / 2);
|
|
|
|
|
for (int col = 0; col <= countX; col++) {
|
|
|
|
|
int y = getTopPadding() - (mHeightGap / 2) - (height / 2);
|
|
|
|
|
for (int row = 0; row <= countY; row++) {
|
|
|
|
|
mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
|
|
|
|
|
float dist = mTmpPointF.length();
|
|
|
|
|
// Crosshairs further from the drag point are more faint
|
|
|
|
|
float alpha = Math.min(MAX_ALPHA,
|
|
|
|
|
DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
|
|
|
|
|
if (alpha > 0.0f) {
|
|
|
|
|
d.setBounds(x, y, x + width, y + height);
|
|
|
|
|
d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
|
|
|
|
|
d.draw(canvas);
|
|
|
|
|
}
|
|
|
|
|
y += mCellHeight + mHeightGap;
|
|
|
|
|
}
|
|
|
|
|
x += mCellWidth + mWidthGap;
|
|
|
|
|
}
|
2010-10-10 11:26:02 -07:00
|
|
|
}
|
2010-09-29 17:14:26 -07:00
|
|
|
|
2010-10-14 13:21:48 -07:00
|
|
|
final Paint paint = mDragOutlinePaint;
|
2010-10-10 11:26:02 -07:00
|
|
|
for (int i = 0; i < mDragOutlines.length; i++) {
|
2010-10-14 07:02:04 -07:00
|
|
|
final float alpha = mDragOutlineAlphas[i];
|
2010-10-10 11:26:02 -07:00
|
|
|
if (alpha > 0) {
|
|
|
|
|
final Point p = mDragOutlines[i];
|
|
|
|
|
final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
|
2010-10-14 07:02:04 -07:00
|
|
|
paint.setAlpha((int)(alpha + .5f));
|
2010-10-10 11:26:02 -07:00
|
|
|
canvas.drawBitmap(b, p.x, p.y, paint);
|
2010-09-29 17:14:26 -07:00
|
|
|
}
|
2010-07-12 14:25:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-30 14:11:56 -07:00
|
|
|
public void setDimmableProgress(float progress) {
|
|
|
|
|
for (int i = 0; i < getChildCount(); i++) {
|
|
|
|
|
Dimmable d = (Dimmable) getChildAt(i);
|
|
|
|
|
d.setDimmableProgress(progress);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float getDimmableProgress() {
|
|
|
|
|
if (getChildCount() > 0) {
|
|
|
|
|
return ((Dimmable) getChildAt(0)).getDimmableProgress();
|
|
|
|
|
}
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-20 21:03:13 -07:00
|
|
|
@Override
|
|
|
|
|
public void cancelLongPress() {
|
|
|
|
|
super.cancelLongPress();
|
|
|
|
|
|
|
|
|
|
// Cancel long press for all children
|
|
|
|
|
final int count = getChildCount();
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
final View child = getChildAt(i);
|
|
|
|
|
child.cancelLongPress();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-27 10:01:56 -07:00
|
|
|
public void setOnInterceptTouchListener(View.OnTouchListener listener) {
|
|
|
|
|
mInterceptTouchListener = listener;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
int getCountX() {
|
2010-07-26 22:02:18 -07:00
|
|
|
return mCountX;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int getCountY() {
|
2010-07-26 22:02:18 -07:00
|
|
|
return mCountY;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2010-06-11 17:34:16 -07:00
|
|
|
public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params) {
|
2010-10-20 17:08:24 -07:00
|
|
|
return addViewToCellLayout(child, index, childId, params, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean addViewToCellLayout(
|
|
|
|
|
View child, int index, int childId, LayoutParams params, boolean markCells) {
|
2010-06-11 17:34:16 -07:00
|
|
|
final LayoutParams lp = params;
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
// Generate an id for each view, this assumes we have at most 256x256 cells
|
|
|
|
|
// per workspace screen
|
2010-07-26 22:02:18 -07:00
|
|
|
if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
|
2010-06-11 17:34:16 -07:00
|
|
|
// If the horizontal or vertical span is set to -1, it is taken to
|
|
|
|
|
// mean that it spans the extent of the CellLayout
|
2010-07-26 22:02:18 -07:00
|
|
|
if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
|
|
|
|
|
if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
|
2010-06-11 17:34:16 -07:00
|
|
|
|
|
|
|
|
child.setId(childId);
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-07-27 10:01:56 -07:00
|
|
|
// We might be in the middle or end of shrinking/fading to a dimmed view
|
|
|
|
|
// Make sure this view's alpha is set the same as all the rest of the views
|
2010-09-03 14:15:02 -07:00
|
|
|
child.setAlpha(getAlpha());
|
2010-06-11 17:34:16 -07:00
|
|
|
addView(child, index, lp);
|
2010-07-27 10:01:56 -07:00
|
|
|
|
2010-10-20 17:08:24 -07:00
|
|
|
if (markCells) markCellsAsOccupiedForView(child);
|
2010-09-17 15:00:07 -07:00
|
|
|
|
2010-06-11 17:34:16 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2010-10-02 16:01:03 -07:00
|
|
|
public void setAcceptsDrops(boolean acceptsDrops) {
|
|
|
|
|
if (mAcceptsDrops != acceptsDrops) {
|
|
|
|
|
mAcceptsDrops = acceptsDrops;
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean getAcceptsDrops() {
|
|
|
|
|
return mAcceptsDrops;
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
@Override
|
|
|
|
|
public void removeAllViews() {
|
|
|
|
|
clearOccupiedCells();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void removeAllViewsInLayout() {
|
|
|
|
|
clearOccupiedCells();
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-20 17:08:24 -07:00
|
|
|
public void removeViewWithoutMarkingCells(View view) {
|
|
|
|
|
super.removeView(view);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
@Override
|
|
|
|
|
public void removeView(View view) {
|
|
|
|
|
markCellsAsUnoccupiedForView(view);
|
|
|
|
|
super.removeView(view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void removeViewAt(int index) {
|
|
|
|
|
markCellsAsUnoccupiedForView(getChildAt(index));
|
|
|
|
|
super.removeViewAt(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void removeViewInLayout(View view) {
|
|
|
|
|
markCellsAsUnoccupiedForView(view);
|
|
|
|
|
super.removeViewInLayout(view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void removeViews(int start, int count) {
|
|
|
|
|
for (int i = start; i < start + count; i++) {
|
|
|
|
|
markCellsAsUnoccupiedForView(getChildAt(i));
|
|
|
|
|
}
|
|
|
|
|
super.removeViews(start, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void removeViewsInLayout(int start, int count) {
|
|
|
|
|
for (int i = start; i < start + count; i++) {
|
|
|
|
|
markCellsAsUnoccupiedForView(getChildAt(i));
|
|
|
|
|
}
|
|
|
|
|
super.removeViewsInLayout(start, count);
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public void requestChildFocus(View child, View focused) {
|
|
|
|
|
super.requestChildFocus(child, focused);
|
|
|
|
|
if (child != null) {
|
|
|
|
|
Rect r = new Rect();
|
|
|
|
|
child.getDrawingRect(r);
|
|
|
|
|
requestRectangleOnScreen(r);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onAttachedToWindow() {
|
|
|
|
|
super.onAttachedToWindow();
|
|
|
|
|
mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-10 17:01:57 -07:00
|
|
|
public void setTagToCellInfoForPoint(int touchX, int touchY) {
|
2009-03-03 19:32:27 -08:00
|
|
|
final CellInfo cellInfo = mCellInfo;
|
2010-06-10 17:01:57 -07:00
|
|
|
final Rect frame = mRect;
|
|
|
|
|
final int x = touchX + mScrollX;
|
|
|
|
|
final int y = touchY + mScrollY;
|
|
|
|
|
final int count = getChildCount();
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-06-10 17:01:57 -07:00
|
|
|
boolean found = false;
|
|
|
|
|
for (int i = count - 1; i >= 0; i--) {
|
|
|
|
|
final View child = getChildAt(i);
|
|
|
|
|
|
|
|
|
|
if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
|
|
|
|
|
child.getHitRect(frame);
|
|
|
|
|
if (frame.contains(x, y)) {
|
|
|
|
|
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
|
|
|
|
|
cellInfo.cell = child;
|
|
|
|
|
cellInfo.cellX = lp.cellX;
|
|
|
|
|
cellInfo.cellY = lp.cellY;
|
|
|
|
|
cellInfo.spanX = lp.cellHSpan;
|
|
|
|
|
cellInfo.spanY = lp.cellVSpan;
|
|
|
|
|
cellInfo.valid = true;
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
2010-06-10 17:01:57 -07:00
|
|
|
}
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2010-06-10 17:01:57 -07:00
|
|
|
if (!found) {
|
2010-07-12 14:25:18 -07:00
|
|
|
final int cellXY[] = mTmpCellXY;
|
2010-06-10 17:01:57 -07:00
|
|
|
pointToCellExact(x, y, cellXY);
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-06-10 17:01:57 -07:00
|
|
|
cellInfo.cell = null;
|
|
|
|
|
cellInfo.cellX = cellXY[0];
|
|
|
|
|
cellInfo.cellY = cellXY[1];
|
|
|
|
|
cellInfo.spanX = 1;
|
|
|
|
|
cellInfo.spanY = 1;
|
2010-09-17 15:00:07 -07:00
|
|
|
cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < mCountX &&
|
|
|
|
|
cellXY[1] < mCountY && !mOccupied[cellXY[0]][cellXY[1]];
|
2010-06-10 17:01:57 -07:00
|
|
|
}
|
|
|
|
|
setTag(cellInfo);
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2010-06-10 17:01:57 -07:00
|
|
|
@Override
|
|
|
|
|
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
2010-07-27 10:01:56 -07:00
|
|
|
if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-06-10 17:01:57 -07:00
|
|
|
final int action = ev.getAction();
|
|
|
|
|
final CellInfo cellInfo = mCellInfo;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-06-10 17:01:57 -07:00
|
|
|
if (action == MotionEvent.ACTION_DOWN) {
|
|
|
|
|
setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
|
2009-03-03 19:32:27 -08:00
|
|
|
} else if (action == MotionEvent.ACTION_UP) {
|
|
|
|
|
cellInfo.cell = null;
|
|
|
|
|
cellInfo.cellX = -1;
|
|
|
|
|
cellInfo.cellY = -1;
|
|
|
|
|
cellInfo.spanX = 0;
|
|
|
|
|
cellInfo.spanY = 0;
|
|
|
|
|
cellInfo.valid = false;
|
|
|
|
|
setTag(cellInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public CellInfo getTag() {
|
2010-09-17 15:00:07 -07:00
|
|
|
return (CellInfo) super.getTag();
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2010-07-12 14:25:18 -07:00
|
|
|
/**
|
|
|
|
|
* Check if the row 'y' is empty from columns 'left' to 'right', inclusive.
|
|
|
|
|
*/
|
2009-03-03 19:32:27 -08:00
|
|
|
private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) {
|
|
|
|
|
for (int x = left; x <= right; x++) {
|
|
|
|
|
if (occupied[x][y]) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-06-11 17:34:16 -07:00
|
|
|
* Given a point, return the cell that strictly encloses that point
|
2009-03-03 19:32:27 -08:00
|
|
|
* @param x X coordinate of the point
|
|
|
|
|
* @param y Y coordinate of the point
|
|
|
|
|
* @param result Array of 2 ints to hold the x and y coordinate of the cell
|
|
|
|
|
*/
|
|
|
|
|
void pointToCellExact(int x, int y, int[] result) {
|
2010-06-11 17:34:16 -07:00
|
|
|
final int hStartPadding = getLeftPadding();
|
|
|
|
|
final int vStartPadding = getTopPadding();
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
|
|
|
|
|
result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
|
|
|
|
|
|
2010-07-26 22:02:18 -07:00
|
|
|
final int xAxis = mCountX;
|
|
|
|
|
final int yAxis = mCountY;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
if (result[0] < 0) result[0] = 0;
|
|
|
|
|
if (result[0] >= xAxis) result[0] = xAxis - 1;
|
|
|
|
|
if (result[1] < 0) result[1] = 0;
|
|
|
|
|
if (result[1] >= yAxis) result[1] = yAxis - 1;
|
|
|
|
|
}
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
|
|
|
|
* Given a point, return the cell that most closely encloses that point
|
|
|
|
|
* @param x X coordinate of the point
|
|
|
|
|
* @param y Y coordinate of the point
|
|
|
|
|
* @param result Array of 2 ints to hold the x and y coordinate of the cell
|
|
|
|
|
*/
|
|
|
|
|
void pointToCellRounded(int x, int y, int[] result) {
|
|
|
|
|
pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given a cell coordinate, return the point that represents the upper left corner of that cell
|
2010-06-11 17:34:16 -07:00
|
|
|
*
|
|
|
|
|
* @param cellX X coordinate of the cell
|
2009-03-03 19:32:27 -08:00
|
|
|
* @param cellY Y coordinate of the cell
|
2010-06-11 17:34:16 -07:00
|
|
|
*
|
2009-03-03 19:32:27 -08:00
|
|
|
* @param result Array of 2 ints to hold the x and y coordinate of the point
|
|
|
|
|
*/
|
|
|
|
|
void cellToPoint(int cellX, int cellY, int[] result) {
|
2010-06-11 17:34:16 -07:00
|
|
|
final int hStartPadding = getLeftPadding();
|
|
|
|
|
final int vStartPadding = getTopPadding();
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
|
|
|
|
|
result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-04 15:00:44 -08:00
|
|
|
int getCellWidth() {
|
|
|
|
|
return mCellWidth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int getCellHeight() {
|
|
|
|
|
return mCellHeight;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-10 00:02:32 -08:00
|
|
|
int getLeftPadding() {
|
2010-06-11 17:34:16 -07:00
|
|
|
return mLeftPadding;
|
2009-11-10 00:02:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int getTopPadding() {
|
2010-06-11 17:34:16 -07:00
|
|
|
return mTopPadding;
|
2009-11-10 00:02:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int getRightPadding() {
|
2010-06-11 17:34:16 -07:00
|
|
|
return mRightPadding;
|
2009-11-10 00:02:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int getBottomPadding() {
|
2010-06-11 17:34:16 -07:00
|
|
|
return mBottomPadding;
|
2009-11-10 00:02:32 -08:00
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
|
// TODO: currently ignoring padding
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
|
2010-06-11 17:34:16 -07:00
|
|
|
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
|
|
|
|
|
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
|
|
|
|
|
throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final int cellWidth = mCellWidth;
|
|
|
|
|
final int cellHeight = mCellHeight;
|
|
|
|
|
|
2010-07-26 22:02:18 -07:00
|
|
|
int numWidthGaps = mCountX - 1;
|
|
|
|
|
int numHeightGaps = mCountY - 1;
|
|
|
|
|
|
2010-10-22 14:54:12 -07:00
|
|
|
if (mWidthGap < 0 || mHeightGap < 0) {
|
|
|
|
|
int vSpaceLeft = heightSpecSize - mTopPadding - mBottomPadding - (cellHeight * mCountY);
|
|
|
|
|
mHeightGap = vSpaceLeft / numHeightGaps;
|
2010-07-26 22:02:18 -07:00
|
|
|
|
2010-10-22 14:54:12 -07:00
|
|
|
int hSpaceLeft = widthSpecSize - mLeftPadding - mRightPadding - (cellWidth * mCountX);
|
|
|
|
|
mWidthGap = hSpaceLeft / numWidthGaps;
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2010-10-22 14:54:12 -07:00
|
|
|
// center it around the min gaps
|
|
|
|
|
int minGap = Math.min(mWidthGap, mHeightGap);
|
|
|
|
|
mWidthGap = mHeightGap = minGap;
|
|
|
|
|
}
|
2010-09-03 14:15:02 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
int count = getChildCount();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
View child = getChildAt(i);
|
|
|
|
|
LayoutParams lp = (LayoutParams) child.getLayoutParams();
|
2010-06-11 17:34:16 -07:00
|
|
|
lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap,
|
|
|
|
|
mLeftPadding, mTopPadding);
|
|
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
|
2010-06-11 17:34:16 -07:00
|
|
|
int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height,
|
|
|
|
|
MeasureSpec.EXACTLY);
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
child.measure(childWidthMeasureSpec, childheightMeasureSpec);
|
|
|
|
|
}
|
2010-09-03 14:15:02 -07:00
|
|
|
if (widthSpecMode == MeasureSpec.AT_MOST) {
|
|
|
|
|
int newWidth = mLeftPadding + mRightPadding + (mCountX * cellWidth) +
|
2010-10-22 14:54:12 -07:00
|
|
|
((mCountX - 1) * mWidthGap);
|
2010-09-03 14:15:02 -07:00
|
|
|
int newHeight = mTopPadding + mBottomPadding + (mCountY * cellHeight) +
|
2010-10-22 14:54:12 -07:00
|
|
|
((mCountY - 1) * mHeightGap);
|
2010-09-03 14:15:02 -07:00
|
|
|
setMeasuredDimension(newWidth, newHeight);
|
|
|
|
|
} else if (widthSpecMode == MeasureSpec.EXACTLY) {
|
|
|
|
|
setMeasuredDimension(widthSpecSize, heightSpecSize);
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2010-09-24 17:43:49 -07:00
|
|
|
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
2009-03-03 19:32:27 -08:00
|
|
|
int count = getChildCount();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
2010-10-19 10:34:32 -07:00
|
|
|
final View child = getChildAt(i);
|
2009-03-03 19:32:27 -08:00
|
|
|
if (child.getVisibility() != GONE) {
|
|
|
|
|
|
|
|
|
|
CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
|
|
|
|
|
|
|
|
|
|
int childLeft = lp.x;
|
|
|
|
|
int childTop = lp.y;
|
|
|
|
|
child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);
|
2009-11-04 15:00:44 -08:00
|
|
|
|
|
|
|
|
if (lp.dropped) {
|
|
|
|
|
lp.dropped = false;
|
|
|
|
|
|
2010-07-12 14:25:18 -07:00
|
|
|
final int[] cellXY = mTmpCellXY;
|
2010-01-25 16:51:08 -08:00
|
|
|
getLocationOnScreen(cellXY);
|
2009-11-04 15:00:44 -08:00
|
|
|
mWallpaperManager.sendWallpaperCommand(getWindowToken(), "android.home.drop",
|
2010-01-25 16:51:08 -08:00
|
|
|
cellXY[0] + childLeft + lp.width / 2,
|
|
|
|
|
cellXY[1] + childTop + lp.height / 2, 0, null);
|
2010-10-19 10:34:32 -07:00
|
|
|
|
2010-10-28 17:57:05 -07:00
|
|
|
((Workspace) mParent).animateViewIntoPosition(child);
|
2009-11-04 15:00:44 -08:00
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-27 10:01:56 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
|
|
|
|
super.onSizeChanged(w, h, oldw, oldh);
|
2010-10-14 09:01:34 -07:00
|
|
|
mBackgroundRect.set(0, 0, w, h);
|
|
|
|
|
updateHoverRect();
|
2010-07-27 10:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
protected void setChildrenDrawingCacheEnabled(boolean enabled) {
|
|
|
|
|
final int count = getChildCount();
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
final View view = getChildAt(i);
|
|
|
|
|
view.setDrawingCacheEnabled(enabled);
|
|
|
|
|
// Update the drawing caches
|
2010-10-26 14:29:35 -07:00
|
|
|
if (!view.isHardwareAccelerated()) {
|
|
|
|
|
view.buildDrawingCache(true);
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
|
|
|
|
|
super.setChildrenDrawnWithCacheEnabled(enabled);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-03 14:15:02 -07:00
|
|
|
public float getBackgroundAlpha() {
|
|
|
|
|
return mBackgroundAlpha;
|
2010-07-27 10:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
2010-10-28 11:11:18 -07:00
|
|
|
public void setBackgroundAlphaMultiplier(float multiplier) {
|
|
|
|
|
mBackgroundAlphaMultiplier = multiplier;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-10 16:32:54 -08:00
|
|
|
public float getBackgroundAlphaMultiplier() {
|
|
|
|
|
return mBackgroundAlphaMultiplier;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-03 14:15:02 -07:00
|
|
|
public void setBackgroundAlpha(float alpha) {
|
|
|
|
|
mBackgroundAlpha = alpha;
|
2010-08-25 17:46:15 -07:00
|
|
|
invalidate();
|
2010-07-27 10:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
2010-09-03 14:15:02 -07:00
|
|
|
// Need to return true to let the view system know we know how to handle alpha-- this is
|
|
|
|
|
// because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
|
|
|
|
|
// versions
|
|
|
|
|
@Override
|
|
|
|
|
protected boolean onSetAlpha(int alpha) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setAlpha(float alpha) {
|
|
|
|
|
setChildrenAlpha(alpha);
|
|
|
|
|
super.setAlpha(alpha);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-27 10:01:56 -07:00
|
|
|
private void setChildrenAlpha(float alpha) {
|
2010-08-25 17:46:15 -07:00
|
|
|
final int childCount = getChildCount();
|
|
|
|
|
for (int i = 0; i < childCount; i++) {
|
2010-07-27 10:01:56 -07:00
|
|
|
getChildAt(i).setAlpha(alpha);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
private boolean isVacantIgnoring(
|
|
|
|
|
int originX, int originY, int spanX, int spanY, View ignoreView) {
|
|
|
|
|
if (ignoreView != null) {
|
|
|
|
|
markCellsAsUnoccupiedForView(ignoreView);
|
|
|
|
|
}
|
2010-09-24 17:43:49 -07:00
|
|
|
boolean isVacant = true;
|
2010-07-12 14:25:18 -07:00
|
|
|
for (int i = 0; i < spanY; i++) {
|
|
|
|
|
if (!isRowEmpty(originY + i, originX, originX + spanX - 1, mOccupied)) {
|
2010-09-24 17:43:49 -07:00
|
|
|
isVacant = false;
|
|
|
|
|
break;
|
2010-07-12 14:25:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-17 15:00:07 -07:00
|
|
|
if (ignoreView != null) {
|
|
|
|
|
markCellsAsOccupiedForView(ignoreView);
|
|
|
|
|
}
|
2010-09-24 17:43:49 -07:00
|
|
|
return isVacant;
|
2010-07-12 14:25:18 -07:00
|
|
|
}
|
|
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
private boolean isVacant(int originX, int originY, int spanX, int spanY) {
|
|
|
|
|
return isVacantIgnoring(originX, originY, spanX, spanY, null);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-13 17:50:32 -07:00
|
|
|
public View getChildAt(int x, int y) {
|
|
|
|
|
final int count = getChildCount();
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
View child = getChildAt(i);
|
|
|
|
|
LayoutParams lp = (LayoutParams) child.getLayoutParams();
|
|
|
|
|
|
|
|
|
|
if ((lp.cellX <= x) && (x < lp.cellX + lp.cellHSpan) &&
|
|
|
|
|
(lp.cellY <= y) && (y < lp.cellY + lp.cellHSpan)) {
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-16 13:55:32 -07:00
|
|
|
/**
|
|
|
|
|
* Estimate the size that a child with the given dimensions will take in the layout.
|
|
|
|
|
*/
|
|
|
|
|
void estimateChildSize(int minWidth, int minHeight, int[] result) {
|
|
|
|
|
// Assuming it's placed at 0, 0, find where the bottom right cell will land
|
|
|
|
|
rectToCell(minWidth, minHeight, result);
|
|
|
|
|
|
|
|
|
|
// Then figure out the rect it will occupy
|
|
|
|
|
cellToRect(0, 0, result[0], result[1], mRectF);
|
|
|
|
|
result[0] = (int)mRectF.width();
|
|
|
|
|
result[1] = (int)mRectF.height();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-12 14:25:18 -07:00
|
|
|
/**
|
|
|
|
|
* Estimate where the top left cell of the dragged item will land if it is dropped.
|
|
|
|
|
*
|
|
|
|
|
* @param originX The X value of the top left corner of the item
|
|
|
|
|
* @param originY The Y value of the top left corner of the item
|
|
|
|
|
* @param spanX The number of horizontal cells that the item spans
|
|
|
|
|
* @param spanY The number of vertical cells that the item spans
|
|
|
|
|
* @param result The estimated drop cell X and Y.
|
|
|
|
|
*/
|
|
|
|
|
void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
|
2010-07-26 22:02:18 -07:00
|
|
|
final int countX = mCountX;
|
|
|
|
|
final int countY = mCountY;
|
2010-07-12 14:25:18 -07:00
|
|
|
|
2010-08-19 13:52:27 -07:00
|
|
|
// pointToCellRounded takes the top left of a cell but will pad that with
|
|
|
|
|
// cellWidth/2 and cellHeight/2 when finding the matching cell
|
|
|
|
|
pointToCellRounded(originX, originY, result);
|
2010-07-12 14:25:18 -07:00
|
|
|
|
|
|
|
|
// If the item isn't fully on this screen, snap to the edges
|
|
|
|
|
int rightOverhang = result[0] + spanX - countX;
|
|
|
|
|
if (rightOverhang > 0) {
|
|
|
|
|
result[0] -= rightOverhang; // Snap to right
|
|
|
|
|
}
|
|
|
|
|
result[0] = Math.max(0, result[0]); // Snap to left
|
|
|
|
|
int bottomOverhang = result[1] + spanY - countY;
|
|
|
|
|
if (bottomOverhang > 0) {
|
|
|
|
|
result[1] -= bottomOverhang; // Snap to bottom
|
|
|
|
|
}
|
|
|
|
|
result[1] = Math.max(0, result[1]); // Snap to top
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-10 11:26:02 -07:00
|
|
|
void visualizeDropLocation(
|
|
|
|
|
View v, Bitmap dragOutline, int originX, int originY, int spanX, int spanY) {
|
|
|
|
|
|
2010-10-14 23:54:22 -07:00
|
|
|
final int oldDragCellX = mDragCell[0];
|
|
|
|
|
final int oldDragCellY = mDragCell[1];
|
2010-10-10 11:26:02 -07:00
|
|
|
final int[] nearest = findNearestVacantArea(originX, originY, spanX, spanY, v, mDragCell);
|
2010-10-27 17:18:37 -07:00
|
|
|
if (v != null) {
|
|
|
|
|
mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
|
|
|
|
|
} else {
|
|
|
|
|
mDragCenter.set(originX, originY);
|
|
|
|
|
}
|
2010-07-12 14:25:18 -07:00
|
|
|
|
2010-10-14 23:54:22 -07:00
|
|
|
if (nearest != null && (nearest[0] != oldDragCellX || nearest[1] != oldDragCellY)) {
|
2010-07-12 14:25:18 -07:00
|
|
|
// Find the top left corner of the rect the object will occupy
|
2010-09-27 11:15:43 -07:00
|
|
|
final int[] topLeft = mTmpPoint;
|
|
|
|
|
cellToPoint(nearest[0], nearest[1], topLeft);
|
|
|
|
|
|
2010-10-10 11:26:02 -07:00
|
|
|
int left = topLeft[0];
|
|
|
|
|
int top = topLeft[1];
|
2010-07-12 14:25:18 -07:00
|
|
|
|
2010-10-27 17:18:37 -07:00
|
|
|
if (v != null) {
|
|
|
|
|
if (v.getParent() instanceof CellLayout) {
|
|
|
|
|
LayoutParams lp = (LayoutParams) v.getLayoutParams();
|
|
|
|
|
left += lp.leftMargin;
|
|
|
|
|
top += lp.topMargin;
|
|
|
|
|
}
|
2010-09-29 17:14:26 -07:00
|
|
|
|
2010-10-27 17:18:37 -07:00
|
|
|
// Offsets due to the size difference between the View and the dragOutline
|
|
|
|
|
left += (v.getWidth() - dragOutline.getWidth()) / 2;
|
|
|
|
|
top += (v.getHeight() - dragOutline.getHeight()) / 2;
|
|
|
|
|
}
|
2010-09-29 17:14:26 -07:00
|
|
|
|
2010-10-10 11:26:02 -07:00
|
|
|
final int oldIndex = mDragOutlineCurrent;
|
2010-10-14 23:54:22 -07:00
|
|
|
mDragOutlineAnims[oldIndex].animateOut();
|
|
|
|
|
mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
|
2010-09-29 17:14:26 -07:00
|
|
|
|
2010-10-14 23:54:22 -07:00
|
|
|
mDragOutlines[mDragOutlineCurrent].set(left, top);
|
|
|
|
|
mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
|
|
|
|
|
mDragOutlineAnims[mDragOutlineCurrent].animateIn();
|
2010-07-12 14:25:18 -07:00
|
|
|
}
|
2010-10-08 15:33:52 -07:00
|
|
|
|
|
|
|
|
// If we are drawing crosshairs, the entire CellLayout needs to be invalidated
|
|
|
|
|
if (mCrosshairsDrawable != null) {
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
2010-07-12 14:25:18 -07:00
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
2009-04-07 21:08:40 -07:00
|
|
|
* Find a vacant area that will fit the given bounds nearest the requested
|
|
|
|
|
* cell location. Uses Euclidean distance to score multiple vacant areas.
|
2010-06-11 17:34:16 -07:00
|
|
|
*
|
2009-05-04 18:03:43 -07:00
|
|
|
* @param pixelX The X location at which you want to search for a vacant area.
|
|
|
|
|
* @param pixelY The Y location at which you want to search for a vacant area.
|
2009-04-07 21:08:40 -07:00
|
|
|
* @param spanX Horizontal span of the object.
|
|
|
|
|
* @param spanY Vertical span of the object.
|
2010-09-27 11:15:43 -07:00
|
|
|
* @param result Array in which to place the result, or null (in which case a new array will
|
|
|
|
|
* be allocated)
|
2009-04-07 21:08:40 -07:00
|
|
|
* @return The X, Y cell of a vacant area that can contain this object,
|
|
|
|
|
* nearest the requested location.
|
2009-03-03 19:32:27 -08:00
|
|
|
*/
|
2010-09-27 17:35:12 -07:00
|
|
|
int[] findNearestVacantArea(
|
2010-09-27 11:15:43 -07:00
|
|
|
int pixelX, int pixelY, int spanX, int spanY, int[] result) {
|
|
|
|
|
return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
|
2010-09-27 17:35:12 -07:00
|
|
|
}
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2010-09-27 17:35:12 -07:00
|
|
|
/**
|
|
|
|
|
* Find a vacant area that will fit the given bounds nearest the requested
|
|
|
|
|
* cell location. Uses Euclidean distance to score multiple vacant areas.
|
|
|
|
|
*
|
|
|
|
|
* @param pixelX The X location at which you want to search for a vacant area.
|
|
|
|
|
* @param pixelY The Y location at which you want to search for a vacant area.
|
|
|
|
|
* @param spanX Horizontal span of the object.
|
|
|
|
|
* @param spanY Vertical span of the object.
|
|
|
|
|
* @param ignoreView Considers space occupied by this view as unoccupied
|
2010-09-27 11:15:43 -07:00
|
|
|
* @param result Previously returned value to possibly recycle.
|
2010-09-27 17:35:12 -07:00
|
|
|
* @return The X, Y cell of a vacant area that can contain this object,
|
|
|
|
|
* nearest the requested location.
|
|
|
|
|
*/
|
|
|
|
|
int[] findNearestVacantArea(
|
2010-09-27 11:15:43 -07:00
|
|
|
int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
|
2010-09-30 12:04:50 -07:00
|
|
|
// mark space take by ignoreView as available (method checks if ignoreView is null)
|
|
|
|
|
markCellsAsUnoccupiedForView(ignoreView);
|
|
|
|
|
|
2009-04-07 21:08:40 -07:00
|
|
|
// Keep track of best-scoring drop area
|
2010-09-27 11:15:43 -07:00
|
|
|
final int[] bestXY = result != null ? result : new int[2];
|
2009-04-07 21:08:40 -07:00
|
|
|
double bestDistance = Double.MAX_VALUE;
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2010-09-27 11:15:43 -07:00
|
|
|
final int countX = mCountX;
|
|
|
|
|
final int countY = mCountY;
|
|
|
|
|
final boolean[][] occupied = mOccupied;
|
|
|
|
|
|
2010-11-11 16:34:41 -08:00
|
|
|
for (int y = 0; y < countY - (spanY - 1); y++) {
|
2010-08-13 11:27:44 -07:00
|
|
|
inner:
|
2010-11-11 16:34:41 -08:00
|
|
|
for (int x = 0; x < countX - (spanX - 1); x++) {
|
2010-08-13 11:27:44 -07:00
|
|
|
for (int i = 0; i < spanX; i++) {
|
|
|
|
|
for (int j = 0; j < spanY; j++) {
|
2010-09-27 11:15:43 -07:00
|
|
|
if (occupied[x + i][y + j]) {
|
2010-11-11 16:34:41 -08:00
|
|
|
// small optimization: we can skip to after the column we just found
|
2010-08-13 11:27:44 -07:00
|
|
|
// an occupied cell
|
2010-11-11 16:34:41 -08:00
|
|
|
x += i;
|
2010-08-13 11:27:44 -07:00
|
|
|
continue inner;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
final int[] cellXY = mTmpCellXY;
|
|
|
|
|
cellToPoint(x, y, cellXY);
|
|
|
|
|
|
|
|
|
|
double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
|
|
|
|
|
+ Math.pow(cellXY[1] - pixelY, 2));
|
|
|
|
|
if (distance <= bestDistance) {
|
|
|
|
|
bestDistance = distance;
|
|
|
|
|
bestXY[0] = x;
|
|
|
|
|
bestXY[1] = y;
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-30 12:04:50 -07:00
|
|
|
// re-mark space taken by ignoreView as occupied
|
|
|
|
|
markCellsAsOccupiedForView(ignoreView);
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-06-11 17:34:16 -07:00
|
|
|
// Return null if no suitable location found
|
2009-04-07 21:08:40 -07:00
|
|
|
if (bestDistance < Double.MAX_VALUE) {
|
|
|
|
|
return bestXY;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
boolean existsEmptyCell() {
|
|
|
|
|
return findCellForSpan(null, 1, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Finds the upper-left coordinate of the first rectangle in the grid that can
|
|
|
|
|
* hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
|
|
|
|
|
* then this method will only return coordinates for rectangles that contain the cell
|
|
|
|
|
* (intersectX, intersectY)
|
|
|
|
|
*
|
|
|
|
|
* @param cellXY The array that will contain the position of a vacant cell if such a cell
|
|
|
|
|
* can be found.
|
|
|
|
|
* @param spanX The horizontal span of the cell we want to find.
|
|
|
|
|
* @param spanY The vertical span of the cell we want to find.
|
|
|
|
|
*
|
|
|
|
|
* @return True if a vacant cell of the specified dimension was found, false otherwise.
|
|
|
|
|
*/
|
|
|
|
|
boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
|
|
|
|
|
return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Like above, but ignores any cells occupied by the item "ignoreView"
|
|
|
|
|
*
|
|
|
|
|
* @param cellXY The array that will contain the position of a vacant cell if such a cell
|
|
|
|
|
* can be found.
|
|
|
|
|
* @param spanX The horizontal span of the cell we want to find.
|
|
|
|
|
* @param spanY The vertical span of the cell we want to find.
|
|
|
|
|
* @param ignoreView The home screen item we should treat as not occupying any space
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
|
|
|
|
|
return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, ignoreView);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Like above, but if intersectX and intersectY are not -1, then this method will try to
|
|
|
|
|
* return coordinates for rectangles that contain the cell [intersectX, intersectY]
|
|
|
|
|
*
|
|
|
|
|
* @param spanX The horizontal span of the cell we want to find.
|
|
|
|
|
* @param spanY The vertical span of the cell we want to find.
|
|
|
|
|
* @param ignoreView The home screen item we should treat as not occupying any space
|
|
|
|
|
* @param intersectX The X coordinate of the cell that we should try to overlap
|
|
|
|
|
* @param intersectX The Y coordinate of the cell that we should try to overlap
|
|
|
|
|
*
|
|
|
|
|
* @return True if a vacant cell of the specified dimension was found, false otherwise.
|
|
|
|
|
*/
|
|
|
|
|
boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
|
|
|
|
|
int intersectX, int intersectY) {
|
|
|
|
|
return findCellForSpanThatIntersectsIgnoring(
|
|
|
|
|
cellXY, spanX, spanY, intersectX, intersectY, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The superset of the above two methods
|
|
|
|
|
*/
|
|
|
|
|
boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
|
|
|
|
|
int intersectX, int intersectY, View ignoreView) {
|
2010-09-30 12:04:50 -07:00
|
|
|
// mark space take by ignoreView as available (method checks if ignoreView is null)
|
|
|
|
|
markCellsAsUnoccupiedForView(ignoreView);
|
2010-09-17 15:00:07 -07:00
|
|
|
|
2010-09-24 17:43:49 -07:00
|
|
|
boolean foundCell = false;
|
2010-09-17 15:00:07 -07:00
|
|
|
while (true) {
|
|
|
|
|
int startX = 0;
|
|
|
|
|
if (intersectX >= 0) {
|
|
|
|
|
startX = Math.max(startX, intersectX - (spanX - 1));
|
|
|
|
|
}
|
|
|
|
|
int endX = mCountX - (spanX - 1);
|
|
|
|
|
if (intersectX >= 0) {
|
|
|
|
|
endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
|
|
|
|
|
}
|
|
|
|
|
int startY = 0;
|
|
|
|
|
if (intersectY >= 0) {
|
|
|
|
|
startY = Math.max(startY, intersectY - (spanY - 1));
|
|
|
|
|
}
|
|
|
|
|
int endY = mCountY - (spanY - 1);
|
|
|
|
|
if (intersectY >= 0) {
|
|
|
|
|
endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-11 16:34:41 -08:00
|
|
|
for (int y = startY; y < endY && !foundCell; y++) {
|
2010-09-17 15:00:07 -07:00
|
|
|
inner:
|
2010-11-11 16:34:41 -08:00
|
|
|
for (int x = startX; x < endX; x++) {
|
2010-09-17 15:00:07 -07:00
|
|
|
for (int i = 0; i < spanX; i++) {
|
|
|
|
|
for (int j = 0; j < spanY; j++) {
|
|
|
|
|
if (mOccupied[x + i][y + j]) {
|
2010-11-11 16:34:41 -08:00
|
|
|
// small optimization: we can skip to after the column we just found
|
2010-09-17 15:00:07 -07:00
|
|
|
// an occupied cell
|
2010-11-11 16:34:41 -08:00
|
|
|
x += i;
|
2010-09-17 15:00:07 -07:00
|
|
|
continue inner;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (cellXY != null) {
|
|
|
|
|
cellXY[0] = x;
|
|
|
|
|
cellXY[1] = y;
|
|
|
|
|
}
|
2010-09-24 17:43:49 -07:00
|
|
|
foundCell = true;
|
|
|
|
|
break;
|
2010-09-17 15:00:07 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (intersectX == -1 && intersectY == -1) {
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
// if we failed to find anything, try again but without any requirements of
|
|
|
|
|
// intersecting
|
|
|
|
|
intersectX = -1;
|
|
|
|
|
intersectY = -1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-30 12:04:50 -07:00
|
|
|
// re-mark space taken by ignoreView as occupied
|
|
|
|
|
markCellsAsOccupiedForView(ignoreView);
|
2010-09-24 17:43:49 -07:00
|
|
|
return foundCell;
|
2010-09-17 15:00:07 -07:00
|
|
|
}
|
|
|
|
|
|
2010-07-12 14:25:18 -07:00
|
|
|
/**
|
2010-09-17 15:00:07 -07:00
|
|
|
* Called when drag has left this CellLayout or has been completed (successfully or not)
|
2010-07-12 14:25:18 -07:00
|
|
|
*/
|
2010-09-17 15:00:07 -07:00
|
|
|
void onDragExit() {
|
2010-10-10 11:26:02 -07:00
|
|
|
// This can actually be called when we aren't in a drag, e.g. when adding a new
|
|
|
|
|
// item to this layout via the customize drawer.
|
|
|
|
|
// Guard against that case.
|
|
|
|
|
if (mDragging) {
|
|
|
|
|
mDragging = false;
|
2010-07-12 14:25:18 -07:00
|
|
|
|
2010-10-10 11:26:02 -07:00
|
|
|
// Fade out the drag indicators
|
|
|
|
|
if (mCrosshairsAnimator != null) {
|
|
|
|
|
mCrosshairsAnimator.animateOut();
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-10-14 23:54:22 -07:00
|
|
|
|
|
|
|
|
// Invalidate the drag data
|
|
|
|
|
mDragCell[0] = -1;
|
|
|
|
|
mDragCell[1] = -1;
|
|
|
|
|
mDragOutlineAnims[mDragOutlineCurrent].animateOut();
|
|
|
|
|
mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
|
|
|
|
|
|
|
|
|
|
setHover(false);
|
2010-07-12 14:25:18 -07:00
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
2010-06-11 17:34:16 -07:00
|
|
|
* Mark a child as having been dropped.
|
2010-09-27 11:15:43 -07:00
|
|
|
* At the beginning of the drag operation, the child may have been on another
|
2010-10-19 10:34:32 -07:00
|
|
|
* screen, but it is re-parented before this method is called.
|
2009-03-03 19:32:27 -08:00
|
|
|
*
|
|
|
|
|
* @param child The child that is being dropped
|
|
|
|
|
*/
|
2010-06-11 17:34:16 -07:00
|
|
|
void onDropChild(View child) {
|
2009-08-17 10:01:15 -07:00
|
|
|
if (child != null) {
|
|
|
|
|
LayoutParams lp = (LayoutParams) child.getLayoutParams();
|
|
|
|
|
lp.isDragging = false;
|
2009-11-04 15:00:44 -08:00
|
|
|
lp.dropped = true;
|
2010-10-19 10:34:32 -07:00
|
|
|
child.setVisibility(View.VISIBLE);
|
2009-08-17 10:01:15 -07:00
|
|
|
child.requestLayout();
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start dragging the specified child
|
2010-06-11 17:34:16 -07:00
|
|
|
*
|
2009-03-03 19:32:27 -08:00
|
|
|
* @param child The child that is being dragged
|
|
|
|
|
*/
|
|
|
|
|
void onDragChild(View child) {
|
|
|
|
|
LayoutParams lp = (LayoutParams) child.getLayoutParams();
|
|
|
|
|
lp.isDragging = true;
|
2010-10-19 10:34:32 -07:00
|
|
|
child.setVisibility(View.GONE);
|
2010-09-27 11:15:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A drag event has begun over this layout.
|
|
|
|
|
* It may have begun over this layout (in which case onDragChild is called first),
|
|
|
|
|
* or it may have begun on another layout.
|
|
|
|
|
*/
|
2010-10-27 17:18:37 -07:00
|
|
|
void onDragEnter() {
|
2010-10-13 17:32:10 -07:00
|
|
|
if (!mDragging) {
|
|
|
|
|
// Fade in the drag indicators
|
|
|
|
|
if (mCrosshairsAnimator != null) {
|
|
|
|
|
mCrosshairsAnimator.animateIn();
|
|
|
|
|
}
|
2010-10-10 11:26:02 -07:00
|
|
|
}
|
|
|
|
|
mDragging = true;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
|
|
|
|
* Computes a bounding rectangle for a range of cells
|
2010-06-11 17:34:16 -07:00
|
|
|
*
|
2009-03-03 19:32:27 -08:00
|
|
|
* @param cellX X coordinate of upper left corner expressed as a cell position
|
|
|
|
|
* @param cellY Y coordinate of upper left corner expressed as a cell position
|
2010-06-11 17:34:16 -07:00
|
|
|
* @param cellHSpan Width in cells
|
2009-03-03 19:32:27 -08:00
|
|
|
* @param cellVSpan Height in cells
|
2010-07-12 14:25:18 -07:00
|
|
|
* @param resultRect Rect into which to put the results
|
2009-03-03 19:32:27 -08:00
|
|
|
*/
|
2010-07-12 14:25:18 -07:00
|
|
|
public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF resultRect) {
|
2009-03-03 19:32:27 -08:00
|
|
|
final int cellWidth = mCellWidth;
|
|
|
|
|
final int cellHeight = mCellHeight;
|
|
|
|
|
final int widthGap = mWidthGap;
|
|
|
|
|
final int heightGap = mHeightGap;
|
2010-06-11 17:34:16 -07:00
|
|
|
|
|
|
|
|
final int hStartPadding = getLeftPadding();
|
|
|
|
|
final int vStartPadding = getTopPadding();
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
|
|
|
|
|
int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
|
|
|
|
|
|
|
|
|
|
int x = hStartPadding + cellX * (cellWidth + widthGap);
|
|
|
|
|
int y = vStartPadding + cellY * (cellHeight + heightGap);
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2010-07-12 14:25:18 -07:00
|
|
|
resultRect.set(x, y, x + width, y + height);
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
2010-06-11 17:34:16 -07:00
|
|
|
* Computes the required horizontal and vertical cell spans to always
|
2009-03-03 19:32:27 -08:00
|
|
|
* fit the given rectangle.
|
2010-06-11 17:34:16 -07:00
|
|
|
*
|
2009-03-03 19:32:27 -08:00
|
|
|
* @param width Width in pixels
|
|
|
|
|
* @param height Height in pixels
|
2010-07-16 13:55:32 -07:00
|
|
|
* @param result An array of length 2 in which to store the result (may be null).
|
2009-03-03 19:32:27 -08:00
|
|
|
*/
|
2010-07-16 13:55:32 -07:00
|
|
|
public int[] rectToCell(int width, int height, int[] result) {
|
2010-10-08 16:58:12 -07:00
|
|
|
return rectToCell(getResources(), width, height, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
|
2009-03-03 19:32:27 -08:00
|
|
|
// Always assume we're working with the smallest span to make sure we
|
|
|
|
|
// reserve enough space in both orientations.
|
2009-09-21 15:23:04 -04:00
|
|
|
int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
|
|
|
|
|
int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
|
2009-03-03 19:32:27 -08:00
|
|
|
int smallerSize = Math.min(actualWidth, actualHeight);
|
2009-09-21 15:23:04 -04:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
// Always round up to next largest cell
|
|
|
|
|
int spanX = (width + smallerSize) / smallerSize;
|
|
|
|
|
int spanY = (height + smallerSize) / smallerSize;
|
2009-09-21 15:23:04 -04:00
|
|
|
|
2010-07-16 13:55:32 -07:00
|
|
|
if (result == null) {
|
|
|
|
|
return new int[] { spanX, spanY };
|
|
|
|
|
}
|
|
|
|
|
result[0] = spanX;
|
|
|
|
|
result[1] = spanY;
|
|
|
|
|
return result;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find the first vacant cell, if there is one.
|
|
|
|
|
*
|
|
|
|
|
* @param vacant Holds the x and y coordinate of the vacant cell
|
|
|
|
|
* @param spanX Horizontal cell span.
|
|
|
|
|
* @param spanY Vertical cell span.
|
2010-06-11 17:34:16 -07:00
|
|
|
*
|
2009-03-03 19:32:27 -08:00
|
|
|
* @return True if a vacant cell was found
|
|
|
|
|
*/
|
|
|
|
|
public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
|
|
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static boolean findVacantCell(int[] vacant, int spanX, int spanY,
|
|
|
|
|
int xCount, int yCount, boolean[][] occupied) {
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x < xCount; x++) {
|
|
|
|
|
for (int y = 0; y < yCount; y++) {
|
|
|
|
|
boolean available = !occupied[x][y];
|
|
|
|
|
out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
|
|
|
|
|
for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
|
|
|
|
|
available = available && !occupied[i][j];
|
|
|
|
|
if (!available) break out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (available) {
|
|
|
|
|
vacant[0] = x;
|
|
|
|
|
vacant[1] = y;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
private void clearOccupiedCells() {
|
|
|
|
|
for (int x = 0; x < mCountX; x++) {
|
|
|
|
|
for (int y = 0; y < mCountY; y++) {
|
|
|
|
|
mOccupied[x][y] = false;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-17 15:00:07 -07:00
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
public void onMove(View view, int newCellX, int newCellY) {
|
|
|
|
|
LayoutParams lp = (LayoutParams) view.getLayoutParams();
|
|
|
|
|
markCellsAsUnoccupiedForView(view);
|
|
|
|
|
markCellsForView(newCellX, newCellY, lp.cellHSpan, lp.cellVSpan, true);
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
private void markCellsAsOccupiedForView(View view) {
|
2010-09-30 12:04:50 -07:00
|
|
|
if (view == null || view.getParent() != this) return;
|
2010-09-17 15:00:07 -07:00
|
|
|
LayoutParams lp = (LayoutParams) view.getLayoutParams();
|
|
|
|
|
markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void markCellsAsUnoccupiedForView(View view) {
|
2010-09-30 12:04:50 -07:00
|
|
|
if (view == null || view.getParent() != this) return;
|
2010-09-17 15:00:07 -07:00
|
|
|
LayoutParams lp = (LayoutParams) view.getLayoutParams();
|
|
|
|
|
markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean value) {
|
|
|
|
|
for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
|
|
|
|
|
for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
|
|
|
|
|
mOccupied[x][y] = value;
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
|
|
|
|
|
return new CellLayout.LayoutParams(getContext(), attrs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
|
|
|
|
|
return p instanceof CellLayout.LayoutParams;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
|
|
|
|
|
return new CellLayout.LayoutParams(p);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-11 17:34:16 -07:00
|
|
|
public static class CellLayoutAnimationController extends LayoutAnimationController {
|
|
|
|
|
public CellLayoutAnimationController(Animation animation, float delay) {
|
|
|
|
|
super(animation, delay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected long getDelayForView(View view) {
|
|
|
|
|
return (int) (Math.random() * 150);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
public static class LayoutParams extends ViewGroup.MarginLayoutParams {
|
|
|
|
|
/**
|
|
|
|
|
* Horizontal location of the item in the grid.
|
|
|
|
|
*/
|
|
|
|
|
@ViewDebug.ExportedProperty
|
|
|
|
|
public int cellX;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Vertical location of the item in the grid.
|
|
|
|
|
*/
|
|
|
|
|
@ViewDebug.ExportedProperty
|
|
|
|
|
public int cellY;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Number of cells spanned horizontally by the item.
|
|
|
|
|
*/
|
|
|
|
|
@ViewDebug.ExportedProperty
|
|
|
|
|
public int cellHSpan;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Number of cells spanned vertically by the item.
|
|
|
|
|
*/
|
|
|
|
|
@ViewDebug.ExportedProperty
|
|
|
|
|
public int cellVSpan;
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
|
|
|
|
* Is this item currently being dragged
|
|
|
|
|
*/
|
|
|
|
|
public boolean isDragging;
|
|
|
|
|
|
|
|
|
|
// X coordinate of the view in the layout.
|
|
|
|
|
@ViewDebug.ExportedProperty
|
|
|
|
|
int x;
|
|
|
|
|
// Y coordinate of the view in the layout.
|
|
|
|
|
@ViewDebug.ExportedProperty
|
|
|
|
|
int y;
|
|
|
|
|
|
2010-10-19 10:34:32 -07:00
|
|
|
/**
|
|
|
|
|
* The old X coordinate of this item, relative to its current parent.
|
|
|
|
|
* Used to animate the item into its new position.
|
|
|
|
|
*/
|
|
|
|
|
int oldX;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The old Y coordinate of this item, relative to its current parent.
|
|
|
|
|
* Used to animate the item into its new position.
|
|
|
|
|
*/
|
|
|
|
|
int oldY;
|
|
|
|
|
|
2009-11-04 15:00:44 -08:00
|
|
|
boolean dropped;
|
2009-10-02 16:06:52 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
public LayoutParams(Context c, AttributeSet attrs) {
|
|
|
|
|
super(c, attrs);
|
|
|
|
|
cellHSpan = 1;
|
|
|
|
|
cellVSpan = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LayoutParams(ViewGroup.LayoutParams source) {
|
|
|
|
|
super(source);
|
|
|
|
|
cellHSpan = 1;
|
|
|
|
|
cellVSpan = 1;
|
|
|
|
|
}
|
2010-06-11 17:34:16 -07:00
|
|
|
|
|
|
|
|
public LayoutParams(LayoutParams source) {
|
|
|
|
|
super(source);
|
|
|
|
|
this.cellX = source.cellX;
|
|
|
|
|
this.cellY = source.cellY;
|
|
|
|
|
this.cellHSpan = source.cellHSpan;
|
|
|
|
|
this.cellVSpan = source.cellVSpan;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
|
2010-01-08 15:07:00 -08:00
|
|
|
super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
|
2009-03-03 19:32:27 -08:00
|
|
|
this.cellX = cellX;
|
|
|
|
|
this.cellY = cellY;
|
|
|
|
|
this.cellHSpan = cellHSpan;
|
|
|
|
|
this.cellVSpan = cellVSpan;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
|
|
|
|
|
int hStartPadding, int vStartPadding) {
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
final int myCellHSpan = cellHSpan;
|
|
|
|
|
final int myCellVSpan = cellVSpan;
|
|
|
|
|
final int myCellX = cellX;
|
|
|
|
|
final int myCellY = cellY;
|
2010-06-11 17:34:16 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
|
|
|
|
|
leftMargin - rightMargin;
|
|
|
|
|
height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
|
|
|
|
|
topMargin - bottomMargin;
|
|
|
|
|
|
|
|
|
|
x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin;
|
|
|
|
|
y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin;
|
|
|
|
|
}
|
2010-06-11 17:34:16 -07:00
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
|
return "(" + this.cellX + ", " + this.cellY + ")";
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
// This class stores info for two purposes:
|
|
|
|
|
// 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
|
|
|
|
|
// its spanX, spanY, and the screen it is on
|
|
|
|
|
// 2. When long clicking on an empty cell in a CellLayout, we save information about the
|
|
|
|
|
// cellX and cellY coordinates and which page was clicked. We then set this as a tag on
|
|
|
|
|
// the CellLayout that was long clicked
|
2009-03-03 19:32:27 -08:00
|
|
|
static final class CellInfo implements ContextMenu.ContextMenuInfo {
|
|
|
|
|
View cell;
|
2010-08-19 13:52:27 -07:00
|
|
|
int cellX = -1;
|
|
|
|
|
int cellY = -1;
|
2009-03-03 19:32:27 -08:00
|
|
|
int spanX;
|
|
|
|
|
int spanY;
|
|
|
|
|
int screen;
|
|
|
|
|
boolean valid;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
2010-06-11 17:34:16 -07:00
|
|
|
return "Cell[view=" + (cell == null ? "null" : cell.getClass())
|
|
|
|
|
+ ", x=" + cellX + ", y=" + cellY + "]";
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|