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