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