blob: d30940e7d553e636efee540535622b38dd028e1e [file] [log] [blame]
/*
* 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.
*/
package com.android.launcher2;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LayoutAnimationController;
import com.android.launcher.R;
import com.android.launcher2.FolderIcon.FolderRingAnimator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Stack;
public class CellLayout extends ViewGroup {
static final String TAG = "CellLayout";
private int mOriginalCellWidth;
private int mOriginalCellHeight;
private int mCellWidth;
private int mCellHeight;
private int mCountX;
private int mCountY;
private int mOriginalWidthGap;
private int mOriginalHeightGap;
private int mWidthGap;
private int mHeightGap;
private int mMaxGap;
private boolean mScrollingTransformsDirty = false;
private final Rect mRect = new Rect();
private final CellInfo mCellInfo = new CellInfo();
// 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.
private final int[] mTmpXY = new int[2];
private final int[] mTmpPoint = new int[2];
private final PointF mTmpPointF = new PointF();
int[] mTempLocation = new int[2];
boolean[][] mOccupied;
boolean[][] mTmpOccupied;
private boolean mLastDownOnOccupiedCell = false;
private OnTouchListener mInterceptTouchListener;
private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
private int[] mFolderLeaveBehindCell = {-1, -1};
private int mForegroundAlpha = 0;
private float mBackgroundAlpha;
private float mBackgroundAlphaMultiplier = 1.0f;
private Drawable mNormalBackground;
private Drawable mActiveGlowBackground;
private Drawable mOverScrollForegroundDrawable;
private Drawable mOverScrollLeft;
private Drawable mOverScrollRight;
private Rect mBackgroundRect;
private Rect mForegroundRect;
private int mForegroundPadding;
// If we're actively dragging something over this screen, mIsDragOverlapping is true
private boolean mIsDragOverlapping = false;
private final Point mDragCenter = new Point();
// These arrays are used to implement the drag visualization on x-large screens.
// They are used as circular arrays, indexed by mDragOutlineCurrent.
private Rect[] mDragOutlines = new Rect[4];
private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
private InterruptibleInOutAnimator[] mDragOutlineAnims =
new InterruptibleInOutAnimator[mDragOutlines.length];
// Used as an index into the above 3 arrays; indicates which is the most current value.
private int mDragOutlineCurrent = 0;
private final Paint mDragOutlinePaint = new Paint();
private BubbleTextView mPressedOrFocusedIcon;
private Drawable mCrosshairsDrawable = null;
private InterruptibleInOutAnimator mCrosshairsAnimator = null;
private float mCrosshairsVisibility = 0.0f;
private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
HashMap<CellLayout.LayoutParams, Animator>();
// When a drag operation is in progress, holds the nearest cell to the touch point
private final int[] mDragCell = new int[2];
private boolean mDragging = false;
private boolean mItemLocationsDirty = false;
private TimeInterpolator mEaseOutInterpolator;
private CellLayoutChildren mChildren;
private boolean mIsHotseat = false;
private float mChildScale = 1f;
private float mHotseatChildScale = 1f;
public static final int MODE_DRAG_OVER = 0;
public static final int MODE_ON_DROP = 1;
public static final int MODE_ON_DROP_EXTERNAL = 2;
public static final int MODE_ACCEPT_DROP = 3;
private static final boolean DESTRUCTIVE_REORDER = true;
private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
private ArrayList<View> mIntersectingViews = new ArrayList<View>();
private Rect mOccupiedRect = new Rect();
private int[] mDirectionVector = new int[2];
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);
// 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);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
mOriginalCellWidth =
mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
mOriginalCellHeight =
mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
mCountX = LauncherModel.getCellCountX();
mCountY = LauncherModel.getCellCountY();
mOccupied = new boolean[mCountX][mCountY];
mTmpOccupied = new boolean[mCountX][mCountY];
a.recycle();
setAlwaysDrawnWithCacheEnabled(false);
final Resources res = getResources();
mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
mForegroundPadding =
res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
mNormalBackground.setFilterBitmap(true);
mActiveGlowBackground.setFilterBitmap(true);
int iconScale = res.getInteger(R.integer.app_icon_scale_percent);
if (iconScale >= 0) {
mChildScale = iconScale / 100f;
}
int hotseatIconScale = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
if (hotseatIconScale >= 0) {
mHotseatChildScale = hotseatIconScale / 100f;
}
// Initialize the data structures used for the drag visualization.
mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
// Set up the animation for fading the crosshairs in and out
int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
invalidate();
}
});
mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
mDragCell[0] = mDragCell[1] = -1;
for (int i = 0; i < mDragOutlines.length; i++) {
mDragOutlines[i] = new Rect(-1, -1, -1, -1);
}
// 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);
final float fromAlphaValue = 0;
final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
for (int i = 0; i < mDragOutlineAnims.length; i++) {
final InterruptibleInOutAnimator anim =
new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
anim.getAnimator().setInterpolator(mEaseOutInterpolator);
final int thisIndex = i;
anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
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) {
if (false) {
Object val = animation.getAnimatedValue();
Log.d(TAG, "anim " + thisIndex + " update: " + val +
", isStopped " + anim.isStopped());
}
// Try to prevent it from continuing to run
animation.cancel();
} else {
mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
CellLayout.this.invalidate(mDragOutlines[thisIndex]);
}
}
});
// 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.
anim.getAnimator().addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
anim.setTag(null);
}
}
});
mDragOutlineAnims[i] = anim;
}
mBackgroundRect = new Rect();
mForegroundRect = new Rect();
mChildren = new CellLayoutChildren(context);
mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
addView(mChildren);
}
static int widthInPortrait(Resources r, int numCells) {
// We use this method from Workspace to figure out how many rows/columns Launcher should
// have. We ignore the left/right padding on CellLayout because it turns out in our design
// the padding extends outside the visible screen size, but it looked fine anyway.
int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
r.getDimensionPixelSize(R.dimen.workspace_height_gap));
return minGap * (numCells - 1) + cellWidth * numCells;
}
static int heightInLandscape(Resources r, int numCells) {
// We use this method from Workspace to figure out how many rows/columns Launcher should
// have. We ignore the left/right padding on CellLayout because it turns out in our design
// the padding extends outside the visible screen size, but it looked fine anyway.
int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
r.getDimensionPixelSize(R.dimen.workspace_height_gap));
return minGap * (numCells - 1) + cellHeight * numCells;
}
public void enableHardwareLayers() {
mChildren.enableHardwareLayers();
}
public void setGridSize(int x, int y) {
mCountX = x;
mCountY = y;
mOccupied = new boolean[mCountX][mCountY];
mTmpOccupied = new boolean[mCountX][mCountY];
requestLayout();
}
private void invalidateBubbleTextView(BubbleTextView icon) {
final int padding = icon.getPressedOrFocusedBackgroundPadding();
invalidate(icon.getLeft() + getPaddingLeft() - padding,
icon.getTop() + getPaddingTop() - padding,
icon.getRight() + getPaddingLeft() + padding,
icon.getBottom() + getPaddingTop() + padding);
}
void setOverScrollAmount(float r, boolean left) {
if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
mOverScrollForegroundDrawable = mOverScrollLeft;
} else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
mOverScrollForegroundDrawable = mOverScrollRight;
}
mForegroundAlpha = (int) Math.round((r * 255));
mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
invalidate();
}
void setPressedOrFocusedIcon(BubbleTextView icon) {
// We draw the pressed or focused BubbleTextView's background in CellLayout because it
// requires an expanded clip rect (due to the glow's blur radius)
BubbleTextView oldIcon = mPressedOrFocusedIcon;
mPressedOrFocusedIcon = icon;
if (oldIcon != null) {
invalidateBubbleTextView(oldIcon);
}
if (mPressedOrFocusedIcon != null) {
invalidateBubbleTextView(mPressedOrFocusedIcon);
}
}
public CellLayoutChildren getChildrenLayout() {
if (getChildCount() > 0) {
return (CellLayoutChildren) getChildAt(0);
}
return null;
}
void setIsDragOverlapping(boolean isDragOverlapping) {
if (mIsDragOverlapping != isDragOverlapping) {
mIsDragOverlapping = isDragOverlapping;
invalidate();
}
}
boolean getIsDragOverlapping() {
return mIsDragOverlapping;
}
protected void setOverscrollTransformsDirty(boolean dirty) {
mScrollingTransformsDirty = dirty;
}
protected void resetOverscrollTransforms() {
if (mScrollingTransformsDirty) {
setOverscrollTransformsDirty(false);
setTranslationX(0);
setRotationY(0);
// It doesn't matter if we pass true or false here, the important thing is that we
// pass 0, which results in the overscroll drawable not being drawn any more.
setOverScrollAmount(0, false);
setPivotX(getMeasuredWidth() / 2);
setPivotY(getMeasuredHeight() / 2);
}
}
@Override
protected void onDraw(Canvas canvas) {
// 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
if (mBackgroundAlpha > 0.0f) {
Drawable bg;
if (mIsDragOverlapping) {
// In the mini case, we draw the active_glow bg *over* the active background
bg = mActiveGlowBackground;
} else {
bg = mNormalBackground;
}
bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
bg.setBounds(mBackgroundRect);
bg.draw(canvas);
}
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 = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
for (int col = 0; col <= countX; col++) {
int y = getPaddingTop() - (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;
}
}
final Paint paint = mDragOutlinePaint;
for (int i = 0; i < mDragOutlines.length; i++) {
final float alpha = mDragOutlineAlphas[i];
if (alpha > 0) {
final Rect r = mDragOutlines[i];
final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
paint.setAlpha((int)(alpha + .5f));
canvas.drawBitmap(b, null, r, paint);
}
}
// We draw the pressed or focused BubbleTextView's background in CellLayout because it
// requires an expanded clip rect (due to the glow's blur radius)
if (mPressedOrFocusedIcon != null) {
final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
if (b != null) {
canvas.drawBitmap(b,
mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
null);
}
}
if (DEBUG_VISUALIZE_OCCUPIED) {
int[] pt = new int[2];
ColorDrawable cd = new ColorDrawable(Color.RED);
cd.setBounds(0, 0, 80, 80);
for (int i = 0; i < mCountX; i++) {
for (int j = 0; j < mCountY; j++) {
if (mOccupied[i][j]) {
cellToPoint(i, j, pt);
canvas.save();
canvas.translate(pt[0], pt[1]);
cd.draw(canvas);
canvas.restore();
}
}
}
}
// The folder outer / inner ring image(s)
for (int i = 0; i < mFolderOuterRings.size(); i++) {
FolderRingAnimator fra = mFolderOuterRings.get(i);
// Draw outer ring
Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
int width = (int) fra.getOuterRingSize();
int height = width;
cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
int centerX = mTempLocation[0] + mCellWidth / 2;
int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
canvas.save();
canvas.translate(centerX - width / 2, centerY - height / 2);
d.setBounds(0, 0, width, height);
d.draw(canvas);
canvas.restore();
// Draw inner ring
d = FolderRingAnimator.sSharedInnerRingDrawable;
width = (int) fra.getInnerRingSize();
height = width;
cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
centerX = mTempLocation[0] + mCellWidth / 2;
centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
canvas.save();
canvas.translate(centerX - width / 2, centerY - width / 2);
d.setBounds(0, 0, width, height);
d.draw(canvas);
canvas.restore();
}
if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
Drawable d = FolderIcon.sSharedFolderLeaveBehind;
int width = d.getIntrinsicWidth();
int height = d.getIntrinsicHeight();
cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
int centerX = mTempLocation[0] + mCellWidth / 2;
int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
canvas.save();
canvas.translate(centerX - width / 2, centerY - width / 2);
d.setBounds(0, 0, width, height);
d.draw(canvas);
canvas.restore();
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (mForegroundAlpha > 0) {
mOverScrollForegroundDrawable.setBounds(mForegroundRect);
Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
mOverScrollForegroundDrawable.draw(canvas);
p.setXfermode(null);
}
}
public void showFolderAccept(FolderRingAnimator fra) {
mFolderOuterRings.add(fra);
}
public void hideFolderAccept(FolderRingAnimator fra) {
if (mFolderOuterRings.contains(fra)) {
mFolderOuterRings.remove(fra);
}
invalidate();
}
public void setFolderLeaveBehindCell(int x, int y) {
mFolderLeaveBehindCell[0] = x;
mFolderLeaveBehindCell[1] = y;
invalidate();
}
public void clearFolderLeaveBehind() {
mFolderLeaveBehindCell[0] = -1;
mFolderLeaveBehindCell[1] = -1;
invalidate();
}
@Override
public boolean shouldDelayChildPressedState() {
return false;
}
@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();
}
}
public void setOnInterceptTouchListener(View.OnTouchListener listener) {
mInterceptTouchListener = listener;
}
int getCountX() {
return mCountX;
}
int getCountY() {
return mCountY;
}
public void setIsHotseat(boolean isHotseat) {
mIsHotseat = isHotseat;
}
public float getChildrenScale() {
return mIsHotseat ? mHotseatChildScale : mChildScale;
}
public boolean addViewToCellLayout(
View child, int index, int childId, LayoutParams params, boolean markCells) {
return addViewToCellLayout(child, index, childId, params, markCells, false);
}
private void scaleChild(BubbleTextView bubbleChild, float pivot, float scale) {
// If we haven't measured the child yet, do it now
// (this happens if we're being dropped from all-apps
if (bubbleChild.getLayoutParams() instanceof LayoutParams &&
(bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
getChildrenLayout().measureChild(bubbleChild);
}
int measuredWidth = bubbleChild.getMeasuredWidth();
int measuredHeight = bubbleChild.getMeasuredHeight();
bubbleChild.setScaleX(scale);
bubbleChild.setScaleY(scale);
}
private void resetChild(BubbleTextView bubbleChild) {
bubbleChild.setScaleX(1f);
bubbleChild.setScaleY(1f);
bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
}
public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
boolean markCells, boolean allApps) {
final LayoutParams lp = params;
// Hotseat icons - scale down and remove text
// Don't scale the all apps button
// scale percent set to -1 means do not scale
// Only scale BubbleTextViews
if (child instanceof BubbleTextView) {
BubbleTextView bubbleChild = (BubbleTextView) child;
// Start the child with 100% scale and visible text
resetChild(bubbleChild);
if (mIsHotseat && !allApps && mHotseatChildScale >= 0) {
// Scale/make transparent for a hotseat
scaleChild(bubbleChild, 0f, mHotseatChildScale);
bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent));
} else if (mChildScale >= 0) {
// Else possibly still scale it if we need to for smaller icons
scaleChild(bubbleChild, 0f, mChildScale);
}
}
// Generate an id for each view, this assumes we have at most 256x256 cells
// per workspace screen
if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
// If the horizontal or vertical span is set to -1, it is taken to
// mean that it spans the extent of the CellLayout
if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
child.setId(childId);
mChildren.addView(child, index, lp);
if (markCells) markCellsAsOccupiedForView(child);
return true;
}
return false;
}
@Override
public void removeAllViews() {
clearOccupiedCells();
mChildren.removeAllViews();
}
@Override
public void removeAllViewsInLayout() {
if (mChildren.getChildCount() > 0) {
clearOccupiedCells();
mChildren.removeAllViewsInLayout();
}
}
public void removeViewWithoutMarkingCells(View view) {
mChildren.removeView(view);
}
@Override
public void removeView(View view) {
markCellsAsUnoccupiedForView(view);
mChildren.removeView(view);
}
@Override
public void removeViewAt(int index) {
markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
mChildren.removeViewAt(index);
}
@Override
public void removeViewInLayout(View view) {
markCellsAsUnoccupiedForView(view);
mChildren.removeViewInLayout(view);
}
@Override
public void removeViews(int start, int count) {
for (int i = start; i < start + count; i++) {
markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
}
mChildren.removeViews(start, count);
}
@Override
public void removeViewsInLayout(int start, int count) {
for (int i = start; i < start + count; i++) {
markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
}
mChildren.removeViewsInLayout(start, count);
}
public void drawChildren(Canvas canvas) {
mChildren.draw(canvas);
}
void buildChildrenLayer() {
mChildren.buildLayer();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
}
public void setTagToCellInfoForPoint(int touchX, int touchY) {
final CellInfo cellInfo = mCellInfo;
Rect frame = mRect;
final int x = touchX + mScrollX;
final int y = touchY + mScrollY;
final int count = mChildren.getChildCount();
boolean found = false;
for (int i = count - 1; i >= 0; i--) {
final View child = mChildren.getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
lp.isLockedToGrid) {
child.getHitRect(frame);
float scale = child.getScaleX();
frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
child.getBottom());
// The child hit rect is relative to the CellLayoutChildren parent, so we need to
// offset that by this CellLayout's padding to test an (x,y) point that is relative
// to this view.
frame.offset(mPaddingLeft, mPaddingTop);
frame.inset((int) (frame.width() * (1f - scale) / 2),
(int) (frame.height() * (1f - scale) / 2));
if (frame.contains(x, y)) {
cellInfo.cell = child;
cellInfo.cellX = lp.cellX;
cellInfo.cellY = lp.cellY;
cellInfo.spanX = lp.cellHSpan;
cellInfo.spanY = lp.cellVSpan;
found = true;
break;
}
}
}
mLastDownOnOccupiedCell = found;
if (!found) {
final int cellXY[] = mTmpXY;
pointToCellExact(x, y, cellXY);
cellInfo.cell = null;
cellInfo.cellX = cellXY[0];
cellInfo.cellY = cellXY[1];
cellInfo.spanX = 1;
cellInfo.spanY = 1;
}
setTag(cellInfo);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// First we clear the tag to ensure that on every touch down we start with a fresh slate,
// even in the case where we return early. Not clearing here was causing bugs whereby on
// long-press we'd end up picking up an item from a previous drag operation.
final int action = ev.getAction();
if (action == MotionEvent.ACTION_DOWN) {
clearTagCellInfo();
}
if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
return true;
}
if (action == MotionEvent.ACTION_DOWN) {
setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
}
return false;
}
private void clearTagCellInfo() {
final CellInfo cellInfo = mCellInfo;
cellInfo.cell = null;
cellInfo.cellX = -1;
cellInfo.cellY = -1;
cellInfo.spanX = 0;
cellInfo.spanY = 0;
setTag(cellInfo);
}
public CellInfo getTag() {
return (CellInfo) super.getTag();
}
/**
* Given a point, return the cell that strictly 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 pointToCellExact(int x, int y, int[] result) {
final int hStartPadding = getPaddingLeft();
final int vStartPadding = getPaddingTop();
result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
final int xAxis = mCountX;
final int yAxis = mCountY;
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;
}
/**
* 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
*
* @param cellX X coordinate of the cell
* @param cellY Y coordinate of the cell
*
* @param result Array of 2 ints to hold the x and y coordinate of the point
*/
void cellToPoint(int cellX, int cellY, int[] result) {
final int hStartPadding = getPaddingLeft();
final int vStartPadding = getPaddingTop();
result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
}
/**
* Given a cell coordinate, return the point that represents the center of the cell
*
* @param cellX X coordinate of the cell
* @param cellY Y coordinate of the cell
*
* @param result Array of 2 ints to hold the x and y coordinate of the point
*/
void cellToCenterPoint(int cellX, int cellY, int[] result) {
final int hStartPadding = getPaddingLeft();
final int vStartPadding = getPaddingTop();
result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
}
public float getDistanceFromCell(float x, float y, int[] cell) {
cellToCenterPoint(cell[0], cell[1], mTmpPoint);
float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
Math.pow(y - mTmpPoint[1], 2));
return distance;
}
int getCellWidth() {
return mCellWidth;
}
int getCellHeight() {
return mCellHeight;
}
int getWidthGap() {
return mWidthGap;
}
int getHeightGap() {
return mHeightGap;
}
Rect getContentRect(Rect r) {
if (r == null) {
r = new Rect();
}
int left = getPaddingLeft();
int top = getPaddingTop();
int right = left + getWidth() - mPaddingLeft - mPaddingRight;
int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
r.set(left, top, right, bottom);
return r;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO: currently ignoring padding
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
}
int numWidthGaps = mCountX - 1;
int numHeightGaps = mCountY - 1;
if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
} else {
mWidthGap = mOriginalWidthGap;
mHeightGap = mOriginalHeightGap;
}
// Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
int newWidth = widthSpecSize;
int newHeight = heightSpecSize;
if (widthSpecMode == MeasureSpec.AT_MOST) {
newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
((mCountX - 1) * mWidthGap);
newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
((mCountY - 1) * mHeightGap);
setMeasuredDimension(newWidth, newHeight);
}
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
mPaddingRight, MeasureSpec.EXACTLY);
int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
mPaddingBottom, MeasureSpec.EXACTLY);
child.measure(childWidthMeasureSpec, childheightMeasureSpec);
}
setMeasuredDimension(newWidth, newHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
child.layout(mPaddingLeft, mPaddingTop,
r - l - mPaddingRight, b - t - mPaddingBottom);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBackgroundRect.set(0, 0, w, h);
mForegroundRect.set(mForegroundPadding, mForegroundPadding,
w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
}
@Override
protected void setChildrenDrawingCacheEnabled(boolean enabled) {
mChildren.setChildrenDrawingCacheEnabled(enabled);
}
@Override
protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
mChildren.setChildrenDrawnWithCacheEnabled(enabled);
}
public float getBackgroundAlpha() {
return mBackgroundAlpha;
}
public void setBackgroundAlphaMultiplier(float multiplier) {
mBackgroundAlphaMultiplier = multiplier;
}
public float getBackgroundAlphaMultiplier() {
return mBackgroundAlphaMultiplier;
}
public void setBackgroundAlpha(float alpha) {
if (mBackgroundAlpha != alpha) {
mBackgroundAlpha = alpha;
invalidate();
}
}
// 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;
}
@Override
public void setAlpha(float alpha) {
setChildrenAlpha(alpha);
super.setAlpha(alpha);
}
private void setChildrenAlpha(float alpha) {
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
getChildAt(i).setAlpha(alpha);
}
}
public View getChildAt(int x, int y) {
return mChildren.getChildAt(x, y);
}
public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
int delay, boolean permanent, boolean adjustOccupied) {
CellLayoutChildren clc = getChildrenLayout();
boolean[][] occupied = mOccupied;
if (!permanent) {
occupied = mTmpOccupied;
}
if (clc.indexOfChild(child) != -1 && !occupied[cellX][cellY]) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final ItemInfo info = (ItemInfo) child.getTag();
// We cancel any existing animations
if (mReorderAnimators.containsKey(lp)) {
mReorderAnimators.get(lp).cancel();
mReorderAnimators.remove(lp);
}
final int oldX = lp.x;
final int oldY = lp.y;
if (adjustOccupied) {
occupied[lp.cellX][lp.cellY] = false;
occupied[cellX][cellY] = true;
}
lp.isLockedToGrid = true;
if (permanent) {
lp.cellX = info.cellX = cellX;
lp.cellY = info.cellY = cellY;
} else {
lp.tmpCellX = cellX;
lp.tmpCellY = cellY;
}
clc.setupLp(lp);
lp.isLockedToGrid = false;
final int newX = lp.x;
final int newY = lp.y;
lp.x = oldX;
lp.y = oldY;
// Exit early if we're not actually moving the view
if (oldX == newX && oldY == newY) {
lp.isLockedToGrid = true;
return true;
}
ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
va.setDuration(duration);
mReorderAnimators.put(lp, va);
va.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float r = ((Float) animation.getAnimatedValue()).floatValue();
child.setTranslationX(r * (newX - oldX));
child.setTranslationY(r * (newY - oldY));
}
});
va.addListener(new AnimatorListenerAdapter() {
boolean cancelled = false;
public void onAnimationEnd(Animator animation) {
// If the animation was cancelled, it means that another animation
// has interrupted this one, and we don't want to lock the item into
// place just yet.
if (!cancelled) {
child.setTranslationX(0);
child.setTranslationY(0);
lp.isLockedToGrid = true;
child.requestLayout();
}
if (mReorderAnimators.containsKey(lp)) {
mReorderAnimators.remove(lp);
}
}
public void onAnimationCancel(Animator animation) {
cancelled = true;
}
});
va.setStartDelay(delay);
va.start();
return true;
}
return false;
}
/**
* 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) {
final int countX = mCountX;
final int countY = mCountY;
// 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);
// 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
}
void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
final int oldDragCellX = mDragCell[0];
final int oldDragCellY = mDragCell[1];
if (v != null && dragOffset == null) {
mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
} else {
mDragCenter.set(originX, originY);
}
if (dragOutline == null && v == null) {
if (mCrosshairsDrawable != null) {
invalidate();
}
return;
}
if (cellX != oldDragCellX || cellY != oldDragCellY) {
mDragCell[0] = cellX;
mDragCell[1] = cellY;
// Find the top left corner of the rect the object will occupy
final int[] topLeft = mTmpPoint;
cellToPoint(cellX, cellY, topLeft);
int left = topLeft[0];
int top = topLeft[1];
if (v != null && dragOffset == null) {
// When drawing the drag outline, it did not account for margin offsets
// added by the view's parent.
MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
left += lp.leftMargin;
top += lp.topMargin;
// Offsets due to the size difference between the View and the dragOutline.
// There is a size difference to account for the outer blur, which may lie
// outside the bounds of the view.
top += (v.getHeight() - dragOutline.getHeight()) / 2;
// We center about the x axis
left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
- dragOutline.getWidth()) / 2;
} else {
if (dragOffset != null && dragRegion != null) {
// Center the drag region *horizontally* in the cell and apply a drag
// outline offset
left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
- dragRegion.width()) / 2;
top += dragOffset.y;
} else {
// Center the drag outline in the cell
left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
- dragOutline.getWidth()) / 2;
top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
- dragOutline.getHeight()) / 2;
}
}
final int oldIndex = mDragOutlineCurrent;
mDragOutlineAnims[oldIndex].animateOut();
mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Rect r = mDragOutlines[mDragOutlineCurrent];
r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
if (resize) {
cellToRect(cellX, cellY, spanX, spanY, r);
}
mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
mDragOutlineAnims[mDragOutlineCurrent].animateIn();
}
// If we are drawing crosshairs, the entire CellLayout needs to be invalidated
if (mCrosshairsDrawable != null) {
invalidate();
}
}
public void clearDragOutlines() {
final int oldIndex = mDragOutlineCurrent;
mDragOutlineAnims[oldIndex].animateOut();
mDragCell[0] = mDragCell[1] = -1;
}
/**
* 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 result Array in which to place the result, or null (in which case a new array will
* be allocated)
* @return The X, Y cell of a vacant area that can contain this object,
* nearest the requested location.
*/
int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
int[] result) {
return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
}
/**
* 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 minSpanX The minimum horizontal span required
* @param minSpanY The minimum vertical span required
* @param spanX Horizontal span of the object.
* @param spanY Vertical span of the object.
* @param result Array in which to place the result, or null (in which case a new array will
* be allocated)
* @return The X, Y cell of a vacant area that can contain this object,
* nearest the requested location.
*/
int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
int spanY, int[] result, int[] resultSpan) {
return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
result, resultSpan);
}
/**
* 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 ignoreOccupied If true, the result can be an occupied cell
* @param result Array in which to place the result, or null (in which case a new array will
* be allocated)
* @return The X, Y cell of a vacant area that can contain this object,
* nearest the requested location.
*/
int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
boolean ignoreOccupied, int[] result) {
return findNearestArea(pixelX, pixelY, spanX, spanY,
spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
}
private final Stack<Rect> mTempRectStack = new Stack<Rect>();
private void lazyInitTempRectStack() {
if (mTempRectStack.isEmpty()) {
for (int i = 0; i < mCountX * mCountY; i++) {
mTempRectStack.push(new Rect());
}
}
}
private void recycleTempRects(Stack<Rect> used) {
while (!used.isEmpty()) {
mTempRectStack.push(used.pop());
}
}
/**
* 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 minSpanX The minimum horizontal span required
* @param minSpanY The minimum vertical span required
* @param spanX Horizontal span of the object.
* @param spanY Vertical span of the object.
* @param ignoreOccupied If true, the result can be an occupied cell
* @param result Array in which to place the result, or null (in which case a new array will
* be allocated)
* @return The X, Y cell of a vacant area that can contain this object,
* nearest the requested location.
*/
int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
boolean[][] occupied) {
lazyInitTempRectStack();
// mark space take by ignoreView as available (method checks if ignoreView is null)
markCellsAsUnoccupiedForView(ignoreView, occupied);
// For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
// to the center of the item, but we are searching based on the top-left cell, so
// we translate the point over to correspond to the top-left.
pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
// Keep track of best-scoring drop area
final int[] bestXY = result != null ? result : new int[2];
double bestDistance = Double.MAX_VALUE;
final Rect bestRect = new Rect(-1, -1, -1, -1);
final Stack<Rect> validRegions = new Stack<Rect>();
final int countX = mCountX;
final int countY = mCountY;
if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
spanX < minSpanX || spanY < minSpanY) {
return bestXY;
}
for (int y = 0; y < countY - (minSpanY - 1); y++) {
inner:
for (int x = 0; x < countX - (minSpanX - 1); x++) {
int ySize = -1;
int xSize = -1;
if (ignoreOccupied) {
// First, let's see if this thing fits anywhere
for (int i = 0; i < minSpanX; i++) {
for (int j = 0; j < minSpanY; j++) {
if (occupied[x + i][y + j]) {
continue inner;
}
}
}
xSize = minSpanX;
ySize = minSpanY;
// We know that the item will fit at _some_ acceptable size, now let's see
// how big we can make it. We'll alternate between incrementing x and y spans
// until we hit a limit.
boolean incX = true;
boolean hitMaxX = xSize >= spanX;
boolean hitMaxY = ySize >= spanY;
while (!(hitMaxX && hitMaxY)) {
if (incX && !hitMaxX) {
for (int j = 0; j < ySize; j++) {
if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
// We can't move out horizontally
hitMaxX = true;
}
}
if (!hitMaxX) {
xSize++;
}
} else if (!hitMaxY) {
for (int i = 0; i < xSize; i++) {
if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
// We can't move out vertically
hitMaxY = true;
}
}
if (!hitMaxY) {
ySize++;
}
}
hitMaxX |= xSize >= spanX;
hitMaxY |= ySize >= spanY;
incX = !incX;
}
incX = true;
hitMaxX = xSize >= spanX;
hitMaxY = ySize >= spanY;
}
final int[] cellXY = mTmpXY;
cellToCenterPoint(x, y, cellXY);
// We verify that the current rect is not a sub-rect of any of our previous
// candidates. In this case, the current rect is disqualified in favour of the
// containing rect.
Rect currentRect = mTempRectStack.pop();
currentRect.set(x, y, x + xSize, y + ySize);
boolean contained = false;
for (Rect r : validRegions) {
if (r.contains(currentRect)) {
contained = true;
break;
}
}
validRegions.push(currentRect);
double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
+ Math.pow(cellXY[1] - pixelY, 2));
if ((distance <= bestDistance && !contained) ||
currentRect.contains(bestRect)) {
bestDistance = distance;
bestXY[0] = x;
bestXY[1] = y;
if (resultSpan != null) {
resultSpan[0] = xSize;
resultSpan[1] = ySize;
}
bestRect.set(currentRect);
}
}
}
// re-mark space taken by ignoreView as occupied
markCellsAsOccupiedForView(ignoreView, occupied);
// Return -1, -1 if no suitable location found
if (bestDistance == Double.MAX_VALUE) {
bestXY[0] = -1;
bestXY[1] = -1;
}
recycleTempRects(validRegions);
return bestXY;
}
/**
* Find a vacant area that will fit the given bounds nearest the requested
* cell location, and will also weigh in a suggested direction vector of the
* desired location. This method computers distance based on unit grid distances,
* not pixel distances.
*
* @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 minSpanX The minimum horizontal span required
* @param minSpanY The minimum vertical span required
* @param spanX Horizontal span of the object.
* @param spanY Vertical span of the object.
* @param ignoreOccupied If true, the result can be an occupied cell
* @param result Array in which to place the result, or null (in which case a new array will
* be allocated)
* @return The X, Y cell of a vacant area that can contain this object,
* nearest the requested location.
*/
private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
boolean[][] occupied, int[] result) {
// Keep track of best-scoring drop area
final int[] bestXY = result != null ? result : new int[2];
float bestDistance = Float.MAX_VALUE;
int bestDirectionScore = Integer.MIN_VALUE;
final int countX = mCountX;
final int countY = mCountY;
for (int y = 0; y < countY - (spanY - 1); y++) {
inner:
for (int x = 0; x < countX - (spanX - 1); x++) {
// First, let's see if this thing fits anywhere
for (int i = 0; i < spanX; i++) {
for (int j = 0; j < spanY; j++) {
if (occupied[x + i][y + j]) {
continue inner;
}
}
}
float distance = (float)
Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
int[] curDirection = mTmpPoint;
computeDirectionVector(cellX, cellY, x, y, curDirection);
int curDirectionScore = direction[0] * curDirection[0] +
direction[1] * curDirection[1];
if (Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
bestDistance = distance;
bestDirectionScore = curDirectionScore;
bestXY[0] = x;
bestXY[1] = y;
}
}
}
// Return -1, -1 if no suitable location found
if (bestDistance == Float.MAX_VALUE) {
bestXY[0] = -1;
bestXY[1] = -1;
}
return bestXY;
}
private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
int[] direction) {
LayoutParams lp = (LayoutParams) v.getLayoutParams();
boolean success = false;
markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
lp.cellVSpan, mTmpOccupied, false);
markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
findNearestArea(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan,
direction, mTmpOccupied, mTempLocation);
if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
lp.tmpCellX = mTempLocation[0];
lp.tmpCellY = mTempLocation[1];
success = true;
}
markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
lp.cellVSpan, mTmpOccupied, true);
return success;
}
private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
int[] direction) {
if (views.size() == 0) return true;
boolean success = false;
// We construct a rect which represents the entire group of views
Rect boundingRect = null;
for (View v: views) {
LayoutParams lp = (LayoutParams) v.getLayoutParams();
markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
lp.cellVSpan, mTmpOccupied, false);
if (boundingRect == null) {
boundingRect = new Rect(lp.tmpCellX, lp.tmpCellY, lp.tmpCellX + lp.cellHSpan,
lp.tmpCellY + lp.cellVSpan);
} else {
boundingRect.union(lp.tmpCellX, lp.tmpCellY, lp.tmpCellX + lp.cellHSpan,
lp.tmpCellY + lp.cellVSpan);
}
}
markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
// TODO: this bounding rect may not be completely filled, lets be more precise about this
// check.
findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(), boundingRect.height(),
direction, mTmpOccupied, mTempLocation);
int deltaX = mTempLocation[0] - boundingRect.left;
int deltaY = mTempLocation[1] - boundingRect.top;
if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
for (View v: views) {
LayoutParams lp = (LayoutParams) v.getLayoutParams();
lp.tmpCellX += deltaX;
lp.tmpCellY += deltaY;
}
success = true;
}
for (View v: views) {
LayoutParams lp = (LayoutParams) v.getLayoutParams();
markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
lp.cellVSpan, mTmpOccupied, true);
}
return success;
}
private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
}
private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
View ignoreView) {
mIntersectingViews.clear();
mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
markCellsForRect(mOccupiedRect, mTmpOccupied, true);
if (ignoreView != null) {
LayoutParams lp = (LayoutParams) ignoreView.getLayoutParams();
lp.tmpCellX = cellX;
lp.tmpCellY = cellY;
}
int childCount = mChildren.getChildCount();
Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
Rect r1 = new Rect();
for (int i = 0; i < childCount; i++) {
View child = mChildren.getChildAt(i);
if (child == ignoreView) continue;
LayoutParams lp = (LayoutParams) child.getLayoutParams();
r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
if (Rect.intersects(r0, r1)) {
if (!lp.canReorder) {
return false;
}
mIntersectingViews.add(child);
}
}
// First we try moving the views as a block
if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction)) {
return true;
}
// Ok, they couldn't move as a block, let's move them individually
for (View v : mIntersectingViews) {
if (!addViewToTempLocation(v, mOccupiedRect, direction)) {
return false;
}
}
return true;
}
/*
* Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
* the provided point and the provided cell
*/
private void computeDirectionVector(int x0, int y0, int x1, int y1, int[] result) {
int deltaX = x1 - x0;
int deltaY = y1 - y0;
double angle = Math.atan(((float) deltaY) / deltaX);
result[0] = 0;
result[1] = 0;
if (Math.abs(Math.cos(angle)) > 0.5f) {
result[0] = (int) Math.signum(deltaX);
}
if (Math.abs(Math.sin(angle)) > 0.5f) {
result[1] = (int) Math.signum(deltaY);
}
}
ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
// This creates a copy of the current occupied array, omitting the current view being
// dragged
resetTempLayoutToCurrent(dragView);
// We find the nearest cell into which we would place the dragged item, assuming there's
// nothing in its way.
int result[] = new int[2];
result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
boolean success = false;
// First we try the exact nearest position of the item being dragged,
// we will then want to try to move this around to other neighbouring positions
success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView);
if (!success) {
// We try shrinking the widget down to size in an alternating pattern, shrink 1 in
// x, then 1 in y etc.
if (spanX > minSpanX && (minSpanY == spanY || decX)) {
return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
dragView, false, solution);
} else if (spanY > minSpanY) {
return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
dragView, true, solution);
}
solution.isSolution = false;
} else {
solution.isSolution = true;
solution.dragViewX = result[0];
solution.dragViewY = result[1];
solution.dragViewSpanX = spanX;
solution.dragViewSpanY = spanY;
copyCurrentStateToSolution(solution, true);
}
return solution;
}
private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
int childCount = mChildren.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = mChildren.getChildAt(i);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
Point p;
if (temp) {
p = new Point(lp.tmpCellX, lp.tmpCellY);
} else {
p = new Point(lp.cellX, lp.cellY);
}
solution.map.put(child, p);
}
}
private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
for (int i = 0; i < mCountX; i++) {
for (int j = 0; j < mCountY; j++) {
mTmpOccupied[i][j] = false;
}
}
int childCount = mChildren.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = mChildren.getChildAt(i);
if (child == dragView) continue;
LayoutParams lp = (LayoutParams) child.getLayoutParams();
Point p = solution.map.get(child);
if (p != null) {
lp.tmpCellX = p.x;
lp.tmpCellY = p.y;
markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan,
mTmpOccupied, true);
}
}
markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
solution.dragViewSpanY, mTmpOccupied, true);
}
private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
commitDragView) {
boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
for (int i = 0; i < mCountX; i++) {
for (int j = 0; j < mCountY; j++) {
occupied[i][j] = false;
}
}
int childCount = mChildren.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = mChildren.getChildAt(i);
if (child == dragView) continue;
LayoutParams lp = (LayoutParams) child.getLayoutParams();
Point p = solution.map.get(child);
if (p != null) {
if (lp.cellX != p.x || lp.cellY != p.y) {
animateChildToPosition(child, p.x, p.y, 150, 0, DESTRUCTIVE_REORDER, false);
}
markCellsForView(p.x, p.y, lp.cellHSpan, lp.cellVSpan, occupied, true);
}
}
if (commitDragView) {
markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
solution.dragViewSpanY, occupied, true);
}
}
private void commitTempPlacement() {
for (int i = 0; i < mCountX; i++) {
for (int j = 0; j < mCountY; j++) {
mOccupied[i][j] = mTmpOccupied[i][j];
}
}
int childCount = mChildren.getChildCount();
for (int i = 0; i < childCount; i++) {
LayoutParams lp = (LayoutParams) mChildren.getChildAt(i).getLayoutParams();
lp.cellX = lp.tmpCellX;
lp.cellY = lp.tmpCellY;
}
}
public void setUseTempCoords(boolean useTempCoords) {
int childCount = mChildren.getChildCount();
for (int i = 0; i < childCount; i++) {
LayoutParams lp = (LayoutParams) mChildren.getChildAt(i).getLayoutParams();
lp.useTmpCoords = useTempCoords;
}
}
private void resetTempLayoutToCurrent(View ignoreView) {
for (int i = 0; i < mCountX; i++) {
for (int j = 0; j < mCountY; j++) {
mTmpOccupied[i][j] = mOccupied[i][j];
}
}
int childCount = mChildren.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = mChildren.getChildAt(i);
if (child == ignoreView) continue;
LayoutParams lp = (LayoutParams) child.getLayoutParams();
lp.tmpCellX = lp.cellX;
lp.tmpCellY = lp.cellY;
}
}
ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
int spanX, int spanY, View dragView, ItemConfiguration solution) {
int[] result = new int[2];
int[] resultSpan = new int[2];
findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
resultSpan);
if (result[0] >= 0 && result[1] >= 0) {
copyCurrentStateToSolution(solution, false);
solution.dragViewX = result[0];
solution.dragViewY = result[1];
solution.dragViewSpanX = resultSpan[0];
solution.dragViewSpanY = resultSpan[1];
solution.isSolution = true;
} else {
solution.isSolution = false;
}
return solution;
}
public void prepareChildForDrag(View child) {
markCellsAsUnoccupiedForView(child);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
lp.cellX = -1;
lp.cellY = -1;
}
int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
View dragView, int[] result, int resultSpan[], int mode) {
// First we determine if things have moved enough to cause a different layout
result = findNearestArea(pixelX, pixelY, 1, 1, result);
if (resultSpan == null) {
resultSpan = new int[2];
}
// We attempt the first algorithm
cellToCenterPoint(result[0], result[1], mTmpPoint);
computeDirectionVector(pixelX, pixelY, mTmpPoint[0], mTmpPoint[1], mDirectionVector);
ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
// We attempt the approach which doesn't shuffle views at all
ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
minSpanY, spanX, spanY, dragView, new ItemConfiguration());
ItemConfiguration finalSolution = null;
if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
finalSolution = swapSolution;
} else if (noShuffleSolution.isSolution) {
finalSolution = noShuffleSolution;
}
boolean foundSolution = true;
if (!DESTRUCTIVE_REORDER) {
setUseTempCoords(true);
}
if (finalSolution != null) {
result[0] = finalSolution.dragViewX;
result[1] = finalSolution.dragViewY;
resultSpan[0] = finalSolution.dragViewSpanX;
resultSpan[1] = finalSolution.dragViewSpanY;
// If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
// committing anything or animating anything as we just want to determine if a solution
// exists
if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
if (!DESTRUCTIVE_REORDER) {
copySolutionToTempState(finalSolution, dragView);
}
setItemPlacementDirty(true);
animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
if (!DESTRUCTIVE_REORDER && mode == MODE_ON_DROP) {
commitTempPlacement();
}
}
} else {
foundSolution = false;
result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
}
if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
setUseTempCoords(false);
}
boolean[][] occupied = mOccupied;
mChildren.requestLayout();
return result;
}
public boolean isItemPlacementDirty() {
return mItemLocationsDirty;
}
public void setItemPlacementDirty(boolean dirty) {
mItemLocationsDirty = dirty;
}
private class ItemConfiguration {
HashMap<View, Point> map = new HashMap<View, Point>();
boolean isSolution = false;
int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
int area() {
return dragViewSpanX * dragViewSpanY;
}
void clear() {
map.clear();
isSolution = false;
}
}
/**
* 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
* @param result Previously returned value to possibly recycle.
* @return The X, Y cell of a vacant area that can contain this object,
* nearest the requested location.
*/
int[] findNearestVacantArea(
int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
}
/**
* 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 minSpanX The minimum horizontal span required
* @param minSpanY The minimum vertical span required
* @param spanX Horizontal span of the object.
* @param spanY Vertical span of the object.
* @param ignoreView Considers space occupied by this view as unoccupied
* @param result Previously returned value to possibly recycle.
* @return The X, Y cell of a vacant area that can contain this object,
* nearest the requested location.
*/
int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
result, resultSpan, mOccupied);
}
/**
* Find a starting cell position 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
* @param result Previously returned value to possibly recycle.
* @return The X, Y cell of a vacant area that can contain this object,
* nearest the requested location.
*/
int[] findNearestArea(
int pixelX, int pixelY, int spanX, int spanY, int[] result) {
return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
}
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, mOccupied);
}
/**
* 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, mOccupied);
}
/**
* 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, mOccupied);
}
/**
* The superset of the above two methods
*/
boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
// mark space take by ignoreView as available (method checks if ignoreView is null)
markCellsAsUnoccupiedForView(ignoreView, occupied);
boolean foundCell = false;
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));
}
for (int y = startY; y < endY && !foundCell; y++) {
inner:
for (int x = startX; x < endX; x++) {
for (int i = 0; i < spanX; i++) {
for (int j = 0; j < spanY; j++) {
if (occupied[x + i][y + j]) {
// small optimization: we can skip to after the column we just found
// an occupied cell
x += i;
continue inner;
}
}
}
if (cellXY != null) {
cellXY[0] = x;
cellXY[1] = y;
}
foundCell = true;
break;
}
}
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;
}
}
// re-mark space taken by ignoreView as occupied
markCellsAsOccupiedForView(ignoreView, occupied);
return foundCell;
}
/**
* 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.
*/
void onDragEnter() {
if (!mDragging) {
// Fade in the drag indicators
if (mCrosshairsAnimator != null) {
mCrosshairsAnimator.animateIn();
}
}
mDragging = true;
}
/**
* Called when drag has left this CellLayout or has been completed (successfully or not)
*/
void onDragExit() {
// 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;
// Fade out the drag indicators
if (mCrosshairsAnimator != null) {
mCrosshairsAnimator.animateOut();
}
}
// Invalidate the drag data
mDragCell[0] = mDragCell[1] = -1;
mDragOutlineAnims[mDragOutlineCurrent].animateOut();
mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
setIsDragOverlapping(false);
}
/**
* Mark a child as having been dropped.
* At the beginning of the drag operation, the child may have been on another
* screen, but it is re-parented before this method is called.
*
* @param child The child that is being dropped
*/
void onDropChild(View child) {
if (child != null) {
LayoutParams lp = (LayoutParams) child.getLayoutParams();
lp.dropped = true;
child.requestLayout();
}
}
/**
* Computes a bounding rectangle for a range of cells
*
* @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
* @param cellHSpan Width in cells
* @param cellVSpan Height in cells
* @param resultRect Rect into which to put the results
*/
public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
final int cellWidth = mCellWidth;
final int cellHeight = mCellHeight;
final int widthGap = mWidthGap;
final int heightGap = mHeightGap;
final int hStartPadding = getPaddingLeft();
final int vStartPadding = getPaddingTop();
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);
resultRect.set(x, y, x + width, y + height);
}
/**
* Computes the required horizontal and vertical cell spans to always
* fit the given rectangle.
*
* @param width Width in pixels
* @param height Height in pixels
* @param result An array of length 2 in which to store the result (may be null).
*/
public int[] rectToCell(int width, int height, int[] result) {
return rectToCell(getResources(), width, height, result);
}
public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
// Always assume we're working with the smallest span to make sure we
// reserve enough space in both orientations.
int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
int smallerSize = Math.min(actualWidth, actualHeight);
// Always round up to next largest cell
int spanX = (int) Math.ceil(width / (float) smallerSize);
int spanY = (int) Math.ceil(height / (float) smallerSize);
if (result == null) {
return new int[] { spanX, spanY };
}
result[0] = spanX;
result[1] = spanY;
return result;
}
public int[] cellSpansToSize(int hSpans, int vSpans) {
int[] size = new int[2];
size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
return size;
}
/**
* Calculate the grid spans needed to fit given item
*/
public void calculateSpans(ItemInfo info) {
final int minWidth;
final int minHeight;
if (info instanceof LauncherAppWidgetInfo) {
minWidth = ((LauncherAppWidgetInfo) info).minWidth;
minHeight = ((LauncherAppWidgetInfo) info).minHeight;
} else if (info instanceof PendingAddWidgetInfo) {
minWidth = ((PendingAddWidgetInfo) info).minWidth;
minHeight = ((PendingAddWidgetInfo) info).minHeight;
} else {
// It's not a widget, so it must be 1x1
info.spanX = info.spanY = 1;
return;
}
int[] spans = rectToCell(minWidth, minHeight, null);
info.spanX = spans[0];
info.spanY = spans[1];
}
/**
* 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.
*
* @return True if a vacant cell was found
*/
public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
}
static boolean findVacantCell(int[] vacant, int spanX, int spanY,
int xCount, int yCount, boolean[][] occupied) {
for (int y = 0; y < yCount; y++) {
for (int x = 0; x < xCount; x++) {
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;
}
private void clearOccupiedCells() {
for (int x = 0; x < mCountX; x++) {
for (int y = 0; y < mCountY; y++) {
mOccupied[x][y] = false;
}
}
}
/**
* Given a view, determines how much that view can be expanded in all directions, in terms of
* whether or not there are other items occupying adjacent cells. Used by the
* AppWidgetResizeFrame to determine how the widget can be resized.
*/
public void getExpandabilityArrayForView(View view, int[] expandability) {
final LayoutParams lp = (LayoutParams) view.getLayoutParams();
boolean flag;
expandability[AppWidgetResizeFrame.LEFT] = 0;
for (int x = lp.cellX - 1; x >= 0; x--) {
flag = false;
for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
if (mOccupied[x][y]) flag = true;
}
if (flag) break;
expandability[AppWidgetResizeFrame.LEFT]++;
}
expandability[AppWidgetResizeFrame.TOP] = 0;
for (int y = lp.cellY - 1; y >= 0; y--) {
flag = false;
for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
if (mOccupied[x][y]) flag = true;
}
if (flag) break;
expandability[AppWidgetResizeFrame.TOP]++;
}
expandability[AppWidgetResizeFrame.RIGHT] = 0;
for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
flag = false;
for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
if (mOccupied[x][y]) flag = true;
}
if (flag) break;
expandability[AppWidgetResizeFrame.RIGHT]++;
}
expandability[AppWidgetResizeFrame.BOTTOM] = 0;
for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
flag = false;
for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
if (mOccupied[x][y]) flag = true;
}
if (flag) break;
expandability[AppWidgetResizeFrame.BOTTOM]++;
}
}
public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
markCellsAsUnoccupiedForView(view);
markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
}
public void markCellsAsOccupiedForView(View view) {
markCellsAsOccupiedForView(view, mOccupied);
}
public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
if (view == null || view.getParent() != mChildren) return;
LayoutParams lp = (LayoutParams) view.getLayoutParams();
markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
}
public void markCellsAsUnoccupiedForView(View view) {
markCellsAsUnoccupiedForView(view, mOccupied);
}
public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
if (view == null || view.getParent() != mChildren) return;
LayoutParams lp = (LayoutParams) view.getLayoutParams();
markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
}
private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
boolean value) {
if (cellX < 0 || cellY < 0) return;
for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
occupied[x][y] = value;
}
}
}
public int getDesiredWidth() {
return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
(Math.max((mCountX - 1), 0) * mWidthGap);
}
public int getDesiredHeight() {
return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
(Math.max((mCountY - 1), 0) * mHeightGap);
}
public boolean isOccupied(int x, int y) {
if (x < mCountX && y < mCountY) {
return mOccupied[x][y];
} else {
throw new RuntimeException("Position exceeds the bound of this CellLayout");
}
}
@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);
}
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);
}
}
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;
/**
* Temporary horizontal location of the item in the grid during reorder
*/
public int tmpCellX;
/**
* Temporary vertical location of the item in the grid during reorder
*/
public int tmpCellY;
/**
* Indicates that the temporary coordinates should be used to layout the items
*/
public boolean useTmpCoords;
/**
* 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;
/**
* Indicates whether the item will set its x, y, width and height parameters freely,
* or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
*/
public boolean isLockedToGrid = true;
/**
* Indicates whether this item can be reordered. Always true except in the case of the
* the AllApps button.
*/
public boolean canReorder = true;
// X coordinate of the view in the layout.
@ViewDebug.ExportedProperty
int x;
// Y coordinate of the view in the layout.
@ViewDebug.ExportedProperty
int y;
boolean dropped;
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
cellHSpan = 1;
cellVSpan = 1;
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
cellHSpan = 1;
cellVSpan = 1;
}
public LayoutParams(LayoutParams source) {
super(source);
this.cellX = source.cellX;
this.cellY = source.cellY;
this.cellHSpan = source.cellHSpan;
this.cellVSpan = source.cellVSpan;
}
public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
this.cellX = cellX;
this.cellY = cellY;
this.cellHSpan = cellHSpan;
this.cellVSpan = cellVSpan;
}
public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
if (isLockedToGrid) {
final int myCellHSpan = cellHSpan;
final int myCellVSpan = cellVSpan;
final int myCellX = useTmpCoords ? tmpCellX : cellX;
final int myCellY = useTmpCoords ? tmpCellY : cellY;
width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
leftMargin - rightMargin;
height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
topMargin - bottomMargin;
x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
}
}
public String toString() {
return "(" + this.cellX + ", " + this.cellY + ")";
}
public void setWidth(int width) {
this.width = width;
}
public int getWidth() {
return width;
}
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return height;
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
}
// 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
static final class CellInfo {
View cell;
int cellX = -1;
int cellY = -1;
int spanX;
int spanY;
int screen;
long container;
@Override
public String toString() {
return "Cell[view=" + (cell == null ? "null" : cell.getClass())
+ ", x=" + cellX + ", y=" + cellY + "]";
}
}
public boolean lastDownOnOccupiedCell() {
return mLastDownOnOccupiedCell;
}
}