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