blob: 284db1bdbeef1a769529903e0ad57936aaa5d8b9 [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;
Chet Haase00397b12010-10-07 11:13:10 -070022import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070028import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080030import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
Adam Cohenb5ba0972011-09-07 18:02:31 -070033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080036import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070037import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070038import android.graphics.drawable.NinePatchDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070040import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.view.MotionEvent;
42import android.view.View;
43import android.view.ViewDebug;
44import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070046import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Adam Cohen66396872011-04-15 17:50:36 -070049import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070050import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070051
Adam Cohen69ce2e52011-07-03 19:25:21 -070052import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070054import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080055import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056
Michael Jurkabdb5c532011-02-01 15:05:06 -080057public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070058 static final String TAG = "CellLayout";
59
Adam Cohen2acce882012-03-28 19:03:19 -070060 private Launcher mLauncher;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 private int mCellWidth;
62 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070063
Adam Cohend22015c2010-07-26 22:02:18 -070064 private int mCountX;
65 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
Adam Cohen234c4cd2011-07-17 21:03:04 -070067 private int mOriginalWidthGap;
68 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069 private int mWidthGap;
70 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070071 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080072 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073
74 private final Rect mRect = new Rect();
75 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070076
Patrick Dubroyde7658b2010-09-27 11:15:43 -070077 // These are temporary variables to prevent having to allocate a new object just to
78 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070079 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070080 private final int[] mTmpPoint = new int[2];
Adam Cohen69ce2e52011-07-03 19:25:21 -070081 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070082
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080084 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070085 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086
Michael Jurkadee05892010-07-27 10:01:56 -070087 private OnTouchListener mInterceptTouchListener;
88
Adam Cohen69ce2e52011-07-03 19:25:21 -070089 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070090 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070091
Adam Cohenb5ba0972011-09-07 18:02:31 -070092 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070093 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070094 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070095
Michael Jurka33945b22010-12-21 18:19:38 -080096 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080097 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -070098 private Drawable mOverScrollForegroundDrawable;
99 private Drawable mOverScrollLeft;
100 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700101 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700102 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700103 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700104
Michael Jurka33945b22010-12-21 18:19:38 -0800105 // If we're actively dragging something over this screen, mIsDragOverlapping is true
106 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700107 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700108
Winson Chung150fbab2010-09-29 17:14:26 -0700109 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700110 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800111 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700112 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700113 private InterruptibleInOutAnimator[] mDragOutlineAnims =
114 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700115
116 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700117 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700118 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700119
Patrick Dubroy96864c32011-03-10 17:17:23 -0800120 private BubbleTextView mPressedOrFocusedIcon;
121
Adam Cohen482ed822012-03-02 14:15:13 -0800122 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
123 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700124 private HashMap<View, ReorderHintAnimation>
125 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
126
127 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700128
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700129 // When a drag operation is in progress, holds the nearest cell to the touch point
130 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Joe Onorato4be866d2010-10-10 11:26:02 -0700132 private boolean mDragging = false;
133
Patrick Dubroyce34a972010-10-19 10:34:32 -0700134 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700135 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700136
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800137 private boolean mIsHotseat = false;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800138
Adam Cohen482ed822012-03-02 14:15:13 -0800139 public static final int MODE_DRAG_OVER = 0;
140 public static final int MODE_ON_DROP = 1;
141 public static final int MODE_ON_DROP_EXTERNAL = 2;
142 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700143 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800144 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
145
Adam Cohena897f392012-04-27 18:12:05 -0700146 static final int LANDSCAPE = 0;
147 static final int PORTRAIT = 1;
148
Adam Cohen7bdfc972012-05-22 16:50:35 -0700149 private static final float REORDER_HINT_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700150 private static final int REORDER_ANIMATION_DURATION = 150;
151 private float mReorderHintAnimationMagnitude;
152
Adam Cohen482ed822012-03-02 14:15:13 -0800153 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
154 private Rect mOccupiedRect = new Rect();
155 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700156 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700157 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700158 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800159
Romain Guy8a0bff52012-05-06 13:14:33 -0700160 private final static PorterDuffXfermode sAddBlendMode =
161 new PorterDuffXfermode(PorterDuff.Mode.ADD);
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 public CellLayout(Context context) {
164 this(context, null);
165 }
166
167 public CellLayout(Context context, AttributeSet attrs) {
168 this(context, attrs, 0);
169 }
170
171 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
172 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700173 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700174
175 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
176 // the user where a dragged item will land when dropped.
177 setWillNotDraw(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700178 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
181
Adam Cohenf4bd5792012-04-27 11:35:29 -0700182 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
183 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700184 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
185 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700186 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700187 mCountX = LauncherModel.getCellCountX();
188 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700189 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800190 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700191 mPreviousReorderDirection[0] = INVALID_DIRECTION;
192 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
194 a.recycle();
195
196 setAlwaysDrawnWithCacheEnabled(false);
197
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700198 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700199
Winson Chung967289b2011-06-30 18:09:30 -0700200 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700201 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800202
Adam Cohenb5ba0972011-09-07 18:02:31 -0700203 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
204 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
205 mForegroundPadding =
206 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800207
Adam Cohen19f37922012-03-21 11:59:11 -0700208 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
209 res.getDimensionPixelSize(R.dimen.app_icon_size));
210
Winson Chungb26f3d62011-06-02 10:49:29 -0700211 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700212 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700213
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700214 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700215
Patrick Dubroyce34a972010-10-19 10:34:32 -0700216 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700217
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700218
Winson Chungb8c69f32011-10-19 21:36:08 -0700219 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700220 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800221 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700222 }
223
224 // When dragging things around the home screens, we show a green outline of
225 // where the item will land. The outlines gradually fade out, leaving a trail
226 // behind the drag path.
227 // Set up all the animations that are used to implement this fading.
228 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700229 final float fromAlphaValue = 0;
230 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700231
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700232 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700233
234 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700235 final InterruptibleInOutAnimator anim =
236 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700237 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700238 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700239 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700240 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700241 final Bitmap outline = (Bitmap)anim.getTag();
242
243 // If an animation is started and then stopped very quickly, we can still
244 // get spurious updates we've cleared the tag. Guard against this.
245 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700246 @SuppressWarnings("all") // suppress dead code warning
247 final boolean debug = false;
248 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700249 Object val = animation.getAnimatedValue();
250 Log.d(TAG, "anim " + thisIndex + " update: " + val +
251 ", isStopped " + anim.isStopped());
252 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 // Try to prevent it from continuing to run
254 animation.cancel();
255 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700256 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800257 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700258 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700259 }
260 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700261 // The animation holds a reference to the drag outline bitmap as long is it's
262 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700263 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700264 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700265 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700266 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700267 anim.setTag(null);
268 }
269 }
270 });
271 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700272 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700273
Michael Jurka18014792010-10-14 09:01:34 -0700274 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700275 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800276
Michael Jurkaa52570f2012-03-20 03:18:20 -0700277 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
278 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
279 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700280 }
281
Michael Jurkaf6440da2011-04-05 14:50:34 -0700282 static int widthInPortrait(Resources r, int numCells) {
283 // We use this method from Workspace to figure out how many rows/columns Launcher should
284 // have. We ignore the left/right padding on CellLayout because it turns out in our design
285 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700286 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700287 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
288 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700289
Winson Chung4b825dcd2011-06-19 12:41:22 -0700290 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700291 }
292
Michael Jurkaf6440da2011-04-05 14:50:34 -0700293 static int heightInLandscape(Resources r, int numCells) {
294 // We use this method from Workspace to figure out how many rows/columns Launcher should
295 // have. We ignore the left/right padding on CellLayout because it turns out in our design
296 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700297 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700298 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
299 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700300
Winson Chung4b825dcd2011-06-19 12:41:22 -0700301 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700302 }
303
Adam Cohen2801caf2011-05-13 20:57:39 -0700304 public void enableHardwareLayers() {
Michael Jurkad51f33a2012-06-28 15:35:26 -0700305 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_HARDWARE, null);
306 }
307
308 public void disableHardwareLayers() {
309 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_NONE, null);
310 }
311
312 public void buildHardwareLayer() {
313 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700314 }
315
316 public void setGridSize(int x, int y) {
317 mCountX = x;
318 mCountY = y;
319 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800320 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700321 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700322 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700323 }
324
Patrick Dubroy96864c32011-03-10 17:17:23 -0800325 private void invalidateBubbleTextView(BubbleTextView icon) {
326 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700327 invalidate(icon.getLeft() + getPaddingLeft() - padding,
328 icon.getTop() + getPaddingTop() - padding,
329 icon.getRight() + getPaddingLeft() + padding,
330 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800331 }
332
Adam Cohenb5ba0972011-09-07 18:02:31 -0700333 void setOverScrollAmount(float r, boolean left) {
334 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
335 mOverScrollForegroundDrawable = mOverScrollLeft;
336 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
337 mOverScrollForegroundDrawable = mOverScrollRight;
338 }
339
340 mForegroundAlpha = (int) Math.round((r * 255));
341 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
342 invalidate();
343 }
344
Patrick Dubroy96864c32011-03-10 17:17:23 -0800345 void setPressedOrFocusedIcon(BubbleTextView icon) {
346 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
347 // requires an expanded clip rect (due to the glow's blur radius)
348 BubbleTextView oldIcon = mPressedOrFocusedIcon;
349 mPressedOrFocusedIcon = icon;
350 if (oldIcon != null) {
351 invalidateBubbleTextView(oldIcon);
352 }
353 if (mPressedOrFocusedIcon != null) {
354 invalidateBubbleTextView(mPressedOrFocusedIcon);
355 }
356 }
357
Michael Jurka33945b22010-12-21 18:19:38 -0800358 void setIsDragOverlapping(boolean isDragOverlapping) {
359 if (mIsDragOverlapping != isDragOverlapping) {
360 mIsDragOverlapping = isDragOverlapping;
361 invalidate();
362 }
363 }
364
365 boolean getIsDragOverlapping() {
366 return mIsDragOverlapping;
367 }
368
Adam Cohenebea84d2011-11-09 17:20:41 -0800369 protected void setOverscrollTransformsDirty(boolean dirty) {
370 mScrollingTransformsDirty = dirty;
371 }
372
373 protected void resetOverscrollTransforms() {
374 if (mScrollingTransformsDirty) {
375 setOverscrollTransformsDirty(false);
376 setTranslationX(0);
377 setRotationY(0);
378 // It doesn't matter if we pass true or false here, the important thing is that we
379 // pass 0, which results in the overscroll drawable not being drawn any more.
380 setOverScrollAmount(0, false);
381 setPivotX(getMeasuredWidth() / 2);
382 setPivotY(getMeasuredHeight() / 2);
383 }
384 }
385
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700386 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700387 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700388 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
389 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
390 // When we're small, we are either drawn normally or in the "accepts drops" state (during
391 // a drag). However, we also drag the mini hover background *over* one of those two
392 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700393 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700394 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800395
396 if (mIsDragOverlapping) {
397 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700398 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700399 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700400 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700401 }
Michael Jurka33945b22010-12-21 18:19:38 -0800402
403 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
404 bg.setBounds(mBackgroundRect);
405 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700406 }
Romain Guya6abce82009-11-10 02:54:41 -0800407
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700408 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700409 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700410 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700411 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800412 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700413 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700414 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800415 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700416 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700417 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800418
419 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
420 // requires an expanded clip rect (due to the glow's blur radius)
421 if (mPressedOrFocusedIcon != null) {
422 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
423 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
424 if (b != null) {
425 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700426 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
427 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800428 null);
429 }
430 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700431
Adam Cohen482ed822012-03-02 14:15:13 -0800432 if (DEBUG_VISUALIZE_OCCUPIED) {
433 int[] pt = new int[2];
434 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700435 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800436 for (int i = 0; i < mCountX; i++) {
437 for (int j = 0; j < mCountY; j++) {
438 if (mOccupied[i][j]) {
439 cellToPoint(i, j, pt);
440 canvas.save();
441 canvas.translate(pt[0], pt[1]);
442 cd.draw(canvas);
443 canvas.restore();
444 }
445 }
446 }
447 }
448
Andrew Flynn850d2e72012-04-26 16:51:20 -0700449 int previewOffset = FolderRingAnimator.sPreviewSize;
450
Adam Cohen69ce2e52011-07-03 19:25:21 -0700451 // The folder outer / inner ring image(s)
452 for (int i = 0; i < mFolderOuterRings.size(); i++) {
453 FolderRingAnimator fra = mFolderOuterRings.get(i);
454
455 // Draw outer ring
456 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
457 int width = (int) fra.getOuterRingSize();
458 int height = width;
459 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
460
461 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700462 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700463
464 canvas.save();
465 canvas.translate(centerX - width / 2, centerY - height / 2);
466 d.setBounds(0, 0, width, height);
467 d.draw(canvas);
468 canvas.restore();
469
470 // Draw inner ring
471 d = FolderRingAnimator.sSharedInnerRingDrawable;
472 width = (int) fra.getInnerRingSize();
473 height = width;
474 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
475
476 centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700477 centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700478 canvas.save();
479 canvas.translate(centerX - width / 2, centerY - width / 2);
480 d.setBounds(0, 0, width, height);
481 d.draw(canvas);
482 canvas.restore();
483 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700484
485 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
486 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
487 int width = d.getIntrinsicWidth();
488 int height = d.getIntrinsicHeight();
489
490 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
491 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700492 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohenc51934b2011-07-26 21:07:43 -0700493
494 canvas.save();
495 canvas.translate(centerX - width / 2, centerY - width / 2);
496 d.setBounds(0, 0, width, height);
497 d.draw(canvas);
498 canvas.restore();
499 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700500 }
501
Adam Cohenb5ba0972011-09-07 18:02:31 -0700502 @Override
503 protected void dispatchDraw(Canvas canvas) {
504 super.dispatchDraw(canvas);
505 if (mForegroundAlpha > 0) {
506 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
507 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700508 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700509 mOverScrollForegroundDrawable.draw(canvas);
510 p.setXfermode(null);
511 }
512 }
513
Adam Cohen69ce2e52011-07-03 19:25:21 -0700514 public void showFolderAccept(FolderRingAnimator fra) {
515 mFolderOuterRings.add(fra);
516 }
517
518 public void hideFolderAccept(FolderRingAnimator fra) {
519 if (mFolderOuterRings.contains(fra)) {
520 mFolderOuterRings.remove(fra);
521 }
522 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700523 }
524
Adam Cohenc51934b2011-07-26 21:07:43 -0700525 public void setFolderLeaveBehindCell(int x, int y) {
526 mFolderLeaveBehindCell[0] = x;
527 mFolderLeaveBehindCell[1] = y;
528 invalidate();
529 }
530
531 public void clearFolderLeaveBehind() {
532 mFolderLeaveBehindCell[0] = -1;
533 mFolderLeaveBehindCell[1] = -1;
534 invalidate();
535 }
536
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700537 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700538 public boolean shouldDelayChildPressedState() {
539 return false;
540 }
541
542 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700543 public void cancelLongPress() {
544 super.cancelLongPress();
545
546 // Cancel long press for all children
547 final int count = getChildCount();
548 for (int i = 0; i < count; i++) {
549 final View child = getChildAt(i);
550 child.cancelLongPress();
551 }
552 }
553
Michael Jurkadee05892010-07-27 10:01:56 -0700554 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
555 mInterceptTouchListener = listener;
556 }
557
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700559 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
561
562 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700563 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 }
565
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800566 public void setIsHotseat(boolean isHotseat) {
567 mIsHotseat = isHotseat;
568 }
569
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800570 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700571 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700572 final LayoutParams lp = params;
573
Andrew Flynnde38e422012-05-08 11:22:15 -0700574 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800575 if (child instanceof BubbleTextView) {
576 BubbleTextView bubbleChild = (BubbleTextView) child;
577
Andrew Flynnde38e422012-05-08 11:22:15 -0700578 Resources res = getResources();
579 if (mIsHotseat) {
580 bubbleChild.setTextColor(res.getColor(android.R.color.transparent));
581 } else {
582 bubbleChild.setTextColor(res.getColor(R.color.workspace_icon_text_color));
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800583 }
584 }
585
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 // Generate an id for each view, this assumes we have at most 256x256 cells
587 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700588 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700589 // If the horizontal or vertical span is set to -1, it is taken to
590 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700591 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
592 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593
Winson Chungaafa03c2010-06-11 17:34:16 -0700594 child.setId(childId);
595
Michael Jurkaa52570f2012-03-20 03:18:20 -0700596 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700597
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700598 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700599
Winson Chungaafa03c2010-06-11 17:34:16 -0700600 return true;
601 }
602 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700606 public void removeAllViews() {
607 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700608 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700609 }
610
611 @Override
612 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700613 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700614 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700615 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700616 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700617 }
618
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700619 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700620 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700621 }
622
Michael Jurka0280c3b2010-09-17 15:00:07 -0700623 @Override
624 public void removeView(View view) {
625 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700626 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700627 }
628
629 @Override
630 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700631 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
632 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700633 }
634
635 @Override
636 public void removeViewInLayout(View view) {
637 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700638 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700639 }
640
641 @Override
642 public void removeViews(int start, int count) {
643 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700644 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700645 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700646 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700647 }
648
649 @Override
650 public void removeViewsInLayout(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.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800655 }
656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 @Override
658 protected void onAttachedToWindow() {
659 super.onAttachedToWindow();
660 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
661 }
662
Michael Jurkaaf442092010-06-10 17:01:57 -0700663 public void setTagToCellInfoForPoint(int touchX, int touchY) {
664 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800665 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700666 final int x = touchX + getScrollX();
667 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700668 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700669
670 boolean found = false;
671 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700672 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800673 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700674
Adam Cohen1b607ed2011-03-03 17:26:50 -0800675 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
676 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700677 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700678
Winson Chungeecf02d2012-03-02 17:14:58 -0800679 float scale = child.getScaleX();
680 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
681 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700682 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
683 // offset that by this CellLayout's padding to test an (x,y) point that is relative
684 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700685 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800686 frame.inset((int) (frame.width() * (1f - scale) / 2),
687 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700688
Michael Jurkaaf442092010-06-10 17:01:57 -0700689 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700690 cellInfo.cell = child;
691 cellInfo.cellX = lp.cellX;
692 cellInfo.cellY = lp.cellY;
693 cellInfo.spanX = lp.cellHSpan;
694 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700695 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700696 break;
697 }
698 }
699 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700700
Michael Jurkad771c962011-08-09 15:00:48 -0700701 mLastDownOnOccupiedCell = found;
702
Michael Jurkaaf442092010-06-10 17:01:57 -0700703 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700704 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700705 pointToCellExact(x, y, cellXY);
706
Michael Jurkaaf442092010-06-10 17:01:57 -0700707 cellInfo.cell = null;
708 cellInfo.cellX = cellXY[0];
709 cellInfo.cellY = cellXY[1];
710 cellInfo.spanX = 1;
711 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700712 }
713 setTag(cellInfo);
714 }
715
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 @Override
717 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700718 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
719 // even in the case where we return early. Not clearing here was causing bugs whereby on
720 // long-press we'd end up picking up an item from a previous drag operation.
721 final int action = ev.getAction();
722
723 if (action == MotionEvent.ACTION_DOWN) {
724 clearTagCellInfo();
725 }
726
Michael Jurkadee05892010-07-27 10:01:56 -0700727 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
728 return true;
729 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730
731 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700732 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800734
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 return false;
736 }
737
Adam Cohenc1997fd2011-08-15 18:26:39 -0700738 private void clearTagCellInfo() {
739 final CellInfo cellInfo = mCellInfo;
740 cellInfo.cell = null;
741 cellInfo.cellX = -1;
742 cellInfo.cellY = -1;
743 cellInfo.spanX = 0;
744 cellInfo.spanY = 0;
745 setTag(cellInfo);
746 }
747
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700749 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
751
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700752 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700753 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 * @param x X coordinate of the point
755 * @param y Y coordinate of the point
756 * @param result Array of 2 ints to hold the x and y coordinate of the cell
757 */
758 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700759 final int hStartPadding = getPaddingLeft();
760 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761
762 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
763 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
764
Adam Cohend22015c2010-07-26 22:02:18 -0700765 final int xAxis = mCountX;
766 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767
768 if (result[0] < 0) result[0] = 0;
769 if (result[0] >= xAxis) result[0] = xAxis - 1;
770 if (result[1] < 0) result[1] = 0;
771 if (result[1] >= yAxis) result[1] = yAxis - 1;
772 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700773
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 /**
775 * Given a point, return the cell that most closely encloses that point
776 * @param x X coordinate of the point
777 * @param y Y coordinate of the point
778 * @param result Array of 2 ints to hold the x and y coordinate of the cell
779 */
780 void pointToCellRounded(int x, int y, int[] result) {
781 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
782 }
783
784 /**
785 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700786 *
787 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700789 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 * @param result Array of 2 ints to hold the x and y coordinate of the point
791 */
792 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700793 final int hStartPadding = getPaddingLeft();
794 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795
796 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
797 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
798 }
799
Adam Cohene3e27a82011-04-15 12:07:39 -0700800 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800801 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700802 *
803 * @param cellX X coordinate of the cell
804 * @param cellY Y coordinate of the cell
805 *
806 * @param result Array of 2 ints to hold the x and y coordinate of the point
807 */
808 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700809 regionToCenterPoint(cellX, cellY, 1, 1, result);
810 }
811
812 /**
813 * Given a cell coordinate and span return the point that represents the center of the regio
814 *
815 * @param cellX X coordinate of the cell
816 * @param cellY Y coordinate of the cell
817 *
818 * @param result Array of 2 ints to hold the x and y coordinate of the point
819 */
820 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700821 final int hStartPadding = getPaddingLeft();
822 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700823 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
824 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
825 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
826 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700827 }
828
Adam Cohen19f37922012-03-21 11:59:11 -0700829 /**
830 * Given a cell coordinate and span fills out a corresponding pixel rect
831 *
832 * @param cellX X coordinate of the cell
833 * @param cellY Y coordinate of the cell
834 * @param result Rect in which to write the result
835 */
836 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
837 final int hStartPadding = getPaddingLeft();
838 final int vStartPadding = getPaddingTop();
839 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
840 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
841 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
842 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
843 }
844
Adam Cohen482ed822012-03-02 14:15:13 -0800845 public float getDistanceFromCell(float x, float y, int[] cell) {
846 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
847 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
848 Math.pow(y - mTmpPoint[1], 2));
849 return distance;
850 }
851
Romain Guy84f296c2009-11-04 15:00:44 -0800852 int getCellWidth() {
853 return mCellWidth;
854 }
855
856 int getCellHeight() {
857 return mCellHeight;
858 }
859
Adam Cohend4844c32011-02-18 19:25:06 -0800860 int getWidthGap() {
861 return mWidthGap;
862 }
863
864 int getHeightGap() {
865 return mHeightGap;
866 }
867
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700868 Rect getContentRect(Rect r) {
869 if (r == null) {
870 r = new Rect();
871 }
872 int left = getPaddingLeft();
873 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700874 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
875 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700876 r.set(left, top, right, bottom);
877 return r;
878 }
879
Adam Cohena897f392012-04-27 18:12:05 -0700880 static void getMetrics(Rect metrics, Resources res, int measureWidth, int measureHeight,
881 int countX, int countY, int orientation) {
882 int numWidthGaps = countX - 1;
883 int numHeightGaps = countY - 1;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700884
885 int widthGap;
886 int heightGap;
887 int cellWidth;
888 int cellHeight;
889 int paddingLeft;
890 int paddingRight;
891 int paddingTop;
892 int paddingBottom;
893
Adam Cohena897f392012-04-27 18:12:05 -0700894 int maxGap = res.getDimensionPixelSize(R.dimen.workspace_max_gap);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700895 if (orientation == LANDSCAPE) {
896 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_land);
897 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_land);
898 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
899 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
900 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_land);
901 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_land);
902 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_land);
903 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_land);
904 } else {
905 // PORTRAIT
906 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_port);
907 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_port);
908 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
909 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
910 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_port);
911 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_port);
912 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_port);
913 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_port);
914 }
915
916 if (widthGap < 0 || heightGap < 0) {
917 int hSpace = measureWidth - paddingLeft - paddingRight;
918 int vSpace = measureHeight - paddingTop - paddingBottom;
Adam Cohena897f392012-04-27 18:12:05 -0700919 int hFreeSpace = hSpace - (countX * cellWidth);
920 int vFreeSpace = vSpace - (countY * cellHeight);
921 widthGap = Math.min(maxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
922 heightGap = Math.min(maxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700923 }
924 metrics.set(cellWidth, cellHeight, widthGap, heightGap);
925 }
926
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 @Override
928 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700930 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
931
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
933 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700934
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
936 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
937 }
938
Adam Cohend22015c2010-07-26 22:02:18 -0700939 int numWidthGaps = mCountX - 1;
940 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941
Adam Cohen234c4cd2011-07-17 21:03:04 -0700942 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Michael Jurkadd13e3d2012-05-01 12:38:17 -0700943 int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight();
944 int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700945 int hFreeSpace = hSpace - (mCountX * mCellWidth);
946 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700947 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
948 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700949 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700950 } else {
951 mWidthGap = mOriginalWidthGap;
952 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700953 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700954
Michael Jurka8c920dd2011-01-20 14:16:56 -0800955 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
956 int newWidth = widthSpecSize;
957 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700958 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700959 newWidth = getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700960 ((mCountX - 1) * mWidthGap);
Michael Jurka8b805b12012-04-18 14:23:14 -0700961 newHeight = getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700962 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700963 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700964 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800965
966 int count = getChildCount();
967 for (int i = 0; i < count; i++) {
968 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700969 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
970 getPaddingRight(), MeasureSpec.EXACTLY);
971 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
972 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800973 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
974 }
975 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 }
977
978 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700979 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800982 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700983 child.layout(getPaddingLeft(), getPaddingTop(),
984 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 }
986 }
987
988 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700989 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
990 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700991 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700992 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
993 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700994 }
995
996 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700998 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
1000
1001 @Override
1002 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001003 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 }
1005
Michael Jurka5f1c5092010-09-03 14:15:02 -07001006 public float getBackgroundAlpha() {
1007 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001008 }
1009
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001010 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001011 if (mBackgroundAlphaMultiplier != multiplier) {
1012 mBackgroundAlphaMultiplier = multiplier;
1013 invalidate();
1014 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001015 }
1016
Adam Cohenddb82192010-11-10 16:32:54 -08001017 public float getBackgroundAlphaMultiplier() {
1018 return mBackgroundAlphaMultiplier;
1019 }
1020
Michael Jurka5f1c5092010-09-03 14:15:02 -07001021 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001022 if (mBackgroundAlpha != alpha) {
1023 mBackgroundAlpha = alpha;
1024 invalidate();
1025 }
Michael Jurkadee05892010-07-27 10:01:56 -07001026 }
1027
Michael Jurkaa52570f2012-03-20 03:18:20 -07001028 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001029 final int childCount = getChildCount();
1030 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001031 getChildAt(i).setAlpha(alpha);
1032 }
1033 }
1034
Michael Jurkaa52570f2012-03-20 03:18:20 -07001035 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1036 if (getChildCount() > 0) {
1037 return (ShortcutAndWidgetContainer) getChildAt(0);
1038 }
1039 return null;
1040 }
1041
Patrick Dubroy440c3602010-07-13 17:50:32 -07001042 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001043 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001044 }
1045
Adam Cohen76fc0852011-06-17 13:26:23 -07001046 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001047 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001048 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001049 boolean[][] occupied = mOccupied;
1050 if (!permanent) {
1051 occupied = mTmpOccupied;
1052 }
1053
Adam Cohen19f37922012-03-21 11:59:11 -07001054 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001055 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1056 final ItemInfo info = (ItemInfo) child.getTag();
1057
1058 // We cancel any existing animations
1059 if (mReorderAnimators.containsKey(lp)) {
1060 mReorderAnimators.get(lp).cancel();
1061 mReorderAnimators.remove(lp);
1062 }
1063
Adam Cohen482ed822012-03-02 14:15:13 -08001064 final int oldX = lp.x;
1065 final int oldY = lp.y;
1066 if (adjustOccupied) {
1067 occupied[lp.cellX][lp.cellY] = false;
1068 occupied[cellX][cellY] = true;
1069 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001070 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001071 if (permanent) {
1072 lp.cellX = info.cellX = cellX;
1073 lp.cellY = info.cellY = cellY;
1074 } else {
1075 lp.tmpCellX = cellX;
1076 lp.tmpCellY = cellY;
1077 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001078 clc.setupLp(lp);
1079 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001080 final int newX = lp.x;
1081 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001082
Adam Cohen76fc0852011-06-17 13:26:23 -07001083 lp.x = oldX;
1084 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001085
Adam Cohen482ed822012-03-02 14:15:13 -08001086 // Exit early if we're not actually moving the view
1087 if (oldX == newX && oldY == newY) {
1088 lp.isLockedToGrid = true;
1089 return true;
1090 }
1091
Michael Jurka2ecf9952012-06-18 12:52:28 -07001092 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001093 va.setDuration(duration);
1094 mReorderAnimators.put(lp, va);
1095
1096 va.addUpdateListener(new AnimatorUpdateListener() {
1097 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001098 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001099 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001100 lp.x = (int) ((1 - r) * oldX + r * newX);
1101 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001102 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001103 }
1104 });
Adam Cohen482ed822012-03-02 14:15:13 -08001105 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001106 boolean cancelled = false;
1107 public void onAnimationEnd(Animator animation) {
1108 // If the animation was cancelled, it means that another animation
1109 // has interrupted this one, and we don't want to lock the item into
1110 // place just yet.
1111 if (!cancelled) {
1112 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001113 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001114 }
1115 if (mReorderAnimators.containsKey(lp)) {
1116 mReorderAnimators.remove(lp);
1117 }
1118 }
1119 public void onAnimationCancel(Animator animation) {
1120 cancelled = true;
1121 }
1122 });
Adam Cohen482ed822012-03-02 14:15:13 -08001123 va.setStartDelay(delay);
1124 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001125 return true;
1126 }
1127 return false;
1128 }
1129
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001130 /**
1131 * Estimate where the top left cell of the dragged item will land if it is dropped.
1132 *
1133 * @param originX The X value of the top left corner of the item
1134 * @param originY The Y value of the top left corner of the item
1135 * @param spanX The number of horizontal cells that the item spans
1136 * @param spanY The number of vertical cells that the item spans
1137 * @param result The estimated drop cell X and Y.
1138 */
1139 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001140 final int countX = mCountX;
1141 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001142
Michael Jurkaa63c4522010-08-19 13:52:27 -07001143 // pointToCellRounded takes the top left of a cell but will pad that with
1144 // cellWidth/2 and cellHeight/2 when finding the matching cell
1145 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001146
1147 // If the item isn't fully on this screen, snap to the edges
1148 int rightOverhang = result[0] + spanX - countX;
1149 if (rightOverhang > 0) {
1150 result[0] -= rightOverhang; // Snap to right
1151 }
1152 result[0] = Math.max(0, result[0]); // Snap to left
1153 int bottomOverhang = result[1] + spanY - countY;
1154 if (bottomOverhang > 0) {
1155 result[1] -= bottomOverhang; // Snap to bottom
1156 }
1157 result[1] = Math.max(0, result[1]); // Snap to top
1158 }
1159
Adam Cohen482ed822012-03-02 14:15:13 -08001160 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1161 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001162 final int oldDragCellX = mDragCell[0];
1163 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001164
Winson Chungb8c69f32011-10-19 21:36:08 -07001165 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001166 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1167 } else {
1168 mDragCenter.set(originX, originY);
1169 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001170
Adam Cohen2801caf2011-05-13 20:57:39 -07001171 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001172 return;
1173 }
1174
Adam Cohen482ed822012-03-02 14:15:13 -08001175 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1176 mDragCell[0] = cellX;
1177 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001178 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001179 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001180 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001181
Joe Onorato4be866d2010-10-10 11:26:02 -07001182 int left = topLeft[0];
1183 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001184
Winson Chungb8c69f32011-10-19 21:36:08 -07001185 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001186 // When drawing the drag outline, it did not account for margin offsets
1187 // added by the view's parent.
1188 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1189 left += lp.leftMargin;
1190 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001191
Adam Cohen99e8b402011-03-25 19:23:43 -07001192 // Offsets due to the size difference between the View and the dragOutline.
1193 // There is a size difference to account for the outer blur, which may lie
1194 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001195 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001196 // We center about the x axis
1197 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1198 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001199 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001200 if (dragOffset != null && dragRegion != null) {
1201 // Center the drag region *horizontally* in the cell and apply a drag
1202 // outline offset
1203 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1204 - dragRegion.width()) / 2;
1205 top += dragOffset.y;
1206 } else {
1207 // Center the drag outline in the cell
1208 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1209 - dragOutline.getWidth()) / 2;
1210 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1211 - dragOutline.getHeight()) / 2;
1212 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001213 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001214 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001215 mDragOutlineAnims[oldIndex].animateOut();
1216 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001217 Rect r = mDragOutlines[mDragOutlineCurrent];
1218 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1219 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001220 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001221 }
Winson Chung150fbab2010-09-29 17:14:26 -07001222
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001223 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1224 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001225 }
1226 }
1227
Adam Cohene0310962011-04-18 16:15:31 -07001228 public void clearDragOutlines() {
1229 final int oldIndex = mDragOutlineCurrent;
1230 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001231 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001232 }
1233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001235 * Find a vacant area that will fit the given bounds nearest the requested
1236 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001237 *
Romain Guy51afc022009-05-04 18:03:43 -07001238 * @param pixelX The X location at which you want to search for a vacant area.
1239 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001240 * @param spanX Horizontal span of the object.
1241 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001242 * @param result Array in which to place the result, or null (in which case a new array will
1243 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001244 * @return The X, Y cell of a vacant area that can contain this object,
1245 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001247 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1248 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001249 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001250 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001251
Michael Jurka6a1435d2010-09-27 17:35:12 -07001252 /**
1253 * Find a vacant area that will fit the given bounds nearest the requested
1254 * cell location. Uses Euclidean distance to score multiple vacant areas.
1255 *
1256 * @param pixelX The X location at which you want to search for a vacant area.
1257 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001258 * @param minSpanX The minimum horizontal span required
1259 * @param minSpanY The minimum vertical span required
1260 * @param spanX Horizontal span of the object.
1261 * @param spanY Vertical span of the object.
1262 * @param result Array in which to place the result, or null (in which case a new array will
1263 * be allocated)
1264 * @return The X, Y cell of a vacant area that can contain this object,
1265 * nearest the requested location.
1266 */
1267 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1268 int spanY, int[] result, int[] resultSpan) {
1269 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1270 result, resultSpan);
1271 }
1272
1273 /**
1274 * Find a vacant area that will fit the given bounds nearest the requested
1275 * cell location. Uses Euclidean distance to score multiple vacant areas.
1276 *
1277 * @param pixelX The X location at which you want to search for a vacant area.
1278 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001279 * @param spanX Horizontal span of the object.
1280 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001281 * @param ignoreOccupied If true, the result can be an occupied cell
1282 * @param result Array in which to place the result, or null (in which case a new array will
1283 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001284 * @return The X, Y cell of a vacant area that can contain this object,
1285 * nearest the requested location.
1286 */
Adam Cohendf035382011-04-11 17:22:04 -07001287 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1288 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001289 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001290 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001291 }
1292
1293 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1294 private void lazyInitTempRectStack() {
1295 if (mTempRectStack.isEmpty()) {
1296 for (int i = 0; i < mCountX * mCountY; i++) {
1297 mTempRectStack.push(new Rect());
1298 }
1299 }
1300 }
Adam Cohen482ed822012-03-02 14:15:13 -08001301
Adam Cohend41fbf52012-02-16 23:53:59 -08001302 private void recycleTempRects(Stack<Rect> used) {
1303 while (!used.isEmpty()) {
1304 mTempRectStack.push(used.pop());
1305 }
1306 }
1307
1308 /**
1309 * Find a vacant area that will fit the given bounds nearest the requested
1310 * cell location. Uses Euclidean distance to score multiple vacant areas.
1311 *
1312 * @param pixelX The X location at which you want to search for a vacant area.
1313 * @param pixelY The Y location at which you want to search for a vacant area.
1314 * @param minSpanX The minimum horizontal span required
1315 * @param minSpanY The minimum vertical span required
1316 * @param spanX Horizontal span of the object.
1317 * @param spanY Vertical span of the object.
1318 * @param ignoreOccupied If true, the result can be an occupied cell
1319 * @param result Array in which to place the result, or null (in which case a new array will
1320 * be allocated)
1321 * @return The X, Y cell of a vacant area that can contain this object,
1322 * nearest the requested location.
1323 */
1324 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001325 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1326 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001327 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001328 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001329 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001330
Adam Cohene3e27a82011-04-15 12:07:39 -07001331 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1332 // to the center of the item, but we are searching based on the top-left cell, so
1333 // we translate the point over to correspond to the top-left.
1334 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1335 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1336
Jeff Sharkey70864282009-04-07 21:08:40 -07001337 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001338 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001339 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001340 final Rect bestRect = new Rect(-1, -1, -1, -1);
1341 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001342
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001343 final int countX = mCountX;
1344 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001345
Adam Cohend41fbf52012-02-16 23:53:59 -08001346 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1347 spanX < minSpanX || spanY < minSpanY) {
1348 return bestXY;
1349 }
1350
1351 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001352 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001353 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1354 int ySize = -1;
1355 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001356 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001357 // First, let's see if this thing fits anywhere
1358 for (int i = 0; i < minSpanX; i++) {
1359 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001360 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001361 continue inner;
1362 }
Michael Jurkac28de512010-08-13 11:27:44 -07001363 }
1364 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001365 xSize = minSpanX;
1366 ySize = minSpanY;
1367
1368 // We know that the item will fit at _some_ acceptable size, now let's see
1369 // how big we can make it. We'll alternate between incrementing x and y spans
1370 // until we hit a limit.
1371 boolean incX = true;
1372 boolean hitMaxX = xSize >= spanX;
1373 boolean hitMaxY = ySize >= spanY;
1374 while (!(hitMaxX && hitMaxY)) {
1375 if (incX && !hitMaxX) {
1376 for (int j = 0; j < ySize; j++) {
1377 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1378 // We can't move out horizontally
1379 hitMaxX = true;
1380 }
1381 }
1382 if (!hitMaxX) {
1383 xSize++;
1384 }
1385 } else if (!hitMaxY) {
1386 for (int i = 0; i < xSize; i++) {
1387 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1388 // We can't move out vertically
1389 hitMaxY = true;
1390 }
1391 }
1392 if (!hitMaxY) {
1393 ySize++;
1394 }
1395 }
1396 hitMaxX |= xSize >= spanX;
1397 hitMaxY |= ySize >= spanY;
1398 incX = !incX;
1399 }
1400 incX = true;
1401 hitMaxX = xSize >= spanX;
1402 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001403 }
Winson Chung0be025d2011-05-23 17:45:09 -07001404 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001405 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406
Adam Cohend41fbf52012-02-16 23:53:59 -08001407 // We verify that the current rect is not a sub-rect of any of our previous
1408 // candidates. In this case, the current rect is disqualified in favour of the
1409 // containing rect.
1410 Rect currentRect = mTempRectStack.pop();
1411 currentRect.set(x, y, x + xSize, y + ySize);
1412 boolean contained = false;
1413 for (Rect r : validRegions) {
1414 if (r.contains(currentRect)) {
1415 contained = true;
1416 break;
1417 }
1418 }
1419 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001420 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1421 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001422
Adam Cohend41fbf52012-02-16 23:53:59 -08001423 if ((distance <= bestDistance && !contained) ||
1424 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001425 bestDistance = distance;
1426 bestXY[0] = x;
1427 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001428 if (resultSpan != null) {
1429 resultSpan[0] = xSize;
1430 resultSpan[1] = ySize;
1431 }
1432 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001433 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 }
1435 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001436 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001437 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438
Adam Cohenc0dcf592011-06-01 15:30:43 -07001439 // Return -1, -1 if no suitable location found
1440 if (bestDistance == Double.MAX_VALUE) {
1441 bestXY[0] = -1;
1442 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001443 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001444 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001445 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001447
Adam Cohen482ed822012-03-02 14:15:13 -08001448 /**
1449 * Find a vacant area that will fit the given bounds nearest the requested
1450 * cell location, and will also weigh in a suggested direction vector of the
1451 * desired location. This method computers distance based on unit grid distances,
1452 * not pixel distances.
1453 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001454 * @param cellX The X cell nearest to which you want to search for a vacant area.
1455 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001456 * @param spanX Horizontal span of the object.
1457 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001458 * @param direction The favored direction in which the views should move from x, y
1459 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1460 * matches exactly. Otherwise we find the best matching direction.
1461 * @param occoupied The array which represents which cells in the CellLayout are occupied
1462 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1463 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001464 * @param result Array in which to place the result, or null (in which case a new array will
1465 * be allocated)
1466 * @return The X, Y cell of a vacant area that can contain this object,
1467 * nearest the requested location.
1468 */
1469 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001470 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001471 // Keep track of best-scoring drop area
1472 final int[] bestXY = result != null ? result : new int[2];
1473 float bestDistance = Float.MAX_VALUE;
1474 int bestDirectionScore = Integer.MIN_VALUE;
1475
1476 final int countX = mCountX;
1477 final int countY = mCountY;
1478
1479 for (int y = 0; y < countY - (spanY - 1); y++) {
1480 inner:
1481 for (int x = 0; x < countX - (spanX - 1); x++) {
1482 // First, let's see if this thing fits anywhere
1483 for (int i = 0; i < spanX; i++) {
1484 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001485 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001486 continue inner;
1487 }
1488 }
1489 }
1490
1491 float distance = (float)
1492 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1493 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001494 computeDirectionVector(x - cellX, y - cellY, curDirection);
1495 // The direction score is just the dot product of the two candidate direction
1496 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001497 int curDirectionScore = direction[0] * curDirection[0] +
1498 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001499 boolean exactDirectionOnly = false;
1500 boolean directionMatches = direction[0] == curDirection[0] &&
1501 direction[0] == curDirection[0];
1502 if ((directionMatches || !exactDirectionOnly) &&
1503 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001504 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1505 bestDistance = distance;
1506 bestDirectionScore = curDirectionScore;
1507 bestXY[0] = x;
1508 bestXY[1] = y;
1509 }
1510 }
1511 }
1512
1513 // Return -1, -1 if no suitable location found
1514 if (bestDistance == Float.MAX_VALUE) {
1515 bestXY[0] = -1;
1516 bestXY[1] = -1;
1517 }
1518 return bestXY;
1519 }
1520
Adam Cohen47a876d2012-03-19 13:21:41 -07001521 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1522 int[] direction,boolean[][] occupied,
1523 boolean blockOccupied[][], int[] result) {
1524 // Keep track of best-scoring drop area
1525 final int[] bestXY = result != null ? result : new int[2];
1526 bestXY[0] = -1;
1527 bestXY[1] = -1;
1528 float bestDistance = Float.MAX_VALUE;
1529
1530 // We use this to march in a single direction
Adam Cohen5b53f292012-03-29 14:30:35 -07001531 if ((direction[0] != 0 && direction[1] != 0) ||
1532 (direction[0] == 0 && direction[1] == 0)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001533 return bestXY;
1534 }
1535
1536 // This will only incrememnet one of x or y based on the assertion above
1537 int x = cellX + direction[0];
1538 int y = cellY + direction[1];
1539 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1540
1541 boolean fail = false;
1542 for (int i = 0; i < spanX; i++) {
1543 for (int j = 0; j < spanY; j++) {
1544 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1545 fail = true;
1546 }
1547 }
1548 }
1549 if (!fail) {
1550 float distance = (float)
1551 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1552 if (Float.compare(distance, bestDistance) < 0) {
1553 bestDistance = distance;
1554 bestXY[0] = x;
1555 bestXY[1] = y;
1556 }
1557 }
1558 x += direction[0];
1559 y += direction[1];
1560 }
1561 return bestXY;
1562 }
1563
Adam Cohen482ed822012-03-02 14:15:13 -08001564 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001565 int[] direction, ItemConfiguration currentState) {
1566 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001567 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001568 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001569 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1570
Adam Cohen8baab352012-03-20 17:39:21 -07001571 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001572
1573 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001574 c.x = mTempLocation[0];
1575 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001576 success = true;
1577
1578 }
Adam Cohen8baab352012-03-20 17:39:21 -07001579 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001580 return success;
1581 }
1582
Adam Cohen47a876d2012-03-19 13:21:41 -07001583 // This method looks in the specified direction to see if there is an additional view
1584 // immediately adjecent in that direction
1585 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen19f37922012-03-21 11:59:11 -07001586 boolean[][] occupied, View dragView, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001587 boolean found = false;
1588
Michael Jurkaa52570f2012-03-20 03:18:20 -07001589 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001590 Rect r0 = new Rect(boundingRect);
1591 Rect r1 = new Rect();
1592
1593 int deltaX = 0;
1594 int deltaY = 0;
1595 if (direction[1] < 0) {
1596 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1597 deltaY = -1;
1598 } else if (direction[1] > 0) {
1599 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1600 deltaY = 1;
1601 } else if (direction[0] < 0) {
1602 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1603 deltaX = -1;
1604 } else if (direction[0] > 0) {
1605 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1606 deltaX = 1;
1607 }
1608
1609 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001610 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen19f37922012-03-21 11:59:11 -07001611 if (views.contains(child) || child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001612 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001613
Adam Cohen8baab352012-03-20 17:39:21 -07001614 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1615 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001616 if (Rect.intersects(r0, r1)) {
1617 if (!lp.canReorder) {
1618 return false;
1619 }
1620 boolean pushed = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001621 for (int x = c.x; x < c.x + c.spanX; x++) {
1622 for (int y = c.y; y < c.y + c.spanY; y++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001623 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1624 && y - deltaY >= 0 && y - deltaY < mCountY;
1625 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1626 pushed = true;
1627 }
1628 }
1629 }
1630 if (pushed) {
1631 views.add(child);
Adam Cohen8baab352012-03-20 17:39:21 -07001632 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001633 found = true;
1634 }
1635 }
1636 }
1637 return found;
1638 }
1639
Adam Cohen482ed822012-03-02 14:15:13 -08001640 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen19f37922012-03-21 11:59:11 -07001641 int[] direction, boolean push, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001642 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001643
Adam Cohen8baab352012-03-20 17:39:21 -07001644 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001645 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001646 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001647 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001648 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001649 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001650 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001651 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001652 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001653 }
1654 }
Adam Cohen8baab352012-03-20 17:39:21 -07001655
1656 @SuppressWarnings("unchecked")
1657 ArrayList<View> dup = (ArrayList<View>) views.clone();
1658 // We try and expand the group of views in the direction vector passed, based on
1659 // whether they are physically adjacent, ie. based on "push mechanics".
Adam Cohen19f37922012-03-21 11:59:11 -07001660 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied, dragView,
Adam Cohen8baab352012-03-20 17:39:21 -07001661 currentState)) {
1662 }
1663
1664 // Mark the occupied state as false for the group of views we want to move.
1665 for (View v: dup) {
1666 CellAndSpan c = currentState.map.get(v);
1667 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1668 }
1669
Adam Cohen47a876d2012-03-19 13:21:41 -07001670 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1671 int top = boundingRect.top;
1672 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001673 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
1674 // for tetris-style interlocking.
1675 for (View v: dup) {
1676 CellAndSpan c = currentState.map.get(v);
1677 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001678 }
1679
Adam Cohen482ed822012-03-02 14:15:13 -08001680 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1681
Adam Cohen8baab352012-03-20 17:39:21 -07001682 if (push) {
1683 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1684 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1685 } else {
1686 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1687 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1688 }
Adam Cohen482ed822012-03-02 14:15:13 -08001689
Adam Cohen8baab352012-03-20 17:39:21 -07001690 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001691 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001692 int deltaX = mTempLocation[0] - boundingRect.left;
1693 int deltaY = mTempLocation[1] - boundingRect.top;
1694 for (View v: dup) {
1695 CellAndSpan c = currentState.map.get(v);
1696 c.x += deltaX;
1697 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001698 }
1699 success = true;
1700 }
Adam Cohen8baab352012-03-20 17:39:21 -07001701
1702 // In either case, we set the occupied array as marked for the location of the views
1703 for (View v: dup) {
1704 CellAndSpan c = currentState.map.get(v);
1705 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001706 }
1707 return success;
1708 }
1709
1710 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1711 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1712 }
1713
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001714 // This method tries to find a reordering solution which satisfies the push mechanic by trying
1715 // to push items in each of the cardinal directions, in an order based on the direction vector
1716 // passed.
1717 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
1718 int[] direction, View ignoreView, ItemConfiguration solution) {
1719 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
1720 // If the direction vector has two non-zero components, we try pushing
1721 // separately in each of the components.
1722 int temp = direction[1];
1723 direction[1] = 0;
1724 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1725 ignoreView, solution)) {
1726 return true;
1727 }
1728 direction[1] = temp;
1729 temp = direction[0];
1730 direction[0] = 0;
1731 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1732 ignoreView, solution)) {
1733 return true;
1734 }
1735 // Revert the direction
1736 direction[0] = temp;
1737
1738 // Now we try pushing in each component of the opposite direction
1739 direction[0] *= -1;
1740 direction[1] *= -1;
1741 temp = direction[1];
1742 direction[1] = 0;
1743 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1744 ignoreView, solution)) {
1745 return true;
1746 }
1747
1748 direction[1] = temp;
1749 temp = direction[0];
1750 direction[0] = 0;
1751 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1752 ignoreView, solution)) {
1753 return true;
1754 }
1755 // revert the direction
1756 direction[0] = temp;
1757 direction[0] *= -1;
1758 direction[1] *= -1;
1759
1760 } else {
1761 // If the direction vector has a single non-zero component, we push first in the
1762 // direction of the vector
1763 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1764 ignoreView, solution)) {
1765 return true;
1766 }
1767
1768 // Then we try the opposite direction
1769 direction[0] *= -1;
1770 direction[1] *= -1;
1771 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1772 ignoreView, solution)) {
1773 return true;
1774 }
1775 // Switch the direction back
1776 direction[0] *= -1;
1777 direction[1] *= -1;
1778
1779 // If we have failed to find a push solution with the above, then we try
1780 // to find a solution by pushing along the perpendicular axis.
1781
1782 // Swap the components
1783 int temp = direction[1];
1784 direction[1] = direction[0];
1785 direction[0] = temp;
1786 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1787 ignoreView, solution)) {
1788 return true;
1789 }
1790
1791 // Then we try the opposite direction
1792 direction[0] *= -1;
1793 direction[1] *= -1;
1794 if (addViewsToTempLocation(intersectingViews, occupied, direction, true,
1795 ignoreView, solution)) {
1796 return true;
1797 }
1798 // Switch the direction back
1799 direction[0] *= -1;
1800 direction[1] *= -1;
1801
1802 // Swap the components back
1803 temp = direction[1];
1804 direction[1] = direction[0];
1805 direction[0] = temp;
1806 }
1807 return false;
1808 }
1809
Adam Cohen482ed822012-03-02 14:15:13 -08001810 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001811 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001812 // Return early if get invalid cell positions
1813 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001814
Adam Cohen8baab352012-03-20 17:39:21 -07001815 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001816 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001817
Adam Cohen8baab352012-03-20 17:39:21 -07001818 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001819 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001820 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001821 if (c != null) {
1822 c.x = cellX;
1823 c.y = cellY;
1824 }
Adam Cohen482ed822012-03-02 14:15:13 -08001825 }
Adam Cohen482ed822012-03-02 14:15:13 -08001826 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1827 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001828 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001829 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001830 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001831 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001832 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001833 if (Rect.intersects(r0, r1)) {
1834 if (!lp.canReorder) {
1835 return false;
1836 }
1837 mIntersectingViews.add(child);
1838 }
1839 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001840
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001841 // First we try to find a solution which respects the push mechanic. That is,
1842 // we try to find a solution such that no displaced item travels through another item
1843 // without also displacing that item.
1844 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07001845 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001846 return true;
1847 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001848
Adam Cohen4abc5bd2012-05-29 21:06:03 -07001849 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohen19f37922012-03-21 11:59:11 -07001850 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, ignoreView,
1851 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001852 return true;
1853 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001854
Adam Cohen482ed822012-03-02 14:15:13 -08001855 // Ok, they couldn't move as a block, let's move them individually
1856 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001857 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001858 return false;
1859 }
1860 }
1861 return true;
1862 }
1863
1864 /*
1865 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1866 * the provided point and the provided cell
1867 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001868 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001869 double angle = Math.atan(((float) deltaY) / deltaX);
1870
1871 result[0] = 0;
1872 result[1] = 0;
1873 if (Math.abs(Math.cos(angle)) > 0.5f) {
1874 result[0] = (int) Math.signum(deltaX);
1875 }
1876 if (Math.abs(Math.sin(angle)) > 0.5f) {
1877 result[1] = (int) Math.signum(deltaY);
1878 }
1879 }
1880
Adam Cohen8baab352012-03-20 17:39:21 -07001881 private void copyOccupiedArray(boolean[][] occupied) {
1882 for (int i = 0; i < mCountX; i++) {
1883 for (int j = 0; j < mCountY; j++) {
1884 occupied[i][j] = mOccupied[i][j];
1885 }
1886 }
1887 }
1888
Adam Cohen482ed822012-03-02 14:15:13 -08001889 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1890 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001891 // Copy the current state into the solution. This solution will be manipulated as necessary.
1892 copyCurrentStateToSolution(solution, false);
1893 // Copy the current occupied array into the temporary occupied array. This array will be
1894 // manipulated as necessary to find a solution.
1895 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001896
1897 // We find the nearest cell into which we would place the dragged item, assuming there's
1898 // nothing in its way.
1899 int result[] = new int[2];
1900 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1901
1902 boolean success = false;
1903 // First we try the exact nearest position of the item being dragged,
1904 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001905 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1906 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001907
1908 if (!success) {
1909 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1910 // x, then 1 in y etc.
1911 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1912 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1913 dragView, false, solution);
1914 } else if (spanY > minSpanY) {
1915 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1916 dragView, true, solution);
1917 }
1918 solution.isSolution = false;
1919 } else {
1920 solution.isSolution = true;
1921 solution.dragViewX = result[0];
1922 solution.dragViewY = result[1];
1923 solution.dragViewSpanX = spanX;
1924 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001925 }
1926 return solution;
1927 }
1928
1929 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001930 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001931 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001932 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001933 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001934 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001935 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001936 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001937 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001938 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001939 }
Adam Cohen8baab352012-03-20 17:39:21 -07001940 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001941 }
1942 }
1943
1944 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1945 for (int i = 0; i < mCountX; i++) {
1946 for (int j = 0; j < mCountY; j++) {
1947 mTmpOccupied[i][j] = false;
1948 }
1949 }
1950
Michael Jurkaa52570f2012-03-20 03:18:20 -07001951 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001952 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001953 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001954 if (child == dragView) continue;
1955 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001956 CellAndSpan c = solution.map.get(child);
1957 if (c != null) {
1958 lp.tmpCellX = c.x;
1959 lp.tmpCellY = c.y;
1960 lp.cellHSpan = c.spanX;
1961 lp.cellVSpan = c.spanY;
1962 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001963 }
1964 }
1965 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1966 solution.dragViewSpanY, mTmpOccupied, true);
1967 }
1968
1969 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1970 commitDragView) {
1971
1972 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1973 for (int i = 0; i < mCountX; i++) {
1974 for (int j = 0; j < mCountY; j++) {
1975 occupied[i][j] = false;
1976 }
1977 }
1978
Michael Jurkaa52570f2012-03-20 03:18:20 -07001979 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001980 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001981 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001982 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001983 CellAndSpan c = solution.map.get(child);
1984 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07001985 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
1986 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001987 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001988 }
1989 }
1990 if (commitDragView) {
1991 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1992 solution.dragViewSpanY, occupied, true);
1993 }
1994 }
1995
Adam Cohen19f37922012-03-21 11:59:11 -07001996 // This method starts or changes the reorder hint animations
1997 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
1998 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07001999 for (int i = 0; i < childCount; i++) {
2000 View child = mShortcutsAndWidgets.getChildAt(i);
2001 if (child == dragView) continue;
2002 CellAndSpan c = solution.map.get(child);
2003 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2004 if (c != null) {
2005 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2006 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002007 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002008 }
2009 }
2010 }
2011
2012 // Class which represents the reorder hint animations. These animations show that an item is
2013 // in a temporary state, and hint at where the item will return to.
2014 class ReorderHintAnimation {
2015 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002016 float finalDeltaX;
2017 float finalDeltaY;
2018 float initDeltaX;
2019 float initDeltaY;
2020 float finalScale;
2021 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002022 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002023 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002024
2025 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2026 int spanX, int spanY) {
2027 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2028 final int x0 = mTmpPoint[0];
2029 final int y0 = mTmpPoint[1];
2030 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2031 final int x1 = mTmpPoint[0];
2032 final int y1 = mTmpPoint[1];
2033 final int dX = x1 - x0;
2034 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002035 finalDeltaX = 0;
2036 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002037 if (dX == dY && dX == 0) {
2038 } else {
2039 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002040 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002041 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002042 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002043 } else {
2044 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002045 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002046 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002047 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002048 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002049 }
2050 }
Adam Cohend024f982012-05-23 18:26:45 -07002051 initDeltaX = child.getTranslationX();
2052 initDeltaY = child.getTranslationY();
2053 finalScale = 1.0f - 4.0f / child.getWidth();
2054 initScale = child.getScaleX();
2055
Brandon Keely50e6e562012-05-08 16:28:49 -07002056 child.setPivotY(child.getMeasuredHeight() * 0.5f);
2057 child.setPivotX(child.getMeasuredWidth() * 0.5f);
Adam Cohen19f37922012-03-21 11:59:11 -07002058 this.child = child;
2059 }
2060
Adam Cohend024f982012-05-23 18:26:45 -07002061 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002062 if (mShakeAnimators.containsKey(child)) {
2063 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002064 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002065 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002066 if (finalDeltaX == 0 && finalDeltaY == 0) {
2067 completeAnimationImmediately();
2068 return;
2069 }
Adam Cohen19f37922012-03-21 11:59:11 -07002070 }
Adam Cohend024f982012-05-23 18:26:45 -07002071 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002072 return;
2073 }
Michael Jurka2ecf9952012-06-18 12:52:28 -07002074 ValueAnimator va = LauncherAnimUtils.ofFloat(0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002075 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002076 va.setRepeatMode(ValueAnimator.REVERSE);
2077 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002078 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002079 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002080 va.addUpdateListener(new AnimatorUpdateListener() {
2081 @Override
2082 public void onAnimationUpdate(ValueAnimator animation) {
2083 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002084 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2085 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002086 child.setTranslationX(x);
2087 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002088 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002089 child.setScaleX(s);
2090 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002091 }
2092 });
2093 va.addListener(new AnimatorListenerAdapter() {
2094 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002095 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002096 initDeltaX = 0;
2097 initDeltaY = 0;
2098 initScale = 1.0f;
Adam Cohen19f37922012-03-21 11:59:11 -07002099 }
2100 });
Adam Cohen19f37922012-03-21 11:59:11 -07002101 mShakeAnimators.put(child, this);
2102 va.start();
2103 }
2104
Adam Cohend024f982012-05-23 18:26:45 -07002105 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002106 if (a != null) {
2107 a.cancel();
2108 }
Adam Cohen19f37922012-03-21 11:59:11 -07002109 }
Adam Cohene7587d22012-05-24 18:50:02 -07002110
Brandon Keely50e6e562012-05-08 16:28:49 -07002111 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002112 if (a != null) {
2113 a.cancel();
2114 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002115
Michael Jurka2ecf9952012-06-18 12:52:28 -07002116 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002117 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002118 s.playTogether(
Michael Jurka2ecf9952012-06-18 12:52:28 -07002119 LauncherAnimUtils.ofFloat(child, "scaleX", 1f),
2120 LauncherAnimUtils.ofFloat(child, "scaleY", 1f),
2121 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2122 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002123 );
2124 s.setDuration(REORDER_ANIMATION_DURATION);
2125 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2126 s.start();
2127 }
Adam Cohen19f37922012-03-21 11:59:11 -07002128 }
2129
2130 private void completeAndClearReorderHintAnimations() {
2131 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002132 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002133 }
2134 mShakeAnimators.clear();
2135 }
2136
Adam Cohen482ed822012-03-02 14:15:13 -08002137 private void commitTempPlacement() {
2138 for (int i = 0; i < mCountX; i++) {
2139 for (int j = 0; j < mCountY; j++) {
2140 mOccupied[i][j] = mTmpOccupied[i][j];
2141 }
2142 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002143 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002144 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002145 View child = mShortcutsAndWidgets.getChildAt(i);
2146 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2147 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002148 // We do a null check here because the item info can be null in the case of the
2149 // AllApps button in the hotseat.
2150 if (info != null) {
2151 info.cellX = lp.cellX = lp.tmpCellX;
2152 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002153 info.spanX = lp.cellHSpan;
2154 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002155 }
Adam Cohen482ed822012-03-02 14:15:13 -08002156 }
Adam Cohen2acce882012-03-28 19:03:19 -07002157 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002158 }
2159
2160 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002161 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002162 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002163 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002164 lp.useTmpCoords = useTempCoords;
2165 }
2166 }
2167
Adam Cohen482ed822012-03-02 14:15:13 -08002168 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2169 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2170 int[] result = new int[2];
2171 int[] resultSpan = new int[2];
2172 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2173 resultSpan);
2174 if (result[0] >= 0 && result[1] >= 0) {
2175 copyCurrentStateToSolution(solution, false);
2176 solution.dragViewX = result[0];
2177 solution.dragViewY = result[1];
2178 solution.dragViewSpanX = resultSpan[0];
2179 solution.dragViewSpanY = resultSpan[1];
2180 solution.isSolution = true;
2181 } else {
2182 solution.isSolution = false;
2183 }
2184 return solution;
2185 }
2186
2187 public void prepareChildForDrag(View child) {
2188 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002189 }
2190
Adam Cohen19f37922012-03-21 11:59:11 -07002191 /* This seems like it should be obvious and straight-forward, but when the direction vector
2192 needs to match with the notion of the dragView pushing other views, we have to employ
2193 a slightly more subtle notion of the direction vector. The question is what two points is
2194 the vector between? The center of the dragView and its desired destination? Not quite, as
2195 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2196 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2197 or right, which helps make pushing feel right.
2198 */
2199 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2200 int spanY, View dragView, int[] resultDirection) {
2201 int[] targetDestination = new int[2];
2202
2203 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2204 Rect dragRect = new Rect();
2205 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2206 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2207
2208 Rect dropRegionRect = new Rect();
2209 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2210 dragView, dropRegionRect, mIntersectingViews);
2211
2212 int dropRegionSpanX = dropRegionRect.width();
2213 int dropRegionSpanY = dropRegionRect.height();
2214
2215 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2216 dropRegionRect.height(), dropRegionRect);
2217
2218 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2219 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2220
2221 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2222 deltaX = 0;
2223 }
2224 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2225 deltaY = 0;
2226 }
2227
2228 if (deltaX == 0 && deltaY == 0) {
2229 // No idea what to do, give a random direction.
2230 resultDirection[0] = 1;
2231 resultDirection[1] = 0;
2232 } else {
2233 computeDirectionVector(deltaX, deltaY, resultDirection);
2234 }
2235 }
2236
2237 // For a given cell and span, fetch the set of views intersecting the region.
2238 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2239 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2240 if (boundingRect != null) {
2241 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2242 }
2243 intersectingViews.clear();
2244 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2245 Rect r1 = new Rect();
2246 final int count = mShortcutsAndWidgets.getChildCount();
2247 for (int i = 0; i < count; i++) {
2248 View child = mShortcutsAndWidgets.getChildAt(i);
2249 if (child == dragView) continue;
2250 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2251 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2252 if (Rect.intersects(r0, r1)) {
2253 mIntersectingViews.add(child);
2254 if (boundingRect != null) {
2255 boundingRect.union(r1);
2256 }
2257 }
2258 }
2259 }
2260
2261 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2262 View dragView, int[] result) {
2263 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2264 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2265 mIntersectingViews);
2266 return !mIntersectingViews.isEmpty();
2267 }
2268
2269 void revertTempState() {
2270 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2271 final int count = mShortcutsAndWidgets.getChildCount();
2272 for (int i = 0; i < count; i++) {
2273 View child = mShortcutsAndWidgets.getChildAt(i);
2274 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2275 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2276 lp.tmpCellX = lp.cellX;
2277 lp.tmpCellY = lp.cellY;
2278 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2279 0, false, false);
2280 }
2281 }
2282 completeAndClearReorderHintAnimations();
2283 setItemPlacementDirty(false);
2284 }
2285
Adam Cohenbebf0422012-04-11 18:06:28 -07002286 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2287 View dragView, int[] direction, boolean commit) {
2288 int[] pixelXY = new int[2];
2289 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2290
2291 // First we determine if things have moved enough to cause a different layout
2292 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2293 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2294
2295 setUseTempCoords(true);
2296 if (swapSolution != null && swapSolution.isSolution) {
2297 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2298 // committing anything or animating anything as we just want to determine if a solution
2299 // exists
2300 copySolutionToTempState(swapSolution, dragView);
2301 setItemPlacementDirty(true);
2302 animateItemsToSolution(swapSolution, dragView, commit);
2303
2304 if (commit) {
2305 commitTempPlacement();
2306 completeAndClearReorderHintAnimations();
2307 setItemPlacementDirty(false);
2308 } else {
2309 beginOrAdjustHintAnimations(swapSolution, dragView,
2310 REORDER_ANIMATION_DURATION);
2311 }
2312 mShortcutsAndWidgets.requestLayout();
2313 }
2314 return swapSolution.isSolution;
2315 }
2316
Adam Cohen482ed822012-03-02 14:15:13 -08002317 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2318 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002319 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002320 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002321
2322 if (resultSpan == null) {
2323 resultSpan = new int[2];
2324 }
2325
Adam Cohen19f37922012-03-21 11:59:11 -07002326 // When we are checking drop validity or actually dropping, we don't recompute the
2327 // direction vector, since we want the solution to match the preview, and it's possible
2328 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002329 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2330 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002331 mDirectionVector[0] = mPreviousReorderDirection[0];
2332 mDirectionVector[1] = mPreviousReorderDirection[1];
2333 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002334 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2335 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2336 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002337 }
2338 } else {
2339 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2340 mPreviousReorderDirection[0] = mDirectionVector[0];
2341 mPreviousReorderDirection[1] = mDirectionVector[1];
2342 }
2343
Adam Cohen482ed822012-03-02 14:15:13 -08002344 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2345 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2346
2347 // We attempt the approach which doesn't shuffle views at all
2348 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2349 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2350
2351 ItemConfiguration finalSolution = null;
2352 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2353 finalSolution = swapSolution;
2354 } else if (noShuffleSolution.isSolution) {
2355 finalSolution = noShuffleSolution;
2356 }
2357
2358 boolean foundSolution = true;
2359 if (!DESTRUCTIVE_REORDER) {
2360 setUseTempCoords(true);
2361 }
2362
2363 if (finalSolution != null) {
2364 result[0] = finalSolution.dragViewX;
2365 result[1] = finalSolution.dragViewY;
2366 resultSpan[0] = finalSolution.dragViewSpanX;
2367 resultSpan[1] = finalSolution.dragViewSpanY;
2368
2369 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2370 // committing anything or animating anything as we just want to determine if a solution
2371 // exists
2372 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2373 if (!DESTRUCTIVE_REORDER) {
2374 copySolutionToTempState(finalSolution, dragView);
2375 }
2376 setItemPlacementDirty(true);
2377 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2378
Adam Cohen19f37922012-03-21 11:59:11 -07002379 if (!DESTRUCTIVE_REORDER &&
2380 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002381 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002382 completeAndClearReorderHintAnimations();
2383 setItemPlacementDirty(false);
2384 } else {
2385 beginOrAdjustHintAnimations(finalSolution, dragView,
2386 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002387 }
2388 }
2389 } else {
2390 foundSolution = false;
2391 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2392 }
2393
2394 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2395 setUseTempCoords(false);
2396 }
Adam Cohen482ed822012-03-02 14:15:13 -08002397
Michael Jurkaa52570f2012-03-20 03:18:20 -07002398 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002399 return result;
2400 }
2401
Adam Cohen19f37922012-03-21 11:59:11 -07002402 void setItemPlacementDirty(boolean dirty) {
2403 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002404 }
Adam Cohen19f37922012-03-21 11:59:11 -07002405 boolean isItemPlacementDirty() {
2406 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002407 }
2408
2409 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002410 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002411 boolean isSolution = false;
2412 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2413
2414 int area() {
2415 return dragViewSpanX * dragViewSpanY;
2416 }
Adam Cohen8baab352012-03-20 17:39:21 -07002417 }
2418
2419 private class CellAndSpan {
2420 int x, y;
2421 int spanX, spanY;
2422
2423 public CellAndSpan(int x, int y, int spanX, int spanY) {
2424 this.x = x;
2425 this.y = y;
2426 this.spanX = spanX;
2427 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002428 }
2429 }
2430
Adam Cohendf035382011-04-11 17:22:04 -07002431 /**
2432 * Find a vacant area that will fit the given bounds nearest the requested
2433 * cell location. Uses Euclidean distance to score multiple vacant areas.
2434 *
2435 * @param pixelX The X location at which you want to search for a vacant area.
2436 * @param pixelY The Y location at which you want to search for a vacant area.
2437 * @param spanX Horizontal span of the object.
2438 * @param spanY Vertical span of the object.
2439 * @param ignoreView Considers space occupied by this view as unoccupied
2440 * @param result Previously returned value to possibly recycle.
2441 * @return The X, Y cell of a vacant area that can contain this object,
2442 * nearest the requested location.
2443 */
2444 int[] findNearestVacantArea(
2445 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2446 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2447 }
2448
2449 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002450 * Find a vacant area that will fit the given bounds nearest the requested
2451 * cell location. Uses Euclidean distance to score multiple vacant areas.
2452 *
2453 * @param pixelX The X location at which you want to search for a vacant area.
2454 * @param pixelY The Y location at which you want to search for a vacant area.
2455 * @param minSpanX The minimum horizontal span required
2456 * @param minSpanY The minimum vertical span required
2457 * @param spanX Horizontal span of the object.
2458 * @param spanY Vertical span of the object.
2459 * @param ignoreView Considers space occupied by this view as unoccupied
2460 * @param result Previously returned value to possibly recycle.
2461 * @return The X, Y cell of a vacant area that can contain this object,
2462 * nearest the requested location.
2463 */
2464 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2465 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002466 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2467 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002468 }
2469
2470 /**
Adam Cohendf035382011-04-11 17:22:04 -07002471 * Find a starting cell position that will fit the given bounds nearest the requested
2472 * cell location. Uses Euclidean distance to score multiple vacant areas.
2473 *
2474 * @param pixelX The X location at which you want to search for a vacant area.
2475 * @param pixelY The Y location at which you want to search for a vacant area.
2476 * @param spanX Horizontal span of the object.
2477 * @param spanY Vertical span of the object.
2478 * @param ignoreView Considers space occupied by this view as unoccupied
2479 * @param result Previously returned value to possibly recycle.
2480 * @return The X, Y cell of a vacant area that can contain this object,
2481 * nearest the requested location.
2482 */
2483 int[] findNearestArea(
2484 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2485 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2486 }
2487
Michael Jurka0280c3b2010-09-17 15:00:07 -07002488 boolean existsEmptyCell() {
2489 return findCellForSpan(null, 1, 1);
2490 }
2491
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002493 * Finds the upper-left coordinate of the first rectangle in the grid that can
2494 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2495 * then this method will only return coordinates for rectangles that contain the cell
2496 * (intersectX, intersectY)
2497 *
2498 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2499 * can be found.
2500 * @param spanX The horizontal span of the cell we want to find.
2501 * @param spanY The vertical span of the cell we want to find.
2502 *
2503 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002504 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002505 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002506 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002507 }
2508
2509 /**
2510 * Like above, but ignores any cells occupied by the item "ignoreView"
2511 *
2512 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2513 * can be found.
2514 * @param spanX The horizontal span of the cell we want to find.
2515 * @param spanY The vertical span of the cell we want to find.
2516 * @param ignoreView The home screen item we should treat as not occupying any space
2517 * @return
2518 */
2519 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002520 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2521 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002522 }
2523
2524 /**
2525 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2526 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2527 *
2528 * @param spanX The horizontal span of the cell we want to find.
2529 * @param spanY The vertical span of the cell we want to find.
2530 * @param ignoreView The home screen item we should treat as not occupying any space
2531 * @param intersectX The X coordinate of the cell that we should try to overlap
2532 * @param intersectX The Y coordinate of the cell that we should try to overlap
2533 *
2534 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2535 */
2536 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2537 int intersectX, int intersectY) {
2538 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002539 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002540 }
2541
2542 /**
2543 * The superset of the above two methods
2544 */
2545 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002546 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002547 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002548 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002549
Michael Jurka28750fb2010-09-24 17:43:49 -07002550 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002551 while (true) {
2552 int startX = 0;
2553 if (intersectX >= 0) {
2554 startX = Math.max(startX, intersectX - (spanX - 1));
2555 }
2556 int endX = mCountX - (spanX - 1);
2557 if (intersectX >= 0) {
2558 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2559 }
2560 int startY = 0;
2561 if (intersectY >= 0) {
2562 startY = Math.max(startY, intersectY - (spanY - 1));
2563 }
2564 int endY = mCountY - (spanY - 1);
2565 if (intersectY >= 0) {
2566 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2567 }
2568
Winson Chungbbc60d82010-11-11 16:34:41 -08002569 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002570 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002571 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002572 for (int i = 0; i < spanX; i++) {
2573 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002574 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002575 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002576 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002577 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002578 continue inner;
2579 }
2580 }
2581 }
2582 if (cellXY != null) {
2583 cellXY[0] = x;
2584 cellXY[1] = y;
2585 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002586 foundCell = true;
2587 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002588 }
2589 }
2590 if (intersectX == -1 && intersectY == -1) {
2591 break;
2592 } else {
2593 // if we failed to find anything, try again but without any requirements of
2594 // intersecting
2595 intersectX = -1;
2596 intersectY = -1;
2597 continue;
2598 }
2599 }
2600
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002601 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002602 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002603 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002604 }
2605
2606 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002607 * A drag event has begun over this layout.
2608 * It may have begun over this layout (in which case onDragChild is called first),
2609 * or it may have begun on another layout.
2610 */
2611 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002612 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002613 mDragging = true;
2614 }
2615
2616 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002617 * Called when drag has left this CellLayout or has been completed (successfully or not)
2618 */
2619 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002620 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002621 // This can actually be called when we aren't in a drag, e.g. when adding a new
2622 // item to this layout via the customize drawer.
2623 // Guard against that case.
2624 if (mDragging) {
2625 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002626 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002627
2628 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002629 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002630 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2631 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002632 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002633 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002634 }
2635
2636 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002637 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002638 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002639 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002640 *
2641 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002642 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002643 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002644 if (child != null) {
2645 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002646 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002647 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002649 }
2650
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002651 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002652 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002653 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002654 * @param cellX X coordinate of upper left corner expressed as a cell position
2655 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002656 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002657 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002658 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002659 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002660 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002661 final int cellWidth = mCellWidth;
2662 final int cellHeight = mCellHeight;
2663 final int widthGap = mWidthGap;
2664 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002665
Winson Chung4b825dcd2011-06-19 12:41:22 -07002666 final int hStartPadding = getPaddingLeft();
2667 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002669 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2670 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2671
2672 int x = hStartPadding + cellX * (cellWidth + widthGap);
2673 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002674
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002675 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002676 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002677
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002678 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002679 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002681 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002682 * @param width Width in pixels
2683 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002684 * @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 -08002685 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002686 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002687 return rectToCell(getResources(), width, height, result);
2688 }
2689
2690 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002691 // Always assume we're working with the smallest span to make sure we
2692 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002693 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2694 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002695 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002696
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002698 int spanX = (int) Math.ceil(width / (float) smallerSize);
2699 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002700
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002701 if (result == null) {
2702 return new int[] { spanX, spanY };
2703 }
2704 result[0] = spanX;
2705 result[1] = spanY;
2706 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002707 }
2708
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002709 public int[] cellSpansToSize(int hSpans, int vSpans) {
2710 int[] size = new int[2];
2711 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2712 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2713 return size;
2714 }
2715
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002716 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002717 * Calculate the grid spans needed to fit given item
2718 */
2719 public void calculateSpans(ItemInfo info) {
2720 final int minWidth;
2721 final int minHeight;
2722
2723 if (info instanceof LauncherAppWidgetInfo) {
2724 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2725 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2726 } else if (info instanceof PendingAddWidgetInfo) {
2727 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2728 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2729 } else {
2730 // It's not a widget, so it must be 1x1
2731 info.spanX = info.spanY = 1;
2732 return;
2733 }
2734 int[] spans = rectToCell(minWidth, minHeight, null);
2735 info.spanX = spans[0];
2736 info.spanY = spans[1];
2737 }
2738
2739 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002740 * Find the first vacant cell, if there is one.
2741 *
2742 * @param vacant Holds the x and y coordinate of the vacant cell
2743 * @param spanX Horizontal cell span.
2744 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002745 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002746 * @return True if a vacant cell was found
2747 */
2748 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002749
Michael Jurka0280c3b2010-09-17 15:00:07 -07002750 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002751 }
2752
2753 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2754 int xCount, int yCount, boolean[][] occupied) {
2755
Adam Cohen2801caf2011-05-13 20:57:39 -07002756 for (int y = 0; y < yCount; y++) {
2757 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002758 boolean available = !occupied[x][y];
2759out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2760 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2761 available = available && !occupied[i][j];
2762 if (!available) break out;
2763 }
2764 }
2765
2766 if (available) {
2767 vacant[0] = x;
2768 vacant[1] = y;
2769 return true;
2770 }
2771 }
2772 }
2773
2774 return false;
2775 }
2776
Michael Jurka0280c3b2010-09-17 15:00:07 -07002777 private void clearOccupiedCells() {
2778 for (int x = 0; x < mCountX; x++) {
2779 for (int y = 0; y < mCountY; y++) {
2780 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002781 }
2782 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002783 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784
Adam Cohend41fbf52012-02-16 23:53:59 -08002785 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002786 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002787 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002788 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789
Adam Cohend4844c32011-02-18 19:25:06 -08002790 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002791 markCellsAsOccupiedForView(view, mOccupied);
2792 }
2793 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002794 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002795 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002796 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002797 }
2798
Adam Cohend4844c32011-02-18 19:25:06 -08002799 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002800 markCellsAsUnoccupiedForView(view, mOccupied);
2801 }
2802 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002803 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002804 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002805 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002806 }
2807
Adam Cohen482ed822012-03-02 14:15:13 -08002808 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2809 boolean value) {
2810 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002811 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2812 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002813 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002814 }
2815 }
2816 }
2817
Adam Cohen2801caf2011-05-13 20:57:39 -07002818 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002819 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002820 (Math.max((mCountX - 1), 0) * mWidthGap);
2821 }
2822
2823 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002824 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002825 (Math.max((mCountY - 1), 0) * mHeightGap);
2826 }
2827
Michael Jurka66d72172011-04-12 16:29:25 -07002828 public boolean isOccupied(int x, int y) {
2829 if (x < mCountX && y < mCountY) {
2830 return mOccupied[x][y];
2831 } else {
2832 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2833 }
2834 }
2835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 @Override
2837 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2838 return new CellLayout.LayoutParams(getContext(), attrs);
2839 }
2840
2841 @Override
2842 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2843 return p instanceof CellLayout.LayoutParams;
2844 }
2845
2846 @Override
2847 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2848 return new CellLayout.LayoutParams(p);
2849 }
2850
Winson Chungaafa03c2010-06-11 17:34:16 -07002851 public static class CellLayoutAnimationController extends LayoutAnimationController {
2852 public CellLayoutAnimationController(Animation animation, float delay) {
2853 super(animation, delay);
2854 }
2855
2856 @Override
2857 protected long getDelayForView(View view) {
2858 return (int) (Math.random() * 150);
2859 }
2860 }
2861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002862 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2863 /**
2864 * Horizontal location of the item in the grid.
2865 */
2866 @ViewDebug.ExportedProperty
2867 public int cellX;
2868
2869 /**
2870 * Vertical location of the item in the grid.
2871 */
2872 @ViewDebug.ExportedProperty
2873 public int cellY;
2874
2875 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002876 * Temporary horizontal location of the item in the grid during reorder
2877 */
2878 public int tmpCellX;
2879
2880 /**
2881 * Temporary vertical location of the item in the grid during reorder
2882 */
2883 public int tmpCellY;
2884
2885 /**
2886 * Indicates that the temporary coordinates should be used to layout the items
2887 */
2888 public boolean useTmpCoords;
2889
2890 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002891 * Number of cells spanned horizontally by the item.
2892 */
2893 @ViewDebug.ExportedProperty
2894 public int cellHSpan;
2895
2896 /**
2897 * Number of cells spanned vertically by the item.
2898 */
2899 @ViewDebug.ExportedProperty
2900 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002901
Adam Cohen1b607ed2011-03-03 17:26:50 -08002902 /**
2903 * Indicates whether the item will set its x, y, width and height parameters freely,
2904 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2905 */
Adam Cohend4844c32011-02-18 19:25:06 -08002906 public boolean isLockedToGrid = true;
2907
Adam Cohen482ed822012-03-02 14:15:13 -08002908 /**
2909 * Indicates whether this item can be reordered. Always true except in the case of the
2910 * the AllApps button.
2911 */
2912 public boolean canReorder = true;
2913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002914 // X coordinate of the view in the layout.
2915 @ViewDebug.ExportedProperty
2916 int x;
2917 // Y coordinate of the view in the layout.
2918 @ViewDebug.ExportedProperty
2919 int y;
2920
Romain Guy84f296c2009-11-04 15:00:44 -08002921 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002923 public LayoutParams(Context c, AttributeSet attrs) {
2924 super(c, attrs);
2925 cellHSpan = 1;
2926 cellVSpan = 1;
2927 }
2928
2929 public LayoutParams(ViewGroup.LayoutParams source) {
2930 super(source);
2931 cellHSpan = 1;
2932 cellVSpan = 1;
2933 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002934
2935 public LayoutParams(LayoutParams source) {
2936 super(source);
2937 this.cellX = source.cellX;
2938 this.cellY = source.cellY;
2939 this.cellHSpan = source.cellHSpan;
2940 this.cellVSpan = source.cellVSpan;
2941 }
2942
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002943 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002944 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002945 this.cellX = cellX;
2946 this.cellY = cellY;
2947 this.cellHSpan = cellHSpan;
2948 this.cellVSpan = cellVSpan;
2949 }
2950
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002951 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002952 if (isLockedToGrid) {
2953 final int myCellHSpan = cellHSpan;
2954 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002955 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2956 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002957
Adam Cohend4844c32011-02-18 19:25:06 -08002958 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2959 leftMargin - rightMargin;
2960 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2961 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002962 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2963 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002964 }
2965 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002966
Winson Chungaafa03c2010-06-11 17:34:16 -07002967 public String toString() {
2968 return "(" + this.cellX + ", " + this.cellY + ")";
2969 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002970
2971 public void setWidth(int width) {
2972 this.width = width;
2973 }
2974
2975 public int getWidth() {
2976 return width;
2977 }
2978
2979 public void setHeight(int height) {
2980 this.height = height;
2981 }
2982
2983 public int getHeight() {
2984 return height;
2985 }
2986
2987 public void setX(int x) {
2988 this.x = x;
2989 }
2990
2991 public int getX() {
2992 return x;
2993 }
2994
2995 public void setY(int y) {
2996 this.y = y;
2997 }
2998
2999 public int getY() {
3000 return y;
3001 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003002 }
3003
Michael Jurka0280c3b2010-09-17 15:00:07 -07003004 // This class stores info for two purposes:
3005 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3006 // its spanX, spanY, and the screen it is on
3007 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3008 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3009 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003010 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003011 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003012 int cellX = -1;
3013 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014 int spanX;
3015 int spanY;
3016 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07003017 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003018
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003019 @Override
3020 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003021 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3022 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003023 }
3024 }
Michael Jurkad771c962011-08-09 15:00:48 -07003025
3026 public boolean lastDownOnOccupiedCell() {
3027 return mLastDownOnOccupiedCell;
3028 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003029}