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