blob: 5969f9842ccb5b52ad33b123f4505f0ea53e299d [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070021import android.animation.ObjectAnimator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070022import android.animation.PropertyValuesHolder;
Chet Haase00397b12010-10-07 11:13:10 -070023import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070024import android.animation.ValueAnimator;
25import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040027import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070029import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080031import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070033import android.graphics.Point;
34import android.graphics.PointF;
Adam Cohenb5ba0972011-09-07 18:02:31 -070035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080038import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070039import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070040import android.graphics.drawable.NinePatchDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070042import android.util.Log;
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
Adam Cohen66396872011-04-15 17:50:36 -070051import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070052import com.android.launcher2.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 Cohenbfbfd262011-06-13 16:55:12 -070056import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080057import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070058
Michael Jurkabdb5c532011-02-01 15:05:06 -080059public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070060 static final String TAG = "CellLayout";
61
Winson Chung4b825dcd2011-06-19 12:41:22 -070062 private int mOriginalCellWidth;
63 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 private int mCellWidth;
65 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070066
Adam Cohend22015c2010-07-26 22:02:18 -070067 private int mCountX;
68 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
Adam Cohen234c4cd2011-07-17 21:03:04 -070070 private int mOriginalWidthGap;
71 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 private int mWidthGap;
73 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070074 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080075 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
77 private final Rect mRect = new Rect();
78 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070079
Patrick Dubroyde7658b2010-09-27 11:15:43 -070080 // These are temporary variables to prevent having to allocate a new object just to
81 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070082 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070083 private final int[] mTmpPoint = new int[2];
84 private final PointF mTmpPointF = new PointF();
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
Michael Jurka33945b22010-12-21 18:19:38 -0800109 // If we're actively dragging something over this screen, mIsDragOverlapping is true
110 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700111 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700112
Winson Chung150fbab2010-09-29 17:14:26 -0700113 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700114 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800115 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700116 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700117 private InterruptibleInOutAnimator[] mDragOutlineAnims =
118 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700119
120 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700121 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700122 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700123
Patrick Dubroy96864c32011-03-10 17:17:23 -0800124 private BubbleTextView mPressedOrFocusedIcon;
125
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700126 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700127 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700128 private float mCrosshairsVisibility = 0.0f;
129
Adam Cohen482ed822012-03-02 14:15:13 -0800130 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
131 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700132
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700133 // When a drag operation is in progress, holds the nearest cell to the touch point
134 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Joe Onorato4be866d2010-10-10 11:26:02 -0700136 private boolean mDragging = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800137 private boolean mItemLocationsDirty = false;
Joe Onorato4be866d2010-10-10 11:26:02 -0700138
Patrick Dubroyce34a972010-10-19 10:34:32 -0700139 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700140 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700141
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800142 private boolean mIsHotseat = false;
Winson Chungeecf02d2012-03-02 17:14:58 -0800143 private float mChildScale = 1f;
144 private float mHotseatChildScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800145
Adam Cohen482ed822012-03-02 14:15:13 -0800146 public static final int MODE_DRAG_OVER = 0;
147 public static final int MODE_ON_DROP = 1;
148 public static final int MODE_ON_DROP_EXTERNAL = 2;
149 public static final int MODE_ACCEPT_DROP = 3;
150 private static final boolean DESTRUCTIVE_REORDER = true;
151 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
152
153 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
154 private Rect mOccupiedRect = new Rect();
155 private int[] mDirectionVector = new int[2];
156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 public CellLayout(Context context) {
158 this(context, null);
159 }
160
161 public CellLayout(Context context, AttributeSet attrs) {
162 this(context, attrs, 0);
163 }
164
165 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
166 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700167
168 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
169 // the user where a dragged item will land when dropped.
170 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
173
Winson Chung4b825dcd2011-06-19 12:41:22 -0700174 mOriginalCellWidth =
175 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
176 mOriginalCellHeight =
177 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700178 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
179 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700180 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700181 mCountX = LauncherModel.getCellCountX();
182 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700183 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800184 mTmpOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
186 a.recycle();
187
188 setAlwaysDrawnWithCacheEnabled(false);
189
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700190 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700191
Winson Chung967289b2011-06-30 18:09:30 -0700192 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700193 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800194
Adam Cohenb5ba0972011-09-07 18:02:31 -0700195 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
196 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
197 mForegroundPadding =
198 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800199
Winson Chungb26f3d62011-06-02 10:49:29 -0700200 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700201 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700202
Winson Chungeecf02d2012-03-02 17:14:58 -0800203 int iconScale = res.getInteger(R.integer.app_icon_scale_percent);
204 if (iconScale >= 0) {
205 mChildScale = iconScale / 100f;
206 }
207 int hotseatIconScale = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
208 if (hotseatIconScale >= 0) {
209 mHotseatChildScale = hotseatIconScale / 100f;
210 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800211
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700212 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700213
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700214 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700215 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700216
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700217 // Set up the animation for fading the crosshairs in and out
218 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700219 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700220 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 public void onAnimationUpdate(ValueAnimator animation) {
222 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700223 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700224 }
225 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700226 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700227
Winson Chungb8c69f32011-10-19 21:36:08 -0700228 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700229 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800230 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700231 }
232
233 // When dragging things around the home screens, we show a green outline of
234 // where the item will land. The outlines gradually fade out, leaving a trail
235 // behind the drag path.
236 // Set up all the animations that are used to implement this fading.
237 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700238 final float fromAlphaValue = 0;
239 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700240
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700241 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700242
243 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700244 final InterruptibleInOutAnimator anim =
245 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700246 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700247 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700248 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700249 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 final Bitmap outline = (Bitmap)anim.getTag();
251
252 // If an animation is started and then stopped very quickly, we can still
253 // get spurious updates we've cleared the tag. Guard against this.
254 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700255 if (false) {
256 Object val = animation.getAnimatedValue();
257 Log.d(TAG, "anim " + thisIndex + " update: " + val +
258 ", isStopped " + anim.isStopped());
259 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700260 // Try to prevent it from continuing to run
261 animation.cancel();
262 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700263 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800264 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700265 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700266 }
267 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 // The animation holds a reference to the drag outline bitmap as long is it's
269 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700270 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700271 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700272 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700273 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 anim.setTag(null);
275 }
276 }
277 });
278 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700279 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700280
Michael Jurka18014792010-10-14 09:01:34 -0700281 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700282 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800283
Michael Jurkaa52570f2012-03-20 03:18:20 -0700284 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
285 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
286 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700287 }
288
Michael Jurkaf6440da2011-04-05 14:50:34 -0700289 static int widthInPortrait(Resources r, int numCells) {
290 // We use this method from Workspace to figure out how many rows/columns Launcher should
291 // have. We ignore the left/right padding on CellLayout because it turns out in our design
292 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700293 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700294 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
295 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700296
Winson Chung4b825dcd2011-06-19 12:41:22 -0700297 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700298 }
299
Michael Jurkaf6440da2011-04-05 14:50:34 -0700300 static int heightInLandscape(Resources r, int numCells) {
301 // We use this method from Workspace to figure out how many rows/columns Launcher should
302 // have. We ignore the left/right padding on CellLayout because it turns out in our design
303 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700304 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700305 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
306 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700307
Winson Chung4b825dcd2011-06-19 12:41:22 -0700308 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700309 }
310
Adam Cohen2801caf2011-05-13 20:57:39 -0700311 public void enableHardwareLayers() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700312 mShortcutsAndWidgets.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700313 }
314
315 public void setGridSize(int x, int y) {
316 mCountX = x;
317 mCountY = y;
318 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800319 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700320 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700321 }
322
Patrick Dubroy96864c32011-03-10 17:17:23 -0800323 private void invalidateBubbleTextView(BubbleTextView icon) {
324 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700325 invalidate(icon.getLeft() + getPaddingLeft() - padding,
326 icon.getTop() + getPaddingTop() - padding,
327 icon.getRight() + getPaddingLeft() + padding,
328 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800329 }
330
Adam Cohenb5ba0972011-09-07 18:02:31 -0700331 void setOverScrollAmount(float r, boolean left) {
332 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
333 mOverScrollForegroundDrawable = mOverScrollLeft;
334 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
335 mOverScrollForegroundDrawable = mOverScrollRight;
336 }
337
338 mForegroundAlpha = (int) Math.round((r * 255));
339 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
340 invalidate();
341 }
342
Patrick Dubroy96864c32011-03-10 17:17:23 -0800343 void setPressedOrFocusedIcon(BubbleTextView icon) {
344 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
345 // requires an expanded clip rect (due to the glow's blur radius)
346 BubbleTextView oldIcon = mPressedOrFocusedIcon;
347 mPressedOrFocusedIcon = icon;
348 if (oldIcon != null) {
349 invalidateBubbleTextView(oldIcon);
350 }
351 if (mPressedOrFocusedIcon != null) {
352 invalidateBubbleTextView(mPressedOrFocusedIcon);
353 }
354 }
355
Michael Jurka33945b22010-12-21 18:19:38 -0800356 void setIsDragOverlapping(boolean isDragOverlapping) {
357 if (mIsDragOverlapping != isDragOverlapping) {
358 mIsDragOverlapping = isDragOverlapping;
359 invalidate();
360 }
361 }
362
363 boolean getIsDragOverlapping() {
364 return mIsDragOverlapping;
365 }
366
Adam Cohenebea84d2011-11-09 17:20:41 -0800367 protected void setOverscrollTransformsDirty(boolean dirty) {
368 mScrollingTransformsDirty = dirty;
369 }
370
371 protected void resetOverscrollTransforms() {
372 if (mScrollingTransformsDirty) {
373 setOverscrollTransformsDirty(false);
374 setTranslationX(0);
375 setRotationY(0);
376 // It doesn't matter if we pass true or false here, the important thing is that we
377 // pass 0, which results in the overscroll drawable not being drawn any more.
378 setOverScrollAmount(0, false);
379 setPivotX(getMeasuredWidth() / 2);
380 setPivotY(getMeasuredHeight() / 2);
381 }
382 }
383
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700384 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700385 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700386 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
387 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
388 // When we're small, we are either drawn normally or in the "accepts drops" state (during
389 // a drag). However, we also drag the mini hover background *over* one of those two
390 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700391 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700392 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800393
394 if (mIsDragOverlapping) {
395 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700396 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700397 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700398 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700399 }
Michael Jurka33945b22010-12-21 18:19:38 -0800400
401 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
402 bg.setBounds(mBackgroundRect);
403 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700404 }
Romain Guya6abce82009-11-10 02:54:41 -0800405
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700406 if (mCrosshairsVisibility > 0.0f) {
407 final int countX = mCountX;
408 final int countY = mCountY;
409
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700410 final float MAX_ALPHA = 0.4f;
411 final int MAX_VISIBLE_DISTANCE = 600;
412 final float DISTANCE_MULTIPLIER = 0.002f;
413
414 final Drawable d = mCrosshairsDrawable;
415 final int width = d.getIntrinsicWidth();
416 final int height = d.getIntrinsicHeight();
417
Winson Chung4b825dcd2011-06-19 12:41:22 -0700418 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700419 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700420 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700421 for (int row = 0; row <= countY; row++) {
422 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
423 float dist = mTmpPointF.length();
424 // Crosshairs further from the drag point are more faint
425 float alpha = Math.min(MAX_ALPHA,
426 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
427 if (alpha > 0.0f) {
428 d.setBounds(x, y, x + width, y + height);
429 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
430 d.draw(canvas);
431 }
432 y += mCellHeight + mHeightGap;
433 }
434 x += mCellWidth + mWidthGap;
435 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700436 }
Winson Chung150fbab2010-09-29 17:14:26 -0700437
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700438 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700439 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700440 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700441 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800442 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700443 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700444 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800445 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700446 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700447 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800448
449 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
450 // requires an expanded clip rect (due to the glow's blur radius)
451 if (mPressedOrFocusedIcon != null) {
452 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
453 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
454 if (b != null) {
455 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700456 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
457 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800458 null);
459 }
460 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700461
Adam Cohen482ed822012-03-02 14:15:13 -0800462 if (DEBUG_VISUALIZE_OCCUPIED) {
463 int[] pt = new int[2];
464 ColorDrawable cd = new ColorDrawable(Color.RED);
465 cd.setBounds(0, 0, 80, 80);
466 for (int i = 0; i < mCountX; i++) {
467 for (int j = 0; j < mCountY; j++) {
468 if (mOccupied[i][j]) {
469 cellToPoint(i, j, pt);
470 canvas.save();
471 canvas.translate(pt[0], pt[1]);
472 cd.draw(canvas);
473 canvas.restore();
474 }
475 }
476 }
477 }
478
Adam Cohen69ce2e52011-07-03 19:25:21 -0700479 // The folder outer / inner ring image(s)
480 for (int i = 0; i < mFolderOuterRings.size(); i++) {
481 FolderRingAnimator fra = mFolderOuterRings.get(i);
482
483 // Draw outer ring
484 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
485 int width = (int) fra.getOuterRingSize();
486 int height = width;
487 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
488
489 int centerX = mTempLocation[0] + mCellWidth / 2;
490 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
491
492 canvas.save();
493 canvas.translate(centerX - width / 2, centerY - height / 2);
494 d.setBounds(0, 0, width, height);
495 d.draw(canvas);
496 canvas.restore();
497
498 // Draw inner ring
499 d = FolderRingAnimator.sSharedInnerRingDrawable;
500 width = (int) fra.getInnerRingSize();
501 height = width;
502 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
503
504 centerX = mTempLocation[0] + mCellWidth / 2;
505 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
506 canvas.save();
507 canvas.translate(centerX - width / 2, centerY - width / 2);
508 d.setBounds(0, 0, width, height);
509 d.draw(canvas);
510 canvas.restore();
511 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700512
513 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
514 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
515 int width = d.getIntrinsicWidth();
516 int height = d.getIntrinsicHeight();
517
518 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
519 int centerX = mTempLocation[0] + mCellWidth / 2;
520 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
521
522 canvas.save();
523 canvas.translate(centerX - width / 2, centerY - width / 2);
524 d.setBounds(0, 0, width, height);
525 d.draw(canvas);
526 canvas.restore();
527 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700528 }
529
Adam Cohenb5ba0972011-09-07 18:02:31 -0700530 @Override
531 protected void dispatchDraw(Canvas canvas) {
532 super.dispatchDraw(canvas);
533 if (mForegroundAlpha > 0) {
534 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
535 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
536 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
537 mOverScrollForegroundDrawable.draw(canvas);
538 p.setXfermode(null);
539 }
540 }
541
Adam Cohen69ce2e52011-07-03 19:25:21 -0700542 public void showFolderAccept(FolderRingAnimator fra) {
543 mFolderOuterRings.add(fra);
544 }
545
546 public void hideFolderAccept(FolderRingAnimator fra) {
547 if (mFolderOuterRings.contains(fra)) {
548 mFolderOuterRings.remove(fra);
549 }
550 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700551 }
552
Adam Cohenc51934b2011-07-26 21:07:43 -0700553 public void setFolderLeaveBehindCell(int x, int y) {
554 mFolderLeaveBehindCell[0] = x;
555 mFolderLeaveBehindCell[1] = y;
556 invalidate();
557 }
558
559 public void clearFolderLeaveBehind() {
560 mFolderLeaveBehindCell[0] = -1;
561 mFolderLeaveBehindCell[1] = -1;
562 invalidate();
563 }
564
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700565 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700566 public boolean shouldDelayChildPressedState() {
567 return false;
568 }
569
570 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700571 public void cancelLongPress() {
572 super.cancelLongPress();
573
574 // Cancel long press for all children
575 final int count = getChildCount();
576 for (int i = 0; i < count; i++) {
577 final View child = getChildAt(i);
578 child.cancelLongPress();
579 }
580 }
581
Michael Jurkadee05892010-07-27 10:01:56 -0700582 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
583 mInterceptTouchListener = listener;
584 }
585
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700587 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 }
589
590 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700591 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 }
593
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800594 public void setIsHotseat(boolean isHotseat) {
595 mIsHotseat = isHotseat;
596 }
597
Winson Chungeecf02d2012-03-02 17:14:58 -0800598 public float getChildrenScale() {
599 return mIsHotseat ? mHotseatChildScale : mChildScale;
600 }
601
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700602 public boolean addViewToCellLayout(
603 View child, int index, int childId, LayoutParams params, boolean markCells) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800604 return addViewToCellLayout(child, index, childId, params, markCells, false);
605 }
606
Winson Chungeecf02d2012-03-02 17:14:58 -0800607 private void scaleChild(BubbleTextView bubbleChild, float pivot, float scale) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800608 // If we haven't measured the child yet, do it now
609 // (this happens if we're being dropped from all-apps
610 if (bubbleChild.getLayoutParams() instanceof LayoutParams &&
611 (bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700612 getShortcutsAndWidgets().measureChild(bubbleChild);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800613 }
614 int measuredWidth = bubbleChild.getMeasuredWidth();
615 int measuredHeight = bubbleChild.getMeasuredHeight();
616
Andrew Flynnbc239a12012-03-06 11:39:49 -0800617 bubbleChild.setScaleX(scale);
618 bubbleChild.setScaleY(scale);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800619 }
620
621 private void resetChild(BubbleTextView bubbleChild) {
622 bubbleChild.setScaleX(1f);
623 bubbleChild.setScaleY(1f);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800624
625 bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
626 }
627
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800628 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
629 boolean markCells, boolean allApps) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700630 final LayoutParams lp = params;
631
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800632 // Hotseat icons - scale down and remove text
633 // Don't scale the all apps button
634 // scale percent set to -1 means do not scale
635 // Only scale BubbleTextViews
636 if (child instanceof BubbleTextView) {
637 BubbleTextView bubbleChild = (BubbleTextView) child;
638
Andrew Flynnbc239a12012-03-06 11:39:49 -0800639 // Start the child with 100% scale and visible text
640 resetChild(bubbleChild);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800641
Winson Chungeecf02d2012-03-02 17:14:58 -0800642 if (mIsHotseat && !allApps && mHotseatChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800643 // Scale/make transparent for a hotseat
Winson Chungeecf02d2012-03-02 17:14:58 -0800644 scaleChild(bubbleChild, 0f, mHotseatChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800645
Andrew Flynnbc239a12012-03-06 11:39:49 -0800646 bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent));
Winson Chungeecf02d2012-03-02 17:14:58 -0800647 } else if (mChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800648 // Else possibly still scale it if we need to for smaller icons
Winson Chungeecf02d2012-03-02 17:14:58 -0800649 scaleChild(bubbleChild, 0f, mChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800650 }
651 }
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 // Generate an id for each view, this assumes we have at most 256x256 cells
654 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700655 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700656 // If the horizontal or vertical span is set to -1, it is taken to
657 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700658 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
659 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660
Winson Chungaafa03c2010-06-11 17:34:16 -0700661 child.setId(childId);
662
Michael Jurkaa52570f2012-03-20 03:18:20 -0700663 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700664
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700665 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666
Winson Chungaafa03c2010-06-11 17:34:16 -0700667 return true;
668 }
669 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700671
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700673 public void removeAllViews() {
674 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700675 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 }
677
678 @Override
679 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700680 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700681 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700682 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700683 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700686 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700687 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700688 }
689
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 @Override
691 public void removeView(View view) {
692 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700693 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700694 }
695
696 @Override
697 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700698 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
699 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700700 }
701
702 @Override
703 public void removeViewInLayout(View view) {
704 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700705 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700706 }
707
708 @Override
709 public void removeViews(int start, int count) {
710 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700711 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700712 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700713 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700714 }
715
716 @Override
717 public void removeViewsInLayout(int start, int count) {
718 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700719 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700720 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700721 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800722 }
723
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 @Override
725 protected void onAttachedToWindow() {
726 super.onAttachedToWindow();
727 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
728 }
729
Michael Jurkaaf442092010-06-10 17:01:57 -0700730 public void setTagToCellInfoForPoint(int touchX, int touchY) {
731 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800732 Rect frame = mRect;
Michael Jurkaaf442092010-06-10 17:01:57 -0700733 final int x = touchX + mScrollX;
734 final int y = touchY + mScrollY;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700735 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700736
737 boolean found = false;
738 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700739 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800740 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700741
Adam Cohen1b607ed2011-03-03 17:26:50 -0800742 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
743 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700744 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700745
Winson Chungeecf02d2012-03-02 17:14:58 -0800746 float scale = child.getScaleX();
747 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
748 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700749 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
750 // offset that by this CellLayout's padding to test an (x,y) point that is relative
751 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700752 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chungeecf02d2012-03-02 17:14:58 -0800753 frame.inset((int) (frame.width() * (1f - scale) / 2),
754 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700755
Michael Jurkaaf442092010-06-10 17:01:57 -0700756 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700757 cellInfo.cell = child;
758 cellInfo.cellX = lp.cellX;
759 cellInfo.cellY = lp.cellY;
760 cellInfo.spanX = lp.cellHSpan;
761 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700762 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700763 break;
764 }
765 }
766 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700767
Michael Jurkad771c962011-08-09 15:00:48 -0700768 mLastDownOnOccupiedCell = found;
769
Michael Jurkaaf442092010-06-10 17:01:57 -0700770 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700771 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700772 pointToCellExact(x, y, cellXY);
773
Michael Jurkaaf442092010-06-10 17:01:57 -0700774 cellInfo.cell = null;
775 cellInfo.cellX = cellXY[0];
776 cellInfo.cellY = cellXY[1];
777 cellInfo.spanX = 1;
778 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700779 }
780 setTag(cellInfo);
781 }
782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 @Override
784 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700785 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
786 // even in the case where we return early. Not clearing here was causing bugs whereby on
787 // long-press we'd end up picking up an item from a previous drag operation.
788 final int action = ev.getAction();
789
790 if (action == MotionEvent.ACTION_DOWN) {
791 clearTagCellInfo();
792 }
793
Michael Jurkadee05892010-07-27 10:01:56 -0700794 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
795 return true;
796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797
798 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700799 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 return false;
803 }
804
Adam Cohenc1997fd2011-08-15 18:26:39 -0700805 private void clearTagCellInfo() {
806 final CellInfo cellInfo = mCellInfo;
807 cellInfo.cell = null;
808 cellInfo.cellX = -1;
809 cellInfo.cellY = -1;
810 cellInfo.spanX = 0;
811 cellInfo.spanY = 0;
812 setTag(cellInfo);
813 }
814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700816 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
818
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700819 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700820 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 * @param x X coordinate of the point
822 * @param y Y coordinate of the point
823 * @param result Array of 2 ints to hold the x and y coordinate of the cell
824 */
825 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700826 final int hStartPadding = getPaddingLeft();
827 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828
829 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
830 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
831
Adam Cohend22015c2010-07-26 22:02:18 -0700832 final int xAxis = mCountX;
833 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834
835 if (result[0] < 0) result[0] = 0;
836 if (result[0] >= xAxis) result[0] = xAxis - 1;
837 if (result[1] < 0) result[1] = 0;
838 if (result[1] >= yAxis) result[1] = yAxis - 1;
839 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700840
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 /**
842 * Given a point, return the cell that most closely encloses that point
843 * @param x X coordinate of the point
844 * @param y Y coordinate of the point
845 * @param result Array of 2 ints to hold the x and y coordinate of the cell
846 */
847 void pointToCellRounded(int x, int y, int[] result) {
848 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
849 }
850
851 /**
852 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700853 *
854 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700856 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 * @param result Array of 2 ints to hold the x and y coordinate of the point
858 */
859 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700860 final int hStartPadding = getPaddingLeft();
861 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862
863 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
864 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
865 }
866
Adam Cohene3e27a82011-04-15 12:07:39 -0700867 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800868 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700869 *
870 * @param cellX X coordinate of the cell
871 * @param cellY Y coordinate of the cell
872 *
873 * @param result Array of 2 ints to hold the x and y coordinate of the point
874 */
875 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700876 regionToCenterPoint(cellX, cellY, 1, 1, result);
877 }
878
879 /**
880 * Given a cell coordinate and span return the point that represents the center of the regio
881 *
882 * @param cellX X coordinate of the cell
883 * @param cellY Y coordinate of the cell
884 *
885 * @param result Array of 2 ints to hold the x and y coordinate of the point
886 */
887 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700888 final int hStartPadding = getPaddingLeft();
889 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700890
Adam Cohen47a876d2012-03-19 13:21:41 -0700891 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
892 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
893 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
894 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700895 }
896
Adam Cohen482ed822012-03-02 14:15:13 -0800897 public float getDistanceFromCell(float x, float y, int[] cell) {
898 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
899 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
900 Math.pow(y - mTmpPoint[1], 2));
901 return distance;
902 }
903
Romain Guy84f296c2009-11-04 15:00:44 -0800904 int getCellWidth() {
905 return mCellWidth;
906 }
907
908 int getCellHeight() {
909 return mCellHeight;
910 }
911
Adam Cohend4844c32011-02-18 19:25:06 -0800912 int getWidthGap() {
913 return mWidthGap;
914 }
915
916 int getHeightGap() {
917 return mHeightGap;
918 }
919
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700920 Rect getContentRect(Rect r) {
921 if (r == null) {
922 r = new Rect();
923 }
924 int left = getPaddingLeft();
925 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700926 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
927 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700928 r.set(left, top, right, bottom);
929 return r;
930 }
931
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 @Override
933 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
934 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700935
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700937 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
938
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
940 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700941
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
943 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
944 }
945
Adam Cohend22015c2010-07-26 22:02:18 -0700946 int numWidthGaps = mCountX - 1;
947 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948
Adam Cohen234c4cd2011-07-17 21:03:04 -0700949 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700950 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
951 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
952 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
953 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
954 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
955 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700956 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700957 } else {
958 mWidthGap = mOriginalWidthGap;
959 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700960 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700961
Michael Jurka8c920dd2011-01-20 14:16:56 -0800962 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
963 int newWidth = widthSpecSize;
964 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700965 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700966 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700967 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700968 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700969 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700970 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700971 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800972
973 int count = getChildCount();
974 for (int i = 0; i < count; i++) {
975 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700976 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
977 mPaddingRight, MeasureSpec.EXACTLY);
978 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
979 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800980 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
981 }
982 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 }
984
985 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700986 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800989 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700990 child.layout(mPaddingLeft, mPaddingTop,
991 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 }
993 }
994
995 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700996 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
997 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700998 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700999 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
1000 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001001 }
1002
1003 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001005 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
1007
1008 @Override
1009 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001010 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 }
1012
Michael Jurka5f1c5092010-09-03 14:15:02 -07001013 public float getBackgroundAlpha() {
1014 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001015 }
1016
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001017 public void setBackgroundAlphaMultiplier(float multiplier) {
1018 mBackgroundAlphaMultiplier = multiplier;
1019 }
1020
Adam Cohenddb82192010-11-10 16:32:54 -08001021 public float getBackgroundAlphaMultiplier() {
1022 return mBackgroundAlphaMultiplier;
1023 }
1024
Michael Jurka5f1c5092010-09-03 14:15:02 -07001025 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001026 if (mBackgroundAlpha != alpha) {
1027 mBackgroundAlpha = alpha;
1028 invalidate();
1029 }
Michael Jurkadee05892010-07-27 10:01:56 -07001030 }
1031
Michael Jurkaa52570f2012-03-20 03:18:20 -07001032 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001033 final int childCount = getChildCount();
1034 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001035 getChildAt(i).setAlpha(alpha);
1036 }
1037 }
1038
Michael Jurkaa52570f2012-03-20 03:18:20 -07001039 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1040 if (getChildCount() > 0) {
1041 return (ShortcutAndWidgetContainer) getChildAt(0);
1042 }
1043 return null;
1044 }
1045
Patrick Dubroy440c3602010-07-13 17:50:32 -07001046 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001047 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001048 }
1049
Adam Cohen76fc0852011-06-17 13:26:23 -07001050 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001051 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001052 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001053 boolean[][] occupied = mOccupied;
1054 if (!permanent) {
1055 occupied = mTmpOccupied;
1056 }
1057
1058 if (clc.indexOfChild(child) != -1 && !occupied[cellX][cellY]) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001059 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1060 final ItemInfo info = (ItemInfo) child.getTag();
1061
1062 // We cancel any existing animations
1063 if (mReorderAnimators.containsKey(lp)) {
1064 mReorderAnimators.get(lp).cancel();
1065 mReorderAnimators.remove(lp);
1066 }
1067
Adam Cohen482ed822012-03-02 14:15:13 -08001068 final int oldX = lp.x;
1069 final int oldY = lp.y;
1070 if (adjustOccupied) {
1071 occupied[lp.cellX][lp.cellY] = false;
1072 occupied[cellX][cellY] = true;
1073 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001074 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001075 if (permanent) {
1076 lp.cellX = info.cellX = cellX;
1077 lp.cellY = info.cellY = cellY;
1078 } else {
1079 lp.tmpCellX = cellX;
1080 lp.tmpCellY = cellY;
1081 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001082 clc.setupLp(lp);
1083 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001084 final int newX = lp.x;
1085 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001086
Adam Cohen76fc0852011-06-17 13:26:23 -07001087 lp.x = oldX;
1088 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001089
Adam Cohen482ed822012-03-02 14:15:13 -08001090 // Exit early if we're not actually moving the view
1091 if (oldX == newX && oldY == newY) {
1092 lp.isLockedToGrid = true;
1093 return true;
1094 }
1095
1096 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
1097 va.setDuration(duration);
1098 mReorderAnimators.put(lp, va);
1099
1100 va.addUpdateListener(new AnimatorUpdateListener() {
1101 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001102 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001103 float r = ((Float) animation.getAnimatedValue()).floatValue();
1104 child.setTranslationX(r * (newX - oldX));
1105 child.setTranslationY(r * (newY - oldY));
Adam Cohenbfbfd262011-06-13 16:55:12 -07001106 }
1107 });
Adam Cohen482ed822012-03-02 14:15:13 -08001108 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001109 boolean cancelled = false;
1110 public void onAnimationEnd(Animator animation) {
1111 // If the animation was cancelled, it means that another animation
1112 // has interrupted this one, and we don't want to lock the item into
1113 // place just yet.
1114 if (!cancelled) {
Adam Cohen482ed822012-03-02 14:15:13 -08001115 child.setTranslationX(0);
1116 child.setTranslationY(0);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001117 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001118 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001119 }
1120 if (mReorderAnimators.containsKey(lp)) {
1121 mReorderAnimators.remove(lp);
1122 }
1123 }
1124 public void onAnimationCancel(Animator animation) {
1125 cancelled = true;
1126 }
1127 });
Adam Cohen482ed822012-03-02 14:15:13 -08001128 va.setStartDelay(delay);
1129 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001130 return true;
1131 }
1132 return false;
1133 }
1134
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001135 /**
1136 * Estimate where the top left cell of the dragged item will land if it is dropped.
1137 *
1138 * @param originX The X value of the top left corner of the item
1139 * @param originY The Y value of the top left corner of the item
1140 * @param spanX The number of horizontal cells that the item spans
1141 * @param spanY The number of vertical cells that the item spans
1142 * @param result The estimated drop cell X and Y.
1143 */
1144 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001145 final int countX = mCountX;
1146 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001147
Michael Jurkaa63c4522010-08-19 13:52:27 -07001148 // pointToCellRounded takes the top left of a cell but will pad that with
1149 // cellWidth/2 and cellHeight/2 when finding the matching cell
1150 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001151
1152 // If the item isn't fully on this screen, snap to the edges
1153 int rightOverhang = result[0] + spanX - countX;
1154 if (rightOverhang > 0) {
1155 result[0] -= rightOverhang; // Snap to right
1156 }
1157 result[0] = Math.max(0, result[0]); // Snap to left
1158 int bottomOverhang = result[1] + spanY - countY;
1159 if (bottomOverhang > 0) {
1160 result[1] -= bottomOverhang; // Snap to bottom
1161 }
1162 result[1] = Math.max(0, result[1]); // Snap to top
1163 }
1164
Adam Cohen482ed822012-03-02 14:15:13 -08001165 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1166 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001167 final int oldDragCellX = mDragCell[0];
1168 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001169
Winson Chungb8c69f32011-10-19 21:36:08 -07001170 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001171 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1172 } else {
1173 mDragCenter.set(originX, originY);
1174 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001175
Adam Cohen2801caf2011-05-13 20:57:39 -07001176 if (dragOutline == null && v == null) {
1177 if (mCrosshairsDrawable != null) {
1178 invalidate();
1179 }
1180 return;
1181 }
1182
Adam Cohen482ed822012-03-02 14:15:13 -08001183 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1184 mDragCell[0] = cellX;
1185 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001186 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001187 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001188 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001189
Joe Onorato4be866d2010-10-10 11:26:02 -07001190 int left = topLeft[0];
1191 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001192
Winson Chungb8c69f32011-10-19 21:36:08 -07001193 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001194 // When drawing the drag outline, it did not account for margin offsets
1195 // added by the view's parent.
1196 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1197 left += lp.leftMargin;
1198 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001199
Adam Cohen99e8b402011-03-25 19:23:43 -07001200 // Offsets due to the size difference between the View and the dragOutline.
1201 // There is a size difference to account for the outer blur, which may lie
1202 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001203 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001204 // We center about the x axis
1205 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1206 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001207 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001208 if (dragOffset != null && dragRegion != null) {
1209 // Center the drag region *horizontally* in the cell and apply a drag
1210 // outline offset
1211 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1212 - dragRegion.width()) / 2;
1213 top += dragOffset.y;
1214 } else {
1215 // Center the drag outline in the cell
1216 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1217 - dragOutline.getWidth()) / 2;
1218 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1219 - dragOutline.getHeight()) / 2;
1220 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001221 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001222 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001223 mDragOutlineAnims[oldIndex].animateOut();
1224 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001225 Rect r = mDragOutlines[mDragOutlineCurrent];
1226 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1227 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001228 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001229 }
Winson Chung150fbab2010-09-29 17:14:26 -07001230
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001231 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1232 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001233 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001234
1235 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1236 if (mCrosshairsDrawable != null) {
1237 invalidate();
1238 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001239 }
1240
Adam Cohene0310962011-04-18 16:15:31 -07001241 public void clearDragOutlines() {
1242 final int oldIndex = mDragOutlineCurrent;
1243 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001244 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001245 }
1246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001248 * Find a vacant area that will fit the given bounds nearest the requested
1249 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001250 *
Romain Guy51afc022009-05-04 18:03:43 -07001251 * @param pixelX The X location at which you want to search for a vacant area.
1252 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001253 * @param spanX Horizontal span of the object.
1254 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001255 * @param result Array in which to place the result, or null (in which case a new array will
1256 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001257 * @return The X, Y cell of a vacant area that can contain this object,
1258 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001260 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1261 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001262 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001263 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001264
Michael Jurka6a1435d2010-09-27 17:35:12 -07001265 /**
1266 * Find a vacant area that will fit the given bounds nearest the requested
1267 * cell location. Uses Euclidean distance to score multiple vacant areas.
1268 *
1269 * @param pixelX The X location at which you want to search for a vacant area.
1270 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001271 * @param minSpanX The minimum horizontal span required
1272 * @param minSpanY The minimum vertical span required
1273 * @param spanX Horizontal span of the object.
1274 * @param spanY Vertical span of the object.
1275 * @param result Array in which to place the result, or null (in which case a new array will
1276 * be allocated)
1277 * @return The X, Y cell of a vacant area that can contain this object,
1278 * nearest the requested location.
1279 */
1280 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1281 int spanY, int[] result, int[] resultSpan) {
1282 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1283 result, resultSpan);
1284 }
1285
1286 /**
1287 * Find a vacant area that will fit the given bounds nearest the requested
1288 * cell location. Uses Euclidean distance to score multiple vacant areas.
1289 *
1290 * @param pixelX The X location at which you want to search for a vacant area.
1291 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001292 * @param spanX Horizontal span of the object.
1293 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001294 * @param ignoreOccupied If true, the result can be an occupied cell
1295 * @param result Array in which to place the result, or null (in which case a new array will
1296 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001297 * @return The X, Y cell of a vacant area that can contain this object,
1298 * nearest the requested location.
1299 */
Adam Cohendf035382011-04-11 17:22:04 -07001300 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1301 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001302 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001303 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001304 }
1305
1306 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1307 private void lazyInitTempRectStack() {
1308 if (mTempRectStack.isEmpty()) {
1309 for (int i = 0; i < mCountX * mCountY; i++) {
1310 mTempRectStack.push(new Rect());
1311 }
1312 }
1313 }
Adam Cohen482ed822012-03-02 14:15:13 -08001314
Adam Cohend41fbf52012-02-16 23:53:59 -08001315 private void recycleTempRects(Stack<Rect> used) {
1316 while (!used.isEmpty()) {
1317 mTempRectStack.push(used.pop());
1318 }
1319 }
1320
1321 /**
1322 * Find a vacant area that will fit the given bounds nearest the requested
1323 * cell location. Uses Euclidean distance to score multiple vacant areas.
1324 *
1325 * @param pixelX The X location at which you want to search for a vacant area.
1326 * @param pixelY The Y location at which you want to search for a vacant area.
1327 * @param minSpanX The minimum horizontal span required
1328 * @param minSpanY The minimum vertical span required
1329 * @param spanX Horizontal span of the object.
1330 * @param spanY Vertical span of the object.
1331 * @param ignoreOccupied If true, the result can be an occupied cell
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[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001338 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1339 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001340 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001341 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001342 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001343
Adam Cohene3e27a82011-04-15 12:07:39 -07001344 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1345 // to the center of the item, but we are searching based on the top-left cell, so
1346 // we translate the point over to correspond to the top-left.
1347 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1348 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1349
Jeff Sharkey70864282009-04-07 21:08:40 -07001350 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001351 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001352 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001353 final Rect bestRect = new Rect(-1, -1, -1, -1);
1354 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001355
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001356 final int countX = mCountX;
1357 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001358
Adam Cohend41fbf52012-02-16 23:53:59 -08001359 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1360 spanX < minSpanX || spanY < minSpanY) {
1361 return bestXY;
1362 }
1363
1364 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001365 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001366 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1367 int ySize = -1;
1368 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001369 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001370 // First, let's see if this thing fits anywhere
1371 for (int i = 0; i < minSpanX; i++) {
1372 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001373 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001374 continue inner;
1375 }
Michael Jurkac28de512010-08-13 11:27:44 -07001376 }
1377 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001378 xSize = minSpanX;
1379 ySize = minSpanY;
1380
1381 // We know that the item will fit at _some_ acceptable size, now let's see
1382 // how big we can make it. We'll alternate between incrementing x and y spans
1383 // until we hit a limit.
1384 boolean incX = true;
1385 boolean hitMaxX = xSize >= spanX;
1386 boolean hitMaxY = ySize >= spanY;
1387 while (!(hitMaxX && hitMaxY)) {
1388 if (incX && !hitMaxX) {
1389 for (int j = 0; j < ySize; j++) {
1390 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1391 // We can't move out horizontally
1392 hitMaxX = true;
1393 }
1394 }
1395 if (!hitMaxX) {
1396 xSize++;
1397 }
1398 } else if (!hitMaxY) {
1399 for (int i = 0; i < xSize; i++) {
1400 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1401 // We can't move out vertically
1402 hitMaxY = true;
1403 }
1404 }
1405 if (!hitMaxY) {
1406 ySize++;
1407 }
1408 }
1409 hitMaxX |= xSize >= spanX;
1410 hitMaxY |= ySize >= spanY;
1411 incX = !incX;
1412 }
1413 incX = true;
1414 hitMaxX = xSize >= spanX;
1415 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001416 }
Winson Chung0be025d2011-05-23 17:45:09 -07001417 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001418 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419
Adam Cohend41fbf52012-02-16 23:53:59 -08001420 // We verify that the current rect is not a sub-rect of any of our previous
1421 // candidates. In this case, the current rect is disqualified in favour of the
1422 // containing rect.
1423 Rect currentRect = mTempRectStack.pop();
1424 currentRect.set(x, y, x + xSize, y + ySize);
1425 boolean contained = false;
1426 for (Rect r : validRegions) {
1427 if (r.contains(currentRect)) {
1428 contained = true;
1429 break;
1430 }
1431 }
1432 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001433 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1434 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001435
Adam Cohend41fbf52012-02-16 23:53:59 -08001436 if ((distance <= bestDistance && !contained) ||
1437 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001438 bestDistance = distance;
1439 bestXY[0] = x;
1440 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001441 if (resultSpan != null) {
1442 resultSpan[0] = xSize;
1443 resultSpan[1] = ySize;
1444 }
1445 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
1448 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001449 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001450 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451
Adam Cohenc0dcf592011-06-01 15:30:43 -07001452 // Return -1, -1 if no suitable location found
1453 if (bestDistance == Double.MAX_VALUE) {
1454 bestXY[0] = -1;
1455 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001456 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001457 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001458 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001460
Adam Cohen482ed822012-03-02 14:15:13 -08001461 /**
1462 * Find a vacant area that will fit the given bounds nearest the requested
1463 * cell location, and will also weigh in a suggested direction vector of the
1464 * desired location. This method computers distance based on unit grid distances,
1465 * not pixel distances.
1466 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001467 * @param cellX The X cell nearest to which you want to search for a vacant area.
1468 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001469 * @param spanX Horizontal span of the object.
1470 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001471 * @param direction The favored direction in which the views should move from x, y
1472 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1473 * matches exactly. Otherwise we find the best matching direction.
1474 * @param occoupied The array which represents which cells in the CellLayout are occupied
1475 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1476 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001477 * @param result Array in which to place the result, or null (in which case a new array will
1478 * be allocated)
1479 * @return The X, Y cell of a vacant area that can contain this object,
1480 * nearest the requested location.
1481 */
1482 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001483 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001484 // Keep track of best-scoring drop area
1485 final int[] bestXY = result != null ? result : new int[2];
1486 float bestDistance = Float.MAX_VALUE;
1487 int bestDirectionScore = Integer.MIN_VALUE;
1488
1489 final int countX = mCountX;
1490 final int countY = mCountY;
1491
1492 for (int y = 0; y < countY - (spanY - 1); y++) {
1493 inner:
1494 for (int x = 0; x < countX - (spanX - 1); x++) {
1495 // First, let's see if this thing fits anywhere
1496 for (int i = 0; i < spanX; i++) {
1497 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001498 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001499 continue inner;
1500 }
1501 }
1502 }
1503
1504 float distance = (float)
1505 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1506 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001507 computeDirectionVector(x - cellX, y - cellY, curDirection);
1508 // The direction score is just the dot product of the two candidate direction
1509 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001510 int curDirectionScore = direction[0] * curDirection[0] +
1511 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001512 boolean exactDirectionOnly = false;
1513 boolean directionMatches = direction[0] == curDirection[0] &&
1514 direction[0] == curDirection[0];
1515 if ((directionMatches || !exactDirectionOnly) &&
1516 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001517 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1518 bestDistance = distance;
1519 bestDirectionScore = curDirectionScore;
1520 bestXY[0] = x;
1521 bestXY[1] = y;
1522 }
1523 }
1524 }
1525
1526 // Return -1, -1 if no suitable location found
1527 if (bestDistance == Float.MAX_VALUE) {
1528 bestXY[0] = -1;
1529 bestXY[1] = -1;
1530 }
1531 return bestXY;
1532 }
1533
Adam Cohen47a876d2012-03-19 13:21:41 -07001534 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1535 int[] direction,boolean[][] occupied,
1536 boolean blockOccupied[][], int[] result) {
1537 // Keep track of best-scoring drop area
1538 final int[] bestXY = result != null ? result : new int[2];
1539 bestXY[0] = -1;
1540 bestXY[1] = -1;
1541 float bestDistance = Float.MAX_VALUE;
1542
1543 // We use this to march in a single direction
1544 if (direction[0] != 0 && direction[1] != 0) {
1545 return bestXY;
1546 }
1547
1548 // This will only incrememnet one of x or y based on the assertion above
1549 int x = cellX + direction[0];
1550 int y = cellY + direction[1];
1551 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1552
1553 boolean fail = false;
1554 for (int i = 0; i < spanX; i++) {
1555 for (int j = 0; j < spanY; j++) {
1556 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1557 fail = true;
1558 }
1559 }
1560 }
1561 if (!fail) {
1562 float distance = (float)
1563 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1564 if (Float.compare(distance, bestDistance) < 0) {
1565 bestDistance = distance;
1566 bestXY[0] = x;
1567 bestXY[1] = y;
1568 }
1569 }
1570 x += direction[0];
1571 y += direction[1];
1572 }
1573 return bestXY;
1574 }
1575
Adam Cohen482ed822012-03-02 14:15:13 -08001576 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
1577 int[] direction) {
1578 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1579 boolean success = false;
1580 markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
1581 lp.cellVSpan, mTmpOccupied, false);
1582 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1583
1584 findNearestArea(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan,
Adam Cohen47a876d2012-03-19 13:21:41 -07001585 direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001586
1587 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
1588 lp.tmpCellX = mTempLocation[0];
1589 lp.tmpCellY = mTempLocation[1];
1590 success = true;
1591
1592 }
1593 markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
1594 lp.cellVSpan, mTmpOccupied, true);
1595 return success;
1596 }
1597
Adam Cohen47a876d2012-03-19 13:21:41 -07001598 // This method looks in the specified direction to see if there is an additional view
1599 // immediately adjecent in that direction
1600 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
1601 boolean[][] occupied) {
1602 boolean found = false;
1603
Michael Jurkaa52570f2012-03-20 03:18:20 -07001604 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001605 Rect r0 = new Rect(boundingRect);
1606 Rect r1 = new Rect();
1607
1608 int deltaX = 0;
1609 int deltaY = 0;
1610 if (direction[1] < 0) {
1611 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1612 deltaY = -1;
1613 } else if (direction[1] > 0) {
1614 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1615 deltaY = 1;
1616 } else if (direction[0] < 0) {
1617 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1618 deltaX = -1;
1619 } else if (direction[0] > 0) {
1620 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1621 deltaX = 1;
1622 }
1623
1624 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001625 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen47a876d2012-03-19 13:21:41 -07001626 if (views.contains(child)) continue;
1627 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1628
1629 r1.set(lp.tmpCellX, lp.tmpCellY, lp.tmpCellX + lp.cellHSpan, lp.tmpCellY + lp.cellVSpan);
1630 if (Rect.intersects(r0, r1)) {
1631 if (!lp.canReorder) {
1632 return false;
1633 }
1634 boolean pushed = false;
1635 for (int x = lp.tmpCellX; x < lp.tmpCellX + lp.cellHSpan; x++) {
1636 for (int y = lp.tmpCellY; y < lp.tmpCellY + lp.cellVSpan; y++) {
1637 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1638 && y - deltaY >= 0 && y - deltaY < mCountY;
1639 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1640 pushed = true;
1641 }
1642 }
1643 }
1644 if (pushed) {
1645 views.add(child);
1646 boundingRect.union(lp.tmpCellX, lp.tmpCellY, lp.tmpCellX + lp.cellHSpan,
1647 lp.tmpCellY + lp.cellVSpan);
1648 found = true;
1649 }
1650 }
1651 }
1652 return found;
1653 }
1654
1655 private boolean pushViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1656 int[] direction) {
1657 if (views.size() == 0) return true;
1658
1659
1660 boolean success = false;
1661
1662 // We construct a rect which represents the entire group of views
1663 Rect boundingRect = null;
1664 for (View v: views) {
1665 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1666 if (boundingRect == null) {
1667 boundingRect = new Rect(lp.tmpCellX, lp.tmpCellY, lp.tmpCellX + lp.cellHSpan,
1668 lp.tmpCellY + lp.cellVSpan);
1669 } else {
1670 boundingRect.union(lp.tmpCellX, lp.tmpCellY, lp.tmpCellX + lp.cellHSpan,
1671 lp.tmpCellY + lp.cellVSpan);
1672 }
1673 }
1674
1675 ArrayList<View> dup = (ArrayList<View>) views.clone();
1676 while (addViewInDirection(dup, boundingRect, direction, mTmpOccupied)) {
1677 }
1678 for (View v: dup) {
1679 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1680 markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
1681 lp.cellVSpan, mTmpOccupied, false);
1682 }
1683
1684 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1685 int top = boundingRect.top;
1686 int left = boundingRect.left;
1687 for (View v: dup) {
1688 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1689 markCellsForView(lp.tmpCellX - left, lp.tmpCellY - top, lp.cellHSpan,
1690 lp.cellVSpan, blockOccupied, true);
1691 }
1692
1693 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1694
1695 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1696 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1697
1698 int deltaX = mTempLocation[0] - boundingRect.left;
1699 int deltaY = mTempLocation[1] - boundingRect.top;
1700 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
1701 for (View v: dup) {
1702 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1703 lp.tmpCellX += deltaX;
1704 lp.tmpCellY += deltaY;
1705 }
1706 success = true;
1707 }
1708 for (View v: dup) {
1709 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1710 markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
1711 lp.cellVSpan, mTmpOccupied, true);
1712 }
1713 return success;
1714 }
1715
Adam Cohen482ed822012-03-02 14:15:13 -08001716 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1717 int[] direction) {
1718 if (views.size() == 0) return true;
1719 boolean success = false;
1720
1721 // We construct a rect which represents the entire group of views
1722 Rect boundingRect = null;
1723 for (View v: views) {
1724 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1725 markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
1726 lp.cellVSpan, mTmpOccupied, false);
1727 if (boundingRect == null) {
1728 boundingRect = new Rect(lp.tmpCellX, lp.tmpCellY, lp.tmpCellX + lp.cellHSpan,
1729 lp.tmpCellY + lp.cellVSpan);
1730 } else {
1731 boundingRect.union(lp.tmpCellX, lp.tmpCellY, lp.tmpCellX + lp.cellHSpan,
1732 lp.tmpCellY + lp.cellVSpan);
1733 }
1734 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001735 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1736 int top = boundingRect.top;
1737 int left = boundingRect.left;
1738 for (View v: views) {
1739 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1740 markCellsForView(lp.tmpCellX - left, lp.tmpCellY - top, lp.cellHSpan,
1741 lp.cellVSpan, blockOccupied, true);
1742 }
1743
Adam Cohen482ed822012-03-02 14:15:13 -08001744 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1745
1746 // TODO: this bounding rect may not be completely filled, lets be more precise about this
1747 // check.
Adam Cohen47a876d2012-03-19 13:21:41 -07001748 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1749 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001750
1751 int deltaX = mTempLocation[0] - boundingRect.left;
1752 int deltaY = mTempLocation[1] - boundingRect.top;
1753 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
1754 for (View v: views) {
1755 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1756 lp.tmpCellX += deltaX;
1757 lp.tmpCellY += deltaY;
1758 }
1759 success = true;
1760 }
1761 for (View v: views) {
1762 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1763 markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
1764 lp.cellVSpan, mTmpOccupied, true);
1765 }
1766 return success;
1767 }
1768
1769 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1770 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1771 }
1772
1773 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
1774 View ignoreView) {
1775 mIntersectingViews.clear();
1776
1777 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001778
1779 if (ignoreView != null) {
1780 LayoutParams lp = (LayoutParams) ignoreView.getLayoutParams();
1781 lp.tmpCellX = cellX;
1782 lp.tmpCellY = cellY;
1783 }
1784
Michael Jurkaa52570f2012-03-20 03:18:20 -07001785 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001786 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1787 Rect r1 = new Rect();
1788 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001789 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001790 if (child == ignoreView) continue;
1791 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1792 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
1793 if (Rect.intersects(r0, r1)) {
1794 if (!lp.canReorder) {
1795 return false;
1796 }
1797 mIntersectingViews.add(child);
1798 }
1799 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001800
1801 if (pushViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction)) {
1802 return true;
1803 }
1804 // Try the opposite direction
1805 direction[0] *= -1;
1806 direction[1] *= -1;
1807 if (pushViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction)) {
1808 return true;
1809 }
1810 // Switch the direction back
1811 direction[0] *= -1;
1812 direction[1] *= -1;
1813
Adam Cohen482ed822012-03-02 14:15:13 -08001814 // First we try moving the views as a block
1815 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction)) {
1816 return true;
1817 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001818
Adam Cohen482ed822012-03-02 14:15:13 -08001819 // Ok, they couldn't move as a block, let's move them individually
1820 for (View v : mIntersectingViews) {
1821 if (!addViewToTempLocation(v, mOccupiedRect, direction)) {
1822 return false;
1823 }
1824 }
1825 return true;
1826 }
1827
1828 /*
1829 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1830 * the provided point and the provided cell
1831 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001832 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001833 double angle = Math.atan(((float) deltaY) / deltaX);
1834
1835 result[0] = 0;
1836 result[1] = 0;
1837 if (Math.abs(Math.cos(angle)) > 0.5f) {
1838 result[0] = (int) Math.signum(deltaX);
1839 }
1840 if (Math.abs(Math.sin(angle)) > 0.5f) {
1841 result[1] = (int) Math.signum(deltaY);
1842 }
1843 }
1844
1845 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1846 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
1847 // This creates a copy of the current occupied array, omitting the current view being
1848 // dragged
1849 resetTempLayoutToCurrent(dragView);
1850
1851 // We find the nearest cell into which we would place the dragged item, assuming there's
1852 // nothing in its way.
1853 int result[] = new int[2];
1854 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1855
1856 boolean success = false;
1857 // First we try the exact nearest position of the item being dragged,
1858 // we will then want to try to move this around to other neighbouring positions
1859 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView);
1860
1861 if (!success) {
1862 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1863 // x, then 1 in y etc.
1864 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1865 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1866 dragView, false, solution);
1867 } else if (spanY > minSpanY) {
1868 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1869 dragView, true, solution);
1870 }
1871 solution.isSolution = false;
1872 } else {
1873 solution.isSolution = true;
1874 solution.dragViewX = result[0];
1875 solution.dragViewY = result[1];
1876 solution.dragViewSpanX = spanX;
1877 solution.dragViewSpanY = spanY;
1878 copyCurrentStateToSolution(solution, true);
1879 }
1880 return solution;
1881 }
1882
1883 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001884 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001885 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001886 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001887 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1888 Point p;
1889 if (temp) {
1890 p = new Point(lp.tmpCellX, lp.tmpCellY);
1891 } else {
1892 p = new Point(lp.cellX, lp.cellY);
1893 }
1894 solution.map.put(child, p);
1895 }
1896 }
1897
1898 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1899 for (int i = 0; i < mCountX; i++) {
1900 for (int j = 0; j < mCountY; j++) {
1901 mTmpOccupied[i][j] = false;
1902 }
1903 }
1904
Michael Jurkaa52570f2012-03-20 03:18:20 -07001905 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001906 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001907 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001908 if (child == dragView) continue;
1909 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1910 Point p = solution.map.get(child);
1911 if (p != null) {
1912 lp.tmpCellX = p.x;
1913 lp.tmpCellY = p.y;
1914 markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan,
1915 mTmpOccupied, true);
1916 }
1917 }
1918 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1919 solution.dragViewSpanY, mTmpOccupied, true);
1920 }
1921
1922 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1923 commitDragView) {
1924
1925 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1926 for (int i = 0; i < mCountX; i++) {
1927 for (int j = 0; j < mCountY; j++) {
1928 occupied[i][j] = false;
1929 }
1930 }
1931
Michael Jurkaa52570f2012-03-20 03:18:20 -07001932 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001933 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001934 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001935 if (child == dragView) continue;
1936 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1937 Point p = solution.map.get(child);
1938 if (p != null) {
1939 if (lp.cellX != p.x || lp.cellY != p.y) {
1940 animateChildToPosition(child, p.x, p.y, 150, 0, DESTRUCTIVE_REORDER, false);
1941 }
1942 markCellsForView(p.x, p.y, lp.cellHSpan, lp.cellVSpan, occupied, true);
1943 }
1944 }
1945 if (commitDragView) {
1946 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1947 solution.dragViewSpanY, occupied, true);
1948 }
1949 }
1950
1951 private void commitTempPlacement() {
1952 for (int i = 0; i < mCountX; i++) {
1953 for (int j = 0; j < mCountY; j++) {
1954 mOccupied[i][j] = mTmpOccupied[i][j];
1955 }
1956 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001957 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001958 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001959 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08001960 lp.cellX = lp.tmpCellX;
1961 lp.cellY = lp.tmpCellY;
1962 }
1963 }
1964
1965 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001966 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001967 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001968 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08001969 lp.useTmpCoords = useTempCoords;
1970 }
1971 }
1972
1973 private void resetTempLayoutToCurrent(View ignoreView) {
1974 for (int i = 0; i < mCountX; i++) {
1975 for (int j = 0; j < mCountY; j++) {
1976 mTmpOccupied[i][j] = mOccupied[i][j];
1977 }
1978 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001979 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001980 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001981 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001982 if (child == ignoreView) continue;
1983 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1984 lp.tmpCellX = lp.cellX;
1985 lp.tmpCellY = lp.cellY;
1986 }
1987 }
1988
1989 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
1990 int spanX, int spanY, View dragView, ItemConfiguration solution) {
1991 int[] result = new int[2];
1992 int[] resultSpan = new int[2];
1993 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
1994 resultSpan);
1995 if (result[0] >= 0 && result[1] >= 0) {
1996 copyCurrentStateToSolution(solution, false);
1997 solution.dragViewX = result[0];
1998 solution.dragViewY = result[1];
1999 solution.dragViewSpanX = resultSpan[0];
2000 solution.dragViewSpanY = resultSpan[1];
2001 solution.isSolution = true;
2002 } else {
2003 solution.isSolution = false;
2004 }
2005 return solution;
2006 }
2007
2008 public void prepareChildForDrag(View child) {
2009 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08002010 }
2011
2012 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
2013 View dragView, int[] result, int resultSpan[], int mode) {
2014
2015 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07002016 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08002017
2018 if (resultSpan == null) {
2019 resultSpan = new int[2];
2020 }
2021
2022 // We attempt the first algorithm
Adam Cohen47a876d2012-03-19 13:21:41 -07002023 regionToCenterPoint(result[0], result[1], spanX, spanY, mTmpPoint);
2024 computeDirectionVector((mTmpPoint[0] - pixelX) / spanX, (mTmpPoint[1] - pixelY) / spanY,
2025 mDirectionVector);
Adam Cohen482ed822012-03-02 14:15:13 -08002026 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
2027 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
2028
2029 // We attempt the approach which doesn't shuffle views at all
2030 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
2031 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
2032
2033 ItemConfiguration finalSolution = null;
2034 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
2035 finalSolution = swapSolution;
2036 } else if (noShuffleSolution.isSolution) {
2037 finalSolution = noShuffleSolution;
2038 }
2039
2040 boolean foundSolution = true;
2041 if (!DESTRUCTIVE_REORDER) {
2042 setUseTempCoords(true);
2043 }
2044
2045 if (finalSolution != null) {
2046 result[0] = finalSolution.dragViewX;
2047 result[1] = finalSolution.dragViewY;
2048 resultSpan[0] = finalSolution.dragViewSpanX;
2049 resultSpan[1] = finalSolution.dragViewSpanY;
2050
2051 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2052 // committing anything or animating anything as we just want to determine if a solution
2053 // exists
2054 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2055 if (!DESTRUCTIVE_REORDER) {
2056 copySolutionToTempState(finalSolution, dragView);
2057 }
2058 setItemPlacementDirty(true);
2059 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2060
2061 if (!DESTRUCTIVE_REORDER && mode == MODE_ON_DROP) {
2062 commitTempPlacement();
2063 }
2064 }
2065 } else {
2066 foundSolution = false;
2067 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2068 }
2069
2070 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2071 setUseTempCoords(false);
2072 }
2073 boolean[][] occupied = mOccupied;
2074
Michael Jurkaa52570f2012-03-20 03:18:20 -07002075 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002076 return result;
2077 }
2078
2079 public boolean isItemPlacementDirty() {
2080 return mItemLocationsDirty;
2081 }
2082
2083 public void setItemPlacementDirty(boolean dirty) {
2084 mItemLocationsDirty = dirty;
2085 }
2086
2087 private class ItemConfiguration {
2088 HashMap<View, Point> map = new HashMap<View, Point>();
2089 boolean isSolution = false;
2090 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2091
2092 int area() {
2093 return dragViewSpanX * dragViewSpanY;
2094 }
2095 void clear() {
2096 map.clear();
2097 isSolution = false;
2098 }
2099 }
2100
Adam Cohendf035382011-04-11 17:22:04 -07002101 /**
2102 * Find a vacant area that will fit the given bounds nearest the requested
2103 * cell location. Uses Euclidean distance to score multiple vacant areas.
2104 *
2105 * @param pixelX The X location at which you want to search for a vacant area.
2106 * @param pixelY The Y location at which you want to search for a vacant area.
2107 * @param spanX Horizontal span of the object.
2108 * @param spanY Vertical span of the object.
2109 * @param ignoreView Considers space occupied by this view as unoccupied
2110 * @param result Previously returned value to possibly recycle.
2111 * @return The X, Y cell of a vacant area that can contain this object,
2112 * nearest the requested location.
2113 */
2114 int[] findNearestVacantArea(
2115 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2116 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2117 }
2118
2119 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002120 * Find a vacant area that will fit the given bounds nearest the requested
2121 * cell location. Uses Euclidean distance to score multiple vacant areas.
2122 *
2123 * @param pixelX The X location at which you want to search for a vacant area.
2124 * @param pixelY The Y location at which you want to search for a vacant area.
2125 * @param minSpanX The minimum horizontal span required
2126 * @param minSpanY The minimum vertical span required
2127 * @param spanX Horizontal span of the object.
2128 * @param spanY Vertical span of the object.
2129 * @param ignoreView Considers space occupied by this view as unoccupied
2130 * @param result Previously returned value to possibly recycle.
2131 * @return The X, Y cell of a vacant area that can contain this object,
2132 * nearest the requested location.
2133 */
2134 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2135 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002136 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2137 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002138 }
2139
2140 /**
Adam Cohendf035382011-04-11 17:22:04 -07002141 * Find a starting cell position that will fit the given bounds nearest the requested
2142 * cell location. Uses Euclidean distance to score multiple vacant areas.
2143 *
2144 * @param pixelX The X location at which you want to search for a vacant area.
2145 * @param pixelY The Y location at which you want to search for a vacant area.
2146 * @param spanX Horizontal span of the object.
2147 * @param spanY Vertical span of the object.
2148 * @param ignoreView Considers space occupied by this view as unoccupied
2149 * @param result Previously returned value to possibly recycle.
2150 * @return The X, Y cell of a vacant area that can contain this object,
2151 * nearest the requested location.
2152 */
2153 int[] findNearestArea(
2154 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2155 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2156 }
2157
Michael Jurka0280c3b2010-09-17 15:00:07 -07002158 boolean existsEmptyCell() {
2159 return findCellForSpan(null, 1, 1);
2160 }
2161
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002163 * Finds the upper-left coordinate of the first rectangle in the grid that can
2164 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2165 * then this method will only return coordinates for rectangles that contain the cell
2166 * (intersectX, intersectY)
2167 *
2168 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2169 * can be found.
2170 * @param spanX The horizontal span of the cell we want to find.
2171 * @param spanY The vertical span of the cell we want to find.
2172 *
2173 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002174 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002175 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002176 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002177 }
2178
2179 /**
2180 * Like above, but ignores any cells occupied by the item "ignoreView"
2181 *
2182 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2183 * can be found.
2184 * @param spanX The horizontal span of the cell we want to find.
2185 * @param spanY The vertical span of the cell we want to find.
2186 * @param ignoreView The home screen item we should treat as not occupying any space
2187 * @return
2188 */
2189 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002190 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2191 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002192 }
2193
2194 /**
2195 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2196 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2197 *
2198 * @param spanX The horizontal span of the cell we want to find.
2199 * @param spanY The vertical span of the cell we want to find.
2200 * @param ignoreView The home screen item we should treat as not occupying any space
2201 * @param intersectX The X coordinate of the cell that we should try to overlap
2202 * @param intersectX The Y coordinate of the cell that we should try to overlap
2203 *
2204 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2205 */
2206 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2207 int intersectX, int intersectY) {
2208 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002209 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002210 }
2211
2212 /**
2213 * The superset of the above two methods
2214 */
2215 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002216 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002217 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002218 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002219
Michael Jurka28750fb2010-09-24 17:43:49 -07002220 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002221 while (true) {
2222 int startX = 0;
2223 if (intersectX >= 0) {
2224 startX = Math.max(startX, intersectX - (spanX - 1));
2225 }
2226 int endX = mCountX - (spanX - 1);
2227 if (intersectX >= 0) {
2228 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2229 }
2230 int startY = 0;
2231 if (intersectY >= 0) {
2232 startY = Math.max(startY, intersectY - (spanY - 1));
2233 }
2234 int endY = mCountY - (spanY - 1);
2235 if (intersectY >= 0) {
2236 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2237 }
2238
Winson Chungbbc60d82010-11-11 16:34:41 -08002239 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002240 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002241 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002242 for (int i = 0; i < spanX; i++) {
2243 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002244 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002245 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002246 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002247 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002248 continue inner;
2249 }
2250 }
2251 }
2252 if (cellXY != null) {
2253 cellXY[0] = x;
2254 cellXY[1] = y;
2255 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002256 foundCell = true;
2257 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002258 }
2259 }
2260 if (intersectX == -1 && intersectY == -1) {
2261 break;
2262 } else {
2263 // if we failed to find anything, try again but without any requirements of
2264 // intersecting
2265 intersectX = -1;
2266 intersectY = -1;
2267 continue;
2268 }
2269 }
2270
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002271 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002272 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002273 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002274 }
2275
2276 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002277 * A drag event has begun over this layout.
2278 * It may have begun over this layout (in which case onDragChild is called first),
2279 * or it may have begun on another layout.
2280 */
2281 void onDragEnter() {
2282 if (!mDragging) {
2283 // Fade in the drag indicators
2284 if (mCrosshairsAnimator != null) {
2285 mCrosshairsAnimator.animateIn();
2286 }
2287 }
2288 mDragging = true;
2289 }
2290
2291 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002292 * Called when drag has left this CellLayout or has been completed (successfully or not)
2293 */
2294 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002295 // This can actually be called when we aren't in a drag, e.g. when adding a new
2296 // item to this layout via the customize drawer.
2297 // Guard against that case.
2298 if (mDragging) {
2299 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002300
Joe Onorato4be866d2010-10-10 11:26:02 -07002301 // Fade out the drag indicators
2302 if (mCrosshairsAnimator != null) {
2303 mCrosshairsAnimator.animateOut();
2304 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002305 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002306
2307 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002308 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002309 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2310 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
2311
Michael Jurka33945b22010-12-21 18:19:38 -08002312 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002313 }
2314
2315 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002316 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002317 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002318 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 *
2320 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002322 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002323 if (child != null) {
2324 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002325 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002326 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328 }
2329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002331 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002332 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 * @param cellX X coordinate of upper left corner expressed as a cell position
2334 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002335 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002337 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002339 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340 final int cellWidth = mCellWidth;
2341 final int cellHeight = mCellHeight;
2342 final int widthGap = mWidthGap;
2343 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002344
Winson Chung4b825dcd2011-06-19 12:41:22 -07002345 final int hStartPadding = getPaddingLeft();
2346 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002347
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2349 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2350
2351 int x = hStartPadding + cellX * (cellWidth + widthGap);
2352 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002353
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002354 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002356
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002357 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002358 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002359 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002360 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361 * @param width Width in pixels
2362 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002363 * @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 -08002364 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002365 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002366 return rectToCell(getResources(), width, height, result);
2367 }
2368
2369 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 // Always assume we're working with the smallest span to make sure we
2371 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002372 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2373 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002375
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002377 int spanX = (int) Math.ceil(width / (float) smallerSize);
2378 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002379
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002380 if (result == null) {
2381 return new int[] { spanX, spanY };
2382 }
2383 result[0] = spanX;
2384 result[1] = spanY;
2385 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 }
2387
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002388 public int[] cellSpansToSize(int hSpans, int vSpans) {
2389 int[] size = new int[2];
2390 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2391 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2392 return size;
2393 }
2394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002396 * Calculate the grid spans needed to fit given item
2397 */
2398 public void calculateSpans(ItemInfo info) {
2399 final int minWidth;
2400 final int minHeight;
2401
2402 if (info instanceof LauncherAppWidgetInfo) {
2403 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2404 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2405 } else if (info instanceof PendingAddWidgetInfo) {
2406 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2407 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2408 } else {
2409 // It's not a widget, so it must be 1x1
2410 info.spanX = info.spanY = 1;
2411 return;
2412 }
2413 int[] spans = rectToCell(minWidth, minHeight, null);
2414 info.spanX = spans[0];
2415 info.spanY = spans[1];
2416 }
2417
2418 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 * Find the first vacant cell, if there is one.
2420 *
2421 * @param vacant Holds the x and y coordinate of the vacant cell
2422 * @param spanX Horizontal cell span.
2423 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002424 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 * @return True if a vacant cell was found
2426 */
2427 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428
Michael Jurka0280c3b2010-09-17 15:00:07 -07002429 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002430 }
2431
2432 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2433 int xCount, int yCount, boolean[][] occupied) {
2434
Adam Cohen2801caf2011-05-13 20:57:39 -07002435 for (int y = 0; y < yCount; y++) {
2436 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 boolean available = !occupied[x][y];
2438out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2439 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2440 available = available && !occupied[i][j];
2441 if (!available) break out;
2442 }
2443 }
2444
2445 if (available) {
2446 vacant[0] = x;
2447 vacant[1] = y;
2448 return true;
2449 }
2450 }
2451 }
2452
2453 return false;
2454 }
2455
Michael Jurka0280c3b2010-09-17 15:00:07 -07002456 private void clearOccupiedCells() {
2457 for (int x = 0; x < mCountX; x++) {
2458 for (int y = 0; y < mCountY; y++) {
2459 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002460 }
2461 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002462 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002463
Adam Cohen1b607ed2011-03-03 17:26:50 -08002464 /**
2465 * Given a view, determines how much that view can be expanded in all directions, in terms of
2466 * whether or not there are other items occupying adjacent cells. Used by the
2467 * AppWidgetResizeFrame to determine how the widget can be resized.
2468 */
Adam Cohend4844c32011-02-18 19:25:06 -08002469 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08002470 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08002471 boolean flag;
2472
Adam Cohen1b607ed2011-03-03 17:26:50 -08002473 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002474 for (int x = lp.cellX - 1; x >= 0; x--) {
2475 flag = false;
2476 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
2477 if (mOccupied[x][y]) flag = true;
2478 }
2479 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002480 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08002481 }
2482
Adam Cohen1b607ed2011-03-03 17:26:50 -08002483 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002484 for (int y = lp.cellY - 1; y >= 0; y--) {
2485 flag = false;
2486 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
2487 if (mOccupied[x][y]) flag = true;
2488 }
2489 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002490 expandability[AppWidgetResizeFrame.TOP]++;
2491 }
Adam Cohend4844c32011-02-18 19:25:06 -08002492
Adam Cohen1b607ed2011-03-03 17:26:50 -08002493 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002494 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
2495 flag = false;
2496 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
2497 if (mOccupied[x][y]) flag = true;
2498 }
2499 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002500 expandability[AppWidgetResizeFrame.RIGHT]++;
2501 }
Adam Cohend4844c32011-02-18 19:25:06 -08002502
Adam Cohen1b607ed2011-03-03 17:26:50 -08002503 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002504 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
2505 flag = false;
2506 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
2507 if (mOccupied[x][y]) flag = true;
2508 }
2509 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002510 expandability[AppWidgetResizeFrame.BOTTOM]++;
2511 }
Adam Cohend4844c32011-02-18 19:25:06 -08002512 }
2513
Adam Cohend41fbf52012-02-16 23:53:59 -08002514 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002515 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002516 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002517 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518
Adam Cohend4844c32011-02-18 19:25:06 -08002519 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002520 markCellsAsOccupiedForView(view, mOccupied);
2521 }
2522 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002523 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002524 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002525 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002526 }
2527
Adam Cohend4844c32011-02-18 19:25:06 -08002528 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002529 markCellsAsUnoccupiedForView(view, mOccupied);
2530 }
2531 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002532 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002533 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002534 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002535 }
2536
Adam Cohen482ed822012-03-02 14:15:13 -08002537 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2538 boolean value) {
2539 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002540 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2541 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002542 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 }
2544 }
2545 }
2546
Adam Cohen2801caf2011-05-13 20:57:39 -07002547 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002548 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002549 (Math.max((mCountX - 1), 0) * mWidthGap);
2550 }
2551
2552 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002553 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002554 (Math.max((mCountY - 1), 0) * mHeightGap);
2555 }
2556
Michael Jurka66d72172011-04-12 16:29:25 -07002557 public boolean isOccupied(int x, int y) {
2558 if (x < mCountX && y < mCountY) {
2559 return mOccupied[x][y];
2560 } else {
2561 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2562 }
2563 }
2564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002565 @Override
2566 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2567 return new CellLayout.LayoutParams(getContext(), attrs);
2568 }
2569
2570 @Override
2571 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2572 return p instanceof CellLayout.LayoutParams;
2573 }
2574
2575 @Override
2576 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2577 return new CellLayout.LayoutParams(p);
2578 }
2579
Winson Chungaafa03c2010-06-11 17:34:16 -07002580 public static class CellLayoutAnimationController extends LayoutAnimationController {
2581 public CellLayoutAnimationController(Animation animation, float delay) {
2582 super(animation, delay);
2583 }
2584
2585 @Override
2586 protected long getDelayForView(View view) {
2587 return (int) (Math.random() * 150);
2588 }
2589 }
2590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2592 /**
2593 * Horizontal location of the item in the grid.
2594 */
2595 @ViewDebug.ExportedProperty
2596 public int cellX;
2597
2598 /**
2599 * Vertical location of the item in the grid.
2600 */
2601 @ViewDebug.ExportedProperty
2602 public int cellY;
2603
2604 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002605 * Temporary horizontal location of the item in the grid during reorder
2606 */
2607 public int tmpCellX;
2608
2609 /**
2610 * Temporary vertical location of the item in the grid during reorder
2611 */
2612 public int tmpCellY;
2613
2614 /**
2615 * Indicates that the temporary coordinates should be used to layout the items
2616 */
2617 public boolean useTmpCoords;
2618
2619 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002620 * Number of cells spanned horizontally by the item.
2621 */
2622 @ViewDebug.ExportedProperty
2623 public int cellHSpan;
2624
2625 /**
2626 * Number of cells spanned vertically by the item.
2627 */
2628 @ViewDebug.ExportedProperty
2629 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002630
Adam Cohen1b607ed2011-03-03 17:26:50 -08002631 /**
2632 * Indicates whether the item will set its x, y, width and height parameters freely,
2633 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2634 */
Adam Cohend4844c32011-02-18 19:25:06 -08002635 public boolean isLockedToGrid = true;
2636
Adam Cohen482ed822012-03-02 14:15:13 -08002637 /**
2638 * Indicates whether this item can be reordered. Always true except in the case of the
2639 * the AllApps button.
2640 */
2641 public boolean canReorder = true;
2642
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002643 // X coordinate of the view in the layout.
2644 @ViewDebug.ExportedProperty
2645 int x;
2646 // Y coordinate of the view in the layout.
2647 @ViewDebug.ExportedProperty
2648 int y;
2649
Romain Guy84f296c2009-11-04 15:00:44 -08002650 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002652 public LayoutParams(Context c, AttributeSet attrs) {
2653 super(c, attrs);
2654 cellHSpan = 1;
2655 cellVSpan = 1;
2656 }
2657
2658 public LayoutParams(ViewGroup.LayoutParams source) {
2659 super(source);
2660 cellHSpan = 1;
2661 cellVSpan = 1;
2662 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002663
2664 public LayoutParams(LayoutParams source) {
2665 super(source);
2666 this.cellX = source.cellX;
2667 this.cellY = source.cellY;
2668 this.cellHSpan = source.cellHSpan;
2669 this.cellVSpan = source.cellVSpan;
2670 }
2671
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002672 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002673 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002674 this.cellX = cellX;
2675 this.cellY = cellY;
2676 this.cellHSpan = cellHSpan;
2677 this.cellVSpan = cellVSpan;
2678 }
2679
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002680 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002681 if (isLockedToGrid) {
2682 final int myCellHSpan = cellHSpan;
2683 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002684 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2685 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002686
Adam Cohend4844c32011-02-18 19:25:06 -08002687 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2688 leftMargin - rightMargin;
2689 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2690 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002691 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2692 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002693 }
2694 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002695
Winson Chungaafa03c2010-06-11 17:34:16 -07002696 public String toString() {
2697 return "(" + this.cellX + ", " + this.cellY + ")";
2698 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002699
2700 public void setWidth(int width) {
2701 this.width = width;
2702 }
2703
2704 public int getWidth() {
2705 return width;
2706 }
2707
2708 public void setHeight(int height) {
2709 this.height = height;
2710 }
2711
2712 public int getHeight() {
2713 return height;
2714 }
2715
2716 public void setX(int x) {
2717 this.x = x;
2718 }
2719
2720 public int getX() {
2721 return x;
2722 }
2723
2724 public void setY(int y) {
2725 this.y = y;
2726 }
2727
2728 public int getY() {
2729 return y;
2730 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002731 }
2732
Michael Jurka0280c3b2010-09-17 15:00:07 -07002733 // This class stores info for two purposes:
2734 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2735 // its spanX, spanY, and the screen it is on
2736 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2737 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2738 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002739 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002740 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002741 int cellX = -1;
2742 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002743 int spanX;
2744 int spanY;
2745 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002746 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002747
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002748 @Override
2749 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002750 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2751 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002752 }
2753 }
Michael Jurkad771c962011-08-09 15:00:48 -07002754
2755 public boolean lastDownOnOccupiedCell() {
2756 return mLastDownOnOccupiedCell;
2757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002758}