blob: 1643be53772a18b7259d35c0b1a6a026b438c774 [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
Michael Jurka0280c3b2010-09-17 15:00:07 -070019import android.animation.Animator;
Chet Haaseb1254a62010-09-07 13:35:00 -070020import android.animation.AnimatorSet;
Michael Jurka0280c3b2010-09-17 15:00:07 -070021import android.animation.ObjectAnimator;
Patrick Dubroycd68ff52010-10-28 17:57:05 -070022import android.animation.TimeInterpolator;
23import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chung68846fd2010-10-29 11:00:27 -070025import android.app.AlertDialog;
Dianne Hackborn8f573952009-08-10 23:21:09 -070026import android.app.WallpaperManager;
Michael Jurkabed61d22012-02-14 22:51:29 -080027import android.appwidget.AppWidgetHostView;
Romain Guy629de3e2010-01-13 12:20:59 -080028import android.appwidget.AppWidgetManager;
29import android.appwidget.AppWidgetProviderInfo;
Winson Chunga9abd0e2010-10-27 17:18:37 -070030import android.content.ClipData;
31import android.content.ClipDescription;
Adam Powell495f2892010-04-16 16:40:55 -070032import android.content.ComponentName;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.Context;
34import android.content.Intent;
Patrick Dubroy7247f632010-08-04 16:02:59 -070035import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.res.TypedArray;
Joe Onorato4be866d2010-10-10 11:26:02 -070037import android.graphics.Bitmap;
Adam Lesinski6b879f02010-11-04 16:15:23 -070038import android.graphics.Camera;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.graphics.Canvas;
Michael Jurkaa63c4522010-08-19 13:52:27 -070040import android.graphics.Matrix;
Winson Chunga9abd0e2010-10-27 17:18:37 -070041import android.graphics.Paint;
Winson Chungb8c69f32011-10-19 21:36:08 -070042import android.graphics.Point;
Winson Chung70fc4382011-08-08 15:31:33 -070043import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.graphics.Rect;
Winson Chunga9abd0e2010-10-27 17:18:37 -070045import android.graphics.RectF;
Joe Onorato4be866d2010-10-10 11:26:02 -070046import android.graphics.Region.Op;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070047import android.graphics.drawable.Drawable;
Joe Onorato956091b2010-02-19 12:47:40 -080048import android.os.IBinder;
Adam Powell495f2892010-04-16 16:40:55 -070049import android.os.Parcelable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.util.AttributeSet;
Winson Chungb26f3d62011-06-02 10:49:29 -070051import android.util.DisplayMetrics;
Daniel Sandler291ad122010-05-24 16:03:53 -040052import android.util.Log;
Winson Chung580e2772010-11-10 16:03:00 -080053import android.util.Pair;
Winson Chunga34abf82010-11-12 12:10:35 -080054import android.view.Display;
Winson Chunga9abd0e2010-10-27 17:18:37 -070055import android.view.DragEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.view.View;
Winson Chung70442722012-02-10 15:43:22 -080058import android.view.ViewConfiguration;
Winson Chung6e314082011-01-27 16:46:51 -080059import android.view.ViewGroup;
Michael Jurkabed61d22012-02-14 22:51:29 -080060import android.view.View.MeasureSpec;
Patrick Dubroycd68ff52010-10-28 17:57:05 -070061import android.view.animation.DecelerateInterpolator;
Winson Chunga6427b12011-07-27 10:53:39 -070062import android.widget.ImageView;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -070063import android.widget.TextView;
Winson Chunga9abd0e2010-10-27 17:18:37 -070064import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065
Adam Cohen66396872011-04-15 17:50:36 -070066import com.android.launcher.R;
Adam Cohen19072da2011-05-31 14:30:45 -070067import com.android.launcher2.FolderIcon.FolderRingAnimator;
Adam Cohen66396872011-04-15 17:50:36 -070068import com.android.launcher2.InstallWidgetReceiver.WidgetMimeTypeHandlerData;
Romain Guyedcce092010-03-04 13:03:17 -080069
Adam Cohen716b51e2011-06-30 12:09:54 -070070import java.util.ArrayList;
71import java.util.HashSet;
72import java.util.List;
73
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074/**
Michael Jurka0142d492010-08-25 17:46:15 -070075 * The workspace is a wide area with a wallpaper and a finite number of pages.
76 * Each page contains a number of icons, folders or widgets the user can
Winson Chungaafa03c2010-06-11 17:34:16 -070077 * interact with. A workspace is meant to be used with a fixed width only.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 */
Michael Jurka0142d492010-08-25 17:46:15 -070079public class Workspace extends SmoothPagedView
Michael Jurkad74c9842011-07-10 12:44:21 -070080 implements DropTarget, DragSource, DragScroller, View.OnTouchListener,
Michael Jurkabed61d22012-02-14 22:51:29 -080081 DragController.DragListener, LauncherTransitionable {
Romain Guye47f55c2009-11-11 19:21:22 -080082 @SuppressWarnings({"UnusedDeclaration"})
Joe Onorato3a8820b2009-11-10 15:06:42 -080083 private static final String TAG = "Launcher.Workspace";
Michael Jurka0142d492010-08-25 17:46:15 -070084
Adam Cohenf34bab52010-09-30 14:11:56 -070085 // Y rotation to apply to the workspace screens
86 private static final float WORKSPACE_ROTATION = 12.5f;
Adam Cohenb5ba0972011-09-07 18:02:31 -070087 private static final float WORKSPACE_OVERSCROLL_ROTATION = 24f;
Adam Cohencff6af82011-09-13 14:51:53 -070088 private static float CAMERA_DISTANCE = 6500;
Adam Cohena985e592010-09-09 11:23:48 -070089
Adam Cohen68d73932010-11-15 10:50:58 -080090 private static final int CHILDREN_OUTLINE_FADE_OUT_DELAY = 0;
91 private static final int CHILDREN_OUTLINE_FADE_OUT_DURATION = 375;
Winson Chung9171e6d2010-11-17 17:39:27 -080092 private static final int CHILDREN_OUTLINE_FADE_IN_DURATION = 100;
Adam Cohenf34bab52010-09-30 14:11:56 -070093
Winson Chungf135c6c2010-11-18 16:32:08 -080094 private static final int BACKGROUND_FADE_OUT_DURATION = 350;
Adam Cohened51cc92011-08-01 20:28:08 -070095 private static final int ADJACENT_SCREEN_DROP_DURATION = 300;
Adam Cohen265b9a62011-12-07 14:37:18 -080096 private static final int FLING_THRESHOLD_VELOCITY = 500;
Adam Cohened51cc92011-08-01 20:28:08 -070097
Winson Chung9171e6d2010-11-17 17:39:27 -080098 // These animators are used to fade the children's outlines
99 private ObjectAnimator mChildrenOutlineFadeInAnimation;
100 private ObjectAnimator mChildrenOutlineFadeOutAnimation;
101 private float mChildrenOutlineAlpha = 0;
102
103 // These properties refer to the background protection gradient used for AllApps and Customize
Michael Jurkae0f5a612011-02-07 16:45:41 -0800104 private ValueAnimator mBackgroundFadeInAnimation;
105 private ValueAnimator mBackgroundFadeOutAnimation;
Winson Chung9171e6d2010-11-17 17:39:27 -0800106 private Drawable mBackground;
Michael Jurka25356e72011-03-03 14:53:11 -0800107 boolean mDrawBackground = true;
Adam Cohenf34bab52010-09-30 14:11:56 -0700108 private float mBackgroundAlpha = 0;
Adam Cohen68d73932010-11-15 10:50:58 -0800109 private float mOverScrollMaxBackgroundAlpha = 0.0f;
Adam Cohene0f66b52010-11-23 15:06:07 -0800110 private int mOverScrollPageIndex = -1;
Adam Cohenf34bab52010-09-30 14:11:56 -0700111
Adam Cohenbeff8c62011-08-31 17:46:01 -0700112 private float mWallpaperScrollRatio = 1.0f;
113
Dianne Hackborn8f573952009-08-10 23:21:09 -0700114 private final WallpaperManager mWallpaperManager;
Michael Jurka9c6fbed2011-03-02 17:41:34 -0800115 private IBinder mWindowToken;
Michael Jurka16706982011-09-26 16:39:22 -0700116 private static final float WALLPAPER_SCREENS_SPAN = 2f;
Winson Chungaafa03c2010-06-11 17:34:16 -0700117
Michael Jurka0142d492010-08-25 17:46:15 -0700118 private int mDefaultPage;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 /**
121 * CellInfo for the cell that is currently being dragged
122 */
123 private CellLayout.CellInfo mDragInfo;
Winson Chungaafa03c2010-06-11 17:34:16 -0700124
Jeff Sharkey70864282009-04-07 21:08:40 -0700125 /**
126 * Target drop area calculated during last acceptDrop call.
127 */
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128 private int[] mTargetCell = new int[2];
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700130 /**
131 * The CellLayout that is currently being dragged over
132 */
133 private CellLayout mDragTargetLayout = null;
Adam Cohen8a18afc2011-12-13 15:57:01 -0800134 private boolean mDragHasEnteredWorkspace = false;
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700135
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private Launcher mLauncher;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800137 private IconCache mIconCache;
Joe Onorato00acb122009-08-04 16:04:30 -0400138 private DragController mDragController;
Winson Chungaafa03c2010-06-11 17:34:16 -0700139
Michael Jurka4516c112010-10-07 15:13:47 -0700140 // These are temporary variables to prevent having to allocate a new object just to
141 // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 private int[] mTempCell = new int[2];
Jeff Sharkey70864282009-04-07 21:08:40 -0700143 private int[] mTempEstimate = new int[2];
Adam Cohene3e27a82011-04-15 12:07:39 -0700144 private float[] mDragViewVisualCenter = new float[2];
Michael Jurkaa63c4522010-08-19 13:52:27 -0700145 private float[] mTempDragCoordinates = new float[2];
Michael Jurka4516c112010-10-07 15:13:47 -0700146 private float[] mTempCellLayoutCenterCoordinates = new float[2];
Michael Jurkaa63c4522010-08-19 13:52:27 -0700147 private float[] mTempDragBottomRightCoordinates = new float[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -0700148 private Matrix mTempInverseMatrix = new Matrix();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Michael Jurkac2f7f472010-12-14 15:34:42 -0800150 private SpringLoadedDragController mSpringLoadedDragController;
Winson Chungb26f3d62011-06-02 10:49:29 -0700151 private float mSpringLoadedShrinkFactor;
Michael Jurkad3ef3062010-11-23 16:23:58 -0800152
Adam Cohend22015c2010-07-26 22:02:18 -0700153 private static final int DEFAULT_CELL_COUNT_X = 4;
154 private static final int DEFAULT_CELL_COUNT_Y = 4;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Patrick Dubroy1262e362010-10-06 15:49:50 -0700156 // State variable that indicates whether the pages are small (ie when you're
Michael Jurkadee05892010-07-27 10:01:56 -0700157 // in all apps or customize mode)
Michael Jurkad74c9842011-07-10 12:44:21 -0700158
159 enum State { NORMAL, SPRING_LOADED, SMALL };
Adam Cohen7777d962011-08-18 18:58:38 -0700160 private State mState = State.NORMAL;
Michael Jurkad74c9842011-07-10 12:44:21 -0700161 private boolean mIsSwitchingState = false;
Michael Jurkad74c9842011-07-10 12:44:21 -0700162
Michael Jurkad74c9842011-07-10 12:44:21 -0700163 boolean mAnimatingViewIntoPlace = false;
164 boolean mIsDragOccuring = false;
165 boolean mChildrenLayersEnabled = true;
Michael Jurkadee05892010-07-27 10:01:56 -0700166
Patrick Dubroy54fa3b92010-11-17 12:18:45 -0800167 /** Is the user is dragging an item near the edge of a page? */
Patrick Dubroy1262e362010-10-06 15:49:50 -0700168 private boolean mInScrollArea = false;
169
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700170 private final HolographicOutlineHelper mOutlineHelper = new HolographicOutlineHelper();
Joe Onorato4be866d2010-10-10 11:26:02 -0700171 private Bitmap mDragOutline = null;
Patrick Dubroy8e58e912010-10-14 13:21:48 -0700172 private final Rect mTempRect = new Rect();
173 private final int[] mTempXY = new int[2];
Adam Cohen21b41102011-11-01 17:29:52 -0700174 private float mOverscrollFade = 0;
Joe Onorato4be866d2010-10-10 11:26:02 -0700175
Winson Chunga9abd0e2010-10-27 17:18:37 -0700176 // Paint used to draw external drop outline
177 private final Paint mExternalDragOutlinePaint = new Paint();
178
Adam Lesinski6b879f02010-11-04 16:15:23 -0700179 // Camera and Matrix used to determine the final position of a neighboring CellLayout
180 private final Matrix mMatrix = new Matrix();
181 private final Camera mCamera = new Camera();
182 private final float mTempFloat2[] = new float[2];
183
Michael Jurkac5b262c2011-01-12 20:24:50 -0800184 enum WallpaperVerticalOffset { TOP, MIDDLE, BOTTOM };
185 int mWallpaperWidth;
186 int mWallpaperHeight;
Michael Jurkaab1983f2011-01-18 15:50:17 -0800187 WallpaperOffsetInterpolator mWallpaperOffset;
Michael Jurkac5b262c2011-01-12 20:24:50 -0800188 boolean mUpdateWallpaperOffsetImmediately = false;
Adam Cohen26976d92011-03-22 15:33:33 -0700189 private Runnable mDelayedResizeRunnable;
Adam Cohencff6af82011-09-13 14:51:53 -0700190 private int mDisplayWidth;
191 private int mDisplayHeight;
Adam Cohencff6af82011-09-13 14:51:53 -0700192 private int mWallpaperTravelWidth;
Michael Jurkac5b262c2011-01-12 20:24:50 -0800193
Adam Cohen19072da2011-05-31 14:30:45 -0700194 // Variables relating to the creation of user folders by hovering shortcuts over shortcuts
Adam Cohenc0dcf592011-06-01 15:30:43 -0700195 private static final int FOLDER_CREATION_TIMEOUT = 250;
Adam Cohen19072da2011-05-31 14:30:45 -0700196 private final Alarm mFolderCreationAlarm = new Alarm();
197 private FolderRingAnimator mDragFolderRingAnimator = null;
198 private View mLastDragOverView = null;
199 private boolean mCreateUserFolderOnDrop = false;
Adam Cohen073a46f2011-05-17 16:28:09 -0700200
Adam Cohenf8d28232011-02-01 21:47:00 -0800201 // Variables relating to touch disambiguation (scrolling workspace vs. scrolling a widget)
202 private float mXDown;
203 private float mYDown;
204 final static float START_DAMPING_TOUCH_SLOP_ANGLE = (float) Math.PI / 6;
205 final static float MAX_SWIPE_ANGLE = (float) Math.PI / 3;
206 final static float TOUCH_SLOP_DAMPING_FACTOR = 4;
207
Adam Cohened66b2b2012-01-23 17:28:51 -0800208 // Relating to the animation of items being dropped externally
209 public static final int ANIMATE_INTO_POSITION = 0;
210 public static final int COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION = 1;
211 public static final int CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION = 2;
212
Winson Chung867ca622012-02-21 15:48:35 -0800213 // Relating to workspace drag fade out
214 private float mDragFadeOutAlpha;
215 private int mDragFadeOutDuration;
216
Adam Cohen4b285c52011-07-21 14:24:06 -0700217 // These variables are used for storing the initial and final values during workspace animations
Adam Cohened51cc92011-08-01 20:28:08 -0700218 private int mSavedScrollX;
219 private float mSavedRotationY;
220 private float mSavedTranslationX;
Adam Cohen4b285c52011-07-21 14:24:06 -0700221 private float mCurrentScaleX;
222 private float mCurrentScaleY;
223 private float mCurrentRotationY;
224 private float mCurrentTranslationX;
225 private float mCurrentTranslationY;
226 private float[] mOldTranslationXs;
227 private float[] mOldTranslationYs;
228 private float[] mOldScaleXs;
229 private float[] mOldScaleYs;
230 private float[] mOldBackgroundAlphas;
231 private float[] mOldBackgroundAlphaMultipliers;
232 private float[] mOldAlphas;
233 private float[] mOldRotationYs;
234 private float[] mNewTranslationXs;
235 private float[] mNewTranslationYs;
236 private float[] mNewScaleXs;
237 private float[] mNewScaleYs;
238 private float[] mNewBackgroundAlphas;
239 private float[] mNewBackgroundAlphaMultipliers;
240 private float[] mNewAlphas;
241 private float[] mNewRotationYs;
Winson Chung70442722012-02-10 15:43:22 -0800242 private float mTransitionProgress;
Adam Cohen4b285c52011-07-21 14:24:06 -0700243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 /**
245 * Used to inflate the Workspace from XML.
246 *
247 * @param context The application's context.
Michael Jurka0142d492010-08-25 17:46:15 -0700248 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 */
250 public Workspace(Context context, AttributeSet attrs) {
251 this(context, attrs, 0);
252 }
253
254 /**
255 * Used to inflate the Workspace from XML.
256 *
257 * @param context The application's context.
Michael Jurka0142d492010-08-25 17:46:15 -0700258 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 * @param defStyle Unused.
260 */
261 public Workspace(Context context, AttributeSet attrs, int defStyle) {
262 super(context, attrs, defStyle);
Michael Jurka0142d492010-08-25 17:46:15 -0700263 mContentIsRefreshable = false;
Michael Jurka5f1c5092010-09-03 14:15:02 -0700264
Winson Chungf0ea4d32011-06-06 14:27:16 -0700265 // With workspace, data is available straight from the get-go
266 setDataIsReady();
267
Winson Chung867ca622012-02-21 15:48:35 -0800268 final Resources res = getResources();
269 mFadeInAdjacentScreens = res.getBoolean(R.bool.config_workspaceFadeAdjacentScreens);
270 mDragFadeOutAlpha = res.getInteger(R.integer.config_dragFadeOutAlpha) / 100f;
271 mDragFadeOutDuration = res.getInteger(R.integer.config_dragFadeOutDuration);
Dianne Hackborn8f573952009-08-10 23:21:09 -0700272 mWallpaperManager = WallpaperManager.getInstance(context);
Winson Chungaafa03c2010-06-11 17:34:16 -0700273
Michael Jurkaf6440da2011-04-05 14:50:34 -0700274 int cellCountX = DEFAULT_CELL_COUNT_X;
275 int cellCountY = DEFAULT_CELL_COUNT_Y;
276
Winson Chungaafa03c2010-06-11 17:34:16 -0700277 TypedArray a = context.obtainStyledAttributes(attrs,
278 R.styleable.Workspace, defStyle, 0);
Michael Jurkaf6440da2011-04-05 14:50:34 -0700279
280 if (LauncherApplication.isScreenLarge()) {
Michael Jurkaf61249b2011-05-27 16:27:15 -0700281 // Determine number of rows/columns dynamically
282 // TODO: This code currently fails on tablets with an aspect ratio < 1.3.
283 // Around that ratio we should make cells the same size in portrait and
284 // landscape
Michael Jurkaf6440da2011-04-05 14:50:34 -0700285 TypedArray actionBarSizeTypedArray =
286 context.obtainStyledAttributes(new int[] { android.R.attr.actionBarSize });
Winson Chunga61dc422012-02-14 14:45:18 -0800287 DisplayMetrics displayMetrics = res.getDisplayMetrics();
Michael Jurkaf61249b2011-05-27 16:27:15 -0700288 final float actionBarHeight = actionBarSizeTypedArray.getDimension(0, 0f);
289 final float systemBarHeight = res.getDimension(R.dimen.status_bar_height);
Winson Chunga61dc422012-02-14 14:45:18 -0800290 final float smallestScreenDim = res.getConfiguration().smallestScreenWidthDp *
291 displayMetrics.density;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700292
Michael Jurkaf61249b2011-05-27 16:27:15 -0700293 cellCountX = 1;
294 while (CellLayout.widthInPortrait(res, cellCountX + 1) <= smallestScreenDim) {
295 cellCountX++;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700296 }
Michael Jurkaf6440da2011-04-05 14:50:34 -0700297
Michael Jurkaf61249b2011-05-27 16:27:15 -0700298 cellCountY = 1;
299 while (actionBarHeight + CellLayout.heightInLandscape(res, cellCountY + 1)
300 <= smallestScreenDim - systemBarHeight) {
301 cellCountY++;
Michael Jurkaf6440da2011-04-05 14:50:34 -0700302 }
Michael Jurkaf6440da2011-04-05 14:50:34 -0700303 }
304
Winson Chungb26f3d62011-06-02 10:49:29 -0700305 mSpringLoadedShrinkFactor =
306 res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
307
Michael Jurkaf6440da2011-04-05 14:50:34 -0700308 // if the value is manually specified, use that instead
309 cellCountX = a.getInt(R.styleable.Workspace_cellCountX, cellCountX);
310 cellCountY = a.getInt(R.styleable.Workspace_cellCountY, cellCountY);
Michael Jurka0142d492010-08-25 17:46:15 -0700311 mDefaultPage = a.getInt(R.styleable.Workspace_defaultScreen, 1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 a.recycle();
313
Adam Cohend22015c2010-07-26 22:02:18 -0700314 LauncherModel.updateWorkspaceLayoutCells(cellCountX, cellCountY);
Joe Onorato0d44e942009-11-16 18:20:51 -0800315 setHapticFeedbackEnabled(false);
Michael Jurka0142d492010-08-25 17:46:15 -0700316
Adam Cohencff6af82011-09-13 14:51:53 -0700317 mLauncher = (Launcher) context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 initWorkspace();
Winson Chungc35afb22011-02-23 13:01:49 -0800319
320 // Disable multitouch across the workspace/all apps/customize tray
321 setMotionEventSplittingEnabled(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
323
Michael Jurka038f9d82011-11-03 13:50:45 -0700324 // estimate the size of a widget with spans hSpan, vSpan. return MAX_VALUE for each
325 // dimension if unsuccessful
326 public int[] estimateItemSize(int hSpan, int vSpan,
327 PendingAddItemInfo pendingItemInfo, boolean springLoaded) {
328 int[] size = new int[2];
329 if (getChildCount() > 0) {
330 CellLayout cl = (CellLayout) mLauncher.getWorkspace().getChildAt(0);
331 RectF r = estimateItemPosition(cl, pendingItemInfo, 0, 0, hSpan, vSpan);
332 size[0] = (int) r.width();
333 size[1] = (int) r.height();
334 if (springLoaded) {
335 size[0] *= mSpringLoadedShrinkFactor;
336 size[1] *= mSpringLoadedShrinkFactor;
337 }
338 return size;
339 } else {
340 size[0] = Integer.MAX_VALUE;
341 size[1] = Integer.MAX_VALUE;
342 return size;
343 }
344 }
345 public RectF estimateItemPosition(CellLayout cl, ItemInfo pendingInfo,
346 int hCell, int vCell, int hSpan, int vSpan) {
347 RectF r = new RectF();
348 cl.cellToRect(hCell, vCell, hSpan, vSpan, r);
Michael Jurka038f9d82011-11-03 13:50:45 -0700349 return r;
350 }
351
Michael Jurkac1f5d262011-09-30 19:32:27 -0700352 public void buildPageHardwareLayers() {
353 if (getWindowToken() != null) {
354 final int childCount = getChildCount();
355 for (int i = 0; i < childCount; i++) {
356 CellLayout cl = (CellLayout) getChildAt(i);
357 cl.buildChildrenLayer();
358 }
359 }
360 }
361
Michael Jurkad74c9842011-07-10 12:44:21 -0700362 public void onDragStart(DragSource source, Object info, int dragAction) {
363 mIsDragOccuring = true;
364 updateChildrenLayersEnabled();
Adam Cohen446e9402011-09-15 18:21:21 -0700365 mLauncher.lockScreenOrientationOnLargeUI();
Winson Chung867ca622012-02-21 15:48:35 -0800366
367 // Fade out the workspace slightly to highlight the currently dragging item
368 animate().alpha(mDragFadeOutAlpha).setDuration(mDragFadeOutDuration).start();
Michael Jurkad74c9842011-07-10 12:44:21 -0700369 }
370
371 public void onDragEnd() {
372 mIsDragOccuring = false;
373 updateChildrenLayersEnabled();
Adam Cohen446e9402011-09-15 18:21:21 -0700374 mLauncher.unlockScreenOrientationOnLargeUI();
Winson Chung867ca622012-02-21 15:48:35 -0800375
376 // Fade the workspace back in after we have completed dragging
377 animate().alpha(1f).setDuration(mDragFadeOutDuration).start();
Michael Jurkad74c9842011-07-10 12:44:21 -0700378 }
379
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 /**
381 * Initializes various states for this workspace.
382 */
Michael Jurka0142d492010-08-25 17:46:15 -0700383 protected void initWorkspace() {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800384 Context context = getContext();
Michael Jurka0142d492010-08-25 17:46:15 -0700385 mCurrentPage = mDefaultPage;
386 Launcher.setScreen(mCurrentPage);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800387 LauncherApplication app = (LauncherApplication)context.getApplicationContext();
388 mIconCache = app.getIconCache();
Winson Chunga9abd0e2010-10-27 17:18:37 -0700389 mExternalDragOutlinePaint.setAntiAlias(true);
Patrick Dubroycd68ff52010-10-28 17:57:05 -0700390 setWillNotDraw(false);
Adam Cohen7777d962011-08-18 18:58:38 -0700391 setChildrenDrawnWithCacheEnabled(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392
Winson Chungb4b7fa72010-11-18 14:38:53 -0800393 try {
394 final Resources res = getResources();
Winson Chungfd3385f2011-06-15 19:51:24 -0700395 mBackground = res.getDrawable(R.drawable.apps_customize_bg);
Winson Chungb4b7fa72010-11-18 14:38:53 -0800396 } catch (Resources.NotFoundException e) {
397 // In this case, we will skip drawing background protection
398 }
Winson Chung9171e6d2010-11-17 17:39:27 -0800399
Michael Jurkaab1983f2011-01-18 15:50:17 -0800400 mWallpaperOffset = new WallpaperOffsetInterpolator();
Adam Cohencff6af82011-09-13 14:51:53 -0700401 Display display = mLauncher.getWindowManager().getDefaultDisplay();
402 mDisplayWidth = display.getWidth();
403 mDisplayHeight = display.getHeight();
404 mWallpaperTravelWidth = (int) (mDisplayWidth *
405 wallpaperTravelToScreenWidthRatio(mDisplayWidth, mDisplayHeight));
Adam Cohen265b9a62011-12-07 14:37:18 -0800406
407 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity);
408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 }
410
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 @Override
Adam Cohenf34bab52010-09-30 14:11:56 -0700412 protected int getScrollMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -0700413 return SmoothPagedView.X_LARGE_MODE;
Adam Cohenf34bab52010-09-30 14:11:56 -0700414 }
415
Michael Jurka08ee7702011-08-11 16:53:35 -0700416 @Override
417 protected void onViewAdded(View child) {
418 super.onViewAdded(child);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 if (!(child instanceof CellLayout)) {
420 throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
421 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700422 CellLayout cl = ((CellLayout) child);
423 cl.setOnInterceptTouchListener(this);
Adam Cohen2801caf2011-05-13 20:57:39 -0700424 cl.setClickable(true);
425 cl.enableHardwareLayers();
Michael Jurkae5fb0f22011-04-11 13:27:46 -0700426 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700427
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 /**
429 * @return The open folder on the current screen, or null if there is none
430 */
431 Folder getOpenFolder() {
Adam Cohen716b51e2011-06-30 12:09:54 -0700432 DragLayer dragLayer = mLauncher.getDragLayer();
Adam Cohen8e776a62011-06-28 18:10:06 -0700433 int count = dragLayer.getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 for (int i = 0; i < count; i++) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700435 View child = dragLayer.getChildAt(i);
Winson Chungaafa03c2010-06-11 17:34:16 -0700436 if (child instanceof Folder) {
437 Folder folder = (Folder) child;
438 if (folder.getInfo().opened)
439 return folder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 }
441 }
442 return null;
443 }
444
Patrick Dubroya0aa0122011-02-24 11:42:23 -0800445 boolean isTouchActive() {
446 return mTouchState != TOUCH_STATE_REST;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 }
448
449 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 * Adds the specified child in the specified screen. The position and dimension of
451 * the child are defined by x, y, spanX and spanY.
452 *
453 * @param child The child to add in one of the workspace's screens.
454 * @param screen The screen in which to add the child.
455 * @param x The X position of the child in the screen's grid.
456 * @param y The Y position of the child in the screen's grid.
457 * @param spanX The number of cells spanned horizontally by the child.
458 * @param spanY The number of cells spanned vertically by the child.
459 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700460 void addInScreen(View child, long container, int screen, int x, int y, int spanX, int spanY) {
461 addInScreen(child, container, screen, x, y, spanX, spanY, false);
Winson Chungaafa03c2010-06-11 17:34:16 -0700462 }
463
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 /**
465 * Adds the specified child in the specified screen. The position and dimension of
466 * the child are defined by x, y, spanX and spanY.
467 *
468 * @param child The child to add in one of the workspace's screens.
469 * @param screen The screen in which to add the child.
470 * @param x The X position of the child in the screen's grid.
471 * @param y The Y position of the child in the screen's grid.
472 * @param spanX The number of cells spanned horizontally by the child.
473 * @param spanY The number of cells spanned vertically by the child.
474 * @param insert When true, the child is inserted at the beginning of the children list.
475 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700476 void addInScreen(View child, long container, int screen, int x, int y, int spanX, int spanY,
477 boolean insert) {
478 if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
479 if (screen < 0 || screen >= getChildCount()) {
480 Log.e(TAG, "The screen must be >= 0 and < " + getChildCount()
481 + " (was " + screen + "); skipping child");
482 return;
483 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 }
485
Winson Chung3d503fb2011-07-13 17:25:49 -0700486 final CellLayout layout;
487 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
488 layout = mLauncher.getHotseat().getLayout();
Winson Chung4d279d92011-07-21 11:46:32 -0700489 child.setOnKeyListener(null);
Winson Chung3d503fb2011-07-13 17:25:49 -0700490
Adam Cohen099f60d2011-08-23 21:07:26 -0700491 // Hide folder title in the hotseat
492 if (child instanceof FolderIcon) {
493 ((FolderIcon) child).setTextVisible(false);
494 }
495
Winson Chung3d503fb2011-07-13 17:25:49 -0700496 if (screen < 0) {
497 screen = mLauncher.getHotseat().getOrderInHotseat(x, y);
498 } else {
499 // Note: We do this to ensure that the hotseat is always laid out in the orientation
500 // of the hotseat in order regardless of which orientation they were added
501 x = mLauncher.getHotseat().getCellXFromOrder(screen);
502 y = mLauncher.getHotseat().getCellYFromOrder(screen);
503 }
504 } else {
Adam Cohen099f60d2011-08-23 21:07:26 -0700505 // Show folder title if not in the hotseat
506 if (child instanceof FolderIcon) {
507 ((FolderIcon) child).setTextVisible(true);
508 }
509
Winson Chung3d503fb2011-07-13 17:25:49 -0700510 layout = (CellLayout) getChildAt(screen);
Adam Cohenac56cff2011-09-28 20:45:37 -0700511 child.setOnKeyListener(new IconKeyEventListener());
Winson Chung3d503fb2011-07-13 17:25:49 -0700512 }
513
Adam Cohened66b2b2012-01-23 17:28:51 -0800514 LayoutParams genericLp = child.getLayoutParams();
515 CellLayout.LayoutParams lp;
516 if (genericLp == null || !(genericLp instanceof CellLayout.LayoutParams)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517 lp = new CellLayout.LayoutParams(x, y, spanX, spanY);
518 } else {
Adam Cohened66b2b2012-01-23 17:28:51 -0800519 lp = (CellLayout.LayoutParams) genericLp;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 lp.cellX = x;
521 lp.cellY = y;
522 lp.cellHSpan = spanX;
523 lp.cellVSpan = spanY;
524 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700525
Adam Cohen7f4eabe2011-04-21 16:19:16 -0700526 if (spanX < 0 && spanY < 0) {
527 lp.isLockedToGrid = false;
528 }
529
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700530 // Get the canonical child id to uniquely represent this view in this screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700531 int childId = LauncherModel.getCellLayoutChildId(container, screen, x, y, spanX, spanY);
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -0700532 boolean markCellsAsOccupied = !(child instanceof Folder);
Winson Chung3d503fb2011-07-13 17:25:49 -0700533 if (!layout.addViewToCellLayout(child, insert ? 0 : -1, childId, lp, markCellsAsOccupied)) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700534 // TODO: This branch occurs when the workspace is adding views
535 // outside of the defined grid
Patrick Dubroy6569f2c2010-07-12 14:25:18 -0700536 // maybe we should be deleting these items from the LauncherModel?
Winson Chungaafa03c2010-06-11 17:34:16 -0700537 Log.w(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout");
538 }
539
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 if (!(child instanceof Folder)) {
Joe Onorato0d44e942009-11-16 18:20:51 -0800541 child.setHapticFeedbackEnabled(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 child.setOnLongClickListener(mLongClickListener);
543 }
Joe Onorato00acb122009-08-04 16:04:30 -0400544 if (child instanceof DropTarget) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700545 mDragController.addDropTarget((DropTarget) child);
Joe Onorato00acb122009-08-04 16:04:30 -0400546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 }
548
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800549 /**
550 * Check if the point (x, y) hits a given page.
551 */
552 private boolean hitsPage(int index, float x, float y) {
553 final View page = getChildAt(index);
554 if (page != null) {
555 float[] localXY = { x, y };
556 mapPointFromSelfToChild(page, localXY);
557 return (localXY[0] >= 0 && localXY[0] < page.getWidth()
558 && localXY[1] >= 0 && localXY[1] < page.getHeight());
559 }
560 return false;
561 }
562
563 @Override
564 protected boolean hitsPreviousPage(float x, float y) {
565 // mNextPage is set to INVALID_PAGE whenever we are stationary.
566 // Calculating "next page" this way ensures that you scroll to whatever page you tap on
567 final int current = (mNextPage == INVALID_PAGE) ? mCurrentPage : mNextPage;
Michael Jurkab72094f2011-09-14 14:53:19 -0700568
569 // Only allow tap to next page on large devices, where there's significant margin outside
570 // the active workspace
571 return LauncherApplication.isScreenLarge() && hitsPage(current - 1, x, y);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800572 }
573
574 @Override
575 protected boolean hitsNextPage(float x, float y) {
576 // mNextPage is set to INVALID_PAGE whenever we are stationary.
577 // Calculating "next page" this way ensures that you scroll to whatever page you tap on
578 final int current = (mNextPage == INVALID_PAGE) ? mCurrentPage : mNextPage;
Michael Jurkab72094f2011-09-14 14:53:19 -0700579
580 // Only allow tap to next page on large devices, where there's significant margin outside
581 // the active workspace
582 return LauncherApplication.isScreenLarge() && hitsPage(current + 1, x, y);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -0800583 }
584
Patrick Dubroye708c522011-03-01 16:03:43 -0800585 /**
586 * Called directly from a CellLayout (not by the framework), after we've been added as a
587 * listener via setOnInterceptTouchEventListener(). This allows us to tell the CellLayout
588 * that it should intercept touch events, which is not something that is normally supported.
589 */
590 @Override
Michael Jurkadee05892010-07-27 10:01:56 -0700591 public boolean onTouch(View v, MotionEvent event) {
Winson Chung70442722012-02-10 15:43:22 -0800592 return (isSmall() || !isFinishedSwitchingState());
Patrick Dubroye708c522011-03-01 16:03:43 -0800593 }
594
Adam Cohenfc53cd22011-07-20 15:45:11 -0700595 public boolean isSwitchingState() {
596 return mIsSwitchingState;
597 }
598
Winson Chung70442722012-02-10 15:43:22 -0800599 /** This differs from isSwitchingState in that we take into account how far the transition
600 * has completed. */
601 private boolean isFinishedSwitchingState() {
602 return !mIsSwitchingState || (mTransitionProgress > 0.5f);
603 }
604
Adam Cohended9f8d2010-11-03 13:25:16 -0700605 protected void onWindowVisibilityChanged (int visibility) {
606 mLauncher.onWindowVisibilityChanged(visibility);
607 }
608
Michael Jurka5f1c5092010-09-03 14:15:02 -0700609 @Override
610 public boolean dispatchUnhandledMove(View focused, int direction) {
Winson Chung70442722012-02-10 15:43:22 -0800611 if (isSmall() || !isFinishedSwitchingState()) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700612 // when the home screens are shrunken, shouldn't allow side-scrolling
613 return false;
614 }
615 return super.dispatchUnhandledMove(focused, direction);
616 }
617
618 @Override
619 public boolean onInterceptTouchEvent(MotionEvent ev) {
Michael Jurkad771c962011-08-09 15:00:48 -0700620 switch (ev.getAction() & MotionEvent.ACTION_MASK) {
621 case MotionEvent.ACTION_DOWN:
Adam Cohenf8d28232011-02-01 21:47:00 -0800622 mXDown = ev.getX();
623 mYDown = ev.getY();
Michael Jurkad771c962011-08-09 15:00:48 -0700624 break;
625 case MotionEvent.ACTION_POINTER_UP:
626 case MotionEvent.ACTION_UP:
627 if (mTouchState == TOUCH_STATE_REST) {
628 final CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage);
629 if (!currentPage.lastDownOnOccupiedCell()) {
630 onWallpaperTap(ev);
631 }
632 }
Adam Cohenf8d28232011-02-01 21:47:00 -0800633 }
Michael Jurka5f1c5092010-09-03 14:15:02 -0700634 return super.onInterceptTouchEvent(ev);
635 }
636
Michael Jurka1adf5392010-10-18 18:10:22 -0700637 @Override
638 protected void determineScrollingStart(MotionEvent ev) {
Winson Chung70442722012-02-10 15:43:22 -0800639 if (isSmall()) return;
640 if (!isFinishedSwitchingState()) return;
Adam Cohenf8d28232011-02-01 21:47:00 -0800641
Winson Chung70442722012-02-10 15:43:22 -0800642 float deltaX = Math.abs(ev.getX() - mXDown);
643 float deltaY = Math.abs(ev.getY() - mYDown);
Adam Cohenf8d28232011-02-01 21:47:00 -0800644
Winson Chung70442722012-02-10 15:43:22 -0800645 if (Float.compare(deltaX, 0f) == 0) return;
Adam Cohenf8d28232011-02-01 21:47:00 -0800646
Winson Chung70442722012-02-10 15:43:22 -0800647 float slope = deltaY / deltaX;
648 float theta = (float) Math.atan(slope);
Adam Cohenf8d28232011-02-01 21:47:00 -0800649
Winson Chung70442722012-02-10 15:43:22 -0800650 if (deltaX > mTouchSlop || deltaY > mTouchSlop) {
651 cancelCurrentPageLongPress();
652 }
653
654 if (theta > MAX_SWIPE_ANGLE) {
655 // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace
656 return;
657 } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) {
658 // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to
659 // increase the touch slop to make it harder to begin scrolling the workspace. This
660 // results in vertically scrolling widgets to more easily. The higher the angle, the
661 // more we increase touch slop.
662 theta -= START_DAMPING_TOUCH_SLOP_ANGLE;
663 float extraRatio = (float)
664 Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE)));
665 super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio);
666 } else {
667 // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special
668 super.determineScrollingStart(ev);
Adam Cohenf8d28232011-02-01 21:47:00 -0800669 }
Michael Jurka1adf5392010-10-18 18:10:22 -0700670 }
671
Winson Chung007c6982011-06-14 13:27:53 -0700672 @Override
673 protected boolean isScrollingIndicatorEnabled() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700674 return mState != State.SPRING_LOADED;
Winson Chung007c6982011-06-14 13:27:53 -0700675 }
676
Patrick Dubroy1262e362010-10-06 15:49:50 -0700677 protected void onPageBeginMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700678 super.onPageBeginMoving();
679
Michael Jurkad74c9842011-07-10 12:44:21 -0700680 if (isHardwareAccelerated()) {
681 updateChildrenLayersEnabled();
Michael Jurka0142d492010-08-25 17:46:15 -0700682 } else {
Michael Jurkad74c9842011-07-10 12:44:21 -0700683 if (mNextPage != INVALID_PAGE) {
684 // we're snapping to a particular screen
685 enableChildrenCache(mCurrentPage, mNextPage);
686 } else {
687 // this is when user is actively dragging a particular screen, they might
688 // swipe it either left or right (but we won't advance by more than one screen)
689 enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1);
690 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 }
Winson Chung007c6982011-06-14 13:27:53 -0700692
693 // Only show page outlines as we pan if we are on large screen
694 if (LauncherApplication.isScreenLarge()) {
695 showOutlines();
696 }
Winson Chung1afedc32012-01-23 16:14:56 -0800697
Winson Chung70442722012-02-10 15:43:22 -0800698 // If we are not fading in adjacent screens, we still need to restore the alpha in case the
699 // user scrolls while we are transitioning (should not affect dispatchDraw optimizations)
700 if (!mFadeInAdjacentScreens) {
701 for (int i = 0; i < getChildCount(); ++i) {
702 getPageAt(i).setAlpha(1f);
703 }
704 }
705
Winson Chung1afedc32012-01-23 16:14:56 -0800706 // Show the scroll indicator as you pan the page
707 showScrollingIndicator(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 }
709
Patrick Dubroy1262e362010-10-06 15:49:50 -0700710 protected void onPageEndMoving() {
Winson Chung007c6982011-06-14 13:27:53 -0700711 super.onPageEndMoving();
712
Michael Jurkad74c9842011-07-10 12:44:21 -0700713 if (isHardwareAccelerated()) {
714 updateChildrenLayersEnabled();
715 } else {
716 clearChildrenCache();
717 }
718
Winson Chung3bc21c32012-01-20 13:59:18 -0800719
720 if (mDragController.isDragging()) {
721 if (isSmall()) {
722 // If we are in springloaded mode, then force an event to check if the current touch
723 // is under a new page (to scroll to)
724 mDragController.forceMoveEvent();
725 }
726 } else {
727 // If we are not mid-dragging, hide the page outlines if we are on a large screen
Winson Chung007c6982011-06-14 13:27:53 -0700728 if (LauncherApplication.isScreenLarge()) {
729 hideOutlines();
730 }
Winson Chung1afedc32012-01-23 16:14:56 -0800731
732 // Hide the scroll indicator as you pan the page
733 if (!mDragController.isDragging()) {
734 hideScrollingIndicator(false);
735 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700736 }
Adam Cohen68d73932010-11-15 10:50:58 -0800737 mOverScrollMaxBackgroundAlpha = 0.0f;
Adam Cohene0f66b52010-11-23 15:06:07 -0800738 mOverScrollPageIndex = -1;
Adam Cohen26976d92011-03-22 15:33:33 -0700739
740 if (mDelayedResizeRunnable != null) {
741 mDelayedResizeRunnable.run();
742 mDelayedResizeRunnable = null;
743 }
Michael Jurka0142d492010-08-25 17:46:15 -0700744 }
745
746 @Override
747 protected void notifyPageSwitchListener() {
748 super.notifyPageSwitchListener();
Michael Jurka0142d492010-08-25 17:46:15 -0700749 Launcher.setScreen(mCurrentPage);
750 };
751
Michael Jurkac5b262c2011-01-12 20:24:50 -0800752 // As a ratio of screen height, the total distance we want the parallax effect to span
Michael Jurkac5b262c2011-01-12 20:24:50 -0800753 // horizontally
754 private float wallpaperTravelToScreenWidthRatio(int width, int height) {
755 float aspectRatio = width / (float) height;
756
757 // At an aspect ratio of 16/10, the wallpaper parallax effect should span 1.5 * screen width
758 // At an aspect ratio of 10/16, the wallpaper parallax effect should span 1.2 * screen width
759 // We will use these two data points to extrapolate how much the wallpaper parallax effect
760 // to span (ie travel) at any aspect ratio:
761
762 final float ASPECT_RATIO_LANDSCAPE = 16/10f;
763 final float ASPECT_RATIO_PORTRAIT = 10/16f;
764 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE = 1.5f;
765 final float WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT = 1.2f;
766
767 // To find out the desired width at different aspect ratios, we use the following two
768 // formulas, where the coefficient on x is the aspect ratio (width/height):
769 // (16/10)x + y = 1.5
770 // (10/16)x + y = 1.2
771 // We solve for x and y and end up with a final formula:
772 final float x =
773 (WALLPAPER_WIDTH_TO_SCREEN_RATIO_LANDSCAPE - WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT) /
774 (ASPECT_RATIO_LANDSCAPE - ASPECT_RATIO_PORTRAIT);
775 final float y = WALLPAPER_WIDTH_TO_SCREEN_RATIO_PORTRAIT - x * ASPECT_RATIO_PORTRAIT;
776 return x * aspectRatio + y;
777 }
778
779 // The range of scroll values for Workspace
780 private int getScrollRange() {
781 return getChildOffset(getChildCount() - 1) - getChildOffset(0);
782 }
783
784 protected void setWallpaperDimension() {
Winson Chung201bc822011-06-20 15:41:53 -0700785 DisplayMetrics displayMetrics = new DisplayMetrics();
Adam Cohencff6af82011-09-13 14:51:53 -0700786 mLauncher.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
Winson Chung201bc822011-06-20 15:41:53 -0700787 final int maxDim = Math.max(displayMetrics.widthPixels, displayMetrics.heightPixels);
788 final int minDim = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800789
790 // We need to ensure that there is enough extra space in the wallpaper for the intended
791 // parallax effects
Michael Jurkaa6471802011-09-22 14:39:16 -0700792 if (LauncherApplication.isScreenLarge()) {
793 mWallpaperWidth = (int) (maxDim * wallpaperTravelToScreenWidthRatio(maxDim, minDim));
Michael Jurka193080b2011-10-28 16:08:53 -0700794 mWallpaperHeight = maxDim;
Michael Jurkaa6471802011-09-22 14:39:16 -0700795 } else {
Michael Jurka16706982011-09-26 16:39:22 -0700796 mWallpaperWidth = Math.max((int) (minDim * WALLPAPER_SCREENS_SPAN), maxDim);
Michael Jurkaa6471802011-09-22 14:39:16 -0700797 mWallpaperHeight = maxDim;
798 }
Winson Chungf7640c82011-02-28 13:47:29 -0800799 new Thread("setWallpaperDimension") {
800 public void run() {
801 mWallpaperManager.suggestDesiredDimensions(mWallpaperWidth, mWallpaperHeight);
802 }
803 }.start();
Michael Jurkac5b262c2011-01-12 20:24:50 -0800804 }
805
Michael Jurkaab1983f2011-01-18 15:50:17 -0800806 public void setVerticalWallpaperOffset(float offset) {
807 mWallpaperOffset.setFinalY(offset);
808 }
809 public float getVerticalWallpaperOffset() {
810 return mWallpaperOffset.getCurrY();
811 }
812 public void setHorizontalWallpaperOffset(float offset) {
813 mWallpaperOffset.setFinalX(offset);
814 }
815 public float getHorizontalWallpaperOffset() {
816 return mWallpaperOffset.getCurrX();
Michael Jurkac5b262c2011-01-12 20:24:50 -0800817 }
818
Michael Jurkaab1983f2011-01-18 15:50:17 -0800819 private float wallpaperOffsetForCurrentScroll() {
Michael Jurkaab1983f2011-01-18 15:50:17 -0800820 // The wallpaper travel width is how far, from left to right, the wallpaper will move
Michael Jurka193080b2011-10-28 16:08:53 -0700821 // at this orientation. On tablets in portrait mode we don't move all the way to the
822 // edges of the wallpaper, or otherwise the parallax effect would be too strong.
823 int wallpaperTravelWidth = mWallpaperWidth;
824 if (LauncherApplication.isScreenLarge()) {
825 wallpaperTravelWidth = mWallpaperTravelWidth;
Michael Jurkaf70c7812011-03-07 16:53:12 -0800826 }
Michael Jurkaab1983f2011-01-18 15:50:17 -0800827
Michael Jurkaab1983f2011-01-18 15:50:17 -0800828 // Set wallpaper offset steps (1 / (number of screens - 1))
Michael Jurka193080b2011-10-28 16:08:53 -0700829 mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 1.0f);
Michael Jurkaab1983f2011-01-18 15:50:17 -0800830
Michael Jurka193080b2011-10-28 16:08:53 -0700831 // For the purposes of computing the scrollRange and overScrollOffset, we assume
832 // that mLayoutScale is 1. This means that when we're in spring-loaded mode,
Adam Cohenbeff8c62011-08-31 17:46:01 -0700833 // there's no discrepancy between the wallpaper offset for a given page.
834 float layoutScale = mLayoutScale;
835 mLayoutScale = 1f;
Michael Jurka09d6eb02011-01-28 16:05:09 -0800836 int scrollRange = getScrollRange();
Michael Jurka09d6eb02011-01-28 16:05:09 -0800837
Adam Cohenbeff8c62011-08-31 17:46:01 -0700838 // Again, we adjust the wallpaper offset to be consistent between values of mLayoutScale
Michael Jurka193080b2011-10-28 16:08:53 -0700839 float adjustedScrollX = Math.max(0, Math.min(mScrollX, mMaxScrollX));
Adam Cohenb5ba0972011-09-07 18:02:31 -0700840 adjustedScrollX *= mWallpaperScrollRatio;
Adam Cohenbeff8c62011-08-31 17:46:01 -0700841 mLayoutScale = layoutScale;
842
Michael Jurkaab1983f2011-01-18 15:50:17 -0800843 float scrollProgress =
Michael Jurka193080b2011-10-28 16:08:53 -0700844 adjustedScrollX / (float) scrollRange;
Michael Jurkaab1983f2011-01-18 15:50:17 -0800845 float offsetInDips = wallpaperTravelWidth * scrollProgress +
Michael Jurkaf70c7812011-03-07 16:53:12 -0800846 (mWallpaperWidth - wallpaperTravelWidth) / 2; // center it
Michael Jurkaab1983f2011-01-18 15:50:17 -0800847 float offset = offsetInDips / (float) mWallpaperWidth;
848 return offset;
849 }
850 private void syncWallpaperOffsetWithScroll() {
Winson Chungb46a2d12011-04-22 14:13:05 -0700851 final boolean enableWallpaperEffects = isHardwareAccelerated();
852 if (enableWallpaperEffects) {
Michael Jurkaab1983f2011-01-18 15:50:17 -0800853 mWallpaperOffset.setFinalX(wallpaperOffsetForCurrentScroll());
Michael Jurkac5b262c2011-01-12 20:24:50 -0800854 }
855 }
856
857 public void updateWallpaperOffsetImmediately() {
858 mUpdateWallpaperOffsetImmediately = true;
859 }
860
Michael Jurkaab1983f2011-01-18 15:50:17 -0800861 private void updateWallpaperOffsets() {
862 boolean updateNow = false;
863 boolean keepUpdating = true;
864 if (mUpdateWallpaperOffsetImmediately) {
865 updateNow = true;
866 keepUpdating = false;
867 mWallpaperOffset.jumpToFinal();
868 mUpdateWallpaperOffsetImmediately = false;
Michael Jurkac5b262c2011-01-12 20:24:50 -0800869 } else {
Michael Jurkaab1983f2011-01-18 15:50:17 -0800870 updateNow = keepUpdating = mWallpaperOffset.computeScrollOffset();
Michael Jurkac5b262c2011-01-12 20:24:50 -0800871 }
Michael Jurkaab1983f2011-01-18 15:50:17 -0800872 if (updateNow) {
Michael Jurka9c6fbed2011-03-02 17:41:34 -0800873 if (mWindowToken != null) {
874 mWallpaperManager.setWallpaperOffsets(mWindowToken,
Michael Jurkaab1983f2011-01-18 15:50:17 -0800875 mWallpaperOffset.getCurrX(), mWallpaperOffset.getCurrY());
876 }
Michael Jurkac5b262c2011-01-12 20:24:50 -0800877 }
Michael Jurkaab1983f2011-01-18 15:50:17 -0800878 if (keepUpdating) {
Michael Jurka7372c592012-01-16 04:21:35 -0800879 invalidate();
Michael Jurkac5b262c2011-01-12 20:24:50 -0800880 }
Michael Jurkaab1983f2011-01-18 15:50:17 -0800881 }
882
Adam Cohenbeff8c62011-08-31 17:46:01 -0700883 @Override
884 protected void updateCurrentPageScroll() {
885 super.updateCurrentPageScroll();
Adam Cohen5037a0b2011-09-01 13:10:40 -0700886 computeWallpaperScrollRatio(mCurrentPage);
Adam Cohenbeff8c62011-08-31 17:46:01 -0700887 }
888
889 @Override
890 protected void snapToPage(int whichPage) {
891 super.snapToPage(whichPage);
Adam Cohen5037a0b2011-09-01 13:10:40 -0700892 computeWallpaperScrollRatio(whichPage);
Adam Cohenbeff8c62011-08-31 17:46:01 -0700893 }
894
Adam Cohen5037a0b2011-09-01 13:10:40 -0700895 private void computeWallpaperScrollRatio(int page) {
Adam Cohenbeff8c62011-08-31 17:46:01 -0700896 // Here, we determine what the desired scroll would be with and without a layout scale,
897 // and compute a ratio between the two. This allows us to adjust the wallpaper offset
898 // as though there is no layout scale.
899 float layoutScale = mLayoutScale;
Adam Cohen5037a0b2011-09-01 13:10:40 -0700900 int scaled = getChildOffset(page) - getRelativeChildOffset(page);
Adam Cohenbeff8c62011-08-31 17:46:01 -0700901 mLayoutScale = 1.0f;
Adam Cohen5037a0b2011-09-01 13:10:40 -0700902 float unscaled = getChildOffset(page) - getRelativeChildOffset(page);
Adam Cohenbeff8c62011-08-31 17:46:01 -0700903 mLayoutScale = layoutScale;
904 if (scaled > 0) {
905 mWallpaperScrollRatio = (1.0f * unscaled) / scaled;
906 } else {
907 mWallpaperScrollRatio = 1f;
908 }
909 }
910
Michael Jurkaab1983f2011-01-18 15:50:17 -0800911 class WallpaperOffsetInterpolator {
912 float mFinalHorizontalWallpaperOffset = 0.0f;
913 float mFinalVerticalWallpaperOffset = 0.5f;
914 float mHorizontalWallpaperOffset = 0.0f;
915 float mVerticalWallpaperOffset = 0.5f;
916 long mLastWallpaperOffsetUpdateTime;
Michael Jurkaca5b8362011-01-27 13:23:26 -0800917 boolean mIsMovingFast;
918 boolean mOverrideHorizontalCatchupConstant;
919 float mHorizontalCatchupConstant = 0.35f;
920 float mVerticalCatchupConstant = 0.35f;
Michael Jurkaab1983f2011-01-18 15:50:17 -0800921
922 public WallpaperOffsetInterpolator() {
923 }
924
Michael Jurkaca5b8362011-01-27 13:23:26 -0800925 public void setOverrideHorizontalCatchupConstant(boolean override) {
926 mOverrideHorizontalCatchupConstant = override;
927 }
928
929 public void setHorizontalCatchupConstant(float f) {
930 mHorizontalCatchupConstant = f;
931 }
932
933 public void setVerticalCatchupConstant(float f) {
934 mVerticalCatchupConstant = f;
935 }
936
Michael Jurkaab1983f2011-01-18 15:50:17 -0800937 public boolean computeScrollOffset() {
938 if (Float.compare(mHorizontalWallpaperOffset, mFinalHorizontalWallpaperOffset) == 0 &&
939 Float.compare(mVerticalWallpaperOffset, mFinalVerticalWallpaperOffset) == 0) {
Michael Jurkaca5b8362011-01-27 13:23:26 -0800940 mIsMovingFast = false;
Michael Jurkaab1983f2011-01-18 15:50:17 -0800941 return false;
942 }
Adam Cohencff6af82011-09-13 14:51:53 -0700943 boolean isLandscape = mDisplayWidth > mDisplayHeight;
Michael Jurkaab1983f2011-01-18 15:50:17 -0800944
945 long currentTime = System.currentTimeMillis();
946 long timeSinceLastUpdate = currentTime - mLastWallpaperOffsetUpdateTime;
947 timeSinceLastUpdate = Math.min((long) (1000/30f), timeSinceLastUpdate);
948 timeSinceLastUpdate = Math.max(1L, timeSinceLastUpdate);
Michael Jurkaca5b8362011-01-27 13:23:26 -0800949
950 float xdiff = Math.abs(mFinalHorizontalWallpaperOffset - mHorizontalWallpaperOffset);
951 if (!mIsMovingFast && xdiff > 0.07) {
952 mIsMovingFast = true;
953 }
954
955 float fractionToCatchUpIn1MsHorizontal;
956 if (mOverrideHorizontalCatchupConstant) {
957 fractionToCatchUpIn1MsHorizontal = mHorizontalCatchupConstant;
958 } else if (mIsMovingFast) {
959 fractionToCatchUpIn1MsHorizontal = isLandscape ? 0.5f : 0.75f;
960 } else {
961 // slow
962 fractionToCatchUpIn1MsHorizontal = isLandscape ? 0.27f : 0.5f;
963 }
964 float fractionToCatchUpIn1MsVertical = mVerticalCatchupConstant;
965
Michael Jurkaca5b8362011-01-27 13:23:26 -0800966 fractionToCatchUpIn1MsHorizontal /= 33f;
967 fractionToCatchUpIn1MsVertical /= 33f;
Michael Jurkaab1983f2011-01-18 15:50:17 -0800968
969 final float UPDATE_THRESHOLD = 0.00001f;
970 float hOffsetDelta = mFinalHorizontalWallpaperOffset - mHorizontalWallpaperOffset;
971 float vOffsetDelta = mFinalVerticalWallpaperOffset - mVerticalWallpaperOffset;
Michael Jurkaca5b8362011-01-27 13:23:26 -0800972 boolean jumpToFinalValue = Math.abs(hOffsetDelta) < UPDATE_THRESHOLD &&
973 Math.abs(vOffsetDelta) < UPDATE_THRESHOLD;
Michael Jurkaa6471802011-09-22 14:39:16 -0700974
975 // Don't have any lag between workspace and wallpaper on non-large devices
976 if (!LauncherApplication.isScreenLarge() || jumpToFinalValue) {
Michael Jurkaab1983f2011-01-18 15:50:17 -0800977 mHorizontalWallpaperOffset = mFinalHorizontalWallpaperOffset;
978 mVerticalWallpaperOffset = mFinalVerticalWallpaperOffset;
979 } else {
980 float percentToCatchUpVertical =
Michael Jurkaca5b8362011-01-27 13:23:26 -0800981 Math.min(1.0f, timeSinceLastUpdate * fractionToCatchUpIn1MsVertical);
Michael Jurkaab1983f2011-01-18 15:50:17 -0800982 float percentToCatchUpHorizontal =
Michael Jurkaca5b8362011-01-27 13:23:26 -0800983 Math.min(1.0f, timeSinceLastUpdate * fractionToCatchUpIn1MsHorizontal);
Michael Jurkaab1983f2011-01-18 15:50:17 -0800984 mHorizontalWallpaperOffset += percentToCatchUpHorizontal * hOffsetDelta;
985 mVerticalWallpaperOffset += percentToCatchUpVertical * vOffsetDelta;
986 }
Michael Jurkaca5b8362011-01-27 13:23:26 -0800987
Michael Jurkaab1983f2011-01-18 15:50:17 -0800988 mLastWallpaperOffsetUpdateTime = System.currentTimeMillis();
989 return true;
990 }
991
992 public float getCurrX() {
993 return mHorizontalWallpaperOffset;
994 }
995
996 public float getFinalX() {
997 return mFinalHorizontalWallpaperOffset;
998 }
999
1000 public float getCurrY() {
1001 return mVerticalWallpaperOffset;
1002 }
1003
1004 public float getFinalY() {
1005 return mFinalVerticalWallpaperOffset;
1006 }
1007
1008 public void setFinalX(float x) {
1009 mFinalHorizontalWallpaperOffset = Math.max(0f, Math.min(x, 1.0f));
1010 }
1011
1012 public void setFinalY(float y) {
1013 mFinalVerticalWallpaperOffset = Math.max(0f, Math.min(y, 1.0f));
1014 }
1015
1016 public void jumpToFinal() {
1017 mHorizontalWallpaperOffset = mFinalHorizontalWallpaperOffset;
1018 mVerticalWallpaperOffset = mFinalVerticalWallpaperOffset;
1019 }
Dianne Hackborn8f573952009-08-10 23:21:09 -07001020 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001021
Michael Jurka340c5f32010-10-21 16:49:19 -07001022 @Override
1023 public void computeScroll() {
1024 super.computeScroll();
Adam Cohenbeff8c62011-08-31 17:46:01 -07001025 syncWallpaperOffsetWithScroll();
Michael Jurka340c5f32010-10-21 16:49:19 -07001026 }
1027
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001028 void showOutlines() {
Michael Jurkad74c9842011-07-10 12:44:21 -07001029 if (!isSmall() && !mIsSwitchingState) {
Winson Chung9171e6d2010-11-17 17:39:27 -08001030 if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel();
1031 if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel();
1032 mChildrenOutlineFadeInAnimation = ObjectAnimator.ofFloat(this, "childrenOutlineAlpha", 1.0f);
1033 mChildrenOutlineFadeInAnimation.setDuration(CHILDREN_OUTLINE_FADE_IN_DURATION);
1034 mChildrenOutlineFadeInAnimation.start();
Michael Jurka3e7c7632010-10-02 16:01:03 -07001035 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001036 }
1037
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001038 void hideOutlines() {
Michael Jurkad74c9842011-07-10 12:44:21 -07001039 if (!isSmall() && !mIsSwitchingState) {
Winson Chung9171e6d2010-11-17 17:39:27 -08001040 if (mChildrenOutlineFadeInAnimation != null) mChildrenOutlineFadeInAnimation.cancel();
1041 if (mChildrenOutlineFadeOutAnimation != null) mChildrenOutlineFadeOutAnimation.cancel();
1042 mChildrenOutlineFadeOutAnimation = ObjectAnimator.ofFloat(this, "childrenOutlineAlpha", 0.0f);
1043 mChildrenOutlineFadeOutAnimation.setDuration(CHILDREN_OUTLINE_FADE_OUT_DURATION);
1044 mChildrenOutlineFadeOutAnimation.setStartDelay(CHILDREN_OUTLINE_FADE_OUT_DELAY);
1045 mChildrenOutlineFadeOutAnimation.start();
Michael Jurka3e7c7632010-10-02 16:01:03 -07001046 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001047 }
1048
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001049 public void showOutlinesTemporarily() {
1050 if (!mIsPageMoving && !isTouchActive()) {
1051 snapToPage(mCurrentPage);
1052 }
1053 }
1054
Winson Chung9171e6d2010-11-17 17:39:27 -08001055 public void setChildrenOutlineAlpha(float alpha) {
1056 mChildrenOutlineAlpha = alpha;
Adam Cohenf34bab52010-09-30 14:11:56 -07001057 for (int i = 0; i < getChildCount(); i++) {
1058 CellLayout cl = (CellLayout) getChildAt(i);
1059 cl.setBackgroundAlpha(alpha);
1060 }
1061 }
1062
Winson Chung9171e6d2010-11-17 17:39:27 -08001063 public float getChildrenOutlineAlpha() {
1064 return mChildrenOutlineAlpha;
1065 }
1066
Michael Jurka9c6fbed2011-03-02 17:41:34 -08001067 void disableBackground() {
1068 mDrawBackground = false;
1069 }
1070 void enableBackground() {
1071 mDrawBackground = true;
1072 }
1073
Michael Jurka7bdb25a2011-08-03 15:16:44 -07001074 private void animateBackgroundGradient(float finalAlpha, boolean animated) {
Winson Chungb4b7fa72010-11-18 14:38:53 -08001075 if (mBackground == null) return;
Michael Jurkab9e14972011-07-25 17:57:40 -07001076 if (mBackgroundFadeInAnimation != null) {
1077 mBackgroundFadeInAnimation.cancel();
1078 mBackgroundFadeInAnimation = null;
1079 }
1080 if (mBackgroundFadeOutAnimation != null) {
1081 mBackgroundFadeOutAnimation.cancel();
1082 mBackgroundFadeOutAnimation = null;
1083 }
Michael Jurka7bdb25a2011-08-03 15:16:44 -07001084 float startAlpha = getBackgroundAlpha();
1085 if (finalAlpha != startAlpha) {
1086 if (animated) {
1087 mBackgroundFadeOutAnimation = ValueAnimator.ofFloat(startAlpha, finalAlpha);
1088 mBackgroundFadeOutAnimation.addUpdateListener(new AnimatorUpdateListener() {
1089 public void onAnimationUpdate(ValueAnimator animation) {
1090 setBackgroundAlpha(((Float) animation.getAnimatedValue()).floatValue());
1091 }
1092 });
1093 mBackgroundFadeOutAnimation.setInterpolator(new DecelerateInterpolator(1.5f));
1094 mBackgroundFadeOutAnimation.setDuration(BACKGROUND_FADE_OUT_DURATION);
1095 mBackgroundFadeOutAnimation.start();
1096 } else {
1097 setBackgroundAlpha(finalAlpha);
1098 }
Michael Jurkab9e14972011-07-25 17:57:40 -07001099 }
Winson Chung9171e6d2010-11-17 17:39:27 -08001100 }
1101
1102 public void setBackgroundAlpha(float alpha) {
Michael Jurka9c6fbed2011-03-02 17:41:34 -08001103 if (alpha != mBackgroundAlpha) {
1104 mBackgroundAlpha = alpha;
1105 invalidate();
1106 }
Winson Chung9171e6d2010-11-17 17:39:27 -08001107 }
1108
Adam Cohenf34bab52010-09-30 14:11:56 -07001109 public float getBackgroundAlpha() {
1110 return mBackgroundAlpha;
1111 }
1112
Adam Lesinski6b879f02010-11-04 16:15:23 -07001113 /**
1114 * Due to 3D transformations, if two CellLayouts are theoretically touching each other,
1115 * on the xy plane, when one is rotated along the y-axis, the gap between them is perceived
1116 * as being larger. This method computes what offset the rotated view should be translated
1117 * in order to minimize this perceived gap.
1118 * @param degrees Angle of the view
1119 * @param width Width of the view
1120 * @param height Height of the view
1121 * @return Offset to be used in a View.setTranslationX() call
1122 */
1123 private float getOffsetXForRotation(float degrees, int width, int height) {
1124 mMatrix.reset();
1125 mCamera.save();
1126 mCamera.rotateY(Math.abs(degrees));
1127 mCamera.getMatrix(mMatrix);
1128 mCamera.restore();
1129
1130 mMatrix.preTranslate(-width * 0.5f, -height * 0.5f);
1131 mMatrix.postTranslate(width * 0.5f, height * 0.5f);
1132 mTempFloat2[0] = width;
1133 mTempFloat2[1] = height;
1134 mMatrix.mapPoints(mTempFloat2);
1135 return (width - mTempFloat2[0]) * (degrees > 0.0f ? 1.0f : -1.0f);
1136 }
1137
Adam Cohen68d73932010-11-15 10:50:58 -08001138 float backgroundAlphaInterpolator(float r) {
1139 float pivotA = 0.1f;
1140 float pivotB = 0.4f;
1141 if (r < pivotA) {
1142 return 0;
1143 } else if (r > pivotB) {
1144 return 1.0f;
1145 } else {
1146 return (r - pivotA)/(pivotB - pivotA);
1147 }
1148 }
1149
1150 float overScrollBackgroundAlphaInterpolator(float r) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001151 float threshold = 0.08f;
Adam Cohen68d73932010-11-15 10:50:58 -08001152
1153 if (r > mOverScrollMaxBackgroundAlpha) {
1154 mOverScrollMaxBackgroundAlpha = r;
1155 } else if (r < mOverScrollMaxBackgroundAlpha) {
1156 r = mOverScrollMaxBackgroundAlpha;
1157 }
1158
1159 return Math.min(r / threshold, 1.0f);
1160 }
1161
Adam Cohenb5ba0972011-09-07 18:02:31 -07001162 private void screenScrolledLargeUI(int screenCenter) {
Adam Cohen70ed13b2011-11-16 15:59:40 -08001163 if (isSwitchingState()) return;
Adam Cohenebea84d2011-11-09 17:20:41 -08001164 boolean isInOverscroll = false;
Adam Cohen21f12b52010-10-07 17:15:37 -07001165 for (int i = 0; i < getChildCount(); i++) {
Adam Cohen1b0aaac2010-10-28 11:11:18 -07001166 CellLayout cl = (CellLayout) getChildAt(i);
1167 if (cl != null) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001168 float scrollProgress = getScrollProgress(screenCenter, cl, i);
Adam Cohen21f12b52010-10-07 17:15:37 -07001169 float rotation = WORKSPACE_ROTATION * scrollProgress;
Adam Lesinski6b879f02010-11-04 16:15:23 -07001170 float translationX = getOffsetXForRotation(rotation, cl.getWidth(), cl.getHeight());
Adam Cohen68d73932010-11-15 10:50:58 -08001171
Adam Cohenb5ba0972011-09-07 18:02:31 -07001172 // If the current page (i) is being over scrolled, we use a different
1173 // set of rules for setting the background alpha multiplier.
Adam Cohencff6af82011-09-13 14:51:53 -07001174 if (!isSmall()) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001175 if ((mOverScrollX < 0 && i == 0) || (mOverScrollX > mMaxScrollX &&
1176 i == getChildCount() -1)) {
1177 isInOverscroll = true;
1178 rotation *= -1;
Adam Cohencff6af82011-09-13 14:51:53 -07001179 cl.setBackgroundAlphaMultiplier(
1180 overScrollBackgroundAlphaInterpolator(Math.abs(scrollProgress)));
1181 mOverScrollPageIndex = i;
Adam Cohenebea84d2011-11-09 17:20:41 -08001182 cl.setOverScrollAmount(Math.abs(scrollProgress), i == 0);
1183 cl.setPivotX(cl.getMeasuredWidth() * (i == 0 ? 0.75f : 0.25f));
1184 cl.setPivotY(cl.getMeasuredHeight() * 0.5f);
1185 cl.setOverscrollTransformsDirty(true);
Adam Cohencff6af82011-09-13 14:51:53 -07001186 } else if (mOverScrollPageIndex != i) {
1187 cl.setBackgroundAlphaMultiplier(
1188 backgroundAlphaInterpolator(Math.abs(scrollProgress)));
1189 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001190 }
Michael Jurka7372c592012-01-16 04:21:35 -08001191 cl.setTranslationX(translationX);
1192 cl.setRotationY(rotation);
Adam Cohen73894962011-10-31 13:17:17 -07001193 if (mFadeInAdjacentScreens && !isSmall()) {
1194 float alpha = 1 - Math.abs(scrollProgress);
Michael Jurka7372c592012-01-16 04:21:35 -08001195 cl.setAlpha(alpha);
Adam Cohen73894962011-10-31 13:17:17 -07001196 }
Adam Cohenf34bab52010-09-30 14:11:56 -07001197 }
1198 }
Adam Cohenebea84d2011-11-09 17:20:41 -08001199 if (!isSwitchingState() && !isInOverscroll) {
1200 ((CellLayout) getChildAt(0)).resetOverscrollTransforms();
1201 ((CellLayout) getChildAt(getChildCount() - 1)).resetOverscrollTransforms();
1202 }
Adam Cohen73894962011-10-31 13:17:17 -07001203 invalidate();
Adam Cohenf34bab52010-09-30 14:11:56 -07001204 }
1205
Adam Cohenb5ba0972011-09-07 18:02:31 -07001206 private void screenScrolledStandardUI(int screenCenter) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001207 if (mOverScrollX < 0 || mOverScrollX > mMaxScrollX) {
1208 int index = mOverScrollX < 0 ? 0 : getChildCount() - 1;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001209 CellLayout cl = (CellLayout) getChildAt(index);
1210 float scrollProgress = getScrollProgress(screenCenter, cl, index);
1211 cl.setOverScrollAmount(Math.abs(scrollProgress), index == 0);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001212 float rotation = - WORKSPACE_OVERSCROLL_ROTATION * scrollProgress;
Adam Cohencff6af82011-09-13 14:51:53 -07001213 cl.setCameraDistance(mDensity * CAMERA_DISTANCE);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001214 cl.setPivotX(cl.getMeasuredWidth() * (index == 0 ? 0.75f : 0.25f));
Adam Cohenebea84d2011-11-09 17:20:41 -08001215 cl.setPivotY(cl.getMeasuredHeight() * 0.5f);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001216 cl.setRotationY(rotation);
Adam Cohenebea84d2011-11-09 17:20:41 -08001217 cl.setOverscrollTransformsDirty(true);
Adam Cohen21b41102011-11-01 17:29:52 -07001218 setFadeForOverScroll(Math.abs(scrollProgress));
Adam Cohenb5ba0972011-09-07 18:02:31 -07001219 } else {
Adam Cohen21b41102011-11-01 17:29:52 -07001220 if (mOverscrollFade != 0) {
1221 setFadeForOverScroll(0);
1222 }
Adam Cohen7842d7f2011-09-12 15:28:15 -07001223 // We don't want to mess with the translations during transitions
1224 if (!isSwitchingState()) {
Adam Cohenebea84d2011-11-09 17:20:41 -08001225 ((CellLayout) getChildAt(0)).resetOverscrollTransforms();
1226 ((CellLayout) getChildAt(getChildCount() - 1)).resetOverscrollTransforms();
Adam Cohen7842d7f2011-09-12 15:28:15 -07001227 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001228 }
1229 }
1230
1231 @Override
1232 protected void screenScrolled(int screenCenter) {
Adam Cohenb5ba0972011-09-07 18:02:31 -07001233 if (LauncherApplication.isScreenLarge()) {
Adam Cohen73894962011-10-31 13:17:17 -07001234 // We don't call super.screenScrolled() here because we handle the adjacent pages alpha
1235 // ourselves (for efficiency), and there are no scrolling indicators to update.
Adam Cohenb5ba0972011-09-07 18:02:31 -07001236 screenScrolledLargeUI(screenCenter);
1237 } else {
Adam Cohen73894962011-10-31 13:17:17 -07001238 super.screenScrolled(screenCenter);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001239 screenScrolledStandardUI(screenCenter);
1240 }
1241 }
1242
1243 @Override
1244 protected void overScroll(float amount) {
1245 if (LauncherApplication.isScreenLarge()) {
1246 dampedOverScroll(amount);
1247 } else {
1248 acceleratedOverScroll(amount);
1249 }
1250 }
1251
Joe Onorato00acb122009-08-04 16:04:30 -04001252 protected void onAttachedToWindow() {
1253 super.onAttachedToWindow();
Michael Jurka9c6fbed2011-03-02 17:41:34 -08001254 mWindowToken = getWindowToken();
Joe Onorato956091b2010-02-19 12:47:40 -08001255 computeScroll();
Michael Jurka9c6fbed2011-03-02 17:41:34 -08001256 mDragController.setWindowToken(mWindowToken);
1257 }
1258
1259 protected void onDetachedFromWindow() {
1260 mWindowToken = null;
Joe Onorato00acb122009-08-04 16:04:30 -04001261 }
1262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -07001264 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Jurkac5b262c2011-01-12 20:24:50 -08001265 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
1266 mUpdateWallpaperOffsetImmediately = true;
1267 }
Michael Jurka0142d492010-08-25 17:46:15 -07001268 super.onLayout(changed, left, top, right, bottom);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 }
1270
1271 @Override
Winson Chung9171e6d2010-11-17 17:39:27 -08001272 protected void onDraw(Canvas canvas) {
Michael Jurkaab1983f2011-01-18 15:50:17 -08001273 updateWallpaperOffsets();
Michael Jurkac5b262c2011-01-12 20:24:50 -08001274
Winson Chung9171e6d2010-11-17 17:39:27 -08001275 // Draw the background gradient if necessary
Michael Jurka9c6fbed2011-03-02 17:41:34 -08001276 if (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground) {
Winson Chung8d006d52010-11-29 15:55:29 -08001277 int alpha = (int) (mBackgroundAlpha * 255);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001278 mBackground.setAlpha(alpha);
1279 mBackground.setBounds(mScrollX, 0, mScrollX + getMeasuredWidth(),
1280 getMeasuredHeight());
1281 mBackground.draw(canvas);
Winson Chung9171e6d2010-11-17 17:39:27 -08001282 }
Adam Cohen073a46f2011-05-17 16:28:09 -07001283
Winson Chung9171e6d2010-11-17 17:39:27 -08001284 super.onDraw(canvas);
1285 }
1286
Adam Cohen21b41102011-11-01 17:29:52 -07001287 boolean isDrawingBackgroundGradient() {
1288 return (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground);
1289 }
1290
Michael Jurkadfab7f02011-11-18 13:01:04 -08001291 @Override
1292 protected void dispatchDraw(Canvas canvas) {
Winson Chung557d6ed2011-07-08 15:34:52 -07001293 super.dispatchDraw(canvas);
Patrick Dubroycd68ff52010-10-28 17:57:05 -07001294
Winson Chung557d6ed2011-07-08 15:34:52 -07001295 if (mInScrollArea && !LauncherApplication.isScreenLarge()) {
1296 final int width = getWidth();
1297 final int height = getHeight();
1298 final int pageHeight = getChildAt(0).getHeight();
Patrick Dubroy0207c522010-11-03 22:12:02 -07001299
Winson Chungdea74b72011-09-13 18:06:43 -07001300 // Set the height of the outline to be the height of the page
1301 final int offset = (height - pageHeight - mPaddingTop - mPaddingBottom) / 2;
1302 final int paddingTop = mPaddingTop + offset;
1303 final int paddingBottom = mPaddingBottom + offset;
Patrick Dubroy0207c522010-11-03 22:12:02 -07001304
Winson Chungaa15ffe2012-01-18 15:45:28 -08001305 final int page = (mNextPage != INVALID_PAGE ? mNextPage : mCurrentPage);
1306 final CellLayout leftPage = (CellLayout) getChildAt(page - 1);
1307 final CellLayout rightPage = (CellLayout) getChildAt(page + 1);
Patrick Dubroy0207c522010-11-03 22:12:02 -07001308
Winson Chung557d6ed2011-07-08 15:34:52 -07001309 if (leftPage != null && leftPage.getIsDragOverlapping()) {
1310 final Drawable d = getResources().getDrawable(R.drawable.page_hover_left_holo);
Winson Chungdea74b72011-09-13 18:06:43 -07001311 d.setBounds(mScrollX, paddingTop, mScrollX + d.getIntrinsicWidth(),
1312 height - paddingBottom);
Winson Chung557d6ed2011-07-08 15:34:52 -07001313 d.draw(canvas);
1314 } else if (rightPage != null && rightPage.getIsDragOverlapping()) {
1315 final Drawable d = getResources().getDrawable(R.drawable.page_hover_right_holo);
Winson Chungdea74b72011-09-13 18:06:43 -07001316 d.setBounds(mScrollX + width - d.getIntrinsicWidth(), paddingTop, mScrollX + width,
1317 height - paddingBottom);
Winson Chung557d6ed2011-07-08 15:34:52 -07001318 d.draw(canvas);
Patrick Dubroy0207c522010-11-03 22:12:02 -07001319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322
1323 @Override
1324 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Joe Onorato67886212009-09-14 19:05:05 -04001325 if (!mLauncher.isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 final Folder openFolder = getOpenFolder();
1327 if (openFolder != null) {
1328 return openFolder.requestFocus(direction, previouslyFocusedRect);
1329 } else {
Michael Jurka0142d492010-08-25 17:46:15 -07001330 return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332 }
1333 return false;
1334 }
1335
1336 @Override
Winson Chung97d85d22011-04-13 11:27:36 -07001337 public int getDescendantFocusability() {
Michael Jurkad74c9842011-07-10 12:44:21 -07001338 if (isSmall()) {
Winson Chung97d85d22011-04-13 11:27:36 -07001339 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
1340 }
1341 return super.getDescendantFocusability();
1342 }
1343
1344 @Override
Romain Guyc2e24c02009-06-01 16:11:41 -07001345 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Joe Onorato67886212009-09-14 19:05:05 -04001346 if (!mLauncher.isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 final Folder openFolder = getOpenFolder();
Michael Jurka0142d492010-08-25 17:46:15 -07001348 if (openFolder != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 openFolder.addFocusables(views, direction);
Michael Jurka0142d492010-08-25 17:46:15 -07001350 } else {
1351 super.addFocusables(views, direction, focusableMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 }
1353 }
1354 }
1355
Michael Jurkad74c9842011-07-10 12:44:21 -07001356 public boolean isSmall() {
Adam Cohen3384c7b2011-07-17 15:48:21 -07001357 return mState == State.SMALL || mState == State.SPRING_LOADED;
Michael Jurkad74c9842011-07-10 12:44:21 -07001358 }
1359
Michael Jurka0142d492010-08-25 17:46:15 -07001360 void enableChildrenCache(int fromPage, int toPage) {
1361 if (fromPage > toPage) {
1362 final int temp = fromPage;
1363 fromPage = toPage;
1364 toPage = temp;
Mike Cleron3a2b3f22009-11-05 17:17:42 -08001365 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001366
Michael Jurkadee05892010-07-27 10:01:56 -07001367 final int screenCount = getChildCount();
Adam Powellfea5d022010-04-29 11:42:45 -07001368
Michael Jurka0142d492010-08-25 17:46:15 -07001369 fromPage = Math.max(fromPage, 0);
1370 toPage = Math.min(toPage, screenCount - 1);
Adam Powellfea5d022010-04-29 11:42:45 -07001371
Michael Jurka0142d492010-08-25 17:46:15 -07001372 for (int i = fromPage; i <= toPage; i++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 final CellLayout layout = (CellLayout) getChildAt(i);
1374 layout.setChildrenDrawnWithCacheEnabled(true);
1375 layout.setChildrenDrawingCacheEnabled(true);
1376 }
1377 }
1378
1379 void clearChildrenCache() {
Michael Jurkadee05892010-07-27 10:01:56 -07001380 final int screenCount = getChildCount();
1381 for (int i = 0; i < screenCount; i++) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 final CellLayout layout = (CellLayout) getChildAt(i);
1383 layout.setChildrenDrawnWithCacheEnabled(false);
Adam Cohen8182e5b2011-08-01 15:43:31 -07001384 // In software mode, we don't want the items to continue to be drawn into bitmaps
1385 if (!isHardwareAccelerated()) {
1386 layout.setChildrenDrawingCacheEnabled(false);
1387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 }
1389 }
1390
Michael Jurkad74c9842011-07-10 12:44:21 -07001391 private void updateChildrenLayersEnabled() {
Adam Cohen7777d962011-08-18 18:58:38 -07001392 boolean small = isSmall() || mIsSwitchingState;
Michael Jurkad74c9842011-07-10 12:44:21 -07001393 boolean dragging = mAnimatingViewIntoPlace || mIsDragOccuring;
1394 boolean enableChildrenLayers = small || dragging || isPageMoving();
1395
1396 if (enableChildrenLayers != mChildrenLayersEnabled) {
1397 mChildrenLayersEnabled = enableChildrenLayers;
Michael Jurka80c69852011-12-16 14:16:32 -08001398 for (int i = 0; i < getPageCount(); i++) {
1399 ((ViewGroup)getChildAt(i)).setChildrenLayersEnabled(mChildrenLayersEnabled);
Michael Jurkad74c9842011-07-10 12:44:21 -07001400 }
Michael Jurkace7e05f2011-02-01 22:02:35 -08001401 }
Michael Jurkace7e05f2011-02-01 22:02:35 -08001402 }
1403
Jeff Brown1d0867c2010-12-02 18:27:39 -08001404 protected void onWallpaperTap(MotionEvent ev) {
1405 final int[] position = mTempCell;
1406 getLocationOnScreen(position);
1407
1408 int pointerIndex = ev.getActionIndex();
1409 position[0] += (int) ev.getX(pointerIndex);
1410 position[1] += (int) ev.getY(pointerIndex);
1411
1412 mWallpaperManager.sendWallpaperCommand(getWindowToken(),
1413 ev.getAction() == MotionEvent.ACTION_UP
1414 ? WallpaperManager.COMMAND_TAP : WallpaperManager.COMMAND_SECONDARY_TAP,
1415 position[0], position[1], 0, null);
1416 }
1417
Adam Cohen61033d32010-11-15 18:29:44 -08001418 /*
1419 * This interpolator emulates the rate at which the perceived scale of an object changes
1420 * as its distance from a camera increases. When this interpolator is applied to a scale
1421 * animation on a view, it evokes the sense that the object is shrinking due to moving away
1422 * from the camera.
1423 */
1424 static class ZInterpolator implements TimeInterpolator {
1425 private float focalLength;
1426
1427 public ZInterpolator(float foc) {
1428 focalLength = foc;
1429 }
1430
Adam Coheneed565d2010-11-15 11:30:05 -08001431 public float getInterpolation(float input) {
1432 return (1.0f - focalLength / (focalLength + input)) /
Adam Cohen61033d32010-11-15 18:29:44 -08001433 (1.0f - focalLength / (focalLength + 1.0f));
Adam Cohencbbaf982010-11-12 14:50:33 -08001434 }
1435 }
1436
Adam Cohen61033d32010-11-15 18:29:44 -08001437 /*
1438 * The exact reverse of ZInterpolator.
1439 */
1440 static class InverseZInterpolator implements TimeInterpolator {
1441 private ZInterpolator zInterpolator;
1442 public InverseZInterpolator(float foc) {
1443 zInterpolator = new ZInterpolator(foc);
1444 }
Adam Cohencbbaf982010-11-12 14:50:33 -08001445 public float getInterpolation(float input) {
Adam Cohen61033d32010-11-15 18:29:44 -08001446 return 1 - zInterpolator.getInterpolation(1 - input);
Adam Cohencbbaf982010-11-12 14:50:33 -08001447 }
1448 }
1449
Adam Cohen61033d32010-11-15 18:29:44 -08001450 /*
1451 * ZInterpolator compounded with an ease-out.
1452 */
1453 static class ZoomOutInterpolator implements TimeInterpolator {
Adam Cohenb64d36e2011-10-17 21:48:02 -07001454 private final DecelerateInterpolator decelerate = new DecelerateInterpolator(0.75f);
1455 private final ZInterpolator zInterpolator = new ZInterpolator(0.13f);
Adam Cohen61033d32010-11-15 18:29:44 -08001456
1457 public float getInterpolation(float input) {
1458 return decelerate.getInterpolation(zInterpolator.getInterpolation(input));
1459 }
1460 }
1461
1462 /*
1463 * InvereZInterpolator compounded with an ease-out.
1464 */
1465 static class ZoomInInterpolator implements TimeInterpolator {
1466 private final InverseZInterpolator inverseZInterpolator = new InverseZInterpolator(0.35f);
1467 private final DecelerateInterpolator decelerate = new DecelerateInterpolator(3.0f);
1468
1469 public float getInterpolation(float input) {
1470 return decelerate.getInterpolation(inverseZInterpolator.getInterpolation(input));
1471 }
1472 }
1473
Adam Cohen61033d32010-11-15 18:29:44 -08001474 private final ZoomInInterpolator mZoomInInterpolator = new ZoomInInterpolator();
Michael Jurka3e7c7632010-10-02 16:01:03 -07001475
Michael Jurka3e7c7632010-10-02 16:01:03 -07001476 /*
Adam Cohen66396872011-04-15 17:50:36 -07001477 *
1478 * We call these methods (onDragStartedWithItemSpans/onDragStartedWithSize) whenever we
1479 * start a drag in Launcher, regardless of whether the drag has ever entered the Workspace
1480 *
1481 * These methods mark the appropriate pages as accepting drops (which alters their visual
1482 * appearance).
1483 *
1484 */
1485 public void onDragStartedWithItem(View v) {
Adam Cohen66396872011-04-15 17:50:36 -07001486 final Canvas canvas = new Canvas();
1487
1488 // We need to add extra padding to the bitmap to make room for the glow effect
1489 final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
1490
1491 // The outline is used to visualize where the item will land if dropped
1492 mDragOutline = createDragOutline(v, canvas, bitmapPadding);
1493 }
1494
Michael Jurka038f9d82011-11-03 13:50:45 -07001495 public void onDragStartedWithItem(PendingAddItemInfo info, Bitmap b, Paint alphaClipPaint) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001496 final Canvas canvas = new Canvas();
1497
1498 // We need to add extra padding to the bitmap to make room for the glow effect
Michael Jurka38b4f7c2010-12-14 16:46:39 -08001499 final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001500
Michael Jurka038f9d82011-11-03 13:50:45 -07001501 int[] size = estimateItemSize(info.spanX, info.spanY, info, false);
Adam Cohen66396872011-04-15 17:50:36 -07001502
Michael Jurkad3ef3062010-11-23 16:23:58 -08001503 // The outline is used to visualize where the item will land if dropped
Peter Ng8db70002011-10-25 15:40:08 -07001504 mDragOutline = createDragOutline(b, canvas, bitmapPadding, size[0], size[1], alphaClipPaint);
Michael Jurka3e7c7632010-10-02 16:01:03 -07001505 }
1506
1507 // we call this method whenever a drag and drop in Launcher finishes, even if Workspace was
1508 // never dragged over
Patrick Dubroy7bccb422011-01-20 14:50:55 -08001509 public void onDragStopped(boolean success) {
1510 // In the success case, DragController has already called onDragExit()
1511 if (!success) {
Adam Cohenc0dcf592011-06-01 15:30:43 -07001512 doDragExit(null);
Patrick Dubroy7bccb422011-01-20 14:50:55 -08001513 }
Michael Jurka3e7c7632010-10-02 16:01:03 -07001514 }
1515
Patrick Dubroy758a9232011-03-03 19:54:56 -08001516 public void exitWidgetResizeMode() {
Adam Cohen716b51e2011-06-30 12:09:54 -07001517 DragLayer dragLayer = mLauncher.getDragLayer();
Adam Cohen67882692011-03-11 15:29:03 -08001518 dragLayer.clearAllResizeFrames();
Patrick Dubroy758a9232011-03-03 19:54:56 -08001519 }
1520
Adam Cohen4b285c52011-07-21 14:24:06 -07001521 private void initAnimationArrays() {
1522 final int childCount = getChildCount();
1523 if (mOldTranslationXs != null) return;
1524 mOldTranslationXs = new float[childCount];
1525 mOldTranslationYs = new float[childCount];
1526 mOldScaleXs = new float[childCount];
1527 mOldScaleYs = new float[childCount];
1528 mOldBackgroundAlphas = new float[childCount];
1529 mOldBackgroundAlphaMultipliers = new float[childCount];
1530 mOldAlphas = new float[childCount];
1531 mOldRotationYs = new float[childCount];
1532 mNewTranslationXs = new float[childCount];
1533 mNewTranslationYs = new float[childCount];
1534 mNewScaleXs = new float[childCount];
1535 mNewScaleYs = new float[childCount];
1536 mNewBackgroundAlphas = new float[childCount];
1537 mNewBackgroundAlphaMultipliers = new float[childCount];
1538 mNewAlphas = new float[childCount];
1539 mNewRotationYs = new float[childCount];
1540 }
1541
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001542 Animator getChangeStateAnimation(final State state, boolean animated) {
1543 return getChangeStateAnimation(state, animated, 0);
Adam Cohen7777d962011-08-18 18:58:38 -07001544 }
1545
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001546 Animator getChangeStateAnimation(final State state, boolean animated, int delay) {
Michael Jurkabdf78552011-10-31 14:34:25 -07001547 if (mState == state) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001548 return null;
Michael Jurka99633da2011-07-27 22:40:17 -07001549 }
1550
Winson Chung1b7c1d32011-10-25 12:09:38 -07001551 // Initialize animation arrays for the first time if necessary
1552 initAnimationArrays();
1553
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001554 AnimatorSet anim = animated ? new AnimatorSet() : null;
Adam Lesinski6b879f02010-11-04 16:15:23 -07001555
Adam Cohen7777d962011-08-18 18:58:38 -07001556 // Stop any scrolling, move to the current page right away
1557 setCurrentPage((mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage);
Winson Chungde1af762011-07-21 16:44:07 -07001558
Winson Chung1b7c1d32011-10-25 12:09:38 -07001559 final State oldState = mState;
1560 final boolean oldStateIsNormal = (oldState == State.NORMAL);
Winson Chung1b7c1d32011-10-25 12:09:38 -07001561 final boolean oldStateIsSmall = (oldState == State.SMALL);
Adam Cohen7777d962011-08-18 18:58:38 -07001562 mState = state;
Winson Chung1b7c1d32011-10-25 12:09:38 -07001563 final boolean stateIsNormal = (state == State.NORMAL);
1564 final boolean stateIsSpringLoaded = (state == State.SPRING_LOADED);
1565 final boolean stateIsSmall = (state == State.SMALL);
1566 float finalScaleFactor = 1.0f;
1567 float finalBackgroundAlpha = stateIsSpringLoaded ? 1.0f : 0f;
1568 float translationX = 0;
1569 float translationY = 0;
Adam Cohen7777d962011-08-18 18:58:38 -07001570 boolean zoomIn = true;
Adam Cohenbeff8c62011-08-31 17:46:01 -07001571
Adam Cohen7777d962011-08-18 18:58:38 -07001572 if (state != State.NORMAL) {
Winson Chung1b7c1d32011-10-25 12:09:38 -07001573 finalScaleFactor = mSpringLoadedShrinkFactor - (stateIsSmall ? 0.1f : 0);
1574 if (oldStateIsNormal && stateIsSmall) {
Adam Cohen7777d962011-08-18 18:58:38 -07001575 zoomIn = false;
Adam Cohen7777d962011-08-18 18:58:38 -07001576 setLayoutScale(finalScaleFactor);
1577 updateChildrenLayersEnabled();
Michael Jurkaca5b8362011-01-27 13:23:26 -08001578 } else {
Adam Cohencff6af82011-09-13 14:51:53 -07001579 finalBackgroundAlpha = 1.0f;
Adam Cohen7777d962011-08-18 18:58:38 -07001580 setLayoutScale(finalScaleFactor);
1581 }
1582 } else {
1583 setLayoutScale(1.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07001584 }
1585
Adam Cohen7777d962011-08-18 18:58:38 -07001586 final int duration = zoomIn ?
1587 getResources().getInteger(R.integer.config_workspaceUnshrinkTime) :
1588 getResources().getInteger(R.integer.config_appsCustomizeWorkspaceShrinkTime);
Winson Chung1b7c1d32011-10-25 12:09:38 -07001589 for (int i = 0; i < getChildCount(); i++) {
1590 final CellLayout cl = (CellLayout) getChildAt(i);
Adam Cohen7777d962011-08-18 18:58:38 -07001591 float rotation = 0f;
Winson Chung1b7c1d32011-10-25 12:09:38 -07001592 float initialAlpha = cl.getAlpha();
1593 float finalAlphaMultiplierValue = 1f;
1594 float finalAlpha = (!mFadeInAdjacentScreens || stateIsSpringLoaded ||
1595 (i == mCurrentPage)) ? 1f : 0f;
Adam Cohen7777d962011-08-18 18:58:38 -07001596
Winson Chung1b7c1d32011-10-25 12:09:38 -07001597 // Determine the pages alpha during the state transition
1598 if ((oldStateIsSmall && stateIsNormal) ||
1599 (oldStateIsNormal && stateIsSmall)) {
1600 // To/from workspace - only show the current page unless the transition is not
1601 // animated and the animation end callback below doesn't run
1602 if (i == mCurrentPage || !animated) {
1603 finalAlpha = 1f;
1604 finalAlphaMultiplierValue = 0f;
1605 } else {
1606 initialAlpha = 0f;
1607 finalAlpha = 0f;
1608 }
Adam Cohen7777d962011-08-18 18:58:38 -07001609 }
1610
Winson Chung1b7c1d32011-10-25 12:09:38 -07001611 // Update the rotation of the screen (don't apply rotation on Phone UI)
Adam Cohen7777d962011-08-18 18:58:38 -07001612 if (LauncherApplication.isScreenLarge()) {
1613 if (i < mCurrentPage) {
1614 rotation = WORKSPACE_ROTATION;
1615 } else if (i > mCurrentPage) {
1616 rotation = -WORKSPACE_ROTATION;
Winson Chungb46a2d12011-04-22 14:13:05 -07001617 }
Michael Jurkac86756c2010-10-10 15:15:47 -07001618 }
Adam Cohen7777d962011-08-18 18:58:38 -07001619
Adam Cohen7777d962011-08-18 18:58:38 -07001620 // If the screen is not xlarge, then don't rotate the CellLayouts
1621 // NOTE: If we don't update the side pages alpha, then we should not hide the side
1622 // pages. see unshrink().
1623 if (LauncherApplication.isScreenLarge()) {
1624 translationX = getOffsetXForRotation(rotation, cl.getWidth(), cl.getHeight());
1625 }
1626
Winson Chung1b7c1d32011-10-25 12:09:38 -07001627 mOldAlphas[i] = initialAlpha;
1628 mNewAlphas[i] = finalAlpha;
Adam Cohen7777d962011-08-18 18:58:38 -07001629 if (animated) {
1630 mOldTranslationXs[i] = cl.getTranslationX();
1631 mOldTranslationYs[i] = cl.getTranslationY();
1632 mOldScaleXs[i] = cl.getScaleX();
1633 mOldScaleYs[i] = cl.getScaleY();
1634 mOldBackgroundAlphas[i] = cl.getBackgroundAlpha();
1635 mOldBackgroundAlphaMultipliers[i] = cl.getBackgroundAlphaMultiplier();
1636 mOldRotationYs[i] = cl.getRotationY();
1637
1638 mNewTranslationXs[i] = translationX;
1639 mNewTranslationYs[i] = translationY;
1640 mNewScaleXs[i] = finalScaleFactor;
1641 mNewScaleYs[i] = finalScaleFactor;
1642 mNewBackgroundAlphas[i] = finalBackgroundAlpha;
1643 mNewBackgroundAlphaMultipliers[i] = finalAlphaMultiplierValue;
1644 mNewRotationYs[i] = rotation;
1645 } else {
1646 cl.setTranslationX(translationX);
1647 cl.setTranslationY(translationY);
1648 cl.setScaleX(finalScaleFactor);
1649 cl.setScaleY(finalScaleFactor);
Winson Chung1b7c1d32011-10-25 12:09:38 -07001650 cl.setBackgroundAlpha(finalBackgroundAlpha);
Adam Cohen7777d962011-08-18 18:58:38 -07001651 cl.setBackgroundAlphaMultiplier(finalAlphaMultiplierValue);
Winson Chung1b7c1d32011-10-25 12:09:38 -07001652 cl.setAlpha(finalAlpha);
Adam Cohen7777d962011-08-18 18:58:38 -07001653 cl.setRotationY(rotation);
Adam Cohen7777d962011-08-18 18:58:38 -07001654 }
Michael Jurkadee05892010-07-27 10:01:56 -07001655 }
Winson Chung8d006d52010-11-29 15:55:29 -08001656
Adam Cohen7777d962011-08-18 18:58:38 -07001657 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08001658 for (int index = 0; index < getChildCount(); index++) {
1659 final int i = index;
1660 final CellLayout cl = (CellLayout) getChildAt(i);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001661 if (mOldAlphas[i] == 0 && mNewAlphas[i] == 0) {
Michael Jurka7372c592012-01-16 04:21:35 -08001662 cl.setTranslationX(mNewTranslationXs[i]);
1663 cl.setTranslationY(mNewTranslationYs[i]);
1664 cl.setScaleX(mNewScaleXs[i]);
1665 cl.setScaleY(mNewScaleYs[i]);
1666 cl.setBackgroundAlpha(mNewBackgroundAlphas[i]);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001667 cl.setBackgroundAlphaMultiplier(mNewBackgroundAlphaMultipliers[i]);
Michael Jurka7372c592012-01-16 04:21:35 -08001668 cl.setAlpha(mNewAlphas[i]);
1669 cl.setRotationY(mNewRotationYs[i]);
Michael Jurka7407d2a2011-12-12 21:48:38 -08001670 } else {
1671 LauncherViewPropertyAnimator a = new LauncherViewPropertyAnimator(cl);
1672 a.translationX(mNewTranslationXs[i])
1673 .translationY(mNewTranslationYs[i])
1674 .scaleX(mNewScaleXs[i])
1675 .scaleY(mNewScaleYs[i])
1676 .setDuration(duration)
1677 .setInterpolator(mZoomInInterpolator);
1678 if (mOldAlphas[i] != mNewAlphas[i]) {
1679 a.alpha(mNewAlphas[i]);
1680 }
1681 anim.play(a);
1682 if (mOldRotationYs[i] != 0 || mNewRotationYs[i] != 0) {
1683 ValueAnimator rotate = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
1684 rotate.setInterpolator(new DecelerateInterpolator(2.0f));
1685 rotate.addUpdateListener(new LauncherAnimatorUpdateListener() {
1686 public void onAnimationUpdate(float a, float b) {
Winson Chungb33e05f2012-01-30 12:18:04 -08001687 cl.setRotationY(a * mOldRotationYs[i] + b * mNewRotationYs[i]);
Michael Jurka7407d2a2011-12-12 21:48:38 -08001688 }
1689 });
1690 anim.play(rotate);
1691 }
1692 if (mOldBackgroundAlphas[i] != 0 ||
1693 mNewBackgroundAlphas[i] != 0 ||
1694 mOldBackgroundAlphaMultipliers[i] != 0 ||
1695 mNewBackgroundAlphaMultipliers[i] != 0) {
1696 ValueAnimator bgAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
1697 bgAnim.setInterpolator(mZoomInInterpolator);
1698 bgAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
1699 public void onAnimationUpdate(float a, float b) {
Michael Jurka7372c592012-01-16 04:21:35 -08001700 cl.setBackgroundAlpha(
Michael Jurka7407d2a2011-12-12 21:48:38 -08001701 a * mOldBackgroundAlphas[i] +
1702 b * mNewBackgroundAlphas[i]);
1703 cl.setBackgroundAlphaMultiplier(
1704 a * mOldBackgroundAlphaMultipliers[i] +
1705 b * mNewBackgroundAlphaMultipliers[i]);
1706 }
1707 });
1708 anim.play(bgAnim);
1709 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001710 }
1711 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001712 anim.setStartDelay(delay);
Adam Cohen7777d962011-08-18 18:58:38 -07001713 }
1714
Winson Chung1b7c1d32011-10-25 12:09:38 -07001715 if (stateIsSpringLoaded) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07001716 // Right now we're covered by Apps Customize
1717 // Show the background gradient immediately, so the gradient will
1718 // be showing once AppsCustomize disappears
1719 animateBackgroundGradient(getResources().getInteger(
1720 R.integer.config_appsCustomizeSpringLoadedBgAlpha) / 100f, false);
1721 } else {
1722 // Fade the background gradient away
1723 animateBackgroundGradient(0f, true);
1724 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001725 return anim;
Michael Jurkadee05892010-07-27 10:01:56 -07001726 }
1727
Michael Jurkabed61d22012-02-14 22:51:29 -08001728 @Override
1729 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
1730 mIsSwitchingState = true;
1731 }
1732
1733 @Override
Winson Chung70442722012-02-10 15:43:22 -08001734 public void onLauncherTransitionStep(Launcher l, float t) {
1735 mTransitionProgress = t;
1736 }
1737
1738 @Override
Michael Jurkabed61d22012-02-14 22:51:29 -08001739 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
1740 mIsSwitchingState = false;
1741 mWallpaperOffset.setOverrideHorizontalCatchupConstant(false);
1742 updateChildrenLayersEnabled();
1743 // The code in getChangeStateAnimation to determine initialAlpha and finalAlpha will ensure
1744 // ensure that only the current page is visible during (and subsequently, after) the
1745 // transition animation. If fade adjacent pages is disabled, then re-enable the page
1746 // visibility after the transition animation.
1747 if (!mFadeInAdjacentScreens) {
1748 for (int i = 0; i < getChildCount(); i++) {
1749 final CellLayout cl = (CellLayout) getChildAt(i);
1750 cl.setAlpha(1f);
1751 }
1752 }
1753 }
1754
1755 @Override
1756 public View getContent() {
1757 return this;
1758 }
1759
Joe Onorato4be866d2010-10-10 11:26:02 -07001760 /**
1761 * Draw the View v into the given Canvas.
1762 *
1763 * @param v the view to draw
1764 * @param destCanvas the canvas to draw on
1765 * @param padding the horizontal and vertical padding to use when drawing
1766 */
Adam Cohenac8c8762011-07-13 11:15:27 -07001767 private void drawDragView(View v, Canvas destCanvas, int padding, boolean pruneToDrawable) {
Joe Onorato4be866d2010-10-10 11:26:02 -07001768 final Rect clipRect = mTempRect;
1769 v.getDrawingRect(clipRect);
1770
Adam Cohen099f60d2011-08-23 21:07:26 -07001771 boolean textVisible = false;
1772
Adam Cohenac8c8762011-07-13 11:15:27 -07001773 destCanvas.save();
1774 if (v instanceof TextView && pruneToDrawable) {
1775 Drawable d = ((TextView) v).getCompoundDrawables()[1];
1776 clipRect.set(0, 0, d.getIntrinsicWidth() + padding, d.getIntrinsicHeight() + padding);
1777 destCanvas.translate(padding / 2, padding / 2);
1778 d.draw(destCanvas);
1779 } else {
1780 if (v instanceof FolderIcon) {
Adam Cohen099f60d2011-08-23 21:07:26 -07001781 // For FolderIcons the text can bleed into the icon area, and so we need to
1782 // hide the text completely (which can't be achieved by clipping).
1783 if (((FolderIcon) v).getTextVisible()) {
1784 ((FolderIcon) v).setTextVisible(false);
1785 textVisible = true;
1786 }
Adam Cohenac8c8762011-07-13 11:15:27 -07001787 } else if (v instanceof BubbleTextView) {
1788 final BubbleTextView tv = (BubbleTextView) v;
1789 clipRect.bottom = tv.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V +
1790 tv.getLayout().getLineTop(0);
1791 } else if (v instanceof TextView) {
1792 final TextView tv = (TextView) v;
1793 clipRect.bottom = tv.getExtendedPaddingTop() - tv.getCompoundDrawablePadding() +
1794 tv.getLayout().getLineTop(0);
1795 }
1796 destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
1797 destCanvas.clipRect(clipRect, Op.REPLACE);
1798 v.draw(destCanvas);
Adam Cohen099f60d2011-08-23 21:07:26 -07001799
1800 // Restore text visibility of FolderIcon if necessary
1801 if (textVisible) {
1802 ((FolderIcon) v).setTextVisible(true);
1803 }
Adam Cohenac8c8762011-07-13 11:15:27 -07001804 }
1805 destCanvas.restore();
1806 }
1807
1808 /**
1809 * Returns a new bitmap to show when the given View is being dragged around.
1810 * Responsibility for the bitmap is transferred to the caller.
1811 */
1812 public Bitmap createDragBitmap(View v, Canvas canvas, int padding) {
Winson Chungea359c62011-08-03 17:06:35 -07001813 final int outlineColor = getResources().getColor(android.R.color.holo_blue_light);
Adam Cohenac8c8762011-07-13 11:15:27 -07001814 Bitmap b;
1815
1816 if (v instanceof TextView) {
1817 Drawable d = ((TextView) v).getCompoundDrawables()[1];
1818 b = Bitmap.createBitmap(d.getIntrinsicWidth() + padding,
1819 d.getIntrinsicHeight() + padding, Bitmap.Config.ARGB_8888);
1820 } else {
1821 b = Bitmap.createBitmap(
1822 v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
Joe Onorato4be866d2010-10-10 11:26:02 -07001823 }
1824
Adam Cohenac8c8762011-07-13 11:15:27 -07001825 canvas.setBitmap(b);
1826 drawDragView(v, canvas, padding, true);
1827 mOutlineHelper.applyOuterBlur(b, canvas, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -07001828 canvas.setBitmap(null);
Joe Onorato4be866d2010-10-10 11:26:02 -07001829
Adam Cohenac8c8762011-07-13 11:15:27 -07001830 return b;
Joe Onorato4be866d2010-10-10 11:26:02 -07001831 }
1832
1833 /**
1834 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
1835 * Responsibility for the bitmap is transferred to the caller.
1836 */
1837 private Bitmap createDragOutline(View v, Canvas canvas, int padding) {
Winson Chungea359c62011-08-03 17:06:35 -07001838 final int outlineColor = getResources().getColor(android.R.color.holo_blue_light);
Joe Onorato4be866d2010-10-10 11:26:02 -07001839 final Bitmap b = Bitmap.createBitmap(
1840 v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
1841
1842 canvas.setBitmap(b);
Winson Chungb8c69f32011-10-19 21:36:08 -07001843 drawDragView(v, canvas, padding, true);
Adam Cohen5bb50bd2010-12-03 11:39:55 -08001844 mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -07001845 canvas.setBitmap(null);
Joe Onorato4be866d2010-10-10 11:26:02 -07001846 return b;
1847 }
1848
1849 /**
Michael Jurkad3ef3062010-11-23 16:23:58 -08001850 * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
1851 * Responsibility for the bitmap is transferred to the caller.
1852 */
Peter Ng8db70002011-10-25 15:40:08 -07001853 private Bitmap createDragOutline(Bitmap orig, Canvas canvas, int padding, int w, int h,
1854 Paint alphaClipPaint) {
Winson Chungea359c62011-08-03 17:06:35 -07001855 final int outlineColor = getResources().getColor(android.R.color.holo_blue_light);
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001856 final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001857 canvas.setBitmap(b);
Michael Jurkaf12c75c2011-01-25 22:41:40 -08001858
1859 Rect src = new Rect(0, 0, orig.getWidth(), orig.getHeight());
1860 float scaleFactor = Math.min((w - padding) / (float) orig.getWidth(),
1861 (h - padding) / (float) orig.getHeight());
1862 int scaledWidth = (int) (scaleFactor * orig.getWidth());
1863 int scaledHeight = (int) (scaleFactor * orig.getHeight());
1864 Rect dst = new Rect(0, 0, scaledWidth, scaledHeight);
1865
1866 // center the image
1867 dst.offset((w - scaledWidth) / 2, (h - scaledHeight) / 2);
1868
Winson Chung1120e032011-11-22 16:11:31 -08001869 canvas.drawBitmap(orig, src, dst, null);
Peter Ng8db70002011-10-25 15:40:08 -07001870 mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor,
1871 alphaClipPaint);
Adam Cohenaaf473c2011-08-03 12:02:47 -07001872 canvas.setBitmap(null);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001873
1874 return b;
1875 }
1876
1877 /**
Winson Chunga9abd0e2010-10-27 17:18:37 -07001878 * Creates a drag outline to represent a drop (that we don't have the actual information for
1879 * yet). May be changed in the future to alter the drop outline slightly depending on the
1880 * clip description mime data.
1881 */
1882 private Bitmap createExternalDragOutline(Canvas canvas, int padding) {
1883 Resources r = getResources();
Winson Chungea359c62011-08-03 17:06:35 -07001884 final int outlineColor = r.getColor(android.R.color.holo_blue_light);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001885 final int iconWidth = r.getDimensionPixelSize(R.dimen.workspace_cell_width);
1886 final int iconHeight = r.getDimensionPixelSize(R.dimen.workspace_cell_height);
1887 final int rectRadius = r.getDimensionPixelSize(R.dimen.external_drop_icon_rect_radius);
1888 final int inset = (int) (Math.min(iconWidth, iconHeight) * 0.2f);
1889 final Bitmap b = Bitmap.createBitmap(
1890 iconWidth + padding, iconHeight + padding, Bitmap.Config.ARGB_8888);
1891
1892 canvas.setBitmap(b);
1893 canvas.drawRoundRect(new RectF(inset, inset, iconWidth - inset, iconHeight - inset),
1894 rectRadius, rectRadius, mExternalDragOutlinePaint);
Adam Cohen5bb50bd2010-12-03 11:39:55 -08001895 mOutlineHelper.applyMediumExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor);
Adam Cohenaaf473c2011-08-03 12:02:47 -07001896 canvas.setBitmap(null);
Winson Chunga9abd0e2010-10-27 17:18:37 -07001897 return b;
1898 }
1899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 void startDrag(CellLayout.CellInfo cellInfo) {
1901 View child = cellInfo.cell;
Winson Chungaafa03c2010-06-11 17:34:16 -07001902
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 // Make sure the drag was started by a long press as opposed to a long click.
Bjorn Bringert7984c942009-12-09 15:38:25 +00001904 if (!child.isInTouchMode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 return;
1906 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001907
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 mDragInfo = cellInfo;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001909 child.setVisibility(GONE);
Joe Onorato4be866d2010-10-10 11:26:02 -07001910
1911 child.clearFocus();
1912 child.setPressed(false);
1913
1914 final Canvas canvas = new Canvas();
1915
Patrick Dubroy8e58e912010-10-14 13:21:48 -07001916 // We need to add extra padding to the bitmap to make room for the glow effect
Michael Jurka38b4f7c2010-12-14 16:46:39 -08001917 final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
Patrick Dubroy8e58e912010-10-14 13:21:48 -07001918
Joe Onorato4be866d2010-10-10 11:26:02 -07001919 // The outline is used to visualize where the item will land if dropped
Patrick Dubroy8e58e912010-10-14 13:21:48 -07001920 mDragOutline = createDragOutline(child, canvas, bitmapPadding);
Adam Cohenac8c8762011-07-13 11:15:27 -07001921 beginDragShared(child, this);
1922 }
1923
1924 public void beginDragShared(View child, DragSource source) {
Winson Chungb8c69f32011-10-19 21:36:08 -07001925 Resources r = getResources();
1926
Adam Cohenac8c8762011-07-13 11:15:27 -07001927 // We need to add extra padding to the bitmap to make room for the glow effect
1928 final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
Joe Onorato4be866d2010-10-10 11:26:02 -07001929
1930 // The drag bitmap follows the touch point around on the screen
Adam Cohenac8c8762011-07-13 11:15:27 -07001931 final Bitmap b = createDragBitmap(child, new Canvas(), bitmapPadding);
Joe Onorato4be866d2010-10-10 11:26:02 -07001932
1933 final int bmpWidth = b.getWidth();
Adam Cohene3e27a82011-04-15 12:07:39 -07001934
Adam Cohen8dfcba42011-07-07 16:38:18 -07001935 mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY);
1936 final int dragLayerX = (int) mTempXY[0] + (child.getWidth() - bmpWidth) / 2;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001937 int dragLayerY = mTempXY[1] - bitmapPadding / 2;
Adam Cohene3e27a82011-04-15 12:07:39 -07001938
Winson Chungb8c69f32011-10-19 21:36:08 -07001939 Point dragVisualizeOffset = null;
Adam Cohene3e27a82011-04-15 12:07:39 -07001940 Rect dragRect = null;
Winson Chungb8c69f32011-10-19 21:36:08 -07001941 if (child instanceof BubbleTextView || child instanceof PagedViewIcon) {
1942 int iconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
1943 int iconPaddingTop = r.getDimensionPixelSize(R.dimen.app_icon_padding_top);
Adam Cohene3e27a82011-04-15 12:07:39 -07001944 int top = child.getPaddingTop();
1945 int left = (bmpWidth - iconSize) / 2;
1946 int right = left + iconSize;
1947 int bottom = top + iconSize;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001948 dragLayerY += top;
Winson Chungb8c69f32011-10-19 21:36:08 -07001949 // Note: The drag region is used to calculate drag layer offsets, but the
1950 // dragVisualizeOffset in addition to the dragRect (the size) to position the outline.
1951 dragVisualizeOffset = new Point(-bitmapPadding / 2, iconPaddingTop - bitmapPadding / 2);
Adam Cohene3e27a82011-04-15 12:07:39 -07001952 dragRect = new Rect(left, top, right, bottom);
Adam Cohen0e4857c2011-06-30 17:05:14 -07001953 } else if (child instanceof FolderIcon) {
Winson Chungb8c69f32011-10-19 21:36:08 -07001954 int previewSize = r.getDimensionPixelSize(R.dimen.folder_preview_size);
Adam Cohen0e4857c2011-06-30 17:05:14 -07001955 dragRect = new Rect(0, 0, child.getWidth(), previewSize);
Adam Cohene3e27a82011-04-15 12:07:39 -07001956 }
1957
Winson Chung1e9cbfe2011-09-30 16:52:26 -07001958 // Clear the pressed state if necessary
1959 if (child instanceof BubbleTextView) {
1960 BubbleTextView icon = (BubbleTextView) child;
1961 icon.clearPressedOrFocusedBackground();
1962 }
1963
Adam Cohenac8c8762011-07-13 11:15:27 -07001964 mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(),
Winson Chungb8c69f32011-10-19 21:36:08 -07001965 DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect);
Joe Onorato4be866d2010-10-10 11:26:02 -07001966 b.recycle();
Winson Chung1afedc32012-01-23 16:14:56 -08001967
1968 // Show the scrolling indicator when you pick up an item
1969 showScrollingIndicator(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 }
1971
Winson Chung3d503fb2011-07-13 17:25:49 -07001972 void addApplicationShortcut(ShortcutInfo info, CellLayout target, long container, int screen,
1973 int cellX, int cellY, boolean insertAtFirst, int intersectX, int intersectY) {
1974 View view = mLauncher.createShortcut(R.layout.application, target, (ShortcutInfo) info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001975
1976 final int[] cellXY = new int[2];
Winson Chung3d503fb2011-07-13 17:25:49 -07001977 target.findCellForSpanThatIntersects(cellXY, 1, 1, intersectX, intersectY);
1978 addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1, insertAtFirst);
1979 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screen, cellXY[0],
1980 cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 }
1982
Adam Cohen4b285c52011-07-21 14:24:06 -07001983 public boolean transitionStateShouldAllowDrop() {
Adam Cohen1b36dc32012-02-13 19:27:37 -08001984 return ((!isSwitchingState() || mTransitionProgress > 0.5f) && mState != State.SMALL);
Adam Cohen4b285c52011-07-21 14:24:06 -07001985 }
1986
Patrick Dubroycd68ff52010-10-28 17:57:05 -07001987 /**
1988 * {@inheritDoc}
1989 */
Adam Cohencb3382b2011-05-24 14:07:08 -07001990 public boolean acceptDrop(DragObject d) {
Patrick Dubroycd68ff52010-10-28 17:57:05 -07001991 // If it's an external drop (e.g. from All Apps), check if it should be accepted
Adam Cohencb3382b2011-05-24 14:07:08 -07001992 if (d.dragSource != this) {
Patrick Dubroycd68ff52010-10-28 17:57:05 -07001993 // Don't accept the drop if we're not over a screen at time of drop
Adam Cohena65beee2011-06-27 21:32:23 -07001994 if (mDragTargetLayout == null) {
Patrick Dubroycd68ff52010-10-28 17:57:05 -07001995 return false;
1996 }
Adam Cohen4b285c52011-07-21 14:24:06 -07001997 if (!transitionStateShouldAllowDrop()) return false;
Patrick Dubroycd68ff52010-10-28 17:57:05 -07001998
Adam Cohena65beee2011-06-27 21:32:23 -07001999 mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset,
2000 d.dragView, mDragViewVisualCenter);
2001
Adam Cohen2e9f4fb2011-08-30 22:46:55 -07002002 // We want the point to be mapped to the dragTarget.
2003 if (mLauncher.isHotseatLayout(mDragTargetLayout)) {
2004 mapPointFromSelfToSibling(mLauncher.getHotseat(), mDragViewVisualCenter);
2005 } else {
2006 mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null);
2007 }
2008
Winson Chung557d6ed2011-07-08 15:34:52 -07002009 int spanX = 1;
2010 int spanY = 1;
2011 View ignoreView = null;
2012 if (mDragInfo != null) {
2013 final CellLayout.CellInfo dragCellInfo = mDragInfo;
2014 spanX = dragCellInfo.spanX;
2015 spanY = dragCellInfo.spanY;
2016 ignoreView = dragCellInfo.cell;
2017 } else {
2018 final ItemInfo dragInfo = (ItemInfo) d.dragInfo;
2019 spanX = dragInfo.spanX;
2020 spanY = dragInfo.spanY;
2021 }
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002022
Adam Cohena65beee2011-06-27 21:32:23 -07002023 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
2024 (int) mDragViewVisualCenter[1], spanX, spanY, mDragTargetLayout, mTargetCell);
Adam Cohena65beee2011-06-27 21:32:23 -07002025 if (willCreateUserFolder((ItemInfo) d.dragInfo, mDragTargetLayout, mTargetCell, true)) {
2026 return true;
2027 }
2028 if (willAddToExistingUserFolder((ItemInfo) d.dragInfo, mDragTargetLayout,
2029 mTargetCell)) {
2030 return true;
2031 }
2032
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002033 // Don't accept the drop if there's no room for the item
2034 if (!mDragTargetLayout.findCellForSpanIgnoring(null, spanX, spanY, ignoreView)) {
Winson Chung96ef4092011-11-22 12:25:14 -08002035 // Don't show the message if we are dropping on the AllApps button and the hotseat
2036 // is full
2037 if (mTargetCell != null && mLauncher.isHotseatLayout(mDragTargetLayout)) {
2038 Hotseat hotseat = mLauncher.getHotseat();
2039 if (Hotseat.isAllAppsButtonRank(
2040 hotseat.getOrderInHotseat(mTargetCell[0], mTargetCell[1]))) {
2041 return false;
2042 }
2043 }
2044
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002045 mLauncher.showOutOfSpaceMessage();
2046 return false;
2047 }
2048 }
2049 return true;
2050 }
2051
Adam Cohena65beee2011-06-27 21:32:23 -07002052 boolean willCreateUserFolder(ItemInfo info, CellLayout target, int[] targetCell,
2053 boolean considerTimeout) {
2054 View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
2055
Winson Chung3d503fb2011-07-13 17:25:49 -07002056 boolean hasntMoved = false;
2057 if (mDragInfo != null) {
2058 CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell);
2059 hasntMoved = (mDragInfo.cellX == targetCell[0] &&
2060 mDragInfo.cellY == targetCell[1]) && (cellParent == target);
2061 }
Adam Cohene0310962011-04-18 16:15:31 -07002062
Adam Cohena65beee2011-06-27 21:32:23 -07002063 if (dropOverView == null || hasntMoved || (considerTimeout && !mCreateUserFolderOnDrop)) {
2064 return false;
2065 }
Adam Cohene0310962011-04-18 16:15:31 -07002066
Adam Cohena65beee2011-06-27 21:32:23 -07002067 boolean aboveShortcut = (dropOverView.getTag() instanceof ShortcutInfo);
Adam Cohene0310962011-04-18 16:15:31 -07002068 boolean willBecomeShortcut =
Adam Cohenc0dcf592011-06-01 15:30:43 -07002069 (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
2070 info.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT);
Adam Cohene0310962011-04-18 16:15:31 -07002071
2072 return (aboveShortcut && willBecomeShortcut);
2073 }
2074
Adam Cohena65beee2011-06-27 21:32:23 -07002075 boolean willAddToExistingUserFolder(Object dragInfo, CellLayout target, int[] targetCell) {
2076 View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
2077 if (dropOverView instanceof FolderIcon) {
2078 FolderIcon fi = (FolderIcon) dropOverView;
2079 if (fi.acceptDrop(dragInfo)) {
2080 return true;
2081 }
2082 }
2083 return false;
2084 }
2085
Winson Chung3d503fb2011-07-13 17:25:49 -07002086 boolean createUserFolderIfNecessary(View newView, long container, CellLayout target,
Winson Chung557d6ed2011-07-08 15:34:52 -07002087 int[] targetCell, boolean external, DragView dragView, Runnable postAnimationRunnable) {
Adam Cohenc0dcf592011-06-01 15:30:43 -07002088 View v = target.getChildAt(targetCell[0], targetCell[1]);
Winson Chungec9a0a42011-07-20 20:42:13 -07002089 boolean hasntMoved = false;
2090 if (mDragInfo != null) {
2091 CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell);
2092 hasntMoved = (mDragInfo.cellX == targetCell[0] &&
2093 mDragInfo.cellY == targetCell[1]) && (cellParent == target);
2094 }
Adam Cohen10b17372011-04-15 14:21:25 -07002095
Adam Cohen19072da2011-05-31 14:30:45 -07002096 if (v == null || hasntMoved || !mCreateUserFolderOnDrop) return false;
2097 mCreateUserFolderOnDrop = false;
Adam Cohenc0dcf592011-06-01 15:30:43 -07002098 final int screen = (targetCell == null) ? mDragInfo.screen : indexOfChild(target);
Adam Cohen10b17372011-04-15 14:21:25 -07002099
2100 boolean aboveShortcut = (v.getTag() instanceof ShortcutInfo);
2101 boolean willBecomeShortcut = (newView.getTag() instanceof ShortcutInfo);
2102
2103 if (aboveShortcut && willBecomeShortcut) {
2104 ShortcutInfo sourceInfo = (ShortcutInfo) newView.getTag();
2105 ShortcutInfo destInfo = (ShortcutInfo) v.getTag();
2106 // if the drag started here, we need to remove it from the workspace
2107 if (!external) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002108 getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell);
Adam Cohen10b17372011-04-15 14:21:25 -07002109 }
2110
Adam Cohend0445262011-07-04 23:53:22 -07002111 Rect folderLocation = new Rect();
Adam Cohenac8c8762011-07-13 11:15:27 -07002112 float scale = mLauncher.getDragLayer().getDescendantRectRelativeToSelf(v, folderLocation);
Adam Cohen10b17372011-04-15 14:21:25 -07002113 target.removeView(v);
Adam Cohend0445262011-07-04 23:53:22 -07002114
Winson Chung3d503fb2011-07-13 17:25:49 -07002115 FolderIcon fi =
2116 mLauncher.addFolder(target, container, screen, targetCell[0], targetCell[1]);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002117 destInfo.cellX = -1;
2118 destInfo.cellY = -1;
2119 sourceInfo.cellX = -1;
2120 sourceInfo.cellY = -1;
Adam Cohend0445262011-07-04 23:53:22 -07002121
Adam Cohen558baaf2011-08-15 15:22:57 -07002122 // If the dragView is null, we can't animate
2123 boolean animate = dragView != null;
2124 if (animate) {
2125 fi.performCreateAnimation(destInfo, v, sourceInfo, dragView, folderLocation, scale,
2126 postAnimationRunnable);
2127 } else {
2128 fi.addItem(destInfo);
2129 fi.addItem(sourceInfo);
2130 }
Adam Cohen10b17372011-04-15 14:21:25 -07002131 return true;
2132 }
2133 return false;
2134 }
2135
Adam Cohena65beee2011-06-27 21:32:23 -07002136 boolean addToExistingFolderIfNecessary(View newView, CellLayout target, int[] targetCell,
Adam Cohen3e8f8112011-07-02 18:03:00 -07002137 DragObject d, boolean external) {
Adam Cohena65beee2011-06-27 21:32:23 -07002138 View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
Adam Cohenc0dcf592011-06-01 15:30:43 -07002139 if (dropOverView instanceof FolderIcon) {
2140 FolderIcon fi = (FolderIcon) dropOverView;
Adam Cohen3e8f8112011-07-02 18:03:00 -07002141 if (fi.acceptDrop(d.dragInfo)) {
2142 fi.onDrop(d);
Adam Cohenc0dcf592011-06-01 15:30:43 -07002143
2144 // if the drag started here, we need to remove it from the workspace
2145 if (!external) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002146 getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell);
Adam Cohenc0dcf592011-06-01 15:30:43 -07002147 }
2148 return true;
2149 }
2150 }
2151 return false;
2152 }
2153
Adam Cohencb3382b2011-05-24 14:07:08 -07002154 public void onDrop(DragObject d) {
Adam Cohencb3382b2011-05-24 14:07:08 -07002155 mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView,
Adam Cohene3e27a82011-04-15 12:07:39 -07002156 mDragViewVisualCenter);
2157
2158 // We want the point to be mapped to the dragTarget.
Adam Cohenba781612011-05-09 14:37:39 -07002159 if (mDragTargetLayout != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002160 if (mLauncher.isHotseatLayout(mDragTargetLayout)) {
2161 mapPointFromSelfToSibling(mLauncher.getHotseat(), mDragViewVisualCenter);
2162 } else {
2163 mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null);
2164 }
Adam Cohenba781612011-05-09 14:37:39 -07002165 }
Michael Jurkac6ee42e2010-09-30 12:04:50 -07002166
Adam Cohen76078c42011-06-09 15:06:52 -07002167 CellLayout dropTargetLayout = mDragTargetLayout;
Michael Jurkab09cac52011-01-05 20:23:39 -08002168
Adam Cohened51cc92011-08-01 20:28:08 -07002169 int snapScreen = -1;
Adam Cohencb3382b2011-05-24 14:07:08 -07002170 if (d.dragSource != this) {
Adam Cohene3e27a82011-04-15 12:07:39 -07002171 final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0],
2172 (int) mDragViewVisualCenter[1] };
Adam Cohen3e8f8112011-07-02 18:03:00 -07002173 onDropExternal(touchXY, d.dragInfo, dropTargetLayout, false, d);
Patrick Dubroyce34a972010-10-19 10:34:32 -07002174 } else if (mDragInfo != null) {
Patrick Dubroyce34a972010-10-19 10:34:32 -07002175 final View cell = mDragInfo.cell;
Patrick Dubroy54fa3b92010-11-17 12:18:45 -08002176
Winson Chungea359c62011-08-03 17:06:35 -07002177 if (dropTargetLayout != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002178 // Move internally
Winson Chung40e882b2011-07-21 19:01:11 -07002179 boolean hasMovedLayouts = (getParentCellLayoutForView(cell) != dropTargetLayout);
2180 boolean hasMovedIntoHotseat = mLauncher.isHotseatLayout(dropTargetLayout);
2181 long container = hasMovedIntoHotseat ?
Winson Chung3d503fb2011-07-13 17:25:49 -07002182 LauncherSettings.Favorites.CONTAINER_HOTSEAT :
2183 LauncherSettings.Favorites.CONTAINER_DESKTOP;
2184 int screen = (mTargetCell[0] < 0) ?
2185 mDragInfo.screen : indexOfChild(dropTargetLayout);
Adam Cohenc0dcf592011-06-01 15:30:43 -07002186 int spanX = mDragInfo != null ? mDragInfo.spanX : 1;
2187 int spanY = mDragInfo != null ? mDragInfo.spanY : 1;
2188 // First we find the cell nearest to point at which the item is
2189 // dropped, without any consideration to whether there is an item there.
2190 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int)
2191 mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell);
2192 // If the item being dropped is a shortcut and the nearest drop
Adam Cohen76078c42011-06-09 15:06:52 -07002193 // cell also contains a shortcut, then create a folder with the two shortcuts.
Winson Chung1c4cf4a2011-07-29 14:49:10 -07002194 if (!mInScrollArea && createUserFolderIfNecessary(cell, container,
Winson Chung3d503fb2011-07-13 17:25:49 -07002195 dropTargetLayout, mTargetCell, false, d.dragView, null)) {
Adam Cohenc0dcf592011-06-01 15:30:43 -07002196 return;
2197 }
2198
Adam Cohen3e8f8112011-07-02 18:03:00 -07002199 if (addToExistingFolderIfNecessary(cell, dropTargetLayout, mTargetCell, d, false)) {
Adam Cohendf035382011-04-11 17:22:04 -07002200 return;
2201 }
2202
2203 // Aside from the special case where we're dropping a shortcut onto a shortcut,
2204 // we need to find the nearest cell location that is vacant
Adam Cohene3e27a82011-04-15 12:07:39 -07002205 mTargetCell = findNearestVacantArea((int) mDragViewVisualCenter[0],
2206 (int) mDragViewVisualCenter[1], mDragInfo.spanX, mDragInfo.spanY, cell,
2207 dropTargetLayout, mTargetCell);
Adam Cohendf035382011-04-11 17:22:04 -07002208
Michael Jurka7cfc2822011-08-02 20:19:24 -07002209 if (mCurrentPage != screen && !hasMovedIntoHotseat) {
Adam Cohened51cc92011-08-01 20:28:08 -07002210 snapScreen = screen;
Patrick Dubroy54fa3b92010-11-17 12:18:45 -08002211 snapToPage(screen);
2212 }
2213
Adam Cohenc0dcf592011-06-01 15:30:43 -07002214 if (mTargetCell[0] >= 0 && mTargetCell[1] >= 0) {
Winson Chung40e882b2011-07-21 19:01:11 -07002215 if (hasMovedLayouts) {
Patrick Dubroy94383362010-10-29 15:03:24 -07002216 // Reparent the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002217 getParentCellLayoutForView(cell).removeView(cell);
2218 addInScreen(cell, container, screen, mTargetCell[0], mTargetCell[1],
2219 mDragInfo.spanX, mDragInfo.spanY);
Patrick Dubroy94383362010-10-29 15:03:24 -07002220 }
2221
2222 // update the item's position after drop
2223 final ItemInfo info = (ItemInfo) cell.getTag();
2224 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
Patrick Dubroy54fa3b92010-11-17 12:18:45 -08002225 dropTargetLayout.onMove(cell, mTargetCell[0], mTargetCell[1]);
Patrick Dubroy94383362010-10-29 15:03:24 -07002226 lp.cellX = mTargetCell[0];
2227 lp.cellY = mTargetCell[1];
Winson Chung3d503fb2011-07-13 17:25:49 -07002228 cell.setId(LauncherModel.getCellLayoutChildId(container, mDragInfo.screen,
Patrick Dubroy94383362010-10-29 15:03:24 -07002229 mTargetCell[0], mTargetCell[1], mDragInfo.spanX, mDragInfo.spanY));
2230
Winson Chung3d503fb2011-07-13 17:25:49 -07002231 if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2232 cell instanceof LauncherAppWidgetHostView) {
Adam Cohend4844c32011-02-18 19:25:06 -08002233 final CellLayout cellLayout = dropTargetLayout;
2234 // We post this call so that the widget has a chance to be placed
2235 // in its final location
2236
2237 final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell;
2238 AppWidgetProviderInfo pinfo = hostView.getAppWidgetInfo();
Adam Cohen27c09b02011-02-28 14:45:11 -08002239 if (pinfo.resizeMode != AppWidgetProviderInfo.RESIZE_NONE) {
Adam Cohen26976d92011-03-22 15:33:33 -07002240 final Runnable resizeRunnable = new Runnable() {
Adam Cohend4844c32011-02-18 19:25:06 -08002241 public void run() {
Adam Cohen716b51e2011-06-30 12:09:54 -07002242 DragLayer dragLayer = mLauncher.getDragLayer();
Adam Cohenc0dcf592011-06-01 15:30:43 -07002243 dragLayer.addResizeFrame(info, hostView, cellLayout);
Adam Cohend4844c32011-02-18 19:25:06 -08002244 }
Adam Cohen26976d92011-03-22 15:33:33 -07002245 };
2246 post(new Runnable() {
2247 public void run() {
2248 if (!isPageMoving()) {
2249 resizeRunnable.run();
2250 } else {
2251 mDelayedResizeRunnable = resizeRunnable;
2252 }
2253 }
Adam Cohend4844c32011-02-18 19:25:06 -08002254 });
2255 }
2256 }
2257
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 LauncherModel.moveItemInDatabase(mLauncher, info, container, screen, lp.cellX,
2259 lp.cellY);
Patrick Dubroycd68ff52010-10-28 17:57:05 -07002260 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 }
Patrick Dubroyce34a972010-10-19 10:34:32 -07002262
Michael Jurka8c920dd2011-01-20 14:16:56 -08002263 final CellLayout parent = (CellLayout) cell.getParent().getParent();
Patrick Dubroyce34a972010-10-19 10:34:32 -07002264
2265 // Prepare it to be animated into its new position
2266 // This must be called after the view has been re-parented
Michael Jurkad74c9842011-07-10 12:44:21 -07002267 final Runnable disableHardwareLayersRunnable = new Runnable() {
2268 @Override
2269 public void run() {
2270 mAnimatingViewIntoPlace = false;
2271 updateChildrenLayersEnabled();
2272 }
2273 };
2274 mAnimatingViewIntoPlace = true;
Adam Cohenfc53cd22011-07-20 15:45:11 -07002275 if (d.dragView.hasDrawn()) {
Adam Cohened51cc92011-08-01 20:28:08 -07002276 int duration = snapScreen < 0 ? -1 : ADJACENT_SCREEN_DROP_DURATION;
Adam Cohened51cc92011-08-01 20:28:08 -07002277 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration,
Adam Cohen6441de02011-12-14 14:25:32 -08002278 disableHardwareLayersRunnable, this);
Adam Cohenfc53cd22011-07-20 15:45:11 -07002279 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -08002280 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07002281 cell.setVisibility(VISIBLE);
2282 }
Adam Cohen716b51e2011-06-30 12:09:54 -07002283 parent.onDropChild(cell);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 }
2285 }
2286
Adam Cohened51cc92011-08-01 20:28:08 -07002287 public void setFinalScrollForPageChange(int screen) {
2288 if (screen >= 0) {
2289 mSavedScrollX = getScrollX();
2290 CellLayout cl = (CellLayout) getChildAt(screen);
2291 mSavedTranslationX = cl.getTranslationX();
2292 mSavedRotationY = cl.getRotationY();
2293 final int newX = getChildOffset(screen) - getRelativeChildOffset(screen);
2294 setScrollX(newX);
2295 cl.setTranslationX(0f);
2296 cl.setRotationY(0f);
2297 }
2298 }
2299
2300 public void resetFinalScrollForPageChange(int screen) {
2301 if (screen >= 0) {
2302 CellLayout cl = (CellLayout) getChildAt(screen);
2303 setScrollX(mSavedScrollX);
2304 cl.setTranslationX(mSavedTranslationX);
2305 cl.setRotationY(mSavedRotationY);
2306 }
2307 }
2308
Adam Cohen76078c42011-06-09 15:06:52 -07002309 public void getViewLocationRelativeToSelf(View v, int[] location) {
Adam Cohen8dfcba42011-07-07 16:38:18 -07002310 getLocationInWindow(location);
Adam Cohene3e27a82011-04-15 12:07:39 -07002311 int x = location[0];
2312 int y = location[1];
2313
Adam Cohen8dfcba42011-07-07 16:38:18 -07002314 v.getLocationInWindow(location);
Adam Cohene3e27a82011-04-15 12:07:39 -07002315 int vX = location[0];
2316 int vY = location[1];
2317
2318 location[0] = vX - x;
2319 location[1] = vY - y;
2320 }
2321
Adam Cohencb3382b2011-05-24 14:07:08 -07002322 public void onDragEnter(DragObject d) {
Adam Cohen8a18afc2011-12-13 15:57:01 -08002323 mDragHasEnteredWorkspace = true;
Winson Chungb26f3d62011-06-02 10:49:29 -07002324 if (mDragTargetLayout != null) {
Winson Chungc07918d2011-07-01 15:35:26 -07002325 mDragTargetLayout.setIsDragOverlapping(false);
Winson Chungb26f3d62011-06-02 10:49:29 -07002326 mDragTargetLayout.onDragExit();
Winson Chungb26f3d62011-06-02 10:49:29 -07002327 }
Winson Chungc07918d2011-07-01 15:35:26 -07002328 mDragTargetLayout = getCurrentDropLayout();
2329 mDragTargetLayout.setIsDragOverlapping(true);
2330 mDragTargetLayout.onDragEnter();
Winson Chungb26f3d62011-06-02 10:49:29 -07002331
Winson Chungc07918d2011-07-01 15:35:26 -07002332 // Because we don't have space in the Phone UI (the CellLayouts run to the edge) we
2333 // don't need to show the outlines
2334 if (LauncherApplication.isScreenLarge()) {
2335 showOutlines();
Michael Jurkad718d6a2010-10-14 15:35:17 -07002336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337 }
2338
Winson Chungc07918d2011-07-01 15:35:26 -07002339 private void doDragExit(DragObject d) {
2340 // Clean up folders
2341 cleanupFolderCreation(d);
2342
2343 // Reset the scroll area and previous drag target
2344 onResetScrollArea();
2345
2346 if (mDragTargetLayout != null) {
2347 mDragTargetLayout.setIsDragOverlapping(false);
2348 mDragTargetLayout.onDragExit();
2349 }
Winson Chungc07918d2011-07-01 15:35:26 -07002350 mLastDragOverView = null;
Adam Cohen74c28d12011-11-18 14:17:11 -08002351 mSpringLoadedDragController.cancel();
Winson Chungc07918d2011-07-01 15:35:26 -07002352
2353 if (!mIsPageMoving) {
2354 hideOutlines();
2355 }
2356 }
2357
2358 public void onDragExit(DragObject d) {
Adam Cohen8a18afc2011-12-13 15:57:01 -08002359 mDragHasEnteredWorkspace = false;
Winson Chungc07918d2011-07-01 15:35:26 -07002360 doDragExit(d);
2361 }
2362
Adam Cohencb3382b2011-05-24 14:07:08 -07002363 public DropTarget getDropTargetDelegate(DragObject d) {
Patrick Dubroy440c3602010-07-13 17:50:32 -07002364 return null;
2365 }
2366
Winson Chunga9abd0e2010-10-27 17:18:37 -07002367 /**
Winson Chung580e2772010-11-10 16:03:00 -08002368 * Tests to see if the drop will be accepted by Launcher, and if so, includes additional data
2369 * in the returned structure related to the widgets that match the drop (or a null list if it is
2370 * a shortcut drop). If the drop is not accepted then a null structure is returned.
2371 */
2372 private Pair<Integer, List<WidgetMimeTypeHandlerData>> validateDrag(DragEvent event) {
2373 final LauncherModel model = mLauncher.getModel();
2374 final ClipDescription desc = event.getClipDescription();
2375 final int mimeTypeCount = desc.getMimeTypeCount();
2376 for (int i = 0; i < mimeTypeCount; ++i) {
2377 final String mimeType = desc.getMimeType(i);
2378 if (mimeType.equals(InstallShortcutReceiver.SHORTCUT_MIMETYPE)) {
2379 return new Pair<Integer, List<WidgetMimeTypeHandlerData>>(i, null);
2380 } else {
2381 final List<WidgetMimeTypeHandlerData> widgets =
2382 model.resolveWidgetsForMimeType(mContext, mimeType);
2383 if (widgets.size() > 0) {
2384 return new Pair<Integer, List<WidgetMimeTypeHandlerData>>(i, widgets);
2385 }
2386 }
2387 }
2388 return null;
2389 }
2390
2391 /**
Winson Chunga9abd0e2010-10-27 17:18:37 -07002392 * Global drag and drop handler
2393 */
2394 @Override
2395 public boolean onDragEvent(DragEvent event) {
Winson Chung68846fd2010-10-29 11:00:27 -07002396 final ClipDescription desc = event.getClipDescription();
Winson Chunga9abd0e2010-10-27 17:18:37 -07002397 final CellLayout layout = (CellLayout) getChildAt(mCurrentPage);
2398 final int[] pos = new int[2];
2399 layout.getLocationOnScreen(pos);
2400 // We need to offset the drag coordinates to layout coordinate space
2401 final int x = (int) event.getX() - pos[0];
2402 final int y = (int) event.getY() - pos[1];
2403
2404 switch (event.getAction()) {
Winson Chung580e2772010-11-10 16:03:00 -08002405 case DragEvent.ACTION_DRAG_STARTED: {
2406 // Validate this drag
2407 Pair<Integer, List<WidgetMimeTypeHandlerData>> test = validateDrag(event);
2408 if (test != null) {
2409 boolean isShortcut = (test.second == null);
2410 if (isShortcut) {
2411 // Check if we have enough space on this screen to add a new shortcut
2412 if (!layout.findCellForSpan(pos, 1, 1)) {
Winson Chung557d6ed2011-07-08 15:34:52 -07002413 mLauncher.showOutOfSpaceMessage();
Winson Chung580e2772010-11-10 16:03:00 -08002414 return false;
2415 }
2416 }
2417 } else {
2418 // Show error message if we couldn't accept any of the items
2419 Toast.makeText(mContext, mContext.getString(R.string.external_drop_widget_error),
Winson Chunga9abd0e2010-10-27 17:18:37 -07002420 Toast.LENGTH_SHORT).show();
2421 return false;
2422 }
2423
Winson Chung68846fd2010-10-29 11:00:27 -07002424 // Create the drag outline
2425 // We need to add extra padding to the bitmap to make room for the glow effect
2426 final Canvas canvas = new Canvas();
Michael Jurka38b4f7c2010-12-14 16:46:39 -08002427 final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
Winson Chung68846fd2010-10-29 11:00:27 -07002428 mDragOutline = createExternalDragOutline(canvas, bitmapPadding);
Winson Chunga9abd0e2010-10-27 17:18:37 -07002429
Winson Chung68846fd2010-10-29 11:00:27 -07002430 // Show the current page outlines to indicate that we can accept this drop
2431 showOutlines();
Winson Chung68846fd2010-10-29 11:00:27 -07002432 layout.onDragEnter();
Winson Chungb8c69f32011-10-19 21:36:08 -07002433 layout.visualizeDropLocation(null, mDragOutline, x, y, 1, 1, null, null);
Winson Chunga9abd0e2010-10-27 17:18:37 -07002434
Winson Chung68846fd2010-10-29 11:00:27 -07002435 return true;
Winson Chung580e2772010-11-10 16:03:00 -08002436 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07002437 case DragEvent.ACTION_DRAG_LOCATION:
2438 // Visualize the drop location
Winson Chungb8c69f32011-10-19 21:36:08 -07002439 layout.visualizeDropLocation(null, mDragOutline, x, y, 1, 1, null, null);
Winson Chunga9abd0e2010-10-27 17:18:37 -07002440 return true;
Winson Chung580e2772010-11-10 16:03:00 -08002441 case DragEvent.ACTION_DROP: {
Winson Chunga9abd0e2010-10-27 17:18:37 -07002442 // Try and add any shortcuts
Winson Chunga9abd0e2010-10-27 17:18:37 -07002443 final LauncherModel model = mLauncher.getModel();
2444 final ClipData data = event.getClipData();
Winson Chung55cef262010-10-28 14:14:18 -07002445
Winson Chung68846fd2010-10-29 11:00:27 -07002446 // We assume that the mime types are ordered in descending importance of
2447 // representation. So we enumerate the list of mime types and alert the
2448 // user if any widgets can handle the drop. Only the most preferred
2449 // representation will be handled.
2450 pos[0] = x;
2451 pos[1] = y;
Winson Chung580e2772010-11-10 16:03:00 -08002452 Pair<Integer, List<WidgetMimeTypeHandlerData>> test = validateDrag(event);
2453 if (test != null) {
2454 final int index = test.first;
2455 final List<WidgetMimeTypeHandlerData> widgets = test.second;
2456 final boolean isShortcut = (widgets == null);
2457 final String mimeType = desc.getMimeType(index);
2458 if (isShortcut) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08002459 final Intent intent = data.getItemAt(index).getIntent();
Winson Chung68846fd2010-10-29 11:00:27 -07002460 Object info = model.infoFromShortcutIntent(mContext, intent, data.getIcon());
Adam Cohend9198822011-11-22 16:42:47 -08002461 if (info != null) {
2462 onDropExternal(new int[] { x, y }, info, layout, false);
2463 }
Winson Chung68846fd2010-10-29 11:00:27 -07002464 } else {
Winson Chung580e2772010-11-10 16:03:00 -08002465 if (widgets.size() == 1) {
Winson Chung68846fd2010-10-29 11:00:27 -07002466 // If there is only one item, then go ahead and add and configure
2467 // that widget
2468 final AppWidgetProviderInfo widgetInfo = widgets.get(0).widgetInfo;
2469 final PendingAddWidgetInfo createInfo =
Michael Jurkac9d95c52011-08-29 14:03:34 -07002470 new PendingAddWidgetInfo(widgetInfo, mimeType, data);
Winson Chung3d503fb2011-07-13 17:25:49 -07002471 mLauncher.addAppWidgetFromDrop(createInfo,
2472 LauncherSettings.Favorites.CONTAINER_DESKTOP, mCurrentPage, null, pos);
Winson Chung580e2772010-11-10 16:03:00 -08002473 } else {
Winson Chung68846fd2010-10-29 11:00:27 -07002474 // Show the widget picker dialog if there is more than one widget
2475 // that can handle this data type
2476 final InstallWidgetReceiver.WidgetListAdapter adapter =
2477 new InstallWidgetReceiver.WidgetListAdapter(mLauncher, mimeType,
2478 data, widgets, layout, mCurrentPage, pos);
2479 final AlertDialog.Builder builder =
2480 new AlertDialog.Builder(mContext);
2481 builder.setAdapter(adapter, adapter);
2482 builder.setCancelable(true);
2483 builder.setTitle(mContext.getString(
2484 R.string.external_drop_widget_pick_title));
2485 builder.setIcon(R.drawable.ic_no_applications);
2486 builder.show();
Winson Chung55cef262010-10-28 14:14:18 -07002487 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07002488 }
2489 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07002490 return true;
Winson Chung580e2772010-11-10 16:03:00 -08002491 }
Winson Chunga9abd0e2010-10-27 17:18:37 -07002492 case DragEvent.ACTION_DRAG_ENDED:
2493 // Hide the page outlines after the drop
Winson Chunga9abd0e2010-10-27 17:18:37 -07002494 layout.onDragExit();
2495 hideOutlines();
2496 return true;
2497 }
2498 return super.onDragEvent(event);
2499 }
2500
Michael Jurka4516c112010-10-07 15:13:47 -07002501 /*
2502 *
2503 * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's
2504 * coordinate space. The argument xy is modified with the return result.
2505 *
2506 */
2507 void mapPointFromSelfToChild(View v, float[] xy) {
2508 mapPointFromSelfToChild(v, xy, null);
2509 }
2510
2511 /*
2512 *
2513 * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's
2514 * coordinate space. The argument xy is modified with the return result.
2515 *
2516 * if cachedInverseMatrix is not null, this method will just use that matrix instead of
Michael Jurkad718d6a2010-10-14 15:35:17 -07002517 * computing it itself; we use this to avoid redundant matrix inversions in
Michael Jurka4516c112010-10-07 15:13:47 -07002518 * findMatchingPageForDragOver
2519 *
2520 */
2521 void mapPointFromSelfToChild(View v, float[] xy, Matrix cachedInverseMatrix) {
2522 if (cachedInverseMatrix == null) {
2523 v.getMatrix().invert(mTempInverseMatrix);
2524 cachedInverseMatrix = mTempInverseMatrix;
2525 }
Winson Chung3bc21c32012-01-20 13:59:18 -08002526 int scrollX = mScrollX;
2527 if (mNextPage != INVALID_PAGE) {
2528 scrollX = mScroller.getFinalX();
2529 }
2530 xy[0] = xy[0] + scrollX - v.getLeft();
Michael Jurka4516c112010-10-07 15:13:47 -07002531 xy[1] = xy[1] + mScrollY - v.getTop();
2532 cachedInverseMatrix.mapPoints(xy);
2533 }
2534
2535 /*
Winson Chung3d503fb2011-07-13 17:25:49 -07002536 * Maps a point from the Workspace's coordinate system to another sibling view's. (Workspace
2537 * covers the full screen)
2538 */
2539 void mapPointFromSelfToSibling(View v, float[] xy) {
2540 xy[0] = xy[0] - v.getLeft();
2541 xy[1] = xy[1] - v.getTop();
2542 }
2543
2544 /*
Michael Jurka4516c112010-10-07 15:13:47 -07002545 *
2546 * Convert the 2D coordinate xy from this CellLayout's coordinate space to
2547 * the parent View's coordinate space. The argument xy is modified with the return result.
2548 *
2549 */
2550 void mapPointFromChildToSelf(View v, float[] xy) {
2551 v.getMatrix().mapPoints(xy);
Winson Chung3bc21c32012-01-20 13:59:18 -08002552 int scrollX = mScrollX;
2553 if (mNextPage != INVALID_PAGE) {
2554 scrollX = mScroller.getFinalX();
2555 }
2556 xy[0] -= (scrollX - v.getLeft());
Michael Jurka4516c112010-10-07 15:13:47 -07002557 xy[1] -= (mScrollY - v.getTop());
2558 }
2559
Adam Cohene3e27a82011-04-15 12:07:39 -07002560 static private float squaredDistance(float[] point1, float[] point2) {
Michael Jurka4516c112010-10-07 15:13:47 -07002561 float distanceX = point1[0] - point2[0];
2562 float distanceY = point2[1] - point2[1];
2563 return distanceX * distanceX + distanceY * distanceY;
Adam Cohene3e27a82011-04-15 12:07:39 -07002564 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002565
Michael Jurka4516c112010-10-07 15:13:47 -07002566 /*
2567 *
2568 * Returns true if the passed CellLayout cl overlaps with dragView
2569 *
2570 */
2571 boolean overlaps(CellLayout cl, DragView dragView,
2572 int dragViewX, int dragViewY, Matrix cachedInverseMatrix) {
2573 // Transform the coordinates of the item being dragged to the CellLayout's coordinates
2574 final float[] draggedItemTopLeft = mTempDragCoordinates;
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002575 draggedItemTopLeft[0] = dragViewX;
2576 draggedItemTopLeft[1] = dragViewY;
Michael Jurka4516c112010-10-07 15:13:47 -07002577 final float[] draggedItemBottomRight = mTempDragBottomRightCoordinates;
Michael Jurkaf12c75c2011-01-25 22:41:40 -08002578 draggedItemBottomRight[0] = draggedItemTopLeft[0] + dragView.getDragRegionWidth();
2579 draggedItemBottomRight[1] = draggedItemTopLeft[1] + dragView.getDragRegionHeight();
Michael Jurkaa63c4522010-08-19 13:52:27 -07002580
Michael Jurka4516c112010-10-07 15:13:47 -07002581 // Transform the dragged item's top left coordinates
2582 // to the CellLayout's local coordinates
2583 mapPointFromSelfToChild(cl, draggedItemTopLeft, cachedInverseMatrix);
2584 float overlapRegionLeft = Math.max(0f, draggedItemTopLeft[0]);
2585 float overlapRegionTop = Math.max(0f, draggedItemTopLeft[1]);
2586
2587 if (overlapRegionLeft <= cl.getWidth() && overlapRegionTop >= 0) {
2588 // Transform the dragged item's bottom right coordinates
2589 // to the CellLayout's local coordinates
2590 mapPointFromSelfToChild(cl, draggedItemBottomRight, cachedInverseMatrix);
2591 float overlapRegionRight = Math.min(cl.getWidth(), draggedItemBottomRight[0]);
2592 float overlapRegionBottom = Math.min(cl.getHeight(), draggedItemBottomRight[1]);
2593
2594 if (overlapRegionRight >= 0 && overlapRegionBottom <= cl.getHeight()) {
2595 float overlap = (overlapRegionRight - overlapRegionLeft) *
2596 (overlapRegionBottom - overlapRegionTop);
2597 if (overlap > 0) {
2598 return true;
2599 }
2600 }
2601 }
2602 return false;
2603 }
2604
2605 /*
2606 *
2607 * This method returns the CellLayout that is currently being dragged to. In order to drag
2608 * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second
2609 * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one
2610 *
2611 * Return null if no CellLayout is currently being dragged over
2612 *
2613 */
2614 private CellLayout findMatchingPageForDragOver(
Adam Cohen00618752011-07-20 12:06:04 -07002615 DragView dragView, float originX, float originY, boolean exact) {
Michael Jurka4516c112010-10-07 15:13:47 -07002616 // We loop through all the screens (ie CellLayouts) and see which ones overlap
2617 // with the item being dragged and then choose the one that's closest to the touch point
Michael Jurkaa63c4522010-08-19 13:52:27 -07002618 final int screenCount = getChildCount();
2619 CellLayout bestMatchingScreen = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002620 float smallestDistSoFar = Float.MAX_VALUE;
Michael Jurka4516c112010-10-07 15:13:47 -07002621
Michael Jurkaa63c4522010-08-19 13:52:27 -07002622 for (int i = 0; i < screenCount; i++) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002623 CellLayout cl = (CellLayout) getChildAt(i);
Michael Jurkaa63c4522010-08-19 13:52:27 -07002624
Adam Cohen00618752011-07-20 12:06:04 -07002625 final float[] touchXy = {originX, originY};
Michael Jurka4516c112010-10-07 15:13:47 -07002626 // Transform the touch coordinates to the CellLayout's local coordinates
2627 // If the touch point is within the bounds of the cell layout, we can return immediately
Michael Jurka0280c3b2010-09-17 15:00:07 -07002628 cl.getMatrix().invert(mTempInverseMatrix);
Michael Jurka4516c112010-10-07 15:13:47 -07002629 mapPointFromSelfToChild(cl, touchXy, mTempInverseMatrix);
Michael Jurkaa63c4522010-08-19 13:52:27 -07002630
Michael Jurka4516c112010-10-07 15:13:47 -07002631 if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() &&
2632 touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) {
2633 return cl;
2634 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002635
Winson Chung96ef4092011-11-22 12:25:14 -08002636 if (!exact) {
Michael Jurka4516c112010-10-07 15:13:47 -07002637 // Get the center of the cell layout in screen coordinates
2638 final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates;
2639 cellLayoutCenter[0] = cl.getWidth()/2;
2640 cellLayoutCenter[1] = cl.getHeight()/2;
2641 mapPointFromChildToSelf(cl, cellLayoutCenter);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002642
Adam Cohen00618752011-07-20 12:06:04 -07002643 touchXy[0] = originX;
2644 touchXy[1] = originY;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002645
Michael Jurka4516c112010-10-07 15:13:47 -07002646 // Calculate the distance between the center of the CellLayout
2647 // and the touch point
2648 float dist = squaredDistance(touchXy, cellLayoutCenter);
2649
2650 if (dist < smallestDistSoFar) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002651 smallestDistSoFar = dist;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002652 bestMatchingScreen = cl;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002653 }
Michael Jurka4516c112010-10-07 15:13:47 -07002654 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002655 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002656 return bestMatchingScreen;
2657 }
2658
Adam Cohene3e27a82011-04-15 12:07:39 -07002659 // This is used to compute the visual center of the dragView. This point is then
2660 // used to visualize drop locations and determine where to drop an item. The idea is that
2661 // the visual center represents the user's interpretation of where the item is, and hence
2662 // is the appropriate point to use when determining drop location.
2663 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
2664 DragView dragView, float[] recycle) {
2665 float res[];
2666 if (recycle == null) {
2667 res = new float[2];
2668 } else {
2669 res = recycle;
2670 }
2671
2672 // First off, the drag view has been shifted in a way that is not represented in the
2673 // x and y values or the x/yOffsets. Here we account for that shift.
2674 x += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetX);
2675 y += getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
2676
2677 // These represent the visual top and left of drag view if a dragRect was provided.
2678 // If a dragRect was not provided, then they correspond to the actual view left and
2679 // top, as the dragRect is in that case taken to be the entire dragView.
2680 // R.dimen.dragViewOffsetY.
2681 int left = x - xOffset;
2682 int top = y - yOffset;
2683
2684 // In order to find the visual center, we shift by half the dragRect
2685 res[0] = left + dragView.getDragRegion().width() / 2;
2686 res[1] = top + dragView.getDragRegion().height() / 2;
2687
2688 return res;
2689 }
2690
Winson Chungea359c62011-08-03 17:06:35 -07002691 private boolean isDragWidget(DragObject d) {
2692 return (d.dragInfo instanceof LauncherAppWidgetInfo ||
2693 d.dragInfo instanceof PendingAddWidgetInfo);
2694 }
2695 private boolean isExternalDragWidget(DragObject d) {
2696 return d.dragSource != this && isDragWidget(d);
2697 }
2698
Adam Cohencb3382b2011-05-24 14:07:08 -07002699 public void onDragOver(DragObject d) {
Winson Chungc07918d2011-07-01 15:35:26 -07002700 // Skip drag over events while we are dragging over side pages
Adam Cohen82ac8a22012-02-14 16:27:49 -08002701 if (mInScrollArea || mIsSwitchingState || mState == State.SMALL) return;
Winson Chungc07918d2011-07-01 15:35:26 -07002702
Winson Chung4afe9b32011-07-27 17:46:20 -07002703 Rect r = new Rect();
Winson Chung3d503fb2011-07-13 17:25:49 -07002704 CellLayout layout = null;
Winson Chungc07918d2011-07-01 15:35:26 -07002705 ItemInfo item = (ItemInfo) d.dragInfo;
2706
2707 // Ensure that we have proper spans for the item that we are dropping
2708 if (item.spanX < 0 || item.spanY < 0) throw new RuntimeException("Improper spans found");
Adam Cohen00618752011-07-20 12:06:04 -07002709 mDragViewVisualCenter = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset,
Winson Chung4afe9b32011-07-27 17:46:20 -07002710 d.dragView, mDragViewVisualCenter);
Adam Cohen00618752011-07-20 12:06:04 -07002711
Winson Chungc07918d2011-07-01 15:35:26 -07002712 // Identify whether we have dragged over a side page
Michael Jurkad74c9842011-07-10 12:44:21 -07002713 if (isSmall()) {
Winson Chungea359c62011-08-03 17:06:35 -07002714 if (mLauncher.getHotseat() != null && !isExternalDragWidget(d)) {
Winson Chung4afe9b32011-07-27 17:46:20 -07002715 mLauncher.getHotseat().getHitRect(r);
2716 if (r.contains(d.x, d.y)) {
2717 layout = mLauncher.getHotseat().getLayout();
2718 }
2719 }
2720 if (layout == null) {
Winson Chung96ef4092011-11-22 12:25:14 -08002721 layout = findMatchingPageForDragOver(d.dragView, d.x, d.y, false);
Winson Chung4afe9b32011-07-27 17:46:20 -07002722 }
Winson Chungc07918d2011-07-01 15:35:26 -07002723 if (layout != mDragTargetLayout) {
2724 // Cancel all intermediate folder states
2725 cleanupFolderCreation(d);
2726
2727 if (mDragTargetLayout != null) {
2728 mDragTargetLayout.setIsDragOverlapping(false);
2729 mDragTargetLayout.onDragExit();
2730 }
2731 mDragTargetLayout = layout;
2732 if (mDragTargetLayout != null) {
2733 mDragTargetLayout.setIsDragOverlapping(true);
2734 mDragTargetLayout.onDragEnter();
2735 } else {
2736 mLastDragOverView = null;
2737 }
2738
Michael Jurkad74c9842011-07-10 12:44:21 -07002739 boolean isInSpringLoadedMode = (mState == State.SPRING_LOADED);
Winson Chungc07918d2011-07-01 15:35:26 -07002740 if (isInSpringLoadedMode) {
Winson Chung4afe9b32011-07-27 17:46:20 -07002741 if (mLauncher.isHotseatLayout(layout)) {
2742 mSpringLoadedDragController.cancel();
2743 } else {
2744 mSpringLoadedDragController.setAlarm(mDragTargetLayout);
2745 }
Winson Chungc07918d2011-07-01 15:35:26 -07002746 }
2747 }
2748 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002749 // Test to see if we are over the hotseat otherwise just use the current page
Winson Chungea359c62011-08-03 17:06:35 -07002750 if (mLauncher.getHotseat() != null && !isDragWidget(d)) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002751 mLauncher.getHotseat().getHitRect(r);
2752 if (r.contains(d.x, d.y)) {
2753 layout = mLauncher.getHotseat().getLayout();
2754 }
2755 }
2756 if (layout == null) {
2757 layout = getCurrentDropLayout();
2758 }
Winson Chungc07918d2011-07-01 15:35:26 -07002759 if (layout != mDragTargetLayout) {
2760 if (mDragTargetLayout != null) {
2761 mDragTargetLayout.setIsDragOverlapping(false);
2762 mDragTargetLayout.onDragExit();
2763 }
2764 mDragTargetLayout = layout;
2765 mDragTargetLayout.setIsDragOverlapping(true);
2766 mDragTargetLayout.onDragEnter();
2767 }
2768 }
2769
2770 // Handle the drag over
2771 if (mDragTargetLayout != null) {
2772 final View child = (mDragInfo == null) ? null : mDragInfo.cell;
Adam Cohene3e27a82011-04-15 12:07:39 -07002773
Winson Chungc07918d2011-07-01 15:35:26 -07002774 // We want the point to be mapped to the dragTarget.
Winson Chung3d503fb2011-07-13 17:25:49 -07002775 if (mLauncher.isHotseatLayout(mDragTargetLayout)) {
2776 mapPointFromSelfToSibling(mLauncher.getHotseat(), mDragViewVisualCenter);
2777 } else {
2778 mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter, null);
2779 }
Winson Chungc07918d2011-07-01 15:35:26 -07002780 ItemInfo info = (ItemInfo) d.dragInfo;
Patrick Dubroy1262e362010-10-06 15:49:50 -07002781
Winson Chungc07918d2011-07-01 15:35:26 -07002782 mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
2783 (int) mDragViewVisualCenter[1], 1, 1, mDragTargetLayout, mTargetCell);
2784 final View dragOverView = mDragTargetLayout.getChildAt(mTargetCell[0],
2785 mTargetCell[1]);
Winson Chung785d2eb2011-04-14 16:08:02 -07002786
Winson Chungc07918d2011-07-01 15:35:26 -07002787 boolean userFolderPending = willCreateUserFolder(info, mDragTargetLayout,
2788 mTargetCell, false);
2789 boolean isOverFolder = dragOverView instanceof FolderIcon;
2790 if (dragOverView != mLastDragOverView) {
2791 cancelFolderCreation();
2792 if (mLastDragOverView != null && mLastDragOverView instanceof FolderIcon) {
2793 ((FolderIcon) mLastDragOverView).onDragExit(d.dragInfo);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002794 }
2795 }
Michael Jurkad718d6a2010-10-14 15:35:17 -07002796
Winson Chungc07918d2011-07-01 15:35:26 -07002797 if (userFolderPending && dragOverView != mLastDragOverView) {
2798 mFolderCreationAlarm.setOnAlarmListener(new
2799 FolderCreationAlarmListener(mDragTargetLayout, mTargetCell[0], mTargetCell[1]));
2800 mFolderCreationAlarm.setAlarm(FOLDER_CREATION_TIMEOUT);
2801 }
Michael Jurkad718d6a2010-10-14 15:35:17 -07002802
Winson Chungc07918d2011-07-01 15:35:26 -07002803 if (dragOverView != mLastDragOverView && isOverFolder) {
2804 ((FolderIcon) dragOverView).onDragEnter(d.dragInfo);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002805 if (mDragTargetLayout != null) {
Winson Chungc07918d2011-07-01 15:35:26 -07002806 mDragTargetLayout.clearDragOutlines();
Michael Jurkad718d6a2010-10-14 15:35:17 -07002807 }
Patrick Dubroy1262e362010-10-06 15:49:50 -07002808 }
Winson Chungc07918d2011-07-01 15:35:26 -07002809 mLastDragOverView = dragOverView;
2810
2811 if (!mCreateUserFolderOnDrop && !isOverFolder) {
2812 mDragTargetLayout.visualizeDropLocation(child, mDragOutline,
2813 (int) mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1],
Winson Chungb8c69f32011-10-19 21:36:08 -07002814 item.spanX, item.spanY, d.dragView.getDragVisualizeOffset(),
2815 d.dragView.getDragRegion());
Winson Chungc07918d2011-07-01 15:35:26 -07002816 }
Patrick Dubroy976ebec2010-08-04 20:03:37 -07002817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002818 }
2819
Winson Chungc07918d2011-07-01 15:35:26 -07002820 private void cleanupFolderCreation(DragObject d) {
2821 if (mDragFolderRingAnimator != null && mCreateUserFolderOnDrop) {
2822 mDragFolderRingAnimator.animateToNaturalState();
2823 }
2824 if (mLastDragOverView != null && mLastDragOverView instanceof FolderIcon) {
2825 if (d != null) {
2826 ((FolderIcon) mLastDragOverView).onDragExit(d.dragInfo);
2827 }
2828 }
2829 mFolderCreationAlarm.cancelAlarm();
2830 }
2831
Adam Cohenc0dcf592011-06-01 15:30:43 -07002832 private void cancelFolderCreation() {
2833 if (mDragFolderRingAnimator != null && mCreateUserFolderOnDrop) {
2834 mDragFolderRingAnimator.animateToNaturalState();
2835 }
2836 mCreateUserFolderOnDrop = false;
2837 mFolderCreationAlarm.cancelAlarm();
2838 }
2839
Adam Cohen19072da2011-05-31 14:30:45 -07002840 class FolderCreationAlarmListener implements OnAlarmListener {
Adam Cohen69ce2e52011-07-03 19:25:21 -07002841 CellLayout layout;
2842 int cellX;
2843 int cellY;
Adam Cohen19072da2011-05-31 14:30:45 -07002844
Adam Cohen69ce2e52011-07-03 19:25:21 -07002845 public FolderCreationAlarmListener(CellLayout layout, int cellX, int cellY) {
2846 this.layout = layout;
2847 this.cellX = cellX;
2848 this.cellY = cellY;
Adam Cohen19072da2011-05-31 14:30:45 -07002849 }
2850
2851 public void onAlarm(Alarm alarm) {
Adam Cohen19072da2011-05-31 14:30:45 -07002852 if (mDragFolderRingAnimator == null) {
2853 mDragFolderRingAnimator = new FolderRingAnimator(mLauncher, null);
2854 }
Adam Cohen69ce2e52011-07-03 19:25:21 -07002855 mDragFolderRingAnimator.setCell(cellX, cellY);
2856 mDragFolderRingAnimator.setCellLayout(layout);
Adam Cohen19072da2011-05-31 14:30:45 -07002857 mDragFolderRingAnimator.animateToAcceptState();
Adam Cohen69ce2e52011-07-03 19:25:21 -07002858 layout.showFolderAccept(mDragFolderRingAnimator);
2859 layout.clearDragOutlines();
Adam Cohen19072da2011-05-31 14:30:45 -07002860 mCreateUserFolderOnDrop = true;
Adam Cohen19072da2011-05-31 14:30:45 -07002861 }
2862 }
2863
Winson Chunga34abf82010-11-12 12:10:35 -08002864 @Override
2865 public void getHitRect(Rect outRect) {
2866 // We want the workspace to have the whole area of the display (it will find the correct
2867 // cell layout to drop to in the existing drag/drop logic.
Adam Cohencff6af82011-09-13 14:51:53 -07002868 outRect.set(0, 0, mDisplayWidth, mDisplayHeight);
Winson Chunga34abf82010-11-12 12:10:35 -08002869 }
2870
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002871 /**
2872 * Add the item specified by dragInfo to the given layout.
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002873 * @return true if successful
2874 */
Adam Cohen120980b2010-12-08 11:05:37 -08002875 public boolean addExternalItemToScreen(ItemInfo dragInfo, CellLayout layout) {
2876 if (layout.findCellForSpan(mTempEstimate, dragInfo.spanX, dragInfo.spanY)) {
Patrick Dubroybbaa75c2011-03-08 18:47:40 -08002877 onDropExternal(dragInfo.dropPos, (ItemInfo) dragInfo, (CellLayout) layout, false);
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002878 return true;
2879 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002880 mLauncher.showOutOfSpaceMessage();
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002881 return false;
2882 }
2883
Adam Cohend5e42732011-03-28 17:33:39 -07002884 private void onDropExternal(int[] touchXY, Object dragInfo,
2885 CellLayout cellLayout, boolean insertAtFirst) {
Adam Cohene3e27a82011-04-15 12:07:39 -07002886 onDropExternal(touchXY, dragInfo, cellLayout, insertAtFirst, null);
Adam Cohend5e42732011-03-28 17:33:39 -07002887 }
2888
Adam Cohen120980b2010-12-08 11:05:37 -08002889 /**
2890 * Drop an item that didn't originate on one of the workspace screens.
2891 * It may have come from Launcher (e.g. from all apps or customize), or it may have
2892 * come from another app altogether.
2893 *
2894 * NOTE: This can also be called when we are outside of a drag event, when we want
2895 * to add an item to one of the workspace screens.
2896 */
Winson Chung557d6ed2011-07-08 15:34:52 -07002897 private void onDropExternal(final int[] touchXY, final Object dragInfo,
2898 final CellLayout cellLayout, boolean insertAtFirst, DragObject d) {
2899 final Runnable exitSpringLoadedRunnable = new Runnable() {
2900 @Override
2901 public void run() {
Adam Cohened66b2b2012-01-23 17:28:51 -08002902 mLauncher.exitSpringLoadedDragModeDelayed(true, false, null);
Winson Chung557d6ed2011-07-08 15:34:52 -07002903 }
2904 };
Adam Cohenb7e16182011-07-15 17:55:02 -07002905
2906 ItemInfo info = (ItemInfo) dragInfo;
2907 int spanX = info.spanX;
2908 int spanY = info.spanY;
2909 if (mDragInfo != null) {
2910 spanX = mDragInfo.spanX;
2911 spanY = mDragInfo.spanY;
2912 }
2913
Winson Chung3d503fb2011-07-13 17:25:49 -07002914 final long container = mLauncher.isHotseatLayout(cellLayout) ?
2915 LauncherSettings.Favorites.CONTAINER_HOTSEAT :
2916 LauncherSettings.Favorites.CONTAINER_DESKTOP;
Winson Chung557d6ed2011-07-08 15:34:52 -07002917 final int screen = indexOfChild(cellLayout);
Winson Chung3d503fb2011-07-13 17:25:49 -07002918 if (!mLauncher.isHotseatLayout(cellLayout) && screen != mCurrentPage
2919 && mState != State.SPRING_LOADED) {
Adam Cohen76078c42011-06-09 15:06:52 -07002920 snapToPage(screen);
2921 }
Adam Cohenb7e16182011-07-15 17:55:02 -07002922
2923 if (info instanceof PendingAddItemInfo) {
2924 final PendingAddItemInfo pendingInfo = (PendingAddItemInfo) dragInfo;
2925
Adam Cohen558baaf2011-08-15 15:22:57 -07002926 boolean findNearestVacantCell = true;
2927 if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
2928 mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY,
2929 cellLayout, mTargetCell);
2930 if (willCreateUserFolder((ItemInfo) d.dragInfo, mDragTargetLayout, mTargetCell,
2931 true) || willAddToExistingUserFolder((ItemInfo) d.dragInfo,
2932 mDragTargetLayout, mTargetCell)) {
2933 findNearestVacantCell = false;
2934 }
2935 }
2936 if (findNearestVacantCell) {
2937 mTargetCell = findNearestVacantArea(touchXY[0], touchXY[1], spanX, spanY, null,
2938 cellLayout, mTargetCell);
2939 }
2940
Adam Cohenb7e16182011-07-15 17:55:02 -07002941 Runnable onAnimationCompleteRunnable = new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002942 @Override
2943 public void run() {
2944 // When dragging and dropping from customization tray, we deal with creating
2945 // widgets/shortcuts/folders in a slightly different way
Adam Cohenb7e16182011-07-15 17:55:02 -07002946 switch (pendingInfo.itemType) {
Winson Chung557d6ed2011-07-08 15:34:52 -07002947 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Adam Cohenb7e16182011-07-15 17:55:02 -07002948 mLauncher.addAppWidgetFromDrop((PendingAddWidgetInfo) pendingInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07002949 container, screen, mTargetCell, null);
Winson Chung557d6ed2011-07-08 15:34:52 -07002950 break;
2951 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Adam Cohenb7e16182011-07-15 17:55:02 -07002952 mLauncher.processShortcutFromDrop(pendingInfo.componentName,
Winson Chung3d503fb2011-07-13 17:25:49 -07002953 container, screen, mTargetCell, null);
Winson Chung557d6ed2011-07-08 15:34:52 -07002954 break;
2955 default:
Adam Cohenb7e16182011-07-15 17:55:02 -07002956 throw new IllegalStateException("Unknown item type: " +
2957 pendingInfo.itemType);
Winson Chung557d6ed2011-07-08 15:34:52 -07002958 }
2959 cellLayout.onDragExit();
2960 }
Adam Cohenb7e16182011-07-15 17:55:02 -07002961 };
2962
Adam Cohened66b2b2012-01-23 17:28:51 -08002963 animateExternalDrop((PendingAddItemInfo) info, cellLayout, d.dragView,
2964 onAnimationCompleteRunnable, ANIMATE_INTO_POSITION);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07002965 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002966 // This is for other drag/drop cases, like dragging from All Apps
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002967 View view = null;
2968
2969 switch (info.itemType) {
2970 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2971 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2972 if (info.container == NO_ID && info instanceof ApplicationInfo) {
2973 // Came from all apps -- make a copy
Michael Jurkac9d95c52011-08-29 14:03:34 -07002974 info = new ShortcutInfo((ApplicationInfo) info);
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002975 }
2976 view = mLauncher.createShortcut(R.layout.application, cellLayout,
2977 (ShortcutInfo) info);
2978 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002979 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Adam Cohenc0dcf592011-06-01 15:30:43 -07002980 view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, cellLayout,
2981 (FolderInfo) info, mIconCache);
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002982 break;
2983 default:
2984 throw new IllegalStateException("Unknown item type: " + info.itemType);
2985 }
2986
Adam Cohenc0dcf592011-06-01 15:30:43 -07002987 // First we find the cell nearest to point at which the item is
2988 // dropped, without any consideration to whether there is an item there.
2989 if (touchXY != null) {
2990 mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY,
2991 cellLayout, mTargetCell);
Winson Chung557d6ed2011-07-08 15:34:52 -07002992 d.postAnimationRunnable = exitSpringLoadedRunnable;
Winson Chung3d503fb2011-07-13 17:25:49 -07002993 if (createUserFolderIfNecessary(view, container, cellLayout, mTargetCell, true,
2994 d.dragView, d.postAnimationRunnable)) {
Adam Cohenc0dcf592011-06-01 15:30:43 -07002995 return;
2996 }
Adam Cohen3e8f8112011-07-02 18:03:00 -07002997 if (addToExistingFolderIfNecessary(view, cellLayout, mTargetCell, d, true)) {
Adam Cohenc0dcf592011-06-01 15:30:43 -07002998 return;
2999 }
Adam Cohen10b17372011-04-15 14:21:25 -07003000 }
3001
Michael Jurkac4e772e2011-02-10 13:32:01 -08003002 if (touchXY != null) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08003003 // when dragging and dropping, just find the closest free spot
Adam Cohenc0dcf592011-06-01 15:30:43 -07003004 mTargetCell = findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, null,
3005 cellLayout, mTargetCell);
Michael Jurka9c6de3d2010-11-23 16:23:58 -08003006 } else {
3007 cellLayout.findCellForSpan(mTargetCell, 1, 1);
3008 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003009 addInScreen(view, container, screen, mTargetCell[0], mTargetCell[1], info.spanX,
3010 info.spanY, insertAtFirst);
Adam Cohen716b51e2011-06-30 12:09:54 -07003011 cellLayout.onDropChild(view);
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07003012 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
Adam Cohend5e42732011-03-28 17:33:39 -07003013 cellLayout.getChildrenLayout().measureChild(view);
3014
Winson Chung3d503fb2011-07-13 17:25:49 -07003015 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screen,
Winson Chungaafa03c2010-06-11 17:34:16 -07003016 lp.cellX, lp.cellY);
Adam Cohen3e8f8112011-07-02 18:03:00 -07003017
3018 if (d.dragView != null) {
Adam Cohen4b285c52011-07-21 14:24:06 -07003019 // We wrap the animation call in the temporary set and reset of the current
3020 // cellLayout to its final transform -- this means we animate the drag view to
3021 // the correct final location.
3022 setFinalTransitionTransform(cellLayout);
Winson Chung557d6ed2011-07-08 15:34:52 -07003023 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view,
Adam Cohen4b285c52011-07-21 14:24:06 -07003024 exitSpringLoadedRunnable);
3025 resetTransitionTransform(cellLayout);
Adam Cohen3e8f8112011-07-02 18:03:00 -07003026 }
Joe Onorato00acb122009-08-04 16:04:30 -04003027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003029
Adam Cohened66b2b2012-01-23 17:28:51 -08003030 public Bitmap createWidgetBitmap(PendingAddWidgetInfo widgetInfo) {
3031 int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(widgetInfo.spanX,
3032 widgetInfo.spanY, widgetInfo, false);
3033 View layout = widgetInfo.boundWidget;
Adam Cohenef3dd6e2012-02-14 20:54:05 -08003034 mLauncher.getDragLayer().removeView(layout);
Adam Cohened66b2b2012-01-23 17:28:51 -08003035 layout.setVisibility(VISIBLE);
3036
3037 int width = MeasureSpec.makeMeasureSpec(unScaledSize[0], MeasureSpec.EXACTLY);
3038 int height = MeasureSpec.makeMeasureSpec(unScaledSize[1], MeasureSpec.EXACTLY);
3039 Bitmap b = Bitmap.createBitmap(unScaledSize[0], unScaledSize[1],
3040 Bitmap.Config.ARGB_8888);
3041 Canvas c = new Canvas(b);
3042
3043 layout.measure(width, height);
3044 layout.layout(0, 0, unScaledSize[0], unScaledSize[1]);
Adam Cohened66b2b2012-01-23 17:28:51 -08003045 layout.draw(c);
3046 c.setBitmap(null);
3047 return b;
3048 }
3049
3050 public void animateExternalDrop(PendingAddItemInfo pendingInfo, CellLayout cellLayout,
3051 DragView dragView, Runnable onCompleteRunnable, int animationType) {
3052 // Now we animate the dragView, (ie. the widget or shortcut preview) into its final
3053 // location and size on the home screen.
3054 int spanX = pendingInfo.spanX;
3055 int spanY = pendingInfo.spanY;
3056 RectF r = estimateItemPosition(cellLayout, pendingInfo,
3057 mTargetCell[0], mTargetCell[1], spanX, spanY);
3058 int loc[] = new int[2];
3059 loc[0] = (int) r.left;
3060 loc[1] = (int) r.top;
3061 setFinalTransitionTransform(cellLayout);
3062 float cellLayoutScale =
3063 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(cellLayout, loc);
3064 resetTransitionTransform(cellLayout);
3065
3066 float dragViewScaleX = r.width() / dragView.getMeasuredWidth();
3067 float dragViewScaleY = r.height() / dragView.getMeasuredHeight();
3068 // The animation will scale the dragView about its center, so we need to center about
3069 // the final location.
3070 loc[0] -= (dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2;
3071 loc[1] -= (dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2;
3072
3073 float scaleX = dragViewScaleX * cellLayoutScale;
3074 float scaleY = dragViewScaleY * cellLayoutScale;
3075
3076 Resources res = mLauncher.getResources();
3077 int duration = res.getInteger(R.integer.config_dropAnimMaxDuration) - 200;
3078
3079 int animationEnd = DragLayer.ANIMATION_END_REMAIN_VISIBLE;
3080 if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET &&
3081 (((PendingAddWidgetInfo) pendingInfo).info.configure == null ||
3082 animationType == COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION)) {
3083 Bitmap crossFadeBitmap = createWidgetBitmap((PendingAddWidgetInfo) pendingInfo);
3084 dragView.setCrossFadeBitmap(crossFadeBitmap);
3085 dragView.crossFade((int) (duration * 0.8f));
3086 animationEnd = DragLayer.ANIMATION_END_DISAPPEAR;
3087 } else {
3088 scaleX = scaleY = Math.min(scaleX, scaleY);
3089 }
3090
Winson Chung7bd1bbb2012-02-13 18:29:29 -08003091 if (animationType == CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION) {
3092 mLauncher.getDragLayer().animateViewIntoPosition(dragView, loc, 0f, 0.1f, 0.1f,
Adam Cohened66b2b2012-01-23 17:28:51 -08003093 DragLayer.ANIMATION_END_DISAPPEAR, onCompleteRunnable, duration);
3094 } else {
Winson Chung7bd1bbb2012-02-13 18:29:29 -08003095 mLauncher.getDragLayer().animateViewIntoPosition(dragView, loc, 1f, scaleX, scaleY,
Adam Cohened66b2b2012-01-23 17:28:51 -08003096 animationEnd, onCompleteRunnable, duration);
3097 }
3098 }
3099
Adam Cohen4b285c52011-07-21 14:24:06 -07003100 public void setFinalTransitionTransform(CellLayout layout) {
3101 if (isSwitchingState()) {
3102 int index = indexOfChild(layout);
3103 mCurrentScaleX = layout.getScaleX();
3104 mCurrentScaleY = layout.getScaleY();
3105 mCurrentTranslationX = layout.getTranslationX();
3106 mCurrentTranslationY = layout.getTranslationY();
3107 mCurrentRotationY = layout.getRotationY();
3108 layout.setScaleX(mNewScaleXs[index]);
3109 layout.setScaleY(mNewScaleYs[index]);
3110 layout.setTranslationX(mNewTranslationXs[index]);
3111 layout.setTranslationY(mNewTranslationYs[index]);
3112 layout.setRotationY(mNewRotationYs[index]);
3113 }
3114 }
3115 public void resetTransitionTransform(CellLayout layout) {
3116 if (isSwitchingState()) {
Adam Cohen4b285c52011-07-21 14:24:06 -07003117 mCurrentScaleX = layout.getScaleX();
3118 mCurrentScaleY = layout.getScaleY();
3119 mCurrentTranslationX = layout.getTranslationX();
3120 mCurrentTranslationY = layout.getTranslationY();
3121 mCurrentRotationY = layout.getRotationY();
3122 layout.setScaleX(mCurrentScaleX);
3123 layout.setScaleY(mCurrentScaleY);
3124 layout.setTranslationX(mCurrentTranslationX);
3125 layout.setTranslationY(mCurrentTranslationY);
3126 layout.setRotationY(mCurrentRotationY);
3127 }
3128 }
3129
Jeff Sharkey70864282009-04-07 21:08:40 -07003130 /**
3131 * Return the current {@link CellLayout}, correctly picking the destination
3132 * screen while a scroll is in progress.
3133 */
Patrick Dubroy5f445422011-02-18 14:35:21 -08003134 public CellLayout getCurrentDropLayout() {
3135 return (CellLayout) getChildAt(mNextPage == INVALID_PAGE ? mCurrentPage : mNextPage);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 }
3137
Jeff Sharkey70864282009-04-07 21:08:40 -07003138 /**
Michael Jurka0280c3b2010-09-17 15:00:07 -07003139 * Return the current CellInfo describing our current drag; this method exists
3140 * so that Launcher can sync this object with the correct info when the activity is created/
3141 * destroyed
3142 *
3143 */
3144 public CellLayout.CellInfo getDragInfo() {
3145 return mDragInfo;
3146 }
3147
3148 /**
Jeff Sharkey70864282009-04-07 21:08:40 -07003149 * Calculate the nearest cell where the given object would be dropped.
Adam Cohene3e27a82011-04-15 12:07:39 -07003150 *
3151 * pixelX and pixelY should be in the coordinate system of layout
Jeff Sharkey70864282009-04-07 21:08:40 -07003152 */
Michael Jurka6a1435d2010-09-27 17:35:12 -07003153 private int[] findNearestVacantArea(int pixelX, int pixelY,
Jeff Sharkey70864282009-04-07 21:08:40 -07003154 int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003155 return layout.findNearestVacantArea(
Adam Cohene3e27a82011-04-15 12:07:39 -07003156 pixelX, pixelY, spanX, spanY, ignoreView, recycle);
Jeff Sharkey70864282009-04-07 21:08:40 -07003157 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003158
Adam Cohendf035382011-04-11 17:22:04 -07003159 /**
3160 * Calculate the nearest cell where the given object would be dropped.
Adam Cohene3e27a82011-04-15 12:07:39 -07003161 *
3162 * pixelX and pixelY should be in the coordinate system of layout
Adam Cohendf035382011-04-11 17:22:04 -07003163 */
3164 private int[] findNearestArea(int pixelX, int pixelY,
Adam Cohene3e27a82011-04-15 12:07:39 -07003165 int spanX, int spanY, CellLayout layout, int[] recycle) {
Adam Cohendf035382011-04-11 17:22:04 -07003166 return layout.findNearestArea(
Adam Cohene3e27a82011-04-15 12:07:39 -07003167 pixelX, pixelY, spanX, spanY, recycle);
Adam Cohendf035382011-04-11 17:22:04 -07003168 }
3169
Adam Cohencff6af82011-09-13 14:51:53 -07003170 void setup(DragController dragController) {
Michael Jurkac2f7f472010-12-14 15:34:42 -08003171 mSpringLoadedDragController = new SpringLoadedDragController(mLauncher);
Joe Onorato00acb122009-08-04 16:04:30 -04003172 mDragController = dragController;
Michael Jurkad74c9842011-07-10 12:44:21 -07003173
Michael Jurkad74c9842011-07-10 12:44:21 -07003174 // hardware layers on children are enabled on startup, but should be disabled until
3175 // needed
3176 updateChildrenLayersEnabled();
3177 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178 }
3179
Patrick Dubroye3887cc2011-01-20 10:43:40 -08003180 /**
3181 * Called at the end of a drag which originated on the workspace.
3182 */
Adam Cohenc0dcf592011-06-01 15:30:43 -07003183 public void onDropCompleted(View target, DragObject d, boolean success) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003184 if (success) {
Michael Jurkad74c9842011-07-10 12:44:21 -07003185 if (target != this) {
3186 if (mDragInfo != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003187 getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell);
Michael Jurkad74c9842011-07-10 12:44:21 -07003188 if (mDragInfo.cell instanceof DropTarget) {
3189 mDragController.removeDropTarget((DropTarget) mDragInfo.cell);
3190 }
Joe Onorato00acb122009-08-04 16:04:30 -04003191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192 }
Patrick Dubroyce34a972010-10-19 10:34:32 -07003193 } else if (mDragInfo != null) {
Patrick Dubroye3887cc2011-01-20 10:43:40 -08003194 // NOTE: When 'success' is true, onDragExit is called by the DragController before
3195 // calling onDropCompleted(). We call it ourselves here, but maybe this should be
3196 // moved into DragController.cancelDrag().
Adam Cohenc0dcf592011-06-01 15:30:43 -07003197 doDragExit(null);
Winson Chung3d503fb2011-07-13 17:25:49 -07003198 CellLayout cellLayout;
3199 if (mLauncher.isHotseatLayout(target)) {
3200 cellLayout = mLauncher.getHotseat().getLayout();
3201 } else {
3202 cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
3203 }
3204 cellLayout.onDropChild(mDragInfo.cell);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205 }
Adam Cohen36cc09b2011-09-29 17:33:15 -07003206 if (d.cancelled && mDragInfo.cell != null) {
3207 mDragInfo.cell.setVisibility(VISIBLE);
3208 }
Joe Onorato4be866d2010-10-10 11:26:02 -07003209 mDragOutline = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003210 mDragInfo = null;
Winson Chung1afedc32012-01-23 16:14:56 -08003211
3212 // Hide the scrolling indicator after you pick up an item
3213 hideScrollingIndicator(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003214 }
3215
Michael Jurka0280c3b2010-09-17 15:00:07 -07003216 public boolean isDropEnabled() {
3217 return true;
3218 }
3219
Michael Jurka0142d492010-08-25 17:46:15 -07003220 @Override
3221 protected void onRestoreInstanceState(Parcelable state) {
3222 super.onRestoreInstanceState(state);
3223 Launcher.setScreen(mCurrentPage);
3224 }
3225
3226 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003227 public void scrollLeft() {
Michael Jurkad74c9842011-07-10 12:44:21 -07003228 if (!isSmall() && !mIsSwitchingState) {
Michael Jurka0142d492010-08-25 17:46:15 -07003229 super.scrollLeft();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 }
Adam Cohen95bb8002011-07-03 23:40:28 -07003231 Folder openFolder = getOpenFolder();
3232 if (openFolder != null) {
3233 openFolder.completeDragExit();
3234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003235 }
3236
Michael Jurka0142d492010-08-25 17:46:15 -07003237 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 public void scrollRight() {
Michael Jurkad74c9842011-07-10 12:44:21 -07003239 if (!isSmall() && !mIsSwitchingState) {
Michael Jurka0142d492010-08-25 17:46:15 -07003240 super.scrollRight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003241 }
Adam Cohen95bb8002011-07-03 23:40:28 -07003242 Folder openFolder = getOpenFolder();
3243 if (openFolder != null) {
3244 openFolder.completeDragExit();
3245 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003246 }
3247
Patrick Dubroy1262e362010-10-06 15:49:50 -07003248 @Override
Winson Chung3e0839e2011-10-03 15:15:18 -07003249 public boolean onEnterScrollArea(int x, int y, int direction) {
Winson Chung1c4cf4a2011-07-29 14:49:10 -07003250 // Ignore the scroll area if we are dragging over the hot seat
3251 if (mLauncher.getHotseat() != null) {
3252 Rect r = new Rect();
3253 mLauncher.getHotseat().getHitRect(r);
3254 if (r.contains(x, y)) {
Winson Chung3e0839e2011-10-03 15:15:18 -07003255 return false;
Winson Chung1c4cf4a2011-07-29 14:49:10 -07003256 }
3257 }
3258
Winson Chung3e0839e2011-10-03 15:15:18 -07003259 boolean result = false;
Michael Jurkad74c9842011-07-10 12:44:21 -07003260 if (!isSmall() && !mIsSwitchingState) {
Michael Jurkad718d6a2010-10-14 15:35:17 -07003261 mInScrollArea = true;
Patrick Dubroy54fa3b92010-11-17 12:18:45 -08003262
Winson Chungaa15ffe2012-01-18 15:45:28 -08003263 final int page = (mNextPage != INVALID_PAGE ? mNextPage : mCurrentPage) +
3264 (direction == DragController.SCROLL_LEFT ? -1 : 1);
Adam Cohenc0dcf592011-06-01 15:30:43 -07003265 cancelFolderCreation();
Patrick Dubroy54fa3b92010-11-17 12:18:45 -08003266
Winson Chungaa15ffe2012-01-18 15:45:28 -08003267 if (0 <= page && page < getChildCount()) {
3268 CellLayout layout = (CellLayout) getChildAt(page);
Winson Chungc07918d2011-07-01 15:35:26 -07003269 // Exit the current layout and mark the overlapping layout
Patrick Dubroy0207c522010-11-03 22:12:02 -07003270 if (mDragTargetLayout != null) {
Winson Chungc07918d2011-07-01 15:35:26 -07003271 mDragTargetLayout.setIsDragOverlapping(false);
Patrick Dubroy0207c522010-11-03 22:12:02 -07003272 mDragTargetLayout.onDragExit();
Patrick Dubroy0207c522010-11-03 22:12:02 -07003273 }
Winson Chungc07918d2011-07-01 15:35:26 -07003274 mDragTargetLayout = layout;
3275 mDragTargetLayout.setIsDragOverlapping(true);
3276
3277 // Workspace is responsible for drawing the edge glow on adjacent pages,
3278 // so we need to redraw the workspace when this may have changed.
3279 invalidate();
Winson Chung3e0839e2011-10-03 15:15:18 -07003280 result = true;
Michael Jurkad718d6a2010-10-14 15:35:17 -07003281 }
Patrick Dubroy1262e362010-10-06 15:49:50 -07003282 }
Winson Chung3e0839e2011-10-03 15:15:18 -07003283 return result;
Patrick Dubroy1262e362010-10-06 15:49:50 -07003284 }
3285
3286 @Override
Winson Chung3e0839e2011-10-03 15:15:18 -07003287 public boolean onExitScrollArea() {
3288 boolean result = false;
Michael Jurkad718d6a2010-10-14 15:35:17 -07003289 if (mInScrollArea) {
Winson Chungc07918d2011-07-01 15:35:26 -07003290 if (mDragTargetLayout != null) {
Winson Chungc07918d2011-07-01 15:35:26 -07003291 mDragTargetLayout.setIsDragOverlapping(false);
Winson Chungc07918d2011-07-01 15:35:26 -07003292 // Workspace is responsible for drawing the edge glow on adjacent pages,
3293 // so we need to redraw the workspace when this may have changed.
3294 invalidate();
3295 }
Adam Cohen8a18afc2011-12-13 15:57:01 -08003296 if (mDragTargetLayout != null && mDragHasEnteredWorkspace) {
3297 // Unmark the overlapping layout and re-enter the current layout
3298 mDragTargetLayout = getCurrentDropLayout();
3299 mDragTargetLayout.onDragEnter();
3300 }
3301 result = true;
Winson Chungc07918d2011-07-01 15:35:26 -07003302 mInScrollArea = false;
Patrick Dubroy1262e362010-10-06 15:49:50 -07003303 }
Winson Chung3e0839e2011-10-03 15:15:18 -07003304 return result;
Patrick Dubroy1262e362010-10-06 15:49:50 -07003305 }
3306
Winson Chungc07918d2011-07-01 15:35:26 -07003307 private void onResetScrollArea() {
3308 if (mDragTargetLayout != null) {
3309 // Unmark the overlapping layout
3310 mDragTargetLayout.setIsDragOverlapping(false);
3311
3312 // Workspace is responsible for drawing the edge glow on adjacent pages,
3313 // so we need to redraw the workspace when this may have changed.
3314 invalidate();
3315 }
Winson Chungc07918d2011-07-01 15:35:26 -07003316 mInScrollArea = false;
3317 }
3318
Winson Chung3d503fb2011-07-13 17:25:49 -07003319 /**
3320 * Returns a specific CellLayout
3321 */
3322 CellLayout getParentCellLayoutForView(View v) {
3323 ArrayList<CellLayout> layouts = getWorkspaceAndHotseatCellLayouts();
3324 for (CellLayout layout : layouts) {
3325 if (layout.getChildrenLayout().indexOfChild(v) > -1) {
3326 return layout;
3327 }
3328 }
3329 return null;
3330 }
3331
3332 /**
3333 * Returns a list of all the CellLayouts in the workspace.
3334 */
3335 ArrayList<CellLayout> getWorkspaceAndHotseatCellLayouts() {
3336 ArrayList<CellLayout> layouts = new ArrayList<CellLayout>();
3337 int screenCount = getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003338 for (int screen = 0; screen < screenCount; screen++) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003339 layouts.add(((CellLayout) getChildAt(screen)));
3340 }
3341 if (mLauncher.getHotseat() != null) {
3342 layouts.add(mLauncher.getHotseat().getLayout());
3343 }
3344 return layouts;
3345 }
3346
3347 /**
3348 * We should only use this to search for specific children. Do not use this method to modify
3349 * CellLayoutChildren directly.
3350 */
3351 ArrayList<CellLayoutChildren> getWorkspaceAndHotseatCellLayoutChildren() {
3352 ArrayList<CellLayoutChildren> childrenLayouts = new ArrayList<CellLayoutChildren>();
3353 int screenCount = getChildCount();
3354 for (int screen = 0; screen < screenCount; screen++) {
3355 childrenLayouts.add(((CellLayout) getChildAt(screen)).getChildrenLayout());
3356 }
3357 if (mLauncher.getHotseat() != null) {
3358 childrenLayouts.add(mLauncher.getHotseat().getLayout().getChildrenLayout());
3359 }
3360 return childrenLayouts;
3361 }
3362
3363 public Folder getFolderForTag(Object tag) {
3364 ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren();
3365 for (CellLayoutChildren layout: childrenLayouts) {
3366 int count = layout.getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003367 for (int i = 0; i < count; i++) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003368 View child = layout.getChildAt(i);
Winson Chung3d503fb2011-07-13 17:25:49 -07003369 if (child instanceof Folder) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003370 Folder f = (Folder) child;
Winson Chungaafa03c2010-06-11 17:34:16 -07003371 if (f.getInfo() == tag && f.getInfo().opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003372 return f;
3373 }
3374 }
3375 }
3376 }
3377 return null;
3378 }
3379
3380 public View getViewForTag(Object tag) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003381 ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren();
3382 for (CellLayoutChildren layout: childrenLayouts) {
3383 int count = layout.getChildCount();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003384 for (int i = 0; i < count; i++) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003385 View child = layout.getChildAt(i);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003386 if (child.getTag() == tag) {
3387 return child;
3388 }
3389 }
3390 }
3391 return null;
3392 }
3393
Adam Cohendf035382011-04-11 17:22:04 -07003394 void clearDropTargets() {
Winson Chung3d503fb2011-07-13 17:25:49 -07003395 ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren();
3396 for (CellLayoutChildren layout: childrenLayouts) {
Adam Cohendf035382011-04-11 17:22:04 -07003397 int childCount = layout.getChildCount();
3398 for (int j = 0; j < childCount; j++) {
3399 View v = layout.getChildAt(j);
3400 if (v instanceof DropTarget) {
3401 mDragController.removeDropTarget((DropTarget) v);
3402 }
3403 }
3404 }
3405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003406
Joe Onorato64e6be72010-03-05 15:05:52 -05003407 void removeItems(final ArrayList<ApplicationInfo> apps) {
Romain Guy629de3e2010-01-13 12:20:59 -08003408 final AppWidgetManager widgets = AppWidgetManager.getInstance(getContext());
Romain Guy574d20e2009-06-01 15:34:04 -07003409
Joe Onorato64e6be72010-03-05 15:05:52 -05003410 final HashSet<String> packageNames = new HashSet<String>();
3411 final int appCount = apps.size();
3412 for (int i = 0; i < appCount; i++) {
3413 packageNames.add(apps.get(i).componentName.getPackageName());
3414 }
3415
Winson Chung3d503fb2011-07-13 17:25:49 -07003416 ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts();
3417 for (final CellLayout layoutParent: cellLayouts) {
Winson Chung7a25a9e2011-01-30 13:33:56 -08003418 final ViewGroup layout = layoutParent.getChildrenLayout();
Romain Guy574d20e2009-06-01 15:34:04 -07003419
Romain Guy629de3e2010-01-13 12:20:59 -08003420 // Avoid ANRs by treating each screen separately
3421 post(new Runnable() {
3422 public void run() {
3423 final ArrayList<View> childrenToRemove = new ArrayList<View>();
3424 childrenToRemove.clear();
Winson Chungaafa03c2010-06-11 17:34:16 -07003425
Romain Guy629de3e2010-01-13 12:20:59 -08003426 int childCount = layout.getChildCount();
3427 for (int j = 0; j < childCount; j++) {
3428 final View view = layout.getChildAt(j);
3429 Object tag = view.getTag();
Winson Chungaafa03c2010-06-11 17:34:16 -07003430
Joe Onorato0589f0f2010-02-08 13:44:00 -08003431 if (tag instanceof ShortcutInfo) {
3432 final ShortcutInfo info = (ShortcutInfo) tag;
Romain Guy629de3e2010-01-13 12:20:59 -08003433 final Intent intent = info.intent;
3434 final ComponentName name = intent.getComponent();
Winson Chungaafa03c2010-06-11 17:34:16 -07003435
Joe Onorato64e6be72010-03-05 15:05:52 -05003436 if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3437 for (String packageName: packageNames) {
3438 if (packageName.equals(name.getPackageName())) {
Joe Onorato64e6be72010-03-05 15:05:52 -05003439 LauncherModel.deleteItemFromDatabase(mLauncher, info);
3440 childrenToRemove.add(view);
3441 }
3442 }
Romain Guy629de3e2010-01-13 12:20:59 -08003443 }
Adam Cohendf2cc412011-04-27 16:56:57 -07003444 } else if (tag instanceof FolderInfo) {
3445 final FolderInfo info = (FolderInfo) tag;
Joe Onorato0589f0f2010-02-08 13:44:00 -08003446 final ArrayList<ShortcutInfo> contents = info.contents;
Romain Guy629de3e2010-01-13 12:20:59 -08003447 final int contentsCount = contents.size();
Adam Cohendf1e4e82011-06-24 15:57:39 -07003448 final ArrayList<ShortcutInfo> appsToRemoveFromFolder =
3449 new ArrayList<ShortcutInfo>();
Winson Chungaafa03c2010-06-11 17:34:16 -07003450
Romain Guy629de3e2010-01-13 12:20:59 -08003451 for (int k = 0; k < contentsCount; k++) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08003452 final ShortcutInfo appInfo = contents.get(k);
Romain Guy629de3e2010-01-13 12:20:59 -08003453 final Intent intent = appInfo.intent;
3454 final ComponentName name = intent.getComponent();
Winson Chungaafa03c2010-06-11 17:34:16 -07003455
Joe Onorato64e6be72010-03-05 15:05:52 -05003456 if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3457 for (String packageName: packageNames) {
3458 if (packageName.equals(name.getPackageName())) {
Adam Cohendf1e4e82011-06-24 15:57:39 -07003459 appsToRemoveFromFolder.add(appInfo);
Joe Onorato64e6be72010-03-05 15:05:52 -05003460 }
3461 }
Romain Guy629de3e2010-01-13 12:20:59 -08003462 }
3463 }
Adam Cohendf1e4e82011-06-24 15:57:39 -07003464 for (ShortcutInfo item: appsToRemoveFromFolder) {
3465 info.remove(item);
3466 LauncherModel.deleteItemFromDatabase(mLauncher, item);
Romain Guy629de3e2010-01-13 12:20:59 -08003467 }
Romain Guy629de3e2010-01-13 12:20:59 -08003468 } else if (tag instanceof LauncherAppWidgetInfo) {
3469 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag;
3470 final AppWidgetProviderInfo provider =
3471 widgets.getAppWidgetInfo(info.appWidgetId);
Daniel Sandlere1cc6c32010-05-07 11:49:29 -04003472 if (provider != null) {
Joe Onorato64e6be72010-03-05 15:05:52 -05003473 for (String packageName: packageNames) {
3474 if (packageName.equals(provider.provider.getPackageName())) {
Joe Onorato64e6be72010-03-05 15:05:52 -05003475 LauncherModel.deleteItemFromDatabase(mLauncher, info);
Winson Chungaafa03c2010-06-11 17:34:16 -07003476 childrenToRemove.add(view);
Joe Onorato64e6be72010-03-05 15:05:52 -05003477 }
3478 }
Romain Guy629de3e2010-01-13 12:20:59 -08003479 }
Romain Guy574d20e2009-06-01 15:34:04 -07003480 }
3481 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003482
Romain Guy629de3e2010-01-13 12:20:59 -08003483 childCount = childrenToRemove.size();
3484 for (int j = 0; j < childCount; j++) {
3485 View child = childrenToRemove.get(j);
Winson Chung7a25a9e2011-01-30 13:33:56 -08003486 // Note: We can not remove the view directly from CellLayoutChildren as this
3487 // does not re-mark the spaces as unoccupied.
3488 layoutParent.removeViewInLayout(child);
Romain Guy629de3e2010-01-13 12:20:59 -08003489 if (child instanceof DropTarget) {
3490 mDragController.removeDropTarget((DropTarget)child);
3491 }
Romain Guy574d20e2009-06-01 15:34:04 -07003492 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003493
Romain Guy629de3e2010-01-13 12:20:59 -08003494 if (childCount > 0) {
3495 layout.requestLayout();
3496 layout.invalidate();
Romain Guy5c16f3e2010-01-12 17:24:58 -08003497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003498 }
Romain Guy629de3e2010-01-13 12:20:59 -08003499 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003500 }
3501 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003502
Joe Onorato64e6be72010-03-05 15:05:52 -05003503 void updateShortcuts(ArrayList<ApplicationInfo> apps) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003504 ArrayList<CellLayoutChildren> childrenLayouts = getWorkspaceAndHotseatCellLayoutChildren();
3505 for (CellLayoutChildren layout: childrenLayouts) {
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003506 int childCount = layout.getChildCount();
3507 for (int j = 0; j < childCount; j++) {
3508 final View view = layout.getChildAt(j);
3509 Object tag = view.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08003510 if (tag instanceof ShortcutInfo) {
3511 ShortcutInfo info = (ShortcutInfo)tag;
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003512 // We need to check for ACTION_MAIN otherwise getComponent() might
3513 // return null for some shortcuts (for instance, for shortcuts to
3514 // web pages.)
3515 final Intent intent = info.intent;
3516 final ComponentName name = intent.getComponent();
3517 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
Joe Onorato64e6be72010-03-05 15:05:52 -05003518 Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
3519 final int appCount = apps.size();
Winson Chungaafa03c2010-06-11 17:34:16 -07003520 for (int k = 0; k < appCount; k++) {
Joe Onorato64e6be72010-03-05 15:05:52 -05003521 ApplicationInfo app = apps.get(k);
3522 if (app.componentName.equals(name)) {
3523 info.setIcon(mIconCache.getIcon(info.intent));
3524 ((TextView)view).setCompoundDrawablesWithIntrinsicBounds(null,
3525 new FastBitmapDrawable(info.getIcon(mIconCache)),
3526 null, null);
3527 }
3528 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003529 }
3530 }
3531 }
3532 }
3533 }
3534
Joe Onorato14f122b2009-11-19 14:06:36 -08003535 void moveToDefaultScreen(boolean animate) {
Winson Chungde1af762011-07-21 16:44:07 -07003536 if (!isSmall()) {
3537 if (animate) {
3538 snapToPage(mDefaultPage);
3539 } else {
3540 setCurrentPage(mDefaultPage);
3541 }
Joe Onoratoc45b1682010-01-11 18:48:40 -05003542 }
Michael Jurka0142d492010-08-25 17:46:15 -07003543 getChildAt(mDefaultPage).requestFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003544 }
3545
Michael Jurka0142d492010-08-25 17:46:15 -07003546 @Override
3547 public void syncPages() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003548 }
Michael Jurka0142d492010-08-25 17:46:15 -07003549
3550 @Override
Winson Chungf314b0e2011-08-16 11:54:27 -07003551 public void syncPageItems(int page, boolean immediate) {
Michael Jurka0142d492010-08-25 17:46:15 -07003552 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07003553
3554 @Override
3555 protected String getCurrentPageDescription() {
3556 int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
3557 return String.format(mContext.getString(R.string.workspace_scroll_format),
3558 page + 1, getChildCount());
3559 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07003560
3561 public void getLocationInDragLayer(int[] loc) {
3562 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
3563 }
Winson Chung32174c82011-07-19 15:47:55 -07003564
Adam Cohen21b41102011-11-01 17:29:52 -07003565 void setFadeForOverScroll(float fade) {
3566 if (!isScrollingIndicatorEnabled()) return;
3567
3568 mOverscrollFade = fade;
3569 float reducedFade = 0.5f + 0.5f * (1 - fade);
3570 final ViewGroup parent = (ViewGroup) getParent();
Winson Chunge96e5cb2011-11-28 13:02:56 -08003571 final ImageView qsbDivider = (ImageView) (parent.findViewById(R.id.qsb_divider));
Adam Cohen21b41102011-11-01 17:29:52 -07003572 final ImageView dockDivider = (ImageView) (parent.findViewById(R.id.dock_divider));
Michael Jurkaafaa0502011-12-13 18:22:50 -08003573 final View scrollIndicator = getScrollingIndicator();
Adam Cohen21b41102011-11-01 17:29:52 -07003574
3575 cancelScrollingIndicatorAnimations();
Winson Chunge96e5cb2011-11-28 13:02:56 -08003576 if (qsbDivider != null) qsbDivider.setAlpha(reducedFade);
3577 if (dockDivider != null) dockDivider.setAlpha(reducedFade);
Adam Cohen21b41102011-11-01 17:29:52 -07003578 scrollIndicator.setAlpha(1 - fade);
3579 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003580}