blob: d30940e7d553e636efee540535622b38dd028e1e [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 Jurka8c920dd2011-01-20 14:16:56 -0800140 private CellLayoutChildren mChildren;
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 Jurka8c920dd2011-01-20 14:16:56 -0800284 mChildren = new CellLayoutChildren(context);
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700285 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800286 addView(mChildren);
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() {
Adam Cohen7ef91832011-08-25 14:06:02 -0700312 mChildren.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
Winson Chung6e314082011-01-27 16:46:51 -0800356 public CellLayoutChildren getChildrenLayout() {
357 if (getChildCount() > 0) {
358 return (CellLayoutChildren) getChildAt(0);
359 }
360 return null;
361 }
362
Michael Jurka33945b22010-12-21 18:19:38 -0800363 void setIsDragOverlapping(boolean isDragOverlapping) {
364 if (mIsDragOverlapping != isDragOverlapping) {
365 mIsDragOverlapping = isDragOverlapping;
366 invalidate();
367 }
368 }
369
370 boolean getIsDragOverlapping() {
371 return mIsDragOverlapping;
372 }
373
Adam Cohenebea84d2011-11-09 17:20:41 -0800374 protected void setOverscrollTransformsDirty(boolean dirty) {
375 mScrollingTransformsDirty = dirty;
376 }
377
378 protected void resetOverscrollTransforms() {
379 if (mScrollingTransformsDirty) {
380 setOverscrollTransformsDirty(false);
381 setTranslationX(0);
382 setRotationY(0);
383 // It doesn't matter if we pass true or false here, the important thing is that we
384 // pass 0, which results in the overscroll drawable not being drawn any more.
385 setOverScrollAmount(0, false);
386 setPivotX(getMeasuredWidth() / 2);
387 setPivotY(getMeasuredHeight() / 2);
388 }
389 }
390
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700391 @Override
Patrick Dubroy1262e362010-10-06 15:49:50 -0700392 protected void onDraw(Canvas canvas) {
Michael Jurka3e7c7632010-10-02 16:01:03 -0700393 // When we're large, we are either drawn in a "hover" state (ie when dragging an item to
394 // a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
395 // When we're small, we are either drawn normally or in the "accepts drops" state (during
396 // a drag). However, we also drag the mini hover background *over* one of those two
397 // backgrounds
Winson Chungb26f3d62011-06-02 10:49:29 -0700398 if (mBackgroundAlpha > 0.0f) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700399 Drawable bg;
Michael Jurka33945b22010-12-21 18:19:38 -0800400
401 if (mIsDragOverlapping) {
402 // In the mini case, we draw the active_glow bg *over* the active background
Michael Jurkabdf78552011-10-31 14:34:25 -0700403 bg = mActiveGlowBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700404 } else {
Michael Jurkabdf78552011-10-31 14:34:25 -0700405 bg = mNormalBackground;
Adam Cohenf34bab52010-09-30 14:11:56 -0700406 }
Michael Jurka33945b22010-12-21 18:19:38 -0800407
408 bg.setAlpha((int) (mBackgroundAlpha * mBackgroundAlphaMultiplier * 255));
409 bg.setBounds(mBackgroundRect);
410 bg.draw(canvas);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700411 }
Romain Guya6abce82009-11-10 02:54:41 -0800412
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700413 if (mCrosshairsVisibility > 0.0f) {
414 final int countX = mCountX;
415 final int countY = mCountY;
416
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700417 final float MAX_ALPHA = 0.4f;
418 final int MAX_VISIBLE_DISTANCE = 600;
419 final float DISTANCE_MULTIPLIER = 0.002f;
420
421 final Drawable d = mCrosshairsDrawable;
422 final int width = d.getIntrinsicWidth();
423 final int height = d.getIntrinsicHeight();
424
Winson Chung4b825dcd2011-06-19 12:41:22 -0700425 int x = getPaddingLeft() - (mWidthGap / 2) - (width / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700426 for (int col = 0; col <= countX; col++) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700427 int y = getPaddingTop() - (mHeightGap / 2) - (height / 2);
Patrick Dubroyde7658b2010-09-27 11:15:43 -0700428 for (int row = 0; row <= countY; row++) {
429 mTmpPointF.set(x - mDragCenter.x, y - mDragCenter.y);
430 float dist = mTmpPointF.length();
431 // Crosshairs further from the drag point are more faint
432 float alpha = Math.min(MAX_ALPHA,
433 DISTANCE_MULTIPLIER * (MAX_VISIBLE_DISTANCE - dist));
434 if (alpha > 0.0f) {
435 d.setBounds(x, y, x + width, y + height);
436 d.setAlpha((int) (alpha * 255 * mCrosshairsVisibility));
437 d.draw(canvas);
438 }
439 y += mCellHeight + mHeightGap;
440 }
441 x += mCellWidth + mWidthGap;
442 }
Joe Onorato4be866d2010-10-10 11:26:02 -0700443 }
Winson Chung150fbab2010-09-29 17:14:26 -0700444
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700445 final Paint paint = mDragOutlinePaint;
Joe Onorato4be866d2010-10-10 11:26:02 -0700446 for (int i = 0; i < mDragOutlines.length; i++) {
Chet Haase472b2812010-10-14 07:02:04 -0700447 final float alpha = mDragOutlineAlphas[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700448 if (alpha > 0) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800449 final Rect r = mDragOutlines[i];
Joe Onorato4be866d2010-10-10 11:26:02 -0700450 final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
Chet Haase472b2812010-10-14 07:02:04 -0700451 paint.setAlpha((int)(alpha + .5f));
Adam Cohend41fbf52012-02-16 23:53:59 -0800452 canvas.drawBitmap(b, null, r, paint);
Winson Chung150fbab2010-09-29 17:14:26 -0700453 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700454 }
Patrick Dubroy96864c32011-03-10 17:17:23 -0800455
456 // We draw the pressed or focused BubbleTextView's background in CellLayout because it
457 // requires an expanded clip rect (due to the glow's blur radius)
458 if (mPressedOrFocusedIcon != null) {
459 final int padding = mPressedOrFocusedIcon.getPressedOrFocusedBackgroundPadding();
460 final Bitmap b = mPressedOrFocusedIcon.getPressedOrFocusedBackground();
461 if (b != null) {
462 canvas.drawBitmap(b,
Winson Chung4b825dcd2011-06-19 12:41:22 -0700463 mPressedOrFocusedIcon.getLeft() + getPaddingLeft() - padding,
464 mPressedOrFocusedIcon.getTop() + getPaddingTop() - padding,
Patrick Dubroy96864c32011-03-10 17:17:23 -0800465 null);
466 }
467 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700468
Adam Cohen482ed822012-03-02 14:15:13 -0800469 if (DEBUG_VISUALIZE_OCCUPIED) {
470 int[] pt = new int[2];
471 ColorDrawable cd = new ColorDrawable(Color.RED);
472 cd.setBounds(0, 0, 80, 80);
473 for (int i = 0; i < mCountX; i++) {
474 for (int j = 0; j < mCountY; j++) {
475 if (mOccupied[i][j]) {
476 cellToPoint(i, j, pt);
477 canvas.save();
478 canvas.translate(pt[0], pt[1]);
479 cd.draw(canvas);
480 canvas.restore();
481 }
482 }
483 }
484 }
485
Adam Cohen69ce2e52011-07-03 19:25:21 -0700486 // The folder outer / inner ring image(s)
487 for (int i = 0; i < mFolderOuterRings.size(); i++) {
488 FolderRingAnimator fra = mFolderOuterRings.get(i);
489
490 // Draw outer ring
491 Drawable d = FolderRingAnimator.sSharedOuterRingDrawable;
492 int width = (int) fra.getOuterRingSize();
493 int height = width;
494 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
495
496 int centerX = mTempLocation[0] + mCellWidth / 2;
497 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
498
499 canvas.save();
500 canvas.translate(centerX - width / 2, centerY - height / 2);
501 d.setBounds(0, 0, width, height);
502 d.draw(canvas);
503 canvas.restore();
504
505 // Draw inner ring
506 d = FolderRingAnimator.sSharedInnerRingDrawable;
507 width = (int) fra.getInnerRingSize();
508 height = width;
509 cellToPoint(fra.mCellX, fra.mCellY, mTempLocation);
510
511 centerX = mTempLocation[0] + mCellWidth / 2;
512 centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
513 canvas.save();
514 canvas.translate(centerX - width / 2, centerY - width / 2);
515 d.setBounds(0, 0, width, height);
516 d.draw(canvas);
517 canvas.restore();
518 }
Adam Cohenc51934b2011-07-26 21:07:43 -0700519
520 if (mFolderLeaveBehindCell[0] >= 0 && mFolderLeaveBehindCell[1] >= 0) {
521 Drawable d = FolderIcon.sSharedFolderLeaveBehind;
522 int width = d.getIntrinsicWidth();
523 int height = d.getIntrinsicHeight();
524
525 cellToPoint(mFolderLeaveBehindCell[0], mFolderLeaveBehindCell[1], mTempLocation);
526 int centerX = mTempLocation[0] + mCellWidth / 2;
527 int centerY = mTempLocation[1] + FolderRingAnimator.sPreviewSize / 2;
528
529 canvas.save();
530 canvas.translate(centerX - width / 2, centerY - width / 2);
531 d.setBounds(0, 0, width, height);
532 d.draw(canvas);
533 canvas.restore();
534 }
Adam Cohen69ce2e52011-07-03 19:25:21 -0700535 }
536
Adam Cohenb5ba0972011-09-07 18:02:31 -0700537 @Override
538 protected void dispatchDraw(Canvas canvas) {
539 super.dispatchDraw(canvas);
540 if (mForegroundAlpha > 0) {
541 mOverScrollForegroundDrawable.setBounds(mForegroundRect);
542 Paint p = ((NinePatchDrawable) mOverScrollForegroundDrawable).getPaint();
543 p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
544 mOverScrollForegroundDrawable.draw(canvas);
545 p.setXfermode(null);
546 }
547 }
548
Adam Cohen69ce2e52011-07-03 19:25:21 -0700549 public void showFolderAccept(FolderRingAnimator fra) {
550 mFolderOuterRings.add(fra);
551 }
552
553 public void hideFolderAccept(FolderRingAnimator fra) {
554 if (mFolderOuterRings.contains(fra)) {
555 mFolderOuterRings.remove(fra);
556 }
557 invalidate();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700558 }
559
Adam Cohenc51934b2011-07-26 21:07:43 -0700560 public void setFolderLeaveBehindCell(int x, int y) {
561 mFolderLeaveBehindCell[0] = x;
562 mFolderLeaveBehindCell[1] = y;
563 invalidate();
564 }
565
566 public void clearFolderLeaveBehind() {
567 mFolderLeaveBehindCell[0] = -1;
568 mFolderLeaveBehindCell[1] = -1;
569 invalidate();
570 }
571
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700572 @Override
Michael Jurkae6235dd2011-10-04 15:02:05 -0700573 public boolean shouldDelayChildPressedState() {
574 return false;
575 }
576
577 @Override
Jeff Sharkey83f111d2009-04-20 21:03:13 -0700578 public void cancelLongPress() {
579 super.cancelLongPress();
580
581 // Cancel long press for all children
582 final int count = getChildCount();
583 for (int i = 0; i < count; i++) {
584 final View child = getChildAt(i);
585 child.cancelLongPress();
586 }
587 }
588
Michael Jurkadee05892010-07-27 10:01:56 -0700589 public void setOnInterceptTouchListener(View.OnTouchListener listener) {
590 mInterceptTouchListener = listener;
591 }
592
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 int getCountX() {
Adam Cohend22015c2010-07-26 22:02:18 -0700594 return mCountX;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 }
596
597 int getCountY() {
Adam Cohend22015c2010-07-26 22:02:18 -0700598 return mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
600
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800601 public void setIsHotseat(boolean isHotseat) {
602 mIsHotseat = isHotseat;
603 }
604
Winson Chungeecf02d2012-03-02 17:14:58 -0800605 public float getChildrenScale() {
606 return mIsHotseat ? mHotseatChildScale : mChildScale;
607 }
608
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700609 public boolean addViewToCellLayout(
610 View child, int index, int childId, LayoutParams params, boolean markCells) {
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800611 return addViewToCellLayout(child, index, childId, params, markCells, false);
612 }
613
Winson Chungeecf02d2012-03-02 17:14:58 -0800614 private void scaleChild(BubbleTextView bubbleChild, float pivot, float scale) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800615 // If we haven't measured the child yet, do it now
616 // (this happens if we're being dropped from all-apps
617 if (bubbleChild.getLayoutParams() instanceof LayoutParams &&
618 (bubbleChild.getMeasuredWidth() | bubbleChild.getMeasuredHeight()) == 0) {
619 getChildrenLayout().measureChild(bubbleChild);
620 }
621 int measuredWidth = bubbleChild.getMeasuredWidth();
622 int measuredHeight = bubbleChild.getMeasuredHeight();
623
Andrew Flynnbc239a12012-03-06 11:39:49 -0800624 bubbleChild.setScaleX(scale);
625 bubbleChild.setScaleY(scale);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800626 }
627
628 private void resetChild(BubbleTextView bubbleChild) {
629 bubbleChild.setScaleX(1f);
630 bubbleChild.setScaleY(1f);
Andrew Flynnbc239a12012-03-06 11:39:49 -0800631
632 bubbleChild.setTextColor(getResources().getColor(R.color.workspace_icon_text_color));
633 }
634
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800635 public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
636 boolean markCells, boolean allApps) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700637 final LayoutParams lp = params;
638
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800639 // Hotseat icons - scale down and remove text
640 // Don't scale the all apps button
641 // scale percent set to -1 means do not scale
642 // Only scale BubbleTextViews
643 if (child instanceof BubbleTextView) {
644 BubbleTextView bubbleChild = (BubbleTextView) child;
645
Andrew Flynnbc239a12012-03-06 11:39:49 -0800646 // Start the child with 100% scale and visible text
647 resetChild(bubbleChild);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800648
Winson Chungeecf02d2012-03-02 17:14:58 -0800649 if (mIsHotseat && !allApps && mHotseatChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800650 // Scale/make transparent for a hotseat
Winson Chungeecf02d2012-03-02 17:14:58 -0800651 scaleChild(bubbleChild, 0f, mHotseatChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800652
Andrew Flynnbc239a12012-03-06 11:39:49 -0800653 bubbleChild.setTextColor(getResources().getColor(android.R.color.transparent));
Winson Chungeecf02d2012-03-02 17:14:58 -0800654 } else if (mChildScale >= 0) {
Andrew Flynnbc239a12012-03-06 11:39:49 -0800655 // Else possibly still scale it if we need to for smaller icons
Winson Chungeecf02d2012-03-02 17:14:58 -0800656 scaleChild(bubbleChild, 0f, mChildScale);
Andrew Flynn0dca1ec2012-02-29 13:33:22 -0800657 }
658 }
659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 // Generate an id for each view, this assumes we have at most 256x256 cells
661 // per workspace screen
Adam Cohend22015c2010-07-26 22:02:18 -0700662 if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700663 // If the horizontal or vertical span is set to -1, it is taken to
664 // mean that it spans the extent of the CellLayout
Adam Cohend22015c2010-07-26 22:02:18 -0700665 if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
666 if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667
Winson Chungaafa03c2010-06-11 17:34:16 -0700668 child.setId(childId);
669
Michael Jurka8c920dd2011-01-20 14:16:56 -0800670 mChildren.addView(child, index, lp);
Michael Jurkadee05892010-07-27 10:01:56 -0700671
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700672 if (markCells) markCellsAsOccupiedForView(child);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700673
Winson Chungaafa03c2010-06-11 17:34:16 -0700674 return true;
675 }
676 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
Michael Jurka3e7c7632010-10-02 16:01:03 -0700678
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 @Override
Michael Jurka0280c3b2010-09-17 15:00:07 -0700680 public void removeAllViews() {
681 clearOccupiedCells();
Michael Jurka8c920dd2011-01-20 14:16:56 -0800682 mChildren.removeAllViews();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700683 }
684
685 @Override
686 public void removeAllViewsInLayout() {
Michael Jurka7cfc2822011-08-02 20:19:24 -0700687 if (mChildren.getChildCount() > 0) {
688 clearOccupiedCells();
689 mChildren.removeAllViewsInLayout();
690 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700691 }
692
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700693 public void removeViewWithoutMarkingCells(View view) {
Michael Jurkacf6125c2011-01-28 15:20:01 -0800694 mChildren.removeView(view);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700695 }
696
Michael Jurka0280c3b2010-09-17 15:00:07 -0700697 @Override
698 public void removeView(View view) {
699 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800700 mChildren.removeView(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700701 }
702
703 @Override
704 public void removeViewAt(int index) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800705 markCellsAsUnoccupiedForView(mChildren.getChildAt(index));
706 mChildren.removeViewAt(index);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700707 }
708
709 @Override
710 public void removeViewInLayout(View view) {
711 markCellsAsUnoccupiedForView(view);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800712 mChildren.removeViewInLayout(view);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700713 }
714
715 @Override
716 public void removeViews(int start, int count) {
717 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800718 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700719 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800720 mChildren.removeViews(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700721 }
722
723 @Override
724 public void removeViewsInLayout(int start, int count) {
725 for (int i = start; i < start + count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800726 markCellsAsUnoccupiedForView(mChildren.getChildAt(i));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700727 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800728 mChildren.removeViewsInLayout(start, count);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700729 }
730
Michael Jurka8c920dd2011-01-20 14:16:56 -0800731 public void drawChildren(Canvas canvas) {
732 mChildren.draw(canvas);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 }
734
Michael Jurkaabded662011-03-04 12:06:57 -0800735 void buildChildrenLayer() {
736 mChildren.buildLayer();
737 }
738
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 @Override
740 protected void onAttachedToWindow() {
741 super.onAttachedToWindow();
742 mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this);
743 }
744
Michael Jurkaaf442092010-06-10 17:01:57 -0700745 public void setTagToCellInfoForPoint(int touchX, int touchY) {
746 final CellInfo cellInfo = mCellInfo;
Winson Chungeecf02d2012-03-02 17:14:58 -0800747 Rect frame = mRect;
Michael Jurkaaf442092010-06-10 17:01:57 -0700748 final int x = touchX + mScrollX;
749 final int y = touchY + mScrollY;
Michael Jurka8c920dd2011-01-20 14:16:56 -0800750 final int count = mChildren.getChildCount();
Michael Jurkaaf442092010-06-10 17:01:57 -0700751
752 boolean found = false;
753 for (int i = count - 1; i >= 0; i--) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800754 final View child = mChildren.getChildAt(i);
Adam Cohend4844c32011-02-18 19:25:06 -0800755 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurkaaf442092010-06-10 17:01:57 -0700756
Adam Cohen1b607ed2011-03-03 17:26:50 -0800757 if ((child.getVisibility() == VISIBLE || child.getAnimation() != null) &&
758 lp.isLockedToGrid) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700759 child.getHitRect(frame);
Winson Chung0be025d2011-05-23 17:45:09 -0700760
Winson Chungeecf02d2012-03-02 17:14:58 -0800761 float scale = child.getScaleX();
762 frame = new Rect(child.getLeft(), child.getTop(), child.getRight(),
763 child.getBottom());
Winson Chung0be025d2011-05-23 17:45:09 -0700764 // The child hit rect is relative to the CellLayoutChildren parent, so we need to
765 // offset that by this CellLayout's padding to test an (x,y) point that is relative
766 // to this view.
Winson Chung4b825dcd2011-06-19 12:41:22 -0700767 frame.offset(mPaddingLeft, mPaddingTop);
Winson Chungeecf02d2012-03-02 17:14:58 -0800768 frame.inset((int) (frame.width() * (1f - scale) / 2),
769 (int) (frame.height() * (1f - scale) / 2));
Winson Chung0be025d2011-05-23 17:45:09 -0700770
Michael Jurkaaf442092010-06-10 17:01:57 -0700771 if (frame.contains(x, y)) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700772 cellInfo.cell = child;
773 cellInfo.cellX = lp.cellX;
774 cellInfo.cellY = lp.cellY;
775 cellInfo.spanX = lp.cellHSpan;
776 cellInfo.spanY = lp.cellVSpan;
Michael Jurkaaf442092010-06-10 17:01:57 -0700777 found = true;
Michael Jurkaaf442092010-06-10 17:01:57 -0700778 break;
779 }
780 }
781 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700782
Michael Jurkad771c962011-08-09 15:00:48 -0700783 mLastDownOnOccupiedCell = found;
784
Michael Jurkaaf442092010-06-10 17:01:57 -0700785 if (!found) {
Winson Chung0be025d2011-05-23 17:45:09 -0700786 final int cellXY[] = mTmpXY;
Michael Jurkaaf442092010-06-10 17:01:57 -0700787 pointToCellExact(x, y, cellXY);
788
Michael Jurkaaf442092010-06-10 17:01:57 -0700789 cellInfo.cell = null;
790 cellInfo.cellX = cellXY[0];
791 cellInfo.cellY = cellXY[1];
792 cellInfo.spanX = 1;
793 cellInfo.spanY = 1;
Michael Jurkaaf442092010-06-10 17:01:57 -0700794 }
795 setTag(cellInfo);
796 }
797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 @Override
799 public boolean onInterceptTouchEvent(MotionEvent ev) {
Adam Cohenc1997fd2011-08-15 18:26:39 -0700800 // First we clear the tag to ensure that on every touch down we start with a fresh slate,
801 // even in the case where we return early. Not clearing here was causing bugs whereby on
802 // long-press we'd end up picking up an item from a previous drag operation.
803 final int action = ev.getAction();
804
805 if (action == MotionEvent.ACTION_DOWN) {
806 clearTagCellInfo();
807 }
808
Michael Jurkadee05892010-07-27 10:01:56 -0700809 if (mInterceptTouchListener != null && mInterceptTouchListener.onTouch(this, ev)) {
810 return true;
811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812
813 if (action == MotionEvent.ACTION_DOWN) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700814 setTagToCellInfoForPoint((int) ev.getX(), (int) ev.getY());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 }
Winson Chungeecf02d2012-03-02 17:14:58 -0800816
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 return false;
818 }
819
Adam Cohenc1997fd2011-08-15 18:26:39 -0700820 private void clearTagCellInfo() {
821 final CellInfo cellInfo = mCellInfo;
822 cellInfo.cell = null;
823 cellInfo.cellX = -1;
824 cellInfo.cellY = -1;
825 cellInfo.spanX = 0;
826 cellInfo.spanY = 0;
827 setTag(cellInfo);
828 }
829
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 public CellInfo getTag() {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700831 return (CellInfo) super.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
833
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700834 /**
Winson Chungaafa03c2010-06-11 17:34:16 -0700835 * Given a point, return the cell that strictly encloses that point
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 * @param x X coordinate of the point
837 * @param y Y coordinate of the point
838 * @param result Array of 2 ints to hold the x and y coordinate of the cell
839 */
840 void pointToCellExact(int x, int y, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700841 final int hStartPadding = getPaddingLeft();
842 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843
844 result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap);
845 result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap);
846
Adam Cohend22015c2010-07-26 22:02:18 -0700847 final int xAxis = mCountX;
848 final int yAxis = mCountY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849
850 if (result[0] < 0) result[0] = 0;
851 if (result[0] >= xAxis) result[0] = xAxis - 1;
852 if (result[1] < 0) result[1] = 0;
853 if (result[1] >= yAxis) result[1] = yAxis - 1;
854 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700855
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 /**
857 * Given a point, return the cell that most closely encloses that point
858 * @param x X coordinate of the point
859 * @param y Y coordinate of the point
860 * @param result Array of 2 ints to hold the x and y coordinate of the cell
861 */
862 void pointToCellRounded(int x, int y, int[] result) {
863 pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result);
864 }
865
866 /**
867 * Given a cell coordinate, return the point that represents the upper left corner of that cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700868 *
869 * @param cellX X coordinate of the cell
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 * @param cellY Y coordinate of the cell
Winson Chungaafa03c2010-06-11 17:34:16 -0700871 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 * @param result Array of 2 ints to hold the x and y coordinate of the point
873 */
874 void cellToPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700875 final int hStartPadding = getPaddingLeft();
876 final int vStartPadding = getPaddingTop();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877
878 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap);
879 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap);
880 }
881
Adam Cohene3e27a82011-04-15 12:07:39 -0700882 /**
Adam Cohen482ed822012-03-02 14:15:13 -0800883 * Given a cell coordinate, return the point that represents the center of the cell
Adam Cohene3e27a82011-04-15 12:07:39 -0700884 *
885 * @param cellX X coordinate of the cell
886 * @param cellY Y coordinate of the cell
887 *
888 * @param result Array of 2 ints to hold the x and y coordinate of the point
889 */
890 void cellToCenterPoint(int cellX, int cellY, int[] result) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700891 final int hStartPadding = getPaddingLeft();
892 final int vStartPadding = getPaddingTop();
Adam Cohene3e27a82011-04-15 12:07:39 -0700893
894 result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap) + mCellWidth / 2;
895 result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap) + mCellHeight / 2;
896 }
897
Adam Cohen482ed822012-03-02 14:15:13 -0800898 public float getDistanceFromCell(float x, float y, int[] cell) {
899 cellToCenterPoint(cell[0], cell[1], mTmpPoint);
900 float distance = (float) Math.sqrt( Math.pow(x - mTmpPoint[0], 2) +
901 Math.pow(y - mTmpPoint[1], 2));
902 return distance;
903 }
904
Romain Guy84f296c2009-11-04 15:00:44 -0800905 int getCellWidth() {
906 return mCellWidth;
907 }
908
909 int getCellHeight() {
910 return mCellHeight;
911 }
912
Adam Cohend4844c32011-02-18 19:25:06 -0800913 int getWidthGap() {
914 return mWidthGap;
915 }
916
917 int getHeightGap() {
918 return mHeightGap;
919 }
920
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700921 Rect getContentRect(Rect r) {
922 if (r == null) {
923 r = new Rect();
924 }
925 int left = getPaddingLeft();
926 int top = getPaddingTop();
Winson Chung4b825dcd2011-06-19 12:41:22 -0700927 int right = left + getWidth() - mPaddingLeft - mPaddingRight;
928 int bottom = top + getHeight() - mPaddingTop - mPaddingBottom;
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700929 r.set(left, top, right, bottom);
930 return r;
931 }
932
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 @Override
934 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
935 // TODO: currently ignoring padding
Winson Chungaafa03c2010-06-11 17:34:16 -0700936
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700938 int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
939
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
941 int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungaafa03c2010-06-11 17:34:16 -0700942
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
944 throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
945 }
946
Adam Cohend22015c2010-07-26 22:02:18 -0700947 int numWidthGaps = mCountX - 1;
948 int numHeightGaps = mCountY - 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949
Adam Cohen234c4cd2011-07-17 21:03:04 -0700950 if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700951 int hSpace = widthSpecSize - mPaddingLeft - mPaddingRight;
952 int vSpace = heightSpecSize - mPaddingTop - mPaddingBottom;
953 int hFreeSpace = hSpace - (mCountX * mOriginalCellWidth);
954 int vFreeSpace = vSpace - (mCountY * mOriginalCellHeight);
955 mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
956 mHeightGap = Math.min(mMaxGap,numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700957 mChildren.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700958 } else {
959 mWidthGap = mOriginalWidthGap;
960 mHeightGap = mOriginalHeightGap;
Winson Chungece7f5b2010-10-22 14:54:12 -0700961 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700962
Michael Jurka8c920dd2011-01-20 14:16:56 -0800963 // Initial values correspond to widthSpecMode == MeasureSpec.EXACTLY
964 int newWidth = widthSpecSize;
965 int newHeight = heightSpecSize;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700966 if (widthSpecMode == MeasureSpec.AT_MOST) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700967 newWidth = mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700968 ((mCountX - 1) * mWidthGap);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700969 newHeight = mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Winson Chungece7f5b2010-10-22 14:54:12 -0700970 ((mCountY - 1) * mHeightGap);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700971 setMeasuredDimension(newWidth, newHeight);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700972 }
Michael Jurka8c920dd2011-01-20 14:16:56 -0800973
974 int count = getChildCount();
975 for (int i = 0; i < count; i++) {
976 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700977 int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth - mPaddingLeft -
978 mPaddingRight, MeasureSpec.EXACTLY);
979 int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight - mPaddingTop -
980 mPaddingBottom, MeasureSpec.EXACTLY);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800981 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
982 }
983 setMeasuredDimension(newWidth, newHeight);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 }
985
986 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700987 protected void onLayout(boolean changed, int l, int t, int r, int b) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 int count = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 for (int i = 0; i < count; i++) {
Michael Jurka8c920dd2011-01-20 14:16:56 -0800990 View child = getChildAt(i);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700991 child.layout(mPaddingLeft, mPaddingTop,
992 r - l - mPaddingRight, b - t - mPaddingBottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
994 }
995
996 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700997 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
998 super.onSizeChanged(w, h, oldw, oldh);
Michael Jurka18014792010-10-14 09:01:34 -0700999 mBackgroundRect.set(0, 0, w, h);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001000 mForegroundRect.set(mForegroundPadding, mForegroundPadding,
1001 w - 2 * mForegroundPadding, h - 2 * mForegroundPadding);
Michael Jurkadee05892010-07-27 10:01:56 -07001002 }
1003
1004 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 protected void setChildrenDrawingCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001006 mChildren.setChildrenDrawingCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 }
1008
1009 @Override
1010 protected void setChildrenDrawnWithCacheEnabled(boolean enabled) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001011 mChildren.setChildrenDrawnWithCacheEnabled(enabled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 }
1013
Michael Jurka5f1c5092010-09-03 14:15:02 -07001014 public float getBackgroundAlpha() {
1015 return mBackgroundAlpha;
Michael Jurkadee05892010-07-27 10:01:56 -07001016 }
1017
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001018 public void setBackgroundAlphaMultiplier(float multiplier) {
1019 mBackgroundAlphaMultiplier = multiplier;
1020 }
1021
Adam Cohenddb82192010-11-10 16:32:54 -08001022 public float getBackgroundAlphaMultiplier() {
1023 return mBackgroundAlphaMultiplier;
1024 }
1025
Michael Jurka5f1c5092010-09-03 14:15:02 -07001026 public void setBackgroundAlpha(float alpha) {
Michael Jurkaafaa0502011-12-13 18:22:50 -08001027 if (mBackgroundAlpha != alpha) {
1028 mBackgroundAlpha = alpha;
1029 invalidate();
1030 }
Michael Jurkadee05892010-07-27 10:01:56 -07001031 }
1032
Michael Jurka5f1c5092010-09-03 14:15:02 -07001033 // Need to return true to let the view system know we know how to handle alpha-- this is
1034 // because when our children have an alpha of 0.0f, they are still rendering their "dimmed"
1035 // versions
1036 @Override
1037 protected boolean onSetAlpha(int alpha) {
1038 return true;
1039 }
1040
Michael Jurkaafaa0502011-12-13 18:22:50 -08001041 @Override
Michael Jurka5f1c5092010-09-03 14:15:02 -07001042 public void setAlpha(float alpha) {
1043 setChildrenAlpha(alpha);
1044 super.setAlpha(alpha);
1045 }
1046
Michael Jurkadee05892010-07-27 10:01:56 -07001047 private void setChildrenAlpha(float alpha) {
Michael Jurka0142d492010-08-25 17:46:15 -07001048 final int childCount = getChildCount();
1049 for (int i = 0; i < childCount; i++) {
Michael Jurkadee05892010-07-27 10:01:56 -07001050 getChildAt(i).setAlpha(alpha);
1051 }
1052 }
1053
Patrick Dubroy440c3602010-07-13 17:50:32 -07001054 public View getChildAt(int x, int y) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001055 return mChildren.getChildAt(x, y);
Patrick Dubroy440c3602010-07-13 17:50:32 -07001056 }
1057
Adam Cohen76fc0852011-06-17 13:26:23 -07001058 public boolean animateChildToPosition(final View child, int cellX, int cellY, int duration,
Adam Cohen482ed822012-03-02 14:15:13 -08001059 int delay, boolean permanent, boolean adjustOccupied) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001060 CellLayoutChildren clc = getChildrenLayout();
Adam Cohen482ed822012-03-02 14:15:13 -08001061 boolean[][] occupied = mOccupied;
1062 if (!permanent) {
1063 occupied = mTmpOccupied;
1064 }
1065
1066 if (clc.indexOfChild(child) != -1 && !occupied[cellX][cellY]) {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001067 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1068 final ItemInfo info = (ItemInfo) child.getTag();
1069
1070 // We cancel any existing animations
1071 if (mReorderAnimators.containsKey(lp)) {
1072 mReorderAnimators.get(lp).cancel();
1073 mReorderAnimators.remove(lp);
1074 }
1075
Adam Cohen482ed822012-03-02 14:15:13 -08001076 final int oldX = lp.x;
1077 final int oldY = lp.y;
1078 if (adjustOccupied) {
1079 occupied[lp.cellX][lp.cellY] = false;
1080 occupied[cellX][cellY] = true;
1081 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001082 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001083 if (permanent) {
1084 lp.cellX = info.cellX = cellX;
1085 lp.cellY = info.cellY = cellY;
1086 } else {
1087 lp.tmpCellX = cellX;
1088 lp.tmpCellY = cellY;
1089 }
Adam Cohenbfbfd262011-06-13 16:55:12 -07001090 clc.setupLp(lp);
1091 lp.isLockedToGrid = false;
Adam Cohen482ed822012-03-02 14:15:13 -08001092 final int newX = lp.x;
1093 final int newY = lp.y;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001094
Adam Cohen76fc0852011-06-17 13:26:23 -07001095 lp.x = oldX;
1096 lp.y = oldY;
Adam Cohen76fc0852011-06-17 13:26:23 -07001097
Adam Cohen482ed822012-03-02 14:15:13 -08001098 // Exit early if we're not actually moving the view
1099 if (oldX == newX && oldY == newY) {
1100 lp.isLockedToGrid = true;
1101 return true;
1102 }
1103
1104 ValueAnimator va = ValueAnimator.ofFloat(0f, 1f);
1105 va.setDuration(duration);
1106 mReorderAnimators.put(lp, va);
1107
1108 va.addUpdateListener(new AnimatorUpdateListener() {
1109 @Override
Adam Cohenbfbfd262011-06-13 16:55:12 -07001110 public void onAnimationUpdate(ValueAnimator animation) {
Adam Cohen482ed822012-03-02 14:15:13 -08001111 float r = ((Float) animation.getAnimatedValue()).floatValue();
1112 child.setTranslationX(r * (newX - oldX));
1113 child.setTranslationY(r * (newY - oldY));
Adam Cohenbfbfd262011-06-13 16:55:12 -07001114 }
1115 });
Adam Cohen482ed822012-03-02 14:15:13 -08001116 va.addListener(new AnimatorListenerAdapter() {
Adam Cohenbfbfd262011-06-13 16:55:12 -07001117 boolean cancelled = false;
1118 public void onAnimationEnd(Animator animation) {
1119 // If the animation was cancelled, it means that another animation
1120 // has interrupted this one, and we don't want to lock the item into
1121 // place just yet.
1122 if (!cancelled) {
Adam Cohen482ed822012-03-02 14:15:13 -08001123 child.setTranslationX(0);
1124 child.setTranslationY(0);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001125 lp.isLockedToGrid = true;
Adam Cohen482ed822012-03-02 14:15:13 -08001126 child.requestLayout();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001127 }
1128 if (mReorderAnimators.containsKey(lp)) {
1129 mReorderAnimators.remove(lp);
1130 }
1131 }
1132 public void onAnimationCancel(Animator animation) {
1133 cancelled = true;
1134 }
1135 });
Adam Cohen482ed822012-03-02 14:15:13 -08001136 va.setStartDelay(delay);
1137 va.start();
Adam Cohenbfbfd262011-06-13 16:55:12 -07001138 return true;
1139 }
1140 return false;
1141 }
1142
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001143 /**
1144 * Estimate where the top left cell of the dragged item will land if it is dropped.
1145 *
1146 * @param originX The X value of the top left corner of the item
1147 * @param originY The Y value of the top left corner of the item
1148 * @param spanX The number of horizontal cells that the item spans
1149 * @param spanY The number of vertical cells that the item spans
1150 * @param result The estimated drop cell X and Y.
1151 */
1152 void estimateDropCell(int originX, int originY, int spanX, int spanY, int[] result) {
Adam Cohend22015c2010-07-26 22:02:18 -07001153 final int countX = mCountX;
1154 final int countY = mCountY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001155
Michael Jurkaa63c4522010-08-19 13:52:27 -07001156 // pointToCellRounded takes the top left of a cell but will pad that with
1157 // cellWidth/2 and cellHeight/2 when finding the matching cell
1158 pointToCellRounded(originX, originY, result);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001159
1160 // If the item isn't fully on this screen, snap to the edges
1161 int rightOverhang = result[0] + spanX - countX;
1162 if (rightOverhang > 0) {
1163 result[0] -= rightOverhang; // Snap to right
1164 }
1165 result[0] = Math.max(0, result[0]); // Snap to left
1166 int bottomOverhang = result[1] + spanY - countY;
1167 if (bottomOverhang > 0) {
1168 result[1] -= bottomOverhang; // Snap to bottom
1169 }
1170 result[1] = Math.max(0, result[1]); // Snap to top
1171 }
1172
Adam Cohen482ed822012-03-02 14:15:13 -08001173 void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX,
1174 int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) {
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001175 final int oldDragCellX = mDragCell[0];
1176 final int oldDragCellY = mDragCell[1];
Adam Cohen482ed822012-03-02 14:15:13 -08001177
Winson Chungb8c69f32011-10-19 21:36:08 -07001178 if (v != null && dragOffset == null) {
Winson Chunga9abd0e2010-10-27 17:18:37 -07001179 mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
1180 } else {
1181 mDragCenter.set(originX, originY);
1182 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001183
Adam Cohen2801caf2011-05-13 20:57:39 -07001184 if (dragOutline == null && v == null) {
1185 if (mCrosshairsDrawable != null) {
1186 invalidate();
1187 }
1188 return;
1189 }
1190
Adam Cohen482ed822012-03-02 14:15:13 -08001191 if (cellX != oldDragCellX || cellY != oldDragCellY) {
1192 mDragCell[0] = cellX;
1193 mDragCell[1] = cellY;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001194 // Find the top left corner of the rect the object will occupy
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001195 final int[] topLeft = mTmpPoint;
Adam Cohen482ed822012-03-02 14:15:13 -08001196 cellToPoint(cellX, cellY, topLeft);
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001197
Joe Onorato4be866d2010-10-10 11:26:02 -07001198 int left = topLeft[0];
1199 int top = topLeft[1];
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001200
Winson Chungb8c69f32011-10-19 21:36:08 -07001201 if (v != null && dragOffset == null) {
Adam Cohen99e8b402011-03-25 19:23:43 -07001202 // When drawing the drag outline, it did not account for margin offsets
1203 // added by the view's parent.
1204 MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
1205 left += lp.leftMargin;
1206 top += lp.topMargin;
Winson Chung150fbab2010-09-29 17:14:26 -07001207
Adam Cohen99e8b402011-03-25 19:23:43 -07001208 // Offsets due to the size difference between the View and the dragOutline.
1209 // There is a size difference to account for the outer blur, which may lie
1210 // outside the bounds of the view.
Winson Chunga9abd0e2010-10-27 17:18:37 -07001211 top += (v.getHeight() - dragOutline.getHeight()) / 2;
Adam Cohenae915ce2011-08-25 13:47:22 -07001212 // We center about the x axis
1213 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1214 - dragOutline.getWidth()) / 2;
Adam Cohen66396872011-04-15 17:50:36 -07001215 } else {
Winson Chungb8c69f32011-10-19 21:36:08 -07001216 if (dragOffset != null && dragRegion != null) {
1217 // Center the drag region *horizontally* in the cell and apply a drag
1218 // outline offset
1219 left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1220 - dragRegion.width()) / 2;
1221 top += dragOffset.y;
1222 } else {
1223 // Center the drag outline in the cell
1224 left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
1225 - dragOutline.getWidth()) / 2;
1226 top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap)
1227 - dragOutline.getHeight()) / 2;
1228 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07001229 }
Joe Onorato4be866d2010-10-10 11:26:02 -07001230 final int oldIndex = mDragOutlineCurrent;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001231 mDragOutlineAnims[oldIndex].animateOut();
1232 mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
Adam Cohend41fbf52012-02-16 23:53:59 -08001233 Rect r = mDragOutlines[mDragOutlineCurrent];
1234 r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
1235 if (resize) {
Adam Cohen482ed822012-03-02 14:15:13 -08001236 cellToRect(cellX, cellY, spanX, spanY, r);
Adam Cohend41fbf52012-02-16 23:53:59 -08001237 }
Winson Chung150fbab2010-09-29 17:14:26 -07001238
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07001239 mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline);
1240 mDragOutlineAnims[mDragOutlineCurrent].animateIn();
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001241 }
Patrick Dubroy49250ad2010-10-08 15:33:52 -07001242
1243 // If we are drawing crosshairs, the entire CellLayout needs to be invalidated
1244 if (mCrosshairsDrawable != null) {
1245 invalidate();
1246 }
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001247 }
1248
Adam Cohene0310962011-04-18 16:15:31 -07001249 public void clearDragOutlines() {
1250 final int oldIndex = mDragOutlineCurrent;
1251 mDragOutlineAnims[oldIndex].animateOut();
Adam Cohend41fbf52012-02-16 23:53:59 -08001252 mDragCell[0] = mDragCell[1] = -1;
Adam Cohene0310962011-04-18 16:15:31 -07001253 }
1254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07001256 * Find a vacant area that will fit the given bounds nearest the requested
1257 * cell location. Uses Euclidean distance to score multiple vacant areas.
Winson Chungaafa03c2010-06-11 17:34:16 -07001258 *
Romain Guy51afc022009-05-04 18:03:43 -07001259 * @param pixelX The X location at which you want to search for a vacant area.
1260 * @param pixelY The Y location at which you want to search for a vacant area.
Jeff Sharkey70864282009-04-07 21:08:40 -07001261 * @param spanX Horizontal span of the object.
1262 * @param spanY Vertical span of the object.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001263 * @param result Array in which to place the result, or null (in which case a new array will
1264 * be allocated)
Jeff Sharkey70864282009-04-07 21:08:40 -07001265 * @return The X, Y cell of a vacant area that can contain this object,
1266 * nearest the requested location.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 */
Adam Cohend41fbf52012-02-16 23:53:59 -08001268 int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY,
1269 int[] result) {
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001270 return findNearestVacantArea(pixelX, pixelY, spanX, spanY, null, result);
Michael Jurka6a1435d2010-09-27 17:35:12 -07001271 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001272
Michael Jurka6a1435d2010-09-27 17:35:12 -07001273 /**
1274 * Find a vacant area that will fit the given bounds nearest the requested
1275 * cell location. Uses Euclidean distance to score multiple vacant areas.
1276 *
1277 * @param pixelX The X location at which you want to search for a vacant area.
1278 * @param pixelY The Y location at which you want to search for a vacant area.
Adam Cohend41fbf52012-02-16 23:53:59 -08001279 * @param minSpanX The minimum horizontal span required
1280 * @param minSpanY The minimum vertical span required
1281 * @param spanX Horizontal span of the object.
1282 * @param spanY Vertical span of the object.
1283 * @param result Array in which to place the result, or null (in which case a new array will
1284 * be allocated)
1285 * @return The X, Y cell of a vacant area that can contain this object,
1286 * nearest the requested location.
1287 */
1288 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1289 int spanY, int[] result, int[] resultSpan) {
1290 return findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null,
1291 result, resultSpan);
1292 }
1293
1294 /**
1295 * Find a vacant area that will fit the given bounds nearest the requested
1296 * cell location. Uses Euclidean distance to score multiple vacant areas.
1297 *
1298 * @param pixelX The X location at which you want to search for a vacant area.
1299 * @param pixelY The Y location at which you want to search for a vacant area.
Michael Jurka6a1435d2010-09-27 17:35:12 -07001300 * @param spanX Horizontal span of the object.
1301 * @param spanY Vertical span of the object.
Adam Cohendf035382011-04-11 17:22:04 -07001302 * @param ignoreOccupied If true, the result can be an occupied cell
1303 * @param result Array in which to place the result, or null (in which case a new array will
1304 * be allocated)
Michael Jurka6a1435d2010-09-27 17:35:12 -07001305 * @return The X, Y cell of a vacant area that can contain this object,
1306 * nearest the requested location.
1307 */
Adam Cohendf035382011-04-11 17:22:04 -07001308 int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, View ignoreView,
1309 boolean ignoreOccupied, int[] result) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001310 return findNearestArea(pixelX, pixelY, spanX, spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001311 spanX, spanY, ignoreView, ignoreOccupied, result, null, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001312 }
1313
1314 private final Stack<Rect> mTempRectStack = new Stack<Rect>();
1315 private void lazyInitTempRectStack() {
1316 if (mTempRectStack.isEmpty()) {
1317 for (int i = 0; i < mCountX * mCountY; i++) {
1318 mTempRectStack.push(new Rect());
1319 }
1320 }
1321 }
Adam Cohen482ed822012-03-02 14:15:13 -08001322
Adam Cohend41fbf52012-02-16 23:53:59 -08001323 private void recycleTempRects(Stack<Rect> used) {
1324 while (!used.isEmpty()) {
1325 mTempRectStack.push(used.pop());
1326 }
1327 }
1328
1329 /**
1330 * Find a vacant area that will fit the given bounds nearest the requested
1331 * cell location. Uses Euclidean distance to score multiple vacant areas.
1332 *
1333 * @param pixelX The X location at which you want to search for a vacant area.
1334 * @param pixelY The Y location at which you want to search for a vacant area.
1335 * @param minSpanX The minimum horizontal span required
1336 * @param minSpanY The minimum vertical span required
1337 * @param spanX Horizontal span of the object.
1338 * @param spanY Vertical span of the object.
1339 * @param ignoreOccupied If true, the result can be an occupied cell
1340 * @param result Array in which to place the result, or null (in which case a new array will
1341 * be allocated)
1342 * @return The X, Y cell of a vacant area that can contain this object,
1343 * nearest the requested location.
1344 */
1345 int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08001346 View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan,
1347 boolean[][] occupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001348 lazyInitTempRectStack();
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001349 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08001350 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001351
Adam Cohene3e27a82011-04-15 12:07:39 -07001352 // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds
1353 // to the center of the item, but we are searching based on the top-left cell, so
1354 // we translate the point over to correspond to the top-left.
1355 pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f;
1356 pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f;
1357
Jeff Sharkey70864282009-04-07 21:08:40 -07001358 // Keep track of best-scoring drop area
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001359 final int[] bestXY = result != null ? result : new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -07001360 double bestDistance = Double.MAX_VALUE;
Adam Cohend41fbf52012-02-16 23:53:59 -08001361 final Rect bestRect = new Rect(-1, -1, -1, -1);
1362 final Stack<Rect> validRegions = new Stack<Rect>();
Winson Chungaafa03c2010-06-11 17:34:16 -07001363
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001364 final int countX = mCountX;
1365 final int countY = mCountY;
Patrick Dubroyde7658b2010-09-27 11:15:43 -07001366
Adam Cohend41fbf52012-02-16 23:53:59 -08001367 if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 ||
1368 spanX < minSpanX || spanY < minSpanY) {
1369 return bestXY;
1370 }
1371
1372 for (int y = 0; y < countY - (minSpanY - 1); y++) {
Michael Jurkac28de512010-08-13 11:27:44 -07001373 inner:
Adam Cohend41fbf52012-02-16 23:53:59 -08001374 for (int x = 0; x < countX - (minSpanX - 1); x++) {
1375 int ySize = -1;
1376 int xSize = -1;
Adam Cohendf035382011-04-11 17:22:04 -07001377 if (ignoreOccupied) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001378 // First, let's see if this thing fits anywhere
1379 for (int i = 0; i < minSpanX; i++) {
1380 for (int j = 0; j < minSpanY; j++) {
Adam Cohendf035382011-04-11 17:22:04 -07001381 if (occupied[x + i][y + j]) {
Adam Cohendf035382011-04-11 17:22:04 -07001382 continue inner;
1383 }
Michael Jurkac28de512010-08-13 11:27:44 -07001384 }
1385 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001386 xSize = minSpanX;
1387 ySize = minSpanY;
1388
1389 // We know that the item will fit at _some_ acceptable size, now let's see
1390 // how big we can make it. We'll alternate between incrementing x and y spans
1391 // until we hit a limit.
1392 boolean incX = true;
1393 boolean hitMaxX = xSize >= spanX;
1394 boolean hitMaxY = ySize >= spanY;
1395 while (!(hitMaxX && hitMaxY)) {
1396 if (incX && !hitMaxX) {
1397 for (int j = 0; j < ySize; j++) {
1398 if (x + xSize > countX -1 || occupied[x + xSize][y + j]) {
1399 // We can't move out horizontally
1400 hitMaxX = true;
1401 }
1402 }
1403 if (!hitMaxX) {
1404 xSize++;
1405 }
1406 } else if (!hitMaxY) {
1407 for (int i = 0; i < xSize; i++) {
1408 if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) {
1409 // We can't move out vertically
1410 hitMaxY = true;
1411 }
1412 }
1413 if (!hitMaxY) {
1414 ySize++;
1415 }
1416 }
1417 hitMaxX |= xSize >= spanX;
1418 hitMaxY |= ySize >= spanY;
1419 incX = !incX;
1420 }
1421 incX = true;
1422 hitMaxX = xSize >= spanX;
1423 hitMaxY = ySize >= spanY;
Michael Jurkac28de512010-08-13 11:27:44 -07001424 }
Winson Chung0be025d2011-05-23 17:45:09 -07001425 final int[] cellXY = mTmpXY;
Adam Cohene3e27a82011-04-15 12:07:39 -07001426 cellToCenterPoint(x, y, cellXY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427
Adam Cohend41fbf52012-02-16 23:53:59 -08001428 // We verify that the current rect is not a sub-rect of any of our previous
1429 // candidates. In this case, the current rect is disqualified in favour of the
1430 // containing rect.
1431 Rect currentRect = mTempRectStack.pop();
1432 currentRect.set(x, y, x + xSize, y + ySize);
1433 boolean contained = false;
1434 for (Rect r : validRegions) {
1435 if (r.contains(currentRect)) {
1436 contained = true;
1437 break;
1438 }
1439 }
1440 validRegions.push(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001441 double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2)
1442 + Math.pow(cellXY[1] - pixelY, 2));
Adam Cohen482ed822012-03-02 14:15:13 -08001443
Adam Cohend41fbf52012-02-16 23:53:59 -08001444 if ((distance <= bestDistance && !contained) ||
1445 currentRect.contains(bestRect)) {
Michael Jurkac28de512010-08-13 11:27:44 -07001446 bestDistance = distance;
1447 bestXY[0] = x;
1448 bestXY[1] = y;
Adam Cohend41fbf52012-02-16 23:53:59 -08001449 if (resultSpan != null) {
1450 resultSpan[0] = xSize;
1451 resultSpan[1] = ySize;
1452 }
1453 bestRect.set(currentRect);
Michael Jurkac28de512010-08-13 11:27:44 -07001454 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 }
1456 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07001457 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08001458 markCellsAsOccupiedForView(ignoreView, occupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459
Adam Cohenc0dcf592011-06-01 15:30:43 -07001460 // Return -1, -1 if no suitable location found
1461 if (bestDistance == Double.MAX_VALUE) {
1462 bestXY[0] = -1;
1463 bestXY[1] = -1;
Jeff Sharkey70864282009-04-07 21:08:40 -07001464 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001465 recycleTempRects(validRegions);
Adam Cohenc0dcf592011-06-01 15:30:43 -07001466 return bestXY;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001468
Adam Cohen482ed822012-03-02 14:15:13 -08001469 /**
1470 * Find a vacant area that will fit the given bounds nearest the requested
1471 * cell location, and will also weigh in a suggested direction vector of the
1472 * desired location. This method computers distance based on unit grid distances,
1473 * not pixel distances.
1474 *
1475 * @param pixelX The X location at which you want to search for a vacant area.
1476 * @param pixelY The Y location at which you want to search for a vacant area.
1477 * @param minSpanX The minimum horizontal span required
1478 * @param minSpanY The minimum vertical span required
1479 * @param spanX Horizontal span of the object.
1480 * @param spanY Vertical span of the object.
1481 * @param ignoreOccupied If true, the result can be an occupied cell
1482 * @param result Array in which to place the result, or null (in which case a new array will
1483 * be allocated)
1484 * @return The X, Y cell of a vacant area that can contain this object,
1485 * nearest the requested location.
1486 */
1487 private int[] findNearestArea(int cellX, int cellY, int spanX, int spanY, int[] direction,
1488 boolean[][] occupied, int[] result) {
1489 // Keep track of best-scoring drop area
1490 final int[] bestXY = result != null ? result : new int[2];
1491 float bestDistance = Float.MAX_VALUE;
1492 int bestDirectionScore = Integer.MIN_VALUE;
1493
1494 final int countX = mCountX;
1495 final int countY = mCountY;
1496
1497 for (int y = 0; y < countY - (spanY - 1); y++) {
1498 inner:
1499 for (int x = 0; x < countX - (spanX - 1); x++) {
1500 // First, let's see if this thing fits anywhere
1501 for (int i = 0; i < spanX; i++) {
1502 for (int j = 0; j < spanY; j++) {
1503 if (occupied[x + i][y + j]) {
1504 continue inner;
1505 }
1506 }
1507 }
1508
1509 float distance = (float)
1510 Math.sqrt((x - cellX) * (x - cellX) + (y - cellY) * (y - cellY));
1511 int[] curDirection = mTmpPoint;
1512 computeDirectionVector(cellX, cellY, x, y, curDirection);
1513 int curDirectionScore = direction[0] * curDirection[0] +
1514 direction[1] * curDirection[1];
1515
1516 if (Float.compare(distance, bestDistance) < 0 || (Float.compare(distance,
1517 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
1534 private boolean addViewToTempLocation(View v, Rect rectOccupiedByPotentialDrop,
1535 int[] direction) {
1536 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1537 boolean success = false;
1538 markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
1539 lp.cellVSpan, mTmpOccupied, false);
1540 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1541
1542 findNearestArea(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan,
1543 direction, mTmpOccupied, mTempLocation);
1544
1545 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
1546 lp.tmpCellX = mTempLocation[0];
1547 lp.tmpCellY = mTempLocation[1];
1548 success = true;
1549
1550 }
1551 markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
1552 lp.cellVSpan, mTmpOccupied, true);
1553 return success;
1554 }
1555
1556 private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop,
1557 int[] direction) {
1558 if (views.size() == 0) return true;
1559 boolean success = false;
1560
1561 // We construct a rect which represents the entire group of views
1562 Rect boundingRect = null;
1563 for (View v: views) {
1564 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1565 markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
1566 lp.cellVSpan, mTmpOccupied, false);
1567 if (boundingRect == null) {
1568 boundingRect = new Rect(lp.tmpCellX, lp.tmpCellY, lp.tmpCellX + lp.cellHSpan,
1569 lp.tmpCellY + lp.cellVSpan);
1570 } else {
1571 boundingRect.union(lp.tmpCellX, lp.tmpCellY, lp.tmpCellX + lp.cellHSpan,
1572 lp.tmpCellY + lp.cellVSpan);
1573 }
1574 }
1575 markCellsForRect(rectOccupiedByPotentialDrop, mTmpOccupied, true);
1576
1577 // TODO: this bounding rect may not be completely filled, lets be more precise about this
1578 // check.
1579 findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(), boundingRect.height(),
1580 direction, mTmpOccupied, mTempLocation);
1581
1582 int deltaX = mTempLocation[0] - boundingRect.left;
1583 int deltaY = mTempLocation[1] - boundingRect.top;
1584 if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
1585 for (View v: views) {
1586 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1587 lp.tmpCellX += deltaX;
1588 lp.tmpCellY += deltaY;
1589 }
1590 success = true;
1591 }
1592 for (View v: views) {
1593 LayoutParams lp = (LayoutParams) v.getLayoutParams();
1594 markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan,
1595 lp.cellVSpan, mTmpOccupied, true);
1596 }
1597 return success;
1598 }
1599
1600 private void markCellsForRect(Rect r, boolean[][] occupied, boolean value) {
1601 markCellsForView(r.left, r.top, r.width(), r.height(), occupied, value);
1602 }
1603
1604 private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction,
1605 View ignoreView) {
1606 mIntersectingViews.clear();
1607
1608 mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY);
1609 markCellsForRect(mOccupiedRect, mTmpOccupied, true);
1610
1611 if (ignoreView != null) {
1612 LayoutParams lp = (LayoutParams) ignoreView.getLayoutParams();
1613 lp.tmpCellX = cellX;
1614 lp.tmpCellY = cellY;
1615 }
1616
1617 int childCount = mChildren.getChildCount();
1618 Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
1619 Rect r1 = new Rect();
1620 for (int i = 0; i < childCount; i++) {
1621 View child = mChildren.getChildAt(i);
1622 if (child == ignoreView) continue;
1623 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1624 r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan);
1625 if (Rect.intersects(r0, r1)) {
1626 if (!lp.canReorder) {
1627 return false;
1628 }
1629 mIntersectingViews.add(child);
1630 }
1631 }
1632 // First we try moving the views as a block
1633 if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction)) {
1634 return true;
1635 }
1636 // Ok, they couldn't move as a block, let's move them individually
1637 for (View v : mIntersectingViews) {
1638 if (!addViewToTempLocation(v, mOccupiedRect, direction)) {
1639 return false;
1640 }
1641 }
1642 return true;
1643 }
1644
1645 /*
1646 * Returns a pair (x, y), where x,y are in {-1, 0, 1} corresponding to vector between
1647 * the provided point and the provided cell
1648 */
1649 private void computeDirectionVector(int x0, int y0, int x1, int y1, int[] result) {
1650 int deltaX = x1 - x0;
1651 int deltaY = y1 - y0;
1652
1653 double angle = Math.atan(((float) deltaY) / deltaX);
1654
1655 result[0] = 0;
1656 result[1] = 0;
1657 if (Math.abs(Math.cos(angle)) > 0.5f) {
1658 result[0] = (int) Math.signum(deltaX);
1659 }
1660 if (Math.abs(Math.sin(angle)) > 0.5f) {
1661 result[1] = (int) Math.signum(deltaY);
1662 }
1663 }
1664
1665 ItemConfiguration simpleSwap(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX,
1666 int spanY, int[] direction, View dragView, boolean decX, ItemConfiguration solution) {
1667 // This creates a copy of the current occupied array, omitting the current view being
1668 // dragged
1669 resetTempLayoutToCurrent(dragView);
1670
1671 // We find the nearest cell into which we would place the dragged item, assuming there's
1672 // nothing in its way.
1673 int result[] = new int[2];
1674 result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
1675
1676 boolean success = false;
1677 // First we try the exact nearest position of the item being dragged,
1678 // we will then want to try to move this around to other neighbouring positions
1679 success = rearrangementExists(result[0], result[1], spanX, spanY, direction, dragView);
1680
1681 if (!success) {
1682 // We try shrinking the widget down to size in an alternating pattern, shrink 1 in
1683 // x, then 1 in y etc.
1684 if (spanX > minSpanX && (minSpanY == spanY || decX)) {
1685 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX - 1, spanY, direction,
1686 dragView, false, solution);
1687 } else if (spanY > minSpanY) {
1688 return simpleSwap(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY - 1, direction,
1689 dragView, true, solution);
1690 }
1691 solution.isSolution = false;
1692 } else {
1693 solution.isSolution = true;
1694 solution.dragViewX = result[0];
1695 solution.dragViewY = result[1];
1696 solution.dragViewSpanX = spanX;
1697 solution.dragViewSpanY = spanY;
1698 copyCurrentStateToSolution(solution, true);
1699 }
1700 return solution;
1701 }
1702
1703 private void copyCurrentStateToSolution(ItemConfiguration solution, boolean temp) {
1704 int childCount = mChildren.getChildCount();
1705 for (int i = 0; i < childCount; i++) {
1706 View child = mChildren.getChildAt(i);
1707 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1708 Point p;
1709 if (temp) {
1710 p = new Point(lp.tmpCellX, lp.tmpCellY);
1711 } else {
1712 p = new Point(lp.cellX, lp.cellY);
1713 }
1714 solution.map.put(child, p);
1715 }
1716 }
1717
1718 private void copySolutionToTempState(ItemConfiguration solution, View dragView) {
1719 for (int i = 0; i < mCountX; i++) {
1720 for (int j = 0; j < mCountY; j++) {
1721 mTmpOccupied[i][j] = false;
1722 }
1723 }
1724
1725 int childCount = mChildren.getChildCount();
1726 for (int i = 0; i < childCount; i++) {
1727 View child = mChildren.getChildAt(i);
1728 if (child == dragView) continue;
1729 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1730 Point p = solution.map.get(child);
1731 if (p != null) {
1732 lp.tmpCellX = p.x;
1733 lp.tmpCellY = p.y;
1734 markCellsForView(lp.tmpCellX, lp.tmpCellY, lp.cellHSpan, lp.cellVSpan,
1735 mTmpOccupied, true);
1736 }
1737 }
1738 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1739 solution.dragViewSpanY, mTmpOccupied, true);
1740 }
1741
1742 private void animateItemsToSolution(ItemConfiguration solution, View dragView, boolean
1743 commitDragView) {
1744
1745 boolean[][] occupied = DESTRUCTIVE_REORDER ? mOccupied : mTmpOccupied;
1746 for (int i = 0; i < mCountX; i++) {
1747 for (int j = 0; j < mCountY; j++) {
1748 occupied[i][j] = false;
1749 }
1750 }
1751
1752 int childCount = mChildren.getChildCount();
1753 for (int i = 0; i < childCount; i++) {
1754 View child = mChildren.getChildAt(i);
1755 if (child == dragView) continue;
1756 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1757 Point p = solution.map.get(child);
1758 if (p != null) {
1759 if (lp.cellX != p.x || lp.cellY != p.y) {
1760 animateChildToPosition(child, p.x, p.y, 150, 0, DESTRUCTIVE_REORDER, false);
1761 }
1762 markCellsForView(p.x, p.y, lp.cellHSpan, lp.cellVSpan, occupied, true);
1763 }
1764 }
1765 if (commitDragView) {
1766 markCellsForView(solution.dragViewX, solution.dragViewY, solution.dragViewSpanX,
1767 solution.dragViewSpanY, occupied, true);
1768 }
1769 }
1770
1771 private void commitTempPlacement() {
1772 for (int i = 0; i < mCountX; i++) {
1773 for (int j = 0; j < mCountY; j++) {
1774 mOccupied[i][j] = mTmpOccupied[i][j];
1775 }
1776 }
1777 int childCount = mChildren.getChildCount();
1778 for (int i = 0; i < childCount; i++) {
1779 LayoutParams lp = (LayoutParams) mChildren.getChildAt(i).getLayoutParams();
1780 lp.cellX = lp.tmpCellX;
1781 lp.cellY = lp.tmpCellY;
1782 }
1783 }
1784
1785 public void setUseTempCoords(boolean useTempCoords) {
1786 int childCount = mChildren.getChildCount();
1787 for (int i = 0; i < childCount; i++) {
1788 LayoutParams lp = (LayoutParams) mChildren.getChildAt(i).getLayoutParams();
1789 lp.useTmpCoords = useTempCoords;
1790 }
1791 }
1792
1793 private void resetTempLayoutToCurrent(View ignoreView) {
1794 for (int i = 0; i < mCountX; i++) {
1795 for (int j = 0; j < mCountY; j++) {
1796 mTmpOccupied[i][j] = mOccupied[i][j];
1797 }
1798 }
1799 int childCount = mChildren.getChildCount();
1800 for (int i = 0; i < childCount; i++) {
1801 View child = mChildren.getChildAt(i);
1802 if (child == ignoreView) continue;
1803 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1804 lp.tmpCellX = lp.cellX;
1805 lp.tmpCellY = lp.cellY;
1806 }
1807 }
1808
1809 ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
1810 int spanX, int spanY, View dragView, ItemConfiguration solution) {
1811 int[] result = new int[2];
1812 int[] resultSpan = new int[2];
1813 findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, null, result,
1814 resultSpan);
1815 if (result[0] >= 0 && result[1] >= 0) {
1816 copyCurrentStateToSolution(solution, false);
1817 solution.dragViewX = result[0];
1818 solution.dragViewY = result[1];
1819 solution.dragViewSpanX = resultSpan[0];
1820 solution.dragViewSpanY = resultSpan[1];
1821 solution.isSolution = true;
1822 } else {
1823 solution.isSolution = false;
1824 }
1825 return solution;
1826 }
1827
1828 public void prepareChildForDrag(View child) {
1829 markCellsAsUnoccupiedForView(child);
1830 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1831 lp.cellX = -1;
1832 lp.cellY = -1;
1833
1834 }
1835
1836 int[] createArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY,
1837 View dragView, int[] result, int resultSpan[], int mode) {
1838
1839 // First we determine if things have moved enough to cause a different layout
1840 result = findNearestArea(pixelX, pixelY, 1, 1, result);
1841
1842 if (resultSpan == null) {
1843 resultSpan = new int[2];
1844 }
1845
1846 // We attempt the first algorithm
1847 cellToCenterPoint(result[0], result[1], mTmpPoint);
1848 computeDirectionVector(pixelX, pixelY, mTmpPoint[0], mTmpPoint[1], mDirectionVector);
1849 ItemConfiguration swapSolution = simpleSwap(pixelX, pixelY, minSpanX, minSpanY,
1850 spanX, spanY, mDirectionVector, dragView, true, new ItemConfiguration());
1851
1852 // We attempt the approach which doesn't shuffle views at all
1853 ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
1854 minSpanY, spanX, spanY, dragView, new ItemConfiguration());
1855
1856 ItemConfiguration finalSolution = null;
1857 if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
1858 finalSolution = swapSolution;
1859 } else if (noShuffleSolution.isSolution) {
1860 finalSolution = noShuffleSolution;
1861 }
1862
1863 boolean foundSolution = true;
1864 if (!DESTRUCTIVE_REORDER) {
1865 setUseTempCoords(true);
1866 }
1867
1868 if (finalSolution != null) {
1869 result[0] = finalSolution.dragViewX;
1870 result[1] = finalSolution.dragViewY;
1871 resultSpan[0] = finalSolution.dragViewSpanX;
1872 resultSpan[1] = finalSolution.dragViewSpanY;
1873
1874 // If we're just testing for a possible location (MODE_ACCEPT_DROP), we don't bother
1875 // committing anything or animating anything as we just want to determine if a solution
1876 // exists
1877 if (mode == MODE_DRAG_OVER || mode == MODE_ON_DROP || mode == MODE_ON_DROP_EXTERNAL) {
1878 if (!DESTRUCTIVE_REORDER) {
1879 copySolutionToTempState(finalSolution, dragView);
1880 }
1881 setItemPlacementDirty(true);
1882 animateItemsToSolution(finalSolution, dragView, mode == MODE_ON_DROP);
1883
1884 if (!DESTRUCTIVE_REORDER && mode == MODE_ON_DROP) {
1885 commitTempPlacement();
1886 }
1887 }
1888 } else {
1889 foundSolution = false;
1890 result[0] = result[1] = resultSpan[0] = resultSpan[1] = -1;
1891 }
1892
1893 if ((mode == MODE_ON_DROP || !foundSolution) && !DESTRUCTIVE_REORDER) {
1894 setUseTempCoords(false);
1895 }
1896 boolean[][] occupied = mOccupied;
1897
1898 mChildren.requestLayout();
1899 return result;
1900 }
1901
1902 public boolean isItemPlacementDirty() {
1903 return mItemLocationsDirty;
1904 }
1905
1906 public void setItemPlacementDirty(boolean dirty) {
1907 mItemLocationsDirty = dirty;
1908 }
1909
1910 private class ItemConfiguration {
1911 HashMap<View, Point> map = new HashMap<View, Point>();
1912 boolean isSolution = false;
1913 int dragViewX, dragViewY, dragViewSpanX, dragViewSpanY;
1914
1915 int area() {
1916 return dragViewSpanX * dragViewSpanY;
1917 }
1918 void clear() {
1919 map.clear();
1920 isSolution = false;
1921 }
1922 }
1923
Adam Cohendf035382011-04-11 17:22:04 -07001924 /**
1925 * Find a vacant area that will fit the given bounds nearest the requested
1926 * cell location. Uses Euclidean distance to score multiple vacant areas.
1927 *
1928 * @param pixelX The X location at which you want to search for a vacant area.
1929 * @param pixelY The Y location at which you want to search for a vacant area.
1930 * @param spanX Horizontal span of the object.
1931 * @param spanY Vertical span of the object.
1932 * @param ignoreView Considers space occupied by this view as unoccupied
1933 * @param result Previously returned value to possibly recycle.
1934 * @return The X, Y cell of a vacant area that can contain this object,
1935 * nearest the requested location.
1936 */
1937 int[] findNearestVacantArea(
1938 int pixelX, int pixelY, int spanX, int spanY, View ignoreView, int[] result) {
1939 return findNearestArea(pixelX, pixelY, spanX, spanY, ignoreView, true, result);
1940 }
1941
1942 /**
Adam Cohend41fbf52012-02-16 23:53:59 -08001943 * Find a vacant area that will fit the given bounds nearest the requested
1944 * cell location. Uses Euclidean distance to score multiple vacant areas.
1945 *
1946 * @param pixelX The X location at which you want to search for a vacant area.
1947 * @param pixelY The Y location at which you want to search for a vacant area.
1948 * @param minSpanX The minimum horizontal span required
1949 * @param minSpanY The minimum vertical span required
1950 * @param spanX Horizontal span of the object.
1951 * @param spanY Vertical span of the object.
1952 * @param ignoreView Considers space occupied by this view as unoccupied
1953 * @param result Previously returned value to possibly recycle.
1954 * @return The X, Y cell of a vacant area that can contain this object,
1955 * nearest the requested location.
1956 */
1957 int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY,
1958 int spanX, int spanY, View ignoreView, int[] result, int[] resultSpan) {
Adam Cohen482ed822012-03-02 14:15:13 -08001959 return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, ignoreView, true,
1960 result, resultSpan, mOccupied);
Adam Cohend41fbf52012-02-16 23:53:59 -08001961 }
1962
1963 /**
Adam Cohendf035382011-04-11 17:22:04 -07001964 * Find a starting cell position that will fit the given bounds nearest the requested
1965 * cell location. Uses Euclidean distance to score multiple vacant areas.
1966 *
1967 * @param pixelX The X location at which you want to search for a vacant area.
1968 * @param pixelY The Y location at which you want to search for a vacant area.
1969 * @param spanX Horizontal span of the object.
1970 * @param spanY Vertical span of the object.
1971 * @param ignoreView Considers space occupied by this view as unoccupied
1972 * @param result Previously returned value to possibly recycle.
1973 * @return The X, Y cell of a vacant area that can contain this object,
1974 * nearest the requested location.
1975 */
1976 int[] findNearestArea(
1977 int pixelX, int pixelY, int spanX, int spanY, int[] result) {
1978 return findNearestArea(pixelX, pixelY, spanX, spanY, null, false, result);
1979 }
1980
Michael Jurka0280c3b2010-09-17 15:00:07 -07001981 boolean existsEmptyCell() {
1982 return findCellForSpan(null, 1, 1);
1983 }
1984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07001986 * Finds the upper-left coordinate of the first rectangle in the grid that can
1987 * hold a cell of the specified dimensions. If intersectX and intersectY are not -1,
1988 * then this method will only return coordinates for rectangles that contain the cell
1989 * (intersectX, intersectY)
1990 *
1991 * @param cellXY The array that will contain the position of a vacant cell if such a cell
1992 * can be found.
1993 * @param spanX The horizontal span of the cell we want to find.
1994 * @param spanY The vertical span of the cell we want to find.
1995 *
1996 * @return True if a vacant cell of the specified dimension was found, false otherwise.
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001997 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001998 boolean findCellForSpan(int[] cellXY, int spanX, int spanY) {
Adam Cohen482ed822012-03-02 14:15:13 -08001999 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002000 }
2001
2002 /**
2003 * Like above, but ignores any cells occupied by the item "ignoreView"
2004 *
2005 * @param cellXY The array that will contain the position of a vacant cell if such a cell
2006 * can be found.
2007 * @param spanX The horizontal span of the cell we want to find.
2008 * @param spanY The vertical span of the cell we want to find.
2009 * @param ignoreView The home screen item we should treat as not occupying any space
2010 * @return
2011 */
2012 boolean findCellForSpanIgnoring(int[] cellXY, int spanX, int spanY, View ignoreView) {
Adam Cohen482ed822012-03-02 14:15:13 -08002013 return findCellForSpanThatIntersectsIgnoring(cellXY, spanX, spanY, -1, -1,
2014 ignoreView, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002015 }
2016
2017 /**
2018 * Like above, but if intersectX and intersectY are not -1, then this method will try to
2019 * return coordinates for rectangles that contain the cell [intersectX, intersectY]
2020 *
2021 * @param spanX The horizontal span of the cell we want to find.
2022 * @param spanY The vertical span of the cell we want to find.
2023 * @param ignoreView The home screen item we should treat as not occupying any space
2024 * @param intersectX The X coordinate of the cell that we should try to overlap
2025 * @param intersectX The Y coordinate of the cell that we should try to overlap
2026 *
2027 * @return True if a vacant cell of the specified dimension was found, false otherwise.
2028 */
2029 boolean findCellForSpanThatIntersects(int[] cellXY, int spanX, int spanY,
2030 int intersectX, int intersectY) {
2031 return findCellForSpanThatIntersectsIgnoring(
Adam Cohen482ed822012-03-02 14:15:13 -08002032 cellXY, spanX, spanY, intersectX, intersectY, null, mOccupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002033 }
2034
2035 /**
2036 * The superset of the above two methods
2037 */
2038 boolean findCellForSpanThatIntersectsIgnoring(int[] cellXY, int spanX, int spanY,
Adam Cohen482ed822012-03-02 14:15:13 -08002039 int intersectX, int intersectY, View ignoreView, boolean occupied[][]) {
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002040 // mark space take by ignoreView as available (method checks if ignoreView is null)
Adam Cohen482ed822012-03-02 14:15:13 -08002041 markCellsAsUnoccupiedForView(ignoreView, occupied);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002042
Michael Jurka28750fb2010-09-24 17:43:49 -07002043 boolean foundCell = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002044 while (true) {
2045 int startX = 0;
2046 if (intersectX >= 0) {
2047 startX = Math.max(startX, intersectX - (spanX - 1));
2048 }
2049 int endX = mCountX - (spanX - 1);
2050 if (intersectX >= 0) {
2051 endX = Math.min(endX, intersectX + (spanX - 1) + (spanX == 1 ? 1 : 0));
2052 }
2053 int startY = 0;
2054 if (intersectY >= 0) {
2055 startY = Math.max(startY, intersectY - (spanY - 1));
2056 }
2057 int endY = mCountY - (spanY - 1);
2058 if (intersectY >= 0) {
2059 endY = Math.min(endY, intersectY + (spanY - 1) + (spanY == 1 ? 1 : 0));
2060 }
2061
Winson Chungbbc60d82010-11-11 16:34:41 -08002062 for (int y = startY; y < endY && !foundCell; y++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002063 inner:
Winson Chungbbc60d82010-11-11 16:34:41 -08002064 for (int x = startX; x < endX; x++) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002065 for (int i = 0; i < spanX; i++) {
2066 for (int j = 0; j < spanY; j++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002067 if (occupied[x + i][y + j]) {
Winson Chungbbc60d82010-11-11 16:34:41 -08002068 // small optimization: we can skip to after the column we just found
Michael Jurka0280c3b2010-09-17 15:00:07 -07002069 // an occupied cell
Winson Chungbbc60d82010-11-11 16:34:41 -08002070 x += i;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002071 continue inner;
2072 }
2073 }
2074 }
2075 if (cellXY != null) {
2076 cellXY[0] = x;
2077 cellXY[1] = y;
2078 }
Michael Jurka28750fb2010-09-24 17:43:49 -07002079 foundCell = true;
2080 break;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002081 }
2082 }
2083 if (intersectX == -1 && intersectY == -1) {
2084 break;
2085 } else {
2086 // if we failed to find anything, try again but without any requirements of
2087 // intersecting
2088 intersectX = -1;
2089 intersectY = -1;
2090 continue;
2091 }
2092 }
2093
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002094 // re-mark space taken by ignoreView as occupied
Adam Cohen482ed822012-03-02 14:15:13 -08002095 markCellsAsOccupiedForView(ignoreView, occupied);
Michael Jurka28750fb2010-09-24 17:43:49 -07002096 return foundCell;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002097 }
2098
2099 /**
Winson Chungc07918d2011-07-01 15:35:26 -07002100 * A drag event has begun over this layout.
2101 * It may have begun over this layout (in which case onDragChild is called first),
2102 * or it may have begun on another layout.
2103 */
2104 void onDragEnter() {
2105 if (!mDragging) {
2106 // Fade in the drag indicators
2107 if (mCrosshairsAnimator != null) {
2108 mCrosshairsAnimator.animateIn();
2109 }
2110 }
2111 mDragging = true;
2112 }
2113
2114 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07002115 * Called when drag has left this CellLayout or has been completed (successfully or not)
2116 */
2117 void onDragExit() {
Joe Onorato4be866d2010-10-10 11:26:02 -07002118 // This can actually be called when we aren't in a drag, e.g. when adding a new
2119 // item to this layout via the customize drawer.
2120 // Guard against that case.
2121 if (mDragging) {
2122 mDragging = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002123
Joe Onorato4be866d2010-10-10 11:26:02 -07002124 // Fade out the drag indicators
2125 if (mCrosshairsAnimator != null) {
2126 mCrosshairsAnimator.animateOut();
2127 }
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002128 }
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002129
2130 // Invalidate the drag data
Adam Cohend41fbf52012-02-16 23:53:59 -08002131 mDragCell[0] = mDragCell[1] = -1;
Patrick Dubroy08ae2ec2010-10-14 23:54:22 -07002132 mDragOutlineAnims[mDragOutlineCurrent].animateOut();
2133 mDragOutlineCurrent = (mDragOutlineCurrent + 1) % mDragOutlineAnims.length;
2134
Michael Jurka33945b22010-12-21 18:19:38 -08002135 setIsDragOverlapping(false);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002136 }
2137
2138 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002139 * Mark a child as having been dropped.
Patrick Dubroyde7658b2010-09-27 11:15:43 -07002140 * At the beginning of the drag operation, the child may have been on another
Patrick Dubroyce34a972010-10-19 10:34:32 -07002141 * screen, but it is re-parented before this method is called.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 *
2143 * @param child The child that is being dropped
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 */
Adam Cohen716b51e2011-06-30 12:09:54 -07002145 void onDropChild(View child) {
Romain Guyd94533d2009-08-17 10:01:15 -07002146 if (child != null) {
2147 LayoutParams lp = (LayoutParams) child.getLayoutParams();
Romain Guy84f296c2009-11-04 15:00:44 -08002148 lp.dropped = true;
Romain Guyd94533d2009-08-17 10:01:15 -07002149 child.requestLayout();
Romain Guyd94533d2009-08-17 10:01:15 -07002150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 }
2152
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 * Computes a bounding rectangle for a range of cells
Winson Chungaafa03c2010-06-11 17:34:16 -07002155 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 * @param cellX X coordinate of upper left corner expressed as a cell position
2157 * @param cellY Y coordinate of upper left corner expressed as a cell position
Winson Chungaafa03c2010-06-11 17:34:16 -07002158 * @param cellHSpan Width in cells
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 * @param cellVSpan Height in cells
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002160 * @param resultRect Rect into which to put the results
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 */
Adam Cohend41fbf52012-02-16 23:53:59 -08002162 public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 final int cellWidth = mCellWidth;
2164 final int cellHeight = mCellHeight;
2165 final int widthGap = mWidthGap;
2166 final int heightGap = mHeightGap;
Winson Chungaafa03c2010-06-11 17:34:16 -07002167
Winson Chung4b825dcd2011-06-19 12:41:22 -07002168 final int hStartPadding = getPaddingLeft();
2169 final int vStartPadding = getPaddingTop();
Winson Chungaafa03c2010-06-11 17:34:16 -07002170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
2172 int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
2173
2174 int x = hStartPadding + cellX * (cellWidth + widthGap);
2175 int y = vStartPadding + cellY * (cellHeight + heightGap);
Winson Chungaafa03c2010-06-11 17:34:16 -07002176
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002177 resultRect.set(x, y, x + width, y + height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 /**
Winson Chungaafa03c2010-06-11 17:34:16 -07002181 * Computes the required horizontal and vertical cell spans to always
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 * fit the given rectangle.
Winson Chungaafa03c2010-06-11 17:34:16 -07002183 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184 * @param width Width in pixels
2185 * @param height Height in pixels
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002186 * @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 -08002187 */
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002188 public int[] rectToCell(int width, int height, int[] result) {
Michael Jurka9987a5c2010-10-08 16:58:12 -07002189 return rectToCell(getResources(), width, height, result);
2190 }
2191
2192 public static int[] rectToCell(Resources resources, int width, int height, int[] result) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 // Always assume we're working with the smallest span to make sure we
2194 // reserve enough space in both orientations.
Joe Onorato79e56262009-09-21 15:23:04 -04002195 int actualWidth = resources.getDimensionPixelSize(R.dimen.workspace_cell_width);
2196 int actualHeight = resources.getDimensionPixelSize(R.dimen.workspace_cell_height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 int smallerSize = Math.min(actualWidth, actualHeight);
Joe Onorato79e56262009-09-21 15:23:04 -04002198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 // Always round up to next largest cell
Winson Chung54c725c2011-08-03 12:03:40 -07002200 int spanX = (int) Math.ceil(width / (float) smallerSize);
2201 int spanY = (int) Math.ceil(height / (float) smallerSize);
Joe Onorato79e56262009-09-21 15:23:04 -04002202
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07002203 if (result == null) {
2204 return new int[] { spanX, spanY };
2205 }
2206 result[0] = spanX;
2207 result[1] = spanY;
2208 return result;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 }
2210
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002211 public int[] cellSpansToSize(int hSpans, int vSpans) {
2212 int[] size = new int[2];
2213 size[0] = hSpans * mCellWidth + (hSpans - 1) * mWidthGap;
2214 size[1] = vSpans * mCellHeight + (vSpans - 1) * mHeightGap;
2215 return size;
2216 }
2217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 /**
Patrick Dubroy047379a2010-12-19 22:02:04 -08002219 * Calculate the grid spans needed to fit given item
2220 */
2221 public void calculateSpans(ItemInfo info) {
2222 final int minWidth;
2223 final int minHeight;
2224
2225 if (info instanceof LauncherAppWidgetInfo) {
2226 minWidth = ((LauncherAppWidgetInfo) info).minWidth;
2227 minHeight = ((LauncherAppWidgetInfo) info).minHeight;
2228 } else if (info instanceof PendingAddWidgetInfo) {
2229 minWidth = ((PendingAddWidgetInfo) info).minWidth;
2230 minHeight = ((PendingAddWidgetInfo) info).minHeight;
2231 } else {
2232 // It's not a widget, so it must be 1x1
2233 info.spanX = info.spanY = 1;
2234 return;
2235 }
2236 int[] spans = rectToCell(minWidth, minHeight, null);
2237 info.spanX = spans[0];
2238 info.spanY = spans[1];
2239 }
2240
2241 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 * Find the first vacant cell, if there is one.
2243 *
2244 * @param vacant Holds the x and y coordinate of the vacant cell
2245 * @param spanX Horizontal cell span.
2246 * @param spanY Vertical cell span.
Winson Chungaafa03c2010-06-11 17:34:16 -07002247 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 * @return True if a vacant cell was found
2249 */
2250 public boolean getVacantCell(int[] vacant, int spanX, int spanY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251
Michael Jurka0280c3b2010-09-17 15:00:07 -07002252 return findVacantCell(vacant, spanX, spanY, mCountX, mCountY, mOccupied);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 }
2254
2255 static boolean findVacantCell(int[] vacant, int spanX, int spanY,
2256 int xCount, int yCount, boolean[][] occupied) {
2257
Adam Cohen2801caf2011-05-13 20:57:39 -07002258 for (int y = 0; y < yCount; y++) {
2259 for (int x = 0; x < xCount; x++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 boolean available = !occupied[x][y];
2261out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) {
2262 for (int j = y; j < y + spanY - 1 && y < yCount; j++) {
2263 available = available && !occupied[i][j];
2264 if (!available) break out;
2265 }
2266 }
2267
2268 if (available) {
2269 vacant[0] = x;
2270 vacant[1] = y;
2271 return true;
2272 }
2273 }
2274 }
2275
2276 return false;
2277 }
2278
Michael Jurka0280c3b2010-09-17 15:00:07 -07002279 private void clearOccupiedCells() {
2280 for (int x = 0; x < mCountX; x++) {
2281 for (int y = 0; y < mCountY; y++) {
2282 mOccupied[x][y] = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002283 }
2284 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002285 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286
Adam Cohen1b607ed2011-03-03 17:26:50 -08002287 /**
2288 * Given a view, determines how much that view can be expanded in all directions, in terms of
2289 * whether or not there are other items occupying adjacent cells. Used by the
2290 * AppWidgetResizeFrame to determine how the widget can be resized.
2291 */
Adam Cohend4844c32011-02-18 19:25:06 -08002292 public void getExpandabilityArrayForView(View view, int[] expandability) {
Adam Cohen1b607ed2011-03-03 17:26:50 -08002293 final LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohend4844c32011-02-18 19:25:06 -08002294 boolean flag;
2295
Adam Cohen1b607ed2011-03-03 17:26:50 -08002296 expandability[AppWidgetResizeFrame.LEFT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002297 for (int x = lp.cellX - 1; x >= 0; x--) {
2298 flag = false;
2299 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
2300 if (mOccupied[x][y]) flag = true;
2301 }
2302 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002303 expandability[AppWidgetResizeFrame.LEFT]++;
Adam Cohend4844c32011-02-18 19:25:06 -08002304 }
2305
Adam Cohen1b607ed2011-03-03 17:26:50 -08002306 expandability[AppWidgetResizeFrame.TOP] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002307 for (int y = lp.cellY - 1; y >= 0; y--) {
2308 flag = false;
2309 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
2310 if (mOccupied[x][y]) flag = true;
2311 }
2312 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002313 expandability[AppWidgetResizeFrame.TOP]++;
2314 }
Adam Cohend4844c32011-02-18 19:25:06 -08002315
Adam Cohen1b607ed2011-03-03 17:26:50 -08002316 expandability[AppWidgetResizeFrame.RIGHT] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002317 for (int x = lp.cellX + lp.cellHSpan; x < mCountX; x++) {
2318 flag = false;
2319 for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan; y++) {
2320 if (mOccupied[x][y]) flag = true;
2321 }
2322 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002323 expandability[AppWidgetResizeFrame.RIGHT]++;
2324 }
Adam Cohend4844c32011-02-18 19:25:06 -08002325
Adam Cohen1b607ed2011-03-03 17:26:50 -08002326 expandability[AppWidgetResizeFrame.BOTTOM] = 0;
Adam Cohend4844c32011-02-18 19:25:06 -08002327 for (int y = lp.cellY + lp.cellVSpan; y < mCountY; y++) {
2328 flag = false;
2329 for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan; x++) {
2330 if (mOccupied[x][y]) flag = true;
2331 }
2332 if (flag) break;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002333 expandability[AppWidgetResizeFrame.BOTTOM]++;
2334 }
Adam Cohend4844c32011-02-18 19:25:06 -08002335 }
2336
Adam Cohend41fbf52012-02-16 23:53:59 -08002337 public void onMove(View view, int newCellX, int newCellY, int newSpanX, int newSpanY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002338 markCellsAsUnoccupiedForView(view);
Adam Cohen482ed822012-03-02 14:15:13 -08002339 markCellsForView(newCellX, newCellY, newSpanX, newSpanY, mOccupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002341
Adam Cohend4844c32011-02-18 19:25:06 -08002342 public void markCellsAsOccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002343 markCellsAsOccupiedForView(view, mOccupied);
2344 }
2345 public void markCellsAsOccupiedForView(View view, boolean[][] occupied) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002346 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002347 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002348 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002349 }
2350
Adam Cohend4844c32011-02-18 19:25:06 -08002351 public void markCellsAsUnoccupiedForView(View view) {
Adam Cohen482ed822012-03-02 14:15:13 -08002352 markCellsAsUnoccupiedForView(view, mOccupied);
2353 }
2354 public void markCellsAsUnoccupiedForView(View view, boolean occupied[][]) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002355 if (view == null || view.getParent() != mChildren) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002356 LayoutParams lp = (LayoutParams) view.getLayoutParams();
Adam Cohen482ed822012-03-02 14:15:13 -08002357 markCellsForView(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, occupied, false);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002358 }
2359
Adam Cohen482ed822012-03-02 14:15:13 -08002360 private void markCellsForView(int cellX, int cellY, int spanX, int spanY, boolean[][] occupied,
2361 boolean value) {
2362 if (cellX < 0 || cellY < 0) return;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002363 for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
2364 for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
Adam Cohen482ed822012-03-02 14:15:13 -08002365 occupied[x][y] = value;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 }
2367 }
2368 }
2369
Adam Cohen2801caf2011-05-13 20:57:39 -07002370 public int getDesiredWidth() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002371 return mPaddingLeft + mPaddingRight + (mCountX * mCellWidth) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002372 (Math.max((mCountX - 1), 0) * mWidthGap);
2373 }
2374
2375 public int getDesiredHeight() {
Winson Chung4b825dcd2011-06-19 12:41:22 -07002376 return mPaddingTop + mPaddingBottom + (mCountY * mCellHeight) +
Adam Cohen2801caf2011-05-13 20:57:39 -07002377 (Math.max((mCountY - 1), 0) * mHeightGap);
2378 }
2379
Michael Jurka66d72172011-04-12 16:29:25 -07002380 public boolean isOccupied(int x, int y) {
2381 if (x < mCountX && y < mCountY) {
2382 return mOccupied[x][y];
2383 } else {
2384 throw new RuntimeException("Position exceeds the bound of this CellLayout");
2385 }
2386 }
2387
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 @Override
2389 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
2390 return new CellLayout.LayoutParams(getContext(), attrs);
2391 }
2392
2393 @Override
2394 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2395 return p instanceof CellLayout.LayoutParams;
2396 }
2397
2398 @Override
2399 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2400 return new CellLayout.LayoutParams(p);
2401 }
2402
Winson Chungaafa03c2010-06-11 17:34:16 -07002403 public static class CellLayoutAnimationController extends LayoutAnimationController {
2404 public CellLayoutAnimationController(Animation animation, float delay) {
2405 super(animation, delay);
2406 }
2407
2408 @Override
2409 protected long getDelayForView(View view) {
2410 return (int) (Math.random() * 150);
2411 }
2412 }
2413
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
2415 /**
2416 * Horizontal location of the item in the grid.
2417 */
2418 @ViewDebug.ExportedProperty
2419 public int cellX;
2420
2421 /**
2422 * Vertical location of the item in the grid.
2423 */
2424 @ViewDebug.ExportedProperty
2425 public int cellY;
2426
2427 /**
Adam Cohen482ed822012-03-02 14:15:13 -08002428 * Temporary horizontal location of the item in the grid during reorder
2429 */
2430 public int tmpCellX;
2431
2432 /**
2433 * Temporary vertical location of the item in the grid during reorder
2434 */
2435 public int tmpCellY;
2436
2437 /**
2438 * Indicates that the temporary coordinates should be used to layout the items
2439 */
2440 public boolean useTmpCoords;
2441
2442 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 * Number of cells spanned horizontally by the item.
2444 */
2445 @ViewDebug.ExportedProperty
2446 public int cellHSpan;
2447
2448 /**
2449 * Number of cells spanned vertically by the item.
2450 */
2451 @ViewDebug.ExportedProperty
2452 public int cellVSpan;
Winson Chungaafa03c2010-06-11 17:34:16 -07002453
Adam Cohen1b607ed2011-03-03 17:26:50 -08002454 /**
2455 * Indicates whether the item will set its x, y, width and height parameters freely,
2456 * or whether these will be computed based on cellX, cellY, cellHSpan and cellVSpan.
2457 */
Adam Cohend4844c32011-02-18 19:25:06 -08002458 public boolean isLockedToGrid = true;
2459
Adam Cohen482ed822012-03-02 14:15:13 -08002460 /**
2461 * Indicates whether this item can be reordered. Always true except in the case of the
2462 * the AllApps button.
2463 */
2464 public boolean canReorder = true;
2465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 // X coordinate of the view in the layout.
2467 @ViewDebug.ExportedProperty
2468 int x;
2469 // Y coordinate of the view in the layout.
2470 @ViewDebug.ExportedProperty
2471 int y;
2472
Romain Guy84f296c2009-11-04 15:00:44 -08002473 boolean dropped;
Romain Guyfcb9e712009-10-02 16:06:52 -07002474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475 public LayoutParams(Context c, AttributeSet attrs) {
2476 super(c, attrs);
2477 cellHSpan = 1;
2478 cellVSpan = 1;
2479 }
2480
2481 public LayoutParams(ViewGroup.LayoutParams source) {
2482 super(source);
2483 cellHSpan = 1;
2484 cellVSpan = 1;
2485 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002486
2487 public LayoutParams(LayoutParams source) {
2488 super(source);
2489 this.cellX = source.cellX;
2490 this.cellY = source.cellY;
2491 this.cellHSpan = source.cellHSpan;
2492 this.cellVSpan = source.cellVSpan;
2493 }
2494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) {
Romain Guy8f19cdd2010-01-08 15:07:00 -08002496 super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 this.cellX = cellX;
2498 this.cellY = cellY;
2499 this.cellHSpan = cellHSpan;
2500 this.cellVSpan = cellVSpan;
2501 }
2502
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002503 public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap) {
Adam Cohend4844c32011-02-18 19:25:06 -08002504 if (isLockedToGrid) {
2505 final int myCellHSpan = cellHSpan;
2506 final int myCellVSpan = cellVSpan;
Adam Cohen482ed822012-03-02 14:15:13 -08002507 final int myCellX = useTmpCoords ? tmpCellX : cellX;
2508 final int myCellY = useTmpCoords ? tmpCellY : cellY;
Adam Cohen1b607ed2011-03-03 17:26:50 -08002509
Adam Cohend4844c32011-02-18 19:25:06 -08002510 width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) -
2511 leftMargin - rightMargin;
2512 height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) -
2513 topMargin - bottomMargin;
Winson Chungeecf02d2012-03-02 17:14:58 -08002514 x = (int) (myCellX * (cellWidth + widthGap) + leftMargin);
2515 y = (int) (myCellY * (cellHeight + heightGap) + topMargin);
Adam Cohend4844c32011-02-18 19:25:06 -08002516 }
2517 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002518
Winson Chungaafa03c2010-06-11 17:34:16 -07002519 public String toString() {
2520 return "(" + this.cellX + ", " + this.cellY + ")";
2521 }
Adam Cohen7f4eabe2011-04-21 16:19:16 -07002522
2523 public void setWidth(int width) {
2524 this.width = width;
2525 }
2526
2527 public int getWidth() {
2528 return width;
2529 }
2530
2531 public void setHeight(int height) {
2532 this.height = height;
2533 }
2534
2535 public int getHeight() {
2536 return height;
2537 }
2538
2539 public void setX(int x) {
2540 this.x = x;
2541 }
2542
2543 public int getX() {
2544 return x;
2545 }
2546
2547 public void setY(int y) {
2548 this.y = y;
2549 }
2550
2551 public int getY() {
2552 return y;
2553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 }
2555
Michael Jurka0280c3b2010-09-17 15:00:07 -07002556 // This class stores info for two purposes:
2557 // 1. When dragging items (mDragInfo in Workspace), we store the View, its cellX & cellY,
2558 // its spanX, spanY, and the screen it is on
2559 // 2. When long clicking on an empty cell in a CellLayout, we save information about the
2560 // cellX and cellY coordinates and which page was clicked. We then set this as a tag on
2561 // the CellLayout that was long clicked
Michael Jurkae5fb0f22011-04-11 13:27:46 -07002562 static final class CellInfo {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 View cell;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002564 int cellX = -1;
2565 int cellY = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 int spanX;
2567 int spanY;
2568 int screen;
Winson Chung3d503fb2011-07-13 17:25:49 -07002569 long container;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002571 @Override
2572 public String toString() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002573 return "Cell[view=" + (cell == null ? "null" : cell.getClass())
2574 + ", x=" + cellX + ", y=" + cellY + "]";
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 }
2576 }
Michael Jurkad771c962011-08-09 15:00:48 -07002577
2578 public boolean lastDownOnOccupiedCell() {
2579 return mLastDownOnOccupiedCell;
2580 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581}