blob: ac41a2bd7b9b556630779c8a32deb2d456064463 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
Michael Jurka629758f2012-06-14 16:18:21 -070020import android.animation.AnimatorListenerAdapter;
Brandon Keely50e6e562012-05-08 16:28:49 -070021import android.animation.AnimatorSet;
Chet Haase00397b12010-10-07 11:13:10 -070022import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070023import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040026import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070027import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070028import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080030import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070031import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070032import android.graphics.Point;
Adam Cohenb5ba0972011-09-07 18:02:31 -070033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080036import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070037import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070038import android.graphics.drawable.NinePatchDrawable;
Adam Cohen1462de32012-07-24 22:34:36 -070039import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070041import android.util.Log;
Adam Cohen1462de32012-07-24 22:34:36 -070042import android.util.SparseArray;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.R;
52import com.android.launcher3.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070053
Adam Cohen69ce2e52011-07-03 19:25:21 -070054import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import java.util.Arrays;
Adam Cohenf3900c22012-11-16 18:28:11 -080056import java.util.Collections;
57import java.util.Comparator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070058import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080059import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070060
Michael Jurkabdb5c532011-02-01 15:05:06 -080061public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070062 static final String TAG = "CellLayout";
63
Adam Cohen2acce882012-03-28 19:03:19 -070064 private Launcher mLauncher;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 private int mCellWidth;
66 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070067
Adam Cohend22015c2010-07-26 22:02:18 -070068 private int mCountX;
69 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070
Adam Cohen234c4cd2011-07-17 21:03:04 -070071 private int mOriginalWidthGap;
72 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073 private int mWidthGap;
74 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070075 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080076 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077
78 private final Rect mRect = new Rect();
79 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070080
Patrick Dubroyde7658b2010-09-27 11:15:43 -070081 // These are temporary variables to prevent having to allocate a new object just to
82 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070083 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070084 private final int[] mTmpPoint = new int[2];
Adam Cohen69ce2e52011-07-03 19:25:21 -070085 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070086
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080088 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070089 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Michael Jurkadee05892010-07-27 10:01:56 -070091 private OnTouchListener mInterceptTouchListener;
92
Adam Cohen69ce2e52011-07-03 19:25:21 -070093 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070094 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070095
Adam Cohenb5ba0972011-09-07 18:02:31 -070096 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070097 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070098 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070099
Michael Jurka33945b22010-12-21 18:19:38 -0800100 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800101 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700102 private Drawable mOverScrollForegroundDrawable;
103 private Drawable mOverScrollLeft;
104 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700105 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700106 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700107 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700108
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700109 // These values allow a fixed measurement to be set on the CellLayout.
110 private int mFixedWidth = -1;
111 private int mFixedHeight = -1;
112
Michael Jurka33945b22010-12-21 18:19:38 -0800113 // If we're actively dragging something over this screen, mIsDragOverlapping is true
114 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700115 private final Point mDragCenter = new Point();
Adam Cohendedbd962013-07-11 14:21:49 -0700116 boolean mUseActiveGlowBackground = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700117
Winson Chung150fbab2010-09-29 17:14:26 -0700118 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700119 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800120 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700121 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700122 private InterruptibleInOutAnimator[] mDragOutlineAnims =
123 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700124
125 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700126 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700127 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700128
Patrick Dubroy96864c32011-03-10 17:17:23 -0800129 private BubbleTextView mPressedOrFocusedIcon;
130
Adam Cohen482ed822012-03-02 14:15:13 -0800131 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
132 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohen19f37922012-03-21 11:59:11 -0700133 private HashMap<View, ReorderHintAnimation>
134 mShakeAnimators = new HashMap<View, ReorderHintAnimation>();
135
136 private boolean mItemPlacementDirty = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700137
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700138 // When a drag operation is in progress, holds the nearest cell to the touch point
139 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Joe Onorato4be866d2010-10-10 11:26:02 -0700141 private boolean mDragging = false;
142
Patrick Dubroyce34a972010-10-19 10:34:32 -0700143 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700144 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700145
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800146 private boolean mIsHotseat = false;
Adam Cohen307fe232012-08-16 17:55:58 -0700147 private float mHotseatScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800148
Adam Cohen482ed822012-03-02 14:15:13 -0800149 public static final int MODE_DRAG_OVER = 0;
150 public static final int MODE_ON_DROP = 1;
151 public static final int MODE_ON_DROP_EXTERNAL = 2;
152 public static final int MODE_ACCEPT_DROP = 3;
Adam Cohen19f37922012-03-21 11:59:11 -0700153 private static final boolean DESTRUCTIVE_REORDER = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800154 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
155
Adam Cohena897f392012-04-27 18:12:05 -0700156 static final int LANDSCAPE = 0;
157 static final int PORTRAIT = 1;
158
Adam Cohen7bdfc972012-05-22 16:50:35 -0700159 private static final float REORDER_HINT_MAGNITUDE = 0.12f;
Adam Cohen19f37922012-03-21 11:59:11 -0700160 private static final int REORDER_ANIMATION_DURATION = 150;
161 private float mReorderHintAnimationMagnitude;
162
Adam Cohen482ed822012-03-02 14:15:13 -0800163 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
164 private Rect mOccupiedRect = new Rect();
165 private int[] mDirectionVector = new int[2];
Adam Cohen19f37922012-03-21 11:59:11 -0700166 int[] mPreviousReorderDirection = new int[2];
Adam Cohenb209e632012-03-27 17:09:36 -0700167 private static final int INVALID_DIRECTION = -100;
Adam Cohenc6cc61d2012-04-04 12:47:08 -0700168 private DropTarget.DragEnforcer mDragEnforcer;
Adam Cohen482ed822012-03-02 14:15:13 -0800169
Romain Guy8a0bff52012-05-06 13:14:33 -0700170 private final static PorterDuffXfermode sAddBlendMode =
171 new PorterDuffXfermode(PorterDuff.Mode.ADD);
Michael Jurkaca993832012-06-29 15:17:04 -0700172 private final static Paint sPaint = new Paint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 public CellLayout(Context context) {
175 this(context, null);
176 }
177
178 public CellLayout(Context context, AttributeSet attrs) {
179 this(context, attrs, 0);
180 }
181
182 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
183 super(context, attrs, defStyle);
Michael Jurka8b805b12012-04-18 14:23:14 -0700184 mDragEnforcer = new DropTarget.DragEnforcer(context);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700185
186 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
187 // the user where a dragged item will land when dropped.
188 setWillNotDraw(false);
Romain Guyce3cbd12013-02-25 15:00:36 -0800189 setClipToPadding(false);
Adam Cohen2acce882012-03-28 19:03:19 -0700190 mLauncher = (Launcher) context;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
193
Adam Cohenf4bd5792012-04-27 11:35:29 -0700194 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
195 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700196 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
197 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700198 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700199 mCountX = LauncherModel.getCellCountX();
200 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700201 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800202 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen5b53f292012-03-29 14:30:35 -0700203 mPreviousReorderDirection[0] = INVALID_DIRECTION;
204 mPreviousReorderDirection[1] = INVALID_DIRECTION;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
206 a.recycle();
207
208 setAlwaysDrawnWithCacheEnabled(false);
209
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700210 final Resources res = getResources();
Adam Cohen307fe232012-08-16 17:55:58 -0700211 mHotseatScale = (res.getInteger(R.integer.hotseat_item_scale_percentage) / 100f);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700212
Winson Chung967289b2011-06-30 18:09:30 -0700213 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700214 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800215
Adam Cohenb5ba0972011-09-07 18:02:31 -0700216 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
217 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
218 mForegroundPadding =
219 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800220
Adam Cohen19f37922012-03-21 11:59:11 -0700221 mReorderHintAnimationMagnitude = (REORDER_HINT_MAGNITUDE *
222 res.getDimensionPixelSize(R.dimen.app_icon_size));
223
Winson Chungb26f3d62011-06-02 10:49:29 -0700224 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700225 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700226
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700227 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700228
Patrick Dubroyce34a972010-10-19 10:34:32 -0700229 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700230
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700231
Winson Chungb8c69f32011-10-19 21:36:08 -0700232 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700233 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800234 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700235 }
236
237 // When dragging things around the home screens, we show a green outline of
238 // where the item will land. The outlines gradually fade out, leaving a trail
239 // behind the drag path.
240 // Set up all the animations that are used to implement this fading.
241 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700242 final float fromAlphaValue = 0;
243 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700244
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700245 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700246
247 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700248 final InterruptibleInOutAnimator anim =
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100249 new InterruptibleInOutAnimator(this, duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700250 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700251 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700252 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700253 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700254 final Bitmap outline = (Bitmap)anim.getTag();
255
256 // If an animation is started and then stopped very quickly, we can still
257 // get spurious updates we've cleared the tag. Guard against this.
258 if (outline == null) {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700259 @SuppressWarnings("all") // suppress dead code warning
260 final boolean debug = false;
261 if (debug) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700262 Object val = animation.getAnimatedValue();
263 Log.d(TAG, "anim " + thisIndex + " update: " + val +
264 ", isStopped " + anim.isStopped());
265 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700266 // Try to prevent it from continuing to run
267 animation.cancel();
268 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700269 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800270 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700271 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700272 }
273 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 // The animation holds a reference to the drag outline bitmap as long is it's
275 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700276 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700277 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700278 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700279 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700280 anim.setTag(null);
281 }
282 }
283 });
284 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700285 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700286
Michael Jurka18014792010-10-14 09:01:34 -0700287 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700288 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800289
Michael Jurkaa52570f2012-03-20 03:18:20 -0700290 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
Adam Cohen2374abf2013-04-16 14:56:57 -0700291 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
292 mCountX);
293
Michael Jurkaa52570f2012-03-20 03:18:20 -0700294 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700295 }
296
Michael Jurkaf6440da2011-04-05 14:50:34 -0700297 static int widthInPortrait(Resources r, int numCells) {
298 // We use this method from Workspace to figure out how many rows/columns Launcher should
299 // have. We ignore the left/right padding on CellLayout because it turns out in our design
300 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700301 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700302 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
303 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700304
Winson Chung4b825dcd2011-06-19 12:41:22 -0700305 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700306 }
307
Michael Jurkaf6440da2011-04-05 14:50:34 -0700308 static int heightInLandscape(Resources r, int numCells) {
309 // We use this method from Workspace to figure out how many rows/columns Launcher should
310 // have. We ignore the left/right padding on CellLayout because it turns out in our design
311 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700312 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700313 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
314 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700315
Winson Chung4b825dcd2011-06-19 12:41:22 -0700316 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700317 }
318
Adam Cohen2801caf2011-05-13 20:57:39 -0700319 public void enableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700320 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_HARDWARE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700321 }
322
323 public void disableHardwareLayers() {
Michael Jurkaca993832012-06-29 15:17:04 -0700324 mShortcutsAndWidgets.setLayerType(LAYER_TYPE_NONE, sPaint);
Michael Jurkad51f33a2012-06-28 15:35:26 -0700325 }
326
327 public void buildHardwareLayer() {
328 mShortcutsAndWidgets.buildLayer();
Adam Cohen2801caf2011-05-13 20:57:39 -0700329 }
330
Adam Cohen307fe232012-08-16 17:55:58 -0700331 public float getChildrenScale() {
332 return mIsHotseat ? mHotseatScale : 1.0f;
333 }
334
Adam Cohen2801caf2011-05-13 20:57:39 -0700335 public void setGridSize(int x, int y) {
336 mCountX = x;
337 mCountY = y;
338 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800339 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen7fbec102012-03-27 12:42:19 -0700340 mTempRectStack.clear();
Adam Cohen2374abf2013-04-16 14:56:57 -0700341 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
342 mCountX);
Adam Cohen76fc0852011-06-17 13:26:23 -0700343 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700344 }
345
Adam Cohen2374abf2013-04-16 14:56:57 -0700346 // Set whether or not to invert the layout horizontally if the layout is in RTL mode.
347 public void setInvertIfRtl(boolean invert) {
348 mShortcutsAndWidgets.setInvertIfRtl(invert);
349 }
350
Patrick Dubroy96864c32011-03-10 17:17:23 -0800351 private void invalidateBubbleTextView(BubbleTextView icon) {
352 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700353 invalidate(icon.getLeft() + getPaddingLeft() - padding,
354 icon.getTop() + getPaddingTop() - padding,
355 icon.getRight() + getPaddingLeft() + padding,
356 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800357 }
358
Adam Cohenb5ba0972011-09-07 18:02:31 -0700359 void setOverScrollAmount(float r, boolean left) {
360 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
361 mOverScrollForegroundDrawable = mOverScrollLeft;
362 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
363 mOverScrollForegroundDrawable = mOverScrollRight;
364 }
365
366 mForegroundAlpha = (int) Math.round((r * 255));
367 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
368 invalidate();
369 }
370
Patrick Dubroy96864c32011-03-10 17:17:23 -0800371 void setPressedOrFocusedIcon(BubbleTextView icon) {
372 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
373 // requires an expanded clip rect (due to the glow's blur radius)
374 BubbleTextView oldIcon = mPressedOrFocusedIcon;
375 mPressedOrFocusedIcon = icon;
376 if (oldIcon != null) {
377 invalidateBubbleTextView(oldIcon);
378 }
379 if (mPressedOrFocusedIcon != null) {
380 invalidateBubbleTextView(mPressedOrFocusedIcon);
381 }
382 }
383
Michael Jurka33945b22010-12-21 18:19:38 -0800384 void setIsDragOverlapping(boolean isDragOverlapping) {
385 if (mIsDragOverlapping != isDragOverlapping) {
386 mIsDragOverlapping = isDragOverlapping;
Adam Cohendedbd962013-07-11 14:21:49 -0700387 setUseActiveGlowBackground(mIsDragOverlapping);
Michael Jurka33945b22010-12-21 18:19:38 -0800388 invalidate();
389 }
390 }
391
Adam Cohendedbd962013-07-11 14:21:49 -0700392 void setUseActiveGlowBackground(boolean use) {
393 mUseActiveGlowBackground = use;
394 }
395
Michael Jurka33945b22010-12-21 18:19:38 -0800396 boolean getIsDragOverlapping() {
397 return mIsDragOverlapping;
398 }
399
Adam Cohenebea84d2011-11-09 17:20:41 -0800400 protected void setOverscrollTransformsDirty(boolean dirty) {
401 mScrollingTransformsDirty = dirty;
402 }
403
404 protected void resetOverscrollTransforms() {
405 if (mScrollingTransformsDirty) {
406 setOverscrollTransformsDirty(false);
407 setTranslationX(0);
408 setRotationY(0);
409 // It doesn't matter if we pass true or false here, the important thing is that we
410 // pass 0, which results in the overscroll drawable not being drawn any more.
411 setOverScrollAmount(0, false);
412 setPivotX(getMeasuredWidth() / 2);
413 setPivotY(getMeasuredHeight() / 2);
414 }
415 }
416
Adam Cohen307fe232012-08-16 17:55:58 -0700417 public void scaleRect(Rect r, float scale) {
418 if (scale != 1.0f) {
419 r.left = (int) (r.left * scale + 0.5f);
420 r.top = (int) (r.top * scale + 0.5f);
421 r.right = (int) (r.right * scale + 0.5f);
422 r.bottom = (int) (r.bottom * scale + 0.5f);
423 }
424 }
425
426 Rect temp = new Rect();
427 void scaleRectAboutCenter(Rect in, Rect out, float scale) {
428 int cx = in.centerX();
429 int cy = in.centerY();
430 out.set(in);
431 out.offset(-cx, -cy);
432 scaleRect(out, scale);
433 out.offset(cx, cy);
434 }
435
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700436 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700437 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700438 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
439 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
440 // When we're small, we are either drawn normally or in the "accepts drops" state (during
441 // a drag). However, we also drag the mini hover background *over* one of those two
442 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700443 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700444 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800445
Adam Cohendedbd962013-07-11 14:21:49 -0700446 if (mUseActiveGlowBackground) {
Michael Jurka33945b22010-12-21 18:19:38 -0800447 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700448 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700449 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700450 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700451 }
Michael Jurka33945b22010-12-21 18:19:38 -0800452
453 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
454 bg.setBounds(mBackgroundRect);
455 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700456 }
Romain Guya6abce82009-11-10 02:54:41 -0800457
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700458 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700459 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700460 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700461 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800462 final Rect r = mDragOutlines[i];
Adam Cohen307fe232012-08-16 17:55:58 -0700463 scaleRectAboutCenter(r, temp, getChildrenScale());
Joe Onorato4be866d2010-10-10 11:26:02 -0700464 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700465 paint.setAlpha((int)(alpha + .5f));
Adam Cohen307fe232012-08-16 17:55:58 -0700466 canvas.drawBitmap(b, null, temp, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700467 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700468 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800469
470 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
471 // requires an expanded clip rect (due to the glow's blur radius)
472 if (mPressedOrFocusedIcon != null) {
473 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
474 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
475 if (b != null) {
476 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700477 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
478 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800479 null);
480 }
481 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700482
Adam Cohen482ed822012-03-02 14:15:13 -0800483 if (DEBUG_VISUALIZE_OCCUPIED) {
484 int[] pt = new int[2];
485 ColorDrawable cd = new ColorDrawable(Color.RED);
Adam Cohene7587d22012-05-24 18:50:02 -0700486 cd.setBounds(0, 0, mCellWidth, mCellHeight);
Adam Cohen482ed822012-03-02 14:15:13 -0800487 for (int i = 0; i < mCountX; i++) {
488 for (int j = 0; j < mCountY; j++) {
489 if (mOccupied[i][j]) {
490 cellToPoint(i, j, pt);
491 canvas.save();
492 canvas.translate(pt[0], pt[1]);
493 cd.draw(canvas);
494 canvas.restore();
495 }
496 }
497 }
498 }
499
Andrew Flynn850d2e72012-04-26 16:51:20 -0700500 int previewOffset = FolderRingAnimator.sPreviewSize;
501
Adam Cohen69ce2e52011-07-03 19:25:21 -0700502 // The folder outer / inner ring image(s)
503 for (int i = 0; i < mFolderOuterRings.size(); i++) {
504 FolderRingAnimator fra = mFolderOuterRings.get(i);
505
506 // Draw outer ring
507 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
508 int width = (int) fra.getOuterRingSize();
509 int height = width;
510 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
511
512 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700513 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700514
515 canvas.save();
516 canvas.translate(centerX - width / 2, centerY - height / 2);
517 d.setBounds(0, 0, width, height);
518 d.draw(canvas);
519 canvas.restore();
520
521 // Draw inner ring
522 d = FolderRingAnimator.sSharedInnerRingDrawable;
523 width = (int) fra.getInnerRingSize();
524 height = width;
525 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
526
527 centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700528 centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohen69ce2e52011-07-03 19:25:21 -0700529 canvas.save();
530 canvas.translate(centerX - width / 2, centerY - width / 2);
531 d.setBounds(0, 0, width, height);
532 d.draw(canvas);
533 canvas.restore();
534 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700535
536 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
537 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
538 int width = d.getIntrinsicWidth();
539 int height = d.getIntrinsicHeight();
540
541 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
542 int centerX = mTempLocation[0] + mCellWidth / 2;
Andrew Flynn850d2e72012-04-26 16:51:20 -0700543 int centerY = mTempLocation[1] + previewOffset / 2;
Adam Cohenc51934b2011-07-26 21:07:43 -0700544
545 canvas.save();
546 canvas.translate(centerX - width / 2, centerY - width / 2);
547 d.setBounds(0, 0, width, height);
548 d.draw(canvas);
549 canvas.restore();
550 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700551 }
552
Adam Cohenb5ba0972011-09-07 18:02:31 -0700553 @Override
554 protected void dispatchDraw(Canvas canvas) {
555 super.dispatchDraw(canvas);
556 if (mForegroundAlpha > 0) {
557 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
558 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
Romain Guy8a0bff52012-05-06 13:14:33 -0700559 p.setXfermode(sAddBlendMode);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700560 mOverScrollForegroundDrawable.draw(canvas);
561 p.setXfermode(null);
562 }
563 }
564
Adam Cohen69ce2e52011-07-03 19:25:21 -0700565 public void showFolderAccept(FolderRingAnimator fra) {
566 mFolderOuterRings.add(fra);
567 }
568
569 public void hideFolderAccept(FolderRingAnimator fra) {
570 if (mFolderOuterRings.contains(fra)) {
571 mFolderOuterRings.remove(fra);
572 }
573 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700574 }
575
Adam Cohenc51934b2011-07-26 21:07:43 -0700576 public void setFolderLeaveBehindCell(int x, int y) {
577 mFolderLeaveBehindCell[0] = x;
578 mFolderLeaveBehindCell[1] = y;
579 invalidate();
580 }
581
582 public void clearFolderLeaveBehind() {
583 mFolderLeaveBehindCell[0] = -1;
584 mFolderLeaveBehindCell[1] = -1;
585 invalidate();
586 }
587
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700588 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700589 public boolean shouldDelayChildPressedState() {
590 return false;
591 }
592
Adam Cohen1462de32012-07-24 22:34:36 -0700593 public void restoreInstanceState(SparseArray<Parcelable> states) {
594 dispatchRestoreInstanceState(states);
595 }
596
Michael Jurkae6235dd2011-10-04 15:02:05 -0700597 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700598 public void cancelLongPress() {
599 super.cancelLongPress();
600
601 // Cancel long press for all children
602 final int count = getChildCount();
603 for (int i = 0; i < count; i++) {
604 final View child = getChildAt(i);
605 child.cancelLongPress();
606 }
607 }
608
Michael Jurkadee05892010-07-27 10:01:56 -0700609 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
610 mInterceptTouchListener = listener;
611 }
612
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700614 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 }
616
617 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700618 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 }
620
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800621 public void setIsHotseat(boolean isHotseat) {
622 mIsHotseat = isHotseat;
623 }
624
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800625 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
Andrew Flynn850d2e72012-04-26 16:51:20 -0700626 boolean markCells) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700627 final LayoutParams lp = params;
628
Andrew Flynnde38e422012-05-08 11:22:15 -0700629 // Hotseat icons - remove text
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800630 if (child instanceof BubbleTextView) {
631 BubbleTextView bubbleChild = (BubbleTextView) child;
632
Andrew Flynnde38e422012-05-08 11:22:15 -0700633 Resources res = getResources();
634 if (mIsHotseat) {
635 bubbleChild.setTextColor(res.getColor(android.R.color.transparent));
636 } else {
637 bubbleChild.setTextColor(res.getColor(R.color.workspace_icon_text_color));
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800638 }
639 }
640
Adam Cohen307fe232012-08-16 17:55:58 -0700641 child.setScaleX(getChildrenScale());
642 child.setScaleY(getChildrenScale());
643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 // Generate an id for each view, this assumes we have at most 256x256 cells
645 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700646 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700647 // If the horizontal or vertical span is set to -1, it is taken to
648 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700649 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
650 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651
Winson Chungaafa03c2010-06-11 17:34:16 -0700652 child.setId(childId);
653
Michael Jurkaa52570f2012-03-20 03:18:20 -0700654 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700655
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700656 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700657
Winson Chungaafa03c2010-06-11 17:34:16 -0700658 return true;
659 }
660 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700662
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664 public void removeAllViews() {
665 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700666 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700667 }
668
669 @Override
670 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700671 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700672 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700673 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700674 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700675 }
676
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700677 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700678 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700679 }
680
Michael Jurka0280c3b2010-09-17 15:00:07 -0700681 @Override
682 public void removeView(View view) {
683 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700684 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700685 }
686
687 @Override
688 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700689 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
690 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700691 }
692
693 @Override
694 public void removeViewInLayout(View view) {
695 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700696 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700697 }
698
699 @Override
700 public void removeViews(int start, int count) {
701 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700702 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700703 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700704 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700705 }
706
707 @Override
708 public void removeViewsInLayout(int start, int count) {
709 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700710 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700711 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700712 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800713 }
714
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 @Override
716 protected void onAttachedToWindow() {
717 super.onAttachedToWindow();
Adam Cohendcd297f2013-06-18 13:13:40 -0700718 if (getParent() instanceof Workspace) {
719 Workspace workspace = (Workspace) getParent();
720 mCellInfo.screenId = workspace.getIdForScreen(this);
721 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 }
723
Michael Jurkaaf442092010-06-10 17:01:57 -0700724 public void setTagToCellInfoForPoint(int touchX, int touchY) {
725 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800726 Rect frame = mRect;
Michael Jurka8b805b12012-04-18 14:23:14 -0700727 final int x = touchX + getScrollX();
728 final int y = touchY + getScrollY();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700729 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700730
731 boolean found = false;
732 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700733 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800734 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700735
Adam Cohen1b607ed2011-03-03 17:26:50 -0800736 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
737 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700738 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700739
Winson Chungeecf02d2012-03-02 17:14:58 -0800740 float scale = child.getScaleX();
741 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
742 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700743 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
744 // offset that by this CellLayout's padding to test an (x,y) point that is relative
745 // to this view.
Michael Jurka8b805b12012-04-18 14:23:14 -0700746 frame.offset(getPaddingLeft(), getPaddingTop());
Winson Chungeecf02d2012-03-02 17:14:58 -0800747 frame.inset((int) (frame.width() * (1f - scale) / 2),
748 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700749
Michael Jurkaaf442092010-06-10 17:01:57 -0700750 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700751 cellInfo.cell = child;
752 cellInfo.cellX = lp.cellX;
753 cellInfo.cellY = lp.cellY;
754 cellInfo.spanX = lp.cellHSpan;
755 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700756 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700757 break;
758 }
759 }
760 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700761
Michael Jurkad771c962011-08-09 15:00:48 -0700762 mLastDownOnOccupiedCell = found;
763
Michael Jurkaaf442092010-06-10 17:01:57 -0700764 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700765 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700766 pointToCellExact(x, y, cellXY);
767
Michael Jurkaaf442092010-06-10 17:01:57 -0700768 cellInfo.cell = null;
769 cellInfo.cellX = cellXY[0];
770 cellInfo.cellY = cellXY[1];
771 cellInfo.spanX = 1;
772 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700773 }
774 setTag(cellInfo);
775 }
776
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 @Override
778 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700779 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
780 // even in the case where we return early. Not clearing here was causing bugs whereby on
781 // long-press we'd end up picking up an item from a previous drag operation.
782 final int action = ev.getAction();
783
784 if (action == MotionEvent.ACTION_DOWN) {
785 clearTagCellInfo();
786 }
787
Michael Jurkadee05892010-07-27 10:01:56 -0700788 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
789 return true;
790 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791
792 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700793 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 return false;
797 }
798
Adam Cohenc1997fd2011-08-15 18:26:39 -0700799 private void clearTagCellInfo() {
800 final CellInfo cellInfo = mCellInfo;
801 cellInfo.cell = null;
802 cellInfo.cellX = -1;
803 cellInfo.cellY = -1;
804 cellInfo.spanX = 0;
805 cellInfo.spanY = 0;
806 setTag(cellInfo);
807 }
808
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700810 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 }
812
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700813 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700814 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 * @param x X coordinate of the point
816 * @param y Y coordinate of the point
817 * @param result Array of 2 ints to hold the x and y coordinate of the cell
818 */
819 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700820 final int hStartPadding = getPaddingLeft();
821 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822
823 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
824 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
825
Adam Cohend22015c2010-07-26 22:02:18 -0700826 final int xAxis = mCountX;
827 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828
829 if (result[0] < 0) result[0] = 0;
830 if (result[0] >= xAxis) result[0] = xAxis - 1;
831 if (result[1] < 0) result[1] = 0;
832 if (result[1] >= yAxis) result[1] = yAxis - 1;
833 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700834
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 /**
836 * Given a point, return the cell that most closely encloses that point
837 * @param x X coordinate of the point
838 * @param y Y coordinate of the point
839 * @param result Array of 2 ints to hold the x and y coordinate of the cell
840 */
841 void pointToCellRounded(int x, int y, int[] result) {
842 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
843 }
844
845 /**
846 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700847 *
848 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700850 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 * @param result Array of 2 ints to hold the x and y coordinate of the point
852 */
853 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700854 final int hStartPadding = getPaddingLeft();
855 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856
857 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
858 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
859 }
860
Adam Cohene3e27a82011-04-15 12:07:39 -0700861 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800862 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700863 *
864 * @param cellX X coordinate of the cell
865 * @param cellY Y coordinate of the cell
866 *
867 * @param result Array of 2 ints to hold the x and y coordinate of the point
868 */
869 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700870 regionToCenterPoint(cellX, cellY, 1, 1, result);
871 }
872
873 /**
874 * Given a cell coordinate and span return the point that represents the center of the regio
875 *
876 * @param cellX X coordinate of the cell
877 * @param cellY Y coordinate of the cell
878 *
879 * @param result Array of 2 ints to hold the x and y coordinate of the point
880 */
881 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700882 final int hStartPadding = getPaddingLeft();
883 final int vStartPadding = getPaddingTop();
Adam Cohen47a876d2012-03-19 13:21:41 -0700884 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
885 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
886 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
887 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700888 }
889
Adam Cohen19f37922012-03-21 11:59:11 -0700890 /**
891 * Given a cell coordinate and span fills out a corresponding pixel rect
892 *
893 * @param cellX X coordinate of the cell
894 * @param cellY Y coordinate of the cell
895 * @param result Rect in which to write the result
896 */
897 void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
898 final int hStartPadding = getPaddingLeft();
899 final int vStartPadding = getPaddingTop();
900 final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
901 final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
902 result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
903 top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
904 }
905
Adam Cohen482ed822012-03-02 14:15:13 -0800906 public float getDistanceFromCell(float x, float y, int[] cell) {
907 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
908 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
909 Math.pow(y - mTmpPoint[1], 2));
910 return distance;
911 }
912
Romain Guy84f296c2009-11-04 15:00:44 -0800913 int getCellWidth() {
914 return mCellWidth;
915 }
916
917 int getCellHeight() {
918 return mCellHeight;
919 }
920
Adam Cohend4844c32011-02-18 19:25:06 -0800921 int getWidthGap() {
922 return mWidthGap;
923 }
924
925 int getHeightGap() {
926 return mHeightGap;
927 }
928
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700929 Rect getContentRect(Rect r) {
930 if (r == null) {
931 r = new Rect();
932 }
933 int left = getPaddingLeft();
934 int top = getPaddingTop();
Michael Jurka8b805b12012-04-18 14:23:14 -0700935 int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
936 int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700937 r.set(left, top, right, bottom);
938 return r;
939 }
940
Adam Cohena897f392012-04-27 18:12:05 -0700941 static void getMetrics(Rect metrics, Resources res, int measureWidth, int measureHeight,
942 int countX, int countY, int orientation) {
943 int numWidthGaps = countX - 1;
944 int numHeightGaps = countY - 1;
Adam Cohenf4bd5792012-04-27 11:35:29 -0700945
946 int widthGap;
947 int heightGap;
948 int cellWidth;
949 int cellHeight;
950 int paddingLeft;
951 int paddingRight;
952 int paddingTop;
953 int paddingBottom;
954
Adam Cohena897f392012-04-27 18:12:05 -0700955 int maxGap = res.getDimensionPixelSize(R.dimen.workspace_max_gap);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700956 if (orientation == LANDSCAPE) {
957 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_land);
958 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_land);
959 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_land);
960 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_land);
961 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_land);
962 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_land);
963 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_land);
964 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_land);
965 } else {
966 // PORTRAIT
967 cellWidth = res.getDimensionPixelSize(R.dimen.workspace_cell_width_port);
968 cellHeight = res.getDimensionPixelSize(R.dimen.workspace_cell_height_port);
969 widthGap = res.getDimensionPixelSize(R.dimen.workspace_width_gap_port);
970 heightGap = res.getDimensionPixelSize(R.dimen.workspace_height_gap_port);
971 paddingLeft = res.getDimensionPixelSize(R.dimen.cell_layout_left_padding_port);
972 paddingRight = res.getDimensionPixelSize(R.dimen.cell_layout_right_padding_port);
973 paddingTop = res.getDimensionPixelSize(R.dimen.cell_layout_top_padding_port);
974 paddingBottom = res.getDimensionPixelSize(R.dimen.cell_layout_bottom_padding_port);
975 }
976
977 if (widthGap < 0 || heightGap < 0) {
978 int hSpace = measureWidth - paddingLeft - paddingRight;
979 int vSpace = measureHeight - paddingTop - paddingBottom;
Adam Cohena897f392012-04-27 18:12:05 -0700980 int hFreeSpace = hSpace - (countX * cellWidth);
981 int vFreeSpace = vSpace - (countY * cellHeight);
982 widthGap = Math.min(maxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
983 heightGap = Math.min(maxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Adam Cohenf4bd5792012-04-27 11:35:29 -0700984 }
985 metrics.set(cellWidth, cellHeight, widthGap, heightGap);
986 }
987
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700988 public void setFixedSize(int width, int height) {
989 mFixedWidth = width;
990 mFixedHeight = height;
991 }
992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 @Override
994 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700996 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
997
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
999 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -07001000
Adam Cohenf0f4eda2013-06-06 21:27:03 -07001001 int newWidth = widthSpecSize;
1002 int newHeight = heightSpecSize;
1003 if (mFixedWidth > 0 && mFixedHeight > 0) {
1004 newWidth = mFixedWidth;
1005 newHeight = mFixedHeight;
1006 } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
1008 }
1009
Adam Cohend22015c2010-07-26 22:02:18 -07001010 int numWidthGaps = mCountX - 1;
1011 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012
Adam Cohen234c4cd2011-07-17 21:03:04 -07001013 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Michael Jurkadd13e3d2012-05-01 12:38:17 -07001014 int hSpace = widthSpecSize - getPaddingLeft() - getPaddingRight();
1015 int vSpace = heightSpecSize - getPaddingTop() - getPaddingBottom();
Adam Cohenf4bd5792012-04-27 11:35:29 -07001016 int hFreeSpace = hSpace - (mCountX * mCellWidth);
1017 int vFreeSpace = vSpace - (mCountY * mCellHeight);
Winson Chung4b825dcd2011-06-19 12:41:22 -07001018 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
1019 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Adam Cohen2374abf2013-04-16 14:56:57 -07001020 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap,
1021 mCountX);
Adam Cohen234c4cd2011-07-17 21:03:04 -07001022 } else {
1023 mWidthGap = mOriginalWidthGap;
1024 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -07001025 }
Michael Jurka5f1c5092010-09-03 14:15:02 -07001026
Michael Jurka8c920dd2011-01-20 14:16:56 -08001027 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
Michael Jurka5f1c5092010-09-03 14:15:02 -07001028 if (widthSpecMode == MeasureSpec.AT_MOST) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001029 newWidth = getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -07001030 ((mCountX - 1) * mWidthGap);
Michael Jurka8b805b12012-04-18 14:23:14 -07001031 newHeight = getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -07001032 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001033 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -07001034 }
Michael Jurka8c920dd2011-01-20 14:16:56 -08001035
1036 int count = getChildCount();
1037 for (int i = 0; i < count; i++) {
1038 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001039 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - getPaddingLeft() -
1040 getPaddingRight(), MeasureSpec.EXACTLY);
1041 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - getPaddingTop() -
1042 getPaddingBottom(), MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -08001043 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
1044 }
1045 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 }
1047
1048 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001049 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001052 View child = getChildAt(i);
Michael Jurka8b805b12012-04-18 14:23:14 -07001053 child.layout(getPaddingLeft(), getPaddingTop(),
1054 r - l - getPaddingRight(), b - t - getPaddingBottom());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 }
1056 }
1057
1058 @Override
Michael Jurkadee05892010-07-27 10:01:56 -07001059 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
1060 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -07001061 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001062 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
Adam Cohen215b4162012-08-30 13:14:08 -07001063 w - mForegroundPadding, h - mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001064 }
1065
1066 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001068 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 }
1070
1071 @Override
1072 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001073 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 }
1075
Michael Jurka5f1c5092010-09-03 14:15:02 -07001076 public float getBackgroundAlpha() {
1077 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001078 }
1079
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001080 public void setBackgroundAlphaMultiplier(float multiplier) {
Michael Jurkaa3d30ad2012-05-08 13:43:43 -07001081 if (mBackgroundAlphaMultiplier != multiplier) {
1082 mBackgroundAlphaMultiplier = multiplier;
1083 invalidate();
1084 }
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001085 }
1086
Adam Cohenddb82192010-11-10 16:32:54 -08001087 public float getBackgroundAlphaMultiplier() {
1088 return mBackgroundAlphaMultiplier;
1089 }
1090
Michael Jurka5f1c5092010-09-03 14:15:02 -07001091 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001092 if (mBackgroundAlpha != alpha) {
1093 mBackgroundAlpha = alpha;
1094 invalidate();
1095 }
Michael Jurkadee05892010-07-27 10:01:56 -07001096 }
1097
Michael Jurkaa52570f2012-03-20 03:18:20 -07001098 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001099 final int childCount = getChildCount();
1100 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001101 getChildAt(i).setAlpha(alpha);
1102 }
1103 }
1104
Michael Jurkaa52570f2012-03-20 03:18:20 -07001105 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1106 if (getChildCount() > 0) {
1107 return (ShortcutAndWidgetContainer) getChildAt(0);
1108 }
1109 return null;
1110 }
1111
Patrick Dubroy440c3602010-07-13 17:50:32 -07001112 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001113 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001114 }
1115
Adam Cohen76fc0852011-06-17 13:26:23 -07001116 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001117 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001118 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001119 boolean[][] occupied = mOccupied;
1120 if (!permanent) {
1121 occupied = mTmpOccupied;
1122 }
1123
Adam Cohen19f37922012-03-21 11:59:11 -07001124 if (clc.indexOfChild(child) != -1) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001125 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1126 final ItemInfo info = (ItemInfo) child.getTag();
1127
1128 // We cancel any existing animations
1129 if (mReorderAnimators.containsKey(lp)) {
1130 mReorderAnimators.get(lp).cancel();
1131 mReorderAnimators.remove(lp);
1132 }
1133
Adam Cohen482ed822012-03-02 14:15:13 -08001134 final int oldX = lp.x;
1135 final int oldY = lp.y;
1136 if (adjustOccupied) {
1137 occupied[lp.cellX][lp.cellY] = false;
1138 occupied[cellX][cellY] = true;
1139 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001140 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001141 if (permanent) {
1142 lp.cellX = info.cellX = cellX;
1143 lp.cellY = info.cellY = cellY;
1144 } else {
1145 lp.tmpCellX = cellX;
1146 lp.tmpCellY = cellY;
1147 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001148 clc.setupLp(lp);
1149 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001150 final int newX = lp.x;
1151 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001152
Adam Cohen76fc0852011-06-17 13:26:23 -07001153 lp.x = oldX;
1154 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001155
Adam Cohen482ed822012-03-02 14:15:13 -08001156 // Exit early if we're not actually moving the view
1157 if (oldX == newX && oldY == newY) {
1158 lp.isLockedToGrid = true;
1159 return true;
1160 }
1161
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001162 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohen482ed822012-03-02 14:15:13 -08001163 va.setDuration(duration);
1164 mReorderAnimators.put(lp, va);
1165
1166 va.addUpdateListener(new AnimatorUpdateListener() {
1167 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001168 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001169 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohen19f37922012-03-21 11:59:11 -07001170 lp.x = (int) ((1 - r) * oldX + r * newX);
1171 lp.y = (int) ((1 - r) * oldY + r * newY);
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001172 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001173 }
1174 });
Adam Cohen482ed822012-03-02 14:15:13 -08001175 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001176 boolean cancelled = false;
1177 public void onAnimationEnd(Animator animation) {
1178 // If the animation was cancelled, it means that another animation
1179 // has interrupted this one, and we don't want to lock the item into
1180 // place just yet.
1181 if (!cancelled) {
1182 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001183 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001184 }
1185 if (mReorderAnimators.containsKey(lp)) {
1186 mReorderAnimators.remove(lp);
1187 }
1188 }
1189 public void onAnimationCancel(Animator animation) {
1190 cancelled = true;
1191 }
1192 });
Adam Cohen482ed822012-03-02 14:15:13 -08001193 va.setStartDelay(delay);
1194 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001195 return true;
1196 }
1197 return false;
1198 }
1199
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001200 /**
1201 * Estimate where the top left cell of the dragged item will land if it is dropped.
1202 *
1203 * @param originX The X value of the top left corner of the item
1204 * @param originY The Y value of the top left corner of the item
1205 * @param spanX The number of horizontal cells that the item spans
1206 * @param spanY The number of vertical cells that the item spans
1207 * @param result The estimated drop cell X and Y.
1208 */
1209 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001210 final int countX = mCountX;
1211 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001212
Michael Jurkaa63c4522010-08-19 13:52:27 -07001213 // pointToCellRounded takes the top left of a cell but will pad that with
1214 // cellWidth/2 and cellHeight/2 when finding the matching cell
1215 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001216
1217 // If the item isn't fully on this screen, snap to the edges
1218 int rightOverhang = result[0] + spanX - countX;
1219 if (rightOverhang > 0) {
1220 result[0] -= rightOverhang; // Snap to right
1221 }
1222 result[0] = Math.max(0, result[0]); // Snap to left
1223 int bottomOverhang = result[1] + spanY - countY;
1224 if (bottomOverhang > 0) {
1225 result[1] -= bottomOverhang; // Snap to bottom
1226 }
1227 result[1] = Math.max(0, result[1]); // Snap to top
1228 }
1229
Adam Cohen482ed822012-03-02 14:15:13 -08001230 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1231 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001232 final int oldDragCellX = mDragCell[0];
1233 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001234
Winson Chungb8c69f32011-10-19 21:36:08 -07001235 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001236 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1237 } else {
1238 mDragCenter.set(originX, originY);
1239 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001240
Adam Cohen2801caf2011-05-13 20:57:39 -07001241 if (dragOutline == null && v == null) {
Adam Cohen2801caf2011-05-13 20:57:39 -07001242 return;
1243 }
1244
Adam Cohen482ed822012-03-02 14:15:13 -08001245 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1246 mDragCell[0] = cellX;
1247 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001248 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001249 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001250 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001251
Joe Onorato4be866d2010-10-10 11:26:02 -07001252 int left = topLeft[0];
1253 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001254
Winson Chungb8c69f32011-10-19 21:36:08 -07001255 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001256 // When drawing the drag outline, it did not account for margin offsets
1257 // added by the view's parent.
1258 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1259 left += lp.leftMargin;
1260 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001261
Adam Cohen99e8b402011-03-25 19:23:43 -07001262 // Offsets due to the size difference between the View and the dragOutline.
1263 // There is a size difference to account for the outer blur, which may lie
1264 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001265 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001266 // We center about the x axis
1267 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1268 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001269 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001270 if (dragOffset != null && dragRegion != null) {
1271 // Center the drag region *horizontally* in the cell and apply a drag
1272 // outline offset
1273 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1274 - dragRegion.width()) / 2;
1275 top += dragOffset.y;
1276 } else {
1277 // Center the drag outline in the cell
1278 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1279 - dragOutline.getWidth()) / 2;
1280 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1281 - dragOutline.getHeight()) / 2;
1282 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001283 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001284 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001285 mDragOutlineAnims[oldIndex].animateOut();
1286 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001287 Rect r = mDragOutlines[mDragOutlineCurrent];
1288 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1289 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001290 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001291 }
Winson Chung150fbab2010-09-29 17:14:26 -07001292
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001293 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1294 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001295 }
1296 }
1297
Adam Cohene0310962011-04-18 16:15:31 -07001298 public void clearDragOutlines() {
1299 final int oldIndex = mDragOutlineCurrent;
1300 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001301 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001302 }
1303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001305 * Find a vacant area that will fit the given bounds nearest the requested
1306 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001307 *
Romain Guy51afc022009-05-04 18:03:43 -07001308 * @param pixelX The X location at which you want to search for a vacant area.
1309 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001310 * @param spanX Horizontal span of the object.
1311 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001312 * @param result Array in which to place the result, or null (in which case a new array will
1313 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001314 * @return The X, Y cell of a vacant area that can contain this object,
1315 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001317 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1318 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001319 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001320 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001321
Michael Jurka6a1435d2010-09-27 17:35:12 -07001322 /**
1323 * Find a vacant area that will fit the given bounds nearest the requested
1324 * cell location. Uses Euclidean distance to score multiple vacant areas.
1325 *
1326 * @param pixelX The X location at which you want to search for a vacant area.
1327 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001328 * @param minSpanX The minimum horizontal span required
1329 * @param minSpanY The minimum vertical span required
1330 * @param spanX Horizontal span of the object.
1331 * @param spanY Vertical span of the object.
1332 * @param result Array in which to place the result, or null (in which case a new array will
1333 * be allocated)
1334 * @return The X, Y cell of a vacant area that can contain this object,
1335 * nearest the requested location.
1336 */
1337 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1338 int spanY, int[] result, int[] resultSpan) {
1339 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1340 result, resultSpan);
1341 }
1342
1343 /**
1344 * Find a vacant area that will fit the given bounds nearest the requested
1345 * cell location. Uses Euclidean distance to score multiple vacant areas.
1346 *
1347 * @param pixelX The X location at which you want to search for a vacant area.
1348 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001349 * @param spanX Horizontal span of the object.
1350 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001351 * @param ignoreOccupied If true, the result can be an occupied cell
1352 * @param result Array in which to place the result, or null (in which case a new array will
1353 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001354 * @return The X, Y cell of a vacant area that can contain this object,
1355 * nearest the requested location.
1356 */
Adam Cohendf035382011-04-11 17:22:04 -07001357 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1358 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001359 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001360 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001361 }
1362
1363 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1364 private void lazyInitTempRectStack() {
1365 if (mTempRectStack.isEmpty()) {
1366 for (int i = 0; i < mCountX * mCountY; i++) {
1367 mTempRectStack.push(new Rect());
1368 }
1369 }
1370 }
Adam Cohen482ed822012-03-02 14:15:13 -08001371
Adam Cohend41fbf52012-02-16 23:53:59 -08001372 private void recycleTempRects(Stack<Rect> used) {
1373 while (!used.isEmpty()) {
1374 mTempRectStack.push(used.pop());
1375 }
1376 }
1377
1378 /**
1379 * Find a vacant area that will fit the given bounds nearest the requested
1380 * cell location. Uses Euclidean distance to score multiple vacant areas.
1381 *
1382 * @param pixelX The X location at which you want to search for a vacant area.
1383 * @param pixelY The Y location at which you want to search for a vacant area.
1384 * @param minSpanX The minimum horizontal span required
1385 * @param minSpanY The minimum vertical span required
1386 * @param spanX Horizontal span of the object.
1387 * @param spanY Vertical span of the object.
1388 * @param ignoreOccupied If true, the result can be an occupied cell
1389 * @param result Array in which to place the result, or null (in which case a new array will
1390 * be allocated)
1391 * @return The X, Y cell of a vacant area that can contain this object,
1392 * nearest the requested location.
1393 */
1394 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001395 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1396 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001397 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001398 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001399 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001400
Adam Cohene3e27a82011-04-15 12:07:39 -07001401 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1402 // to the center of the item, but we are searching based on the top-left cell, so
1403 // we translate the point over to correspond to the top-left.
1404 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1405 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1406
Jeff Sharkey70864282009-04-07 21:08:40 -07001407 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001408 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001409 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001410 final Rect bestRect = new Rect(-1, -1, -1, -1);
1411 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001412
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001413 final int countX = mCountX;
1414 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001415
Adam Cohend41fbf52012-02-16 23:53:59 -08001416 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1417 spanX < minSpanX || spanY < minSpanY) {
1418 return bestXY;
1419 }
1420
1421 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001422 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001423 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1424 int ySize = -1;
1425 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001426 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001427 // First, let's see if this thing fits anywhere
1428 for (int i = 0; i < minSpanX; i++) {
1429 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001430 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001431 continue inner;
1432 }
Michael Jurkac28de512010-08-13 11:27:44 -07001433 }
1434 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001435 xSize = minSpanX;
1436 ySize = minSpanY;
1437
1438 // We know that the item will fit at _some_ acceptable size, now let's see
1439 // how big we can make it. We'll alternate between incrementing x and y spans
1440 // until we hit a limit.
1441 boolean incX = true;
1442 boolean hitMaxX = xSize >= spanX;
1443 boolean hitMaxY = ySize >= spanY;
1444 while (!(hitMaxX && hitMaxY)) {
1445 if (incX && !hitMaxX) {
1446 for (int j = 0; j < ySize; j++) {
1447 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1448 // We can't move out horizontally
1449 hitMaxX = true;
1450 }
1451 }
1452 if (!hitMaxX) {
1453 xSize++;
1454 }
1455 } else if (!hitMaxY) {
1456 for (int i = 0; i < xSize; i++) {
1457 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1458 // We can't move out vertically
1459 hitMaxY = true;
1460 }
1461 }
1462 if (!hitMaxY) {
1463 ySize++;
1464 }
1465 }
1466 hitMaxX |= xSize >= spanX;
1467 hitMaxY |= ySize >= spanY;
1468 incX = !incX;
1469 }
1470 incX = true;
1471 hitMaxX = xSize >= spanX;
1472 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001473 }
Winson Chung0be025d2011-05-23 17:45:09 -07001474 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001475 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476
Adam Cohend41fbf52012-02-16 23:53:59 -08001477 // We verify that the current rect is not a sub-rect of any of our previous
1478 // candidates. In this case, the current rect is disqualified in favour of the
1479 // containing rect.
1480 Rect currentRect = mTempRectStack.pop();
1481 currentRect.set(x, y, x + xSize, y + ySize);
1482 boolean contained = false;
1483 for (Rect r : validRegions) {
1484 if (r.contains(currentRect)) {
1485 contained = true;
1486 break;
1487 }
1488 }
1489 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001490 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1491 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001492
Adam Cohend41fbf52012-02-16 23:53:59 -08001493 if ((distance <= bestDistance && !contained) ||
1494 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001495 bestDistance = distance;
1496 bestXY[0] = x;
1497 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001498 if (resultSpan != null) {
1499 resultSpan[0] = xSize;
1500 resultSpan[1] = ySize;
1501 }
1502 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001503 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 }
1505 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001506 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001507 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508
Adam Cohenc0dcf592011-06-01 15:30:43 -07001509 // Return -1, -1 if no suitable location found
1510 if (bestDistance == Double.MAX_VALUE) {
1511 bestXY[0] = -1;
1512 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001513 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001514 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001515 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001517
Adam Cohen482ed822012-03-02 14:15:13 -08001518 /**
1519 * Find a vacant area that will fit the given bounds nearest the requested
1520 * cell location, and will also weigh in a suggested direction vector of the
1521 * desired location. This method computers distance based on unit grid distances,
1522 * not pixel distances.
1523 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001524 * @param cellX The X cell nearest to which you want to search for a vacant area.
1525 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001526 * @param spanX Horizontal span of the object.
1527 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001528 * @param direction The favored direction in which the views should move from x, y
1529 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1530 * matches exactly. Otherwise we find the best matching direction.
1531 * @param occoupied The array which represents which cells in the CellLayout are occupied
1532 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1533 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001534 * @param result Array in which to place the result, or null (in which case a new array will
1535 * be allocated)
1536 * @return The X, Y cell of a vacant area that can contain this object,
1537 * nearest the requested location.
1538 */
1539 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001540 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001541 // Keep track of best-scoring drop area
1542 final int[] bestXY = result != null ? result : new int[2];
1543 float bestDistance = Float.MAX_VALUE;
1544 int bestDirectionScore = Integer.MIN_VALUE;
1545
1546 final int countX = mCountX;
1547 final int countY = mCountY;
1548
1549 for (int y = 0; y < countY - (spanY - 1); y++) {
1550 inner:
1551 for (int x = 0; x < countX - (spanX - 1); x++) {
1552 // First, let's see if this thing fits anywhere
1553 for (int i = 0; i < spanX; i++) {
1554 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001555 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001556 continue inner;
1557 }
1558 }
1559 }
1560
1561 float distance = (float)
1562 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1563 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001564 computeDirectionVector(x - cellX, y - cellY, curDirection);
1565 // The direction score is just the dot product of the two candidate direction
1566 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001567 int curDirectionScore = direction[0] * curDirection[0] +
1568 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001569 boolean exactDirectionOnly = false;
1570 boolean directionMatches = direction[0] == curDirection[0] &&
1571 direction[0] == curDirection[0];
1572 if ((directionMatches || !exactDirectionOnly) &&
1573 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001574 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1575 bestDistance = distance;
1576 bestDirectionScore = curDirectionScore;
1577 bestXY[0] = x;
1578 bestXY[1] = y;
1579 }
1580 }
1581 }
1582
1583 // Return -1, -1 if no suitable location found
1584 if (bestDistance == Float.MAX_VALUE) {
1585 bestXY[0] = -1;
1586 bestXY[1] = -1;
1587 }
1588 return bestXY;
1589 }
1590
1591 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001592 int[] direction, ItemConfiguration currentState) {
1593 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001594 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001595 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001596 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1597
Adam Cohen8baab352012-03-20 17:39:21 -07001598 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001599
1600 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001601 c.x = mTempLocation[0];
1602 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001603 success = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001604 }
Adam Cohen8baab352012-03-20 17:39:21 -07001605 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001606 return success;
1607 }
1608
Adam Cohenf3900c22012-11-16 18:28:11 -08001609 /**
1610 * This helper class defines a cluster of views. It helps with defining complex edges
1611 * of the cluster and determining how those edges interact with other views. The edges
1612 * essentially define a fine-grained boundary around the cluster of views -- like a more
1613 * precise version of a bounding box.
1614 */
1615 private class ViewCluster {
1616 final static int LEFT = 0;
1617 final static int TOP = 1;
1618 final static int RIGHT = 2;
1619 final static int BOTTOM = 3;
Adam Cohen47a876d2012-03-19 13:21:41 -07001620
Adam Cohenf3900c22012-11-16 18:28:11 -08001621 ArrayList<View> views;
1622 ItemConfiguration config;
1623 Rect boundingRect = new Rect();
Adam Cohen47a876d2012-03-19 13:21:41 -07001624
Adam Cohenf3900c22012-11-16 18:28:11 -08001625 int[] leftEdge = new int[mCountY];
1626 int[] rightEdge = new int[mCountY];
1627 int[] topEdge = new int[mCountX];
1628 int[] bottomEdge = new int[mCountX];
1629 boolean leftEdgeDirty, rightEdgeDirty, topEdgeDirty, bottomEdgeDirty, boundingRectDirty;
1630
1631 @SuppressWarnings("unchecked")
1632 public ViewCluster(ArrayList<View> views, ItemConfiguration config) {
1633 this.views = (ArrayList<View>) views.clone();
1634 this.config = config;
1635 resetEdges();
Adam Cohen47a876d2012-03-19 13:21:41 -07001636 }
1637
Adam Cohenf3900c22012-11-16 18:28:11 -08001638 void resetEdges() {
1639 for (int i = 0; i < mCountX; i++) {
1640 topEdge[i] = -1;
1641 bottomEdge[i] = -1;
1642 }
1643 for (int i = 0; i < mCountY; i++) {
1644 leftEdge[i] = -1;
1645 rightEdge[i] = -1;
1646 }
1647 leftEdgeDirty = true;
1648 rightEdgeDirty = true;
1649 bottomEdgeDirty = true;
1650 topEdgeDirty = true;
1651 boundingRectDirty = true;
1652 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001653
Adam Cohenf3900c22012-11-16 18:28:11 -08001654 void computeEdge(int which, int[] edge) {
1655 int count = views.size();
1656 for (int i = 0; i < count; i++) {
1657 CellAndSpan cs = config.map.get(views.get(i));
1658 switch (which) {
1659 case LEFT:
1660 int left = cs.x;
1661 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1662 if (left < edge[j] || edge[j] < 0) {
1663 edge[j] = left;
Adam Cohena56dc102012-07-13 13:41:42 -07001664 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001665 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001666 break;
1667 case RIGHT:
1668 int right = cs.x + cs.spanX;
1669 for (int j = cs.y; j < cs.y + cs.spanY; j++) {
1670 if (right > edge[j]) {
1671 edge[j] = right;
1672 }
1673 }
1674 break;
1675 case TOP:
1676 int top = cs.y;
1677 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1678 if (top < edge[j] || edge[j] < 0) {
1679 edge[j] = top;
1680 }
1681 }
1682 break;
1683 case BOTTOM:
1684 int bottom = cs.y + cs.spanY;
1685 for (int j = cs.x; j < cs.x + cs.spanX; j++) {
1686 if (bottom > edge[j]) {
1687 edge[j] = bottom;
1688 }
1689 }
1690 break;
Adam Cohen47a876d2012-03-19 13:21:41 -07001691 }
1692 }
1693 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001694
1695 boolean isViewTouchingEdge(View v, int whichEdge) {
1696 CellAndSpan cs = config.map.get(v);
1697
1698 int[] edge = getEdge(whichEdge);
1699
1700 switch (whichEdge) {
1701 case LEFT:
1702 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1703 if (edge[i] == cs.x + cs.spanX) {
1704 return true;
1705 }
1706 }
1707 break;
1708 case RIGHT:
1709 for (int i = cs.y; i < cs.y + cs.spanY; i++) {
1710 if (edge[i] == cs.x) {
1711 return true;
1712 }
1713 }
1714 break;
1715 case TOP:
1716 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1717 if (edge[i] == cs.y + cs.spanY) {
1718 return true;
1719 }
1720 }
1721 break;
1722 case BOTTOM:
1723 for (int i = cs.x; i < cs.x + cs.spanX; i++) {
1724 if (edge[i] == cs.y) {
1725 return true;
1726 }
1727 }
1728 break;
1729 }
1730 return false;
1731 }
1732
1733 void shift(int whichEdge, int delta) {
1734 for (View v: views) {
1735 CellAndSpan c = config.map.get(v);
1736 switch (whichEdge) {
1737 case LEFT:
1738 c.x -= delta;
1739 break;
1740 case RIGHT:
1741 c.x += delta;
1742 break;
1743 case TOP:
1744 c.y -= delta;
1745 break;
1746 case BOTTOM:
1747 default:
1748 c.y += delta;
1749 break;
1750 }
1751 }
1752 resetEdges();
1753 }
1754
1755 public void addView(View v) {
1756 views.add(v);
1757 resetEdges();
1758 }
1759
1760 public Rect getBoundingRect() {
1761 if (boundingRectDirty) {
1762 boolean first = true;
1763 for (View v: views) {
1764 CellAndSpan c = config.map.get(v);
1765 if (first) {
1766 boundingRect.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1767 first = false;
1768 } else {
1769 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
1770 }
1771 }
1772 }
1773 return boundingRect;
1774 }
1775
1776 public int[] getEdge(int which) {
1777 switch (which) {
1778 case LEFT:
1779 return getLeftEdge();
1780 case RIGHT:
1781 return getRightEdge();
1782 case TOP:
1783 return getTopEdge();
1784 case BOTTOM:
1785 default:
1786 return getBottomEdge();
1787 }
1788 }
1789
1790 public int[] getLeftEdge() {
1791 if (leftEdgeDirty) {
1792 computeEdge(LEFT, leftEdge);
1793 }
1794 return leftEdge;
1795 }
1796
1797 public int[] getRightEdge() {
1798 if (rightEdgeDirty) {
1799 computeEdge(RIGHT, rightEdge);
1800 }
1801 return rightEdge;
1802 }
1803
1804 public int[] getTopEdge() {
1805 if (topEdgeDirty) {
1806 computeEdge(TOP, topEdge);
1807 }
1808 return topEdge;
1809 }
1810
1811 public int[] getBottomEdge() {
1812 if (bottomEdgeDirty) {
1813 computeEdge(BOTTOM, bottomEdge);
1814 }
1815 return bottomEdge;
1816 }
1817
1818 PositionComparator comparator = new PositionComparator();
1819 class PositionComparator implements Comparator<View> {
1820 int whichEdge = 0;
1821 public int compare(View left, View right) {
1822 CellAndSpan l = config.map.get(left);
1823 CellAndSpan r = config.map.get(right);
1824 switch (whichEdge) {
1825 case LEFT:
1826 return (r.x + r.spanX) - (l.x + l.spanX);
1827 case RIGHT:
1828 return l.x - r.x;
1829 case TOP:
1830 return (r.y + r.spanY) - (l.y + l.spanY);
1831 case BOTTOM:
1832 default:
1833 return l.y - r.y;
1834 }
1835 }
1836 }
1837
1838 public void sortConfigurationForEdgePush(int edge) {
1839 comparator.whichEdge = edge;
1840 Collections.sort(config.sortedViews, comparator);
1841 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001842 }
1843
Adam Cohenf3900c22012-11-16 18:28:11 -08001844 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1845 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohene0489502012-08-27 15:18:53 -07001846
Adam Cohenf3900c22012-11-16 18:28:11 -08001847 ViewCluster cluster = new ViewCluster(views, currentState);
1848 Rect clusterRect = cluster.getBoundingRect();
1849 int whichEdge;
1850 int pushDistance;
1851 boolean fail = false;
1852
1853 // Determine the edge of the cluster that will be leading the push and how far
1854 // the cluster must be shifted.
1855 if (direction[0] < 0) {
1856 whichEdge = ViewCluster.LEFT;
1857 pushDistance = clusterRect.right - rectOccupiedByPotentialDrop.left;
Adam Cohene0489502012-08-27 15:18:53 -07001858 } else if (direction[0] > 0) {
Adam Cohenf3900c22012-11-16 18:28:11 -08001859 whichEdge = ViewCluster.RIGHT;
1860 pushDistance = rectOccupiedByPotentialDrop.right - clusterRect.left;
1861 } else if (direction[1] < 0) {
1862 whichEdge = ViewCluster.TOP;
1863 pushDistance = clusterRect.bottom - rectOccupiedByPotentialDrop.top;
1864 } else {
1865 whichEdge = ViewCluster.BOTTOM;
1866 pushDistance = rectOccupiedByPotentialDrop.bottom - clusterRect.top;
Adam Cohene0489502012-08-27 15:18:53 -07001867 }
1868
Adam Cohenf3900c22012-11-16 18:28:11 -08001869 // Break early for invalid push distance.
1870 if (pushDistance <= 0) {
1871 return false;
Adam Cohene0489502012-08-27 15:18:53 -07001872 }
Adam Cohenf3900c22012-11-16 18:28:11 -08001873
1874 // Mark the occupied state as false for the group of views we want to move.
1875 for (View v: views) {
1876 CellAndSpan c = currentState.map.get(v);
1877 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1878 }
1879
1880 // We save the current configuration -- if we fail to find a solution we will revert
1881 // to the initial state. The process of finding a solution modifies the configuration
1882 // in place, hence the need for revert in the failure case.
1883 currentState.save();
1884
1885 // The pushing algorithm is simplified by considering the views in the order in which
1886 // they would be pushed by the cluster. For example, if the cluster is leading with its
1887 // left edge, we consider sort the views by their right edge, from right to left.
1888 cluster.sortConfigurationForEdgePush(whichEdge);
1889
1890 while (pushDistance > 0 && !fail) {
1891 for (View v: currentState.sortedViews) {
1892 // For each view that isn't in the cluster, we see if the leading edge of the
1893 // cluster is contacting the edge of that view. If so, we add that view to the
1894 // cluster.
1895 if (!cluster.views.contains(v) && v != dragView) {
1896 if (cluster.isViewTouchingEdge(v, whichEdge)) {
1897 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1898 if (!lp.canReorder) {
1899 // The push solution includes the all apps button, this is not viable.
1900 fail = true;
1901 break;
1902 }
1903 cluster.addView(v);
1904 CellAndSpan c = currentState.map.get(v);
1905
1906 // Adding view to cluster, mark it as not occupied.
1907 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1908 }
1909 }
1910 }
1911 pushDistance--;
1912
1913 // The cluster has been completed, now we move the whole thing over in the appropriate
1914 // direction.
1915 cluster.shift(whichEdge, 1);
1916 }
1917
1918 boolean foundSolution = false;
1919 clusterRect = cluster.getBoundingRect();
1920
1921 // Due to the nature of the algorithm, the only check required to verify a valid solution
1922 // is to ensure that completed shifted cluster lies completely within the cell layout.
1923 if (!fail && clusterRect.left >= 0 && clusterRect.right <= mCountX && clusterRect.top >= 0 &&
1924 clusterRect.bottom <= mCountY) {
1925 foundSolution = true;
1926 } else {
1927 currentState.restore();
1928 }
1929
1930 // In either case, we set the occupied array as marked for the location of the views
1931 for (View v: cluster.views) {
1932 CellAndSpan c = currentState.map.get(v);
1933 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
1934 }
1935
1936 return foundSolution;
Adam Cohene0489502012-08-27 15:18:53 -07001937 }
1938
Adam Cohen482ed822012-03-02 14:15:13 -08001939 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohenf3900c22012-11-16 18:28:11 -08001940 int[] direction, View dragView, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001941 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001942
Adam Cohen8baab352012-03-20 17:39:21 -07001943 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001944 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001945 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001946 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001947 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001948 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001949 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001950 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001951 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001952 }
1953 }
Adam Cohen8baab352012-03-20 17:39:21 -07001954
Adam Cohen8baab352012-03-20 17:39:21 -07001955 // Mark the occupied state as false for the group of views we want to move.
Adam Cohenf3900c22012-11-16 18:28:11 -08001956 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001957 CellAndSpan c = currentState.map.get(v);
1958 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1959 }
1960
Adam Cohen47a876d2012-03-19 13:21:41 -07001961 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1962 int top = boundingRect.top;
1963 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001964 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
Adam Cohena56dc102012-07-13 13:41:42 -07001965 // for interlocking.
Adam Cohenf3900c22012-11-16 18:28:11 -08001966 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001967 CellAndSpan c = currentState.map.get(v);
1968 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001969 }
1970
Adam Cohen482ed822012-03-02 14:15:13 -08001971 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1972
Adam Cohenf3900c22012-11-16 18:28:11 -08001973 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1974 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001975
Adam Cohen8baab352012-03-20 17:39:21 -07001976 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001977 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001978 int deltaX = mTempLocation[0] - boundingRect.left;
1979 int deltaY = mTempLocation[1] - boundingRect.top;
Adam Cohenf3900c22012-11-16 18:28:11 -08001980 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001981 CellAndSpan c = currentState.map.get(v);
1982 c.x += deltaX;
1983 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001984 }
1985 success = true;
1986 }
Adam Cohen8baab352012-03-20 17:39:21 -07001987
1988 // In either case, we set the occupied array as marked for the location of the views
Adam Cohenf3900c22012-11-16 18:28:11 -08001989 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001990 CellAndSpan c = currentState.map.get(v);
1991 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001992 }
1993 return success;
1994 }
1995
1996 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1997 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1998 }
1999
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002000 // This method tries to find a reordering solution which satisfies the push mechanic by trying
2001 // to push items in each of the cardinal directions, in an order based on the direction vector
2002 // passed.
2003 private boolean attemptPushInDirection(ArrayList<View> intersectingViews, Rect occupied,
2004 int[] direction, View ignoreView, ItemConfiguration solution) {
2005 if ((Math.abs(direction[0]) + Math.abs(direction[1])) > 1) {
2006 // If the direction vector has two non-zero components, we try pushing
2007 // separately in each of the components.
2008 int temp = direction[1];
2009 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002010
2011 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002012 ignoreView, solution)) {
2013 return true;
2014 }
2015 direction[1] = temp;
2016 temp = direction[0];
2017 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002018
2019 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002020 ignoreView, solution)) {
2021 return true;
2022 }
2023 // Revert the direction
2024 direction[0] = temp;
2025
2026 // Now we try pushing in each component of the opposite direction
2027 direction[0] *= -1;
2028 direction[1] *= -1;
2029 temp = direction[1];
2030 direction[1] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002031 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002032 ignoreView, solution)) {
2033 return true;
2034 }
2035
2036 direction[1] = temp;
2037 temp = direction[0];
2038 direction[0] = 0;
Adam Cohenf3900c22012-11-16 18:28:11 -08002039 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002040 ignoreView, solution)) {
2041 return true;
2042 }
2043 // revert the direction
2044 direction[0] = temp;
2045 direction[0] *= -1;
2046 direction[1] *= -1;
2047
2048 } else {
2049 // If the direction vector has a single non-zero component, we push first in the
2050 // direction of the vector
Adam Cohenf3900c22012-11-16 18:28:11 -08002051 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002052 ignoreView, solution)) {
2053 return true;
2054 }
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002055 // Then we try the opposite direction
2056 direction[0] *= -1;
2057 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002058 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002059 ignoreView, solution)) {
2060 return true;
2061 }
2062 // Switch the direction back
2063 direction[0] *= -1;
2064 direction[1] *= -1;
2065
2066 // If we have failed to find a push solution with the above, then we try
2067 // to find a solution by pushing along the perpendicular axis.
2068
2069 // Swap the components
2070 int temp = direction[1];
2071 direction[1] = direction[0];
2072 direction[0] = temp;
Adam Cohenf3900c22012-11-16 18:28:11 -08002073 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002074 ignoreView, solution)) {
2075 return true;
2076 }
2077
2078 // Then we try the opposite direction
2079 direction[0] *= -1;
2080 direction[1] *= -1;
Adam Cohenf3900c22012-11-16 18:28:11 -08002081 if (pushViewsToTempLocation(intersectingViews, occupied, direction,
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002082 ignoreView, solution)) {
2083 return true;
2084 }
2085 // Switch the direction back
2086 direction[0] *= -1;
2087 direction[1] *= -1;
2088
2089 // Swap the components back
2090 temp = direction[1];
2091 direction[1] = direction[0];
2092 direction[0] = temp;
2093 }
2094 return false;
2095 }
2096
Adam Cohen482ed822012-03-02 14:15:13 -08002097 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07002098 View ignoreView, ItemConfiguration solution) {
Winson Chunge3e03bc2012-05-01 15:10:11 -07002099 // Return early if get invalid cell positions
2100 if (cellX < 0 || cellY < 0) return false;
Adam Cohen482ed822012-03-02 14:15:13 -08002101
Adam Cohen8baab352012-03-20 17:39:21 -07002102 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08002103 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002104
Adam Cohen8baab352012-03-20 17:39:21 -07002105 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08002106 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07002107 CellAndSpan c = solution.map.get(ignoreView);
Adam Cohen19f37922012-03-21 11:59:11 -07002108 if (c != null) {
2109 c.x = cellX;
2110 c.y = cellY;
2111 }
Adam Cohen482ed822012-03-02 14:15:13 -08002112 }
Adam Cohen482ed822012-03-02 14:15:13 -08002113 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2114 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07002115 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08002116 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002117 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002118 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002119 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08002120 if (Rect.intersects(r0, r1)) {
2121 if (!lp.canReorder) {
2122 return false;
2123 }
2124 mIntersectingViews.add(child);
2125 }
2126 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002127
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002128 // First we try to find a solution which respects the push mechanic. That is,
2129 // we try to find a solution such that no displaced item travels through another item
2130 // without also displacing that item.
2131 if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002132 solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07002133 return true;
2134 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002135
Adam Cohen4abc5bd2012-05-29 21:06:03 -07002136 // Next we try moving the views as a block, but without requiring the push mechanic.
Adam Cohenf3900c22012-11-16 18:28:11 -08002137 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView,
Adam Cohen19f37922012-03-21 11:59:11 -07002138 solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002139 return true;
2140 }
Adam Cohen47a876d2012-03-19 13:21:41 -07002141
Adam Cohen482ed822012-03-02 14:15:13 -08002142 // Ok, they couldn't move as a block, let's move them individually
2143 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07002144 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002145 return false;
2146 }
2147 }
2148 return true;
2149 }
2150
2151 /*
2152 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
2153 * the provided point and the provided cell
2154 */
Adam Cohen47a876d2012-03-19 13:21:41 -07002155 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08002156 double angle = Math.atan(((float) deltaY) / deltaX);
2157
2158 result[0] = 0;
2159 result[1] = 0;
2160 if (Math.abs(Math.cos(angle)) > 0.5f) {
2161 result[0] = (int) Math.signum(deltaX);
2162 }
2163 if (Math.abs(Math.sin(angle)) > 0.5f) {
2164 result[1] = (int) Math.signum(deltaY);
2165 }
2166 }
2167
Adam Cohen8baab352012-03-20 17:39:21 -07002168 private void copyOccupiedArray(boolean[][] occupied) {
2169 for (int i = 0; i < mCountX; i++) {
2170 for (int j = 0; j < mCountY; j++) {
2171 occupied[i][j] = mOccupied[i][j];
2172 }
2173 }
2174 }
2175
Adam Cohen482ed822012-03-02 14:15:13 -08002176 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
2177 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07002178 // Copy the current state into the solution. This solution will be manipulated as necessary.
2179 copyCurrentStateToSolution(solution, false);
2180 // Copy the current occupied array into the temporary occupied array. This array will be
2181 // manipulated as necessary to find a solution.
2182 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08002183
2184 // We find the nearest cell into which we would place the dragged item, assuming there's
2185 // nothing in its way.
2186 int result[] = new int[2];
2187 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2188
2189 boolean success = false;
2190 // First we try the exact nearest position of the item being dragged,
2191 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07002192 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
2193 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08002194
2195 if (!success) {
2196 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
2197 // x, then 1 in y etc.
2198 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
2199 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
2200 dragView, false, solution);
2201 } else if (spanY > minSpanY) {
2202 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
2203 dragView, true, solution);
2204 }
2205 solution.isSolution = false;
2206 } else {
2207 solution.isSolution = true;
2208 solution.dragViewX = result[0];
2209 solution.dragViewY = result[1];
2210 solution.dragViewSpanX = spanX;
2211 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002212 }
2213 return solution;
2214 }
2215
2216 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002217 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002218 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002219 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002220 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002221 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08002222 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07002223 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002224 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07002225 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08002226 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002227 solution.add(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08002228 }
2229 }
2230
2231 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
2232 for (int i = 0; i < mCountX; i++) {
2233 for (int j = 0; j < mCountY; j++) {
2234 mTmpOccupied[i][j] = false;
2235 }
2236 }
2237
Michael Jurkaa52570f2012-03-20 03:18:20 -07002238 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002239 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002240 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002241 if (child == dragView) continue;
2242 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07002243 CellAndSpan c = solution.map.get(child);
2244 if (c != null) {
2245 lp.tmpCellX = c.x;
2246 lp.tmpCellY = c.y;
2247 lp.cellHSpan = c.spanX;
2248 lp.cellVSpan = c.spanY;
2249 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002250 }
2251 }
2252 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2253 solution.dragViewSpanY, mTmpOccupied, true);
2254 }
2255
2256 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
2257 commitDragView) {
2258
2259 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
2260 for (int i = 0; i < mCountX; i++) {
2261 for (int j = 0; j < mCountY; j++) {
2262 occupied[i][j] = false;
2263 }
2264 }
2265
Michael Jurkaa52570f2012-03-20 03:18:20 -07002266 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002267 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002268 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08002269 if (child == dragView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07002270 CellAndSpan c = solution.map.get(child);
2271 if (c != null) {
Adam Cohen19f37922012-03-21 11:59:11 -07002272 animateChildToPosition(child, c.x, c.y, REORDER_ANIMATION_DURATION, 0,
2273 DESTRUCTIVE_REORDER, false);
Adam Cohen8baab352012-03-20 17:39:21 -07002274 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08002275 }
2276 }
2277 if (commitDragView) {
2278 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
2279 solution.dragViewSpanY, occupied, true);
2280 }
2281 }
2282
Adam Cohen19f37922012-03-21 11:59:11 -07002283 // This method starts or changes the reorder hint animations
2284 private void beginOrAdjustHintAnimations(ItemConfiguration solution, View dragView, int delay) {
2285 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen19f37922012-03-21 11:59:11 -07002286 for (int i = 0; i < childCount; i++) {
2287 View child = mShortcutsAndWidgets.getChildAt(i);
2288 if (child == dragView) continue;
2289 CellAndSpan c = solution.map.get(child);
2290 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2291 if (c != null) {
2292 ReorderHintAnimation rha = new ReorderHintAnimation(child, lp.cellX, lp.cellY,
2293 c.x, c.y, c.spanX, c.spanY);
Adam Cohend024f982012-05-23 18:26:45 -07002294 rha.animate();
Adam Cohen19f37922012-03-21 11:59:11 -07002295 }
2296 }
2297 }
2298
2299 // Class which represents the reorder hint animations. These animations show that an item is
2300 // in a temporary state, and hint at where the item will return to.
2301 class ReorderHintAnimation {
2302 View child;
Adam Cohend024f982012-05-23 18:26:45 -07002303 float finalDeltaX;
2304 float finalDeltaY;
2305 float initDeltaX;
2306 float initDeltaY;
2307 float finalScale;
2308 float initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002309 private static final int DURATION = 300;
Adam Cohene7587d22012-05-24 18:50:02 -07002310 Animator a;
Adam Cohen19f37922012-03-21 11:59:11 -07002311
2312 public ReorderHintAnimation(View child, int cellX0, int cellY0, int cellX1, int cellY1,
2313 int spanX, int spanY) {
2314 regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
2315 final int x0 = mTmpPoint[0];
2316 final int y0 = mTmpPoint[1];
2317 regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
2318 final int x1 = mTmpPoint[0];
2319 final int y1 = mTmpPoint[1];
2320 final int dX = x1 - x0;
2321 final int dY = y1 - y0;
Adam Cohend024f982012-05-23 18:26:45 -07002322 finalDeltaX = 0;
2323 finalDeltaY = 0;
Adam Cohen19f37922012-03-21 11:59:11 -07002324 if (dX == dY && dX == 0) {
2325 } else {
2326 if (dY == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002327 finalDeltaX = - Math.signum(dX) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002328 } else if (dX == 0) {
Adam Cohend024f982012-05-23 18:26:45 -07002329 finalDeltaY = - Math.signum(dY) * mReorderHintAnimationMagnitude;
Adam Cohen19f37922012-03-21 11:59:11 -07002330 } else {
2331 double angle = Math.atan( (float) (dY) / dX);
Adam Cohend024f982012-05-23 18:26:45 -07002332 finalDeltaX = (int) (- Math.signum(dX) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002333 Math.abs(Math.cos(angle) * mReorderHintAnimationMagnitude));
Adam Cohend024f982012-05-23 18:26:45 -07002334 finalDeltaY = (int) (- Math.signum(dY) *
Adam Cohenfe41ac62012-05-23 14:00:37 -07002335 Math.abs(Math.sin(angle) * mReorderHintAnimationMagnitude));
Adam Cohen19f37922012-03-21 11:59:11 -07002336 }
2337 }
Adam Cohend024f982012-05-23 18:26:45 -07002338 initDeltaX = child.getTranslationX();
2339 initDeltaY = child.getTranslationY();
Adam Cohen307fe232012-08-16 17:55:58 -07002340 finalScale = getChildrenScale() - 4.0f / child.getWidth();
Adam Cohend024f982012-05-23 18:26:45 -07002341 initScale = child.getScaleX();
Adam Cohen19f37922012-03-21 11:59:11 -07002342 this.child = child;
2343 }
2344
Adam Cohend024f982012-05-23 18:26:45 -07002345 void animate() {
Adam Cohen19f37922012-03-21 11:59:11 -07002346 if (mShakeAnimators.containsKey(child)) {
2347 ReorderHintAnimation oldAnimation = mShakeAnimators.get(child);
Adam Cohend024f982012-05-23 18:26:45 -07002348 oldAnimation.cancel();
Adam Cohen19f37922012-03-21 11:59:11 -07002349 mShakeAnimators.remove(child);
Adam Cohene7587d22012-05-24 18:50:02 -07002350 if (finalDeltaX == 0 && finalDeltaY == 0) {
2351 completeAnimationImmediately();
2352 return;
2353 }
Adam Cohen19f37922012-03-21 11:59:11 -07002354 }
Adam Cohend024f982012-05-23 18:26:45 -07002355 if (finalDeltaX == 0 && finalDeltaY == 0) {
Adam Cohen19f37922012-03-21 11:59:11 -07002356 return;
2357 }
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002358 ValueAnimator va = LauncherAnimUtils.ofFloat(child, 0f, 1f);
Adam Cohene7587d22012-05-24 18:50:02 -07002359 a = va;
Adam Cohen19f37922012-03-21 11:59:11 -07002360 va.setRepeatMode(ValueAnimator.REVERSE);
2361 va.setRepeatCount(ValueAnimator.INFINITE);
Adam Cohen7bdfc972012-05-22 16:50:35 -07002362 va.setDuration(DURATION);
Adam Cohend024f982012-05-23 18:26:45 -07002363 va.setStartDelay((int) (Math.random() * 60));
Adam Cohen19f37922012-03-21 11:59:11 -07002364 va.addUpdateListener(new AnimatorUpdateListener() {
2365 @Override
2366 public void onAnimationUpdate(ValueAnimator animation) {
2367 float r = ((Float) animation.getAnimatedValue()).floatValue();
Adam Cohend024f982012-05-23 18:26:45 -07002368 float x = r * finalDeltaX + (1 - r) * initDeltaX;
2369 float y = r * finalDeltaY + (1 - r) * initDeltaY;
Adam Cohen19f37922012-03-21 11:59:11 -07002370 child.setTranslationX(x);
2371 child.setTranslationY(y);
Adam Cohend024f982012-05-23 18:26:45 -07002372 float s = r * finalScale + (1 - r) * initScale;
Brandon Keely50e6e562012-05-08 16:28:49 -07002373 child.setScaleX(s);
2374 child.setScaleY(s);
Adam Cohen19f37922012-03-21 11:59:11 -07002375 }
2376 });
2377 va.addListener(new AnimatorListenerAdapter() {
2378 public void onAnimationRepeat(Animator animation) {
Adam Cohen19f37922012-03-21 11:59:11 -07002379 // We make sure to end only after a full period
Adam Cohend024f982012-05-23 18:26:45 -07002380 initDeltaX = 0;
2381 initDeltaY = 0;
Adam Cohen307fe232012-08-16 17:55:58 -07002382 initScale = getChildrenScale();
Adam Cohen19f37922012-03-21 11:59:11 -07002383 }
2384 });
Adam Cohen19f37922012-03-21 11:59:11 -07002385 mShakeAnimators.put(child, this);
2386 va.start();
2387 }
2388
Adam Cohend024f982012-05-23 18:26:45 -07002389 private void cancel() {
Adam Cohene7587d22012-05-24 18:50:02 -07002390 if (a != null) {
2391 a.cancel();
2392 }
Adam Cohen19f37922012-03-21 11:59:11 -07002393 }
Adam Cohene7587d22012-05-24 18:50:02 -07002394
Brandon Keely50e6e562012-05-08 16:28:49 -07002395 private void completeAnimationImmediately() {
Adam Cohene7587d22012-05-24 18:50:02 -07002396 if (a != null) {
2397 a.cancel();
2398 }
Brandon Keely50e6e562012-05-08 16:28:49 -07002399
Michael Jurka2ecf9952012-06-18 12:52:28 -07002400 AnimatorSet s = LauncherAnimUtils.createAnimatorSet();
Adam Cohene7587d22012-05-24 18:50:02 -07002401 a = s;
Brandon Keely50e6e562012-05-08 16:28:49 -07002402 s.playTogether(
Adam Cohen307fe232012-08-16 17:55:58 -07002403 LauncherAnimUtils.ofFloat(child, "scaleX", getChildrenScale()),
2404 LauncherAnimUtils.ofFloat(child, "scaleY", getChildrenScale()),
Michael Jurka2ecf9952012-06-18 12:52:28 -07002405 LauncherAnimUtils.ofFloat(child, "translationX", 0f),
2406 LauncherAnimUtils.ofFloat(child, "translationY", 0f)
Brandon Keely50e6e562012-05-08 16:28:49 -07002407 );
2408 s.setDuration(REORDER_ANIMATION_DURATION);
2409 s.setInterpolator(new android.view.animation.DecelerateInterpolator(1.5f));
2410 s.start();
2411 }
Adam Cohen19f37922012-03-21 11:59:11 -07002412 }
2413
2414 private void completeAndClearReorderHintAnimations() {
2415 for (ReorderHintAnimation a: mShakeAnimators.values()) {
Brandon Keely50e6e562012-05-08 16:28:49 -07002416 a.completeAnimationImmediately();
Adam Cohen19f37922012-03-21 11:59:11 -07002417 }
2418 mShakeAnimators.clear();
2419 }
2420
Adam Cohen482ed822012-03-02 14:15:13 -08002421 private void commitTempPlacement() {
2422 for (int i = 0; i < mCountX; i++) {
2423 for (int j = 0; j < mCountY; j++) {
2424 mOccupied[i][j] = mTmpOccupied[i][j];
2425 }
2426 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07002427 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002428 for (int i = 0; i < childCount; i++) {
Adam Cohenea889a22012-03-27 16:45:39 -07002429 View child = mShortcutsAndWidgets.getChildAt(i);
2430 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2431 ItemInfo info = (ItemInfo) child.getTag();
Adam Cohen2acce882012-03-28 19:03:19 -07002432 // We do a null check here because the item info can be null in the case of the
2433 // AllApps button in the hotseat.
2434 if (info != null) {
Adam Cohen487f7dd2012-06-28 18:12:10 -07002435 if (info.cellX != lp.tmpCellX || info.cellY != lp.tmpCellY ||
2436 info.spanX != lp.cellHSpan || info.spanY != lp.cellVSpan) {
2437 info.requiresDbUpdate = true;
2438 }
Adam Cohen2acce882012-03-28 19:03:19 -07002439 info.cellX = lp.cellX = lp.tmpCellX;
2440 info.cellY = lp.cellY = lp.tmpCellY;
Adam Cohenbebf0422012-04-11 18:06:28 -07002441 info.spanX = lp.cellHSpan;
2442 info.spanY = lp.cellVSpan;
Adam Cohen2acce882012-03-28 19:03:19 -07002443 }
Adam Cohen482ed822012-03-02 14:15:13 -08002444 }
Adam Cohen2acce882012-03-28 19:03:19 -07002445 mLauncher.getWorkspace().updateItemLocationsInDatabase(this);
Adam Cohen482ed822012-03-02 14:15:13 -08002446 }
2447
2448 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002449 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08002450 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002451 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002452 lp.useTmpCoords = useTempCoords;
2453 }
2454 }
2455
Adam Cohen482ed822012-03-02 14:15:13 -08002456 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
2457 int spanX, int spanY, View dragView, ItemConfiguration solution) {
2458 int[] result = new int[2];
2459 int[] resultSpan = new int[2];
2460 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
2461 resultSpan);
2462 if (result[0] >= 0 && result[1] >= 0) {
2463 copyCurrentStateToSolution(solution, false);
2464 solution.dragViewX = result[0];
2465 solution.dragViewY = result[1];
2466 solution.dragViewSpanX = resultSpan[0];
2467 solution.dragViewSpanY = resultSpan[1];
2468 solution.isSolution = true;
2469 } else {
2470 solution.isSolution = false;
2471 }
2472 return solution;
2473 }
2474
2475 public void prepareChildForDrag(View child) {
2476 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002477 }
2478
Adam Cohen19f37922012-03-21 11:59:11 -07002479 /* This seems like it should be obvious and straight-forward, but when the direction vector
2480 needs to match with the notion of the dragView pushing other views, we have to employ
2481 a slightly more subtle notion of the direction vector. The question is what two points is
2482 the vector between? The center of the dragView and its desired destination? Not quite, as
2483 this doesn't necessarily coincide with the interaction of the dragView and items occupying
2484 those cells. Instead we use some heuristics to often lock the vector to up, down, left
2485 or right, which helps make pushing feel right.
2486 */
2487 private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX,
2488 int spanY, View dragView, int[] resultDirection) {
2489 int[] targetDestination = new int[2];
2490
2491 findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
2492 Rect dragRect = new Rect();
2493 regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
2494 dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
2495
2496 Rect dropRegionRect = new Rect();
2497 getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY,
2498 dragView, dropRegionRect, mIntersectingViews);
2499
2500 int dropRegionSpanX = dropRegionRect.width();
2501 int dropRegionSpanY = dropRegionRect.height();
2502
2503 regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(),
2504 dropRegionRect.height(), dropRegionRect);
2505
2506 int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
2507 int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;
2508
2509 if (dropRegionSpanX == mCountX || spanX == mCountX) {
2510 deltaX = 0;
2511 }
2512 if (dropRegionSpanY == mCountY || spanY == mCountY) {
2513 deltaY = 0;
2514 }
2515
2516 if (deltaX == 0 && deltaY == 0) {
2517 // No idea what to do, give a random direction.
2518 resultDirection[0] = 1;
2519 resultDirection[1] = 0;
2520 } else {
2521 computeDirectionVector(deltaX, deltaY, resultDirection);
2522 }
2523 }
2524
2525 // For a given cell and span, fetch the set of views intersecting the region.
2526 private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY,
2527 View dragView, Rect boundingRect, ArrayList<View> intersectingViews) {
2528 if (boundingRect != null) {
2529 boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
2530 }
2531 intersectingViews.clear();
2532 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
2533 Rect r1 = new Rect();
2534 final int count = mShortcutsAndWidgets.getChildCount();
2535 for (int i = 0; i < count; i++) {
2536 View child = mShortcutsAndWidgets.getChildAt(i);
2537 if (child == dragView) continue;
2538 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2539 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
2540 if (Rect.intersects(r0, r1)) {
2541 mIntersectingViews.add(child);
2542 if (boundingRect != null) {
2543 boundingRect.union(r1);
2544 }
2545 }
2546 }
2547 }
2548
2549 boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
2550 View dragView, int[] result) {
2551 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
2552 getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
2553 mIntersectingViews);
2554 return !mIntersectingViews.isEmpty();
2555 }
2556
2557 void revertTempState() {
2558 if (!isItemPlacementDirty() || DESTRUCTIVE_REORDER) return;
2559 final int count = mShortcutsAndWidgets.getChildCount();
2560 for (int i = 0; i < count; i++) {
2561 View child = mShortcutsAndWidgets.getChildAt(i);
2562 LayoutParams lp = (LayoutParams) child.getLayoutParams();
2563 if (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY) {
2564 lp.tmpCellX = lp.cellX;
2565 lp.tmpCellY = lp.cellY;
2566 animateChildToPosition(child, lp.cellX, lp.cellY, REORDER_ANIMATION_DURATION,
2567 0, false, false);
2568 }
2569 }
2570 completeAndClearReorderHintAnimations();
2571 setItemPlacementDirty(false);
2572 }
2573
Adam Cohenbebf0422012-04-11 18:06:28 -07002574 boolean createAreaForResize(int cellX, int cellY, int spanX, int spanY,
2575 View dragView, int[] direction, boolean commit) {
2576 int[] pixelXY = new int[2];
2577 regionToCenterPoint(cellX, cellY, spanX, spanY, pixelXY);
2578
2579 // First we determine if things have moved enough to cause a different layout
2580 ItemConfiguration swapSolution = simpleSwap(pixelXY[0], pixelXY[1], spanX, spanY,
2581 spanX, spanY, direction, dragView, true, new ItemConfiguration());
2582
2583 setUseTempCoords(true);
2584 if (swapSolution != null && swapSolution.isSolution) {
2585 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2586 // committing anything or animating anything as we just want to determine if a solution
2587 // exists
2588 copySolutionToTempState(swapSolution, dragView);
2589 setItemPlacementDirty(true);
2590 animateItemsToSolution(swapSolution, dragView, commit);
2591
2592 if (commit) {
2593 commitTempPlacement();
2594 completeAndClearReorderHintAnimations();
2595 setItemPlacementDirty(false);
2596 } else {
2597 beginOrAdjustHintAnimations(swapSolution, dragView,
2598 REORDER_ANIMATION_DURATION);
2599 }
2600 mShortcutsAndWidgets.requestLayout();
2601 }
2602 return swapSolution.isSolution;
2603 }
2604
Adam Cohen482ed822012-03-02 14:15:13 -08002605 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2606 View dragView, int[] result, int resultSpan[], int mode) {
Adam Cohen482ed822012-03-02 14:15:13 -08002607 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002608 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002609
2610 if (resultSpan == null) {
2611 resultSpan = new int[2];
2612 }
2613
Adam Cohen19f37922012-03-21 11:59:11 -07002614 // When we are checking drop validity or actually dropping, we don't recompute the
2615 // direction vector, since we want the solution to match the preview, and it's possible
2616 // that the exact position of the item has changed to result in a new reordering outcome.
Adam Cohenb209e632012-03-27 17:09:36 -07002617 if ((mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL || mode == MODE_ACCEPT_DROP)
2618 && mPreviousReorderDirection[0] != INVALID_DIRECTION) {
Adam Cohen19f37922012-03-21 11:59:11 -07002619 mDirectionVector[0] = mPreviousReorderDirection[0];
2620 mDirectionVector[1] = mPreviousReorderDirection[1];
2621 // We reset this vector after drop
Adam Cohenb209e632012-03-27 17:09:36 -07002622 if (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2623 mPreviousReorderDirection[0] = INVALID_DIRECTION;
2624 mPreviousReorderDirection[1] = INVALID_DIRECTION;
Adam Cohen19f37922012-03-21 11:59:11 -07002625 }
2626 } else {
2627 getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
2628 mPreviousReorderDirection[0] = mDirectionVector[0];
2629 mPreviousReorderDirection[1] = mDirectionVector[1];
2630 }
2631
Adam Cohen482ed822012-03-02 14:15:13 -08002632 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2633 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2634
2635 // We attempt the approach which doesn't shuffle views at all
2636 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2637 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2638
2639 ItemConfiguration finalSolution = null;
2640 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2641 finalSolution = swapSolution;
2642 } else if (noShuffleSolution.isSolution) {
2643 finalSolution = noShuffleSolution;
2644 }
2645
2646 boolean foundSolution = true;
2647 if (!DESTRUCTIVE_REORDER) {
2648 setUseTempCoords(true);
2649 }
2650
2651 if (finalSolution != null) {
2652 result[0] = finalSolution.dragViewX;
2653 result[1] = finalSolution.dragViewY;
2654 resultSpan[0] = finalSolution.dragViewSpanX;
2655 resultSpan[1] = finalSolution.dragViewSpanY;
2656
2657 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2658 // committing anything or animating anything as we just want to determine if a solution
2659 // exists
2660 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2661 if (!DESTRUCTIVE_REORDER) {
2662 copySolutionToTempState(finalSolution, dragView);
2663 }
2664 setItemPlacementDirty(true);
2665 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2666
Adam Cohen19f37922012-03-21 11:59:11 -07002667 if (!DESTRUCTIVE_REORDER &&
2668 (mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL)) {
Adam Cohen482ed822012-03-02 14:15:13 -08002669 commitTempPlacement();
Adam Cohen19f37922012-03-21 11:59:11 -07002670 completeAndClearReorderHintAnimations();
2671 setItemPlacementDirty(false);
2672 } else {
2673 beginOrAdjustHintAnimations(finalSolution, dragView,
2674 REORDER_ANIMATION_DURATION);
Adam Cohen482ed822012-03-02 14:15:13 -08002675 }
2676 }
2677 } else {
2678 foundSolution = false;
2679 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2680 }
2681
2682 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2683 setUseTempCoords(false);
2684 }
Adam Cohen482ed822012-03-02 14:15:13 -08002685
Michael Jurkaa52570f2012-03-20 03:18:20 -07002686 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002687 return result;
2688 }
2689
Adam Cohen19f37922012-03-21 11:59:11 -07002690 void setItemPlacementDirty(boolean dirty) {
2691 mItemPlacementDirty = dirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002692 }
Adam Cohen19f37922012-03-21 11:59:11 -07002693 boolean isItemPlacementDirty() {
2694 return mItemPlacementDirty;
Adam Cohen482ed822012-03-02 14:15:13 -08002695 }
2696
2697 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002698 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohenf3900c22012-11-16 18:28:11 -08002699 private HashMap<View, CellAndSpan> savedMap = new HashMap<View, CellAndSpan>();
2700 ArrayList<View> sortedViews = new ArrayList<View>();
Adam Cohen482ed822012-03-02 14:15:13 -08002701 boolean isSolution = false;
2702 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2703
Adam Cohenf3900c22012-11-16 18:28:11 -08002704 void save() {
2705 // Copy current state into savedMap
2706 for (View v: map.keySet()) {
2707 map.get(v).copy(savedMap.get(v));
2708 }
2709 }
2710
2711 void restore() {
2712 // Restore current state from savedMap
2713 for (View v: savedMap.keySet()) {
2714 savedMap.get(v).copy(map.get(v));
2715 }
2716 }
2717
2718 void add(View v, CellAndSpan cs) {
2719 map.put(v, cs);
2720 savedMap.put(v, new CellAndSpan());
2721 sortedViews.add(v);
2722 }
2723
Adam Cohen482ed822012-03-02 14:15:13 -08002724 int area() {
2725 return dragViewSpanX * dragViewSpanY;
2726 }
Adam Cohen8baab352012-03-20 17:39:21 -07002727 }
2728
2729 private class CellAndSpan {
2730 int x, y;
2731 int spanX, spanY;
2732
Adam Cohenf3900c22012-11-16 18:28:11 -08002733 public CellAndSpan() {
2734 }
2735
2736 public void copy(CellAndSpan copy) {
2737 copy.x = x;
2738 copy.y = y;
2739 copy.spanX = spanX;
2740 copy.spanY = spanY;
2741 }
2742
Adam Cohen8baab352012-03-20 17:39:21 -07002743 public CellAndSpan(int x, int y, int spanX, int spanY) {
2744 this.x = x;
2745 this.y = y;
2746 this.spanX = spanX;
2747 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002748 }
Adam Cohenf3900c22012-11-16 18:28:11 -08002749
2750 public String toString() {
2751 return "(" + x + ", " + y + ": " + spanX + ", " + spanY + ")";
2752 }
2753
Adam Cohen482ed822012-03-02 14:15:13 -08002754 }
2755
Adam Cohendf035382011-04-11 17:22:04 -07002756 /**
2757 * Find a vacant area that will fit the given bounds nearest the requested
2758 * cell location. Uses Euclidean distance to score multiple vacant areas.
2759 *
2760 * @param pixelX The X location at which you want to search for a vacant area.
2761 * @param pixelY The Y location at which you want to search for a vacant area.
2762 * @param spanX Horizontal span of the object.
2763 * @param spanY Vertical span of the object.
2764 * @param ignoreView Considers space occupied by this view as unoccupied
2765 * @param result Previously returned value to possibly recycle.
2766 * @return The X, Y cell of a vacant area that can contain this object,
2767 * nearest the requested location.
2768 */
2769 int[] findNearestVacantArea(
2770 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2771 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2772 }
2773
2774 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002775 * Find a vacant area that will fit the given bounds nearest the requested
2776 * cell location. Uses Euclidean distance to score multiple vacant areas.
2777 *
2778 * @param pixelX The X location at which you want to search for a vacant area.
2779 * @param pixelY The Y location at which you want to search for a vacant area.
2780 * @param minSpanX The minimum horizontal span required
2781 * @param minSpanY The minimum vertical span required
2782 * @param spanX Horizontal span of the object.
2783 * @param spanY Vertical span of the object.
2784 * @param ignoreView Considers space occupied by this view as unoccupied
2785 * @param result Previously returned value to possibly recycle.
2786 * @return The X, Y cell of a vacant area that can contain this object,
2787 * nearest the requested location.
2788 */
2789 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2790 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002791 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2792 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002793 }
2794
2795 /**
Adam Cohendf035382011-04-11 17:22:04 -07002796 * Find a starting cell position that will fit the given bounds nearest the requested
2797 * cell location. Uses Euclidean distance to score multiple vacant areas.
2798 *
2799 * @param pixelX The X location at which you want to search for a vacant area.
2800 * @param pixelY The Y location at which you want to search for a vacant area.
2801 * @param spanX Horizontal span of the object.
2802 * @param spanY Vertical span of the object.
2803 * @param ignoreView Considers space occupied by this view as unoccupied
2804 * @param result Previously returned value to possibly recycle.
2805 * @return The X, Y cell of a vacant area that can contain this object,
2806 * nearest the requested location.
2807 */
2808 int[] findNearestArea(
2809 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2810 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2811 }
2812
Michael Jurka0280c3b2010-09-17 15:00:07 -07002813 boolean existsEmptyCell() {
2814 return findCellForSpan(null, 1, 1);
2815 }
2816
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002817 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002818 * Finds the upper-left coordinate of the first rectangle in the grid that can
2819 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2820 * then this method will only return coordinates for rectangles that contain the cell
2821 * (intersectX, intersectY)
2822 *
2823 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2824 * can be found.
2825 * @param spanX The horizontal span of the cell we want to find.
2826 * @param spanY The vertical span of the cell we want to find.
2827 *
2828 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002829 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002830 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002831 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002832 }
2833
2834 /**
2835 * Like above, but ignores any cells occupied by the item "ignoreView"
2836 *
2837 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2838 * can be found.
2839 * @param spanX The horizontal span of the cell we want to find.
2840 * @param spanY The vertical span of the cell we want to find.
2841 * @param ignoreView The home screen item we should treat as not occupying any space
2842 * @return
2843 */
2844 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002845 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2846 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002847 }
2848
2849 /**
2850 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2851 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2852 *
2853 * @param spanX The horizontal span of the cell we want to find.
2854 * @param spanY The vertical span of the cell we want to find.
2855 * @param ignoreView The home screen item we should treat as not occupying any space
2856 * @param intersectX The X coordinate of the cell that we should try to overlap
2857 * @param intersectX The Y coordinate of the cell that we should try to overlap
2858 *
2859 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2860 */
2861 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2862 int intersectX, int intersectY) {
2863 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002864 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002865 }
2866
2867 /**
2868 * The superset of the above two methods
2869 */
2870 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002871 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002872 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002873 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002874
Michael Jurka28750fb2010-09-24 17:43:49 -07002875 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002876 while (true) {
2877 int startX = 0;
2878 if (intersectX >= 0) {
2879 startX = Math.max(startX, intersectX - (spanX - 1));
2880 }
2881 int endX = mCountX - (spanX - 1);
2882 if (intersectX >= 0) {
2883 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2884 }
2885 int startY = 0;
2886 if (intersectY >= 0) {
2887 startY = Math.max(startY, intersectY - (spanY - 1));
2888 }
2889 int endY = mCountY - (spanY - 1);
2890 if (intersectY >= 0) {
2891 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2892 }
2893
Winson Chungbbc60d82010-11-11 16:34:41 -08002894 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002895 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002896 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002897 for (int i = 0; i < spanX; i++) {
2898 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002899 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002900 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002901 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002902 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002903 continue inner;
2904 }
2905 }
2906 }
2907 if (cellXY != null) {
2908 cellXY[0] = x;
2909 cellXY[1] = y;
2910 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002911 foundCell = true;
2912 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002913 }
2914 }
2915 if (intersectX == -1 && intersectY == -1) {
2916 break;
2917 } else {
2918 // if we failed to find anything, try again but without any requirements of
2919 // intersecting
2920 intersectX = -1;
2921 intersectY = -1;
2922 continue;
2923 }
2924 }
2925
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002926 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002927 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002928 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002929 }
2930
2931 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002932 * A drag event has begun over this layout.
2933 * It may have begun over this layout (in which case onDragChild is called first),
2934 * or it may have begun on another layout.
2935 */
2936 void onDragEnter() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002937 mDragEnforcer.onDragEnter();
Winson Chungc07918d2011-07-01 15:35:26 -07002938 mDragging = true;
2939 }
2940
2941 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002942 * Called when drag has left this CellLayout or has been completed (successfully or not)
2943 */
2944 void onDragExit() {
Adam Cohenc6cc61d2012-04-04 12:47:08 -07002945 mDragEnforcer.onDragExit();
Joe Onorato4be866d2010-10-10 11:26:02 -07002946 // This can actually be called when we aren't in a drag, e.g. when adding a new
2947 // item to this layout via the customize drawer.
2948 // Guard against that case.
2949 if (mDragging) {
2950 mDragging = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002951 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002952
2953 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002954 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002955 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2956 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
Adam Cohen19f37922012-03-21 11:59:11 -07002957 revertTempState();
Michael Jurka33945b22010-12-21 18:19:38 -08002958 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002959 }
2960
2961 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002962 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002963 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002964 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 *
2966 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002968 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002969 if (child != null) {
2970 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002971 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002972 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 }
2975
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002978 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 * @param cellX X coordinate of upper left corner expressed as a cell position
2980 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002981 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002982 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002983 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002985 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002986 final int cellWidth = mCellWidth;
2987 final int cellHeight = mCellHeight;
2988 final int widthGap = mWidthGap;
2989 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002990
Winson Chung4b825dcd2011-06-19 12:41:22 -07002991 final int hStartPadding = getPaddingLeft();
2992 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002993
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002994 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2995 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2996
2997 int x = hStartPadding + cellX * (cellWidth + widthGap);
2998 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002999
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07003000 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003001 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07003004 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003005 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07003006 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003007 * @param width Width in pixels
3008 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07003009 * @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 -08003010 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07003011 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07003012 return rectToCell(getResources(), width, height, result);
3013 }
3014
3015 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003016 // Always assume we're working with the smallest span to make sure we
3017 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04003018 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
3019 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04003021
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003022 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07003023 int spanX = (int) Math.ceil(width / (float) smallerSize);
3024 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04003025
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07003026 if (result == null) {
3027 return new int[] { spanX, spanY };
3028 }
3029 result[0] = spanX;
3030 result[1] = spanY;
3031 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003032 }
3033
Michael Jurkaf12c75c2011-01-25 22:41:40 -08003034 public int[] cellSpansToSize(int hSpans, int vSpans) {
3035 int[] size = new int[2];
3036 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
3037 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
3038 return size;
3039 }
3040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08003042 * Calculate the grid spans needed to fit given item
3043 */
3044 public void calculateSpans(ItemInfo info) {
3045 final int minWidth;
3046 final int minHeight;
3047
3048 if (info instanceof LauncherAppWidgetInfo) {
3049 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
3050 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
3051 } else if (info instanceof PendingAddWidgetInfo) {
3052 minWidth = ((PendingAddWidgetInfo) info).minWidth;
3053 minHeight = ((PendingAddWidgetInfo) info).minHeight;
3054 } else {
3055 // It's not a widget, so it must be 1x1
3056 info.spanX = info.spanY = 1;
3057 return;
3058 }
3059 int[] spans = rectToCell(minWidth, minHeight, null);
3060 info.spanX = spans[0];
3061 info.spanY = spans[1];
3062 }
3063
3064 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003065 * Find the first vacant cell, if there is one.
3066 *
3067 * @param vacant Holds the x and y coordinate of the vacant cell
3068 * @param spanX Horizontal cell span.
3069 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07003070 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003071 * @return True if a vacant cell was found
3072 */
3073 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003074
Michael Jurka0280c3b2010-09-17 15:00:07 -07003075 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003076 }
3077
3078 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
3079 int xCount, int yCount, boolean[][] occupied) {
3080
Adam Cohen2801caf2011-05-13 20:57:39 -07003081 for (int y = 0; y < yCount; y++) {
3082 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003083 boolean available = !occupied[x][y];
3084out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
3085 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
3086 available = available && !occupied[i][j];
3087 if (!available) break out;
3088 }
3089 }
3090
3091 if (available) {
3092 vacant[0] = x;
3093 vacant[1] = y;
3094 return true;
3095 }
3096 }
3097 }
3098
3099 return false;
3100 }
3101
Michael Jurka0280c3b2010-09-17 15:00:07 -07003102 private void clearOccupiedCells() {
3103 for (int x = 0; x < mCountX; x++) {
3104 for (int y = 0; y < mCountY; y++) {
3105 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106 }
3107 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07003108 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003109
Adam Cohend41fbf52012-02-16 23:53:59 -08003110 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003111 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08003112 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114
Adam Cohend4844c32011-02-18 19:25:06 -08003115 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003116 markCellsAsOccupiedForView(view, mOccupied);
3117 }
3118 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003119 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003120 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003121 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003122 }
3123
Adam Cohend4844c32011-02-18 19:25:06 -08003124 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08003125 markCellsAsUnoccupiedForView(view, mOccupied);
3126 }
3127 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07003128 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003129 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08003130 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07003131 }
3132
Adam Cohen482ed822012-03-02 14:15:13 -08003133 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
3134 boolean value) {
3135 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07003136 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
3137 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08003138 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003139 }
3140 }
3141 }
3142
Adam Cohen2801caf2011-05-13 20:57:39 -07003143 public int getDesiredWidth() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003144 return getPaddingLeft() + getPaddingRight() + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003145 (Math.max((mCountX - 1), 0) * mWidthGap);
3146 }
3147
3148 public int getDesiredHeight() {
Michael Jurka8b805b12012-04-18 14:23:14 -07003149 return getPaddingTop() + getPaddingBottom() + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07003150 (Math.max((mCountY - 1), 0) * mHeightGap);
3151 }
3152
Michael Jurka66d72172011-04-12 16:29:25 -07003153 public boolean isOccupied(int x, int y) {
3154 if (x < mCountX && y < mCountY) {
3155 return mOccupied[x][y];
3156 } else {
3157 throw new RuntimeException("Position exceeds the bound of this CellLayout");
3158 }
3159 }
3160
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003161 @Override
3162 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
3163 return new CellLayout.LayoutParams(getContext(), attrs);
3164 }
3165
3166 @Override
3167 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
3168 return p instanceof CellLayout.LayoutParams;
3169 }
3170
3171 @Override
3172 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
3173 return new CellLayout.LayoutParams(p);
3174 }
3175
Winson Chungaafa03c2010-06-11 17:34:16 -07003176 public static class CellLayoutAnimationController extends LayoutAnimationController {
3177 public CellLayoutAnimationController(Animation animation, float delay) {
3178 super(animation, delay);
3179 }
3180
3181 @Override
3182 protected long getDelayForView(View view) {
3183 return (int) (Math.random() * 150);
3184 }
3185 }
3186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003187 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
3188 /**
3189 * Horizontal location of the item in the grid.
3190 */
3191 @ViewDebug.ExportedProperty
3192 public int cellX;
3193
3194 /**
3195 * Vertical location of the item in the grid.
3196 */
3197 @ViewDebug.ExportedProperty
3198 public int cellY;
3199
3200 /**
Adam Cohen482ed822012-03-02 14:15:13 -08003201 * Temporary horizontal location of the item in the grid during reorder
3202 */
3203 public int tmpCellX;
3204
3205 /**
3206 * Temporary vertical location of the item in the grid during reorder
3207 */
3208 public int tmpCellY;
3209
3210 /**
3211 * Indicates that the temporary coordinates should be used to layout the items
3212 */
3213 public boolean useTmpCoords;
3214
3215 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003216 * Number of cells spanned horizontally by the item.
3217 */
3218 @ViewDebug.ExportedProperty
3219 public int cellHSpan;
3220
3221 /**
3222 * Number of cells spanned vertically by the item.
3223 */
3224 @ViewDebug.ExportedProperty
3225 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07003226
Adam Cohen1b607ed2011-03-03 17:26:50 -08003227 /**
3228 * Indicates whether the item will set its x, y, width and height parameters freely,
3229 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
3230 */
Adam Cohend4844c32011-02-18 19:25:06 -08003231 public boolean isLockedToGrid = true;
3232
Adam Cohen482ed822012-03-02 14:15:13 -08003233 /**
3234 * Indicates whether this item can be reordered. Always true except in the case of the
3235 * the AllApps button.
3236 */
3237 public boolean canReorder = true;
3238
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003239 // X coordinate of the view in the layout.
3240 @ViewDebug.ExportedProperty
3241 int x;
3242 // Y coordinate of the view in the layout.
3243 @ViewDebug.ExportedProperty
3244 int y;
3245
Romain Guy84f296c2009-11-04 15:00:44 -08003246 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07003247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003248 public LayoutParams(Context c, AttributeSet attrs) {
3249 super(c, attrs);
3250 cellHSpan = 1;
3251 cellVSpan = 1;
3252 }
3253
3254 public LayoutParams(ViewGroup.LayoutParams source) {
3255 super(source);
3256 cellHSpan = 1;
3257 cellVSpan = 1;
3258 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003259
3260 public LayoutParams(LayoutParams source) {
3261 super(source);
3262 this.cellX = source.cellX;
3263 this.cellY = source.cellY;
3264 this.cellHSpan = source.cellHSpan;
3265 this.cellVSpan = source.cellVSpan;
3266 }
3267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003268 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08003269 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003270 this.cellX = cellX;
3271 this.cellY = cellY;
3272 this.cellHSpan = cellHSpan;
3273 this.cellVSpan = cellVSpan;
3274 }
3275
Adam Cohen2374abf2013-04-16 14:56:57 -07003276 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap,
3277 boolean invertHorizontally, int colCount) {
Adam Cohend4844c32011-02-18 19:25:06 -08003278 if (isLockedToGrid) {
3279 final int myCellHSpan = cellHSpan;
3280 final int myCellVSpan = cellVSpan;
Adam Cohen2374abf2013-04-16 14:56:57 -07003281 int myCellX = useTmpCoords ? tmpCellX : cellX;
3282 int myCellY = useTmpCoords ? tmpCellY : cellY;
3283
3284 if (invertHorizontally) {
3285 myCellX = colCount - myCellX - cellHSpan;
3286 }
Adam Cohen1b607ed2011-03-03 17:26:50 -08003287
Adam Cohend4844c32011-02-18 19:25:06 -08003288 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
3289 leftMargin - rightMargin;
3290 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
3291 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08003292 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
3293 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08003294 }
3295 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003296
Winson Chungaafa03c2010-06-11 17:34:16 -07003297 public String toString() {
3298 return "(" + this.cellX + ", " + this.cellY + ")";
3299 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07003300
3301 public void setWidth(int width) {
3302 this.width = width;
3303 }
3304
3305 public int getWidth() {
3306 return width;
3307 }
3308
3309 public void setHeight(int height) {
3310 this.height = height;
3311 }
3312
3313 public int getHeight() {
3314 return height;
3315 }
3316
3317 public void setX(int x) {
3318 this.x = x;
3319 }
3320
3321 public int getX() {
3322 return x;
3323 }
3324
3325 public void setY(int y) {
3326 this.y = y;
3327 }
3328
3329 public int getY() {
3330 return y;
3331 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003332 }
3333
Michael Jurka0280c3b2010-09-17 15:00:07 -07003334 // This class stores info for two purposes:
3335 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
3336 // its spanX, spanY, and the screen it is on
3337 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
3338 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
3339 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07003340 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003341 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07003342 int cellX = -1;
3343 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003344 int spanX;
3345 int spanY;
Adam Cohendcd297f2013-06-18 13:13:40 -07003346 long screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07003347 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003348
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003349 @Override
3350 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07003351 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
3352 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003353 }
3354 }
Michael Jurkad771c962011-08-09 15:00:48 -07003355
3356 public boolean lastDownOnOccupiedCell() {
3357 return mLastDownOnOccupiedCell;
3358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003359}