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