blob: 8dc43d17f73855ea4e34820b0d15c6edb40612a6 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Joe Onorato4be866d2010-10-10 11:26:02 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Michael Jurka18014792010-10-14 09:01:34 -070021import android.animation.ObjectAnimator;
Adam Cohenbfbfd262011-06-13 16:55:12 -070022import android.animation.PropertyValuesHolder;
Chet Haase00397b12010-10-07 11:13:10 -070023import android.animation.TimeInterpolator;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070024import android.animation.ValueAnimator;
25import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.content.Context;
Joe Onorato79e56262009-09-21 15:23:04 -040027import android.content.res.Resources;
Winson Chungaafa03c2010-06-11 17:34:16 -070028import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070029import android.graphics.Bitmap;
Winson Chungaafa03c2010-06-11 17:34:16 -070030import android.graphics.Canvas;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -080031import android.graphics.Color;
Joe Onorato4be866d2010-10-10 11:26:02 -070032import android.graphics.Paint;
Patrick Dubroyde7658b2010-09-27 11:15:43 -070033import android.graphics.Point;
34import android.graphics.PointF;
Adam Cohenb5ba0972011-09-07 18:02:31 -070035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.Rect;
Adam Cohen482ed822012-03-02 14:15:13 -080038import android.graphics.drawable.ColorDrawable;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070039import android.graphics.drawable.Drawable;
Adam Cohenb5ba0972011-09-07 18:02:31 -070040import android.graphics.drawable.NinePatchDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.util.AttributeSet;
Joe Onorato4be866d2010-10-10 11:26:02 -070042import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.view.MotionEvent;
44import android.view.View;
45import android.view.ViewDebug;
46import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.view.animation.Animation;
Winson Chung150fbab2010-09-29 17:14:26 -070048import android.view.animation.DecelerateInterpolator;
Winson Chungaafa03c2010-06-11 17:34:16 -070049import android.view.animation.LayoutAnimationController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Adam Cohen66396872011-04-15 17:50:36 -070051import com.android.launcher.R;
Adam Cohen69ce2e52011-07-03 19:25:21 -070052import com.android.launcher2.FolderIcon.FolderRingAnimator;
Patrick Dubroy8e58e912010-10-14 13:21:48 -070053
Adam Cohen69ce2e52011-07-03 19:25:21 -070054import java.util.ArrayList;
Adam Cohenc0dcf592011-06-01 15:30:43 -070055import java.util.Arrays;
Adam Cohenbfbfd262011-06-13 16:55:12 -070056import java.util.HashMap;
Adam Cohend41fbf52012-02-16 23:53:59 -080057import java.util.Stack;
Adam Cohenc0dcf592011-06-01 15:30:43 -070058
Michael Jurkabdb5c532011-02-01 15:05:06 -080059public class CellLayout extends ViewGroup {
Winson Chungaafa03c2010-06-11 17:34:16 -070060 static final String TAG = "CellLayout";
61
Winson Chung4b825dcd2011-06-19 12:41:22 -070062 private int mOriginalCellWidth;
63 private int mOriginalCellHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 private int mCellWidth;
65 private int mCellHeight;
Winson Chungaafa03c2010-06-11 17:34:16 -070066
Adam Cohend22015c2010-07-26 22:02:18 -070067 private int mCountX;
68 private int mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069
Adam Cohen234c4cd2011-07-17 21:03:04 -070070 private int mOriginalWidthGap;
71 private int mOriginalHeightGap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 private int mWidthGap;
73 private int mHeightGap;
Winson Chung4b825dcd2011-06-19 12:41:22 -070074 private int mMaxGap;
Adam Cohenebea84d2011-11-09 17:20:41 -080075 private boolean mScrollingTransformsDirty = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076
77 private final Rect mRect = new Rect();
78 private final CellInfo mCellInfo = new CellInfo();
Winson Chungaafa03c2010-06-11 17:34:16 -070079
Patrick Dubroyde7658b2010-09-27 11:15:43 -070080 // These are temporary variables to prevent having to allocate a new object just to
81 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
Winson Chung0be025d2011-05-23 17:45:09 -070082 private final int[] mTmpXY = new int[2];
Patrick Dubroyde7658b2010-09-27 11:15:43 -070083 private final int[] mTmpPoint = new int[2];
84 private final PointF mTmpPointF = new PointF();
Adam Cohen69ce2e52011-07-03 19:25:21 -070085 int[] mTempLocation = new int[2];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -070086
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087 boolean[][] mOccupied;
Adam Cohen482ed822012-03-02 14:15:13 -080088 boolean[][] mTmpOccupied;
Michael Jurkad771c962011-08-09 15:00:48 -070089 private boolean mLastDownOnOccupiedCell = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Michael Jurkadee05892010-07-27 10:01:56 -070091 private OnTouchListener mInterceptTouchListener;
92
Adam Cohen69ce2e52011-07-03 19:25:21 -070093 private ArrayList<FolderRingAnimator> mFolderOuterRings = new ArrayList<FolderRingAnimator>();
Adam Cohenc51934b2011-07-26 21:07:43 -070094 private int[] mFolderLeaveBehindCell = {-1, -1};
Adam Cohen69ce2e52011-07-03 19:25:21 -070095
Adam Cohenb5ba0972011-09-07 18:02:31 -070096 private int mForegroundAlpha = 0;
Michael Jurka5f1c5092010-09-03 14:15:02 -070097 private float mBackgroundAlpha;
Adam Cohen1b0aaac2010-10-28 11:11:18 -070098 private float mBackgroundAlphaMultiplier = 1.0f;
Adam Cohenf34bab52010-09-30 14:11:56 -070099
Michael Jurka33945b22010-12-21 18:19:38 -0800100 private Drawable mNormalBackground;
Michael Jurka33945b22010-12-21 18:19:38 -0800101 private Drawable mActiveGlowBackground;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700102 private Drawable mOverScrollForegroundDrawable;
103 private Drawable mOverScrollLeft;
104 private Drawable mOverScrollRight;
Michael Jurka18014792010-10-14 09:01:34 -0700105 private Rect mBackgroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700106 private Rect mForegroundRect;
Adam Cohenb5ba0972011-09-07 18:02:31 -0700107 private int mForegroundPadding;
Patrick Dubroy1262e362010-10-06 15:49:50 -0700108
Michael Jurka33945b22010-12-21 18:19:38 -0800109 // If we're actively dragging something over this screen, mIsDragOverlapping is true
110 private boolean mIsDragOverlapping = false;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700111 private final Point mDragCenter = new Point();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700112
Winson Chung150fbab2010-09-29 17:14:26 -0700113 // These arrays are used to implement the drag visualization on x-large screens.
Joe Onorato4be866d2010-10-10 11:26:02 -0700114 // They are used as circular arrays, indexed by mDragOutlineCurrent.
Adam Cohend41fbf52012-02-16 23:53:59 -0800115 private Rect[] mDragOutlines = new Rect[4];
Chet Haase472b2812010-10-14 07:02:04 -0700116 private float[] mDragOutlineAlphas = new float[mDragOutlines.length];
Joe Onorato4be866d2010-10-10 11:26:02 -0700117 private InterruptibleInOutAnimator[] mDragOutlineAnims =
118 new InterruptibleInOutAnimator[mDragOutlines.length];
Winson Chung150fbab2010-09-29 17:14:26 -0700119
120 // Used as an index into the above 3 arrays; indicates which is the most current value.
Joe Onorato4be866d2010-10-10 11:26:02 -0700121 private int mDragOutlineCurrent = 0;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700122 private final Paint mDragOutlinePaint = new Paint();
Winson Chung150fbab2010-09-29 17:14:26 -0700123
Patrick Dubroy96864c32011-03-10 17:17:23 -0800124 private BubbleTextView mPressedOrFocusedIcon;
125
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700126 private Drawable mCrosshairsDrawable = null;
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700127 private InterruptibleInOutAnimator mCrosshairsAnimator = null;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700128 private float mCrosshairsVisibility = 0.0f;
129
Adam Cohen482ed822012-03-02 14:15:13 -0800130 private HashMap<CellLayout.LayoutParams, Animator> mReorderAnimators = new
131 HashMap<CellLayout.LayoutParams, Animator>();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700132
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700133 // When a drag operation is in progress, holds the nearest cell to the touch point
134 private final int[] mDragCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Joe Onorato4be866d2010-10-10 11:26:02 -0700136 private boolean mDragging = false;
Adam Cohen482ed822012-03-02 14:15:13 -0800137 private boolean mItemLocationsDirty = false;
Joe Onorato4be866d2010-10-10 11:26:02 -0700138
Patrick Dubroyce34a972010-10-19 10:34:32 -0700139 private TimeInterpolator mEaseOutInterpolator;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700140 private ShortcutAndWidgetContainer mShortcutsAndWidgets;
Patrick Dubroyce34a972010-10-19 10:34:32 -0700141
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800142 private boolean mIsHotseat = false;
Winson Chungeecf02d2012-03-02 17:14:58 -0800143 private float mChildScale = 1f;
144 private float mHotseatChildScale = 1f;
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800145
Adam Cohen482ed822012-03-02 14:15:13 -0800146 public static final int MODE_DRAG_OVER = 0;
147 public static final int MODE_ON_DROP = 1;
148 public static final int MODE_ON_DROP_EXTERNAL = 2;
149 public static final int MODE_ACCEPT_DROP = 3;
150 private static final boolean DESTRUCTIVE_REORDER = true;
151 private static final boolean DEBUG_VISUALIZE_OCCUPIED = false;
152
153 private ArrayList<View> mIntersectingViews = new ArrayList<View>();
154 private Rect mOccupiedRect = new Rect();
155 private int[] mDirectionVector = new int[2];
156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 public CellLayout(Context context) {
158 this(context, null);
159 }
160
161 public CellLayout(Context context, AttributeSet attrs) {
162 this(context, attrs, 0);
163 }
164
165 public CellLayout(Context context, AttributeSet attrs, int defStyle) {
166 super(context, attrs, defStyle);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700167
168 // A ViewGroup usually does not draw, but CellLayout needs to draw a rectangle to show
169 // the user where a dragged item will land when dropped.
170 setWillNotDraw(false);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);
173
Winson Chung4b825dcd2011-06-19 12:41:22 -0700174 mOriginalCellWidth =
175 mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10);
176 mOriginalCellHeight =
177 mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700178 mWidthGap = mOriginalWidthGap = a.getDimensionPixelSize(R.styleable.CellLayout_widthGap, 0);
179 mHeightGap = mOriginalHeightGap = a.getDimensionPixelSize(R.styleable.CellLayout_heightGap, 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700180 mMaxGap = a.getDimensionPixelSize(R.styleable.CellLayout_maxGap, 0);
Adam Cohend22015c2010-07-26 22:02:18 -0700181 mCountX = LauncherModel.getCellCountX();
182 mCountY = LauncherModel.getCellCountY();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700183 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800184 mTmpOccupied = new boolean[mCountX][mCountY];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
186 a.recycle();
187
188 setAlwaysDrawnWithCacheEnabled(false);
189
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700190 final Resources res = getResources();
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700191
Winson Chung967289b2011-06-30 18:09:30 -0700192 mNormalBackground = res.getDrawable(R.drawable.homescreen_blue_normal_holo);
Winson Chungdea74b72011-09-13 18:06:43 -0700193 mActiveGlowBackground = res.getDrawable(R.drawable.homescreen_blue_strong_holo);
Michael Jurka33945b22010-12-21 18:19:38 -0800194
Adam Cohenb5ba0972011-09-07 18:02:31 -0700195 mOverScrollLeft = res.getDrawable(R.drawable.overscroll_glow_left);
196 mOverScrollRight = res.getDrawable(R.drawable.overscroll_glow_right);
197 mForegroundPadding =
198 res.getDimensionPixelSize(R.dimen.workspace_overscroll_drawable_padding);
Michael Jurka33945b22010-12-21 18:19:38 -0800199
Winson Chungb26f3d62011-06-02 10:49:29 -0700200 mNormalBackground.setFilterBitmap(true);
Winson Chungb26f3d62011-06-02 10:49:29 -0700201 mActiveGlowBackground.setFilterBitmap(true);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700202
Winson Chungeecf02d2012-03-02 17:14:58 -0800203 int iconScale = res.getInteger(R.integer.app_icon_scale_percent);
204 if (iconScale >= 0) {
205 mChildScale = iconScale / 100f;
206 }
207 int hotseatIconScale = res.getInteger(R.integer.app_icon_hotseat_scale_percent);
208 if (hotseatIconScale >= 0) {
209 mHotseatChildScale = hotseatIconScale / 100f;
210 }
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800211
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700212 // Initialize the data structures used for the drag visualization.
Winson Chung150fbab2010-09-29 17:14:26 -0700213
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700214 mCrosshairsDrawable = res.getDrawable(R.drawable.gardening_crosshairs);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700215 mEaseOutInterpolator = new DecelerateInterpolator(2.5f); // Quint ease out
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700216
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700217 // Set up the animation for fading the crosshairs in and out
218 int animDuration = res.getInteger(R.integer.config_crosshairsFadeInTime);
Patrick Dubroy49250ad2010-10-08 15:33:52 -0700219 mCrosshairsAnimator = new InterruptibleInOutAnimator(animDuration, 0.0f, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700220 mCrosshairsAnimator.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700221 public void onAnimationUpdate(ValueAnimator animation) {
222 mCrosshairsVisibility = ((Float) animation.getAnimatedValue()).floatValue();
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700223 invalidate();
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700224 }
225 });
Patrick Dubroyce34a972010-10-19 10:34:32 -0700226 mCrosshairsAnimator.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700227
Winson Chungb8c69f32011-10-19 21:36:08 -0700228 mDragCell[0] = mDragCell[1] = -1;
Joe Onorato4be866d2010-10-10 11:26:02 -0700229 for (int i = 0; i < mDragOutlines.length; i++) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800230 mDragOutlines[i] = new Rect(-1, -1, -1, -1);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700231 }
232
233 // When dragging things around the home screens, we show a green outline of
234 // where the item will land. The outlines gradually fade out, leaving a trail
235 // behind the drag path.
236 // Set up all the animations that are used to implement this fading.
237 final int duration = res.getInteger(R.integer.config_dragOutlineFadeTime);
Chet Haase472b2812010-10-14 07:02:04 -0700238 final float fromAlphaValue = 0;
239 final float toAlphaValue = (float)res.getInteger(R.integer.config_dragOutlineMaxAlpha);
Joe Onorato4be866d2010-10-10 11:26:02 -0700240
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700241 Arrays.fill(mDragOutlineAlphas, fromAlphaValue);
Joe Onorato4be866d2010-10-10 11:26:02 -0700242
243 for (int i = 0; i < mDragOutlineAnims.length; i++) {
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700244 final InterruptibleInOutAnimator anim =
245 new InterruptibleInOutAnimator(duration, fromAlphaValue, toAlphaValue);
Patrick Dubroyce34a972010-10-19 10:34:32 -0700246 anim.getAnimator().setInterpolator(mEaseOutInterpolator);
Patrick Dubroy046e7eb2010-10-06 12:14:43 -0700247 final int thisIndex = i;
Chet Haase472b2812010-10-14 07:02:04 -0700248 anim.getAnimator().addUpdateListener(new AnimatorUpdateListener() {
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700249 public void onAnimationUpdate(ValueAnimator animation) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700250 final Bitmap outline = (Bitmap)anim.getTag();
251
252 // If an animation is started and then stopped very quickly, we can still
253 // get spurious updates we've cleared the tag. Guard against this.
254 if (outline == null) {
Patrick Dubroyfe6bd872010-10-13 17:32:10 -0700255 if (false) {
256 Object val = animation.getAnimatedValue();
257 Log.d(TAG, "anim " + thisIndex + " update: " + val +
258 ", isStopped " + anim.isStopped());
259 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700260 // Try to prevent it from continuing to run
261 animation.cancel();
262 } else {
Chet Haase472b2812010-10-14 07:02:04 -0700263 mDragOutlineAlphas[thisIndex] = (Float) animation.getAnimatedValue();
Adam Cohend41fbf52012-02-16 23:53:59 -0800264 CellLayout.this.invalidate(mDragOutlines[thisIndex]);
Joe Onorato4be866d2010-10-10 11:26:02 -0700265 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700266 }
267 });
Joe Onorato4be866d2010-10-10 11:26:02 -0700268 // The animation holds a reference to the drag outline bitmap as long is it's
269 // running. This way the bitmap can be GCed when the animations are complete.
Chet Haase472b2812010-10-14 07:02:04 -0700270 anim.getAnimator().addListener(new AnimatorListenerAdapter() {
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700271 @Override
Joe Onorato4be866d2010-10-10 11:26:02 -0700272 public void onAnimationEnd(Animator animation) {
Chet Haase472b2812010-10-14 07:02:04 -0700273 if ((Float) ((ValueAnimator) animation).getAnimatedValue() == 0f) {
Joe Onorato4be866d2010-10-10 11:26:02 -0700274 anim.setTag(null);
275 }
276 }
277 });
278 mDragOutlineAnims[i] = anim;
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700279 }
Patrick Dubroyce34a972010-10-19 10:34:32 -0700280
Michael Jurka18014792010-10-14 09:01:34 -0700281 mBackgroundRect = new Rect();
Adam Cohenb5ba0972011-09-07 18:02:31 -0700282 mForegroundRect = new Rect();
Michael Jurkabea15192010-11-17 12:33:46 -0800283
Michael Jurkaa52570f2012-03-20 03:18:20 -0700284 mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context);
285 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
286 addView(mShortcutsAndWidgets);
Michael Jurka18014792010-10-14 09:01:34 -0700287 }
288
Michael Jurkaf6440da2011-04-05 14:50:34 -0700289 static int widthInPortrait(Resources r, int numCells) {
290 // We use this method from Workspace to figure out how many rows/columns Launcher should
291 // have. We ignore the left/right padding on CellLayout because it turns out in our design
292 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700293 int cellWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700294 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
295 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700296
Winson Chung4b825dcd2011-06-19 12:41:22 -0700297 return minGap * (numCells - 1) + cellWidth * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700298 }
299
Michael Jurkaf6440da2011-04-05 14:50:34 -0700300 static int heightInLandscape(Resources r, int numCells) {
301 // We use this method from Workspace to figure out how many rows/columns Launcher should
302 // have. We ignore the left/right padding on CellLayout because it turns out in our design
303 // the padding extends outside the visible screen size, but it looked fine anyway.
Michael Jurkaf6440da2011-04-05 14:50:34 -0700304 int cellHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700305 int minGap = Math.min(r.getDimensionPixelSize(R.dimen.workspace_width_gap),
306 r.getDimensionPixelSize(R.dimen.workspace_height_gap));
Michael Jurkaf6440da2011-04-05 14:50:34 -0700307
Winson Chung4b825dcd2011-06-19 12:41:22 -0700308 return minGap * (numCells - 1) + cellHeight * numCells;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700309 }
310
Adam Cohen2801caf2011-05-13 20:57:39 -0700311 public void enableHardwareLayers() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700312 mShortcutsAndWidgets.enableHardwareLayers();
Adam Cohen2801caf2011-05-13 20:57:39 -0700313 }
314
315 public void setGridSize(int x, int y) {
316 mCountX = x;
317 mCountY = y;
318 mOccupied = new boolean[mCountX][mCountY];
Adam Cohen482ed822012-03-02 14:15:13 -0800319 mTmpOccupied = new boolean[mCountX][mCountY];
Adam Cohen76fc0852011-06-17 13:26:23 -0700320 requestLayout();
Adam Cohen2801caf2011-05-13 20:57:39 -0700321 }
322
Patrick Dubroy96864c32011-03-10 17:17:23 -0800323 private void invalidateBubbleTextView(BubbleTextView icon) {
324 final int padding = icon.getPressedOrFocusedBackgroundPadding();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700325 invalidate(icon.getLeft() + getPaddingLeft() - padding,
326 icon.getTop() + getPaddingTop() - padding,
327 icon.getRight() + getPaddingLeft() + padding,
328 icon.getBottom() + getPaddingTop() + padding);
Patrick Dubroy96864c32011-03-10 17:17:23 -0800329 }
330
Adam Cohenb5ba0972011-09-07 18:02:31 -0700331 void setOverScrollAmount(float r, boolean left) {
332 if (left && mOverScrollForegroundDrawable != mOverScrollLeft) {
333 mOverScrollForegroundDrawable = mOverScrollLeft;
334 } else if (!left && mOverScrollForegroundDrawable != mOverScrollRight) {
335 mOverScrollForegroundDrawable = mOverScrollRight;
336 }
337
338 mForegroundAlpha = (int) Math.round((r * 255));
339 mOverScrollForegroundDrawable.setAlpha(mForegroundAlpha);
340 invalidate();
341 }
342
Patrick Dubroy96864c32011-03-10 17:17:23 -0800343 void setPressedOrFocusedIcon(BubbleTextView icon) {
344 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
345 // requires an expanded clip rect (due to the glow's blur radius)
346 BubbleTextView oldIcon = mPressedOrFocusedIcon;
347 mPressedOrFocusedIcon = icon;
348 if (oldIcon != null) {
349 invalidateBubbleTextView(oldIcon);
350 }
351 if (mPressedOrFocusedIcon != null) {
352 invalidateBubbleTextView(mPressedOrFocusedIcon);
353 }
354 }
355
Michael Jurka33945b22010-12-21 18:19:38 -0800356 void setIsDragOverlapping(boolean isDragOverlapping) {
357 if (mIsDragOverlapping != isDragOverlapping) {
358 mIsDragOverlapping = isDragOverlapping;
359 invalidate();
360 }
361 }
362
363 boolean getIsDragOverlapping() {
364 return mIsDragOverlapping;
365 }
366
Adam Cohenebea84d2011-11-09 17:20:41 -0800367 protected void setOverscrollTransformsDirty(boolean dirty) {
368 mScrollingTransformsDirty = dirty;
369 }
370
371 protected void resetOverscrollTransforms() {
372 if (mScrollingTransformsDirty) {
373 setOverscrollTransformsDirty(false);
374 setTranslationX(0);
375 setRotationY(0);
376 // It doesn't matter if we pass true or false here, the important thing is that we
377 // pass 0, which results in the overscroll drawable not being drawn any more.
378 setOverScrollAmount(0, false);
379 setPivotX(getMeasuredWidth() / 2);
380 setPivotY(getMeasuredHeight() / 2);
381 }
382 }
383
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700384 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700385 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700386 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
387 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
388 // When we're small, we are either drawn normally or in the "accepts drops" state (during
389 // a drag). However, we also drag the mini hover background *over* one of those two
390 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700391 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700392 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800393
394 if (mIsDragOverlapping) {
395 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700396 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700397 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700398 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700399 }
Michael Jurka33945b22010-12-21 18:19:38 -0800400
401 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
402 bg.setBounds(mBackgroundRect);
403 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700404 }
Romain Guya6abce82009-11-10 02:54:41 -0800405
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700406 if (mCrosshairsVisibility > 0.0f) {
407 final int countX = mCountX;
408 final int countY = mCountY;
409
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700410 final float MAX_ALPHA = 0.4f;
411 final int MAX_VISIBLE_DISTANCE = 600;
412 final float DISTANCE_MULTIPLIER = 0.002f;
413
414 final Drawable d = mCrosshairsDrawable;
415 final int width = d.getIntrinsicWidth();
416 final int height = d.getIntrinsicHeight();
417
Winson Chung4b825dcd2011-06-19 12:41:22 -0700418 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700419 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700420 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700421 for (int row = 0; row <= countY; row++) {
422 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
423 float dist = mTmpPointF.length();
424 // Crosshairs further from the drag point are more faint
425 float alpha = Math.min(MAX_ALPHA,
426 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
427 if (alpha > 0.0f) {
428 d.setBounds(x, y, x + width, y + height);
429 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
430 d.draw(canvas);
431 }
432 y += mCellHeight + mHeightGap;
433 }
434 x += mCellWidth + mWidthGap;
435 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700436 }
Winson Chung150fbab2010-09-29 17:14:26 -0700437
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700438 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700439 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700440 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700441 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800442 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700443 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700444 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800445 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700446 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700447 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800448
449 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
450 // requires an expanded clip rect (due to the glow's blur radius)
451 if (mPressedOrFocusedIcon != null) {
452 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
453 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
454 if (b != null) {
455 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700456 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
457 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800458 null);
459 }
460 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700461
Adam Cohen482ed822012-03-02 14:15:13 -0800462 if (DEBUG_VISUALIZE_OCCUPIED) {
463 int[] pt = new int[2];
464 ColorDrawable cd = new ColorDrawable(Color.RED);
465 cd.setBounds(0, 0, 80, 80);
466 for (int i = 0; i < mCountX; i++) {
467 for (int j = 0; j < mCountY; j++) {
468 if (mOccupied[i][j]) {
469 cellToPoint(i, j, pt);
470 canvas.save();
471 canvas.translate(pt[0], pt[1]);
472 cd.draw(canvas);
473 canvas.restore();
474 }
475 }
476 }
477 }
478
Adam Cohen69ce2e52011-07-03 19:25:21 -0700479 // The folder outer / inner ring image(s)
480 for (int i = 0; i < mFolderOuterRings.size(); i++) {
481 FolderRingAnimator fra = mFolderOuterRings.get(i);
482
483 // Draw outer ring
484 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
485 int width = (int) fra.getOuterRingSize();
486 int height = width;
487 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
488
489 int centerX = mTempLocation[0] + mCellWidth / 2;
490 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
491
492 canvas.save();
493 canvas.translate(centerX - width / 2, centerY - height / 2);
494 d.setBounds(0, 0, width, height);
495 d.draw(canvas);
496 canvas.restore();
497
498 // Draw inner ring
499 d = FolderRingAnimator.sSharedInnerRingDrawable;
500 width = (int) fra.getInnerRingSize();
501 height = width;
502 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
503
504 centerX = mTempLocation[0] + mCellWidth / 2;
505 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
506 canvas.save();
507 canvas.translate(centerX - width / 2, centerY - width / 2);
508 d.setBounds(0, 0, width, height);
509 d.draw(canvas);
510 canvas.restore();
511 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700512
513 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
514 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
515 int width = d.getIntrinsicWidth();
516 int height = d.getIntrinsicHeight();
517
518 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
519 int centerX = mTempLocation[0] + mCellWidth / 2;
520 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
521
522 canvas.save();
523 canvas.translate(centerX - width / 2, centerY - width / 2);
524 d.setBounds(0, 0, width, height);
525 d.draw(canvas);
526 canvas.restore();
527 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700528 }
529
Adam Cohenb5ba0972011-09-07 18:02:31 -0700530 @Override
531 protected void dispatchDraw(Canvas canvas) {
532 super.dispatchDraw(canvas);
533 if (mForegroundAlpha > 0) {
534 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
535 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
536 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
537 mOverScrollForegroundDrawable.draw(canvas);
538 p.setXfermode(null);
539 }
540 }
541
Adam Cohen69ce2e52011-07-03 19:25:21 -0700542 public void showFolderAccept(FolderRingAnimator fra) {
543 mFolderOuterRings.add(fra);
544 }
545
546 public void hideFolderAccept(FolderRingAnimator fra) {
547 if (mFolderOuterRings.contains(fra)) {
548 mFolderOuterRings.remove(fra);
549 }
550 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700551 }
552
Adam Cohenc51934b2011-07-26 21:07:43 -0700553 public void setFolderLeaveBehindCell(int x, int y) {
554 mFolderLeaveBehindCell[0] = x;
555 mFolderLeaveBehindCell[1] = y;
556 invalidate();
557 }
558
559 public void clearFolderLeaveBehind() {
560 mFolderLeaveBehindCell[0] = -1;
561 mFolderLeaveBehindCell[1] = -1;
562 invalidate();
563 }
564
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700565 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700566 public boolean shouldDelayChildPressedState() {
567 return false;
568 }
569
570 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700571 public void cancelLongPress() {
572 super.cancelLongPress();
573
574 // Cancel long press for all children
575 final int count = getChildCount();
576 for (int i = 0; i < count; i++) {
577 final View child = getChildAt(i);
578 child.cancelLongPress();
579 }
580 }
581
Michael Jurkadee05892010-07-27 10:01:56 -0700582 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
583 mInterceptTouchListener = listener;
584 }
585
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700587 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 }
589
590 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700591 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 }
593
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800594 public void setIsHotseat(boolean isHotseat) {
595 mIsHotseat = isHotseat;
596 }
597
Winson Chungeecf02d2012-03-02 17:14:58 -0800598 public float getChildrenScale() {
599 return mIsHotseat ? mHotseatChildScale : mChildScale;
600 }
601
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700602 public boolean addViewToCellLayout(
603 View child, int index, int childId, LayoutParams params, boolean markCells) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800604 return addViewToCellLayout(child, index, childId, params, markCells, false);
605 }
606
Winson Chungeecf02d2012-03-02 17:14:58 -0800607 private void scaleChild(BubbleTextView bubbleChild, float pivot, float scale) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800608 // If we haven't measured the child yet, do it now
609 // (this happens if we're being dropped from all-apps
610 if (bubbleChild.getLayoutParams() instanceof LayoutParams &&
611 (bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700612 getShortcutsAndWidgets().measureChild(bubbleChild);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800613 }
614 int measuredWidth = bubbleChild.getMeasuredWidth();
615 int measuredHeight = bubbleChild.getMeasuredHeight();
616
Andrew Flynnbc239a12012-03-06 11:39:49 -0800617 bubbleChild.setScaleX(scale);
618 bubbleChild.setScaleY(scale);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800619 }
620
621 private void resetChild(BubbleTextView bubbleChild) {
622 bubbleChild.setScaleX(1f);
623 bubbleChild.setScaleY(1f);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800624
625 bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
626 }
627
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800628 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
629 boolean markCells, boolean allApps) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700630 final LayoutParams lp = params;
631
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800632 // Hotseat icons - scale down and remove text
633 // Don't scale the all apps button
634 // scale percent set to -1 means do not scale
635 // Only scale BubbleTextViews
636 if (child instanceof BubbleTextView) {
637 BubbleTextView bubbleChild = (BubbleTextView) child;
638
Andrew Flynnbc239a12012-03-06 11:39:49 -0800639 // Start the child with 100% scale and visible text
640 resetChild(bubbleChild);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800641
Winson Chungeecf02d2012-03-02 17:14:58 -0800642 if (mIsHotseat && !allApps && mHotseatChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800643 // Scale/make transparent for a hotseat
Winson Chungeecf02d2012-03-02 17:14:58 -0800644 scaleChild(bubbleChild, 0f, mHotseatChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800645
Andrew Flynnbc239a12012-03-06 11:39:49 -0800646 bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent));
Winson Chungeecf02d2012-03-02 17:14:58 -0800647 } else if (mChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800648 // Else possibly still scale it if we need to for smaller icons
Winson Chungeecf02d2012-03-02 17:14:58 -0800649 scaleChild(bubbleChild, 0f, mChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800650 }
651 }
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 // Generate an id for each view, this assumes we have at most 256x256 cells
654 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700655 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700656 // If the horizontal or vertical span is set to -1, it is taken to
657 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700658 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
659 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660
Winson Chungaafa03c2010-06-11 17:34:16 -0700661 child.setId(childId);
662
Michael Jurkaa52570f2012-03-20 03:18:20 -0700663 mShortcutsAndWidgets.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700664
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700665 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666
Winson Chungaafa03c2010-06-11 17:34:16 -0700667 return true;
668 }
669 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700671
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700673 public void removeAllViews() {
674 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700675 mShortcutsAndWidgets.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 }
677
678 @Override
679 public void removeAllViewsInLayout() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700680 if (mShortcutsAndWidgets.getChildCount() > 0) {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700681 clearOccupiedCells();
Michael Jurkaa52570f2012-03-20 03:18:20 -0700682 mShortcutsAndWidgets.removeAllViewsInLayout();
Michael Jurka7cfc2822011-08-02 20:19:24 -0700683 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684 }
685
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700686 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700687 mShortcutsAndWidgets.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700688 }
689
Michael Jurka0280c3b2010-09-17 15:00:07 -0700690 @Override
691 public void removeView(View view) {
692 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700693 mShortcutsAndWidgets.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700694 }
695
696 @Override
697 public void removeViewAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700698 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(index));
699 mShortcutsAndWidgets.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700700 }
701
702 @Override
703 public void removeViewInLayout(View view) {
704 markCellsAsUnoccupiedForView(view);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700705 mShortcutsAndWidgets.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700706 }
707
708 @Override
709 public void removeViews(int start, int count) {
710 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700711 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700712 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700713 mShortcutsAndWidgets.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700714 }
715
716 @Override
717 public void removeViewsInLayout(int start, int count) {
718 for (int i = start; i < start + count; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700719 markCellsAsUnoccupiedForView(mShortcutsAndWidgets.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700720 }
Michael Jurkaa52570f2012-03-20 03:18:20 -0700721 mShortcutsAndWidgets.removeViewsInLayout(start, count);
Michael Jurkaabded662011-03-04 12:06:57 -0800722 }
723
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 @Override
725 protected void onAttachedToWindow() {
726 super.onAttachedToWindow();
727 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
728 }
729
Michael Jurkaaf442092010-06-10 17:01:57 -0700730 public void setTagToCellInfoForPoint(int touchX, int touchY) {
731 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800732 Rect frame = mRect;
Michael Jurkaaf442092010-06-10 17:01:57 -0700733 final int x = touchX + mScrollX;
734 final int y = touchY + mScrollY;
Michael Jurkaa52570f2012-03-20 03:18:20 -0700735 final int count = mShortcutsAndWidgets.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700736
737 boolean found = false;
738 for (int i = count - 1; i >= 0; i--) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700739 final View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800740 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700741
Adam Cohen1b607ed2011-03-03 17:26:50 -0800742 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
743 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700744 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700745
Winson Chungeecf02d2012-03-02 17:14:58 -0800746 float scale = child.getScaleX();
747 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
748 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700749 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
750 // offset that by this CellLayout's padding to test an (x,y) point that is relative
751 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700752 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chungeecf02d2012-03-02 17:14:58 -0800753 frame.inset((int) (frame.width() * (1f - scale) / 2),
754 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700755
Michael Jurkaaf442092010-06-10 17:01:57 -0700756 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700757 cellInfo.cell = child;
758 cellInfo.cellX = lp.cellX;
759 cellInfo.cellY = lp.cellY;
760 cellInfo.spanX = lp.cellHSpan;
761 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700762 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700763 break;
764 }
765 }
766 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700767
Michael Jurkad771c962011-08-09 15:00:48 -0700768 mLastDownOnOccupiedCell = found;
769
Michael Jurkaaf442092010-06-10 17:01:57 -0700770 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700771 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700772 pointToCellExact(x, y, cellXY);
773
Michael Jurkaaf442092010-06-10 17:01:57 -0700774 cellInfo.cell = null;
775 cellInfo.cellX = cellXY[0];
776 cellInfo.cellY = cellXY[1];
777 cellInfo.spanX = 1;
778 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700779 }
780 setTag(cellInfo);
781 }
782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 @Override
784 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700785 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
786 // even in the case where we return early. Not clearing here was causing bugs whereby on
787 // long-press we'd end up picking up an item from a previous drag operation.
788 final int action = ev.getAction();
789
790 if (action == MotionEvent.ACTION_DOWN) {
791 clearTagCellInfo();
792 }
793
Michael Jurkadee05892010-07-27 10:01:56 -0700794 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
795 return true;
796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797
798 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700799 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 return false;
803 }
804
Adam Cohenc1997fd2011-08-15 18:26:39 -0700805 private void clearTagCellInfo() {
806 final CellInfo cellInfo = mCellInfo;
807 cellInfo.cell = null;
808 cellInfo.cellX = -1;
809 cellInfo.cellY = -1;
810 cellInfo.spanX = 0;
811 cellInfo.spanY = 0;
812 setTag(cellInfo);
813 }
814
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700816 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
818
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700819 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700820 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 * @param x X coordinate of the point
822 * @param y Y coordinate of the point
823 * @param result Array of 2 ints to hold the x and y coordinate of the cell
824 */
825 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700826 final int hStartPadding = getPaddingLeft();
827 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828
829 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
830 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
831
Adam Cohend22015c2010-07-26 22:02:18 -0700832 final int xAxis = mCountX;
833 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834
835 if (result[0] < 0) result[0] = 0;
836 if (result[0] >= xAxis) result[0] = xAxis - 1;
837 if (result[1] < 0) result[1] = 0;
838 if (result[1] >= yAxis) result[1] = yAxis - 1;
839 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700840
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 /**
842 * Given a point, return the cell that most closely encloses that point
843 * @param x X coordinate of the point
844 * @param y Y coordinate of the point
845 * @param result Array of 2 ints to hold the x and y coordinate of the cell
846 */
847 void pointToCellRounded(int x, int y, int[] result) {
848 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
849 }
850
851 /**
852 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700853 *
854 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700856 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 * @param result Array of 2 ints to hold the x and y coordinate of the point
858 */
859 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700860 final int hStartPadding = getPaddingLeft();
861 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862
863 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
864 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
865 }
866
Adam Cohene3e27a82011-04-15 12:07:39 -0700867 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800868 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700869 *
870 * @param cellX X coordinate of the cell
871 * @param cellY Y coordinate of the cell
872 *
873 * @param result Array of 2 ints to hold the x and y coordinate of the point
874 */
875 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Adam Cohen47a876d2012-03-19 13:21:41 -0700876 regionToCenterPoint(cellX, cellY, 1, 1, result);
877 }
878
879 /**
880 * Given a cell coordinate and span return the point that represents the center of the regio
881 *
882 * @param cellX X coordinate of the cell
883 * @param cellY Y coordinate of the cell
884 *
885 * @param result Array of 2 ints to hold the x and y coordinate of the point
886 */
887 void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700888 final int hStartPadding = getPaddingLeft();
889 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700890
Adam Cohen47a876d2012-03-19 13:21:41 -0700891 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) +
892 (spanX * mCellWidth + (spanX - 1) * mWidthGap) / 2;
893 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) +
894 (spanY * mCellHeight + (spanY - 1) * mHeightGap) / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -0700895 }
896
Adam Cohen482ed822012-03-02 14:15:13 -0800897 public float getDistanceFromCell(float x, float y, int[] cell) {
898 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
899 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
900 Math.pow(y - mTmpPoint[1], 2));
901 return distance;
902 }
903
Romain Guy84f296c2009-11-04 15:00:44 -0800904 int getCellWidth() {
905 return mCellWidth;
906 }
907
908 int getCellHeight() {
909 return mCellHeight;
910 }
911
Adam Cohend4844c32011-02-18 19:25:06 -0800912 int getWidthGap() {
913 return mWidthGap;
914 }
915
916 int getHeightGap() {
917 return mHeightGap;
918 }
919
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700920 Rect getContentRect(Rect r) {
921 if (r == null) {
922 r = new Rect();
923 }
924 int left = getPaddingLeft();
925 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700926 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
927 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700928 r.set(left, top, right, bottom);
929 return r;
930 }
931
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 @Override
933 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
934 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700935
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700937 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
938
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
940 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700941
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
943 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
944 }
945
Adam Cohend22015c2010-07-26 22:02:18 -0700946 int numWidthGaps = mCountX - 1;
947 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948
Adam Cohen234c4cd2011-07-17 21:03:04 -0700949 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700950 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
951 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
952 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
953 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
954 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
955 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700956 mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700957 } else {
958 mWidthGap = mOriginalWidthGap;
959 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700960 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700961
Michael Jurka8c920dd2011-01-20 14:16:56 -0800962 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
963 int newWidth = widthSpecSize;
964 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700965 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700966 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700967 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700968 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700969 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700970 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700971 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800972
973 int count = getChildCount();
974 for (int i = 0; i < count; i++) {
975 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700976 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
977 mPaddingRight, MeasureSpec.EXACTLY);
978 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
979 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800980 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
981 }
982 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 }
984
985 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700986 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800989 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700990 child.layout(mPaddingLeft, mPaddingTop,
991 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 }
993 }
994
995 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700996 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
997 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700998 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -0700999 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
1000 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001001 }
1002
1003 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001005 mShortcutsAndWidgets.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
1007
1008 @Override
1009 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001010 mShortcutsAndWidgets.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 }
1012
Michael Jurka5f1c5092010-09-03 14:15:02 -07001013 public float getBackgroundAlpha() {
1014 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001015 }
1016
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001017 public void setBackgroundAlphaMultiplier(float multiplier) {
1018 mBackgroundAlphaMultiplier = multiplier;
1019 }
1020
Adam Cohenddb82192010-11-10 16:32:54 -08001021 public float getBackgroundAlphaMultiplier() {
1022 return mBackgroundAlphaMultiplier;
1023 }
1024
Michael Jurka5f1c5092010-09-03 14:15:02 -07001025 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001026 if (mBackgroundAlpha != alpha) {
1027 mBackgroundAlpha = alpha;
1028 invalidate();
1029 }
Michael Jurkadee05892010-07-27 10:01:56 -07001030 }
1031
Michael Jurkaa52570f2012-03-20 03:18:20 -07001032 public void setShortcutAndWidgetAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001033 final int childCount = getChildCount();
1034 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001035 getChildAt(i).setAlpha(alpha);
1036 }
1037 }
1038
Michael Jurkaa52570f2012-03-20 03:18:20 -07001039 public ShortcutAndWidgetContainer getShortcutsAndWidgets() {
1040 if (getChildCount() > 0) {
1041 return (ShortcutAndWidgetContainer) getChildAt(0);
1042 }
1043 return null;
1044 }
1045
Patrick Dubroy440c3602010-07-13 17:50:32 -07001046 public View getChildAt(int x, int y) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001047 return mShortcutsAndWidgets.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001048 }
1049
Adam Cohen76fc0852011-06-17 13:26:23 -07001050 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001051 int delay, boolean permanent, boolean adjustOccupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001052 ShortcutAndWidgetContainer clc = getShortcutsAndWidgets();
Adam Cohen482ed822012-03-02 14:15:13 -08001053 boolean[][] occupied = mOccupied;
1054 if (!permanent) {
1055 occupied = mTmpOccupied;
1056 }
1057
1058 if (clc.indexOfChild(child) != -1 && !occupied[cellX][cellY]) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001059 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1060 final ItemInfo info = (ItemInfo) child.getTag();
1061
1062 // We cancel any existing animations
1063 if (mReorderAnimators.containsKey(lp)) {
1064 mReorderAnimators.get(lp).cancel();
1065 mReorderAnimators.remove(lp);
1066 }
1067
Adam Cohen482ed822012-03-02 14:15:13 -08001068 final int oldX = lp.x;
1069 final int oldY = lp.y;
1070 if (adjustOccupied) {
1071 occupied[lp.cellX][lp.cellY] = false;
1072 occupied[cellX][cellY] = true;
1073 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001074 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001075 if (permanent) {
1076 lp.cellX = info.cellX = cellX;
1077 lp.cellY = info.cellY = cellY;
1078 } else {
1079 lp.tmpCellX = cellX;
1080 lp.tmpCellY = cellY;
1081 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001082 clc.setupLp(lp);
1083 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001084 final int newX = lp.x;
1085 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001086
Adam Cohen76fc0852011-06-17 13:26:23 -07001087 lp.x = oldX;
1088 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001089
Adam Cohen482ed822012-03-02 14:15:13 -08001090 // Exit early if we're not actually moving the view
1091 if (oldX == newX && oldY == newY) {
1092 lp.isLockedToGrid = true;
1093 return true;
1094 }
1095
1096 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
1097 va.setDuration(duration);
1098 mReorderAnimators.put(lp, va);
1099
1100 va.addUpdateListener(new AnimatorUpdateListener() {
1101 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001102 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001103 float r = ((Float) animation.getAnimatedValue()).floatValue();
1104 child.setTranslationX(r * (newX - oldX));
1105 child.setTranslationY(r * (newY - oldY));
Adam Cohenbfbfd262011-06-13 16:55:12 -07001106 }
1107 });
Adam Cohen482ed822012-03-02 14:15:13 -08001108 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001109 boolean cancelled = false;
1110 public void onAnimationEnd(Animator animation) {
1111 // If the animation was cancelled, it means that another animation
1112 // has interrupted this one, and we don't want to lock the item into
1113 // place just yet.
1114 if (!cancelled) {
Adam Cohen482ed822012-03-02 14:15:13 -08001115 child.setTranslationX(0);
1116 child.setTranslationY(0);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001117 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001118 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001119 }
1120 if (mReorderAnimators.containsKey(lp)) {
1121 mReorderAnimators.remove(lp);
1122 }
1123 }
1124 public void onAnimationCancel(Animator animation) {
1125 cancelled = true;
1126 }
1127 });
Adam Cohen482ed822012-03-02 14:15:13 -08001128 va.setStartDelay(delay);
1129 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001130 return true;
1131 }
1132 return false;
1133 }
1134
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001135 /**
1136 * Estimate where the top left cell of the dragged item will land if it is dropped.
1137 *
1138 * @param originX The X value of the top left corner of the item
1139 * @param originY The Y value of the top left corner of the item
1140 * @param spanX The number of horizontal cells that the item spans
1141 * @param spanY The number of vertical cells that the item spans
1142 * @param result The estimated drop cell X and Y.
1143 */
1144 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001145 final int countX = mCountX;
1146 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001147
Michael Jurkaa63c4522010-08-19 13:52:27 -07001148 // pointToCellRounded takes the top left of a cell but will pad that with
1149 // cellWidth/2 and cellHeight/2 when finding the matching cell
1150 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001151
1152 // If the item isn't fully on this screen, snap to the edges
1153 int rightOverhang = result[0] + spanX - countX;
1154 if (rightOverhang > 0) {
1155 result[0] -= rightOverhang; // Snap to right
1156 }
1157 result[0] = Math.max(0, result[0]); // Snap to left
1158 int bottomOverhang = result[1] + spanY - countY;
1159 if (bottomOverhang > 0) {
1160 result[1] -= bottomOverhang; // Snap to bottom
1161 }
1162 result[1] = Math.max(0, result[1]); // Snap to top
1163 }
1164
Adam Cohen482ed822012-03-02 14:15:13 -08001165 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1166 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001167 final int oldDragCellX = mDragCell[0];
1168 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001169
Winson Chungb8c69f32011-10-19 21:36:08 -07001170 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001171 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1172 } else {
1173 mDragCenter.set(originX, originY);
1174 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001175
Adam Cohen2801caf2011-05-13 20:57:39 -07001176 if (dragOutline == null && v == null) {
1177 if (mCrosshairsDrawable != null) {
1178 invalidate();
1179 }
1180 return;
1181 }
1182
Adam Cohen482ed822012-03-02 14:15:13 -08001183 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1184 mDragCell[0] = cellX;
1185 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001186 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001187 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001188 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001189
Joe Onorato4be866d2010-10-10 11:26:02 -07001190 int left = topLeft[0];
1191 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001192
Winson Chungb8c69f32011-10-19 21:36:08 -07001193 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001194 // When drawing the drag outline, it did not account for margin offsets
1195 // added by the view's parent.
1196 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1197 left += lp.leftMargin;
1198 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001199
Adam Cohen99e8b402011-03-25 19:23:43 -07001200 // Offsets due to the size difference between the View and the dragOutline.
1201 // There is a size difference to account for the outer blur, which may lie
1202 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001203 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001204 // We center about the x axis
1205 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1206 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001207 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001208 if (dragOffset != null && dragRegion != null) {
1209 // Center the drag region *horizontally* in the cell and apply a drag
1210 // outline offset
1211 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1212 - dragRegion.width()) / 2;
1213 top += dragOffset.y;
1214 } else {
1215 // Center the drag outline in the cell
1216 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1217 - dragOutline.getWidth()) / 2;
1218 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1219 - dragOutline.getHeight()) / 2;
1220 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001221 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001222 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001223 mDragOutlineAnims[oldIndex].animateOut();
1224 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001225 Rect r = mDragOutlines[mDragOutlineCurrent];
1226 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1227 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001228 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001229 }
Winson Chung150fbab2010-09-29 17:14:26 -07001230
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001231 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1232 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001233 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001234
1235 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1236 if (mCrosshairsDrawable != null) {
1237 invalidate();
1238 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001239 }
1240
Adam Cohene0310962011-04-18 16:15:31 -07001241 public void clearDragOutlines() {
1242 final int oldIndex = mDragOutlineCurrent;
1243 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001244 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001245 }
1246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001248 * Find a vacant area that will fit the given bounds nearest the requested
1249 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001250 *
Romain Guy51afc022009-05-04 18:03:43 -07001251 * @param pixelX The X location at which you want to search for a vacant area.
1252 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001253 * @param spanX Horizontal span of the object.
1254 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001255 * @param result Array in which to place the result, or null (in which case a new array will
1256 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001257 * @return The X, Y cell of a vacant area that can contain this object,
1258 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001260 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1261 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001262 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001263 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001264
Michael Jurka6a1435d2010-09-27 17:35:12 -07001265 /**
1266 * Find a vacant area that will fit the given bounds nearest the requested
1267 * cell location. Uses Euclidean distance to score multiple vacant areas.
1268 *
1269 * @param pixelX The X location at which you want to search for a vacant area.
1270 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001271 * @param minSpanX The minimum horizontal span required
1272 * @param minSpanY The minimum vertical span required
1273 * @param spanX Horizontal span of the object.
1274 * @param spanY Vertical span of the object.
1275 * @param result Array in which to place the result, or null (in which case a new array will
1276 * be allocated)
1277 * @return The X, Y cell of a vacant area that can contain this object,
1278 * nearest the requested location.
1279 */
1280 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1281 int spanY, int[] result, int[] resultSpan) {
1282 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1283 result, resultSpan);
1284 }
1285
1286 /**
1287 * Find a vacant area that will fit the given bounds nearest the requested
1288 * cell location. Uses Euclidean distance to score multiple vacant areas.
1289 *
1290 * @param pixelX The X location at which you want to search for a vacant area.
1291 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001292 * @param spanX Horizontal span of the object.
1293 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001294 * @param ignoreOccupied If true, the result can be an occupied cell
1295 * @param result Array in which to place the result, or null (in which case a new array will
1296 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001297 * @return The X, Y cell of a vacant area that can contain this object,
1298 * nearest the requested location.
1299 */
Adam Cohendf035382011-04-11 17:22:04 -07001300 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1301 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001302 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001303 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001304 }
1305
1306 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1307 private void lazyInitTempRectStack() {
1308 if (mTempRectStack.isEmpty()) {
1309 for (int i = 0; i < mCountX * mCountY; i++) {
1310 mTempRectStack.push(new Rect());
1311 }
1312 }
1313 }
Adam Cohen482ed822012-03-02 14:15:13 -08001314
Adam Cohend41fbf52012-02-16 23:53:59 -08001315 private void recycleTempRects(Stack<Rect> used) {
1316 while (!used.isEmpty()) {
1317 mTempRectStack.push(used.pop());
1318 }
1319 }
1320
1321 /**
1322 * Find a vacant area that will fit the given bounds nearest the requested
1323 * cell location. Uses Euclidean distance to score multiple vacant areas.
1324 *
1325 * @param pixelX The X location at which you want to search for a vacant area.
1326 * @param pixelY The Y location at which you want to search for a vacant area.
1327 * @param minSpanX The minimum horizontal span required
1328 * @param minSpanY The minimum vertical span required
1329 * @param spanX Horizontal span of the object.
1330 * @param spanY Vertical span of the object.
1331 * @param ignoreOccupied If true, the result can be an occupied cell
1332 * @param result Array in which to place the result, or null (in which case a new array will
1333 * be allocated)
1334 * @return The X, Y cell of a vacant area that can contain this object,
1335 * nearest the requested location.
1336 */
1337 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001338 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1339 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001340 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001341 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001342 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001343
Adam Cohene3e27a82011-04-15 12:07:39 -07001344 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1345 // to the center of the item, but we are searching based on the top-left cell, so
1346 // we translate the point over to correspond to the top-left.
1347 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1348 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1349
Jeff Sharkey70864282009-04-07 21:08:40 -07001350 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001351 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001352 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001353 final Rect bestRect = new Rect(-1, -1, -1, -1);
1354 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001355
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001356 final int countX = mCountX;
1357 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001358
Adam Cohend41fbf52012-02-16 23:53:59 -08001359 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1360 spanX < minSpanX || spanY < minSpanY) {
1361 return bestXY;
1362 }
1363
1364 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001365 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001366 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1367 int ySize = -1;
1368 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001369 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001370 // First, let's see if this thing fits anywhere
1371 for (int i = 0; i < minSpanX; i++) {
1372 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001373 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001374 continue inner;
1375 }
Michael Jurkac28de512010-08-13 11:27:44 -07001376 }
1377 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001378 xSize = minSpanX;
1379 ySize = minSpanY;
1380
1381 // We know that the item will fit at _some_ acceptable size, now let's see
1382 // how big we can make it. We'll alternate between incrementing x and y spans
1383 // until we hit a limit.
1384 boolean incX = true;
1385 boolean hitMaxX = xSize >= spanX;
1386 boolean hitMaxY = ySize >= spanY;
1387 while (!(hitMaxX && hitMaxY)) {
1388 if (incX && !hitMaxX) {
1389 for (int j = 0; j < ySize; j++) {
1390 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1391 // We can't move out horizontally
1392 hitMaxX = true;
1393 }
1394 }
1395 if (!hitMaxX) {
1396 xSize++;
1397 }
1398 } else if (!hitMaxY) {
1399 for (int i = 0; i < xSize; i++) {
1400 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1401 // We can't move out vertically
1402 hitMaxY = true;
1403 }
1404 }
1405 if (!hitMaxY) {
1406 ySize++;
1407 }
1408 }
1409 hitMaxX |= xSize >= spanX;
1410 hitMaxY |= ySize >= spanY;
1411 incX = !incX;
1412 }
1413 incX = true;
1414 hitMaxX = xSize >= spanX;
1415 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001416 }
Winson Chung0be025d2011-05-23 17:45:09 -07001417 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001418 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419
Adam Cohend41fbf52012-02-16 23:53:59 -08001420 // We verify that the current rect is not a sub-rect of any of our previous
1421 // candidates. In this case, the current rect is disqualified in favour of the
1422 // containing rect.
1423 Rect currentRect = mTempRectStack.pop();
1424 currentRect.set(x, y, x + xSize, y + ySize);
1425 boolean contained = false;
1426 for (Rect r : validRegions) {
1427 if (r.contains(currentRect)) {
1428 contained = true;
1429 break;
1430 }
1431 }
1432 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001433 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1434 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001435
Adam Cohend41fbf52012-02-16 23:53:59 -08001436 if ((distance <= bestDistance && !contained) ||
1437 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001438 bestDistance = distance;
1439 bestXY[0] = x;
1440 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001441 if (resultSpan != null) {
1442 resultSpan[0] = xSize;
1443 resultSpan[1] = ySize;
1444 }
1445 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
1448 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001449 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001450 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451
Adam Cohenc0dcf592011-06-01 15:30:43 -07001452 // Return -1, -1 if no suitable location found
1453 if (bestDistance == Double.MAX_VALUE) {
1454 bestXY[0] = -1;
1455 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001456 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001457 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001458 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001460
Adam Cohen482ed822012-03-02 14:15:13 -08001461 /**
1462 * Find a vacant area that will fit the given bounds nearest the requested
1463 * cell location, and will also weigh in a suggested direction vector of the
1464 * desired location. This method computers distance based on unit grid distances,
1465 * not pixel distances.
1466 *
Adam Cohen47a876d2012-03-19 13:21:41 -07001467 * @param cellX The X cell nearest to which you want to search for a vacant area.
1468 * @param cellY The Y cell nearest which you want to search for a vacant area.
Adam Cohen482ed822012-03-02 14:15:13 -08001469 * @param spanX Horizontal span of the object.
1470 * @param spanY Vertical span of the object.
Adam Cohen47a876d2012-03-19 13:21:41 -07001471 * @param direction The favored direction in which the views should move from x, y
1472 * @param exactDirectionOnly If this parameter is true, then only solutions where the direction
1473 * matches exactly. Otherwise we find the best matching direction.
1474 * @param occoupied The array which represents which cells in the CellLayout are occupied
1475 * @param blockOccupied The array which represents which cells in the specified block (cellX,
1476 * cellY, spanX, spanY) are occupied. This is used when try to move a group of views.
Adam Cohen482ed822012-03-02 14:15:13 -08001477 * @param result Array in which to place the result, or null (in which case a new array will
1478 * be allocated)
1479 * @return The X, Y cell of a vacant area that can contain this object,
1480 * nearest the requested location.
1481 */
1482 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen47a876d2012-03-19 13:21:41 -07001483 boolean[][] occupied, boolean blockOccupied[][], int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001484 // Keep track of best-scoring drop area
1485 final int[] bestXY = result != null ? result : new int[2];
1486 float bestDistance = Float.MAX_VALUE;
1487 int bestDirectionScore = Integer.MIN_VALUE;
1488
1489 final int countX = mCountX;
1490 final int countY = mCountY;
1491
1492 for (int y = 0; y < countY - (spanY - 1); y++) {
1493 inner:
1494 for (int x = 0; x < countX - (spanX - 1); x++) {
1495 // First, let's see if this thing fits anywhere
1496 for (int i = 0; i < spanX; i++) {
1497 for (int j = 0; j < spanY; j++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001498 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
Adam Cohen482ed822012-03-02 14:15:13 -08001499 continue inner;
1500 }
1501 }
1502 }
1503
1504 float distance = (float)
1505 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1506 int[] curDirection = mTmpPoint;
Adam Cohen47a876d2012-03-19 13:21:41 -07001507 computeDirectionVector(x - cellX, y - cellY, curDirection);
1508 // The direction score is just the dot product of the two candidate direction
1509 // and that passed in.
Adam Cohen482ed822012-03-02 14:15:13 -08001510 int curDirectionScore = direction[0] * curDirection[0] +
1511 direction[1] * curDirection[1];
Adam Cohen47a876d2012-03-19 13:21:41 -07001512 boolean exactDirectionOnly = false;
1513 boolean directionMatches = direction[0] == curDirection[0] &&
1514 direction[0] == curDirection[0];
1515 if ((directionMatches || !exactDirectionOnly) &&
1516 Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
Adam Cohen482ed822012-03-02 14:15:13 -08001517 bestDistance) == 0 && curDirectionScore > bestDirectionScore)) {
1518 bestDistance = distance;
1519 bestDirectionScore = curDirectionScore;
1520 bestXY[0] = x;
1521 bestXY[1] = y;
1522 }
1523 }
1524 }
1525
1526 // Return -1, -1 if no suitable location found
1527 if (bestDistance == Float.MAX_VALUE) {
1528 bestXY[0] = -1;
1529 bestXY[1] = -1;
1530 }
1531 return bestXY;
1532 }
1533
Adam Cohen47a876d2012-03-19 13:21:41 -07001534 private int[] findNearestAreaInDirection(int cellX, int cellY, int spanX, int spanY,
1535 int[] direction,boolean[][] occupied,
1536 boolean blockOccupied[][], int[] result) {
1537 // Keep track of best-scoring drop area
1538 final int[] bestXY = result != null ? result : new int[2];
1539 bestXY[0] = -1;
1540 bestXY[1] = -1;
1541 float bestDistance = Float.MAX_VALUE;
1542
1543 // We use this to march in a single direction
1544 if (direction[0] != 0 && direction[1] != 0) {
1545 return bestXY;
1546 }
1547
1548 // This will only incrememnet one of x or y based on the assertion above
1549 int x = cellX + direction[0];
1550 int y = cellY + direction[1];
1551 while (x >= 0 && x + spanX <= mCountX && y >= 0 && y + spanY <= mCountY) {
1552
1553 boolean fail = false;
1554 for (int i = 0; i < spanX; i++) {
1555 for (int j = 0; j < spanY; j++) {
1556 if (occupied[x + i][y + j] && (blockOccupied == null || blockOccupied[i][j])) {
1557 fail = true;
1558 }
1559 }
1560 }
1561 if (!fail) {
1562 float distance = (float)
1563 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1564 if (Float.compare(distance, bestDistance) < 0) {
1565 bestDistance = distance;
1566 bestXY[0] = x;
1567 bestXY[1] = y;
1568 }
1569 }
1570 x += direction[0];
1571 y += direction[1];
1572 }
1573 return bestXY;
1574 }
1575
Adam Cohen482ed822012-03-02 14:15:13 -08001576 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001577 int[] direction, ItemConfiguration currentState) {
1578 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001579 boolean success = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001580 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001581 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1582
Adam Cohen8baab352012-03-20 17:39:21 -07001583 findNearestArea(c.x, c.y, c.spanX, c.spanY, direction, mTmpOccupied, null, mTempLocation);
Adam Cohen482ed822012-03-02 14:15:13 -08001584
1585 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001586 c.x = mTempLocation[0];
1587 c.y = mTempLocation[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001588 success = true;
1589
1590 }
Adam Cohen8baab352012-03-20 17:39:21 -07001591 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001592 return success;
1593 }
1594
Adam Cohen47a876d2012-03-19 13:21:41 -07001595 // This method looks in the specified direction to see if there is an additional view
1596 // immediately adjecent in that direction
1597 private boolean addViewInDirection(ArrayList<View> views, Rect boundingRect, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001598 boolean[][] occupied, ItemConfiguration currentState) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001599 boolean found = false;
1600
Michael Jurkaa52570f2012-03-20 03:18:20 -07001601 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen47a876d2012-03-19 13:21:41 -07001602 Rect r0 = new Rect(boundingRect);
1603 Rect r1 = new Rect();
1604
1605 int deltaX = 0;
1606 int deltaY = 0;
1607 if (direction[1] < 0) {
1608 r0.set(r0.left, r0.top - 1, r0.right, r0.bottom);
1609 deltaY = -1;
1610 } else if (direction[1] > 0) {
1611 r0.set(r0.left, r0.top, r0.right, r0.bottom + 1);
1612 deltaY = 1;
1613 } else if (direction[0] < 0) {
1614 r0.set(r0.left - 1, r0.top, r0.right, r0.bottom);
1615 deltaX = -1;
1616 } else if (direction[0] > 0) {
1617 r0.set(r0.left, r0.top, r0.right + 1, r0.bottom);
1618 deltaX = 1;
1619 }
1620
1621 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001622 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen47a876d2012-03-19 13:21:41 -07001623 if (views.contains(child)) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001624 CellAndSpan c = currentState.map.get(child);
Adam Cohen47a876d2012-03-19 13:21:41 -07001625
Adam Cohen8baab352012-03-20 17:39:21 -07001626 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1627 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001628 if (Rect.intersects(r0, r1)) {
1629 if (!lp.canReorder) {
1630 return false;
1631 }
1632 boolean pushed = false;
Adam Cohen8baab352012-03-20 17:39:21 -07001633 for (int x = c.x; x < c.x + c.spanX; x++) {
1634 for (int y = c.y; y < c.y + c.spanY; y++) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001635 boolean inBounds = x - deltaX >= 0 && x -deltaX < mCountX
1636 && y - deltaY >= 0 && y - deltaY < mCountY;
1637 if (inBounds && occupied[x - deltaX][y - deltaY]) {
1638 pushed = true;
1639 }
1640 }
1641 }
1642 if (pushed) {
1643 views.add(child);
Adam Cohen8baab352012-03-20 17:39:21 -07001644 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen47a876d2012-03-19 13:21:41 -07001645 found = true;
1646 }
1647 }
1648 }
1649 return found;
1650 }
1651
Adam Cohen482ed822012-03-02 14:15:13 -08001652 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
Adam Cohen8baab352012-03-20 17:39:21 -07001653 int[] direction, boolean push, ItemConfiguration currentState) {
Adam Cohen482ed822012-03-02 14:15:13 -08001654 if (views.size() == 0) return true;
Adam Cohen482ed822012-03-02 14:15:13 -08001655
Adam Cohen8baab352012-03-20 17:39:21 -07001656 boolean success = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001657 Rect boundingRect = null;
Adam Cohen8baab352012-03-20 17:39:21 -07001658 // We construct a rect which represents the entire group of views passed in
Adam Cohen482ed822012-03-02 14:15:13 -08001659 for (View v: views) {
Adam Cohen8baab352012-03-20 17:39:21 -07001660 CellAndSpan c = currentState.map.get(v);
Adam Cohen482ed822012-03-02 14:15:13 -08001661 if (boundingRect == null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001662 boundingRect = new Rect(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001663 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001664 boundingRect.union(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001665 }
1666 }
Adam Cohen8baab352012-03-20 17:39:21 -07001667
1668 @SuppressWarnings("unchecked")
1669 ArrayList<View> dup = (ArrayList<View>) views.clone();
1670 // We try and expand the group of views in the direction vector passed, based on
1671 // whether they are physically adjacent, ie. based on "push mechanics".
1672 while (push && addViewInDirection(dup, boundingRect, direction, mTmpOccupied,
1673 currentState)) {
1674 }
1675
1676 // Mark the occupied state as false for the group of views we want to move.
1677 for (View v: dup) {
1678 CellAndSpan c = currentState.map.get(v);
1679 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, false);
1680 }
1681
Adam Cohen47a876d2012-03-19 13:21:41 -07001682 boolean[][] blockOccupied = new boolean[boundingRect.width()][boundingRect.height()];
1683 int top = boundingRect.top;
1684 int left = boundingRect.left;
Adam Cohen8baab352012-03-20 17:39:21 -07001685 // We mark more precisely which parts of the bounding rect are truly occupied, allowing
1686 // for tetris-style interlocking.
1687 for (View v: dup) {
1688 CellAndSpan c = currentState.map.get(v);
1689 markCellsForView(c.x - left, c.y - top, c.spanX, c.spanY, blockOccupied, true);
Adam Cohen47a876d2012-03-19 13:21:41 -07001690 }
1691
Adam Cohen482ed822012-03-02 14:15:13 -08001692 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1693
Adam Cohen8baab352012-03-20 17:39:21 -07001694 if (push) {
1695 findNearestAreaInDirection(boundingRect.left, boundingRect.top, boundingRect.width(),
1696 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1697 } else {
1698 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(),
1699 boundingRect.height(), direction, mTmpOccupied, blockOccupied, mTempLocation);
1700 }
Adam Cohen482ed822012-03-02 14:15:13 -08001701
Adam Cohen8baab352012-03-20 17:39:21 -07001702 // If we successfuly found a location by pushing the block of views, we commit it
Adam Cohen482ed822012-03-02 14:15:13 -08001703 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
Adam Cohen8baab352012-03-20 17:39:21 -07001704 int deltaX = mTempLocation[0] - boundingRect.left;
1705 int deltaY = mTempLocation[1] - boundingRect.top;
1706 for (View v: dup) {
1707 CellAndSpan c = currentState.map.get(v);
1708 c.x += deltaX;
1709 c.y += deltaY;
Adam Cohen482ed822012-03-02 14:15:13 -08001710 }
1711 success = true;
1712 }
Adam Cohen8baab352012-03-20 17:39:21 -07001713
1714 // In either case, we set the occupied array as marked for the location of the views
1715 for (View v: dup) {
1716 CellAndSpan c = currentState.map.get(v);
1717 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001718 }
1719 return success;
1720 }
1721
1722 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1723 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1724 }
1725
1726 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
Adam Cohen8baab352012-03-20 17:39:21 -07001727 View ignoreView, ItemConfiguration solution) {
Adam Cohen482ed822012-03-02 14:15:13 -08001728
Adam Cohen8baab352012-03-20 17:39:21 -07001729 mIntersectingViews.clear();
Adam Cohen482ed822012-03-02 14:15:13 -08001730 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001731
Adam Cohen8baab352012-03-20 17:39:21 -07001732 // Mark the desired location of the view currently being dragged.
Adam Cohen482ed822012-03-02 14:15:13 -08001733 if (ignoreView != null) {
Adam Cohen8baab352012-03-20 17:39:21 -07001734 CellAndSpan c = solution.map.get(ignoreView);
1735 c.x = cellX;
1736 c.y = cellY;
Adam Cohen482ed822012-03-02 14:15:13 -08001737 }
1738
Adam Cohen8baab352012-03-20 17:39:21 -07001739 //int childCount = mChildren.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001740 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1741 Rect r1 = new Rect();
Adam Cohen8baab352012-03-20 17:39:21 -07001742 for (View child: solution.map.keySet()) {
Adam Cohen482ed822012-03-02 14:15:13 -08001743 if (child == ignoreView) continue;
Adam Cohen8baab352012-03-20 17:39:21 -07001744 CellAndSpan c = solution.map.get(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001745 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001746 r1.set(c.x, c.y, c.x + c.spanX, c.y + c.spanY);
Adam Cohen482ed822012-03-02 14:15:13 -08001747 if (Rect.intersects(r0, r1)) {
1748 if (!lp.canReorder) {
1749 return false;
1750 }
1751 mIntersectingViews.add(child);
1752 }
1753 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001754
Adam Cohen8baab352012-03-20 17:39:21 -07001755 // We try to move the intersecting views as a block using the push mechanic
1756 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001757 return true;
1758 }
1759 // Try the opposite direction
1760 direction[0] *= -1;
1761 direction[1] *= -1;
Adam Cohen8baab352012-03-20 17:39:21 -07001762 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, true, solution)) {
Adam Cohen47a876d2012-03-19 13:21:41 -07001763 return true;
1764 }
1765 // Switch the direction back
1766 direction[0] *= -1;
1767 direction[1] *= -1;
1768
Adam Cohen8baab352012-03-20 17:39:21 -07001769 // Next we try moving the views as a block , but without requiring the push mechanic
1770 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, false, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001771 return true;
1772 }
Adam Cohen47a876d2012-03-19 13:21:41 -07001773
Adam Cohen482ed822012-03-02 14:15:13 -08001774 // Ok, they couldn't move as a block, let's move them individually
1775 for (View v : mIntersectingViews) {
Adam Cohen8baab352012-03-20 17:39:21 -07001776 if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) {
Adam Cohen482ed822012-03-02 14:15:13 -08001777 return false;
1778 }
1779 }
1780 return true;
1781 }
1782
1783 /*
1784 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1785 * the provided point and the provided cell
1786 */
Adam Cohen47a876d2012-03-19 13:21:41 -07001787 private void computeDirectionVector(float deltaX, float deltaY, int[] result) {
Adam Cohen482ed822012-03-02 14:15:13 -08001788 double angle = Math.atan(((float) deltaY) / deltaX);
1789
1790 result[0] = 0;
1791 result[1] = 0;
1792 if (Math.abs(Math.cos(angle)) > 0.5f) {
1793 result[0] = (int) Math.signum(deltaX);
1794 }
1795 if (Math.abs(Math.sin(angle)) > 0.5f) {
1796 result[1] = (int) Math.signum(deltaY);
1797 }
1798 }
1799
Adam Cohen8baab352012-03-20 17:39:21 -07001800 private void copyOccupiedArray(boolean[][] occupied) {
1801 for (int i = 0; i < mCountX; i++) {
1802 for (int j = 0; j < mCountY; j++) {
1803 occupied[i][j] = mOccupied[i][j];
1804 }
1805 }
1806 }
1807
Adam Cohen482ed822012-03-02 14:15:13 -08001808 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1809 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
Adam Cohen8baab352012-03-20 17:39:21 -07001810 // Copy the current state into the solution. This solution will be manipulated as necessary.
1811 copyCurrentStateToSolution(solution, false);
1812 // Copy the current occupied array into the temporary occupied array. This array will be
1813 // manipulated as necessary to find a solution.
1814 copyOccupiedArray(mTmpOccupied);
Adam Cohen482ed822012-03-02 14:15:13 -08001815
1816 // We find the nearest cell into which we would place the dragged item, assuming there's
1817 // nothing in its way.
1818 int result[] = new int[2];
1819 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1820
1821 boolean success = false;
1822 // First we try the exact nearest position of the item being dragged,
1823 // we will then want to try to move this around to other neighbouring positions
Adam Cohen8baab352012-03-20 17:39:21 -07001824 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView,
1825 solution);
Adam Cohen482ed822012-03-02 14:15:13 -08001826
1827 if (!success) {
1828 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1829 // x, then 1 in y etc.
1830 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1831 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1832 dragView, false, solution);
1833 } else if (spanY > minSpanY) {
1834 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1835 dragView, true, solution);
1836 }
1837 solution.isSolution = false;
1838 } else {
1839 solution.isSolution = true;
1840 solution.dragViewX = result[0];
1841 solution.dragViewY = result[1];
1842 solution.dragViewSpanX = spanX;
1843 solution.dragViewSpanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08001844 }
1845 return solution;
1846 }
1847
1848 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001849 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001850 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001851 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001852 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001853 CellAndSpan c;
Adam Cohen482ed822012-03-02 14:15:13 -08001854 if (temp) {
Adam Cohen8baab352012-03-20 17:39:21 -07001855 c = new CellAndSpan(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001856 } else {
Adam Cohen8baab352012-03-20 17:39:21 -07001857 c = new CellAndSpan(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan);
Adam Cohen482ed822012-03-02 14:15:13 -08001858 }
Adam Cohen8baab352012-03-20 17:39:21 -07001859 solution.map.put(child, c);
Adam Cohen482ed822012-03-02 14:15:13 -08001860 }
1861 }
1862
1863 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1864 for (int i = 0; i < mCountX; i++) {
1865 for (int j = 0; j < mCountY; j++) {
1866 mTmpOccupied[i][j] = false;
1867 }
1868 }
1869
Michael Jurkaa52570f2012-03-20 03:18:20 -07001870 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001871 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001872 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001873 if (child == dragView) continue;
1874 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001875 CellAndSpan c = solution.map.get(child);
1876 if (c != null) {
1877 lp.tmpCellX = c.x;
1878 lp.tmpCellY = c.y;
1879 lp.cellHSpan = c.spanX;
1880 lp.cellVSpan = c.spanY;
1881 markCellsForView(c.x, c.y, c.spanX, c.spanY, mTmpOccupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001882 }
1883 }
1884 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1885 solution.dragViewSpanY, mTmpOccupied, true);
1886 }
1887
1888 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1889 commitDragView) {
1890
1891 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1892 for (int i = 0; i < mCountX; i++) {
1893 for (int j = 0; j < mCountY; j++) {
1894 occupied[i][j] = false;
1895 }
1896 }
1897
Michael Jurkaa52570f2012-03-20 03:18:20 -07001898 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001899 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001900 View child = mShortcutsAndWidgets.getChildAt(i);
Adam Cohen482ed822012-03-02 14:15:13 -08001901 if (child == dragView) continue;
1902 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Adam Cohen8baab352012-03-20 17:39:21 -07001903 CellAndSpan c = solution.map.get(child);
1904 if (c != null) {
1905 if (lp.cellX != c.x || lp.cellY != c.y) {
1906 animateChildToPosition(child, c.x, c.y, 150, 0, DESTRUCTIVE_REORDER, false);
Adam Cohen482ed822012-03-02 14:15:13 -08001907 }
Adam Cohen8baab352012-03-20 17:39:21 -07001908 markCellsForView(c.x, c.y, c.spanX, c.spanY, occupied, true);
Adam Cohen482ed822012-03-02 14:15:13 -08001909 }
1910 }
1911 if (commitDragView) {
1912 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1913 solution.dragViewSpanY, occupied, true);
1914 }
1915 }
1916
1917 private void commitTempPlacement() {
1918 for (int i = 0; i < mCountX; i++) {
1919 for (int j = 0; j < mCountY; j++) {
1920 mOccupied[i][j] = mTmpOccupied[i][j];
1921 }
1922 }
Michael Jurkaa52570f2012-03-20 03:18:20 -07001923 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001924 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001925 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08001926 lp.cellX = lp.tmpCellX;
1927 lp.cellY = lp.tmpCellY;
1928 }
1929 }
1930
1931 public void setUseTempCoords(boolean useTempCoords) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001932 int childCount = mShortcutsAndWidgets.getChildCount();
Adam Cohen482ed822012-03-02 14:15:13 -08001933 for (int i = 0; i < childCount; i++) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07001934 LayoutParams lp = (LayoutParams) mShortcutsAndWidgets.getChildAt(i).getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08001935 lp.useTmpCoords = useTempCoords;
1936 }
1937 }
1938
Adam Cohen482ed822012-03-02 14:15:13 -08001939 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
1940 int spanX, int spanY, View dragView, ItemConfiguration solution) {
1941 int[] result = new int[2];
1942 int[] resultSpan = new int[2];
1943 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
1944 resultSpan);
1945 if (result[0] >= 0 && result[1] >= 0) {
1946 copyCurrentStateToSolution(solution, false);
1947 solution.dragViewX = result[0];
1948 solution.dragViewY = result[1];
1949 solution.dragViewSpanX = resultSpan[0];
1950 solution.dragViewSpanY = resultSpan[1];
1951 solution.isSolution = true;
1952 } else {
1953 solution.isSolution = false;
1954 }
1955 return solution;
1956 }
1957
1958 public void prepareChildForDrag(View child) {
1959 markCellsAsUnoccupiedForView(child);
Adam Cohen482ed822012-03-02 14:15:13 -08001960 }
1961
1962 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
1963 View dragView, int[] result, int resultSpan[], int mode) {
1964
1965 // First we determine if things have moved enough to cause a different layout
Adam Cohen47a876d2012-03-19 13:21:41 -07001966 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
Adam Cohen482ed822012-03-02 14:15:13 -08001967
1968 if (resultSpan == null) {
1969 resultSpan = new int[2];
1970 }
1971
1972 // We attempt the first algorithm
Adam Cohen47a876d2012-03-19 13:21:41 -07001973 regionToCenterPoint(result[0], result[1], spanX, spanY, mTmpPoint);
1974 computeDirectionVector((mTmpPoint[0] - pixelX) / spanX, (mTmpPoint[1] - pixelY) / spanY,
1975 mDirectionVector);
Adam Cohen482ed822012-03-02 14:15:13 -08001976 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
1977 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
1978
1979 // We attempt the approach which doesn't shuffle views at all
1980 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
1981 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
1982
1983 ItemConfiguration finalSolution = null;
1984 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
1985 finalSolution = swapSolution;
1986 } else if (noShuffleSolution.isSolution) {
1987 finalSolution = noShuffleSolution;
1988 }
1989
1990 boolean foundSolution = true;
1991 if (!DESTRUCTIVE_REORDER) {
1992 setUseTempCoords(true);
1993 }
1994
1995 if (finalSolution != null) {
1996 result[0] = finalSolution.dragViewX;
1997 result[1] = finalSolution.dragViewY;
1998 resultSpan[0] = finalSolution.dragViewSpanX;
1999 resultSpan[1] = finalSolution.dragViewSpanY;
2000
2001 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
2002 // committing anything or animating anything as we just want to determine if a solution
2003 // exists
2004 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
2005 if (!DESTRUCTIVE_REORDER) {
2006 copySolutionToTempState(finalSolution, dragView);
2007 }
2008 setItemPlacementDirty(true);
2009 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
2010
2011 if (!DESTRUCTIVE_REORDER && mode == MODE_ON_DROP) {
2012 commitTempPlacement();
2013 }
2014 }
2015 } else {
2016 foundSolution = false;
2017 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
2018 }
2019
2020 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
2021 setUseTempCoords(false);
2022 }
Adam Cohen482ed822012-03-02 14:15:13 -08002023
Michael Jurkaa52570f2012-03-20 03:18:20 -07002024 mShortcutsAndWidgets.requestLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08002025 return result;
2026 }
2027
2028 public boolean isItemPlacementDirty() {
2029 return mItemLocationsDirty;
2030 }
2031
2032 public void setItemPlacementDirty(boolean dirty) {
2033 mItemLocationsDirty = dirty;
2034 }
2035
2036 private class ItemConfiguration {
Adam Cohen8baab352012-03-20 17:39:21 -07002037 HashMap<View, CellAndSpan> map = new HashMap<View, CellAndSpan>();
Adam Cohen482ed822012-03-02 14:15:13 -08002038 boolean isSolution = false;
2039 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
2040
2041 int area() {
2042 return dragViewSpanX * dragViewSpanY;
2043 }
Adam Cohen8baab352012-03-20 17:39:21 -07002044 }
2045
2046 private class CellAndSpan {
2047 int x, y;
2048 int spanX, spanY;
2049
2050 public CellAndSpan(int x, int y, int spanX, int spanY) {
2051 this.x = x;
2052 this.y = y;
2053 this.spanX = spanX;
2054 this.spanY = spanY;
Adam Cohen482ed822012-03-02 14:15:13 -08002055 }
2056 }
2057
Adam Cohendf035382011-04-11 17:22:04 -07002058 /**
2059 * Find a vacant area that will fit the given bounds nearest the requested
2060 * cell location. Uses Euclidean distance to score multiple vacant areas.
2061 *
2062 * @param pixelX The X location at which you want to search for a vacant area.
2063 * @param pixelY The Y location at which you want to search for a vacant area.
2064 * @param spanX Horizontal span of the object.
2065 * @param spanY Vertical span of the object.
2066 * @param ignoreView Considers space occupied by this view as unoccupied
2067 * @param result Previously returned value to possibly recycle.
2068 * @return The X, Y cell of a vacant area that can contain this object,
2069 * nearest the requested location.
2070 */
2071 int[] findNearestVacantArea(
2072 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
2073 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
2074 }
2075
2076 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08002077 * Find a vacant area that will fit the given bounds nearest the requested
2078 * cell location. Uses Euclidean distance to score multiple vacant areas.
2079 *
2080 * @param pixelX The X location at which you want to search for a vacant area.
2081 * @param pixelY The Y location at which you want to search for a vacant area.
2082 * @param minSpanX The minimum horizontal span required
2083 * @param minSpanY The minimum vertical span required
2084 * @param spanX Horizontal span of the object.
2085 * @param spanY Vertical span of the object.
2086 * @param ignoreView Considers space occupied by this view as unoccupied
2087 * @param result Previously returned value to possibly recycle.
2088 * @return The X, Y cell of a vacant area that can contain this object,
2089 * nearest the requested location.
2090 */
2091 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
2092 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08002093 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
2094 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08002095 }
2096
2097 /**
Adam Cohendf035382011-04-11 17:22:04 -07002098 * Find a starting cell position that will fit the given bounds nearest the requested
2099 * cell location. Uses Euclidean distance to score multiple vacant areas.
2100 *
2101 * @param pixelX The X location at which you want to search for a vacant area.
2102 * @param pixelY The Y location at which you want to search for a vacant area.
2103 * @param spanX Horizontal span of the object.
2104 * @param spanY Vertical span of the object.
2105 * @param ignoreView Considers space occupied by this view as unoccupied
2106 * @param result Previously returned value to possibly recycle.
2107 * @return The X, Y cell of a vacant area that can contain this object,
2108 * nearest the requested location.
2109 */
2110 int[] findNearestArea(
2111 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
2112 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
2113 }
2114
Michael Jurka0280c3b2010-09-17 15:00:07 -07002115 boolean existsEmptyCell() {
2116 return findCellForSpan(null, 1, 1);
2117 }
2118
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002120 * Finds the upper-left coordinate of the first rectangle in the grid that can
2121 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
2122 * then this method will only return coordinates for rectangles that contain the cell
2123 * (intersectX, intersectY)
2124 *
2125 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2126 * can be found.
2127 * @param spanX The horizontal span of the cell we want to find.
2128 * @param spanY The vertical span of the cell we want to find.
2129 *
2130 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002131 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07002132 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08002133 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002134 }
2135
2136 /**
2137 * Like above, but ignores any cells occupied by the item "ignoreView"
2138 *
2139 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2140 * can be found.
2141 * @param spanX The horizontal span of the cell we want to find.
2142 * @param spanY The vertical span of the cell we want to find.
2143 * @param ignoreView The home screen item we should treat as not occupying any space
2144 * @return
2145 */
2146 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002147 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2148 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002149 }
2150
2151 /**
2152 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2153 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2154 *
2155 * @param spanX The horizontal span of the cell we want to find.
2156 * @param spanY The vertical span of the cell we want to find.
2157 * @param ignoreView The home screen item we should treat as not occupying any space
2158 * @param intersectX The X coordinate of the cell that we should try to overlap
2159 * @param intersectX The Y coordinate of the cell that we should try to overlap
2160 *
2161 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2162 */
2163 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2164 int intersectX, int intersectY) {
2165 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002166 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002167 }
2168
2169 /**
2170 * The superset of the above two methods
2171 */
2172 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002173 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002174 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002175 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002176
Michael Jurka28750fb2010-09-24 17:43:49 -07002177 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002178 while (true) {
2179 int startX = 0;
2180 if (intersectX >= 0) {
2181 startX = Math.max(startX, intersectX - (spanX - 1));
2182 }
2183 int endX = mCountX - (spanX - 1);
2184 if (intersectX >= 0) {
2185 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2186 }
2187 int startY = 0;
2188 if (intersectY >= 0) {
2189 startY = Math.max(startY, intersectY - (spanY - 1));
2190 }
2191 int endY = mCountY - (spanY - 1);
2192 if (intersectY >= 0) {
2193 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2194 }
2195
Winson Chungbbc60d82010-11-11 16:34:41 -08002196 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002197 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002198 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002199 for (int i = 0; i < spanX; i++) {
2200 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002201 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002202 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002203 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002204 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002205 continue inner;
2206 }
2207 }
2208 }
2209 if (cellXY != null) {
2210 cellXY[0] = x;
2211 cellXY[1] = y;
2212 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002213 foundCell = true;
2214 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002215 }
2216 }
2217 if (intersectX == -1 && intersectY == -1) {
2218 break;
2219 } else {
2220 // if we failed to find anything, try again but without any requirements of
2221 // intersecting
2222 intersectX = -1;
2223 intersectY = -1;
2224 continue;
2225 }
2226 }
2227
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002228 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002229 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002230 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002231 }
2232
2233 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002234 * A drag event has begun over this layout.
2235 * It may have begun over this layout (in which case onDragChild is called first),
2236 * or it may have begun on another layout.
2237 */
2238 void onDragEnter() {
2239 if (!mDragging) {
2240 // Fade in the drag indicators
2241 if (mCrosshairsAnimator != null) {
2242 mCrosshairsAnimator.animateIn();
2243 }
2244 }
2245 mDragging = true;
2246 }
2247
2248 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002249 * Called when drag has left this CellLayout or has been completed (successfully or not)
2250 */
2251 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002252 // This can actually be called when we aren't in a drag, e.g. when adding a new
2253 // item to this layout via the customize drawer.
2254 // Guard against that case.
2255 if (mDragging) {
2256 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002257
Joe Onorato4be866d2010-10-10 11:26:02 -07002258 // Fade out the drag indicators
2259 if (mCrosshairsAnimator != null) {
2260 mCrosshairsAnimator.animateOut();
2261 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002262 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002263
2264 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002265 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002266 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2267 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
2268
Michael Jurka33945b22010-12-21 18:19:38 -08002269 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002270 }
2271
2272 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002273 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002274 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002275 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276 *
2277 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002279 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002280 if (child != null) {
2281 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002282 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002283 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002284 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 }
2286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002289 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 * @param cellX X coordinate of upper left corner expressed as a cell position
2291 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002292 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002294 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002296 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 final int cellWidth = mCellWidth;
2298 final int cellHeight = mCellHeight;
2299 final int widthGap = mWidthGap;
2300 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002301
Winson Chung4b825dcd2011-06-19 12:41:22 -07002302 final int hStartPadding = getPaddingLeft();
2303 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002304
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2306 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2307
2308 int x = hStartPadding + cellX * (cellWidth + widthGap);
2309 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002310
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002311 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002313
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002314 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002315 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002317 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002318 * @param width Width in pixels
2319 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002320 * @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 -08002321 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002322 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002323 return rectToCell(getResources(), width, height, result);
2324 }
2325
2326 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 // Always assume we're working with the smallest span to make sure we
2328 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002329 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2330 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002331 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002334 int spanX = (int) Math.ceil(width / (float) smallerSize);
2335 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002336
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002337 if (result == null) {
2338 return new int[] { spanX, spanY };
2339 }
2340 result[0] = spanX;
2341 result[1] = spanY;
2342 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002343 }
2344
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002345 public int[] cellSpansToSize(int hSpans, int vSpans) {
2346 int[] size = new int[2];
2347 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2348 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2349 return size;
2350 }
2351
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002353 * Calculate the grid spans needed to fit given item
2354 */
2355 public void calculateSpans(ItemInfo info) {
2356 final int minWidth;
2357 final int minHeight;
2358
2359 if (info instanceof LauncherAppWidgetInfo) {
2360 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2361 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2362 } else if (info instanceof PendingAddWidgetInfo) {
2363 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2364 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2365 } else {
2366 // It's not a widget, so it must be 1x1
2367 info.spanX = info.spanY = 1;
2368 return;
2369 }
2370 int[] spans = rectToCell(minWidth, minHeight, null);
2371 info.spanX = spans[0];
2372 info.spanY = spans[1];
2373 }
2374
2375 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 * Find the first vacant cell, if there is one.
2377 *
2378 * @param vacant Holds the x and y coordinate of the vacant cell
2379 * @param spanX Horizontal cell span.
2380 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002381 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 * @return True if a vacant cell was found
2383 */
2384 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385
Michael Jurka0280c3b2010-09-17 15:00:07 -07002386 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002387 }
2388
2389 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2390 int xCount, int yCount, boolean[][] occupied) {
2391
Adam Cohen2801caf2011-05-13 20:57:39 -07002392 for (int y = 0; y < yCount; y++) {
2393 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 boolean available = !occupied[x][y];
2395out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2396 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2397 available = available && !occupied[i][j];
2398 if (!available) break out;
2399 }
2400 }
2401
2402 if (available) {
2403 vacant[0] = x;
2404 vacant[1] = y;
2405 return true;
2406 }
2407 }
2408 }
2409
2410 return false;
2411 }
2412
Michael Jurka0280c3b2010-09-17 15:00:07 -07002413 private void clearOccupiedCells() {
2414 for (int x = 0; x < mCountX; x++) {
2415 for (int y = 0; y < mCountY; y++) {
2416 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 }
2418 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002419 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420
Adam Cohen1b607ed2011-03-03 17:26:50 -08002421 /**
2422 * Given a view, determines how much that view can be expanded in all directions, in terms of
2423 * whether or not there are other items occupying adjacent cells. Used by the
2424 * AppWidgetResizeFrame to determine how the widget can be resized.
2425 */
Adam Cohend4844c32011-02-18 19:25:06 -08002426 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08002427 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08002428 boolean flag;
2429
Adam Cohen1b607ed2011-03-03 17:26:50 -08002430 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002431 for (int x = lp.cellX - 1; x >= 0; x--) {
2432 flag = false;
2433 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
2434 if (mOccupied[x][y]) flag = true;
2435 }
2436 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002437 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08002438 }
2439
Adam Cohen1b607ed2011-03-03 17:26:50 -08002440 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002441 for (int y = lp.cellY - 1; y >= 0; y--) {
2442 flag = false;
2443 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
2444 if (mOccupied[x][y]) flag = true;
2445 }
2446 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002447 expandability[AppWidgetResizeFrame.TOP]++;
2448 }
Adam Cohend4844c32011-02-18 19:25:06 -08002449
Adam Cohen1b607ed2011-03-03 17:26:50 -08002450 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002451 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
2452 flag = false;
2453 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
2454 if (mOccupied[x][y]) flag = true;
2455 }
2456 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002457 expandability[AppWidgetResizeFrame.RIGHT]++;
2458 }
Adam Cohend4844c32011-02-18 19:25:06 -08002459
Adam Cohen1b607ed2011-03-03 17:26:50 -08002460 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002461 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
2462 flag = false;
2463 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
2464 if (mOccupied[x][y]) flag = true;
2465 }
2466 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002467 expandability[AppWidgetResizeFrame.BOTTOM]++;
2468 }
Adam Cohend4844c32011-02-18 19:25:06 -08002469 }
2470
Adam Cohend41fbf52012-02-16 23:53:59 -08002471 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002472 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002473 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002474 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475
Adam Cohend4844c32011-02-18 19:25:06 -08002476 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002477 markCellsAsOccupiedForView(view, mOccupied);
2478 }
2479 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002480 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002481 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002482 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002483 }
2484
Adam Cohend4844c32011-02-18 19:25:06 -08002485 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002486 markCellsAsUnoccupiedForView(view, mOccupied);
2487 }
2488 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurkaa52570f2012-03-20 03:18:20 -07002489 if (view == null || view.getParent() != mShortcutsAndWidgets) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002490 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002491 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002492 }
2493
Adam Cohen482ed822012-03-02 14:15:13 -08002494 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2495 boolean value) {
2496 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002497 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2498 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002499 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 }
2501 }
2502 }
2503
Adam Cohen2801caf2011-05-13 20:57:39 -07002504 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002505 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002506 (Math.max((mCountX - 1), 0) * mWidthGap);
2507 }
2508
2509 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002510 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002511 (Math.max((mCountY - 1), 0) * mHeightGap);
2512 }
2513
Michael Jurka66d72172011-04-12 16:29:25 -07002514 public boolean isOccupied(int x, int y) {
2515 if (x < mCountX && y < mCountY) {
2516 return mOccupied[x][y];
2517 } else {
2518 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2519 }
2520 }
2521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 @Override
2523 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2524 return new CellLayout.LayoutParams(getContext(), attrs);
2525 }
2526
2527 @Override
2528 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2529 return p instanceof CellLayout.LayoutParams;
2530 }
2531
2532 @Override
2533 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2534 return new CellLayout.LayoutParams(p);
2535 }
2536
Winson Chungaafa03c2010-06-11 17:34:16 -07002537 public static class CellLayoutAnimationController extends LayoutAnimationController {
2538 public CellLayoutAnimationController(Animation animation, float delay) {
2539 super(animation, delay);
2540 }
2541
2542 @Override
2543 protected long getDelayForView(View view) {
2544 return (int) (Math.random() * 150);
2545 }
2546 }
2547
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2549 /**
2550 * Horizontal location of the item in the grid.
2551 */
2552 @ViewDebug.ExportedProperty
2553 public int cellX;
2554
2555 /**
2556 * Vertical location of the item in the grid.
2557 */
2558 @ViewDebug.ExportedProperty
2559 public int cellY;
2560
2561 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002562 * Temporary horizontal location of the item in the grid during reorder
2563 */
2564 public int tmpCellX;
2565
2566 /**
2567 * Temporary vertical location of the item in the grid during reorder
2568 */
2569 public int tmpCellY;
2570
2571 /**
2572 * Indicates that the temporary coordinates should be used to layout the items
2573 */
2574 public boolean useTmpCoords;
2575
2576 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 * Number of cells spanned horizontally by the item.
2578 */
2579 @ViewDebug.ExportedProperty
2580 public int cellHSpan;
2581
2582 /**
2583 * Number of cells spanned vertically by the item.
2584 */
2585 @ViewDebug.ExportedProperty
2586 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002587
Adam Cohen1b607ed2011-03-03 17:26:50 -08002588 /**
2589 * Indicates whether the item will set its x, y, width and height parameters freely,
2590 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2591 */
Adam Cohend4844c32011-02-18 19:25:06 -08002592 public boolean isLockedToGrid = true;
2593
Adam Cohen482ed822012-03-02 14:15:13 -08002594 /**
2595 * Indicates whether this item can be reordered. Always true except in the case of the
2596 * the AllApps button.
2597 */
2598 public boolean canReorder = true;
2599
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002600 // X coordinate of the view in the layout.
2601 @ViewDebug.ExportedProperty
2602 int x;
2603 // Y coordinate of the view in the layout.
2604 @ViewDebug.ExportedProperty
2605 int y;
2606
Romain Guy84f296c2009-11-04 15:00:44 -08002607 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002608
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002609 public LayoutParams(Context c, AttributeSet attrs) {
2610 super(c, attrs);
2611 cellHSpan = 1;
2612 cellVSpan = 1;
2613 }
2614
2615 public LayoutParams(ViewGroup.LayoutParams source) {
2616 super(source);
2617 cellHSpan = 1;
2618 cellVSpan = 1;
2619 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002620
2621 public LayoutParams(LayoutParams source) {
2622 super(source);
2623 this.cellX = source.cellX;
2624 this.cellY = source.cellY;
2625 this.cellHSpan = source.cellHSpan;
2626 this.cellVSpan = source.cellVSpan;
2627 }
2628
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002629 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002630 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002631 this.cellX = cellX;
2632 this.cellY = cellY;
2633 this.cellHSpan = cellHSpan;
2634 this.cellVSpan = cellVSpan;
2635 }
2636
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002637 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002638 if (isLockedToGrid) {
2639 final int myCellHSpan = cellHSpan;
2640 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002641 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2642 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002643
Adam Cohend4844c32011-02-18 19:25:06 -08002644 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2645 leftMargin - rightMargin;
2646 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2647 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002648 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2649 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002650 }
2651 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002652
Winson Chungaafa03c2010-06-11 17:34:16 -07002653 public String toString() {
2654 return "(" + this.cellX + ", " + this.cellY + ")";
2655 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002656
2657 public void setWidth(int width) {
2658 this.width = width;
2659 }
2660
2661 public int getWidth() {
2662 return width;
2663 }
2664
2665 public void setHeight(int height) {
2666 this.height = height;
2667 }
2668
2669 public int getHeight() {
2670 return height;
2671 }
2672
2673 public void setX(int x) {
2674 this.x = x;
2675 }
2676
2677 public int getX() {
2678 return x;
2679 }
2680
2681 public void setY(int y) {
2682 this.y = y;
2683 }
2684
2685 public int getY() {
2686 return y;
2687 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002688 }
2689
Michael Jurka0280c3b2010-09-17 15:00:07 -07002690 // This class stores info for two purposes:
2691 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2692 // its spanX, spanY, and the screen it is on
2693 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2694 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2695 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002696 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002698 int cellX = -1;
2699 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002700 int spanX;
2701 int spanY;
2702 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002703 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002704
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002705 @Override
2706 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002707 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2708 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002709 }
2710 }
Michael Jurkad771c962011-08-09 15:00:48 -07002711
2712 public boolean lastDownOnOccupiedCell() {
2713 return mLastDownOnOccupiedCell;
2714 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002715}