blob: ba20a76baf49223b71e322ae1e60e900d7023256 [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;
20import android.animation.AnimatorListenerAdapter;
Chet Haase00397b12010-10-07 11:13:10 -070021import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040025import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070026import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070027import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080029import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070030import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070031import android.graphics.Point;
32import android.graphics.PointF;
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];
81 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070082 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070083
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080085 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070086 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087
Michael Jurkadee05892010-07-27 10:01:56 -070088 private OnTouchListener mInterceptTouchListener;
89
Adam Cohen69ce2e52011-07-03 19:25:21 -070090 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070091 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070092
Adam Cohenb5ba0972011-09-07 18:02:31 -070093 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070094 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070095 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070096
Michael Jurka33945b22010-12-21 18:19:38 -080097 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -080098 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -070099 private Drawable mOverScrollForegroundDrawable;
100 private Drawable mOverScrollLeft;
101 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700102 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700103 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700104 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700105
Michael Jurka33945b22010-12-21 18:19:38 -0800106 // If we're actively dragging something over this screen, mIsDragOverlapping is true
107 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700108 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700109
Winson Chung150fbab2010-09-29 17:14:26 -0700110 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700111 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800112 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700113 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700114 private InterruptibleInOutAnimator[] mDragOutlineAnims =
115 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700116
117 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700118 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700119 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700120
Patrick Dubroy96864c32011-03-10 17:17:23 -0800121 private BubbleTextView mPressedOrFocusedIcon;
122
Adam Cohen482ed822012-03-02 14:15:13 -0800123 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
124 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700125 private HashMap<View, ReorderHintAnimation>
126 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
127
128 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700129
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700130 // When a drag operation is in progress, holds the nearest cell to the touch point
131 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Joe Onorato4be866d2010-10-10 11:26:02 -0700133 private boolean mDragging = false;
134
Patrick Dubroyce34a972010-10-19 10:34:32 -0700135 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700136 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700137
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800138 private boolean mIsHotseat = false;
Winson Chungeecf02d2012-03-02 17:14:58 -0800139 private float mChildScale = 1f;
140 private float mHotseatChildScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800141
Adam Cohen482ed822012-03-02 14:15:13 -0800142 public static final int MODE_DRAG_OVER = 0;
143 public static final int MODE_ON_DROP = 1;
144 public static final int MODE_ON_DROP_EXTERNAL = 2;
145 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700146 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800147 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
148
Adam Cohena897f392012-04-27 18:12:05 -0700149 static final int LANDSCAPE = 0;
150 static final int PORTRAIT = 1;
151
Adam Cohen19f37922012-03-21 11:59:11 -0700152 private static final float REORDER_HINT_MAGNITUDE = 0.27f;
153 private static final int REORDER_ANIMATION_DURATION = 150;
154 private float mReorderHintAnimationMagnitude;
155
Adam Cohen482ed822012-03-02 14:15:13 -0800156 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
157 private Rect mOccupiedRect = new Rect();
158 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700159 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700160 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700161 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800162
Romain Guy8a0bff52012-05-06 13:14:33 -0700163 private final static PorterDuffXfermode sAddBlendMode =
164 new PorterDuffXfermode(PorterDuff.Mode.ADD);
165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 public CellLayout(Context context) {
167 this(context, null);
168 }
169
170 public CellLayout(Context context, AttributeSet attrs) {
171 this(context, attrs, 0);
172 }
173
174 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
175 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700176 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700177
178 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
179 // the user where a dragged item will land when dropped.
180 setWillNotDraw(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700181 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
184
Adam Cohenf4bd5792012-04-27 11:35:29 -0700185 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
186 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700187 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
188 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700189 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700190 mCountX = LauncherModel.getCellCountX();
191 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700192 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800193 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700194 mPreviousReorderDirection[0] = INVALID_DIRECTION;
195 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
197 a.recycle();
198
199 setAlwaysDrawnWithCacheEnabled(false);
200
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700201 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700202
Winson Chung967289b2011-06-30 18:09:30 -0700203 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700204 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800205
Adam Cohenb5ba0972011-09-07 18:02:31 -0700206 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
207 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
208 mForegroundPadding =
209 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800210
Adam Cohen19f37922012-03-21 11:59:11 -0700211 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
212 res.getDimensionPixelSize(R.dimen.app_icon_size));
213
Winson Chungb26f3d62011-06-02 10:49:29 -0700214 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700215 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700216
Winson Chungeecf02d2012-03-02 17:14:58 -0800217 int iconScale = res.getInteger(R.integer.app_icon_scale_percent);
218 if (iconScale >= 0) {
219 mChildScale = iconScale / 100f;
220 }
221 int hotseatIconScale = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
222 if (hotseatIconScale >= 0) {
223 mHotseatChildScale = hotseatIconScale / 100f;
224 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800225
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700226 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700227
Patrick Dubroyce34a972010-10-19 10:34:32 -0700228 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700229
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700230
Winson Chungb8c69f32011-10-19 21:36:08 -0700231 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700232 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800233 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700234 }
235
236 // When dragging things around the home screens, we show a green outline of
237 // where the item will land. The outlines gradually fade out, leaving a trail
238 // behind the drag path.
239 // Set up all the animations that are used to implement this fading.
240 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700241 final float fromAlphaValue = 0;
242 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700243
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700244 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700245
246 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700247 final InterruptibleInOutAnimator anim =
248 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700249 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700250 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700251 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700252 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700253 final Bitmap outline = (Bitmap)anim.getTag();
254
255 // If an animation is started and then stopped very quickly, we can still
256 // get spurious updates we've cleared the tag. Guard against this.
257 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700258 @SuppressWarnings("all") // suppress dead code warning
259 final boolean debug = false;
260 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700261 Object val = animation.getAnimatedValue();
262 Log.d(TAG, "anim " + thisIndex + " update: " + val +
263 ", isStopped " + anim.isStopped());
264 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700265 // Try to prevent it from continuing to run
266 animation.cancel();
267 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700268 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800269 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700270 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700271 }
272 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700273 // The animation holds a reference to the drag outline bitmap as long is it's
274 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700275 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700276 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700277 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700278 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700279 anim.setTag(null);
280 }
281 }
282 });
283 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700284 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700285
Michael Jurka18014792010-10-14 09:01:34 -0700286 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700287 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800288
Michael Jurkaa52570f2012-03-20 03:18:20 -0700289 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
290 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
291 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700292 }
293
Michael Jurkaf6440da2011-04-05 14:50:34 -0700294 static int widthInPortrait(Resources r, int numCells) {
295 // We use this method from Workspace to figure out how many rows/columns Launcher should
296 // have. We ignore the left/right padding on CellLayout because it turns out in our design
297 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700298 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700299 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
300 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700301
Winson Chung4b825dcd2011-06-19 12:41:22 -0700302 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700303 }
304
Michael Jurkaf6440da2011-04-05 14:50:34 -0700305 static int heightInLandscape(Resources r, int numCells) {
306 // We use this method from Workspace to figure out how many rows/columns Launcher should
307 // have. We ignore the left/right padding on CellLayout because it turns out in our design
308 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700309 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700310 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
311 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700312
Winson Chung4b825dcd2011-06-19 12:41:22 -0700313 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700314 }
315
Adam Cohen2801caf2011-05-13 20:57:39 -0700316 public void enableHardwareLayers() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700317 mShortcutsAndWidgets.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700318 }
319
320 public void setGridSize(int x, int y) {
321 mCountX = x;
322 mCountY = y;
323 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800324 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700325 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700326 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700327 }
328
Patrick Dubroy96864c32011-03-10 17:17:23 -0800329 private void invalidateBubbleTextView(BubbleTextView icon) {
330 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700331 invalidate(icon.getLeft() + getPaddingLeft() - padding,
332 icon.getTop() + getPaddingTop() - padding,
333 icon.getRight() + getPaddingLeft() + padding,
334 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800335 }
336
Adam Cohenb5ba0972011-09-07 18:02:31 -0700337 void setOverScrollAmount(float r, boolean left) {
338 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
339 mOverScrollForegroundDrawable = mOverScrollLeft;
340 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
341 mOverScrollForegroundDrawable = mOverScrollRight;
342 }
343
344 mForegroundAlpha = (int) Math.round((r * 255));
345 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
346 invalidate();
347 }
348
Patrick Dubroy96864c32011-03-10 17:17:23 -0800349 void setPressedOrFocusedIcon(BubbleTextView icon) {
350 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
351 // requires an expanded clip rect (due to the glow's blur radius)
352 BubbleTextView oldIcon = mPressedOrFocusedIcon;
353 mPressedOrFocusedIcon = icon;
354 if (oldIcon != null) {
355 invalidateBubbleTextView(oldIcon);
356 }
357 if (mPressedOrFocusedIcon != null) {
358 invalidateBubbleTextView(mPressedOrFocusedIcon);
359 }
360 }
361
Michael Jurka33945b22010-12-21 18:19:38 -0800362 void setIsDragOverlapping(boolean isDragOverlapping) {
363 if (mIsDragOverlapping != isDragOverlapping) {
364 mIsDragOverlapping = isDragOverlapping;
365 invalidate();
366 }
367 }
368
369 boolean getIsDragOverlapping() {
370 return mIsDragOverlapping;
371 }
372
Adam Cohenebea84d2011-11-09 17:20:41 -0800373 protected void setOverscrollTransformsDirty(boolean dirty) {
374 mScrollingTransformsDirty = dirty;
375 }
376
377 protected void resetOverscrollTransforms() {
378 if (mScrollingTransformsDirty) {
379 setOverscrollTransformsDirty(false);
380 setTranslationX(0);
381 setRotationY(0);
382 // It doesn't matter if we pass true or false here, the important thing is that we
383 // pass 0, which results in the overscroll drawable not being drawn any more.
384 setOverScrollAmount(0, false);
385 setPivotX(getMeasuredWidth() / 2);
386 setPivotY(getMeasuredHeight() / 2);
387 }
388 }
389
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700390 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700391 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700392 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
393 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
394 // When we're small, we are either drawn normally or in the "accepts drops" state (during
395 // a drag). However, we also drag the mini hover background *over* one of those two
396 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700397 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700398 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800399
400 if (mIsDragOverlapping) {
401 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700402 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700403 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700404 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700405 }
Michael Jurka33945b22010-12-21 18:19:38 -0800406
407 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
408 bg.setBounds(mBackgroundRect);
409 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700410 }
Romain Guya6abce82009-11-10 02:54:41 -0800411
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700412 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700413 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700414 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700415 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800416 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700417 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700418 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800419 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700420 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700421 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800422
423 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
424 // requires an expanded clip rect (due to the glow's blur radius)
425 if (mPressedOrFocusedIcon != null) {
426 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
427 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
428 if (b != null) {
429 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700430 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
431 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800432 null);
433 }
434 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700435
Adam Cohen482ed822012-03-02 14:15:13 -0800436 if (DEBUG_VISUALIZE_OCCUPIED) {
437 int[] pt = new int[2];
438 ColorDrawable cd = new ColorDrawable(Color.RED);
439 cd.setBounds(0, 0, 80, 80);
440 for (int i = 0; i < mCountX; i++) {
441 for (int j = 0; j < mCountY; j++) {
442 if (mOccupied[i][j]) {
443 cellToPoint(i, j, pt);
444 canvas.save();
445 canvas.translate(pt[0], pt[1]);
446 cd.draw(canvas);
447 canvas.restore();
448 }
449 }
450 }
451 }
452
Andrew Flynn850d2e72012-04-26 16:51:20 -0700453 int previewOffset = FolderRingAnimator.sPreviewSize;
454
Adam Cohen69ce2e52011-07-03 19:25:21 -0700455 // The folder outer / inner ring image(s)
456 for (int i = 0; i < mFolderOuterRings.size(); i++) {
457 FolderRingAnimator fra = mFolderOuterRings.get(i);
458
459 // Draw outer ring
460 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
461 int width = (int) fra.getOuterRingSize();
462 int height = width;
463 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
464
465 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700466 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700467
468 canvas.save();
469 canvas.translate(centerX - width / 2, centerY - height / 2);
470 d.setBounds(0, 0, width, height);
471 d.draw(canvas);
472 canvas.restore();
473
474 // Draw inner ring
475 d = FolderRingAnimator.sSharedInnerRingDrawable;
476 width = (int) fra.getInnerRingSize();
477 height = width;
478 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
479
480 centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700481 centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700482 canvas.save();
483 canvas.translate(centerX - width / 2, centerY - width / 2);
484 d.setBounds(0, 0, width, height);
485 d.draw(canvas);
486 canvas.restore();
487 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700488
489 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
490 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
491 int width = d.getIntrinsicWidth();
492 int height = d.getIntrinsicHeight();
493
494 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
495 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700496 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohenc51934b2011-07-26 21:07:43 -0700497
498 canvas.save();
499 canvas.translate(centerX - width / 2, centerY - width / 2);
500 d.setBounds(0, 0, width, height);
501 d.draw(canvas);
502 canvas.restore();
503 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700504 }
505
Adam Cohenb5ba0972011-09-07 18:02:31 -0700506 @Override
507 protected void dispatchDraw(Canvas canvas) {
508 super.dispatchDraw(canvas);
509 if (mForegroundAlpha > 0) {
510 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
511 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700512 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700513 mOverScrollForegroundDrawable.draw(canvas);
514 p.setXfermode(null);
515 }
516 }
517
Adam Cohen69ce2e52011-07-03 19:25:21 -0700518 public void showFolderAccept(FolderRingAnimator fra) {
519 mFolderOuterRings.add(fra);
520 }
521
522 public void hideFolderAccept(FolderRingAnimator fra) {
523 if (mFolderOuterRings.contains(fra)) {
524 mFolderOuterRings.remove(fra);
525 }
526 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700527 }
528
Adam Cohenc51934b2011-07-26 21:07:43 -0700529 public void setFolderLeaveBehindCell(int x, int y) {
530 mFolderLeaveBehindCell[0] = x;
531 mFolderLeaveBehindCell[1] = y;
532 invalidate();
533 }
534
535 public void clearFolderLeaveBehind() {
536 mFolderLeaveBehindCell[0] = -1;
537 mFolderLeaveBehindCell[1] = -1;
538 invalidate();
539 }
540
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700541 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700542 public boolean shouldDelayChildPressedState() {
543 return false;
544 }
545
546 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700547 public void cancelLongPress() {
548 super.cancelLongPress();
549
550 // Cancel long press for all children
551 final int count = getChildCount();
552 for (int i = 0; i < count; i++) {
553 final View child = getChildAt(i);
554 child.cancelLongPress();
555 }
556 }
557
Michael Jurkadee05892010-07-27 10:01:56 -0700558 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
559 mInterceptTouchListener = listener;
560 }
561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700563 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 }
565
566 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700567 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 }
569
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800570 public void setIsHotseat(boolean isHotseat) {
571 mIsHotseat = isHotseat;
572 }
573
Winson Chungeecf02d2012-03-02 17:14:58 -0800574 public float getChildrenScale() {
575 return mIsHotseat ? mHotseatChildScale : mChildScale;
576 }
577
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800578
Andrew Flynn850d2e72012-04-26 16:51:20 -0700579 private void scaleChild(BubbleTextView bubbleChild, float scale) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800580 // If we haven't measured the child yet, do it now
581 // (this happens if we're being dropped from all-apps
582 if (bubbleChild.getLayoutParams() instanceof LayoutParams &&
583 (bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700584 getShortcutsAndWidgets().measureChild(bubbleChild);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800585 }
Andrew Flynnbc239a12012-03-06 11:39:49 -0800586
Andrew Flynnbc239a12012-03-06 11:39:49 -0800587 bubbleChild.setScaleX(scale);
588 bubbleChild.setScaleY(scale);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800589 }
590
591 private void resetChild(BubbleTextView bubbleChild) {
592 bubbleChild.setScaleX(1f);
593 bubbleChild.setScaleY(1f);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800594
595 bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
596 }
597
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800598 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700599 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700600 final LayoutParams lp = params;
601
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800602 // Hotseat icons - scale down and remove text
603 // Don't scale the all apps button
604 // scale percent set to -1 means do not scale
605 // Only scale BubbleTextViews
606 if (child instanceof BubbleTextView) {
607 BubbleTextView bubbleChild = (BubbleTextView) child;
608
Andrew Flynnbc239a12012-03-06 11:39:49 -0800609 // Start the child with 100% scale and visible text
610 resetChild(bubbleChild);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800611
Andrew Flynn850d2e72012-04-26 16:51:20 -0700612 if (mIsHotseat && mHotseatChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800613 // Scale/make transparent for a hotseat
Andrew Flynn850d2e72012-04-26 16:51:20 -0700614 scaleChild(bubbleChild, mHotseatChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800615
Andrew Flynnbc239a12012-03-06 11:39:49 -0800616 bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent));
Winson Chungeecf02d2012-03-02 17:14:58 -0800617 } else if (mChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800618 // Else possibly still scale it if we need to for smaller icons
Andrew Flynn850d2e72012-04-26 16:51:20 -0700619 scaleChild(bubbleChild, mChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800620 }
621 }
622
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 // Generate an id for each view, this assumes we have at most 256x256 cells
624 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700625 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700626 // If the horizontal or vertical span is set to -1, it is taken to
627 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700628 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
629 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630
Winson Chungaafa03c2010-06-11 17:34:16 -0700631 child.setId(childId);
632
Michael Jurkaa52570f2012-03-20 03:18:20 -0700633 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700634
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700635 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700636
Winson Chungaafa03c2010-06-11 17:34:16 -0700637 return true;
638 }
639 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700643 public void removeAllViews() {
644 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700645 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700646 }
647
648 @Override
649 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700650 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700651 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700652 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700653 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654 }
655
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700656 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700657 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700658 }
659
Michael Jurka0280c3b2010-09-17 15:00:07 -0700660 @Override
661 public void removeView(View view) {
662 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700663 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 }
665
666 @Override
667 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700668 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
669 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700670 }
671
672 @Override
673 public void removeViewInLayout(View view) {
674 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700675 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 }
677
678 @Override
679 public void removeViews(int start, int count) {
680 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700681 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700682 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700683 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
686 @Override
687 public void removeViewsInLayout(int start, int count) {
688 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700691 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800692 }
693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 @Override
695 protected void onAttachedToWindow() {
696 super.onAttachedToWindow();
697 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
698 }
699
Michael Jurkaaf442092010-06-10 17:01:57 -0700700 public void setTagToCellInfoForPoint(int touchX, int touchY) {
701 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800702 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700703 final int x = touchX + getScrollX();
704 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700705 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700706
707 boolean found = false;
708 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700709 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800710 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700711
Adam Cohen1b607ed2011-03-03 17:26:50 -0800712 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
713 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700714 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700715
Winson Chungeecf02d2012-03-02 17:14:58 -0800716 float scale = child.getScaleX();
717 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
718 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700719 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
720 // offset that by this CellLayout's padding to test an (x,y) point that is relative
721 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800723 frame.inset((int) (frame.width() * (1f - scale) / 2),
724 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700725
Michael Jurkaaf442092010-06-10 17:01:57 -0700726 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700727 cellInfo.cell = child;
728 cellInfo.cellX = lp.cellX;
729 cellInfo.cellY = lp.cellY;
730 cellInfo.spanX = lp.cellHSpan;
731 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700732 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700733 break;
734 }
735 }
736 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700737
Michael Jurkad771c962011-08-09 15:00:48 -0700738 mLastDownOnOccupiedCell = found;
739
Michael Jurkaaf442092010-06-10 17:01:57 -0700740 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700741 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700742 pointToCellExact(x, y, cellXY);
743
Michael Jurkaaf442092010-06-10 17:01:57 -0700744 cellInfo.cell = null;
745 cellInfo.cellX = cellXY[0];
746 cellInfo.cellY = cellXY[1];
747 cellInfo.spanX = 1;
748 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700749 }
750 setTag(cellInfo);
751 }
752
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 @Override
754 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700755 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
756 // even in the case where we return early. Not clearing here was causing bugs whereby on
757 // long-press we'd end up picking up an item from a previous drag operation.
758 final int action = ev.getAction();
759
760 if (action == MotionEvent.ACTION_DOWN) {
761 clearTagCellInfo();
762 }
763
Michael Jurkadee05892010-07-27 10:01:56 -0700764 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
765 return true;
766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767
768 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700769 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800771
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 return false;
773 }
774
Adam Cohenc1997fd2011-08-15 18:26:39 -0700775 private void clearTagCellInfo() {
776 final CellInfo cellInfo = mCellInfo;
777 cellInfo.cell = null;
778 cellInfo.cellX = -1;
779 cellInfo.cellY = -1;
780 cellInfo.spanX = 0;
781 cellInfo.spanY = 0;
782 setTag(cellInfo);
783 }
784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700786 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 }
788
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700789 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700790 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 * @param x X coordinate of the point
792 * @param y Y coordinate of the point
793 * @param result Array of 2 ints to hold the x and y coordinate of the cell
794 */
795 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700796 final int hStartPadding = getPaddingLeft();
797 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798
799 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
800 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
801
Adam Cohend22015c2010-07-26 22:02:18 -0700802 final int xAxis = mCountX;
803 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804
805 if (result[0] < 0) result[0] = 0;
806 if (result[0] >= xAxis) result[0] = xAxis - 1;
807 if (result[1] < 0) result[1] = 0;
808 if (result[1] >= yAxis) result[1] = yAxis - 1;
809 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700810
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 /**
812 * Given a point, return the cell that most closely encloses that point
813 * @param x X coordinate of the point
814 * @param y Y coordinate of the point
815 * @param result Array of 2 ints to hold the x and y coordinate of the cell
816 */
817 void pointToCellRounded(int x, int y, int[] result) {
818 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
819 }
820
821 /**
822 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700823 *
824 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700826 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 * @param result Array of 2 ints to hold the x and y coordinate of the point
828 */
829 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700830 final int hStartPadding = getPaddingLeft();
831 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832
833 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
834 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
835 }
836
Adam Cohene3e27a82011-04-15 12:07:39 -0700837 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800838 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700839 *
840 * @param cellX X coordinate of the cell
841 * @param cellY Y coordinate of the cell
842 *
843 * @param result Array of 2 ints to hold the x and y coordinate of the point
844 */
845 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700846 regionToCenterPoint(cellX, cellY, 1, 1, result);
847 }
848
849 /**
850 * Given a cell coordinate and span return the point that represents the center of the regio
851 *
852 * @param cellX X coordinate of the cell
853 * @param cellY Y coordinate of the cell
854 *
855 * @param result Array of 2 ints to hold the x and y coordinate of the point
856 */
857 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700858 final int hStartPadding = getPaddingLeft();
859 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700860 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
861 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
862 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
863 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700864 }
865
Adam Cohen19f37922012-03-21 11:59:11 -0700866 /**
867 * Given a cell coordinate and span fills out a corresponding pixel rect
868 *
869 * @param cellX X coordinate of the cell
870 * @param cellY Y coordinate of the cell
871 * @param result Rect in which to write the result
872 */
873 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
874 final int hStartPadding = getPaddingLeft();
875 final int vStartPadding = getPaddingTop();
876 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
877 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
878 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
879 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
880 }
881
Adam Cohen482ed822012-03-02 14:15:13 -0800882 public float getDistanceFromCell(float x, float y, int[] cell) {
883 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
884 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
885 Math.pow(y - mTmpPoint[1], 2));
886 return distance;
887 }
888
Romain Guy84f296c2009-11-04 15:00:44 -0800889 int getCellWidth() {
890 return mCellWidth;
891 }
892
893 int getCellHeight() {
894 return mCellHeight;
895 }
896
Adam Cohend4844c32011-02-18 19:25:06 -0800897 int getWidthGap() {
898 return mWidthGap;
899 }
900
901 int getHeightGap() {
902 return mHeightGap;
903 }
904
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700905 Rect getContentRect(Rect r) {
906 if (r == null) {
907 r = new Rect();
908 }
909 int left = getPaddingLeft();
910 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700911 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
912 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700913 r.set(left, top, right, bottom);
914 return r;
915 }
916
Adam Cohena897f392012-04-27 18:12:05 -0700917 static void getMetrics(Rect metrics, Resources res, int measureWidth, int measureHeight,
918 int countX, int countY, int orientation) {
919 int numWidthGaps = countX - 1;
920 int numHeightGaps = countY - 1;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700921
922 int widthGap;
923 int heightGap;
924 int cellWidth;
925 int cellHeight;
926 int paddingLeft;
927 int paddingRight;
928 int paddingTop;
929 int paddingBottom;
930
Adam Cohena897f392012-04-27 18:12:05 -0700931 int maxGap = res.getDimensionPixelSize(R.dimen.workspace_max_gap);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700932 if (orientation == LANDSCAPE) {
933 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_land);
934 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_land);
935 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
936 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
937 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_land);
938 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_land);
939 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_land);
940 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_land);
941 } else {
942 // PORTRAIT
943 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_port);
944 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_port);
945 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
946 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
947 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_port);
948 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_port);
949 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_port);
950 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_port);
951 }
952
953 if (widthGap < 0 || heightGap < 0) {
954 int hSpace = measureWidth - paddingLeft - paddingRight;
955 int vSpace = measureHeight - paddingTop - paddingBottom;
Adam Cohena897f392012-04-27 18:12:05 -0700956 int hFreeSpace = hSpace - (countX * cellWidth);
957 int vFreeSpace = vSpace - (countY * cellHeight);
958 widthGap = Math.min(maxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
959 heightGap = Math.min(maxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700960 }
961 metrics.set(cellWidth, cellHeight, widthGap, heightGap);
962 }
963
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 @Override
965 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700967 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
968
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
970 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700971
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
973 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
974 }
975
Adam Cohend22015c2010-07-26 22:02:18 -0700976 int numWidthGaps = mCountX - 1;
977 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978
Adam Cohen234c4cd2011-07-17 21:03:04 -0700979 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Michael Jurkadd13e3d2012-05-01 12:38:17 -0700980 int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight();
981 int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -0700982 int hFreeSpace = hSpace - (mCountX * mCellWidth);
983 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700984 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
985 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700986 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700987 } else {
988 mWidthGap = mOriginalWidthGap;
989 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700990 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700991
Michael Jurka8c920dd2011-01-20 14:16:56 -0800992 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
993 int newWidth = widthSpecSize;
994 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700995 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700996 newWidth = getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700997 ((mCountX - 1) * mWidthGap);
Michael Jurka8b805b12012-04-18 14:23:14 -0700998 newHeight = getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700999 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001000 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001001 }
Michael Jurka8c920dd2011-01-20 14:16:56 -08001002
1003 int count = getChildCount();
1004 for (int i = 0; i < count; i++) {
1005 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001006 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
1007 getPaddingRight(), MeasureSpec.EXACTLY);
1008 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
1009 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -08001010 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
1011 }
1012 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
1014
1015 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001016 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001019 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001020 child.layout(getPaddingLeft(), getPaddingTop(),
1021 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 }
1023 }
1024
1025 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001026 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1027 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001028 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001029 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
1030 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001031 }
1032
1033 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001035 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037
1038 @Override
1039 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001040 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 }
1042
Michael Jurka5f1c5092010-09-03 14:15:02 -07001043 public float getBackgroundAlpha() {
1044 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001045 }
1046
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001047 public void setBackgroundAlphaMultiplier(float multiplier) {
1048 mBackgroundAlphaMultiplier = multiplier;
1049 }
1050
Adam Cohenddb82192010-11-10 16:32:54 -08001051 public float getBackgroundAlphaMultiplier() {
1052 return mBackgroundAlphaMultiplier;
1053 }
1054
Michael Jurka5f1c5092010-09-03 14:15:02 -07001055 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001056 if (mBackgroundAlpha != alpha) {
1057 mBackgroundAlpha = alpha;
1058 invalidate();
1059 }
Michael Jurkadee05892010-07-27 10:01:56 -07001060 }
1061
Michael Jurkaa52570f2012-03-20 03:18:20 -07001062 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001063 final int childCount = getChildCount();
1064 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001065 getChildAt(i).setAlpha(alpha);
1066 }
1067 }
1068
Michael Jurkaa52570f2012-03-20 03:18:20 -07001069 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1070 if (getChildCount() > 0) {
1071 return (ShortcutAndWidgetContainer) getChildAt(0);
1072 }
1073 return null;
1074 }
1075
Patrick Dubroy440c3602010-07-13 17:50:32 -07001076 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001077 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001078 }
1079
Adam Cohen76fc0852011-06-17 13:26:23 -07001080 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001081 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001082 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001083 boolean[][] occupied = mOccupied;
1084 if (!permanent) {
1085 occupied = mTmpOccupied;
1086 }
1087
Adam Cohen19f37922012-03-21 11:59:11 -07001088 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001089 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1090 final ItemInfo info = (ItemInfo) child.getTag();
1091
1092 // We cancel any existing animations
1093 if (mReorderAnimators.containsKey(lp)) {
1094 mReorderAnimators.get(lp).cancel();
1095 mReorderAnimators.remove(lp);
1096 }
1097
Adam Cohen482ed822012-03-02 14:15:13 -08001098 final int oldX = lp.x;
1099 final int oldY = lp.y;
1100 if (adjustOccupied) {
1101 occupied[lp.cellX][lp.cellY] = false;
1102 occupied[cellX][cellY] = true;
1103 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001104 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001105 if (permanent) {
1106 lp.cellX = info.cellX = cellX;
1107 lp.cellY = info.cellY = cellY;
1108 } else {
1109 lp.tmpCellX = cellX;
1110 lp.tmpCellY = cellY;
1111 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001112 clc.setupLp(lp);
1113 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001114 final int newX = lp.x;
1115 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001116
Adam Cohen76fc0852011-06-17 13:26:23 -07001117 lp.x = oldX;
1118 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001119
Adam Cohen482ed822012-03-02 14:15:13 -08001120 // Exit early if we're not actually moving the view
1121 if (oldX == newX && oldY == newY) {
1122 lp.isLockedToGrid = true;
1123 return true;
1124 }
1125
1126 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
1127 va.setDuration(duration);
1128 mReorderAnimators.put(lp, va);
1129
1130 va.addUpdateListener(new AnimatorUpdateListener() {
1131 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001132 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001133 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001134 lp.x = (int) ((1 - r) * oldX + r * newX);
1135 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001136 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001137 }
1138 });
Adam Cohen482ed822012-03-02 14:15:13 -08001139 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001140 boolean cancelled = false;
1141 public void onAnimationEnd(Animator animation) {
1142 // If the animation was cancelled, it means that another animation
1143 // has interrupted this one, and we don't want to lock the item into
1144 // place just yet.
1145 if (!cancelled) {
1146 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001147 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001148 }
1149 if (mReorderAnimators.containsKey(lp)) {
1150 mReorderAnimators.remove(lp);
1151 }
1152 }
1153 public void onAnimationCancel(Animator animation) {
1154 cancelled = true;
1155 }
1156 });
Adam Cohen482ed822012-03-02 14:15:13 -08001157 va.setStartDelay(delay);
1158 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001159 return true;
1160 }
1161 return false;
1162 }
1163
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001164 /**
1165 * Estimate where the top left cell of the dragged item will land if it is dropped.
1166 *
1167 * @param originX The X value of the top left corner of the item
1168 * @param originY The Y value of the top left corner of the item
1169 * @param spanX The number of horizontal cells that the item spans
1170 * @param spanY The number of vertical cells that the item spans
1171 * @param result The estimated drop cell X and Y.
1172 */
1173 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001174 final int countX = mCountX;
1175 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001176
Michael Jurkaa63c4522010-08-19 13:52:27 -07001177 // pointToCellRounded takes the top left of a cell but will pad that with
1178 // cellWidth/2 and cellHeight/2 when finding the matching cell
1179 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001180
1181 // If the item isn't fully on this screen, snap to the edges
1182 int rightOverhang = result[0] + spanX - countX;
1183 if (rightOverhang > 0) {
1184 result[0] -= rightOverhang; // Snap to right
1185 }
1186 result[0] = Math.max(0, result[0]); // Snap to left
1187 int bottomOverhang = result[1] + spanY - countY;
1188 if (bottomOverhang > 0) {
1189 result[1] -= bottomOverhang; // Snap to bottom
1190 }
1191 result[1] = Math.max(0, result[1]); // Snap to top
1192 }
1193
Adam Cohen482ed822012-03-02 14:15:13 -08001194 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1195 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001196 final int oldDragCellX = mDragCell[0];
1197 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001198
Winson Chungb8c69f32011-10-19 21:36:08 -07001199 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001200 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1201 } else {
1202 mDragCenter.set(originX, originY);
1203 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001204
Adam Cohen2801caf2011-05-13 20:57:39 -07001205 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001206 return;
1207 }
1208
Adam Cohen482ed822012-03-02 14:15:13 -08001209 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1210 mDragCell[0] = cellX;
1211 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001212 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001213 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001214 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001215
Joe Onorato4be866d2010-10-10 11:26:02 -07001216 int left = topLeft[0];
1217 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001218
Winson Chungb8c69f32011-10-19 21:36:08 -07001219 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001220 // When drawing the drag outline, it did not account for margin offsets
1221 // added by the view's parent.
1222 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1223 left += lp.leftMargin;
1224 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001225
Adam Cohen99e8b402011-03-25 19:23:43 -07001226 // Offsets due to the size difference between the View and the dragOutline.
1227 // There is a size difference to account for the outer blur, which may lie
1228 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001229 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001230 // We center about the x axis
1231 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1232 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001233 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001234 if (dragOffset != null && dragRegion != null) {
1235 // Center the drag region *horizontally* in the cell and apply a drag
1236 // outline offset
1237 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1238 - dragRegion.width()) / 2;
1239 top += dragOffset.y;
1240 } else {
1241 // Center the drag outline in the cell
1242 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1243 - dragOutline.getWidth()) / 2;
1244 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1245 - dragOutline.getHeight()) / 2;
1246 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001247 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001248 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001249 mDragOutlineAnims[oldIndex].animateOut();
1250 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001251 Rect r = mDragOutlines[mDragOutlineCurrent];
1252 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1253 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001254 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001255 }
Winson Chung150fbab2010-09-29 17:14:26 -07001256
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001257 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1258 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001259 }
1260 }
1261
Adam Cohene0310962011-04-18 16:15:31 -07001262 public void clearDragOutlines() {
1263 final int oldIndex = mDragOutlineCurrent;
1264 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001265 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001269 * Find a vacant area that will fit the given bounds nearest the requested
1270 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001271 *
Romain Guy51afc022009-05-04 18:03:43 -07001272 * @param pixelX The X location at which you want to search for a vacant area.
1273 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001274 * @param spanX Horizontal span of the object.
1275 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001276 * @param result Array in which to place the result, or null (in which case a new array will
1277 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001278 * @return The X, Y cell of a vacant area that can contain this object,
1279 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001281 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1282 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001283 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001284 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001285
Michael Jurka6a1435d2010-09-27 17:35:12 -07001286 /**
1287 * Find a vacant area that will fit the given bounds nearest the requested
1288 * cell location. Uses Euclidean distance to score multiple vacant areas.
1289 *
1290 * @param pixelX The X location at which you want to search for a vacant area.
1291 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001292 * @param minSpanX The minimum horizontal span required
1293 * @param minSpanY The minimum vertical span required
1294 * @param spanX Horizontal span of the object.
1295 * @param spanY Vertical span of the object.
1296 * @param result Array in which to place the result, or null (in which case a new array will
1297 * be allocated)
1298 * @return The X, Y cell of a vacant area that can contain this object,
1299 * nearest the requested location.
1300 */
1301 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1302 int spanY, int[] result, int[] resultSpan) {
1303 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1304 result, resultSpan);
1305 }
1306
1307 /**
1308 * Find a vacant area that will fit the given bounds nearest the requested
1309 * cell location. Uses Euclidean distance to score multiple vacant areas.
1310 *
1311 * @param pixelX The X location at which you want to search for a vacant area.
1312 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001313 * @param spanX Horizontal span of the object.
1314 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001315 * @param ignoreOccupied If true, the result can be an occupied cell
1316 * @param result Array in which to place the result, or null (in which case a new array will
1317 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001318 * @return The X, Y cell of a vacant area that can contain this object,
1319 * nearest the requested location.
1320 */
Adam Cohendf035382011-04-11 17:22:04 -07001321 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1322 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001323 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001324 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001325 }
1326
1327 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1328 private void lazyInitTempRectStack() {
1329 if (mTempRectStack.isEmpty()) {
1330 for (int i = 0; i < mCountX * mCountY; i++) {
1331 mTempRectStack.push(new Rect());
1332 }
1333 }
1334 }
Adam Cohen482ed822012-03-02 14:15:13 -08001335
Adam Cohend41fbf52012-02-16 23:53:59 -08001336 private void recycleTempRects(Stack<Rect> used) {
1337 while (!used.isEmpty()) {
1338 mTempRectStack.push(used.pop());
1339 }
1340 }
1341
1342 /**
1343 * Find a vacant area that will fit the given bounds nearest the requested
1344 * cell location. Uses Euclidean distance to score multiple vacant areas.
1345 *
1346 * @param pixelX The X location at which you want to search for a vacant area.
1347 * @param pixelY The Y location at which you want to search for a vacant area.
1348 * @param minSpanX The minimum horizontal span required
1349 * @param minSpanY The minimum vertical span required
1350 * @param spanX Horizontal span of the object.
1351 * @param spanY Vertical span of the object.
1352 * @param ignoreOccupied If true, the result can be an occupied cell
1353 * @param result Array in which to place the result, or null (in which case a new array will
1354 * be allocated)
1355 * @return The X, Y cell of a vacant area that can contain this object,
1356 * nearest the requested location.
1357 */
1358 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001359 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1360 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001361 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001362 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001363 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001364
Adam Cohene3e27a82011-04-15 12:07:39 -07001365 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1366 // to the center of the item, but we are searching based on the top-left cell, so
1367 // we translate the point over to correspond to the top-left.
1368 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1369 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1370
Jeff Sharkey70864282009-04-07 21:08:40 -07001371 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001372 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001373 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001374 final Rect bestRect = new Rect(-1, -1, -1, -1);
1375 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001376
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001377 final int countX = mCountX;
1378 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001379
Adam Cohend41fbf52012-02-16 23:53:59 -08001380 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1381 spanX < minSpanX || spanY < minSpanY) {
1382 return bestXY;
1383 }
1384
1385 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001386 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001387 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1388 int ySize = -1;
1389 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001390 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001391 // First, let's see if this thing fits anywhere
1392 for (int i = 0; i < minSpanX; i++) {
1393 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001394 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001395 continue inner;
1396 }
Michael Jurkac28de512010-08-13 11:27:44 -07001397 }
1398 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001399 xSize = minSpanX;
1400 ySize = minSpanY;
1401
1402 // We know that the item will fit at _some_ acceptable size, now let's see
1403 // how big we can make it. We'll alternate between incrementing x and y spans
1404 // until we hit a limit.
1405 boolean incX = true;
1406 boolean hitMaxX = xSize >= spanX;
1407 boolean hitMaxY = ySize >= spanY;
1408 while (!(hitMaxX && hitMaxY)) {
1409 if (incX && !hitMaxX) {
1410 for (int j = 0; j < ySize; j++) {
1411 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1412 // We can't move out horizontally
1413 hitMaxX = true;
1414 }
1415 }
1416 if (!hitMaxX) {
1417 xSize++;
1418 }
1419 } else if (!hitMaxY) {
1420 for (int i = 0; i < xSize; i++) {
1421 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1422 // We can't move out vertically
1423 hitMaxY = true;
1424 }
1425 }
1426 if (!hitMaxY) {
1427 ySize++;
1428 }
1429 }
1430 hitMaxX |= xSize >= spanX;
1431 hitMaxY |= ySize >= spanY;
1432 incX = !incX;
1433 }
1434 incX = true;
1435 hitMaxX = xSize >= spanX;
1436 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001437 }
Winson Chung0be025d2011-05-23 17:45:09 -07001438 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001439 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440
Adam Cohend41fbf52012-02-16 23:53:59 -08001441 // We verify that the current rect is not a sub-rect of any of our previous
1442 // candidates. In this case, the current rect is disqualified in favour of the
1443 // containing rect.
1444 Rect currentRect = mTempRectStack.pop();
1445 currentRect.set(x, y, x + xSize, y + ySize);
1446 boolean contained = false;
1447 for (Rect r : validRegions) {
1448 if (r.contains(currentRect)) {
1449 contained = true;
1450 break;
1451 }
1452 }
1453 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001454 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1455 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001456
Adam Cohend41fbf52012-02-16 23:53:59 -08001457 if ((distance <= bestDistance && !contained) ||
1458 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001459 bestDistance = distance;
1460 bestXY[0] = x;
1461 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001462 if (resultSpan != null) {
1463 resultSpan[0] = xSize;
1464 resultSpan[1] = ySize;
1465 }
1466 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
1469 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001470 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001471 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472
Adam Cohenc0dcf592011-06-01 15:30:43 -07001473 // Return -1, -1 if no suitable location found
1474 if (bestDistance == Double.MAX_VALUE) {
1475 bestXY[0] = -1;
1476 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001477 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001478 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001479 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001481
Adam Cohen482ed822012-03-02 14:15:13 -08001482 /**
1483 * Find a vacant area that will fit the given bounds nearest the requested
1484 * cell location, and will also weigh in a suggested direction vector of the
1485 * desired location. This method computers distance based on unit grid distances,
1486 * not pixel distances.
1487 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001488 * @param cellX The X cell nearest to which you want to search for a vacant area.
1489 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001490 * @param spanX Horizontal span of the object.
1491 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001492 * @param direction The favored direction in which the views should move from x, y
1493 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1494 * matches exactly. Otherwise we find the best matching direction.
1495 * @param occoupied The array which represents which cells in the CellLayout are occupied
1496 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1497 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001498 * @param result Array in which to place the result, or null (in which case a new array will
1499 * be allocated)
1500 * @return The X, Y cell of a vacant area that can contain this object,
1501 * nearest the requested location.
1502 */
1503 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001504 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001505 // Keep track of best-scoring drop area
1506 final int[] bestXY = result != null ? result : new int[2];
1507 float bestDistance = Float.MAX_VALUE;
1508 int bestDirectionScore = Integer.MIN_VALUE;
1509
1510 final int countX = mCountX;
1511 final int countY = mCountY;
1512
1513 for (int y = 0; y < countY - (spanY - 1); y++) {
1514 inner:
1515 for (int x = 0; x < countX - (spanX - 1); x++) {
1516 // First, let's see if this thing fits anywhere
1517 for (int i = 0; i < spanX; i++) {
1518 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001519 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001520 continue inner;
1521 }
1522 }
1523 }
1524
1525 float distance = (float)
1526 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1527 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001528 computeDirectionVector(x - cellX, y - cellY, curDirection);
1529 // The direction score is just the dot product of the two candidate direction
1530 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001531 int curDirectionScore = direction[0] * curDirection[0] +
1532 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001533 boolean exactDirectionOnly = false;
1534 boolean directionMatches = direction[0] == curDirection[0] &&
1535 direction[0] == curDirection[0];
1536 if ((directionMatches || !exactDirectionOnly) &&
1537 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001538 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1539 bestDistance = distance;
1540 bestDirectionScore = curDirectionScore;
1541 bestXY[0] = x;
1542 bestXY[1] = y;
1543 }
1544 }
1545 }
1546
1547 // Return -1, -1 if no suitable location found
1548 if (bestDistance == Float.MAX_VALUE) {
1549 bestXY[0] = -1;
1550 bestXY[1] = -1;
1551 }
1552 return bestXY;
1553 }
1554
Adam Cohen47a876d2012-03-19 13:21:41 -07001555 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1556 int[] direction,boolean[][] occupied,
1557 boolean blockOccupied[][], int[] result) {
1558 // Keep track of best-scoring drop area
1559 final int[] bestXY = result != null ? result : new int[2];
1560 bestXY[0] = -1;
1561 bestXY[1] = -1;
1562 float bestDistance = Float.MAX_VALUE;
1563
1564 // We use this to march in a single direction
Adam Cohen5b53f292012-03-29 14:30:35 -07001565 if ((direction[0] != 0 && direction[1] != 0) ||
1566 (direction[0] == 0 && direction[1] == 0)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001567 return bestXY;
1568 }
1569
1570 // This will only incrememnet one of x or y based on the assertion above
1571 int x = cellX + direction[0];
1572 int y = cellY + direction[1];
1573 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1574
1575 boolean fail = false;
1576 for (int i = 0; i < spanX; i++) {
1577 for (int j = 0; j < spanY; j++) {
1578 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1579 fail = true;
1580 }
1581 }
1582 }
1583 if (!fail) {
1584 float distance = (float)
1585 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1586 if (Float.compare(distance, bestDistance) < 0) {
1587 bestDistance = distance;
1588 bestXY[0] = x;
1589 bestXY[1] = y;
1590 }
1591 }
1592 x += direction[0];
1593 y += direction[1];
1594 }
1595 return bestXY;
1596 }
1597
Adam Cohen482ed822012-03-02 14:15:13 -08001598 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001599 int[] direction, ItemConfiguration currentState) {
1600 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001601 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001602 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001603 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1604
Adam Cohen8baab352012-03-20 17:39:21 -07001605 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001606
1607 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001608 c.x = mTempLocation[0];
1609 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001610 success = true;
1611
1612 }
Adam Cohen8baab352012-03-20 17:39:21 -07001613 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001614 return success;
1615 }
1616
Adam Cohen47a876d2012-03-19 13:21:41 -07001617 // This method looks in the specified direction to see if there is an additional view
1618 // immediately adjecent in that direction
1619 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen19f37922012-03-21 11:59:11 -07001620 boolean[][] occupied, View dragView, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001621 boolean found = false;
1622
Michael Jurkaa52570f2012-03-20 03:18:20 -07001623 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001624 Rect r0 = new Rect(boundingRect);
1625 Rect r1 = new Rect();
1626
1627 int deltaX = 0;
1628 int deltaY = 0;
1629 if (direction[1] < 0) {
1630 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1631 deltaY = -1;
1632 } else if (direction[1] > 0) {
1633 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1634 deltaY = 1;
1635 } else if (direction[0] < 0) {
1636 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1637 deltaX = -1;
1638 } else if (direction[0] > 0) {
1639 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1640 deltaX = 1;
1641 }
1642
1643 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001644 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen19f37922012-03-21 11:59:11 -07001645 if (views.contains(child) || child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001646 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001647
Adam Cohen8baab352012-03-20 17:39:21 -07001648 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1649 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001650 if (Rect.intersects(r0, r1)) {
1651 if (!lp.canReorder) {
1652 return false;
1653 }
1654 boolean pushed = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001655 for (int x = c.x; x < c.x + c.spanX; x++) {
1656 for (int y = c.y; y < c.y + c.spanY; y++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001657 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1658 && y - deltaY >= 0 && y - deltaY < mCountY;
1659 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1660 pushed = true;
1661 }
1662 }
1663 }
1664 if (pushed) {
1665 views.add(child);
Adam Cohen8baab352012-03-20 17:39:21 -07001666 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001667 found = true;
1668 }
1669 }
1670 }
1671 return found;
1672 }
1673
Adam Cohen482ed822012-03-02 14:15:13 -08001674 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen19f37922012-03-21 11:59:11 -07001675 int[] direction, boolean push, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001676 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001677
Adam Cohen8baab352012-03-20 17:39:21 -07001678 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001679 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001680 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001681 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001682 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001683 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001684 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001685 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001686 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001687 }
1688 }
Adam Cohen8baab352012-03-20 17:39:21 -07001689
1690 @SuppressWarnings("unchecked")
1691 ArrayList<View> dup = (ArrayList<View>) views.clone();
1692 // We try and expand the group of views in the direction vector passed, based on
1693 // whether they are physically adjacent, ie. based on "push mechanics".
Adam Cohen19f37922012-03-21 11:59:11 -07001694 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied, dragView,
Adam Cohen8baab352012-03-20 17:39:21 -07001695 currentState)) {
1696 }
1697
1698 // Mark the occupied state as false for the group of views we want to move.
1699 for (View v: dup) {
1700 CellAndSpan c = currentState.map.get(v);
1701 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1702 }
1703
Adam Cohen47a876d2012-03-19 13:21:41 -07001704 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1705 int top = boundingRect.top;
1706 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001707 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
1708 // for tetris-style interlocking.
1709 for (View v: dup) {
1710 CellAndSpan c = currentState.map.get(v);
1711 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001712 }
1713
Adam Cohen482ed822012-03-02 14:15:13 -08001714 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1715
Adam Cohen8baab352012-03-20 17:39:21 -07001716 if (push) {
1717 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1718 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1719 } else {
1720 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1721 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1722 }
Adam Cohen482ed822012-03-02 14:15:13 -08001723
Adam Cohen8baab352012-03-20 17:39:21 -07001724 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001725 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001726 int deltaX = mTempLocation[0] - boundingRect.left;
1727 int deltaY = mTempLocation[1] - boundingRect.top;
1728 for (View v: dup) {
1729 CellAndSpan c = currentState.map.get(v);
1730 c.x += deltaX;
1731 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001732 }
1733 success = true;
1734 }
Adam Cohen8baab352012-03-20 17:39:21 -07001735
1736 // In either case, we set the occupied array as marked for the location of the views
1737 for (View v: dup) {
1738 CellAndSpan c = currentState.map.get(v);
1739 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001740 }
1741 return success;
1742 }
1743
1744 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1745 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1746 }
1747
1748 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001749 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07001750 // Return early if get invalid cell positions
1751 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08001752
Adam Cohen8baab352012-03-20 17:39:21 -07001753 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001754 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001755
Adam Cohen8baab352012-03-20 17:39:21 -07001756 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001757 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001758 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001759 if (c != null) {
1760 c.x = cellX;
1761 c.y = cellY;
1762 }
Adam Cohen482ed822012-03-02 14:15:13 -08001763 }
Adam Cohen482ed822012-03-02 14:15:13 -08001764 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1765 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001766 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001767 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001768 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001769 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001770 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001771 if (Rect.intersects(r0, r1)) {
1772 if (!lp.canReorder) {
1773 return false;
1774 }
1775 mIntersectingViews.add(child);
1776 }
1777 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001778
Adam Cohen8baab352012-03-20 17:39:21 -07001779 // We try to move the intersecting views as a block using the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001780 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1781 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001782 return true;
1783 }
1784 // Try the opposite direction
1785 direction[0] *= -1;
1786 direction[1] *= -1;
Adam Cohen19f37922012-03-21 11:59:11 -07001787 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1788 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001789 return true;
1790 }
1791 // Switch the direction back
1792 direction[0] *= -1;
1793 direction[1] *= -1;
1794
Adam Cohen8baab352012-03-20 17:39:21 -07001795 // Next we try moving the views as a block , but without requiring the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001796 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, ignoreView,
1797 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001798 return true;
1799 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001800
Adam Cohen482ed822012-03-02 14:15:13 -08001801 // Ok, they couldn't move as a block, let's move them individually
1802 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001803 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001804 return false;
1805 }
1806 }
1807 return true;
1808 }
1809
1810 /*
1811 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1812 * the provided point and the provided cell
1813 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001814 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001815 double angle = Math.atan(((float) deltaY) / deltaX);
1816
1817 result[0] = 0;
1818 result[1] = 0;
1819 if (Math.abs(Math.cos(angle)) > 0.5f) {
1820 result[0] = (int) Math.signum(deltaX);
1821 }
1822 if (Math.abs(Math.sin(angle)) > 0.5f) {
1823 result[1] = (int) Math.signum(deltaY);
1824 }
1825 }
1826
Adam Cohen8baab352012-03-20 17:39:21 -07001827 private void copyOccupiedArray(boolean[][] occupied) {
1828 for (int i = 0; i < mCountX; i++) {
1829 for (int j = 0; j < mCountY; j++) {
1830 occupied[i][j] = mOccupied[i][j];
1831 }
1832 }
1833 }
1834
Adam Cohen482ed822012-03-02 14:15:13 -08001835 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1836 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001837 // Copy the current state into the solution. This solution will be manipulated as necessary.
1838 copyCurrentStateToSolution(solution, false);
1839 // Copy the current occupied array into the temporary occupied array. This array will be
1840 // manipulated as necessary to find a solution.
1841 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001842
1843 // We find the nearest cell into which we would place the dragged item, assuming there's
1844 // nothing in its way.
1845 int result[] = new int[2];
1846 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1847
1848 boolean success = false;
1849 // First we try the exact nearest position of the item being dragged,
1850 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001851 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1852 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001853
1854 if (!success) {
1855 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1856 // x, then 1 in y etc.
1857 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1858 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1859 dragView, false, solution);
1860 } else if (spanY > minSpanY) {
1861 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1862 dragView, true, solution);
1863 }
1864 solution.isSolution = false;
1865 } else {
1866 solution.isSolution = true;
1867 solution.dragViewX = result[0];
1868 solution.dragViewY = result[1];
1869 solution.dragViewSpanX = spanX;
1870 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001871 }
1872 return solution;
1873 }
1874
1875 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001876 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001877 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001878 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001879 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001880 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001881 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001882 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001883 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001884 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001885 }
Adam Cohen8baab352012-03-20 17:39:21 -07001886 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001887 }
1888 }
1889
1890 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1891 for (int i = 0; i < mCountX; i++) {
1892 for (int j = 0; j < mCountY; j++) {
1893 mTmpOccupied[i][j] = false;
1894 }
1895 }
1896
Michael Jurkaa52570f2012-03-20 03:18:20 -07001897 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001898 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001899 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001900 if (child == dragView) continue;
1901 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001902 CellAndSpan c = solution.map.get(child);
1903 if (c != null) {
1904 lp.tmpCellX = c.x;
1905 lp.tmpCellY = c.y;
1906 lp.cellHSpan = c.spanX;
1907 lp.cellVSpan = c.spanY;
1908 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001909 }
1910 }
1911 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1912 solution.dragViewSpanY, mTmpOccupied, true);
1913 }
1914
1915 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1916 commitDragView) {
1917
1918 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1919 for (int i = 0; i < mCountX; i++) {
1920 for (int j = 0; j < mCountY; j++) {
1921 occupied[i][j] = false;
1922 }
1923 }
1924
Michael Jurkaa52570f2012-03-20 03:18:20 -07001925 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001926 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001927 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001928 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001929 CellAndSpan c = solution.map.get(child);
1930 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07001931 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
1932 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001933 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001934 }
1935 }
1936 if (commitDragView) {
1937 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1938 solution.dragViewSpanY, occupied, true);
1939 }
1940 }
1941
Adam Cohen19f37922012-03-21 11:59:11 -07001942 // This method starts or changes the reorder hint animations
1943 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
1944 int childCount = mShortcutsAndWidgets.getChildCount();
1945 int timeForPriorAnimationToComplete = getMaxCompletionTime();
1946 for (int i = 0; i < childCount; i++) {
1947 View child = mShortcutsAndWidgets.getChildAt(i);
1948 if (child == dragView) continue;
1949 CellAndSpan c = solution.map.get(child);
1950 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1951 if (c != null) {
1952 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
1953 c.x, c.y, c.spanX, c.spanY);
1954 rha.animate(timeForPriorAnimationToComplete);
1955 }
1956 }
1957 }
1958
1959 // Class which represents the reorder hint animations. These animations show that an item is
1960 // in a temporary state, and hint at where the item will return to.
1961 class ReorderHintAnimation {
1962 View child;
1963 float deltaX;
1964 float deltaY;
1965 private static final int DURATION = 140;
1966 private int repeatCount;
1967 private boolean cancelOnCycleComplete = false;
1968 ValueAnimator va;
1969
1970 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
1971 int spanX, int spanY) {
1972 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1973 final int x0 = mTmpPoint[0];
1974 final int y0 = mTmpPoint[1];
1975 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1976 final int x1 = mTmpPoint[0];
1977 final int y1 = mTmpPoint[1];
1978 final int dX = x1 - x0;
1979 final int dY = y1 - y0;
1980 deltaX = 0;
1981 deltaY = 0;
1982 if (dX == dY && dX == 0) {
1983 } else {
1984 if (dY == 0) {
1985 deltaX = mReorderHintAnimationMagnitude;
1986 } else if (dX == 0) {
1987 deltaY = mReorderHintAnimationMagnitude;
1988 } else {
1989 double angle = Math.atan( (float) (dY) / dX);
1990 deltaX = (int) (Math.cos(angle) * mReorderHintAnimationMagnitude);
1991 deltaY = (int) (Math.sin(angle) * mReorderHintAnimationMagnitude);
1992 }
1993 }
1994 this.child = child;
1995 }
1996
1997 void animate(int delay) {
1998 if (mShakeAnimators.containsKey(child)) {
1999 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
2000 oldAnimation.completeAnimation();
2001 mShakeAnimators.remove(child);
2002 }
2003 if (deltaX == 0 && deltaY == 0) {
2004 return;
2005 }
2006 va = ValueAnimator.ofFloat(0f, 1f);
2007 va.setRepeatMode(ValueAnimator.REVERSE);
2008 va.setRepeatCount(ValueAnimator.INFINITE);
2009 va.setDuration(DURATION);
2010 va.addUpdateListener(new AnimatorUpdateListener() {
2011 @Override
2012 public void onAnimationUpdate(ValueAnimator animation) {
2013 float r = ((Float) animation.getAnimatedValue()).floatValue();
2014 float x = r * deltaX;
2015 float y = r * deltaY;
2016 child.setTranslationX(x);
2017 child.setTranslationY(y);
2018 }
2019 });
2020 va.addListener(new AnimatorListenerAdapter() {
2021 public void onAnimationRepeat(Animator animation) {
2022 repeatCount++;
2023 // We make sure to end only after a full period
2024 if (cancelOnCycleComplete && repeatCount % 2 == 0) {
2025 va.cancel();
2026 }
2027 }
2028 });
2029 va.setStartDelay(Math.max(REORDER_ANIMATION_DURATION, delay));
2030 mShakeAnimators.put(child, this);
2031 va.start();
2032 }
2033
2034
2035 private void completeAnimation() {
2036 cancelOnCycleComplete = true;
2037 }
2038
2039 // Returns the time required to complete the current oscillating animation
2040 private int completionTime() {
2041 if (repeatCount % 2 == 0) {
2042 return (int) (va.getDuration() - va.getCurrentPlayTime() + DURATION);
2043 } else {
2044 return (int) (va.getDuration() - va.getCurrentPlayTime());
2045 }
2046 }
2047 }
2048
2049 private void completeAndClearReorderHintAnimations() {
2050 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2051 a.completeAnimation();
2052 }
2053 mShakeAnimators.clear();
2054 }
2055
2056 private int getMaxCompletionTime() {
2057 int maxTime = 0;
2058 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2059 maxTime = Math.max(maxTime, a.completionTime());
2060 }
2061 return maxTime;
2062 }
2063
Adam Cohen482ed822012-03-02 14:15:13 -08002064 private void commitTempPlacement() {
2065 for (int i = 0; i < mCountX; i++) {
2066 for (int j = 0; j < mCountY; j++) {
2067 mOccupied[i][j] = mTmpOccupied[i][j];
2068 }
2069 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002070 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002071 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002072 View child = mShortcutsAndWidgets.getChildAt(i);
2073 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2074 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002075 // We do a null check here because the item info can be null in the case of the
2076 // AllApps button in the hotseat.
2077 if (info != null) {
2078 info.cellX = lp.cellX = lp.tmpCellX;
2079 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002080 info.spanX = lp.cellHSpan;
2081 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002082 }
Adam Cohen482ed822012-03-02 14:15:13 -08002083 }
Adam Cohen2acce882012-03-28 19:03:19 -07002084 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002085 }
2086
2087 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002088 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002089 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002090 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002091 lp.useTmpCoords = useTempCoords;
2092 }
2093 }
2094
Adam Cohen482ed822012-03-02 14:15:13 -08002095 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2096 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2097 int[] result = new int[2];
2098 int[] resultSpan = new int[2];
2099 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2100 resultSpan);
2101 if (result[0] >= 0 && result[1] >= 0) {
2102 copyCurrentStateToSolution(solution, false);
2103 solution.dragViewX = result[0];
2104 solution.dragViewY = result[1];
2105 solution.dragViewSpanX = resultSpan[0];
2106 solution.dragViewSpanY = resultSpan[1];
2107 solution.isSolution = true;
2108 } else {
2109 solution.isSolution = false;
2110 }
2111 return solution;
2112 }
2113
2114 public void prepareChildForDrag(View child) {
2115 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002116 }
2117
Adam Cohen19f37922012-03-21 11:59:11 -07002118 /* This seems like it should be obvious and straight-forward, but when the direction vector
2119 needs to match with the notion of the dragView pushing other views, we have to employ
2120 a slightly more subtle notion of the direction vector. The question is what two points is
2121 the vector between? The center of the dragView and its desired destination? Not quite, as
2122 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2123 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2124 or right, which helps make pushing feel right.
2125 */
2126 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2127 int spanY, View dragView, int[] resultDirection) {
2128 int[] targetDestination = new int[2];
2129
2130 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2131 Rect dragRect = new Rect();
2132 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2133 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2134
2135 Rect dropRegionRect = new Rect();
2136 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2137 dragView, dropRegionRect, mIntersectingViews);
2138
2139 int dropRegionSpanX = dropRegionRect.width();
2140 int dropRegionSpanY = dropRegionRect.height();
2141
2142 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2143 dropRegionRect.height(), dropRegionRect);
2144
2145 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2146 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2147
2148 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2149 deltaX = 0;
2150 }
2151 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2152 deltaY = 0;
2153 }
2154
2155 if (deltaX == 0 && deltaY == 0) {
2156 // No idea what to do, give a random direction.
2157 resultDirection[0] = 1;
2158 resultDirection[1] = 0;
2159 } else {
2160 computeDirectionVector(deltaX, deltaY, resultDirection);
2161 }
2162 }
2163
2164 // For a given cell and span, fetch the set of views intersecting the region.
2165 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2166 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2167 if (boundingRect != null) {
2168 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2169 }
2170 intersectingViews.clear();
2171 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2172 Rect r1 = new Rect();
2173 final int count = mShortcutsAndWidgets.getChildCount();
2174 for (int i = 0; i < count; i++) {
2175 View child = mShortcutsAndWidgets.getChildAt(i);
2176 if (child == dragView) continue;
2177 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2178 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2179 if (Rect.intersects(r0, r1)) {
2180 mIntersectingViews.add(child);
2181 if (boundingRect != null) {
2182 boundingRect.union(r1);
2183 }
2184 }
2185 }
2186 }
2187
2188 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2189 View dragView, int[] result) {
2190 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2191 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2192 mIntersectingViews);
2193 return !mIntersectingViews.isEmpty();
2194 }
2195
2196 void revertTempState() {
2197 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2198 final int count = mShortcutsAndWidgets.getChildCount();
2199 for (int i = 0; i < count; i++) {
2200 View child = mShortcutsAndWidgets.getChildAt(i);
2201 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2202 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2203 lp.tmpCellX = lp.cellX;
2204 lp.tmpCellY = lp.cellY;
2205 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2206 0, false, false);
2207 }
2208 }
2209 completeAndClearReorderHintAnimations();
2210 setItemPlacementDirty(false);
2211 }
2212
Adam Cohenbebf0422012-04-11 18:06:28 -07002213 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2214 View dragView, int[] direction, boolean commit) {
2215 int[] pixelXY = new int[2];
2216 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2217
2218 // First we determine if things have moved enough to cause a different layout
2219 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2220 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2221
2222 setUseTempCoords(true);
2223 if (swapSolution != null && swapSolution.isSolution) {
2224 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2225 // committing anything or animating anything as we just want to determine if a solution
2226 // exists
2227 copySolutionToTempState(swapSolution, dragView);
2228 setItemPlacementDirty(true);
2229 animateItemsToSolution(swapSolution, dragView, commit);
2230
2231 if (commit) {
2232 commitTempPlacement();
2233 completeAndClearReorderHintAnimations();
2234 setItemPlacementDirty(false);
2235 } else {
2236 beginOrAdjustHintAnimations(swapSolution, dragView,
2237 REORDER_ANIMATION_DURATION);
2238 }
2239 mShortcutsAndWidgets.requestLayout();
2240 }
2241 return swapSolution.isSolution;
2242 }
2243
Adam Cohen482ed822012-03-02 14:15:13 -08002244 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2245 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002246 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002247 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002248
2249 if (resultSpan == null) {
2250 resultSpan = new int[2];
2251 }
2252
Adam Cohen19f37922012-03-21 11:59:11 -07002253 // When we are checking drop validity or actually dropping, we don't recompute the
2254 // direction vector, since we want the solution to match the preview, and it's possible
2255 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002256 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2257 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002258 mDirectionVector[0] = mPreviousReorderDirection[0];
2259 mDirectionVector[1] = mPreviousReorderDirection[1];
2260 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002261 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2262 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2263 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002264 }
2265 } else {
2266 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2267 mPreviousReorderDirection[0] = mDirectionVector[0];
2268 mPreviousReorderDirection[1] = mDirectionVector[1];
2269 }
2270
Adam Cohen482ed822012-03-02 14:15:13 -08002271 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2272 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2273
2274 // We attempt the approach which doesn't shuffle views at all
2275 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2276 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2277
2278 ItemConfiguration finalSolution = null;
2279 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2280 finalSolution = swapSolution;
2281 } else if (noShuffleSolution.isSolution) {
2282 finalSolution = noShuffleSolution;
2283 }
2284
2285 boolean foundSolution = true;
2286 if (!DESTRUCTIVE_REORDER) {
2287 setUseTempCoords(true);
2288 }
2289
2290 if (finalSolution != null) {
2291 result[0] = finalSolution.dragViewX;
2292 result[1] = finalSolution.dragViewY;
2293 resultSpan[0] = finalSolution.dragViewSpanX;
2294 resultSpan[1] = finalSolution.dragViewSpanY;
2295
2296 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2297 // committing anything or animating anything as we just want to determine if a solution
2298 // exists
2299 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2300 if (!DESTRUCTIVE_REORDER) {
2301 copySolutionToTempState(finalSolution, dragView);
2302 }
2303 setItemPlacementDirty(true);
2304 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2305
Adam Cohen19f37922012-03-21 11:59:11 -07002306 if (!DESTRUCTIVE_REORDER &&
2307 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002308 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002309 completeAndClearReorderHintAnimations();
2310 setItemPlacementDirty(false);
2311 } else {
2312 beginOrAdjustHintAnimations(finalSolution, dragView,
2313 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002314 }
2315 }
2316 } else {
2317 foundSolution = false;
2318 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2319 }
2320
2321 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2322 setUseTempCoords(false);
2323 }
Adam Cohen482ed822012-03-02 14:15:13 -08002324
Michael Jurkaa52570f2012-03-20 03:18:20 -07002325 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002326 return result;
2327 }
2328
Adam Cohen19f37922012-03-21 11:59:11 -07002329 void setItemPlacementDirty(boolean dirty) {
2330 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002331 }
Adam Cohen19f37922012-03-21 11:59:11 -07002332 boolean isItemPlacementDirty() {
2333 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002334 }
2335
2336 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002337 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002338 boolean isSolution = false;
2339 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2340
2341 int area() {
2342 return dragViewSpanX * dragViewSpanY;
2343 }
Adam Cohen8baab352012-03-20 17:39:21 -07002344 }
2345
2346 private class CellAndSpan {
2347 int x, y;
2348 int spanX, spanY;
2349
2350 public CellAndSpan(int x, int y, int spanX, int spanY) {
2351 this.x = x;
2352 this.y = y;
2353 this.spanX = spanX;
2354 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002355 }
2356 }
2357
Adam Cohendf035382011-04-11 17:22:04 -07002358 /**
2359 * Find a vacant area that will fit the given bounds nearest the requested
2360 * cell location. Uses Euclidean distance to score multiple vacant areas.
2361 *
2362 * @param pixelX The X location at which you want to search for a vacant area.
2363 * @param pixelY The Y location at which you want to search for a vacant area.
2364 * @param spanX Horizontal span of the object.
2365 * @param spanY Vertical span of the object.
2366 * @param ignoreView Considers space occupied by this view as unoccupied
2367 * @param result Previously returned value to possibly recycle.
2368 * @return The X, Y cell of a vacant area that can contain this object,
2369 * nearest the requested location.
2370 */
2371 int[] findNearestVacantArea(
2372 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2373 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2374 }
2375
2376 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002377 * Find a vacant area that will fit the given bounds nearest the requested
2378 * cell location. Uses Euclidean distance to score multiple vacant areas.
2379 *
2380 * @param pixelX The X location at which you want to search for a vacant area.
2381 * @param pixelY The Y location at which you want to search for a vacant area.
2382 * @param minSpanX The minimum horizontal span required
2383 * @param minSpanY The minimum vertical span required
2384 * @param spanX Horizontal span of the object.
2385 * @param spanY Vertical span of the object.
2386 * @param ignoreView Considers space occupied by this view as unoccupied
2387 * @param result Previously returned value to possibly recycle.
2388 * @return The X, Y cell of a vacant area that can contain this object,
2389 * nearest the requested location.
2390 */
2391 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2392 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002393 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2394 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002395 }
2396
2397 /**
Adam Cohendf035382011-04-11 17:22:04 -07002398 * Find a starting cell position that will fit the given bounds nearest the requested
2399 * cell location. Uses Euclidean distance to score multiple vacant areas.
2400 *
2401 * @param pixelX The X location at which you want to search for a vacant area.
2402 * @param pixelY The Y location at which you want to search for a vacant area.
2403 * @param spanX Horizontal span of the object.
2404 * @param spanY Vertical span of the object.
2405 * @param ignoreView Considers space occupied by this view as unoccupied
2406 * @param result Previously returned value to possibly recycle.
2407 * @return The X, Y cell of a vacant area that can contain this object,
2408 * nearest the requested location.
2409 */
2410 int[] findNearestArea(
2411 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2412 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2413 }
2414
Michael Jurka0280c3b2010-09-17 15:00:07 -07002415 boolean existsEmptyCell() {
2416 return findCellForSpan(null, 1, 1);
2417 }
2418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002420 * Finds the upper-left coordinate of the first rectangle in the grid that can
2421 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2422 * then this method will only return coordinates for rectangles that contain the cell
2423 * (intersectX, intersectY)
2424 *
2425 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2426 * can be found.
2427 * @param spanX The horizontal span of the cell we want to find.
2428 * @param spanY The vertical span of the cell we want to find.
2429 *
2430 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002431 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002432 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002433 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002434 }
2435
2436 /**
2437 * Like above, but ignores any cells occupied by the item "ignoreView"
2438 *
2439 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2440 * can be found.
2441 * @param spanX The horizontal span of the cell we want to find.
2442 * @param spanY The vertical span of the cell we want to find.
2443 * @param ignoreView The home screen item we should treat as not occupying any space
2444 * @return
2445 */
2446 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002447 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2448 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002449 }
2450
2451 /**
2452 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2453 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2454 *
2455 * @param spanX The horizontal span of the cell we want to find.
2456 * @param spanY The vertical span of the cell we want to find.
2457 * @param ignoreView The home screen item we should treat as not occupying any space
2458 * @param intersectX The X coordinate of the cell that we should try to overlap
2459 * @param intersectX The Y coordinate of the cell that we should try to overlap
2460 *
2461 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2462 */
2463 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2464 int intersectX, int intersectY) {
2465 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002466 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002467 }
2468
2469 /**
2470 * The superset of the above two methods
2471 */
2472 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002473 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002474 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002475 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002476
Michael Jurka28750fb2010-09-24 17:43:49 -07002477 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002478 while (true) {
2479 int startX = 0;
2480 if (intersectX >= 0) {
2481 startX = Math.max(startX, intersectX - (spanX - 1));
2482 }
2483 int endX = mCountX - (spanX - 1);
2484 if (intersectX >= 0) {
2485 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2486 }
2487 int startY = 0;
2488 if (intersectY >= 0) {
2489 startY = Math.max(startY, intersectY - (spanY - 1));
2490 }
2491 int endY = mCountY - (spanY - 1);
2492 if (intersectY >= 0) {
2493 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2494 }
2495
Winson Chungbbc60d82010-11-11 16:34:41 -08002496 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002497 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002498 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002499 for (int i = 0; i < spanX; i++) {
2500 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002501 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002502 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002503 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002504 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002505 continue inner;
2506 }
2507 }
2508 }
2509 if (cellXY != null) {
2510 cellXY[0] = x;
2511 cellXY[1] = y;
2512 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002513 foundCell = true;
2514 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002515 }
2516 }
2517 if (intersectX == -1 && intersectY == -1) {
2518 break;
2519 } else {
2520 // if we failed to find anything, try again but without any requirements of
2521 // intersecting
2522 intersectX = -1;
2523 intersectY = -1;
2524 continue;
2525 }
2526 }
2527
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002528 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002529 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002530 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002531 }
2532
2533 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002534 * A drag event has begun over this layout.
2535 * It may have begun over this layout (in which case onDragChild is called first),
2536 * or it may have begun on another layout.
2537 */
2538 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002539 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002540 mDragging = true;
2541 }
2542
2543 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002544 * Called when drag has left this CellLayout or has been completed (successfully or not)
2545 */
2546 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002547 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002548 // This can actually be called when we aren't in a drag, e.g. when adding a new
2549 // item to this layout via the customize drawer.
2550 // Guard against that case.
2551 if (mDragging) {
2552 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002553 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002554
2555 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002556 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002557 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2558 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002559 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002560 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002561 }
2562
2563 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002564 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002565 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002566 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567 *
2568 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002570 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002571 if (child != null) {
2572 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002573 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002574 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002575 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 }
2577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002578 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002580 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 * @param cellX X coordinate of upper left corner expressed as a cell position
2582 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002583 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002584 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002585 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002586 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002587 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002588 final int cellWidth = mCellWidth;
2589 final int cellHeight = mCellHeight;
2590 final int widthGap = mWidthGap;
2591 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002592
Winson Chung4b825dcd2011-06-19 12:41:22 -07002593 final int hStartPadding = getPaddingLeft();
2594 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002595
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002596 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2597 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2598
2599 int x = hStartPadding + cellX * (cellWidth + widthGap);
2600 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002601
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002602 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002603 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002604
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002605 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002606 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002607 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002608 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002609 * @param width Width in pixels
2610 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002611 * @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 -08002612 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002613 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002614 return rectToCell(getResources(), width, height, result);
2615 }
2616
2617 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002618 // Always assume we're working with the smallest span to make sure we
2619 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002620 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2621 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002622 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002623
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002624 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002625 int spanX = (int) Math.ceil(width / (float) smallerSize);
2626 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002627
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002628 if (result == null) {
2629 return new int[] { spanX, spanY };
2630 }
2631 result[0] = spanX;
2632 result[1] = spanY;
2633 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 }
2635
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002636 public int[] cellSpansToSize(int hSpans, int vSpans) {
2637 int[] size = new int[2];
2638 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2639 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2640 return size;
2641 }
2642
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002643 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002644 * Calculate the grid spans needed to fit given item
2645 */
2646 public void calculateSpans(ItemInfo info) {
2647 final int minWidth;
2648 final int minHeight;
2649
2650 if (info instanceof LauncherAppWidgetInfo) {
2651 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2652 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2653 } else if (info instanceof PendingAddWidgetInfo) {
2654 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2655 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2656 } else {
2657 // It's not a widget, so it must be 1x1
2658 info.spanX = info.spanY = 1;
2659 return;
2660 }
2661 int[] spans = rectToCell(minWidth, minHeight, null);
2662 info.spanX = spans[0];
2663 info.spanY = spans[1];
2664 }
2665
2666 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002667 * Find the first vacant cell, if there is one.
2668 *
2669 * @param vacant Holds the x and y coordinate of the vacant cell
2670 * @param spanX Horizontal cell span.
2671 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002672 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002673 * @return True if a vacant cell was found
2674 */
2675 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002676
Michael Jurka0280c3b2010-09-17 15:00:07 -07002677 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002678 }
2679
2680 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2681 int xCount, int yCount, boolean[][] occupied) {
2682
Adam Cohen2801caf2011-05-13 20:57:39 -07002683 for (int y = 0; y < yCount; y++) {
2684 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002685 boolean available = !occupied[x][y];
2686out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2687 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2688 available = available && !occupied[i][j];
2689 if (!available) break out;
2690 }
2691 }
2692
2693 if (available) {
2694 vacant[0] = x;
2695 vacant[1] = y;
2696 return true;
2697 }
2698 }
2699 }
2700
2701 return false;
2702 }
2703
Michael Jurka0280c3b2010-09-17 15:00:07 -07002704 private void clearOccupiedCells() {
2705 for (int x = 0; x < mCountX; x++) {
2706 for (int y = 0; y < mCountY; y++) {
2707 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002708 }
2709 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002710 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002711
Adam Cohend41fbf52012-02-16 23:53:59 -08002712 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002713 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002714 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002716
Adam Cohend4844c32011-02-18 19:25:06 -08002717 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002718 markCellsAsOccupiedForView(view, mOccupied);
2719 }
2720 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002721 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002722 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002723 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002724 }
2725
Adam Cohend4844c32011-02-18 19:25:06 -08002726 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002727 markCellsAsUnoccupiedForView(view, mOccupied);
2728 }
2729 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002730 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002731 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002732 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002733 }
2734
Adam Cohen482ed822012-03-02 14:15:13 -08002735 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2736 boolean value) {
2737 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002738 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2739 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002740 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002741 }
2742 }
2743 }
2744
Adam Cohen2801caf2011-05-13 20:57:39 -07002745 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002746 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002747 (Math.max((mCountX - 1), 0) * mWidthGap);
2748 }
2749
2750 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002751 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002752 (Math.max((mCountY - 1), 0) * mHeightGap);
2753 }
2754
Michael Jurka66d72172011-04-12 16:29:25 -07002755 public boolean isOccupied(int x, int y) {
2756 if (x < mCountX && y < mCountY) {
2757 return mOccupied[x][y];
2758 } else {
2759 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2760 }
2761 }
2762
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763 @Override
2764 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2765 return new CellLayout.LayoutParams(getContext(), attrs);
2766 }
2767
2768 @Override
2769 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2770 return p instanceof CellLayout.LayoutParams;
2771 }
2772
2773 @Override
2774 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2775 return new CellLayout.LayoutParams(p);
2776 }
2777
Winson Chungaafa03c2010-06-11 17:34:16 -07002778 public static class CellLayoutAnimationController extends LayoutAnimationController {
2779 public CellLayoutAnimationController(Animation animation, float delay) {
2780 super(animation, delay);
2781 }
2782
2783 @Override
2784 protected long getDelayForView(View view) {
2785 return (int) (Math.random() * 150);
2786 }
2787 }
2788
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2790 /**
2791 * Horizontal location of the item in the grid.
2792 */
2793 @ViewDebug.ExportedProperty
2794 public int cellX;
2795
2796 /**
2797 * Vertical location of the item in the grid.
2798 */
2799 @ViewDebug.ExportedProperty
2800 public int cellY;
2801
2802 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002803 * Temporary horizontal location of the item in the grid during reorder
2804 */
2805 public int tmpCellX;
2806
2807 /**
2808 * Temporary vertical location of the item in the grid during reorder
2809 */
2810 public int tmpCellY;
2811
2812 /**
2813 * Indicates that the temporary coordinates should be used to layout the items
2814 */
2815 public boolean useTmpCoords;
2816
2817 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002818 * Number of cells spanned horizontally by the item.
2819 */
2820 @ViewDebug.ExportedProperty
2821 public int cellHSpan;
2822
2823 /**
2824 * Number of cells spanned vertically by the item.
2825 */
2826 @ViewDebug.ExportedProperty
2827 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002828
Adam Cohen1b607ed2011-03-03 17:26:50 -08002829 /**
2830 * Indicates whether the item will set its x, y, width and height parameters freely,
2831 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2832 */
Adam Cohend4844c32011-02-18 19:25:06 -08002833 public boolean isLockedToGrid = true;
2834
Adam Cohen482ed822012-03-02 14:15:13 -08002835 /**
2836 * Indicates whether this item can be reordered. Always true except in the case of the
2837 * the AllApps button.
2838 */
2839 public boolean canReorder = true;
2840
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002841 // X coordinate of the view in the layout.
2842 @ViewDebug.ExportedProperty
2843 int x;
2844 // Y coordinate of the view in the layout.
2845 @ViewDebug.ExportedProperty
2846 int y;
2847
Romain Guy84f296c2009-11-04 15:00:44 -08002848 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002850 public LayoutParams(Context c, AttributeSet attrs) {
2851 super(c, attrs);
2852 cellHSpan = 1;
2853 cellVSpan = 1;
2854 }
2855
2856 public LayoutParams(ViewGroup.LayoutParams source) {
2857 super(source);
2858 cellHSpan = 1;
2859 cellVSpan = 1;
2860 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002861
2862 public LayoutParams(LayoutParams source) {
2863 super(source);
2864 this.cellX = source.cellX;
2865 this.cellY = source.cellY;
2866 this.cellHSpan = source.cellHSpan;
2867 this.cellVSpan = source.cellVSpan;
2868 }
2869
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002870 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002871 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002872 this.cellX = cellX;
2873 this.cellY = cellY;
2874 this.cellHSpan = cellHSpan;
2875 this.cellVSpan = cellVSpan;
2876 }
2877
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002878 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002879 if (isLockedToGrid) {
2880 final int myCellHSpan = cellHSpan;
2881 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002882 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2883 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002884
Adam Cohend4844c32011-02-18 19:25:06 -08002885 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2886 leftMargin - rightMargin;
2887 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2888 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002889 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2890 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002891 }
2892 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002893
Winson Chungaafa03c2010-06-11 17:34:16 -07002894 public String toString() {
2895 return "(" + this.cellX + ", " + this.cellY + ")";
2896 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002897
2898 public void setWidth(int width) {
2899 this.width = width;
2900 }
2901
2902 public int getWidth() {
2903 return width;
2904 }
2905
2906 public void setHeight(int height) {
2907 this.height = height;
2908 }
2909
2910 public int getHeight() {
2911 return height;
2912 }
2913
2914 public void setX(int x) {
2915 this.x = x;
2916 }
2917
2918 public int getX() {
2919 return x;
2920 }
2921
2922 public void setY(int y) {
2923 this.y = y;
2924 }
2925
2926 public int getY() {
2927 return y;
2928 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 }
2930
Michael Jurka0280c3b2010-09-17 15:00:07 -07002931 // This class stores info for two purposes:
2932 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2933 // its spanX, spanY, and the screen it is on
2934 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2935 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2936 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002937 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002939 int cellX = -1;
2940 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002941 int spanX;
2942 int spanY;
2943 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002944 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002946 @Override
2947 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002948 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2949 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 }
2951 }
Michael Jurkad771c962011-08-09 15:00:48 -07002952
2953 public boolean lastDownOnOccupiedCell() {
2954 return mLastDownOnOccupiedCell;
2955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956}