blob: fe69e9fd06f565a4f0363aa2416bf1b653e068b1 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Brandon Keely50e6e562012-05-08 16:28:49 -070021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
Chet Haase00397b12010-10-07 11:13:10 -070023import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070024import android.animation.ValueAnimator;
25import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040027import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070029import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080031import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070033import android.graphics.Point;
Adam Cohenb5ba0972011-09-07 18:02:31 -070034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080037import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070038import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070039import android.graphics.drawable.NinePatchDrawable;
Adam Cohen1462de32012-07-24 22:34:36 -070040import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070042import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070043import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.view.MotionEvent;
45import android.view.View;
46import android.view.ViewDebug;
47import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070049import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051
Adam Cohen66396872011-04-15 17:50:36 -070052import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070053import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070054
Adam Cohen69ce2e52011-07-03 19:25:21 -070055import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070057import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080058import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070059
Michael Jurkabdb5c532011-02-01 15:05:06 -080060public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070061 static final String TAG = "CellLayout";
62
Adam Cohen2acce882012-03-28 19:03:19 -070063 private Launcher mLauncher;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 private int mCellWidth;
65 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070066
Adam Cohend22015c2010-07-26 22:02:18 -070067 private int mCountX;
68 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
Adam Cohen234c4cd2011-07-17 21:03:04 -070070 private int mOriginalWidthGap;
71 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 private int mWidthGap;
73 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070074 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080075 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
77 private final Rect mRect = new Rect();
78 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070079
Patrick Dubroyde7658b2010-09-27 11:15:43 -070080 // These are temporary variables to prevent having to allocate a new object just to
81 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070082 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070083 private final int[] mTmpPoint = new int[2];
Adam Cohen69ce2e52011-07-03 19:25:21 -070084 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070085
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080087 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070088 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Michael Jurkadee05892010-07-27 10:01:56 -070090 private OnTouchListener mInterceptTouchListener;
91
Adam Cohen69ce2e52011-07-03 19:25:21 -070092 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070093 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070094
Adam Cohenb5ba0972011-09-07 18:02:31 -070095 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070096 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070097 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070098
Michael Jurka33945b22010-12-21 18:19:38 -080099 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800100 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700101 private Drawable mOverScrollForegroundDrawable;
102 private Drawable mOverScrollLeft;
103 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700104 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700106 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700107
Michael Jurka33945b22010-12-21 18:19:38 -0800108 // If we're actively dragging something over this screen, mIsDragOverlapping is true
109 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700110 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700111
Winson Chung150fbab2010-09-29 17:14:26 -0700112 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700113 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800114 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700115 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700116 private InterruptibleInOutAnimator[] mDragOutlineAnims =
117 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700118
119 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700120 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700121 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700122
Patrick Dubroy96864c32011-03-10 17:17:23 -0800123 private BubbleTextView mPressedOrFocusedIcon;
124
Adam Cohen482ed822012-03-02 14:15:13 -0800125 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
126 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700127 private HashMap<View, ReorderHintAnimation>
128 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
129
130 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700131
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700132 // When a drag operation is in progress, holds the nearest cell to the touch point
133 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Joe Onorato4be866d2010-10-10 11:26:02 -0700135 private boolean mDragging = false;
136
Patrick Dubroyce34a972010-10-19 10:34:32 -0700137 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700138 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700139
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800140 private boolean mIsHotseat = false;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800141
Adam Cohen482ed822012-03-02 14:15:13 -0800142 public static final int MODE_DRAG_OVER = 0;
143 public static final int MODE_ON_DROP = 1;
144 public static final int MODE_ON_DROP_EXTERNAL = 2;
145 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700146 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800147 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
148
Adam Cohena897f392012-04-27 18:12:05 -0700149 static final int LANDSCAPE = 0;
150 static final int PORTRAIT = 1;
151
Adam Cohen7bdfc972012-05-22 16:50:35 -0700152 private static final float REORDER_HINT_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700153 private static final int REORDER_ANIMATION_DURATION = 150;
154 private float mReorderHintAnimationMagnitude;
155
Adam Cohen482ed822012-03-02 14:15:13 -0800156 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
157 private Rect mOccupiedRect = new Rect();
158 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700159 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700160 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700161 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800162
Romain Guy8a0bff52012-05-06 13:14:33 -0700163 private final static PorterDuffXfermode sAddBlendMode =
164 new PorterDuffXfermode(PorterDuff.Mode.ADD);
Michael Jurkaca993832012-06-29 15:17:04 -0700165 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 public CellLayout(Context context) {
168 this(context, null);
169 }
170
171 public CellLayout(Context context, AttributeSet attrs) {
172 this(context, attrs, 0);
173 }
174
175 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
176 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700177 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700178
179 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
180 // the user where a dragged item will land when dropped.
181 setWillNotDraw(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700182 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
185
Adam Cohenf4bd5792012-04-27 11:35:29 -0700186 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
187 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700188 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
189 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700190 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700191 mCountX = LauncherModel.getCellCountX();
192 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700193 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800194 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700195 mPreviousReorderDirection[0] = INVALID_DIRECTION;
196 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
198 a.recycle();
199
200 setAlwaysDrawnWithCacheEnabled(false);
201
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700202 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700203
Winson Chung967289b2011-06-30 18:09:30 -0700204 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700205 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800206
Adam Cohenb5ba0972011-09-07 18:02:31 -0700207 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
208 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
209 mForegroundPadding =
210 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800211
Adam Cohen19f37922012-03-21 11:59:11 -0700212 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
213 res.getDimensionPixelSize(R.dimen.app_icon_size));
214
Winson Chungb26f3d62011-06-02 10:49:29 -0700215 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700216 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700217
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700218 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700219
Patrick Dubroyce34a972010-10-19 10:34:32 -0700220 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700221
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700222
Winson Chungb8c69f32011-10-19 21:36:08 -0700223 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700224 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800225 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226 }
227
228 // When dragging things around the home screens, we show a green outline of
229 // where the item will land. The outlines gradually fade out, leaving a trail
230 // behind the drag path.
231 // Set up all the animations that are used to implement this fading.
232 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700233 final float fromAlphaValue = 0;
234 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700235
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700236 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700237
238 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700239 final InterruptibleInOutAnimator anim =
240 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700241 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700242 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700243 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700244 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700245 final Bitmap outline = (Bitmap)anim.getTag();
246
247 // If an animation is started and then stopped very quickly, we can still
248 // get spurious updates we've cleared the tag. Guard against this.
249 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700250 @SuppressWarnings("all") // suppress dead code warning
251 final boolean debug = false;
252 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700253 Object val = animation.getAnimatedValue();
254 Log.d(TAG, "anim " + thisIndex + " update: " + val +
255 ", isStopped " + anim.isStopped());
256 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700257 // Try to prevent it from continuing to run
258 animation.cancel();
259 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700260 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800261 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700262 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700263 }
264 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700265 // The animation holds a reference to the drag outline bitmap as long is it's
266 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700267 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700268 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700269 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700270 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700271 anim.setTag(null);
272 }
273 }
274 });
275 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700276 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700277
Michael Jurka18014792010-10-14 09:01:34 -0700278 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700279 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800280
Michael Jurkaa52570f2012-03-20 03:18:20 -0700281 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
282 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
283 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700284 }
285
Michael Jurkaf6440da2011-04-05 14:50:34 -0700286 static int widthInPortrait(Resources r, int numCells) {
287 // We use this method from Workspace to figure out how many rows/columns Launcher should
288 // have. We ignore the left/right padding on CellLayout because it turns out in our design
289 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700290 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700291 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
292 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700293
Winson Chung4b825dcd2011-06-19 12:41:22 -0700294 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700295 }
296
Michael Jurkaf6440da2011-04-05 14:50:34 -0700297 static int heightInLandscape(Resources r, int numCells) {
298 // We use this method from Workspace to figure out how many rows/columns Launcher should
299 // have. We ignore the left/right padding on CellLayout because it turns out in our design
300 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700301 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700302 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
303 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700304
Winson Chung4b825dcd2011-06-19 12:41:22 -0700305 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700306 }
307
Adam Cohen2801caf2011-05-13 20:57:39 -0700308 public void enableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700309 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_HARDWARE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700310 }
311
312 public void disableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700313 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700314 }
315
316 public void buildHardwareLayer() {
317 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700318 }
319
320 public void setGridSize(int x, int y) {
321 mCountX = x;
322 mCountY = y;
323 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800324 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700325 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700326 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700327 }
328
Patrick Dubroy96864c32011-03-10 17:17:23 -0800329 private void invalidateBubbleTextView(BubbleTextView icon) {
330 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700331 invalidate(icon.getLeft() + getPaddingLeft() - padding,
332 icon.getTop() + getPaddingTop() - padding,
333 icon.getRight() + getPaddingLeft() + padding,
334 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800335 }
336
Adam Cohenb5ba0972011-09-07 18:02:31 -0700337 void setOverScrollAmount(float r, boolean left) {
338 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
339 mOverScrollForegroundDrawable = mOverScrollLeft;
340 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
341 mOverScrollForegroundDrawable = mOverScrollRight;
342 }
343
344 mForegroundAlpha = (int) Math.round((r * 255));
345 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
346 invalidate();
347 }
348
Patrick Dubroy96864c32011-03-10 17:17:23 -0800349 void setPressedOrFocusedIcon(BubbleTextView icon) {
350 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
351 // requires an expanded clip rect (due to the glow's blur radius)
352 BubbleTextView oldIcon = mPressedOrFocusedIcon;
353 mPressedOrFocusedIcon = icon;
354 if (oldIcon != null) {
355 invalidateBubbleTextView(oldIcon);
356 }
357 if (mPressedOrFocusedIcon != null) {
358 invalidateBubbleTextView(mPressedOrFocusedIcon);
359 }
360 }
361
Michael Jurka33945b22010-12-21 18:19:38 -0800362 void setIsDragOverlapping(boolean isDragOverlapping) {
363 if (mIsDragOverlapping != isDragOverlapping) {
364 mIsDragOverlapping = isDragOverlapping;
365 invalidate();
366 }
367 }
368
369 boolean getIsDragOverlapping() {
370 return mIsDragOverlapping;
371 }
372
Adam Cohenebea84d2011-11-09 17:20:41 -0800373 protected void setOverscrollTransformsDirty(boolean dirty) {
374 mScrollingTransformsDirty = dirty;
375 }
376
377 protected void resetOverscrollTransforms() {
378 if (mScrollingTransformsDirty) {
379 setOverscrollTransformsDirty(false);
380 setTranslationX(0);
381 setRotationY(0);
382 // It doesn't matter if we pass true or false here, the important thing is that we
383 // pass 0, which results in the overscroll drawable not being drawn any more.
384 setOverScrollAmount(0, false);
385 setPivotX(getMeasuredWidth() / 2);
386 setPivotY(getMeasuredHeight() / 2);
387 }
388 }
389
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700390 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700391 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700392 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
393 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
394 // When we're small, we are either drawn normally or in the "accepts drops" state (during
395 // a drag). However, we also drag the mini hover background *over* one of those two
396 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700397 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700398 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800399
400 if (mIsDragOverlapping) {
401 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700402 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700403 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700404 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700405 }
Michael Jurka33945b22010-12-21 18:19:38 -0800406
407 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
408 bg.setBounds(mBackgroundRect);
409 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700410 }
Romain Guya6abce82009-11-10 02:54:41 -0800411
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700412 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700413 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700414 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700415 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800416 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700417 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700418 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800419 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700420 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700421 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800422
423 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
424 // requires an expanded clip rect (due to the glow's blur radius)
425 if (mPressedOrFocusedIcon != null) {
426 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
427 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
428 if (b != null) {
429 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700430 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
431 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800432 null);
433 }
434 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700435
Adam Cohen482ed822012-03-02 14:15:13 -0800436 if (DEBUG_VISUALIZE_OCCUPIED) {
437 int[] pt = new int[2];
438 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700439 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800440 for (int i = 0; i < mCountX; i++) {
441 for (int j = 0; j < mCountY; j++) {
442 if (mOccupied[i][j]) {
443 cellToPoint(i, j, pt);
444 canvas.save();
445 canvas.translate(pt[0], pt[1]);
446 cd.draw(canvas);
447 canvas.restore();
448 }
449 }
450 }
451 }
452
Andrew Flynn850d2e72012-04-26 16:51:20 -0700453 int previewOffset = FolderRingAnimator.sPreviewSize;
454
Adam Cohen69ce2e52011-07-03 19:25:21 -0700455 // The folder outer / inner ring image(s)
456 for (int i = 0; i < mFolderOuterRings.size(); i++) {
457 FolderRingAnimator fra = mFolderOuterRings.get(i);
458
459 // Draw outer ring
460 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
461 int width = (int) fra.getOuterRingSize();
462 int height = width;
463 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
464
465 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700466 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700467
468 canvas.save();
469 canvas.translate(centerX - width / 2, centerY - height / 2);
470 d.setBounds(0, 0, width, height);
471 d.draw(canvas);
472 canvas.restore();
473
474 // Draw inner ring
475 d = FolderRingAnimator.sSharedInnerRingDrawable;
476 width = (int) fra.getInnerRingSize();
477 height = width;
478 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
479
480 centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700481 centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700482 canvas.save();
483 canvas.translate(centerX - width / 2, centerY - width / 2);
484 d.setBounds(0, 0, width, height);
485 d.draw(canvas);
486 canvas.restore();
487 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700488
489 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
490 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
491 int width = d.getIntrinsicWidth();
492 int height = d.getIntrinsicHeight();
493
494 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
495 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700496 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohenc51934b2011-07-26 21:07:43 -0700497
498 canvas.save();
499 canvas.translate(centerX - width / 2, centerY - width / 2);
500 d.setBounds(0, 0, width, height);
501 d.draw(canvas);
502 canvas.restore();
503 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700504 }
505
Adam Cohenb5ba0972011-09-07 18:02:31 -0700506 @Override
507 protected void dispatchDraw(Canvas canvas) {
508 super.dispatchDraw(canvas);
509 if (mForegroundAlpha > 0) {
510 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
511 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700512 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700513 mOverScrollForegroundDrawable.draw(canvas);
514 p.setXfermode(null);
515 }
516 }
517
Adam Cohen69ce2e52011-07-03 19:25:21 -0700518 public void showFolderAccept(FolderRingAnimator fra) {
519 mFolderOuterRings.add(fra);
520 }
521
522 public void hideFolderAccept(FolderRingAnimator fra) {
523 if (mFolderOuterRings.contains(fra)) {
524 mFolderOuterRings.remove(fra);
525 }
526 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700527 }
528
Adam Cohenc51934b2011-07-26 21:07:43 -0700529 public void setFolderLeaveBehindCell(int x, int y) {
530 mFolderLeaveBehindCell[0] = x;
531 mFolderLeaveBehindCell[1] = y;
532 invalidate();
533 }
534
535 public void clearFolderLeaveBehind() {
536 mFolderLeaveBehindCell[0] = -1;
537 mFolderLeaveBehindCell[1] = -1;
538 invalidate();
539 }
540
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700541 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700542 public boolean shouldDelayChildPressedState() {
543 return false;
544 }
545
Adam Cohen1462de32012-07-24 22:34:36 -0700546 public void restoreInstanceState(SparseArray<Parcelable> states) {
547 dispatchRestoreInstanceState(states);
548 }
549
Michael Jurkae6235dd2011-10-04 15:02:05 -0700550 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700551 public void cancelLongPress() {
552 super.cancelLongPress();
553
554 // Cancel long press for all children
555 final int count = getChildCount();
556 for (int i = 0; i < count; i++) {
557 final View child = getChildAt(i);
558 child.cancelLongPress();
559 }
560 }
561
Michael Jurkadee05892010-07-27 10:01:56 -0700562 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
563 mInterceptTouchListener = listener;
564 }
565
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700567 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 }
569
570 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700571 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 }
573
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800574 public void setIsHotseat(boolean isHotseat) {
575 mIsHotseat = isHotseat;
576 }
577
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800578 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700579 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700580 final LayoutParams lp = params;
581
Andrew Flynnde38e422012-05-08 11:22:15 -0700582 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800583 if (child instanceof BubbleTextView) {
584 BubbleTextView bubbleChild = (BubbleTextView) child;
585
Andrew Flynnde38e422012-05-08 11:22:15 -0700586 Resources res = getResources();
587 if (mIsHotseat) {
588 bubbleChild.setTextColor(res.getColor(android.R.color.transparent));
589 } else {
590 bubbleChild.setTextColor(res.getColor(R.color.workspace_icon_text_color));
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800591 }
592 }
593
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 // Generate an id for each view, this assumes we have at most 256x256 cells
595 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700596 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700597 // If the horizontal or vertical span is set to -1, it is taken to
598 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700599 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
600 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601
Winson Chungaafa03c2010-06-11 17:34:16 -0700602 child.setId(childId);
603
Michael Jurkaa52570f2012-03-20 03:18:20 -0700604 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700605
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700606 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700607
Winson Chungaafa03c2010-06-11 17:34:16 -0700608 return true;
609 }
610 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700612
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700614 public void removeAllViews() {
615 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700616 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700617 }
618
619 @Override
620 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700621 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700622 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700623 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700624 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700625 }
626
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700627 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700628 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700629 }
630
Michael Jurka0280c3b2010-09-17 15:00:07 -0700631 @Override
632 public void removeView(View view) {
633 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700634 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700635 }
636
637 @Override
638 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700639 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
640 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700641 }
642
643 @Override
644 public void removeViewInLayout(View view) {
645 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700646 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700647 }
648
649 @Override
650 public void removeViews(int start, int count) {
651 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700652 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700653 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700654 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700655 }
656
657 @Override
658 public void removeViewsInLayout(int start, int count) {
659 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700660 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700661 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700662 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800663 }
664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 @Override
666 protected void onAttachedToWindow() {
667 super.onAttachedToWindow();
668 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
669 }
670
Michael Jurkaaf442092010-06-10 17:01:57 -0700671 public void setTagToCellInfoForPoint(int touchX, int touchY) {
672 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800673 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700674 final int x = touchX + getScrollX();
675 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700676 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700677
678 boolean found = false;
679 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700680 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800681 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700682
Adam Cohen1b607ed2011-03-03 17:26:50 -0800683 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
684 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700685 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700686
Winson Chungeecf02d2012-03-02 17:14:58 -0800687 float scale = child.getScaleX();
688 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
689 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700690 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
691 // offset that by this CellLayout's padding to test an (x,y) point that is relative
692 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700693 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800694 frame.inset((int) (frame.width() * (1f - scale) / 2),
695 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700696
Michael Jurkaaf442092010-06-10 17:01:57 -0700697 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700698 cellInfo.cell = child;
699 cellInfo.cellX = lp.cellX;
700 cellInfo.cellY = lp.cellY;
701 cellInfo.spanX = lp.cellHSpan;
702 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700703 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700704 break;
705 }
706 }
707 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700708
Michael Jurkad771c962011-08-09 15:00:48 -0700709 mLastDownOnOccupiedCell = found;
710
Michael Jurkaaf442092010-06-10 17:01:57 -0700711 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700712 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700713 pointToCellExact(x, y, cellXY);
714
Michael Jurkaaf442092010-06-10 17:01:57 -0700715 cellInfo.cell = null;
716 cellInfo.cellX = cellXY[0];
717 cellInfo.cellY = cellXY[1];
718 cellInfo.spanX = 1;
719 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700720 }
721 setTag(cellInfo);
722 }
723
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 @Override
725 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700726 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
727 // even in the case where we return early. Not clearing here was causing bugs whereby on
728 // long-press we'd end up picking up an item from a previous drag operation.
729 final int action = ev.getAction();
730
731 if (action == MotionEvent.ACTION_DOWN) {
732 clearTagCellInfo();
733 }
734
Michael Jurkadee05892010-07-27 10:01:56 -0700735 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
736 return true;
737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738
739 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700740 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800742
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 return false;
744 }
745
Adam Cohenc1997fd2011-08-15 18:26:39 -0700746 private void clearTagCellInfo() {
747 final CellInfo cellInfo = mCellInfo;
748 cellInfo.cell = null;
749 cellInfo.cellX = -1;
750 cellInfo.cellY = -1;
751 cellInfo.spanX = 0;
752 cellInfo.spanY = 0;
753 setTag(cellInfo);
754 }
755
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700757 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 }
759
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700760 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700761 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 * @param x X coordinate of the point
763 * @param y Y coordinate of the point
764 * @param result Array of 2 ints to hold the x and y coordinate of the cell
765 */
766 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700767 final int hStartPadding = getPaddingLeft();
768 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769
770 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
771 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
772
Adam Cohend22015c2010-07-26 22:02:18 -0700773 final int xAxis = mCountX;
774 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775
776 if (result[0] < 0) result[0] = 0;
777 if (result[0] >= xAxis) result[0] = xAxis - 1;
778 if (result[1] < 0) result[1] = 0;
779 if (result[1] >= yAxis) result[1] = yAxis - 1;
780 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700781
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 /**
783 * Given a point, return the cell that most closely encloses that point
784 * @param x X coordinate of the point
785 * @param y Y coordinate of the point
786 * @param result Array of 2 ints to hold the x and y coordinate of the cell
787 */
788 void pointToCellRounded(int x, int y, int[] result) {
789 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
790 }
791
792 /**
793 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700794 *
795 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700797 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 * @param result Array of 2 ints to hold the x and y coordinate of the point
799 */
800 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700801 final int hStartPadding = getPaddingLeft();
802 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803
804 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
805 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
806 }
807
Adam Cohene3e27a82011-04-15 12:07:39 -0700808 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800809 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700810 *
811 * @param cellX X coordinate of the cell
812 * @param cellY Y coordinate of the cell
813 *
814 * @param result Array of 2 ints to hold the x and y coordinate of the point
815 */
816 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700817 regionToCenterPoint(cellX, cellY, 1, 1, result);
818 }
819
820 /**
821 * Given a cell coordinate and span return the point that represents the center of the regio
822 *
823 * @param cellX X coordinate of the cell
824 * @param cellY Y coordinate of the cell
825 *
826 * @param result Array of 2 ints to hold the x and y coordinate of the point
827 */
828 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700829 final int hStartPadding = getPaddingLeft();
830 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700831 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
832 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
833 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
834 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700835 }
836
Adam Cohen19f37922012-03-21 11:59:11 -0700837 /**
838 * Given a cell coordinate and span fills out a corresponding pixel rect
839 *
840 * @param cellX X coordinate of the cell
841 * @param cellY Y coordinate of the cell
842 * @param result Rect in which to write the result
843 */
844 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
845 final int hStartPadding = getPaddingLeft();
846 final int vStartPadding = getPaddingTop();
847 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
848 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
849 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
850 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
851 }
852
Adam Cohen482ed822012-03-02 14:15:13 -0800853 public float getDistanceFromCell(float x, float y, int[] cell) {
854 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
855 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
856 Math.pow(y - mTmpPoint[1], 2));
857 return distance;
858 }
859
Romain Guy84f296c2009-11-04 15:00:44 -0800860 int getCellWidth() {
861 return mCellWidth;
862 }
863
864 int getCellHeight() {
865 return mCellHeight;
866 }
867
Adam Cohend4844c32011-02-18 19:25:06 -0800868 int getWidthGap() {
869 return mWidthGap;
870 }
871
872 int getHeightGap() {
873 return mHeightGap;
874 }
875
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700876 Rect getContentRect(Rect r) {
877 if (r == null) {
878 r = new Rect();
879 }
880 int left = getPaddingLeft();
881 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700882 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
883 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700884 r.set(left, top, right, bottom);
885 return r;
886 }
887
Adam Cohena897f392012-04-27 18:12:05 -0700888 static void getMetrics(Rect metrics, Resources res, int measureWidth, int measureHeight,
889 int countX, int countY, int orientation) {
890 int numWidthGaps = countX - 1;
891 int numHeightGaps = countY - 1;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700892
893 int widthGap;
894 int heightGap;
895 int cellWidth;
896 int cellHeight;
897 int paddingLeft;
898 int paddingRight;
899 int paddingTop;
900 int paddingBottom;
901
Adam Cohena897f392012-04-27 18:12:05 -0700902 int maxGap = res.getDimensionPixelSize(R.dimen.workspace_max_gap);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700903 if (orientation == LANDSCAPE) {
904 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_land);
905 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_land);
906 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
907 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
908 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_land);
909 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_land);
910 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_land);
911 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_land);
912 } else {
913 // PORTRAIT
914 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_port);
915 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_port);
916 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
917 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
918 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_port);
919 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_port);
920 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_port);
921 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_port);
922 }
923
924 if (widthGap < 0 || heightGap < 0) {
925 int hSpace = measureWidth - paddingLeft - paddingRight;
926 int vSpace = measureHeight - paddingTop - paddingBottom;
Adam Cohena897f392012-04-27 18:12:05 -0700927 int hFreeSpace = hSpace - (countX * cellWidth);
928 int vFreeSpace = vSpace - (countY * cellHeight);
929 widthGap = Math.min(maxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
930 heightGap = Math.min(maxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700931 }
932 metrics.set(cellWidth, cellHeight, widthGap, heightGap);
933 }
934
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 @Override
936 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700938 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
939
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
941 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700942
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
944 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
945 }
946
Adam Cohend22015c2010-07-26 22:02:18 -0700947 int numWidthGaps = mCountX - 1;
948 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949
Adam Cohen234c4cd2011-07-17 21:03:04 -0700950 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Michael Jurkadd13e3d2012-05-01 12:38:17 -0700951 int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight();
952 int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700953 int hFreeSpace = hSpace - (mCountX * mCellWidth);
954 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700955 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
956 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700957 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700958 } else {
959 mWidthGap = mOriginalWidthGap;
960 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700961 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700962
Michael Jurka8c920dd2011-01-20 14:16:56 -0800963 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
964 int newWidth = widthSpecSize;
965 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700966 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700967 newWidth = getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700968 ((mCountX - 1) * mWidthGap);
Michael Jurka8b805b12012-04-18 14:23:14 -0700969 newHeight = getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700970 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700971 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700972 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800973
974 int count = getChildCount();
975 for (int i = 0; i < count; i++) {
976 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700977 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
978 getPaddingRight(), MeasureSpec.EXACTLY);
979 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
980 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800981 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
982 }
983 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 }
985
986 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700987 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800990 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700991 child.layout(getPaddingLeft(), getPaddingTop(),
992 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
994 }
995
996 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700997 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
998 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700999 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001000 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
1001 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001002 }
1003
1004 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001006 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 }
1008
1009 @Override
1010 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001011 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 }
1013
Michael Jurka5f1c5092010-09-03 14:15:02 -07001014 public float getBackgroundAlpha() {
1015 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001016 }
1017
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001018 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001019 if (mBackgroundAlphaMultiplier != multiplier) {
1020 mBackgroundAlphaMultiplier = multiplier;
1021 invalidate();
1022 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001023 }
1024
Adam Cohenddb82192010-11-10 16:32:54 -08001025 public float getBackgroundAlphaMultiplier() {
1026 return mBackgroundAlphaMultiplier;
1027 }
1028
Michael Jurka5f1c5092010-09-03 14:15:02 -07001029 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001030 if (mBackgroundAlpha != alpha) {
1031 mBackgroundAlpha = alpha;
1032 invalidate();
1033 }
Michael Jurkadee05892010-07-27 10:01:56 -07001034 }
1035
Michael Jurkaa52570f2012-03-20 03:18:20 -07001036 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001037 final int childCount = getChildCount();
1038 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001039 getChildAt(i).setAlpha(alpha);
1040 }
1041 }
1042
Michael Jurkaa52570f2012-03-20 03:18:20 -07001043 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1044 if (getChildCount() > 0) {
1045 return (ShortcutAndWidgetContainer) getChildAt(0);
1046 }
1047 return null;
1048 }
1049
Patrick Dubroy440c3602010-07-13 17:50:32 -07001050 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001051 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001052 }
1053
Adam Cohen76fc0852011-06-17 13:26:23 -07001054 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001055 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001056 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001057 boolean[][] occupied = mOccupied;
1058 if (!permanent) {
1059 occupied = mTmpOccupied;
1060 }
1061
Adam Cohen19f37922012-03-21 11:59:11 -07001062 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001063 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1064 final ItemInfo info = (ItemInfo) child.getTag();
1065
1066 // We cancel any existing animations
1067 if (mReorderAnimators.containsKey(lp)) {
1068 mReorderAnimators.get(lp).cancel();
1069 mReorderAnimators.remove(lp);
1070 }
1071
Adam Cohen482ed822012-03-02 14:15:13 -08001072 final int oldX = lp.x;
1073 final int oldY = lp.y;
1074 if (adjustOccupied) {
1075 occupied[lp.cellX][lp.cellY] = false;
1076 occupied[cellX][cellY] = true;
1077 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001078 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001079 if (permanent) {
1080 lp.cellX = info.cellX = cellX;
1081 lp.cellY = info.cellY = cellY;
1082 } else {
1083 lp.tmpCellX = cellX;
1084 lp.tmpCellY = cellY;
1085 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001086 clc.setupLp(lp);
1087 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001088 final int newX = lp.x;
1089 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001090
Adam Cohen76fc0852011-06-17 13:26:23 -07001091 lp.x = oldX;
1092 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001093
Adam Cohen482ed822012-03-02 14:15:13 -08001094 // Exit early if we're not actually moving the view
1095 if (oldX == newX && oldY == newY) {
1096 lp.isLockedToGrid = true;
1097 return true;
1098 }
1099
Michael Jurka2ecf9952012-06-18 12:52:28 -07001100 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001101 va.setDuration(duration);
1102 mReorderAnimators.put(lp, va);
1103
1104 va.addUpdateListener(new AnimatorUpdateListener() {
1105 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001106 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001107 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001108 lp.x = (int) ((1 - r) * oldX + r * newX);
1109 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001110 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001111 }
1112 });
Adam Cohen482ed822012-03-02 14:15:13 -08001113 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001114 boolean cancelled = false;
1115 public void onAnimationEnd(Animator animation) {
1116 // If the animation was cancelled, it means that another animation
1117 // has interrupted this one, and we don't want to lock the item into
1118 // place just yet.
1119 if (!cancelled) {
1120 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001121 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001122 }
1123 if (mReorderAnimators.containsKey(lp)) {
1124 mReorderAnimators.remove(lp);
1125 }
1126 }
1127 public void onAnimationCancel(Animator animation) {
1128 cancelled = true;
1129 }
1130 });
Adam Cohen482ed822012-03-02 14:15:13 -08001131 va.setStartDelay(delay);
1132 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001133 return true;
1134 }
1135 return false;
1136 }
1137
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001138 /**
1139 * Estimate where the top left cell of the dragged item will land if it is dropped.
1140 *
1141 * @param originX The X value of the top left corner of the item
1142 * @param originY The Y value of the top left corner of the item
1143 * @param spanX The number of horizontal cells that the item spans
1144 * @param spanY The number of vertical cells that the item spans
1145 * @param result The estimated drop cell X and Y.
1146 */
1147 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001148 final int countX = mCountX;
1149 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001150
Michael Jurkaa63c4522010-08-19 13:52:27 -07001151 // pointToCellRounded takes the top left of a cell but will pad that with
1152 // cellWidth/2 and cellHeight/2 when finding the matching cell
1153 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001154
1155 // If the item isn't fully on this screen, snap to the edges
1156 int rightOverhang = result[0] + spanX - countX;
1157 if (rightOverhang > 0) {
1158 result[0] -= rightOverhang; // Snap to right
1159 }
1160 result[0] = Math.max(0, result[0]); // Snap to left
1161 int bottomOverhang = result[1] + spanY - countY;
1162 if (bottomOverhang > 0) {
1163 result[1] -= bottomOverhang; // Snap to bottom
1164 }
1165 result[1] = Math.max(0, result[1]); // Snap to top
1166 }
1167
Adam Cohen482ed822012-03-02 14:15:13 -08001168 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1169 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001170 final int oldDragCellX = mDragCell[0];
1171 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001172
Winson Chungb8c69f32011-10-19 21:36:08 -07001173 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001174 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1175 } else {
1176 mDragCenter.set(originX, originY);
1177 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001178
Adam Cohen2801caf2011-05-13 20:57:39 -07001179 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001180 return;
1181 }
1182
Adam Cohen482ed822012-03-02 14:15:13 -08001183 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1184 mDragCell[0] = cellX;
1185 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001186 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001187 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001188 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001189
Joe Onorato4be866d2010-10-10 11:26:02 -07001190 int left = topLeft[0];
1191 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001192
Winson Chungb8c69f32011-10-19 21:36:08 -07001193 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001194 // When drawing the drag outline, it did not account for margin offsets
1195 // added by the view's parent.
1196 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1197 left += lp.leftMargin;
1198 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001199
Adam Cohen99e8b402011-03-25 19:23:43 -07001200 // Offsets due to the size difference between the View and the dragOutline.
1201 // There is a size difference to account for the outer blur, which may lie
1202 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001203 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001204 // We center about the x axis
1205 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1206 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001207 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001208 if (dragOffset != null && dragRegion != null) {
1209 // Center the drag region *horizontally* in the cell and apply a drag
1210 // outline offset
1211 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1212 - dragRegion.width()) / 2;
1213 top += dragOffset.y;
1214 } else {
1215 // Center the drag outline in the cell
1216 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1217 - dragOutline.getWidth()) / 2;
1218 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1219 - dragOutline.getHeight()) / 2;
1220 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001221 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001222 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001223 mDragOutlineAnims[oldIndex].animateOut();
1224 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001225 Rect r = mDragOutlines[mDragOutlineCurrent];
1226 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1227 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001228 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001229 }
Winson Chung150fbab2010-09-29 17:14:26 -07001230
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001231 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1232 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001233 }
1234 }
1235
Adam Cohene0310962011-04-18 16:15:31 -07001236 public void clearDragOutlines() {
1237 final int oldIndex = mDragOutlineCurrent;
1238 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001239 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001240 }
1241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001243 * Find a vacant area that will fit the given bounds nearest the requested
1244 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001245 *
Romain Guy51afc022009-05-04 18:03:43 -07001246 * @param pixelX The X location at which you want to search for a vacant area.
1247 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001248 * @param spanX Horizontal span of the object.
1249 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001250 * @param result Array in which to place the result, or null (in which case a new array will
1251 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001252 * @return The X, Y cell of a vacant area that can contain this object,
1253 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001255 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1256 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001257 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001258 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001259
Michael Jurka6a1435d2010-09-27 17:35:12 -07001260 /**
1261 * Find a vacant area that will fit the given bounds nearest the requested
1262 * cell location. Uses Euclidean distance to score multiple vacant areas.
1263 *
1264 * @param pixelX The X location at which you want to search for a vacant area.
1265 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001266 * @param minSpanX The minimum horizontal span required
1267 * @param minSpanY The minimum vertical span required
1268 * @param spanX Horizontal span of the object.
1269 * @param spanY Vertical span of the object.
1270 * @param result Array in which to place the result, or null (in which case a new array will
1271 * be allocated)
1272 * @return The X, Y cell of a vacant area that can contain this object,
1273 * nearest the requested location.
1274 */
1275 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1276 int spanY, int[] result, int[] resultSpan) {
1277 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1278 result, resultSpan);
1279 }
1280
1281 /**
1282 * Find a vacant area that will fit the given bounds nearest the requested
1283 * cell location. Uses Euclidean distance to score multiple vacant areas.
1284 *
1285 * @param pixelX The X location at which you want to search for a vacant area.
1286 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001287 * @param spanX Horizontal span of the object.
1288 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001289 * @param ignoreOccupied If true, the result can be an occupied cell
1290 * @param result Array in which to place the result, or null (in which case a new array will
1291 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001292 * @return The X, Y cell of a vacant area that can contain this object,
1293 * nearest the requested location.
1294 */
Adam Cohendf035382011-04-11 17:22:04 -07001295 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1296 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001297 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001298 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001299 }
1300
1301 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1302 private void lazyInitTempRectStack() {
1303 if (mTempRectStack.isEmpty()) {
1304 for (int i = 0; i < mCountX * mCountY; i++) {
1305 mTempRectStack.push(new Rect());
1306 }
1307 }
1308 }
Adam Cohen482ed822012-03-02 14:15:13 -08001309
Adam Cohend41fbf52012-02-16 23:53:59 -08001310 private void recycleTempRects(Stack<Rect> used) {
1311 while (!used.isEmpty()) {
1312 mTempRectStack.push(used.pop());
1313 }
1314 }
1315
1316 /**
1317 * Find a vacant area that will fit the given bounds nearest the requested
1318 * cell location. Uses Euclidean distance to score multiple vacant areas.
1319 *
1320 * @param pixelX The X location at which you want to search for a vacant area.
1321 * @param pixelY The Y location at which you want to search for a vacant area.
1322 * @param minSpanX The minimum horizontal span required
1323 * @param minSpanY The minimum vertical span required
1324 * @param spanX Horizontal span of the object.
1325 * @param spanY Vertical span of the object.
1326 * @param ignoreOccupied If true, the result can be an occupied cell
1327 * @param result Array in which to place the result, or null (in which case a new array will
1328 * be allocated)
1329 * @return The X, Y cell of a vacant area that can contain this object,
1330 * nearest the requested location.
1331 */
1332 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001333 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1334 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001335 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001336 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001337 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001338
Adam Cohene3e27a82011-04-15 12:07:39 -07001339 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1340 // to the center of the item, but we are searching based on the top-left cell, so
1341 // we translate the point over to correspond to the top-left.
1342 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1343 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1344
Jeff Sharkey70864282009-04-07 21:08:40 -07001345 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001346 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001347 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001348 final Rect bestRect = new Rect(-1, -1, -1, -1);
1349 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001350
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001351 final int countX = mCountX;
1352 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001353
Adam Cohend41fbf52012-02-16 23:53:59 -08001354 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1355 spanX < minSpanX || spanY < minSpanY) {
1356 return bestXY;
1357 }
1358
1359 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001360 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001361 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1362 int ySize = -1;
1363 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001364 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001365 // First, let's see if this thing fits anywhere
1366 for (int i = 0; i < minSpanX; i++) {
1367 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001368 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001369 continue inner;
1370 }
Michael Jurkac28de512010-08-13 11:27:44 -07001371 }
1372 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001373 xSize = minSpanX;
1374 ySize = minSpanY;
1375
1376 // We know that the item will fit at _some_ acceptable size, now let's see
1377 // how big we can make it. We'll alternate between incrementing x and y spans
1378 // until we hit a limit.
1379 boolean incX = true;
1380 boolean hitMaxX = xSize >= spanX;
1381 boolean hitMaxY = ySize >= spanY;
1382 while (!(hitMaxX && hitMaxY)) {
1383 if (incX && !hitMaxX) {
1384 for (int j = 0; j < ySize; j++) {
1385 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1386 // We can't move out horizontally
1387 hitMaxX = true;
1388 }
1389 }
1390 if (!hitMaxX) {
1391 xSize++;
1392 }
1393 } else if (!hitMaxY) {
1394 for (int i = 0; i < xSize; i++) {
1395 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1396 // We can't move out vertically
1397 hitMaxY = true;
1398 }
1399 }
1400 if (!hitMaxY) {
1401 ySize++;
1402 }
1403 }
1404 hitMaxX |= xSize >= spanX;
1405 hitMaxY |= ySize >= spanY;
1406 incX = !incX;
1407 }
1408 incX = true;
1409 hitMaxX = xSize >= spanX;
1410 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001411 }
Winson Chung0be025d2011-05-23 17:45:09 -07001412 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001413 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414
Adam Cohend41fbf52012-02-16 23:53:59 -08001415 // We verify that the current rect is not a sub-rect of any of our previous
1416 // candidates. In this case, the current rect is disqualified in favour of the
1417 // containing rect.
1418 Rect currentRect = mTempRectStack.pop();
1419 currentRect.set(x, y, x + xSize, y + ySize);
1420 boolean contained = false;
1421 for (Rect r : validRegions) {
1422 if (r.contains(currentRect)) {
1423 contained = true;
1424 break;
1425 }
1426 }
1427 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001428 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1429 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001430
Adam Cohend41fbf52012-02-16 23:53:59 -08001431 if ((distance <= bestDistance && !contained) ||
1432 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001433 bestDistance = distance;
1434 bestXY[0] = x;
1435 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001436 if (resultSpan != null) {
1437 resultSpan[0] = xSize;
1438 resultSpan[1] = ySize;
1439 }
1440 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001441 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 }
1443 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001444 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001445 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446
Adam Cohenc0dcf592011-06-01 15:30:43 -07001447 // Return -1, -1 if no suitable location found
1448 if (bestDistance == Double.MAX_VALUE) {
1449 bestXY[0] = -1;
1450 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001451 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001452 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001453 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001455
Adam Cohen482ed822012-03-02 14:15:13 -08001456 /**
1457 * Find a vacant area that will fit the given bounds nearest the requested
1458 * cell location, and will also weigh in a suggested direction vector of the
1459 * desired location. This method computers distance based on unit grid distances,
1460 * not pixel distances.
1461 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001462 * @param cellX The X cell nearest to which you want to search for a vacant area.
1463 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001464 * @param spanX Horizontal span of the object.
1465 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001466 * @param direction The favored direction in which the views should move from x, y
1467 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1468 * matches exactly. Otherwise we find the best matching direction.
1469 * @param occoupied The array which represents which cells in the CellLayout are occupied
1470 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1471 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001472 * @param result Array in which to place the result, or null (in which case a new array will
1473 * be allocated)
1474 * @return The X, Y cell of a vacant area that can contain this object,
1475 * nearest the requested location.
1476 */
1477 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001478 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001479 // Keep track of best-scoring drop area
1480 final int[] bestXY = result != null ? result : new int[2];
1481 float bestDistance = Float.MAX_VALUE;
1482 int bestDirectionScore = Integer.MIN_VALUE;
1483
1484 final int countX = mCountX;
1485 final int countY = mCountY;
1486
1487 for (int y = 0; y < countY - (spanY - 1); y++) {
1488 inner:
1489 for (int x = 0; x < countX - (spanX - 1); x++) {
1490 // First, let's see if this thing fits anywhere
1491 for (int i = 0; i < spanX; i++) {
1492 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001493 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001494 continue inner;
1495 }
1496 }
1497 }
1498
1499 float distance = (float)
1500 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1501 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001502 computeDirectionVector(x - cellX, y - cellY, curDirection);
1503 // The direction score is just the dot product of the two candidate direction
1504 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001505 int curDirectionScore = direction[0] * curDirection[0] +
1506 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001507 boolean exactDirectionOnly = false;
1508 boolean directionMatches = direction[0] == curDirection[0] &&
1509 direction[0] == curDirection[0];
1510 if ((directionMatches || !exactDirectionOnly) &&
1511 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001512 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1513 bestDistance = distance;
1514 bestDirectionScore = curDirectionScore;
1515 bestXY[0] = x;
1516 bestXY[1] = y;
1517 }
1518 }
1519 }
1520
1521 // Return -1, -1 if no suitable location found
1522 if (bestDistance == Float.MAX_VALUE) {
1523 bestXY[0] = -1;
1524 bestXY[1] = -1;
1525 }
1526 return bestXY;
1527 }
1528
Adam Cohen47a876d2012-03-19 13:21:41 -07001529 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1530 int[] direction,boolean[][] occupied,
1531 boolean blockOccupied[][], int[] result) {
1532 // Keep track of best-scoring drop area
1533 final int[] bestXY = result != null ? result : new int[2];
1534 bestXY[0] = -1;
1535 bestXY[1] = -1;
1536 float bestDistance = Float.MAX_VALUE;
1537
1538 // We use this to march in a single direction
Adam Cohen5b53f292012-03-29 14:30:35 -07001539 if ((direction[0] != 0 && direction[1] != 0) ||
1540 (direction[0] == 0 && direction[1] == 0)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001541 return bestXY;
1542 }
1543
1544 // This will only incrememnet one of x or y based on the assertion above
1545 int x = cellX + direction[0];
1546 int y = cellY + direction[1];
1547 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001548 boolean fail = false;
1549 for (int i = 0; i < spanX; i++) {
1550 for (int j = 0; j < spanY; j++) {
1551 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1552 fail = true;
1553 }
1554 }
1555 }
1556 if (!fail) {
1557 float distance = (float)
1558 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1559 if (Float.compare(distance, bestDistance) < 0) {
1560 bestDistance = distance;
1561 bestXY[0] = x;
1562 bestXY[1] = y;
1563 }
1564 }
1565 x += direction[0];
1566 y += direction[1];
1567 }
1568 return bestXY;
1569 }
1570
Adam Cohen482ed822012-03-02 14:15:13 -08001571 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001572 int[] direction, ItemConfiguration currentState) {
1573 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001574 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001575 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001576 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1577
Adam Cohen8baab352012-03-20 17:39:21 -07001578 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001579
1580 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001581 c.x = mTempLocation[0];
1582 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001583 success = true;
1584
1585 }
Adam Cohen8baab352012-03-20 17:39:21 -07001586 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001587 return success;
1588 }
1589
Adam Cohena56dc102012-07-13 13:41:42 -07001590 // This method looks in the specified direction to see if there are additional views adjacent
1591 // to the current set of views in the. If there is, then these views are added to the current
1592 // set of views. This is performed iteratively, giving a cascading push behaviour.
Adam Cohen47a876d2012-03-19 13:21:41 -07001593 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen19f37922012-03-21 11:59:11 -07001594 boolean[][] occupied, View dragView, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001595 boolean found = false;
1596
Michael Jurkaa52570f2012-03-20 03:18:20 -07001597 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001598 Rect r0 = new Rect(boundingRect);
1599 Rect r1 = new Rect();
1600
Adam Cohena56dc102012-07-13 13:41:42 -07001601 // First, we consider the rect of the views that we are trying to translate
Adam Cohen47a876d2012-03-19 13:21:41 -07001602 int deltaX = 0;
1603 int deltaY = 0;
1604 if (direction[1] < 0) {
Adam Cohena56dc102012-07-13 13:41:42 -07001605 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom - 1);
Adam Cohen47a876d2012-03-19 13:21:41 -07001606 deltaY = -1;
1607 } else if (direction[1] > 0) {
Adam Cohena56dc102012-07-13 13:41:42 -07001608 r0.set(r0.left, r0.top + 1, r0.right, r0.bottom + 1);
Adam Cohen47a876d2012-03-19 13:21:41 -07001609 deltaY = 1;
1610 } else if (direction[0] < 0) {
Adam Cohena56dc102012-07-13 13:41:42 -07001611 r0.set(r0.left - 1, r0.top, r0.right - 1, r0.bottom);
Adam Cohen47a876d2012-03-19 13:21:41 -07001612 deltaX = -1;
1613 } else if (direction[0] > 0) {
Adam Cohena56dc102012-07-13 13:41:42 -07001614 r0.set(r0.left + 1, r0.top, r0.right + 1, r0.bottom);
Adam Cohen47a876d2012-03-19 13:21:41 -07001615 deltaX = 1;
1616 }
1617
Adam Cohena56dc102012-07-13 13:41:42 -07001618 // Now we see which views, if any, are being overlapped by shifting the current group
1619 // of views in the desired direction.
Adam Cohen47a876d2012-03-19 13:21:41 -07001620 for (int i = 0; i < childCount; i++) {
Adam Cohena56dc102012-07-13 13:41:42 -07001621 // We don't need to worry about views already in our group, or the current drag view.
Michael Jurkaa52570f2012-03-20 03:18:20 -07001622 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen19f37922012-03-21 11:59:11 -07001623 if (views.contains(child) || child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001624 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001625
Adam Cohen8baab352012-03-20 17:39:21 -07001626 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1627 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001628 if (Rect.intersects(r0, r1)) {
1629 if (!lp.canReorder) {
1630 return false;
1631 }
Adam Cohena56dc102012-07-13 13:41:42 -07001632 // First we verify that the view in question is at the border of the extents
1633 // of the block of items we are pushing
1634 if ((direction[0] < 0 && c.x == r0.left) ||
1635 (direction[0] > 0 && c.x == r0.right - 1) ||
1636 (direction[1] < 0 && c.y == r0.top) ||
1637 (direction[1] > 0 && c.y == r0.bottom - 1)) {
1638 boolean pushed = false;
1639 // Since the bounding rect is a course description of the region (there can
1640 // be holes at the edge of the block), we need to check to verify that a solid
1641 // piece is intersecting. This ensures that interlocking is possible.
1642 for (int x = c.x; x < c.x + c.spanX; x++) {
1643 for (int y = c.y; y < c.y + c.spanY; y++) {
1644 if (occupied[x - deltaX][y - deltaY]) {
1645 pushed = true;
1646 break;
1647 }
1648 if (pushed) break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001649 }
1650 }
Adam Cohena56dc102012-07-13 13:41:42 -07001651 if (pushed) {
1652 views.add(child);
1653 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1654 found = true;
1655 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001656 }
1657 }
1658 }
1659 return found;
1660 }
1661
Adam Cohen482ed822012-03-02 14:15:13 -08001662 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen19f37922012-03-21 11:59:11 -07001663 int[] direction, boolean push, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001664 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001665
Adam Cohen8baab352012-03-20 17:39:21 -07001666 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001667 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001668 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001669 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001670 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001671 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001672 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001673 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001674 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001675 }
1676 }
Adam Cohen8baab352012-03-20 17:39:21 -07001677
1678 @SuppressWarnings("unchecked")
1679 ArrayList<View> dup = (ArrayList<View>) views.clone();
1680 // We try and expand the group of views in the direction vector passed, based on
1681 // whether they are physically adjacent, ie. based on "push mechanics".
Adam Cohen19f37922012-03-21 11:59:11 -07001682 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied, dragView,
Adam Cohen8baab352012-03-20 17:39:21 -07001683 currentState)) {
1684 }
1685
1686 // Mark the occupied state as false for the group of views we want to move.
1687 for (View v: dup) {
1688 CellAndSpan c = currentState.map.get(v);
1689 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1690 }
1691
Adam Cohen47a876d2012-03-19 13:21:41 -07001692 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1693 int top = boundingRect.top;
1694 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001695 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001696 // for interlocking.
Adam Cohen8baab352012-03-20 17:39:21 -07001697 for (View v: dup) {
1698 CellAndSpan c = currentState.map.get(v);
1699 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001700 }
1701
Adam Cohen482ed822012-03-02 14:15:13 -08001702 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1703
Adam Cohen8baab352012-03-20 17:39:21 -07001704 if (push) {
1705 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1706 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1707 } else {
1708 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1709 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1710 }
Adam Cohen482ed822012-03-02 14:15:13 -08001711
Adam Cohen8baab352012-03-20 17:39:21 -07001712 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001713 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001714 int deltaX = mTempLocation[0] - boundingRect.left;
1715 int deltaY = mTempLocation[1] - boundingRect.top;
1716 for (View v: dup) {
1717 CellAndSpan c = currentState.map.get(v);
1718 c.x += deltaX;
1719 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001720 }
1721 success = true;
1722 }
Adam Cohen8baab352012-03-20 17:39:21 -07001723
1724 // In either case, we set the occupied array as marked for the location of the views
1725 for (View v: dup) {
1726 CellAndSpan c = currentState.map.get(v);
1727 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001728 }
1729 return success;
1730 }
1731
1732 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1733 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1734 }
1735
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001736 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1737 // to push items in each of the cardinal directions, in an order based on the direction vector
1738 // passed.
1739 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1740 int[] direction, View ignoreView, ItemConfiguration solution) {
1741 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
1742 // If the direction vector has two non-zero components, we try pushing
1743 // separately in each of the components.
1744 int temp = direction[1];
1745 direction[1] = 0;
1746 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1747 ignoreView, solution)) {
1748 return true;
1749 }
1750 direction[1] = temp;
1751 temp = direction[0];
1752 direction[0] = 0;
1753 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1754 ignoreView, solution)) {
1755 return true;
1756 }
1757 // Revert the direction
1758 direction[0] = temp;
1759
1760 // Now we try pushing in each component of the opposite direction
1761 direction[0] *= -1;
1762 direction[1] *= -1;
1763 temp = direction[1];
1764 direction[1] = 0;
1765 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1766 ignoreView, solution)) {
1767 return true;
1768 }
1769
1770 direction[1] = temp;
1771 temp = direction[0];
1772 direction[0] = 0;
1773 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1774 ignoreView, solution)) {
1775 return true;
1776 }
1777 // revert the direction
1778 direction[0] = temp;
1779 direction[0] *= -1;
1780 direction[1] *= -1;
1781
1782 } else {
1783 // If the direction vector has a single non-zero component, we push first in the
1784 // direction of the vector
1785 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1786 ignoreView, solution)) {
1787 return true;
1788 }
1789
1790 // Then we try the opposite direction
1791 direction[0] *= -1;
1792 direction[1] *= -1;
1793 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1794 ignoreView, solution)) {
1795 return true;
1796 }
1797 // Switch the direction back
1798 direction[0] *= -1;
1799 direction[1] *= -1;
1800
1801 // If we have failed to find a push solution with the above, then we try
1802 // to find a solution by pushing along the perpendicular axis.
1803
1804 // Swap the components
1805 int temp = direction[1];
1806 direction[1] = direction[0];
1807 direction[0] = temp;
1808 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1809 ignoreView, solution)) {
1810 return true;
1811 }
1812
1813 // Then we try the opposite direction
1814 direction[0] *= -1;
1815 direction[1] *= -1;
1816 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1817 ignoreView, solution)) {
1818 return true;
1819 }
1820 // Switch the direction back
1821 direction[0] *= -1;
1822 direction[1] *= -1;
1823
1824 // Swap the components back
1825 temp = direction[1];
1826 direction[1] = direction[0];
1827 direction[0] = temp;
1828 }
1829 return false;
1830 }
1831
Adam Cohen482ed822012-03-02 14:15:13 -08001832 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001833 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001834 // Return early if get invalid cell positions
1835 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001836
Adam Cohen8baab352012-03-20 17:39:21 -07001837 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001838 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001839
Adam Cohen8baab352012-03-20 17:39:21 -07001840 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001841 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001842 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001843 if (c != null) {
1844 c.x = cellX;
1845 c.y = cellY;
1846 }
Adam Cohen482ed822012-03-02 14:15:13 -08001847 }
Adam Cohen482ed822012-03-02 14:15:13 -08001848 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1849 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001850 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001851 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001852 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001853 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001854 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001855 if (Rect.intersects(r0, r1)) {
1856 if (!lp.canReorder) {
1857 return false;
1858 }
1859 mIntersectingViews.add(child);
1860 }
1861 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001862
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001863 // First we try to find a solution which respects the push mechanic. That is,
1864 // we try to find a solution such that no displaced item travels through another item
1865 // without also displacing that item.
1866 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001867 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001868 return true;
1869 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001870
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001871 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohen19f37922012-03-21 11:59:11 -07001872 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, ignoreView,
1873 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001874 return true;
1875 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001876
Adam Cohen482ed822012-03-02 14:15:13 -08001877 // Ok, they couldn't move as a block, let's move them individually
1878 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001879 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001880 return false;
1881 }
1882 }
1883 return true;
1884 }
1885
1886 /*
1887 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1888 * the provided point and the provided cell
1889 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001890 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001891 double angle = Math.atan(((float) deltaY) / deltaX);
1892
1893 result[0] = 0;
1894 result[1] = 0;
1895 if (Math.abs(Math.cos(angle)) > 0.5f) {
1896 result[0] = (int) Math.signum(deltaX);
1897 }
1898 if (Math.abs(Math.sin(angle)) > 0.5f) {
1899 result[1] = (int) Math.signum(deltaY);
1900 }
1901 }
1902
Adam Cohen8baab352012-03-20 17:39:21 -07001903 private void copyOccupiedArray(boolean[][] occupied) {
1904 for (int i = 0; i < mCountX; i++) {
1905 for (int j = 0; j < mCountY; j++) {
1906 occupied[i][j] = mOccupied[i][j];
1907 }
1908 }
1909 }
1910
Adam Cohen482ed822012-03-02 14:15:13 -08001911 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1912 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001913 // Copy the current state into the solution. This solution will be manipulated as necessary.
1914 copyCurrentStateToSolution(solution, false);
1915 // Copy the current occupied array into the temporary occupied array. This array will be
1916 // manipulated as necessary to find a solution.
1917 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001918
1919 // We find the nearest cell into which we would place the dragged item, assuming there's
1920 // nothing in its way.
1921 int result[] = new int[2];
1922 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1923
1924 boolean success = false;
1925 // First we try the exact nearest position of the item being dragged,
1926 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001927 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1928 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001929
1930 if (!success) {
1931 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1932 // x, then 1 in y etc.
1933 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1934 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1935 dragView, false, solution);
1936 } else if (spanY > minSpanY) {
1937 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1938 dragView, true, solution);
1939 }
1940 solution.isSolution = false;
1941 } else {
1942 solution.isSolution = true;
1943 solution.dragViewX = result[0];
1944 solution.dragViewY = result[1];
1945 solution.dragViewSpanX = spanX;
1946 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001947 }
1948 return solution;
1949 }
1950
1951 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001952 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001953 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001954 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001955 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001956 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001957 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001958 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001959 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001960 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001961 }
Adam Cohen8baab352012-03-20 17:39:21 -07001962 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001963 }
1964 }
1965
1966 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1967 for (int i = 0; i < mCountX; i++) {
1968 for (int j = 0; j < mCountY; j++) {
1969 mTmpOccupied[i][j] = false;
1970 }
1971 }
1972
Michael Jurkaa52570f2012-03-20 03:18:20 -07001973 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001974 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001975 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001976 if (child == dragView) continue;
1977 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001978 CellAndSpan c = solution.map.get(child);
1979 if (c != null) {
1980 lp.tmpCellX = c.x;
1981 lp.tmpCellY = c.y;
1982 lp.cellHSpan = c.spanX;
1983 lp.cellVSpan = c.spanY;
1984 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001985 }
1986 }
1987 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1988 solution.dragViewSpanY, mTmpOccupied, true);
1989 }
1990
1991 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1992 commitDragView) {
1993
1994 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1995 for (int i = 0; i < mCountX; i++) {
1996 for (int j = 0; j < mCountY; j++) {
1997 occupied[i][j] = false;
1998 }
1999 }
2000
Michael Jurkaa52570f2012-03-20 03:18:20 -07002001 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002002 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002003 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002004 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002005 CellAndSpan c = solution.map.get(child);
2006 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002007 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2008 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002009 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002010 }
2011 }
2012 if (commitDragView) {
2013 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2014 solution.dragViewSpanY, occupied, true);
2015 }
2016 }
2017
Adam Cohen19f37922012-03-21 11:59:11 -07002018 // This method starts or changes the reorder hint animations
2019 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2020 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002021 for (int i = 0; i < childCount; i++) {
2022 View child = mShortcutsAndWidgets.getChildAt(i);
2023 if (child == dragView) continue;
2024 CellAndSpan c = solution.map.get(child);
2025 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2026 if (c != null) {
2027 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2028 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002029 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002030 }
2031 }
2032 }
2033
2034 // Class which represents the reorder hint animations. These animations show that an item is
2035 // in a temporary state, and hint at where the item will return to.
2036 class ReorderHintAnimation {
2037 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002038 float finalDeltaX;
2039 float finalDeltaY;
2040 float initDeltaX;
2041 float initDeltaY;
2042 float finalScale;
2043 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002044 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002045 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002046
2047 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2048 int spanX, int spanY) {
2049 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2050 final int x0 = mTmpPoint[0];
2051 final int y0 = mTmpPoint[1];
2052 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2053 final int x1 = mTmpPoint[0];
2054 final int y1 = mTmpPoint[1];
2055 final int dX = x1 - x0;
2056 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002057 finalDeltaX = 0;
2058 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002059 if (dX == dY && dX == 0) {
2060 } else {
2061 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002062 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002063 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002064 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002065 } else {
2066 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002067 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002068 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002069 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002070 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002071 }
2072 }
Adam Cohend024f982012-05-23 18:26:45 -07002073 initDeltaX = child.getTranslationX();
2074 initDeltaY = child.getTranslationY();
2075 finalScale = 1.0f - 4.0f / child.getWidth();
2076 initScale = child.getScaleX();
2077
Brandon Keely50e6e562012-05-08 16:28:49 -07002078 child.setPivotY(child.getMeasuredHeight() * 0.5f);
2079 child.setPivotX(child.getMeasuredWidth() * 0.5f);
Adam Cohen19f37922012-03-21 11:59:11 -07002080 this.child = child;
2081 }
2082
Adam Cohend024f982012-05-23 18:26:45 -07002083 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002084 if (mShakeAnimators.containsKey(child)) {
2085 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002086 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002087 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002088 if (finalDeltaX == 0 && finalDeltaY == 0) {
2089 completeAnimationImmediately();
2090 return;
2091 }
Adam Cohen19f37922012-03-21 11:59:11 -07002092 }
Adam Cohend024f982012-05-23 18:26:45 -07002093 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002094 return;
2095 }
Michael Jurka2ecf9952012-06-18 12:52:28 -07002096 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002097 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002098 va.setRepeatMode(ValueAnimator.REVERSE);
2099 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002100 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002101 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002102 va.addUpdateListener(new AnimatorUpdateListener() {
2103 @Override
2104 public void onAnimationUpdate(ValueAnimator animation) {
2105 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002106 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2107 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002108 child.setTranslationX(x);
2109 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002110 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002111 child.setScaleX(s);
2112 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002113 }
2114 });
2115 va.addListener(new AnimatorListenerAdapter() {
2116 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002117 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002118 initDeltaX = 0;
2119 initDeltaY = 0;
2120 initScale = 1.0f;
Adam Cohen19f37922012-03-21 11:59:11 -07002121 }
2122 });
Adam Cohen19f37922012-03-21 11:59:11 -07002123 mShakeAnimators.put(child, this);
2124 va.start();
2125 }
2126
Adam Cohend024f982012-05-23 18:26:45 -07002127 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002128 if (a != null) {
2129 a.cancel();
2130 }
Adam Cohen19f37922012-03-21 11:59:11 -07002131 }
Adam Cohene7587d22012-05-24 18:50:02 -07002132
Brandon Keely50e6e562012-05-08 16:28:49 -07002133 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002134 if (a != null) {
2135 a.cancel();
2136 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002137
Michael Jurka2ecf9952012-06-18 12:52:28 -07002138 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002139 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002140 s.playTogether(
Michael Jurka2ecf9952012-06-18 12:52:28 -07002141 LauncherAnimUtils.ofFloat(child, "scaleX", 1f),
2142 LauncherAnimUtils.ofFloat(child, "scaleY", 1f),
2143 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2144 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002145 );
2146 s.setDuration(REORDER_ANIMATION_DURATION);
2147 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2148 s.start();
2149 }
Adam Cohen19f37922012-03-21 11:59:11 -07002150 }
2151
2152 private void completeAndClearReorderHintAnimations() {
2153 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002154 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002155 }
2156 mShakeAnimators.clear();
2157 }
2158
Adam Cohen482ed822012-03-02 14:15:13 -08002159 private void commitTempPlacement() {
2160 for (int i = 0; i < mCountX; i++) {
2161 for (int j = 0; j < mCountY; j++) {
2162 mOccupied[i][j] = mTmpOccupied[i][j];
2163 }
2164 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002165 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002166 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002167 View child = mShortcutsAndWidgets.getChildAt(i);
2168 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2169 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002170 // We do a null check here because the item info can be null in the case of the
2171 // AllApps button in the hotseat.
2172 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002173 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2174 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2175 info.requiresDbUpdate = true;
2176 }
Adam Cohen2acce882012-03-28 19:03:19 -07002177 info.cellX = lp.cellX = lp.tmpCellX;
2178 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002179 info.spanX = lp.cellHSpan;
2180 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002181 }
Adam Cohen482ed822012-03-02 14:15:13 -08002182 }
Adam Cohen2acce882012-03-28 19:03:19 -07002183 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002184 }
2185
2186 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002187 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002188 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002189 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002190 lp.useTmpCoords = useTempCoords;
2191 }
2192 }
2193
Adam Cohen482ed822012-03-02 14:15:13 -08002194 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2195 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2196 int[] result = new int[2];
2197 int[] resultSpan = new int[2];
2198 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2199 resultSpan);
2200 if (result[0] >= 0 && result[1] >= 0) {
2201 copyCurrentStateToSolution(solution, false);
2202 solution.dragViewX = result[0];
2203 solution.dragViewY = result[1];
2204 solution.dragViewSpanX = resultSpan[0];
2205 solution.dragViewSpanY = resultSpan[1];
2206 solution.isSolution = true;
2207 } else {
2208 solution.isSolution = false;
2209 }
2210 return solution;
2211 }
2212
2213 public void prepareChildForDrag(View child) {
2214 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002215 }
2216
Adam Cohen19f37922012-03-21 11:59:11 -07002217 /* This seems like it should be obvious and straight-forward, but when the direction vector
2218 needs to match with the notion of the dragView pushing other views, we have to employ
2219 a slightly more subtle notion of the direction vector. The question is what two points is
2220 the vector between? The center of the dragView and its desired destination? Not quite, as
2221 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2222 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2223 or right, which helps make pushing feel right.
2224 */
2225 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2226 int spanY, View dragView, int[] resultDirection) {
2227 int[] targetDestination = new int[2];
2228
2229 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2230 Rect dragRect = new Rect();
2231 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2232 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2233
2234 Rect dropRegionRect = new Rect();
2235 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2236 dragView, dropRegionRect, mIntersectingViews);
2237
2238 int dropRegionSpanX = dropRegionRect.width();
2239 int dropRegionSpanY = dropRegionRect.height();
2240
2241 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2242 dropRegionRect.height(), dropRegionRect);
2243
2244 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2245 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2246
2247 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2248 deltaX = 0;
2249 }
2250 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2251 deltaY = 0;
2252 }
2253
2254 if (deltaX == 0 && deltaY == 0) {
2255 // No idea what to do, give a random direction.
2256 resultDirection[0] = 1;
2257 resultDirection[1] = 0;
2258 } else {
2259 computeDirectionVector(deltaX, deltaY, resultDirection);
2260 }
2261 }
2262
2263 // For a given cell and span, fetch the set of views intersecting the region.
2264 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2265 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2266 if (boundingRect != null) {
2267 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2268 }
2269 intersectingViews.clear();
2270 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2271 Rect r1 = new Rect();
2272 final int count = mShortcutsAndWidgets.getChildCount();
2273 for (int i = 0; i < count; i++) {
2274 View child = mShortcutsAndWidgets.getChildAt(i);
2275 if (child == dragView) continue;
2276 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2277 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2278 if (Rect.intersects(r0, r1)) {
2279 mIntersectingViews.add(child);
2280 if (boundingRect != null) {
2281 boundingRect.union(r1);
2282 }
2283 }
2284 }
2285 }
2286
2287 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2288 View dragView, int[] result) {
2289 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2290 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2291 mIntersectingViews);
2292 return !mIntersectingViews.isEmpty();
2293 }
2294
2295 void revertTempState() {
2296 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2297 final int count = mShortcutsAndWidgets.getChildCount();
2298 for (int i = 0; i < count; i++) {
2299 View child = mShortcutsAndWidgets.getChildAt(i);
2300 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2301 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2302 lp.tmpCellX = lp.cellX;
2303 lp.tmpCellY = lp.cellY;
2304 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2305 0, false, false);
2306 }
2307 }
2308 completeAndClearReorderHintAnimations();
2309 setItemPlacementDirty(false);
2310 }
2311
Adam Cohenbebf0422012-04-11 18:06:28 -07002312 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2313 View dragView, int[] direction, boolean commit) {
2314 int[] pixelXY = new int[2];
2315 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2316
2317 // First we determine if things have moved enough to cause a different layout
2318 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2319 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2320
2321 setUseTempCoords(true);
2322 if (swapSolution != null && swapSolution.isSolution) {
2323 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2324 // committing anything or animating anything as we just want to determine if a solution
2325 // exists
2326 copySolutionToTempState(swapSolution, dragView);
2327 setItemPlacementDirty(true);
2328 animateItemsToSolution(swapSolution, dragView, commit);
2329
2330 if (commit) {
2331 commitTempPlacement();
2332 completeAndClearReorderHintAnimations();
2333 setItemPlacementDirty(false);
2334 } else {
2335 beginOrAdjustHintAnimations(swapSolution, dragView,
2336 REORDER_ANIMATION_DURATION);
2337 }
2338 mShortcutsAndWidgets.requestLayout();
2339 }
2340 return swapSolution.isSolution;
2341 }
2342
Adam Cohen482ed822012-03-02 14:15:13 -08002343 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2344 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002345 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002346 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002347
2348 if (resultSpan == null) {
2349 resultSpan = new int[2];
2350 }
2351
Adam Cohen19f37922012-03-21 11:59:11 -07002352 // When we are checking drop validity or actually dropping, we don't recompute the
2353 // direction vector, since we want the solution to match the preview, and it's possible
2354 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002355 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2356 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002357 mDirectionVector[0] = mPreviousReorderDirection[0];
2358 mDirectionVector[1] = mPreviousReorderDirection[1];
2359 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002360 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2361 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2362 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002363 }
2364 } else {
2365 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2366 mPreviousReorderDirection[0] = mDirectionVector[0];
2367 mPreviousReorderDirection[1] = mDirectionVector[1];
2368 }
2369
Adam Cohen482ed822012-03-02 14:15:13 -08002370 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2371 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2372
2373 // We attempt the approach which doesn't shuffle views at all
2374 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2375 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2376
2377 ItemConfiguration finalSolution = null;
2378 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2379 finalSolution = swapSolution;
2380 } else if (noShuffleSolution.isSolution) {
2381 finalSolution = noShuffleSolution;
2382 }
2383
2384 boolean foundSolution = true;
2385 if (!DESTRUCTIVE_REORDER) {
2386 setUseTempCoords(true);
2387 }
2388
2389 if (finalSolution != null) {
2390 result[0] = finalSolution.dragViewX;
2391 result[1] = finalSolution.dragViewY;
2392 resultSpan[0] = finalSolution.dragViewSpanX;
2393 resultSpan[1] = finalSolution.dragViewSpanY;
2394
2395 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2396 // committing anything or animating anything as we just want to determine if a solution
2397 // exists
2398 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2399 if (!DESTRUCTIVE_REORDER) {
2400 copySolutionToTempState(finalSolution, dragView);
2401 }
2402 setItemPlacementDirty(true);
2403 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2404
Adam Cohen19f37922012-03-21 11:59:11 -07002405 if (!DESTRUCTIVE_REORDER &&
2406 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002407 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002408 completeAndClearReorderHintAnimations();
2409 setItemPlacementDirty(false);
2410 } else {
2411 beginOrAdjustHintAnimations(finalSolution, dragView,
2412 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002413 }
2414 }
2415 } else {
2416 foundSolution = false;
2417 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2418 }
2419
2420 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2421 setUseTempCoords(false);
2422 }
Adam Cohen482ed822012-03-02 14:15:13 -08002423
Michael Jurkaa52570f2012-03-20 03:18:20 -07002424 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002425 return result;
2426 }
2427
Adam Cohen19f37922012-03-21 11:59:11 -07002428 void setItemPlacementDirty(boolean dirty) {
2429 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002430 }
Adam Cohen19f37922012-03-21 11:59:11 -07002431 boolean isItemPlacementDirty() {
2432 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002433 }
2434
2435 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002436 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002437 boolean isSolution = false;
2438 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2439
2440 int area() {
2441 return dragViewSpanX * dragViewSpanY;
2442 }
Adam Cohen8baab352012-03-20 17:39:21 -07002443 }
2444
2445 private class CellAndSpan {
2446 int x, y;
2447 int spanX, spanY;
2448
2449 public CellAndSpan(int x, int y, int spanX, int spanY) {
2450 this.x = x;
2451 this.y = y;
2452 this.spanX = spanX;
2453 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002454 }
2455 }
2456
Adam Cohendf035382011-04-11 17:22:04 -07002457 /**
2458 * Find a vacant area that will fit the given bounds nearest the requested
2459 * cell location. Uses Euclidean distance to score multiple vacant areas.
2460 *
2461 * @param pixelX The X location at which you want to search for a vacant area.
2462 * @param pixelY The Y location at which you want to search for a vacant area.
2463 * @param spanX Horizontal span of the object.
2464 * @param spanY Vertical span of the object.
2465 * @param ignoreView Considers space occupied by this view as unoccupied
2466 * @param result Previously returned value to possibly recycle.
2467 * @return The X, Y cell of a vacant area that can contain this object,
2468 * nearest the requested location.
2469 */
2470 int[] findNearestVacantArea(
2471 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2472 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2473 }
2474
2475 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002476 * Find a vacant area that will fit the given bounds nearest the requested
2477 * cell location. Uses Euclidean distance to score multiple vacant areas.
2478 *
2479 * @param pixelX The X location at which you want to search for a vacant area.
2480 * @param pixelY The Y location at which you want to search for a vacant area.
2481 * @param minSpanX The minimum horizontal span required
2482 * @param minSpanY The minimum vertical span required
2483 * @param spanX Horizontal span of the object.
2484 * @param spanY Vertical span of the object.
2485 * @param ignoreView Considers space occupied by this view as unoccupied
2486 * @param result Previously returned value to possibly recycle.
2487 * @return The X, Y cell of a vacant area that can contain this object,
2488 * nearest the requested location.
2489 */
2490 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2491 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002492 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2493 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002494 }
2495
2496 /**
Adam Cohendf035382011-04-11 17:22:04 -07002497 * Find a starting cell position that will fit the given bounds nearest the requested
2498 * cell location. Uses Euclidean distance to score multiple vacant areas.
2499 *
2500 * @param pixelX The X location at which you want to search for a vacant area.
2501 * @param pixelY The Y location at which you want to search for a vacant area.
2502 * @param spanX Horizontal span of the object.
2503 * @param spanY Vertical span of the object.
2504 * @param ignoreView Considers space occupied by this view as unoccupied
2505 * @param result Previously returned value to possibly recycle.
2506 * @return The X, Y cell of a vacant area that can contain this object,
2507 * nearest the requested location.
2508 */
2509 int[] findNearestArea(
2510 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2511 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2512 }
2513
Michael Jurka0280c3b2010-09-17 15:00:07 -07002514 boolean existsEmptyCell() {
2515 return findCellForSpan(null, 1, 1);
2516 }
2517
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002519 * Finds the upper-left coordinate of the first rectangle in the grid that can
2520 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2521 * then this method will only return coordinates for rectangles that contain the cell
2522 * (intersectX, intersectY)
2523 *
2524 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2525 * can be found.
2526 * @param spanX The horizontal span of the cell we want to find.
2527 * @param spanY The vertical span of the cell we want to find.
2528 *
2529 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002530 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002531 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002532 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002533 }
2534
2535 /**
2536 * Like above, but ignores any cells occupied by the item "ignoreView"
2537 *
2538 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2539 * can be found.
2540 * @param spanX The horizontal span of the cell we want to find.
2541 * @param spanY The vertical span of the cell we want to find.
2542 * @param ignoreView The home screen item we should treat as not occupying any space
2543 * @return
2544 */
2545 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002546 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2547 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002548 }
2549
2550 /**
2551 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2552 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2553 *
2554 * @param spanX The horizontal span of the cell we want to find.
2555 * @param spanY The vertical span of the cell we want to find.
2556 * @param ignoreView The home screen item we should treat as not occupying any space
2557 * @param intersectX The X coordinate of the cell that we should try to overlap
2558 * @param intersectX The Y coordinate of the cell that we should try to overlap
2559 *
2560 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2561 */
2562 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2563 int intersectX, int intersectY) {
2564 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002565 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002566 }
2567
2568 /**
2569 * The superset of the above two methods
2570 */
2571 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002572 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002573 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002574 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002575
Michael Jurka28750fb2010-09-24 17:43:49 -07002576 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002577 while (true) {
2578 int startX = 0;
2579 if (intersectX >= 0) {
2580 startX = Math.max(startX, intersectX - (spanX - 1));
2581 }
2582 int endX = mCountX - (spanX - 1);
2583 if (intersectX >= 0) {
2584 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2585 }
2586 int startY = 0;
2587 if (intersectY >= 0) {
2588 startY = Math.max(startY, intersectY - (spanY - 1));
2589 }
2590 int endY = mCountY - (spanY - 1);
2591 if (intersectY >= 0) {
2592 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2593 }
2594
Winson Chungbbc60d82010-11-11 16:34:41 -08002595 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002596 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002597 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002598 for (int i = 0; i < spanX; i++) {
2599 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002600 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002601 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002602 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002603 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002604 continue inner;
2605 }
2606 }
2607 }
2608 if (cellXY != null) {
2609 cellXY[0] = x;
2610 cellXY[1] = y;
2611 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002612 foundCell = true;
2613 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002614 }
2615 }
2616 if (intersectX == -1 && intersectY == -1) {
2617 break;
2618 } else {
2619 // if we failed to find anything, try again but without any requirements of
2620 // intersecting
2621 intersectX = -1;
2622 intersectY = -1;
2623 continue;
2624 }
2625 }
2626
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002627 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002628 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002629 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002630 }
2631
2632 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002633 * A drag event has begun over this layout.
2634 * It may have begun over this layout (in which case onDragChild is called first),
2635 * or it may have begun on another layout.
2636 */
2637 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002638 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002639 mDragging = true;
2640 }
2641
2642 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002643 * Called when drag has left this CellLayout or has been completed (successfully or not)
2644 */
2645 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002646 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002647 // This can actually be called when we aren't in a drag, e.g. when adding a new
2648 // item to this layout via the customize drawer.
2649 // Guard against that case.
2650 if (mDragging) {
2651 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002652 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002653
2654 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002655 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002656 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2657 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002658 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002659 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002660 }
2661
2662 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002663 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002664 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002665 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002666 *
2667 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002668 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002669 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002670 if (child != null) {
2671 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002672 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002673 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002675 }
2676
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002677 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002678 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002679 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680 * @param cellX X coordinate of upper left corner expressed as a cell position
2681 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002682 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002683 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002684 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002685 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002686 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002687 final int cellWidth = mCellWidth;
2688 final int cellHeight = mCellHeight;
2689 final int widthGap = mWidthGap;
2690 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002691
Winson Chung4b825dcd2011-06-19 12:41:22 -07002692 final int hStartPadding = getPaddingLeft();
2693 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002695 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2696 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2697
2698 int x = hStartPadding + cellX * (cellWidth + widthGap);
2699 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002700
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002701 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002702 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002703
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002704 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002705 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002706 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002707 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002708 * @param width Width in pixels
2709 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002710 * @param result An array of length 2 in which to store the result (may be null).
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002711 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002712 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002713 return rectToCell(getResources(), width, height, result);
2714 }
2715
2716 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002717 // Always assume we're working with the smallest span to make sure we
2718 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002719 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2720 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002721 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002722
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002723 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002724 int spanX = (int) Math.ceil(width / (float) smallerSize);
2725 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002726
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002727 if (result == null) {
2728 return new int[] { spanX, spanY };
2729 }
2730 result[0] = spanX;
2731 result[1] = spanY;
2732 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002733 }
2734
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002735 public int[] cellSpansToSize(int hSpans, int vSpans) {
2736 int[] size = new int[2];
2737 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2738 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2739 return size;
2740 }
2741
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002742 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002743 * Calculate the grid spans needed to fit given item
2744 */
2745 public void calculateSpans(ItemInfo info) {
2746 final int minWidth;
2747 final int minHeight;
2748
2749 if (info instanceof LauncherAppWidgetInfo) {
2750 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2751 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2752 } else if (info instanceof PendingAddWidgetInfo) {
2753 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2754 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2755 } else {
2756 // It's not a widget, so it must be 1x1
2757 info.spanX = info.spanY = 1;
2758 return;
2759 }
2760 int[] spans = rectToCell(minWidth, minHeight, null);
2761 info.spanX = spans[0];
2762 info.spanY = spans[1];
2763 }
2764
2765 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 * Find the first vacant cell, if there is one.
2767 *
2768 * @param vacant Holds the x and y coordinate of the vacant cell
2769 * @param spanX Horizontal cell span.
2770 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002771 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002772 * @return True if a vacant cell was found
2773 */
2774 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775
Michael Jurka0280c3b2010-09-17 15:00:07 -07002776 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002777 }
2778
2779 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2780 int xCount, int yCount, boolean[][] occupied) {
2781
Adam Cohen2801caf2011-05-13 20:57:39 -07002782 for (int y = 0; y < yCount; y++) {
2783 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784 boolean available = !occupied[x][y];
2785out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2786 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2787 available = available && !occupied[i][j];
2788 if (!available) break out;
2789 }
2790 }
2791
2792 if (available) {
2793 vacant[0] = x;
2794 vacant[1] = y;
2795 return true;
2796 }
2797 }
2798 }
2799
2800 return false;
2801 }
2802
Michael Jurka0280c3b2010-09-17 15:00:07 -07002803 private void clearOccupiedCells() {
2804 for (int x = 0; x < mCountX; x++) {
2805 for (int y = 0; y < mCountY; y++) {
2806 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002807 }
2808 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002810
Adam Cohend41fbf52012-02-16 23:53:59 -08002811 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002812 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002813 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002814 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002815
Adam Cohend4844c32011-02-18 19:25:06 -08002816 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002817 markCellsAsOccupiedForView(view, mOccupied);
2818 }
2819 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002820 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002821 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002822 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002823 }
2824
Adam Cohend4844c32011-02-18 19:25:06 -08002825 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002826 markCellsAsUnoccupiedForView(view, mOccupied);
2827 }
2828 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002829 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002830 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002831 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002832 }
2833
Adam Cohen482ed822012-03-02 14:15:13 -08002834 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2835 boolean value) {
2836 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002837 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2838 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002839 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840 }
2841 }
2842 }
2843
Adam Cohen2801caf2011-05-13 20:57:39 -07002844 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002845 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002846 (Math.max((mCountX - 1), 0) * mWidthGap);
2847 }
2848
2849 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002850 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002851 (Math.max((mCountY - 1), 0) * mHeightGap);
2852 }
2853
Michael Jurka66d72172011-04-12 16:29:25 -07002854 public boolean isOccupied(int x, int y) {
2855 if (x < mCountX && y < mCountY) {
2856 return mOccupied[x][y];
2857 } else {
2858 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2859 }
2860 }
2861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002862 @Override
2863 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2864 return new CellLayout.LayoutParams(getContext(), attrs);
2865 }
2866
2867 @Override
2868 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2869 return p instanceof CellLayout.LayoutParams;
2870 }
2871
2872 @Override
2873 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2874 return new CellLayout.LayoutParams(p);
2875 }
2876
Winson Chungaafa03c2010-06-11 17:34:16 -07002877 public static class CellLayoutAnimationController extends LayoutAnimationController {
2878 public CellLayoutAnimationController(Animation animation, float delay) {
2879 super(animation, delay);
2880 }
2881
2882 @Override
2883 protected long getDelayForView(View view) {
2884 return (int) (Math.random() * 150);
2885 }
2886 }
2887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002888 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2889 /**
2890 * Horizontal location of the item in the grid.
2891 */
2892 @ViewDebug.ExportedProperty
2893 public int cellX;
2894
2895 /**
2896 * Vertical location of the item in the grid.
2897 */
2898 @ViewDebug.ExportedProperty
2899 public int cellY;
2900
2901 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002902 * Temporary horizontal location of the item in the grid during reorder
2903 */
2904 public int tmpCellX;
2905
2906 /**
2907 * Temporary vertical location of the item in the grid during reorder
2908 */
2909 public int tmpCellY;
2910
2911 /**
2912 * Indicates that the temporary coordinates should be used to layout the items
2913 */
2914 public boolean useTmpCoords;
2915
2916 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002917 * Number of cells spanned horizontally by the item.
2918 */
2919 @ViewDebug.ExportedProperty
2920 public int cellHSpan;
2921
2922 /**
2923 * Number of cells spanned vertically by the item.
2924 */
2925 @ViewDebug.ExportedProperty
2926 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002927
Adam Cohen1b607ed2011-03-03 17:26:50 -08002928 /**
2929 * Indicates whether the item will set its x, y, width and height parameters freely,
2930 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2931 */
Adam Cohend4844c32011-02-18 19:25:06 -08002932 public boolean isLockedToGrid = true;
2933
Adam Cohen482ed822012-03-02 14:15:13 -08002934 /**
2935 * Indicates whether this item can be reordered. Always true except in the case of the
2936 * the AllApps button.
2937 */
2938 public boolean canReorder = true;
2939
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940 // X coordinate of the view in the layout.
2941 @ViewDebug.ExportedProperty
2942 int x;
2943 // Y coordinate of the view in the layout.
2944 @ViewDebug.ExportedProperty
2945 int y;
2946
Romain Guy84f296c2009-11-04 15:00:44 -08002947 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002948
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 public LayoutParams(Context c, AttributeSet attrs) {
2950 super(c, attrs);
2951 cellHSpan = 1;
2952 cellVSpan = 1;
2953 }
2954
2955 public LayoutParams(ViewGroup.LayoutParams source) {
2956 super(source);
2957 cellHSpan = 1;
2958 cellVSpan = 1;
2959 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002960
2961 public LayoutParams(LayoutParams source) {
2962 super(source);
2963 this.cellX = source.cellX;
2964 this.cellY = source.cellY;
2965 this.cellHSpan = source.cellHSpan;
2966 this.cellVSpan = source.cellVSpan;
2967 }
2968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002970 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 this.cellX = cellX;
2972 this.cellY = cellY;
2973 this.cellHSpan = cellHSpan;
2974 this.cellVSpan = cellVSpan;
2975 }
2976
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002977 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002978 if (isLockedToGrid) {
2979 final int myCellHSpan = cellHSpan;
2980 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002981 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2982 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002983
Adam Cohend4844c32011-02-18 19:25:06 -08002984 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2985 leftMargin - rightMargin;
2986 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2987 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002988 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2989 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002990 }
2991 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002992
Winson Chungaafa03c2010-06-11 17:34:16 -07002993 public String toString() {
2994 return "(" + this.cellX + ", " + this.cellY + ")";
2995 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002996
2997 public void setWidth(int width) {
2998 this.width = width;
2999 }
3000
3001 public int getWidth() {
3002 return width;
3003 }
3004
3005 public void setHeight(int height) {
3006 this.height = height;
3007 }
3008
3009 public int getHeight() {
3010 return height;
3011 }
3012
3013 public void setX(int x) {
3014 this.x = x;
3015 }
3016
3017 public int getX() {
3018 return x;
3019 }
3020
3021 public void setY(int y) {
3022 this.y = y;
3023 }
3024
3025 public int getY() {
3026 return y;
3027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 }
3029
Michael Jurka0280c3b2010-09-17 15:00:07 -07003030 // This class stores info for two purposes:
3031 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3032 // its spanX, spanY, and the screen it is on
3033 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3034 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3035 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003036 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003037 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003038 int cellX = -1;
3039 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040 int spanX;
3041 int spanY;
3042 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07003043 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003045 @Override
3046 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003047 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3048 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049 }
3050 }
Michael Jurkad771c962011-08-09 15:00:48 -07003051
3052 public boolean lastDownOnOccupiedCell() {
3053 return mLastDownOnOccupiedCell;
3054 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003055}