blob: c993f0e5b705e64c36674fe158e2f8127d848640 [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) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002151 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2152 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2153 info.requiresDbUpdate = true;
2154 }
Adam Cohen2acce882012-03-28 19:03:19 -07002155 info.cellX = lp.cellX = lp.tmpCellX;
2156 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002157 info.spanX = lp.cellHSpan;
2158 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002159 }
Adam Cohen482ed822012-03-02 14:15:13 -08002160 }
Adam Cohen2acce882012-03-28 19:03:19 -07002161 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002162 }
2163
2164 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002165 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002166 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002167 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002168 lp.useTmpCoords = useTempCoords;
2169 }
2170 }
2171
Adam Cohen482ed822012-03-02 14:15:13 -08002172 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2173 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2174 int[] result = new int[2];
2175 int[] resultSpan = new int[2];
2176 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2177 resultSpan);
2178 if (result[0] >= 0 && result[1] >= 0) {
2179 copyCurrentStateToSolution(solution, false);
2180 solution.dragViewX = result[0];
2181 solution.dragViewY = result[1];
2182 solution.dragViewSpanX = resultSpan[0];
2183 solution.dragViewSpanY = resultSpan[1];
2184 solution.isSolution = true;
2185 } else {
2186 solution.isSolution = false;
2187 }
2188 return solution;
2189 }
2190
2191 public void prepareChildForDrag(View child) {
2192 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002193 }
2194
Adam Cohen19f37922012-03-21 11:59:11 -07002195 /* This seems like it should be obvious and straight-forward, but when the direction vector
2196 needs to match with the notion of the dragView pushing other views, we have to employ
2197 a slightly more subtle notion of the direction vector. The question is what two points is
2198 the vector between? The center of the dragView and its desired destination? Not quite, as
2199 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2200 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2201 or right, which helps make pushing feel right.
2202 */
2203 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2204 int spanY, View dragView, int[] resultDirection) {
2205 int[] targetDestination = new int[2];
2206
2207 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2208 Rect dragRect = new Rect();
2209 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2210 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2211
2212 Rect dropRegionRect = new Rect();
2213 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2214 dragView, dropRegionRect, mIntersectingViews);
2215
2216 int dropRegionSpanX = dropRegionRect.width();
2217 int dropRegionSpanY = dropRegionRect.height();
2218
2219 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2220 dropRegionRect.height(), dropRegionRect);
2221
2222 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2223 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2224
2225 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2226 deltaX = 0;
2227 }
2228 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2229 deltaY = 0;
2230 }
2231
2232 if (deltaX == 0 && deltaY == 0) {
2233 // No idea what to do, give a random direction.
2234 resultDirection[0] = 1;
2235 resultDirection[1] = 0;
2236 } else {
2237 computeDirectionVector(deltaX, deltaY, resultDirection);
2238 }
2239 }
2240
2241 // For a given cell and span, fetch the set of views intersecting the region.
2242 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2243 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2244 if (boundingRect != null) {
2245 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2246 }
2247 intersectingViews.clear();
2248 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2249 Rect r1 = new Rect();
2250 final int count = mShortcutsAndWidgets.getChildCount();
2251 for (int i = 0; i < count; i++) {
2252 View child = mShortcutsAndWidgets.getChildAt(i);
2253 if (child == dragView) continue;
2254 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2255 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2256 if (Rect.intersects(r0, r1)) {
2257 mIntersectingViews.add(child);
2258 if (boundingRect != null) {
2259 boundingRect.union(r1);
2260 }
2261 }
2262 }
2263 }
2264
2265 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2266 View dragView, int[] result) {
2267 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2268 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2269 mIntersectingViews);
2270 return !mIntersectingViews.isEmpty();
2271 }
2272
2273 void revertTempState() {
2274 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2275 final int count = mShortcutsAndWidgets.getChildCount();
2276 for (int i = 0; i < count; i++) {
2277 View child = mShortcutsAndWidgets.getChildAt(i);
2278 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2279 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2280 lp.tmpCellX = lp.cellX;
2281 lp.tmpCellY = lp.cellY;
2282 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2283 0, false, false);
2284 }
2285 }
2286 completeAndClearReorderHintAnimations();
2287 setItemPlacementDirty(false);
2288 }
2289
Adam Cohenbebf0422012-04-11 18:06:28 -07002290 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2291 View dragView, int[] direction, boolean commit) {
2292 int[] pixelXY = new int[2];
2293 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2294
2295 // First we determine if things have moved enough to cause a different layout
2296 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2297 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2298
2299 setUseTempCoords(true);
2300 if (swapSolution != null && swapSolution.isSolution) {
2301 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2302 // committing anything or animating anything as we just want to determine if a solution
2303 // exists
2304 copySolutionToTempState(swapSolution, dragView);
2305 setItemPlacementDirty(true);
2306 animateItemsToSolution(swapSolution, dragView, commit);
2307
2308 if (commit) {
2309 commitTempPlacement();
2310 completeAndClearReorderHintAnimations();
2311 setItemPlacementDirty(false);
2312 } else {
2313 beginOrAdjustHintAnimations(swapSolution, dragView,
2314 REORDER_ANIMATION_DURATION);
2315 }
2316 mShortcutsAndWidgets.requestLayout();
2317 }
2318 return swapSolution.isSolution;
2319 }
2320
Adam Cohen482ed822012-03-02 14:15:13 -08002321 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2322 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002323 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002324 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002325
2326 if (resultSpan == null) {
2327 resultSpan = new int[2];
2328 }
2329
Adam Cohen19f37922012-03-21 11:59:11 -07002330 // When we are checking drop validity or actually dropping, we don't recompute the
2331 // direction vector, since we want the solution to match the preview, and it's possible
2332 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002333 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2334 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002335 mDirectionVector[0] = mPreviousReorderDirection[0];
2336 mDirectionVector[1] = mPreviousReorderDirection[1];
2337 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002338 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2339 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2340 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002341 }
2342 } else {
2343 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2344 mPreviousReorderDirection[0] = mDirectionVector[0];
2345 mPreviousReorderDirection[1] = mDirectionVector[1];
2346 }
2347
Adam Cohen482ed822012-03-02 14:15:13 -08002348 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2349 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2350
2351 // We attempt the approach which doesn't shuffle views at all
2352 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2353 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2354
2355 ItemConfiguration finalSolution = null;
2356 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2357 finalSolution = swapSolution;
2358 } else if (noShuffleSolution.isSolution) {
2359 finalSolution = noShuffleSolution;
2360 }
2361
2362 boolean foundSolution = true;
2363 if (!DESTRUCTIVE_REORDER) {
2364 setUseTempCoords(true);
2365 }
2366
2367 if (finalSolution != null) {
2368 result[0] = finalSolution.dragViewX;
2369 result[1] = finalSolution.dragViewY;
2370 resultSpan[0] = finalSolution.dragViewSpanX;
2371 resultSpan[1] = finalSolution.dragViewSpanY;
2372
2373 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2374 // committing anything or animating anything as we just want to determine if a solution
2375 // exists
2376 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2377 if (!DESTRUCTIVE_REORDER) {
2378 copySolutionToTempState(finalSolution, dragView);
2379 }
2380 setItemPlacementDirty(true);
2381 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2382
Adam Cohen19f37922012-03-21 11:59:11 -07002383 if (!DESTRUCTIVE_REORDER &&
2384 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002385 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002386 completeAndClearReorderHintAnimations();
2387 setItemPlacementDirty(false);
2388 } else {
2389 beginOrAdjustHintAnimations(finalSolution, dragView,
2390 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002391 }
2392 }
2393 } else {
2394 foundSolution = false;
2395 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2396 }
2397
2398 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2399 setUseTempCoords(false);
2400 }
Adam Cohen482ed822012-03-02 14:15:13 -08002401
Michael Jurkaa52570f2012-03-20 03:18:20 -07002402 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002403 return result;
2404 }
2405
Adam Cohen19f37922012-03-21 11:59:11 -07002406 void setItemPlacementDirty(boolean dirty) {
2407 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002408 }
Adam Cohen19f37922012-03-21 11:59:11 -07002409 boolean isItemPlacementDirty() {
2410 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002411 }
2412
2413 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002414 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002415 boolean isSolution = false;
2416 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2417
2418 int area() {
2419 return dragViewSpanX * dragViewSpanY;
2420 }
Adam Cohen8baab352012-03-20 17:39:21 -07002421 }
2422
2423 private class CellAndSpan {
2424 int x, y;
2425 int spanX, spanY;
2426
2427 public CellAndSpan(int x, int y, int spanX, int spanY) {
2428 this.x = x;
2429 this.y = y;
2430 this.spanX = spanX;
2431 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002432 }
2433 }
2434
Adam Cohendf035382011-04-11 17:22:04 -07002435 /**
2436 * Find a vacant area that will fit the given bounds nearest the requested
2437 * cell location. Uses Euclidean distance to score multiple vacant areas.
2438 *
2439 * @param pixelX The X location at which you want to search for a vacant area.
2440 * @param pixelY The Y location at which you want to search for a vacant area.
2441 * @param spanX Horizontal span of the object.
2442 * @param spanY Vertical span of the object.
2443 * @param ignoreView Considers space occupied by this view as unoccupied
2444 * @param result Previously returned value to possibly recycle.
2445 * @return The X, Y cell of a vacant area that can contain this object,
2446 * nearest the requested location.
2447 */
2448 int[] findNearestVacantArea(
2449 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2450 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2451 }
2452
2453 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002454 * Find a vacant area that will fit the given bounds nearest the requested
2455 * cell location. Uses Euclidean distance to score multiple vacant areas.
2456 *
2457 * @param pixelX The X location at which you want to search for a vacant area.
2458 * @param pixelY The Y location at which you want to search for a vacant area.
2459 * @param minSpanX The minimum horizontal span required
2460 * @param minSpanY The minimum vertical span required
2461 * @param spanX Horizontal span of the object.
2462 * @param spanY Vertical span of the object.
2463 * @param ignoreView Considers space occupied by this view as unoccupied
2464 * @param result Previously returned value to possibly recycle.
2465 * @return The X, Y cell of a vacant area that can contain this object,
2466 * nearest the requested location.
2467 */
2468 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2469 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002470 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2471 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002472 }
2473
2474 /**
Adam Cohendf035382011-04-11 17:22:04 -07002475 * Find a starting cell position that will fit the given bounds nearest the requested
2476 * cell location. Uses Euclidean distance to score multiple vacant areas.
2477 *
2478 * @param pixelX The X location at which you want to search for a vacant area.
2479 * @param pixelY The Y location at which you want to search for a vacant area.
2480 * @param spanX Horizontal span of the object.
2481 * @param spanY Vertical span of the object.
2482 * @param ignoreView Considers space occupied by this view as unoccupied
2483 * @param result Previously returned value to possibly recycle.
2484 * @return The X, Y cell of a vacant area that can contain this object,
2485 * nearest the requested location.
2486 */
2487 int[] findNearestArea(
2488 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2489 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2490 }
2491
Michael Jurka0280c3b2010-09-17 15:00:07 -07002492 boolean existsEmptyCell() {
2493 return findCellForSpan(null, 1, 1);
2494 }
2495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002497 * Finds the upper-left coordinate of the first rectangle in the grid that can
2498 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2499 * then this method will only return coordinates for rectangles that contain the cell
2500 * (intersectX, intersectY)
2501 *
2502 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2503 * can be found.
2504 * @param spanX The horizontal span of the cell we want to find.
2505 * @param spanY The vertical span of the cell we want to find.
2506 *
2507 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002508 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002509 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002510 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002511 }
2512
2513 /**
2514 * Like above, but ignores any cells occupied by the item "ignoreView"
2515 *
2516 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2517 * can be found.
2518 * @param spanX The horizontal span of the cell we want to find.
2519 * @param spanY The vertical span of the cell we want to find.
2520 * @param ignoreView The home screen item we should treat as not occupying any space
2521 * @return
2522 */
2523 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002524 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2525 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002526 }
2527
2528 /**
2529 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2530 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2531 *
2532 * @param spanX The horizontal span of the cell we want to find.
2533 * @param spanY The vertical span of the cell we want to find.
2534 * @param ignoreView The home screen item we should treat as not occupying any space
2535 * @param intersectX The X coordinate of the cell that we should try to overlap
2536 * @param intersectX The Y coordinate of the cell that we should try to overlap
2537 *
2538 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2539 */
2540 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2541 int intersectX, int intersectY) {
2542 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002543 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002544 }
2545
2546 /**
2547 * The superset of the above two methods
2548 */
2549 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002550 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002551 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002552 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002553
Michael Jurka28750fb2010-09-24 17:43:49 -07002554 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002555 while (true) {
2556 int startX = 0;
2557 if (intersectX >= 0) {
2558 startX = Math.max(startX, intersectX - (spanX - 1));
2559 }
2560 int endX = mCountX - (spanX - 1);
2561 if (intersectX >= 0) {
2562 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2563 }
2564 int startY = 0;
2565 if (intersectY >= 0) {
2566 startY = Math.max(startY, intersectY - (spanY - 1));
2567 }
2568 int endY = mCountY - (spanY - 1);
2569 if (intersectY >= 0) {
2570 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2571 }
2572
Winson Chungbbc60d82010-11-11 16:34:41 -08002573 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002574 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002575 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002576 for (int i = 0; i < spanX; i++) {
2577 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002578 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002579 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002580 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002581 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002582 continue inner;
2583 }
2584 }
2585 }
2586 if (cellXY != null) {
2587 cellXY[0] = x;
2588 cellXY[1] = y;
2589 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002590 foundCell = true;
2591 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002592 }
2593 }
2594 if (intersectX == -1 && intersectY == -1) {
2595 break;
2596 } else {
2597 // if we failed to find anything, try again but without any requirements of
2598 // intersecting
2599 intersectX = -1;
2600 intersectY = -1;
2601 continue;
2602 }
2603 }
2604
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002605 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002606 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002607 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002608 }
2609
2610 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002611 * A drag event has begun over this layout.
2612 * It may have begun over this layout (in which case onDragChild is called first),
2613 * or it may have begun on another layout.
2614 */
2615 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002616 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002617 mDragging = true;
2618 }
2619
2620 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002621 * Called when drag has left this CellLayout or has been completed (successfully or not)
2622 */
2623 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002624 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002625 // This can actually be called when we aren't in a drag, e.g. when adding a new
2626 // item to this layout via the customize drawer.
2627 // Guard against that case.
2628 if (mDragging) {
2629 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002630 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002631
2632 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002633 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002634 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2635 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002636 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002637 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002638 }
2639
2640 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002641 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002642 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002643 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002644 *
2645 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002647 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002648 if (child != null) {
2649 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002650 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002651 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002652 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002653 }
2654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002655 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002656 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002657 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 * @param cellX X coordinate of upper left corner expressed as a cell position
2659 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002660 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002661 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002662 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002663 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002664 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002665 final int cellWidth = mCellWidth;
2666 final int cellHeight = mCellHeight;
2667 final int widthGap = mWidthGap;
2668 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002669
Winson Chung4b825dcd2011-06-19 12:41:22 -07002670 final int hStartPadding = getPaddingLeft();
2671 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002672
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002673 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2674 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2675
2676 int x = hStartPadding + cellX * (cellWidth + widthGap);
2677 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002678
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002679 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002682 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002683 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002684 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002685 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002686 * @param width Width in pixels
2687 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002688 * @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 -08002689 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002690 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002691 return rectToCell(getResources(), width, height, result);
2692 }
2693
2694 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002695 // Always assume we're working with the smallest span to make sure we
2696 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002697 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2698 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002699 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002700
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002701 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002702 int spanX = (int) Math.ceil(width / (float) smallerSize);
2703 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002704
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002705 if (result == null) {
2706 return new int[] { spanX, spanY };
2707 }
2708 result[0] = spanX;
2709 result[1] = spanY;
2710 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002711 }
2712
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002713 public int[] cellSpansToSize(int hSpans, int vSpans) {
2714 int[] size = new int[2];
2715 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2716 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2717 return size;
2718 }
2719
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002721 * Calculate the grid spans needed to fit given item
2722 */
2723 public void calculateSpans(ItemInfo info) {
2724 final int minWidth;
2725 final int minHeight;
2726
2727 if (info instanceof LauncherAppWidgetInfo) {
2728 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2729 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2730 } else if (info instanceof PendingAddWidgetInfo) {
2731 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2732 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2733 } else {
2734 // It's not a widget, so it must be 1x1
2735 info.spanX = info.spanY = 1;
2736 return;
2737 }
2738 int[] spans = rectToCell(minWidth, minHeight, null);
2739 info.spanX = spans[0];
2740 info.spanY = spans[1];
2741 }
2742
2743 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002744 * Find the first vacant cell, if there is one.
2745 *
2746 * @param vacant Holds the x and y coordinate of the vacant cell
2747 * @param spanX Horizontal cell span.
2748 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002749 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002750 * @return True if a vacant cell was found
2751 */
2752 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002753
Michael Jurka0280c3b2010-09-17 15:00:07 -07002754 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002755 }
2756
2757 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2758 int xCount, int yCount, boolean[][] occupied) {
2759
Adam Cohen2801caf2011-05-13 20:57:39 -07002760 for (int y = 0; y < yCount; y++) {
2761 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002762 boolean available = !occupied[x][y];
2763out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2764 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2765 available = available && !occupied[i][j];
2766 if (!available) break out;
2767 }
2768 }
2769
2770 if (available) {
2771 vacant[0] = x;
2772 vacant[1] = y;
2773 return true;
2774 }
2775 }
2776 }
2777
2778 return false;
2779 }
2780
Michael Jurka0280c3b2010-09-17 15:00:07 -07002781 private void clearOccupiedCells() {
2782 for (int x = 0; x < mCountX; x++) {
2783 for (int y = 0; y < mCountY; y++) {
2784 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002785 }
2786 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002787 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002788
Adam Cohend41fbf52012-02-16 23:53:59 -08002789 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002790 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002791 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002792 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002793
Adam Cohend4844c32011-02-18 19:25:06 -08002794 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002795 markCellsAsOccupiedForView(view, mOccupied);
2796 }
2797 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002798 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002799 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002800 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002801 }
2802
Adam Cohend4844c32011-02-18 19:25:06 -08002803 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002804 markCellsAsUnoccupiedForView(view, mOccupied);
2805 }
2806 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002807 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002808 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002809 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002810 }
2811
Adam Cohen482ed822012-03-02 14:15:13 -08002812 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2813 boolean value) {
2814 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002815 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2816 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002817 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002818 }
2819 }
2820 }
2821
Adam Cohen2801caf2011-05-13 20:57:39 -07002822 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002823 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002824 (Math.max((mCountX - 1), 0) * mWidthGap);
2825 }
2826
2827 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002828 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002829 (Math.max((mCountY - 1), 0) * mHeightGap);
2830 }
2831
Michael Jurka66d72172011-04-12 16:29:25 -07002832 public boolean isOccupied(int x, int y) {
2833 if (x < mCountX && y < mCountY) {
2834 return mOccupied[x][y];
2835 } else {
2836 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2837 }
2838 }
2839
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840 @Override
2841 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2842 return new CellLayout.LayoutParams(getContext(), attrs);
2843 }
2844
2845 @Override
2846 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2847 return p instanceof CellLayout.LayoutParams;
2848 }
2849
2850 @Override
2851 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2852 return new CellLayout.LayoutParams(p);
2853 }
2854
Winson Chungaafa03c2010-06-11 17:34:16 -07002855 public static class CellLayoutAnimationController extends LayoutAnimationController {
2856 public CellLayoutAnimationController(Animation animation, float delay) {
2857 super(animation, delay);
2858 }
2859
2860 @Override
2861 protected long getDelayForView(View view) {
2862 return (int) (Math.random() * 150);
2863 }
2864 }
2865
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002866 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2867 /**
2868 * Horizontal location of the item in the grid.
2869 */
2870 @ViewDebug.ExportedProperty
2871 public int cellX;
2872
2873 /**
2874 * Vertical location of the item in the grid.
2875 */
2876 @ViewDebug.ExportedProperty
2877 public int cellY;
2878
2879 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002880 * Temporary horizontal location of the item in the grid during reorder
2881 */
2882 public int tmpCellX;
2883
2884 /**
2885 * Temporary vertical location of the item in the grid during reorder
2886 */
2887 public int tmpCellY;
2888
2889 /**
2890 * Indicates that the temporary coordinates should be used to layout the items
2891 */
2892 public boolean useTmpCoords;
2893
2894 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002895 * Number of cells spanned horizontally by the item.
2896 */
2897 @ViewDebug.ExportedProperty
2898 public int cellHSpan;
2899
2900 /**
2901 * Number of cells spanned vertically by the item.
2902 */
2903 @ViewDebug.ExportedProperty
2904 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002905
Adam Cohen1b607ed2011-03-03 17:26:50 -08002906 /**
2907 * Indicates whether the item will set its x, y, width and height parameters freely,
2908 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2909 */
Adam Cohend4844c32011-02-18 19:25:06 -08002910 public boolean isLockedToGrid = true;
2911
Adam Cohen482ed822012-03-02 14:15:13 -08002912 /**
2913 * Indicates whether this item can be reordered. Always true except in the case of the
2914 * the AllApps button.
2915 */
2916 public boolean canReorder = true;
2917
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002918 // X coordinate of the view in the layout.
2919 @ViewDebug.ExportedProperty
2920 int x;
2921 // Y coordinate of the view in the layout.
2922 @ViewDebug.ExportedProperty
2923 int y;
2924
Romain Guy84f296c2009-11-04 15:00:44 -08002925 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002926
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002927 public LayoutParams(Context c, AttributeSet attrs) {
2928 super(c, attrs);
2929 cellHSpan = 1;
2930 cellVSpan = 1;
2931 }
2932
2933 public LayoutParams(ViewGroup.LayoutParams source) {
2934 super(source);
2935 cellHSpan = 1;
2936 cellVSpan = 1;
2937 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002938
2939 public LayoutParams(LayoutParams source) {
2940 super(source);
2941 this.cellX = source.cellX;
2942 this.cellY = source.cellY;
2943 this.cellHSpan = source.cellHSpan;
2944 this.cellVSpan = source.cellVSpan;
2945 }
2946
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002947 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002948 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 this.cellX = cellX;
2950 this.cellY = cellY;
2951 this.cellHSpan = cellHSpan;
2952 this.cellVSpan = cellVSpan;
2953 }
2954
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002955 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002956 if (isLockedToGrid) {
2957 final int myCellHSpan = cellHSpan;
2958 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002959 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2960 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002961
Adam Cohend4844c32011-02-18 19:25:06 -08002962 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2963 leftMargin - rightMargin;
2964 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2965 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002966 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2967 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002968 }
2969 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002970
Winson Chungaafa03c2010-06-11 17:34:16 -07002971 public String toString() {
2972 return "(" + this.cellX + ", " + this.cellY + ")";
2973 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002974
2975 public void setWidth(int width) {
2976 this.width = width;
2977 }
2978
2979 public int getWidth() {
2980 return width;
2981 }
2982
2983 public void setHeight(int height) {
2984 this.height = height;
2985 }
2986
2987 public int getHeight() {
2988 return height;
2989 }
2990
2991 public void setX(int x) {
2992 this.x = x;
2993 }
2994
2995 public int getX() {
2996 return x;
2997 }
2998
2999 public void setY(int y) {
3000 this.y = y;
3001 }
3002
3003 public int getY() {
3004 return y;
3005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 }
3007
Michael Jurka0280c3b2010-09-17 15:00:07 -07003008 // This class stores info for two purposes:
3009 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3010 // its spanX, spanY, and the screen it is on
3011 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3012 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3013 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003014 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003016 int cellX = -1;
3017 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003018 int spanX;
3019 int spanY;
3020 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07003021 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003023 @Override
3024 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003025 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3026 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003027 }
3028 }
Michael Jurkad771c962011-08-09 15:00:48 -07003029
3030 public boolean lastDownOnOccupiedCell() {
3031 return mLastDownOnOccupiedCell;
3032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003033}