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