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.launcher2;
18
19import java.util.ArrayList;
20import java.util.HashSet;
21
22import android.app.WallpaperManager;
23import android.appwidget.AppWidgetManager;
24import android.appwidget.AppWidgetProviderInfo;
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.pm.PackageManager;
29import android.content.pm.ProviderInfo;
30import android.content.res.TypedArray;
31import android.graphics.Canvas;
32import android.graphics.Rect;
33import android.graphics.drawable.Drawable;
34import android.net.Uri;
35import android.os.IBinder;
36import android.os.Parcel;
37import android.os.Parcelable;
38import android.util.AttributeSet;
39import android.util.Log;
40import android.view.MotionEvent;
41import android.view.VelocityTracker;
42import android.view.View;
43import android.view.ViewConfiguration;
44import android.view.ViewGroup;
45import android.view.ViewParent;
46import android.view.animation.Interpolator;
47import android.widget.Scroller;
48import android.widget.TextView;
49
50import com.android.launcher.R;
51
52/**
53 * The workspace is a wide area with a wallpaper and a finite number of screens. Each
54 * screen contains a number of icons, folders or widgets the user can interact with.
55 * A workspace is meant to be used with a fixed width only.
56 */
57public class Workspace extends ViewGroup implements DropTarget, DragSource, DragScroller {
58    @SuppressWarnings({"UnusedDeclaration"})
59    private static final String TAG = "Launcher.Workspace";
60    private static final int INVALID_SCREEN = -1;
61
62    /**
63     * The velocity at which a fling gesture will cause us to snap to the next screen
64     */
65    private static final int SNAP_VELOCITY = 600;
66
67    private final WallpaperManager mWallpaperManager;
68
69    private int mDefaultScreen;
70
71    private boolean mFirstLayout = true;
72
73    private int mCurrentScreen;
74    private int mNextScreen = INVALID_SCREEN;
75    private Scroller mScroller;
76    private VelocityTracker mVelocityTracker;
77
78    /**
79     * CellInfo for the cell that is currently being dragged
80     */
81    private CellLayout.CellInfo mDragInfo;
82
83    /**
84     * Target drop area calculated during last acceptDrop call.
85     */
86    private int[] mTargetCell = null;
87
88    private float mLastMotionX;
89    private float mLastMotionY;
90
91    private final static int TOUCH_STATE_REST = 0;
92    private final static int TOUCH_STATE_SCROLLING = 1;
93
94    private int mTouchState = TOUCH_STATE_REST;
95
96    private OnLongClickListener mLongClickListener;
97
98    private Launcher mLauncher;
99    private IconCache mIconCache;
100    private DragController mDragController;
101
102    /**
103     * Cache of vacant cells, used during drag events and invalidated as needed.
104     */
105    private CellLayout.CellInfo mVacantCache = null;
106
107    private int[] mTempCell = new int[2];
108    private int[] mTempEstimate = new int[2];
109
110    private boolean mAllowLongPress = true;
111
112    private int mTouchSlop;
113    private int mMaximumVelocity;
114
115    private static final int INVALID_POINTER = -1;
116
117    private int mActivePointerId = INVALID_POINTER;
118
119    private Drawable mPreviousIndicator;
120    private Drawable mNextIndicator;
121
122    private static final float NANOTIME_DIV = 1000000000.0f;
123    private static final float SMOOTHING_SPEED = 0.75f;
124    private static final float SMOOTHING_CONSTANT = (float) (0.016 / Math.log(SMOOTHING_SPEED));
125    private float mSmoothingTime;
126    private float mTouchX;
127
128    private WorkspaceOvershootInterpolator mScrollInterpolator;
129
130    private static final float BASELINE_FLING_VELOCITY = 2500.f;
131    private static final float FLING_VELOCITY_INFLUENCE = 0.4f;
132
133    private static class WorkspaceOvershootInterpolator implements Interpolator {
134        private static final float DEFAULT_TENSION = 1.3f;
135        private float mTension;
136
137        public WorkspaceOvershootInterpolator() {
138            mTension = DEFAULT_TENSION;
139        }
140
141        public void setDistance(int distance) {
142            mTension = distance > 0 ? DEFAULT_TENSION / distance : DEFAULT_TENSION;
143        }
144
145        public void disableSettle() {
146            mTension = 0.f;
147        }
148
149        public float getInterpolation(float t) {
150            // _o(t) = t * t * ((tension + 1) * t + tension)
151            // o(t) = _o(t - 1) + 1
152            t -= 1.0f;
153            return t * t * ((mTension + 1) * t + mTension) + 1.0f;
154        }
155    }
156
157    /**
158     * Used to inflate the Workspace from XML.
159     *
160     * @param context The application's context.
161     * @param attrs The attribtues set containing the Workspace's customization values.
162     */
163    public Workspace(Context context, AttributeSet attrs) {
164        this(context, attrs, 0);
165    }
166
167    /**
168     * Used to inflate the Workspace from XML.
169     *
170     * @param context The application's context.
171     * @param attrs The attribtues set containing the Workspace's customization values.
172     * @param defStyle Unused.
173     */
174    public Workspace(Context context, AttributeSet attrs, int defStyle) {
175        super(context, attrs, defStyle);
176
177        mWallpaperManager = WallpaperManager.getInstance(context);
178
179        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Workspace, defStyle, 0);
180        mDefaultScreen = a.getInt(R.styleable.Workspace_defaultScreen, 1);
181        a.recycle();
182
183        setHapticFeedbackEnabled(false);
184        initWorkspace();
185    }
186
187    /**
188     * Initializes various states for this workspace.
189     */
190    private void initWorkspace() {
191        Context context = getContext();
192        mScrollInterpolator = new WorkspaceOvershootInterpolator();
193        mScroller = new Scroller(context, mScrollInterpolator);
194        mCurrentScreen = mDefaultScreen;
195        Launcher.setScreen(mCurrentScreen);
196        LauncherApplication app = (LauncherApplication)context.getApplicationContext();
197        mIconCache = app.getIconCache();
198
199        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
200        mTouchSlop = configuration.getScaledTouchSlop();
201        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
202    }
203
204    @Override
205    public void addView(View child, int index, LayoutParams params) {
206        if (!(child instanceof CellLayout)) {
207            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
208        }
209        super.addView(child, index, params);
210    }
211
212    @Override
213    public void addView(View child) {
214        if (!(child instanceof CellLayout)) {
215            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
216        }
217        super.addView(child);
218    }
219
220    @Override
221    public void addView(View child, int index) {
222        if (!(child instanceof CellLayout)) {
223            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
224        }
225        super.addView(child, index);
226    }
227
228    @Override
229    public void addView(View child, int width, int height) {
230        if (!(child instanceof CellLayout)) {
231            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
232        }
233        super.addView(child, width, height);
234    }
235
236    @Override
237    public void addView(View child, LayoutParams params) {
238        if (!(child instanceof CellLayout)) {
239            throw new IllegalArgumentException("A Workspace can only have CellLayout children.");
240        }
241        super.addView(child, params);
242    }
243
244    /**
245     * @return The open folder on the current screen, or null if there is none
246     */
247    Folder getOpenFolder() {
248        CellLayout currentScreen = (CellLayout) getChildAt(mCurrentScreen);
249        int count = currentScreen.getChildCount();
250        for (int i = 0; i < count; i++) {
251            View child = currentScreen.getChildAt(i);
252            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
253            if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
254                return (Folder) child;
255            }
256        }
257        return null;
258    }
259
260    ArrayList<Folder> getOpenFolders() {
261        final int screens = getChildCount();
262        ArrayList<Folder> folders = new ArrayList<Folder>(screens);
263
264        for (int screen = 0; screen < screens; screen++) {
265            CellLayout currentScreen = (CellLayout) getChildAt(screen);
266            int count = currentScreen.getChildCount();
267            for (int i = 0; i < count; i++) {
268                View child = currentScreen.getChildAt(i);
269                CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
270                if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
271                    folders.add((Folder) child);
272                    break;
273                }
274            }
275        }
276
277        return folders;
278    }
279
280    boolean isDefaultScreenShowing() {
281        return mCurrentScreen == mDefaultScreen;
282    }
283
284    /**
285     * Returns the index of the currently displayed screen.
286     *
287     * @return The index of the currently displayed screen.
288     */
289    int getCurrentScreen() {
290        return mCurrentScreen;
291    }
292
293    /**
294     * Sets the current screen.
295     *
296     * @param currentScreen
297     */
298    void setCurrentScreen(int currentScreen) {
299        if (!mScroller.isFinished()) mScroller.abortAnimation();
300        clearVacantCache();
301        mCurrentScreen = Math.max(0, Math.min(currentScreen, getChildCount() - 1));
302        mPreviousIndicator.setLevel(mCurrentScreen);
303        mNextIndicator.setLevel(mCurrentScreen);
304        scrollTo(mCurrentScreen * getWidth(), 0);
305        updateWallpaperOffset();
306        invalidate();
307    }
308
309    /**
310     * Adds the specified child in the current screen. The position and dimension of
311     * the child are defined by x, y, spanX and spanY.
312     *
313     * @param child The child to add in one of the workspace's screens.
314     * @param x The X position of the child in the screen's grid.
315     * @param y The Y position of the child in the screen's grid.
316     * @param spanX The number of cells spanned horizontally by the child.
317     * @param spanY The number of cells spanned vertically by the child.
318     */
319    void addInCurrentScreen(View child, int x, int y, int spanX, int spanY) {
320        addInScreen(child, mCurrentScreen, x, y, spanX, spanY, false);
321    }
322
323    /**
324     * Adds the specified child in the current screen. The position and dimension of
325     * the child are defined by x, y, spanX and spanY.
326     *
327     * @param child The child to add in one of the workspace's screens.
328     * @param x The X position of the child in the screen's grid.
329     * @param y The Y position of the child in the screen's grid.
330     * @param spanX The number of cells spanned horizontally by the child.
331     * @param spanY The number of cells spanned vertically by the child.
332     * @param insert When true, the child is inserted at the beginning of the children list.
333     */
334    void addInCurrentScreen(View child, int x, int y, int spanX, int spanY, boolean insert) {
335        addInScreen(child, mCurrentScreen, x, y, spanX, spanY, insert);
336    }
337
338    /**
339     * Adds the specified child in the specified screen. The position and dimension of
340     * the child are defined by x, y, spanX and spanY.
341     *
342     * @param child The child to add in one of the workspace's screens.
343     * @param screen The screen in which to add the child.
344     * @param x The X position of the child in the screen's grid.
345     * @param y The Y position of the child in the screen's grid.
346     * @param spanX The number of cells spanned horizontally by the child.
347     * @param spanY The number of cells spanned vertically by the child.
348     */
349    void addInScreen(View child, int screen, int x, int y, int spanX, int spanY) {
350        addInScreen(child, screen, x, y, spanX, spanY, false);
351    }
352
353    /**
354     * Adds the specified child in the specified screen. The position and dimension of
355     * the child are defined by x, y, spanX and spanY.
356     *
357     * @param child The child to add in one of the workspace's screens.
358     * @param screen The screen in which to add the child.
359     * @param x The X position of the child in the screen's grid.
360     * @param y The Y position of the child in the screen's grid.
361     * @param spanX The number of cells spanned horizontally by the child.
362     * @param spanY The number of cells spanned vertically by the child.
363     * @param insert When true, the child is inserted at the beginning of the children list.
364     */
365    void addInScreen(View child, int screen, int x, int y, int spanX, int spanY, boolean insert) {
366        if (screen < 0 || screen >= getChildCount()) {
367            Log.e(TAG, "The screen must be >= 0 and < " + getChildCount()
368                + " (was " + screen + "); skipping child");
369            return;
370        }
371
372        clearVacantCache();
373
374        final CellLayout group = (CellLayout) getChildAt(screen);
375        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
376        if (lp == null) {
377            lp = new CellLayout.LayoutParams(x, y, spanX, spanY);
378        } else {
379            lp.cellX = x;
380            lp.cellY = y;
381            lp.cellHSpan = spanX;
382            lp.cellVSpan = spanY;
383        }
384        group.addView(child, insert ? 0 : -1, lp);
385        if (!(child instanceof Folder)) {
386            child.setHapticFeedbackEnabled(false);
387            child.setOnLongClickListener(mLongClickListener);
388        }
389        if (child instanceof DropTarget) {
390            mDragController.addDropTarget((DropTarget)child);
391        }
392    }
393
394    CellLayout.CellInfo findAllVacantCells(boolean[] occupied) {
395        CellLayout group = (CellLayout) getChildAt(mCurrentScreen);
396        if (group != null) {
397            return group.findAllVacantCells(occupied, null);
398        }
399        return null;
400    }
401
402    private void clearVacantCache() {
403        if (mVacantCache != null) {
404            mVacantCache.clearVacantCells();
405            mVacantCache = null;
406        }
407    }
408
409    /**
410     * Registers the specified listener on each screen contained in this workspace.
411     *
412     * @param l The listener used to respond to long clicks.
413     */
414    @Override
415    public void setOnLongClickListener(OnLongClickListener l) {
416        mLongClickListener = l;
417        final int count = getChildCount();
418        for (int i = 0; i < count; i++) {
419            getChildAt(i).setOnLongClickListener(l);
420        }
421    }
422
423    private void updateWallpaperOffset() {
424        updateWallpaperOffset(getChildAt(getChildCount() - 1).getRight() - (mRight - mLeft));
425    }
426
427    private void updateWallpaperOffset(int scrollRange) {
428        IBinder token = getWindowToken();
429        if (token != null) {
430            mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 0 );
431            mWallpaperManager.setWallpaperOffsets(getWindowToken(),
432                    Math.max(0.f, Math.min(mScrollX/(float)scrollRange, 1.f)), 0);
433        }
434    }
435
436    @Override
437    public void scrollTo(int x, int y) {
438        super.scrollTo(x, y);
439        mTouchX = x;
440        mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
441    }
442
443    @Override
444    public void computeScroll() {
445        if (mScroller.computeScrollOffset()) {
446            mTouchX = mScrollX = mScroller.getCurrX();
447            mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
448            mScrollY = mScroller.getCurrY();
449            updateWallpaperOffset();
450            postInvalidate();
451        } else if (mNextScreen != INVALID_SCREEN) {
452            mCurrentScreen = Math.max(0, Math.min(mNextScreen, getChildCount() - 1));
453            mPreviousIndicator.setLevel(mCurrentScreen);
454            mNextIndicator.setLevel(mCurrentScreen);
455            Launcher.setScreen(mCurrentScreen);
456            mNextScreen = INVALID_SCREEN;
457            clearChildrenCache();
458        } else if (mTouchState == TOUCH_STATE_SCROLLING) {
459            final float now = System.nanoTime() / NANOTIME_DIV;
460            final float e = (float) Math.exp((now - mSmoothingTime) / SMOOTHING_CONSTANT);
461            final float dx = mTouchX - mScrollX;
462            mScrollX += dx * e;
463            mSmoothingTime = now;
464
465            // Keep generating points as long as we're more than 1px away from the target
466            if (dx > 1.f || dx < -1.f) {
467                updateWallpaperOffset();
468                postInvalidate();
469            }
470        }
471    }
472
473    @Override
474    protected void dispatchDraw(Canvas canvas) {
475        boolean restore = false;
476        int restoreCount = 0;
477
478        // ViewGroup.dispatchDraw() supports many features we don't need:
479        // clip to padding, layout animation, animation listener, disappearing
480        // children, etc. The following implementation attempts to fast-track
481        // the drawing dispatch by drawing only what we know needs to be drawn.
482
483        boolean fastDraw = mTouchState != TOUCH_STATE_SCROLLING && mNextScreen == INVALID_SCREEN;
484        // If we are not scrolling or flinging, draw only the current screen
485        if (fastDraw) {
486            drawChild(canvas, getChildAt(mCurrentScreen), getDrawingTime());
487        } else {
488            final long drawingTime = getDrawingTime();
489            final float scrollPos = (float) mScrollX / getWidth();
490            final int leftScreen = (int) scrollPos;
491            final int rightScreen = leftScreen + 1;
492            if (leftScreen >= 0) {
493                drawChild(canvas, getChildAt(leftScreen), drawingTime);
494            }
495            if (scrollPos != leftScreen && rightScreen < getChildCount()) {
496                drawChild(canvas, getChildAt(rightScreen), drawingTime);
497            }
498        }
499
500        if (restore) {
501            canvas.restoreToCount(restoreCount);
502        }
503    }
504
505    protected void onAttachedToWindow() {
506        super.onAttachedToWindow();
507        computeScroll();
508        mDragController.setWindowToken(getWindowToken());
509    }
510
511    @Override
512    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
513        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
514
515        final int width = MeasureSpec.getSize(widthMeasureSpec);
516        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
517        if (widthMode != MeasureSpec.EXACTLY) {
518            throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
519        }
520
521        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
522        if (heightMode != MeasureSpec.EXACTLY) {
523            throw new IllegalStateException("Workspace can only be used in EXACTLY mode.");
524        }
525
526        // The children are given the same width and height as the workspace
527        final int count = getChildCount();
528        for (int i = 0; i < count; i++) {
529            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
530        }
531
532
533        if (mFirstLayout) {
534            setHorizontalScrollBarEnabled(false);
535            scrollTo(mCurrentScreen * width, 0);
536            setHorizontalScrollBarEnabled(true);
537            updateWallpaperOffset(width * (getChildCount() - 1));
538            mFirstLayout = false;
539        }
540    }
541
542    @Override
543    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
544        int childLeft = 0;
545
546        final int count = getChildCount();
547        for (int i = 0; i < count; i++) {
548            final View child = getChildAt(i);
549            if (child.getVisibility() != View.GONE) {
550                final int childWidth = child.getMeasuredWidth();
551                child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight());
552                childLeft += childWidth;
553            }
554        }
555    }
556
557    @Override
558    public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
559        int screen = indexOfChild(child);
560        if (screen != mCurrentScreen || !mScroller.isFinished()) {
561            snapToScreen(screen);
562            return true;
563        }
564        return false;
565    }
566
567    @Override
568    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
569        if (!mLauncher.isAllAppsVisible()) {
570            final Folder openFolder = getOpenFolder();
571            if (openFolder != null) {
572                return openFolder.requestFocus(direction, previouslyFocusedRect);
573            } else {
574                int focusableScreen;
575                if (mNextScreen != INVALID_SCREEN) {
576                    focusableScreen = mNextScreen;
577                } else {
578                    focusableScreen = mCurrentScreen;
579                }
580                getChildAt(focusableScreen).requestFocus(direction, previouslyFocusedRect);
581            }
582        }
583        return false;
584    }
585
586    @Override
587    public boolean dispatchUnhandledMove(View focused, int direction) {
588        if (direction == View.FOCUS_LEFT) {
589            if (getCurrentScreen() > 0) {
590                snapToScreen(getCurrentScreen() - 1);
591                return true;
592            }
593        } else if (direction == View.FOCUS_RIGHT) {
594            if (getCurrentScreen() < getChildCount() - 1) {
595                snapToScreen(getCurrentScreen() + 1);
596                return true;
597            }
598        }
599        return super.dispatchUnhandledMove(focused, direction);
600    }
601
602    @Override
603    public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
604        if (!mLauncher.isAllAppsVisible()) {
605            final Folder openFolder = getOpenFolder();
606            if (openFolder == null) {
607                getChildAt(mCurrentScreen).addFocusables(views, direction);
608                if (direction == View.FOCUS_LEFT) {
609                    if (mCurrentScreen > 0) {
610                        getChildAt(mCurrentScreen - 1).addFocusables(views, direction);
611                    }
612                } else if (direction == View.FOCUS_RIGHT){
613                    if (mCurrentScreen < getChildCount() - 1) {
614                        getChildAt(mCurrentScreen + 1).addFocusables(views, direction);
615                    }
616                }
617            } else {
618                openFolder.addFocusables(views, direction);
619            }
620        }
621    }
622
623    @Override
624    public boolean dispatchTouchEvent(MotionEvent ev) {
625        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
626            if (mLauncher.isAllAppsVisible()) {
627                return false;
628            }
629        }
630        return super.dispatchTouchEvent(ev);
631    }
632
633    @Override
634    public boolean onInterceptTouchEvent(MotionEvent ev) {
635        final boolean allAppsVisible = mLauncher.isAllAppsVisible();
636        if (allAppsVisible) {
637            return false; // We don't want the events.  Let them fall through to the all apps view.
638        }
639
640        /*
641         * This method JUST determines whether we want to intercept the motion.
642         * If we return true, onTouchEvent will be called and we do the actual
643         * scrolling there.
644         */
645
646        /*
647         * Shortcut the most recurring case: the user is in the dragging
648         * state and he is moving his finger.  We want to intercept this
649         * motion.
650         */
651        final int action = ev.getAction();
652        if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
653            return true;
654        }
655
656        acquireVelocityTrackerAndAddMovement(ev);
657
658        switch (action & MotionEvent.ACTION_MASK) {
659            case MotionEvent.ACTION_MOVE: {
660                /*
661                 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
662                 * whether the user has moved far enough from his original down touch.
663                 */
664
665                /*
666                 * Locally do absolute value. mLastMotionX is set to the y value
667                 * of the down event.
668                 */
669                final int pointerIndex = ev.findPointerIndex(mActivePointerId);
670                final float x = ev.getX(pointerIndex);
671                final float y = ev.getY(pointerIndex);
672                final int xDiff = (int) Math.abs(x - mLastMotionX);
673                final int yDiff = (int) Math.abs(y - mLastMotionY);
674
675                final int touchSlop = mTouchSlop;
676                boolean xMoved = xDiff > touchSlop;
677                boolean yMoved = yDiff > touchSlop;
678
679                if (xMoved || yMoved) {
680
681                    if (xMoved) {
682                        // Scroll if the user moved far enough along the X axis
683                        mTouchState = TOUCH_STATE_SCROLLING;
684                        mLastMotionX = x;
685                        mTouchX = mScrollX;
686                        mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
687                        enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
688                    }
689                    // Either way, cancel any pending longpress
690                    if (mAllowLongPress) {
691                        mAllowLongPress = false;
692                        // Try canceling the long press. It could also have been scheduled
693                        // by a distant descendant, so use the mAllowLongPress flag to block
694                        // everything
695                        final View currentScreen = getChildAt(mCurrentScreen);
696                        currentScreen.cancelLongPress();
697                    }
698                }
699                break;
700            }
701
702            case MotionEvent.ACTION_DOWN: {
703                final float x = ev.getX();
704                final float y = ev.getY();
705                // Remember location of down touch
706                mLastMotionX = x;
707                mLastMotionY = y;
708                mActivePointerId = ev.getPointerId(0);
709                mAllowLongPress = true;
710
711                /*
712                 * If being flinged and user touches the screen, initiate drag;
713                 * otherwise don't.  mScroller.isFinished should be false when
714                 * being flinged.
715                 */
716                mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
717                break;
718            }
719
720            case MotionEvent.ACTION_CANCEL:
721            case MotionEvent.ACTION_UP:
722
723                if (mTouchState != TOUCH_STATE_SCROLLING) {
724                    final CellLayout currentScreen = (CellLayout)getChildAt(mCurrentScreen);
725                    if (!currentScreen.lastDownOnOccupiedCell()) {
726                        getLocationOnScreen(mTempCell);
727                        // Send a tap to the wallpaper if the last down was on empty space
728                        final int pointerIndex = ev.findPointerIndex(mActivePointerId);
729                        mWallpaperManager.sendWallpaperCommand(getWindowToken(),
730                                "android.wallpaper.tap",
731                                mTempCell[0] + (int) ev.getX(pointerIndex),
732                                mTempCell[1] + (int) ev.getY(pointerIndex), 0, null);
733                    }
734                }
735
736                // Release the drag
737                clearChildrenCache();
738                mTouchState = TOUCH_STATE_REST;
739                mActivePointerId = INVALID_POINTER;
740                mAllowLongPress = false;
741                releaseVelocityTracker();
742                break;
743
744            case MotionEvent.ACTION_POINTER_UP:
745                onSecondaryPointerUp(ev);
746                break;
747        }
748
749        /*
750         * The only time we want to intercept motion events is if we are in the
751         * drag mode.
752         */
753        return mTouchState != TOUCH_STATE_REST;
754    }
755
756    private void onSecondaryPointerUp(MotionEvent ev) {
757        final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
758                MotionEvent.ACTION_POINTER_INDEX_SHIFT;
759        final int pointerId = ev.getPointerId(pointerIndex);
760        if (pointerId == mActivePointerId) {
761            // This was our active pointer going up. Choose a new
762            // active pointer and adjust accordingly.
763            // TODO: Make this decision more intelligent.
764            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
765            mLastMotionX = ev.getX(newPointerIndex);
766            mLastMotionY = ev.getY(newPointerIndex);
767            mActivePointerId = ev.getPointerId(newPointerIndex);
768            if (mVelocityTracker != null) {
769                mVelocityTracker.clear();
770            }
771        }
772    }
773
774    /**
775     * If one of our descendant views decides that it could be focused now, only
776     * pass that along if it's on the current screen.
777     *
778     * This happens when live folders requery, and if they're off screen, they
779     * end up calling requestFocus, which pulls it on screen.
780     */
781    @Override
782    public void focusableViewAvailable(View focused) {
783        View current = getChildAt(mCurrentScreen);
784        View v = focused;
785        while (true) {
786            if (v == current) {
787                super.focusableViewAvailable(focused);
788                return;
789            }
790            if (v == this) {
791                return;
792            }
793            ViewParent parent = v.getParent();
794            if (parent instanceof View) {
795                v = (View)v.getParent();
796            } else {
797                return;
798            }
799        }
800    }
801
802    void enableChildrenCache(int fromScreen, int toScreen) {
803        if (fromScreen > toScreen) {
804            final int temp = fromScreen;
805            fromScreen = toScreen;
806            toScreen = temp;
807        }
808
809        final int count = getChildCount();
810
811        fromScreen = Math.max(fromScreen, 0);
812        toScreen = Math.min(toScreen, count - 1);
813
814        for (int i = fromScreen; i <= toScreen; i++) {
815            final CellLayout layout = (CellLayout) getChildAt(i);
816            layout.setChildrenDrawnWithCacheEnabled(true);
817            layout.setChildrenDrawingCacheEnabled(true);
818        }
819    }
820
821    void clearChildrenCache() {
822        final int count = getChildCount();
823        for (int i = 0; i < count; i++) {
824            final CellLayout layout = (CellLayout) getChildAt(i);
825            layout.setChildrenDrawnWithCacheEnabled(false);
826        }
827    }
828
829    @Override
830    public boolean onTouchEvent(MotionEvent ev) {
831
832        if (mLauncher.isAllAppsVisible()) {
833            // Cancel any scrolling that is in progress.
834            if (!mScroller.isFinished()) {
835                mScroller.abortAnimation();
836            }
837            snapToScreen(mCurrentScreen);
838            return false; // We don't want the events.  Let them fall through to the all apps view.
839        }
840
841        acquireVelocityTrackerAndAddMovement(ev);
842
843        final int action = ev.getAction();
844
845        switch (action & MotionEvent.ACTION_MASK) {
846        case MotionEvent.ACTION_DOWN:
847            /*
848             * If being flinged and user touches, stop the fling. isFinished
849             * will be false if being flinged.
850             */
851            if (!mScroller.isFinished()) {
852                mScroller.abortAnimation();
853            }
854
855            // Remember where the motion event started
856            mLastMotionX = ev.getX();
857            mActivePointerId = ev.getPointerId(0);
858            if (mTouchState == TOUCH_STATE_SCROLLING) {
859                enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
860            }
861            break;
862        case MotionEvent.ACTION_MOVE:
863            if (mTouchState == TOUCH_STATE_SCROLLING) {
864                // Scroll to follow the motion event
865                final int pointerIndex = ev.findPointerIndex(mActivePointerId);
866                final float x = ev.getX(pointerIndex);
867                final float deltaX = mLastMotionX - x;
868                mLastMotionX = x;
869
870                if (deltaX < 0) {
871                    if (mTouchX > 0) {
872                        mTouchX += Math.max(-mTouchX, deltaX);
873                        mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
874                        invalidate();
875                    }
876                } else if (deltaX > 0) {
877                    final float availableToScroll = getChildAt(getChildCount() - 1).getRight() -
878                            mTouchX - getWidth();
879                    if (availableToScroll > 0) {
880                        mTouchX += Math.min(availableToScroll, deltaX);
881                        mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
882                        invalidate();
883                    }
884                } else {
885                    awakenScrollBars();
886                }
887            }
888            break;
889        case MotionEvent.ACTION_UP:
890            if (mTouchState == TOUCH_STATE_SCROLLING) {
891                final VelocityTracker velocityTracker = mVelocityTracker;
892                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
893                final int velocityX = (int) velocityTracker.getXVelocity(mActivePointerId);
894
895                final int screenWidth = getWidth();
896                final int whichScreen = (mScrollX + (screenWidth / 2)) / screenWidth;
897                final float scrolledPos = (float) mScrollX / screenWidth;
898
899                if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
900                    // Fling hard enough to move left.
901                    // Don't fling across more than one screen at a time.
902                    final int bound = scrolledPos < whichScreen ?
903                            mCurrentScreen - 1 : mCurrentScreen;
904                    snapToScreen(Math.min(whichScreen, bound), velocityX, true);
905                } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
906                    // Fling hard enough to move right
907                    // Don't fling across more than one screen at a time.
908                    final int bound = scrolledPos > whichScreen ?
909                            mCurrentScreen + 1 : mCurrentScreen;
910                    snapToScreen(Math.max(whichScreen, bound), velocityX, true);
911                } else {
912                    snapToScreen(whichScreen, 0, true);
913                }
914            }
915            mTouchState = TOUCH_STATE_REST;
916            mActivePointerId = INVALID_POINTER;
917            releaseVelocityTracker();
918            break;
919        case MotionEvent.ACTION_CANCEL:
920            if (mTouchState == TOUCH_STATE_SCROLLING) {
921                final int screenWidth = getWidth();
922                final int whichScreen = (mScrollX + (screenWidth / 2)) / screenWidth;
923                snapToScreen(whichScreen, 0, true);
924            }
925            mTouchState = TOUCH_STATE_REST;
926            mActivePointerId = INVALID_POINTER;
927            releaseVelocityTracker();
928            break;
929        case MotionEvent.ACTION_POINTER_UP:
930            onSecondaryPointerUp(ev);
931            break;
932        }
933
934        return true;
935    }
936
937    private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
938        if (mVelocityTracker == null) {
939            mVelocityTracker = VelocityTracker.obtain();
940        }
941        mVelocityTracker.addMovement(ev);
942    }
943
944    private void releaseVelocityTracker() {
945        if (mVelocityTracker != null) {
946            mVelocityTracker.recycle();
947            mVelocityTracker = null;
948        }
949    }
950
951    void snapToScreen(int whichScreen) {
952        snapToScreen(whichScreen, 0, false);
953    }
954
955    private void snapToScreen(int whichScreen, int velocity, boolean settle) {
956        //if (!mScroller.isFinished()) return;
957
958        whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
959
960        clearVacantCache();
961        enableChildrenCache(mCurrentScreen, whichScreen);
962
963        mNextScreen = whichScreen;
964
965        mPreviousIndicator.setLevel(mNextScreen);
966        mNextIndicator.setLevel(mNextScreen);
967
968        View focusedChild = getFocusedChild();
969        if (focusedChild != null && whichScreen != mCurrentScreen &&
970                focusedChild == getChildAt(mCurrentScreen)) {
971            focusedChild.clearFocus();
972        }
973
974        final int screenDelta = Math.max(1, Math.abs(whichScreen - mCurrentScreen));
975        final int newX = whichScreen * getWidth();
976        final int delta = newX - mScrollX;
977        int duration = (screenDelta + 1) * 100;
978
979        if (!mScroller.isFinished()) {
980            mScroller.abortAnimation();
981        }
982
983        if (settle) {
984            mScrollInterpolator.setDistance(screenDelta);
985        } else {
986            mScrollInterpolator.disableSettle();
987        }
988
989        velocity = Math.abs(velocity);
990        if (velocity > 0) {
991            duration += (duration / (velocity / BASELINE_FLING_VELOCITY))
992                    * FLING_VELOCITY_INFLUENCE;
993        } else {
994            duration += 100;
995        }
996
997        awakenScrollBars(duration);
998        mScroller.startScroll(mScrollX, 0, delta, 0, duration);
999        invalidate();
1000    }
1001
1002    void startDrag(CellLayout.CellInfo cellInfo) {
1003        View child = cellInfo.cell;
1004
1005        // Make sure the drag was started by a long press as opposed to a long click.
1006        if (!child.isInTouchMode()) {
1007            return;
1008        }
1009
1010        mDragInfo = cellInfo;
1011        mDragInfo.screen = mCurrentScreen;
1012
1013        CellLayout current = ((CellLayout) getChildAt(mCurrentScreen));
1014
1015        current.onDragChild(child);
1016        mDragController.startDrag(child, this, child.getTag(), DragController.DRAG_ACTION_MOVE);
1017        invalidate();
1018    }
1019
1020    @Override
1021    protected Parcelable onSaveInstanceState() {
1022        final SavedState state = new SavedState(super.onSaveInstanceState());
1023        state.currentScreen = mCurrentScreen;
1024        return state;
1025    }
1026
1027    @Override
1028    protected void onRestoreInstanceState(Parcelable state) {
1029        SavedState savedState = (SavedState) state;
1030        super.onRestoreInstanceState(savedState.getSuperState());
1031        if (savedState.currentScreen != -1) {
1032            mCurrentScreen = savedState.currentScreen;
1033            Launcher.setScreen(mCurrentScreen);
1034        }
1035    }
1036
1037    void addApplicationShortcut(ShortcutInfo info, CellLayout.CellInfo cellInfo) {
1038        addApplicationShortcut(info, cellInfo, false);
1039    }
1040
1041    void addApplicationShortcut(ShortcutInfo info, CellLayout.CellInfo cellInfo,
1042            boolean insertAtFirst) {
1043        final CellLayout layout = (CellLayout) getChildAt(cellInfo.screen);
1044        final int[] result = new int[2];
1045
1046        layout.cellToPoint(cellInfo.cellX, cellInfo.cellY, result);
1047        onDropExternal(result[0], result[1], info, layout, insertAtFirst);
1048    }
1049
1050    public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
1051            DragView dragView, Object dragInfo) {
1052        final CellLayout cellLayout = getCurrentDropLayout();
1053        if (source != this) {
1054            onDropExternal(x - xOffset, y - yOffset, dragInfo, cellLayout);
1055        } else {
1056            // Move internally
1057            if (mDragInfo != null) {
1058                final View cell = mDragInfo.cell;
1059                int index = mScroller.isFinished() ? mCurrentScreen : mNextScreen;
1060                if (index != mDragInfo.screen) {
1061                    final CellLayout originalCellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1062                    originalCellLayout.removeView(cell);
1063                    cellLayout.addView(cell);
1064                }
1065                mTargetCell = estimateDropCell(x - xOffset, y - yOffset,
1066                        mDragInfo.spanX, mDragInfo.spanY, cell, cellLayout, mTargetCell);
1067                cellLayout.onDropChild(cell, mTargetCell);
1068
1069                final ItemInfo info = (ItemInfo) cell.getTag();
1070                CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
1071                LauncherModel.moveItemInDatabase(mLauncher, info,
1072                        LauncherSettings.Favorites.CONTAINER_DESKTOP, index, lp.cellX, lp.cellY);
1073            }
1074        }
1075    }
1076
1077    public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
1078            DragView dragView, Object dragInfo) {
1079        clearVacantCache();
1080    }
1081
1082    public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
1083            DragView dragView, Object dragInfo) {
1084    }
1085
1086    public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
1087            DragView dragView, Object dragInfo) {
1088        clearVacantCache();
1089    }
1090
1091    private void onDropExternal(int x, int y, Object dragInfo, CellLayout cellLayout) {
1092        onDropExternal(x, y, dragInfo, cellLayout, false);
1093    }
1094
1095    private void onDropExternal(int x, int y, Object dragInfo, CellLayout cellLayout,
1096            boolean insertAtFirst) {
1097        // Drag from somewhere else
1098        ItemInfo info = (ItemInfo) dragInfo;
1099
1100        View view;
1101
1102        switch (info.itemType) {
1103        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1104        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1105            if (info.container == NO_ID && info instanceof ApplicationInfo) {
1106                // Came from all apps -- make a copy
1107                info = new ShortcutInfo((ApplicationInfo)info);
1108            }
1109            view = mLauncher.createShortcut(R.layout.application, cellLayout, (ShortcutInfo)info);
1110            break;
1111        case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1112            view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher,
1113                    (ViewGroup) getChildAt(mCurrentScreen), ((UserFolderInfo) info));
1114            break;
1115        default:
1116            throw new IllegalStateException("Unknown item type: " + info.itemType);
1117        }
1118
1119        cellLayout.addView(view, insertAtFirst ? 0 : -1);
1120        view.setHapticFeedbackEnabled(false);
1121        view.setOnLongClickListener(mLongClickListener);
1122        if (view instanceof DropTarget) {
1123            mDragController.addDropTarget((DropTarget) view);
1124        }
1125
1126        mTargetCell = estimateDropCell(x, y, 1, 1, view, cellLayout, mTargetCell);
1127        cellLayout.onDropChild(view, mTargetCell);
1128        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
1129
1130        LauncherModel.addOrMoveItemInDatabase(mLauncher, info,
1131                LauncherSettings.Favorites.CONTAINER_DESKTOP, mCurrentScreen, lp.cellX, lp.cellY);
1132    }
1133
1134    /**
1135     * Return the current {@link CellLayout}, correctly picking the destination
1136     * screen while a scroll is in progress.
1137     */
1138    private CellLayout getCurrentDropLayout() {
1139        int index = mScroller.isFinished() ? mCurrentScreen : mNextScreen;
1140        return (CellLayout) getChildAt(index);
1141    }
1142
1143    /**
1144     * {@inheritDoc}
1145     */
1146    public boolean acceptDrop(DragSource source, int x, int y,
1147            int xOffset, int yOffset, DragView dragView, Object dragInfo) {
1148        final CellLayout layout = getCurrentDropLayout();
1149        final CellLayout.CellInfo cellInfo = mDragInfo;
1150        final int spanX = cellInfo == null ? 1 : cellInfo.spanX;
1151        final int spanY = cellInfo == null ? 1 : cellInfo.spanY;
1152
1153        if (mVacantCache == null) {
1154            final View ignoreView = cellInfo == null ? null : cellInfo.cell;
1155            mVacantCache = layout.findAllVacantCells(null, ignoreView);
1156        }
1157
1158        return mVacantCache.findCellForSpan(mTempEstimate, spanX, spanY, false);
1159    }
1160
1161    /**
1162     * {@inheritDoc}
1163     */
1164    public Rect estimateDropLocation(DragSource source, int x, int y,
1165            int xOffset, int yOffset, DragView dragView, Object dragInfo, Rect recycle) {
1166        final CellLayout layout = getCurrentDropLayout();
1167
1168        final CellLayout.CellInfo cellInfo = mDragInfo;
1169        final int spanX = cellInfo == null ? 1 : cellInfo.spanX;
1170        final int spanY = cellInfo == null ? 1 : cellInfo.spanY;
1171        final View ignoreView = cellInfo == null ? null : cellInfo.cell;
1172
1173        final Rect location = recycle != null ? recycle : new Rect();
1174
1175        // Find drop cell and convert into rectangle
1176        int[] dropCell = estimateDropCell(x - xOffset, y - yOffset,
1177                spanX, spanY, ignoreView, layout, mTempCell);
1178
1179        if (dropCell == null) {
1180            return null;
1181        }
1182
1183        layout.cellToPoint(dropCell[0], dropCell[1], mTempEstimate);
1184        location.left = mTempEstimate[0];
1185        location.top = mTempEstimate[1];
1186
1187        layout.cellToPoint(dropCell[0] + spanX, dropCell[1] + spanY, mTempEstimate);
1188        location.right = mTempEstimate[0];
1189        location.bottom = mTempEstimate[1];
1190
1191        return location;
1192    }
1193
1194    /**
1195     * Calculate the nearest cell where the given object would be dropped.
1196     */
1197    private int[] estimateDropCell(int pixelX, int pixelY,
1198            int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) {
1199        // Create vacant cell cache if none exists
1200        if (mVacantCache == null) {
1201            mVacantCache = layout.findAllVacantCells(null, ignoreView);
1202        }
1203
1204        // Find the best target drop location
1205        return layout.findNearestVacantArea(pixelX, pixelY,
1206                spanX, spanY, mVacantCache, recycle);
1207    }
1208
1209    void setLauncher(Launcher launcher) {
1210        mLauncher = launcher;
1211    }
1212
1213    public void setDragController(DragController dragController) {
1214        mDragController = dragController;
1215    }
1216
1217    public void onDropCompleted(View target, boolean success) {
1218        clearVacantCache();
1219
1220        if (success){
1221            if (target != this && mDragInfo != null) {
1222                final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1223                cellLayout.removeView(mDragInfo.cell);
1224                if (mDragInfo.cell instanceof DropTarget) {
1225                    mDragController.removeDropTarget((DropTarget)mDragInfo.cell);
1226                }
1227                //final Object tag = mDragInfo.cell.getTag();
1228            }
1229        } else {
1230            if (mDragInfo != null) {
1231                final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1232                cellLayout.onDropAborted(mDragInfo.cell);
1233            }
1234        }
1235
1236        mDragInfo = null;
1237    }
1238
1239    public void scrollLeft() {
1240        clearVacantCache();
1241        if (mScroller.isFinished()) {
1242            if (mCurrentScreen > 0) snapToScreen(mCurrentScreen - 1);
1243        } else {
1244            if (mNextScreen > 0) snapToScreen(mNextScreen - 1);
1245        }
1246    }
1247
1248    public void scrollRight() {
1249        clearVacantCache();
1250        if (mScroller.isFinished()) {
1251            if (mCurrentScreen < getChildCount() -1) snapToScreen(mCurrentScreen + 1);
1252        } else {
1253            if (mNextScreen < getChildCount() -1) snapToScreen(mNextScreen + 1);
1254        }
1255    }
1256
1257    public int getScreenForView(View v) {
1258        int result = -1;
1259        if (v != null) {
1260            ViewParent vp = v.getParent();
1261            int count = getChildCount();
1262            for (int i = 0; i < count; i++) {
1263                if (vp == getChildAt(i)) {
1264                    return i;
1265                }
1266            }
1267        }
1268        return result;
1269    }
1270
1271    public Folder getFolderForTag(Object tag) {
1272        int screenCount = getChildCount();
1273        for (int screen = 0; screen < screenCount; screen++) {
1274            CellLayout currentScreen = ((CellLayout) getChildAt(screen));
1275            int count = currentScreen.getChildCount();
1276            for (int i = 0; i < count; i++) {
1277                View child = currentScreen.getChildAt(i);
1278                CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
1279                if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
1280                    Folder f = (Folder) child;
1281                    if (f.getInfo() == tag) {
1282                        return f;
1283                    }
1284                }
1285            }
1286        }
1287        return null;
1288    }
1289
1290    public View getViewForTag(Object tag) {
1291        int screenCount = getChildCount();
1292        for (int screen = 0; screen < screenCount; screen++) {
1293            CellLayout currentScreen = ((CellLayout) getChildAt(screen));
1294            int count = currentScreen.getChildCount();
1295            for (int i = 0; i < count; i++) {
1296                View child = currentScreen.getChildAt(i);
1297                if (child.getTag() == tag) {
1298                    return child;
1299                }
1300            }
1301        }
1302        return null;
1303    }
1304
1305    /**
1306     * @return True is long presses are still allowed for the current touch
1307     */
1308    public boolean allowLongPress() {
1309        return mAllowLongPress;
1310    }
1311
1312    /**
1313     * Set true to allow long-press events to be triggered, usually checked by
1314     * {@link Launcher} to accept or block dpad-initiated long-presses.
1315     */
1316    public void setAllowLongPress(boolean allowLongPress) {
1317        mAllowLongPress = allowLongPress;
1318    }
1319
1320    void removeItems(final ArrayList<ApplicationInfo> apps) {
1321        final int count = getChildCount();
1322        final PackageManager manager = getContext().getPackageManager();
1323        final AppWidgetManager widgets = AppWidgetManager.getInstance(getContext());
1324
1325        final HashSet<String> packageNames = new HashSet<String>();
1326        final int appCount = apps.size();
1327        for (int i = 0; i < appCount; i++) {
1328            packageNames.add(apps.get(i).componentName.getPackageName());
1329        }
1330
1331        for (int i = 0; i < count; i++) {
1332            final CellLayout layout = (CellLayout) getChildAt(i);
1333
1334            // Avoid ANRs by treating each screen separately
1335            post(new Runnable() {
1336                public void run() {
1337                    final ArrayList<View> childrenToRemove = new ArrayList<View>();
1338                    childrenToRemove.clear();
1339
1340                    int childCount = layout.getChildCount();
1341                    for (int j = 0; j < childCount; j++) {
1342                        final View view = layout.getChildAt(j);
1343                        Object tag = view.getTag();
1344
1345                        if (tag instanceof ShortcutInfo) {
1346                            final ShortcutInfo info = (ShortcutInfo) tag;
1347                            final Intent intent = info.intent;
1348                            final ComponentName name = intent.getComponent();
1349
1350                            if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
1351                                for (String packageName: packageNames) {
1352                                    if (packageName.equals(name.getPackageName())) {
1353                                        LauncherModel.deleteItemFromDatabase(mLauncher, info);
1354                                        childrenToRemove.add(view);
1355                                    }
1356                                }
1357                            }
1358                        } else if (tag instanceof UserFolderInfo) {
1359                            final UserFolderInfo info = (UserFolderInfo) tag;
1360                            final ArrayList<ShortcutInfo> contents = info.contents;
1361                            final ArrayList<ShortcutInfo> toRemove = new ArrayList<ShortcutInfo>(1);
1362                            final int contentsCount = contents.size();
1363                            boolean removedFromFolder = false;
1364
1365                            for (int k = 0; k < contentsCount; k++) {
1366                                final ShortcutInfo appInfo = contents.get(k);
1367                                final Intent intent = appInfo.intent;
1368                                final ComponentName name = intent.getComponent();
1369
1370                                if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
1371                                    for (String packageName: packageNames) {
1372                                        if (packageName.equals(name.getPackageName())) {
1373                                            toRemove.add(appInfo);
1374                                            LauncherModel.deleteItemFromDatabase(mLauncher, appInfo);
1375                                            removedFromFolder = true;
1376                                        }
1377                                    }
1378                                }
1379                            }
1380
1381                            contents.removeAll(toRemove);
1382                            if (removedFromFolder) {
1383                                final Folder folder = getOpenFolder();
1384                                if (folder != null) folder.notifyDataSetChanged();
1385                            }
1386                        } else if (tag instanceof LiveFolderInfo) {
1387                            final LiveFolderInfo info = (LiveFolderInfo) tag;
1388                            final Uri uri = info.uri;
1389                            final ProviderInfo providerInfo = manager.resolveContentProvider(
1390                                    uri.getAuthority(), 0);
1391
1392                            if (providerInfo != null) {
1393                                for (String packageName: packageNames) {
1394                                    if (packageName.equals(providerInfo.packageName)) {
1395                                        LauncherModel.deleteItemFromDatabase(mLauncher, info);
1396                                        childrenToRemove.add(view);
1397                                    }
1398                                }
1399                            }
1400                        } else if (tag instanceof LauncherAppWidgetInfo) {
1401                            final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag;
1402                            final AppWidgetProviderInfo provider =
1403                                    widgets.getAppWidgetInfo(info.appWidgetId);
1404                            if (provider != null) {
1405                                for (String packageName: packageNames) {
1406                                    if (packageName.equals(provider.provider.getPackageName())) {
1407                                        LauncherModel.deleteItemFromDatabase(mLauncher, info);
1408                                        childrenToRemove.add(view);
1409                                    }
1410                                }
1411                            }
1412                        }
1413                    }
1414
1415                    childCount = childrenToRemove.size();
1416                    for (int j = 0; j < childCount; j++) {
1417                        View child = childrenToRemove.get(j);
1418                        layout.removeViewInLayout(child);
1419                        if (child instanceof DropTarget) {
1420                            mDragController.removeDropTarget((DropTarget)child);
1421                        }
1422                    }
1423
1424                    if (childCount > 0) {
1425                        layout.requestLayout();
1426                        layout.invalidate();
1427                    }
1428                }
1429            });
1430        }
1431    }
1432
1433    void updateShortcuts(ArrayList<ApplicationInfo> apps) {
1434        final PackageManager pm = mLauncher.getPackageManager();
1435
1436        final int count = getChildCount();
1437        for (int i = 0; i < count; i++) {
1438            final CellLayout layout = (CellLayout) getChildAt(i);
1439            int childCount = layout.getChildCount();
1440            for (int j = 0; j < childCount; j++) {
1441                final View view = layout.getChildAt(j);
1442                Object tag = view.getTag();
1443                if (tag instanceof ShortcutInfo) {
1444                    ShortcutInfo info = (ShortcutInfo)tag;
1445                    // We need to check for ACTION_MAIN otherwise getComponent() might
1446                    // return null for some shortcuts (for instance, for shortcuts to
1447                    // web pages.)
1448                    final Intent intent = info.intent;
1449                    final ComponentName name = intent.getComponent();
1450                    if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
1451                            Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
1452                        final int appCount = apps.size();
1453                        for (int k=0; k<appCount; k++) {
1454                            ApplicationInfo app = apps.get(k);
1455                            if (app.componentName.equals(name)) {
1456                                info.setIcon(mIconCache.getIcon(info.intent));
1457                                ((TextView)view).setCompoundDrawablesWithIntrinsicBounds(null,
1458                                        new FastBitmapDrawable(info.getIcon(mIconCache)),
1459                                        null, null);
1460                                }
1461                        }
1462                    }
1463                }
1464            }
1465        }
1466    }
1467
1468    void moveToDefaultScreen(boolean animate) {
1469        if (animate) {
1470            snapToScreen(mDefaultScreen);
1471        } else {
1472            setCurrentScreen(mDefaultScreen);
1473        }
1474        getChildAt(mDefaultScreen).requestFocus();
1475    }
1476
1477    void setIndicators(Drawable previous, Drawable next) {
1478        mPreviousIndicator = previous;
1479        mNextIndicator = next;
1480        previous.setLevel(mCurrentScreen);
1481        next.setLevel(mCurrentScreen);
1482    }
1483
1484    public static class SavedState extends BaseSavedState {
1485        int currentScreen = -1;
1486
1487        SavedState(Parcelable superState) {
1488            super(superState);
1489        }
1490
1491        private SavedState(Parcel in) {
1492            super(in);
1493            currentScreen = in.readInt();
1494        }
1495
1496        @Override
1497        public void writeToParcel(Parcel out, int flags) {
1498            super.writeToParcel(out, flags);
1499            out.writeInt(currentScreen);
1500        }
1501
1502        public static final Parcelable.Creator<SavedState> CREATOR =
1503                new Parcelable.Creator<SavedState>() {
1504            public SavedState createFromParcel(Parcel in) {
1505                return new SavedState(in);
1506            }
1507
1508            public SavedState[] newArray(int size) {
1509                return new SavedState[size];
1510            }
1511        };
1512    }
1513}
1514