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