Workspace.java revision dbf08b4aee0bd9c296552485a34f15f997a56f3f
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.AnimatorSet;
22import android.animation.LayoutTransition;
23import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
25import android.animation.ValueAnimator;
26import android.animation.ValueAnimator.AnimatorUpdateListener;
27import android.annotation.SuppressLint;
28import android.app.WallpaperManager;
29import android.appwidget.AppWidgetHostView;
30import android.appwidget.AppWidgetProviderInfo;
31import android.content.ComponentName;
32import android.content.Context;
33import android.content.res.Resources;
34import android.graphics.Bitmap;
35import android.graphics.Canvas;
36import android.graphics.Matrix;
37import android.graphics.Point;
38import android.graphics.PointF;
39import android.graphics.Rect;
40import android.graphics.drawable.Drawable;
41import android.os.Handler;
42import android.os.IBinder;
43import android.os.Parcelable;
44import android.util.AttributeSet;
45import android.util.Log;
46import android.util.Property;
47import android.util.SparseArray;
48import android.view.MotionEvent;
49import android.view.View;
50import android.view.ViewDebug;
51import android.view.ViewGroup;
52import android.view.accessibility.AccessibilityManager;
53import android.view.animation.DecelerateInterpolator;
54import android.view.animation.Interpolator;
55import android.widget.TextView;
56
57import com.android.launcher3.Launcher.CustomContentCallbacks;
58import com.android.launcher3.Launcher.LauncherOverlay;
59import com.android.launcher3.UninstallDropTarget.DropTargetSource;
60import com.android.launcher3.accessibility.AccessibileDragListenerAdapter;
61import com.android.launcher3.accessibility.OverviewAccessibilityDelegate;
62import com.android.launcher3.accessibility.OverviewScreenAccessibilityDelegate;
63import com.android.launcher3.accessibility.WorkspaceAccessibilityHelper;
64import com.android.launcher3.compat.AppWidgetManagerCompat;
65import com.android.launcher3.compat.UserHandleCompat;
66import com.android.launcher3.config.FeatureFlags;
67import com.android.launcher3.config.ProviderConfig;
68import com.android.launcher3.dragndrop.DragController;
69import com.android.launcher3.dragndrop.DragLayer;
70import com.android.launcher3.dragndrop.DragOptions;
71import com.android.launcher3.dragndrop.DragScroller;
72import com.android.launcher3.dragndrop.DragView;
73import com.android.launcher3.dragndrop.SpringLoadedDragController;
74import com.android.launcher3.folder.Folder;
75import com.android.launcher3.folder.FolderIcon;
76import com.android.launcher3.graphics.DragPreviewProvider;
77import com.android.launcher3.shortcuts.DeepShortcutManager;
78import com.android.launcher3.shortcuts.ShortcutsContainerListener;
79import com.android.launcher3.userevent.nano.LauncherLogProto;
80import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
81import com.android.launcher3.util.ItemInfoMatcher;
82import com.android.launcher3.util.LongArrayMap;
83import com.android.launcher3.util.MultiStateAlphaController;
84import com.android.launcher3.util.Thunk;
85import com.android.launcher3.util.VerticalFlingDetector;
86import com.android.launcher3.util.WallpaperOffsetInterpolator;
87import com.android.launcher3.widget.PendingAddShortcutInfo;
88import com.android.launcher3.widget.PendingAddWidgetInfo;
89
90import java.util.ArrayList;
91import java.util.HashMap;
92import java.util.HashSet;
93
94/**
95 * The workspace is a wide area with a wallpaper and a finite number of pages.
96 * Each page contains a number of icons, folders or widgets the user can
97 * interact with. A workspace is meant to be used with a fixed width only.
98 */
99public class Workspace extends PagedView
100        implements DropTarget, DragSource, DragScroller, View.OnTouchListener,
101        DragController.DragListener, LauncherTransitionable, ViewGroup.OnHierarchyChangeListener,
102        Insettable, DropTargetSource {
103    private static final String TAG = "Launcher.Workspace";
104
105    private static boolean ENFORCE_DRAG_EVENT_ORDER = false;
106
107    private static final int SNAP_OFF_EMPTY_SCREEN_DURATION = 400;
108    private static final int FADE_EMPTY_SCREEN_DURATION = 150;
109
110    private static final int ADJACENT_SCREEN_DROP_DURATION = 300;
111
112    private static final boolean MAP_NO_RECURSE = false;
113    private static final boolean MAP_RECURSE = true;
114
115    // The screen id used for the empty screen always present to the right.
116    public static final long EXTRA_EMPTY_SCREEN_ID = -201;
117    // The is the first screen. It is always present, even if its empty.
118    public static final long FIRST_SCREEN_ID = 0;
119
120    private final static long CUSTOM_CONTENT_SCREEN_ID = -301;
121
122    private static final long CUSTOM_CONTENT_GESTURE_DELAY = 200;
123    private long mTouchDownTime = -1;
124    private long mCustomContentShowTime = -1;
125
126    private LayoutTransition mLayoutTransition;
127    @Thunk final WallpaperManager mWallpaperManager;
128
129    private ShortcutAndWidgetContainer mDragSourceInternal;
130
131    @Thunk LongArrayMap<CellLayout> mWorkspaceScreens = new LongArrayMap<>();
132    @Thunk ArrayList<Long> mScreenOrder = new ArrayList<Long>();
133
134    @Thunk Runnable mRemoveEmptyScreenRunnable;
135    @Thunk boolean mDeferRemoveExtraEmptyScreen = false;
136
137    /**
138     * CellInfo for the cell that is currently being dragged
139     */
140    private CellLayout.CellInfo mDragInfo;
141
142    /**
143     * Target drop area calculated during last acceptDrop call.
144     */
145    @Thunk int[] mTargetCell = new int[2];
146    private int mDragOverX = -1;
147    private int mDragOverY = -1;
148
149    CustomContentCallbacks mCustomContentCallbacks;
150    boolean mCustomContentShowing;
151    private float mLastCustomContentScrollProgress = -1f;
152    private String mCustomContentDescription = "";
153
154    /**
155     * The CellLayout that is currently being dragged over
156     */
157    @Thunk CellLayout mDragTargetLayout = null;
158    /**
159     * The CellLayout that we will show as highlighted
160     */
161    private CellLayout mDragOverlappingLayout = null;
162
163    /**
164     * The CellLayout which will be dropped to
165     */
166    private CellLayout mDropToLayout = null;
167
168    @Thunk Launcher mLauncher;
169    @Thunk IconCache mIconCache;
170    @Thunk DragController mDragController;
171
172    // These are temporary variables to prevent having to allocate a new object just to
173    // return an (x, y) value from helper functions. Do NOT use them to maintain other state.
174    private static final Rect sTempRect = new Rect();
175    private final int[] mTempXY = new int[2];
176    @Thunk float[] mDragViewVisualCenter = new float[2];
177    private float[] mTempCellLayoutCenterCoordinates = new float[2];
178    private int[] mTempVisiblePagesRange = new int[2];
179    private Matrix mTempMatrix = new Matrix();
180
181    private SpringLoadedDragController mSpringLoadedDragController;
182    private float mOverviewModeShrinkFactor;
183
184    // State variable that indicates whether the pages are small (ie when you're
185    // in all apps or customize mode)
186
187    public enum State {
188        NORMAL          (false, false),
189        NORMAL_HIDDEN   (false, false),
190        SPRING_LOADED   (false, true),
191        OVERVIEW        (true, true),
192        OVERVIEW_HIDDEN (true, false);
193
194        public final boolean shouldUpdateWidget;
195        public final boolean hasMultipleVisiblePages;
196
197        State(boolean shouldUpdateWidget, boolean hasMultipleVisiblePages) {
198            this.shouldUpdateWidget = shouldUpdateWidget;
199            this.hasMultipleVisiblePages = hasMultipleVisiblePages;
200        }
201    }
202
203    // Direction used for moving the workspace and hotseat UI
204    public enum Direction {
205        X  (TRANSLATION_X),
206        Y  (TRANSLATION_Y);
207
208        private final Property<View, Float> viewProperty;
209
210        Direction(Property<View, Float> viewProperty) {
211            this.viewProperty = viewProperty;
212        }
213    }
214
215    private static final int HOTSEAT_STATE_ALPHA_INDEX = 2;
216
217    /**
218     * These values correspond to {@link Direction#X} & {@link Direction#Y}
219     */
220    private float[] mPageAlpha = new float[] {1, 1};
221    /**
222     * Hotseat alpha can be changed when moving horizontally, vertically, changing states.
223     * The values correspond to {@link Direction#X}, {@link Direction#Y} &
224     * {@link #HOTSEAT_STATE_ALPHA_INDEX} respectively.
225     */
226    private float[] mHotseatAlpha = new float[] {1, 1, 1};
227
228    public static final int QSB_ALPHA_INDEX_STATE_CHANGE = 0;
229    public static final int QSB_ALPHA_INDEX_Y_TRANSLATION = 1;
230    public static final int QSB_ALPHA_INDEX_PAGE_SCROLL = 2;
231    public static final int QSB_ALPHA_INDEX_OVERLAY_SCROLL = 3;
232
233
234    MultiStateAlphaController mQsbAlphaController;
235
236    @ViewDebug.ExportedProperty(category = "launcher")
237    private State mState = State.NORMAL;
238    private boolean mIsSwitchingState = false;
239
240    boolean mAnimatingViewIntoPlace = false;
241    boolean mChildrenLayersEnabled = true;
242
243    private boolean mStripScreensOnPageStopMoving = false;
244
245    /** Is the user is dragging an item near the edge of a page? */
246    private boolean mInScrollArea = false;
247
248    private DragPreviewProvider mOutlineProvider = null;
249    public static final int DRAG_BITMAP_PADDING = DragPreviewProvider.DRAG_BITMAP_PADDING;
250    private boolean mWorkspaceFadeInAdjacentScreens;
251
252    final WallpaperOffsetInterpolator mWallpaperOffset;
253    private boolean mUnlockWallpaperFromDefaultPageOnLayout;
254
255    @Thunk Runnable mDelayedResizeRunnable;
256    private Runnable mDelayedSnapToPageRunnable;
257
258    // Variables relating to the creation of user folders by hovering shortcuts over shortcuts
259    private static final int FOLDER_CREATION_TIMEOUT = 0;
260    public static final int REORDER_TIMEOUT = 350;
261    private final Alarm mFolderCreationAlarm = new Alarm();
262    private final Alarm mReorderAlarm = new Alarm();
263    private FolderIcon.PreviewBackground mFolderCreateBg;
264    private FolderIcon mDragOverFolderIcon = null;
265    private boolean mCreateUserFolderOnDrop = false;
266    private boolean mAddToExistingFolderOnDrop = false;
267    private float mMaxDistanceForFolderCreation;
268
269    private final Canvas mCanvas = new Canvas();
270
271    // Variables relating to touch disambiguation (scrolling workspace vs. scrolling a widget)
272    private float mXDown;
273    private float mYDown;
274    final static float START_DAMPING_TOUCH_SLOP_ANGLE = (float) Math.PI / 6;
275    final static float MAX_SWIPE_ANGLE = (float) Math.PI / 3;
276    final static float TOUCH_SLOP_DAMPING_FACTOR = 4;
277
278    // Relating to the animation of items being dropped externally
279    public static final int ANIMATE_INTO_POSITION_AND_DISAPPEAR = 0;
280    public static final int ANIMATE_INTO_POSITION_AND_REMAIN = 1;
281    public static final int ANIMATE_INTO_POSITION_AND_RESIZE = 2;
282    public static final int COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION = 3;
283    public static final int CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION = 4;
284
285    // Related to dragging, folder creation and reordering
286    private static final int DRAG_MODE_NONE = 0;
287    private static final int DRAG_MODE_CREATE_FOLDER = 1;
288    private static final int DRAG_MODE_ADD_TO_FOLDER = 2;
289    private static final int DRAG_MODE_REORDER = 3;
290    private int mDragMode = DRAG_MODE_NONE;
291    @Thunk int mLastReorderX = -1;
292    @Thunk int mLastReorderY = -1;
293
294    private SparseArray<Parcelable> mSavedStates;
295    private final ArrayList<Integer> mRestoredPages = new ArrayList<Integer>();
296
297    private float mCurrentScale;
298    private float mTransitionProgress;
299
300    @Thunk Runnable mDeferredAction;
301    private boolean mDeferDropAfterUninstall;
302    private boolean mUninstallSuccessful;
303
304    // State related to Launcher Overlay
305    LauncherOverlay mLauncherOverlay;
306    boolean mScrollInteractionBegan;
307    boolean mStartedSendingScrollEvents;
308    float mLastOverlaySroll = 0;
309    // Total over scrollX in the overlay direction.
310    private int mUnboundedScrollX;
311    private boolean mForceDrawAdjacentPages = false;
312    // Total over scrollX in the overlay direction.
313    private float mOverlayTranslation;
314    private int mFirstPageScrollX;
315    private boolean mIgnoreQsbScroll;
316
317    // Handles workspace state transitions
318    private WorkspaceStateTransitionAnimation mStateTransitionAnimation;
319
320    private AccessibilityDelegate mPagesAccessibilityDelegate;
321    private OnStateChangeListener mOnStateChangeListener;
322
323    /**
324     * Used to inflate the Workspace from XML.
325     *
326     * @param context The application's context.
327     * @param attrs The attributes set containing the Workspace's customization values.
328     */
329    public Workspace(Context context, AttributeSet attrs) {
330        this(context, attrs, 0);
331    }
332
333    /**
334     * Used to inflate the Workspace from XML.
335     *
336     * @param context The application's context.
337     * @param attrs The attributes set containing the Workspace's customization values.
338     * @param defStyle Unused.
339     */
340    public Workspace(Context context, AttributeSet attrs, int defStyle) {
341        super(context, attrs, defStyle);
342
343        mLauncher = (Launcher) context;
344        mStateTransitionAnimation = new WorkspaceStateTransitionAnimation(mLauncher, this);
345        final Resources res = getResources();
346        DeviceProfile grid = mLauncher.getDeviceProfile();
347        mWorkspaceFadeInAdjacentScreens = grid.shouldFadeAdjacentWorkspaceScreens();
348        mWallpaperManager = WallpaperManager.getInstance(context);
349
350        mWallpaperOffset = new WallpaperOffsetInterpolator(this);
351        mOverviewModeShrinkFactor =
352                res.getInteger(R.integer.config_workspaceOverviewShrinkPercentage) / 100f;
353
354        setOnHierarchyChangeListener(this);
355        setHapticFeedbackEnabled(false);
356
357        initWorkspace();
358
359        // Disable multitouch across the workspace/all apps/customize tray
360        setMotionEventSplittingEnabled(true);
361    }
362
363    @Override
364    public void setInsets(Rect insets) {
365        mInsets.set(insets);
366
367        CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID);
368        if (customScreen != null) {
369            View customContent = customScreen.getShortcutsAndWidgets().getChildAt(0);
370            if (customContent instanceof Insettable) {
371                ((Insettable) customContent).setInsets(mInsets);
372            }
373        }
374    }
375
376    public void setOnStateChangeListener(OnStateChangeListener listener) {
377        mOnStateChangeListener = listener;
378    }
379
380    // estimate the size of a widget with spans hSpan, vSpan. return MAX_VALUE for each
381    // dimension if unsuccessful
382    public int[] estimateItemSize(ItemInfo itemInfo, boolean springLoaded) {
383        float shrinkFactor = mLauncher.getDeviceProfile().workspaceSpringLoadShrinkFactor;
384        int[] size = new int[2];
385        if (getChildCount() > 0) {
386            // Use the first non-custom page to estimate the child position
387            CellLayout cl = (CellLayout) getChildAt(numCustomPages());
388            Rect r = estimateItemPosition(cl, 0, 0, itemInfo.spanX, itemInfo.spanY);
389            size[0] = r.width();
390            size[1] = r.height();
391            if (springLoaded) {
392                size[0] *= shrinkFactor;
393                size[1] *= shrinkFactor;
394            }
395            return size;
396        } else {
397            size[0] = Integer.MAX_VALUE;
398            size[1] = Integer.MAX_VALUE;
399            return size;
400        }
401    }
402
403    public Rect estimateItemPosition(CellLayout cl, int hCell, int vCell, int hSpan, int vSpan) {
404        Rect r = new Rect();
405        cl.cellToRect(hCell, vCell, hSpan, vSpan, r);
406        return r;
407    }
408
409    @Override
410    public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
411        if (ENFORCE_DRAG_EVENT_ORDER) {
412            enfoceDragParity("onDragStart", 0, 0);
413        }
414
415        if (mOutlineProvider != null) {
416            // The outline is used to visualize where the item will land if dropped
417            mOutlineProvider.generateDragOutline(mCanvas);
418        }
419
420        updateChildrenLayersEnabled(false);
421        mLauncher.onDragStarted();
422        mLauncher.lockScreenOrientation();
423        mLauncher.onInteractionBegin();
424        // Prevent any Un/InstallShortcutReceivers from updating the db while we are dragging
425        InstallShortcutReceiver.enableInstallQueue();
426
427        // Do not add a new page if it is a accessible drag which was not started by the workspace.
428        // We do not support accessibility drag from other sources and instead provide a direct
429        // action for move/add to homescreen.
430        // When a accessible drag is started by the folder, we only allow rearranging withing the
431        // folder.
432        boolean addNewPage = !(options.isAccessibleDrag && dragObject.dragSource != this);
433
434        if (addNewPage) {
435            mDeferRemoveExtraEmptyScreen = false;
436            addExtraEmptyScreenOnDrag();
437
438            if (dragObject.dragInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET
439                    && dragObject.dragSource != this) {
440                // When dragging a widget from different source, move to a page which has
441                // enough space to place this widget (after rearranging/resizing). We special case
442                // widgets as they cannot be placed inside a folder.
443                // Start at the current page and search right (on LTR) until finding a page with
444                // enough space. Since an empty screen is the furthest right, a page must be found.
445                int currentPage = getPageNearestToCenterOfScreen();
446                for (int pageIndex = currentPage; pageIndex < getPageCount(); pageIndex++) {
447                    CellLayout page = (CellLayout) getPageAt(pageIndex);
448                    if (page.hasReorderSolution(dragObject.dragInfo)) {
449                        setCurrentPage(pageIndex);
450                        break;
451                    }
452                }
453            }
454        }
455
456        if (!FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
457            // Always enter the spring loaded mode
458            mLauncher.enterSpringLoadedDragMode();
459        }
460    }
461
462    public void deferRemoveExtraEmptyScreen() {
463        mDeferRemoveExtraEmptyScreen = true;
464    }
465
466    @Override
467    public void onDragEnd() {
468        if (ENFORCE_DRAG_EVENT_ORDER) {
469            enfoceDragParity("onDragEnd", 0, 0);
470        }
471
472        if (!mDeferRemoveExtraEmptyScreen) {
473            removeExtraEmptyScreen(true, mDragSourceInternal != null);
474        }
475
476        updateChildrenLayersEnabled(false);
477        mLauncher.unlockScreenOrientation(false);
478
479        // Re-enable any Un/InstallShortcutReceiver and now process any queued items
480        InstallShortcutReceiver.disableAndFlushInstallQueue(getContext());
481
482        mDragSourceInternal = null;
483        mLauncher.onInteractionEnd();
484    }
485
486    /**
487     * Initializes various states for this workspace.
488     */
489    protected void initWorkspace() {
490        mCurrentPage = getDefaultPage();
491        LauncherAppState app = LauncherAppState.getInstance();
492        DeviceProfile grid = mLauncher.getDeviceProfile();
493        mIconCache = app.getIconCache();
494        setWillNotDraw(false);
495        setClipChildren(false);
496        setClipToPadding(false);
497        setChildrenDrawnWithCacheEnabled(true);
498
499        setMinScale(mOverviewModeShrinkFactor);
500        setupLayoutTransition();
501
502        mMaxDistanceForFolderCreation = (0.55f * grid.iconSizePx);
503
504        // Set the wallpaper dimensions when Launcher starts up
505        setWallpaperDimension();
506
507        setEdgeGlowColor(getResources().getColor(R.color.workspace_edge_effect_color));
508    }
509
510    @Override
511    public void initParentViews(View parent) {
512        super.initParentViews(parent);
513        mPageIndicator.setAccessibilityDelegate(new OverviewAccessibilityDelegate());
514        mQsbAlphaController = new MultiStateAlphaController(mLauncher.getQsbContainer(), 4);
515    }
516
517    private int getDefaultPage() {
518        return numCustomPages();
519    }
520
521    private void setupLayoutTransition() {
522        // We want to show layout transitions when pages are deleted, to close the gap.
523        mLayoutTransition = new LayoutTransition();
524        mLayoutTransition.enableTransitionType(LayoutTransition.DISAPPEARING);
525        mLayoutTransition.enableTransitionType(LayoutTransition.CHANGE_DISAPPEARING);
526        mLayoutTransition.disableTransitionType(LayoutTransition.APPEARING);
527        mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING);
528        setLayoutTransition(mLayoutTransition);
529    }
530
531    void enableLayoutTransitions() {
532        setLayoutTransition(mLayoutTransition);
533    }
534    void disableLayoutTransitions() {
535        setLayoutTransition(null);
536    }
537
538    @Override
539    public void onChildViewAdded(View parent, View child) {
540        if (!(child instanceof CellLayout)) {
541            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
542        }
543        CellLayout cl = ((CellLayout) child);
544        cl.setOnInterceptTouchListener(this);
545        cl.setClickable(true);
546        cl.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
547        super.onChildViewAdded(parent, child);
548    }
549
550    protected boolean shouldDrawChild(View child) {
551        final CellLayout cl = (CellLayout) child;
552        return super.shouldDrawChild(child) &&
553            (mIsSwitchingState ||
554             cl.getShortcutsAndWidgets().getAlpha() > 0 ||
555             cl.getBackgroundAlpha() > 0);
556    }
557
558    /**
559     * @return The open folder on the current screen, or null if there is none
560     */
561    public Folder getOpenFolder() {
562        DragLayer dragLayer = mLauncher.getDragLayer();
563        // Iterate in reverse order. Folder is added later to the dragLayer,
564        // and will be one of the last views.
565        for (int i = dragLayer.getChildCount() - 1; i >= 0; i--) {
566            View child = dragLayer.getChildAt(i);
567            if (child instanceof Folder) {
568                Folder folder = (Folder) child;
569                if (folder.getInfo().opened)
570                    return folder;
571            }
572        }
573        return null;
574    }
575
576    boolean isTouchActive() {
577        return mTouchState != TOUCH_STATE_REST;
578    }
579
580    private int getEmbeddedQsbId() {
581        return mLauncher.getDeviceProfile().isVerticalBarLayout()
582                ? R.id.qsb_container : R.id.workspace_blocked_row;
583    }
584
585    /**
586     * Initializes and binds the first page
587     * @param qsb an exisitng qsb to recycle or null.
588     */
589    public void bindAndInitFirstWorkspaceScreen(View qsb) {
590        if (!FeatureFlags.QSB_ON_FIRST_SCREEN) {
591            return;
592        }
593        // Add the first page
594        CellLayout firstPage = insertNewWorkspaceScreen(Workspace.FIRST_SCREEN_ID, 0);
595        if (FeatureFlags.PULLDOWN_SEARCH) {
596            firstPage.setOnTouchListener(new VerticalFlingDetector(mLauncher) {
597                // detect fling when touch started from empty space
598                @Override
599                public boolean onTouch(View v, MotionEvent ev) {
600                    if (workspaceInModalState()) return false;
601                    if (shouldConsumeTouch(v)) return true;
602                    if (super.onTouch(v, ev)) {
603                        mLauncher.startSearch("", false, null, false);
604                    }
605                    return false;
606                }
607            });
608            firstPage.setOnInterceptTouchListener(new VerticalFlingDetector(mLauncher) {
609                // detect fling when touch started from on top of the icons
610                @Override
611                public boolean onTouch(View v, MotionEvent ev) {
612                    if (shouldConsumeTouch(v)) return true;
613                    if (super.onTouch(v, ev)) {
614                        mLauncher.startSearch("", false, null, false);
615                    }
616                    return false;
617                }
618            });
619        }
620        // Always add a QSB on the first screen.
621        if (qsb == null) {
622            // In transposed layout, we add the QSB in the Grid. As workspace does not touch the
623            // edges, we do not need a full width QSB.
624            qsb = mLauncher.getLayoutInflater().inflate(
625                    mLauncher.getDeviceProfile().isVerticalBarLayout()
626                            ? R.layout.qsb_container : R.layout.qsb_blocker_view,
627                    firstPage, false);
628        }
629
630        CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, firstPage.getCountX(), 1);
631        lp.canReorder = false;
632        if (!firstPage.addViewToCellLayout(qsb, 0, getEmbeddedQsbId(), lp, true)) {
633            Log.e(TAG, "Failed to add to item at (0, 0) to CellLayout");
634        }
635    }
636
637    @Override
638    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
639        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
640
641        // Update the QSB to match the cell height. This is treating the QSB essentially as a child
642        // of workspace despite that it's not a true child.
643        // Note that it relies on the strict ordering of measuring the workspace before the QSB
644        // at the dragLayer level.
645        if (getChildCount() > 0) {
646            CellLayout firstPage = (CellLayout) getChildAt(0);
647            int cellHeight = firstPage.getCellHeight();
648
649            View qsbContainer = mLauncher.getQsbContainer();
650            ViewGroup.LayoutParams lp = qsbContainer.getLayoutParams();
651            if (cellHeight > 0 && lp.height != cellHeight) {
652                lp.height = cellHeight;
653                qsbContainer.setLayoutParams(lp);
654            }
655        }
656    }
657
658    public void removeAllWorkspaceScreens() {
659        // Disable all layout transitions before removing all pages to ensure that we don't get the
660        // transition animations competing with us changing the scroll when we add pages or the
661        // custom content screen
662        disableLayoutTransitions();
663
664        // Since we increment the current page when we call addCustomContentPage via bindScreens
665        // (and other places), we need to adjust the current page back when we clear the pages
666        if (hasCustomContent()) {
667            removeCustomContentPage();
668        }
669
670        // Recycle the QSB widget
671        View qsb = findViewById(getEmbeddedQsbId());
672        if (qsb != null) {
673            ((ViewGroup) qsb.getParent()).removeView(qsb);
674        }
675
676        // Remove the pages and clear the screen models
677        removeAllViews();
678        mScreenOrder.clear();
679        mWorkspaceScreens.clear();
680
681        // Ensure that the first page is always present
682        bindAndInitFirstWorkspaceScreen(qsb);
683
684        // Re-enable the layout transitions
685        enableLayoutTransitions();
686    }
687
688    public void insertNewWorkspaceScreenBeforeEmptyScreen(long screenId) {
689        // Find the index to insert this view into.  If the empty screen exists, then
690        // insert it before that.
691        int insertIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID);
692        if (insertIndex < 0) {
693            insertIndex = mScreenOrder.size();
694        }
695        insertNewWorkspaceScreen(screenId, insertIndex);
696    }
697
698    public void insertNewWorkspaceScreen(long screenId) {
699        insertNewWorkspaceScreen(screenId, getChildCount());
700    }
701
702    public CellLayout insertNewWorkspaceScreen(long screenId, int insertIndex) {
703        if (mWorkspaceScreens.containsKey(screenId)) {
704            throw new RuntimeException("Screen id " + screenId + " already exists!");
705        }
706
707        // Inflate the cell layout, but do not add it automatically so that we can get the newly
708        // created CellLayout.
709        CellLayout newScreen = (CellLayout) mLauncher.getLayoutInflater().inflate(
710                        R.layout.workspace_screen, this, false /* attachToRoot */);
711        newScreen.setOnLongClickListener(mLongClickListener);
712        newScreen.setOnClickListener(mLauncher);
713        newScreen.setSoundEffectsEnabled(false);
714        mWorkspaceScreens.put(screenId, newScreen);
715        mScreenOrder.add(insertIndex, screenId);
716        addView(newScreen, insertIndex);
717
718        if (mLauncher.getAccessibilityDelegate().isInAccessibleDrag()) {
719            newScreen.enableAccessibleDrag(true, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG);
720        }
721
722        return newScreen;
723    }
724
725    public void createCustomContentContainer() {
726        CellLayout customScreen = (CellLayout)
727                mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, this, false);
728        customScreen.disableDragTarget();
729        customScreen.disableJailContent();
730
731        mWorkspaceScreens.put(CUSTOM_CONTENT_SCREEN_ID, customScreen);
732        mScreenOrder.add(0, CUSTOM_CONTENT_SCREEN_ID);
733
734        // We want no padding on the custom content
735        customScreen.setPadding(0, 0, 0, 0);
736
737        addFullScreenPage(customScreen);
738
739        // Update the custom content hint
740        if (mRestorePage != INVALID_RESTORE_PAGE) {
741            mRestorePage = mRestorePage + 1;
742        } else {
743            setCurrentPage(getCurrentPage() + 1);
744        }
745    }
746
747    public void removeCustomContentPage() {
748        CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID);
749        if (customScreen == null) {
750            throw new RuntimeException("Expected custom content screen to exist");
751        }
752
753        mWorkspaceScreens.remove(CUSTOM_CONTENT_SCREEN_ID);
754        mScreenOrder.remove(CUSTOM_CONTENT_SCREEN_ID);
755        removeView(customScreen);
756
757        if (mCustomContentCallbacks != null) {
758            mCustomContentCallbacks.onScrollProgressChanged(0);
759            mCustomContentCallbacks.onHide();
760        }
761
762        mCustomContentCallbacks = null;
763
764        // Update the custom content hint
765        if (mRestorePage != INVALID_RESTORE_PAGE) {
766            mRestorePage = mRestorePage - 1;
767        } else {
768            setCurrentPage(getCurrentPage() - 1);
769        }
770    }
771
772    public void addToCustomContentPage(View customContent, CustomContentCallbacks callbacks,
773            String description) {
774        if (getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID) < 0) {
775            throw new RuntimeException("Expected custom content screen to exist");
776        }
777
778        // Add the custom content to the full screen custom page
779        CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID);
780        int spanX = customScreen.getCountX();
781        int spanY = customScreen.getCountY();
782        CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, spanX, spanY);
783        lp.canReorder  = false;
784        lp.isFullscreen = true;
785        if (customContent instanceof Insettable) {
786            ((Insettable)customContent).setInsets(mInsets);
787        }
788
789        // Verify that the child is removed from any existing parent.
790        if (customContent.getParent() instanceof ViewGroup) {
791            ViewGroup parent = (ViewGroup) customContent.getParent();
792            parent.removeView(customContent);
793        }
794        customScreen.removeAllViews();
795        customContent.setFocusable(true);
796        customContent.setOnKeyListener(new FullscreenKeyEventListener());
797        customContent.setOnFocusChangeListener(mLauncher.mFocusHandler
798                .getHideIndicatorOnFocusListener());
799        customScreen.addViewToCellLayout(customContent, 0, 0, lp, true);
800        mCustomContentDescription = description;
801
802        mCustomContentCallbacks = callbacks;
803    }
804
805    public void addExtraEmptyScreenOnDrag() {
806        boolean lastChildOnScreen = false;
807        boolean childOnFinalScreen = false;
808
809        // Cancel any pending removal of empty screen
810        mRemoveEmptyScreenRunnable = null;
811
812        if (mDragSourceInternal != null) {
813            if (mDragSourceInternal.getChildCount() == 1) {
814                lastChildOnScreen = true;
815            }
816            CellLayout cl = (CellLayout) mDragSourceInternal.getParent();
817            if (indexOfChild(cl) == getChildCount() - 1) {
818                childOnFinalScreen = true;
819            }
820        }
821
822        // If this is the last item on the final screen
823        if (lastChildOnScreen && childOnFinalScreen) {
824            return;
825        }
826        if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) {
827            insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID);
828        }
829    }
830
831    public boolean addExtraEmptyScreen() {
832        if (!mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID)) {
833            insertNewWorkspaceScreen(EXTRA_EMPTY_SCREEN_ID);
834            return true;
835        }
836        return false;
837    }
838
839    private void convertFinalScreenToEmptyScreenIfNecessary() {
840        if (mLauncher.isWorkspaceLoading()) {
841            // Invalid and dangerous operation if workspace is loading
842            return;
843        }
844
845        if (hasExtraEmptyScreen() || mScreenOrder.size() == 0) return;
846        long finalScreenId = mScreenOrder.get(mScreenOrder.size() - 1);
847
848        if (finalScreenId == CUSTOM_CONTENT_SCREEN_ID) return;
849        CellLayout finalScreen = mWorkspaceScreens.get(finalScreenId);
850
851        // If the final screen is empty, convert it to the extra empty screen
852        if (finalScreen.getShortcutsAndWidgets().getChildCount() == 0 &&
853                !finalScreen.isDropPending()) {
854            mWorkspaceScreens.remove(finalScreenId);
855            mScreenOrder.remove(finalScreenId);
856
857            // if this is the last non-custom content screen, convert it to the empty screen
858            mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, finalScreen);
859            mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID);
860
861            // Update the model if we have changed any screens
862            mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
863        }
864    }
865
866    public void removeExtraEmptyScreen(final boolean animate, boolean stripEmptyScreens) {
867        removeExtraEmptyScreenDelayed(animate, null, 0, stripEmptyScreens);
868    }
869
870    public void removeExtraEmptyScreenDelayed(final boolean animate, final Runnable onComplete,
871            final int delay, final boolean stripEmptyScreens) {
872        if (mLauncher.isWorkspaceLoading()) {
873            // Don't strip empty screens if the workspace is still loading
874            return;
875        }
876
877        if (delay > 0) {
878            postDelayed(new Runnable() {
879                @Override
880                public void run() {
881                    removeExtraEmptyScreenDelayed(animate, onComplete, 0, stripEmptyScreens);
882                }
883            }, delay);
884            return;
885        }
886
887        convertFinalScreenToEmptyScreenIfNecessary();
888        if (hasExtraEmptyScreen()) {
889            int emptyIndex = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID);
890            if (getNextPage() == emptyIndex) {
891                snapToPage(getNextPage() - 1, SNAP_OFF_EMPTY_SCREEN_DURATION);
892                fadeAndRemoveEmptyScreen(SNAP_OFF_EMPTY_SCREEN_DURATION, FADE_EMPTY_SCREEN_DURATION,
893                        onComplete, stripEmptyScreens);
894            } else {
895                snapToPage(getNextPage(), 0);
896                fadeAndRemoveEmptyScreen(0, FADE_EMPTY_SCREEN_DURATION,
897                        onComplete, stripEmptyScreens);
898            }
899            return;
900        } else if (stripEmptyScreens) {
901            // If we're not going to strip the empty screens after removing
902            // the extra empty screen, do it right away.
903            stripEmptyScreens();
904        }
905
906        if (onComplete != null) {
907            onComplete.run();
908        }
909    }
910
911    private void fadeAndRemoveEmptyScreen(int delay, int duration, final Runnable onComplete,
912            final boolean stripEmptyScreens) {
913        // XXX: Do we need to update LM workspace screens below?
914        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
915        PropertyValuesHolder bgAlpha = PropertyValuesHolder.ofFloat("backgroundAlpha", 0f);
916
917        final CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID);
918
919        mRemoveEmptyScreenRunnable = new Runnable() {
920            @Override
921            public void run() {
922                if (hasExtraEmptyScreen()) {
923                    mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID);
924                    mScreenOrder.remove(EXTRA_EMPTY_SCREEN_ID);
925                    removeView(cl);
926                    if (stripEmptyScreens) {
927                        stripEmptyScreens();
928                    }
929                    // Update the page indicator to reflect the removed page.
930                    showPageIndicatorAtCurrentScroll();
931                }
932            }
933        };
934
935        ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(cl, alpha, bgAlpha);
936        oa.setDuration(duration);
937        oa.setStartDelay(delay);
938        oa.addListener(new AnimatorListenerAdapter() {
939            @Override
940            public void onAnimationEnd(Animator animation) {
941                if (mRemoveEmptyScreenRunnable != null) {
942                    mRemoveEmptyScreenRunnable.run();
943                }
944                if (onComplete != null) {
945                    onComplete.run();
946                }
947            }
948        });
949        oa.start();
950    }
951
952    public boolean hasExtraEmptyScreen() {
953        int nScreens = getChildCount();
954        nScreens = nScreens - numCustomPages();
955        return mWorkspaceScreens.containsKey(EXTRA_EMPTY_SCREEN_ID) && nScreens > 1;
956    }
957
958    public long commitExtraEmptyScreen() {
959        if (mLauncher.isWorkspaceLoading()) {
960            // Invalid and dangerous operation if workspace is loading
961            return -1;
962        }
963
964        int index = getPageIndexForScreenId(EXTRA_EMPTY_SCREEN_ID);
965        CellLayout cl = mWorkspaceScreens.get(EXTRA_EMPTY_SCREEN_ID);
966        mWorkspaceScreens.remove(EXTRA_EMPTY_SCREEN_ID);
967        mScreenOrder.remove(EXTRA_EMPTY_SCREEN_ID);
968
969        long newId = LauncherSettings.Settings.call(getContext().getContentResolver(),
970                LauncherSettings.Settings.METHOD_NEW_SCREEN_ID)
971                .getLong(LauncherSettings.Settings.EXTRA_VALUE);
972        mWorkspaceScreens.put(newId, cl);
973        mScreenOrder.add(newId);
974
975        // Update the model for the new screen
976        mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
977
978        return newId;
979    }
980
981    public CellLayout getScreenWithId(long screenId) {
982        return mWorkspaceScreens.get(screenId);
983    }
984
985    public long getIdForScreen(CellLayout layout) {
986        int index = mWorkspaceScreens.indexOfValue(layout);
987        if (index != -1) {
988            return mWorkspaceScreens.keyAt(index);
989        }
990        return -1;
991    }
992
993    public int getPageIndexForScreenId(long screenId) {
994        return indexOfChild(mWorkspaceScreens.get(screenId));
995    }
996
997    public long getScreenIdForPageIndex(int index) {
998        if (0 <= index && index < mScreenOrder.size()) {
999            return mScreenOrder.get(index);
1000        }
1001        return -1;
1002    }
1003
1004    public ArrayList<Long> getScreenOrder() {
1005        return mScreenOrder;
1006    }
1007
1008    public void stripEmptyScreens() {
1009        if (mLauncher.isWorkspaceLoading()) {
1010            // Don't strip empty screens if the workspace is still loading.
1011            // This is dangerous and can result in data loss.
1012            return;
1013        }
1014
1015        if (isPageMoving()) {
1016            mStripScreensOnPageStopMoving = true;
1017            return;
1018        }
1019
1020        int currentPage = getNextPage();
1021        ArrayList<Long> removeScreens = new ArrayList<Long>();
1022        int total = mWorkspaceScreens.size();
1023        for (int i = 0; i < total; i++) {
1024            long id = mWorkspaceScreens.keyAt(i);
1025            CellLayout cl = mWorkspaceScreens.valueAt(i);
1026            // FIRST_SCREEN_ID can never be removed.
1027            if ((!FeatureFlags.QSB_ON_FIRST_SCREEN || id > FIRST_SCREEN_ID)
1028                    && cl.getShortcutsAndWidgets().getChildCount() == 0) {
1029                removeScreens.add(id);
1030            }
1031        }
1032
1033        boolean isInAccessibleDrag = mLauncher.getAccessibilityDelegate().isInAccessibleDrag();
1034
1035        // We enforce at least one page to add new items to. In the case that we remove the last
1036        // such screen, we convert the last screen to the empty screen
1037        int minScreens = 1 + numCustomPages();
1038
1039        int pageShift = 0;
1040        for (Long id: removeScreens) {
1041            CellLayout cl = mWorkspaceScreens.get(id);
1042            mWorkspaceScreens.remove(id);
1043            mScreenOrder.remove(id);
1044
1045            if (getChildCount() > minScreens) {
1046                if (indexOfChild(cl) < currentPage) {
1047                    pageShift++;
1048                }
1049
1050                if (isInAccessibleDrag) {
1051                    cl.enableAccessibleDrag(false, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG);
1052                }
1053
1054                removeView(cl);
1055            } else {
1056                // if this is the last non-custom content screen, convert it to the empty screen
1057                mRemoveEmptyScreenRunnable = null;
1058                mWorkspaceScreens.put(EXTRA_EMPTY_SCREEN_ID, cl);
1059                mScreenOrder.add(EXTRA_EMPTY_SCREEN_ID);
1060            }
1061        }
1062
1063        if (!removeScreens.isEmpty()) {
1064            // Update the model if we have changed any screens
1065            mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
1066        }
1067
1068        if (pageShift >= 0) {
1069            setCurrentPage(currentPage - pageShift);
1070        }
1071    }
1072
1073    // See implementation for parameter definition.
1074    void addInScreen(View child, long container, long screenId,
1075            int x, int y, int spanX, int spanY) {
1076        addInScreen(child, container, screenId, x, y, spanX, spanY, false, false);
1077    }
1078
1079    // At bind time, we use the rank (screenId) to compute x and y for hotseat items.
1080    // See implementation for parameter definition.
1081    public void addInScreenFromBind(View child, long container, long screenId, int x, int y,
1082            int spanX, int spanY) {
1083        addInScreen(child, container, screenId, x, y, spanX, spanY, false, true);
1084    }
1085
1086    // See implementation for parameter definition.
1087    void addInScreen(View child, long container, long screenId, int x, int y, int spanX, int spanY,
1088            boolean insert) {
1089        addInScreen(child, container, screenId, x, y, spanX, spanY, insert, false);
1090    }
1091
1092    /**
1093     * Adds the specified child in the specified screen. The position and dimension of
1094     * the child are defined by x, y, spanX and spanY.
1095     *
1096     * @param child The child to add in one of the workspace's screens.
1097     * @param screenId The screen in which to add the child.
1098     * @param x The X position of the child in the screen's grid.
1099     * @param y The Y position of the child in the screen's grid.
1100     * @param spanX The number of cells spanned horizontally by the child.
1101     * @param spanY The number of cells spanned vertically by the child.
1102     * @param insert When true, the child is inserted at the beginning of the children list.
1103     * @param computeXYFromRank When true, we use the rank (stored in screenId) to compute
1104     *                          the x and y position in which to place hotseat items. Otherwise
1105     *                          we use the x and y position to compute the rank.
1106     */
1107    void addInScreen(View child, long container, long screenId, int x, int y, int spanX, int spanY,
1108            boolean insert, boolean computeXYFromRank) {
1109        if (container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
1110            if (getScreenWithId(screenId) == null) {
1111                Log.e(TAG, "Skipping child, screenId " + screenId + " not found");
1112                // DEBUGGING - Print out the stack trace to see where we are adding from
1113                new Throwable().printStackTrace();
1114                return;
1115            }
1116        }
1117        if (screenId == EXTRA_EMPTY_SCREEN_ID) {
1118            // This should never happen
1119            throw new RuntimeException("Screen id should not be EXTRA_EMPTY_SCREEN_ID");
1120        }
1121
1122        final CellLayout layout;
1123        if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1124            layout = mLauncher.getHotseat().getLayout();
1125            child.setOnKeyListener(new HotseatIconKeyEventListener());
1126
1127            // Hide folder title in the hotseat
1128            if (child instanceof FolderIcon) {
1129                ((FolderIcon) child).setTextVisible(false);
1130            }
1131
1132            if (computeXYFromRank) {
1133                x = mLauncher.getHotseat().getCellXFromOrder((int) screenId);
1134                y = mLauncher.getHotseat().getCellYFromOrder((int) screenId);
1135            } else {
1136                screenId = mLauncher.getHotseat().getOrderInHotseat(x, y);
1137            }
1138        } else {
1139            // Show folder title if not in the hotseat
1140            if (child instanceof FolderIcon) {
1141                ((FolderIcon) child).setTextVisible(true);
1142            }
1143            layout = getScreenWithId(screenId);
1144            child.setOnKeyListener(new IconKeyEventListener());
1145        }
1146
1147        ViewGroup.LayoutParams genericLp = child.getLayoutParams();
1148        CellLayout.LayoutParams lp;
1149        if (genericLp == null || !(genericLp instanceof CellLayout.LayoutParams)) {
1150            lp = new CellLayout.LayoutParams(x, y, spanX, spanY);
1151        } else {
1152            lp = (CellLayout.LayoutParams) genericLp;
1153            lp.cellX = x;
1154            lp.cellY = y;
1155            lp.cellHSpan = spanX;
1156            lp.cellVSpan = spanY;
1157        }
1158
1159        if (spanX < 0 && spanY < 0) {
1160            lp.isLockedToGrid = false;
1161        }
1162
1163        // Get the canonical child id to uniquely represent this view in this screen
1164        ItemInfo info = (ItemInfo) child.getTag();
1165        int childId = mLauncher.getViewIdForItem(info);
1166
1167        boolean markCellsAsOccupied = !(child instanceof Folder);
1168        if (!layout.addViewToCellLayout(child, insert ? 0 : -1, childId, lp, markCellsAsOccupied)) {
1169            // TODO: This branch occurs when the workspace is adding views
1170            // outside of the defined grid
1171            // maybe we should be deleting these items from the LauncherModel?
1172            Log.e(TAG, "Failed to add to item at (" + lp.cellX + "," + lp.cellY + ") to CellLayout");
1173        }
1174
1175        if (!(child instanceof Folder)) {
1176            child.setHapticFeedbackEnabled(false);
1177            child.setOnLongClickListener(mLongClickListener);
1178            if (child instanceof BubbleTextView && DeepShortcutManager.supportsShortcuts(info)) {
1179                // TODO: only add this listener if the item has shortcuts associated with it.
1180                child.setOnTouchListener(new ShortcutsContainerListener((BubbleTextView) child));
1181            }
1182        }
1183        if (child instanceof DropTarget) {
1184            mDragController.addDropTarget((DropTarget) child);
1185        }
1186    }
1187
1188    /**
1189     * Called directly from a CellLayout (not by the framework), after we've been added as a
1190     * listener via setOnInterceptTouchEventListener(). This allows us to tell the CellLayout
1191     * that it should intercept touch events, which is not something that is normally supported.
1192     */
1193    @SuppressLint("ClickableViewAccessibility")
1194    @Override
1195    public boolean onTouch(View v, MotionEvent event) {
1196        return shouldConsumeTouch(v);
1197    }
1198
1199    private boolean shouldConsumeTouch(View v) {
1200        return (workspaceInModalState() || !isFinishedSwitchingState())
1201                || (!workspaceInModalState() && indexOfChild(v) != mCurrentPage);
1202    }
1203
1204    public boolean isSwitchingState() {
1205        return mIsSwitchingState;
1206    }
1207
1208    /** This differs from isSwitchingState in that we take into account how far the transition
1209     *  has completed. */
1210    public boolean isFinishedSwitchingState() {
1211        return !mIsSwitchingState || (mTransitionProgress > 0.5f);
1212    }
1213
1214    protected void onWindowVisibilityChanged (int visibility) {
1215        mLauncher.onWindowVisibilityChanged(visibility);
1216    }
1217
1218    @Override
1219    public boolean dispatchUnhandledMove(View focused, int direction) {
1220        if (workspaceInModalState() || !isFinishedSwitchingState()) {
1221            // when the home screens are shrunken, shouldn't allow side-scrolling
1222            return false;
1223        }
1224        return super.dispatchUnhandledMove(focused, direction);
1225    }
1226
1227    @Override
1228    public boolean onInterceptTouchEvent(MotionEvent ev) {
1229        switch (ev.getAction() & MotionEvent.ACTION_MASK) {
1230        case MotionEvent.ACTION_DOWN:
1231            mXDown = ev.getX();
1232            mYDown = ev.getY();
1233            mTouchDownTime = System.currentTimeMillis();
1234            break;
1235        case MotionEvent.ACTION_POINTER_UP:
1236        case MotionEvent.ACTION_UP:
1237            if (mTouchState == TOUCH_STATE_REST) {
1238                final CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage);
1239                if (currentPage != null) {
1240                    onWallpaperTap(ev);
1241                }
1242            }
1243        }
1244        return super.onInterceptTouchEvent(ev);
1245    }
1246
1247    @Override
1248    public boolean onGenericMotionEvent(MotionEvent event) {
1249        // Ignore pointer scroll events if the custom content doesn't allow scrolling.
1250        if ((getScreenIdForPageIndex(getCurrentPage()) == CUSTOM_CONTENT_SCREEN_ID)
1251                && (mCustomContentCallbacks != null)
1252                && !mCustomContentCallbacks.isScrollingAllowed()) {
1253            return false;
1254        }
1255        return super.onGenericMotionEvent(event);
1256    }
1257
1258    protected void reinflateWidgetsIfNecessary() {
1259        final int clCount = getChildCount();
1260        for (int i = 0; i < clCount; i++) {
1261            CellLayout cl = (CellLayout) getChildAt(i);
1262            ShortcutAndWidgetContainer swc = cl.getShortcutsAndWidgets();
1263            final int itemCount = swc.getChildCount();
1264            for (int j = 0; j < itemCount; j++) {
1265                View v = swc.getChildAt(j);
1266
1267                if (v instanceof LauncherAppWidgetHostView
1268                        && v.getTag() instanceof LauncherAppWidgetInfo) {
1269                    LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
1270                    LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) v;
1271                    if (lahv.isReinflateRequired()) {
1272                        // Remove and rebind the current widget (which was inflated in the wrong
1273                        // orientation), but don't delete it from the database
1274                        mLauncher.removeItem(lahv, info, false  /* deleteFromDb */);
1275                        mLauncher.bindAppWidget(info);
1276                    }
1277                }
1278            }
1279        }
1280    }
1281
1282    @Override
1283    protected void determineScrollingStart(MotionEvent ev) {
1284        if (!isFinishedSwitchingState()) return;
1285
1286        float deltaX = ev.getX() - mXDown;
1287        float absDeltaX = Math.abs(deltaX);
1288        float absDeltaY = Math.abs(ev.getY() - mYDown);
1289
1290        if (Float.compare(absDeltaX, 0f) == 0) return;
1291
1292        float slope = absDeltaY / absDeltaX;
1293        float theta = (float) Math.atan(slope);
1294
1295        if (absDeltaX > mTouchSlop || absDeltaY > mTouchSlop) {
1296            cancelCurrentPageLongPress();
1297        }
1298
1299        boolean passRightSwipesToCustomContent =
1300                (mTouchDownTime - mCustomContentShowTime) > CUSTOM_CONTENT_GESTURE_DELAY;
1301
1302        boolean swipeInIgnoreDirection = mIsRtl ? deltaX < 0 : deltaX > 0;
1303        boolean onCustomContentScreen =
1304                getScreenIdForPageIndex(getCurrentPage()) == CUSTOM_CONTENT_SCREEN_ID;
1305        if (swipeInIgnoreDirection && onCustomContentScreen && passRightSwipesToCustomContent) {
1306            // Pass swipes to the right to the custom content page.
1307            return;
1308        }
1309
1310        if (onCustomContentScreen && (mCustomContentCallbacks != null)
1311                && !mCustomContentCallbacks.isScrollingAllowed()) {
1312            // Don't allow workspace scrolling if the current custom content screen doesn't allow
1313            // scrolling.
1314            return;
1315        }
1316
1317        if (theta > MAX_SWIPE_ANGLE) {
1318            // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace
1319            return;
1320        } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) {
1321            // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to
1322            // increase the touch slop to make it harder to begin scrolling the workspace. This
1323            // results in vertically scrolling widgets to more easily. The higher the angle, the
1324            // more we increase touch slop.
1325            theta -= START_DAMPING_TOUCH_SLOP_ANGLE;
1326            float extraRatio = (float)
1327                    Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE)));
1328            super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio);
1329        } else {
1330            // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special
1331            super.determineScrollingStart(ev);
1332        }
1333    }
1334
1335    protected void onPageBeginMoving() {
1336        super.onPageBeginMoving();
1337
1338        if (isHardwareAccelerated()) {
1339            updateChildrenLayersEnabled(false);
1340        } else {
1341            if (mNextPage != INVALID_PAGE) {
1342                // we're snapping to a particular screen
1343                enableChildrenCache(mCurrentPage, mNextPage);
1344            } else {
1345                // this is when user is actively dragging a particular screen, they might
1346                // swipe it either left or right (but we won't advance by more than one screen)
1347                enableChildrenCache(mCurrentPage - 1, mCurrentPage + 1);
1348            }
1349        }
1350    }
1351
1352    protected void onPageEndMoving() {
1353        super.onPageEndMoving();
1354
1355        if (isHardwareAccelerated()) {
1356            updateChildrenLayersEnabled(false);
1357        } else {
1358            clearChildrenCache();
1359        }
1360
1361        if (mDragController.isDragging()) {
1362            if (workspaceInModalState()) {
1363                // If we are in springloaded mode, then force an event to check if the current touch
1364                // is under a new page (to scroll to)
1365                mDragController.forceTouchMove();
1366            }
1367        }
1368
1369        if (mDelayedResizeRunnable != null && !mIsSwitchingState) {
1370            mDelayedResizeRunnable.run();
1371            mDelayedResizeRunnable = null;
1372        }
1373
1374        if (mDelayedSnapToPageRunnable != null) {
1375            mDelayedSnapToPageRunnable.run();
1376            mDelayedSnapToPageRunnable = null;
1377        }
1378        if (mStripScreensOnPageStopMoving) {
1379            stripEmptyScreens();
1380            mStripScreensOnPageStopMoving = false;
1381        }
1382    }
1383
1384    protected void onScrollInteractionBegin() {
1385        super.onScrollInteractionEnd();
1386        mScrollInteractionBegan = true;
1387    }
1388
1389    protected void onScrollInteractionEnd() {
1390        super.onScrollInteractionEnd();
1391        mScrollInteractionBegan = false;
1392        if (mStartedSendingScrollEvents) {
1393            mStartedSendingScrollEvents = false;
1394            mLauncherOverlay.onScrollInteractionEnd();
1395        }
1396    }
1397
1398    public void setLauncherOverlay(LauncherOverlay overlay) {
1399        mLauncherOverlay = overlay;
1400        // A new overlay has been set. Reset event tracking
1401        mStartedSendingScrollEvents = false;
1402        onOverlayScrollChanged(0);
1403    }
1404
1405    @Override
1406    protected int getUnboundedScrollX() {
1407        if (isScrollingOverlay()) {
1408            return mUnboundedScrollX;
1409        }
1410
1411        return super.getUnboundedScrollX();
1412    }
1413
1414    private boolean isScrollingOverlay() {
1415        return mLauncherOverlay != null &&
1416                ((mIsRtl && mUnboundedScrollX > mMaxScrollX) || (!mIsRtl && mUnboundedScrollX < 0));
1417    }
1418
1419    @Override
1420    protected void snapToDestination() {
1421        // If we're overscrolling the overlay, we make sure to immediately reset the PagedView
1422        // to it's baseline position instead of letting the overscroll settle. The overlay handles
1423        // it's own settling, and every gesture to the overlay should be self-contained and start
1424        // from 0, so we zero it out here.
1425        if (isScrollingOverlay()) {
1426            int finalScroll = mIsRtl ? mMaxScrollX : 0;
1427
1428            // We reset mWasInOverscroll so that PagedView doesn't zero out the overscroll
1429            // interaction when we call scrollTo.
1430            mWasInOverscroll = false;
1431            scrollTo(finalScroll, getScrollY());
1432        } else {
1433            super.snapToDestination();
1434        }
1435    }
1436
1437    @Override
1438    public void scrollTo(int x, int y) {
1439        mUnboundedScrollX = x;
1440        super.scrollTo(x, y);
1441    }
1442
1443    private void onWorkspaceOverallScrollChanged() {
1444        if (!mIgnoreQsbScroll) {
1445            mLauncher.getQsbContainer().setTranslationX(
1446                    mOverlayTranslation + mFirstPageScrollX - getScrollX());
1447        }
1448    }
1449
1450    @Override
1451    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
1452        super.onScrollChanged(l, t, oldl, oldt);
1453        onWorkspaceOverallScrollChanged();
1454
1455        // Update the page indicator progress.
1456        boolean isTransitioning = mIsSwitchingState
1457                || (getLayoutTransition() != null && getLayoutTransition().isRunning());
1458        if (!isTransitioning) {
1459            showPageIndicatorAtCurrentScroll();
1460        }
1461    }
1462
1463    private void showPageIndicatorAtCurrentScroll() {
1464        if (mPageIndicator != null) {
1465            mPageIndicator.setScroll(getScrollX(), computeMaxScrollX());
1466        }
1467    }
1468
1469    @Override
1470    protected void overScroll(float amount) {
1471        boolean shouldOverScroll = (amount <= 0 && (!hasCustomContent() || mIsRtl)) ||
1472                (amount >= 0 && (!hasCustomContent() || !mIsRtl));
1473
1474        boolean shouldScrollOverlay = mLauncherOverlay != null &&
1475                ((amount <= 0 && !mIsRtl) || (amount >= 0 && mIsRtl));
1476
1477        boolean shouldZeroOverlay = mLauncherOverlay != null && mLastOverlaySroll != 0 &&
1478                ((amount >= 0 && !mIsRtl) || (amount <= 0 && mIsRtl));
1479
1480        if (shouldScrollOverlay) {
1481            if (!mStartedSendingScrollEvents && mScrollInteractionBegan) {
1482                mStartedSendingScrollEvents = true;
1483                mLauncherOverlay.onScrollInteractionBegin();
1484            }
1485
1486            mLastOverlaySroll = Math.abs(amount / getViewportWidth());
1487            mLauncherOverlay.onScrollChange(mLastOverlaySroll, mIsRtl);
1488        } else if (shouldOverScroll) {
1489            dampedOverScroll(amount);
1490        }
1491
1492        if (shouldZeroOverlay) {
1493            mLauncherOverlay.onScrollChange(0, mIsRtl);
1494        }
1495    }
1496
1497    private final Interpolator mAlphaInterpolator = new DecelerateInterpolator(3f);
1498
1499    /**
1500     * The overlay scroll is being controlled locally, just update our overlay effect
1501     */
1502    public void onOverlayScrollChanged(float scroll) {
1503        float offset = 0f;
1504        float slip = 0f;
1505
1506        scroll = Math.max(scroll - offset, 0);
1507        scroll = Math.min(1, scroll / (1 - offset));
1508
1509        float alpha = 1 - mAlphaInterpolator.getInterpolation(scroll);
1510        float transX = mLauncher.getDragLayer().getMeasuredWidth() * scroll;
1511        transX *= 1 - slip;
1512
1513        if (mIsRtl) {
1514            transX = -transX;
1515        }
1516        mOverlayTranslation = transX;
1517
1518        // TODO(adamcohen): figure out a final effect here. We may need to recommend
1519        // different effects based on device performance. On at least one relatively high-end
1520        // device I've tried, translating the launcher causes things to get quite laggy.
1521        setWorkspaceTranslationAndAlpha(Direction.X, transX, alpha);
1522        setHotseatTranslationAndAlpha(Direction.X, transX, alpha);
1523        onWorkspaceOverallScrollChanged();
1524
1525        mQsbAlphaController.setAlphaAtIndex(alpha, QSB_ALPHA_INDEX_OVERLAY_SCROLL);
1526    }
1527
1528    /**
1529     * Moves the workspace UI in the Y direction.
1530     * @param translation the amount of shift.
1531     * @param alpha the alpha for the workspace page
1532     */
1533    public void setWorkspaceYTranslationAndAlpha(float translation, float alpha) {
1534        setWorkspaceTranslationAndAlpha(Direction.Y, translation, alpha);
1535
1536        mLauncher.getQsbContainer().setTranslationY(translation);
1537        mQsbAlphaController.setAlphaAtIndex(alpha, QSB_ALPHA_INDEX_Y_TRANSLATION);
1538    }
1539
1540    /**
1541     * Moves the workspace UI in the provided direction.
1542     * @param direction the direction to move the workspace
1543     * @param translation the amount of shift.
1544     * @param alpha the alpha for the workspace page
1545     */
1546    private void setWorkspaceTranslationAndAlpha(Direction direction, float translation, float alpha) {
1547        Property<View, Float> property = direction.viewProperty;
1548        mPageAlpha[direction.ordinal()] = alpha;
1549        float finalAlpha = mPageAlpha[0] * mPageAlpha[1];
1550
1551        View currentChild = getChildAt(getCurrentPage());
1552        if (currentChild != null) {
1553            property.set(currentChild, translation);
1554            currentChild.setAlpha(finalAlpha);
1555        }
1556
1557        // When the animation finishes, reset all pages, just in case we missed a page.
1558        if (Float.compare(translation, 0) == 0) {
1559            for (int i = getChildCount() - 1; i >= 0; i--) {
1560                View child = getChildAt(i);
1561                property.set(child, translation);
1562                child.setAlpha(finalAlpha);
1563            }
1564        }
1565    }
1566
1567    /**
1568     * Moves the Hotseat UI in the provided direction.
1569     * @param direction the direction to move the workspace
1570     * @param translation the amount of shift.
1571     * @param alpha the alpha for the hotseat page
1572     */
1573    public void setHotseatTranslationAndAlpha(Direction direction, float translation, float alpha) {
1574        Property<View, Float> property = direction.viewProperty;
1575        // Skip the page indicator movement in the vertical bar layout
1576        if (direction != Direction.Y || !mLauncher.getDeviceProfile().isVerticalBarLayout()) {
1577            property.set(mPageIndicator, translation);
1578        }
1579        property.set(mLauncher.getHotseat(), translation);
1580        setHotseatAlphaAtIndex(alpha, direction.ordinal());
1581    }
1582
1583    private void setHotseatAlphaAtIndex(float alpha, int index) {
1584        mHotseatAlpha[index] = alpha;
1585        final float hotseatAlpha = mHotseatAlpha[0] * mHotseatAlpha[1] * mHotseatAlpha[2];
1586        final float pageIndicatorAlpha = mHotseatAlpha[0] * mHotseatAlpha[2];
1587
1588        mLauncher.getHotseat().setAlpha(hotseatAlpha);
1589        mPageIndicator.setAlpha(pageIndicatorAlpha);
1590    }
1591
1592    public ValueAnimator createHotseatAlphaAnimator(float finalValue) {
1593        if (Float.compare(finalValue, mHotseatAlpha[HOTSEAT_STATE_ALPHA_INDEX]) == 0) {
1594            // Return a dummy animator to avoid null checks.
1595            return ValueAnimator.ofFloat(0, 0);
1596        } else {
1597            ValueAnimator animator = ValueAnimator
1598                    .ofFloat(mHotseatAlpha[HOTSEAT_STATE_ALPHA_INDEX], finalValue);
1599            animator.addUpdateListener(new AnimatorUpdateListener() {
1600                @Override
1601                public void onAnimationUpdate(ValueAnimator valueAnimator) {
1602                    float value = (Float) valueAnimator.getAnimatedValue();
1603                    setHotseatAlphaAtIndex(value, HOTSEAT_STATE_ALPHA_INDEX);
1604                }
1605            });
1606
1607            AccessibilityManager am = (AccessibilityManager)
1608                    mLauncher.getSystemService(Context.ACCESSIBILITY_SERVICE);
1609            final boolean accessibilityEnabled = am.isEnabled();
1610            animator.addUpdateListener(
1611                    new AlphaUpdateListener(mLauncher.getHotseat(), accessibilityEnabled));
1612            animator.addUpdateListener(
1613                    new AlphaUpdateListener(mPageIndicator, accessibilityEnabled));
1614            return animator;
1615        }
1616    }
1617
1618    @Override
1619    protected Matrix getPageShiftMatrix() {
1620        if (Float.compare(mOverlayTranslation, 0) != 0) {
1621            // The pages are translated by mOverlayTranslation. incorporate that in the
1622            // visible page calculation by shifting everything back by that same amount.
1623            mTempMatrix.set(getMatrix());
1624            mTempMatrix.postTranslate(-mOverlayTranslation, 0);
1625            return mTempMatrix;
1626        }
1627        return super.getPageShiftMatrix();
1628    }
1629
1630    @Override
1631    protected void getEdgeVerticalPostion(int[] pos) {
1632        View child = getChildAt(getPageCount() - 1);
1633        pos[0] = child.getTop();
1634        pos[1] = child.getBottom();
1635    }
1636
1637    @Override
1638    protected void notifyPageSwitchListener() {
1639        super.notifyPageSwitchListener();
1640
1641        if (hasCustomContent() && getNextPage() == 0 && !mCustomContentShowing) {
1642            mCustomContentShowing = true;
1643            if (mCustomContentCallbacks != null) {
1644                mCustomContentCallbacks.onShow(false);
1645                mCustomContentShowTime = System.currentTimeMillis();
1646            }
1647        } else if (hasCustomContent() && getNextPage() != 0 && mCustomContentShowing) {
1648            mCustomContentShowing = false;
1649            if (mCustomContentCallbacks != null) {
1650                mCustomContentCallbacks.onHide();
1651            }
1652        }
1653    }
1654
1655    protected CustomContentCallbacks getCustomContentCallbacks() {
1656        return mCustomContentCallbacks;
1657    }
1658
1659    protected void setWallpaperDimension() {
1660        Utilities.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1661            @Override
1662            public void run() {
1663                final Point size = LauncherAppState.getInstance()
1664                        .getInvariantDeviceProfile().defaultWallpaperSize;
1665                if (size.x != mWallpaperManager.getDesiredMinimumWidth()
1666                        || size.y != mWallpaperManager.getDesiredMinimumHeight()) {
1667                    mWallpaperManager.suggestDesiredDimensions(size.x, size.y);
1668                }
1669            }
1670        });
1671    }
1672
1673    public void lockWallpaperToDefaultPage() {
1674        mWallpaperOffset.setLockToDefaultPage(true);
1675    }
1676
1677    public void unlockWallpaperFromDefaultPageOnNextLayout() {
1678        if (mWallpaperOffset.isLockedToDefaultPage()) {
1679            mUnlockWallpaperFromDefaultPageOnLayout = true;
1680            requestLayout();
1681        }
1682    }
1683
1684    protected void snapToPage(int whichPage, Runnable r) {
1685        snapToPage(whichPage, SLOW_PAGE_SNAP_ANIMATION_DURATION, r);
1686    }
1687
1688    protected void snapToPage(int whichPage, int duration, Runnable r) {
1689        if (mDelayedSnapToPageRunnable != null) {
1690            mDelayedSnapToPageRunnable.run();
1691        }
1692        mDelayedSnapToPageRunnable = r;
1693        snapToPage(whichPage, duration);
1694    }
1695
1696    public void snapToScreenId(long screenId) {
1697        snapToScreenId(screenId, null);
1698    }
1699
1700    protected void snapToScreenId(long screenId, Runnable r) {
1701        snapToPage(getPageIndexForScreenId(screenId), r);
1702    }
1703
1704    @Override
1705    public void computeScroll() {
1706        super.computeScroll();
1707        mWallpaperOffset.syncWithScroll();
1708    }
1709
1710    public void computeScrollWithoutInvalidation() {
1711        computeScrollHelper(false);
1712    }
1713
1714    @Override
1715    protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
1716        if (!isSwitchingState()) {
1717            super.determineScrollingStart(ev, touchSlopScale);
1718        }
1719    }
1720
1721    @Override
1722    public void announceForAccessibility(CharSequence text) {
1723        // Don't announce if apps is on top of us.
1724        if (!mLauncher.isAppsViewVisible()) {
1725            super.announceForAccessibility(text);
1726        }
1727    }
1728
1729    public void showOutlinesTemporarily() {
1730        if (!mIsPageMoving && !isTouchActive()) {
1731            snapToPage(mCurrentPage);
1732        }
1733    }
1734
1735    private void updatePageAlphaValues(int screenCenter) {
1736        if (mWorkspaceFadeInAdjacentScreens &&
1737                !workspaceInModalState() &&
1738                !mIsSwitchingState) {
1739            for (int i = numCustomPages(); i < getChildCount(); i++) {
1740                CellLayout child = (CellLayout) getChildAt(i);
1741                if (child != null) {
1742                    float scrollProgress = getScrollProgress(screenCenter, child, i);
1743                    float alpha = 1 - Math.abs(scrollProgress);
1744                    child.getShortcutsAndWidgets().setAlpha(alpha);
1745
1746                    if (isQsbContainerPage(i)) {
1747                        mQsbAlphaController.setAlphaAtIndex(alpha, QSB_ALPHA_INDEX_PAGE_SCROLL);
1748                    }
1749                }
1750            }
1751        }
1752    }
1753
1754    public boolean hasCustomContent() {
1755        return (mScreenOrder.size() > 0 && mScreenOrder.get(0) == CUSTOM_CONTENT_SCREEN_ID);
1756    }
1757
1758    public int numCustomPages() {
1759        return hasCustomContent() ? 1 : 0;
1760    }
1761
1762    public boolean isOnOrMovingToCustomContent() {
1763        return hasCustomContent() && getNextPage() == 0 && mRestorePage == INVALID_RESTORE_PAGE;
1764    }
1765
1766    private void updateStateForCustomContent(int screenCenter) {
1767        float translationX = 0;
1768        float progress = 0;
1769        if (hasCustomContent()) {
1770            int index = mScreenOrder.indexOf(CUSTOM_CONTENT_SCREEN_ID);
1771
1772            int scrollDelta = getScrollX() - getScrollForPage(index) -
1773                    getLayoutTransitionOffsetForPage(index);
1774            float scrollRange = getScrollForPage(index + 1) - getScrollForPage(index);
1775            translationX = scrollRange - scrollDelta;
1776            progress = (scrollRange - scrollDelta) / scrollRange;
1777
1778            if (mIsRtl) {
1779                translationX = Math.min(0, translationX);
1780            } else {
1781                translationX = Math.max(0, translationX);
1782            }
1783            progress = Math.max(0, progress);
1784        }
1785
1786        if (Float.compare(progress, mLastCustomContentScrollProgress) == 0) return;
1787
1788        CellLayout cc = mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID);
1789        if (progress > 0 && cc.getVisibility() != VISIBLE && !workspaceInModalState()) {
1790            cc.setVisibility(VISIBLE);
1791        }
1792
1793        mLastCustomContentScrollProgress = progress;
1794
1795        // We should only update the drag layer background alpha if we are not in all apps or the
1796        // widgets tray
1797        if (mState == State.NORMAL) {
1798            mLauncher.getDragLayer().setBackgroundAlpha(progress == 1 ? 0 : progress * 0.8f);
1799        }
1800
1801        if (mLauncher.getHotseat() != null) {
1802            mLauncher.getHotseat().setTranslationX(translationX);
1803        }
1804
1805        if (mPageIndicator != null) {
1806            mPageIndicator.setTranslationX(translationX);
1807        }
1808
1809        if (mCustomContentCallbacks != null) {
1810            mCustomContentCallbacks.onScrollProgressChanged(progress);
1811        }
1812    }
1813
1814    @Override
1815    protected void screenScrolled(int screenCenter) {
1816        updatePageAlphaValues(screenCenter);
1817        updateStateForCustomContent(screenCenter);
1818        enableHwLayersOnVisiblePages();
1819    }
1820
1821    protected void onAttachedToWindow() {
1822        super.onAttachedToWindow();
1823        IBinder windowToken = getWindowToken();
1824        mWallpaperOffset.setWindowToken(windowToken);
1825        computeScroll();
1826        mDragController.setWindowToken(windowToken);
1827    }
1828
1829    protected void onDetachedFromWindow() {
1830        super.onDetachedFromWindow();
1831        mWallpaperOffset.setWindowToken(null);
1832    }
1833
1834    protected void onResume() {
1835        // Update wallpaper dimensions if they were changed since last onResume
1836        // (we also always set the wallpaper dimensions in the constructor)
1837        if (LauncherAppState.getInstance().hasWallpaperChangedSinceLastCheck()) {
1838            setWallpaperDimension();
1839        }
1840        mWallpaperOffset.onResume();
1841    }
1842
1843    @Override
1844    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
1845        if (mUnlockWallpaperFromDefaultPageOnLayout) {
1846            mWallpaperOffset.setLockToDefaultPage(false);
1847            mUnlockWallpaperFromDefaultPageOnLayout = false;
1848        }
1849        if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
1850            mWallpaperOffset.syncWithScroll();
1851            mWallpaperOffset.jumpToFinal();
1852        }
1853        super.onLayout(changed, left, top, right, bottom);
1854        mFirstPageScrollX = getScrollForPage(0);
1855        onWorkspaceOverallScrollChanged();
1856
1857        final LayoutTransition transition = getLayoutTransition();
1858        // If the transition is running defer updating max scroll, as some empty pages could
1859        // still be present, and a max scroll change could cause sudden jumps in scroll.
1860        if (transition != null && transition.isRunning()) {
1861            transition.addTransitionListener(new LayoutTransition.TransitionListener() {
1862
1863                @Override
1864                public void startTransition(LayoutTransition transition, ViewGroup container,
1865                                            View view, int transitionType) {
1866                    mIgnoreQsbScroll = true;
1867                }
1868
1869                @Override
1870                public void endTransition(LayoutTransition transition, ViewGroup container,
1871                                          View view, int transitionType) {
1872                    // Wait until all transitions are complete.
1873                    if (!transition.isRunning()) {
1874                        mIgnoreQsbScroll = false;
1875                        transition.removeTransitionListener(this);
1876                        mFirstPageScrollX = getScrollForPage(0);
1877                        onWorkspaceOverallScrollChanged();
1878                    }
1879                }
1880            });
1881        }
1882
1883    }
1884
1885    @Override
1886    public int getDescendantFocusability() {
1887        if (workspaceInModalState()) {
1888            return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
1889        }
1890        return super.getDescendantFocusability();
1891    }
1892
1893    public boolean workspaceInModalState() {
1894        return mState != State.NORMAL;
1895    }
1896
1897    void enableChildrenCache(int fromPage, int toPage) {
1898        if (fromPage > toPage) {
1899            final int temp = fromPage;
1900            fromPage = toPage;
1901            toPage = temp;
1902        }
1903
1904        final int screenCount = getChildCount();
1905
1906        fromPage = Math.max(fromPage, 0);
1907        toPage = Math.min(toPage, screenCount - 1);
1908
1909        for (int i = fromPage; i <= toPage; i++) {
1910            final CellLayout layout = (CellLayout) getChildAt(i);
1911            layout.setChildrenDrawnWithCacheEnabled(true);
1912            layout.setChildrenDrawingCacheEnabled(true);
1913        }
1914    }
1915
1916    void clearChildrenCache() {
1917        final int screenCount = getChildCount();
1918        for (int i = 0; i < screenCount; i++) {
1919            final CellLayout layout = (CellLayout) getChildAt(i);
1920            layout.setChildrenDrawnWithCacheEnabled(false);
1921            // In software mode, we don't want the items to continue to be drawn into bitmaps
1922            if (!isHardwareAccelerated()) {
1923                layout.setChildrenDrawingCacheEnabled(false);
1924            }
1925        }
1926    }
1927
1928    @Thunk void updateChildrenLayersEnabled(boolean force) {
1929        boolean small = mState == State.OVERVIEW || mIsSwitchingState;
1930        boolean enableChildrenLayers = force || small || mAnimatingViewIntoPlace || isPageMoving();
1931
1932        if (enableChildrenLayers != mChildrenLayersEnabled) {
1933            mChildrenLayersEnabled = enableChildrenLayers;
1934            if (mChildrenLayersEnabled) {
1935                enableHwLayersOnVisiblePages();
1936            } else {
1937                for (int i = 0; i < getPageCount(); i++) {
1938                    final CellLayout cl = (CellLayout) getChildAt(i);
1939                    cl.enableHardwareLayer(false);
1940                }
1941            }
1942        }
1943    }
1944
1945    private void enableHwLayersOnVisiblePages() {
1946        if (mChildrenLayersEnabled) {
1947            final int screenCount = getChildCount();
1948            getVisiblePages(mTempVisiblePagesRange);
1949            int leftScreen = mTempVisiblePagesRange[0];
1950            int rightScreen = mTempVisiblePagesRange[1];
1951            if (leftScreen == rightScreen) {
1952                // make sure we're caching at least two pages always
1953                if (rightScreen < screenCount - 1) {
1954                    rightScreen++;
1955                } else if (leftScreen > 0) {
1956                    leftScreen--;
1957                }
1958            }
1959
1960            final CellLayout customScreen = mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID);
1961            for (int i = 0; i < screenCount; i++) {
1962                final CellLayout layout = (CellLayout) getPageAt(i);
1963
1964                // enable layers between left and right screen inclusive, except for the
1965                // customScreen, which may animate its content during transitions.
1966                boolean enableLayer = layout != customScreen &&
1967                        leftScreen <= i && i <= rightScreen && shouldDrawChild(layout);
1968                layout.enableHardwareLayer(enableLayer);
1969            }
1970        }
1971    }
1972
1973    public void buildPageHardwareLayers() {
1974        // force layers to be enabled just for the call to buildLayer
1975        updateChildrenLayersEnabled(true);
1976        if (getWindowToken() != null) {
1977            final int childCount = getChildCount();
1978            for (int i = 0; i < childCount; i++) {
1979                CellLayout cl = (CellLayout) getChildAt(i);
1980                cl.buildHardwareLayer();
1981            }
1982        }
1983        updateChildrenLayersEnabled(false);
1984    }
1985
1986    @Override
1987    protected void getVisiblePages(int[] range) {
1988        super.getVisiblePages(range);
1989        if (mForceDrawAdjacentPages) {
1990            // In overview mode, make sure that the two side pages are visible.
1991            range[0] = Utilities.boundToRange(getCurrentPage() - 1, numCustomPages(), range[1]);
1992            range[1] = Utilities.boundToRange(getCurrentPage() + 1, range[0], getPageCount() - 1);
1993        }
1994    }
1995
1996    protected void onWallpaperTap(MotionEvent ev) {
1997        final int[] position = mTempXY;
1998        getLocationOnScreen(position);
1999
2000        int pointerIndex = ev.getActionIndex();
2001        position[0] += (int) ev.getX(pointerIndex);
2002        position[1] += (int) ev.getY(pointerIndex);
2003
2004        mWallpaperManager.sendWallpaperCommand(getWindowToken(),
2005                ev.getAction() == MotionEvent.ACTION_UP
2006                        ? WallpaperManager.COMMAND_TAP : WallpaperManager.COMMAND_SECONDARY_TAP,
2007                position[0], position[1], 0, null);
2008    }
2009
2010    public void prepareDragWithProvider(DragPreviewProvider outlineProvider) {
2011        mOutlineProvider = outlineProvider;
2012    }
2013
2014    public void exitWidgetResizeMode() {
2015        DragLayer dragLayer = mLauncher.getDragLayer();
2016        dragLayer.clearAllResizeFrames();
2017    }
2018
2019    @Override
2020    protected void getFreeScrollPageRange(int[] range) {
2021        getOverviewModePages(range);
2022    }
2023
2024    private void getOverviewModePages(int[] range) {
2025        int start = numCustomPages();
2026        int end = getChildCount() - 1;
2027
2028        range[0] = Math.max(0, Math.min(start, getChildCount() - 1));
2029        range[1] = Math.max(0, end);
2030    }
2031
2032    public void onStartReordering() {
2033        super.onStartReordering();
2034        // Reordering handles its own animations, disable the automatic ones.
2035        disableLayoutTransitions();
2036    }
2037
2038    public void onEndReordering() {
2039        super.onEndReordering();
2040
2041        if (mLauncher.isWorkspaceLoading()) {
2042            // Invalid and dangerous operation if workspace is loading
2043            return;
2044        }
2045
2046        mScreenOrder.clear();
2047        int count = getChildCount();
2048        for (int i = 0; i < count; i++) {
2049            CellLayout cl = ((CellLayout) getChildAt(i));
2050            mScreenOrder.add(getIdForScreen(cl));
2051        }
2052
2053        mLauncher.getModel().updateWorkspaceScreenOrder(mLauncher, mScreenOrder);
2054
2055        // Re-enable auto layout transitions for page deletion.
2056        enableLayoutTransitions();
2057    }
2058
2059    public boolean isInOverviewMode() {
2060        return mState == State.OVERVIEW;
2061    }
2062
2063    public void snapToPageFromOverView(int whichPage) {
2064        mStateTransitionAnimation.snapToPageFromOverView(whichPage);
2065    }
2066
2067    int getOverviewModeTranslationY() {
2068        DeviceProfile grid = mLauncher.getDeviceProfile();
2069        int overviewButtonBarHeight = grid.getOverviewModeButtonBarHeight();
2070
2071        int scaledHeight = (int) (mOverviewModeShrinkFactor * getNormalChildHeight());
2072        Rect workspacePadding = grid.getWorkspacePadding(sTempRect);
2073        int workspaceTop = mInsets.top + workspacePadding.top;
2074        int workspaceBottom = getViewportHeight() - mInsets.bottom - workspacePadding.bottom;
2075        int overviewTop = mInsets.top;
2076        int overviewBottom = getViewportHeight() - mInsets.bottom - overviewButtonBarHeight;
2077        int workspaceOffsetTopEdge = workspaceTop + ((workspaceBottom - workspaceTop) - scaledHeight) / 2;
2078        int overviewOffsetTopEdge = overviewTop + (overviewBottom - overviewTop - scaledHeight) / 2;
2079        return -workspaceOffsetTopEdge + overviewOffsetTopEdge;
2080    }
2081
2082    float getSpringLoadedTranslationY() {
2083        DeviceProfile grid = mLauncher.getDeviceProfile();
2084        if (grid.isVerticalBarLayout() || getChildCount() == 0) {
2085            return 0;
2086        }
2087
2088        float scaledHeight = grid.workspaceSpringLoadShrinkFactor * getNormalChildHeight();
2089        float shrunkTop = mInsets.top + grid.dropTargetBarSizePx;
2090        float shrunkBottom = getViewportHeight() - mInsets.bottom
2091                - grid.getWorkspacePadding(sTempRect).bottom
2092                - grid.workspaceSpringLoadedBottomSpace;
2093        float totalShrunkSpace = shrunkBottom - shrunkTop;
2094
2095        float desiredCellTop = shrunkTop + (totalShrunkSpace - scaledHeight) / 2;
2096
2097        float halfHeight = getHeight() / 2;
2098        float myCenter = getTop() + halfHeight;
2099        float cellTopFromCenter = halfHeight - getChildAt(0).getTop();
2100        float actualCellTop = myCenter - cellTopFromCenter * grid.workspaceSpringLoadShrinkFactor;
2101        return (desiredCellTop - actualCellTop) / grid.workspaceSpringLoadShrinkFactor;
2102    }
2103
2104    float getOverviewModeShrinkFactor() {
2105        return mOverviewModeShrinkFactor;
2106    }
2107
2108    /**
2109     * Sets the current workspace {@link State}, returning an animation transitioning the workspace
2110     * to that new state.
2111     */
2112    public Animator setStateWithAnimation(State toState, boolean animated,
2113            HashMap<View, Integer> layerViews) {
2114        // Create the animation to the new state
2115        AnimatorSet workspaceAnim =  mStateTransitionAnimation.getAnimationToState(mState,
2116                toState, animated, layerViews);
2117
2118        boolean shouldNotifyWidgetChange = !mState.shouldUpdateWidget
2119                && toState.shouldUpdateWidget;
2120        // Update the current state
2121        mState = toState;
2122        updateAccessibilityFlags();
2123
2124        if (shouldNotifyWidgetChange) {
2125            mLauncher.notifyWidgetProvidersChanged();
2126        }
2127
2128        if (mOnStateChangeListener != null) {
2129            mOnStateChangeListener.prepareStateChange(toState, animated ? workspaceAnim : null);
2130        }
2131
2132        return workspaceAnim;
2133    }
2134
2135    public State getState() {
2136        return mState;
2137    }
2138
2139    public void updateAccessibilityFlags() {
2140        // TODO: Update the accessibility flags appropriately when dragging.
2141        if (!mLauncher.getAccessibilityDelegate().isInAccessibleDrag()) {
2142            if (Utilities.ATLEAST_LOLLIPOP) {
2143                int total = getPageCount();
2144                for (int i = numCustomPages(); i < total; i++) {
2145                    updateAccessibilityFlags((CellLayout) getPageAt(i), i);
2146                }
2147                setImportantForAccessibility((mState == State.NORMAL || mState == State.OVERVIEW)
2148                        ? IMPORTANT_FOR_ACCESSIBILITY_AUTO
2149                        : IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
2150            } else {
2151                int accessible = mState == State.NORMAL ?
2152                        IMPORTANT_FOR_ACCESSIBILITY_AUTO :
2153                        IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS;
2154                setImportantForAccessibility(accessible);
2155            }
2156        }
2157    }
2158
2159    private void updateAccessibilityFlags(CellLayout page, int pageNo) {
2160        if (mState == State.OVERVIEW) {
2161            page.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
2162            page.getShortcutsAndWidgets().setImportantForAccessibility(
2163                    IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
2164            page.setContentDescription(getPageDescription(pageNo));
2165
2166            // No custom action for the first page.
2167            if (!FeatureFlags.QSB_ON_FIRST_SCREEN || pageNo > 0) {
2168                if (mPagesAccessibilityDelegate == null) {
2169                    mPagesAccessibilityDelegate = new OverviewScreenAccessibilityDelegate(this);
2170                }
2171                page.setAccessibilityDelegate(mPagesAccessibilityDelegate);
2172            }
2173        } else {
2174            int accessible = mState == State.NORMAL ?
2175                    IMPORTANT_FOR_ACCESSIBILITY_AUTO :
2176                        IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS;
2177            page.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
2178            page.getShortcutsAndWidgets().setImportantForAccessibility(accessible);
2179            page.setContentDescription(null);
2180            page.setAccessibilityDelegate(null);
2181        }
2182    }
2183
2184    @Override
2185    public void onLauncherTransitionPrepare(Launcher l, boolean animated,
2186            boolean multiplePagesVisible) {
2187        mIsSwitchingState = true;
2188        mTransitionProgress = 0;
2189
2190        if (multiplePagesVisible) {
2191            mForceDrawAdjacentPages = true;
2192        }
2193        invalidate(); // This will call dispatchDraw(), which calls getVisiblePages().
2194
2195        updateChildrenLayersEnabled(false);
2196        hideCustomContentIfNecessary();
2197    }
2198
2199    @Override
2200    public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
2201        if (mPageIndicator != null) {
2202            boolean isNewStateSpringLoaded = mState == State.SPRING_LOADED;
2203            mPageIndicator.setShouldAutoHide(!isNewStateSpringLoaded);
2204            if (isNewStateSpringLoaded) {
2205                // Show the page indicator at the same time as the rest of the transition.
2206                showPageIndicatorAtCurrentScroll();
2207            }
2208        }
2209    }
2210
2211    @Override
2212    public void onLauncherTransitionStep(Launcher l, float t) {
2213        mTransitionProgress = t;
2214    }
2215
2216    @Override
2217    public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
2218        mIsSwitchingState = false;
2219        updateChildrenLayersEnabled(false);
2220        showCustomContentIfNecessary();
2221        mForceDrawAdjacentPages = false;
2222        if (mState == State.SPRING_LOADED) {
2223            showPageIndicatorAtCurrentScroll();
2224        }
2225    }
2226
2227    void updateCustomContentVisibility() {
2228        int visibility = mState == Workspace.State.NORMAL ? VISIBLE : INVISIBLE;
2229        setCustomContentVisibility(visibility);
2230    }
2231
2232    void setCustomContentVisibility(int visibility) {
2233        if (hasCustomContent()) {
2234            mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(visibility);
2235        }
2236    }
2237
2238    void showCustomContentIfNecessary() {
2239        boolean show  = mState == Workspace.State.NORMAL;
2240        if (show && hasCustomContent()) {
2241            mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(VISIBLE);
2242        }
2243    }
2244
2245    void hideCustomContentIfNecessary() {
2246        boolean hide  = mState != Workspace.State.NORMAL;
2247        if (hide && hasCustomContent()) {
2248            disableLayoutTransitions();
2249            mWorkspaceScreens.get(CUSTOM_CONTENT_SCREEN_ID).setVisibility(INVISIBLE);
2250            enableLayoutTransitions();
2251        }
2252    }
2253
2254    /**
2255     * Returns the drawable for the given text view.
2256     */
2257    public static Drawable getTextViewIcon(TextView tv) {
2258        final Drawable[] drawables = tv.getCompoundDrawables();
2259        for (int i = 0; i < drawables.length; i++) {
2260            if (drawables[i] != null) {
2261                return drawables[i];
2262            }
2263        }
2264        return null;
2265    }
2266
2267    public void startDrag(CellLayout.CellInfo cellInfo, DragOptions options) {
2268        View child = cellInfo.cell;
2269
2270        // Make sure the drag was started by a long press as opposed to a long click.
2271        if (!child.isInTouchMode()) {
2272            return;
2273        }
2274
2275        mDragInfo = cellInfo;
2276        child.setVisibility(INVISIBLE);
2277        CellLayout layout = (CellLayout) child.getParent().getParent();
2278        layout.prepareChildForDrag(child);
2279
2280        if (options.isAccessibleDrag) {
2281            mDragController.addDragListener(new AccessibileDragListenerAdapter(
2282                    this, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG) {
2283                @Override
2284                protected void enableAccessibleDrag(boolean enable) {
2285                    super.enableAccessibleDrag(enable);
2286                    setEnableForLayout(mLauncher.getHotseat().getLayout(),enable);
2287
2288                    // We need to allow our individual children to become click handlers in this
2289                    // case, so temporarily unset the click handlers.
2290                    setOnClickListener(enable ? null : mLauncher);
2291                }
2292            });
2293        }
2294
2295        beginDragShared(child, this, options);
2296    }
2297
2298    public void beginDragShared(View child, DragSource source, DragOptions options) {
2299        Object dragObject = child.getTag();
2300        if (!(dragObject instanceof ItemInfo)) {
2301            String msg = "Drag started with a view that has no tag set. This "
2302                    + "will cause a crash (issue 11627249) down the line. "
2303                    + "View: " + child + "  tag: " + child.getTag();
2304            throw new IllegalStateException(msg);
2305        }
2306        beginDragShared(child, source, (ItemInfo) dragObject,
2307                new DragPreviewProvider(child), options);
2308    }
2309
2310
2311    public DragView beginDragShared(View child, DragSource source, ItemInfo dragObject,
2312            DragPreviewProvider previewProvider, DragOptions dragOptions) {
2313        child.clearFocus();
2314        child.setPressed(false);
2315        mOutlineProvider = previewProvider;
2316
2317        // The drag bitmap follows the touch point around on the screen
2318        final Bitmap b = previewProvider.createDragBitmap(mCanvas);
2319        int halfPadding = previewProvider.previewPadding / 2;
2320
2321        float scale = previewProvider.getScaleAndPosition(b, mTempXY);
2322        int dragLayerX = mTempXY[0];
2323        int dragLayerY = mTempXY[1];
2324
2325        DeviceProfile grid = mLauncher.getDeviceProfile();
2326        Point dragVisualizeOffset = null;
2327        Rect dragRect = null;
2328        if (child instanceof BubbleTextView) {
2329            int iconSize = grid.iconSizePx;
2330            int top = child.getPaddingTop();
2331            int left = (b.getWidth() - iconSize) / 2;
2332            int right = left + iconSize;
2333            int bottom = top + iconSize;
2334            dragLayerY += top;
2335            // Note: The drag region is used to calculate drag layer offsets, but the
2336            // dragVisualizeOffset in addition to the dragRect (the size) to position the outline.
2337            dragVisualizeOffset = new Point(- halfPadding, halfPadding);
2338            dragRect = new Rect(left, top, right, bottom);
2339        } else if (child instanceof FolderIcon) {
2340            int previewSize = grid.folderIconSizePx;
2341            dragVisualizeOffset = new Point(- halfPadding, halfPadding - child.getPaddingTop());
2342            dragRect = new Rect(0, child.getPaddingTop(), child.getWidth(), previewSize);
2343        }
2344
2345        // Clear the pressed state if necessary
2346        if (child instanceof BubbleTextView) {
2347            BubbleTextView icon = (BubbleTextView) child;
2348            icon.clearPressedBackground();
2349        }
2350
2351        if (child.getParent() instanceof ShortcutAndWidgetContainer) {
2352            mDragSourceInternal = (ShortcutAndWidgetContainer) child.getParent();
2353        }
2354
2355        DragView dv = mDragController.startDrag(b, dragLayerX, dragLayerY, source,
2356                dragObject, dragVisualizeOffset, dragRect, scale, dragOptions);
2357        dv.setIntrinsicIconScaleFactor(source.getIntrinsicIconScaleFactor());
2358        b.recycle();
2359        return dv;
2360    }
2361
2362    public boolean transitionStateShouldAllowDrop() {
2363        return ((!isSwitchingState() || mTransitionProgress > 0.5f) &&
2364                (mState == State.NORMAL || mState == State.SPRING_LOADED));
2365    }
2366
2367    /**
2368     * {@inheritDoc}
2369     */
2370    public boolean acceptDrop(DragObject d) {
2371        // If it's an external drop (e.g. from All Apps), check if it should be accepted
2372        CellLayout dropTargetLayout = mDropToLayout;
2373        if (d.dragSource != this) {
2374            // Don't accept the drop if we're not over a screen at time of drop
2375            if (dropTargetLayout == null) {
2376                return false;
2377            }
2378            if (!transitionStateShouldAllowDrop()) return false;
2379
2380            mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter);
2381
2382            // We want the point to be mapped to the dragTarget.
2383            if (mLauncher.isHotseatLayout(dropTargetLayout)) {
2384                mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter);
2385            } else {
2386                mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter);
2387            }
2388
2389            int spanX = 1;
2390            int spanY = 1;
2391            if (mDragInfo != null) {
2392                final CellLayout.CellInfo dragCellInfo = mDragInfo;
2393                spanX = dragCellInfo.spanX;
2394                spanY = dragCellInfo.spanY;
2395            } else {
2396                spanX = d.dragInfo.spanX;
2397                spanY = d.dragInfo.spanY;
2398            }
2399
2400            int minSpanX = spanX;
2401            int minSpanY = spanY;
2402            if (d.dragInfo instanceof PendingAddWidgetInfo) {
2403                minSpanX = ((PendingAddWidgetInfo) d.dragInfo).minSpanX;
2404                minSpanY = ((PendingAddWidgetInfo) d.dragInfo).minSpanY;
2405            }
2406
2407            mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
2408                    (int) mDragViewVisualCenter[1], minSpanX, minSpanY, dropTargetLayout,
2409                    mTargetCell);
2410            float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0],
2411                    mDragViewVisualCenter[1], mTargetCell);
2412            if (mCreateUserFolderOnDrop && willCreateUserFolder(d.dragInfo,
2413                    dropTargetLayout, mTargetCell, distance, true)) {
2414                return true;
2415            }
2416
2417            if (mAddToExistingFolderOnDrop && willAddToExistingUserFolder(d.dragInfo,
2418                    dropTargetLayout, mTargetCell, distance)) {
2419                return true;
2420            }
2421
2422            int[] resultSpan = new int[2];
2423            mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0],
2424                    (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY,
2425                    null, mTargetCell, resultSpan, CellLayout.MODE_ACCEPT_DROP);
2426            boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0;
2427
2428            // Don't accept the drop if there's no room for the item
2429            if (!foundCell) {
2430                // Don't show the message if we are dropping on the AllApps button and the hotseat
2431                // is full
2432                boolean isHotseat = mLauncher.isHotseatLayout(dropTargetLayout);
2433                if (mTargetCell != null && isHotseat && !FeatureFlags.NO_ALL_APPS_ICON) {
2434                    Hotseat hotseat = mLauncher.getHotseat();
2435                    if (mLauncher.getDeviceProfile().inv.isAllAppsButtonRank(
2436                            hotseat.getOrderInHotseat(mTargetCell[0], mTargetCell[1]))) {
2437                        return false;
2438                    }
2439                }
2440
2441                mLauncher.showOutOfSpaceMessage(isHotseat);
2442                return false;
2443            }
2444        }
2445
2446        long screenId = getIdForScreen(dropTargetLayout);
2447        if (screenId == EXTRA_EMPTY_SCREEN_ID) {
2448            commitExtraEmptyScreen();
2449        }
2450
2451        return true;
2452    }
2453
2454    boolean willCreateUserFolder(ItemInfo info, CellLayout target, int[] targetCell,
2455            float distance, boolean considerTimeout) {
2456        if (distance > mMaxDistanceForFolderCreation) return false;
2457        View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
2458        return willCreateUserFolder(info, dropOverView, considerTimeout);
2459    }
2460
2461    boolean willCreateUserFolder(ItemInfo info, View dropOverView, boolean considerTimeout) {
2462        if (dropOverView != null) {
2463            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams();
2464            if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY)) {
2465                return false;
2466            }
2467        }
2468
2469        boolean hasntMoved = false;
2470        if (mDragInfo != null) {
2471            hasntMoved = dropOverView == mDragInfo.cell;
2472        }
2473
2474        if (dropOverView == null || hasntMoved || (considerTimeout && !mCreateUserFolderOnDrop)) {
2475            return false;
2476        }
2477
2478        boolean aboveShortcut = (dropOverView.getTag() instanceof ShortcutInfo);
2479        boolean willBecomeShortcut =
2480                (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
2481                        info.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT ||
2482                        info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT);
2483
2484        return (aboveShortcut && willBecomeShortcut);
2485    }
2486
2487    boolean willAddToExistingUserFolder(ItemInfo dragInfo, CellLayout target, int[] targetCell,
2488            float distance) {
2489        if (distance > mMaxDistanceForFolderCreation) return false;
2490        View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
2491        return willAddToExistingUserFolder(dragInfo, dropOverView);
2492
2493    }
2494    boolean willAddToExistingUserFolder(ItemInfo dragInfo, View dropOverView) {
2495        if (dropOverView != null) {
2496            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) dropOverView.getLayoutParams();
2497            if (lp.useTmpCoords && (lp.tmpCellX != lp.cellX || lp.tmpCellY != lp.cellY)) {
2498                return false;
2499            }
2500        }
2501
2502        if (dropOverView instanceof FolderIcon) {
2503            FolderIcon fi = (FolderIcon) dropOverView;
2504            if (fi.acceptDrop(dragInfo)) {
2505                return true;
2506            }
2507        }
2508        return false;
2509    }
2510
2511    boolean createUserFolderIfNecessary(View newView, long container, CellLayout target,
2512            int[] targetCell, float distance, boolean external, DragView dragView,
2513            Runnable postAnimationRunnable) {
2514        if (distance > mMaxDistanceForFolderCreation) return false;
2515        View v = target.getChildAt(targetCell[0], targetCell[1]);
2516
2517        boolean hasntMoved = false;
2518        if (mDragInfo != null) {
2519            CellLayout cellParent = getParentCellLayoutForView(mDragInfo.cell);
2520            hasntMoved = (mDragInfo.cellX == targetCell[0] &&
2521                    mDragInfo.cellY == targetCell[1]) && (cellParent == target);
2522        }
2523
2524        if (v == null || hasntMoved || !mCreateUserFolderOnDrop) return false;
2525        mCreateUserFolderOnDrop = false;
2526        final long screenId = getIdForScreen(target);
2527
2528        boolean aboveShortcut = (v.getTag() instanceof ShortcutInfo);
2529        boolean willBecomeShortcut = (newView.getTag() instanceof ShortcutInfo);
2530
2531        if (aboveShortcut && willBecomeShortcut) {
2532            ShortcutInfo sourceInfo = (ShortcutInfo) newView.getTag();
2533            ShortcutInfo destInfo = (ShortcutInfo) v.getTag();
2534            // if the drag started here, we need to remove it from the workspace
2535            if (!external) {
2536                getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell);
2537            }
2538
2539            Rect folderLocation = new Rect();
2540            float scale = mLauncher.getDragLayer().getDescendantRectRelativeToSelf(v, folderLocation);
2541            target.removeView(v);
2542
2543            FolderIcon fi =
2544                mLauncher.addFolder(target, container, screenId, targetCell[0], targetCell[1]);
2545            destInfo.cellX = -1;
2546            destInfo.cellY = -1;
2547            sourceInfo.cellX = -1;
2548            sourceInfo.cellY = -1;
2549
2550            // If the dragView is null, we can't animate
2551            boolean animate = dragView != null;
2552            if (animate) {
2553                // In order to keep everything continuous, we hand off the currently rendered
2554                // folder background to the newly created icon. This preserves animation state.
2555                fi.setFolderBackground(mFolderCreateBg);
2556                mFolderCreateBg = new FolderIcon.PreviewBackground();
2557                fi.performCreateAnimation(destInfo, v, sourceInfo, dragView, folderLocation, scale,
2558                        postAnimationRunnable);
2559            } else {
2560                fi.prepareCreate(v);
2561                fi.addItem(destInfo);
2562                fi.addItem(sourceInfo);
2563            }
2564            return true;
2565        }
2566        return false;
2567    }
2568
2569    boolean addToExistingFolderIfNecessary(View newView, CellLayout target, int[] targetCell,
2570            float distance, DragObject d, boolean external) {
2571        if (distance > mMaxDistanceForFolderCreation) return false;
2572
2573        View dropOverView = target.getChildAt(targetCell[0], targetCell[1]);
2574        if (!mAddToExistingFolderOnDrop) return false;
2575        mAddToExistingFolderOnDrop = false;
2576
2577        if (dropOverView instanceof FolderIcon) {
2578            FolderIcon fi = (FolderIcon) dropOverView;
2579            if (fi.acceptDrop(d.dragInfo)) {
2580                fi.onDrop(d);
2581
2582                // if the drag started here, we need to remove it from the workspace
2583                if (!external) {
2584                    getParentCellLayoutForView(mDragInfo.cell).removeView(mDragInfo.cell);
2585                }
2586                return true;
2587            }
2588        }
2589        return false;
2590    }
2591
2592    @Override
2593    public void prepareAccessibilityDrop() { }
2594
2595    public void onDrop(final DragObject d) {
2596        mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter);
2597        CellLayout dropTargetLayout = mDropToLayout;
2598
2599        // We want the point to be mapped to the dragTarget.
2600        if (dropTargetLayout != null) {
2601            if (mLauncher.isHotseatLayout(dropTargetLayout)) {
2602                mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter);
2603            } else {
2604                mapPointFromSelfToChild(dropTargetLayout, mDragViewVisualCenter);
2605            }
2606        }
2607
2608        int snapScreen = -1;
2609        boolean resizeOnDrop = false;
2610        if (d.dragSource != this) {
2611            final int[] touchXY = new int[] { (int) mDragViewVisualCenter[0],
2612                    (int) mDragViewVisualCenter[1] };
2613            onDropExternal(touchXY, d.dragInfo, dropTargetLayout, false, d);
2614        } else if (mDragInfo != null) {
2615            final View cell = mDragInfo.cell;
2616
2617            if (dropTargetLayout != null && !d.cancelled) {
2618                // Move internally
2619                boolean hasMovedLayouts = (getParentCellLayoutForView(cell) != dropTargetLayout);
2620                boolean hasMovedIntoHotseat = mLauncher.isHotseatLayout(dropTargetLayout);
2621                long container = hasMovedIntoHotseat ?
2622                        LauncherSettings.Favorites.CONTAINER_HOTSEAT :
2623                        LauncherSettings.Favorites.CONTAINER_DESKTOP;
2624                long screenId = (mTargetCell[0] < 0) ?
2625                        mDragInfo.screenId : getIdForScreen(dropTargetLayout);
2626                int spanX = mDragInfo != null ? mDragInfo.spanX : 1;
2627                int spanY = mDragInfo != null ? mDragInfo.spanY : 1;
2628                // First we find the cell nearest to point at which the item is
2629                // dropped, without any consideration to whether there is an item there.
2630
2631                mTargetCell = findNearestArea((int) mDragViewVisualCenter[0], (int)
2632                        mDragViewVisualCenter[1], spanX, spanY, dropTargetLayout, mTargetCell);
2633                float distance = dropTargetLayout.getDistanceFromCell(mDragViewVisualCenter[0],
2634                        mDragViewVisualCenter[1], mTargetCell);
2635
2636                // If the item being dropped is a shortcut and the nearest drop
2637                // cell also contains a shortcut, then create a folder with the two shortcuts.
2638                if (!mInScrollArea && createUserFolderIfNecessary(cell, container,
2639                        dropTargetLayout, mTargetCell, distance, false, d.dragView, null)) {
2640                    return;
2641                }
2642
2643                if (addToExistingFolderIfNecessary(cell, dropTargetLayout, mTargetCell,
2644                        distance, d, false)) {
2645                    return;
2646                }
2647
2648                // Aside from the special case where we're dropping a shortcut onto a shortcut,
2649                // we need to find the nearest cell location that is vacant
2650                ItemInfo item = d.dragInfo;
2651                int minSpanX = item.spanX;
2652                int minSpanY = item.spanY;
2653                if (item.minSpanX > 0 && item.minSpanY > 0) {
2654                    minSpanX = item.minSpanX;
2655                    minSpanY = item.minSpanY;
2656                }
2657
2658                int[] resultSpan = new int[2];
2659                mTargetCell = dropTargetLayout.performReorder((int) mDragViewVisualCenter[0],
2660                        (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY, cell,
2661                        mTargetCell, resultSpan, CellLayout.MODE_ON_DROP);
2662
2663                boolean foundCell = mTargetCell[0] >= 0 && mTargetCell[1] >= 0;
2664
2665                // if the widget resizes on drop
2666                if (foundCell && (cell instanceof AppWidgetHostView) &&
2667                        (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY)) {
2668                    resizeOnDrop = true;
2669                    item.spanX = resultSpan[0];
2670                    item.spanY = resultSpan[1];
2671                    AppWidgetHostView awhv = (AppWidgetHostView) cell;
2672                    AppWidgetResizeFrame.updateWidgetSizeRanges(awhv, mLauncher, resultSpan[0],
2673                            resultSpan[1]);
2674                }
2675
2676                if (getScreenIdForPageIndex(mCurrentPage) != screenId && !hasMovedIntoHotseat) {
2677                    snapScreen = getPageIndexForScreenId(screenId);
2678                    snapToPage(snapScreen);
2679                }
2680
2681                if (foundCell) {
2682                    final ItemInfo info = (ItemInfo) cell.getTag();
2683                    if (hasMovedLayouts) {
2684                        // Reparent the view
2685                        CellLayout parentCell = getParentCellLayoutForView(cell);
2686                        if (parentCell != null) {
2687                            parentCell.removeView(cell);
2688                        } else if (ProviderConfig.IS_DOGFOOD_BUILD) {
2689                            throw new NullPointerException("mDragInfo.cell has null parent");
2690                        }
2691                        addInScreen(cell, container, screenId, mTargetCell[0], mTargetCell[1],
2692                                info.spanX, info.spanY);
2693                    }
2694
2695                    // update the item's position after drop
2696                    CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
2697                    lp.cellX = lp.tmpCellX = mTargetCell[0];
2698                    lp.cellY = lp.tmpCellY = mTargetCell[1];
2699                    lp.cellHSpan = item.spanX;
2700                    lp.cellVSpan = item.spanY;
2701                    lp.isLockedToGrid = true;
2702
2703                    if (container != LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2704                            cell instanceof LauncherAppWidgetHostView) {
2705                        final CellLayout cellLayout = dropTargetLayout;
2706                        // We post this call so that the widget has a chance to be placed
2707                        // in its final location
2708
2709                        final LauncherAppWidgetHostView hostView = (LauncherAppWidgetHostView) cell;
2710                        AppWidgetProviderInfo pInfo = hostView.getAppWidgetInfo();
2711                        if (pInfo != null && pInfo.resizeMode != AppWidgetProviderInfo.RESIZE_NONE
2712                                && !d.accessibleDrag) {
2713                            mDelayedResizeRunnable = new Runnable() {
2714                                public void run() {
2715                                    if (!isPageMoving() && !mIsSwitchingState) {
2716                                        DragLayer dragLayer = mLauncher.getDragLayer();
2717                                        dragLayer.addResizeFrame(info, hostView, cellLayout);
2718                                    }
2719                                }
2720                            };
2721                        }
2722                    }
2723
2724                    LauncherModel.modifyItemInDatabase(mLauncher, info, container, screenId, lp.cellX,
2725                            lp.cellY, item.spanX, item.spanY);
2726                } else {
2727                    // If we can't find a drop location, we return the item to its original position
2728                    CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
2729                    mTargetCell[0] = lp.cellX;
2730                    mTargetCell[1] = lp.cellY;
2731                    CellLayout layout = (CellLayout) cell.getParent().getParent();
2732                    layout.markCellsAsOccupiedForView(cell);
2733                }
2734            }
2735
2736            final CellLayout parent = (CellLayout) cell.getParent().getParent();
2737            // Prepare it to be animated into its new position
2738            // This must be called after the view has been re-parented
2739            final Runnable onCompleteRunnable = new Runnable() {
2740                @Override
2741                public void run() {
2742                    mAnimatingViewIntoPlace = false;
2743                    updateChildrenLayersEnabled(false);
2744                }
2745            };
2746            mAnimatingViewIntoPlace = true;
2747            if (d.dragView.hasDrawn()) {
2748                final ItemInfo info = (ItemInfo) cell.getTag();
2749                boolean isWidget = info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET
2750                        || info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
2751                if (isWidget) {
2752                    int animationType = resizeOnDrop ? ANIMATE_INTO_POSITION_AND_RESIZE :
2753                            ANIMATE_INTO_POSITION_AND_DISAPPEAR;
2754                    animateWidgetDrop(info, parent, d.dragView,
2755                            onCompleteRunnable, animationType, cell, false);
2756                } else {
2757                    int duration = snapScreen < 0 ? -1 : ADJACENT_SCREEN_DROP_DURATION;
2758                    mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, cell, duration,
2759                            onCompleteRunnable, this);
2760                }
2761            } else {
2762                d.deferDragViewCleanupPostAnimation = false;
2763                cell.setVisibility(VISIBLE);
2764            }
2765            parent.onDropChild(cell);
2766        }
2767    }
2768
2769    /**
2770     * Computes the area relative to dragLayer which is used to display a page.
2771     */
2772    public void getPageAreaRelativeToDragLayer(Rect outArea) {
2773        CellLayout child = (CellLayout) getChildAt(getNextPage());
2774        if (child == null) {
2775            return;
2776        }
2777        ShortcutAndWidgetContainer boundingLayout = child.getShortcutsAndWidgets();
2778
2779        // Use the absolute left instead of the child left, as we want the visible area
2780        // irrespective of the visible child. Since the view can only scroll horizontally, the
2781        // top position is not affected.
2782        mTempXY[0] = getViewportOffsetX() + getPaddingLeft() + boundingLayout.getLeft();
2783        mTempXY[1] = child.getTop() + boundingLayout.getTop();
2784
2785        float scale = mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY);
2786        outArea.set(mTempXY[0], mTempXY[1],
2787                (int) (mTempXY[0] + scale * boundingLayout.getMeasuredWidth()),
2788                (int) (mTempXY[1] + scale * boundingLayout.getMeasuredHeight()));
2789    }
2790
2791    @Override
2792    public void onDragEnter(DragObject d) {
2793        if (ENFORCE_DRAG_EVENT_ORDER) {
2794            enfoceDragParity("onDragEnter", 1, 1);
2795        }
2796
2797        mCreateUserFolderOnDrop = false;
2798        mAddToExistingFolderOnDrop = false;
2799
2800        mDropToLayout = null;
2801        setDropLayoutForDragObject(d);
2802
2803        if (!workspaceInModalState() && FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND) {
2804            mLauncher.getDragLayer().showPageHints();
2805        }
2806    }
2807
2808    @Override
2809    public void onDragExit(DragObject d) {
2810        if (ENFORCE_DRAG_EVENT_ORDER) {
2811            enfoceDragParity("onDragExit", -1, 0);
2812        }
2813
2814        // Here we store the final page that will be dropped to, if the workspace in fact
2815        // receives the drop
2816        if (mInScrollArea) {
2817            if (isPageMoving()) {
2818                // If the user drops while the page is scrolling, we should use that page as the
2819                // destination instead of the page that is being hovered over.
2820                mDropToLayout = (CellLayout) getPageAt(getNextPage());
2821            } else {
2822                mDropToLayout = mDragOverlappingLayout;
2823            }
2824        } else {
2825            mDropToLayout = mDragTargetLayout;
2826        }
2827
2828        if (mDragMode == DRAG_MODE_CREATE_FOLDER) {
2829            mCreateUserFolderOnDrop = true;
2830        } else if (mDragMode == DRAG_MODE_ADD_TO_FOLDER) {
2831            mAddToExistingFolderOnDrop = true;
2832        }
2833
2834        // Reset the scroll area and previous drag target
2835        onResetScrollArea();
2836        setCurrentDropLayout(null);
2837        setCurrentDragOverlappingLayout(null);
2838
2839        mSpringLoadedDragController.cancel();
2840
2841        mLauncher.getDragLayer().hidePageHints();
2842    }
2843
2844    private void enfoceDragParity(String event, int update, int expectedValue) {
2845        enfoceDragParity(this, event, update, expectedValue);
2846        for (int i = 0; i < getChildCount(); i++) {
2847            enfoceDragParity(getChildAt(i), event, update, expectedValue);
2848        }
2849    }
2850
2851    private void enfoceDragParity(View v, String event, int update, int expectedValue) {
2852        Object tag = v.getTag(R.id.drag_event_parity);
2853        int value = tag == null ? 0 : (Integer) tag;
2854        value += update;
2855        v.setTag(R.id.drag_event_parity, value);
2856
2857        if (value != expectedValue) {
2858            Log.e(TAG, event + ": Drag contract violated: " + value);
2859        }
2860    }
2861
2862    void setCurrentDropLayout(CellLayout layout) {
2863        if (mDragTargetLayout != null) {
2864            mDragTargetLayout.revertTempState();
2865            mDragTargetLayout.onDragExit();
2866        }
2867        mDragTargetLayout = layout;
2868        if (mDragTargetLayout != null) {
2869            mDragTargetLayout.onDragEnter();
2870        }
2871        cleanupReorder(true);
2872        cleanupFolderCreation();
2873        setCurrentDropOverCell(-1, -1);
2874    }
2875
2876    void setCurrentDragOverlappingLayout(CellLayout layout) {
2877        if (mDragOverlappingLayout != null) {
2878            mDragOverlappingLayout.setIsDragOverlapping(false);
2879        }
2880        mDragOverlappingLayout = layout;
2881        if (mDragOverlappingLayout != null) {
2882            mDragOverlappingLayout.setIsDragOverlapping(true);
2883        }
2884        // Invalidating the scrim will also force this CellLayout
2885        // to be invalidated so that it is highlighted if necessary.
2886        mLauncher.getDragLayer().invalidateScrim();
2887    }
2888
2889    public CellLayout getCurrentDragOverlappingLayout() {
2890        return mDragOverlappingLayout;
2891    }
2892
2893    void setCurrentDropOverCell(int x, int y) {
2894        if (x != mDragOverX || y != mDragOverY) {
2895            mDragOverX = x;
2896            mDragOverY = y;
2897            setDragMode(DRAG_MODE_NONE);
2898        }
2899    }
2900
2901    void setDragMode(int dragMode) {
2902        if (dragMode != mDragMode) {
2903            if (dragMode == DRAG_MODE_NONE) {
2904                cleanupAddToFolder();
2905                // We don't want to cancel the re-order alarm every time the target cell changes
2906                // as this feels to slow / unresponsive.
2907                cleanupReorder(false);
2908                cleanupFolderCreation();
2909            } else if (dragMode == DRAG_MODE_ADD_TO_FOLDER) {
2910                cleanupReorder(true);
2911                cleanupFolderCreation();
2912            } else if (dragMode == DRAG_MODE_CREATE_FOLDER) {
2913                cleanupAddToFolder();
2914                cleanupReorder(true);
2915            } else if (dragMode == DRAG_MODE_REORDER) {
2916                cleanupAddToFolder();
2917                cleanupFolderCreation();
2918            }
2919            mDragMode = dragMode;
2920        }
2921    }
2922
2923    private void cleanupFolderCreation() {
2924        if (mFolderCreateBg != null) {
2925            mFolderCreateBg.animateToRest();
2926        }
2927        mFolderCreationAlarm.setOnAlarmListener(null);
2928        mFolderCreationAlarm.cancelAlarm();
2929    }
2930
2931    private void cleanupAddToFolder() {
2932        if (mDragOverFolderIcon != null) {
2933            mDragOverFolderIcon.onDragExit();
2934            mDragOverFolderIcon = null;
2935        }
2936    }
2937
2938    private void cleanupReorder(boolean cancelAlarm) {
2939        // Any pending reorders are canceled
2940        if (cancelAlarm) {
2941            mReorderAlarm.cancelAlarm();
2942        }
2943        mLastReorderX = -1;
2944        mLastReorderY = -1;
2945    }
2946
2947   /*
2948    *
2949    * Convert the 2D coordinate xy from the parent View's coordinate space to this CellLayout's
2950    * coordinate space. The argument xy is modified with the return result.
2951    */
2952   void mapPointFromSelfToChild(View v, float[] xy) {
2953       xy[0] = xy[0] - v.getLeft();
2954       xy[1] = xy[1] - v.getTop();
2955   }
2956
2957   boolean isPointInSelfOverHotseat(int x, int y) {
2958       mTempXY[0] = x;
2959       mTempXY[1] = y;
2960       mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY, true);
2961       View hotseat = mLauncher.getHotseat();
2962       return mTempXY[0] >= hotseat.getLeft() &&
2963               mTempXY[0] <= hotseat.getRight() &&
2964               mTempXY[1] >= hotseat.getTop() &&
2965               mTempXY[1] <= hotseat.getBottom();
2966   }
2967
2968   void mapPointFromSelfToHotseatLayout(Hotseat hotseat, float[] xy) {
2969       mTempXY[0] = (int) xy[0];
2970       mTempXY[1] = (int) xy[1];
2971       mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY, true);
2972       mLauncher.getDragLayer().mapCoordInSelfToDescendent(hotseat.getLayout(), mTempXY);
2973
2974       xy[0] = mTempXY[0];
2975       xy[1] = mTempXY[1];
2976   }
2977
2978   /*
2979    *
2980    * Convert the 2D coordinate xy from this CellLayout's coordinate space to
2981    * the parent View's coordinate space. The argument xy is modified with the return result.
2982    *
2983    */
2984   void mapPointFromChildToSelf(View v, float[] xy) {
2985       xy[0] += v.getLeft();
2986       xy[1] += v.getTop();
2987   }
2988
2989   static private float squaredDistance(float[] point1, float[] point2) {
2990        float distanceX = point1[0] - point2[0];
2991        float distanceY = point2[1] - point2[1];
2992        return distanceX * distanceX + distanceY * distanceY;
2993   }
2994
2995    /*
2996     *
2997     * This method returns the CellLayout that is currently being dragged to. In order to drag
2998     * to a CellLayout, either the touch point must be directly over the CellLayout, or as a second
2999     * strategy, we see if the dragView is overlapping any CellLayout and choose the closest one
3000     *
3001     * Return null if no CellLayout is currently being dragged over
3002     *
3003     */
3004    private CellLayout findMatchingPageForDragOver(
3005            DragView dragView, float originX, float originY, boolean exact) {
3006        // We loop through all the screens (ie CellLayouts) and see which ones overlap
3007        // with the item being dragged and then choose the one that's closest to the touch point
3008        final int screenCount = getChildCount();
3009        CellLayout bestMatchingScreen = null;
3010        float smallestDistSoFar = Float.MAX_VALUE;
3011
3012        for (int i = 0; i < screenCount; i++) {
3013            // The custom content screen is not a valid drag over option
3014            if (mScreenOrder.get(i) == CUSTOM_CONTENT_SCREEN_ID) {
3015                continue;
3016            }
3017
3018            CellLayout cl = (CellLayout) getChildAt(i);
3019
3020            final float[] touchXy = {originX, originY};
3021            mapPointFromSelfToChild(cl, touchXy);
3022
3023            if (touchXy[0] >= 0 && touchXy[0] <= cl.getWidth() &&
3024                    touchXy[1] >= 0 && touchXy[1] <= cl.getHeight()) {
3025                return cl;
3026            }
3027
3028            if (!exact) {
3029                // Get the center of the cell layout in screen coordinates
3030                final float[] cellLayoutCenter = mTempCellLayoutCenterCoordinates;
3031                cellLayoutCenter[0] = cl.getWidth()/2;
3032                cellLayoutCenter[1] = cl.getHeight()/2;
3033                mapPointFromChildToSelf(cl, cellLayoutCenter);
3034
3035                touchXy[0] = originX;
3036                touchXy[1] = originY;
3037
3038                // Calculate the distance between the center of the CellLayout
3039                // and the touch point
3040                float dist = squaredDistance(touchXy, cellLayoutCenter);
3041
3042                if (dist < smallestDistSoFar) {
3043                    smallestDistSoFar = dist;
3044                    bestMatchingScreen = cl;
3045                }
3046            }
3047        }
3048        return bestMatchingScreen;
3049    }
3050
3051    private boolean isDragWidget(DragObject d) {
3052        return (d.dragInfo instanceof LauncherAppWidgetInfo ||
3053                d.dragInfo instanceof PendingAddWidgetInfo);
3054    }
3055
3056    public void onDragOver(DragObject d) {
3057        // Skip drag over events while we are dragging over side pages
3058        if (mInScrollArea || !transitionStateShouldAllowDrop()) return;
3059
3060        ItemInfo item = d.dragInfo;
3061        if (item == null) {
3062            if (ProviderConfig.IS_DOGFOOD_BUILD) {
3063                throw new NullPointerException("DragObject has null info");
3064            }
3065            return;
3066        }
3067
3068        // Ensure that we have proper spans for the item that we are dropping
3069        if (item.spanX < 0 || item.spanY < 0) throw new RuntimeException("Improper spans found");
3070        mDragViewVisualCenter = d.getVisualCenter(mDragViewVisualCenter);
3071
3072        final View child = (mDragInfo == null) ? null : mDragInfo.cell;
3073        if (setDropLayoutForDragObject(d)) {
3074            boolean isInSpringLoadedMode = (mState == State.SPRING_LOADED);
3075            if (isInSpringLoadedMode) {
3076                if (mLauncher.isHotseatLayout(mDragTargetLayout)) {
3077                    mSpringLoadedDragController.cancel();
3078                } else {
3079                    mSpringLoadedDragController.setAlarm(mDragTargetLayout);
3080                }
3081            }
3082        }
3083
3084        // Handle the drag over
3085        if (mDragTargetLayout != null) {
3086            // We want the point to be mapped to the dragTarget.
3087            if (mLauncher.isHotseatLayout(mDragTargetLayout)) {
3088                mapPointFromSelfToHotseatLayout(mLauncher.getHotseat(), mDragViewVisualCenter);
3089            } else {
3090                mapPointFromSelfToChild(mDragTargetLayout, mDragViewVisualCenter);
3091            }
3092
3093            int minSpanX = item.spanX;
3094            int minSpanY = item.spanY;
3095            if (item.minSpanX > 0 && item.minSpanY > 0) {
3096                minSpanX = item.minSpanX;
3097                minSpanY = item.minSpanY;
3098            }
3099
3100            mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
3101                    (int) mDragViewVisualCenter[1], minSpanX, minSpanY,
3102                    mDragTargetLayout, mTargetCell);
3103            int reorderX = mTargetCell[0];
3104            int reorderY = mTargetCell[1];
3105
3106            setCurrentDropOverCell(mTargetCell[0], mTargetCell[1]);
3107
3108            float targetCellDistance = mDragTargetLayout.getDistanceFromCell(
3109                    mDragViewVisualCenter[0], mDragViewVisualCenter[1], mTargetCell);
3110
3111            manageFolderFeedback(mDragTargetLayout, mTargetCell, targetCellDistance, d);
3112
3113            boolean nearestDropOccupied = mDragTargetLayout.isNearestDropLocationOccupied((int)
3114                    mDragViewVisualCenter[0], (int) mDragViewVisualCenter[1], item.spanX,
3115                    item.spanY, child, mTargetCell);
3116
3117            if (!nearestDropOccupied) {
3118                mDragTargetLayout.visualizeDropLocation(child, mOutlineProvider,
3119                        mTargetCell[0], mTargetCell[1], item.spanX, item.spanY, false, d);
3120            } else if ((mDragMode == DRAG_MODE_NONE || mDragMode == DRAG_MODE_REORDER)
3121                    && !mReorderAlarm.alarmPending() && (mLastReorderX != reorderX ||
3122                    mLastReorderY != reorderY)) {
3123
3124                int[] resultSpan = new int[2];
3125                mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0],
3126                        (int) mDragViewVisualCenter[1], minSpanX, minSpanY, item.spanX, item.spanY,
3127                        child, mTargetCell, resultSpan, CellLayout.MODE_SHOW_REORDER_HINT);
3128
3129                // Otherwise, if we aren't adding to or creating a folder and there's no pending
3130                // reorder, then we schedule a reorder
3131                ReorderAlarmListener listener = new ReorderAlarmListener(mDragViewVisualCenter,
3132                        minSpanX, minSpanY, item.spanX, item.spanY, d, child);
3133                mReorderAlarm.setOnAlarmListener(listener);
3134                mReorderAlarm.setAlarm(REORDER_TIMEOUT);
3135            }
3136
3137            if (mDragMode == DRAG_MODE_CREATE_FOLDER || mDragMode == DRAG_MODE_ADD_TO_FOLDER ||
3138                    !nearestDropOccupied) {
3139                if (mDragTargetLayout != null) {
3140                    mDragTargetLayout.revertTempState();
3141                }
3142            }
3143        }
3144    }
3145
3146    /**
3147     * Updates {@link #mDragTargetLayout} and {@link #mDragOverlappingLayout}
3148     * based on the DragObject's position.
3149     *
3150     * The layout will be:
3151     * - The Hotseat if the drag object is over it
3152     * - A side page if we are in spring-loaded mode and the drag object is over it
3153     * - The current page otherwise
3154     *
3155     * @return whether the layout is different from the current {@link #mDragTargetLayout}.
3156     */
3157    private boolean setDropLayoutForDragObject(DragObject d) {
3158        CellLayout layout = null;
3159        // Test to see if we are over the hotseat first
3160        if (mLauncher.getHotseat() != null && !isDragWidget(d)) {
3161            if (isPointInSelfOverHotseat(d.x, d.y)) {
3162                layout = mLauncher.getHotseat().getLayout();
3163            }
3164        }
3165        if (layout == null) {
3166            // Identify whether we have dragged over a side page,
3167            // otherwise just use the current page
3168            layout = workspaceInModalState() ?
3169                    findMatchingPageForDragOver(d.dragView, d.x, d.y, false)
3170                    : getCurrentDropLayout();
3171        }
3172        if (layout != mDragTargetLayout) {
3173            setCurrentDropLayout(layout);
3174            setCurrentDragOverlappingLayout(layout);
3175            return true;
3176        }
3177        return false;
3178    }
3179
3180    private void manageFolderFeedback(CellLayout targetLayout,
3181            int[] targetCell, float distance, DragObject dragObject) {
3182        if (distance > mMaxDistanceForFolderCreation) return;
3183
3184        final View dragOverView = mDragTargetLayout.getChildAt(mTargetCell[0], mTargetCell[1]);
3185        ItemInfo info = dragObject.dragInfo;
3186        boolean userFolderPending = willCreateUserFolder(info, dragOverView, false);
3187        if (mDragMode == DRAG_MODE_NONE && userFolderPending &&
3188                !mFolderCreationAlarm.alarmPending()) {
3189
3190            FolderCreationAlarmListener listener = new
3191                    FolderCreationAlarmListener(targetLayout, targetCell[0], targetCell[1]);
3192
3193            if (!dragObject.accessibleDrag) {
3194                mFolderCreationAlarm.setOnAlarmListener(listener);
3195                mFolderCreationAlarm.setAlarm(FOLDER_CREATION_TIMEOUT);
3196            } else {
3197                listener.onAlarm(mFolderCreationAlarm);
3198            }
3199
3200            if (dragObject.stateAnnouncer != null) {
3201                dragObject.stateAnnouncer.announce(WorkspaceAccessibilityHelper
3202                        .getDescriptionForDropOver(dragOverView, getContext()));
3203            }
3204            return;
3205        }
3206
3207        boolean willAddToFolder = willAddToExistingUserFolder(info, dragOverView);
3208        if (willAddToFolder && mDragMode == DRAG_MODE_NONE) {
3209            mDragOverFolderIcon = ((FolderIcon) dragOverView);
3210            mDragOverFolderIcon.onDragEnter(info);
3211            if (targetLayout != null) {
3212                targetLayout.clearDragOutlines();
3213            }
3214            setDragMode(DRAG_MODE_ADD_TO_FOLDER);
3215
3216            if (dragObject.stateAnnouncer != null) {
3217                dragObject.stateAnnouncer.announce(WorkspaceAccessibilityHelper
3218                        .getDescriptionForDropOver(dragOverView, getContext()));
3219            }
3220            return;
3221        }
3222
3223        if (mDragMode == DRAG_MODE_ADD_TO_FOLDER && !willAddToFolder) {
3224            setDragMode(DRAG_MODE_NONE);
3225        }
3226        if (mDragMode == DRAG_MODE_CREATE_FOLDER && !userFolderPending) {
3227            setDragMode(DRAG_MODE_NONE);
3228        }
3229    }
3230
3231    class FolderCreationAlarmListener implements OnAlarmListener {
3232        CellLayout layout;
3233        int cellX;
3234        int cellY;
3235
3236        FolderIcon.PreviewBackground bg = new FolderIcon.PreviewBackground();
3237
3238        public FolderCreationAlarmListener(CellLayout layout, int cellX, int cellY) {
3239            this.layout = layout;
3240            this.cellX = cellX;
3241            this.cellY = cellY;
3242
3243            DeviceProfile grid = mLauncher.getDeviceProfile();
3244            BubbleTextView cell = (BubbleTextView) layout.getChildAt(cellX, cellY);
3245
3246            bg.setup(getResources().getDisplayMetrics(), grid, null,
3247                    cell.getMeasuredWidth(), cell.getPaddingTop());
3248
3249            // The full preview background should appear behind the icon
3250            bg.isClipping = false;
3251        }
3252
3253        public void onAlarm(Alarm alarm) {
3254            mFolderCreateBg = bg;
3255            mFolderCreateBg.animateToAccept(layout, cellX, cellY);
3256            layout.clearDragOutlines();
3257            setDragMode(DRAG_MODE_CREATE_FOLDER);
3258        }
3259    }
3260
3261    class ReorderAlarmListener implements OnAlarmListener {
3262        float[] dragViewCenter;
3263        int minSpanX, minSpanY, spanX, spanY;
3264        DragObject dragObject;
3265        View child;
3266
3267        public ReorderAlarmListener(float[] dragViewCenter, int minSpanX, int minSpanY, int spanX,
3268                int spanY, DragObject dragObject, View child) {
3269            this.dragViewCenter = dragViewCenter;
3270            this.minSpanX = minSpanX;
3271            this.minSpanY = minSpanY;
3272            this.spanX = spanX;
3273            this.spanY = spanY;
3274            this.child = child;
3275            this.dragObject = dragObject;
3276        }
3277
3278        public void onAlarm(Alarm alarm) {
3279            int[] resultSpan = new int[2];
3280            mTargetCell = findNearestArea((int) mDragViewVisualCenter[0],
3281                    (int) mDragViewVisualCenter[1], minSpanX, minSpanY, mDragTargetLayout,
3282                    mTargetCell);
3283            mLastReorderX = mTargetCell[0];
3284            mLastReorderY = mTargetCell[1];
3285
3286            mTargetCell = mDragTargetLayout.performReorder((int) mDragViewVisualCenter[0],
3287                (int) mDragViewVisualCenter[1], minSpanX, minSpanY, spanX, spanY,
3288                child, mTargetCell, resultSpan, CellLayout.MODE_DRAG_OVER);
3289
3290            if (mTargetCell[0] < 0 || mTargetCell[1] < 0) {
3291                mDragTargetLayout.revertTempState();
3292            } else {
3293                setDragMode(DRAG_MODE_REORDER);
3294            }
3295
3296            boolean resize = resultSpan[0] != spanX || resultSpan[1] != spanY;
3297            mDragTargetLayout.visualizeDropLocation(child, mOutlineProvider,
3298                mTargetCell[0], mTargetCell[1], resultSpan[0], resultSpan[1], resize, dragObject);
3299        }
3300    }
3301
3302    @Override
3303    public void getHitRectRelativeToDragLayer(Rect outRect) {
3304        // We want the workspace to have the whole area of the display (it will find the correct
3305        // cell layout to drop to in the existing drag/drop logic.
3306        mLauncher.getDragLayer().getDescendantRectRelativeToSelf(this, outRect);
3307    }
3308
3309    /**
3310     * Drop an item that didn't originate on one of the workspace screens.
3311     * It may have come from Launcher (e.g. from all apps or customize), or it may have
3312     * come from another app altogether.
3313     *
3314     * NOTE: This can also be called when we are outside of a drag event, when we want
3315     * to add an item to one of the workspace screens.
3316     */
3317    private void onDropExternal(final int[] touchXY, final ItemInfo dragInfo,
3318            final CellLayout cellLayout, boolean insertAtFirst, DragObject d) {
3319        final Runnable exitSpringLoadedRunnable = new Runnable() {
3320            @Override
3321            public void run() {
3322                mLauncher.exitSpringLoadedDragModeDelayed(true,
3323                        Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
3324            }
3325        };
3326
3327        ItemInfo info = dragInfo;
3328        int spanX = info.spanX;
3329        int spanY = info.spanY;
3330        if (mDragInfo != null) {
3331            spanX = mDragInfo.spanX;
3332            spanY = mDragInfo.spanY;
3333        }
3334
3335        final long container = mLauncher.isHotseatLayout(cellLayout) ?
3336                LauncherSettings.Favorites.CONTAINER_HOTSEAT :
3337                    LauncherSettings.Favorites.CONTAINER_DESKTOP;
3338        final long screenId = getIdForScreen(cellLayout);
3339        if (!mLauncher.isHotseatLayout(cellLayout)
3340                && screenId != getScreenIdForPageIndex(mCurrentPage)
3341                && mState != State.SPRING_LOADED) {
3342            snapToScreenId(screenId, null);
3343        }
3344
3345        if (info instanceof PendingAddItemInfo) {
3346            final PendingAddItemInfo pendingInfo = (PendingAddItemInfo) dragInfo;
3347
3348            boolean findNearestVacantCell = true;
3349            if (pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
3350                mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY,
3351                        cellLayout, mTargetCell);
3352                float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0],
3353                        mDragViewVisualCenter[1], mTargetCell);
3354                if (willCreateUserFolder(d.dragInfo, cellLayout, mTargetCell, distance, true)
3355                        || willAddToExistingUserFolder(
3356                                d.dragInfo, cellLayout, mTargetCell, distance)) {
3357                    findNearestVacantCell = false;
3358                }
3359            }
3360
3361            final ItemInfo item = d.dragInfo;
3362            boolean updateWidgetSize = false;
3363            if (findNearestVacantCell) {
3364                int minSpanX = item.spanX;
3365                int minSpanY = item.spanY;
3366                if (item.minSpanX > 0 && item.minSpanY > 0) {
3367                    minSpanX = item.minSpanX;
3368                    minSpanY = item.minSpanY;
3369                }
3370                int[] resultSpan = new int[2];
3371                mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0],
3372                        (int) mDragViewVisualCenter[1], minSpanX, minSpanY, info.spanX, info.spanY,
3373                        null, mTargetCell, resultSpan, CellLayout.MODE_ON_DROP_EXTERNAL);
3374
3375                if (resultSpan[0] != item.spanX || resultSpan[1] != item.spanY) {
3376                    updateWidgetSize = true;
3377                }
3378                item.spanX = resultSpan[0];
3379                item.spanY = resultSpan[1];
3380            }
3381
3382            Runnable onAnimationCompleteRunnable = new Runnable() {
3383                @Override
3384                public void run() {
3385                    // Normally removeExtraEmptyScreen is called in Workspace#onDragEnd, but when
3386                    // adding an item that may not be dropped right away (due to a config activity)
3387                    // we defer the removal until the activity returns.
3388                    deferRemoveExtraEmptyScreen();
3389
3390                    // When dragging and dropping from customization tray, we deal with creating
3391                    // widgets/shortcuts/folders in a slightly different way
3392                    mLauncher.addPendingItem(pendingInfo, container, screenId, mTargetCell,
3393                            item.spanX, item.spanY);
3394                }
3395            };
3396            boolean isWidget = pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET
3397                    || pendingInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
3398
3399            AppWidgetHostView finalView = isWidget ?
3400                    ((PendingAddWidgetInfo) pendingInfo).boundWidget : null;
3401
3402            if (finalView != null && updateWidgetSize) {
3403                AppWidgetResizeFrame.updateWidgetSizeRanges(finalView, mLauncher, item.spanX,
3404                        item.spanY);
3405            }
3406
3407            int animationStyle = ANIMATE_INTO_POSITION_AND_DISAPPEAR;
3408            if (isWidget && ((PendingAddWidgetInfo) pendingInfo).info != null &&
3409                    ((PendingAddWidgetInfo) pendingInfo).info.configure != null) {
3410                animationStyle = ANIMATE_INTO_POSITION_AND_REMAIN;
3411            }
3412            animateWidgetDrop(info, cellLayout, d.dragView, onAnimationCompleteRunnable,
3413                    animationStyle, finalView, true);
3414        } else {
3415            // This is for other drag/drop cases, like dragging from All Apps
3416            View view = null;
3417
3418            switch (info.itemType) {
3419            case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3420            case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
3421            case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
3422                if (info.container == NO_ID && info instanceof AppInfo) {
3423                    // Came from all apps -- make a copy
3424                    info = ((AppInfo) info).makeShortcut();
3425                    d.dragInfo = info;
3426                }
3427                view = mLauncher.createShortcut(cellLayout, (ShortcutInfo) info);
3428                break;
3429            case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
3430                view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, cellLayout,
3431                        (FolderInfo) info, mIconCache);
3432                break;
3433            default:
3434                throw new IllegalStateException("Unknown item type: " + info.itemType);
3435            }
3436
3437            // First we find the cell nearest to point at which the item is
3438            // dropped, without any consideration to whether there is an item there.
3439            if (touchXY != null) {
3440                mTargetCell = findNearestArea((int) touchXY[0], (int) touchXY[1], spanX, spanY,
3441                        cellLayout, mTargetCell);
3442                float distance = cellLayout.getDistanceFromCell(mDragViewVisualCenter[0],
3443                        mDragViewVisualCenter[1], mTargetCell);
3444                d.postAnimationRunnable = exitSpringLoadedRunnable;
3445                if (createUserFolderIfNecessary(view, container, cellLayout, mTargetCell, distance,
3446                        true, d.dragView, d.postAnimationRunnable)) {
3447                    return;
3448                }
3449                if (addToExistingFolderIfNecessary(view, cellLayout, mTargetCell, distance, d,
3450                        true)) {
3451                    return;
3452                }
3453            }
3454
3455            if (touchXY != null) {
3456                // when dragging and dropping, just find the closest free spot
3457                mTargetCell = cellLayout.performReorder((int) mDragViewVisualCenter[0],
3458                        (int) mDragViewVisualCenter[1], 1, 1, 1, 1,
3459                        null, mTargetCell, null, CellLayout.MODE_ON_DROP_EXTERNAL);
3460            } else {
3461                cellLayout.findCellForSpan(mTargetCell, 1, 1);
3462            }
3463            // Add the item to DB before adding to screen ensures that the container and other
3464            // values of the info is properly updated.
3465            LauncherModel.addOrMoveItemInDatabase(mLauncher, info, container, screenId,
3466                    mTargetCell[0], mTargetCell[1]);
3467
3468            addInScreen(view, container, screenId, mTargetCell[0], mTargetCell[1], info.spanX,
3469                    info.spanY, insertAtFirst);
3470            cellLayout.onDropChild(view);
3471            cellLayout.getShortcutsAndWidgets().measureChild(view);
3472
3473            if (d.dragView != null) {
3474                // We wrap the animation call in the temporary set and reset of the current
3475                // cellLayout to its final transform -- this means we animate the drag view to
3476                // the correct final location.
3477                setFinalTransitionTransform(cellLayout);
3478                mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, view,
3479                        exitSpringLoadedRunnable, this);
3480                resetTransitionTransform(cellLayout);
3481            }
3482        }
3483    }
3484
3485    public Bitmap createWidgetBitmap(ItemInfo widgetInfo, View layout) {
3486        int[] unScaledSize = mLauncher.getWorkspace().estimateItemSize(widgetInfo, false);
3487        int visibility = layout.getVisibility();
3488        layout.setVisibility(VISIBLE);
3489
3490        int width = MeasureSpec.makeMeasureSpec(unScaledSize[0], MeasureSpec.EXACTLY);
3491        int height = MeasureSpec.makeMeasureSpec(unScaledSize[1], MeasureSpec.EXACTLY);
3492        Bitmap b = Bitmap.createBitmap(unScaledSize[0], unScaledSize[1],
3493                Bitmap.Config.ARGB_8888);
3494        mCanvas.setBitmap(b);
3495
3496        layout.measure(width, height);
3497        layout.layout(0, 0, unScaledSize[0], unScaledSize[1]);
3498        layout.draw(mCanvas);
3499        mCanvas.setBitmap(null);
3500        layout.setVisibility(visibility);
3501        return b;
3502    }
3503
3504    private void getFinalPositionForDropAnimation(int[] loc, float[] scaleXY,
3505            DragView dragView, CellLayout layout, ItemInfo info, int[] targetCell, boolean scale) {
3506        // Now we animate the dragView, (ie. the widget or shortcut preview) into its final
3507        // location and size on the home screen.
3508        int spanX = info.spanX;
3509        int spanY = info.spanY;
3510
3511        Rect r = estimateItemPosition(layout, targetCell[0], targetCell[1], spanX, spanY);
3512        loc[0] = r.left;
3513        loc[1] = r.top;
3514
3515        setFinalTransitionTransform(layout);
3516        float cellLayoutScale =
3517                mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(layout, loc, true);
3518        resetTransitionTransform(layout);
3519
3520        float dragViewScaleX;
3521        float dragViewScaleY;
3522        if (scale) {
3523            dragViewScaleX = (1.0f * r.width()) / dragView.getMeasuredWidth();
3524            dragViewScaleY = (1.0f * r.height()) / dragView.getMeasuredHeight();
3525        } else {
3526            dragViewScaleX = 1f;
3527            dragViewScaleY = 1f;
3528        }
3529
3530        // The animation will scale the dragView about its center, so we need to center about
3531        // the final location.
3532        loc[0] -= (dragView.getMeasuredWidth() - cellLayoutScale * r.width()) / 2
3533                - Math.ceil(layout.getUnusedHorizontalSpace() / 2f);
3534        loc[1] -= (dragView.getMeasuredHeight() - cellLayoutScale * r.height()) / 2;
3535
3536        scaleXY[0] = dragViewScaleX * cellLayoutScale;
3537        scaleXY[1] = dragViewScaleY * cellLayoutScale;
3538    }
3539
3540    public void animateWidgetDrop(ItemInfo info, CellLayout cellLayout, final DragView dragView,
3541            final Runnable onCompleteRunnable, int animationType, final View finalView,
3542            boolean external) {
3543        Rect from = new Rect();
3544        mLauncher.getDragLayer().getViewRectRelativeToSelf(dragView, from);
3545
3546        int[] finalPos = new int[2];
3547        float scaleXY[] = new float[2];
3548        boolean scalePreview = !(info instanceof PendingAddShortcutInfo);
3549        getFinalPositionForDropAnimation(finalPos, scaleXY, dragView, cellLayout, info, mTargetCell,
3550                scalePreview);
3551
3552        Resources res = mLauncher.getResources();
3553        final int duration = res.getInteger(R.integer.config_dropAnimMaxDuration) - 200;
3554
3555        boolean isWidget = info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET ||
3556                info.itemType == LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET;
3557        if ((animationType == ANIMATE_INTO_POSITION_AND_RESIZE || external) && finalView != null) {
3558            Bitmap crossFadeBitmap = createWidgetBitmap(info, finalView);
3559            dragView.setCrossFadeBitmap(crossFadeBitmap);
3560            dragView.crossFade((int) (duration * 0.8f));
3561        } else if (isWidget && external) {
3562            scaleXY[0] = scaleXY[1] = Math.min(scaleXY[0],  scaleXY[1]);
3563        }
3564
3565        DragLayer dragLayer = mLauncher.getDragLayer();
3566        if (animationType == CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION) {
3567            mLauncher.getDragLayer().animateViewIntoPosition(dragView, finalPos, 0f, 0.1f, 0.1f,
3568                    DragLayer.ANIMATION_END_DISAPPEAR, onCompleteRunnable, duration);
3569        } else {
3570            int endStyle;
3571            if (animationType == ANIMATE_INTO_POSITION_AND_REMAIN) {
3572                endStyle = DragLayer.ANIMATION_END_REMAIN_VISIBLE;
3573            } else {
3574                endStyle = DragLayer.ANIMATION_END_DISAPPEAR;
3575            }
3576
3577            Runnable onComplete = new Runnable() {
3578                @Override
3579                public void run() {
3580                    if (finalView != null) {
3581                        finalView.setVisibility(VISIBLE);
3582                    }
3583                    if (onCompleteRunnable != null) {
3584                        onCompleteRunnable.run();
3585                    }
3586                }
3587            };
3588            dragLayer.animateViewIntoPosition(dragView, from.left, from.top, finalPos[0],
3589                    finalPos[1], 1, 1, 1, scaleXY[0], scaleXY[1], onComplete, endStyle,
3590                    duration, this);
3591        }
3592    }
3593
3594    public void setFinalTransitionTransform(CellLayout layout) {
3595        if (isSwitchingState()) {
3596            mCurrentScale = getScaleX();
3597            setScaleX(mStateTransitionAnimation.getFinalScale());
3598            setScaleY(mStateTransitionAnimation.getFinalScale());
3599        }
3600    }
3601    public void resetTransitionTransform(CellLayout layout) {
3602        if (isSwitchingState()) {
3603            setScaleX(mCurrentScale);
3604            setScaleY(mCurrentScale);
3605        }
3606    }
3607
3608    public WorkspaceStateTransitionAnimation getStateTransitionAnimation() {
3609        return mStateTransitionAnimation;
3610    }
3611
3612    /**
3613     * Return the current {@link CellLayout}, correctly picking the destination
3614     * screen while a scroll is in progress.
3615     */
3616    public CellLayout getCurrentDropLayout() {
3617        return (CellLayout) getChildAt(getNextPage());
3618    }
3619
3620    /**
3621     * Return the current CellInfo describing our current drag; this method exists
3622     * so that Launcher can sync this object with the correct info when the activity is created/
3623     * destroyed
3624     *
3625     */
3626    public CellLayout.CellInfo getDragInfo() {
3627        return mDragInfo;
3628    }
3629
3630    public int getCurrentPageOffsetFromCustomContent() {
3631        return getNextPage() - numCustomPages();
3632    }
3633
3634    /**
3635     * Calculate the nearest cell where the given object would be dropped.
3636     *
3637     * pixelX and pixelY should be in the coordinate system of layout
3638     */
3639    @Thunk int[] findNearestArea(int pixelX, int pixelY,
3640            int spanX, int spanY, CellLayout layout, int[] recycle) {
3641        return layout.findNearestArea(
3642                pixelX, pixelY, spanX, spanY, recycle);
3643    }
3644
3645    void setup(DragController dragController) {
3646        mSpringLoadedDragController = new SpringLoadedDragController(mLauncher);
3647        mDragController = dragController;
3648
3649        // hardware layers on children are enabled on startup, but should be disabled until
3650        // needed
3651        updateChildrenLayersEnabled(false);
3652    }
3653
3654    /**
3655     * Called at the end of a drag which originated on the workspace.
3656     */
3657    public void onDropCompleted(final View target, final DragObject d,
3658            final boolean isFlingToDelete, final boolean success) {
3659        if (mDeferDropAfterUninstall) {
3660            mDeferredAction = new Runnable() {
3661                public void run() {
3662                    onDropCompleted(target, d, isFlingToDelete, success);
3663                    mDeferredAction = null;
3664                }
3665            };
3666            return;
3667        }
3668
3669        boolean beingCalledAfterUninstall = mDeferredAction != null;
3670
3671        if (success && !(beingCalledAfterUninstall && !mUninstallSuccessful)) {
3672            if (target != this && mDragInfo != null) {
3673                removeWorkspaceItem(mDragInfo.cell);
3674            }
3675        } else if (mDragInfo != null) {
3676            final CellLayout cellLayout = mLauncher.getCellLayout(
3677                    mDragInfo.container, mDragInfo.screenId);
3678            if (cellLayout != null) {
3679                cellLayout.onDropChild(mDragInfo.cell);
3680            } else if (ProviderConfig.IS_DOGFOOD_BUILD) {
3681                throw new RuntimeException("Invalid state: cellLayout == null in "
3682                        + "Workspace#onDropCompleted. Please file a bug. ");
3683            };
3684        }
3685        if ((d.cancelled || (beingCalledAfterUninstall && !mUninstallSuccessful))
3686                && mDragInfo.cell != null) {
3687            mDragInfo.cell.setVisibility(VISIBLE);
3688        }
3689        mOutlineProvider = null;
3690        mDragInfo = null;
3691
3692        if (!isFlingToDelete) {
3693            // Fling to delete already exits spring loaded mode after the animation finishes.
3694            mLauncher.exitSpringLoadedDragModeDelayed(success,
3695                    Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, mDelayedResizeRunnable);
3696            mDelayedResizeRunnable = null;
3697        }
3698    }
3699
3700    /**
3701     * For opposite operation. See {@link #addInScreen}.
3702     */
3703    public void removeWorkspaceItem(View v) {
3704        CellLayout parentCell = getParentCellLayoutForView(v);
3705        if (parentCell != null) {
3706            parentCell.removeView(v);
3707        } else if (ProviderConfig.IS_DOGFOOD_BUILD) {
3708            // When an app is uninstalled using the drop target, we wait until resume to remove
3709            // the icon. We also remove all the corresponding items from the workspace at
3710            // {@link Launcher#bindComponentsRemoved}. That call can come before or after
3711            // {@link Launcher#mOnResumeCallbacks} depending on how busy the worker thread is.
3712            Log.e(TAG, "mDragInfo.cell has null parent");
3713        }
3714        if (v instanceof DropTarget) {
3715            mDragController.removeDropTarget((DropTarget) v);
3716        }
3717    }
3718
3719    /**
3720     * Removes all folder listeners
3721     */
3722    public void removeFolderListeners() {
3723        mapOverItems(false, new ItemOperator() {
3724            @Override
3725            public boolean evaluate(ItemInfo info, View view) {
3726                if (view instanceof FolderIcon) {
3727                    ((FolderIcon) view).removeListeners();
3728                }
3729                return false;
3730            }
3731        });
3732    }
3733
3734    @Override
3735    public void deferCompleteDropAfterUninstallActivity() {
3736        mDeferDropAfterUninstall = true;
3737    }
3738
3739    /// maybe move this into a smaller part
3740    @Override
3741    public void onDragObjectRemoved(boolean success) {
3742        mDeferDropAfterUninstall = false;
3743        mUninstallSuccessful = success;
3744        if (mDeferredAction != null) {
3745            mDeferredAction.run();
3746        }
3747    }
3748
3749    @Override
3750    public float getIntrinsicIconScaleFactor() {
3751        return 1f;
3752    }
3753
3754    @Override
3755    public boolean supportsFlingToDelete() {
3756        return true;
3757    }
3758
3759    @Override
3760    public boolean supportsAppInfoDropTarget() {
3761        return !FeatureFlags.LAUNCHER3_LEGACY_WORKSPACE_DND;
3762    }
3763
3764    @Override
3765    public boolean supportsDeleteDropTarget() {
3766        return true;
3767    }
3768
3769    @Override
3770    public void onFlingToDelete(DragObject d, PointF vec) {
3771        // Do nothing
3772    }
3773
3774    @Override
3775    public void onFlingToDeleteCompleted() {
3776        // Do nothing
3777    }
3778
3779    public boolean isDropEnabled() {
3780        return true;
3781    }
3782
3783    @Override
3784    protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
3785        // We don't dispatch restoreInstanceState to our children using this code path.
3786        // Some pages will be restored immediately as their items are bound immediately, and
3787        // others we will need to wait until after their items are bound.
3788        mSavedStates = container;
3789    }
3790
3791    public void restoreInstanceStateForChild(int child) {
3792        if (mSavedStates != null) {
3793            mRestoredPages.add(child);
3794            CellLayout cl = (CellLayout) getChildAt(child);
3795            if (cl != null) {
3796                cl.restoreInstanceState(mSavedStates);
3797            }
3798        }
3799    }
3800
3801    public void restoreInstanceStateForRemainingPages() {
3802        int count = getChildCount();
3803        for (int i = 0; i < count; i++) {
3804            if (!mRestoredPages.contains(i)) {
3805                restoreInstanceStateForChild(i);
3806            }
3807        }
3808        mRestoredPages.clear();
3809        mSavedStates = null;
3810    }
3811
3812    @Override
3813    public void scrollLeft() {
3814        if (!workspaceInModalState() && !mIsSwitchingState) {
3815            super.scrollLeft();
3816        }
3817        Folder openFolder = getOpenFolder();
3818        if (openFolder != null) {
3819            openFolder.completeDragExit();
3820        }
3821    }
3822
3823    @Override
3824    public void scrollRight() {
3825        if (!workspaceInModalState() && !mIsSwitchingState) {
3826            super.scrollRight();
3827        }
3828        Folder openFolder = getOpenFolder();
3829        if (openFolder != null) {
3830            openFolder.completeDragExit();
3831        }
3832    }
3833
3834    @Override
3835    public boolean onEnterScrollArea(int x, int y, int direction) {
3836        // Ignore the scroll area if we are dragging over the hot seat
3837        boolean isPortrait = !mLauncher.getDeviceProfile().isLandscape;
3838        if (mLauncher.getHotseat() != null && isPortrait) {
3839            Rect r = new Rect();
3840            mLauncher.getHotseat().getHitRect(r);
3841            if (r.contains(x, y)) {
3842                return false;
3843            }
3844        }
3845
3846        boolean result = false;
3847        if (!workspaceInModalState() && !mIsSwitchingState && getOpenFolder() == null) {
3848            mInScrollArea = true;
3849
3850            final int page = getNextPage() +
3851                       (direction == DragController.SCROLL_LEFT ? -1 : 1);
3852            // We always want to exit the current layout to ensure parity of enter / exit
3853            setCurrentDropLayout(null);
3854
3855            if (0 <= page && page < getChildCount()) {
3856                // Ensure that we are not dragging over to the custom content screen
3857                if (getScreenIdForPageIndex(page) == CUSTOM_CONTENT_SCREEN_ID) {
3858                    return false;
3859                }
3860
3861                CellLayout layout = (CellLayout) getChildAt(page);
3862                setCurrentDragOverlappingLayout(layout);
3863
3864                // Workspace is responsible for drawing the edge glow on adjacent pages,
3865                // so we need to redraw the workspace when this may have changed.
3866                invalidate();
3867                result = true;
3868            }
3869        }
3870        return result;
3871    }
3872
3873    @Override
3874    public boolean onExitScrollArea() {
3875        boolean result = false;
3876        if (mInScrollArea) {
3877            invalidate();
3878            CellLayout layout = getCurrentDropLayout();
3879            setCurrentDropLayout(layout);
3880            setCurrentDragOverlappingLayout(layout);
3881
3882            result = true;
3883            mInScrollArea = false;
3884        }
3885        return result;
3886    }
3887
3888    private void onResetScrollArea() {
3889        setCurrentDragOverlappingLayout(null);
3890        mInScrollArea = false;
3891    }
3892
3893    /**
3894     * Returns a specific CellLayout
3895     */
3896    CellLayout getParentCellLayoutForView(View v) {
3897        ArrayList<CellLayout> layouts = getWorkspaceAndHotseatCellLayouts();
3898        for (CellLayout layout : layouts) {
3899            if (layout.getShortcutsAndWidgets().indexOfChild(v) > -1) {
3900                return layout;
3901            }
3902        }
3903        return null;
3904    }
3905
3906    /**
3907     * Returns a list of all the CellLayouts in the workspace.
3908     */
3909    ArrayList<CellLayout> getWorkspaceAndHotseatCellLayouts() {
3910        ArrayList<CellLayout> layouts = new ArrayList<CellLayout>();
3911        int screenCount = getChildCount();
3912        for (int screen = 0; screen < screenCount; screen++) {
3913            layouts.add(((CellLayout) getChildAt(screen)));
3914        }
3915        if (mLauncher.getHotseat() != null) {
3916            layouts.add(mLauncher.getHotseat().getLayout());
3917        }
3918        return layouts;
3919    }
3920
3921    /**
3922     * We should only use this to search for specific children.  Do not use this method to modify
3923     * ShortcutsAndWidgetsContainer directly. Includes ShortcutAndWidgetContainers from
3924     * the hotseat and workspace pages
3925     */
3926    ArrayList<ShortcutAndWidgetContainer> getAllShortcutAndWidgetContainers() {
3927        ArrayList<ShortcutAndWidgetContainer> childrenLayouts = new ArrayList<>();
3928        int screenCount = getChildCount();
3929        for (int screen = 0; screen < screenCount; screen++) {
3930            childrenLayouts.add(((CellLayout) getChildAt(screen)).getShortcutsAndWidgets());
3931        }
3932        if (mLauncher.getHotseat() != null) {
3933            childrenLayouts.add(mLauncher.getHotseat().getLayout().getShortcutsAndWidgets());
3934        }
3935        return childrenLayouts;
3936    }
3937
3938    public View getHomescreenIconByItemId(final long id) {
3939        return getFirstMatch(new ItemOperator() {
3940
3941            @Override
3942            public boolean evaluate(ItemInfo info, View v) {
3943                return info != null && info.id == id;
3944            }
3945        });
3946    }
3947
3948    public View getViewForTag(final Object tag) {
3949        return getFirstMatch(new ItemOperator() {
3950
3951            @Override
3952            public boolean evaluate(ItemInfo info, View v) {
3953                return info == tag;
3954            }
3955        });
3956    }
3957
3958    public LauncherAppWidgetHostView getWidgetForAppWidgetId(final int appWidgetId) {
3959        return (LauncherAppWidgetHostView) getFirstMatch(new ItemOperator() {
3960
3961            @Override
3962            public boolean evaluate(ItemInfo info, View v) {
3963                return (info instanceof LauncherAppWidgetInfo) &&
3964                        ((LauncherAppWidgetInfo) info).appWidgetId == appWidgetId;
3965            }
3966        });
3967    }
3968
3969    public View getFirstMatch(final ItemOperator operator) {
3970        final View[] value = new View[1];
3971        mapOverItems(MAP_NO_RECURSE, new ItemOperator() {
3972            @Override
3973            public boolean evaluate(ItemInfo info, View v) {
3974                if (operator.evaluate(info, v)) {
3975                    value[0] = v;
3976                    return true;
3977                }
3978                return false;
3979            }
3980        });
3981        return value[0];
3982    }
3983
3984    void clearDropTargets() {
3985        mapOverItems(MAP_NO_RECURSE, new ItemOperator() {
3986            @Override
3987            public boolean evaluate(ItemInfo info, View v) {
3988                if (v instanceof DropTarget) {
3989                    mDragController.removeDropTarget((DropTarget) v);
3990                }
3991                // not done, process all the shortcuts
3992                return false;
3993            }
3994        });
3995    }
3996
3997    /**
3998     * Removes items that match the {@param matcher}. When applications are removed
3999     * as a part of an update, this is called to ensure that other widgets and application
4000     * shortcuts are not removed.
4001     */
4002    public void removeItemsByMatcher(final ItemInfoMatcher matcher) {
4003        ArrayList<CellLayout> cellLayouts = getWorkspaceAndHotseatCellLayouts();
4004        for (final CellLayout layoutParent: cellLayouts) {
4005            final ViewGroup layout = layoutParent.getShortcutsAndWidgets();
4006
4007            final HashMap<ItemInfo, View> children = new HashMap<>();
4008            for (int j = 0; j < layout.getChildCount(); j++) {
4009                final View view = layout.getChildAt(j);
4010                children.put((ItemInfo) view.getTag(), view);
4011            }
4012
4013            final ArrayList<View> childrenToRemove = new ArrayList<>();
4014            final HashMap<FolderInfo, ArrayList<ShortcutInfo>> folderAppsToRemove = new HashMap<>();
4015            LauncherModel.ItemInfoFilter filter = new LauncherModel.ItemInfoFilter() {
4016                @Override
4017                public boolean filterItem(ItemInfo parent, ItemInfo info,
4018                        ComponentName cn) {
4019                    if (parent instanceof FolderInfo) {
4020                        if (matcher.matches(info, cn)) {
4021                            FolderInfo folder = (FolderInfo) parent;
4022                            ArrayList<ShortcutInfo> appsToRemove;
4023                            if (folderAppsToRemove.containsKey(folder)) {
4024                                appsToRemove = folderAppsToRemove.get(folder);
4025                            } else {
4026                                appsToRemove = new ArrayList<ShortcutInfo>();
4027                                folderAppsToRemove.put(folder, appsToRemove);
4028                            }
4029                            appsToRemove.add((ShortcutInfo) info);
4030                            return true;
4031                        }
4032                    } else {
4033                        if (matcher.matches(info, cn)) {
4034                            childrenToRemove.add(children.get(info));
4035                            return true;
4036                        }
4037                    }
4038                    return false;
4039                }
4040            };
4041            LauncherModel.filterItemInfos(children.keySet(), filter);
4042
4043            // Remove all the apps from their folders
4044            for (FolderInfo folder : folderAppsToRemove.keySet()) {
4045                ArrayList<ShortcutInfo> appsToRemove = folderAppsToRemove.get(folder);
4046                for (ShortcutInfo info : appsToRemove) {
4047                    folder.remove(info, false);
4048                }
4049            }
4050
4051            // Remove all the other children
4052            for (View child : childrenToRemove) {
4053                // Note: We can not remove the view directly from CellLayoutChildren as this
4054                // does not re-mark the spaces as unoccupied.
4055                layoutParent.removeViewInLayout(child);
4056                if (child instanceof DropTarget) {
4057                    mDragController.removeDropTarget((DropTarget) child);
4058                }
4059            }
4060
4061            if (childrenToRemove.size() > 0) {
4062                layout.requestLayout();
4063                layout.invalidate();
4064            }
4065        }
4066
4067        // Strip all the empty screens
4068        stripEmptyScreens();
4069    }
4070
4071    public interface ItemOperator {
4072        /**
4073         * Process the next itemInfo, possibly with side-effect on the next item.
4074         *
4075         * @param info info for the shortcut
4076         * @param view view for the shortcut
4077         * @return true if done, false to continue the map
4078         */
4079        public boolean evaluate(ItemInfo info, View view);
4080    }
4081
4082    /**
4083     * Map the operator over the shortcuts and widgets, return the first-non-null value.
4084     *
4085     * @param recurse true: iterate over folder children. false: op get the folders themselves.
4086     * @param op the operator to map over the shortcuts
4087     */
4088    void mapOverItems(boolean recurse, ItemOperator op) {
4089        ArrayList<ShortcutAndWidgetContainer> containers = getAllShortcutAndWidgetContainers();
4090        final int containerCount = containers.size();
4091        for (int containerIdx = 0; containerIdx < containerCount; containerIdx++) {
4092            ShortcutAndWidgetContainer container = containers.get(containerIdx);
4093            // map over all the shortcuts on the workspace
4094            final int itemCount = container.getChildCount();
4095            for (int itemIdx = 0; itemIdx < itemCount; itemIdx++) {
4096                View item = container.getChildAt(itemIdx);
4097                ItemInfo info = (ItemInfo) item.getTag();
4098                if (recurse && info instanceof FolderInfo && item instanceof FolderIcon) {
4099                    FolderIcon folder = (FolderIcon) item;
4100                    ArrayList<View> folderChildren = folder.getFolder().getItemsInReadingOrder();
4101                    // map over all the children in the folder
4102                    final int childCount = folderChildren.size();
4103                    for (int childIdx = 0; childIdx < childCount; childIdx++) {
4104                        View child = folderChildren.get(childIdx);
4105                        info = (ItemInfo) child.getTag();
4106                        if (op.evaluate(info, child)) {
4107                            return;
4108                        }
4109                    }
4110                } else {
4111                    if (op.evaluate(info, item)) {
4112                        return;
4113                    }
4114                }
4115            }
4116        }
4117    }
4118
4119    void updateShortcuts(ArrayList<ShortcutInfo> shortcuts) {
4120        int total  = shortcuts.size();
4121        final HashSet<ShortcutInfo> updates = new HashSet<ShortcutInfo>(total);
4122        final HashSet<Long> folderIds = new HashSet<>();
4123
4124        for (int i = 0; i < total; i++) {
4125            ShortcutInfo s = shortcuts.get(i);
4126            updates.add(s);
4127            folderIds.add(s.container);
4128        }
4129
4130        mapOverItems(MAP_RECURSE, new ItemOperator() {
4131            @Override
4132            public boolean evaluate(ItemInfo info, View v) {
4133                if (info instanceof ShortcutInfo && v instanceof BubbleTextView &&
4134                        updates.contains(info)) {
4135                    ShortcutInfo si = (ShortcutInfo) info;
4136                    BubbleTextView shortcut = (BubbleTextView) v;
4137                    Drawable oldIcon = getTextViewIcon(shortcut);
4138                    boolean oldPromiseState = (oldIcon instanceof PreloadIconDrawable)
4139                            && ((PreloadIconDrawable) oldIcon).hasNotCompleted();
4140                    shortcut.applyFromShortcutInfo(si, mIconCache,
4141                            si.isPromise() != oldPromiseState);
4142                }
4143                // process all the shortcuts
4144                return false;
4145            }
4146        });
4147
4148        // Update folder icons
4149        mapOverItems(MAP_NO_RECURSE, new ItemOperator() {
4150            @Override
4151            public boolean evaluate(ItemInfo info, View v) {
4152                if (info instanceof FolderInfo && folderIds.contains(info.id)) {
4153                    ((FolderInfo) info).itemsChanged(false);
4154                }
4155                // process all the shortcuts
4156                return false;
4157            }
4158        });
4159    }
4160
4161    public void removeAbandonedPromise(String packageName, UserHandleCompat user) {
4162        HashSet<String> packages = new HashSet<>(1);
4163        packages.add(packageName);
4164        LauncherModel.deletePackageFromDatabase(mLauncher, packageName, user);
4165        removeItemsByMatcher(ItemInfoMatcher.ofPackages(packages, user));
4166    }
4167
4168    public void updateRestoreItems(final HashSet<ItemInfo> updates) {
4169        mapOverItems(MAP_RECURSE, new ItemOperator() {
4170            @Override
4171            public boolean evaluate(ItemInfo info, View v) {
4172                if (info instanceof ShortcutInfo && v instanceof BubbleTextView
4173                        && updates.contains(info)) {
4174                    ((BubbleTextView) v).applyState(false);
4175                } else if (v instanceof PendingAppWidgetHostView
4176                        && info instanceof LauncherAppWidgetInfo
4177                        && updates.contains(info)) {
4178                    ((PendingAppWidgetHostView) v).applyState();
4179                }
4180                // process all the shortcuts
4181                return false;
4182            }
4183        });
4184    }
4185
4186    public void widgetsRestored(final ArrayList<LauncherAppWidgetInfo> changedInfo) {
4187        if (!changedInfo.isEmpty()) {
4188            DeferredWidgetRefresh widgetRefresh = new DeferredWidgetRefresh(changedInfo,
4189                    mLauncher.getAppWidgetHost());
4190
4191            LauncherAppWidgetInfo item = changedInfo.get(0);
4192            final AppWidgetProviderInfo widgetInfo;
4193            if (item.hasRestoreFlag(LauncherAppWidgetInfo.FLAG_ID_NOT_VALID)) {
4194                widgetInfo = AppWidgetManagerCompat
4195                        .getInstance(mLauncher).findProvider(item.providerName, item.user);
4196            } else {
4197                widgetInfo = AppWidgetManagerCompat.getInstance(mLauncher)
4198                        .getAppWidgetInfo(item.appWidgetId);
4199            }
4200
4201            if (widgetInfo != null) {
4202                // Re-inflate the widgets which have changed status
4203                widgetRefresh.run();
4204            } else {
4205                // widgetRefresh will automatically run when the packages are updated.
4206                // For now just update the progress bars
4207                mapOverItems(MAP_NO_RECURSE, new ItemOperator() {
4208                    @Override
4209                    public boolean evaluate(ItemInfo info, View view) {
4210                        if (view instanceof PendingAppWidgetHostView
4211                                && changedInfo.contains(info)) {
4212                            ((LauncherAppWidgetInfo) info).installProgress = 100;
4213                            ((PendingAppWidgetHostView) view).applyState();
4214                        }
4215                        // process all the shortcuts
4216                        return false;
4217                    }
4218                });
4219            }
4220        }
4221    }
4222
4223    private void moveToScreen(int page, boolean animate) {
4224        if (!workspaceInModalState()) {
4225            if (animate) {
4226                snapToPage(page);
4227            } else {
4228                setCurrentPage(page);
4229            }
4230        }
4231        View child = getChildAt(page);
4232        if (child != null) {
4233            child.requestFocus();
4234        }
4235    }
4236
4237    void moveToDefaultScreen(boolean animate) {
4238        moveToScreen(getDefaultPage(), animate);
4239    }
4240
4241    void moveToCustomContentScreen(boolean animate) {
4242        if (hasCustomContent()) {
4243            int ccIndex = getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID);
4244            if (animate) {
4245                snapToPage(ccIndex);
4246            } else {
4247                setCurrentPage(ccIndex);
4248            }
4249            View child = getChildAt(ccIndex);
4250            if (child != null) {
4251                child.requestFocus();
4252            }
4253         }
4254        exitWidgetResizeMode();
4255    }
4256
4257    @Override
4258    protected String getPageIndicatorDescription() {
4259        return getResources().getString(R.string.all_apps_button_label);
4260    }
4261
4262    @Override
4263    protected String getCurrentPageDescription() {
4264        if (hasCustomContent() && getNextPage() == 0) {
4265            return mCustomContentDescription;
4266        }
4267        int page = (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
4268        return getPageDescription(page);
4269    }
4270
4271    private String getPageDescription(int page) {
4272        int delta = numCustomPages();
4273        int nScreens = getChildCount() - delta;
4274        int extraScreenId = mScreenOrder.indexOf(EXTRA_EMPTY_SCREEN_ID);
4275        if (extraScreenId >= 0 && nScreens > 1) {
4276            if (page == extraScreenId) {
4277                return getContext().getString(R.string.workspace_new_page);
4278            }
4279            nScreens--;
4280        }
4281        if (nScreens == 0) {
4282            // When the workspace is not loaded, we do not know how many screen will be bound.
4283            return getContext().getString(R.string.all_apps_home_button_label);
4284        }
4285        return getContext().getString(R.string.workspace_scroll_format,
4286                page + 1 - delta, nScreens);
4287    }
4288
4289    @Override
4290    public void fillInLaunchSourceData(View v, ItemInfo info, Target target, Target targetParent) {
4291        target.gridX = info.cellX;
4292        target.gridY = info.cellY;
4293        target.pageIndex = getCurrentPage();
4294        targetParent.containerType = LauncherLogProto.WORKSPACE;
4295        if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
4296            target.rank = info.rank;
4297            targetParent.containerType = LauncherLogProto.HOTSEAT;
4298        } else if (info.container >= 0) {
4299            targetParent.containerType = LauncherLogProto.FOLDER;
4300        }
4301    }
4302
4303    /**
4304     * Used as a workaround to ensure that the AppWidgetService receives the
4305     * PACKAGE_ADDED broadcast before updating widgets.
4306     */
4307    private class DeferredWidgetRefresh implements Runnable {
4308        private final ArrayList<LauncherAppWidgetInfo> mInfos;
4309        private final LauncherAppWidgetHost mHost;
4310        private final Handler mHandler;
4311
4312        private boolean mRefreshPending;
4313
4314        public DeferredWidgetRefresh(ArrayList<LauncherAppWidgetInfo> infos,
4315                LauncherAppWidgetHost host) {
4316            mInfos = infos;
4317            mHost = host;
4318            mHandler = new Handler();
4319            mRefreshPending = true;
4320
4321            mHost.addProviderChangeListener(this);
4322            // Force refresh after 10 seconds, if we don't get the provider changed event.
4323            // This could happen when the provider is no longer available in the app.
4324            mHandler.postDelayed(this, 10000);
4325        }
4326
4327        @Override
4328        public void run() {
4329            mHost.removeProviderChangeListener(this);
4330            mHandler.removeCallbacks(this);
4331
4332            if (!mRefreshPending) {
4333                return;
4334            }
4335
4336            mRefreshPending = false;
4337
4338            mapOverItems(MAP_NO_RECURSE, new ItemOperator() {
4339                @Override
4340                public boolean evaluate(ItemInfo info, View view) {
4341                    if (view instanceof PendingAppWidgetHostView && mInfos.contains(info)) {
4342                        PendingAppWidgetHostView hostView = (PendingAppWidgetHostView) view;
4343                        mLauncher.removeItem(view, info, false /* deleteFromDb */);
4344                        mLauncher.bindAppWidget((LauncherAppWidgetInfo) info);
4345                    }
4346                    // process all the shortcuts
4347                    return false;
4348                }
4349            });
4350        }
4351    }
4352
4353    public interface OnStateChangeListener {
4354
4355        /**
4356         * Called when the workspace state is changing.
4357         * @param toState final state
4358         * @param targetAnim animation which will be played during the transition or null.
4359         */
4360        void prepareStateChange(State toState, AnimatorSet targetAnim);
4361    }
4362
4363    public static final boolean isQsbContainerPage(int pageNo) {
4364        return pageNo == 0;
4365    }
4366}
4367