Workspace.java revision d4b28d2e1bb73fcdb1e46bb06e1666e27af2e643
131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/*
231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * you may not use this file except in compliance with the License.
631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * You may obtain a copy of the License at
731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
1031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * See the License for the specific language governing permissions and
1431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * limitations under the License.
1531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
1631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17325dc23624160689e59fbac708cf6f222b20d025Daniel Sandlerpackage com.android.launcher3;
1831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
190280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurkaimport android.animation.Animator;
20f358a4bbd6ce5b6ddadfae19e6b9c6872530d627Adam Cohenimport android.animation.AnimatorListenerAdapter;
2122cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohenimport android.animation.LayoutTransition;
220280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurkaimport android.animation.ObjectAnimator;
23ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohenimport android.animation.PropertyValuesHolder;
247066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyalimport android.annotation.SuppressLint;
25c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohenimport android.annotation.TargetApi;
268f573952b8729b4319043ae0908997ecd2d68951Dianne Hackbornimport android.app.WallpaperManager;
27bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurkaimport android.appwidget.AppWidgetHostView;
28629de3ef739883c0962423cc0c3a26299f162d3dRomain Guyimport android.appwidget.AppWidgetProviderInfo;
29495f2892288504f4ab87e62957b3f71144dd73c2Adam Powellimport android.content.ComponentName;
3031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Context;
317247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroyimport android.content.res.Resources;
324be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onoratoimport android.graphics.Bitmap;
3331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Canvas;
34c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyalimport android.graphics.Matrix;
35babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chungimport android.graphics.Paint;
36b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chungimport android.graphics.Point;
37043f2af567178b82b0b41f12d379e7dd12da2936Winson Chungimport android.graphics.PointF;
3831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Rect;
394be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onoratoimport android.graphics.Region.Op;
40f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Projectimport android.graphics.drawable.Drawable;
41c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohenimport android.os.Build;
420fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyalimport android.os.Handler;
43956091ba7863bd72086275e61084864994dd6fa7Joe Onoratoimport android.os.IBinder;
44495f2892288504f4ab87e62957b3f71144dd73c2Adam Powellimport android.os.Parcelable;
4531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.util.AttributeSet;
46291ad12232c98e383d44d76ffe09e97e204c61bcDaniel Sandlerimport android.util.Log;
471462de39f01cec0ba785386532719cb0207dd827Adam Cohenimport android.util.SparseArray;
4831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.MotionEvent;
4931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.View;
504ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyalimport android.view.ViewDebug;
516e3140865d3f0def2e55934d8b0b2c1503386e54Winson Chungimport android.view.ViewGroup;
5253805218809cc1f93a0f1307b442df7c65a3068cAdam Cohenimport android.view.accessibility.AccessibilityManager;
53c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyalimport android.view.animation.DecelerateInterpolator;
54c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyalimport android.view.animation.Interpolator;
55f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Projectimport android.widget.TextView;
5683a8f042adda926489494dff217c15ab696139b4Sunny Goyal
57bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohenimport com.android.launcher3.Launcher.CustomContentCallbacks;
58c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohenimport com.android.launcher3.Launcher.LauncherOverlay;
59d5bd67dfa9ee5fda2384a75231b7a68ceb8e9bd5Sunny Goyalimport com.android.launcher3.UninstallDropTarget.DropTargetSource;
6083a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
6183a8f042adda926489494dff217c15ab696139b4Sunny Goyalimport com.android.launcher3.accessibility.LauncherAccessibilityDelegate.AccessibilityDragSource;
6258376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyalimport com.android.launcher3.accessibility.OverviewScreenAccessibilityDelegate;
63e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyalimport com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
64470f900fce017dc015851bfe3e591be5feedf6ccSunny Goyalimport com.android.launcher3.compat.AppWidgetManagerCompat;
65651077bdd603bb182be039925fd17bdf0da15016Sunny Goyalimport com.android.launcher3.compat.UserHandleCompat;
66e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickhamimport com.android.launcher3.config.FeatureFlags;
676c56c68555dcb3d99b05d5faecd582a1f683cc92Sunny Goyalimport com.android.launcher3.config.ProviderConfig;
68fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.DragController;
69fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.DragLayer;
70fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.DragScroller;
71fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.DragView;
72fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.SpringLoadedDragController;
73470f900fce017dc015851bfe3e591be5feedf6ccSunny Goyalimport com.android.launcher3.folder.Folder;
74470f900fce017dc015851bfe3e591be5feedf6ccSunny Goyalimport com.android.launcher3.folder.FolderIcon;
75aa953654279d7cca29682d85111e398ea1f20390Hyunyoung Songimport com.android.launcher3.logging.UserEventDispatcher;
76d4b28d2e1bb73fcdb1e46bb06e1666e27af2e643Tony Wickhamimport com.android.launcher3.pageindicators.PageIndicatorLine;
77ddec1c739ef37c3a042982b8943fe42e04b65f4cHyunyoung Songimport com.android.launcher3.userevent.nano.LauncherLogProto;
78ddec1c739ef37c3a042982b8943fe42e04b65f4cHyunyoung Songimport com.android.launcher3.userevent.nano.LauncherLogProto.Target;
79e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyalimport com.android.launcher3.util.LongArrayMap;
80091440a9cb9d4f42406631004aa484cbb79214caAdam Cohenimport com.android.launcher3.util.Thunk;
819eba1fd75e9fa6b0dc5cad9a4e817b3b167d2461Sunny Goyalimport com.android.launcher3.util.WallpaperOffsetInterpolator;
823f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.widget.PendingAddShortcutInfo;
833f471440a8b6b71d4c15501a96befd3b715c9e8fHyunyoung Songimport com.android.launcher3.widget.PendingAddWidgetInfo;
8483a8f042adda926489494dff217c15ab696139b4Sunny Goyal
85716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohenimport java.util.ArrayList;
86dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohenimport java.util.HashMap;
87716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohenimport java.util.HashSet;
8895abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyalimport java.util.concurrent.atomic.AtomicInteger;
89716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen
9031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
910142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka * The workspace is a wide area with a wallpaper and a finite number of pages.
920142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka * Each page contains a number of icons, folders or widgets the user can
93aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung * interact with. A workspace is meant to be used with a fixed width only.
9431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
955a1f53b306a4fcf56aad577987386ea1b2dfe6b9Sunny Goyalpublic class Workspace extends PagedView
96d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        implements DropTarget, DragSource, DragScroller, View.OnTouchListener,
9777e1f47520958edd7ec04e203db65b932537bd1cJohn Spurlock        DragController.DragListener, LauncherTransitionable, ViewGroup.OnHierarchyChangeListener,
98aa953654279d7cca29682d85111e398ea1f20390Hyunyoung Song        Insettable, DropTargetSource, AccessibilityDragSource, UserEventDispatcher.LaunchSourceProvider {
993a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato    private static final String TAG = "Launcher.Workspace";
1000142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
10158376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal    private static boolean ENFORCE_DRAG_EVENT_ORDER = false;
10258376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal
103cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal    private static final int SNAP_OFF_EMPTY_SCREEN_DURATION = 400;
104cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal    private static final int FADE_EMPTY_SCREEN_DURATION = 150;
105ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
106ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen    private static final int ADJACENT_SCREEN_DROP_DURATION = 300;
107ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen
108cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal    private static final boolean MAP_NO_RECURSE = false;
109cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal    private static final boolean MAP_RECURSE = true;
110cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal
111cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal    // The screen id used for the empty screen always present to the right.
112da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal    public static final long EXTRA_EMPTY_SCREEN_ID = -201;
113da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal    // The is the first screen. It is always present, even if its empty.
114da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal    public static final long FIRST_SCREEN_ID = 0;
115da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal
116cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal    private final static long CUSTOM_CONTENT_SCREEN_ID = -301;
117aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren
1183d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen    private static final long CUSTOM_CONTENT_GESTURE_DELAY = 200;
1193d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen    private long mTouchDownTime = -1;
1203d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen    private long mCustomContentShowTime = -1;
1213d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen
12222cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen    private LayoutTransition mLayoutTransition;
123091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk final WallpaperManager mWallpaperManager;
124aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1255084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen    private ShortcutAndWidgetContainer mDragSourceInternal;
1265084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen
127e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal    @Thunk LongArrayMap<CellLayout> mWorkspaceScreens = new LongArrayMap<>();
128091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk ArrayList<Long> mScreenOrder = new ArrayList<Long>();
129dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
130091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk Runnable mRemoveEmptyScreenRunnable;
131091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk boolean mDeferRemoveExtraEmptyScreen = false;
132e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    @Thunk boolean mAddNewPageOnDrag = true;
133ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
13431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
13531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * CellInfo for the cell that is currently being dragged
13631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
13731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private CellLayout.CellInfo mDragInfo;
138aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
13970864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    /**
14070864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * Target drop area calculated during last acceptDrop call.
14170864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     */
142091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk int[] mTargetCell = new int[2];
143c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    private int mDragOverX = -1;
144c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    private int mDragOverY = -1;
14531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
146bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen    CustomContentCallbacks mCustomContentCallbacks;
147bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen    boolean mCustomContentShowing;
148c36fa5ce395bd39d16f6a998372700ed53d5d9d6Adam Cohen    private float mLastCustomContentScrollProgress = -1f;
14953805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen    private String mCustomContentDescription = "";
150bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen
1516569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy    /**
1526569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy     * The CellLayout that is currently being dragged over
1536569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy     */
154091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk CellLayout mDragTargetLayout = null;
155c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    /**
15695cdb3a0211650e76adb451588ce89fd585b178fTony Wickham     * The CellLayout that we will show as highlighted
157c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen     */
158c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    private CellLayout mDragOverlappingLayout = null;
159c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
160c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    /**
161c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen     * The CellLayout which will be dropped to
162c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen     */
163c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    private CellLayout mDropToLayout = null;
164c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
165091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk Launcher mLauncher;
166091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk IconCache mIconCache;
167091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk DragController mDragController;
168aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1694516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    // These are temporary variables to prevent having to allocate a new object just to
1704516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
171cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal    private static final Rect sTempRect = new Rect();
172cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal    private final int[] mTempXY = new int[2];
173091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk float[] mDragViewVisualCenter = new float[2];
1744516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    private float[] mTempCellLayoutCenterCoordinates = new float[2];
175cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal    private int[] mTempVisiblePagesRange = new int[2];
176ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal    private Matrix mTempMatrix = new Matrix();
17731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
178c2f7f4794867cd1b3e97fd0f379ad8ad15958a6cMichael Jurka    private SpringLoadedDragController mSpringLoadedDragController;
179b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung    private float mSpringLoadedShrinkFactor;
180f358a4bbd6ce5b6ddadfae19e6b9c6872530d627Adam Cohen    private float mOverviewModeShrinkFactor;
181d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka
1821262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    // State variable that indicates whether the pages are small (ie when you're
183dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    // in all apps or customize mode)
184d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
185006ee269ba2e9946a83b42f96d4a0296254cba4aWinson Chung    enum State {
186da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        NORMAL          (SearchDropTargetBar.State.INVISIBLE, false, false),
187da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        NORMAL_HIDDEN   (SearchDropTargetBar.State.INVISIBLE, false, false),
188f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham        SPRING_LOADED   (SearchDropTargetBar.State.DROP_TARGET, false, true),
189f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham        OVERVIEW        (SearchDropTargetBar.State.INVISIBLE, true, true),
190f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham        OVERVIEW_HIDDEN (SearchDropTargetBar.State.INVISIBLE, true, false);
191006ee269ba2e9946a83b42f96d4a0296254cba4aWinson Chung
1922e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal        public final SearchDropTargetBar.State searchDropTargetBarState;
1932e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal        public final boolean shouldUpdateWidget;
194f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham        public final boolean hasMultipleVisiblePages;
195006ee269ba2e9946a83b42f96d4a0296254cba4aWinson Chung
196f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham        State(SearchDropTargetBar.State searchBarState, boolean shouldUpdateWidget,
197f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham                boolean hasMultipleVisiblePages) {
1982e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            searchDropTargetBarState = searchBarState;
1992e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            this.shouldUpdateWidget = shouldUpdateWidget;
200f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham            this.hasMultipleVisiblePages = hasMultipleVisiblePages;
201006ee269ba2e9946a83b42f96d4a0296254cba4aWinson Chung        }
2022e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal    }
203006ee269ba2e9946a83b42f96d4a0296254cba4aWinson Chung
2044ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyal    @ViewDebug.ExportedProperty(category = "launcher")
2057777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen    private State mState = State.NORMAL;
206d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    private boolean mIsSwitchingState = false;
207d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
208d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    boolean mAnimatingViewIntoPlace = false;
209d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    boolean mChildrenLayersEnabled = true;
210dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka
211accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen    private boolean mStripScreensOnPageStopMoving = false;
212accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen
21354fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy    /** Is the user is dragging an item near the edge of a page? */
2141262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    private boolean mInScrollArea = false;
2151262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy
216e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler    private HolographicOutlineHelper mOutlineHelper;
217091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk Bitmap mDragOutline = null;
218f8304f05db24b504b7a3bc113929af29df7daf6eMichael Jurka    public static final int DRAG_BITMAP_PADDING = 2;
219869390b6abd6e005a5ce85a8ee3c11daeecce919Michael Jurka    private boolean mWorkspaceFadeInAdjacentScreens;
2204be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
2219eba1fd75e9fa6b0dc5cad9a4e817b3b167d2461Sunny Goyal    final WallpaperOffsetInterpolator mWallpaperOffset;
2222a4f4928a964fa8eef9ea6542f835ad4a79b9bd5Michael Jurka
223091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk Runnable mDelayedResizeRunnable;
224f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    private Runnable mDelayedSnapToPageRunnable;
225c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
22619072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen    // Variables relating to the creation of user folders by hovering shortcuts over shortcuts
227482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen    private static final int FOLDER_CREATION_TIMEOUT = 0;
228fa3c58f647e603760c0bd929166c0edee25675a9Adam Cohen    public static final int REORDER_TIMEOUT = 350;
22919072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen    private final Alarm mFolderCreationAlarm = new Alarm();
230482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen    private final Alarm mReorderAlarm = new Alarm();
231f172b747c24f28e29baaaf58f08bab48847b7a40Adam Cohen    private FolderIcon.PreviewBackground mFolderCreateBg;
232c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    private FolderIcon mDragOverFolderIcon = null;
23319072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen    private boolean mCreateUserFolderOnDrop = false;
234c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    private boolean mAddToExistingFolderOnDrop = false;
2353aff81c759a2912bed45643f7e1fb503fcfe69b0Adam Cohen    private float mMaxDistanceForFolderCreation;
236073a46ff635d089cbcb15d939d1de42dd2b86c97Adam Cohen
237508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal    private final Canvas mCanvas = new Canvas();
238508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal
239f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen    // Variables relating to touch disambiguation (scrolling workspace vs. scrolling a widget)
240f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen    private float mXDown;
241f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen    private float mYDown;
242f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen    final static float START_DAMPING_TOUCH_SLOP_ANGLE = (float) Math.PI / 6;
243f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen    final static float MAX_SWIPE_ANGLE = (float) Math.PI / 3;
244f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen    final static float TOUCH_SLOP_DAMPING_FACTOR = 4;
245f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen
246ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    // Relating to the animation of items being dropped externally
247d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen    public static final int ANIMATE_INTO_POSITION_AND_DISAPPEAR = 0;
248d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen    public static final int ANIMATE_INTO_POSITION_AND_REMAIN = 1;
249d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen    public static final int ANIMATE_INTO_POSITION_AND_RESIZE = 2;
250d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen    public static final int COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION = 3;
251d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen    public static final int CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION = 4;
252ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
253482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen    // Related to dragging, folder creation and reordering
254482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen    private static final int DRAG_MODE_NONE = 0;
255482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen    private static final int DRAG_MODE_CREATE_FOLDER = 1;
256482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen    private static final int DRAG_MODE_ADD_TO_FOLDER = 2;
257482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen    private static final int DRAG_MODE_REORDER = 3;
258482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen    private int mDragMode = DRAG_MODE_NONE;
259091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk int mLastReorderX = -1;
260091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk int mLastReorderY = -1;
261482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
2621462de39f01cec0ba785386532719cb0207dd827Adam Cohen    private SparseArray<Parcelable> mSavedStates;
2631462de39f01cec0ba785386532719cb0207dd827Adam Cohen    private final ArrayList<Integer> mRestoredPages = new ArrayList<Integer>();
2641462de39f01cec0ba785386532719cb0207dd827Adam Cohen
2657d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen    private float mCurrentScale;
2667044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung    private float mTransitionProgress;
2674b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen
268091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk Runnable mDeferredAction;
2691e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    private boolean mDeferDropAfterUninstall;
2701e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    private boolean mUninstallSuccessful;
2711e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
272c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    // State related to Launcher Overlay
273c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    LauncherOverlay mLauncherOverlay;
274c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    boolean mScrollInteractionBegan;
275c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    boolean mStartedSendingScrollEvents;
276c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    float mLastOverlaySroll = 0;
277c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    // Total over scrollX in the overlay direction.
278c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    private int mUnboundedScrollX;
27929d853c2ddf6432685a54e3966820f1a81cee183Tony Wickham    private boolean mForceDrawAdjacentPages = false;
280c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    // Total over scrollX in the overlay direction.
281c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    private float mOverlayTranslation;
282c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen
283dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung    // Handles workspace state transitions
284dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung    private WorkspaceStateTransitionAnimation mStateTransitionAnimation;
285dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung
2861d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal    private AccessibilityDelegate mPagesAccessibilityDelegate;
2871d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal
28831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
28931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Used to inflate the Workspace from XML.
29031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
29131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param context The application's context.
2920142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka     * @param attrs The attributes set containing the Workspace's customization values.
29331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
29431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public Workspace(Context context, AttributeSet attrs) {
29531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        this(context, attrs, 0);
29631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
29731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
29831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
29931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Used to inflate the Workspace from XML.
30031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
30131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param context The application's context.
3020142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka     * @param attrs The attributes set containing the Workspace's customization values.
30331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param defStyle Unused.
30431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
30531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public Workspace(Context context, AttributeSet attrs, int defStyle) {
30631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super(context, attrs, defStyle);
3075f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka
308e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler        mOutlineHelper = HolographicOutlineHelper.obtain(context);
309e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler
3108bc66c7fe4d8f7ac1715010806f49840194279a5Michael Jurka        mLauncher = (Launcher) context;
311dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung        mStateTransitionAnimation = new WorkspaceStateTransitionAnimation(mLauncher, this);
312867ca62c0e4b811d46734ff940d59c7a787525c8Winson Chung        final Resources res = getResources();
3132e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen        DeviceProfile grid = mLauncher.getDeviceProfile();
3142e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen        mWorkspaceFadeInAdjacentScreens = grid.shouldFadeAdjacentWorkspaceScreens();
315869390b6abd6e005a5ce85a8ee3c11daeecce919Michael Jurka        mFadeInAdjacentScreens = false;
3168f573952b8729b4319043ae0908997ecd2d68951Dianne Hackborn        mWallpaperManager = WallpaperManager.getInstance(context);
317aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3189eba1fd75e9fa6b0dc5cad9a4e817b3b167d2461Sunny Goyal        mWallpaperOffset = new WallpaperOffsetInterpolator(this);
3199eba1fd75e9fa6b0dc5cad9a4e817b3b167d2461Sunny Goyal
320b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        mSpringLoadedShrinkFactor =
3214f7eb4077e901a28b2b698060079292b56e991bdWinson Chung                res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
3224f7eb4077e901a28b2b698060079292b56e991bdWinson Chung        mOverviewModeShrinkFactor =
3234f7eb4077e901a28b2b698060079292b56e991bdWinson Chung                res.getInteger(R.integer.config_workspaceOverviewShrinkPercentage) / 100f;
32431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3258b805b17158886035b38261eb611d8641701ae43Michael Jurka        setOnHierarchyChangeListener(this);
3260d44e9482b95d8f163b28bf20131c4349185b589Joe Onorato        setHapticFeedbackEnabled(false);
3270142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
32831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        initWorkspace();
329c35afb2c9df8cc56c77bf21ce2eaa213f6f31251Winson Chung
330c35afb2c9df8cc56c77bf21ce2eaa213f6f31251Winson Chung        // Disable multitouch across the workspace/all apps/customize tray
331c35afb2c9df8cc56c77bf21ce2eaa213f6f31251Winson Chung        setMotionEventSplittingEnabled(true);
33231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
33331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
33477e1f47520958edd7ec04e203db65b932537bd1cJohn Spurlock    @Override
33577e1f47520958edd7ec04e203db65b932537bd1cJohn Spurlock    public void setInsets(Rect insets) {
33677e1f47520958edd7ec04e203db65b932537bd1cJohn Spurlock        mInsets.set(insets);
3376400b84d5594d7b9244e3b118f107033eb8b87a0Adam Cohen
3386400b84d5594d7b9244e3b118f107033eb8b87a0Adam Cohen        CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID);
3396400b84d5594d7b9244e3b118f107033eb8b87a0Adam Cohen        if (customScreen != null) {
3406400b84d5594d7b9244e3b118f107033eb8b87a0Adam Cohen            View customContent = customScreen.getShortcutsAndWidgets().getChildAt(0);
3416400b84d5594d7b9244e3b118f107033eb8b87a0Adam Cohen            if (customContent instanceof Insettable) {
3426400b84d5594d7b9244e3b118f107033eb8b87a0Adam Cohen                ((Insettable) customContent).setInsets(mInsets);
3436400b84d5594d7b9244e3b118f107033eb8b87a0Adam Cohen            }
3446400b84d5594d7b9244e3b118f107033eb8b87a0Adam Cohen        }
34577e1f47520958edd7ec04e203db65b932537bd1cJohn Spurlock    }
34677e1f47520958edd7ec04e203db65b932537bd1cJohn Spurlock
347038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka    // estimate the size of a widget with spans hSpan, vSpan. return MAX_VALUE for each
348038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka    // dimension if unsuccessful
3495b0e669169ea2c951bf2f6f71faf793b24db3c23Sunny Goyal    public int[] estimateItemSize(ItemInfo itemInfo, boolean springLoaded) {
350038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        int[] size = new int[2];
351038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        if (getChildCount() > 0) {
352ad7db6e8ddddae423d027163a1bdf7015210f7ceWinson Chung            // Use the first non-custom page to estimate the child position
353ad7db6e8ddddae423d027163a1bdf7015210f7ceWinson Chung            CellLayout cl = (CellLayout) getChildAt(numCustomPages());
354a501d49a6a8b684bc83aa6b536f43247d75bacdfTony Wickham            Rect r = estimateItemPosition(cl, 0, 0, itemInfo.spanX, itemInfo.spanY);
355d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            size[0] = r.width();
356d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            size[1] = r.height();
357038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            if (springLoaded) {
358038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka                size[0] *= mSpringLoadedShrinkFactor;
359038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka                size[1] *= mSpringLoadedShrinkFactor;
360038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            }
361038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            return size;
362038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        } else {
363038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            size[0] = Integer.MAX_VALUE;
364038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            size[1] = Integer.MAX_VALUE;
365038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka            return size;
366038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        }
367038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka    }
368dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
369a501d49a6a8b684bc83aa6b536f43247d75bacdfTony Wickham    public Rect estimateItemPosition(CellLayout cl, int hCell, int vCell, int hSpan, int vSpan) {
370d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        Rect r = new Rect();
371038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        cl.cellToRect(hCell, vCell, hSpan, vSpan, r);
372038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka        return r;
373038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka    }
374038f9d8bfb53288e7cf5812f62ec3d5b25fec965Michael Jurka
37558376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal    @Override
376aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal    public void onDragStart(final DragSource source, ItemInfo info, int dragAction) {
37758376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        if (ENFORCE_DRAG_EVENT_ORDER) {
37858376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal            enfoceDragParity("onDragStart", 0, 0);
37958376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        }
38058376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal
3813a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka        updateChildrenLayersEnabled(false);
382641d71d6da6c77cc32e5d0f65ccae89caed1ac34Winson Chung        mLauncher.lockScreenOrientation();
383ab2d9d72ae0a37553a82fa845c412167c727a258Sandeep Siddhartha        mLauncher.onInteractionBegin();
384f561bdf68dd304e1d7c3f7796621a4cc39ca15f2Winson Chung        // Prevent any Un/InstallShortcutReceivers from updating the db while we are dragging
385f561bdf68dd304e1d7c3f7796621a4cc39ca15f2Winson Chung        InstallShortcutReceiver.enableInstallQueue();
38658376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal
38758376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        if (mAddNewPageOnDrag) {
38858376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal            mDeferRemoveExtraEmptyScreen = false;
38958376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal            addExtraEmptyScreenOnDrag();
39058376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        }
391d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    }
392d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
393e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    public void setAddNewPageOnDrag(boolean addPage) {
394e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal        mAddNewPageOnDrag = addPage;
395e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    }
396689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen
397689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen    public void deferRemoveExtraEmptyScreen() {
398689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen        mDeferRemoveExtraEmptyScreen = true;
399689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen    }
400689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen
40158376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal    @Override
402d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    public void onDragEnd() {
40358376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        if (ENFORCE_DRAG_EVENT_ORDER) {
40458376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal            enfoceDragParity("onDragEnd", 0, 0);
40558376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        }
40658376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal
407689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen        if (!mDeferRemoveExtraEmptyScreen) {
408689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen            removeExtraEmptyScreen(true, mDragSourceInternal != null);
409689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen        }
410689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen
4113a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka        updateChildrenLayersEnabled(false);
4124b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung        mLauncher.unlockScreenOrientation(false);
413f561bdf68dd304e1d7c3f7796621a4cc39ca15f2Winson Chung
414f561bdf68dd304e1d7c3f7796621a4cc39ca15f2Winson Chung        // Re-enable any Un/InstallShortcutReceiver and now process any queued items
415f561bdf68dd304e1d7c3f7796621a4cc39ca15f2Winson Chung        InstallShortcutReceiver.disableAndFlushInstallQueue(getContext());
4165084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen
4175084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        mDragSourceInternal = null;
418ab2d9d72ae0a37553a82fa845c412167c727a258Sandeep Siddhartha        mLauncher.onInteractionEnd();
419d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    }
420d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
42131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
42231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Initializes various states for this workspace.
42331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
4240142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    protected void initWorkspace() {
42577ccb8d03c17aeb32dccfb0930ae974f6cca7455Sunny Goyal        mCurrentPage = getDefaultPage();
426cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler        LauncherAppState app = LauncherAppState.getInstance();
4272e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen        DeviceProfile grid = mLauncher.getDeviceProfile();
4280589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = app.getIconCache();
429cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy        setWillNotDraw(false);
430ce3cbd145b4222779abae32869da8dd3c2aefb67Romain Guy        setClipChildren(false);
431ce3cbd145b4222779abae32869da8dd3c2aefb67Romain Guy        setClipToPadding(false);
4327777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        setChildrenDrawnWithCacheEnabled(true);
433dadd984a891a574cebf3dfd38a454a0e6a4228b3Derek Prothro
434c82d2622bc7305fd3545fe7df97a30187283113fWinson Chung        setMinScale(mOverviewModeShrinkFactor);
43522cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen        setupLayoutTransition();
43631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
4375f8afe6280eae34620067696173e71943e1a30a3Winson Chung        mMaxDistanceForFolderCreation = (0.55f * grid.iconSizePx);
438a6a0547fa6df0769fcd107a2e5f7c7d527fa0700Michael Jurka
439a6a0547fa6df0769fcd107a2e5f7c7d527fa0700Michael Jurka        // Set the wallpaper dimensions when Launcher starts up
440824fcb3a3e05166d7e1bd9c2cd821aeaebc994c5Adam Cohen        setWallpaperDimension();
4414d113a5ff44ff1a7f19263bde21581fbf9a54212Sunny Goyal
4424d113a5ff44ff1a7f19263bde21581fbf9a54212Sunny Goyal        setEdgeGlowColor(getResources().getColor(R.color.workspace_edge_effect_color));
44331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
44431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
44577ccb8d03c17aeb32dccfb0930ae974f6cca7455Sunny Goyal    private int getDefaultPage() {
44677ccb8d03c17aeb32dccfb0930ae974f6cca7455Sunny Goyal        return numCustomPages();
44777ccb8d03c17aeb32dccfb0930ae974f6cca7455Sunny Goyal    }
44877ccb8d03c17aeb32dccfb0930ae974f6cca7455Sunny Goyal
44922cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen    private void setupLayoutTransition() {
45022cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen        // We want to show layout transitions when pages are deleted, to close the gap.
45122cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen        mLayoutTransition = new LayoutTransition();
45222cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen        mLayoutTransition.enableTransitionType(LayoutTransition.DISAPPEARING);
45322cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen        mLayoutTransition.enableTransitionType(LayoutTransition.CHANGE_DISAPPEARING);
45422cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen        mLayoutTransition.disableTransitionType(LayoutTransition.APPEARING);
45522cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen        mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING);
45622cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen        setLayoutTransition(mLayoutTransition);
45722cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen    }
45822cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen
459964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung    void enableLayoutTransitions() {
460964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        setLayoutTransition(mLayoutTransition);
461964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung    }
462964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung    void disableLayoutTransitions() {
463964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        setLayoutTransition(null);
464964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung    }
465964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung
46631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
4678b805b17158886035b38261eb611d8641701ae43Michael Jurka    public void onChildViewAdded(View parent, View child) {
46831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!(child instanceof CellLayout)) {
46931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
47031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
4712801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        CellLayout cl = ((CellLayout) child);
4722801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        cl.setOnInterceptTouchListener(this);
4732801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        cl.setClickable(true);
474c46bfef5dfd8c6867641bdc32c27b03e2f183726Sunny Goyal        cl.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
475f70696d8cc9b68c1491496de952349866dd6f6edWinson Chung        super.onChildViewAdded(parent, child);
4768b805b17158886035b38261eb611d8641701ae43Michael Jurka    }
4778b805b17158886035b38261eb611d8641701ae43Michael Jurka
478920d7f433e5be7b928fe52312c2e65d278b13ee5Michael Jurka    protected boolean shouldDrawChild(View child) {
479920d7f433e5be7b928fe52312c2e65d278b13ee5Michael Jurka        final CellLayout cl = (CellLayout) child;
480920d7f433e5be7b928fe52312c2e65d278b13ee5Michael Jurka        return super.shouldDrawChild(child) &&
481f4bd23662d7fb76007624222a1fe678b3a37b874Winson Chung            (mIsSwitchingState ||
482f4bd23662d7fb76007624222a1fe678b3a37b874Winson Chung             cl.getShortcutsAndWidgets().getAlpha() > 0 ||
483920d7f433e5be7b928fe52312c2e65d278b13ee5Michael Jurka             cl.getBackgroundAlpha() > 0);
484920d7f433e5be7b928fe52312c2e65d278b13ee5Michael Jurka    }
485920d7f433e5be7b928fe52312c2e65d278b13ee5Michael Jurka
48631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
48731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @return The open folder on the current screen, or null if there is none
48831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
48983a8f042adda926489494dff217c15ab696139b4Sunny Goyal    public Folder getOpenFolder() {
490716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen        DragLayer dragLayer = mLauncher.getDragLayer();
4918e776a62c5cc60737f5cdbbeacfec5f958112fa7Adam Cohen        int count = dragLayer.getChildCount();
49231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        for (int i = 0; i < count; i++) {
4938e776a62c5cc60737f5cdbbeacfec5f958112fa7Adam Cohen            View child = dragLayer.getChildAt(i);
494aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            if (child instanceof Folder) {
495aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                Folder folder = (Folder) child;
496aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                if (folder.getInfo().opened)
497aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung                    return folder;
49831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
49931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
50031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return null;
50131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
50231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
503a0aa0121b91fde17e427ab2763ce312e64cf5d33Patrick Dubroy    boolean isTouchActive() {
504a0aa0121b91fde17e427ab2763ce312e64cf5d33Patrick Dubroy        return mTouchState != TOUCH_STATE_REST;
50531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
50631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
507da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal    /**
508da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal     * Initializes and binds the first page
509da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal     * @param qsb an exisitng qsb to recycle or null.
510da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal     */
511da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal    public void bindAndInitFirstWorkspaceScreen(View qsb) {
512da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        // Add the first page
513da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        CellLayout firstPage = insertNewWorkspaceScreen(Workspace.FIRST_SCREEN_ID, 0);
514da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal
515da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        if (qsb == null) {
516da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal            // Always add a QSB on the first screen.
517da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal            qsb = mLauncher.getLayoutInflater().inflate(R.layout.qsb_container,
518da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal                    firstPage, false);
519da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        }
520da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal
521da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) qsb.getLayoutParams();
522da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        lp.cellX = 0;
523da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        lp.cellY = 0;
524da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        lp.cellHSpan = firstPage.getCountX();
525da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        lp.cellVSpan = 1;
526da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        lp.canReorder = false;
527da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal
528da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        if (!firstPage.addViewToCellLayout(qsb, 0, R.id.qsb_container, lp, true)) {
529da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal            Log.e(TAG, "Failed to add to item at (0, 0) to CellLayout");
530da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        }
531da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal    }
532da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal
5339e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung    public void removeAllWorkspaceScreens() {
534964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        // Disable all layout transitions before removing all pages to ensure that we don't get the
535964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        // transition animations competing with us changing the scroll when we add pages or the
536964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        // custom content screen
537964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        disableLayoutTransitions();
538964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung
539964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        // Since we increment the current page when we call addCustomContentPage via bindScreens
540964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        // (and other places), we need to adjust the current page back when we clear the pages
541964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        if (hasCustomContent()) {
542964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung            removeCustomContentPage();
543964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        }
544964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung
545da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        // Recycle the QSB widget
546da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        View qsb = findViewById(R.id.qsb_container);
547da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        if (qsb != null) {
548da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal            ((ViewGroup) qsb.getParent()).removeView(qsb);
549da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        }
550da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal
5519e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung        // Remove the pages and clear the screen models
5529e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung        removeAllViews();
5539e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung        mScreenOrder.clear();
5549e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung        mWorkspaceScreens.clear();
555964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung
556da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        // Ensure that the first page is always present
557da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        bindAndInitFirstWorkspaceScreen(qsb);
558da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal
559964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        // Re-enable the layout transitions
560964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        enableLayoutTransitions();
5619e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung    }
5629e6a0a20d40675ef98c7fdb8892cf34b90152f7aWinson Chung
563da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal    public void insertNewWorkspaceScreenBeforeEmptyScreen(long screenId) {
56464359a53dc827797917a8d9a0697a91996789801Winson Chung        // Find the index to insert this view into.  If the empty screen exists, then
56564359a53dc827797917a8d9a0697a91996789801Winson Chung        // insert it before that.
56664359a53dc827797917a8d9a0697a91996789801Winson Chung        int insertIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID);
56764359a53dc827797917a8d9a0697a91996789801Winson Chung        if (insertIndex < 0) {
56864359a53dc827797917a8d9a0697a91996789801Winson Chung            insertIndex = mScreenOrder.size();
56964359a53dc827797917a8d9a0697a91996789801Winson Chung        }
570da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        insertNewWorkspaceScreen(screenId, insertIndex);
571dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
572dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
573da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal    public void insertNewWorkspaceScreen(long screenId) {
574da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        insertNewWorkspaceScreen(screenId, getChildCount());
57564359a53dc827797917a8d9a0697a91996789801Winson Chung    }
57664359a53dc827797917a8d9a0697a91996789801Winson Chung
577da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal    public CellLayout insertNewWorkspaceScreen(long screenId, int insertIndex) {
5785084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        if (mWorkspaceScreens.containsKey(screenId)) {
5795084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            throw new RuntimeException("Screen id " + screenId + " already exists!");
5805084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        }
5815084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen
5827066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal        // Inflate the cell layout, but do not add it automatically so that we can get the newly
5837066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal        // created CellLayout.
5847066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal        CellLayout newScreen = (CellLayout) mLauncher.getLayoutInflater().inflate(
5857066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal                        R.layout.workspace_screen, this, false /* attachToRoot */);
586dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
5877d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen        newScreen.setOnLongClickListener(mLongClickListener);
588f358a4bbd6ce5b6ddadfae19e6b9c6872530d627Adam Cohen        newScreen.setOnClickListener(mLauncher);
589837b2413f4d101b19484b52ce7b1ce7137140133Winson Chung        newScreen.setSoundEffectsEnabled(false);
590dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mWorkspaceScreens.put(screenId, newScreen);
59164359a53dc827797917a8d9a0697a91996789801Winson Chung        mScreenOrder.add(insertIndex, screenId);
59264359a53dc827797917a8d9a0697a91996789801Winson Chung        addView(newScreen, insertIndex);
593c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
5943d9490ab9520ffe3798539ed1626463a7eb6c9e1Winson Chung        LauncherAccessibilityDelegate delegate =
5953d9490ab9520ffe3798539ed1626463a7eb6c9e1Winson Chung                LauncherAppState.getInstance().getAccessibilityDelegate();
5963d9490ab9520ffe3798539ed1626463a7eb6c9e1Winson Chung        if (delegate != null && delegate.isInAccessibleDrag()) {
597e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal            newScreen.enableAccessibleDrag(true, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG);
598c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        }
599da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal
600da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal        return newScreen;
601dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
602dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
603dadd984a891a574cebf3dfd38a454a0e6a4228b3Derek Prothro    public void createCustomContentContainer() {
604dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        CellLayout customScreen = (CellLayout)
6057066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal                mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, this, false);
606c50438c802355dff714b04f42a32e518cb9e91d8Adam Cohen        customScreen.disableDragTarget();
607e2fd14b9f61d58297ff962e8c31f49439e0309a9Sunny Goyal        customScreen.disableJailContent();
608dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
609dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mWorkspaceScreens.put(CUSTOM_CONTENT_SCREEN_ID, customScreen);
610dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mScreenOrder.add(0, CUSTOM_CONTENT_SCREEN_ID);
611edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen
6122702ea006084a441f085c779ff77be1128a19badAdam Cohen        // We want no padding on the custom content
6132702ea006084a441f085c779ff77be1128a19badAdam Cohen        customScreen.setPadding(0, 0, 0, 0);
6142702ea006084a441f085c779ff77be1128a19badAdam Cohen
615edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen        addFullScreenPage(customScreen);
616ee0ce2b29f9cb06cc143c6f30ffc77f4915b0da9Michael Jurka
61782e5c98446e1f1765aabca1725cba181a56edcb4Winson Chung        // Update the custom content hint
61821cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen        if (mRestorePage != INVALID_RESTORE_PAGE) {
61921cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen            mRestorePage = mRestorePage + 1;
62021cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen        } else {
62121cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen            setCurrentPage(getCurrentPage() + 1);
62221cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen        }
623dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
624dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
625a888158880fea53479447d883391f552604e46b6Dave Hawkey    public void removeCustomContentPage() {
626a888158880fea53479447d883391f552604e46b6Dave Hawkey        CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID);
627a888158880fea53479447d883391f552604e46b6Dave Hawkey        if (customScreen == null) {
628a888158880fea53479447d883391f552604e46b6Dave Hawkey            throw new RuntimeException("Expected custom content screen to exist");
629a888158880fea53479447d883391f552604e46b6Dave Hawkey        }
630a888158880fea53479447d883391f552604e46b6Dave Hawkey
631a888158880fea53479447d883391f552604e46b6Dave Hawkey        mWorkspaceScreens.remove(CUSTOM_CONTENT_SCREEN_ID);
632a888158880fea53479447d883391f552604e46b6Dave Hawkey        mScreenOrder.remove(CUSTOM_CONTENT_SCREEN_ID);
633a888158880fea53479447d883391f552604e46b6Dave Hawkey        removeView(customScreen);
634bb6fda6ac3c4246b91cf66b28cdbc89c3d8a3e52Adam Cohen
635bb6fda6ac3c4246b91cf66b28cdbc89c3d8a3e52Adam Cohen        if (mCustomContentCallbacks != null) {
636bb6fda6ac3c4246b91cf66b28cdbc89c3d8a3e52Adam Cohen            mCustomContentCallbacks.onScrollProgressChanged(0);
637bb6fda6ac3c4246b91cf66b28cdbc89c3d8a3e52Adam Cohen            mCustomContentCallbacks.onHide();
638bb6fda6ac3c4246b91cf66b28cdbc89c3d8a3e52Adam Cohen        }
639bb6fda6ac3c4246b91cf66b28cdbc89c3d8a3e52Adam Cohen
640a888158880fea53479447d883391f552604e46b6Dave Hawkey        mCustomContentCallbacks = null;
641a888158880fea53479447d883391f552604e46b6Dave Hawkey
64282e5c98446e1f1765aabca1725cba181a56edcb4Winson Chung        // Update the custom content hint
64321cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen        if (mRestorePage != INVALID_RESTORE_PAGE) {
64421cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen            mRestorePage = mRestorePage - 1;
64521cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen        } else {
64621cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen            setCurrentPage(getCurrentPage() - 1);
64721cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen        }
648a888158880fea53479447d883391f552604e46b6Dave Hawkey    }
649a888158880fea53479447d883391f552604e46b6Dave Hawkey
65053805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen    public void addToCustomContentPage(View customContent, CustomContentCallbacks callbacks,
65153805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen            String description) {
65298ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung        if (getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID) < 0) {
65398ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung            throw new RuntimeException("Expected custom content screen to exist");
65498ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung        }
65598ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung
65698ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung        // Add the custom content to the full screen custom page
65798ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung        CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID);
65898ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung        int spanX = customScreen.getCountX();
65998ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung        int spanY = customScreen.getCountY();
66098ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung        CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, spanX, spanY);
66198ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung        lp.canReorder  = false;
66298ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung        lp.isFullscreen = true;
66377e1f47520958edd7ec04e203db65b932537bd1cJohn Spurlock        if (customContent instanceof Insettable) {
66477e1f47520958edd7ec04e203db65b932537bd1cJohn Spurlock            ((Insettable)customContent).setInsets(mInsets);
66577e1f47520958edd7ec04e203db65b932537bd1cJohn Spurlock        }
666166ebd4da5d85195bd1f562572f468726ade548fAdam Cohen
667166ebd4da5d85195bd1f562572f468726ade548fAdam Cohen        // Verify that the child is removed from any existing parent.
668166ebd4da5d85195bd1f562572f468726ade548fAdam Cohen        if (customContent.getParent() instanceof ViewGroup) {
669166ebd4da5d85195bd1f562572f468726ade548fAdam Cohen            ViewGroup parent = (ViewGroup) customContent.getParent();
670166ebd4da5d85195bd1f562572f468726ade548fAdam Cohen            parent.removeView(customContent);
671166ebd4da5d85195bd1f562572f468726ade548fAdam Cohen        }
672225ad9b7ff206b0beb1b448de38fa1b5679f2ed0Adam Cohen        customScreen.removeAllViews();
6730fa5ada2261a29ad9be7195c8cdb8cb50bdd0742Tony Wickham        customContent.setFocusable(true);
6740fa5ada2261a29ad9be7195c8cdb8cb50bdd0742Tony Wickham        customContent.setOnKeyListener(new FullscreenKeyEventListener());
67562b9b6054e46074f57ccf156a8663b4392cd82d9Tony Wickham        customContent.setOnFocusChangeListener(mLauncher.mFocusHandler
67662b9b6054e46074f57ccf156a8663b4392cd82d9Tony Wickham                .getHideIndicatorOnFocusListener());
67798ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung        customScreen.addViewToCellLayout(customContent, 0, 0, lp, true);
67853805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen        mCustomContentDescription = description;
67998ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung
68098ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung        mCustomContentCallbacks = callbacks;
68198ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung    }
68298ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung
6835084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen    public void addExtraEmptyScreenOnDrag() {
6845084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        boolean lastChildOnScreen = false;
6855084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        boolean childOnFinalScreen = false;
6865084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen
687ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        // Cancel any pending removal of empty screen
688ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        mRemoveEmptyScreenRunnable = null;
689ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
6905084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        if (mDragSourceInternal != null) {
6915084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            if (mDragSourceInternal.getChildCount() == 1) {
6925084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen                lastChildOnScreen = true;
6935084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            }
6945084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            CellLayout cl = (CellLayout) mDragSourceInternal.getParent();
6955084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            if (indexOfChild(cl) == getChildCount() - 1) {
6965084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen                childOnFinalScreen = true;
6975084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            }
6985084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        }
6995084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen
7005084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        // If this is the last item on the final screen
7015084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        if (lastChildOnScreen && childOnFinalScreen) {
7025084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            return;
7035084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        }
7045084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) {
7055084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID);
7065084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        }
7075084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen    }
7085084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen
7095084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen    public boolean addExtraEmptyScreen() {
7105084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) {
7115084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID);
7125084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            return true;
7135084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        }
7145084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        return false;
7155084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen    }
7165084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen
717ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen    private void convertFinalScreenToEmptyScreenIfNecessary() {
718dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen        if (mLauncher.isWorkspaceLoading()) {
719dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen            // Invalid and dangerous operation if workspace is loading
720dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen            return;
721dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen        }
722dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen
723ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        if (hasExtraEmptyScreen() || mScreenOrder.size() == 0) return;
724ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        long finalScreenId = mScreenOrder.get(mScreenOrder.size() - 1);
725ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
726ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        if (finalScreenId == CUSTOM_CONTENT_SCREEN_ID) return;
727ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        CellLayout finalScreen = mWorkspaceScreens.get(finalScreenId);
728ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
729ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        // If the final screen is empty, convert it to the extra empty screen
730917e38851cbc6caffe1f8dade4db452bace522baAdam Cohen        if (finalScreen.getShortcutsAndWidgets().getChildCount() == 0 &&
731917e38851cbc6caffe1f8dade4db452bace522baAdam Cohen                !finalScreen.isDropPending()) {
732ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            mWorkspaceScreens.remove(finalScreenId);
733ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            mScreenOrder.remove(finalScreenId);
734ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
735ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            // if this is the last non-custom content screen, convert it to the empty screen
736ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, finalScreen);
737ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID);
738a90303bb6fdad59e4a55855edba68c9902192599Winson Chung
739f0716b7a7475fc80a97fd6f95efecab570d186fbWinson Chung            // Update the model if we have changed any screens
740f0716b7a7475fc80a97fd6f95efecab570d186fbWinson Chung            mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
741ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        }
742ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen    }
743ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
744689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen    public void removeExtraEmptyScreen(final boolean animate, boolean stripEmptyScreens) {
745689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen        removeExtraEmptyScreenDelayed(animate, null, 0, stripEmptyScreens);
746ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen    }
747ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
748689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen    public void removeExtraEmptyScreenDelayed(final boolean animate, final Runnable onComplete,
749ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            final int delay, final boolean stripEmptyScreens) {
750dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen        if (mLauncher.isWorkspaceLoading()) {
751dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen            // Don't strip empty screens if the workspace is still loading
752dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen            return;
753dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen        }
754dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen
755ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        if (delay > 0) {
756ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            postDelayed(new Runnable() {
757ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                @Override
758ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                public void run() {
759689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen                    removeExtraEmptyScreenDelayed(animate, onComplete, 0, stripEmptyScreens);
760ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                }
761ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            }, delay);
762ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            return;
763ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        }
764ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
765ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        convertFinalScreenToEmptyScreenIfNecessary();
7662f81731e0986e21bd8407a8793112433795a2d2eMichael Jurka        if (hasExtraEmptyScreen()) {
767ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            int emptyIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID);
768ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            if (getNextPage() == emptyIndex) {
769ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                snapToPage(getNextPage() - 1, SNAP_OFF_EMPTY_SCREEN_DURATION);
770ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                fadeAndRemoveEmptyScreen(SNAP_OFF_EMPTY_SCREEN_DURATION, FADE_EMPTY_SCREEN_DURATION,
771ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                        onComplete, stripEmptyScreens);
772ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            } else {
7732b7bf87bba2a17374958b73ef07397282d7e655eSunny Goyal                snapToPage(getNextPage(), 0);
774ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                fadeAndRemoveEmptyScreen(0, FADE_EMPTY_SCREEN_DURATION,
775ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                        onComplete, stripEmptyScreens);
776ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            }
777ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            return;
7783a14eeb54eb6f901629b26bf9e2ccf2d5bc564ddAdam Cohen        } else if (stripEmptyScreens) {
7793a14eeb54eb6f901629b26bf9e2ccf2d5bc564ddAdam Cohen            // If we're not going to strip the empty screens after removing
7803a14eeb54eb6f901629b26bf9e2ccf2d5bc564ddAdam Cohen            // the extra empty screen, do it right away.
7813a14eeb54eb6f901629b26bf9e2ccf2d5bc564ddAdam Cohen            stripEmptyScreens();
7825084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        }
7833a14eeb54eb6f901629b26bf9e2ccf2d5bc564ddAdam Cohen
784ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        if (onComplete != null) {
785ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            onComplete.run();
786ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        }
787ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen    }
788ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
789ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen    private void fadeAndRemoveEmptyScreen(int delay, int duration, final Runnable onComplete,
790ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            final boolean stripEmptyScreens) {
791a90303bb6fdad59e4a55855edba68c9902192599Winson Chung        // XXX: Do we need to update LM workspace screens below?
792ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
793ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        PropertyValuesHolder bgAlpha = PropertyValuesHolder.ofFloat("backgroundAlpha", 0f);
794ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
795ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        final CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID);
796ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
797ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        mRemoveEmptyScreenRunnable = new Runnable() {
798ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            @Override
799ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            public void run() {
800ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                if (hasExtraEmptyScreen()) {
801ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                    mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID);
802ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                    mScreenOrder.remove(EXTRA_EMPTY_SCREEN_ID);
803ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                    removeView(cl);
804ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                    if (stripEmptyScreens) {
805ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                        stripEmptyScreens();
806ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                    }
807d4b28d2e1bb73fcdb1e46bb06e1666e27af2e643Tony Wickham                    // Update the page indicator to reflect the removed page.
808d4b28d2e1bb73fcdb1e46bb06e1666e27af2e643Tony Wickham                    showPageIndicatorAtCurrentScroll();
809ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                }
810ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            }
811ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        };
812ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
813ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(cl, alpha, bgAlpha);
814ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        oa.setDuration(duration);
815ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        oa.setStartDelay(delay);
816ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        oa.addListener(new AnimatorListenerAdapter() {
817ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            @Override
818ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            public void onAnimationEnd(Animator animation) {
819ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                if (mRemoveEmptyScreenRunnable != null) {
820ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                    mRemoveEmptyScreenRunnable.run();
821ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                }
822ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                if (onComplete != null) {
823ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                    onComplete.run();
824ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                }
825ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            }
826ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        });
827ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        oa.start();
8285084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen    }
8295084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen
8302f81731e0986e21bd8407a8793112433795a2d2eMichael Jurka    public boolean hasExtraEmptyScreen() {
8312f81731e0986e21bd8407a8793112433795a2d2eMichael Jurka        int nScreens = getChildCount();
832fe0ace3583bc57adbaaec730b1b1757032e90c41Michael Jurka        nScreens = nScreens - numCustomPages();
8332f81731e0986e21bd8407a8793112433795a2d2eMichael Jurka        return mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID) && nScreens > 1;
8342f81731e0986e21bd8407a8793112433795a2d2eMichael Jurka    }
8352f81731e0986e21bd8407a8793112433795a2d2eMichael Jurka
836dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    public long commitExtraEmptyScreen() {
837dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen        if (mLauncher.isWorkspaceLoading()) {
838dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen            // Invalid and dangerous operation if workspace is loading
839dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen            return -1;
840dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen        }
841a90303bb6fdad59e4a55855edba68c9902192599Winson Chung
84289f9705077c054b541af7da52be832760e2ae2e8Winson Chung        int index = getPageIndexForScreenId(EXTRA_EMPTY_SCREEN_ID);
843dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID);
844c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID);
845dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mScreenOrder.remove(EXTRA_EMPTY_SCREEN_ID);
846dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
847d249748abf357925b326d57ab68eb6c2b23c4ef6Sunny Goyal        long newId = LauncherSettings.Settings.call(getContext().getContentResolver(),
848d249748abf357925b326d57ab68eb6c2b23c4ef6Sunny Goyal                LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
849d249748abf357925b326d57ab68eb6c2b23c4ef6Sunny Goyal                .getLong(LauncherSettings.Settings.EXTRA_VALUE);
850dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mWorkspaceScreens.put(newId, cl);
851dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mScreenOrder.add(newId);
852dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
85364359a53dc827797917a8d9a0697a91996789801Winson Chung        // Update the model for the new screen
85464359a53dc827797917a8d9a0697a91996789801Winson Chung        mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
85564359a53dc827797917a8d9a0697a91996789801Winson Chung
856dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        return newId;
857dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
858dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
859dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    public CellLayout getScreenWithId(long screenId) {
8609eba1fd75e9fa6b0dc5cad9a4e817b3b167d2461Sunny Goyal        return mWorkspaceScreens.get(screenId);
861dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
862dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
863dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    public long getIdForScreen(CellLayout layout) {
864e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        int index = mWorkspaceScreens.indexOfValue(layout);
865e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        if (index != -1) {
866e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal            return mWorkspaceScreens.keyAt(index);
867dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
868dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        return -1;
869dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
870dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
871dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    public int getPageIndexForScreenId(long screenId) {
872dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        return indexOfChild(mWorkspaceScreens.get(screenId));
873dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
874dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
875dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    public long getScreenIdForPageIndex(int index) {
8765f8afe6280eae34620067696173e71943e1a30a3Winson Chung        if (0 <= index && index < mScreenOrder.size()) {
8775f8afe6280eae34620067696173e71943e1a30a3Winson Chung            return mScreenOrder.get(index);
8785f8afe6280eae34620067696173e71943e1a30a3Winson Chung        }
8795f8afe6280eae34620067696173e71943e1a30a3Winson Chung        return -1;
880dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
881dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
88283a8f042adda926489494dff217c15ab696139b4Sunny Goyal    public ArrayList<Long> getScreenOrder() {
883c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        return mScreenOrder;
884c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    }
885c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
886dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    public void stripEmptyScreens() {
88765e43032c8962765a3e89826d4312042a03ee3beAdam Cohen        if (mLauncher.isWorkspaceLoading()) {
888dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen            // Don't strip empty screens if the workspace is still loading.
889dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen            // This is dangerous and can result in data loss.
890517a7f5fd03fb89bd6b465013b278c9f281d0b26Adam Cohen            return;
891517a7f5fd03fb89bd6b465013b278c9f281d0b26Adam Cohen        }
892517a7f5fd03fb89bd6b465013b278c9f281d0b26Adam Cohen
893accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen        if (isPageMoving()) {
894accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen            mStripScreensOnPageStopMoving = true;
895accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen            return;
896accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen        }
897accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen
898accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen        int currentPage = getNextPage();
899dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        ArrayList<Long> removeScreens = new ArrayList<Long>();
900e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        int total = mWorkspaceScreens.size();
901e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        for (int i = 0; i < total; i++) {
902e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal            long id = mWorkspaceScreens.keyAt(i);
903e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal            CellLayout cl = mWorkspaceScreens.valueAt(i);
904da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal            // FIRST_SCREEN_ID can never be removed.
905da4fe1a6244457f144e0a331cada3ada17157809Sunny Goyal            if (id > FIRST_SCREEN_ID && cl.getShortcutsAndWidgets().getChildCount() == 0) {
906dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                removeScreens.add(id);
907dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
908dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
909dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
910de1e67c38856ab1253426bfb4f892895eeb5f6d9Vadim Tryshev        LauncherAccessibilityDelegate delegate =
911de1e67c38856ab1253426bfb4f892895eeb5f6d9Vadim Tryshev                LauncherAppState.getInstance().getAccessibilityDelegate();
912de1e67c38856ab1253426bfb4f892895eeb5f6d9Vadim Tryshev
9135084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        // We enforce at least one page to add new items to. In the case that we remove the last
9145084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        // such screen, we convert the last screen to the empty screen
915fe0ace3583bc57adbaaec730b1b1757032e90c41Michael Jurka        int minScreens = 1 + numCustomPages();
9165084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen
917dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        int pageShift = 0;
918dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        for (Long id: removeScreens) {
919dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            CellLayout cl = mWorkspaceScreens.get(id);
920dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            mWorkspaceScreens.remove(id);
921dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            mScreenOrder.remove(id);
9225084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen
9235084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            if (getChildCount() > minScreens) {
9245084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen                if (indexOfChild(cl) < currentPage) {
9255084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen                    pageShift++;
9265084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen                }
927de1e67c38856ab1253426bfb4f892895eeb5f6d9Vadim Tryshev
928de1e67c38856ab1253426bfb4f892895eeb5f6d9Vadim Tryshev                if (delegate != null && delegate.isInAccessibleDrag()) {
929de1e67c38856ab1253426bfb4f892895eeb5f6d9Vadim Tryshev                    cl.enableAccessibleDrag(false, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG);
930de1e67c38856ab1253426bfb4f892895eeb5f6d9Vadim Tryshev                }
931de1e67c38856ab1253426bfb4f892895eeb5f6d9Vadim Tryshev
9325084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen                removeView(cl);
9335084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            } else {
9345084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen                // if this is the last non-custom content screen, convert it to the empty screen
935ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                mRemoveEmptyScreenRunnable = null;
9365084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen                mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, cl);
9375084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen                mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID);
938dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            }
939dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
94064359a53dc827797917a8d9a0697a91996789801Winson Chung
94164359a53dc827797917a8d9a0697a91996789801Winson Chung        if (!removeScreens.isEmpty()) {
94264359a53dc827797917a8d9a0697a91996789801Winson Chung            // Update the model if we have changed any screens
94364359a53dc827797917a8d9a0697a91996789801Winson Chung            mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
94464359a53dc827797917a8d9a0697a91996789801Winson Chung        }
945accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen
946accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen        if (pageShift >= 0) {
947accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen            setCurrentPage(currentPage - pageShift);
948accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen        }
949dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
950dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
951dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    // See implementation for parameter definition.
952dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    void addInScreen(View child, long container, long screenId,
953dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            int x, int y, int spanX, int spanY) {
954dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        addInScreen(child, container, screenId, x, y, spanX, spanY, false, false);
955dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
956dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
957dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    // At bind time, we use the rank (screenId) to compute x and y for hotseat items.
958dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    // See implementation for parameter definition.
959f9c184a619e4e4b82cf9e0bf318ca6d8deaaaee7Adam Cohen    public void addInScreenFromBind(View child, long container, long screenId, int x, int y,
960dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            int spanX, int spanY) {
961dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        addInScreen(child, container, screenId, x, y, spanX, spanY, false, true);
962dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
963dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
964dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    // See implementation for parameter definition.
965dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    void addInScreen(View child, long container, long screenId, int x, int y, int spanX, int spanY,
966dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            boolean insert) {
967dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        addInScreen(child, container, screenId, x, y, spanX, spanY, insert, false);
968aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung    }
969aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
97031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
97131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Adds the specified child in the specified screen. The position and dimension of
97231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * the child are defined by x, y, spanX and spanY.
97331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
97431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param child The child to add in one of the workspace's screens.
975dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     * @param screenId The screen in which to add the child.
97631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param x The X position of the child in the screen's grid.
97731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param y The Y position of the child in the screen's grid.
97831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param spanX The number of cells spanned horizontally by the child.
97931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param spanY The number of cells spanned vertically by the child.
98031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param insert When true, the child is inserted at the beginning of the children list.
981dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     * @param computeXYFromRank When true, we use the rank (stored in screenId) to compute
982dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     *                          the x and y position in which to place hotseat items. Otherwise
983dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     *                          we use the x and y position to compute the rank.
98431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
985dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    void addInScreen(View child, long container, long screenId, int x, int y, int spanX, int spanY,
986dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            boolean insert, boolean computeXYFromRank) {
9873d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
988dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            if (getScreenWithId(screenId) == null) {
989dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                Log.e(TAG, "Skipping child, screenId " + screenId + " not found");
9908481e32e5ba318d11c31317072a8a23aacd3d0b9Winson Chung                // DEBUGGING - Print out the stack trace to see where we are adding from
9918481e32e5ba318d11c31317072a8a23aacd3d0b9Winson Chung                new Throwable().printStackTrace();
9923d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                return;
9933d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
9943d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
995dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (screenId == EXTRA_EMPTY_SCREEN_ID) {
996dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen            // This should never happen
997dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen            throw new RuntimeException("Screen id should not be EXTRA_EMPTY_SCREEN_ID");
998dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
999dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
10003d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        final CellLayout layout;
10013d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
10023d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            layout = mLauncher.getHotseat().getLayout();
1003b3726d90a567e992e4d0f43e7c7acdd8739ae805Sunny Goyal            child.setOnKeyListener(new HotseatIconKeyEventListener());
10043d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
1005099f60d51f6310e1290687e83958e665b3324504Adam Cohen            // Hide folder title in the hotseat
1006099f60d51f6310e1290687e83958e665b3324504Adam Cohen            if (child instanceof FolderIcon) {
1007099f60d51f6310e1290687e83958e665b3324504Adam Cohen                ((FolderIcon) child).setTextVisible(false);
1008099f60d51f6310e1290687e83958e665b3324504Adam Cohen            }
1009099f60d51f6310e1290687e83958e665b3324504Adam Cohen
1010dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            if (computeXYFromRank) {
1011dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                x = mLauncher.getHotseat().getCellXFromOrder((int) screenId);
1012dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                y = mLauncher.getHotseat().getCellYFromOrder((int) screenId);
10133d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            } else {
1014dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                screenId = mLauncher.getHotseat().getOrderInHotseat(x, y);
10153d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
10163d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        } else {
1017099f60d51f6310e1290687e83958e665b3324504Adam Cohen            // Show folder title if not in the hotseat
1018099f60d51f6310e1290687e83958e665b3324504Adam Cohen            if (child instanceof FolderIcon) {
1019099f60d51f6310e1290687e83958e665b3324504Adam Cohen                ((FolderIcon) child).setTextVisible(true);
1020099f60d51f6310e1290687e83958e665b3324504Adam Cohen            }
1021dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            layout = getScreenWithId(screenId);
1022ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            child.setOnKeyListener(new IconKeyEventListener());
102331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
102431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
102596d30a165185dc59617e181314f4d22634e22952Adam Cohen        ViewGroup.LayoutParams genericLp = child.getLayoutParams();
1026ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        CellLayout.LayoutParams lp;
1027ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        if (genericLp == null || !(genericLp instanceof CellLayout.LayoutParams)) {
102831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp = new CellLayout.LayoutParams(x, y, spanX, spanY);
102931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } else {
1030ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            lp = (CellLayout.LayoutParams) genericLp;
103131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp.cellX = x;
103231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp.cellY = y;
103331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp.cellHSpan = spanX;
103431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            lp.cellVSpan = spanY;
103531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1036aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
10377f4eabe3709a72b416569136e4a095431c493c8bAdam Cohen        if (spanX < 0 && spanY < 0) {
10387f4eabe3709a72b416569136e4a095431c493c8bAdam Cohen            lp.isLockedToGrid = false;
10397f4eabe3709a72b416569136e4a095431c493c8bAdam Cohen        }
10407f4eabe3709a72b416569136e4a095431c493c8bAdam Cohen
10416569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy        // Get the canonical child id to uniquely represent this view in this screen
1042c76e1dda0875c13131462d80d0c1721a6149e08dAdam Cohen        ItemInfo info = (ItemInfo) child.getTag();
1043c76e1dda0875c13131462d80d0c1721a6149e08dAdam Cohen        int childId = mLauncher.getViewIdForItem(info);
1044c76e1dda0875c13131462d80d0c1721a6149e08dAdam Cohen
1045f3ca3ab6958b104cbf2c2fa04add97d372a94d1cMichael Jurka        boolean markCellsAsOccupied = !(child instanceof Folder);
10463d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (!layout.addViewToCellLayout(child, insert ? 0 : -1, childId, lp, markCellsAsOccupied)) {
1047aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            // TODO: This branch occurs when the workspace is adding views
1048aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            // outside of the defined grid
10496569f2c80e179c2f8ed73dae6b01d971ec20f005Patrick Dubroy            // maybe we should be deleting these items from the LauncherModel?
1050a13654529cb557abd946da4d4637593565917ecdSunny Goyal            Log.e(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout");
1051aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung        }
1052aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
105331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!(child instanceof Folder)) {
10540d44e9482b95d8f163b28bf20131c4349185b589Joe Onorato            child.setHapticFeedbackEnabled(false);
105531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            child.setOnLongClickListener(mLongClickListener);
105631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
105700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        if (child instanceof DropTarget) {
1058aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung            mDragController.addDropTarget((DropTarget) child);
105900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        }
106031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
106131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1062d0ce1ec2e21ce3bb0ba3549a01d1d06b440a8e45Patrick Dubroy    /**
1063e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy     * Called directly from a CellLayout (not by the framework), after we've been added as a
1064e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy     * listener via setOnInterceptTouchEventListener(). This allows us to tell the CellLayout
1065e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy     * that it should intercept touch events, which is not something that is normally supported.
1066e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy     */
10677066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal    @SuppressLint("ClickableViewAccessibility")
1068e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy    @Override
1069dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka    public boolean onTouch(View v, MotionEvent event) {
10706c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        return (workspaceInModalState() || !isFinishedSwitchingState())
10716c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen                || (!workspaceInModalState() && indexOfChild(v) != mCurrentPage);
1072e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy    }
1073e708c5249b26978057acb9370cbfb8f6f747f5b8Patrick Dubroy
1074fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen    public boolean isSwitchingState() {
1075fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen        return mIsSwitchingState;
1076fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen    }
1077fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen
10787044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung    /** This differs from isSwitchingState in that we take into account how far the transition
10797044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung     *  has completed. */
10809b0b2fefb2de9aca457ab1c38ffd08d7667f4065Winson Chung    public boolean isFinishedSwitchingState() {
10817044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        return !mIsSwitchingState || (mTransitionProgress > 0.5f);
10827044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung    }
10837044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung
1084ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    protected void onWindowVisibilityChanged (int visibility) {
1085ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mLauncher.onWindowVisibilityChanged(visibility);
1086ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1087ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
10885f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    @Override
10895f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    public boolean dispatchUnhandledMove(View focused, int direction) {
10906c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        if (workspaceInModalState() || !isFinishedSwitchingState()) {
10915f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            // when the home screens are shrunken, shouldn't allow side-scrolling
10925f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka            return false;
10935f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        }
10945f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        return super.dispatchUnhandledMove(focused, direction);
10955f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    }
10965f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka
10975f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    @Override
10985f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    public boolean onInterceptTouchEvent(MotionEvent ev) {
1099d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka        switch (ev.getAction() & MotionEvent.ACTION_MASK) {
1100d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka        case MotionEvent.ACTION_DOWN:
1101f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen            mXDown = ev.getX();
1102f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen            mYDown = ev.getY();
11033d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen            mTouchDownTime = System.currentTimeMillis();
1104d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka            break;
1105d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka        case MotionEvent.ACTION_POINTER_UP:
1106d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka        case MotionEvent.ACTION_UP:
1107d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka            if (mTouchState == TOUCH_STATE_REST) {
1108d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka                final CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage);
11092805e639cdea6ae0051155611d122ed27556e658Sunny Goyal                if (currentPage != null) {
1110d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka                    onWallpaperTap(ev);
1111d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka                }
1112d771c96e5d156ffde5d35ee13ce053de60dc3163Michael Jurka            }
1113f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen        }
11145f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka        return super.onInterceptTouchEvent(ev);
11155f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka    }
11165f1c509d5ad1954a7e38e77db4d5f27c7345fd39Michael Jurka
1117d3fbe683bb52c8be9a14783bb70f32a46cf6f6dbJan-Willem Maarse    @Override
1118d3fbe683bb52c8be9a14783bb70f32a46cf6f6dbJan-Willem Maarse    public boolean onGenericMotionEvent(MotionEvent event) {
1119d3fbe683bb52c8be9a14783bb70f32a46cf6f6dbJan-Willem Maarse        // Ignore pointer scroll events if the custom content doesn't allow scrolling.
1120d3fbe683bb52c8be9a14783bb70f32a46cf6f6dbJan-Willem Maarse        if ((getScreenIdForPageIndex(getCurrentPage()) == CUSTOM_CONTENT_SCREEN_ID)
1121d3fbe683bb52c8be9a14783bb70f32a46cf6f6dbJan-Willem Maarse                && (mCustomContentCallbacks != null)
1122d3fbe683bb52c8be9a14783bb70f32a46cf6f6dbJan-Willem Maarse                && !mCustomContentCallbacks.isScrollingAllowed()) {
1123d3fbe683bb52c8be9a14783bb70f32a46cf6f6dbJan-Willem Maarse            return false;
1124d3fbe683bb52c8be9a14783bb70f32a46cf6f6dbJan-Willem Maarse        }
1125d3fbe683bb52c8be9a14783bb70f32a46cf6f6dbJan-Willem Maarse        return super.onGenericMotionEvent(event);
1126d3fbe683bb52c8be9a14783bb70f32a46cf6f6dbJan-Willem Maarse    }
1127d3fbe683bb52c8be9a14783bb70f32a46cf6f6dbJan-Willem Maarse
11283d50932a93dd367537db3548bde29d1380b371c0Adam Cohen    protected void reinflateWidgetsIfNecessary() {
11293d50932a93dd367537db3548bde29d1380b371c0Adam Cohen        final int clCount = getChildCount();
11303d50932a93dd367537db3548bde29d1380b371c0Adam Cohen        for (int i = 0; i < clCount; i++) {
11313d50932a93dd367537db3548bde29d1380b371c0Adam Cohen            CellLayout cl = (CellLayout) getChildAt(i);
11323d50932a93dd367537db3548bde29d1380b371c0Adam Cohen            ShortcutAndWidgetContainer swc = cl.getShortcutsAndWidgets();
11333d50932a93dd367537db3548bde29d1380b371c0Adam Cohen            final int itemCount = swc.getChildCount();
11343d50932a93dd367537db3548bde29d1380b371c0Adam Cohen            for (int j = 0; j < itemCount; j++) {
11353d50932a93dd367537db3548bde29d1380b371c0Adam Cohen                View v = swc.getChildAt(j);
11363d50932a93dd367537db3548bde29d1380b371c0Adam Cohen
113787af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                if (v instanceof LauncherAppWidgetHostView
113887af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                        && v.getTag() instanceof LauncherAppWidgetInfo) {
11393d50932a93dd367537db3548bde29d1380b371c0Adam Cohen                    LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
114087af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                    LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) v;
114187af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                    if (lahv.isReinflateRequired()) {
1142c0b52fefbc11531d560359c093484099f9d1298eWinson                        // Remove and rebind the current widget (which was inflated in the wrong
1143c0b52fefbc11531d560359c093484099f9d1298eWinson                        // orientation), but don't delete it from the database
11442949fb5b16a07259e221c5c04470e90c8afb4ea8Winson                        mLauncher.removeItem(lahv, info, false  /* deleteFromDb */);
11453d50932a93dd367537db3548bde29d1380b371c0Adam Cohen                        mLauncher.bindAppWidget(info);
11463d50932a93dd367537db3548bde29d1380b371c0Adam Cohen                    }
11473d50932a93dd367537db3548bde29d1380b371c0Adam Cohen                }
11483d50932a93dd367537db3548bde29d1380b371c0Adam Cohen            }
11493d50932a93dd367537db3548bde29d1380b371c0Adam Cohen        }
11503d50932a93dd367537db3548bde29d1380b371c0Adam Cohen    }
11513d50932a93dd367537db3548bde29d1380b371c0Adam Cohen
11521adf5391a3a3d215b226adf3702019c22a99e3b1Michael Jurka    @Override
11531adf5391a3a3d215b226adf3702019c22a99e3b1Michael Jurka    protected void determineScrollingStart(MotionEvent ev) {
11547044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        if (!isFinishedSwitchingState()) return;
1155f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen
11563d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen        float deltaX = ev.getX() - mXDown;
11573d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen        float absDeltaX = Math.abs(deltaX);
11583d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen        float absDeltaY = Math.abs(ev.getY() - mYDown);
1159f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen
11603d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen        if (Float.compare(absDeltaX, 0f) == 0) return;
1161f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen
11623d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen        float slope = absDeltaY / absDeltaX;
11637044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        float theta = (float) Math.atan(slope);
1164f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen
11653d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen        if (absDeltaX > mTouchSlop || absDeltaY > mTouchSlop) {
11667044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            cancelCurrentPageLongPress();
11677044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        }
11687044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung
11693d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen        boolean passRightSwipesToCustomContent =
11703d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen                (mTouchDownTime - mCustomContentShowTime) > CUSTOM_CONTENT_GESTURE_DELAY;
11713d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen
11727066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal        boolean swipeInIgnoreDirection = mIsRtl ? deltaX < 0 : deltaX > 0;
11732ff91c46fa6f1b84fb08b6f31499934d30d8a91dJan-Willem Maarse        boolean onCustomContentScreen =
11742ff91c46fa6f1b84fb08b6f31499934d30d8a91dJan-Willem Maarse                getScreenIdForPageIndex(getCurrentPage()) == CUSTOM_CONTENT_SCREEN_ID;
11752ff91c46fa6f1b84fb08b6f31499934d30d8a91dJan-Willem Maarse        if (swipeInIgnoreDirection && onCustomContentScreen && passRightSwipesToCustomContent) {
11763d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen            // Pass swipes to the right to the custom content page.
11773d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen            return;
11783d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen        }
11793d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen
11802ff91c46fa6f1b84fb08b6f31499934d30d8a91dJan-Willem Maarse        if (onCustomContentScreen && (mCustomContentCallbacks != null)
11812ff91c46fa6f1b84fb08b6f31499934d30d8a91dJan-Willem Maarse                && !mCustomContentCallbacks.isScrollingAllowed()) {
11822ff91c46fa6f1b84fb08b6f31499934d30d8a91dJan-Willem Maarse            // Don't allow workspace scrolling if the current custom content screen doesn't allow
11832ff91c46fa6f1b84fb08b6f31499934d30d8a91dJan-Willem Maarse            // scrolling.
11842ff91c46fa6f1b84fb08b6f31499934d30d8a91dJan-Willem Maarse            return;
11852ff91c46fa6f1b84fb08b6f31499934d30d8a91dJan-Willem Maarse        }
11862ff91c46fa6f1b84fb08b6f31499934d30d8a91dJan-Willem Maarse
11877044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        if (theta > MAX_SWIPE_ANGLE) {
11887044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace
11897044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            return;
11907044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) {
11917044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to
11927044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            // increase the touch slop to make it harder to begin scrolling the workspace. This
11937044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            // results in vertically scrolling widgets to more easily. The higher the angle, the
11947044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            // more we increase touch slop.
11957044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            theta -= START_DAMPING_TOUCH_SLOP_ANGLE;
11967044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            float extraRatio = (float)
11977044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE)));
11987044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio);
11997044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        } else {
12007044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special
12017044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            super.determineScrollingStart(ev);
1202f8d2823d885ba682140aee1ae0504c1c5e67a24bAdam Cohen        }
12031adf5391a3a3d215b226adf3702019c22a99e3b1Michael Jurka    }
12041adf5391a3a3d215b226adf3702019c22a99e3b1Michael Jurka
12051262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    protected void onPageBeginMoving() {
1206007c69867d821ea2b271398577a8b3440b3a7046Winson Chung        super.onPageBeginMoving();
1207007c69867d821ea2b271398577a8b3440b3a7046Winson Chung
1208d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (isHardwareAccelerated()) {
12093a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka            updateChildrenLayersEnabled(false);
12100142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        } else {
1211d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            if (mNextPage != INVALID_PAGE) {
1212d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                // we're snapping to a particular screen
1213d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                enableChildrenCache(mCurrentPage, mNextPage);
1214d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            } else {
1215d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                // this is when user is actively dragging a particular screen, they might
1216d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                // swipe it either left or right (but we won't advance by more than one screen)
1217d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1);
1218d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            }
121931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
122031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
122131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
12221262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    protected void onPageEndMoving() {
1223007c69867d821ea2b271398577a8b3440b3a7046Winson Chung        super.onPageEndMoving();
1224007c69867d821ea2b271398577a8b3440b3a7046Winson Chung
1225d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (isHardwareAccelerated()) {
12263a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka            updateChildrenLayersEnabled(false);
1227d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        } else {
1228d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            clearChildrenCache();
1229d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        }
1230d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
12313bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung        if (mDragController.isDragging()) {
12326c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen            if (workspaceInModalState()) {
12333bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung                // If we are in springloaded mode, then force an event to check if the current touch
12343bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung                // is under a new page (to scroll to)
123525460a1d5cb473033408c9e76898bb77e785c66fWinson Chung                mDragController.forceTouchMove();
12363bc21c35fa167ac233e19df0c9a01b0a07173ed8Winson Chung            }
12371262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy        }
123826976d9b53161f80faf822d482ac771b8621cf31Adam Cohen
1239e03856267544d2209c2844c9c06c74e8e52ff104Tony Wickham        if (mDelayedResizeRunnable != null && !mIsSwitchingState) {
124026976d9b53161f80faf822d482ac771b8621cf31Adam Cohen            mDelayedResizeRunnable.run();
124126976d9b53161f80faf822d482ac771b8621cf31Adam Cohen            mDelayedResizeRunnable = null;
124226976d9b53161f80faf822d482ac771b8621cf31Adam Cohen        }
1243f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
1244f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        if (mDelayedSnapToPageRunnable != null) {
1245f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            mDelayedSnapToPageRunnable.run();
1246f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            mDelayedSnapToPageRunnable = null;
1247f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        }
1248accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen        if (mStripScreensOnPageStopMoving) {
1249accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen            stripEmptyScreens();
1250accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen            mStripScreensOnPageStopMoving = false;
1251accfd5648ccb3a3248b422a8c1971b7de8d3207bAdam Cohen        }
1252c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    }
1253c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen
1254c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    protected void onScrollInteractionBegin() {
1255c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen        super.onScrollInteractionEnd();
1256c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen        mScrollInteractionBegan = true;
1257c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    }
1258c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen
1259c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    protected void onScrollInteractionEnd() {
1260c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen        super.onScrollInteractionEnd();
1261c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen        mScrollInteractionBegan = false;
1262c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen        if (mStartedSendingScrollEvents) {
1263c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen            mStartedSendingScrollEvents = false;
1264c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen            mLauncherOverlay.onScrollInteractionEnd();
1265c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen        }
1266c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    }
1267c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen
1268c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    public void setLauncherOverlay(LauncherOverlay overlay) {
1269c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen        mLauncherOverlay = overlay;
1270c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        // A new overlay has been set. Reset event tracking
1271c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        mStartedSendingScrollEvents = false;
1272c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        onOverlayScrollChanged(0);
1273c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    }
1274c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal
1275c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    @Override
1276c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    protected int getUnboundedScrollX() {
1277061380a04d067d06adf41c72b4a6892827777accSunny Goyal        if (isScrollingOverlay()) {
1278061380a04d067d06adf41c72b4a6892827777accSunny Goyal            return mUnboundedScrollX;
1279c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        }
1280c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal
1281c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        return super.getUnboundedScrollX();
1282c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    }
1283c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal
1284061380a04d067d06adf41c72b4a6892827777accSunny Goyal    private boolean isScrollingOverlay() {
1285061380a04d067d06adf41c72b4a6892827777accSunny Goyal        return mLauncherOverlay != null &&
1286061380a04d067d06adf41c72b4a6892827777accSunny Goyal                ((mIsRtl && mUnboundedScrollX > mMaxScrollX) || (!mIsRtl && mUnboundedScrollX < 0));
1287061380a04d067d06adf41c72b4a6892827777accSunny Goyal    }
1288061380a04d067d06adf41c72b4a6892827777accSunny Goyal
1289061380a04d067d06adf41c72b4a6892827777accSunny Goyal    @Override
1290061380a04d067d06adf41c72b4a6892827777accSunny Goyal    protected void snapToDestination() {
1291061380a04d067d06adf41c72b4a6892827777accSunny Goyal        // If we're overscrolling the overlay, we make sure to immediately reset the PagedView
1292061380a04d067d06adf41c72b4a6892827777accSunny Goyal        // to it's baseline position instead of letting the overscroll settle. The overlay handles
1293061380a04d067d06adf41c72b4a6892827777accSunny Goyal        // it's own settling, and every gesture to the overlay should be self-contained and start
1294061380a04d067d06adf41c72b4a6892827777accSunny Goyal        // from 0, so we zero it out here.
1295061380a04d067d06adf41c72b4a6892827777accSunny Goyal        if (isScrollingOverlay()) {
1296061380a04d067d06adf41c72b4a6892827777accSunny Goyal            int finalScroll = mIsRtl ? mMaxScrollX : 0;
1297061380a04d067d06adf41c72b4a6892827777accSunny Goyal
1298061380a04d067d06adf41c72b4a6892827777accSunny Goyal            // We reset mWasInOverscroll so that PagedView doesn't zero out the overscroll
1299061380a04d067d06adf41c72b4a6892827777accSunny Goyal            // interaction when we call scrollTo.
1300061380a04d067d06adf41c72b4a6892827777accSunny Goyal            mWasInOverscroll = false;
1301061380a04d067d06adf41c72b4a6892827777accSunny Goyal            scrollTo(finalScroll, getScrollY());
1302061380a04d067d06adf41c72b4a6892827777accSunny Goyal        } else {
1303061380a04d067d06adf41c72b4a6892827777accSunny Goyal            super.snapToDestination();
1304061380a04d067d06adf41c72b4a6892827777accSunny Goyal        }
1305061380a04d067d06adf41c72b4a6892827777accSunny Goyal    }
1306061380a04d067d06adf41c72b4a6892827777accSunny Goyal
1307c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    @Override
1308c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    public void scrollTo(int x, int y) {
1309c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        mUnboundedScrollX = x;
1310c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        super.scrollTo(x, y);
1311c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    }
1312c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen
1313c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    @Override
1314f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
1315f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham        super.onScrollChanged(l, t, oldl, oldt);
1316f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham
1317f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham        // Update the page indicator progress.
1318f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham        boolean isTransitioning = mIsSwitchingState
1319f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham                || (getLayoutTransition() != null && getLayoutTransition().isRunning());
1320ddb77003e728cac32de009e709f80280e68f6b6eTony Wickham        if (!isTransitioning) {
1321f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham            showPageIndicatorAtCurrentScroll();
1322f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham        }
1323f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham    }
1324f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham
1325f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham    private void showPageIndicatorAtCurrentScroll() {
1326ddb77003e728cac32de009e709f80280e68f6b6eTony Wickham        if (mPageIndicator != null) {
1327ddb77003e728cac32de009e709f80280e68f6b6eTony Wickham            mPageIndicator.setScroll(getScrollX(), computeMaxScrollX());
1328ddb77003e728cac32de009e709f80280e68f6b6eTony Wickham        }
1329f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham    }
1330f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham
1331f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham    @Override
1332c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen    protected void overScroll(float amount) {
13337066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal        boolean shouldOverScroll = (amount <= 0 && (!hasCustomContent() || mIsRtl)) ||
13347066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal                (amount >= 0 && (!hasCustomContent() || !mIsRtl));
1335c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen
13368c4ca9277dcb92cb965a1e58eef59ad9868ae44eAdam Cohen        boolean shouldScrollOverlay = mLauncherOverlay != null &&
13377066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal                ((amount <= 0 && !mIsRtl) || (amount >= 0 && mIsRtl));
13388c4ca9277dcb92cb965a1e58eef59ad9868ae44eAdam Cohen
13398c4ca9277dcb92cb965a1e58eef59ad9868ae44eAdam Cohen        boolean shouldZeroOverlay = mLauncherOverlay != null && mLastOverlaySroll != 0 &&
13407066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal                ((amount >= 0 && !mIsRtl) || (amount <= 0 && mIsRtl));
1341c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen
1342c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen        if (shouldScrollOverlay) {
1343c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen            if (!mStartedSendingScrollEvents && mScrollInteractionBegan) {
1344c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen                mStartedSendingScrollEvents = true;
1345c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen                mLauncherOverlay.onScrollInteractionBegin();
1346c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen            }
1347c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen
1348c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal            mLastOverlaySroll = Math.abs(amount / getViewportWidth());
1349c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal            mLauncherOverlay.onScrollChange(mLastOverlaySroll, mIsRtl);
1350c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen        } else if (shouldOverScroll) {
1351c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen            dampedOverScroll(amount);
1352c2d6e897dbcb96b7b629e42002966368fbda2f95Adam Cohen        }
13538c4ca9277dcb92cb965a1e58eef59ad9868ae44eAdam Cohen
13548c4ca9277dcb92cb965a1e58eef59ad9868ae44eAdam Cohen        if (shouldZeroOverlay) {
13557066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal            mLauncherOverlay.onScrollChange(0, mIsRtl);
13568c4ca9277dcb92cb965a1e58eef59ad9868ae44eAdam Cohen        }
135789cbbb9d2e6885f176d59d8e7476a4001fe6e7f5Adam Cohen    }
135889cbbb9d2e6885f176d59d8e7476a4001fe6e7f5Adam Cohen
1359c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    private final Interpolator mAlphaInterpolator = new DecelerateInterpolator(3f);
1360c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal
1361c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    /**
1362c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal     * The overlay scroll is being controlled locally, just update our overlay effect
1363c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal     */
1364c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    public void onOverlayScrollChanged(float scroll) {
1365c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        float offset = 0f;
1366c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        float slip = 0f;
1367c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal
1368c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        scroll = Math.max(scroll - offset, 0);
1369c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        scroll = Math.min(1, scroll / (1 - offset));
1370c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal
1371c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        float alpha = 1 - mAlphaInterpolator.getInterpolation(scroll);
1372c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        float transX = mLauncher.getDragLayer().getMeasuredWidth() * scroll;
1373c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        transX *= 1 - slip;
1374c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal
1375c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        if (mIsRtl) {
1376c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal            transX = -transX;
1377c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        }
1378ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal        mOverlayTranslation = transX;
1379c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal
1380c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        // TODO(adamcohen): figure out a final effect here. We may need to recommend
1381c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        // different effects based on device performance. On at least one relatively high-end
1382c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        // device I've tried, translating the launcher causes things to get quite laggy.
1383c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        setTranslationAndAlpha(mLauncher.getSearchDropTargetBar(), transX, alpha);
1384c487bd34ace268dd8be5480ba884baed6096f76eSunny Goyal        setTranslationAndAlpha(getPageIndicator(), transX, alpha);
1385c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        setTranslationAndAlpha(getChildAt(getCurrentPage()), transX, alpha);
1386c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        setTranslationAndAlpha(mLauncher.getHotseat(), transX, alpha);
1387c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal
1388c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        // When the animation finishes, reset all pages, just in case we missed a page.
1389c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        if (transX == 0) {
1390c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal            for (int i = getChildCount() - 1; i >= 0; i--) {
1391c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal                setTranslationAndAlpha(getChildAt(i), 0, alpha);
1392c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal            }
1393c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        }
1394c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    }
1395c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal
1396ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal    @Override
1397ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal    protected Matrix getPageShiftMatrix() {
1398ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal        if (Float.compare(mOverlayTranslation, 0) != 0) {
1399ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal            // The pages are translated by mOverlayTranslation. incorporate that in the
1400ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal            // visible page calculation by shifting everything back by that same amount.
1401ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal            mTempMatrix.set(getMatrix());
1402ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal            mTempMatrix.postTranslate(-mOverlayTranslation, 0);
1403ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal            return mTempMatrix;
1404ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal        }
1405ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal        return super.getPageShiftMatrix();
1406ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal    }
1407ee68816be8e4667548ac8925a14d014f1d66d460Sunny Goyal
1408c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    private void setTranslationAndAlpha(View v, float transX, float alpha) {
1409c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        if (v != null) {
1410c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal            v.setTranslationX(transX);
1411c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal            v.setAlpha(alpha);
1412c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal        }
1413c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal    }
1414c86df470ab360a6e3f7edfd4b9cadcc5c31734f8Sunny Goyal
141589cbbb9d2e6885f176d59d8e7476a4001fe6e7f5Adam Cohen    @Override
14164d113a5ff44ff1a7f19263bde21581fbf9a54212Sunny Goyal    protected void getEdgeVerticalPostion(int[] pos) {
14174d113a5ff44ff1a7f19263bde21581fbf9a54212Sunny Goyal        View child = getChildAt(getPageCount() - 1);
14184d113a5ff44ff1a7f19263bde21581fbf9a54212Sunny Goyal        pos[0] = child.getTop();
14194d113a5ff44ff1a7f19263bde21581fbf9a54212Sunny Goyal        pos[1] = child.getBottom();
14204d113a5ff44ff1a7f19263bde21581fbf9a54212Sunny Goyal    }
14214d113a5ff44ff1a7f19263bde21581fbf9a54212Sunny Goyal
14224d113a5ff44ff1a7f19263bde21581fbf9a54212Sunny Goyal    @Override
142389cbbb9d2e6885f176d59d8e7476a4001fe6e7f5Adam Cohen    protected void notifyPageSwitchListener() {
142489cbbb9d2e6885f176d59d8e7476a4001fe6e7f5Adam Cohen        super.notifyPageSwitchListener();
1425bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen
14266ad0e7dcaeef394cc1c28294dde309da69233412Adam Cohen        if (hasCustomContent() && getNextPage() == 0 && !mCustomContentShowing) {
1427bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen            mCustomContentShowing = true;
1428bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen            if (mCustomContentCallbacks != null) {
14293a8a8f7c2355863006208fced945f63c9bdb40b4Selim Cinek                mCustomContentCallbacks.onShow(false);
14303d1b2b420684a61cf68baf4bd2a6a5ac9680d16aAdam Cohen                mCustomContentShowTime = System.currentTimeMillis();
1431bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen            }
14326ad0e7dcaeef394cc1c28294dde309da69233412Adam Cohen        } else if (hasCustomContent() && getNextPage() != 0 && mCustomContentShowing) {
1433bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen            mCustomContentShowing = false;
1434bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen            if (mCustomContentCallbacks != null) {
1435bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen                mCustomContentCallbacks.onHide();
1436bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen            }
1437bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen        }
143801f2d7fa4b24b21543012060305d693899b4beaaChris Craik    }
14390142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka
14406fecd410e8567af916b1bbd53198d9878c686177Adam Cohen    protected CustomContentCallbacks getCustomContentCallbacks() {
14416fecd410e8567af916b1bbd53198d9878c686177Adam Cohen        return mCustomContentCallbacks;
14426fecd410e8567af916b1bbd53198d9878c686177Adam Cohen    }
14436fecd410e8567af916b1bbd53198d9878c686177Adam Cohen
1444824fcb3a3e05166d7e1bd9c2cd821aeaebc994c5Adam Cohen    protected void setWallpaperDimension() {
14456f866095c0177a794050e578f148b640f4e0a0abSunny Goyal        Utilities.THREAD_POOL_EXECUTOR.execute(new Runnable() {
14466f866095c0177a794050e578f148b640f4e0a0abSunny Goyal            @Override
14476f866095c0177a794050e578f148b640f4e0a0abSunny Goyal            public void run() {
14486f866095c0177a794050e578f148b640f4e0a0abSunny Goyal                final Point size = LauncherAppState.getInstance()
14496f866095c0177a794050e578f148b640f4e0a0abSunny Goyal                        .getInvariantDeviceProfile().defaultWallpaperSize;
14506f866095c0177a794050e578f148b640f4e0a0abSunny Goyal                if (size.x != mWallpaperManager.getDesiredMinimumWidth()
14516f866095c0177a794050e578f148b640f4e0a0abSunny Goyal                        || size.y != mWallpaperManager.getDesiredMinimumHeight()) {
14526f866095c0177a794050e578f148b640f4e0a0abSunny Goyal                    mWallpaperManager.suggestDesiredDimensions(size.x, size.y);
14534b17147c28488c20b01aea18297fd6a327bad213Sunny Goyal                }
1454a6a0547fa6df0769fcd107a2e5f7c7d527fa0700Michael Jurka            }
14556f866095c0177a794050e578f148b640f4e0a0abSunny Goyal        });
1456c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka    }
1457c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka
1458f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    protected void snapToPage(int whichPage, Runnable r) {
1459ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        snapToPage(whichPage, SLOW_PAGE_SNAP_ANIMATION_DURATION, r);
1460ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen    }
1461ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen
1462ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen    protected void snapToPage(int whichPage, int duration, Runnable r) {
1463f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        if (mDelayedSnapToPageRunnable != null) {
1464f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            mDelayedSnapToPageRunnable.run();
1465f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        }
1466f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        mDelayedSnapToPageRunnable = r;
1467ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        snapToPage(whichPage, duration);
1468f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    }
1469f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
1470db364c3355a7f5212459c74bd7e837f12aca463bAdam Cohen    public void snapToScreenId(long screenId) {
1471db364c3355a7f5212459c74bd7e837f12aca463bAdam Cohen        snapToScreenId(screenId, null);
1472db364c3355a7f5212459c74bd7e837f12aca463bAdam Cohen    }
1473db364c3355a7f5212459c74bd7e837f12aca463bAdam Cohen
1474dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    protected void snapToScreenId(long screenId, Runnable r) {
1475dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        snapToPage(getPageIndexForScreenId(screenId), r);
1476dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
1477dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
1478340c5f3a708efa280a120708594247952ca65182Michael Jurka    @Override
1479340c5f3a708efa280a120708594247952ca65182Michael Jurka    public void computeScroll() {
1480340c5f3a708efa280a120708594247952ca65182Michael Jurka        super.computeScroll();
1481fe09cb7fe3ec1ab3e7aabd2d0415de61df31c4dbMichael Jurka        mWallpaperOffset.syncWithScroll();
1482cc07e7a9c6e383f35a8de717be71201f666b01a8Michael Jurka    }
1483cc07e7a9c6e383f35a8de717be71201f666b01a8Michael Jurka
148495cdb3a0211650e76adb451588ce89fd585b178fTony Wickham    public void computeScrollWithoutInvalidation() {
148595cdb3a0211650e76adb451588ce89fd585b178fTony Wickham        computeScrollHelper(false);
148695cdb3a0211650e76adb451588ce89fd585b178fTony Wickham    }
148795cdb3a0211650e76adb451588ce89fd585b178fTony Wickham
1488ed05f09aa1f83fc7ed02c8c40fa473a7f1a3a1e0Jason Monk    @Override
1489a5209e33fcce5cd429086971ef1b57bcfc106c23Tony Wickham    protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
1490a5209e33fcce5cd429086971ef1b57bcfc106c23Tony Wickham        if (!isSwitchingState()) {
1491a5209e33fcce5cd429086971ef1b57bcfc106c23Tony Wickham            super.determineScrollingStart(ev, touchSlopScale);
1492a5209e33fcce5cd429086971ef1b57bcfc106c23Tony Wickham        }
1493a5209e33fcce5cd429086971ef1b57bcfc106c23Tony Wickham    }
1494a5209e33fcce5cd429086971ef1b57bcfc106c23Tony Wickham
1495a5209e33fcce5cd429086971ef1b57bcfc106c23Tony Wickham    @Override
1496ed05f09aa1f83fc7ed02c8c40fa473a7f1a3a1e0Jason Monk    public void announceForAccessibility(CharSequence text) {
1497ed05f09aa1f83fc7ed02c8c40fa473a7f1a3a1e0Jason Monk        // Don't announce if apps is on top of us.
1498b745afbdd75157c73d581b345118cdaff99e912dWinson Chung        if (!mLauncher.isAppsViewVisible()) {
1499ed05f09aa1f83fc7ed02c8c40fa473a7f1a3a1e0Jason Monk            super.announceForAccessibility(text);
1500ed05f09aa1f83fc7ed02c8c40fa473a7f1a3a1e0Jason Monk        }
1501ed05f09aa1f83fc7ed02c8c40fa473a7f1a3a1e0Jason Monk    }
1502ed05f09aa1f83fc7ed02c8c40fa473a7f1a3a1e0Jason Monk
150394f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy    public void showOutlinesTemporarily() {
150494f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy        if (!mIsPageMoving && !isTouchActive()) {
150594f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy            snapToPage(mCurrentPage);
150694f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy        }
150794f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy    }
150894f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy
1509a3d30adbfd4f013260f1f5ba3a56bc9bb4a11717Michael Jurka    private void updatePageAlphaValues(int screenCenter) {
1510869390b6abd6e005a5ce85a8ee3c11daeecce919Michael Jurka        if (mWorkspaceFadeInAdjacentScreens &&
1511c50438c802355dff714b04f42a32e518cb9e91d8Adam Cohen                !workspaceInModalState() &&
15124d113a5ff44ff1a7f19263bde21581fbf9a54212Sunny Goyal                !mIsSwitchingState) {
151384a465aa0e795619b248047c5e21cd77c82b1ff1Adam Cohen            for (int i = numCustomPages(); i < getChildCount(); i++) {
1514869390b6abd6e005a5ce85a8ee3c11daeecce919Michael Jurka                CellLayout child = (CellLayout) getChildAt(i);
1515869390b6abd6e005a5ce85a8ee3c11daeecce919Michael Jurka                if (child != null) {
1516869390b6abd6e005a5ce85a8ee3c11daeecce919Michael Jurka                    float scrollProgress = getScrollProgress(screenCenter, child, i);
151773894964c27f68a3a7ffb45bfba76520613fd3f0Adam Cohen                    float alpha = 1 - Math.abs(scrollProgress);
1518869390b6abd6e005a5ce85a8ee3c11daeecce919Michael Jurka                    child.getShortcutsAndWidgets().setAlpha(alpha);
151973894964c27f68a3a7ffb45bfba76520613fd3f0Adam Cohen                }
1520f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen            }
1521f34bab59fc0260f926aec44d044883dce1b4191fAdam Cohen        }
1522a3d30adbfd4f013260f1f5ba3a56bc9bb4a11717Michael Jurka    }
1523a3d30adbfd4f013260f1f5ba3a56bc9bb4a11717Michael Jurka
1524c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
1525e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    @Override
1526c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    public void enableAccessibleDrag(boolean enable) {
1527c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        for (int i = 0; i < getChildCount(); i++) {
1528c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            CellLayout child = (CellLayout) getChildAt(i);
1529e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal            child.enableAccessibleDrag(enable, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG);
1530c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        }
1531c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
1532c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        if (enable) {
1533c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            // We need to allow our individual children to become click handlers in this case
1534c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            setOnClickListener(null);
1535c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        } else {
1536c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            // Reset our click listener
1537c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            setOnClickListener(mLauncher);
1538c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        }
1539006ee269ba2e9946a83b42f96d4a0296254cba4aWinson Chung        mLauncher.getSearchDropTargetBar().enableAccessibleDrag(enable);
154034d2c915c4762a073b60bb676a9e905392e9679cTony Wickham        mLauncher.getAppInfoDropTargetBar().enableAccessibleDrag(enable);
1541e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal        mLauncher.getHotseat().getLayout()
1542e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal            .enableAccessibleDrag(enable, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG);
1543c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
1544c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
154598ca0f7203f66c491a5362c2f4db79547872bdfeWinson Chung    public boolean hasCustomContent() {
1546edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen        return (mScreenOrder.size() > 0 && mScreenOrder.get(0) == CUSTOM_CONTENT_SCREEN_ID);
1547edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen    }
1548edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen
1549fe0ace3583bc57adbaaec730b1b1757032e90c41Michael Jurka    public int numCustomPages() {
1550fe0ace3583bc57adbaaec730b1b1757032e90c41Michael Jurka        return hasCustomContent() ? 1 : 0;
1551fe0ace3583bc57adbaaec730b1b1757032e90c41Michael Jurka    }
1552fe0ace3583bc57adbaaec730b1b1757032e90c41Michael Jurka
1553bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen    public boolean isOnOrMovingToCustomContent() {
15544b66bf3cff049ba39da74c0fb0f9edbced410f7cAdam Cohen        return hasCustomContent() && getNextPage() == 0 && mRestorePage == INVALID_RESTORE_PAGE;
1555bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen    }
1556bffe745b1e195deedc1dcc60f37950b3b0b4c652Adam Cohen
1557edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen    private void updateStateForCustomContent(int screenCenter) {
1558a888158880fea53479447d883391f552604e46b6Dave Hawkey        float translationX = 0;
1559a888158880fea53479447d883391f552604e46b6Dave Hawkey        float progress = 0;
1560af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski        if (hasCustomContent()) {
1561edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen            int index = mScreenOrder.indexOf(CUSTOM_CONTENT_SCREEN_ID);
1562564a2e7116b026c1d73829e65d47b660b03a3c72Adam Cohen
1563a45de07ece42b8dac4d5a99326ebf2d4b72ef267Adam Cohen            int scrollDelta = getScrollX() - getScrollForPage(index) -
1564a45de07ece42b8dac4d5a99326ebf2d4b72ef267Adam Cohen                    getLayoutTransitionOffsetForPage(index);
1565a45de07ece42b8dac4d5a99326ebf2d4b72ef267Adam Cohen            float scrollRange = getScrollForPage(index + 1) - getScrollForPage(index);
1566a45de07ece42b8dac4d5a99326ebf2d4b72ef267Adam Cohen            translationX = scrollRange - scrollDelta;
1567a45de07ece42b8dac4d5a99326ebf2d4b72ef267Adam Cohen            progress = (scrollRange - scrollDelta) / scrollRange;
1568af9b0e51aac239457fbd038f7cb2088265054e42Adam Cohen
15697066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal            if (mIsRtl) {
1570af9b0e51aac239457fbd038f7cb2088265054e42Adam Cohen                translationX = Math.min(0, translationX);
1571af9b0e51aac239457fbd038f7cb2088265054e42Adam Cohen            } else {
1572af9b0e51aac239457fbd038f7cb2088265054e42Adam Cohen                translationX = Math.max(0, translationX);
1573af9b0e51aac239457fbd038f7cb2088265054e42Adam Cohen            }
1574955806dfc49aa793a386722800be9433f94c5563Adam Cohen            progress = Math.max(0, progress);
1575a888158880fea53479447d883391f552604e46b6Dave Hawkey        }
1576edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen
1577a888158880fea53479447d883391f552604e46b6Dave Hawkey        if (Float.compare(progress, mLastCustomContentScrollProgress) == 0) return;
157884add1de6d0964d675542af2a70f7f67e5da556aAdam Cohen
157984add1de6d0964d675542af2a70f7f67e5da556aAdam Cohen        CellLayout cc = mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID);
15806c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        if (progress > 0 && cc.getVisibility() != VISIBLE && !workspaceInModalState()) {
158184add1de6d0964d675542af2a70f7f67e5da556aAdam Cohen            cc.setVisibility(VISIBLE);
158284add1de6d0964d675542af2a70f7f67e5da556aAdam Cohen        }
158384add1de6d0964d675542af2a70f7f67e5da556aAdam Cohen
1584a888158880fea53479447d883391f552604e46b6Dave Hawkey        mLastCustomContentScrollProgress = progress;
1585c36fa5ce395bd39d16f6a998372700ed53d5d9d6Adam Cohen
15860f785720667ab8afe4b4620a6c333d382d8659edWinson Chung        // We should only update the drag layer background alpha if we are not in all apps or the
15870f785720667ab8afe4b4620a6c333d382d8659edWinson Chung        // widgets tray
15880f785720667ab8afe4b4620a6c333d382d8659edWinson Chung        if (mState == State.NORMAL) {
15890c9143d06b64ca40c64e66ddd65848deeead8131Sunny Goyal            mLauncher.getDragLayer().setBackgroundAlpha(progress == 1 ? 0 : progress * 0.8f);
15900f785720667ab8afe4b4620a6c333d382d8659edWinson Chung        }
159122cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen
1592a888158880fea53479447d883391f552604e46b6Dave Hawkey        if (mLauncher.getHotseat() != null) {
1593a888158880fea53479447d883391f552604e46b6Dave Hawkey            mLauncher.getHotseat().setTranslationX(translationX);
1594a888158880fea53479447d883391f552604e46b6Dave Hawkey        }
1595c36fa5ce395bd39d16f6a998372700ed53d5d9d6Adam Cohen
1596a888158880fea53479447d883391f552604e46b6Dave Hawkey        if (getPageIndicator() != null) {
1597c487bd34ace268dd8be5480ba884baed6096f76eSunny Goyal            getPageIndicator().setTranslationX(translationX);
1598a888158880fea53479447d883391f552604e46b6Dave Hawkey        }
1599c36fa5ce395bd39d16f6a998372700ed53d5d9d6Adam Cohen
1600a888158880fea53479447d883391f552604e46b6Dave Hawkey        if (mCustomContentCallbacks != null) {
1601a888158880fea53479447d883391f552604e46b6Dave Hawkey            mCustomContentCallbacks.onScrollProgressChanged(progress);
1602edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen        }
1603edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen    }
1604edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen
1605a3d30adbfd4f013260f1f5ba3a56bc9bb4a11717Michael Jurka    @Override
160653805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen    protected OnClickListener getPageIndicatorClickListener() {
160753805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen        AccessibilityManager am = (AccessibilityManager)
160853805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen                getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
160953805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen        if (!am.isTouchExplorationEnabled()) {
161053805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen            return null;
161153805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen        }
16129eba1fd75e9fa6b0dc5cad9a4e817b3b167d2461Sunny Goyal        return new OnClickListener() {
161353805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen            @Override
161453805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen            public void onClick(View arg0) {
1615dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung                mLauncher.showOverviewMode(true);
161653805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen            }
161753805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen        };
161853805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen    }
161953805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen
162053805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen    @Override
1621a3d30adbfd4f013260f1f5ba3a56bc9bb4a11717Michael Jurka    protected void screenScrolled(int screenCenter) {
1622a3d30adbfd4f013260f1f5ba3a56bc9bb4a11717Michael Jurka        updatePageAlphaValues(screenCenter);
1623edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen        updateStateForCustomContent(screenCenter);
1624d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka        enableHwLayersOnVisiblePages();
1625b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen    }
1626b5ba097015c4794fa822f30b38a60a7070a00097Adam Cohen
162700acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    protected void onAttachedToWindow() {
162800acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        super.onAttachedToWindow();
16299eba1fd75e9fa6b0dc5cad9a4e817b3b167d2461Sunny Goyal        IBinder windowToken = getWindowToken();
16309eba1fd75e9fa6b0dc5cad9a4e817b3b167d2461Sunny Goyal        mWallpaperOffset.setWindowToken(windowToken);
1631956091ba7863bd72086275e61084864994dd6fa7Joe Onorato        computeScroll();
16329eba1fd75e9fa6b0dc5cad9a4e817b3b167d2461Sunny Goyal        mDragController.setWindowToken(windowToken);
16339c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka    }
16349c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka
16359c6fbed48783dacd9bc6b4ec5f2cc8e59ec7ee37Michael Jurka    protected void onDetachedFromWindow() {
1636e6661950f54f1813aec69928cecb3e5d4446c7e7Romain Guy        super.onDetachedFromWindow();
16379eba1fd75e9fa6b0dc5cad9a4e817b3b167d2461Sunny Goyal        mWallpaperOffset.setWindowToken(null);
163800acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
163900acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
164053805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen    protected void onResume() {
164153805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen        if (getPageIndicator() != null) {
164253805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen            // In case accessibility state has changed, we need to perform this on every
164353805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen            // attach to window
1644d36d9476dc2ed915d90f8df5916450c02d107effAdam Cohen            OnClickListener listener = getPageIndicatorClickListener();
1645d36d9476dc2ed915d90f8df5916450c02d107effAdam Cohen            if (listener != null) {
1646c487bd34ace268dd8be5480ba884baed6096f76eSunny Goyal                getPageIndicator().setOnClickListener(listener);
1647d36d9476dc2ed915d90f8df5916450c02d107effAdam Cohen            }
164853805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen        }
1649a6a0547fa6df0769fcd107a2e5f7c7d527fa0700Michael Jurka
1650a6a0547fa6df0769fcd107a2e5f7c7d527fa0700Michael Jurka        // Update wallpaper dimensions if they were changed since last onResume
1651a6a0547fa6df0769fcd107a2e5f7c7d527fa0700Michael Jurka        // (we also always set the wallpaper dimensions in the constructor)
1652a6a0547fa6df0769fcd107a2e5f7c7d527fa0700Michael Jurka        if (LauncherAppState.getInstance().hasWallpaperChangedSinceLastCheck()) {
1653824fcb3a3e05166d7e1bd9c2cd821aeaebc994c5Adam Cohen            setWallpaperDimension();
1654a6a0547fa6df0769fcd107a2e5f7c7d527fa0700Michael Jurka        }
16559eba1fd75e9fa6b0dc5cad9a4e817b3b167d2461Sunny Goyal        mWallpaperOffset.onResume();
165653805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen    }
165753805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen
165831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
165928750fba6a2d141eb9a1e566718c17236030b815Michael Jurka    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
1660c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
1661fe09cb7fe3ec1ab3e7aabd2d0415de61df31c4dbMichael Jurka            mWallpaperOffset.syncWithScroll();
1662cc07e7a9c6e383f35a8de717be71201f666b01a8Michael Jurka            mWallpaperOffset.jumpToFinal();
1663c5b262ccf639fedac2aff5bb8238342f95396338Michael Jurka        }
16640142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        super.onLayout(changed, left, top, right, bottom);
166531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
166631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
166731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
166831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
1669b745afbdd75157c73d581b345118cdaff99e912dWinson Chung        if (!mLauncher.isAppsViewVisible()) {
167031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final Folder openFolder = getOpenFolder();
167131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (openFolder != null) {
167231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                return openFolder.requestFocus(direction, previouslyFocusedRect);
167331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            } else {
16740142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
167531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
167631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
167731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return false;
167831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
167931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
168031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
168197d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung    public int getDescendantFocusability() {
16826c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        if (workspaceInModalState()) {
168397d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung            return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
168497d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        }
168597d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        return super.getDescendantFocusability();
168697d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung    }
168797d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung
168897d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung    @Override
1689c2e24c0a37ddeab930f731c062705d435e477f75Romain Guy    public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
1690b745afbdd75157c73d581b345118cdaff99e912dWinson Chung        if (!mLauncher.isAppsViewVisible()) {
169131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final Folder openFolder = getOpenFolder();
16920142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            if (openFolder != null) {
169331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                openFolder.addFocusables(views, direction);
16940142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            } else {
16950142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                super.addFocusables(views, direction, focusableMode);
169631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
169731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
169831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
169931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17006c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen    public boolean workspaceInModalState() {
17016c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        return mState != State.NORMAL;
1702d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka    }
1703d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
17040142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    void enableChildrenCache(int fromPage, int toPage) {
17050142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        if (fromPage > toPage) {
17060142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            final int temp = fromPage;
17070142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            fromPage = toPage;
17080142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            toPage = temp;
17093a2b3f2be58843d26549fb0ec6c6533627c7cd19Mike Cleron        }
1710aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
1711dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        final int screenCount = getChildCount();
1712fea5d0250767ab938f623a404e6292a32dd2fdf5Adam Powell
17130142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        fromPage = Math.max(fromPage, 0);
17140142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        toPage = Math.min(toPage, screenCount - 1);
1715fea5d0250767ab938f623a404e6292a32dd2fdf5Adam Powell
17160142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        for (int i = fromPage; i <= toPage; i++) {
171731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final CellLayout layout = (CellLayout) getChildAt(i);
171831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            layout.setChildrenDrawnWithCacheEnabled(true);
171931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            layout.setChildrenDrawingCacheEnabled(true);
172031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
172131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
172231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
172331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    void clearChildrenCache() {
1724dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        final int screenCount = getChildCount();
1725dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        for (int i = 0; i < screenCount; i++) {
172631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            final CellLayout layout = (CellLayout) getChildAt(i);
172731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            layout.setChildrenDrawnWithCacheEnabled(false);
17288182e5bc3c2d1a0140df345599b89369d457bb4aAdam Cohen            // In software mode, we don't want the items to continue to be drawn into bitmaps
17298182e5bc3c2d1a0140df345599b89369d457bb4aAdam Cohen            if (!isHardwareAccelerated()) {
17308182e5bc3c2d1a0140df345599b89369d457bb4aAdam Cohen                layout.setChildrenDrawingCacheEnabled(false);
17318182e5bc3c2d1a0140df345599b89369d457bb4aAdam Cohen            }
173231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
173331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
173431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1735091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk void updateChildrenLayersEnabled(boolean force) {
17366c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        boolean small = mState == State.OVERVIEW || mIsSwitchingState;
17373a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka        boolean enableChildrenLayers = force || small || mAnimatingViewIntoPlace || isPageMoving();
1738d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
1739d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        if (enableChildrenLayers != mChildrenLayersEnabled) {
1740d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            mChildrenLayersEnabled = enableChildrenLayers;
1741d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka            if (mChildrenLayersEnabled) {
1742d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka                enableHwLayersOnVisiblePages();
1743d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka            } else {
1744d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka                for (int i = 0; i < getPageCount(); i++) {
1745d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka                    final CellLayout cl = (CellLayout) getChildAt(i);
174601f2d7fa4b24b21543012060305d693899b4beaaChris Craik                    cl.enableHardwareLayer(false);
1747d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka                }
1748d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka            }
1749d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka        }
1750d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka    }
1751d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka
1752d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka    private void enableHwLayersOnVisiblePages() {
1753d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka        if (mChildrenLayersEnabled) {
1754d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka            final int screenCount = getChildCount();
1755d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka            getVisiblePages(mTempVisiblePagesRange);
1756d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka            int leftScreen = mTempVisiblePagesRange[0];
1757d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka            int rightScreen = mTempVisiblePagesRange[1];
1758d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka            if (leftScreen == rightScreen) {
1759d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka                // make sure we're caching at least two pages always
1760d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka                if (rightScreen < screenCount - 1) {
1761d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka                    rightScreen++;
1762d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka                } else if (leftScreen > 0) {
1763d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka                    leftScreen--;
1764d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka                }
1765d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka            }
176601f2d7fa4b24b21543012060305d693899b4beaaChris Craik
176701f2d7fa4b24b21543012060305d693899b4beaaChris Craik            final CellLayout customScreen = mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID);
1768d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka            for (int i = 0; i < screenCount; i++) {
176947639b9c1db9a5d0d89ce6b4b36d6b3c0d489602Michael Jurka                final CellLayout layout = (CellLayout) getPageAt(i);
177001f2d7fa4b24b21543012060305d693899b4beaaChris Craik
177101f2d7fa4b24b21543012060305d693899b4beaaChris Craik                // enable layers between left and right screen inclusive, except for the
177201f2d7fa4b24b21543012060305d693899b4beaaChris Craik                // customScreen, which may animate its content during transitions.
177301f2d7fa4b24b21543012060305d693899b4beaaChris Craik                boolean enableLayer = layout != customScreen &&
177401f2d7fa4b24b21543012060305d693899b4beaaChris Craik                        leftScreen <= i && i <= rightScreen && shouldDrawChild(layout);
177501f2d7fa4b24b21543012060305d693899b4beaaChris Craik                layout.enableHardwareLayer(enableLayer);
17763a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka            }
17773a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka        }
17783a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka    }
17793a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka
17803a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka    public void buildPageHardwareLayers() {
17813a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka        // force layers to be enabled just for the call to buildLayer
17823a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka        updateChildrenLayersEnabled(true);
17833a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka        if (getWindowToken() != null) {
17843a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka            final int childCount = getChildCount();
17853a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka            for (int i = 0; i < childCount; i++) {
17863a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka                CellLayout cl = (CellLayout) getChildAt(i);
1787d51f33a6316c34fc69b8040946ed9a62519bb225Michael Jurka                cl.buildHardwareLayer();
1788d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            }
1789ce7e05fbe8abd5f25ec47e0e05b5cc76ceb39d2eMichael Jurka        }
17903a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka        updateChildrenLayersEnabled(false);
1791ce7e05fbe8abd5f25ec47e0e05b5cc76ceb39d2eMichael Jurka    }
1792ce7e05fbe8abd5f25ec47e0e05b5cc76ceb39d2eMichael Jurka
1793ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal    @Override
1794ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal    protected void getVisiblePages(int[] range) {
1795ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal        super.getVisiblePages(range);
179629d853c2ddf6432685a54e3966820f1a81cee183Tony Wickham        if (mForceDrawAdjacentPages) {
1797ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal            // In overview mode, make sure that the two side pages are visible.
1798f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham            range[0] = Utilities.boundToRange(getCurrentPage() - 1, numCustomPages(), range[1]);
1799f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham            range[1] = Utilities.boundToRange(getCurrentPage() + 1, range[0], getPageCount() - 1);
1800ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal        }
1801ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal    }
1802ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal
18031d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown    protected void onWallpaperTap(MotionEvent ev) {
1804cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal        final int[] position = mTempXY;
18051d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown        getLocationOnScreen(position);
18061d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown
18071d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown        int pointerIndex = ev.getActionIndex();
18081d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown        position[0] += (int) ev.getX(pointerIndex);
18091d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown        position[1] += (int) ev.getY(pointerIndex);
18101d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown
18111d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown        mWallpaperManager.sendWallpaperCommand(getWindowToken(),
18121d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown                ev.getAction() == MotionEvent.ACTION_UP
18131d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown                        ? WallpaperManager.COMMAND_TAP : WallpaperManager.COMMAND_SECONDARY_TAP,
18141d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown                position[0], position[1], 0, null);
18151d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown    }
18161d0867c8de6a889bccde9b7e768daef182a25e5cJeff Brown
181761033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen    /*
18186639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    *
18196639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    * We call these methods (onDragStartedWithItemSpans/onDragStartedWithSize) whenever we
18206639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    * start a drag in Launcher, regardless of whether the drag has ever entered the Workspace
18216639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    *
18226639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    * These methods mark the appropriate pages as accepting drops (which alters their visual
18236639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    * appearance).
18246639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    *
18256639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen    */
1826508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal    private static Rect getDrawableBounds(Drawable d) {
1827859002985e0e5d73f54c942f2024607431db60fcMathew Inwood        Rect bounds = new Rect();
1828859002985e0e5d73f54c942f2024607431db60fcMathew Inwood        d.copyBounds(bounds);
1829859002985e0e5d73f54c942f2024607431db60fcMathew Inwood        if (bounds.width() == 0 || bounds.height() == 0) {
1830859002985e0e5d73f54c942f2024607431db60fcMathew Inwood            bounds.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
183195abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal        } else {
183295abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal            bounds.offsetTo(0, 0);
183395abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal        }
183495abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal        if (d instanceof PreloadIconDrawable) {
183595abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal            int inset = -((PreloadIconDrawable) d).getOutset();
183695abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal            bounds.inset(inset, inset);
1837859002985e0e5d73f54c942f2024607431db60fcMathew Inwood        }
1838859002985e0e5d73f54c942f2024607431db60fcMathew Inwood        return bounds;
1839859002985e0e5d73f54c942f2024607431db60fcMathew Inwood    }
1840859002985e0e5d73f54c942f2024607431db60fcMathew Inwood
1841babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung    public void onExternalDragStartedWithItem(View v) {
1842babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        // Compose a drag bitmap with the view scaled to the icon size
18432e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen        DeviceProfile grid = mLauncher.getDeviceProfile();
1844babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        int iconSize = grid.iconSizePx;
1845babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        int bmpWidth = v.getMeasuredWidth();
1846babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        int bmpHeight = v.getMeasuredHeight();
1847babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung
1848babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        // If this is a text view, use its drawable instead
1849babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        if (v instanceof TextView) {
1850b745afbdd75157c73d581b345118cdaff99e912dWinson Chung            Drawable d = getTextViewIcon((TextView) v);
1851859002985e0e5d73f54c942f2024607431db60fcMathew Inwood            Rect bounds = getDrawableBounds(d);
1852859002985e0e5d73f54c942f2024607431db60fcMathew Inwood            bmpWidth = bounds.width();
1853859002985e0e5d73f54c942f2024607431db60fcMathew Inwood            bmpHeight = bounds.height();
1854babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        }
1855babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung
1856babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        // Compose the bitmap to create the icon from
1857babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        Bitmap b = Bitmap.createBitmap(bmpWidth, bmpHeight,
1858babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung                Bitmap.Config.ARGB_8888);
1859508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.setBitmap(b);
1860508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        drawDragView(v, mCanvas, 0);
1861508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.setBitmap(null);
1862babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung
1863babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        // The outline is used to visualize where the item will land if dropped
1864508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mDragOutline = createDragOutline(b, DRAG_BITMAP_PADDING, iconSize, iconSize, true);
1865babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung    }
1866babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung
18678c3339bde96d11fd1d7f818cd39e7438e309ffd1Michael Jurka    public void onDragStartedWithItem(PendingAddItemInfo info, Bitmap b, boolean clipAlpha) {
1868869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham        // Find a page that has enough space to place this widget (after rearranging/resizing).
1869869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham        // Start at the current page and search right (on LTR) until finding a page with enough
1870869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham        // space. Since an empty screen is the furthest right, a page must be found.
18714cab3b550b3c296cd120bdf23f524f34b155dca5Tony Wickham        int currentPageInOverview = getPageNearestToCenterOfScreen();
18724cab3b550b3c296cd120bdf23f524f34b155dca5Tony Wickham        for (int pageIndex = currentPageInOverview; pageIndex < getPageCount(); pageIndex++) {
1873869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham            CellLayout page = (CellLayout) getPageAt(pageIndex);
1874869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham            if (page.hasReorderSolution(info)) {
1875869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham                setCurrentPage(pageIndex);
1876869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham                break;
1877869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham            }
1878869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham        }
1879869306140d3bb8782040ed65d3ae0ba498c2de6cTony Wickham
18805b0e669169ea2c951bf2f6f71faf793b24db3c23Sunny Goyal        int[] size = estimateItemSize(info, false);
18816639687cd67bab1aeef2a75e5c6bc458b20dc082Adam Cohen
1882d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        // The outline is used to visualize where the item will land if dropped
1883508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mDragOutline = createDragOutline(b, DRAG_BITMAP_PADDING, size[0], size[1], clipAlpha);
18843e7c7634531302271270c8cf418abc959d621cbcMichael Jurka    }
18853e7c7634531302271270c8cf418abc959d621cbcMichael Jurka
1886758a9239f045ff4eb85c421a2f67a720b5b3c621Patrick Dubroy    public void exitWidgetResizeMode() {
1887716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen        DragLayer dragLayer = mLauncher.getDragLayer();
18886788269814a678af976dfc7d6135993e172ce586Adam Cohen        dragLayer.clearAllResizeFrames();
1889758a9239f045ff4eb85c421a2f67a720b5b3c621Patrick Dubroy    }
1890758a9239f045ff4eb85c421a2f67a720b5b3c621Patrick Dubroy
1891f358a4bbd6ce5b6ddadfae19e6b9c6872530d627Adam Cohen    @Override
18926f127a614c6a3e64a5de963f3a11d68d3368cd84Adam Cohen    protected void getFreeScrollPageRange(int[] range) {
18936f127a614c6a3e64a5de963f3a11d68d3368cd84Adam Cohen        getOverviewModePages(range);
18946f127a614c6a3e64a5de963f3a11d68d3368cd84Adam Cohen    }
18956f127a614c6a3e64a5de963f3a11d68d3368cd84Adam Cohen
18966f127a614c6a3e64a5de963f3a11d68d3368cd84Adam Cohen    private void getOverviewModePages(int[] range) {
1897fe0ace3583bc57adbaaec730b1b1757032e90c41Michael Jurka        int start = numCustomPages();
18981003be9e626570ce3ca44baf9c60bad3c4438c8bAdam Cohen        int end = getChildCount() - 1;
18991003be9e626570ce3ca44baf9c60bad3c4438c8bAdam Cohen
19001003be9e626570ce3ca44baf9c60bad3c4438c8bAdam Cohen        range[0] = Math.max(0, Math.min(start, getChildCount() - 1));
1901f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham        range[1] = Math.max(0, end);
19026f127a614c6a3e64a5de963f3a11d68d3368cd84Adam Cohen    }
1903dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen
19041d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal    public void onStartReordering() {
1905dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen        super.onStartReordering();
190622cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen        // Reordering handles its own animations, disable the automatic ones.
1907964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        disableLayoutTransitions();
1908dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen    }
1909dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen
19101d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal    public void onEndReordering() {
1911dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen        super.onEndReordering();
1912dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen
1913dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen        if (mLauncher.isWorkspaceLoading()) {
1914dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen            // Invalid and dangerous operation if workspace is loading
1915dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen            return;
1916dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen        }
1917dcb173d0e348c23d967bb0939e63b6ce4e1bd503Adam Cohen
1918dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen        mScreenOrder.clear();
19192bf63d520576baa433a53a77e4e24386e40c1cdaAdam Cohen        int count = getChildCount();
1920dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen        for (int i = 0; i < count; i++) {
1921dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen            CellLayout cl = ((CellLayout) getChildAt(i));
1922dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen            mScreenOrder.add(getIdForScreen(cl));
1923dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen        }
1924d64d1763c590b1c954b759156683abf39ff22945Winson Chung
1925dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen        mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
192622cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen
192722cba7f9958569a5e4c1dfcce4969daeaa6c898cAdam Cohen        // Re-enable auto layout transitions for page deletion.
1928964df6b93e8171a0d3a6f212fed095065f1a65ddWinson Chung        enableLayoutTransitions();
1929dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen    }
1930dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen
1931f358a4bbd6ce5b6ddadfae19e6b9c6872530d627Adam Cohen    public boolean isInOverviewMode() {
1932f358a4bbd6ce5b6ddadfae19e6b9c6872530d627Adam Cohen        return mState == State.OVERVIEW;
1933f358a4bbd6ce5b6ddadfae19e6b9c6872530d627Adam Cohen    }
1934f358a4bbd6ce5b6ddadfae19e6b9c6872530d627Adam Cohen
1935ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal    public void snapToPageFromOverView(int whichPage) {
1936ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal        mStateTransitionAnimation.snapToPageFromOverView(whichPage);
1937ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal    }
1938ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal
1939410f3cdfb4a8c2cf82749a3e73201fb7930540aeAdam Cohen    int getOverviewModeTranslationY() {
19402e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen        DeviceProfile grid = mLauncher.getDeviceProfile();
19414f7eb4077e901a28b2b698060079292b56e991bdWinson Chung        Rect workspacePadding = grid.getWorkspacePadding(Utilities.isRtl(getResources()));
19424f7eb4077e901a28b2b698060079292b56e991bdWinson Chung        int overviewButtonBarHeight = grid.getOverviewModeButtonBarHeight();
1943410f3cdfb4a8c2cf82749a3e73201fb7930540aeAdam Cohen
1944c82d2622bc7305fd3545fe7df97a30187283113fWinson Chung        int scaledHeight = (int) (mOverviewModeShrinkFactor * getNormalChildHeight());
19454f7eb4077e901a28b2b698060079292b56e991bdWinson Chung        int workspaceTop = mInsets.top + workspacePadding.top;
19464f7eb4077e901a28b2b698060079292b56e991bdWinson Chung        int workspaceBottom = getViewportHeight() - mInsets.bottom - workspacePadding.bottom;
19474f7eb4077e901a28b2b698060079292b56e991bdWinson Chung        int overviewTop = mInsets.top;
19484f7eb4077e901a28b2b698060079292b56e991bdWinson Chung        int overviewBottom = getViewportHeight() - mInsets.bottom - overviewButtonBarHeight;
19494f7eb4077e901a28b2b698060079292b56e991bdWinson Chung        int workspaceOffsetTopEdge = workspaceTop + ((workspaceBottom - workspaceTop) - scaledHeight) / 2;
19504f7eb4077e901a28b2b698060079292b56e991bdWinson Chung        int overviewOffsetTopEdge = overviewTop + (overviewBottom - overviewTop - scaledHeight) / 2;
19514f7eb4077e901a28b2b698060079292b56e991bdWinson Chung        return -workspaceOffsetTopEdge + overviewOffsetTopEdge;
1952410f3cdfb4a8c2cf82749a3e73201fb7930540aeAdam Cohen    }
1953410f3cdfb4a8c2cf82749a3e73201fb7930540aeAdam Cohen
195434d2c915c4762a073b60bb676a9e905392e9679cTony Wickham    int getSpringLoadedTranslationY() {
1955e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickham        DeviceProfile grid = mLauncher.getDeviceProfile();
1956e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickham        Rect workspacePadding = grid.getWorkspacePadding(Utilities.isRtl(getResources()));
1957e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickham        int scaledHeight = (int) (mSpringLoadedShrinkFactor * getNormalChildHeight());
1958e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickham        int workspaceTop = mInsets.top + workspacePadding.top;
1959e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickham        int workspaceBottom = getViewportHeight() - mInsets.bottom - workspacePadding.bottom;
1960e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickham        int workspaceHeight = workspaceBottom - workspaceTop;
1961e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickham        // Center the spring-loaded pages by translating it up by half of the reduced height.
1962e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickham        return -(workspaceHeight - scaledHeight) / 2;
196334d2c915c4762a073b60bb676a9e905392e9679cTony Wickham    }
196434d2c915c4762a073b60bb676a9e905392e9679cTony Wickham
1965dadb304b4682998c43b9c07bef45b9a9380f3287Tony Wickham    float getOverviewModeShrinkFactor() {
1966dadb304b4682998c43b9c07bef45b9a9380f3287Tony Wickham        return mOverviewModeShrinkFactor;
1967dadb304b4682998c43b9c07bef45b9a9380f3287Tony Wickham    }
1968dadb304b4682998c43b9c07bef45b9a9380f3287Tony Wickham
1969dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung    /**
1970dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung     * Sets the current workspace {@link State}, returning an animation transitioning the workspace
1971dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung     * to that new state.
1972dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung     */
1973ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal    public Animator setStateWithAnimation(State toState, boolean animated,
1974006ee269ba2e9946a83b42f96d4a0296254cba4aWinson Chung            HashMap<View, Integer> layerViews) {
1975dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung        // Create the animation to the new state
1976f89aa3579a42f234b045546257b8e449e2c6fa8fSunny Goyal        Animator workspaceAnim =  mStateTransitionAnimation.getAnimationToState(mState,
19772a87fe8c41338e746c6959d992edfeb9cb00bcc1Winson                toState, animated, layerViews);
1978dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung
19792e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal        boolean shouldNotifyWidgetChange = !mState.shouldUpdateWidget
19802e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal                && toState.shouldUpdateWidget;
1981dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung        // Update the current state
1982dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung        mState = toState;
198353805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen        updateAccessibilityFlags();
1984dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung
19852e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal        if (shouldNotifyWidgetChange) {
19862e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            mLauncher.notifyWidgetProvidersChanged();
19872e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal        }
19882e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal
1989dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung        return workspaceAnim;
199053805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen    }
199153805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen
1992e25c5d1c94949044c4cd7416f60ea0dc12dadc8dAdam Cohen    State getState() {
1993e25c5d1c94949044c4cd7416f60ea0dc12dadc8dAdam Cohen        return mState;
1994e25c5d1c94949044c4cd7416f60ea0dc12dadc8dAdam Cohen    }
1995e25c5d1c94949044c4cd7416f60ea0dc12dadc8dAdam Cohen
19961d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal    public void updateAccessibilityFlags() {
19979fc953b94dbc6b99e6de08c9dcc80a0cb8e3e319Sunny Goyal        if (Utilities.ATLEAST_LOLLIPOP) {
19981d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            int total = getPageCount();
19991d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            for (int i = numCustomPages(); i < total; i++) {
20001d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal                updateAccessibilityFlags((CellLayout) getPageAt(i), i);
20011d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            }
2002f89aa3579a42f234b045546257b8e449e2c6fa8fSunny Goyal            setImportantForAccessibility((mState == State.NORMAL || mState == State.OVERVIEW)
2003f89aa3579a42f234b045546257b8e449e2c6fa8fSunny Goyal                    ? IMPORTANT_FOR_ACCESSIBILITY_AUTO
2004e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                    : IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
20051d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal        } else {
20061d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            int accessible = mState == State.NORMAL ?
20077af0d4474fe811b66db67f358ee0b5ef84b97a18Vadim Tryshev                    IMPORTANT_FOR_ACCESSIBILITY_AUTO :
20081d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal                        IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS;
20091d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            setImportantForAccessibility(accessible);
20101d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal        }
20111d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal    }
20121d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal
20131d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal    private void updateAccessibilityFlags(CellLayout page, int pageNo) {
20141d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal        if (mState == State.OVERVIEW) {
20151d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            page.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
20161d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            page.getShortcutsAndWidgets().setImportantForAccessibility(
20171d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal                    IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
20181d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            page.setContentDescription(getPageDescription(pageNo));
20191d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal
20201d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            if (mPagesAccessibilityDelegate == null) {
20211d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal                mPagesAccessibilityDelegate = new OverviewScreenAccessibilityDelegate(this);
20221d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            }
20231d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            page.setAccessibilityDelegate(mPagesAccessibilityDelegate);
20241d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal        } else {
20251d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            int accessible = mState == State.NORMAL ?
20267af0d4474fe811b66db67f358ee0b5ef84b97a18Vadim Tryshev                    IMPORTANT_FOR_ACCESSIBILITY_AUTO :
20271d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal                        IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS;
20281d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            page.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
20291d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            page.getShortcutsAndWidgets().setImportantForAccessibility(accessible);
20301d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            page.setContentDescription(null);
20311d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal            page.setAccessibilityDelegate(null);
20321d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal        }
2033edaaa3023251eb2bd71e3da180df39334f77d4a8Adam Cohen    }
2034edaaa3023251eb2bd71e3da180df39334f77d4a8Adam Cohen
2035bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    @Override
2036f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham    public void onLauncherTransitionPrepare(Launcher l, boolean animated,
2037f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham            boolean multiplePagesVisible) {
2038dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung        mIsSwitchingState = true;
2039a0628cc521614835933db74ba5a2536a936629c5Tony Wickham        mTransitionProgress = 0;
2040dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung
2041f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham        if (multiplePagesVisible) {
2042f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham            mForceDrawAdjacentPages = true;
2043f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham        }
2044f898b970cc9130d629d4bc969222aaa96a8a013aTony Wickham        invalidate(); // This will call dispatchDraw(), which calls getVisiblePages().
2045dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung
2046dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung        updateChildrenLayersEnabled(false);
2047dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung        hideCustomContentIfNecessary();
2048bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    }
2049bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
2050bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    @Override
2051a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka    public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
2052d4b28d2e1bb73fcdb1e46bb06e1666e27af2e643Tony Wickham        if (mPageIndicator instanceof PageIndicatorLine) {
2053d4b28d2e1bb73fcdb1e46bb06e1666e27af2e643Tony Wickham            ((PageIndicatorLine) mPageIndicator).setShouldAutoHide(mState != State.SPRING_LOADED);
2054d4b28d2e1bb73fcdb1e46bb06e1666e27af2e643Tony Wickham        }
2055a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka    }
2056a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka
2057a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka    @Override
20587044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung    public void onLauncherTransitionStep(Launcher l, float t) {
20597044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        mTransitionProgress = t;
20607044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung    }
20617044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung
20627044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung    @Override
2063bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
2064dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung        mIsSwitchingState = false;
2065f343499f9b2c01978027e810255274b714ac457dAdam Cohen        updateChildrenLayersEnabled(false);
2066dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung        showCustomContentIfNecessary();
206729d853c2ddf6432685a54e3966820f1a81cee183Tony Wickham        mForceDrawAdjacentPages = false;
2068d4b28d2e1bb73fcdb1e46bb06e1666e27af2e643Tony Wickham        if (mState == State.SPRING_LOADED) {
2069f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham            showPageIndicatorAtCurrentScroll();
2070f549dab31d0fa3e4d0cf8d6025b20e49aafb2d59Tony Wickham        }
20713f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen    }
20723f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen
20733f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen    void updateCustomContentVisibility() {
20743f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen        int visibility = mState == Workspace.State.NORMAL ? VISIBLE : INVISIBLE;
2075dadb304b4682998c43b9c07bef45b9a9380f3287Tony Wickham        setCustomContentVisibility(visibility);
2076dadb304b4682998c43b9c07bef45b9a9380f3287Tony Wickham    }
2077dadb304b4682998c43b9c07bef45b9a9380f3287Tony Wickham
2078dadb304b4682998c43b9c07bef45b9a9380f3287Tony Wickham    void setCustomContentVisibility(int visibility) {
20793f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen        if (hasCustomContent()) {
20803f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen            mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(visibility);
20813f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen        }
20823f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen    }
20833f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen
20843f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen    void showCustomContentIfNecessary() {
20853f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen        boolean show  = mState == Workspace.State.NORMAL;
20863f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen        if (show && hasCustomContent()) {
20873f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen            mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(VISIBLE);
20883f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen        }
20893f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen    }
20903f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen
20913f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen    void hideCustomContentIfNecessary() {
20923f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen        boolean hide  = mState != Workspace.State.NORMAL;
20933f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen        if (hide && hasCustomContent()) {
20941774a8a0c67ae225b31dfb37213d6cadc630e48cAdam Cohen            disableLayoutTransitions();
20953f452c87db9c3639285eafd17fff6a6f9d28da44Adam Cohen            mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(INVISIBLE);
20961774a8a0c67ae225b31dfb37213d6cadc630e48cAdam Cohen            enableLayoutTransitions();
2097f343499f9b2c01978027e810255274b714ac457dAdam Cohen        }
2098f343499f9b2c01978027e810255274b714ac457dAdam Cohen    }
2099f343499f9b2c01978027e810255274b714ac457dAdam Cohen
21004be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato    /**
2101b745afbdd75157c73d581b345118cdaff99e912dWinson Chung     * Returns the drawable for the given text view.
2102b745afbdd75157c73d581b345118cdaff99e912dWinson Chung     */
2103b745afbdd75157c73d581b345118cdaff99e912dWinson Chung    public static Drawable getTextViewIcon(TextView tv) {
2104b745afbdd75157c73d581b345118cdaff99e912dWinson Chung        final Drawable[] drawables = tv.getCompoundDrawables();
2105b745afbdd75157c73d581b345118cdaff99e912dWinson Chung        for (int i = 0; i < drawables.length; i++) {
2106b745afbdd75157c73d581b345118cdaff99e912dWinson Chung            if (drawables[i] != null) {
2107b745afbdd75157c73d581b345118cdaff99e912dWinson Chung                return drawables[i];
2108b745afbdd75157c73d581b345118cdaff99e912dWinson Chung            }
2109b745afbdd75157c73d581b345118cdaff99e912dWinson Chung        }
2110b745afbdd75157c73d581b345118cdaff99e912dWinson Chung        return null;
2111b745afbdd75157c73d581b345118cdaff99e912dWinson Chung    }
2112b745afbdd75157c73d581b345118cdaff99e912dWinson Chung
2113b745afbdd75157c73d581b345118cdaff99e912dWinson Chung    /**
21144be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     * Draw the View v into the given Canvas.
21154be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     *
21164be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     * @param v the view to draw
21174be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     * @param destCanvas the canvas to draw on
21184be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     * @param padding the horizontal and vertical padding to use when drawing
21194be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     */
2120508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal    private static void drawDragView(View v, Canvas destCanvas, int padding) {
2121ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        destCanvas.save();
212295abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal        if (v instanceof TextView) {
2123b745afbdd75157c73d581b345118cdaff99e912dWinson Chung            Drawable d = getTextViewIcon((TextView) v);
2124859002985e0e5d73f54c942f2024607431db60fcMathew Inwood            Rect bounds = getDrawableBounds(d);
212595abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal            destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top);
2126ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            d.draw(destCanvas);
2127ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        } else {
2128106bf64f4a5b4b7772c971f62b6207b5a73529a2Sunny Goyal            final Rect clipRect = sTempRect;
2129106bf64f4a5b4b7772c971f62b6207b5a73529a2Sunny Goyal            v.getDrawingRect(clipRect);
2130106bf64f4a5b4b7772c971f62b6207b5a73529a2Sunny Goyal
2131106bf64f4a5b4b7772c971f62b6207b5a73529a2Sunny Goyal            boolean textVisible = false;
2132ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            if (v instanceof FolderIcon) {
2133099f60d51f6310e1290687e83958e665b3324504Adam Cohen                // For FolderIcons the text can bleed into the icon area, and so we need to
2134099f60d51f6310e1290687e83958e665b3324504Adam Cohen                // hide the text completely (which can't be achieved by clipping).
2135099f60d51f6310e1290687e83958e665b3324504Adam Cohen                if (((FolderIcon) v).getTextVisible()) {
2136099f60d51f6310e1290687e83958e665b3324504Adam Cohen                    ((FolderIcon) v).setTextVisible(false);
2137099f60d51f6310e1290687e83958e665b3324504Adam Cohen                    textVisible = true;
2138099f60d51f6310e1290687e83958e665b3324504Adam Cohen                }
2139ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            }
2140ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
2141ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            destCanvas.clipRect(clipRect, Op.REPLACE);
2142ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            v.draw(destCanvas);
2143099f60d51f6310e1290687e83958e665b3324504Adam Cohen
2144099f60d51f6310e1290687e83958e665b3324504Adam Cohen            // Restore text visibility of FolderIcon if necessary
2145099f60d51f6310e1290687e83958e665b3324504Adam Cohen            if (textVisible) {
2146099f60d51f6310e1290687e83958e665b3324504Adam Cohen                ((FolderIcon) v).setTextVisible(true);
2147099f60d51f6310e1290687e83958e665b3324504Adam Cohen            }
21484be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        }
2149ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        destCanvas.restore();
2150ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen    }
21514be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
2152ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen    /**
2153ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen     * Returns a new bitmap to show when the given View is being dragged around.
2154ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen     * Responsibility for the bitmap is transferred to the caller.
215595abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal     * @param expectedPadding padding to add to the drag view. If a different padding was used
215695abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal     * its value will be changed
2157ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen     */
2158508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal    public Bitmap createDragBitmap(View v, AtomicInteger expectedPadding) {
2159ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        Bitmap b;
21604be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
216195abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal        int padding = expectedPadding.get();
2162ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        if (v instanceof TextView) {
2163b745afbdd75157c73d581b345118cdaff99e912dWinson Chung            Drawable d = getTextViewIcon((TextView) v);
2164859002985e0e5d73f54c942f2024607431db60fcMathew Inwood            Rect bounds = getDrawableBounds(d);
2165859002985e0e5d73f54c942f2024607431db60fcMathew Inwood            b = Bitmap.createBitmap(bounds.width() + padding,
2166859002985e0e5d73f54c942f2024607431db60fcMathew Inwood                    bounds.height() + padding, Bitmap.Config.ARGB_8888);
216795abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal            expectedPadding.set(padding - bounds.left - bounds.top);
2168ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        } else {
2169ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            b = Bitmap.createBitmap(
2170ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen                    v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
2171ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        }
2172ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen
2173508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.setBitmap(b);
2174508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        drawDragView(v, mCanvas, padding);
2175508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.setBitmap(null);
2176ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen
2177ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen        return b;
21784be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato    }
21794be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
21804be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato    /**
21814be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
21824be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     * Responsibility for the bitmap is transferred to the caller.
21834be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato     */
2184508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal    private Bitmap createDragOutline(View v, int padding) {
2185410f3cdfb4a8c2cf82749a3e73201fb7930540aeAdam Cohen        final int outlineColor = getResources().getColor(R.color.outline_color);
21864be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        final Bitmap b = Bitmap.createBitmap(
21874be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato                v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
21884be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
2189508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.setBitmap(b);
2190508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        drawDragView(v, mCanvas, padding);
2191508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mOutlineHelper.applyExpensiveOutlineWithBlur(b, mCanvas, outlineColor, outlineColor);
2192508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.setBitmap(null);
21934be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        return b;
21944be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato    }
21954be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
21964be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato    /**
2197d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka     * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
2198d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka     * Responsibility for the bitmap is transferred to the caller.
2199d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka     */
2200508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal    private Bitmap createDragOutline(Bitmap orig, int padding, int w, int h,
22018c3339bde96d11fd1d7f818cd39e7438e309ffd1Michael Jurka            boolean clipAlpha) {
2202410f3cdfb4a8c2cf82749a3e73201fb7930540aeAdam Cohen        final int outlineColor = getResources().getColor(R.color.outline_color);
2203f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
2204508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.setBitmap(b);
2205f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka
2206f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        Rect src = new Rect(0, 0, orig.getWidth(), orig.getHeight());
2207f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        float scaleFactor = Math.min((w - padding) / (float) orig.getWidth(),
2208f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka                (h - padding) / (float) orig.getHeight());
2209f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        int scaledWidth = (int) (scaleFactor * orig.getWidth());
2210f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        int scaledHeight = (int) (scaleFactor * orig.getHeight());
2211f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        Rect dst = new Rect(0, 0, scaledWidth, scaledHeight);
2212f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka
2213f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        // center the image
2214f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka        dst.offset((w - scaledWidth) / 2, (h - scaledHeight) / 2);
2215f12c75cb48f87955600c56ccbe0aac84b0c11b28Michael Jurka
2216508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.drawBitmap(orig, src, dst, null);
2217508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mOutlineHelper.applyExpensiveOutlineWithBlur(b, mCanvas, outlineColor, outlineColor,
22188c3339bde96d11fd1d7f818cd39e7438e309ffd1Michael Jurka                clipAlpha);
2219508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.setBitmap(null);
2220d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka
2221d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        return b;
2222d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka    }
2223d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka
2224c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    public void startDrag(CellLayout.CellInfo cellInfo) {
2225c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        startDrag(cellInfo, false);
2226c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
2227c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
2228e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    @Override
2229c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    public void startDrag(CellLayout.CellInfo cellInfo, boolean accessible) {
223031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        View child = cellInfo.cell;
2231aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
223231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // Make sure the drag was started by a long press as opposed to a long click.
22337984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert        if (!child.isInTouchMode()) {
223431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            return;
223531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
2236aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
223731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mDragInfo = cellInfo;
2238d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        child.setVisibility(INVISIBLE);
2239482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        CellLayout layout = (CellLayout) child.getParent().getParent();
2240482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        layout.prepareChildForDrag(child);
22414be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
2242c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen        beginDragShared(child, this, accessible);
2243508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal    }
2244508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal
2245b745afbdd75157c73d581b345118cdaff99e912dWinson Chung    public void beginDragShared(View child, DragSource source, boolean accessible) {
2246b745afbdd75157c73d581b345118cdaff99e912dWinson Chung        beginDragShared(child, new Point(), source, accessible);
2247c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen    }
2248c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
2249b745afbdd75157c73d581b345118cdaff99e912dWinson Chung    public void beginDragShared(View child, Point relativeTouchPos, DragSource source,
2250b745afbdd75157c73d581b345118cdaff99e912dWinson Chung            boolean accessible) {
22514be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        child.clearFocus();
22524be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        child.setPressed(false);
22534be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
22544be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        // The outline is used to visualize where the item will land if dropped
2255508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mDragOutline = createDragOutline(child, DRAG_BITMAP_PADDING);
2256ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen
225762d18edcc74ce96e2955048f5e0fc0ce3e4ec275Anjali Koppal        mLauncher.onDragStarted(child);
22584be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        // The drag bitmap follows the touch point around on the screen
225995abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal        AtomicInteger padding = new AtomicInteger(DRAG_BITMAP_PADDING);
2260508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        final Bitmap b = createDragBitmap(child, padding);
22614be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato
22624be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        final int bmpWidth = b.getWidth();
2263eecf02da58adef5486bf0c9ff7127ea891f457a4Winson Chung        final int bmpHeight = b.getHeight();
2264e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
2265307fe23f125cbbd5512ad8d4660025f2ab68f30bAdam Cohen        float scale = mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY);
2266babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        int dragLayerX = Math.round(mTempXY[0] - (bmpWidth - scale * child.getWidth()) / 2);
2267babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        int dragLayerY = Math.round(mTempXY[1] - (bmpHeight - scale * bmpHeight) / 2
226895abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal                        - padding.get() / 2);
2269e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
22702e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen        DeviceProfile grid = mLauncher.getDeviceProfile();
2271b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung        Point dragVisualizeOffset = null;
2272e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        Rect dragRect = null;
2273508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        if (child instanceof BubbleTextView) {
2274b745afbdd75157c73d581b345118cdaff99e912dWinson Chung            BubbleTextView icon = (BubbleTextView) child;
22755f8afe6280eae34620067696173e71943e1a30a3Winson Chung            int iconSize = grid.iconSizePx;
2276e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            int top = child.getPaddingTop();
2277e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            int left = (bmpWidth - iconSize) / 2;
2278e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            int right = left + iconSize;
2279e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            int bottom = top + iconSize;
2280b745afbdd75157c73d581b345118cdaff99e912dWinson Chung            if (icon.isLayoutHorizontal()) {
2281b745afbdd75157c73d581b345118cdaff99e912dWinson Chung                // If the layout is horizontal, then if we are just picking up the icon, then just
2282b745afbdd75157c73d581b345118cdaff99e912dWinson Chung                // use the child position since the icon is top-left aligned.  Otherwise, offset
2283b745afbdd75157c73d581b345118cdaff99e912dWinson Chung                // the drag layer position horizontally so that the icon is under the current
2284b745afbdd75157c73d581b345118cdaff99e912dWinson Chung                // touch position.
2285b745afbdd75157c73d581b345118cdaff99e912dWinson Chung                if (icon.getIcon().getBounds().contains(relativeTouchPos.x, relativeTouchPos.y)) {
2286b745afbdd75157c73d581b345118cdaff99e912dWinson Chung                    dragLayerX = Math.round(mTempXY[0]);
2287b745afbdd75157c73d581b345118cdaff99e912dWinson Chung                } else {
2288b745afbdd75157c73d581b345118cdaff99e912dWinson Chung                    dragLayerX = Math.round(mTempXY[0] + relativeTouchPos.x - (bmpWidth / 2));
2289b745afbdd75157c73d581b345118cdaff99e912dWinson Chung                }
2290b745afbdd75157c73d581b345118cdaff99e912dWinson Chung            }
2291fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen            dragLayerY += top;
2292b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung            // Note: The drag region is used to calculate drag layer offsets, but the
2293b8c69f3c17a40adc2d85e8e996f754c383c293dcWinson Chung            // dragVisualizeOffset in addition to the dragRect (the size) to position the outline.
229495abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal            dragVisualizeOffset = new Point(-padding.get() / 2, padding.get() / 2);
2295e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            dragRect = new Rect(left, top, right, bottom);
22960e4857c302da6adbd45231d7a8bae0289fa33250Adam Cohen        } else if (child instanceof FolderIcon) {
22975f8afe6280eae34620067696173e71943e1a30a3Winson Chung            int previewSize = grid.folderIconSizePx;
229892bce4316422c69ee26e6ece204fbf1499753086Sunny Goyal            dragVisualizeOffset = new Point(-padding.get() / 2,
229992bce4316422c69ee26e6ece204fbf1499753086Sunny Goyal                    padding.get() / 2 - child.getPaddingTop());
23005f8afe6280eae34620067696173e71943e1a30a3Winson Chung            dragRect = new Rect(0, child.getPaddingTop(), child.getWidth(), previewSize);
2301e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        }
2302e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen
23031e9cbfef04366a206d59922c14fe4356b7cca426Winson Chung        // Clear the pressed state if necessary
23041e9cbfef04366a206d59922c14fe4356b7cca426Winson Chung        if (child instanceof BubbleTextView) {
23051e9cbfef04366a206d59922c14fe4356b7cca426Winson Chung            BubbleTextView icon = (BubbleTextView) child;
2306508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal            icon.clearPressedBackground();
23071e9cbfef04366a206d59922c14fe4356b7cca426Winson Chung        }
23081e9cbfef04366a206d59922c14fe4356b7cca426Winson Chung
2309aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal        Object dragObject = child.getTag();
2310aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal        if (!(dragObject instanceof ItemInfo)) {
23112f32ad29075bf666199cc49fe20014e00991d1bbAdam Cohen            String msg = "Drag started with a view that has no tag set. This "
23122f32ad29075bf666199cc49fe20014e00991d1bbAdam Cohen                    + "will cause a crash (issue 11627249) down the line. "
23132f32ad29075bf666199cc49fe20014e00991d1bbAdam Cohen                    + "View: " + child + "  tag: " + child.getTag();
23142f32ad29075bf666199cc49fe20014e00991d1bbAdam Cohen            throw new IllegalStateException(msg);
23152f32ad29075bf666199cc49fe20014e00991d1bbAdam Cohen        }
23162f32ad29075bf666199cc49fe20014e00991d1bbAdam Cohen
23175084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        if (child.getParent() instanceof ShortcutAndWidgetContainer) {
23185084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen            mDragSourceInternal = (ShortcutAndWidgetContainer) child.getParent();
23195084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen        }
23205084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen
2321aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal        DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source,
2322aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal                (ItemInfo) dragObject, DragController.DRAG_ACTION_MOVE, dragVisualizeOffset,
2323aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal                dragRect, scale, accessible);
2324eeb5bbc9409978cc2ae52d48380399fcde3d9f85Winson Chung        dv.setIntrinsicIconScaleFactor(source.getIntrinsicIconScaleFactor());
23255084cbab0eb0c06bd2778e0a48efaf392717cd10Adam Cohen
23264be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        b.recycle();
232794e0d37f53e699364ebff732cf75ab81a577fb91Tony Wickham
2328e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickham        if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
23290f97b7874fe29f36e7a40d2fe4b45d1e0e24fe1bTony Wickham            mLauncher.enterSpringLoadedDragMode();
23300f97b7874fe29f36e7a40d2fe4b45d1e0e24fe1bTony Wickham        }
233131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
233231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2333babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung    public void beginExternalDragShared(View child, DragSource source) {
23342e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen        DeviceProfile grid = mLauncher.getDeviceProfile();
2335babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        int iconSize = grid.iconSizePx;
2336babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung
2337babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        // Notify launcher of drag start
2338babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        mLauncher.onDragStarted(child);
2339babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung
2340babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        // Compose a new drag bitmap that is of the icon size
234195abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal        AtomicInteger padding = new AtomicInteger(DRAG_BITMAP_PADDING);
2342508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        final Bitmap tmpB = createDragBitmap(child, padding);
2343babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        Bitmap b = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
2344babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        Paint p = new Paint();
2345babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        p.setFilterBitmap(true);
2346508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.setBitmap(b);
2347508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.drawBitmap(tmpB, new Rect(0, 0, tmpB.getWidth(), tmpB.getHeight()),
2348babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung                new Rect(0, 0, iconSize, iconSize), p);
2349508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.setBitmap(null);
2350babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung
2351babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        // Find the child's location on the screen
2352babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        int bmpWidth = tmpB.getWidth();
2353babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        float iconScale = (float) bmpWidth / iconSize;
2354babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        float scale = mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY) * iconScale;
2355babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        int dragLayerX = Math.round(mTempXY[0] - (bmpWidth - scale * child.getWidth()) / 2);
2356babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        int dragLayerY = Math.round(mTempXY[1]);
2357babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung
2358babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        // Note: The drag region is used to calculate drag layer offsets, but the
2359babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        // dragVisualizeOffset in addition to the dragRect (the size) to position the outline.
236095abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal        Point dragVisualizeOffset = new Point(-padding.get() / 2, padding.get() / 2);
2361babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        Rect dragRect = new Rect(0, 0, iconSize, iconSize);
2362babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung
2363aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal        Object dragObject = child.getTag();
2364aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal        if (!(dragObject instanceof ItemInfo)) {
2365babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung            String msg = "Drag started with a view that has no tag set. This "
2366babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung                    + "will cause a crash (issue 11627249) down the line. "
2367babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung                    + "View: " + child + "  tag: " + child.getTag();
2368babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung            throw new IllegalStateException(msg);
2369babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        }
2370babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung
2371babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        // Start the drag
2372aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal        DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source,
2373aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal                (ItemInfo) dragObject, DragController.DRAG_ACTION_MOVE, dragVisualizeOffset,
2374aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal                dragRect, scale, false);
2375babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        dv.setIntrinsicIconScaleFactor(source.getIntrinsicIconScaleFactor());
2376babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung
2377babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        // Recycle temporary bitmaps
2378babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung        tmpB.recycle();
237994e0d37f53e699364ebff732cf75ab81a577fb91Tony Wickham
2380e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickham        if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
23810f97b7874fe29f36e7a40d2fe4b45d1e0e24fe1bTony Wickham            mLauncher.enterSpringLoadedDragMode();
23820f97b7874fe29f36e7a40d2fe4b45d1e0e24fe1bTony Wickham        }
2383babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung    }
2384babb53ef8fd0fcba32fcc8fc213663f0f27b8f5eWinson Chung
23854b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    public boolean transitionStateShouldAllowDrop() {
23866c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        return ((!isSwitchingState() || mTransitionProgress > 0.5f) &&
23876c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen                (mState == State.NORMAL || mState == State.SPRING_LOADED));
23884b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    }
23894b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen
2390cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy    /**
2391cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy     * {@inheritDoc}
2392cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy     */
2393cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen    public boolean acceptDrop(DragObject d) {
2394cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy        // If it's an external drop (e.g. from All Apps), check if it should be accepted
2395c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        CellLayout dropTargetLayout = mDropToLayout;
2396cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen        if (d.dragSource != this) {
2397cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy            // Don't accept the drop if we're not over a screen at time of drop
2398c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            if (dropTargetLayout == null) {
2399cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy                return false;
2400cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy            }
24014b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            if (!transitionStateShouldAllowDrop()) return false;
2402cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy
24031587d5362f5a66e005aa36fdfc7082c34b8ea3b7Sunny Goyal            mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter);
2404a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen
24052e9f4fb38e833b18fe403da17b00f830d6a5e86cAdam Cohen            // We want the point to be mapped to the dragTarget.
2406c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            if (mLauncher.isHotseatLayout(dropTargetLayout)) {
2407307fe23f125cbbd5512ad8d4660025f2ab68f30bAdam Cohen                mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter);
24082e9f4fb38e833b18fe403da17b00f830d6a5e86cAdam Cohen            } else {
2409106bf64f4a5b4b7772c971f62b6207b5a73529a2Sunny Goyal                mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter);
24102e9f4fb38e833b18fe403da17b00f830d6a5e86cAdam Cohen            }
24112e9f4fb38e833b18fe403da17b00f830d6a5e86cAdam Cohen
2412557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            int spanX = 1;
2413557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            int spanY = 1;
2414557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            if (mDragInfo != null) {
2415557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                final CellLayout.CellInfo dragCellInfo = mDragInfo;
2416557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                spanX = dragCellInfo.spanX;
2417557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                spanY = dragCellInfo.spanY;
2418557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            } else {
2419aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal                spanX = d.dragInfo.spanX;
2420aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal                spanY = d.dragInfo.spanY;
2421557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            }
2422cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy
2423d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            int minSpanX = spanX;
2424d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            int minSpanY = spanY;
2425d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            if (d.dragInfo instanceof PendingAddWidgetInfo) {
2426d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                minSpanX = ((PendingAddWidgetInfo) d.dragInfo).minSpanX;
2427d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                minSpanY = ((PendingAddWidgetInfo) d.dragInfo).minSpanY;
2428d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            }
2429482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
2430a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
2431c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                    (int) mDragViewVisualCenter[1], minSpanX, minSpanY, dropTargetLayout,
2432f0777b9d7a8f4befa22a36f227a052616152108fAdam Cohen                    mTargetCell);
2433c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0],
2434482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                    mDragViewVisualCenter[1], mTargetCell);
2435aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal            if (mCreateUserFolderOnDrop && willCreateUserFolder(d.dragInfo,
2436ec0d61d79ef50010660b426b259a8ccb14656b31Adam Cohen                    dropTargetLayout, mTargetCell, distance, true)) {
2437a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen                return true;
2438a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            }
2439ec0d61d79ef50010660b426b259a8ccb14656b31Adam Cohen
2440aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal            if (mAddToExistingFolderOnDrop && willAddToExistingUserFolder(d.dragInfo,
2441ec0d61d79ef50010660b426b259a8ccb14656b31Adam Cohen                    dropTargetLayout, mTargetCell, distance)) {
2442a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen                return true;
2443a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            }
2444a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen
2445482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            int[] resultSpan = new int[2];
2446fa3c58f647e603760c0bd929166c0edee25675a9Adam Cohen            mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0],
2447482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                    (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY,
2448482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                    null, mTargetCell, resultSpan, CellLayout.MODE_ACCEPT_DROP);
2449482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0;
2450482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
2451cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy            // Don't accept the drop if there's no room for the item
2452482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            if (!foundCell) {
245396ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                // Don't show the message if we are dropping on the AllApps button and the hotseat
245496ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                // is full
2455c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                boolean isHotseat = mLauncher.isHotseatLayout(dropTargetLayout);
245693eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung                if (mTargetCell != null && isHotseat) {
245796ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                    Hotseat hotseat = mLauncher.getHotseat();
24580dca1ec41479a74f8da080224fa0c7eacab674d6Andrew Flynn                    if (hotseat.isAllAppsButtonRank(
245996ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                            hotseat.getOrderInHotseat(mTargetCell[0], mTargetCell[1]))) {
246096ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                        return false;
246196ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                    }
246296ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                }
246396ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung
246493eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung                mLauncher.showOutOfSpaceMessage(isHotseat);
2465cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy                return false;
2466cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy            }
2467cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy        }
2468dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen
2469dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen        long screenId = getIdForScreen(dropTargetLayout);
2470dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen        if (screenId == EXTRA_EMPTY_SCREEN_ID) {
2471dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen            commitExtraEmptyScreen();
2472dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen        }
2473dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen
2474cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy        return true;
2475cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy    }
2476cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy
2477e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal    boolean willCreateUserFolder(ItemInfo info, CellLayout target, int[] targetCell,
2478e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            float distance, boolean considerTimeout) {
2479482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        if (distance > mMaxDistanceForFolderCreation) return false;
2480a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
2481e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        return willCreateUserFolder(info, dropOverView, considerTimeout);
2482e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal    }
2483a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen
2484e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal    boolean willCreateUserFolder(ItemInfo info, View dropOverView, boolean considerTimeout) {
248519f3792523fe2d55ea791a9286398a6120920690Adam Cohen        if (dropOverView != null) {
248619f3792523fe2d55ea791a9286398a6120920690Adam Cohen            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams();
2487b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal            if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY)) {
248819f3792523fe2d55ea791a9286398a6120920690Adam Cohen                return false;
248919f3792523fe2d55ea791a9286398a6120920690Adam Cohen            }
249019f3792523fe2d55ea791a9286398a6120920690Adam Cohen        }
249119f3792523fe2d55ea791a9286398a6120920690Adam Cohen
24923d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        boolean hasntMoved = false;
24933d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mDragInfo != null) {
2494482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            hasntMoved = dropOverView == mDragInfo.cell;
24953d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
2496e0310965022e7a1adb7ad489505d404186608689Adam Cohen
2497a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        if (dropOverView == null || hasntMoved || (considerTimeout && !mCreateUserFolderOnDrop)) {
2498a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            return false;
2499a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        }
2500e0310965022e7a1adb7ad489505d404186608689Adam Cohen
2501a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        boolean aboveShortcut = (dropOverView.getTag() instanceof ShortcutInfo);
2502e0310965022e7a1adb7ad489505d404186608689Adam Cohen        boolean willBecomeShortcut =
2503c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
2504e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                        info.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT);
2505e0310965022e7a1adb7ad489505d404186608689Adam Cohen
2506e0310965022e7a1adb7ad489505d404186608689Adam Cohen        return (aboveShortcut && willBecomeShortcut);
2507e0310965022e7a1adb7ad489505d404186608689Adam Cohen    }
2508e0310965022e7a1adb7ad489505d404186608689Adam Cohen
2509aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal    boolean willAddToExistingUserFolder(ItemInfo dragInfo, CellLayout target, int[] targetCell,
2510482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            float distance) {
2511482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        if (distance > mMaxDistanceForFolderCreation) return false;
2512a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
2513e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        return willAddToExistingUserFolder(dragInfo, dropOverView);
251419f3792523fe2d55ea791a9286398a6120920690Adam Cohen
2515e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal    }
25169d4380856ff41ecb26c0d5aee1747b6060d2ef0eSunny Goyal    boolean willAddToExistingUserFolder(ItemInfo dragInfo, View dropOverView) {
251719f3792523fe2d55ea791a9286398a6120920690Adam Cohen        if (dropOverView != null) {
251819f3792523fe2d55ea791a9286398a6120920690Adam Cohen            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams();
2519b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal            if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY)) {
252019f3792523fe2d55ea791a9286398a6120920690Adam Cohen                return false;
252119f3792523fe2d55ea791a9286398a6120920690Adam Cohen            }
252219f3792523fe2d55ea791a9286398a6120920690Adam Cohen        }
252319f3792523fe2d55ea791a9286398a6120920690Adam Cohen
2524a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        if (dropOverView instanceof FolderIcon) {
2525a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            FolderIcon fi = (FolderIcon) dropOverView;
2526a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            if (fi.acceptDrop(dragInfo)) {
2527a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen                return true;
2528a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            }
2529a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        }
2530a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        return false;
2531a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen    }
2532a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen
25333d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    boolean createUserFolderIfNecessary(View newView, long container, CellLayout target,
2534482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            int[] targetCell, float distance, boolean external, DragView dragView,
2535482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            Runnable postAnimationRunnable) {
2536482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        if (distance > mMaxDistanceForFolderCreation) return false;
2537c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        View v = target.getChildAt(targetCell[0], targetCell[1]);
253819f3792523fe2d55ea791a9286398a6120920690Adam Cohen
2539ec9a0a4ca5458e65ba2498e4f7eab0ae6e91964aWinson Chung        boolean hasntMoved = false;
2540ec9a0a4ca5458e65ba2498e4f7eab0ae6e91964aWinson Chung        if (mDragInfo != null) {
2541ec9a0a4ca5458e65ba2498e4f7eab0ae6e91964aWinson Chung            CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell);
2542ec9a0a4ca5458e65ba2498e4f7eab0ae6e91964aWinson Chung            hasntMoved = (mDragInfo.cellX == targetCell[0] &&
2543ec9a0a4ca5458e65ba2498e4f7eab0ae6e91964aWinson Chung                    mDragInfo.cellY == targetCell[1]) && (cellParent == target);
2544ec9a0a4ca5458e65ba2498e4f7eab0ae6e91964aWinson Chung        }
254510b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen
254619072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen        if (v == null || hasntMoved || !mCreateUserFolderOnDrop) return false;
254719072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen        mCreateUserFolderOnDrop = false;
2548b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal        final long screenId = getIdForScreen(target);
254910b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen
255010b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen        boolean aboveShortcut = (v.getTag() instanceof ShortcutInfo);
255110b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen        boolean willBecomeShortcut = (newView.getTag() instanceof ShortcutInfo);
255210b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen
255310b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen        if (aboveShortcut && willBecomeShortcut) {
255410b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            ShortcutInfo sourceInfo = (ShortcutInfo) newView.getTag();
255510b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            ShortcutInfo destInfo = (ShortcutInfo) v.getTag();
255610b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            // if the drag started here, we need to remove it from the workspace
255710b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            if (!external) {
25583d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell);
255910b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            }
256010b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen
2561d044526d82e7938cf35b296978d9d5302f98b4afAdam Cohen            Rect folderLocation = new Rect();
2562ac8c87687b8e063c981f28cb31bab2cc110f26ccAdam Cohen            float scale = mLauncher.getDragLayer().getDescendantRectRelativeToSelf(v, folderLocation);
256310b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            target.removeView(v);
2564d044526d82e7938cf35b296978d9d5302f98b4afAdam Cohen
25653d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            FolderIcon fi =
2566dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                mLauncher.addFolder(target, container, screenId, targetCell[0], targetCell[1]);
2567a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            destInfo.cellX = -1;
2568a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            destInfo.cellY = -1;
2569a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            sourceInfo.cellX = -1;
2570a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            sourceInfo.cellY = -1;
2571d044526d82e7938cf35b296978d9d5302f98b4afAdam Cohen
2572558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            // If the dragView is null, we can't animate
2573558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            boolean animate = dragView != null;
2574558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            if (animate) {
2575efca0279eb927faebffc38c8382818df67fcd159Adam Cohen                // In order to keep everything continuous, we hand off the currently rendered
2576efca0279eb927faebffc38c8382818df67fcd159Adam Cohen                // folder background to the newly created icon. This preserves animation state.
2577efca0279eb927faebffc38c8382818df67fcd159Adam Cohen                fi.setFolderBackground(mFolderCreateBg);
2578efca0279eb927faebffc38c8382818df67fcd159Adam Cohen                mFolderCreateBg = new FolderIcon.PreviewBackground();
2579558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                fi.performCreateAnimation(destInfo, v, sourceInfo, dragView, folderLocation, scale,
2580558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                        postAnimationRunnable);
2581558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            } else {
2582558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                fi.addItem(destInfo);
2583558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                fi.addItem(sourceInfo);
2584558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            }
258510b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            return true;
258610b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen        }
258710b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen        return false;
258810b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen    }
258910b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen
2590a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen    boolean addToExistingFolderIfNecessary(View newView, CellLayout target, int[] targetCell,
2591482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            float distance, DragObject d, boolean external) {
2592482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        if (distance > mMaxDistanceForFolderCreation) return false;
2593482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
2594a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
2595c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (!mAddToExistingFolderOnDrop) return false;
2596c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        mAddToExistingFolderOnDrop = false;
259719f3792523fe2d55ea791a9286398a6120920690Adam Cohen
2598c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        if (dropOverView instanceof FolderIcon) {
2599c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            FolderIcon fi = (FolderIcon) dropOverView;
26003e8f811f31a2275e778244427271f77f4116c08bAdam Cohen            if (fi.acceptDrop(d.dragInfo)) {
26013e8f811f31a2275e778244427271f77f4116c08bAdam Cohen                fi.onDrop(d);
2602c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen
2603c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                // if the drag started here, we need to remove it from the workspace
2604c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                if (!external) {
26053d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell);
2606c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                }
2607c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                return true;
2608c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            }
2609c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        }
2610c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen        return false;
2611c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen    }
2612c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen
2613e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    @Override
2614e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    public void prepareAccessibilityDrop() { }
2615e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal
2616d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen    public void onDrop(final DragObject d) {
26171587d5362f5a66e005aa36fdfc7082c34b8ea3b7Sunny Goyal        mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter);
2618c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        CellLayout dropTargetLayout = mDropToLayout;
2619c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
2620e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen        // We want the point to be mapped to the dragTarget.
2621c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (dropTargetLayout != null) {
2622c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            if (mLauncher.isHotseatLayout(dropTargetLayout)) {
2623bb00ff2666d52a0df763a446ad3e398c643d23ddAdam Cohen                mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter);
26243d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            } else {
2625106bf64f4a5b4b7772c971f62b6207b5a73529a2Sunny Goyal                mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter);
26263d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
2627ba781611b3e2fb1c42daa7167c1079b90b100efaAdam Cohen        }
2628c6ee42e25f203e408826e7eab4ad8faf67ed2ff9Michael Jurka
2629ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal        int snapScreen = -1;
2630d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        boolean resizeOnDrop = false;
2631cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen        if (d.dragSource != this) {
2632e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0],
2633e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen                    (int) mDragViewVisualCenter[1] };
26343e8f811f31a2275e778244427271f77f4116c08bAdam Cohen            onDropExternal(touchXY, d.dragInfo, dropTargetLayout, false, d);
2635ce34a9768b01115def95f000a6a8f35870f10d3aPatrick Dubroy        } else if (mDragInfo != null) {
2636ce34a9768b01115def95f000a6a8f35870f10d3aPatrick Dubroy            final View cell = mDragInfo.cell;
263754fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy
26381e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            if (dropTargetLayout != null && !d.cancelled) {
2639cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy                // Move internally
264040e882bf19a837ee62ec328c5748b75be7960d39Winson Chung                boolean hasMovedLayouts = (getParentCellLayoutForView(cell) != dropTargetLayout);
264140e882bf19a837ee62ec328c5748b75be7960d39Winson Chung                boolean hasMovedIntoHotseat = mLauncher.isHotseatLayout(dropTargetLayout);
264240e882bf19a837ee62ec328c5748b75be7960d39Winson Chung                long container = hasMovedIntoHotseat ?
26433d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        LauncherSettings.Favorites.CONTAINER_HOTSEAT :
26443d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        LauncherSettings.Favorites.CONTAINER_DESKTOP;
2645dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                long screenId = (mTargetCell[0] < 0) ?
2646dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        mDragInfo.screenId : getIdForScreen(dropTargetLayout);
2647c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                int spanX = mDragInfo != null ? mDragInfo.spanX : 1;
2648c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                int spanY = mDragInfo != null ? mDragInfo.spanY : 1;
2649c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                // First we find the cell nearest to point at which the item is
2650c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                // dropped, without any consideration to whether there is an item there.
2651482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
2652c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int)
2653c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                        mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell);
2654482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0],
2655482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                        mDragViewVisualCenter[1], mTargetCell);
2656482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
2657c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                // If the item being dropped is a shortcut and the nearest drop
265876078c4ef35b6e95fb6d8129a32cf52a36625e28Adam Cohen                // cell also contains a shortcut, then create a folder with the two shortcuts.
26591c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung                if (!mInScrollArea && createUserFolderIfNecessary(cell, container,
2660482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                        dropTargetLayout, mTargetCell, distance, false, d.dragView, null)) {
2661c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                    return;
2662c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                }
2663c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen
2664482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                if (addToExistingFolderIfNecessary(cell, dropTargetLayout, mTargetCell,
2665482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                        distance, d, false)) {
2666df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                    return;
2667df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                }
2668df0353815c629fc678824b07a234b89a1ff94208Adam Cohen
2669df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                // Aside from the special case where we're dropping a shortcut onto a shortcut,
2670df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                // we need to find the nearest cell location that is vacant
2671aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal                ItemInfo item = d.dragInfo;
2672d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                int minSpanX = item.spanX;
2673d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                int minSpanY = item.spanY;
2674d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                if (item.minSpanX > 0 && item.minSpanY > 0) {
2675d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    minSpanX = item.minSpanX;
2676d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    minSpanY = item.minSpanY;
2677d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                }
2678482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
2679d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                int[] resultSpan = new int[2];
2680fa3c58f647e603760c0bd929166c0edee25675a9Adam Cohen                mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0],
2681482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                        (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, cell,
2682482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                        mTargetCell, resultSpan, CellLayout.MODE_ON_DROP);
2683482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
2684d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0;
26859e05a5ea951b4d5ffce324da8dd0656353306e6fAdam Cohen
2686aaa5c21ee2e03def644fb6ba8eadbb59873ddd45Adam Cohen                // if the widget resizes on drop
26879e05a5ea951b4d5ffce324da8dd0656353306e6fAdam Cohen                if (foundCell && (cell instanceof AppWidgetHostView) &&
2688aaa5c21ee2e03def644fb6ba8eadbb59873ddd45Adam Cohen                        (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY)) {
2689d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    resizeOnDrop = true;
2690d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    item.spanX = resultSpan[0];
2691d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    item.spanY = resultSpan[1];
26927bdfc9700b1cad043c04c757f134db1bf3df00daAdam Cohen                    AppWidgetHostView awhv = (AppWidgetHostView) cell;
26937bdfc9700b1cad043c04c757f134db1bf3df00daAdam Cohen                    AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, resultSpan[0],
26947bdfc9700b1cad043c04c757f134db1bf3df00daAdam Cohen                            resultSpan[1]);
2695d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                }
2696df0353815c629fc678824b07a234b89a1ff94208Adam Cohen
2697dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                if (getScreenIdForPageIndex(mCurrentPage) != screenId && !hasMovedIntoHotseat) {
2698dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    snapScreen = getPageIndexForScreenId(screenId);
2699dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    snapToPage(snapScreen);
270054fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy                }
270154fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy
2702d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                if (foundCell) {
2703d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    final ItemInfo info = (ItemInfo) cell.getTag();
270440e882bf19a837ee62ec328c5748b75be7960d39Winson Chung                    if (hasMovedLayouts) {
27059438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                        // Reparent the view
270625611b181f19a302fc6fa1aa1489ee1585c11316Sunny Goyal                        CellLayout parentCell = getParentCellLayoutForView(cell);
270725611b181f19a302fc6fa1aa1489ee1585c11316Sunny Goyal                        if (parentCell != null) {
270825611b181f19a302fc6fa1aa1489ee1585c11316Sunny Goyal                            parentCell.removeView(cell);
27096c56c68555dcb3d99b05d5faecd582a1f683cc92Sunny Goyal                        } else if (ProviderConfig.IS_DOGFOOD_BUILD) {
271025611b181f19a302fc6fa1aa1489ee1585c11316Sunny Goyal                            throw new NullPointerException("mDragInfo.cell has null parent");
271125611b181f19a302fc6fa1aa1489ee1585c11316Sunny Goyal                        }
2712dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        addInScreen(cell, container, screenId, mTargetCell[0], mTargetCell[1],
2713d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                                info.spanX, info.spanY);
27149438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                    }
2715cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy
27169438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                    // update the item's position after drop
27179438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                    CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
271819f3792523fe2d55ea791a9286398a6120920690Adam Cohen                    lp.cellX = lp.tmpCellX = mTargetCell[0];
271919f3792523fe2d55ea791a9286398a6120920690Adam Cohen                    lp.cellY = lp.tmpCellY = mTargetCell[1];
2720d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    lp.cellHSpan = item.spanX;
2721d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    lp.cellVSpan = item.spanY;
272219f3792523fe2d55ea791a9286398a6120920690Adam Cohen                    lp.isLockedToGrid = true;
27239438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy
27243d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
27253d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                            cell instanceof LauncherAppWidgetHostView) {
2726d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                        final CellLayout cellLayout = dropTargetLayout;
2727d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                        // We post this call so that the widget has a chance to be placed
2728d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                        // in its final location
2729d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
2730d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                        final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell;
27312434d40ef271966096fea707ac4207d80d1b2901Sunny Goyal                        AppWidgetProviderInfo pInfo = hostView.getAppWidgetInfo();
2732c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen                        if (pInfo != null && pInfo.resizeMode != AppWidgetProviderInfo.RESIZE_NONE
2733c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen                                && !d.accessibleDrag) {
2734e03856267544d2209c2844c9c06c74e8e52ff104Tony Wickham                            mDelayedResizeRunnable = new Runnable() {
2735d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                                public void run() {
2736e03856267544d2209c2844c9c06c74e8e52ff104Tony Wickham                                    if (!isPageMoving() && !mIsSwitchingState) {
2737e03856267544d2209c2844c9c06c74e8e52ff104Tony Wickham                                        DragLayer dragLayer = mLauncher.getDragLayer();
2738e03856267544d2209c2844c9c06c74e8e52ff104Tony Wickham                                        dragLayer.addResizeFrame(info, hostView, cellLayout);
273926976d9b53161f80faf822d482ac771b8621cf31Adam Cohen                                    }
274026976d9b53161f80faf822d482ac771b8621cf31Adam Cohen                                }
2741e03856267544d2209c2844c9c06c74e8e52ff104Tony Wickham                            };
2742d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                        }
2743d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen                    }
2744d4844c3e731b00547a31f23a00f8bd4a271e2b62Adam Cohen
2745949debe6e64034be1625ec21de100ea7e68545e2Adam Cohen                    LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId, lp.cellX,
2746949debe6e64034be1625ec21de100ea7e68545e2Adam Cohen                            lp.cellY, item.spanX, item.spanY);
2747d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                } else {
2748d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    // If we can't find a drop location, we return the item to its original position
2749d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
2750d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    mTargetCell[0] = lp.cellX;
2751d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    mTargetCell[1] = lp.cellY;
2752f1dcdf61132a6321d0c1c0eb670c6cf1493ab981Adam Cohen                    CellLayout layout = (CellLayout) cell.getParent().getParent();
2753f1dcdf61132a6321d0c1c0eb670c6cf1493ab981Adam Cohen                    layout.markCellsAsOccupiedForView(cell);
27549438336724b4f8b54c7cdc49c5f01dd9568926ebPatrick Dubroy                }
275531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
2756cd68ff5b88de9b5136ff5a9ef698e4db2fc5db66Patrick Dubroy
27578c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            final CellLayout parent = (CellLayout) cell.getParent().getParent();
2758ce34a9768b01115def95f000a6a8f35870f10d3aPatrick Dubroy            // Prepare it to be animated into its new position
2759ce34a9768b01115def95f000a6a8f35870f10d3aPatrick Dubroy            // This must be called after the view has been re-parented
2760d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            final Runnable onCompleteRunnable = new Runnable() {
2761d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                @Override
2762d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                public void run() {
2763d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                    mAnimatingViewIntoPlace = false;
27643a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka                    updateChildrenLayersEnabled(false);
2765d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                }
2766d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            };
2767d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka            mAnimatingViewIntoPlace = true;
2768fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen            if (d.dragView.hasDrawn()) {
2769d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                final ItemInfo info = (ItemInfo) cell.getTag();
27705940042d39b576553c2499bcf3d0641281e6ad52Adam Cohen                boolean isWidget = info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET
27715940042d39b576553c2499bcf3d0641281e6ad52Adam Cohen                        || info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
27725940042d39b576553c2499bcf3d0641281e6ad52Adam Cohen                if (isWidget) {
2773d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    int animationType = resizeOnDrop ? ANIMATE_INTO_POSITION_AND_RESIZE :
2774d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                            ANIMATE_INTO_POSITION_AND_DISAPPEAR;
2775d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    animateWidgetDrop(info, parent, d.dragView,
2776d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                            onCompleteRunnable, animationType, cell, false);
2777d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                } else {
2778ce5a7e5ee4c880081faef3a508f68725d0317ee9Sunny Goyal                    int duration = snapScreen < 0 ? -1 : ADJACENT_SCREEN_DROP_DURATION;
277985b467bf3f7a483512ca8485c9c18254d460b079Adam Cohen                    mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration,
278085b467bf3f7a483512ca8485c9c18254d460b079Adam Cohen                            onCompleteRunnable, this);
2781d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                }
2782fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen            } else {
27837bd1bbb509f9569fa18d6b4d33242679fd98bc9bWinson Chung                d.deferDragViewCleanupPostAnimation = false;
2784fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen                cell.setVisibility(VISIBLE);
2785fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen            }
2786716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen            parent.onDropChild(cell);
278731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
278831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
278931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
279041d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal    /**
279141d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal     * Computes the area relative to dragLayer which is used to display a page.
279241d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal     */
279341d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal    public void getPageAreaRelativeToDragLayer(Rect outArea) {
279441d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        CellLayout child = (CellLayout) getChildAt(getNextPage());
279541d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        if (child == null) {
279641d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal            return;
2797ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen        }
279841d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        ShortcutAndWidgetContainer boundingLayout = child.getShortcutsAndWidgets();
2799ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen
280041d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        // Use the absolute left instead of the child left, as we want the visible area
280141d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        // irrespective of the visible child. Since the view can only scroll horizontally, the
280241d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        // top position is not affected.
280341d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        mTempXY[0] = getViewportOffsetX() + getPaddingLeft() + boundingLayout.getLeft();
280441d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        mTempXY[1] = child.getTop() + boundingLayout.getTop();
280541d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal
280641d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        float scale = mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY);
280741d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        outArea.set(mTempXY[0], mTempXY[1],
280841d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal                (int) (mTempXY[0] + scale * boundingLayout.getMeasuredWidth()),
280941d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal                (int) (mTempXY[1] + scale * boundingLayout.getMeasuredHeight()));
2810ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen    }
2811ed51cc9a12c50d12e7d30ed209a49f1a40858043Adam Cohen
281258376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal    @Override
2813cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen    public void onDragEnter(DragObject d) {
281458376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        if (ENFORCE_DRAG_EVENT_ORDER) {
281558376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal            enfoceDragParity("onDragEnter", 1, 1);
281658376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        }
281758376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal
2818c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        mCreateUserFolderOnDrop = false;
2819c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        mAddToExistingFolderOnDrop = false;
2820c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
2821c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        mDropToLayout = null;
2822c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        CellLayout layout = getCurrentDropLayout();
2823c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        setCurrentDropLayout(layout);
2824c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        setCurrentDragOverlappingLayout(layout);
28250f97b7874fe29f36e7a40d2fe4b45d1e0e24fe1bTony Wickham
2826e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickham        if (!workspaceInModalState() && FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
28270f97b7874fe29f36e7a40d2fe4b45d1e0e24fe1bTony Wickham            mLauncher.getDragLayer().showPageHints();
28280f97b7874fe29f36e7a40d2fe4b45d1e0e24fe1bTony Wickham        }
2829c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    }
2830b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung
283158376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal    @Override
2832c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    public void onDragExit(DragObject d) {
283358376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        if (ENFORCE_DRAG_EVENT_ORDER) {
283458376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal            enfoceDragParity("onDragExit", -1, 0);
283558376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        }
2836fd3385fe9e0f034b04f99d5d59a58d74fe040da4Winson Chung
2837c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        // Here we store the final page that will be dropped to, if the workspace in fact
2838c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        // receives the drop
2839c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (mInScrollArea) {
2840cc1cfe4b0c521a2add64f851a32f39be2b0327a6Winson Chung            if (isPageMoving()) {
2841cc1cfe4b0c521a2add64f851a32f39be2b0327a6Winson Chung                // If the user drops while the page is scrolling, we should use that page as the
2842cc1cfe4b0c521a2add64f851a32f39be2b0327a6Winson Chung                // destination instead of the page that is being hovered over.
2843cc1cfe4b0c521a2add64f851a32f39be2b0327a6Winson Chung                mDropToLayout = (CellLayout) getPageAt(getNextPage());
2844cc1cfe4b0c521a2add64f851a32f39be2b0327a6Winson Chung            } else {
2845cc1cfe4b0c521a2add64f851a32f39be2b0327a6Winson Chung                mDropToLayout = mDragOverlappingLayout;
2846cc1cfe4b0c521a2add64f851a32f39be2b0327a6Winson Chung            }
2847c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        } else {
2848c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDropToLayout = mDragTargetLayout;
2849c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        }
2850c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
2851c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (mDragMode == DRAG_MODE_CREATE_FOLDER) {
2852c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mCreateUserFolderOnDrop = true;
2853c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        } else if (mDragMode == DRAG_MODE_ADD_TO_FOLDER) {
2854c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mAddToExistingFolderOnDrop = true;
2855482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        }
2856482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
2857c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // Reset the scroll area and previous drag target
2858c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        onResetScrollArea();
2859c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        setCurrentDropLayout(null);
2860c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        setCurrentDragOverlappingLayout(null);
2861c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
286274c28d1a6cf13fdfa55ff9518d2b27be10d01f6eAdam Cohen        mSpringLoadedDragController.cancel();
28630f97b7874fe29f36e7a40d2fe4b45d1e0e24fe1bTony Wickham
28640f97b7874fe29f36e7a40d2fe4b45d1e0e24fe1bTony Wickham        mLauncher.getDragLayer().hidePageHints();
2865c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    }
2866c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
286758376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal    private void enfoceDragParity(String event, int update, int expectedValue) {
286858376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        enfoceDragParity(this, event, update, expectedValue);
286958376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        for (int i = 0; i < getChildCount(); i++) {
287058376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal            enfoceDragParity(getChildAt(i), event, update, expectedValue);
287158376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        }
287258376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal    }
287358376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal
287458376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal    private void enfoceDragParity(View v, String event, int update, int expectedValue) {
287558376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        Object tag = v.getTag(R.id.drag_event_parity);
287658376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        int value = tag == null ? 0 : (Integer) tag;
287758376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        value += update;
287858376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        v.setTag(R.id.drag_event_parity, value);
287958376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal
288058376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        if (value != expectedValue) {
288158376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal            Log.e(TAG, event + ": Drag contract violated: " + value);
288258376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal        }
288358376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal    }
288458376925f9596c23f5c9ffa6c99630dfddcfce9cSunny Goyal
2885c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    void setCurrentDropLayout(CellLayout layout) {
2886c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (mDragTargetLayout != null) {
2887c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDragTargetLayout.revertTempState();
2888c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDragTargetLayout.onDragExit();
2889c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        }
2890c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        mDragTargetLayout = layout;
2891c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (mDragTargetLayout != null) {
2892c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDragTargetLayout.onDragEnter();
2893c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        }
2894c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        cleanupReorder(true);
2895c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        cleanupFolderCreation();
2896c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        setCurrentDropOverCell(-1, -1);
2897c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    }
2898c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
2899c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    void setCurrentDragOverlappingLayout(CellLayout layout) {
2900c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (mDragOverlappingLayout != null) {
2901c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDragOverlappingLayout.setIsDragOverlapping(false);
2902c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        }
2903c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        mDragOverlappingLayout = layout;
2904c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (mDragOverlappingLayout != null) {
2905c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDragOverlappingLayout.setIsDragOverlapping(true);
2906c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        }
290795cdb3a0211650e76adb451588ce89fd585b178fTony Wickham        // Invalidating the scrim will also force this CellLayout
290895cdb3a0211650e76adb451588ce89fd585b178fTony Wickham        // to be invalidated so that it is highlighted if necessary.
290995cdb3a0211650e76adb451588ce89fd585b178fTony Wickham        mLauncher.getDragLayer().invalidateScrim();
291095cdb3a0211650e76adb451588ce89fd585b178fTony Wickham    }
291195cdb3a0211650e76adb451588ce89fd585b178fTony Wickham
291295cdb3a0211650e76adb451588ce89fd585b178fTony Wickham    public CellLayout getCurrentDragOverlappingLayout() {
291395cdb3a0211650e76adb451588ce89fd585b178fTony Wickham        return mDragOverlappingLayout;
2914c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    }
2915c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
2916c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    void setCurrentDropOverCell(int x, int y) {
2917c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (x != mDragOverX || y != mDragOverY) {
2918c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDragOverX = x;
2919c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDragOverY = y;
2920c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            setDragMode(DRAG_MODE_NONE);
2921c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        }
2922c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    }
2923c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
2924c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    void setDragMode(int dragMode) {
2925c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (dragMode != mDragMode) {
2926c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            if (dragMode == DRAG_MODE_NONE) {
2927c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                cleanupAddToFolder();
2928c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                // We don't want to cancel the re-order alarm every time the target cell changes
2929c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                // as this feels to slow / unresponsive.
2930c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                cleanupReorder(false);
2931c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                cleanupFolderCreation();
2932c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            } else if (dragMode == DRAG_MODE_ADD_TO_FOLDER) {
2933c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                cleanupReorder(true);
2934c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                cleanupFolderCreation();
2935c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            } else if (dragMode == DRAG_MODE_CREATE_FOLDER) {
2936c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                cleanupAddToFolder();
2937c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                cleanupReorder(true);
2938c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            } else if (dragMode == DRAG_MODE_REORDER) {
2939c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                cleanupAddToFolder();
2940c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                cleanupFolderCreation();
2941c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            }
2942c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDragMode = dragMode;
2943c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        }
2944c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    }
2945c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
2946c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    private void cleanupFolderCreation() {
2947f172b747c24f28e29baaaf58f08bab48847b7a40Adam Cohen        if (mFolderCreateBg != null) {
2948f172b747c24f28e29baaaf58f08bab48847b7a40Adam Cohen            mFolderCreateBg.animateToRest();
2949f172b747c24f28e29baaaf58f08bab48847b7a40Adam Cohen        }
29508ec05f9fa47f28740f92905a8614118a93835789Adam Cohen        mFolderCreationAlarm.setOnAlarmListener(null);
2951c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        mFolderCreationAlarm.cancelAlarm();
2952c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    }
2953c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
2954c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    private void cleanupAddToFolder() {
2955c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (mDragOverFolderIcon != null) {
2956c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDragOverFolderIcon.onDragExit(null);
2957c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDragOverFolderIcon = null;
2958c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        }
2959c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    }
2960c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
2961c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen    private void cleanupReorder(boolean cancelAlarm) {
2962c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        // Any pending reorders are canceled
2963c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (cancelAlarm) {
2964c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mReorderAlarm.cancelAlarm();
2965c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        }
2966c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        mLastReorderX = -1;
2967c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        mLastReorderY = -1;
296831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
296931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
29704516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka   /*
29714516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    *
29724516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's
29734516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    * coordinate space. The argument xy is modified with the return result.
29744516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    */
2975106bf64f4a5b4b7772c971f62b6207b5a73529a2Sunny Goyal   void mapPointFromSelfToChild(View v, float[] xy) {
29767d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen       xy[0] = xy[0] - v.getLeft();
29777d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen       xy[1] = xy[1] - v.getTop();
29787d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen   }
29797d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen
2980cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal   boolean isPointInSelfOverHotseat(int x, int y) {
2981cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal       mTempXY[0] = x;
2982cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal       mTempXY[1] = y;
2983cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal       mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY, true);
2984cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal       return mLauncher.getDeviceProfile().isInHotseatRect(mTempXY[0], mTempXY[1]);
29854516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka   }
29864516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
2987c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen   void mapPointFromSelfToHotseatLayout(Hotseat hotseat, float[] xy) {
2988cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal       mTempXY[0] = (int) xy[0];
2989cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal       mTempXY[1] = (int) xy[1];
2990cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal       mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY, true);
2991cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal       mLauncher.getDragLayer().mapCoordInSelfToDescendent(hotseat.getLayout(), mTempXY);
29927d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen
2993cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal       xy[0] = mTempXY[0];
2994cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal       xy[1] = mTempXY[1];
2995c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen   }
2996c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
29973d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung   /*
29984516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    *
29994516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    * Convert the 2D coordinate xy from this CellLayout's coordinate space to
30004516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    * the parent View's coordinate space. The argument xy is modified with the return result.
30014516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    *
30024516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    */
30034516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka   void mapPointFromChildToSelf(View v, float[] xy) {
30047d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen       xy[0] += v.getLeft();
30057d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen       xy[1] += v.getTop();
30064516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka   }
30074516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
3008e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen   static private float squaredDistance(float[] point1, float[] point2) {
30094516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        float distanceX = point1[0] - point2[0];
30104516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        float distanceY = point2[1] - point2[1];
30114516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        return distanceX * distanceX + distanceY * distanceY;
3012e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen   }
30130280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
30144516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    /*
30154516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     *
30164516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     * This method returns the CellLayout that is currently being dragged to. In order to drag
30174516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second
30184516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one
30194516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     *
30204516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     * Return null if no CellLayout is currently being dragged over
30214516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     *
30224516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka     */
30234516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka    private CellLayout findMatchingPageForDragOver(
302400618753e9f0ad941c13efb0b6804e4a06ecb076Adam Cohen            DragView dragView, float originX, float originY, boolean exact) {
30254516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        // We loop through all the screens (ie CellLayouts) and see which ones overlap
30264516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka        // with the item being dragged and then choose the one that's closest to the touch point
3027a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        final int screenCount = getChildCount();
3028a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        CellLayout bestMatchingScreen = null;
30290280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        float smallestDistSoFar = Float.MAX_VALUE;
30304516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
3031a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        for (int i = 0; i < screenCount; i++) {
3032df041bc7ad99a6d33a5dd83a5dff28a617fedf12Adam Cohen            // The custom content screen is not a valid drag over option
3033df041bc7ad99a6d33a5dd83a5dff28a617fedf12Adam Cohen            if (mScreenOrder.get(i) == CUSTOM_CONTENT_SCREEN_ID) {
3034df041bc7ad99a6d33a5dd83a5dff28a617fedf12Adam Cohen                continue;
3035df041bc7ad99a6d33a5dd83a5dff28a617fedf12Adam Cohen            }
3036df041bc7ad99a6d33a5dd83a5dff28a617fedf12Adam Cohen
30373d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            CellLayout cl = (CellLayout) getChildAt(i);
3038a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
303900618753e9f0ad941c13efb0b6804e4a06ecb076Adam Cohen            final float[] touchXy = {originX, originY};
3040106bf64f4a5b4b7772c971f62b6207b5a73529a2Sunny Goyal            mapPointFromSelfToChild(cl, touchXy);
30414516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
30424516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() &&
30434516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                    touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) {
30444516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                return cl;
30454516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            }
30464516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
304796ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung            if (!exact) {
30484516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                // Get the center of the cell layout in screen coordinates
30494516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates;
30504516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                cellLayoutCenter[0] = cl.getWidth()/2;
30514516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                cellLayoutCenter[1] = cl.getHeight()/2;
30524516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                mapPointFromChildToSelf(cl, cellLayoutCenter);
30534516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
305400618753e9f0ad941c13efb0b6804e4a06ecb076Adam Cohen                touchXy[0] = originX;
305500618753e9f0ad941c13efb0b6804e4a06ecb076Adam Cohen                touchXy[1] = originY;
30564516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
30574516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                // Calculate the distance between the center of the CellLayout
30584516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                // and the touch point
30594516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                float dist = squaredDistance(touchXy, cellLayoutCenter);
30604516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka
30614516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka                if (dist < smallestDistSoFar) {
30620280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    smallestDistSoFar = dist;
3063a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                    bestMatchingScreen = cl;
3064a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka                }
30654516c11039d77066281f69f9b5d30fdaf1bc05aeMichael Jurka            }
3066a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        }
3067a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        return bestMatchingScreen;
3068a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka    }
3069a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
3070ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung    private boolean isDragWidget(DragObject d) {
3071ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung        return (d.dragInfo instanceof LauncherAppWidgetInfo ||
3072ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung                d.dragInfo instanceof PendingAddWidgetInfo);
3073ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung    }
3074ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung    private boolean isExternalDragWidget(DragObject d) {
3075ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung        return d.dragSource != this && isDragWidget(d);
3076ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung    }
3077ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung
3078cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen    public void onDragOver(DragObject d) {
3079c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // Skip drag over events while we are dragging over side pages
30806c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        if (mInScrollArea || !transitionStateShouldAllowDrop()) return;
3081c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
30823d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        CellLayout layout = null;
3083aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal        ItemInfo item = d.dragInfo;
308425611b181f19a302fc6fa1aa1489ee1585c11316Sunny Goyal        if (item == null) {
30856c56c68555dcb3d99b05d5faecd582a1f683cc92Sunny Goyal            if (ProviderConfig.IS_DOGFOOD_BUILD) {
308625611b181f19a302fc6fa1aa1489ee1585c11316Sunny Goyal                throw new NullPointerException("DragObject has null info");
308725611b181f19a302fc6fa1aa1489ee1585c11316Sunny Goyal            }
308825611b181f19a302fc6fa1aa1489ee1585c11316Sunny Goyal            return;
308925611b181f19a302fc6fa1aa1489ee1585c11316Sunny Goyal        }
3090c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
3091c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // Ensure that we have proper spans for the item that we are dropping
3092c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        if (item.spanX < 0 || item.spanY < 0) throw new RuntimeException("Improper spans found");
30931587d5362f5a66e005aa36fdfc7082c34b8ea3b7Sunny Goyal        mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter);
309400618753e9f0ad941c13efb0b6804e4a06ecb076Adam Cohen
3095482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        final View child = (mDragInfo == null) ? null : mDragInfo.cell;
3096c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // Identify whether we have dragged over a side page
30976c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        if (workspaceInModalState()) {
3098ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung            if (mLauncher.getHotseat() != null && !isExternalDragWidget(d)) {
3099cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal                if (isPointInSelfOverHotseat(d.x, d.y)) {
31004afe9b31f51022538b30646fe83780e83bf22490Winson Chung                    layout = mLauncher.getHotseat().getLayout();
31014afe9b31f51022538b30646fe83780e83bf22490Winson Chung                }
31024afe9b31f51022538b30646fe83780e83bf22490Winson Chung            }
31034afe9b31f51022538b30646fe83780e83bf22490Winson Chung            if (layout == null) {
310496ef409ebdc798a82786e5dcecfd7d897be0ed30Winson Chung                layout = findMatchingPageForDragOver(d.dragView, d.x, d.y, false);
31054afe9b31f51022538b30646fe83780e83bf22490Winson Chung            }
3106c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            if (layout != mDragTargetLayout) {
3107c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                setCurrentDropLayout(layout);
3108c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                setCurrentDragOverlappingLayout(layout);
3109c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
3110d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka                boolean isInSpringLoadedMode = (mState == State.SPRING_LOADED);
3111c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                if (isInSpringLoadedMode) {
31124afe9b31f51022538b30646fe83780e83bf22490Winson Chung                    if (mLauncher.isHotseatLayout(layout)) {
31134afe9b31f51022538b30646fe83780e83bf22490Winson Chung                        mSpringLoadedDragController.cancel();
31144afe9b31f51022538b30646fe83780e83bf22490Winson Chung                    } else {
31154afe9b31f51022538b30646fe83780e83bf22490Winson Chung                        mSpringLoadedDragController.setAlarm(mDragTargetLayout);
31164afe9b31f51022538b30646fe83780e83bf22490Winson Chung                    }
3117d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka                }
3118d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka            }
3119c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        } else {
31203d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            // Test to see if we are over the hotseat otherwise just use the current page
3121ea359c6aee44c0fe3bb94f7002c3b49208b32b7fWinson Chung            if (mLauncher.getHotseat() != null && !isDragWidget(d)) {
3122cf25b52763f3fa376aee7c4a63b5a064ca1c0e2aSunny Goyal                if (isPointInSelfOverHotseat(d.x, d.y)) {
31233d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    layout = mLauncher.getHotseat().getLayout();
31243d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                }
31253d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
31263d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            if (layout == null) {
31273d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                layout = getCurrentDropLayout();
31283d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
3129c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            if (layout != mDragTargetLayout) {
3130c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                setCurrentDropLayout(layout);
3131c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                setCurrentDragOverlappingLayout(layout);
3132c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            }
3133c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        }
3134a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
3135c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        // Handle the drag over
3136c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        if (mDragTargetLayout != null) {
3137c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            // We want the point to be mapped to the dragTarget.
31383d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            if (mLauncher.isHotseatLayout(mDragTargetLayout)) {
3139c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter);
31403d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            } else {
3141106bf64f4a5b4b7772c971f62b6207b5a73529a2Sunny Goyal                mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter);
31423d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
3143c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
314474c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen            int minSpanX = item.spanX;
314574c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen            int minSpanY = item.spanY;
314674c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen            if (item.minSpanX > 0 && item.minSpanY > 0) {
314774c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen                minSpanX = item.minSpanX;
314874c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen                minSpanY = item.minSpanY;
314974c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen            }
315074c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen
3151c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
315274c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen                    (int) mDragViewVisualCenter[1], minSpanX, minSpanY,
3153d024f9845a0974ab525baad085f316031cd5a742Adam Cohen                    mDragTargetLayout, mTargetCell);
315474c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen            int reorderX = mTargetCell[0];
315574c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen            int reorderY = mTargetCell[1];
3156c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
3157c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            setCurrentDropOverCell(mTargetCell[0], mTargetCell[1]);
3158c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
3159482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            float targetCellDistance = mDragTargetLayout.getDistanceFromCell(
3160482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                    mDragViewVisualCenter[0], mDragViewVisualCenter[1], mTargetCell);
3161482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
3162e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            manageFolderFeedback(mDragTargetLayout, mTargetCell, targetCellDistance, d);
3163c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
3164c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            boolean nearestDropOccupied = mDragTargetLayout.isNearestDropLocationOccupied((int)
3165c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                    mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], item.spanX,
3166c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                    item.spanY, child, mTargetCell);
3167c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
3168c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            if (!nearestDropOccupied) {
316919f3792523fe2d55ea791a9286398a6120920690Adam Cohen                mDragTargetLayout.visualizeDropLocation(child, mDragOutline,
3170e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                        mTargetCell[0], mTargetCell[1], item.spanX, item.spanY, false, d);
3171c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            } else if ((mDragMode == DRAG_MODE_NONE || mDragMode == DRAG_MODE_REORDER)
317274c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen                    && !mReorderAlarm.alarmPending() && (mLastReorderX != reorderX ||
317374c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen                    mLastReorderY != reorderY)) {
3174d024f9845a0974ab525baad085f316031cd5a742Adam Cohen
3175fa3c58f647e603760c0bd929166c0edee25675a9Adam Cohen                int[] resultSpan = new int[2];
3176fa3c58f647e603760c0bd929166c0edee25675a9Adam Cohen                mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0],
3177fa3c58f647e603760c0bd929166c0edee25675a9Adam Cohen                        (int) mDragViewVisualCenter[1], minSpanX, minSpanY, item.spanX, item.spanY,
3178fa3c58f647e603760c0bd929166c0edee25675a9Adam Cohen                        child, mTargetCell, resultSpan, CellLayout.MODE_SHOW_REORDER_HINT);
3179fa3c58f647e603760c0bd929166c0edee25675a9Adam Cohen
318019f3792523fe2d55ea791a9286398a6120920690Adam Cohen                // Otherwise, if we aren't adding to or creating a folder and there's no pending
318119f3792523fe2d55ea791a9286398a6120920690Adam Cohen                // reorder, then we schedule a reorder
3182482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                ReorderAlarmListener listener = new ReorderAlarmListener(mDragViewVisualCenter,
3183e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                        minSpanX, minSpanY, item.spanX, item.spanY, d, child);
3184482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                mReorderAlarm.setOnAlarmListener(listener);
3185482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                mReorderAlarm.setAlarm(REORDER_TIMEOUT);
3186c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            }
3187c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
3188c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            if (mDragMode == DRAG_MODE_CREATE_FOLDER || mDragMode == DRAG_MODE_ADD_TO_FOLDER ||
3189c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                    !nearestDropOccupied) {
3190c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                if (mDragTargetLayout != null) {
3191c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                    mDragTargetLayout.revertTempState();
3192d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                }
3193482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            }
3194c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        }
3195c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    }
3196c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
3197e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal    private void manageFolderFeedback(CellLayout targetLayout,
3198e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            int[] targetCell, float distance, DragObject dragObject) {
3199e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        if (distance > mMaxDistanceForFolderCreation) return;
3200e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal
3201e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        final View dragOverView = mDragTargetLayout.getChildAt(mTargetCell[0], mTargetCell[1]);
32029d4380856ff41ecb26c0d5aee1747b6060d2ef0eSunny Goyal        ItemInfo info = dragObject.dragInfo;
3203e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        boolean userFolderPending = willCreateUserFolder(info, dragOverView, false);
3204c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (mDragMode == DRAG_MODE_NONE && userFolderPending &&
3205c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                !mFolderCreationAlarm.alarmPending()) {
3206c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
3207c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            FolderCreationAlarmListener listener = new
3208c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen                    FolderCreationAlarmListener(targetLayout, targetCell[0], targetCell[1]);
3209c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen
3210e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            if (!dragObject.accessibleDrag) {
3211c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen                mFolderCreationAlarm.setOnAlarmListener(listener);
3212c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen                mFolderCreationAlarm.setAlarm(FOLDER_CREATION_TIMEOUT);
3213c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            } else {
3214c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen                listener.onAlarm(mFolderCreationAlarm);
3215c9735cff2e558aa3f3810e49c15ef13049b9429cAdam Cohen            }
3216e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal
3217e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            if (dragObject.stateAnnouncer != null) {
3218e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                dragObject.stateAnnouncer.announce(WorkspaceAccessibilityHelper
3219e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                        .getDescriptionForDropOver(dragOverView, getContext()));
3220e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            }
3221c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            return;
3222482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        }
3223482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
3224e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        boolean willAddToFolder = willAddToExistingUserFolder(info, dragOverView);
3225482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        if (willAddToFolder && mDragMode == DRAG_MODE_NONE) {
3226c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDragOverFolderIcon = ((FolderIcon) dragOverView);
3227c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            mDragOverFolderIcon.onDragEnter(info);
3228482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            if (targetLayout != null) {
3229482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                targetLayout.clearDragOutlines();
3230482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            }
3231c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            setDragMode(DRAG_MODE_ADD_TO_FOLDER);
3232e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal
3233e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            if (dragObject.stateAnnouncer != null) {
3234e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                dragObject.stateAnnouncer.announce(WorkspaceAccessibilityHelper
3235e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                        .getDescriptionForDropOver(dragOverView, getContext()));
3236e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            }
3237c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            return;
3238482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        }
3239482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
3240c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (mDragMode == DRAG_MODE_ADD_TO_FOLDER && !willAddToFolder) {
3241c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            setDragMode(DRAG_MODE_NONE);
3242c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        }
3243c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        if (mDragMode == DRAG_MODE_CREATE_FOLDER && !userFolderPending) {
3244c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            setDragMode(DRAG_MODE_NONE);
3245976ebec9e31643d3513f9f0de2b433d9aa186ce9Patrick Dubroy        }
3246c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen    }
3247c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen
324819072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen    class FolderCreationAlarmListener implements OnAlarmListener {
324969ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen        CellLayout layout;
325069ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen        int cellX;
325169ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen        int cellY;
325219072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen
3253f172b747c24f28e29baaaf58f08bab48847b7a40Adam Cohen        FolderIcon.PreviewBackground bg = new FolderIcon.PreviewBackground();
3254f172b747c24f28e29baaaf58f08bab48847b7a40Adam Cohen
325569ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen        public FolderCreationAlarmListener(CellLayout layout, int cellX, int cellY) {
325669ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen            this.layout = layout;
325769ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen            this.cellX = cellX;
325869ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen            this.cellY = cellY;
3259efca0279eb927faebffc38c8382818df67fcd159Adam Cohen
3260efca0279eb927faebffc38c8382818df67fcd159Adam Cohen            DeviceProfile grid = mLauncher.getDeviceProfile();
3261efca0279eb927faebffc38c8382818df67fcd159Adam Cohen            BubbleTextView cell = (BubbleTextView) layout.getChildAt(cellX, cellY);
3262efca0279eb927faebffc38c8382818df67fcd159Adam Cohen
3263f172b747c24f28e29baaaf58f08bab48847b7a40Adam Cohen            bg.setup(getResources().getDisplayMetrics(), grid, null,
3264efca0279eb927faebffc38c8382818df67fcd159Adam Cohen                    cell.getMeasuredWidth(), cell.getPaddingTop());
3265f172b747c24f28e29baaaf58f08bab48847b7a40Adam Cohen
3266f172b747c24f28e29baaaf58f08bab48847b7a40Adam Cohen            // The full preview background should appear behind the icon
3267f172b747c24f28e29baaaf58f08bab48847b7a40Adam Cohen            bg.isClipping = false;
326819072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen        }
326919072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen
327019072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen        public void onAlarm(Alarm alarm) {
3271f172b747c24f28e29baaaf58f08bab48847b7a40Adam Cohen            mFolderCreateBg = bg;
3272efca0279eb927faebffc38c8382818df67fcd159Adam Cohen            mFolderCreateBg.animateToAccept(layout, cellX, cellY);
327369ce2e5beaa1a57c7b8fa14a2d6ff0b3abeb93c0Adam Cohen            layout.clearDragOutlines();
3274c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            setDragMode(DRAG_MODE_CREATE_FOLDER);
3275482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        }
3276482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen    }
3277482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
3278482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen    class ReorderAlarmListener implements OnAlarmListener {
3279482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        float[] dragViewCenter;
3280482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        int minSpanX, minSpanY, spanX, spanY;
3281e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        DragObject dragObject;
3282482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        View child;
3283482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
3284482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        public ReorderAlarmListener(float[] dragViewCenter, int minSpanX, int minSpanY, int spanX,
3285e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                int spanY, DragObject dragObject, View child) {
3286482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            this.dragViewCenter = dragViewCenter;
3287482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            this.minSpanX = minSpanX;
3288482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            this.minSpanY = minSpanY;
3289482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            this.spanX = spanX;
3290482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            this.spanY = spanY;
3291482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            this.child = child;
3292e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            this.dragObject = dragObject;
3293482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        }
3294482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
3295482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen        public void onAlarm(Alarm alarm) {
3296482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            int[] resultSpan = new int[2];
329719f3792523fe2d55ea791a9286398a6120920690Adam Cohen            mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
329874c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen                    (int) mDragViewVisualCenter[1], minSpanX, minSpanY, mDragTargetLayout,
329974c5491d1b84e072d29065748b41f0bb67459f59Adam Cohen                    mTargetCell);
330019f3792523fe2d55ea791a9286398a6120920690Adam Cohen            mLastReorderX = mTargetCell[0];
330119f3792523fe2d55ea791a9286398a6120920690Adam Cohen            mLastReorderY = mTargetCell[1];
330219f3792523fe2d55ea791a9286398a6120920690Adam Cohen
3303fa3c58f647e603760c0bd929166c0edee25675a9Adam Cohen            mTargetCell = mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0],
3304482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY,
3305482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                child, mTargetCell, resultSpan, CellLayout.MODE_DRAG_OVER);
3306482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
330719f3792523fe2d55ea791a9286398a6120920690Adam Cohen            if (mTargetCell[0] < 0 || mTargetCell[1] < 0) {
330819f3792523fe2d55ea791a9286398a6120920690Adam Cohen                mDragTargetLayout.revertTempState();
3309c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            } else {
3310c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                setDragMode(DRAG_MODE_REORDER);
331119f3792523fe2d55ea791a9286398a6120920690Adam Cohen            }
3312482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
3313482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            boolean resize = resultSpan[0] != spanX || resultSpan[1] != spanY;
3314482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            mDragTargetLayout.visualizeDropLocation(child, mDragOutline,
3315e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                mTargetCell[0], mTargetCell[1], resultSpan[0], resultSpan[1], resize, dragObject);
331619072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen        }
331719072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen    }
331819072da7441e07d5e90026d9e9e1b38f11245e44Adam Cohen
3319a34abf8c781485b788fddacb352d586bffca886cWinson Chung    @Override
33207d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen    public void getHitRectRelativeToDragLayer(Rect outRect) {
3321a34abf8c781485b788fddacb352d586bffca886cWinson Chung        // We want the workspace to have the whole area of the display (it will find the correct
3322a34abf8c781485b788fddacb352d586bffca886cWinson Chung        // cell layout to drop to in the existing drag/drop logic.
33237d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen        mLauncher.getDragLayer().getDescendantRectRelativeToSelf(this, outRect);
3324a34abf8c781485b788fddacb352d586bffca886cWinson Chung    }
3325a34abf8c781485b788fddacb352d586bffca886cWinson Chung
33262b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy    /**
3327120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     * Drop an item that didn't originate on one of the workspace screens.
3328120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     * It may have come from Launcher (e.g. from all apps or customize), or it may have
3329120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     * come from another app altogether.
3330120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     *
3331120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     * NOTE: This can also be called when we are outside of a drag event, when we want
3332120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     * to add an item to one of the workspace screens.
3333120980bd00f5eecec5717f49a3d7db96571025a9Adam Cohen     */
3334aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal    private void onDropExternal(final int[] touchXY, final ItemInfo dragInfo,
3335557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            final CellLayout cellLayout, boolean insertAtFirst, DragObject d) {
3336557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        final Runnable exitSpringLoadedRunnable = new Runnable() {
3337557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            @Override
3338557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            public void run() {
3339689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen                mLauncher.exitSpringLoadedDragModeDelayed(true,
3340689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen                        Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
3341557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            }
3342557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        };
3343b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen
3344aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal        ItemInfo info = dragInfo;
3345b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen        int spanX = info.spanX;
3346b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen        int spanY = info.spanY;
3347b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen        if (mDragInfo != null) {
3348b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen            spanX = mDragInfo.spanX;
3349b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen            spanY = mDragInfo.spanY;
3350b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen        }
3351b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen
33523d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        final long container = mLauncher.isHotseatLayout(cellLayout) ?
33533d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                LauncherSettings.Favorites.CONTAINER_HOTSEAT :
33543d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    LauncherSettings.Favorites.CONTAINER_DESKTOP;
3355dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        final long screenId = getIdForScreen(cellLayout);
3356dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (!mLauncher.isHotseatLayout(cellLayout)
3357dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                && screenId != getScreenIdForPageIndex(mCurrentPage)
33583d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                && mState != State.SPRING_LOADED) {
3359dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            snapToScreenId(screenId, null);
336076078c4ef35b6e95fb6d8129a32cf52a36625e28Adam Cohen        }
3361b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen
3362b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen        if (info instanceof PendingAddItemInfo) {
3363b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen            final PendingAddItemInfo pendingInfo = (PendingAddItemInfo) dragInfo;
3364b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen
3365558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            boolean findNearestVacantCell = true;
3366558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
3367558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY,
3368558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                        cellLayout, mTargetCell);
3369482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0],
3370482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                        mDragViewVisualCenter[1], mTargetCell);
3371aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal                if (willCreateUserFolder(d.dragInfo, cellLayout, mTargetCell, distance, true)
3372aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal                        || willAddToExistingUserFolder(
3373aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal                                d.dragInfo, cellLayout, mTargetCell, distance)) {
3374558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                    findNearestVacantCell = false;
3375558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                }
3376558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            }
3377482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen
3378aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal            final ItemInfo item = d.dragInfo;
3379aaa5c21ee2e03def644fb6ba8eadbb59873ddd45Adam Cohen            boolean updateWidgetSize = false;
3380558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            if (findNearestVacantCell) {
3381482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                int minSpanX = item.spanX;
3382482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                int minSpanY = item.spanY;
3383482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                if (item.minSpanX > 0 && item.minSpanY > 0) {
3384482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                    minSpanX = item.minSpanX;
3385482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                    minSpanY = item.minSpanY;
3386482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                }
3387d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                int[] resultSpan = new int[2];
3388fa3c58f647e603760c0bd929166c0edee25675a9Adam Cohen                mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0],
3389482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                        (int) mDragViewVisualCenter[1], minSpanX, minSpanY, info.spanX, info.spanY,
3390482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                        null, mTargetCell, resultSpan, CellLayout.MODE_ON_DROP_EXTERNAL);
33919e05a5ea951b4d5ffce324da8dd0656353306e6fAdam Cohen
33929e05a5ea951b4d5ffce324da8dd0656353306e6fAdam Cohen                if (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY) {
33939e05a5ea951b4d5ffce324da8dd0656353306e6fAdam Cohen                    updateWidgetSize = true;
33949e05a5ea951b4d5ffce324da8dd0656353306e6fAdam Cohen                }
3395d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                item.spanX = resultSpan[0];
3396d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                item.spanY = resultSpan[1];
3397558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            }
3398558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen
3399b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen            Runnable onAnimationCompleteRunnable = new Runnable() {
3400557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                @Override
3401557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                public void run() {
3402689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen                    // Normally removeExtraEmptyScreen is called in Workspace#onDragEnd, but when
3403689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen                    // adding an item that may not be dropped right away (due to a config activity)
3404689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen                    // we defer the removal until the activity returns.
3405689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen                    deferRemoveExtraEmptyScreen();
3406689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen
3407557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                    // When dragging and dropping from customization tray, we deal with creating
3408557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                    // widgets/shortcuts/folders in a slightly different way
340971b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal                    mLauncher.addPendingItem(pendingInfo, container, screenId, mTargetCell,
341071b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal                            item.spanX, item.spanY);
3411557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                }
3412b7e1618c627c6471c565f4f599228648d9e70a10Adam Cohen            };
34135940042d39b576553c2499bcf3d0641281e6ad52Adam Cohen            boolean isWidget = pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET
34145940042d39b576553c2499bcf3d0641281e6ad52Adam Cohen                    || pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
34155940042d39b576553c2499bcf3d0641281e6ad52Adam Cohen
3416b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal            AppWidgetHostView finalView = isWidget ?
3417b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal                    ((PendingAddWidgetInfo) pendingInfo).boundWidget : null;
34189e05a5ea951b4d5ffce324da8dd0656353306e6fAdam Cohen
3419b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal            if (finalView != null && updateWidgetSize) {
3420b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal                AppWidgetResizeFrame.updateWidgetSizeRanges(finalView, mLauncher, item.spanX,
34219e05a5ea951b4d5ffce324da8dd0656353306e6fAdam Cohen                        item.spanY);
34229e05a5ea951b4d5ffce324da8dd0656353306e6fAdam Cohen            }
34239e05a5ea951b4d5ffce324da8dd0656353306e6fAdam Cohen
3424d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            int animationStyle = ANIMATE_INTO_POSITION_AND_DISAPPEAR;
34255940042d39b576553c2499bcf3d0641281e6ad52Adam Cohen            if (isWidget && ((PendingAddWidgetInfo) pendingInfo).info != null &&
3426d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    ((PendingAddWidgetInfo) pendingInfo).info.configure != null) {
3427d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                animationStyle = ANIMATE_INTO_POSITION_AND_REMAIN;
3428d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            }
3429d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            animateWidgetDrop(info, cellLayout, d.dragView, onAnimationCompleteRunnable,
3430d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    animationStyle, finalView, true);
34319c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka        } else {
34329c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            // This is for other drag/drop cases, like dragging from All Apps
34339c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            View view = null;
343431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
34359c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            switch (info.itemType) {
34369c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
34379c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
3438eadbfc564d84aaf1d800da3d0d6edf6312f89648Michael Jurka                if (info.container == NO_ID && info instanceof AppInfo) {
34399c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                    // Came from all apps -- make a copy
344071b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal                    info = ((AppInfo) info).makeShortcut();
34419c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                }
3442dfaccf64bd59343de483b12eee6db61c960aedceSunny Goyal                view = mLauncher.createShortcut(cellLayout, (ShortcutInfo) info);
34439c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                break;
3444df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
3445c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, cellLayout,
3446c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                        (FolderInfo) info, mIconCache);
34479c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                break;
34489c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            default:
34499c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                throw new IllegalStateException("Unknown item type: " + info.itemType);
34509c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            }
345131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3452c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            // First we find the cell nearest to point at which the item is
3453c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            // dropped, without any consideration to whether there is an item there.
3454c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen            if (touchXY != null) {
3455c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY,
3456c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                        cellLayout, mTargetCell);
3457482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0],
3458482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                        mDragViewVisualCenter[1], mTargetCell);
3459557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                d.postAnimationRunnable = exitSpringLoadedRunnable;
3460482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                if (createUserFolderIfNecessary(view, container, cellLayout, mTargetCell, distance,
3461482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                        true, d.dragView, d.postAnimationRunnable)) {
3462c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                    return;
3463c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                }
3464482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                if (addToExistingFolderIfNecessary(view, cellLayout, mTargetCell, distance, d,
3465482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                        true)) {
3466c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                    return;
3467c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen                }
346810b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen            }
346910b1737910ea7890ca95bbbe5363fd5aa513b856Adam Cohen
3470c4e772e4b3f21bde342e475ebc49b5b76b8f74e8Michael Jurka            if (touchXY != null) {
34719c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                // when dragging and dropping, just find the closest free spot
3472fa3c58f647e603760c0bd929166c0edee25675a9Adam Cohen                mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0],
3473482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                        (int) mDragViewVisualCenter[1], 1, 1, 1, 1,
3474ea889a294be004f4b1c21e9b874f9e63abfb8bd6Adam Cohen                        null, mTargetCell, null, CellLayout.MODE_ON_DROP_EXTERNAL);
34759c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka            } else {
34769c6de3d7c3e7d40e1e443b6fd69e3077ab9e9673Michael Jurka                cellLayout.findCellForSpan(mTargetCell, 1, 1);
347731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
347895abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal            // Add the item to DB before adding to screen ensures that the container and other
347995abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal            // values of the info is properly updated.
348095abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal            LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screenId,
348195abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal                    mTargetCell[0], mTargetCell[1]);
348295abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal
3483dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            addInScreen(view, container, screenId, mTargetCell[0], mTargetCell[1], info.spanX,
34843d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    info.spanY, insertAtFirst);
3485716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen            cellLayout.onDropChild(view);
3486a52570f8f9ad65b85e33a2f2e87722f9edd6c6f4Michael Jurka            cellLayout.getShortcutsAndWidgets().measureChild(view);
3487d5e42733799817d11d739b0a882da8dda721126dAdam Cohen
34883e8f811f31a2275e778244427271f77f4116c08bAdam Cohen            if (d.dragView != null) {
34894b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen                // We wrap the animation call in the temporary set and reset of the current
34904b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen                // cellLayout to its final transform -- this means we animate the drag view to
34914b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen                // the correct final location.
34924b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen                setFinalTransitionTransform(cellLayout);
3493557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view,
349428f852ab678d9750879b6d3549bf5da176cf1225Adam Cohen                        exitSpringLoadedRunnable, this);
34954b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen                resetTransitionTransform(cellLayout);
34964b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen            }
34974b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        }
34984b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    }
34994b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen
3500d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen    public Bitmap createWidgetBitmap(ItemInfo widgetInfo, View layout) {
35015b0e669169ea2c951bf2f6f71faf793b24db3c23Sunny Goyal        int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(widgetInfo, false);
3502d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        int visibility = layout.getVisibility();
3503ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        layout.setVisibility(VISIBLE);
3504ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3505ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int width = MeasureSpec.makeMeasureSpec(unScaledSize[0], MeasureSpec.EXACTLY);
3506ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int height = MeasureSpec.makeMeasureSpec(unScaledSize[1], MeasureSpec.EXACTLY);
3507ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        Bitmap b = Bitmap.createBitmap(unScaledSize[0], unScaledSize[1],
3508ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                Bitmap.Config.ARGB_8888);
3509508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.setBitmap(b);
3510ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3511ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        layout.measure(width, height);
3512ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        layout.layout(0, 0, unScaledSize[0], unScaledSize[1]);
3513508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        layout.draw(mCanvas);
3514508da15509224b46fcccabbe78f3e92fe69a67d8Sunny Goyal        mCanvas.setBitmap(null);
3515d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        layout.setVisibility(visibility);
3516ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        return b;
3517ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    }
3518ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3519d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen    private void getFinalPositionForDropAnimation(int[] loc, float[] scaleXY,
3520a501d49a6a8b684bc83aa6b536f43247d75bacdfTony Wickham            DragView dragView, CellLayout layout, ItemInfo info, int[] targetCell, boolean scale) {
3521ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        // Now we animate the dragView, (ie. the widget or shortcut preview) into its final
3522ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        // location and size on the home screen.
3523d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        int spanX = info.spanX;
3524d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        int spanY = info.spanY;
3525d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen
3526a501d49a6a8b684bc83aa6b536f43247d75bacdfTony Wickham        Rect r = estimateItemPosition(layout, targetCell[0], targetCell[1], spanX, spanY);
3527d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        loc[0] = r.left;
3528d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        loc[1] = r.top;
3529d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen
3530d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        setFinalTransitionTransform(layout);
3531ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        float cellLayoutScale =
35327d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen                mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(layout, loc, true);
3533d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        resetTransitionTransform(layout);
3534a2fe78630a6b01e51e3d3240ad9852a8822b4220Michael Jurka
3535a2fe78630a6b01e51e3d3240ad9852a8822b4220Michael Jurka        float dragViewScaleX;
3536a2fe78630a6b01e51e3d3240ad9852a8822b4220Michael Jurka        float dragViewScaleY;
3537a2fe78630a6b01e51e3d3240ad9852a8822b4220Michael Jurka        if (scale) {
3538a2fe78630a6b01e51e3d3240ad9852a8822b4220Michael Jurka            dragViewScaleX = (1.0f * r.width()) / dragView.getMeasuredWidth();
3539a2fe78630a6b01e51e3d3240ad9852a8822b4220Michael Jurka            dragViewScaleY = (1.0f * r.height()) / dragView.getMeasuredHeight();
3540a2fe78630a6b01e51e3d3240ad9852a8822b4220Michael Jurka        } else {
3541a2fe78630a6b01e51e3d3240ad9852a8822b4220Michael Jurka            dragViewScaleX = 1f;
3542a2fe78630a6b01e51e3d3240ad9852a8822b4220Michael Jurka            dragViewScaleY = 1f;
3543a2fe78630a6b01e51e3d3240ad9852a8822b4220Michael Jurka        }
3544ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3545ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        // The animation will scale the dragView about its center, so we need to center about
3546ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        // the final location.
3547a501d49a6a8b684bc83aa6b536f43247d75bacdfTony Wickham        loc[0] -= (dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2
3548a501d49a6a8b684bc83aa6b536f43247d75bacdfTony Wickham                - Math.ceil(layout.getUnusedHorizontalSpace() / 2f);
3549ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        loc[1] -= (dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2;
3550ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3551d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        scaleXY[0] = dragViewScaleX * cellLayoutScale;
3552d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        scaleXY[1] = dragViewScaleY * cellLayoutScale;
3553d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen    }
3554d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen
3555a501d49a6a8b684bc83aa6b536f43247d75bacdfTony Wickham    public void animateWidgetDrop(ItemInfo info, CellLayout cellLayout, final DragView dragView,
3556d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            final Runnable onCompleteRunnable, int animationType, final View finalView,
3557d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            boolean external) {
3558d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        Rect from = new Rect();
3559d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        mLauncher.getDragLayer().getViewRectRelativeToSelf(dragView, from);
3560d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen
3561d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        int[] finalPos = new int[2];
3562d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        float scaleXY[] = new float[2];
3563a2fe78630a6b01e51e3d3240ad9852a8822b4220Michael Jurka        boolean scalePreview = !(info instanceof PendingAddShortcutInfo);
3564d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        getFinalPositionForDropAnimation(finalPos, scaleXY, dragView, cellLayout, info, mTargetCell,
3565a501d49a6a8b684bc83aa6b536f43247d75bacdfTony Wickham                scalePreview);
3566ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3567ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        Resources res = mLauncher.getResources();
3568ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen        final int duration = res.getInteger(R.integer.config_dropAnimMaxDuration) - 200;
3569ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
35705940042d39b576553c2499bcf3d0641281e6ad52Adam Cohen        boolean isWidget = info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
35715940042d39b576553c2499bcf3d0641281e6ad52Adam Cohen                info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
3572d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        if ((animationType == ANIMATE_INTO_POSITION_AND_RESIZE || external) && finalView != null) {
3573d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            Bitmap crossFadeBitmap = createWidgetBitmap(info, finalView);
3574ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            dragView.setCrossFadeBitmap(crossFadeBitmap);
3575ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            dragView.crossFade((int) (duration * 0.8f));
35765940042d39b576553c2499bcf3d0641281e6ad52Adam Cohen        } else if (isWidget && external) {
3577d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            scaleXY[0] = scaleXY[1] = Math.min(scaleXY[0],  scaleXY[1]);
3578ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
3579ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
3580d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        DragLayer dragLayer = mLauncher.getDragLayer();
35817bd1bbb509f9569fa18d6b4d33242679fd98bc9bWinson Chung        if (animationType == CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION) {
3582d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            mLauncher.getDragLayer().animateViewIntoPosition(dragView, finalPos, 0f, 0.1f, 0.1f,
3583ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    DragLayer.ANIMATION_END_DISAPPEAR, onCompleteRunnable, duration);
3584ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        } else {
3585d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            int endStyle;
3586d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            if (animationType == ANIMATE_INTO_POSITION_AND_REMAIN) {
3587d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                endStyle = DragLayer.ANIMATION_END_REMAIN_VISIBLE;
3588d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            } else {
3589a501d49a6a8b684bc83aa6b536f43247d75bacdfTony Wickham                endStyle = DragLayer.ANIMATION_END_DISAPPEAR;
3590d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            }
3591d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen
3592d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            Runnable onComplete = new Runnable() {
3593d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                @Override
3594d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                public void run() {
3595d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    if (finalView != null) {
3596d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                        finalView.setVisibility(VISIBLE);
3597d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    }
3598d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    if (onCompleteRunnable != null) {
3599d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                        onCompleteRunnable.run();
3600d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    }
3601d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                }
3602d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            };
3603d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            dragLayer.animateViewIntoPosition(dragView, from.left, from.top, finalPos[0],
3604d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    finalPos[1], 1, 1, 1, scaleXY[0], scaleXY[1], onComplete, endStyle,
3605d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    duration, this);
3606ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
3607ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    }
3608ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
36094b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    public void setFinalTransitionTransform(CellLayout layout) {
36104b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        if (isSwitchingState()) {
36117d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen            mCurrentScale = getScaleX();
3612dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung            setScaleX(mStateTransitionAnimation.getFinalScale());
3613dc61c4d41468a8beced2863de1b863b340cf49baWinson Chung            setScaleY(mStateTransitionAnimation.getFinalScale());
36144b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        }
36154b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    }
36164b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen    public void resetTransitionTransform(CellLayout layout) {
36174b285c5a60028f7c9ce39cfb318a730d0ae95c27Adam Cohen        if (isSwitchingState()) {
36187d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen            setScaleX(mCurrentScale);
36197d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen            setScaleY(mCurrentScale);
3620af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka        }
362131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
3622aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
3623dadb304b4682998c43b9c07bef45b9a9380f3287Tony Wickham    public WorkspaceStateTransitionAnimation getStateTransitionAnimation() {
3624dadb304b4682998c43b9c07bef45b9a9380f3287Tony Wickham        return mStateTransitionAnimation;
3625dadb304b4682998c43b9c07bef45b9a9380f3287Tony Wickham    }
3626dadb304b4682998c43b9c07bef45b9a9380f3287Tony Wickham
362770864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    /**
362870864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * Return the current {@link CellLayout}, correctly picking the destination
362970864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * screen while a scroll is in progress.
363070864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     */
36315f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy    public CellLayout getCurrentDropLayout() {
3632360e63fd3e77247002b86da2a77bd8dfe8c8a807Winson Chung        return (CellLayout) getChildAt(getNextPage());
363370864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    }
363431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
363570864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey    /**
36360280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     * Return the current CellInfo describing our current drag; this method exists
36370280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     * so that Launcher can sync this object with the correct info when the activity is created/
36380280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     * destroyed
36390280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     *
36400280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka     */
36410280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    public CellLayout.CellInfo getDragInfo() {
36420280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        return mDragInfo;
36430280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
36440280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
36459b9fb967b2305413520dc676d6d3c472b2b6b25eWinson Chung    public int getCurrentPageOffsetFromCustomContent() {
364621cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen        return getNextPage() - numCustomPages();
364721cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen    }
364821cd002fef13ff0b6336f10f97e29738a2b0ecb9Adam Cohen
36490280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    /**
365070864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     * Calculate the nearest cell where the given object would be dropped.
3651e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen     *
3652e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen     * pixelX and pixelY should be in the coordinate system of layout
365370864289fba6daf07b8de98524cdfb765a62552dJeff Sharkey     */
3654091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk int[] findNearestArea(int pixelX, int pixelY,
3655e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen            int spanX, int spanY, CellLayout layout, int[] recycle) {
3656df0353815c629fc678824b07a234b89a1ff94208Adam Cohen        return layout.findNearestArea(
3657e3e27a854f3eca363d3c5ce353d19de475272d87Adam Cohen                pixelX, pixelY, spanX, spanY, recycle);
3658df0353815c629fc678824b07a234b89a1ff94208Adam Cohen    }
3659df0353815c629fc678824b07a234b89a1ff94208Adam Cohen
3660cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen    void setup(DragController dragController) {
3661c2f7f4794867cd1b3e97fd0f379ad8ad15958a6cMichael Jurka        mSpringLoadedDragController = new SpringLoadedDragController(mLauncher);
366200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        mDragController = dragController;
3663d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka
3664d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        // hardware layers on children are enabled on startup, but should be disabled until
3665d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        // needed
36663a0469d1e40459529e2d4d57ebc493f930f7c01aMichael Jurka        updateChildrenLayersEnabled(false);
366731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
366831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3669e3887cc4ec3cebda2333298e56a35acb49ccdf13Patrick Dubroy    /**
3670e3887cc4ec3cebda2333298e56a35acb49ccdf13Patrick Dubroy     * Called at the end of a drag which originated on the workspace.
3671e3887cc4ec3cebda2333298e56a35acb49ccdf13Patrick Dubroy     */
36721e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    public void onDropCompleted(final View target, final DragObject d,
36731e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            final boolean isFlingToDelete, final boolean success) {
36741e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        if (mDeferDropAfterUninstall) {
36751e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            mDeferredAction = new Runnable() {
3676ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                public void run() {
3677ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                    onDropCompleted(target, d, isFlingToDelete, success);
3678ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                    mDeferredAction = null;
3679ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen                }
3680ad4e15cae4b628677fc249628a9ff661e67add78Adam Cohen            };
36811e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            return;
36821e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        }
36831e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
36841e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        boolean beingCalledAfterUninstall = mDeferredAction != null;
36851e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
36861e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        if (success && !(beingCalledAfterUninstall && !mUninstallSuccessful)) {
368795deb3a57cfebe51e5b8ea74e0a870123d1347e9Sunny Goyal            if (target != this && mDragInfo != null) {
368871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal                removeWorkspaceItem(mDragInfo.cell);
368931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
3690ce34a9768b01115def95f000a6a8f35870f10d3aPatrick Dubroy        } else if (mDragInfo != null) {
36919282059e44604330763340e6c37ffb653b8ef187Sunny Goyal            final CellLayout cellLayout = mLauncher.getCellLayout(
36929282059e44604330763340e6c37ffb653b8ef187Sunny Goyal                    mDragInfo.container, mDragInfo.screenId);
3693eedb00a674358bb88dce1e0d3a90bd6cb9e97cfcJorim Jaggi            if (cellLayout != null) {
3694eedb00a674358bb88dce1e0d3a90bd6cb9e97cfcJorim Jaggi                cellLayout.onDropChild(mDragInfo.cell);
36956c56c68555dcb3d99b05d5faecd582a1f683cc92Sunny Goyal            } else if (ProviderConfig.IS_DOGFOOD_BUILD) {
36969282059e44604330763340e6c37ffb653b8ef187Sunny Goyal                throw new RuntimeException("Invalid state: cellLayout == null in "
36979282059e44604330763340e6c37ffb653b8ef187Sunny Goyal                        + "Workspace#onDropCompleted. Please file a bug. ");
36989282059e44604330763340e6c37ffb653b8ef187Sunny Goyal            };
369931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
37001e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        if ((d.cancelled || (beingCalledAfterUninstall && !mUninstallSuccessful))
37011e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka                && mDragInfo.cell != null) {
37021e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            mDragInfo.cell.setVisibility(VISIBLE);
370336cc09b07b19198f4ea886583cef462ade27192cAdam Cohen        }
37044be866d3a1665aa2098cb5d38d535b1ad1aab6d6Joe Onorato        mDragOutline = null;
370531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mDragInfo = null;
370694e0d37f53e699364ebff732cf75ab81a577fb91Tony Wickham
37072516428710f13d5abc425c896151ad013267cc3dTony Wickham        if (!isFlingToDelete) {
37082516428710f13d5abc425c896151ad013267cc3dTony Wickham            // Fling to delete already exits spring loaded mode after the animation finishes.
37092516428710f13d5abc425c896151ad013267cc3dTony Wickham            mLauncher.exitSpringLoadedDragModeDelayed(success,
3710e03856267544d2209c2844c9c06c74e8e52ff104Tony Wickham                    Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, mDelayedResizeRunnable);
3711e03856267544d2209c2844c9c06c74e8e52ff104Tony Wickham            mDelayedResizeRunnable = null;
37122516428710f13d5abc425c896151ad013267cc3dTony Wickham        }
371331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
371431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
371531178b8237ccb6af666df60ef60c116c8afdf316Hyunyoung Song    /**
371631178b8237ccb6af666df60ef60c116c8afdf316Hyunyoung Song     * For opposite operation. See {@link #addInScreen}.
371731178b8237ccb6af666df60ef60c116c8afdf316Hyunyoung Song     */
371871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal    public void removeWorkspaceItem(View v) {
371971b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        CellLayout parentCell = getParentCellLayoutForView(v);
372071b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        if (parentCell != null) {
372171b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal            parentCell.removeView(v);
37226c56c68555dcb3d99b05d5faecd582a1f683cc92Sunny Goyal        } else if (ProviderConfig.IS_DOGFOOD_BUILD) {
3723912bdfcffa04f8917ff56cc4e059208c13cbe29cSunny Goyal            // When an app is uninstalled using the drop target, we wait until resume to remove
3724912bdfcffa04f8917ff56cc4e059208c13cbe29cSunny Goyal            // the icon. We also remove all the corresponding items from the workspace at
3725912bdfcffa04f8917ff56cc4e059208c13cbe29cSunny Goyal            // {@link Launcher#bindComponentsRemoved}. That call can come before or after
3726912bdfcffa04f8917ff56cc4e059208c13cbe29cSunny Goyal            // {@link Launcher#mOnResumeCallbacks} depending on how busy the worker thread is.
3727912bdfcffa04f8917ff56cc4e059208c13cbe29cSunny Goyal            Log.e(TAG, "mDragInfo.cell has null parent");
372871b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        }
372971b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        if (v instanceof DropTarget) {
373071b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal            mDragController.removeDropTarget((DropTarget) v);
373171b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal        }
373271b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal    }
373371b5c0b988a64b3a0613ded5403749bc537ee8a5Sunny Goyal
3734aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal    /**
3735aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal     * Removes all folder listeners
3736aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal     */
3737aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal    public void removeFolderListeners() {
3738aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal        mapOverItems(false, new ItemOperator() {
3739aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal            @Override
3740aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal            public boolean evaluate(ItemInfo info, View view) {
3741aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal                if (view instanceof FolderIcon) {
3742aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal                    ((FolderIcon) view).removeListeners();
3743aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal                }
3744aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal                return false;
3745aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal            }
3746aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal        });
3747aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal    }
3748aaf7d1d13bd88bdeac5c56a9ab1d293fdaea3c11Sunny Goyal
3749fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
37501e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    public void deferCompleteDropAfterUninstallActivity() {
37511e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        mDeferDropAfterUninstall = true;
37521e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    }
37531e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
37541e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    /// maybe move this into a smaller part
3755fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
3756d5bd67dfa9ee5fda2384a75231b7a68ceb8e9bd5Sunny Goyal    public void onDragObjectRemoved(boolean success) {
37571e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        mDeferDropAfterUninstall = false;
37581e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        mUninstallSuccessful = success;
37591e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        if (mDeferredAction != null) {
37601e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            mDeferredAction.run();
37611e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        }
37621e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    }
37631e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
3764a48487a814c07a9f1f45eb3ffe3d873b3dc31b3bWinson Chung    @Override
3765eeb5bbc9409978cc2ae52d48380399fcde3d9f85Winson Chung    public float getIntrinsicIconScaleFactor() {
3766eeb5bbc9409978cc2ae52d48380399fcde3d9f85Winson Chung        return 1f;
3767eeb5bbc9409978cc2ae52d48380399fcde3d9f85Winson Chung    }
3768eeb5bbc9409978cc2ae52d48380399fcde3d9f85Winson Chung
3769eeb5bbc9409978cc2ae52d48380399fcde3d9f85Winson Chung    @Override
3770043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung    public boolean supportsFlingToDelete() {
3771043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung        return true;
3772043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung    }
3773043f2af567178b82b0b41f12d379e7dd12da2936Winson Chung
3774a48487a814c07a9f1f45eb3ffe3d873b3dc31b3bWinson Chung    @Override
37751eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood    public boolean supportsAppInfoDropTarget() {
3776e0c332365604976d999eb25d4bdcc49ec75b7a1cTony Wickham        return !FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND;
37771eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood    }
37781eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood
37791eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood    @Override
37801eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood    public boolean supportsDeleteDropTarget() {
37811eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood        return true;
37821eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood    }
37831eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood
37841eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood    @Override
3785ddec73471eb6cc1f15eb9421a205bb2362509075Sunny Goyal    public void onFlingToDelete(DragObject d, PointF vec) {
3786a48487a814c07a9f1f45eb3ffe3d873b3dc31b3bWinson Chung        // Do nothing
3787a48487a814c07a9f1f45eb3ffe3d873b3dc31b3bWinson Chung    }
3788a48487a814c07a9f1f45eb3ffe3d873b3dc31b3bWinson Chung
3789a48487a814c07a9f1f45eb3ffe3d873b3dc31b3bWinson Chung    @Override
3790a48487a814c07a9f1f45eb3ffe3d873b3dc31b3bWinson Chung    public void onFlingToDeleteCompleted() {
3791a48487a814c07a9f1f45eb3ffe3d873b3dc31b3bWinson Chung        // Do nothing
3792a48487a814c07a9f1f45eb3ffe3d873b3dc31b3bWinson Chung    }
3793a48487a814c07a9f1f45eb3ffe3d873b3dc31b3bWinson Chung
37940280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    public boolean isDropEnabled() {
37950280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        return true;
37960280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
37970280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
37980142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
37991462de39f01cec0ba785386532719cb0207dd827Adam Cohen    protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
38001462de39f01cec0ba785386532719cb0207dd827Adam Cohen        // We don't dispatch restoreInstanceState to our children using this code path.
3801a888158880fea53479447d883391f552604e46b6Dave Hawkey        // Some pages will be restored immediately as their items are bound immediately, and
38021462de39f01cec0ba785386532719cb0207dd827Adam Cohen        // others we will need to wait until after their items are bound.
38031462de39f01cec0ba785386532719cb0207dd827Adam Cohen        mSavedStates = container;
38041462de39f01cec0ba785386532719cb0207dd827Adam Cohen    }
38051462de39f01cec0ba785386532719cb0207dd827Adam Cohen
38061462de39f01cec0ba785386532719cb0207dd827Adam Cohen    public void restoreInstanceStateForChild(int child) {
38071462de39f01cec0ba785386532719cb0207dd827Adam Cohen        if (mSavedStates != null) {
38081462de39f01cec0ba785386532719cb0207dd827Adam Cohen            mRestoredPages.add(child);
38091462de39f01cec0ba785386532719cb0207dd827Adam Cohen            CellLayout cl = (CellLayout) getChildAt(child);
3810b0ee08109ec89959d9bbd526c6e5f7ab96c8c3eaAdam Cohen            if (cl != null) {
3811b0ee08109ec89959d9bbd526c6e5f7ab96c8c3eaAdam Cohen                cl.restoreInstanceState(mSavedStates);
3812b0ee08109ec89959d9bbd526c6e5f7ab96c8c3eaAdam Cohen            }
38131462de39f01cec0ba785386532719cb0207dd827Adam Cohen        }
38141462de39f01cec0ba785386532719cb0207dd827Adam Cohen    }
38151462de39f01cec0ba785386532719cb0207dd827Adam Cohen
38161462de39f01cec0ba785386532719cb0207dd827Adam Cohen    public void restoreInstanceStateForRemainingPages() {
38171462de39f01cec0ba785386532719cb0207dd827Adam Cohen        int count = getChildCount();
38181462de39f01cec0ba785386532719cb0207dd827Adam Cohen        for (int i = 0; i < count; i++) {
38191462de39f01cec0ba785386532719cb0207dd827Adam Cohen            if (!mRestoredPages.contains(i)) {
38201462de39f01cec0ba785386532719cb0207dd827Adam Cohen                restoreInstanceStateForChild(i);
38211462de39f01cec0ba785386532719cb0207dd827Adam Cohen            }
38221462de39f01cec0ba785386532719cb0207dd827Adam Cohen        }
38231462de39f01cec0ba785386532719cb0207dd827Adam Cohen        mRestoredPages.clear();
3824d8e596db4bcd19409bae99938f8b5c91b6267f74Winson Chung        mSavedStates = null;
38251462de39f01cec0ba785386532719cb0207dd827Adam Cohen    }
38261462de39f01cec0ba785386532719cb0207dd827Adam Cohen
38271462de39f01cec0ba785386532719cb0207dd827Adam Cohen    @Override
382831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void scrollLeft() {
38296c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        if (!workspaceInModalState() && !mIsSwitchingState) {
38300142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            super.scrollLeft();
383131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
383295bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        Folder openFolder = getOpenFolder();
383395bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        if (openFolder != null) {
383495bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen            openFolder.completeDragExit();
383595bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        }
383631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
383731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
38380142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka    @Override
383931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void scrollRight() {
38406c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        if (!workspaceInModalState() && !mIsSwitchingState) {
38410142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            super.scrollRight();
384231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
384395bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        Folder openFolder = getOpenFolder();
384495bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        if (openFolder != null) {
384595bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen            openFolder.completeDragExit();
384695bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        }
384731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
3848138a04170d964da9cdec228e95b976875ae52481Karl Rosaen
38491262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    @Override
38503e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung    public boolean onEnterScrollArea(int x, int y, int direction) {
38511c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung        // Ignore the scroll area if we are dragging over the hot seat
38524f3e9383eb422c87e2689548653b89f34f5516a5Sunny Goyal        boolean isPortrait = !mLauncher.getDeviceProfile().isLandscape;
385310bfc6e9294a4a336e505b57bac6fbac57bd8395Winson Chung        if (mLauncher.getHotseat() != null && isPortrait) {
38541c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung            Rect r = new Rect();
38551c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung            mLauncher.getHotseat().getHitRect(r);
38561c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung            if (r.contains(x, y)) {
38573e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung                return false;
38581c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung            }
38591c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung        }
38601c4cf4a11f3f7f24bbf65f958fdd22295d0bbd55Winson Chung
38613e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung        boolean result = false;
38626c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        if (!workspaceInModalState() && !mIsSwitchingState && getOpenFolder() == null) {
3863d718d6a7ba424b600052a147e5633a201a04ccb7Michael Jurka            mInScrollArea = true;
386454fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy
3865360e63fd3e77247002b86da2a77bd8dfe8c8a807Winson Chung            final int page = getNextPage() +
3866aa15ffe60e28b923ebf3e3162ce427ca6feae803Winson Chung                       (direction == DragController.SCROLL_LEFT ? -1 : 1);
3867c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            // We always want to exit the current layout to ensure parity of enter / exit
3868c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            setCurrentDropLayout(null);
386954fa3b95557c283976e8c1aa8a157b460b0b4513Patrick Dubroy
3870aa15ffe60e28b923ebf3e3162ce427ca6feae803Winson Chung            if (0 <= page && page < getChildCount()) {
3871482a5b6ed389ef943990277e461444626c34ebf2Winson Chung                // Ensure that we are not dragging over to the custom content screen
3872482a5b6ed389ef943990277e461444626c34ebf2Winson Chung                if (getScreenIdForPageIndex(page) == CUSTOM_CONTENT_SCREEN_ID) {
3873482a5b6ed389ef943990277e461444626c34ebf2Winson Chung                    return false;
3874482a5b6ed389ef943990277e461444626c34ebf2Winson Chung                }
3875482a5b6ed389ef943990277e461444626c34ebf2Winson Chung
3876aa15ffe60e28b923ebf3e3162ce427ca6feae803Winson Chung                CellLayout layout = (CellLayout) getChildAt(page);
3877c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen                setCurrentDragOverlappingLayout(layout);
387870b9530f3147663d38875260b48d931793b7d378Patrick Dubroy
3879c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                // Workspace is responsible for drawing the edge glow on adjacent pages,
3880c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                // so we need to redraw the workspace when this may have changed.
3881c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung                invalidate();
38823e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung                result = true;
3883c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            }
388470b9530f3147663d38875260b48d931793b7d378Patrick Dubroy        }
38853e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung        return result;
388608ae2ec4847a971ad1b19c163e3a0d6307a8ed72Patrick Dubroy    }
388708ae2ec4847a971ad1b19c163e3a0d6307a8ed72Patrick Dubroy
38881262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    @Override
38893e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung    public boolean onExitScrollArea() {
38903e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung        boolean result = false;
3891d718d6a7ba424b600052a147e5633a201a04ccb7Michael Jurka        if (mInScrollArea) {
3892c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            invalidate();
3893c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            CellLayout layout = getCurrentDropLayout();
3894c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            setCurrentDropLayout(layout);
3895c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen            setCurrentDragOverlappingLayout(layout);
3896c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen
38978a18afc4aa7217e293e0e96a23ac1cedaabc3738Adam Cohen            result = true;
3898c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            mInScrollArea = false;
3899c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        }
39003e0839e5f830ab7e64223ebe186d97729eda3e22Winson Chung        return result;
3901c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    }
3902c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung
3903c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung    private void onResetScrollArea() {
3904c6cc61d45836e4081920883cc4d448ccb0bb8417Adam Cohen        setCurrentDragOverlappingLayout(null);
3905c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung        mInScrollArea = false;
39061262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy    }
39071262e369484ce7f2565655ed80e6299232c70bd7Patrick Dubroy
39083d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    /**
39093d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * Returns a specific CellLayout
39103d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     */
39113d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    CellLayout getParentCellLayoutForView(View v) {
39123d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        ArrayList<CellLayout> layouts = getWorkspaceAndHotseatCellLayouts();
39133d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        for (CellLayout layout : layouts) {
3914a52570f8f9ad65b85e33a2f2e87722f9edd6c6f4Michael Jurka            if (layout.getShortcutsAndWidgets().indexOfChild(v) > -1) {
39153d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                return layout;
39163d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            }
39173d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
39183d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return null;
39193d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    }
39203d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
39213d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    /**
39223d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * Returns a list of all the CellLayouts in the workspace.
39233d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     */
39243d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    ArrayList<CellLayout> getWorkspaceAndHotseatCellLayouts() {
39253d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        ArrayList<CellLayout> layouts = new ArrayList<CellLayout>();
39263d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int screenCount = getChildCount();
39273d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        for (int screen = 0; screen < screenCount; screen++) {
39283d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            layouts.add(((CellLayout) getChildAt(screen)));
39293d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
39303d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mLauncher.getHotseat() != null) {
39313d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            layouts.add(mLauncher.getHotseat().getLayout());
39323d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
39333d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return layouts;
39343d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    }
39353d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
39363d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    /**
39373d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * We should only use this to search for specific children.  Do not use this method to modify
3938a52570f8f9ad65b85e33a2f2e87722f9edd6c6f4Michael Jurka     * ShortcutsAndWidgetsContainer directly. Includes ShortcutAndWidgetContainers from
3939a52570f8f9ad65b85e33a2f2e87722f9edd6c6f4Michael Jurka     * the hotseat and workspace pages
39403d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     */
3941a52570f8f9ad65b85e33a2f2e87722f9edd6c6f4Michael Jurka    ArrayList<ShortcutAndWidgetContainer> getAllShortcutAndWidgetContainers() {
394208442b84a25a22f3f21aad89ed5a6adccaa2166dSunny Goyal        ArrayList<ShortcutAndWidgetContainer> childrenLayouts = new ArrayList<>();
39433d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int screenCount = getChildCount();
394431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        for (int screen = 0; screen < screenCount; screen++) {
3945a52570f8f9ad65b85e33a2f2e87722f9edd6c6f4Michael Jurka            childrenLayouts.add(((CellLayout) getChildAt(screen)).getShortcutsAndWidgets());
39463d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
39473d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mLauncher.getHotseat() != null) {
3948a52570f8f9ad65b85e33a2f2e87722f9edd6c6f4Michael Jurka            childrenLayouts.add(mLauncher.getHotseat().getLayout().getShortcutsAndWidgets());
39493d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
39503d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return childrenLayouts;
39513d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    }
39523d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
3953317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal    public View getHomescreenIconByItemId(final long id) {
3954317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal        return getFirstMatch(new ItemOperator() {
3955317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal
3956317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal            @Override
3957c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            public boolean evaluate(ItemInfo info, View v) {
3958a75053f5f90b9494cc60a99e2b80b419eb3724b9Sunny Goyal                return info != null && info.id == id;
3959317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal            }
3960317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal        });
3961317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal    }
3962317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal
3963aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren    public View getViewForTag(final Object tag) {
3964ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        return getFirstMatch(new ItemOperator() {
3965ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
3966ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal            @Override
3967c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            public boolean evaluate(ItemInfo info, View v) {
3968ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal                return info == tag;
3969ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal            }
3970ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        });
3971ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    }
3972ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
3973ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    public LauncherAppWidgetHostView getWidgetForAppWidgetId(final int appWidgetId) {
3974ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        return (LauncherAppWidgetHostView) getFirstMatch(new ItemOperator() {
3975ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
3976ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal            @Override
3977c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            public boolean evaluate(ItemInfo info, View v) {
3978ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal                return (info instanceof LauncherAppWidgetInfo) &&
3979ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal                        ((LauncherAppWidgetInfo) info).appWidgetId == appWidgetId;
3980ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal            }
3981ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        });
3982ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal    }
3983ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal
39846d02c7a0337d37e5b1c596a4acec0c292a9b7f93Sunny Goyal    public View getFirstMatch(final ItemOperator operator) {
3985aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        final View[] value = new View[1];
3986651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal        mapOverItems(MAP_NO_RECURSE, new ItemOperator() {
3987aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren            @Override
3988c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            public boolean evaluate(ItemInfo info, View v) {
3989c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal                if (operator.evaluate(info, v)) {
3990aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                    value[0] = v;
3991aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                    return true;
399231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
3993aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                return false;
399431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
3995aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        });
3996aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        return value[0];
399731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
399831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3999df0353815c629fc678824b07a234b89a1ff94208Adam Cohen    void clearDropTargets() {
4000651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal        mapOverItems(MAP_NO_RECURSE, new ItemOperator() {
4001aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren            @Override
4002c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            public boolean evaluate(ItemInfo info, View v) {
4003df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                if (v instanceof DropTarget) {
4004df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                    mDragController.removeDropTarget((DropTarget) v);
4005df0353815c629fc678824b07a234b89a1ff94208Adam Cohen                }
4006aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                // not done, process all the shortcuts
4007aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                return false;
4008df0353815c629fc678824b07a234b89a1ff94208Adam Cohen            }
4009aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        });
4010df0353815c629fc678824b07a234b89a1ff94208Adam Cohen    }
401131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
401283892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung    // Removes ALL items that match a given package name, this is usually called when a package
401383892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung    // has been removed and we want to remove all components (widgets, shortcuts, apps) that
401483892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung    // belong to that package.
40153bbbabc54ac73a4451b0a862a3faac6426b1b7b4Sunny Goyal    void removeItemsByPackageName(final HashSet<String> packageNames, final UserHandleCompat user) {
401664359a53dc827797917a8d9a0697a91996789801Winson Chung        // Filter out all the ItemInfos that this is going to affect
401764359a53dc827797917a8d9a0697a91996789801Winson Chung        final HashSet<ItemInfo> infos = new HashSet<ItemInfo>();
401864359a53dc827797917a8d9a0697a91996789801Winson Chung        final HashSet<ComponentName> cns = new HashSet<ComponentName>();
401983892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung        ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts();
402083892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung        for (CellLayout layoutParent : cellLayouts) {
402183892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            ViewGroup layout = layoutParent.getShortcutsAndWidgets();
402283892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            int childCount = layout.getChildCount();
402383892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            for (int i = 0; i < childCount; ++i) {
402483892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                View view = layout.getChildAt(i);
402564359a53dc827797917a8d9a0697a91996789801Winson Chung                infos.add((ItemInfo) view.getTag());
402683892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            }
402783892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung        }
402864359a53dc827797917a8d9a0697a91996789801Winson Chung        LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() {
402964359a53dc827797917a8d9a0697a91996789801Winson Chung            @Override
403064359a53dc827797917a8d9a0697a91996789801Winson Chung            public boolean filterItem(ItemInfo parent, ItemInfo info,
403164359a53dc827797917a8d9a0697a91996789801Winson Chung                                      ComponentName cn) {
4032ed13187a745866483139e2878037e1f8427ce567Kenny Guy                if (packageNames.contains(cn.getPackageName())
4033ed13187a745866483139e2878037e1f8427ce567Kenny Guy                        && info.user.equals(user)) {
403464359a53dc827797917a8d9a0697a91996789801Winson Chung                    cns.add(cn);
403564359a53dc827797917a8d9a0697a91996789801Winson Chung                    return true;
403664359a53dc827797917a8d9a0697a91996789801Winson Chung                }
403764359a53dc827797917a8d9a0697a91996789801Winson Chung                return false;
403864359a53dc827797917a8d9a0697a91996789801Winson Chung            }
403964359a53dc827797917a8d9a0697a91996789801Winson Chung        };
404064359a53dc827797917a8d9a0697a91996789801Winson Chung        LauncherModel.filterItemInfos(infos, filter);
404183892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung
404264359a53dc827797917a8d9a0697a91996789801Winson Chung        // Remove the affected components
4043ed13187a745866483139e2878037e1f8427ce567Kenny Guy        removeItemsByComponentName(cns, user);
404483892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung    }
404583892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung
40464390acea35aefae52bd3fe38f5fdbb7ea322afb0Sunny Goyal    /**
40474390acea35aefae52bd3fe38f5fdbb7ea322afb0Sunny Goyal     * Removes items that match the item info specified. When applications are removed
40484390acea35aefae52bd3fe38f5fdbb7ea322afb0Sunny Goyal     * as a part of an update, this is called to ensure that other widgets and application
40494390acea35aefae52bd3fe38f5fdbb7ea322afb0Sunny Goyal     * shortcuts are not removed.
40504390acea35aefae52bd3fe38f5fdbb7ea322afb0Sunny Goyal     */
4051ed13187a745866483139e2878037e1f8427ce567Kenny Guy    void removeItemsByComponentName(final HashSet<ComponentName> componentNames,
4052ed13187a745866483139e2878037e1f8427ce567Kenny Guy            final UserHandleCompat user) {
40533d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts();
40543d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        for (final CellLayout layoutParent: cellLayouts) {
4055a52570f8f9ad65b85e33a2f2e87722f9edd6c6f4Michael Jurka            final ViewGroup layout = layoutParent.getShortcutsAndWidgets();
4056629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy
405764359a53dc827797917a8d9a0697a91996789801Winson Chung            final HashMap<ItemInfo, View> children = new HashMap<ItemInfo, View>();
405864359a53dc827797917a8d9a0697a91996789801Winson Chung            for (int j = 0; j < layout.getChildCount(); j++) {
405964359a53dc827797917a8d9a0697a91996789801Winson Chung                final View view = layout.getChildAt(j);
406064359a53dc827797917a8d9a0697a91996789801Winson Chung                children.put((ItemInfo) view.getTag(), view);
406164359a53dc827797917a8d9a0697a91996789801Winson Chung            }
406264359a53dc827797917a8d9a0697a91996789801Winson Chung
406364359a53dc827797917a8d9a0697a91996789801Winson Chung            final ArrayList<View> childrenToRemove = new ArrayList<View>();
406464359a53dc827797917a8d9a0697a91996789801Winson Chung            final HashMap<FolderInfo, ArrayList<ShortcutInfo>> folderAppsToRemove =
406564359a53dc827797917a8d9a0697a91996789801Winson Chung                    new HashMap<FolderInfo, ArrayList<ShortcutInfo>>();
406664359a53dc827797917a8d9a0697a91996789801Winson Chung            LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() {
406764359a53dc827797917a8d9a0697a91996789801Winson Chung                @Override
406864359a53dc827797917a8d9a0697a91996789801Winson Chung                public boolean filterItem(ItemInfo parent, ItemInfo info,
406964359a53dc827797917a8d9a0697a91996789801Winson Chung                                          ComponentName cn) {
407064359a53dc827797917a8d9a0697a91996789801Winson Chung                    if (parent instanceof FolderInfo) {
4071ed13187a745866483139e2878037e1f8427ce567Kenny Guy                        if (componentNames.contains(cn) && info.user.equals(user)) {
407264359a53dc827797917a8d9a0697a91996789801Winson Chung                            FolderInfo folder = (FolderInfo) parent;
407364359a53dc827797917a8d9a0697a91996789801Winson Chung                            ArrayList<ShortcutInfo> appsToRemove;
407464359a53dc827797917a8d9a0697a91996789801Winson Chung                            if (folderAppsToRemove.containsKey(folder)) {
407564359a53dc827797917a8d9a0697a91996789801Winson Chung                                appsToRemove = folderAppsToRemove.get(folder);
407664359a53dc827797917a8d9a0697a91996789801Winson Chung                            } else {
407764359a53dc827797917a8d9a0697a91996789801Winson Chung                                appsToRemove = new ArrayList<ShortcutInfo>();
407864359a53dc827797917a8d9a0697a91996789801Winson Chung                                folderAppsToRemove.put(folder, appsToRemove);
4079629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                            }
408064359a53dc827797917a8d9a0697a91996789801Winson Chung                            appsToRemove.add((ShortcutInfo) info);
408164359a53dc827797917a8d9a0697a91996789801Winson Chung                            return true;
4082574d20ec84551370987dde530c27ec493bdef564Romain Guy                        }
408364359a53dc827797917a8d9a0697a91996789801Winson Chung                    } else {
4084ed13187a745866483139e2878037e1f8427ce567Kenny Guy                        if (componentNames.contains(cn) && info.user.equals(user)) {
408564359a53dc827797917a8d9a0697a91996789801Winson Chung                            childrenToRemove.add(children.get(info));
408664359a53dc827797917a8d9a0697a91996789801Winson Chung                            return true;
4087629de3ef739883c0962423cc0c3a26299f162d3dRomain Guy                        }
4088574d20ec84551370987dde530c27ec493bdef564Romain Guy                    }
408964359a53dc827797917a8d9a0697a91996789801Winson Chung                    return false;
409064359a53dc827797917a8d9a0697a91996789801Winson Chung                }
409164359a53dc827797917a8d9a0697a91996789801Winson Chung            };
409264359a53dc827797917a8d9a0697a91996789801Winson Chung            LauncherModel.filterItemInfos(children.keySet(), filter);
4093aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
409464359a53dc827797917a8d9a0697a91996789801Winson Chung            // Remove all the apps from their folders
409564359a53dc827797917a8d9a0697a91996789801Winson Chung            for (FolderInfo folder : folderAppsToRemove.keySet()) {
409664359a53dc827797917a8d9a0697a91996789801Winson Chung                ArrayList<ShortcutInfo> appsToRemove = folderAppsToRemove.get(folder);
409764359a53dc827797917a8d9a0697a91996789801Winson Chung                for (ShortcutInfo info : appsToRemove) {
4098c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal                    folder.remove(info, false);
409964359a53dc827797917a8d9a0697a91996789801Winson Chung                }
410064359a53dc827797917a8d9a0697a91996789801Winson Chung            }
410164359a53dc827797917a8d9a0697a91996789801Winson Chung
410264359a53dc827797917a8d9a0697a91996789801Winson Chung            // Remove all the other children
410364359a53dc827797917a8d9a0697a91996789801Winson Chung            for (View child : childrenToRemove) {
410464359a53dc827797917a8d9a0697a91996789801Winson Chung                // Note: We can not remove the view directly from CellLayoutChildren as this
410564359a53dc827797917a8d9a0697a91996789801Winson Chung                // does not re-mark the spaces as unoccupied.
410664359a53dc827797917a8d9a0697a91996789801Winson Chung                layoutParent.removeViewInLayout(child);
410764359a53dc827797917a8d9a0697a91996789801Winson Chung                if (child instanceof DropTarget) {
410864359a53dc827797917a8d9a0697a91996789801Winson Chung                    mDragController.removeDropTarget((DropTarget) child);
410931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
411064359a53dc827797917a8d9a0697a91996789801Winson Chung            }
411164359a53dc827797917a8d9a0697a91996789801Winson Chung
411264359a53dc827797917a8d9a0697a91996789801Winson Chung            if (childrenToRemove.size() > 0) {
411364359a53dc827797917a8d9a0697a91996789801Winson Chung                layout.requestLayout();
411464359a53dc827797917a8d9a0697a91996789801Winson Chung                layout.invalidate();
411564359a53dc827797917a8d9a0697a91996789801Winson Chung            }
411631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
41172efec4e29f80db4293664d3ed4a89d37454510e1Winson Chung
411864359a53dc827797917a8d9a0697a91996789801Winson Chung        // Strip all the empty screens
411964359a53dc827797917a8d9a0697a91996789801Winson Chung        stripEmptyScreens();
412031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
4121f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4122f9c184a619e4e4b82cf9e0bf318ca6d8deaaaee7Adam Cohen    public interface ItemOperator {
4123aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        /**
4124651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal         * Process the next itemInfo, possibly with side-effect on {@link ItemOperator#value}.
4125aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren         *
4126aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren         * @param info info for the shortcut
4127aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren         * @param view view for the shortcut
4128aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren         * @return true if done, false to continue the map
4129aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren         */
4130c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal        public boolean evaluate(ItemInfo info, View view);
4131aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren    }
4132aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren
4133aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren    /**
4134651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal     * Map the operator over the shortcuts and widgets, return the first-non-null value.
4135aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren     *
4136aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren     * @param recurse true: iterate over folder children. false: op get the folders themselves.
4137aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren     * @param op the operator to map over the shortcuts
4138aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren     */
4139651077bdd603bb182be039925fd17bdf0da15016Sunny Goyal    void mapOverItems(boolean recurse, ItemOperator op) {
4140aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        ArrayList<ShortcutAndWidgetContainer> containers = getAllShortcutAndWidgetContainers();
4141aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        final int containerCount = containers.size();
4142aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        for (int containerIdx = 0; containerIdx < containerCount; containerIdx++) {
4143aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren            ShortcutAndWidgetContainer container = containers.get(containerIdx);
4144aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren            // map over all the shortcuts on the workspace
4145aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren            final int itemCount = container.getChildCount();
4146aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren            for (int itemIdx = 0; itemIdx < itemCount; itemIdx++) {
4147aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                View item = container.getChildAt(itemIdx);
4148aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                ItemInfo info = (ItemInfo) item.getTag();
4149aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                if (recurse && info instanceof FolderInfo && item instanceof FolderIcon) {
4150aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                    FolderIcon folder = (FolderIcon) item;
4151aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                    ArrayList<View> folderChildren = folder.getFolder().getItemsInReadingOrder();
4152aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                    // map over all the children in the folder
4153aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                    final int childCount = folderChildren.size();
4154aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                    for (int childIdx = 0; childIdx < childCount; childIdx++) {
4155aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                        View child = folderChildren.get(childIdx);
4156aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                        info = (ItemInfo) child.getTag();
4157c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal                        if (op.evaluate(info, child)) {
4158aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                            return;
4159aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                        }
4160aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                    }
4161aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                } else {
4162c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal                    if (op.evaluate(info, item)) {
4163aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                        return;
4164aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                    }
4165aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren                }
4166aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren            }
4167aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren        }
4168aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren    }
4169aeff7ea43409d817490fbb8c22b8d4b9725bb54fChris Wren
4170b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal    void updateShortcuts(ArrayList<ShortcutInfo> shortcuts) {
4171c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal        int total  = shortcuts.size();
4172c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal        final HashSet<ShortcutInfo> updates = new HashSet<ShortcutInfo>(total);
4173c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal        final HashSet<Long> folderIds = new HashSet<>();
4174c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal
4175c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal        for (int i = 0; i < total; i++) {
4176c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            ShortcutInfo s = shortcuts.get(i);
4177c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            updates.add(s);
4178c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            folderIds.add(s.container);
4179c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal        }
4180c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal
4181b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal        mapOverItems(MAP_RECURSE, new ItemOperator() {
4182b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal            @Override
4183c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            public boolean evaluate(ItemInfo info, View v) {
4184b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal                if (info instanceof ShortcutInfo && v instanceof BubbleTextView &&
4185b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal                        updates.contains(info)) {
41864390acea35aefae52bd3fe38f5fdbb7ea322afb0Sunny Goyal                    ShortcutInfo si = (ShortcutInfo) info;
4187b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal                    BubbleTextView shortcut = (BubbleTextView) v;
41883fcab663a141b31916152c8bc185b93b718d79dfSunny Goyal                    Drawable oldIcon = getTextViewIcon(shortcut);
41893fcab663a141b31916152c8bc185b93b718d79dfSunny Goyal                    boolean oldPromiseState = (oldIcon instanceof PreloadIconDrawable)
41903fcab663a141b31916152c8bc185b93b718d79dfSunny Goyal                            && ((PreloadIconDrawable) oldIcon).hasNotCompleted();
4191dfaccf64bd59343de483b12eee6db61c960aedceSunny Goyal                    shortcut.applyFromShortcutInfo(si, mIconCache,
41924390acea35aefae52bd3fe38f5fdbb7ea322afb0Sunny Goyal                            si.isPromise() != oldPromiseState);
4193c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal                }
4194c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal                // process all the shortcuts
4195c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal                return false;
4196c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            }
4197c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal        });
4198b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal
4199c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal        // Update folder icons
4200c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal        mapOverItems(MAP_NO_RECURSE, new ItemOperator() {
4201c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            @Override
4202c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            public boolean evaluate(ItemInfo info, View v) {
4203c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal                if (info instanceof FolderInfo && folderIds.contains(info.id)) {
4204c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal                    ((FolderInfo) info).itemsChanged(false);
4205b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal                }
4206b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal                // process all the shortcuts
4207b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal                return false;
4208b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal            }
4209b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal        });
4210b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal    }
4211b50cc8c5d6125715b139d978887830a5341971aaSunny Goyal
4212e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal    public void removeAbandonedPromise(String packageName, UserHandleCompat user) {
42133bbbabc54ac73a4451b0a862a3faac6426b1b7b4Sunny Goyal        HashSet<String> packages = new HashSet<>(1);
4214e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal        packages.add(packageName);
4215e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal        LauncherModel.deletePackageFromDatabase(mLauncher, packageName, user);
4216e7b8cd9e4f8d38d0445f3a032fafd44332f70878Sunny Goyal        removeItemsByPackageName(packages, user);
421740c5ed303909c4df71037be3429aa1423e59585fChris Wren    }
421840c5ed303909c4df71037be3429aa1423e59585fChris Wren
4219756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal    public void updateRestoreItems(final HashSet<ItemInfo> updates) {
4220a22666f68151269853e7ab6ab696ee6361bac793Sunny Goyal        mapOverItems(MAP_RECURSE, new ItemOperator() {
4221a22666f68151269853e7ab6ab696ee6361bac793Sunny Goyal            @Override
4222c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            public boolean evaluate(ItemInfo info, View v) {
4223756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal                if (info instanceof ShortcutInfo && v instanceof BubbleTextView
4224756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal                        && updates.contains(info)) {
4225756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal                    ((BubbleTextView) v).applyState(false);
4226756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal                } else if (v instanceof PendingAppWidgetHostView
4227756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal                        && info instanceof LauncherAppWidgetInfo
4228756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal                        && updates.contains(info)) {
4229756adbc3e41ee1edb53c580b8c679f343924fab5Sunny Goyal                    ((PendingAppWidgetHostView) v).applyState();
4230a22666f68151269853e7ab6ab696ee6361bac793Sunny Goyal                }
4231a22666f68151269853e7ab6ab696ee6361bac793Sunny Goyal                // process all the shortcuts
4232a22666f68151269853e7ab6ab696ee6361bac793Sunny Goyal                return false;
4233a22666f68151269853e7ab6ab696ee6361bac793Sunny Goyal            }
4234a22666f68151269853e7ab6ab696ee6361bac793Sunny Goyal        });
4235a22666f68151269853e7ab6ab696ee6361bac793Sunny Goyal    }
4236a22666f68151269853e7ab6ab696ee6361bac793Sunny Goyal
423787af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal    public void widgetsRestored(final ArrayList<LauncherAppWidgetInfo> changedInfo) {
42380fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        if (!changedInfo.isEmpty()) {
42390fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            DeferredWidgetRefresh widgetRefresh = new DeferredWidgetRefresh(changedInfo,
42400fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                    mLauncher.getAppWidgetHost());
42412e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal
42422e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            LauncherAppWidgetInfo item = changedInfo.get(0);
42432e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            final AppWidgetProviderInfo widgetInfo;
42442e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
42452e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal                widgetInfo = AppWidgetManagerCompat
42462e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal                        .getInstance(mLauncher).findProvider(item.providerName, item.user);
42472e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            } else {
42482e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal                widgetInfo = AppWidgetManagerCompat.getInstance(mLauncher)
42492e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal                        .getAppWidgetInfo(item.appWidgetId);
42502e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            }
42512e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal
42522e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            if (widgetInfo != null) {
42530fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                // Re-inflate the widgets which have changed status
42540fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                widgetRefresh.run();
42550fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            } else {
42560fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                // widgetRefresh will automatically run when the packages are updated.
42574390acea35aefae52bd3fe38f5fdbb7ea322afb0Sunny Goyal                // For now just update the progress bars
425887af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                mapOverItems(MAP_NO_RECURSE, new ItemOperator() {
425987af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                    @Override
426087af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                    public boolean evaluate(ItemInfo info, View view) {
426187af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                        if (view instanceof PendingAppWidgetHostView
426287af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                                && changedInfo.contains(info)) {
426387af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                            ((LauncherAppWidgetInfo) info).installProgress = 100;
426487af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                            ((PendingAppWidgetHostView) view).applyState();
426587af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                        }
426687af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                        // process all the shortcuts
426787af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                        return false;
42684390acea35aefae52bd3fe38f5fdbb7ea322afb0Sunny Goyal                    }
426987af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                });
42700fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            }
4271ff572277112ec3d6a6a8c1be274d6fa1019e3648Sunny Goyal        }
4272f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project    }
4273f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
4274af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski    private void moveToScreen(int page, boolean animate) {
42756c5891a9fce95eee3d87823d11d21889743e9c68Adam Cohen        if (!workspaceInModalState()) {
4276de1af7661548692d370518528ff91c7422b9c8aeWinson Chung            if (animate) {
4277af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski                snapToPage(page);
4278de1af7661548692d370518528ff91c7422b9c8aeWinson Chung            } else {
4279af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski                setCurrentPage(page);
4280de1af7661548692d370518528ff91c7422b9c8aeWinson Chung            }
4281c45b16862918a0c6c253bba12032165b030565afJoe Onorato        }
4282af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski        View child = getChildAt(page);
428364359a53dc827797917a8d9a0697a91996789801Winson Chung        if (child != null) {
428464359a53dc827797917a8d9a0697a91996789801Winson Chung            child.requestFocus();
428564359a53dc827797917a8d9a0697a91996789801Winson Chung        }
428631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
428731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
4288af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski    void moveToDefaultScreen(boolean animate) {
428977ccb8d03c17aeb32dccfb0930ae974f6cca7455Sunny Goyal        moveToScreen(getDefaultPage(), animate);
4290af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski    }
4291af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski
4292af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski    void moveToCustomContentScreen(boolean animate) {
4293af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski        if (hasCustomContent()) {
4294af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski            int ccIndex = getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID);
4295af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski            if (animate) {
4296af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski                snapToPage(ccIndex);
4297af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski            } else {
4298af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski                setCurrentPage(ccIndex);
4299af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski            }
4300af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski            View child = getChildAt(ccIndex);
4301af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski            if (child != null) {
4302af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski                child.requestFocus();
4303af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski            }
4304af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski         }
430576128b6359fc430542811fbb32e4a1d593a9ef07Adam Cohen        exitWidgetResizeMode();
4306af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski    }
4307af110e8603b2f372286d9155cbe8955a6a329e2dAllan Wojciechowski
430853805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen    protected String getPageIndicatorDescription() {
430953805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen        String settings = getResources().getString(R.string.settings_button_text);
431053805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen        return getCurrentPageDescription() + ", " + settings;
431153805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen    }
431253805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen
43136a0f57dfafced837a2a282d8feec28d5418be3b9Winson Chung    protected String getCurrentPageDescription() {
431453805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen        if (hasCustomContent() && getNextPage() == 0) {
431553805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen            return mCustomContentDescription;
431653805218809cc1f93a0f1307b442df7c65a3068cAdam Cohen        }
43171d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal        int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
43181d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal        return getPageDescription(page);
43191d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal    }
43201d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal
43211d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal    private String getPageDescription(int page) {
43221d08f70441999c66b76c97e48b4149e1433be3c3Sunny Goyal        int delta = numCustomPages();
4323e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        int nScreens = getChildCount() - delta;
4324e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        int extraScreenId = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID);
4325e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        if (extraScreenId >= 0 && nScreens > 1) {
4326e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            if (page == extraScreenId) {
4327e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                return getContext().getString(R.string.workspace_new_page);
4328e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            }
4329e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            nScreens--;
4330e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal        }
43315be3e4cc529f75b894d4603c9fd1342b370766e0Sunny Goyal        if (nScreens == 0) {
43325be3e4cc529f75b894d4603c9fd1342b370766e0Sunny Goyal            // When the workspace is not loaded, we do not know how many screen will be bound.
43335be3e4cc529f75b894d4603c9fd1342b370766e0Sunny Goyal            return getContext().getString(R.string.all_apps_home_button_label);
43345be3e4cc529f75b894d4603c9fd1342b370766e0Sunny Goyal        }
4335c5c723bbe43206ffa218b677984bc56a877359c4Sunny Goyal        return getContext().getString(R.string.workspace_scroll_format,
4336e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                page + 1 - delta, nScreens);
43376a0f57dfafced837a2a282d8feec28d5418be3b9Winson Chung    }
43388dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen
43398f1eff7b6cc8621888ee46605c32e601f80a890bWinson Chung    @Override
4340ddec1c739ef37c3a042982b8943fe42e04b65f4cHyunyoung Song    public void fillInLaunchSourceData(View v, ItemInfo info, Target target, Target targetParent) {
4341ddec1c739ef37c3a042982b8943fe42e04b65f4cHyunyoung Song        target.itemType = LauncherLogProto.APP_ICON;
4342ddec1c739ef37c3a042982b8943fe42e04b65f4cHyunyoung Song        target.gridX = info.cellX;
4343ddec1c739ef37c3a042982b8943fe42e04b65f4cHyunyoung Song        target.gridY = info.cellY;
4344ddec1c739ef37c3a042982b8943fe42e04b65f4cHyunyoung Song        target.pageIndex = getCurrentPage();
4345ddec1c739ef37c3a042982b8943fe42e04b65f4cHyunyoung Song        targetParent.containerType = LauncherLogProto.WORKSPACE;
43468f1eff7b6cc8621888ee46605c32e601f80a890bWinson Chung    }
43478f1eff7b6cc8621888ee46605c32e601f80a890bWinson Chung
43480fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    /**
43490fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal     * Used as a workaround to ensure that the AppWidgetService receives the
43500fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal     * PACKAGE_ADDED broadcast before updating widgets.
43510fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal     */
43520fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    private class DeferredWidgetRefresh implements Runnable {
43530fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        private final ArrayList<LauncherAppWidgetInfo> mInfos;
43540fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        private final LauncherAppWidgetHost mHost;
43550fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        private final Handler mHandler;
43560fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
43570fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        private boolean mRefreshPending;
43580fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
43590fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        public DeferredWidgetRefresh(ArrayList<LauncherAppWidgetInfo> infos,
43600fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                LauncherAppWidgetHost host) {
43610fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            mInfos = infos;
43620fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            mHost = host;
43630fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            mHandler = new Handler();
43640fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            mRefreshPending = true;
43650fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
43660fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            mHost.addProviderChangeListener(this);
43670fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            // Force refresh after 10 seconds, if we don't get the provider changed event.
43680fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            // This could happen when the provider is no longer available in the app.
43690fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            mHandler.postDelayed(this, 10000);
43700fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        }
43710fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
43720fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        @Override
43730fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        public void run() {
43740fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            mHost.removeProviderChangeListener(this);
43750fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            mHandler.removeCallbacks(this);
43760fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
43770fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            if (!mRefreshPending) {
43780fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                return;
43790fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            }
43800fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
43810fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal            mRefreshPending = false;
43820fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal
438387af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal            mapOverItems(MAP_NO_RECURSE, new ItemOperator() {
438487af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                @Override
438587af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                public boolean evaluate(ItemInfo info, View view) {
438687af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                    if (view instanceof PendingAppWidgetHostView && mInfos.contains(info)) {
438787af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                        PendingAppWidgetHostView hostView = (PendingAppWidgetHostView) view;
438887af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                        mLauncher.removeItem(view, info, false /* deleteFromDb */);
438987af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                        mLauncher.bindAppWidget((LauncherAppWidgetInfo) info);
439087af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                    }
439187af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                    // process all the shortcuts
439287af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal                    return false;
43930fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal                }
439487af0fded6d91b85c7a4bbf066f93d58c241b146Sunny Goyal            });
43950fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal        }
43960fc1be164e982433e619bcbb16aa67e28ff681efSunny Goyal    }
439731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
4398