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            if (!mLauncher.isWorkspaceLocked()) {
562                snapToScreen(screen);
563            }
564            return true;
565        }
566        return false;
567    }
568
569    @Override
570    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
571        if (!mLauncher.isAllAppsVisible()) {
572            final Folder openFolder = getOpenFolder();
573            if (openFolder != null) {
574                return openFolder.requestFocus(direction, previouslyFocusedRect);
575            } else {
576                int focusableScreen;
577                if (mNextScreen != INVALID_SCREEN) {
578                    focusableScreen = mNextScreen;
579                } else {
580                    focusableScreen = mCurrentScreen;
581                }
582                getChildAt(focusableScreen).requestFocus(direction, previouslyFocusedRect);
583            }
584        }
585        return false;
586    }
587
588    @Override
589    public boolean dispatchUnhandledMove(View focused, int direction) {
590        if (direction == View.FOCUS_LEFT) {
591            if (getCurrentScreen() > 0) {
592                snapToScreen(getCurrentScreen() - 1);
593                return true;
594            }
595        } else if (direction == View.FOCUS_RIGHT) {
596            if (getCurrentScreen() < getChildCount() - 1) {
597                snapToScreen(getCurrentScreen() + 1);
598                return true;
599            }
600        }
601        return super.dispatchUnhandledMove(focused, direction);
602    }
603
604    @Override
605    public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
606        if (!mLauncher.isAllAppsVisible()) {
607            final Folder openFolder = getOpenFolder();
608            if (openFolder == null) {
609                getChildAt(mCurrentScreen).addFocusables(views, direction);
610                if (direction == View.FOCUS_LEFT) {
611                    if (mCurrentScreen > 0) {
612                        getChildAt(mCurrentScreen - 1).addFocusables(views, direction);
613                    }
614                } else if (direction == View.FOCUS_RIGHT){
615                    if (mCurrentScreen < getChildCount() - 1) {
616                        getChildAt(mCurrentScreen + 1).addFocusables(views, direction);
617                    }
618                }
619            } else {
620                openFolder.addFocusables(views, direction);
621            }
622        }
623    }
624
625    @Override
626    public boolean dispatchTouchEvent(MotionEvent ev) {
627        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
628            if (mLauncher.isWorkspaceLocked() || mLauncher.isAllAppsVisible()) {
629                return false;
630            }
631        }
632        return super.dispatchTouchEvent(ev);
633    }
634
635    @Override
636    public boolean onInterceptTouchEvent(MotionEvent ev) {
637        final boolean workspaceLocked = mLauncher.isWorkspaceLocked();
638        final boolean allAppsVisible = mLauncher.isAllAppsVisible();
639        if (workspaceLocked || allAppsVisible) {
640            return false; // We don't want the events.  Let them fall through to the all apps view.
641        }
642
643        /*
644         * This method JUST determines whether we want to intercept the motion.
645         * If we return true, onTouchEvent will be called and we do the actual
646         * scrolling there.
647         */
648
649        /*
650         * Shortcut the most recurring case: the user is in the dragging
651         * state and he is moving his finger.  We want to intercept this
652         * motion.
653         */
654        final int action = ev.getAction();
655        if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
656            return true;
657        }
658
659        if (mVelocityTracker == null) {
660            mVelocityTracker = VelocityTracker.obtain();
661        }
662        mVelocityTracker.addMovement(ev);
663
664        switch (action & MotionEvent.ACTION_MASK) {
665            case MotionEvent.ACTION_MOVE: {
666                /*
667                 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
668                 * whether the user has moved far enough from his original down touch.
669                 */
670
671                /*
672                 * Locally do absolute value. mLastMotionX is set to the y value
673                 * of the down event.
674                 */
675                final int pointerIndex = ev.findPointerIndex(mActivePointerId);
676                final float x = ev.getX(pointerIndex);
677                final float y = ev.getY(pointerIndex);
678                final int xDiff = (int) Math.abs(x - mLastMotionX);
679                final int yDiff = (int) Math.abs(y - mLastMotionY);
680
681                final int touchSlop = mTouchSlop;
682                boolean xMoved = xDiff > touchSlop;
683                boolean yMoved = yDiff > touchSlop;
684
685                if (xMoved || yMoved) {
686
687                    if (xMoved) {
688                        // Scroll if the user moved far enough along the X axis
689                        mTouchState = TOUCH_STATE_SCROLLING;
690                        mLastMotionX = x;
691                        mTouchX = mScrollX;
692                        mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
693                        enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
694                    }
695                    // Either way, cancel any pending longpress
696                    if (mAllowLongPress) {
697                        mAllowLongPress = false;
698                        // Try canceling the long press. It could also have been scheduled
699                        // by a distant descendant, so use the mAllowLongPress flag to block
700                        // everything
701                        final View currentScreen = getChildAt(mCurrentScreen);
702                        currentScreen.cancelLongPress();
703                    }
704                }
705                break;
706            }
707
708            case MotionEvent.ACTION_DOWN: {
709                final float x = ev.getX();
710                final float y = ev.getY();
711                // Remember location of down touch
712                mLastMotionX = x;
713                mLastMotionY = y;
714                mActivePointerId = ev.getPointerId(0);
715                mAllowLongPress = true;
716
717                /*
718                 * If being flinged and user touches the screen, initiate drag;
719                 * otherwise don't.  mScroller.isFinished should be false when
720                 * being flinged.
721                 */
722                mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
723                break;
724            }
725
726            case MotionEvent.ACTION_CANCEL:
727            case MotionEvent.ACTION_UP:
728
729                if (mTouchState != TOUCH_STATE_SCROLLING) {
730                    final CellLayout currentScreen = (CellLayout)getChildAt(mCurrentScreen);
731                    if (!currentScreen.lastDownOnOccupiedCell()) {
732                        getLocationOnScreen(mTempCell);
733                        // Send a tap to the wallpaper if the last down was on empty space
734                        final int pointerIndex = ev.findPointerIndex(mActivePointerId);
735                        mWallpaperManager.sendWallpaperCommand(getWindowToken(),
736                                "android.wallpaper.tap",
737                                mTempCell[0] + (int) ev.getX(pointerIndex),
738                                mTempCell[1] + (int) ev.getY(pointerIndex), 0, null);
739                    }
740                }
741
742                // Release the drag
743                clearChildrenCache();
744                mTouchState = TOUCH_STATE_REST;
745                mActivePointerId = INVALID_POINTER;
746                mAllowLongPress = false;
747
748                if (mVelocityTracker != null) {
749                    mVelocityTracker.recycle();
750                    mVelocityTracker = null;
751                }
752
753                break;
754
755            case MotionEvent.ACTION_POINTER_UP:
756                onSecondaryPointerUp(ev);
757                break;
758        }
759
760        /*
761         * The only time we want to intercept motion events is if we are in the
762         * drag mode.
763         */
764        return mTouchState != TOUCH_STATE_REST;
765    }
766
767    private void onSecondaryPointerUp(MotionEvent ev) {
768        final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
769                MotionEvent.ACTION_POINTER_INDEX_SHIFT;
770        final int pointerId = ev.getPointerId(pointerIndex);
771        if (pointerId == mActivePointerId) {
772            // This was our active pointer going up. Choose a new
773            // active pointer and adjust accordingly.
774            // TODO: Make this decision more intelligent.
775            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
776            mLastMotionX = ev.getX(newPointerIndex);
777            mLastMotionY = ev.getY(newPointerIndex);
778            mActivePointerId = ev.getPointerId(newPointerIndex);
779            if (mVelocityTracker != null) {
780                mVelocityTracker.clear();
781            }
782        }
783    }
784
785    /**
786     * If one of our descendant views decides that it could be focused now, only
787     * pass that along if it's on the current screen.
788     *
789     * This happens when live folders requery, and if they're off screen, they
790     * end up calling requestFocus, which pulls it on screen.
791     */
792    @Override
793    public void focusableViewAvailable(View focused) {
794        View current = getChildAt(mCurrentScreen);
795        View v = focused;
796        while (true) {
797            if (v == current) {
798                super.focusableViewAvailable(focused);
799                return;
800            }
801            if (v == this) {
802                return;
803            }
804            ViewParent parent = v.getParent();
805            if (parent instanceof View) {
806                v = (View)v.getParent();
807            } else {
808                return;
809            }
810        }
811    }
812
813    void enableChildrenCache(int fromScreen, int toScreen) {
814        if (fromScreen > toScreen) {
815            final int temp = fromScreen;
816            fromScreen = toScreen;
817            toScreen = temp;
818        }
819
820        final int count = getChildCount();
821
822        fromScreen = Math.max(fromScreen, 0);
823        toScreen = Math.min(toScreen, count - 1);
824
825        for (int i = fromScreen; i <= toScreen; i++) {
826            final CellLayout layout = (CellLayout) getChildAt(i);
827            layout.setChildrenDrawnWithCacheEnabled(true);
828            layout.setChildrenDrawingCacheEnabled(true);
829        }
830    }
831
832    void clearChildrenCache() {
833        final int count = getChildCount();
834        for (int i = 0; i < count; i++) {
835            final CellLayout layout = (CellLayout) getChildAt(i);
836            layout.setChildrenDrawnWithCacheEnabled(false);
837        }
838    }
839
840    @Override
841    public boolean onTouchEvent(MotionEvent ev) {
842
843        if (mLauncher.isWorkspaceLocked()) {
844            return false; // We don't want the events.  Let them fall through to the all apps view.
845        }
846        if (mLauncher.isAllAppsVisible()) {
847            // Cancel any scrolling that is in progress.
848            if (!mScroller.isFinished()) {
849                mScroller.abortAnimation();
850            }
851            snapToScreen(mCurrentScreen);
852            return false; // We don't want the events.  Let them fall through to the all apps view.
853        }
854
855        if (mVelocityTracker == null) {
856            mVelocityTracker = VelocityTracker.obtain();
857        }
858        mVelocityTracker.addMovement(ev);
859
860        final int action = ev.getAction();
861
862        switch (action & MotionEvent.ACTION_MASK) {
863        case MotionEvent.ACTION_DOWN:
864            /*
865             * If being flinged and user touches, stop the fling. isFinished
866             * will be false if being flinged.
867             */
868            if (!mScroller.isFinished()) {
869                mScroller.abortAnimation();
870            }
871
872            // Remember where the motion event started
873            mLastMotionX = ev.getX();
874            mActivePointerId = ev.getPointerId(0);
875            if (mTouchState == TOUCH_STATE_SCROLLING) {
876                enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1);
877            }
878            break;
879        case MotionEvent.ACTION_MOVE:
880            if (mTouchState == TOUCH_STATE_SCROLLING) {
881                // Scroll to follow the motion event
882                final int pointerIndex = ev.findPointerIndex(mActivePointerId);
883                final float x = ev.getX(pointerIndex);
884                final float deltaX = mLastMotionX - x;
885                mLastMotionX = x;
886
887                if (deltaX < 0) {
888                    if (mTouchX > 0) {
889                        mTouchX += Math.max(-mTouchX, deltaX);
890                        mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
891                        invalidate();
892                    }
893                } else if (deltaX > 0) {
894                    final float availableToScroll = getChildAt(getChildCount() - 1).getRight() -
895                            mTouchX - getWidth();
896                    if (availableToScroll > 0) {
897                        mTouchX += Math.min(availableToScroll, deltaX);
898                        mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
899                        invalidate();
900                    }
901                } else {
902                    awakenScrollBars();
903                }
904            }
905            break;
906        case MotionEvent.ACTION_UP:
907            if (mTouchState == TOUCH_STATE_SCROLLING) {
908                final VelocityTracker velocityTracker = mVelocityTracker;
909                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
910                final int velocityX = (int) velocityTracker.getXVelocity(mActivePointerId);
911
912                final int screenWidth = getWidth();
913                final int whichScreen = (mScrollX + (screenWidth / 2)) / screenWidth;
914                final float scrolledPos = (float) mScrollX / screenWidth;
915
916                if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
917                    // Fling hard enough to move left.
918                    // Don't fling across more than one screen at a time.
919                    final int bound = scrolledPos < whichScreen ?
920                            mCurrentScreen - 1 : mCurrentScreen;
921                    snapToScreen(Math.min(whichScreen, bound), velocityX, true);
922                } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) {
923                    // Fling hard enough to move right
924                    // Don't fling across more than one screen at a time.
925                    final int bound = scrolledPos > whichScreen ?
926                            mCurrentScreen + 1 : mCurrentScreen;
927                    snapToScreen(Math.max(whichScreen, bound), velocityX, true);
928                } else {
929                    snapToScreen(whichScreen, 0, true);
930                }
931
932                if (mVelocityTracker != null) {
933                    mVelocityTracker.recycle();
934                    mVelocityTracker = null;
935                }
936            }
937            mTouchState = TOUCH_STATE_REST;
938            mActivePointerId = INVALID_POINTER;
939            break;
940        case MotionEvent.ACTION_CANCEL:
941            mTouchState = TOUCH_STATE_REST;
942            mActivePointerId = INVALID_POINTER;
943            break;
944        case MotionEvent.ACTION_POINTER_UP:
945            onSecondaryPointerUp(ev);
946            break;
947        }
948
949        return true;
950    }
951
952    void snapToScreen(int whichScreen) {
953        snapToScreen(whichScreen, 0, false);
954    }
955
956    private void snapToScreen(int whichScreen, int velocity, boolean settle) {
957        //if (!mScroller.isFinished()) return;
958
959        whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
960
961        clearVacantCache();
962        enableChildrenCache(mCurrentScreen, whichScreen);
963
964        mNextScreen = whichScreen;
965
966        mPreviousIndicator.setLevel(mNextScreen);
967        mNextIndicator.setLevel(mNextScreen);
968
969        View focusedChild = getFocusedChild();
970        if (focusedChild != null && whichScreen != mCurrentScreen &&
971                focusedChild == getChildAt(mCurrentScreen)) {
972            focusedChild.clearFocus();
973        }
974
975        final int screenDelta = Math.max(1, Math.abs(whichScreen - mCurrentScreen));
976        final int newX = whichScreen * getWidth();
977        final int delta = newX - mScrollX;
978        int duration = (screenDelta + 1) * 100;
979
980        if (!mScroller.isFinished()) {
981            mScroller.abortAnimation();
982        }
983
984        if (settle) {
985            mScrollInterpolator.setDistance(screenDelta);
986        } else {
987            mScrollInterpolator.disableSettle();
988        }
989
990        velocity = Math.abs(velocity);
991        if (velocity > 0) {
992            duration += (duration / (velocity / BASELINE_FLING_VELOCITY))
993                    * FLING_VELOCITY_INFLUENCE;
994        } else {
995            duration += 100;
996        }
997
998        awakenScrollBars(duration);
999        mScroller.startScroll(mScrollX, 0, delta, 0, duration);
1000        invalidate();
1001    }
1002
1003    void startDrag(CellLayout.CellInfo cellInfo) {
1004        View child = cellInfo.cell;
1005
1006        // Make sure the drag was started by a long press as opposed to a long click.
1007        if (!child.isInTouchMode()) {
1008            return;
1009        }
1010
1011        mDragInfo = cellInfo;
1012        mDragInfo.screen = mCurrentScreen;
1013
1014        CellLayout current = ((CellLayout) getChildAt(mCurrentScreen));
1015
1016        current.onDragChild(child);
1017        mDragController.startDrag(child, this, child.getTag(), DragController.DRAG_ACTION_MOVE);
1018        invalidate();
1019    }
1020
1021    @Override
1022    protected Parcelable onSaveInstanceState() {
1023        final SavedState state = new SavedState(super.onSaveInstanceState());
1024        state.currentScreen = mCurrentScreen;
1025        return state;
1026    }
1027
1028    @Override
1029    protected void onRestoreInstanceState(Parcelable state) {
1030        SavedState savedState = (SavedState) state;
1031        super.onRestoreInstanceState(savedState.getSuperState());
1032        if (savedState.currentScreen != -1) {
1033            mCurrentScreen = savedState.currentScreen;
1034            Launcher.setScreen(mCurrentScreen);
1035        }
1036    }
1037
1038    void addApplicationShortcut(ShortcutInfo info, CellLayout.CellInfo cellInfo) {
1039        addApplicationShortcut(info, cellInfo, false);
1040    }
1041
1042    void addApplicationShortcut(ShortcutInfo info, CellLayout.CellInfo cellInfo,
1043            boolean insertAtFirst) {
1044        final CellLayout layout = (CellLayout) getChildAt(cellInfo.screen);
1045        final int[] result = new int[2];
1046
1047        layout.cellToPoint(cellInfo.cellX, cellInfo.cellY, result);
1048        onDropExternal(result[0], result[1], info, layout, insertAtFirst);
1049    }
1050
1051    public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
1052            DragView dragView, Object dragInfo) {
1053        final CellLayout cellLayout = getCurrentDropLayout();
1054        if (source != this) {
1055            onDropExternal(x - xOffset, y - yOffset, dragInfo, cellLayout);
1056        } else {
1057            // Move internally
1058            if (mDragInfo != null) {
1059                final View cell = mDragInfo.cell;
1060                int index = mScroller.isFinished() ? mCurrentScreen : mNextScreen;
1061                if (index != mDragInfo.screen) {
1062                    final CellLayout originalCellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1063                    originalCellLayout.removeView(cell);
1064                    cellLayout.addView(cell);
1065                }
1066                mTargetCell = estimateDropCell(x - xOffset, y - yOffset,
1067                        mDragInfo.spanX, mDragInfo.spanY, cell, cellLayout, mTargetCell);
1068                cellLayout.onDropChild(cell, mTargetCell);
1069
1070                final ItemInfo info = (ItemInfo) cell.getTag();
1071                CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams();
1072                LauncherModel.moveItemInDatabase(mLauncher, info,
1073                        LauncherSettings.Favorites.CONTAINER_DESKTOP, index, lp.cellX, lp.cellY);
1074            }
1075        }
1076    }
1077
1078    public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
1079            DragView dragView, Object dragInfo) {
1080        clearVacantCache();
1081    }
1082
1083    public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
1084            DragView dragView, Object dragInfo) {
1085    }
1086
1087    public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
1088            DragView dragView, Object dragInfo) {
1089        clearVacantCache();
1090    }
1091
1092    private void onDropExternal(int x, int y, Object dragInfo, CellLayout cellLayout) {
1093        onDropExternal(x, y, dragInfo, cellLayout, false);
1094    }
1095
1096    private void onDropExternal(int x, int y, Object dragInfo, CellLayout cellLayout,
1097            boolean insertAtFirst) {
1098        // Drag from somewhere else
1099        ItemInfo info = (ItemInfo) dragInfo;
1100
1101        View view;
1102
1103        switch (info.itemType) {
1104        case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
1105        case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
1106            if (info.container == NO_ID && info instanceof ApplicationInfo) {
1107                // Came from all apps -- make a copy
1108                info = new ShortcutInfo((ApplicationInfo)info);
1109            }
1110            view = mLauncher.createShortcut(R.layout.application, cellLayout, (ShortcutInfo)info);
1111            break;
1112        case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
1113            view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher,
1114                    (ViewGroup) getChildAt(mCurrentScreen), ((UserFolderInfo) info));
1115            break;
1116        default:
1117            throw new IllegalStateException("Unknown item type: " + info.itemType);
1118        }
1119
1120        cellLayout.addView(view, insertAtFirst ? 0 : -1);
1121        view.setHapticFeedbackEnabled(false);
1122        view.setOnLongClickListener(mLongClickListener);
1123        if (view instanceof DropTarget) {
1124            mDragController.addDropTarget((DropTarget) view);
1125        }
1126
1127        mTargetCell = estimateDropCell(x, y, 1, 1, view, cellLayout, mTargetCell);
1128        cellLayout.onDropChild(view, mTargetCell);
1129        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams();
1130
1131        LauncherModel.addOrMoveItemInDatabase(mLauncher, info,
1132                LauncherSettings.Favorites.CONTAINER_DESKTOP, mCurrentScreen, lp.cellX, lp.cellY);
1133    }
1134
1135    /**
1136     * Return the current {@link CellLayout}, correctly picking the destination
1137     * screen while a scroll is in progress.
1138     */
1139    private CellLayout getCurrentDropLayout() {
1140        int index = mScroller.isFinished() ? mCurrentScreen : mNextScreen;
1141        return (CellLayout) getChildAt(index);
1142    }
1143
1144    /**
1145     * {@inheritDoc}
1146     */
1147    public boolean acceptDrop(DragSource source, int x, int y,
1148            int xOffset, int yOffset, DragView dragView, Object dragInfo) {
1149        final CellLayout layout = getCurrentDropLayout();
1150        final CellLayout.CellInfo cellInfo = mDragInfo;
1151        final int spanX = cellInfo == null ? 1 : cellInfo.spanX;
1152        final int spanY = cellInfo == null ? 1 : cellInfo.spanY;
1153
1154        if (mVacantCache == null) {
1155            final View ignoreView = cellInfo == null ? null : cellInfo.cell;
1156            mVacantCache = layout.findAllVacantCells(null, ignoreView);
1157        }
1158
1159        return mVacantCache.findCellForSpan(mTempEstimate, spanX, spanY, false);
1160    }
1161
1162    /**
1163     * {@inheritDoc}
1164     */
1165    public Rect estimateDropLocation(DragSource source, int x, int y,
1166            int xOffset, int yOffset, DragView dragView, Object dragInfo, Rect recycle) {
1167        final CellLayout layout = getCurrentDropLayout();
1168
1169        final CellLayout.CellInfo cellInfo = mDragInfo;
1170        final int spanX = cellInfo == null ? 1 : cellInfo.spanX;
1171        final int spanY = cellInfo == null ? 1 : cellInfo.spanY;
1172        final View ignoreView = cellInfo == null ? null : cellInfo.cell;
1173
1174        final Rect location = recycle != null ? recycle : new Rect();
1175
1176        // Find drop cell and convert into rectangle
1177        int[] dropCell = estimateDropCell(x - xOffset, y - yOffset,
1178                spanX, spanY, ignoreView, layout, mTempCell);
1179
1180        if (dropCell == null) {
1181            return null;
1182        }
1183
1184        layout.cellToPoint(dropCell[0], dropCell[1], mTempEstimate);
1185        location.left = mTempEstimate[0];
1186        location.top = mTempEstimate[1];
1187
1188        layout.cellToPoint(dropCell[0] + spanX, dropCell[1] + spanY, mTempEstimate);
1189        location.right = mTempEstimate[0];
1190        location.bottom = mTempEstimate[1];
1191
1192        return location;
1193    }
1194
1195    /**
1196     * Calculate the nearest cell where the given object would be dropped.
1197     */
1198    private int[] estimateDropCell(int pixelX, int pixelY,
1199            int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) {
1200        // Create vacant cell cache if none exists
1201        if (mVacantCache == null) {
1202            mVacantCache = layout.findAllVacantCells(null, ignoreView);
1203        }
1204
1205        // Find the best target drop location
1206        return layout.findNearestVacantArea(pixelX, pixelY,
1207                spanX, spanY, mVacantCache, recycle);
1208    }
1209
1210    void setLauncher(Launcher launcher) {
1211        mLauncher = launcher;
1212    }
1213
1214    public void setDragController(DragController dragController) {
1215        mDragController = dragController;
1216    }
1217
1218    public void onDropCompleted(View target, boolean success) {
1219        clearVacantCache();
1220
1221        if (success){
1222            if (target != this && mDragInfo != null) {
1223                final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1224                cellLayout.removeView(mDragInfo.cell);
1225                if (mDragInfo.cell instanceof DropTarget) {
1226                    mDragController.removeDropTarget((DropTarget)mDragInfo.cell);
1227                }
1228                //final Object tag = mDragInfo.cell.getTag();
1229            }
1230        } else {
1231            if (mDragInfo != null) {
1232                final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen);
1233                cellLayout.onDropAborted(mDragInfo.cell);
1234            }
1235        }
1236
1237        mDragInfo = null;
1238    }
1239
1240    public void scrollLeft() {
1241        clearVacantCache();
1242        if (mScroller.isFinished()) {
1243            if (mCurrentScreen > 0) snapToScreen(mCurrentScreen - 1);
1244        } else {
1245            if (mNextScreen > 0) snapToScreen(mNextScreen - 1);
1246        }
1247    }
1248
1249    public void scrollRight() {
1250        clearVacantCache();
1251        if (mScroller.isFinished()) {
1252            if (mCurrentScreen < getChildCount() -1) snapToScreen(mCurrentScreen + 1);
1253        } else {
1254            if (mNextScreen < getChildCount() -1) snapToScreen(mNextScreen + 1);
1255        }
1256    }
1257
1258    public int getScreenForView(View v) {
1259        int result = -1;
1260        if (v != null) {
1261            ViewParent vp = v.getParent();
1262            int count = getChildCount();
1263            for (int i = 0; i < count; i++) {
1264                if (vp == getChildAt(i)) {
1265                    return i;
1266                }
1267            }
1268        }
1269        return result;
1270    }
1271
1272    public Folder getFolderForTag(Object tag) {
1273        int screenCount = getChildCount();
1274        for (int screen = 0; screen < screenCount; screen++) {
1275            CellLayout currentScreen = ((CellLayout) getChildAt(screen));
1276            int count = currentScreen.getChildCount();
1277            for (int i = 0; i < count; i++) {
1278                View child = currentScreen.getChildAt(i);
1279                CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
1280                if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) {
1281                    Folder f = (Folder) child;
1282                    if (f.getInfo() == tag) {
1283                        return f;
1284                    }
1285                }
1286            }
1287        }
1288        return null;
1289    }
1290
1291    public View getViewForTag(Object tag) {
1292        int screenCount = getChildCount();
1293        for (int screen = 0; screen < screenCount; screen++) {
1294            CellLayout currentScreen = ((CellLayout) getChildAt(screen));
1295            int count = currentScreen.getChildCount();
1296            for (int i = 0; i < count; i++) {
1297                View child = currentScreen.getChildAt(i);
1298                if (child.getTag() == tag) {
1299                    return child;
1300                }
1301            }
1302        }
1303        return null;
1304    }
1305
1306    /**
1307     * @return True is long presses are still allowed for the current touch
1308     */
1309    public boolean allowLongPress() {
1310        return mAllowLongPress;
1311    }
1312
1313    /**
1314     * Set true to allow long-press events to be triggered, usually checked by
1315     * {@link Launcher} to accept or block dpad-initiated long-presses.
1316     */
1317    public void setAllowLongPress(boolean allowLongPress) {
1318        mAllowLongPress = allowLongPress;
1319    }
1320
1321    void removeItems(final ArrayList<ApplicationInfo> apps) {
1322        final int count = getChildCount();
1323        final PackageManager manager = getContext().getPackageManager();
1324        final AppWidgetManager widgets = AppWidgetManager.getInstance(getContext());
1325
1326        final HashSet<String> packageNames = new HashSet<String>();
1327        final int appCount = apps.size();
1328        for (int i = 0; i < appCount; i++) {
1329            packageNames.add(apps.get(i).componentName.getPackageName());
1330        }
1331
1332        for (int i = 0; i < count; i++) {
1333            final CellLayout layout = (CellLayout) getChildAt(i);
1334
1335            // Avoid ANRs by treating each screen separately
1336            post(new Runnable() {
1337                public void run() {
1338                    final ArrayList<View> childrenToRemove = new ArrayList<View>();
1339                    childrenToRemove.clear();
1340
1341                    int childCount = layout.getChildCount();
1342                    for (int j = 0; j < childCount; j++) {
1343                        final View view = layout.getChildAt(j);
1344                        Object tag = view.getTag();
1345
1346                        if (tag instanceof ShortcutInfo) {
1347                            final ShortcutInfo info = (ShortcutInfo) tag;
1348                            final Intent intent = info.intent;
1349                            final ComponentName name = intent.getComponent();
1350
1351                            if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
1352                                for (String packageName: packageNames) {
1353                                    if (packageName.equals(name.getPackageName())) {
1354                                        // TODO: This should probably be done on a worker thread
1355                                        LauncherModel.deleteItemFromDatabase(mLauncher, info);
1356                                        childrenToRemove.add(view);
1357                                    }
1358                                }
1359                            }
1360                        } else if (tag instanceof UserFolderInfo) {
1361                            final UserFolderInfo info = (UserFolderInfo) tag;
1362                            final ArrayList<ShortcutInfo> contents = info.contents;
1363                            final ArrayList<ShortcutInfo> toRemove = new ArrayList<ShortcutInfo>(1);
1364                            final int contentsCount = contents.size();
1365                            boolean removedFromFolder = false;
1366
1367                            for (int k = 0; k < contentsCount; k++) {
1368                                final ShortcutInfo appInfo = contents.get(k);
1369                                final Intent intent = appInfo.intent;
1370                                final ComponentName name = intent.getComponent();
1371
1372                                if (Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
1373                                    for (String packageName: packageNames) {
1374                                        if (packageName.equals(name.getPackageName())) {
1375                                            toRemove.add(appInfo);
1376                                            // TODO: This should probably be done on a worker thread
1377                                            LauncherModel.deleteItemFromDatabase(
1378                                                    mLauncher, appInfo);
1379                                            removedFromFolder = true;
1380                                        }
1381                                    }
1382                                }
1383                            }
1384
1385                            contents.removeAll(toRemove);
1386                            if (removedFromFolder) {
1387                                final Folder folder = getOpenFolder();
1388                                if (folder != null) folder.notifyDataSetChanged();
1389                            }
1390                        } else if (tag instanceof LiveFolderInfo) {
1391                            final LiveFolderInfo info = (LiveFolderInfo) tag;
1392                            final Uri uri = info.uri;
1393                            final ProviderInfo providerInfo = manager.resolveContentProvider(
1394                                    uri.getAuthority(), 0);
1395
1396                            if (providerInfo != null) {
1397                                for (String packageName: packageNames) {
1398                                    if (packageName.equals(providerInfo.packageName)) {
1399                                        // TODO: This should probably be done on a worker thread
1400                                        LauncherModel.deleteItemFromDatabase(mLauncher, info);
1401                                        childrenToRemove.add(view);
1402                                    }
1403                                }
1404                            }
1405                        } else if (tag instanceof LauncherAppWidgetInfo) {
1406                            final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) tag;
1407                            final AppWidgetProviderInfo provider =
1408                                    widgets.getAppWidgetInfo(info.appWidgetId);
1409                            if (provider != null) {
1410                                for (String packageName: packageNames) {
1411                                    if (packageName.equals(provider.provider.getPackageName())) {
1412                                        // TODO: This should probably be done on a worker thread
1413                                        LauncherModel.deleteItemFromDatabase(mLauncher, info);
1414                                        childrenToRemove.add(view);
1415                                    }
1416                                }
1417                            }
1418                        }
1419                    }
1420
1421                    childCount = childrenToRemove.size();
1422                    for (int j = 0; j < childCount; j++) {
1423                        View child = childrenToRemove.get(j);
1424                        layout.removeViewInLayout(child);
1425                        if (child instanceof DropTarget) {
1426                            mDragController.removeDropTarget((DropTarget)child);
1427                        }
1428                    }
1429
1430                    if (childCount > 0) {
1431                        layout.requestLayout();
1432                        layout.invalidate();
1433                    }
1434                }
1435            });
1436        }
1437    }
1438
1439    void updateShortcuts(ArrayList<ApplicationInfo> apps) {
1440        final PackageManager pm = mLauncher.getPackageManager();
1441
1442        final int count = getChildCount();
1443        for (int i = 0; i < count; i++) {
1444            final CellLayout layout = (CellLayout) getChildAt(i);
1445            int childCount = layout.getChildCount();
1446            for (int j = 0; j < childCount; j++) {
1447                final View view = layout.getChildAt(j);
1448                Object tag = view.getTag();
1449                if (tag instanceof ShortcutInfo) {
1450                    ShortcutInfo info = (ShortcutInfo)tag;
1451                    // We need to check for ACTION_MAIN otherwise getComponent() might
1452                    // return null for some shortcuts (for instance, for shortcuts to
1453                    // web pages.)
1454                    final Intent intent = info.intent;
1455                    final ComponentName name = intent.getComponent();
1456                    if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION &&
1457                            Intent.ACTION_MAIN.equals(intent.getAction()) && name != null) {
1458                        final int appCount = apps.size();
1459                        for (int k=0; k<appCount; k++) {
1460                            ApplicationInfo app = apps.get(k);
1461                            if (app.componentName.equals(name)) {
1462                                info.setIcon(mIconCache.getIcon(info.intent));
1463                                ((TextView)view).setCompoundDrawablesWithIntrinsicBounds(null,
1464                                        new FastBitmapDrawable(info.getIcon(mIconCache)),
1465                                        null, null);
1466                                }
1467                        }
1468                    }
1469                }
1470            }
1471        }
1472    }
1473
1474    void moveToDefaultScreen(boolean animate) {
1475        if (animate) {
1476            snapToScreen(mDefaultScreen);
1477        } else {
1478            setCurrentScreen(mDefaultScreen);
1479        }
1480        getChildAt(mDefaultScreen).requestFocus();
1481    }
1482
1483    void setIndicators(Drawable previous, Drawable next) {
1484        mPreviousIndicator = previous;
1485        mNextIndicator = next;
1486        previous.setLevel(mCurrentScreen);
1487        next.setLevel(mCurrentScreen);
1488    }
1489
1490    public static class SavedState extends BaseSavedState {
1491        int currentScreen = -1;
1492
1493        SavedState(Parcelable superState) {
1494            super(superState);
1495        }
1496
1497        private SavedState(Parcel in) {
1498            super(in);
1499            currentScreen = in.readInt();
1500        }
1501
1502        @Override
1503        public void writeToParcel(Parcel out, int flags) {
1504            super.writeToParcel(out, flags);
1505            out.writeInt(currentScreen);
1506        }
1507
1508        public static final Parcelable.Creator<SavedState> CREATOR =
1509                new Parcelable.Creator<SavedState>() {
1510            public SavedState createFromParcel(Parcel in) {
1511                return new SavedState(in);
1512            }
1513
1514            public SavedState[] newArray(int size) {
1515                return new SavedState[size];
1516            }
1517        };
1518    }
1519}
1520