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