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