blob: 3b77f352e4c1c9f8f3f0755e44ca52ba6b35c3ce [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();
Adam Cohen6b8a02d2012-03-22 15:13:40 -07001104 lp.x = (int) (r * newX + (1 - r) * oldX);
1105 lp.y = (int) (r * newY + (1 - r) * oldY);
1106 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001107 }
1108 });
Adam Cohen482ed822012-03-02 14:15:13 -08001109 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001110 boolean cancelled = false;
1111 public void onAnimationEnd(Animator animation) {
1112 // If the animation was cancelled, it means that another animation
1113 // has interrupted this one, and we don't want to lock the item into
1114 // place just yet.
1115 if (!cancelled) {
1116 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001117 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001118 }
1119 if (mReorderAnimators.containsKey(lp)) {
1120 mReorderAnimators.remove(lp);
1121 }
1122 }
1123 public void onAnimationCancel(Animator animation) {
1124 cancelled = true;
1125 }
1126 });
Adam Cohen482ed822012-03-02 14:15:13 -08001127 va.setStartDelay(delay);
1128 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001129 return true;
1130 }
1131 return false;
1132 }
1133
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001134 /**
1135 * Estimate where the top left cell of the dragged item will land if it is dropped.
1136 *
1137 * @param originX The X value of the top left corner of the item
1138 * @param originY The Y value of the top left corner of the item
1139 * @param spanX The number of horizontal cells that the item spans
1140 * @param spanY The number of vertical cells that the item spans
1141 * @param result The estimated drop cell X and Y.
1142 */
1143 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001144 final int countX = mCountX;
1145 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001146
Michael Jurkaa63c4522010-08-19 13:52:27 -07001147 // pointToCellRounded takes the top left of a cell but will pad that with
1148 // cellWidth/2 and cellHeight/2 when finding the matching cell
1149 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001150
1151 // If the item isn't fully on this screen, snap to the edges
1152 int rightOverhang = result[0] + spanX - countX;
1153 if (rightOverhang > 0) {
1154 result[0] -= rightOverhang; // Snap to right
1155 }
1156 result[0] = Math.max(0, result[0]); // Snap to left
1157 int bottomOverhang = result[1] + spanY - countY;
1158 if (bottomOverhang > 0) {
1159 result[1] -= bottomOverhang; // Snap to bottom
1160 }
1161 result[1] = Math.max(0, result[1]); // Snap to top
1162 }
1163
Adam Cohen482ed822012-03-02 14:15:13 -08001164 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1165 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001166 final int oldDragCellX = mDragCell[0];
1167 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001168
Winson Chungb8c69f32011-10-19 21:36:08 -07001169 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001170 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1171 } else {
1172 mDragCenter.set(originX, originY);
1173 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001174
Adam Cohen2801caf2011-05-13 20:57:39 -07001175 if (dragOutline == null && v == null) {
1176 if (mCrosshairsDrawable != null) {
1177 invalidate();
1178 }
1179 return;
1180 }
1181
Adam Cohen482ed822012-03-02 14:15:13 -08001182 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1183 mDragCell[0] = cellX;
1184 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001185 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001186 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001187 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001188
Joe Onorato4be866d2010-10-10 11:26:02 -07001189 int left = topLeft[0];
1190 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001191
Winson Chungb8c69f32011-10-19 21:36:08 -07001192 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001193 // When drawing the drag outline, it did not account for margin offsets
1194 // added by the view's parent.
1195 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1196 left += lp.leftMargin;
1197 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001198
Adam Cohen99e8b402011-03-25 19:23:43 -07001199 // Offsets due to the size difference between the View and the dragOutline.
1200 // There is a size difference to account for the outer blur, which may lie
1201 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001202 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001203 // We center about the x axis
1204 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1205 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001206 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001207 if (dragOffset != null && dragRegion != null) {
1208 // Center the drag region *horizontally* in the cell and apply a drag
1209 // outline offset
1210 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1211 - dragRegion.width()) / 2;
1212 top += dragOffset.y;
1213 } else {
1214 // Center the drag outline in the cell
1215 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1216 - dragOutline.getWidth()) / 2;
1217 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1218 - dragOutline.getHeight()) / 2;
1219 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001220 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001221 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001222 mDragOutlineAnims[oldIndex].animateOut();
1223 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001224 Rect r = mDragOutlines[mDragOutlineCurrent];
1225 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1226 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001227 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001228 }
Winson Chung150fbab2010-09-29 17:14:26 -07001229
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001230 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1231 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001232 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001233
1234 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1235 if (mCrosshairsDrawable != null) {
1236 invalidate();
1237 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001238 }
1239
Adam Cohene0310962011-04-18 16:15:31 -07001240 public void clearDragOutlines() {
1241 final int oldIndex = mDragOutlineCurrent;
1242 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001243 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001244 }
1245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001247 * Find a vacant area that will fit the given bounds nearest the requested
1248 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001249 *
Romain Guy51afc022009-05-04 18:03:43 -07001250 * @param pixelX The X location at which you want to search for a vacant area.
1251 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001252 * @param spanX Horizontal span of the object.
1253 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001254 * @param result Array in which to place the result, or null (in which case a new array will
1255 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001256 * @return The X, Y cell of a vacant area that can contain this object,
1257 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001259 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1260 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001261 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001262 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001263
Michael Jurka6a1435d2010-09-27 17:35:12 -07001264 /**
1265 * Find a vacant area that will fit the given bounds nearest the requested
1266 * cell location. Uses Euclidean distance to score multiple vacant areas.
1267 *
1268 * @param pixelX The X location at which you want to search for a vacant area.
1269 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001270 * @param minSpanX The minimum horizontal span required
1271 * @param minSpanY The minimum vertical span required
1272 * @param spanX Horizontal span of the object.
1273 * @param spanY Vertical span of the object.
1274 * @param result Array in which to place the result, or null (in which case a new array will
1275 * be allocated)
1276 * @return The X, Y cell of a vacant area that can contain this object,
1277 * nearest the requested location.
1278 */
1279 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1280 int spanY, int[] result, int[] resultSpan) {
1281 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1282 result, resultSpan);
1283 }
1284
1285 /**
1286 * Find a vacant area that will fit the given bounds nearest the requested
1287 * cell location. Uses Euclidean distance to score multiple vacant areas.
1288 *
1289 * @param pixelX The X location at which you want to search for a vacant area.
1290 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001291 * @param spanX Horizontal span of the object.
1292 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001293 * @param ignoreOccupied If true, the result can be an occupied cell
1294 * @param result Array in which to place the result, or null (in which case a new array will
1295 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001296 * @return The X, Y cell of a vacant area that can contain this object,
1297 * nearest the requested location.
1298 */
Adam Cohendf035382011-04-11 17:22:04 -07001299 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1300 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001301 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001302 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001303 }
1304
1305 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1306 private void lazyInitTempRectStack() {
1307 if (mTempRectStack.isEmpty()) {
1308 for (int i = 0; i < mCountX * mCountY; i++) {
1309 mTempRectStack.push(new Rect());
1310 }
1311 }
1312 }
Adam Cohen482ed822012-03-02 14:15:13 -08001313
Adam Cohend41fbf52012-02-16 23:53:59 -08001314 private void recycleTempRects(Stack<Rect> used) {
1315 while (!used.isEmpty()) {
1316 mTempRectStack.push(used.pop());
1317 }
1318 }
1319
1320 /**
1321 * Find a vacant area that will fit the given bounds nearest the requested
1322 * cell location. Uses Euclidean distance to score multiple vacant areas.
1323 *
1324 * @param pixelX The X location at which you want to search for a vacant area.
1325 * @param pixelY The Y location at which you want to search for a vacant area.
1326 * @param minSpanX The minimum horizontal span required
1327 * @param minSpanY The minimum vertical span required
1328 * @param spanX Horizontal span of the object.
1329 * @param spanY Vertical span of the object.
1330 * @param ignoreOccupied If true, the result can be an occupied cell
1331 * @param result Array in which to place the result, or null (in which case a new array will
1332 * be allocated)
1333 * @return The X, Y cell of a vacant area that can contain this object,
1334 * nearest the requested location.
1335 */
1336 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001337 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1338 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001339 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001340 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001341 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001342
Adam Cohene3e27a82011-04-15 12:07:39 -07001343 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1344 // to the center of the item, but we are searching based on the top-left cell, so
1345 // we translate the point over to correspond to the top-left.
1346 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1347 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1348
Jeff Sharkey70864282009-04-07 21:08:40 -07001349 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001350 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001351 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001352 final Rect bestRect = new Rect(-1, -1, -1, -1);
1353 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001354
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001355 final int countX = mCountX;
1356 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001357
Adam Cohend41fbf52012-02-16 23:53:59 -08001358 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1359 spanX < minSpanX || spanY < minSpanY) {
1360 return bestXY;
1361 }
1362
1363 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001364 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001365 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1366 int ySize = -1;
1367 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001368 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001369 // First, let's see if this thing fits anywhere
1370 for (int i = 0; i < minSpanX; i++) {
1371 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001372 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001373 continue inner;
1374 }
Michael Jurkac28de512010-08-13 11:27:44 -07001375 }
1376 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001377 xSize = minSpanX;
1378 ySize = minSpanY;
1379
1380 // We know that the item will fit at _some_ acceptable size, now let's see
1381 // how big we can make it. We'll alternate between incrementing x and y spans
1382 // until we hit a limit.
1383 boolean incX = true;
1384 boolean hitMaxX = xSize >= spanX;
1385 boolean hitMaxY = ySize >= spanY;
1386 while (!(hitMaxX && hitMaxY)) {
1387 if (incX && !hitMaxX) {
1388 for (int j = 0; j < ySize; j++) {
1389 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1390 // We can't move out horizontally
1391 hitMaxX = true;
1392 }
1393 }
1394 if (!hitMaxX) {
1395 xSize++;
1396 }
1397 } else if (!hitMaxY) {
1398 for (int i = 0; i < xSize; i++) {
1399 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1400 // We can't move out vertically
1401 hitMaxY = true;
1402 }
1403 }
1404 if (!hitMaxY) {
1405 ySize++;
1406 }
1407 }
1408 hitMaxX |= xSize >= spanX;
1409 hitMaxY |= ySize >= spanY;
1410 incX = !incX;
1411 }
1412 incX = true;
1413 hitMaxX = xSize >= spanX;
1414 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001415 }
Winson Chung0be025d2011-05-23 17:45:09 -07001416 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001417 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418
Adam Cohend41fbf52012-02-16 23:53:59 -08001419 // We verify that the current rect is not a sub-rect of any of our previous
1420 // candidates. In this case, the current rect is disqualified in favour of the
1421 // containing rect.
1422 Rect currentRect = mTempRectStack.pop();
1423 currentRect.set(x, y, x + xSize, y + ySize);
1424 boolean contained = false;
1425 for (Rect r : validRegions) {
1426 if (r.contains(currentRect)) {
1427 contained = true;
1428 break;
1429 }
1430 }
1431 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001432 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1433 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001434
Adam Cohend41fbf52012-02-16 23:53:59 -08001435 if ((distance <= bestDistance && !contained) ||
1436 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001437 bestDistance = distance;
1438 bestXY[0] = x;
1439 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001440 if (resultSpan != null) {
1441 resultSpan[0] = xSize;
1442 resultSpan[1] = ySize;
1443 }
1444 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001445 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
1447 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001448 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001449 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450
Adam Cohenc0dcf592011-06-01 15:30:43 -07001451 // Return -1, -1 if no suitable location found
1452 if (bestDistance == Double.MAX_VALUE) {
1453 bestXY[0] = -1;
1454 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001455 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001456 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001457 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001459
Adam Cohen482ed822012-03-02 14:15:13 -08001460 /**
1461 * Find a vacant area that will fit the given bounds nearest the requested
1462 * cell location, and will also weigh in a suggested direction vector of the
1463 * desired location. This method computers distance based on unit grid distances,
1464 * not pixel distances.
1465 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001466 * @param cellX The X cell nearest to which you want to search for a vacant area.
1467 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001468 * @param spanX Horizontal span of the object.
1469 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001470 * @param direction The favored direction in which the views should move from x, y
1471 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1472 * matches exactly. Otherwise we find the best matching direction.
1473 * @param occoupied The array which represents which cells in the CellLayout are occupied
1474 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1475 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001476 * @param result Array in which to place the result, or null (in which case a new array will
1477 * be allocated)
1478 * @return The X, Y cell of a vacant area that can contain this object,
1479 * nearest the requested location.
1480 */
1481 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001482 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001483 // Keep track of best-scoring drop area
1484 final int[] bestXY = result != null ? result : new int[2];
1485 float bestDistance = Float.MAX_VALUE;
1486 int bestDirectionScore = Integer.MIN_VALUE;
1487
1488 final int countX = mCountX;
1489 final int countY = mCountY;
1490
1491 for (int y = 0; y < countY - (spanY - 1); y++) {
1492 inner:
1493 for (int x = 0; x < countX - (spanX - 1); x++) {
1494 // First, let's see if this thing fits anywhere
1495 for (int i = 0; i < spanX; i++) {
1496 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001497 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001498 continue inner;
1499 }
1500 }
1501 }
1502
1503 float distance = (float)
1504 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1505 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001506 computeDirectionVector(x - cellX, y - cellY, curDirection);
1507 // The direction score is just the dot product of the two candidate direction
1508 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001509 int curDirectionScore = direction[0] * curDirection[0] +
1510 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001511 boolean exactDirectionOnly = false;
1512 boolean directionMatches = direction[0] == curDirection[0] &&
1513 direction[0] == curDirection[0];
1514 if ((directionMatches || !exactDirectionOnly) &&
1515 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001516 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1517 bestDistance = distance;
1518 bestDirectionScore = curDirectionScore;
1519 bestXY[0] = x;
1520 bestXY[1] = y;
1521 }
1522 }
1523 }
1524
1525 // Return -1, -1 if no suitable location found
1526 if (bestDistance == Float.MAX_VALUE) {
1527 bestXY[0] = -1;
1528 bestXY[1] = -1;
1529 }
1530 return bestXY;
1531 }
1532
Adam Cohen47a876d2012-03-19 13:21:41 -07001533 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1534 int[] direction,boolean[][] occupied,
1535 boolean blockOccupied[][], int[] result) {
1536 // Keep track of best-scoring drop area
1537 final int[] bestXY = result != null ? result : new int[2];
1538 bestXY[0] = -1;
1539 bestXY[1] = -1;
1540 float bestDistance = Float.MAX_VALUE;
1541
1542 // We use this to march in a single direction
1543 if (direction[0] != 0 && direction[1] != 0) {
1544 return bestXY;
1545 }
1546
1547 // This will only incrememnet one of x or y based on the assertion above
1548 int x = cellX + direction[0];
1549 int y = cellY + direction[1];
1550 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1551
1552 boolean fail = false;
1553 for (int i = 0; i < spanX; i++) {
1554 for (int j = 0; j < spanY; j++) {
1555 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1556 fail = true;
1557 }
1558 }
1559 }
1560 if (!fail) {
1561 float distance = (float)
1562 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1563 if (Float.compare(distance, bestDistance) < 0) {
1564 bestDistance = distance;
1565 bestXY[0] = x;
1566 bestXY[1] = y;
1567 }
1568 }
1569 x += direction[0];
1570 y += direction[1];
1571 }
1572 return bestXY;
1573 }
1574
Adam Cohen482ed822012-03-02 14:15:13 -08001575 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001576 int[] direction, ItemConfiguration currentState) {
1577 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001578 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001579 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001580 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1581
Adam Cohen8baab352012-03-20 17:39:21 -07001582 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001583
1584 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001585 c.x = mTempLocation[0];
1586 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001587 success = true;
1588
1589 }
Adam Cohen8baab352012-03-20 17:39:21 -07001590 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001591 return success;
1592 }
1593
Adam Cohen47a876d2012-03-19 13:21:41 -07001594 // This method looks in the specified direction to see if there is an additional view
1595 // immediately adjecent in that direction
1596 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001597 boolean[][] occupied, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001598 boolean found = false;
1599
Michael Jurkaa52570f2012-03-20 03:18:20 -07001600 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001601 Rect r0 = new Rect(boundingRect);
1602 Rect r1 = new Rect();
1603
1604 int deltaX = 0;
1605 int deltaY = 0;
1606 if (direction[1] < 0) {
1607 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1608 deltaY = -1;
1609 } else if (direction[1] > 0) {
1610 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1611 deltaY = 1;
1612 } else if (direction[0] < 0) {
1613 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1614 deltaX = -1;
1615 } else if (direction[0] > 0) {
1616 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1617 deltaX = 1;
1618 }
1619
1620 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001621 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen47a876d2012-03-19 13:21:41 -07001622 if (views.contains(child)) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001623 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001624
Adam Cohen8baab352012-03-20 17:39:21 -07001625 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1626 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001627 if (Rect.intersects(r0, r1)) {
1628 if (!lp.canReorder) {
1629 return false;
1630 }
1631 boolean pushed = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001632 for (int x = c.x; x < c.x + c.spanX; x++) {
1633 for (int y = c.y; y < c.y + c.spanY; y++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001634 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1635 && y - deltaY >= 0 && y - deltaY < mCountY;
1636 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1637 pushed = true;
1638 }
1639 }
1640 }
1641 if (pushed) {
1642 views.add(child);
Adam Cohen8baab352012-03-20 17:39:21 -07001643 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001644 found = true;
1645 }
1646 }
1647 }
1648 return found;
1649 }
1650
Adam Cohen482ed822012-03-02 14:15:13 -08001651 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001652 int[] direction, boolean push, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001653 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001654
Adam Cohen8baab352012-03-20 17:39:21 -07001655 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001656 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001657 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001658 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001659 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001660 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001661 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001662 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001663 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001664 }
1665 }
Adam Cohen8baab352012-03-20 17:39:21 -07001666
1667 @SuppressWarnings("unchecked")
1668 ArrayList<View> dup = (ArrayList<View>) views.clone();
1669 // We try and expand the group of views in the direction vector passed, based on
1670 // whether they are physically adjacent, ie. based on "push mechanics".
1671 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied,
1672 currentState)) {
1673 }
1674
1675 // Mark the occupied state as false for the group of views we want to move.
1676 for (View v: dup) {
1677 CellAndSpan c = currentState.map.get(v);
1678 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1679 }
1680
Adam Cohen47a876d2012-03-19 13:21:41 -07001681 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1682 int top = boundingRect.top;
1683 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001684 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
1685 // for tetris-style interlocking.
1686 for (View v: dup) {
1687 CellAndSpan c = currentState.map.get(v);
1688 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001689 }
1690
Adam Cohen482ed822012-03-02 14:15:13 -08001691 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1692
Adam Cohen8baab352012-03-20 17:39:21 -07001693 if (push) {
1694 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1695 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1696 } else {
1697 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1698 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1699 }
Adam Cohen482ed822012-03-02 14:15:13 -08001700
Adam Cohen8baab352012-03-20 17:39:21 -07001701 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001702 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001703 int deltaX = mTempLocation[0] - boundingRect.left;
1704 int deltaY = mTempLocation[1] - boundingRect.top;
1705 for (View v: dup) {
1706 CellAndSpan c = currentState.map.get(v);
1707 c.x += deltaX;
1708 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001709 }
1710 success = true;
1711 }
Adam Cohen8baab352012-03-20 17:39:21 -07001712
1713 // In either case, we set the occupied array as marked for the location of the views
1714 for (View v: dup) {
1715 CellAndSpan c = currentState.map.get(v);
1716 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001717 }
1718 return success;
1719 }
1720
1721 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1722 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1723 }
1724
1725 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001726 View ignoreView, ItemConfiguration solution) {
Adam Cohen482ed822012-03-02 14:15:13 -08001727
Adam Cohen8baab352012-03-20 17:39:21 -07001728 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001729 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001730
Adam Cohen8baab352012-03-20 17:39:21 -07001731 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001732 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001733 CellAndSpan c = solution.map.get(ignoreView);
1734 c.x = cellX;
1735 c.y = cellY;
Adam Cohen482ed822012-03-02 14:15:13 -08001736 }
1737
Adam Cohen8baab352012-03-20 17:39:21 -07001738 //int childCount = mChildren.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001739 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1740 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001741 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001742 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001743 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001744 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001745 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001746 if (Rect.intersects(r0, r1)) {
1747 if (!lp.canReorder) {
1748 return false;
1749 }
1750 mIntersectingViews.add(child);
1751 }
1752 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001753
Adam Cohen8baab352012-03-20 17:39:21 -07001754 // We try to move the intersecting views as a block using the push mechanic
1755 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001756 return true;
1757 }
1758 // Try the opposite direction
1759 direction[0] *= -1;
1760 direction[1] *= -1;
Adam Cohen8baab352012-03-20 17:39:21 -07001761 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001762 return true;
1763 }
1764 // Switch the direction back
1765 direction[0] *= -1;
1766 direction[1] *= -1;
1767
Adam Cohen8baab352012-03-20 17:39:21 -07001768 // Next we try moving the views as a block , but without requiring the push mechanic
1769 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001770 return true;
1771 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001772
Adam Cohen482ed822012-03-02 14:15:13 -08001773 // Ok, they couldn't move as a block, let's move them individually
1774 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001775 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001776 return false;
1777 }
1778 }
1779 return true;
1780 }
1781
1782 /*
1783 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1784 * the provided point and the provided cell
1785 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001786 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001787 double angle = Math.atan(((float) deltaY) / deltaX);
1788
1789 result[0] = 0;
1790 result[1] = 0;
1791 if (Math.abs(Math.cos(angle)) > 0.5f) {
1792 result[0] = (int) Math.signum(deltaX);
1793 }
1794 if (Math.abs(Math.sin(angle)) > 0.5f) {
1795 result[1] = (int) Math.signum(deltaY);
1796 }
1797 }
1798
Adam Cohen8baab352012-03-20 17:39:21 -07001799 private void copyOccupiedArray(boolean[][] occupied) {
1800 for (int i = 0; i < mCountX; i++) {
1801 for (int j = 0; j < mCountY; j++) {
1802 occupied[i][j] = mOccupied[i][j];
1803 }
1804 }
1805 }
1806
Adam Cohen482ed822012-03-02 14:15:13 -08001807 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1808 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001809 // Copy the current state into the solution. This solution will be manipulated as necessary.
1810 copyCurrentStateToSolution(solution, false);
1811 // Copy the current occupied array into the temporary occupied array. This array will be
1812 // manipulated as necessary to find a solution.
1813 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001814
1815 // We find the nearest cell into which we would place the dragged item, assuming there's
1816 // nothing in its way.
1817 int result[] = new int[2];
1818 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1819
1820 boolean success = false;
1821 // First we try the exact nearest position of the item being dragged,
1822 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001823 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1824 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001825
1826 if (!success) {
1827 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1828 // x, then 1 in y etc.
1829 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1830 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1831 dragView, false, solution);
1832 } else if (spanY > minSpanY) {
1833 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1834 dragView, true, solution);
1835 }
1836 solution.isSolution = false;
1837 } else {
1838 solution.isSolution = true;
1839 solution.dragViewX = result[0];
1840 solution.dragViewY = result[1];
1841 solution.dragViewSpanX = spanX;
1842 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001843 }
1844 return solution;
1845 }
1846
1847 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001848 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001849 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001850 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001851 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001852 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001853 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001854 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001855 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001856 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001857 }
Adam Cohen8baab352012-03-20 17:39:21 -07001858 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001859 }
1860 }
1861
1862 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1863 for (int i = 0; i < mCountX; i++) {
1864 for (int j = 0; j < mCountY; j++) {
1865 mTmpOccupied[i][j] = false;
1866 }
1867 }
1868
Michael Jurkaa52570f2012-03-20 03:18:20 -07001869 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001870 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001871 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001872 if (child == dragView) continue;
1873 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001874 CellAndSpan c = solution.map.get(child);
1875 if (c != null) {
1876 lp.tmpCellX = c.x;
1877 lp.tmpCellY = c.y;
1878 lp.cellHSpan = c.spanX;
1879 lp.cellVSpan = c.spanY;
1880 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001881 }
1882 }
1883 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1884 solution.dragViewSpanY, mTmpOccupied, true);
1885 }
1886
1887 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1888 commitDragView) {
1889
1890 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1891 for (int i = 0; i < mCountX; i++) {
1892 for (int j = 0; j < mCountY; j++) {
1893 occupied[i][j] = false;
1894 }
1895 }
1896
Michael Jurkaa52570f2012-03-20 03:18:20 -07001897 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001898 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001899 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001900 if (child == dragView) continue;
1901 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001902 CellAndSpan c = solution.map.get(child);
1903 if (c != null) {
1904 if (lp.cellX != c.x || lp.cellY != c.y) {
1905 animateChildToPosition(child, c.x, c.y, 150, 0, DESTRUCTIVE_REORDER, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001906 }
Adam Cohen8baab352012-03-20 17:39:21 -07001907 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001908 }
1909 }
1910 if (commitDragView) {
1911 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1912 solution.dragViewSpanY, occupied, true);
1913 }
1914 }
1915
1916 private void commitTempPlacement() {
1917 for (int i = 0; i < mCountX; i++) {
1918 for (int j = 0; j < mCountY; j++) {
1919 mOccupied[i][j] = mTmpOccupied[i][j];
1920 }
1921 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001922 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001923 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001924 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08001925 lp.cellX = lp.tmpCellX;
1926 lp.cellY = lp.tmpCellY;
1927 }
1928 }
1929
1930 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001931 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001932 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001933 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08001934 lp.useTmpCoords = useTempCoords;
1935 }
1936 }
1937
Adam Cohen482ed822012-03-02 14:15:13 -08001938 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
1939 int spanX, int spanY, View dragView, ItemConfiguration solution) {
1940 int[] result = new int[2];
1941 int[] resultSpan = new int[2];
1942 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
1943 resultSpan);
1944 if (result[0] >= 0 && result[1] >= 0) {
1945 copyCurrentStateToSolution(solution, false);
1946 solution.dragViewX = result[0];
1947 solution.dragViewY = result[1];
1948 solution.dragViewSpanX = resultSpan[0];
1949 solution.dragViewSpanY = resultSpan[1];
1950 solution.isSolution = true;
1951 } else {
1952 solution.isSolution = false;
1953 }
1954 return solution;
1955 }
1956
1957 public void prepareChildForDrag(View child) {
1958 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001959 }
1960
1961 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
1962 View dragView, int[] result, int resultSpan[], int mode) {
1963
1964 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07001965 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08001966
1967 if (resultSpan == null) {
1968 resultSpan = new int[2];
1969 }
1970
1971 // We attempt the first algorithm
Adam Cohen47a876d2012-03-19 13:21:41 -07001972 regionToCenterPoint(result[0], result[1], spanX, spanY, mTmpPoint);
1973 computeDirectionVector((mTmpPoint[0] - pixelX) / spanX, (mTmpPoint[1] - pixelY) / spanY,
1974 mDirectionVector);
Adam Cohen482ed822012-03-02 14:15:13 -08001975 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
1976 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
1977
1978 // We attempt the approach which doesn't shuffle views at all
1979 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
1980 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
1981
1982 ItemConfiguration finalSolution = null;
1983 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
1984 finalSolution = swapSolution;
1985 } else if (noShuffleSolution.isSolution) {
1986 finalSolution = noShuffleSolution;
1987 }
1988
1989 boolean foundSolution = true;
1990 if (!DESTRUCTIVE_REORDER) {
1991 setUseTempCoords(true);
1992 }
1993
1994 if (finalSolution != null) {
1995 result[0] = finalSolution.dragViewX;
1996 result[1] = finalSolution.dragViewY;
1997 resultSpan[0] = finalSolution.dragViewSpanX;
1998 resultSpan[1] = finalSolution.dragViewSpanY;
1999
2000 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2001 // committing anything or animating anything as we just want to determine if a solution
2002 // exists
2003 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2004 if (!DESTRUCTIVE_REORDER) {
2005 copySolutionToTempState(finalSolution, dragView);
2006 }
2007 setItemPlacementDirty(true);
2008 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2009
2010 if (!DESTRUCTIVE_REORDER && mode == MODE_ON_DROP) {
2011 commitTempPlacement();
2012 }
2013 }
2014 } else {
2015 foundSolution = false;
2016 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2017 }
2018
2019 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2020 setUseTempCoords(false);
2021 }
Adam Cohen482ed822012-03-02 14:15:13 -08002022
Michael Jurkaa52570f2012-03-20 03:18:20 -07002023 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002024 return result;
2025 }
2026
2027 public boolean isItemPlacementDirty() {
2028 return mItemLocationsDirty;
2029 }
2030
2031 public void setItemPlacementDirty(boolean dirty) {
2032 mItemLocationsDirty = dirty;
2033 }
2034
2035 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002036 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002037 boolean isSolution = false;
2038 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2039
2040 int area() {
2041 return dragViewSpanX * dragViewSpanY;
2042 }
Adam Cohen8baab352012-03-20 17:39:21 -07002043 }
2044
2045 private class CellAndSpan {
2046 int x, y;
2047 int spanX, spanY;
2048
2049 public CellAndSpan(int x, int y, int spanX, int spanY) {
2050 this.x = x;
2051 this.y = y;
2052 this.spanX = spanX;
2053 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002054 }
2055 }
2056
Adam Cohendf035382011-04-11 17:22:04 -07002057 /**
2058 * Find a vacant area that will fit the given bounds nearest the requested
2059 * cell location. Uses Euclidean distance to score multiple vacant areas.
2060 *
2061 * @param pixelX The X location at which you want to search for a vacant area.
2062 * @param pixelY The Y location at which you want to search for a vacant area.
2063 * @param spanX Horizontal span of the object.
2064 * @param spanY Vertical span of the object.
2065 * @param ignoreView Considers space occupied by this view as unoccupied
2066 * @param result Previously returned value to possibly recycle.
2067 * @return The X, Y cell of a vacant area that can contain this object,
2068 * nearest the requested location.
2069 */
2070 int[] findNearestVacantArea(
2071 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2072 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2073 }
2074
2075 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002076 * Find a vacant area that will fit the given bounds nearest the requested
2077 * cell location. Uses Euclidean distance to score multiple vacant areas.
2078 *
2079 * @param pixelX The X location at which you want to search for a vacant area.
2080 * @param pixelY The Y location at which you want to search for a vacant area.
2081 * @param minSpanX The minimum horizontal span required
2082 * @param minSpanY The minimum vertical span required
2083 * @param spanX Horizontal span of the object.
2084 * @param spanY Vertical span of the object.
2085 * @param ignoreView Considers space occupied by this view as unoccupied
2086 * @param result Previously returned value to possibly recycle.
2087 * @return The X, Y cell of a vacant area that can contain this object,
2088 * nearest the requested location.
2089 */
2090 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2091 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002092 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2093 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002094 }
2095
2096 /**
Adam Cohendf035382011-04-11 17:22:04 -07002097 * Find a starting cell position that will fit the given bounds nearest the requested
2098 * cell location. Uses Euclidean distance to score multiple vacant areas.
2099 *
2100 * @param pixelX The X location at which you want to search for a vacant area.
2101 * @param pixelY The Y location at which you want to search for a vacant area.
2102 * @param spanX Horizontal span of the object.
2103 * @param spanY Vertical span of the object.
2104 * @param ignoreView Considers space occupied by this view as unoccupied
2105 * @param result Previously returned value to possibly recycle.
2106 * @return The X, Y cell of a vacant area that can contain this object,
2107 * nearest the requested location.
2108 */
2109 int[] findNearestArea(
2110 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2111 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2112 }
2113
Michael Jurka0280c3b2010-09-17 15:00:07 -07002114 boolean existsEmptyCell() {
2115 return findCellForSpan(null, 1, 1);
2116 }
2117
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002119 * Finds the upper-left coordinate of the first rectangle in the grid that can
2120 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2121 * then this method will only return coordinates for rectangles that contain the cell
2122 * (intersectX, intersectY)
2123 *
2124 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2125 * can be found.
2126 * @param spanX The horizontal span of the cell we want to find.
2127 * @param spanY The vertical span of the cell we want to find.
2128 *
2129 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002130 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002131 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002132 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002133 }
2134
2135 /**
2136 * Like above, but ignores any cells occupied by the item "ignoreView"
2137 *
2138 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2139 * can be found.
2140 * @param spanX The horizontal span of the cell we want to find.
2141 * @param spanY The vertical span of the cell we want to find.
2142 * @param ignoreView The home screen item we should treat as not occupying any space
2143 * @return
2144 */
2145 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002146 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2147 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002148 }
2149
2150 /**
2151 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2152 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2153 *
2154 * @param spanX The horizontal span of the cell we want to find.
2155 * @param spanY The vertical span of the cell we want to find.
2156 * @param ignoreView The home screen item we should treat as not occupying any space
2157 * @param intersectX The X coordinate of the cell that we should try to overlap
2158 * @param intersectX The Y coordinate of the cell that we should try to overlap
2159 *
2160 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2161 */
2162 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2163 int intersectX, int intersectY) {
2164 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002165 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002166 }
2167
2168 /**
2169 * The superset of the above two methods
2170 */
2171 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002172 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002173 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002174 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002175
Michael Jurka28750fb2010-09-24 17:43:49 -07002176 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002177 while (true) {
2178 int startX = 0;
2179 if (intersectX >= 0) {
2180 startX = Math.max(startX, intersectX - (spanX - 1));
2181 }
2182 int endX = mCountX - (spanX - 1);
2183 if (intersectX >= 0) {
2184 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2185 }
2186 int startY = 0;
2187 if (intersectY >= 0) {
2188 startY = Math.max(startY, intersectY - (spanY - 1));
2189 }
2190 int endY = mCountY - (spanY - 1);
2191 if (intersectY >= 0) {
2192 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2193 }
2194
Winson Chungbbc60d82010-11-11 16:34:41 -08002195 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002196 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002197 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002198 for (int i = 0; i < spanX; i++) {
2199 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002200 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002201 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002202 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002203 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002204 continue inner;
2205 }
2206 }
2207 }
2208 if (cellXY != null) {
2209 cellXY[0] = x;
2210 cellXY[1] = y;
2211 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002212 foundCell = true;
2213 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002214 }
2215 }
2216 if (intersectX == -1 && intersectY == -1) {
2217 break;
2218 } else {
2219 // if we failed to find anything, try again but without any requirements of
2220 // intersecting
2221 intersectX = -1;
2222 intersectY = -1;
2223 continue;
2224 }
2225 }
2226
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002227 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002228 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002229 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002230 }
2231
2232 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002233 * A drag event has begun over this layout.
2234 * It may have begun over this layout (in which case onDragChild is called first),
2235 * or it may have begun on another layout.
2236 */
2237 void onDragEnter() {
2238 if (!mDragging) {
2239 // Fade in the drag indicators
2240 if (mCrosshairsAnimator != null) {
2241 mCrosshairsAnimator.animateIn();
2242 }
2243 }
2244 mDragging = true;
2245 }
2246
2247 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002248 * Called when drag has left this CellLayout or has been completed (successfully or not)
2249 */
2250 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002251 // This can actually be called when we aren't in a drag, e.g. when adding a new
2252 // item to this layout via the customize drawer.
2253 // Guard against that case.
2254 if (mDragging) {
2255 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002256
Joe Onorato4be866d2010-10-10 11:26:02 -07002257 // Fade out the drag indicators
2258 if (mCrosshairsAnimator != null) {
2259 mCrosshairsAnimator.animateOut();
2260 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002261 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002262
2263 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002264 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002265 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2266 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
2267
Michael Jurka33945b22010-12-21 18:19:38 -08002268 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002269 }
2270
2271 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002272 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002273 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002274 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275 *
2276 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002278 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002279 if (child != null) {
2280 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002281 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002282 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002283 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 }
2285
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002288 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002289 * @param cellX X coordinate of upper left corner expressed as a cell position
2290 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002291 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002292 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002293 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002294 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002295 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002296 final int cellWidth = mCellWidth;
2297 final int cellHeight = mCellHeight;
2298 final int widthGap = mWidthGap;
2299 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002300
Winson Chung4b825dcd2011-06-19 12:41:22 -07002301 final int hStartPadding = getPaddingLeft();
2302 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002304 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2305 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2306
2307 int x = hStartPadding + cellX * (cellWidth + widthGap);
2308 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002309
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002310 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002312
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002314 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002316 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 * @param width Width in pixels
2318 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002319 * @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 -08002320 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002321 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002322 return rectToCell(getResources(), width, height, result);
2323 }
2324
2325 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 // Always assume we're working with the smallest span to make sure we
2327 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002328 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2329 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002333 int spanX = (int) Math.ceil(width / (float) smallerSize);
2334 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002335
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002336 if (result == null) {
2337 return new int[] { spanX, spanY };
2338 }
2339 result[0] = spanX;
2340 result[1] = spanY;
2341 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 }
2343
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002344 public int[] cellSpansToSize(int hSpans, int vSpans) {
2345 int[] size = new int[2];
2346 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2347 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2348 return size;
2349 }
2350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002352 * Calculate the grid spans needed to fit given item
2353 */
2354 public void calculateSpans(ItemInfo info) {
2355 final int minWidth;
2356 final int minHeight;
2357
2358 if (info instanceof LauncherAppWidgetInfo) {
2359 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2360 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2361 } else if (info instanceof PendingAddWidgetInfo) {
2362 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2363 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2364 } else {
2365 // It's not a widget, so it must be 1x1
2366 info.spanX = info.spanY = 1;
2367 return;
2368 }
2369 int[] spans = rectToCell(minWidth, minHeight, null);
2370 info.spanX = spans[0];
2371 info.spanY = spans[1];
2372 }
2373
2374 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002375 * Find the first vacant cell, if there is one.
2376 *
2377 * @param vacant Holds the x and y coordinate of the vacant cell
2378 * @param spanX Horizontal cell span.
2379 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002380 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 * @return True if a vacant cell was found
2382 */
2383 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384
Michael Jurka0280c3b2010-09-17 15:00:07 -07002385 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 }
2387
2388 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2389 int xCount, int yCount, boolean[][] occupied) {
2390
Adam Cohen2801caf2011-05-13 20:57:39 -07002391 for (int y = 0; y < yCount; y++) {
2392 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 boolean available = !occupied[x][y];
2394out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2395 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2396 available = available && !occupied[i][j];
2397 if (!available) break out;
2398 }
2399 }
2400
2401 if (available) {
2402 vacant[0] = x;
2403 vacant[1] = y;
2404 return true;
2405 }
2406 }
2407 }
2408
2409 return false;
2410 }
2411
Michael Jurka0280c3b2010-09-17 15:00:07 -07002412 private void clearOccupiedCells() {
2413 for (int x = 0; x < mCountX; x++) {
2414 for (int y = 0; y < mCountY; y++) {
2415 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 }
2417 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002418 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419
Adam Cohen1b607ed2011-03-03 17:26:50 -08002420 /**
2421 * Given a view, determines how much that view can be expanded in all directions, in terms of
2422 * whether or not there are other items occupying adjacent cells. Used by the
2423 * AppWidgetResizeFrame to determine how the widget can be resized.
2424 */
Adam Cohend4844c32011-02-18 19:25:06 -08002425 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08002426 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08002427 boolean flag;
2428
Adam Cohen1b607ed2011-03-03 17:26:50 -08002429 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002430 for (int x = lp.cellX - 1; x >= 0; x--) {
2431 flag = false;
2432 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
2433 if (mOccupied[x][y]) flag = true;
2434 }
2435 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002436 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08002437 }
2438
Adam Cohen1b607ed2011-03-03 17:26:50 -08002439 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002440 for (int y = lp.cellY - 1; y >= 0; y--) {
2441 flag = false;
2442 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
2443 if (mOccupied[x][y]) flag = true;
2444 }
2445 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002446 expandability[AppWidgetResizeFrame.TOP]++;
2447 }
Adam Cohend4844c32011-02-18 19:25:06 -08002448
Adam Cohen1b607ed2011-03-03 17:26:50 -08002449 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002450 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
2451 flag = false;
2452 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
2453 if (mOccupied[x][y]) flag = true;
2454 }
2455 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002456 expandability[AppWidgetResizeFrame.RIGHT]++;
2457 }
Adam Cohend4844c32011-02-18 19:25:06 -08002458
Adam Cohen1b607ed2011-03-03 17:26:50 -08002459 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002460 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
2461 flag = false;
2462 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
2463 if (mOccupied[x][y]) flag = true;
2464 }
2465 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002466 expandability[AppWidgetResizeFrame.BOTTOM]++;
2467 }
Adam Cohend4844c32011-02-18 19:25:06 -08002468 }
2469
Adam Cohend41fbf52012-02-16 23:53:59 -08002470 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002471 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002472 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474
Adam Cohend4844c32011-02-18 19:25:06 -08002475 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002476 markCellsAsOccupiedForView(view, mOccupied);
2477 }
2478 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002479 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002480 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002481 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002482 }
2483
Adam Cohend4844c32011-02-18 19:25:06 -08002484 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002485 markCellsAsUnoccupiedForView(view, mOccupied);
2486 }
2487 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002488 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002489 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002490 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002491 }
2492
Adam Cohen482ed822012-03-02 14:15:13 -08002493 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2494 boolean value) {
2495 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002496 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2497 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002498 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 }
2500 }
2501 }
2502
Adam Cohen2801caf2011-05-13 20:57:39 -07002503 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002504 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002505 (Math.max((mCountX - 1), 0) * mWidthGap);
2506 }
2507
2508 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002509 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002510 (Math.max((mCountY - 1), 0) * mHeightGap);
2511 }
2512
Michael Jurka66d72172011-04-12 16:29:25 -07002513 public boolean isOccupied(int x, int y) {
2514 if (x < mCountX && y < mCountY) {
2515 return mOccupied[x][y];
2516 } else {
2517 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2518 }
2519 }
2520
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 @Override
2522 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2523 return new CellLayout.LayoutParams(getContext(), attrs);
2524 }
2525
2526 @Override
2527 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2528 return p instanceof CellLayout.LayoutParams;
2529 }
2530
2531 @Override
2532 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2533 return new CellLayout.LayoutParams(p);
2534 }
2535
Winson Chungaafa03c2010-06-11 17:34:16 -07002536 public static class CellLayoutAnimationController extends LayoutAnimationController {
2537 public CellLayoutAnimationController(Animation animation, float delay) {
2538 super(animation, delay);
2539 }
2540
2541 @Override
2542 protected long getDelayForView(View view) {
2543 return (int) (Math.random() * 150);
2544 }
2545 }
2546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2548 /**
2549 * Horizontal location of the item in the grid.
2550 */
2551 @ViewDebug.ExportedProperty
2552 public int cellX;
2553
2554 /**
2555 * Vertical location of the item in the grid.
2556 */
2557 @ViewDebug.ExportedProperty
2558 public int cellY;
2559
2560 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002561 * Temporary horizontal location of the item in the grid during reorder
2562 */
2563 public int tmpCellX;
2564
2565 /**
2566 * Temporary vertical location of the item in the grid during reorder
2567 */
2568 public int tmpCellY;
2569
2570 /**
2571 * Indicates that the temporary coordinates should be used to layout the items
2572 */
2573 public boolean useTmpCoords;
2574
2575 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 * Number of cells spanned horizontally by the item.
2577 */
2578 @ViewDebug.ExportedProperty
2579 public int cellHSpan;
2580
2581 /**
2582 * Number of cells spanned vertically by the item.
2583 */
2584 @ViewDebug.ExportedProperty
2585 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002586
Adam Cohen1b607ed2011-03-03 17:26:50 -08002587 /**
2588 * Indicates whether the item will set its x, y, width and height parameters freely,
2589 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2590 */
Adam Cohend4844c32011-02-18 19:25:06 -08002591 public boolean isLockedToGrid = true;
2592
Adam Cohen482ed822012-03-02 14:15:13 -08002593 /**
2594 * Indicates whether this item can be reordered. Always true except in the case of the
2595 * the AllApps button.
2596 */
2597 public boolean canReorder = true;
2598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002599 // X coordinate of the view in the layout.
2600 @ViewDebug.ExportedProperty
2601 int x;
2602 // Y coordinate of the view in the layout.
2603 @ViewDebug.ExportedProperty
2604 int y;
2605
Romain Guy84f296c2009-11-04 15:00:44 -08002606 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002607
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002608 public LayoutParams(Context c, AttributeSet attrs) {
2609 super(c, attrs);
2610 cellHSpan = 1;
2611 cellVSpan = 1;
2612 }
2613
2614 public LayoutParams(ViewGroup.LayoutParams source) {
2615 super(source);
2616 cellHSpan = 1;
2617 cellVSpan = 1;
2618 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002619
2620 public LayoutParams(LayoutParams source) {
2621 super(source);
2622 this.cellX = source.cellX;
2623 this.cellY = source.cellY;
2624 this.cellHSpan = source.cellHSpan;
2625 this.cellVSpan = source.cellVSpan;
2626 }
2627
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002628 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002629 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002630 this.cellX = cellX;
2631 this.cellY = cellY;
2632 this.cellHSpan = cellHSpan;
2633 this.cellVSpan = cellVSpan;
2634 }
2635
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002636 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002637 if (isLockedToGrid) {
2638 final int myCellHSpan = cellHSpan;
2639 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002640 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2641 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002642
Adam Cohend4844c32011-02-18 19:25:06 -08002643 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2644 leftMargin - rightMargin;
2645 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2646 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002647 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2648 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002649 }
2650 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002651
Winson Chungaafa03c2010-06-11 17:34:16 -07002652 public String toString() {
2653 return "(" + this.cellX + ", " + this.cellY + ")";
2654 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002655
2656 public void setWidth(int width) {
2657 this.width = width;
2658 }
2659
2660 public int getWidth() {
2661 return width;
2662 }
2663
2664 public void setHeight(int height) {
2665 this.height = height;
2666 }
2667
2668 public int getHeight() {
2669 return height;
2670 }
2671
2672 public void setX(int x) {
2673 this.x = x;
2674 }
2675
2676 public int getX() {
2677 return x;
2678 }
2679
2680 public void setY(int y) {
2681 this.y = y;
2682 }
2683
2684 public int getY() {
2685 return y;
2686 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002687 }
2688
Michael Jurka0280c3b2010-09-17 15:00:07 -07002689 // This class stores info for two purposes:
2690 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2691 // its spanX, spanY, and the screen it is on
2692 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2693 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2694 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002695 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002696 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002697 int cellX = -1;
2698 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002699 int spanX;
2700 int spanY;
2701 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002702 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002703
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002704 @Override
2705 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002706 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2707 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002708 }
2709 }
Michael Jurkad771c962011-08-09 15:00:48 -07002710
2711 public boolean lastDownOnOccupiedCell() {
2712 return mLastDownOnOccupiedCell;
2713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002714}