blob: 10eb8f8a41d41835f074220c5305ba3f9253d7d2 [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;
Winson Chung4b825dcd2011-06-19 12:41:22 -070061 private int mOriginalCellWidth;
62 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 private int mCellWidth;
64 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070065
Adam Cohend22015c2010-07-26 22:02:18 -070066 private int mCountX;
67 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068
Adam Cohen234c4cd2011-07-17 21:03:04 -070069 private int mOriginalWidthGap;
70 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071 private int mWidthGap;
72 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070073 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080074 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075
76 private final Rect mRect = new Rect();
77 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070078
Patrick Dubroyde7658b2010-09-27 11:15:43 -070079 // These are temporary variables to prevent having to allocate a new object just to
80 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070081 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070082 private final int[] mTmpPoint = new int[2];
83 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070084 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070085
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080087 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070088 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089
Michael Jurkadee05892010-07-27 10:01:56 -070090 private OnTouchListener mInterceptTouchListener;
91
Adam Cohen69ce2e52011-07-03 19:25:21 -070092 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070093 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070094
Adam Cohenb5ba0972011-09-07 18:02:31 -070095 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070096 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070097 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070098
Michael Jurka33945b22010-12-21 18:19:38 -080099 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800100 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700101 private Drawable mOverScrollForegroundDrawable;
102 private Drawable mOverScrollLeft;
103 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700104 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700105 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700106 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700107
Michael Jurka33945b22010-12-21 18:19:38 -0800108 // If we're actively dragging something over this screen, mIsDragOverlapping is true
109 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700110 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700111
Winson Chung150fbab2010-09-29 17:14:26 -0700112 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700113 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800114 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700115 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700116 private InterruptibleInOutAnimator[] mDragOutlineAnims =
117 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700118
119 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700120 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700121 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700122
Patrick Dubroy96864c32011-03-10 17:17:23 -0800123 private BubbleTextView mPressedOrFocusedIcon;
124
Adam Cohen482ed822012-03-02 14:15:13 -0800125 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
126 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700127 private HashMap<View, ReorderHintAnimation>
128 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
129
130 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700131
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700132 // When a drag operation is in progress, holds the nearest cell to the touch point
133 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Joe Onorato4be866d2010-10-10 11:26:02 -0700135 private boolean mDragging = false;
136
Patrick Dubroyce34a972010-10-19 10:34:32 -0700137 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700138 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700139
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800140 private boolean mIsHotseat = false;
Winson Chungeecf02d2012-03-02 17:14:58 -0800141 private float mChildScale = 1f;
142 private float mHotseatChildScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800143
Adam Cohen482ed822012-03-02 14:15:13 -0800144 public static final int MODE_DRAG_OVER = 0;
145 public static final int MODE_ON_DROP = 1;
146 public static final int MODE_ON_DROP_EXTERNAL = 2;
147 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700148 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800149 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
150
Adam Cohen19f37922012-03-21 11:59:11 -0700151 private static final float REORDER_HINT_MAGNITUDE = 0.27f;
152 private static final int REORDER_ANIMATION_DURATION = 150;
153 private float mReorderHintAnimationMagnitude;
154
Adam Cohen482ed822012-03-02 14:15:13 -0800155 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
156 private Rect mOccupiedRect = new Rect();
157 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700158 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700159 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700160 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 public CellLayout(Context context) {
163 this(context, null);
164 }
165
166 public CellLayout(Context context, AttributeSet attrs) {
167 this(context, attrs, 0);
168 }
169
170 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
171 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700172 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700173
174 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
175 // the user where a dragged item will land when dropped.
176 setWillNotDraw(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700177 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
180
Winson Chung4b825dcd2011-06-19 12:41:22 -0700181 mOriginalCellWidth =
182 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
183 mOriginalCellHeight =
184 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700185 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
186 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700187 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700188 mCountX = LauncherModel.getCellCountX();
189 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700190 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800191 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700192 mPreviousReorderDirection[0] = INVALID_DIRECTION;
193 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
195 a.recycle();
196
197 setAlwaysDrawnWithCacheEnabled(false);
198
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700199 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700200
Winson Chung967289b2011-06-30 18:09:30 -0700201 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700202 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800203
Adam Cohenb5ba0972011-09-07 18:02:31 -0700204 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
205 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
206 mForegroundPadding =
207 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800208
Adam Cohen19f37922012-03-21 11:59:11 -0700209 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
210 res.getDimensionPixelSize(R.dimen.app_icon_size));
211
Winson Chungb26f3d62011-06-02 10:49:29 -0700212 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700213 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700214
Winson Chungeecf02d2012-03-02 17:14:58 -0800215 int iconScale = res.getInteger(R.integer.app_icon_scale_percent);
216 if (iconScale >= 0) {
217 mChildScale = iconScale / 100f;
218 }
219 int hotseatIconScale = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
220 if (hotseatIconScale >= 0) {
221 mHotseatChildScale = hotseatIconScale / 100f;
222 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800223
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700224 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700225
Patrick Dubroyce34a972010-10-19 10:34:32 -0700226 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700227
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700228
Winson Chungb8c69f32011-10-19 21:36:08 -0700229 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700230 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800231 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700232 }
233
234 // When dragging things around the home screens, we show a green outline of
235 // where the item will land. The outlines gradually fade out, leaving a trail
236 // behind the drag path.
237 // Set up all the animations that are used to implement this fading.
238 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700239 final float fromAlphaValue = 0;
240 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700241
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700242 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700243
244 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700245 final InterruptibleInOutAnimator anim =
246 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700247 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700248 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700249 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700250 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700251 final Bitmap outline = (Bitmap)anim.getTag();
252
253 // If an animation is started and then stopped very quickly, we can still
254 // get spurious updates we've cleared the tag. Guard against this.
255 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700256 @SuppressWarnings("all") // suppress dead code warning
257 final boolean debug = false;
258 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700259 Object val = animation.getAnimatedValue();
260 Log.d(TAG, "anim " + thisIndex + " update: " + val +
261 ", isStopped " + anim.isStopped());
262 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700263 // Try to prevent it from continuing to run
264 animation.cancel();
265 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700266 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800267 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700269 }
270 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700271 // The animation holds a reference to the drag outline bitmap as long is it's
272 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700273 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700274 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700275 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700276 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700277 anim.setTag(null);
278 }
279 }
280 });
281 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700282 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700283
Michael Jurka18014792010-10-14 09:01:34 -0700284 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700285 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800286
Michael Jurkaa52570f2012-03-20 03:18:20 -0700287 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
288 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
289 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700290 }
291
Michael Jurkaf6440da2011-04-05 14:50:34 -0700292 static int widthInPortrait(Resources r, int numCells) {
293 // We use this method from Workspace to figure out how many rows/columns Launcher should
294 // have. We ignore the left/right padding on CellLayout because it turns out in our design
295 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700296 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700297 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
298 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700299
Winson Chung4b825dcd2011-06-19 12:41:22 -0700300 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700301 }
302
Michael Jurkaf6440da2011-04-05 14:50:34 -0700303 static int heightInLandscape(Resources r, int numCells) {
304 // We use this method from Workspace to figure out how many rows/columns Launcher should
305 // have. We ignore the left/right padding on CellLayout because it turns out in our design
306 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700307 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700308 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
309 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700310
Winson Chung4b825dcd2011-06-19 12:41:22 -0700311 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700312 }
313
Adam Cohen2801caf2011-05-13 20:57:39 -0700314 public void enableHardwareLayers() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700315 mShortcutsAndWidgets.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700316 }
317
318 public void setGridSize(int x, int y) {
319 mCountX = x;
320 mCountY = y;
321 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800322 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700323 mTempRectStack.clear();
Adam Cohen76fc0852011-06-17 13:26:23 -0700324 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700325 }
326
Patrick Dubroy96864c32011-03-10 17:17:23 -0800327 private void invalidateBubbleTextView(BubbleTextView icon) {
328 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700329 invalidate(icon.getLeft() + getPaddingLeft() - padding,
330 icon.getTop() + getPaddingTop() - padding,
331 icon.getRight() + getPaddingLeft() + padding,
332 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800333 }
334
Adam Cohenb5ba0972011-09-07 18:02:31 -0700335 void setOverScrollAmount(float r, boolean left) {
336 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
337 mOverScrollForegroundDrawable = mOverScrollLeft;
338 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
339 mOverScrollForegroundDrawable = mOverScrollRight;
340 }
341
342 mForegroundAlpha = (int) Math.round((r * 255));
343 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
344 invalidate();
345 }
346
Patrick Dubroy96864c32011-03-10 17:17:23 -0800347 void setPressedOrFocusedIcon(BubbleTextView icon) {
348 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
349 // requires an expanded clip rect (due to the glow's blur radius)
350 BubbleTextView oldIcon = mPressedOrFocusedIcon;
351 mPressedOrFocusedIcon = icon;
352 if (oldIcon != null) {
353 invalidateBubbleTextView(oldIcon);
354 }
355 if (mPressedOrFocusedIcon != null) {
356 invalidateBubbleTextView(mPressedOrFocusedIcon);
357 }
358 }
359
Michael Jurka33945b22010-12-21 18:19:38 -0800360 void setIsDragOverlapping(boolean isDragOverlapping) {
361 if (mIsDragOverlapping != isDragOverlapping) {
362 mIsDragOverlapping = isDragOverlapping;
363 invalidate();
364 }
365 }
366
367 boolean getIsDragOverlapping() {
368 return mIsDragOverlapping;
369 }
370
Adam Cohenebea84d2011-11-09 17:20:41 -0800371 protected void setOverscrollTransformsDirty(boolean dirty) {
372 mScrollingTransformsDirty = dirty;
373 }
374
375 protected void resetOverscrollTransforms() {
376 if (mScrollingTransformsDirty) {
377 setOverscrollTransformsDirty(false);
378 setTranslationX(0);
379 setRotationY(0);
380 // It doesn't matter if we pass true or false here, the important thing is that we
381 // pass 0, which results in the overscroll drawable not being drawn any more.
382 setOverScrollAmount(0, false);
383 setPivotX(getMeasuredWidth() / 2);
384 setPivotY(getMeasuredHeight() / 2);
385 }
386 }
387
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700388 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700389 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700390 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
391 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
392 // When we're small, we are either drawn normally or in the "accepts drops" state (during
393 // a drag). However, we also drag the mini hover background *over* one of those two
394 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700395 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700396 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800397
398 if (mIsDragOverlapping) {
399 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700400 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700401 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700402 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700403 }
Michael Jurka33945b22010-12-21 18:19:38 -0800404
405 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
406 bg.setBounds(mBackgroundRect);
407 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700408 }
Romain Guya6abce82009-11-10 02:54:41 -0800409
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700410 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700411 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700412 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700413 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800414 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700415 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700416 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800417 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700418 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700419 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800420
421 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
422 // requires an expanded clip rect (due to the glow's blur radius)
423 if (mPressedOrFocusedIcon != null) {
424 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
425 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
426 if (b != null) {
427 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700428 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
429 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800430 null);
431 }
432 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700433
Adam Cohen482ed822012-03-02 14:15:13 -0800434 if (DEBUG_VISUALIZE_OCCUPIED) {
435 int[] pt = new int[2];
436 ColorDrawable cd = new ColorDrawable(Color.RED);
437 cd.setBounds(0, 0, 80, 80);
438 for (int i = 0; i < mCountX; i++) {
439 for (int j = 0; j < mCountY; j++) {
440 if (mOccupied[i][j]) {
441 cellToPoint(i, j, pt);
442 canvas.save();
443 canvas.translate(pt[0], pt[1]);
444 cd.draw(canvas);
445 canvas.restore();
446 }
447 }
448 }
449 }
450
Adam Cohen69ce2e52011-07-03 19:25:21 -0700451 // The folder outer / inner ring image(s)
452 for (int i = 0; i < mFolderOuterRings.size(); i++) {
453 FolderRingAnimator fra = mFolderOuterRings.get(i);
454
455 // Draw outer ring
456 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
457 int width = (int) fra.getOuterRingSize();
458 int height = width;
459 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
460
461 int centerX = mTempLocation[0] + mCellWidth / 2;
462 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
463
464 canvas.save();
465 canvas.translate(centerX - width / 2, centerY - height / 2);
466 d.setBounds(0, 0, width, height);
467 d.draw(canvas);
468 canvas.restore();
469
470 // Draw inner ring
471 d = FolderRingAnimator.sSharedInnerRingDrawable;
472 width = (int) fra.getInnerRingSize();
473 height = width;
474 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
475
476 centerX = mTempLocation[0] + mCellWidth / 2;
477 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
478 canvas.save();
479 canvas.translate(centerX - width / 2, centerY - width / 2);
480 d.setBounds(0, 0, width, height);
481 d.draw(canvas);
482 canvas.restore();
483 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700484
485 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
486 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
487 int width = d.getIntrinsicWidth();
488 int height = d.getIntrinsicHeight();
489
490 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
491 int centerX = mTempLocation[0] + mCellWidth / 2;
492 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
493
494 canvas.save();
495 canvas.translate(centerX - width / 2, centerY - width / 2);
496 d.setBounds(0, 0, width, height);
497 d.draw(canvas);
498 canvas.restore();
499 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700500 }
501
Adam Cohenb5ba0972011-09-07 18:02:31 -0700502 @Override
503 protected void dispatchDraw(Canvas canvas) {
504 super.dispatchDraw(canvas);
505 if (mForegroundAlpha > 0) {
506 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
507 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
508 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
509 mOverScrollForegroundDrawable.draw(canvas);
510 p.setXfermode(null);
511 }
512 }
513
Adam Cohen69ce2e52011-07-03 19:25:21 -0700514 public void showFolderAccept(FolderRingAnimator fra) {
515 mFolderOuterRings.add(fra);
516 }
517
518 public void hideFolderAccept(FolderRingAnimator fra) {
519 if (mFolderOuterRings.contains(fra)) {
520 mFolderOuterRings.remove(fra);
521 }
522 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700523 }
524
Adam Cohenc51934b2011-07-26 21:07:43 -0700525 public void setFolderLeaveBehindCell(int x, int y) {
526 mFolderLeaveBehindCell[0] = x;
527 mFolderLeaveBehindCell[1] = y;
528 invalidate();
529 }
530
531 public void clearFolderLeaveBehind() {
532 mFolderLeaveBehindCell[0] = -1;
533 mFolderLeaveBehindCell[1] = -1;
534 invalidate();
535 }
536
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700537 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700538 public boolean shouldDelayChildPressedState() {
539 return false;
540 }
541
542 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700543 public void cancelLongPress() {
544 super.cancelLongPress();
545
546 // Cancel long press for all children
547 final int count = getChildCount();
548 for (int i = 0; i < count; i++) {
549 final View child = getChildAt(i);
550 child.cancelLongPress();
551 }
552 }
553
Michael Jurkadee05892010-07-27 10:01:56 -0700554 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
555 mInterceptTouchListener = listener;
556 }
557
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700559 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
561
562 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700563 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 }
565
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800566 public void setIsHotseat(boolean isHotseat) {
567 mIsHotseat = isHotseat;
568 }
569
Winson Chungeecf02d2012-03-02 17:14:58 -0800570 public float getChildrenScale() {
571 return mIsHotseat ? mHotseatChildScale : mChildScale;
572 }
573
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700574 public boolean addViewToCellLayout(
575 View child, int index, int childId, LayoutParams params, boolean markCells) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800576 return addViewToCellLayout(child, index, childId, params, markCells, false);
577 }
578
Winson Chungeecf02d2012-03-02 17:14:58 -0800579 private void scaleChild(BubbleTextView bubbleChild, float pivot, 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,
599 boolean markCells, boolean allApps) {
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
Winson Chungeecf02d2012-03-02 17:14:58 -0800612 if (mIsHotseat && !allApps && mHotseatChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800613 // Scale/make transparent for a hotseat
Winson Chungeecf02d2012-03-02 17:14:58 -0800614 scaleChild(bubbleChild, 0f, 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
Winson Chungeecf02d2012-03-02 17:14:58 -0800619 scaleChild(bubbleChild, 0f, 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
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 @Override
918 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
919 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700920
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700922 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
923
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
925 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700926
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
928 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
929 }
930
Adam Cohend22015c2010-07-26 22:02:18 -0700931 int numWidthGaps = mCountX - 1;
932 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933
Adam Cohen234c4cd2011-07-17 21:03:04 -0700934 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700935 int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight();
936 int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700937 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
938 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
939 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
940 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700941 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700942 } else {
943 mWidthGap = mOriginalWidthGap;
944 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700945 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700946
Michael Jurka8c920dd2011-01-20 14:16:56 -0800947 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
948 int newWidth = widthSpecSize;
949 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700950 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700951 newWidth = getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700952 ((mCountX - 1) * mWidthGap);
Michael Jurka8b805b12012-04-18 14:23:14 -0700953 newHeight = getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700954 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700955 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700956 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800957
958 int count = getChildCount();
959 for (int i = 0; i < count; i++) {
960 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700961 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
962 getPaddingRight(), MeasureSpec.EXACTLY);
963 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
964 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800965 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
966 }
967 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 }
969
970 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700971 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800974 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -0700975 child.layout(getPaddingLeft(), getPaddingTop(),
976 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 }
978 }
979
980 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700981 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
982 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700983 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700984 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
985 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -0700986 }
987
988 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700990 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 }
992
993 @Override
994 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700995 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
997
Michael Jurka5f1c5092010-09-03 14:15:02 -0700998 public float getBackgroundAlpha() {
999 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001000 }
1001
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001002 public void setBackgroundAlphaMultiplier(float multiplier) {
1003 mBackgroundAlphaMultiplier = multiplier;
1004 }
1005
Adam Cohenddb82192010-11-10 16:32:54 -08001006 public float getBackgroundAlphaMultiplier() {
1007 return mBackgroundAlphaMultiplier;
1008 }
1009
Michael Jurka5f1c5092010-09-03 14:15:02 -07001010 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001011 if (mBackgroundAlpha != alpha) {
1012 mBackgroundAlpha = alpha;
1013 invalidate();
1014 }
Michael Jurkadee05892010-07-27 10:01:56 -07001015 }
1016
Michael Jurkaa52570f2012-03-20 03:18:20 -07001017 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001018 final int childCount = getChildCount();
1019 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001020 getChildAt(i).setAlpha(alpha);
1021 }
1022 }
1023
Michael Jurkaa52570f2012-03-20 03:18:20 -07001024 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1025 if (getChildCount() > 0) {
1026 return (ShortcutAndWidgetContainer) getChildAt(0);
1027 }
1028 return null;
1029 }
1030
Patrick Dubroy440c3602010-07-13 17:50:32 -07001031 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001032 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001033 }
1034
Adam Cohen76fc0852011-06-17 13:26:23 -07001035 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001036 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001037 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001038 boolean[][] occupied = mOccupied;
1039 if (!permanent) {
1040 occupied = mTmpOccupied;
1041 }
1042
Adam Cohen19f37922012-03-21 11:59:11 -07001043 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001044 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1045 final ItemInfo info = (ItemInfo) child.getTag();
1046
1047 // We cancel any existing animations
1048 if (mReorderAnimators.containsKey(lp)) {
1049 mReorderAnimators.get(lp).cancel();
1050 mReorderAnimators.remove(lp);
1051 }
1052
Adam Cohen482ed822012-03-02 14:15:13 -08001053 final int oldX = lp.x;
1054 final int oldY = lp.y;
1055 if (adjustOccupied) {
1056 occupied[lp.cellX][lp.cellY] = false;
1057 occupied[cellX][cellY] = true;
1058 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001059 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001060 if (permanent) {
1061 lp.cellX = info.cellX = cellX;
1062 lp.cellY = info.cellY = cellY;
1063 } else {
1064 lp.tmpCellX = cellX;
1065 lp.tmpCellY = cellY;
1066 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001067 clc.setupLp(lp);
1068 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001069 final int newX = lp.x;
1070 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001071
Adam Cohen76fc0852011-06-17 13:26:23 -07001072 lp.x = oldX;
1073 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001074
Adam Cohen482ed822012-03-02 14:15:13 -08001075 // Exit early if we're not actually moving the view
1076 if (oldX == newX && oldY == newY) {
1077 lp.isLockedToGrid = true;
1078 return true;
1079 }
1080
1081 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
1082 va.setDuration(duration);
1083 mReorderAnimators.put(lp, va);
1084
1085 va.addUpdateListener(new AnimatorUpdateListener() {
1086 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001087 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001088 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001089 lp.x = (int) ((1 - r) * oldX + r * newX);
1090 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001091 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001092 }
1093 });
Adam Cohen482ed822012-03-02 14:15:13 -08001094 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001095 boolean cancelled = false;
1096 public void onAnimationEnd(Animator animation) {
1097 // If the animation was cancelled, it means that another animation
1098 // has interrupted this one, and we don't want to lock the item into
1099 // place just yet.
1100 if (!cancelled) {
1101 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001102 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001103 }
1104 if (mReorderAnimators.containsKey(lp)) {
1105 mReorderAnimators.remove(lp);
1106 }
1107 }
1108 public void onAnimationCancel(Animator animation) {
1109 cancelled = true;
1110 }
1111 });
Adam Cohen482ed822012-03-02 14:15:13 -08001112 va.setStartDelay(delay);
1113 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001114 return true;
1115 }
1116 return false;
1117 }
1118
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001119 /**
1120 * Estimate where the top left cell of the dragged item will land if it is dropped.
1121 *
1122 * @param originX The X value of the top left corner of the item
1123 * @param originY The Y value of the top left corner of the item
1124 * @param spanX The number of horizontal cells that the item spans
1125 * @param spanY The number of vertical cells that the item spans
1126 * @param result The estimated drop cell X and Y.
1127 */
1128 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001129 final int countX = mCountX;
1130 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001131
Michael Jurkaa63c4522010-08-19 13:52:27 -07001132 // pointToCellRounded takes the top left of a cell but will pad that with
1133 // cellWidth/2 and cellHeight/2 when finding the matching cell
1134 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001135
1136 // If the item isn't fully on this screen, snap to the edges
1137 int rightOverhang = result[0] + spanX - countX;
1138 if (rightOverhang > 0) {
1139 result[0] -= rightOverhang; // Snap to right
1140 }
1141 result[0] = Math.max(0, result[0]); // Snap to left
1142 int bottomOverhang = result[1] + spanY - countY;
1143 if (bottomOverhang > 0) {
1144 result[1] -= bottomOverhang; // Snap to bottom
1145 }
1146 result[1] = Math.max(0, result[1]); // Snap to top
1147 }
1148
Adam Cohen482ed822012-03-02 14:15:13 -08001149 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1150 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001151 final int oldDragCellX = mDragCell[0];
1152 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001153
Winson Chungb8c69f32011-10-19 21:36:08 -07001154 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001155 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1156 } else {
1157 mDragCenter.set(originX, originY);
1158 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001159
Adam Cohen2801caf2011-05-13 20:57:39 -07001160 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001161 return;
1162 }
1163
Adam Cohen482ed822012-03-02 14:15:13 -08001164 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1165 mDragCell[0] = cellX;
1166 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001167 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001168 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001169 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001170
Joe Onorato4be866d2010-10-10 11:26:02 -07001171 int left = topLeft[0];
1172 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001173
Winson Chungb8c69f32011-10-19 21:36:08 -07001174 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001175 // When drawing the drag outline, it did not account for margin offsets
1176 // added by the view's parent.
1177 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1178 left += lp.leftMargin;
1179 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001180
Adam Cohen99e8b402011-03-25 19:23:43 -07001181 // Offsets due to the size difference between the View and the dragOutline.
1182 // There is a size difference to account for the outer blur, which may lie
1183 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001184 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001185 // We center about the x axis
1186 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1187 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001188 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001189 if (dragOffset != null && dragRegion != null) {
1190 // Center the drag region *horizontally* in the cell and apply a drag
1191 // outline offset
1192 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1193 - dragRegion.width()) / 2;
1194 top += dragOffset.y;
1195 } else {
1196 // Center the drag outline in the cell
1197 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1198 - dragOutline.getWidth()) / 2;
1199 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1200 - dragOutline.getHeight()) / 2;
1201 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001202 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001203 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001204 mDragOutlineAnims[oldIndex].animateOut();
1205 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001206 Rect r = mDragOutlines[mDragOutlineCurrent];
1207 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1208 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001209 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001210 }
Winson Chung150fbab2010-09-29 17:14:26 -07001211
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001212 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1213 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001214 }
1215 }
1216
Adam Cohene0310962011-04-18 16:15:31 -07001217 public void clearDragOutlines() {
1218 final int oldIndex = mDragOutlineCurrent;
1219 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001220 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001221 }
1222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001224 * Find a vacant area that will fit the given bounds nearest the requested
1225 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001226 *
Romain Guy51afc022009-05-04 18:03:43 -07001227 * @param pixelX The X location at which you want to search for a vacant area.
1228 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001229 * @param spanX Horizontal span of the object.
1230 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001231 * @param result Array in which to place the result, or null (in which case a new array will
1232 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001233 * @return The X, Y cell of a vacant area that can contain this object,
1234 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001236 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1237 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001238 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001239 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001240
Michael Jurka6a1435d2010-09-27 17:35:12 -07001241 /**
1242 * Find a vacant area that will fit the given bounds nearest the requested
1243 * cell location. Uses Euclidean distance to score multiple vacant areas.
1244 *
1245 * @param pixelX The X location at which you want to search for a vacant area.
1246 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001247 * @param minSpanX The minimum horizontal span required
1248 * @param minSpanY The minimum vertical span required
1249 * @param spanX Horizontal span of the object.
1250 * @param spanY Vertical span of the object.
1251 * @param result Array in which to place the result, or null (in which case a new array will
1252 * be allocated)
1253 * @return The X, Y cell of a vacant area that can contain this object,
1254 * nearest the requested location.
1255 */
1256 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1257 int spanY, int[] result, int[] resultSpan) {
1258 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1259 result, resultSpan);
1260 }
1261
1262 /**
1263 * Find a vacant area that will fit the given bounds nearest the requested
1264 * cell location. Uses Euclidean distance to score multiple vacant areas.
1265 *
1266 * @param pixelX The X location at which you want to search for a vacant area.
1267 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001268 * @param spanX Horizontal span of the object.
1269 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001270 * @param ignoreOccupied If true, the result can be an occupied cell
1271 * @param result Array in which to place the result, or null (in which case a new array will
1272 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001273 * @return The X, Y cell of a vacant area that can contain this object,
1274 * nearest the requested location.
1275 */
Adam Cohendf035382011-04-11 17:22:04 -07001276 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1277 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001278 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001279 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001280 }
1281
1282 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1283 private void lazyInitTempRectStack() {
1284 if (mTempRectStack.isEmpty()) {
1285 for (int i = 0; i < mCountX * mCountY; i++) {
1286 mTempRectStack.push(new Rect());
1287 }
1288 }
1289 }
Adam Cohen482ed822012-03-02 14:15:13 -08001290
Adam Cohend41fbf52012-02-16 23:53:59 -08001291 private void recycleTempRects(Stack<Rect> used) {
1292 while (!used.isEmpty()) {
1293 mTempRectStack.push(used.pop());
1294 }
1295 }
1296
1297 /**
1298 * Find a vacant area that will fit the given bounds nearest the requested
1299 * cell location. Uses Euclidean distance to score multiple vacant areas.
1300 *
1301 * @param pixelX The X location at which you want to search for a vacant area.
1302 * @param pixelY The Y location at which you want to search for a vacant area.
1303 * @param minSpanX The minimum horizontal span required
1304 * @param minSpanY The minimum vertical span required
1305 * @param spanX Horizontal span of the object.
1306 * @param spanY Vertical span of the object.
1307 * @param ignoreOccupied If true, the result can be an occupied cell
1308 * @param result Array in which to place the result, or null (in which case a new array will
1309 * be allocated)
1310 * @return The X, Y cell of a vacant area that can contain this object,
1311 * nearest the requested location.
1312 */
1313 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001314 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1315 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001316 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001317 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001318 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001319
Adam Cohene3e27a82011-04-15 12:07:39 -07001320 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1321 // to the center of the item, but we are searching based on the top-left cell, so
1322 // we translate the point over to correspond to the top-left.
1323 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1324 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1325
Jeff Sharkey70864282009-04-07 21:08:40 -07001326 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001327 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001328 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001329 final Rect bestRect = new Rect(-1, -1, -1, -1);
1330 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001331
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001332 final int countX = mCountX;
1333 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001334
Adam Cohend41fbf52012-02-16 23:53:59 -08001335 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1336 spanX < minSpanX || spanY < minSpanY) {
1337 return bestXY;
1338 }
1339
1340 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001341 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001342 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1343 int ySize = -1;
1344 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001345 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001346 // First, let's see if this thing fits anywhere
1347 for (int i = 0; i < minSpanX; i++) {
1348 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001349 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001350 continue inner;
1351 }
Michael Jurkac28de512010-08-13 11:27:44 -07001352 }
1353 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001354 xSize = minSpanX;
1355 ySize = minSpanY;
1356
1357 // We know that the item will fit at _some_ acceptable size, now let's see
1358 // how big we can make it. We'll alternate between incrementing x and y spans
1359 // until we hit a limit.
1360 boolean incX = true;
1361 boolean hitMaxX = xSize >= spanX;
1362 boolean hitMaxY = ySize >= spanY;
1363 while (!(hitMaxX && hitMaxY)) {
1364 if (incX && !hitMaxX) {
1365 for (int j = 0; j < ySize; j++) {
1366 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1367 // We can't move out horizontally
1368 hitMaxX = true;
1369 }
1370 }
1371 if (!hitMaxX) {
1372 xSize++;
1373 }
1374 } else if (!hitMaxY) {
1375 for (int i = 0; i < xSize; i++) {
1376 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1377 // We can't move out vertically
1378 hitMaxY = true;
1379 }
1380 }
1381 if (!hitMaxY) {
1382 ySize++;
1383 }
1384 }
1385 hitMaxX |= xSize >= spanX;
1386 hitMaxY |= ySize >= spanY;
1387 incX = !incX;
1388 }
1389 incX = true;
1390 hitMaxX = xSize >= spanX;
1391 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001392 }
Winson Chung0be025d2011-05-23 17:45:09 -07001393 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001394 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395
Adam Cohend41fbf52012-02-16 23:53:59 -08001396 // We verify that the current rect is not a sub-rect of any of our previous
1397 // candidates. In this case, the current rect is disqualified in favour of the
1398 // containing rect.
1399 Rect currentRect = mTempRectStack.pop();
1400 currentRect.set(x, y, x + xSize, y + ySize);
1401 boolean contained = false;
1402 for (Rect r : validRegions) {
1403 if (r.contains(currentRect)) {
1404 contained = true;
1405 break;
1406 }
1407 }
1408 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001409 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1410 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001411
Adam Cohend41fbf52012-02-16 23:53:59 -08001412 if ((distance <= bestDistance && !contained) ||
1413 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001414 bestDistance = distance;
1415 bestXY[0] = x;
1416 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001417 if (resultSpan != null) {
1418 resultSpan[0] = xSize;
1419 resultSpan[1] = ySize;
1420 }
1421 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
1424 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001425 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001426 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427
Adam Cohenc0dcf592011-06-01 15:30:43 -07001428 // Return -1, -1 if no suitable location found
1429 if (bestDistance == Double.MAX_VALUE) {
1430 bestXY[0] = -1;
1431 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001432 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001433 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001434 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001436
Adam Cohen482ed822012-03-02 14:15:13 -08001437 /**
1438 * Find a vacant area that will fit the given bounds nearest the requested
1439 * cell location, and will also weigh in a suggested direction vector of the
1440 * desired location. This method computers distance based on unit grid distances,
1441 * not pixel distances.
1442 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001443 * @param cellX The X cell nearest to which you want to search for a vacant area.
1444 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001445 * @param spanX Horizontal span of the object.
1446 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001447 * @param direction The favored direction in which the views should move from x, y
1448 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1449 * matches exactly. Otherwise we find the best matching direction.
1450 * @param occoupied The array which represents which cells in the CellLayout are occupied
1451 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1452 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001453 * @param result Array in which to place the result, or null (in which case a new array will
1454 * be allocated)
1455 * @return The X, Y cell of a vacant area that can contain this object,
1456 * nearest the requested location.
1457 */
1458 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001459 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001460 // Keep track of best-scoring drop area
1461 final int[] bestXY = result != null ? result : new int[2];
1462 float bestDistance = Float.MAX_VALUE;
1463 int bestDirectionScore = Integer.MIN_VALUE;
1464
1465 final int countX = mCountX;
1466 final int countY = mCountY;
1467
1468 for (int y = 0; y < countY - (spanY - 1); y++) {
1469 inner:
1470 for (int x = 0; x < countX - (spanX - 1); x++) {
1471 // First, let's see if this thing fits anywhere
1472 for (int i = 0; i < spanX; i++) {
1473 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001474 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001475 continue inner;
1476 }
1477 }
1478 }
1479
1480 float distance = (float)
1481 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1482 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001483 computeDirectionVector(x - cellX, y - cellY, curDirection);
1484 // The direction score is just the dot product of the two candidate direction
1485 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001486 int curDirectionScore = direction[0] * curDirection[0] +
1487 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001488 boolean exactDirectionOnly = false;
1489 boolean directionMatches = direction[0] == curDirection[0] &&
1490 direction[0] == curDirection[0];
1491 if ((directionMatches || !exactDirectionOnly) &&
1492 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001493 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1494 bestDistance = distance;
1495 bestDirectionScore = curDirectionScore;
1496 bestXY[0] = x;
1497 bestXY[1] = y;
1498 }
1499 }
1500 }
1501
1502 // Return -1, -1 if no suitable location found
1503 if (bestDistance == Float.MAX_VALUE) {
1504 bestXY[0] = -1;
1505 bestXY[1] = -1;
1506 }
1507 return bestXY;
1508 }
1509
Adam Cohen47a876d2012-03-19 13:21:41 -07001510 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1511 int[] direction,boolean[][] occupied,
1512 boolean blockOccupied[][], int[] result) {
1513 // Keep track of best-scoring drop area
1514 final int[] bestXY = result != null ? result : new int[2];
1515 bestXY[0] = -1;
1516 bestXY[1] = -1;
1517 float bestDistance = Float.MAX_VALUE;
1518
1519 // We use this to march in a single direction
Adam Cohen5b53f292012-03-29 14:30:35 -07001520 if ((direction[0] != 0 && direction[1] != 0) ||
1521 (direction[0] == 0 && direction[1] == 0)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001522 return bestXY;
1523 }
1524
1525 // This will only incrememnet one of x or y based on the assertion above
1526 int x = cellX + direction[0];
1527 int y = cellY + direction[1];
1528 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1529
1530 boolean fail = false;
1531 for (int i = 0; i < spanX; i++) {
1532 for (int j = 0; j < spanY; j++) {
1533 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1534 fail = true;
1535 }
1536 }
1537 }
1538 if (!fail) {
1539 float distance = (float)
1540 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1541 if (Float.compare(distance, bestDistance) < 0) {
1542 bestDistance = distance;
1543 bestXY[0] = x;
1544 bestXY[1] = y;
1545 }
1546 }
1547 x += direction[0];
1548 y += direction[1];
1549 }
1550 return bestXY;
1551 }
1552
Adam Cohen482ed822012-03-02 14:15:13 -08001553 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001554 int[] direction, ItemConfiguration currentState) {
1555 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001556 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001557 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001558 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1559
Adam Cohen8baab352012-03-20 17:39:21 -07001560 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001561
1562 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001563 c.x = mTempLocation[0];
1564 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001565 success = true;
1566
1567 }
Adam Cohen8baab352012-03-20 17:39:21 -07001568 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001569 return success;
1570 }
1571
Adam Cohen47a876d2012-03-19 13:21:41 -07001572 // This method looks in the specified direction to see if there is an additional view
1573 // immediately adjecent in that direction
1574 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen19f37922012-03-21 11:59:11 -07001575 boolean[][] occupied, View dragView, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001576 boolean found = false;
1577
Michael Jurkaa52570f2012-03-20 03:18:20 -07001578 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001579 Rect r0 = new Rect(boundingRect);
1580 Rect r1 = new Rect();
1581
1582 int deltaX = 0;
1583 int deltaY = 0;
1584 if (direction[1] < 0) {
1585 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1586 deltaY = -1;
1587 } else if (direction[1] > 0) {
1588 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1589 deltaY = 1;
1590 } else if (direction[0] < 0) {
1591 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1592 deltaX = -1;
1593 } else if (direction[0] > 0) {
1594 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1595 deltaX = 1;
1596 }
1597
1598 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001599 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen19f37922012-03-21 11:59:11 -07001600 if (views.contains(child) || child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001601 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001602
Adam Cohen8baab352012-03-20 17:39:21 -07001603 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1604 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001605 if (Rect.intersects(r0, r1)) {
1606 if (!lp.canReorder) {
1607 return false;
1608 }
1609 boolean pushed = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001610 for (int x = c.x; x < c.x + c.spanX; x++) {
1611 for (int y = c.y; y < c.y + c.spanY; y++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001612 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1613 && y - deltaY >= 0 && y - deltaY < mCountY;
1614 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1615 pushed = true;
1616 }
1617 }
1618 }
1619 if (pushed) {
1620 views.add(child);
Adam Cohen8baab352012-03-20 17:39:21 -07001621 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001622 found = true;
1623 }
1624 }
1625 }
1626 return found;
1627 }
1628
Adam Cohen482ed822012-03-02 14:15:13 -08001629 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen19f37922012-03-21 11:59:11 -07001630 int[] direction, boolean push, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001631 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001632
Adam Cohen8baab352012-03-20 17:39:21 -07001633 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001634 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001635 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001636 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001637 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001638 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001639 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001640 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001641 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001642 }
1643 }
Adam Cohen8baab352012-03-20 17:39:21 -07001644
1645 @SuppressWarnings("unchecked")
1646 ArrayList<View> dup = (ArrayList<View>) views.clone();
1647 // We try and expand the group of views in the direction vector passed, based on
1648 // whether they are physically adjacent, ie. based on "push mechanics".
Adam Cohen19f37922012-03-21 11:59:11 -07001649 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied, dragView,
Adam Cohen8baab352012-03-20 17:39:21 -07001650 currentState)) {
1651 }
1652
1653 // Mark the occupied state as false for the group of views we want to move.
1654 for (View v: dup) {
1655 CellAndSpan c = currentState.map.get(v);
1656 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1657 }
1658
Adam Cohen47a876d2012-03-19 13:21:41 -07001659 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1660 int top = boundingRect.top;
1661 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001662 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
1663 // for tetris-style interlocking.
1664 for (View v: dup) {
1665 CellAndSpan c = currentState.map.get(v);
1666 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001667 }
1668
Adam Cohen482ed822012-03-02 14:15:13 -08001669 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1670
Adam Cohen8baab352012-03-20 17:39:21 -07001671 if (push) {
1672 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1673 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1674 } else {
1675 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1676 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1677 }
Adam Cohen482ed822012-03-02 14:15:13 -08001678
Adam Cohen8baab352012-03-20 17:39:21 -07001679 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001680 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001681 int deltaX = mTempLocation[0] - boundingRect.left;
1682 int deltaY = mTempLocation[1] - boundingRect.top;
1683 for (View v: dup) {
1684 CellAndSpan c = currentState.map.get(v);
1685 c.x += deltaX;
1686 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001687 }
1688 success = true;
1689 }
Adam Cohen8baab352012-03-20 17:39:21 -07001690
1691 // In either case, we set the occupied array as marked for the location of the views
1692 for (View v: dup) {
1693 CellAndSpan c = currentState.map.get(v);
1694 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001695 }
1696 return success;
1697 }
1698
1699 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1700 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1701 }
1702
1703 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001704 View ignoreView, ItemConfiguration solution) {
Adam Cohen482ed822012-03-02 14:15:13 -08001705
Adam Cohen8baab352012-03-20 17:39:21 -07001706 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001707 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001708
Adam Cohen8baab352012-03-20 17:39:21 -07001709 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001710 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001711 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07001712 if (c != null) {
1713 c.x = cellX;
1714 c.y = cellY;
1715 }
Adam Cohen482ed822012-03-02 14:15:13 -08001716 }
Adam Cohen482ed822012-03-02 14:15:13 -08001717 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1718 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001719 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001720 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001721 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001722 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001723 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001724 if (Rect.intersects(r0, r1)) {
1725 if (!lp.canReorder) {
1726 return false;
1727 }
1728 mIntersectingViews.add(child);
1729 }
1730 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001731
Adam Cohen8baab352012-03-20 17:39:21 -07001732 // We try to move the intersecting views as a block using the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001733 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1734 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001735 return true;
1736 }
1737 // Try the opposite direction
1738 direction[0] *= -1;
1739 direction[1] *= -1;
Adam Cohen19f37922012-03-21 11:59:11 -07001740 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, ignoreView,
1741 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001742 return true;
1743 }
1744 // Switch the direction back
1745 direction[0] *= -1;
1746 direction[1] *= -1;
1747
Adam Cohen8baab352012-03-20 17:39:21 -07001748 // Next we try moving the views as a block , but without requiring the push mechanic
Adam Cohen19f37922012-03-21 11:59:11 -07001749 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, ignoreView,
1750 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001751 return true;
1752 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001753
Adam Cohen482ed822012-03-02 14:15:13 -08001754 // Ok, they couldn't move as a block, let's move them individually
1755 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001756 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001757 return false;
1758 }
1759 }
1760 return true;
1761 }
1762
1763 /*
1764 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1765 * the provided point and the provided cell
1766 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001767 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001768 double angle = Math.atan(((float) deltaY) / deltaX);
1769
1770 result[0] = 0;
1771 result[1] = 0;
1772 if (Math.abs(Math.cos(angle)) > 0.5f) {
1773 result[0] = (int) Math.signum(deltaX);
1774 }
1775 if (Math.abs(Math.sin(angle)) > 0.5f) {
1776 result[1] = (int) Math.signum(deltaY);
1777 }
1778 }
1779
Adam Cohen8baab352012-03-20 17:39:21 -07001780 private void copyOccupiedArray(boolean[][] occupied) {
1781 for (int i = 0; i < mCountX; i++) {
1782 for (int j = 0; j < mCountY; j++) {
1783 occupied[i][j] = mOccupied[i][j];
1784 }
1785 }
1786 }
1787
Adam Cohen482ed822012-03-02 14:15:13 -08001788 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1789 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001790 // Copy the current state into the solution. This solution will be manipulated as necessary.
1791 copyCurrentStateToSolution(solution, false);
1792 // Copy the current occupied array into the temporary occupied array. This array will be
1793 // manipulated as necessary to find a solution.
1794 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001795
1796 // We find the nearest cell into which we would place the dragged item, assuming there's
1797 // nothing in its way.
1798 int result[] = new int[2];
1799 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1800
1801 boolean success = false;
1802 // First we try the exact nearest position of the item being dragged,
1803 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001804 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1805 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001806
1807 if (!success) {
1808 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1809 // x, then 1 in y etc.
1810 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1811 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1812 dragView, false, solution);
1813 } else if (spanY > minSpanY) {
1814 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1815 dragView, true, solution);
1816 }
1817 solution.isSolution = false;
1818 } else {
1819 solution.isSolution = true;
1820 solution.dragViewX = result[0];
1821 solution.dragViewY = result[1];
1822 solution.dragViewSpanX = spanX;
1823 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001824 }
1825 return solution;
1826 }
1827
1828 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001829 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001830 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001831 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001832 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001833 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001834 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001835 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001836 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001837 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001838 }
Adam Cohen8baab352012-03-20 17:39:21 -07001839 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001840 }
1841 }
1842
1843 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1844 for (int i = 0; i < mCountX; i++) {
1845 for (int j = 0; j < mCountY; j++) {
1846 mTmpOccupied[i][j] = false;
1847 }
1848 }
1849
Michael Jurkaa52570f2012-03-20 03:18:20 -07001850 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001851 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001852 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001853 if (child == dragView) continue;
1854 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001855 CellAndSpan c = solution.map.get(child);
1856 if (c != null) {
1857 lp.tmpCellX = c.x;
1858 lp.tmpCellY = c.y;
1859 lp.cellHSpan = c.spanX;
1860 lp.cellVSpan = c.spanY;
1861 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001862 }
1863 }
1864 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1865 solution.dragViewSpanY, mTmpOccupied, true);
1866 }
1867
1868 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1869 commitDragView) {
1870
1871 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1872 for (int i = 0; i < mCountX; i++) {
1873 for (int j = 0; j < mCountY; j++) {
1874 occupied[i][j] = false;
1875 }
1876 }
1877
Michael Jurkaa52570f2012-03-20 03:18:20 -07001878 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001879 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001880 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001881 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001882 CellAndSpan c = solution.map.get(child);
1883 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07001884 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
1885 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07001886 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001887 }
1888 }
1889 if (commitDragView) {
1890 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1891 solution.dragViewSpanY, occupied, true);
1892 }
1893 }
1894
Adam Cohen19f37922012-03-21 11:59:11 -07001895 // This method starts or changes the reorder hint animations
1896 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
1897 int childCount = mShortcutsAndWidgets.getChildCount();
1898 int timeForPriorAnimationToComplete = getMaxCompletionTime();
1899 for (int i = 0; i < childCount; i++) {
1900 View child = mShortcutsAndWidgets.getChildAt(i);
1901 if (child == dragView) continue;
1902 CellAndSpan c = solution.map.get(child);
1903 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1904 if (c != null) {
1905 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
1906 c.x, c.y, c.spanX, c.spanY);
1907 rha.animate(timeForPriorAnimationToComplete);
1908 }
1909 }
1910 }
1911
1912 // Class which represents the reorder hint animations. These animations show that an item is
1913 // in a temporary state, and hint at where the item will return to.
1914 class ReorderHintAnimation {
1915 View child;
1916 float deltaX;
1917 float deltaY;
1918 private static final int DURATION = 140;
1919 private int repeatCount;
1920 private boolean cancelOnCycleComplete = false;
1921 ValueAnimator va;
1922
1923 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
1924 int spanX, int spanY) {
1925 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
1926 final int x0 = mTmpPoint[0];
1927 final int y0 = mTmpPoint[1];
1928 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
1929 final int x1 = mTmpPoint[0];
1930 final int y1 = mTmpPoint[1];
1931 final int dX = x1 - x0;
1932 final int dY = y1 - y0;
1933 deltaX = 0;
1934 deltaY = 0;
1935 if (dX == dY && dX == 0) {
1936 } else {
1937 if (dY == 0) {
1938 deltaX = mReorderHintAnimationMagnitude;
1939 } else if (dX == 0) {
1940 deltaY = mReorderHintAnimationMagnitude;
1941 } else {
1942 double angle = Math.atan( (float) (dY) / dX);
1943 deltaX = (int) (Math.cos(angle) * mReorderHintAnimationMagnitude);
1944 deltaY = (int) (Math.sin(angle) * mReorderHintAnimationMagnitude);
1945 }
1946 }
1947 this.child = child;
1948 }
1949
1950 void animate(int delay) {
1951 if (mShakeAnimators.containsKey(child)) {
1952 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
1953 oldAnimation.completeAnimation();
1954 mShakeAnimators.remove(child);
1955 }
1956 if (deltaX == 0 && deltaY == 0) {
1957 return;
1958 }
1959 va = ValueAnimator.ofFloat(0f, 1f);
1960 va.setRepeatMode(ValueAnimator.REVERSE);
1961 va.setRepeatCount(ValueAnimator.INFINITE);
1962 va.setDuration(DURATION);
1963 va.addUpdateListener(new AnimatorUpdateListener() {
1964 @Override
1965 public void onAnimationUpdate(ValueAnimator animation) {
1966 float r = ((Float) animation.getAnimatedValue()).floatValue();
1967 float x = r * deltaX;
1968 float y = r * deltaY;
1969 child.setTranslationX(x);
1970 child.setTranslationY(y);
1971 }
1972 });
1973 va.addListener(new AnimatorListenerAdapter() {
1974 public void onAnimationRepeat(Animator animation) {
1975 repeatCount++;
1976 // We make sure to end only after a full period
1977 if (cancelOnCycleComplete && repeatCount % 2 == 0) {
1978 va.cancel();
1979 }
1980 }
1981 });
1982 va.setStartDelay(Math.max(REORDER_ANIMATION_DURATION, delay));
1983 mShakeAnimators.put(child, this);
1984 va.start();
1985 }
1986
1987
1988 private void completeAnimation() {
1989 cancelOnCycleComplete = true;
1990 }
1991
1992 // Returns the time required to complete the current oscillating animation
1993 private int completionTime() {
1994 if (repeatCount % 2 == 0) {
1995 return (int) (va.getDuration() - va.getCurrentPlayTime() + DURATION);
1996 } else {
1997 return (int) (va.getDuration() - va.getCurrentPlayTime());
1998 }
1999 }
2000 }
2001
2002 private void completeAndClearReorderHintAnimations() {
2003 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2004 a.completeAnimation();
2005 }
2006 mShakeAnimators.clear();
2007 }
2008
2009 private int getMaxCompletionTime() {
2010 int maxTime = 0;
2011 for (ReorderHintAnimation a: mShakeAnimators.values()) {
2012 maxTime = Math.max(maxTime, a.completionTime());
2013 }
2014 return maxTime;
2015 }
2016
Adam Cohen482ed822012-03-02 14:15:13 -08002017 private void commitTempPlacement() {
2018 for (int i = 0; i < mCountX; i++) {
2019 for (int j = 0; j < mCountY; j++) {
2020 mOccupied[i][j] = mTmpOccupied[i][j];
2021 }
2022 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002023 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002024 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002025 View child = mShortcutsAndWidgets.getChildAt(i);
2026 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2027 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002028 // We do a null check here because the item info can be null in the case of the
2029 // AllApps button in the hotseat.
2030 if (info != null) {
2031 info.cellX = lp.cellX = lp.tmpCellX;
2032 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002033 info.spanX = lp.cellHSpan;
2034 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002035 }
Adam Cohen482ed822012-03-02 14:15:13 -08002036 }
Adam Cohen2acce882012-03-28 19:03:19 -07002037 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002038 }
2039
2040 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002041 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002042 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002043 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002044 lp.useTmpCoords = useTempCoords;
2045 }
2046 }
2047
Adam Cohen482ed822012-03-02 14:15:13 -08002048 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2049 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2050 int[] result = new int[2];
2051 int[] resultSpan = new int[2];
2052 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2053 resultSpan);
2054 if (result[0] >= 0 && result[1] >= 0) {
2055 copyCurrentStateToSolution(solution, false);
2056 solution.dragViewX = result[0];
2057 solution.dragViewY = result[1];
2058 solution.dragViewSpanX = resultSpan[0];
2059 solution.dragViewSpanY = resultSpan[1];
2060 solution.isSolution = true;
2061 } else {
2062 solution.isSolution = false;
2063 }
2064 return solution;
2065 }
2066
2067 public void prepareChildForDrag(View child) {
2068 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002069 }
2070
Adam Cohen19f37922012-03-21 11:59:11 -07002071 /* This seems like it should be obvious and straight-forward, but when the direction vector
2072 needs to match with the notion of the dragView pushing other views, we have to employ
2073 a slightly more subtle notion of the direction vector. The question is what two points is
2074 the vector between? The center of the dragView and its desired destination? Not quite, as
2075 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2076 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2077 or right, which helps make pushing feel right.
2078 */
2079 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2080 int spanY, View dragView, int[] resultDirection) {
2081 int[] targetDestination = new int[2];
2082
2083 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2084 Rect dragRect = new Rect();
2085 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2086 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2087
2088 Rect dropRegionRect = new Rect();
2089 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2090 dragView, dropRegionRect, mIntersectingViews);
2091
2092 int dropRegionSpanX = dropRegionRect.width();
2093 int dropRegionSpanY = dropRegionRect.height();
2094
2095 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2096 dropRegionRect.height(), dropRegionRect);
2097
2098 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2099 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2100
2101 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2102 deltaX = 0;
2103 }
2104 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2105 deltaY = 0;
2106 }
2107
2108 if (deltaX == 0 && deltaY == 0) {
2109 // No idea what to do, give a random direction.
2110 resultDirection[0] = 1;
2111 resultDirection[1] = 0;
2112 } else {
2113 computeDirectionVector(deltaX, deltaY, resultDirection);
2114 }
2115 }
2116
2117 // For a given cell and span, fetch the set of views intersecting the region.
2118 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2119 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2120 if (boundingRect != null) {
2121 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2122 }
2123 intersectingViews.clear();
2124 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2125 Rect r1 = new Rect();
2126 final int count = mShortcutsAndWidgets.getChildCount();
2127 for (int i = 0; i < count; i++) {
2128 View child = mShortcutsAndWidgets.getChildAt(i);
2129 if (child == dragView) continue;
2130 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2131 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2132 if (Rect.intersects(r0, r1)) {
2133 mIntersectingViews.add(child);
2134 if (boundingRect != null) {
2135 boundingRect.union(r1);
2136 }
2137 }
2138 }
2139 }
2140
2141 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2142 View dragView, int[] result) {
2143 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2144 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2145 mIntersectingViews);
2146 return !mIntersectingViews.isEmpty();
2147 }
2148
2149 void revertTempState() {
2150 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2151 final int count = mShortcutsAndWidgets.getChildCount();
2152 for (int i = 0; i < count; i++) {
2153 View child = mShortcutsAndWidgets.getChildAt(i);
2154 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2155 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2156 lp.tmpCellX = lp.cellX;
2157 lp.tmpCellY = lp.cellY;
2158 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2159 0, false, false);
2160 }
2161 }
2162 completeAndClearReorderHintAnimations();
2163 setItemPlacementDirty(false);
2164 }
2165
Adam Cohenbebf0422012-04-11 18:06:28 -07002166 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2167 View dragView, int[] direction, boolean commit) {
2168 int[] pixelXY = new int[2];
2169 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2170
2171 // First we determine if things have moved enough to cause a different layout
2172 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2173 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2174
2175 setUseTempCoords(true);
2176 if (swapSolution != null && swapSolution.isSolution) {
2177 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2178 // committing anything or animating anything as we just want to determine if a solution
2179 // exists
2180 copySolutionToTempState(swapSolution, dragView);
2181 setItemPlacementDirty(true);
2182 animateItemsToSolution(swapSolution, dragView, commit);
2183
2184 if (commit) {
2185 commitTempPlacement();
2186 completeAndClearReorderHintAnimations();
2187 setItemPlacementDirty(false);
2188 } else {
2189 beginOrAdjustHintAnimations(swapSolution, dragView,
2190 REORDER_ANIMATION_DURATION);
2191 }
2192 mShortcutsAndWidgets.requestLayout();
2193 }
2194 return swapSolution.isSolution;
2195 }
2196
Adam Cohen482ed822012-03-02 14:15:13 -08002197 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2198 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002199 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002200 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002201
2202 if (resultSpan == null) {
2203 resultSpan = new int[2];
2204 }
2205
Adam Cohen19f37922012-03-21 11:59:11 -07002206 // When we are checking drop validity or actually dropping, we don't recompute the
2207 // direction vector, since we want the solution to match the preview, and it's possible
2208 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002209 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2210 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002211 mDirectionVector[0] = mPreviousReorderDirection[0];
2212 mDirectionVector[1] = mPreviousReorderDirection[1];
2213 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002214 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2215 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2216 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002217 }
2218 } else {
2219 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2220 mPreviousReorderDirection[0] = mDirectionVector[0];
2221 mPreviousReorderDirection[1] = mDirectionVector[1];
2222 }
2223
Adam Cohen482ed822012-03-02 14:15:13 -08002224 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2225 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2226
2227 // We attempt the approach which doesn't shuffle views at all
2228 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2229 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2230
2231 ItemConfiguration finalSolution = null;
2232 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2233 finalSolution = swapSolution;
2234 } else if (noShuffleSolution.isSolution) {
2235 finalSolution = noShuffleSolution;
2236 }
2237
2238 boolean foundSolution = true;
2239 if (!DESTRUCTIVE_REORDER) {
2240 setUseTempCoords(true);
2241 }
2242
2243 if (finalSolution != null) {
2244 result[0] = finalSolution.dragViewX;
2245 result[1] = finalSolution.dragViewY;
2246 resultSpan[0] = finalSolution.dragViewSpanX;
2247 resultSpan[1] = finalSolution.dragViewSpanY;
2248
2249 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2250 // committing anything or animating anything as we just want to determine if a solution
2251 // exists
2252 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2253 if (!DESTRUCTIVE_REORDER) {
2254 copySolutionToTempState(finalSolution, dragView);
2255 }
2256 setItemPlacementDirty(true);
2257 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2258
Adam Cohen19f37922012-03-21 11:59:11 -07002259 if (!DESTRUCTIVE_REORDER &&
2260 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002261 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002262 completeAndClearReorderHintAnimations();
2263 setItemPlacementDirty(false);
2264 } else {
2265 beginOrAdjustHintAnimations(finalSolution, dragView,
2266 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002267 }
2268 }
2269 } else {
2270 foundSolution = false;
2271 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2272 }
2273
2274 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2275 setUseTempCoords(false);
2276 }
Adam Cohen482ed822012-03-02 14:15:13 -08002277
Michael Jurkaa52570f2012-03-20 03:18:20 -07002278 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002279 return result;
2280 }
2281
Adam Cohen19f37922012-03-21 11:59:11 -07002282 void setItemPlacementDirty(boolean dirty) {
2283 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002284 }
Adam Cohen19f37922012-03-21 11:59:11 -07002285 boolean isItemPlacementDirty() {
2286 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002287 }
2288
2289 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002290 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002291 boolean isSolution = false;
2292 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2293
2294 int area() {
2295 return dragViewSpanX * dragViewSpanY;
2296 }
Adam Cohen8baab352012-03-20 17:39:21 -07002297 }
2298
2299 private class CellAndSpan {
2300 int x, y;
2301 int spanX, spanY;
2302
2303 public CellAndSpan(int x, int y, int spanX, int spanY) {
2304 this.x = x;
2305 this.y = y;
2306 this.spanX = spanX;
2307 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002308 }
2309 }
2310
Adam Cohendf035382011-04-11 17:22:04 -07002311 /**
2312 * Find a vacant area that will fit the given bounds nearest the requested
2313 * cell location. Uses Euclidean distance to score multiple vacant areas.
2314 *
2315 * @param pixelX The X location at which you want to search for a vacant area.
2316 * @param pixelY The Y location at which you want to search for a vacant area.
2317 * @param spanX Horizontal span of the object.
2318 * @param spanY Vertical span of the object.
2319 * @param ignoreView Considers space occupied by this view as unoccupied
2320 * @param result Previously returned value to possibly recycle.
2321 * @return The X, Y cell of a vacant area that can contain this object,
2322 * nearest the requested location.
2323 */
2324 int[] findNearestVacantArea(
2325 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2326 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2327 }
2328
2329 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002330 * Find a vacant area that will fit the given bounds nearest the requested
2331 * cell location. Uses Euclidean distance to score multiple vacant areas.
2332 *
2333 * @param pixelX The X location at which you want to search for a vacant area.
2334 * @param pixelY The Y location at which you want to search for a vacant area.
2335 * @param minSpanX The minimum horizontal span required
2336 * @param minSpanY The minimum vertical span required
2337 * @param spanX Horizontal span of the object.
2338 * @param spanY Vertical span of the object.
2339 * @param ignoreView Considers space occupied by this view as unoccupied
2340 * @param result Previously returned value to possibly recycle.
2341 * @return The X, Y cell of a vacant area that can contain this object,
2342 * nearest the requested location.
2343 */
2344 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2345 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002346 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2347 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002348 }
2349
2350 /**
Adam Cohendf035382011-04-11 17:22:04 -07002351 * Find a starting cell position that will fit the given bounds nearest the requested
2352 * cell location. Uses Euclidean distance to score multiple vacant areas.
2353 *
2354 * @param pixelX The X location at which you want to search for a vacant area.
2355 * @param pixelY The Y location at which you want to search for a vacant area.
2356 * @param spanX Horizontal span of the object.
2357 * @param spanY Vertical span of the object.
2358 * @param ignoreView Considers space occupied by this view as unoccupied
2359 * @param result Previously returned value to possibly recycle.
2360 * @return The X, Y cell of a vacant area that can contain this object,
2361 * nearest the requested location.
2362 */
2363 int[] findNearestArea(
2364 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2365 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2366 }
2367
Michael Jurka0280c3b2010-09-17 15:00:07 -07002368 boolean existsEmptyCell() {
2369 return findCellForSpan(null, 1, 1);
2370 }
2371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002373 * Finds the upper-left coordinate of the first rectangle in the grid that can
2374 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2375 * then this method will only return coordinates for rectangles that contain the cell
2376 * (intersectX, intersectY)
2377 *
2378 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2379 * can be found.
2380 * @param spanX The horizontal span of the cell we want to find.
2381 * @param spanY The vertical span of the cell we want to find.
2382 *
2383 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002384 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002385 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002386 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002387 }
2388
2389 /**
2390 * Like above, but ignores any cells occupied by the item "ignoreView"
2391 *
2392 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2393 * can be found.
2394 * @param spanX The horizontal span of the cell we want to find.
2395 * @param spanY The vertical span of the cell we want to find.
2396 * @param ignoreView The home screen item we should treat as not occupying any space
2397 * @return
2398 */
2399 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002400 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2401 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002402 }
2403
2404 /**
2405 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2406 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2407 *
2408 * @param spanX The horizontal span of the cell we want to find.
2409 * @param spanY The vertical span of the cell we want to find.
2410 * @param ignoreView The home screen item we should treat as not occupying any space
2411 * @param intersectX The X coordinate of the cell that we should try to overlap
2412 * @param intersectX The Y coordinate of the cell that we should try to overlap
2413 *
2414 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2415 */
2416 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2417 int intersectX, int intersectY) {
2418 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002419 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002420 }
2421
2422 /**
2423 * The superset of the above two methods
2424 */
2425 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002426 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002427 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002428 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002429
Michael Jurka28750fb2010-09-24 17:43:49 -07002430 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002431 while (true) {
2432 int startX = 0;
2433 if (intersectX >= 0) {
2434 startX = Math.max(startX, intersectX - (spanX - 1));
2435 }
2436 int endX = mCountX - (spanX - 1);
2437 if (intersectX >= 0) {
2438 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2439 }
2440 int startY = 0;
2441 if (intersectY >= 0) {
2442 startY = Math.max(startY, intersectY - (spanY - 1));
2443 }
2444 int endY = mCountY - (spanY - 1);
2445 if (intersectY >= 0) {
2446 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2447 }
2448
Winson Chungbbc60d82010-11-11 16:34:41 -08002449 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002450 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002451 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002452 for (int i = 0; i < spanX; i++) {
2453 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002454 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002455 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002456 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002457 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002458 continue inner;
2459 }
2460 }
2461 }
2462 if (cellXY != null) {
2463 cellXY[0] = x;
2464 cellXY[1] = y;
2465 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002466 foundCell = true;
2467 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002468 }
2469 }
2470 if (intersectX == -1 && intersectY == -1) {
2471 break;
2472 } else {
2473 // if we failed to find anything, try again but without any requirements of
2474 // intersecting
2475 intersectX = -1;
2476 intersectY = -1;
2477 continue;
2478 }
2479 }
2480
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002481 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002482 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002483 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002484 }
2485
2486 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002487 * A drag event has begun over this layout.
2488 * It may have begun over this layout (in which case onDragChild is called first),
2489 * or it may have begun on another layout.
2490 */
2491 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002492 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002493 mDragging = true;
2494 }
2495
2496 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002497 * Called when drag has left this CellLayout or has been completed (successfully or not)
2498 */
2499 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002500 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002501 // This can actually be called when we aren't in a drag, e.g. when adding a new
2502 // item to this layout via the customize drawer.
2503 // Guard against that case.
2504 if (mDragging) {
2505 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002506 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002507
2508 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002509 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002510 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2511 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002512 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002513 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002514 }
2515
2516 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002517 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002518 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002519 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 *
2521 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002523 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002524 if (child != null) {
2525 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002526 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002527 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002528 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529 }
2530
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002532 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002533 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002534 * @param cellX X coordinate of upper left corner expressed as a cell position
2535 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002536 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002538 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002540 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 final int cellWidth = mCellWidth;
2542 final int cellHeight = mCellHeight;
2543 final int widthGap = mWidthGap;
2544 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002545
Winson Chung4b825dcd2011-06-19 12:41:22 -07002546 final int hStartPadding = getPaddingLeft();
2547 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2550 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2551
2552 int x = hStartPadding + cellX * (cellWidth + widthGap);
2553 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002554
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002555 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002557
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002559 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002561 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002562 * @param width Width in pixels
2563 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002564 * @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 -08002565 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002566 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002567 return rectToCell(getResources(), width, height, result);
2568 }
2569
2570 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002571 // Always assume we're working with the smallest span to make sure we
2572 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002573 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2574 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002578 int spanX = (int) Math.ceil(width / (float) smallerSize);
2579 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002580
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002581 if (result == null) {
2582 return new int[] { spanX, spanY };
2583 }
2584 result[0] = spanX;
2585 result[1] = spanY;
2586 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 }
2588
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002589 public int[] cellSpansToSize(int hSpans, int vSpans) {
2590 int[] size = new int[2];
2591 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2592 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2593 return size;
2594 }
2595
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002596 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002597 * Calculate the grid spans needed to fit given item
2598 */
2599 public void calculateSpans(ItemInfo info) {
2600 final int minWidth;
2601 final int minHeight;
2602
2603 if (info instanceof LauncherAppWidgetInfo) {
2604 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2605 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2606 } else if (info instanceof PendingAddWidgetInfo) {
2607 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2608 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2609 } else {
2610 // It's not a widget, so it must be 1x1
2611 info.spanX = info.spanY = 1;
2612 return;
2613 }
2614 int[] spans = rectToCell(minWidth, minHeight, null);
2615 info.spanX = spans[0];
2616 info.spanY = spans[1];
2617 }
2618
2619 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002620 * Find the first vacant cell, if there is one.
2621 *
2622 * @param vacant Holds the x and y coordinate of the vacant cell
2623 * @param spanX Horizontal cell span.
2624 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002625 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 * @return True if a vacant cell was found
2627 */
2628 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002629
Michael Jurka0280c3b2010-09-17 15:00:07 -07002630 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002631 }
2632
2633 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2634 int xCount, int yCount, boolean[][] occupied) {
2635
Adam Cohen2801caf2011-05-13 20:57:39 -07002636 for (int y = 0; y < yCount; y++) {
2637 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002638 boolean available = !occupied[x][y];
2639out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2640 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2641 available = available && !occupied[i][j];
2642 if (!available) break out;
2643 }
2644 }
2645
2646 if (available) {
2647 vacant[0] = x;
2648 vacant[1] = y;
2649 return true;
2650 }
2651 }
2652 }
2653
2654 return false;
2655 }
2656
Michael Jurka0280c3b2010-09-17 15:00:07 -07002657 private void clearOccupiedCells() {
2658 for (int x = 0; x < mCountX; x++) {
2659 for (int y = 0; y < mCountY; y++) {
2660 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002661 }
2662 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002664
Adam Cohend41fbf52012-02-16 23:53:59 -08002665 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002666 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002667 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002669
Adam Cohend4844c32011-02-18 19:25:06 -08002670 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002671 markCellsAsOccupiedForView(view, mOccupied);
2672 }
2673 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002674 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002675 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002676 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002677 }
2678
Adam Cohend4844c32011-02-18 19:25:06 -08002679 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002680 markCellsAsUnoccupiedForView(view, mOccupied);
2681 }
2682 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002683 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002684 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002685 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002686 }
2687
Adam Cohen482ed822012-03-02 14:15:13 -08002688 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2689 boolean value) {
2690 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002691 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2692 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002693 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002694 }
2695 }
2696 }
2697
Adam Cohen2801caf2011-05-13 20:57:39 -07002698 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002699 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002700 (Math.max((mCountX - 1), 0) * mWidthGap);
2701 }
2702
2703 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07002704 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002705 (Math.max((mCountY - 1), 0) * mHeightGap);
2706 }
2707
Michael Jurka66d72172011-04-12 16:29:25 -07002708 public boolean isOccupied(int x, int y) {
2709 if (x < mCountX && y < mCountY) {
2710 return mOccupied[x][y];
2711 } else {
2712 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2713 }
2714 }
2715
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002716 @Override
2717 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2718 return new CellLayout.LayoutParams(getContext(), attrs);
2719 }
2720
2721 @Override
2722 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2723 return p instanceof CellLayout.LayoutParams;
2724 }
2725
2726 @Override
2727 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2728 return new CellLayout.LayoutParams(p);
2729 }
2730
Winson Chungaafa03c2010-06-11 17:34:16 -07002731 public static class CellLayoutAnimationController extends LayoutAnimationController {
2732 public CellLayoutAnimationController(Animation animation, float delay) {
2733 super(animation, delay);
2734 }
2735
2736 @Override
2737 protected long getDelayForView(View view) {
2738 return (int) (Math.random() * 150);
2739 }
2740 }
2741
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002742 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2743 /**
2744 * Horizontal location of the item in the grid.
2745 */
2746 @ViewDebug.ExportedProperty
2747 public int cellX;
2748
2749 /**
2750 * Vertical location of the item in the grid.
2751 */
2752 @ViewDebug.ExportedProperty
2753 public int cellY;
2754
2755 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002756 * Temporary horizontal location of the item in the grid during reorder
2757 */
2758 public int tmpCellX;
2759
2760 /**
2761 * Temporary vertical location of the item in the grid during reorder
2762 */
2763 public int tmpCellY;
2764
2765 /**
2766 * Indicates that the temporary coordinates should be used to layout the items
2767 */
2768 public boolean useTmpCoords;
2769
2770 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 * Number of cells spanned horizontally by the item.
2772 */
2773 @ViewDebug.ExportedProperty
2774 public int cellHSpan;
2775
2776 /**
2777 * Number of cells spanned vertically by the item.
2778 */
2779 @ViewDebug.ExportedProperty
2780 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002781
Adam Cohen1b607ed2011-03-03 17:26:50 -08002782 /**
2783 * Indicates whether the item will set its x, y, width and height parameters freely,
2784 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2785 */
Adam Cohend4844c32011-02-18 19:25:06 -08002786 public boolean isLockedToGrid = true;
2787
Adam Cohen482ed822012-03-02 14:15:13 -08002788 /**
2789 * Indicates whether this item can be reordered. Always true except in the case of the
2790 * the AllApps button.
2791 */
2792 public boolean canReorder = true;
2793
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002794 // X coordinate of the view in the layout.
2795 @ViewDebug.ExportedProperty
2796 int x;
2797 // Y coordinate of the view in the layout.
2798 @ViewDebug.ExportedProperty
2799 int y;
2800
Romain Guy84f296c2009-11-04 15:00:44 -08002801 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002803 public LayoutParams(Context c, AttributeSet attrs) {
2804 super(c, attrs);
2805 cellHSpan = 1;
2806 cellVSpan = 1;
2807 }
2808
2809 public LayoutParams(ViewGroup.LayoutParams source) {
2810 super(source);
2811 cellHSpan = 1;
2812 cellVSpan = 1;
2813 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002814
2815 public LayoutParams(LayoutParams source) {
2816 super(source);
2817 this.cellX = source.cellX;
2818 this.cellY = source.cellY;
2819 this.cellHSpan = source.cellHSpan;
2820 this.cellVSpan = source.cellVSpan;
2821 }
2822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002824 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002825 this.cellX = cellX;
2826 this.cellY = cellY;
2827 this.cellHSpan = cellHSpan;
2828 this.cellVSpan = cellVSpan;
2829 }
2830
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002831 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002832 if (isLockedToGrid) {
2833 final int myCellHSpan = cellHSpan;
2834 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002835 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2836 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002837
Adam Cohend4844c32011-02-18 19:25:06 -08002838 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2839 leftMargin - rightMargin;
2840 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2841 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002842 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2843 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002844 }
2845 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002846
Winson Chungaafa03c2010-06-11 17:34:16 -07002847 public String toString() {
2848 return "(" + this.cellX + ", " + this.cellY + ")";
2849 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002850
2851 public void setWidth(int width) {
2852 this.width = width;
2853 }
2854
2855 public int getWidth() {
2856 return width;
2857 }
2858
2859 public void setHeight(int height) {
2860 this.height = height;
2861 }
2862
2863 public int getHeight() {
2864 return height;
2865 }
2866
2867 public void setX(int x) {
2868 this.x = x;
2869 }
2870
2871 public int getX() {
2872 return x;
2873 }
2874
2875 public void setY(int y) {
2876 this.y = y;
2877 }
2878
2879 public int getY() {
2880 return y;
2881 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002882 }
2883
Michael Jurka0280c3b2010-09-17 15:00:07 -07002884 // This class stores info for two purposes:
2885 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2886 // its spanX, spanY, and the screen it is on
2887 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2888 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2889 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002890 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002891 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002892 int cellX = -1;
2893 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002894 int spanX;
2895 int spanY;
2896 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002897 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002899 @Override
2900 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002901 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2902 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002903 }
2904 }
Michael Jurkad771c962011-08-09 15:00:48 -07002905
2906 public boolean lastDownOnOccupiedCell() {
2907 return mLastDownOnOccupiedCell;
2908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002909}