Home | History | Annotate | Download | only in launcher2
      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 
     17 package com.android.launcher2;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.content.res.Resources;
     22 import android.graphics.Bitmap;
     23 import android.graphics.Canvas;
     24 import android.graphics.PixelFormat;
     25 import android.graphics.Rect;
     26 import android.renderscript.Allocation;
     27 import android.renderscript.Element;
     28 import android.renderscript.ProgramFragment;
     29 import android.renderscript.ProgramStore;
     30 import android.renderscript.ProgramVertex;
     31 import android.renderscript.RSSurfaceView;
     32 import android.renderscript.RenderScriptGL;
     33 import android.renderscript.RenderScript;
     34 import android.renderscript.Sampler;
     35 import android.renderscript.Script;
     36 import android.renderscript.ScriptC;
     37 import android.renderscript.SimpleMesh;
     38 import android.renderscript.Type;
     39 import android.util.AttributeSet;
     40 import android.util.DisplayMetrics;
     41 import android.util.Log;
     42 import android.view.KeyEvent;
     43 import android.view.MotionEvent;
     44 import android.view.SoundEffectConstants;
     45 import android.view.SurfaceHolder;
     46 import android.view.VelocityTracker;
     47 import android.view.View;
     48 import android.view.ViewConfiguration;
     49 import android.view.accessibility.AccessibilityEvent;
     50 
     51 import java.util.ArrayList;
     52 import java.util.Arrays;
     53 import java.util.Collections;
     54 
     55 import com.android.launcher.R;
     56 
     57 public class AllApps3D extends RSSurfaceView
     58         implements AllAppsView, View.OnClickListener, View.OnLongClickListener, DragSource {
     59     private static final String TAG = "Launcher.AllApps3D";
     60 
     61     /** Bit for mLocks for when there are icons being loaded. */
     62     private static final int LOCK_ICONS_PENDING = 1;
     63 
     64     private static final int TRACKING_NONE = 0;
     65     private static final int TRACKING_FLING = 1;
     66     private static final int TRACKING_HOME = 2;
     67 
     68     private static final int SELECTED_NONE = 0;
     69     private static final int SELECTED_FOCUSED = 1;
     70     private static final int SELECTED_PRESSED = 2;
     71 
     72     private static final int SELECTION_NONE = 0;
     73     private static final int SELECTION_ICONS = 1;
     74     private static final int SELECTION_HOME = 2;
     75 
     76     private Launcher mLauncher;
     77     private DragController mDragController;
     78 
     79     /** When this is 0, modifications are allowed, when it's not, they're not.
     80      * TODO: What about scrolling? */
     81     private int mLocks = LOCK_ICONS_PENDING;
     82 
     83     private int mSlop;
     84     private int mMaxFlingVelocity;
     85 
     86     private Defines mDefines = new Defines();
     87     private ArrayList<ApplicationInfo> mAllAppsList;
     88 
     89     private static RenderScriptGL sRS;
     90     private static RolloRS sRollo;
     91 
     92     private static boolean sZoomDirty = false;
     93     private static boolean sAnimateNextZoom;
     94     private static float sNextZoom;
     95 
     96     /**
     97      * True when we are using arrow keys or trackball to drive navigation
     98      */
     99     private boolean mArrowNavigation = false;
    100     private boolean mStartedScrolling;
    101 
    102     /**
    103      * Used to keep track of the selection when AllAppsView loses window focus.
    104      * One of the SELECTION_ constants.
    105      */
    106     private int mLastSelection;
    107 
    108     /**
    109      * Used to keep track of the selection when AllAppsView loses window focus
    110      */
    111     private int mLastSelectedIcon;
    112 
    113     private VelocityTracker mVelocityTracker;
    114     private int mTouchTracking;
    115     private int mMotionDownRawX;
    116     private int mMotionDownRawY;
    117     private int mDownIconIndex = -1;
    118     private int mCurrentIconIndex = -1;
    119     private int[] mTouchYBorders;
    120     private int[] mTouchXBorders;
    121 
    122     private boolean mShouldGainFocus;
    123 
    124     private boolean mHaveSurface = false;
    125     private float mZoom;
    126     private float mVelocity;
    127     private AAMessage mMessageProc;
    128 
    129     private int mColumnsPerPage;
    130     private int mRowsPerPage;
    131     private boolean mSurrendered;
    132 
    133     private int mRestoreFocusIndex = -1;
    134 
    135     @SuppressWarnings({"UnusedDeclaration"})
    136     static class Defines {
    137         public static final int ALLOC_PARAMS = 0;
    138         public static final int ALLOC_STATE = 1;
    139         public static final int ALLOC_ICON_IDS = 3;
    140         public static final int ALLOC_LABEL_IDS = 4;
    141         public static final int ALLOC_VP_CONSTANTS = 5;
    142 
    143         public static final int COLUMNS_PER_PAGE_PORTRAIT = 4;
    144         public static final int ROWS_PER_PAGE_PORTRAIT = 4;
    145 
    146         public static final int COLUMNS_PER_PAGE_LANDSCAPE = 6;
    147         public static final int ROWS_PER_PAGE_LANDSCAPE = 3;
    148 
    149         public static final int ICON_WIDTH_PX = 64;
    150         public static final int ICON_TEXTURE_WIDTH_PX = 74;
    151         public static final int SELECTION_TEXTURE_WIDTH_PX = 74 + 20;
    152 
    153         public static final int ICON_HEIGHT_PX = 64;
    154         public static final int ICON_TEXTURE_HEIGHT_PX = 74;
    155         public static final int SELECTION_TEXTURE_HEIGHT_PX = 74 + 20;
    156     }
    157 
    158     public AllApps3D(Context context, AttributeSet attrs) {
    159         super(context, attrs);
    160         setFocusable(true);
    161         setSoundEffectsEnabled(false);
    162         getHolder().setFormat(PixelFormat.TRANSLUCENT);
    163         final ViewConfiguration config = ViewConfiguration.get(context);
    164         mSlop = config.getScaledTouchSlop();
    165         mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
    166 
    167         setOnClickListener(this);
    168         setOnLongClickListener(this);
    169         setZOrderOnTop(true);
    170         getHolder().setFormat(PixelFormat.TRANSLUCENT);
    171 
    172         if (sRS == null) {
    173             sRS = createRenderScript(true);
    174         } else {
    175             createRenderScript(sRS);
    176         }
    177 
    178         final DisplayMetrics metrics = getResources().getDisplayMetrics();
    179         final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;
    180         mColumnsPerPage = isPortrait ? Defines.COLUMNS_PER_PAGE_PORTRAIT :
    181                 Defines.COLUMNS_PER_PAGE_LANDSCAPE;
    182         mRowsPerPage = isPortrait ? Defines.ROWS_PER_PAGE_PORTRAIT :
    183                 Defines.ROWS_PER_PAGE_LANDSCAPE;
    184 
    185         if (sRollo != null) {
    186             sRollo.mAllApps = this;
    187             sRollo.mRes = getResources();
    188             sRollo.mInitialize = true;
    189         }
    190     }
    191 
    192     @SuppressWarnings({"UnusedDeclaration"})
    193     public AllApps3D(Context context, AttributeSet attrs, int defStyle) {
    194         this(context, attrs);
    195     }
    196 
    197     public void surrender() {
    198         if (sRS != null) {
    199             sRS.contextSetSurface(0, 0, null);
    200             sRS.mMessageCallback = null;
    201         }
    202         mSurrendered = true;
    203     }
    204 
    205     /**
    206      * Note that this implementation prohibits this view from ever being reattached.
    207      */
    208     @Override
    209     protected void onDetachedFromWindow() {
    210         sRS.mMessageCallback = null;
    211         if (!mSurrendered) {
    212             Log.i(TAG, "onDetachedFromWindow");
    213             destroyRenderScript();
    214             sRS = null;
    215             sRollo = null;
    216         }
    217     }
    218 
    219     /**
    220      * If you have an attached click listener, View always plays the click sound!?!?
    221      * Deal with sound effects by hand.
    222      */
    223     public void reallyPlaySoundEffect(int sound) {
    224         boolean old = isSoundEffectsEnabled();
    225         setSoundEffectsEnabled(true);
    226         playSoundEffect(sound);
    227         setSoundEffectsEnabled(old);
    228     }
    229 
    230     public void setLauncher(Launcher launcher) {
    231         mLauncher = launcher;
    232     }
    233 
    234     @Override
    235     public void surfaceDestroyed(SurfaceHolder holder) {
    236         super.surfaceDestroyed(holder);
    237         // Without this, we leak mMessageCallback which leaks the context.
    238         if (!mSurrendered) {
    239             sRS.mMessageCallback = null;
    240         }
    241         // We may lose any callbacks that are pending, so make sure that we re-sync that
    242         // on the next surfaceChanged.
    243         sZoomDirty = true;
    244         mHaveSurface = false;
    245     }
    246 
    247     @Override
    248     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    249         //long startTime = SystemClock.uptimeMillis();
    250 
    251         super.surfaceChanged(holder, format, w, h);
    252 
    253         if (mSurrendered) return;
    254 
    255         mHaveSurface = true;
    256 
    257         if (sRollo == null) {
    258             sRollo = new RolloRS(this);
    259             sRollo.init(getResources(), w, h);
    260             if (mAllAppsList != null) {
    261                 sRollo.setApps(mAllAppsList);
    262             }
    263             if (mShouldGainFocus) {
    264                 gainFocus();
    265                 mShouldGainFocus = false;
    266             }
    267         } else if (sRollo.mInitialize) {
    268             sRollo.initGl();
    269             sRollo.mInitialize = false;
    270         }
    271 
    272         initTouchState(w, h);
    273 
    274         sRollo.dirtyCheck();
    275         sRollo.resize(w, h);
    276 
    277         if (sRS != null) {
    278             sRS.mMessageCallback = mMessageProc = new AAMessage();
    279         }
    280 
    281         if (sRollo.mUniformAlloc != null) {
    282             float tf[] = new float[] {72.f, 72.f,
    283                                       120.f, 120.f, 0.f, 0.f,
    284                                       120.f, 680.f,
    285                                       (2.f / 480.f), 0, -((float)w / 2) - 0.25f, -380.25f};
    286             if (w > h) {
    287                 tf[6] = 40.f;
    288                 tf[7] = h - 40.f;
    289                 tf[9] = 1.f;
    290                 tf[10] = -((float)w / 2) - 0.25f;
    291                 tf[11] = -((float)h / 2) - 0.25f;
    292             }
    293 
    294             sRollo.mUniformAlloc.data(tf);
    295         }
    296 
    297         //long endTime = SystemClock.uptimeMillis();
    298         //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
    299     }
    300 
    301     @Override
    302     public void onWindowFocusChanged(boolean hasWindowFocus) {
    303         super.onWindowFocusChanged(hasWindowFocus);
    304 
    305         if (mSurrendered) return;
    306 
    307         if (mArrowNavigation) {
    308             if (!hasWindowFocus) {
    309                 // Clear selection when we lose window focus
    310                 mLastSelectedIcon = sRollo.mState.selectedIconIndex;
    311                 sRollo.setHomeSelected(SELECTED_NONE);
    312                 sRollo.clearSelectedIcon();
    313                 sRollo.mState.save();
    314             } else {
    315                 if (sRollo.mState.iconCount > 0) {
    316                     if (mLastSelection == SELECTION_ICONS) {
    317                         int selection = mLastSelectedIcon;
    318                         final int firstIcon = Math.round(sRollo.mScrollPos) * mColumnsPerPage;
    319                         if (selection < 0 || // No selection
    320                                 selection < firstIcon || // off the top of the screen
    321                                 selection >= sRollo.mState.iconCount || // past last icon
    322                                 selection >= firstIcon + // past last icon on screen
    323                                     (mColumnsPerPage * mRowsPerPage)) {
    324                             selection = firstIcon;
    325                         }
    326 
    327                         // Select the first icon when we gain window focus
    328                         sRollo.selectIcon(selection, SELECTED_FOCUSED);
    329                         sRollo.mState.save();
    330                     } else if (mLastSelection == SELECTION_HOME) {
    331                         sRollo.setHomeSelected(SELECTED_FOCUSED);
    332                         sRollo.mState.save();
    333                     }
    334                 }
    335             }
    336         }
    337     }
    338 
    339     @Override
    340     protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
    341         super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    342 
    343         if (!isVisible() || mSurrendered) {
    344             return;
    345         }
    346 
    347         if (gainFocus) {
    348             if (sRollo != null) {
    349                 gainFocus();
    350             } else {
    351                 mShouldGainFocus = true;
    352             }
    353         } else {
    354             if (sRollo != null) {
    355                 if (mArrowNavigation) {
    356                     // Clear selection when we lose focus
    357                     sRollo.clearSelectedIcon();
    358                     sRollo.setHomeSelected(SELECTED_NONE);
    359                     sRollo.mState.save();
    360                     mArrowNavigation = false;
    361                 }
    362             } else {
    363                 mShouldGainFocus = false;
    364             }
    365         }
    366     }
    367 
    368     private void gainFocus() {
    369         if (!mArrowNavigation && sRollo.mState.iconCount > 0) {
    370             // Select the first icon when we gain keyboard focus
    371             mArrowNavigation = true;
    372             sRollo.selectIcon(Math.round(sRollo.mScrollPos) * mColumnsPerPage, SELECTED_FOCUSED);
    373             sRollo.mState.save();
    374         }
    375     }
    376 
    377     @Override
    378     public boolean onKeyDown(int keyCode, KeyEvent event) {
    379 
    380         boolean handled = false;
    381 
    382         if (!isVisible()) {
    383             return false;
    384         }
    385         final int iconCount = sRollo.mState.iconCount;
    386 
    387         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
    388             if (mArrowNavigation) {
    389                 if (mLastSelection == SELECTION_HOME) {
    390                     reallyPlaySoundEffect(SoundEffectConstants.CLICK);
    391                     mLauncher.closeAllApps(true);
    392                 } else {
    393                     int whichApp = sRollo.mState.selectedIconIndex;
    394                     if (whichApp >= 0) {
    395                         ApplicationInfo app = mAllAppsList.get(whichApp);
    396                         mLauncher.startActivitySafely(app.intent, app);
    397                         handled = true;
    398                     }
    399                 }
    400             }
    401         }
    402 
    403         if (iconCount > 0) {
    404             final boolean isPortrait = getWidth() < getHeight();
    405 
    406             mArrowNavigation = true;
    407 
    408             int currentSelection = sRollo.mState.selectedIconIndex;
    409             int currentTopRow = Math.round(sRollo.mScrollPos);
    410 
    411             // The column of the current selection, in the range 0..COLUMNS_PER_PAGE_PORTRAIT-1
    412             final int currentPageCol = currentSelection % mColumnsPerPage;
    413 
    414             // The row of the current selection, in the range 0..ROWS_PER_PAGE_PORTRAIT-1
    415             final int currentPageRow = (currentSelection - (currentTopRow * mColumnsPerPage))
    416                     / mRowsPerPage;
    417 
    418             int newSelection = currentSelection;
    419 
    420             switch (keyCode) {
    421             case KeyEvent.KEYCODE_DPAD_UP:
    422                 if (mLastSelection == SELECTION_HOME) {
    423                     if (isPortrait) {
    424                         sRollo.setHomeSelected(SELECTED_NONE);
    425                         int lastRowCount = iconCount % mColumnsPerPage;
    426                         if (lastRowCount == 0) {
    427                             lastRowCount = mColumnsPerPage;
    428                         }
    429                         newSelection = iconCount - lastRowCount + (mColumnsPerPage / 2);
    430                         if (newSelection >= iconCount) {
    431                             newSelection = iconCount-1;
    432                         }
    433                         int target = (newSelection / mColumnsPerPage) - (mRowsPerPage - 1);
    434                         if (target < 0) {
    435                             target = 0;
    436                         }
    437                         if (currentTopRow != target) {
    438                             sRollo.moveTo(target);
    439                         }
    440                     }
    441                 } else {
    442                     if (currentPageRow > 0) {
    443                         newSelection = currentSelection - mColumnsPerPage;
    444                         if (currentTopRow > newSelection / mColumnsPerPage) {
    445                             sRollo.moveTo(newSelection / mColumnsPerPage);
    446                         }
    447                     } else if (currentTopRow > 0) {
    448                         newSelection = currentSelection - mColumnsPerPage;
    449                         sRollo.moveTo(newSelection / mColumnsPerPage);
    450                     } else if (currentPageRow != 0) {
    451                         newSelection = currentTopRow * mRowsPerPage;
    452                     }
    453                 }
    454                 handled = true;
    455                 break;
    456 
    457             case KeyEvent.KEYCODE_DPAD_DOWN: {
    458                 final int rowCount = iconCount / mColumnsPerPage
    459                         + (iconCount % mColumnsPerPage == 0 ? 0 : 1);
    460                 final int currentRow = currentSelection / mColumnsPerPage;
    461                 if (mLastSelection != SELECTION_HOME) {
    462                     if (currentRow < rowCount-1) {
    463                         sRollo.setHomeSelected(SELECTED_NONE);
    464                         if (currentSelection < 0) {
    465                             newSelection = 0;
    466                         } else {
    467                             newSelection = currentSelection + mColumnsPerPage;
    468                         }
    469                         if (newSelection >= iconCount) {
    470                             // Go from D to G in this arrangement:
    471                             //     A B C D
    472                             //     E F G
    473                             newSelection = iconCount - 1;
    474                         }
    475                         if (currentPageRow >= mRowsPerPage - 1) {
    476                             sRollo.moveTo((newSelection / mColumnsPerPage) - mRowsPerPage + 1);
    477                         }
    478                     } else if (isPortrait) {
    479                         newSelection = -1;
    480                         sRollo.setHomeSelected(SELECTED_FOCUSED);
    481                     }
    482                 }
    483                 handled = true;
    484                 break;
    485             }
    486             case KeyEvent.KEYCODE_DPAD_LEFT:
    487                 if (mLastSelection != SELECTION_HOME) {
    488                     if (currentPageCol > 0) {
    489                         newSelection = currentSelection - 1;
    490                     }
    491                 } else if (!isPortrait) {
    492                     newSelection = ((int) (sRollo.mScrollPos) * mColumnsPerPage) +
    493                             (mRowsPerPage / 2 * mColumnsPerPage) + mColumnsPerPage - 1;
    494                     sRollo.setHomeSelected(SELECTED_NONE);
    495                 }
    496                 handled = true;
    497                 break;
    498             case KeyEvent.KEYCODE_DPAD_RIGHT:
    499                 if (mLastSelection != SELECTION_HOME) {
    500                     if (!isPortrait && (currentPageCol == mColumnsPerPage - 1 ||
    501                             currentSelection == iconCount - 1)) {
    502                         newSelection = -1;
    503                         sRollo.setHomeSelected(SELECTED_FOCUSED);
    504                     } else if ((currentPageCol < mColumnsPerPage - 1) &&
    505                             (currentSelection < iconCount - 1)) {
    506                         newSelection = currentSelection + 1;
    507                     }
    508                 }
    509                 handled = true;
    510                 break;
    511             }
    512             if (newSelection != currentSelection) {
    513                 sRollo.selectIcon(newSelection, SELECTED_FOCUSED);
    514                 sRollo.mState.save();
    515             }
    516         }
    517         return handled;
    518     }
    519 
    520     void initTouchState(int width, int height) {
    521         boolean isPortrait = width < height;
    522 
    523         int[] viewPos = new int[2];
    524         getLocationOnScreen(viewPos);
    525 
    526         mTouchXBorders = new int[mColumnsPerPage + 1];
    527         mTouchYBorders = new int[mRowsPerPage + 1];
    528 
    529         // TODO: Put this in a config file/define
    530         int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE_PORTRAIT;
    531         if (!isPortrait) cellHeight -= 12;
    532         int centerY = (int) (height * (isPortrait ? 0.5f : 0.47f));
    533         if (!isPortrait) centerY += cellHeight / 2;
    534         int half = (int) Math.floor((mRowsPerPage + 1) / 2);
    535         int end = mTouchYBorders.length - (half + 1);
    536 
    537         for (int i = -half; i <= end; i++) {
    538             mTouchYBorders[i + half] = centerY + (i * cellHeight) - viewPos[1];
    539         }
    540 
    541         int x = 0;
    542         // TODO: Put this in a config file/define
    543         int columnWidth = 120;
    544         for (int i = 0; i < mColumnsPerPage + 1; i++) {
    545             mTouchXBorders[i] = x - viewPos[0];
    546             x += columnWidth;
    547         }
    548     }
    549 
    550     int chooseTappedIcon(int x, int y) {
    551         float pos = sRollo != null ? sRollo.mScrollPos : 0;
    552 
    553         int oldY = y;
    554 
    555         // Adjust for scroll position if not zero.
    556         y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
    557 
    558         int col = -1;
    559         int row = -1;
    560         final int columnsCount = mColumnsPerPage;
    561         for (int i=0; i< columnsCount; i++) {
    562             if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
    563                 col = i;
    564                 break;
    565             }
    566         }
    567         final int rowsCount = mRowsPerPage;
    568         for (int i=0; i< rowsCount; i++) {
    569             if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
    570                 row = i;
    571                 break;
    572             }
    573         }
    574 
    575         if (row < 0 || col < 0) {
    576             return -1;
    577         }
    578 
    579         int index = (((int) pos) * columnsCount) + (row * columnsCount) + col;
    580 
    581         if (index >= mAllAppsList.size()) {
    582             return -1;
    583         } else {
    584             return index;
    585         }
    586     }
    587 
    588     @Override
    589     public boolean onTouchEvent(MotionEvent ev)
    590     {
    591         mArrowNavigation = false;
    592 
    593         if (!isVisible()) {
    594             return true;
    595         }
    596 
    597         if (mLocks != 0) {
    598             return true;
    599         }
    600 
    601         super.onTouchEvent(ev);
    602 
    603         int x = (int)ev.getX();
    604         int y = (int)ev.getY();
    605 
    606         final boolean isPortrait = getWidth() < getHeight();
    607         int action = ev.getAction();
    608         switch (action) {
    609         case MotionEvent.ACTION_DOWN:
    610             if ((isPortrait && y > mTouchYBorders[mTouchYBorders.length-1]) ||
    611                     (!isPortrait && x > mTouchXBorders[mTouchXBorders.length-1])) {
    612                 mTouchTracking = TRACKING_HOME;
    613                 sRollo.setHomeSelected(SELECTED_PRESSED);
    614                 sRollo.mState.save();
    615                 mCurrentIconIndex = -1;
    616             } else {
    617                 mTouchTracking = TRACKING_FLING;
    618 
    619                 mMotionDownRawX = (int)ev.getRawX();
    620                 mMotionDownRawY = (int)ev.getRawY();
    621 
    622                 sRollo.mState.newPositionX = ev.getRawY() / getHeight();
    623                 sRollo.mState.newTouchDown = 1;
    624 
    625                 if (!sRollo.checkClickOK()) {
    626                     sRollo.clearSelectedIcon();
    627                 } else {
    628                     mDownIconIndex = mCurrentIconIndex
    629                             = sRollo.selectIcon(x, y, SELECTED_PRESSED);
    630                     if (mDownIconIndex < 0) {
    631                         // if nothing was selected, no long press.
    632                         cancelLongPress();
    633                     }
    634                 }
    635                 sRollo.mState.save();
    636                 sRollo.move();
    637                 mVelocityTracker = VelocityTracker.obtain();
    638                 mVelocityTracker.addMovement(ev);
    639                 mStartedScrolling = false;
    640             }
    641             break;
    642         case MotionEvent.ACTION_MOVE:
    643         case MotionEvent.ACTION_OUTSIDE:
    644             if (mTouchTracking == TRACKING_HOME) {
    645                 sRollo.setHomeSelected((isPortrait &&
    646                         y > mTouchYBorders[mTouchYBorders.length-1]) || (!isPortrait
    647                         && x > mTouchXBorders[mTouchXBorders.length-1])
    648                         ? SELECTED_PRESSED : SELECTED_NONE);
    649                 sRollo.mState.save();
    650             } else if (mTouchTracking == TRACKING_FLING) {
    651                 int rawY = (int)ev.getRawY();
    652                 int slop;
    653                 slop = Math.abs(rawY - mMotionDownRawY);
    654 
    655                 if (!mStartedScrolling && slop < mSlop) {
    656                     // don't update anything so when we do start scrolling
    657                     // below, we get the right delta.
    658                     mCurrentIconIndex = chooseTappedIcon(x, y);
    659                     if (mDownIconIndex != mCurrentIconIndex) {
    660                         // If a different icon is selected, don't allow it to be picked up.
    661                         // This handles off-axis dragging.
    662                         cancelLongPress();
    663                         mCurrentIconIndex = -1;
    664                     }
    665                 } else {
    666                     if (!mStartedScrolling) {
    667                         cancelLongPress();
    668                         mCurrentIconIndex = -1;
    669                     }
    670                     sRollo.mState.newPositionX = ev.getRawY() / getHeight();
    671                     sRollo.mState.newTouchDown = 1;
    672                     sRollo.move();
    673 
    674                     mStartedScrolling = true;
    675                     sRollo.clearSelectedIcon();
    676                     mVelocityTracker.addMovement(ev);
    677                     sRollo.mState.save();
    678                 }
    679             }
    680             break;
    681         case MotionEvent.ACTION_UP:
    682         case MotionEvent.ACTION_CANCEL:
    683             if (mTouchTracking == TRACKING_HOME) {
    684                 if (action == MotionEvent.ACTION_UP) {
    685                     if ((isPortrait && y > mTouchYBorders[mTouchYBorders.length-1]) ||
    686                         (!isPortrait && x > mTouchXBorders[mTouchXBorders.length-1])) {
    687                         reallyPlaySoundEffect(SoundEffectConstants.CLICK);
    688                         mLauncher.closeAllApps(true);
    689                     }
    690                     sRollo.setHomeSelected(SELECTED_NONE);
    691                     sRollo.mState.save();
    692                 }
    693                 mCurrentIconIndex = -1;
    694             } else if (mTouchTracking == TRACKING_FLING) {
    695                 sRollo.mState.newTouchDown = 0;
    696                 sRollo.mState.newPositionX = ev.getRawY() / getHeight();
    697 
    698                 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
    699                 sRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
    700                 sRollo.clearSelectedIcon();
    701                 sRollo.mState.save();
    702                 sRollo.fling();
    703 
    704                 if (mVelocityTracker != null) {
    705                     mVelocityTracker.recycle();
    706                     mVelocityTracker = null;
    707                 }
    708             }
    709             mTouchTracking = TRACKING_NONE;
    710             break;
    711         }
    712 
    713         return true;
    714     }
    715 
    716     public void onClick(View v) {
    717         if (mLocks != 0 || !isVisible()) {
    718             return;
    719         }
    720         if (sRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
    721                 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
    722             reallyPlaySoundEffect(SoundEffectConstants.CLICK);
    723             ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
    724             mLauncher.startActivitySafely(app.intent, app);
    725         }
    726     }
    727 
    728     public boolean onLongClick(View v) {
    729         if (mLocks != 0 || !isVisible()) {
    730             return true;
    731         }
    732         if (sRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
    733                 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
    734             ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
    735 
    736             Bitmap bmp = app.iconBitmap;
    737             final int w = bmp.getWidth();
    738             final int h = bmp.getHeight();
    739 
    740             // We don't really have an accurate location to use.  This will do.
    741             int screenX = mMotionDownRawX - (w / 2);
    742             int screenY = mMotionDownRawY - h;
    743 
    744             mDragController.startDrag(bmp, screenX, screenY,
    745                     0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
    746 
    747             mLauncher.closeAllApps(true);
    748         }
    749         return true;
    750     }
    751 
    752     @Override
    753     public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
    754         if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
    755             if (!isVisible()) {
    756                 return false;
    757             }
    758             String text = null;
    759             int index;
    760             int count = mAllAppsList.size() + 1; // +1 is home
    761             int pos = -1;
    762             switch (mLastSelection) {
    763             case SELECTION_ICONS:
    764                 index = sRollo.mState.selectedIconIndex;
    765                 if (index >= 0) {
    766                     ApplicationInfo info = mAllAppsList.get(index);
    767                     if (info.title != null) {
    768                         text = info.title.toString();
    769                         pos = index;
    770                     }
    771                 }
    772                 break;
    773             case SELECTION_HOME:
    774                 text = getContext().getString(R.string.all_apps_home_button_label);
    775                 pos = count;
    776                 break;
    777             }
    778             if (text != null) {
    779                 event.setEnabled(true);
    780                 event.getText().add(text);
    781                 //event.setContentDescription(text);
    782                 event.setItemCount(count);
    783                 event.setCurrentItemIndex(pos);
    784             }
    785         }
    786         return false;
    787     }
    788 
    789     public void setDragController(DragController dragger) {
    790         mDragController = dragger;
    791     }
    792 
    793     public void onDropCompleted(View target, boolean success) {
    794     }
    795 
    796     /**
    797      * Zoom to the specifed level.
    798      *
    799      * @param zoom [0..1] 0 is hidden, 1 is open
    800      */
    801     public void zoom(float zoom, boolean animate) {
    802         cancelLongPress();
    803         sNextZoom = zoom;
    804         sAnimateNextZoom = animate;
    805         // if we do setZoom while we don't have a surface, we won't
    806         // get the callbacks that actually set mZoom.
    807         if (sRollo == null || !mHaveSurface) {
    808             sZoomDirty = true;
    809             mZoom = zoom;
    810         } else {
    811             sRollo.setZoom(zoom, animate);
    812         }
    813     }
    814 
    815     /**
    816      * If sRollo is null, then we're not visible.  This is also used to guard against
    817      * sRollo being null.
    818      */
    819     public boolean isVisible() {
    820         return sRollo != null && mZoom > 0.001f;
    821     }
    822 
    823     public boolean isOpaque() {
    824         return mZoom > 0.999f;
    825     }
    826 
    827     public void setApps(ArrayList<ApplicationInfo> list) {
    828         if (sRS == null) {
    829             // We've been removed from the window.  Don't bother with all this.
    830             return;
    831         }
    832 
    833         if (list != null) {
    834             Collections.sort(list, LauncherModel.APP_NAME_COMPARATOR);
    835         }
    836 
    837         boolean reload = false;
    838         if (mAllAppsList == null) {
    839             reload = true;
    840         } else if (list.size() != mAllAppsList.size()) {
    841             reload = true;
    842         } else {
    843             final int count = list.size();
    844             for (int i = 0; i < count; i++) {
    845                 if (list.get(i) != mAllAppsList.get(i)) {
    846                     reload = true;
    847                     break;
    848                 }
    849             }
    850         }
    851 
    852         mAllAppsList = list;
    853         if (sRollo != null && reload) {
    854             sRollo.setApps(list);
    855         }
    856 
    857         if (hasFocus() && mRestoreFocusIndex != -1) {
    858             sRollo.selectIcon(mRestoreFocusIndex, SELECTED_FOCUSED);
    859             sRollo.mState.save();
    860             mRestoreFocusIndex = -1;
    861         }
    862 
    863         mLocks &= ~LOCK_ICONS_PENDING;
    864     }
    865 
    866     public void addApps(ArrayList<ApplicationInfo> list) {
    867         if (mAllAppsList == null) {
    868             // Not done loading yet.  We'll find out about it later.
    869             return;
    870         }
    871         if (sRS == null) {
    872             // We've been removed from the window.  Don't bother with all this.
    873             return;
    874         }
    875 
    876         final int N = list.size();
    877         if (sRollo != null) {
    878             sRollo.pause();
    879             sRollo.reallocAppsList(sRollo.mState.iconCount + N);
    880         }
    881 
    882         for (int i=0; i<N; i++) {
    883             final ApplicationInfo item = list.get(i);
    884             int index = Collections.binarySearch(mAllAppsList, item,
    885                     LauncherModel.APP_NAME_COMPARATOR);
    886             if (index < 0) {
    887                 index = -(index+1);
    888             }
    889             mAllAppsList.add(index, item);
    890             if (sRollo != null) {
    891                 sRollo.addApp(index, item);
    892             }
    893         }
    894 
    895         if (sRollo != null) {
    896             sRollo.saveAppsList();
    897             sRollo.resume();
    898         }
    899     }
    900 
    901     public void removeApps(ArrayList<ApplicationInfo> list) {
    902         if (mAllAppsList == null) {
    903             // Not done loading yet.  We'll find out about it later.
    904             return;
    905         }
    906 
    907         if (sRollo != null) {
    908             sRollo.pause();
    909         }
    910         final int N = list.size();
    911         for (int i=0; i<N; i++) {
    912             final ApplicationInfo item = list.get(i);
    913             int index = findAppByComponent(mAllAppsList, item);
    914             if (index >= 0) {
    915                 mAllAppsList.remove(index);
    916                 if (sRollo != null) {
    917                     sRollo.removeApp(index);
    918                 }
    919             } else {
    920                 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
    921                 // Try to recover.  This should keep us from crashing for now.
    922             }
    923         }
    924 
    925         if (sRollo != null) {
    926             sRollo.saveAppsList();
    927             sRollo.resume();
    928         }
    929     }
    930 
    931     public void updateApps(ArrayList<ApplicationInfo> list) {
    932         // Just remove and add, because they may need to be re-sorted.
    933         removeApps(list);
    934         addApps(list);
    935     }
    936 
    937     private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
    938         ComponentName component = item.intent.getComponent();
    939         final int N = list.size();
    940         for (int i=0; i<N; i++) {
    941             ApplicationInfo x = list.get(i);
    942             if (x.intent.getComponent().equals(component)) {
    943                 return i;
    944             }
    945         }
    946         return -1;
    947     }
    948 
    949     /*
    950     private static int countPages(int iconCount) {
    951         int iconsPerPage = getColumnsCount() * Defines.ROWS_PER_PAGE_PORTRAIT;
    952         int pages = iconCount / iconsPerPage;
    953         if (pages*iconsPerPage != iconCount) {
    954             pages++;
    955         }
    956         return pages;
    957     }
    958     */
    959 
    960     class AAMessage extends RenderScript.RSMessage {
    961         public void run() {
    962             sRollo.mScrollPos = ((float)mData[0]) / (1 << 16);
    963             mVelocity = ((float)mData[1]) / (1 << 16);
    964 
    965             boolean lastVisible = isVisible();
    966             mZoom = ((float)mData[2]) / (1 << 16);
    967 
    968             final boolean visible = isVisible();
    969             if (visible != lastVisible) {
    970                 post(new Runnable() {
    971                     public void run() {
    972                         if (visible) {
    973                             showSurface();
    974                         } else {
    975                             hideSurface();
    976                         }
    977                     }
    978                 });
    979             }
    980 
    981             sZoomDirty = false;
    982         }
    983     }
    984 
    985     public static class RolloRS {
    986         // Allocations ======
    987         private int mWidth;
    988         private int mHeight;
    989 
    990         private Resources mRes;
    991         private Script mScript;
    992         private Script.Invokable mInvokeMove;
    993         private Script.Invokable mInvokeMoveTo;
    994         private Script.Invokable mInvokeFling;
    995         private Script.Invokable mInvokeResetWAR;
    996         private Script.Invokable mInvokeSetZoom;
    997 
    998         private ProgramStore mPSIcons;
    999         private ProgramFragment mPFTexMip;
   1000         private ProgramFragment mPFTexMipAlpha;
   1001         private ProgramFragment mPFTexNearest;
   1002         private ProgramVertex mPV;
   1003         private ProgramVertex mPVCurve;
   1004         private SimpleMesh mMesh;
   1005         private ProgramVertex.MatrixAllocation mPVA;
   1006 
   1007         private Allocation mUniformAlloc;
   1008 
   1009         private Allocation mHomeButtonNormal;
   1010         private Allocation mHomeButtonFocused;
   1011         private Allocation mHomeButtonPressed;
   1012 
   1013         private Allocation[] mIcons;
   1014         private int[] mIconIds;
   1015         private Allocation mAllocIconIds;
   1016 
   1017         private Allocation[] mLabels;
   1018         private int[] mLabelIds;
   1019         private Allocation mAllocLabelIds;
   1020         private Allocation mSelectedIcon;
   1021 
   1022         private Bitmap mSelectionBitmap;
   1023         private Canvas mSelectionCanvas;
   1024 
   1025         private float mScrollPos;
   1026 
   1027         Params mParams;
   1028         State mState;
   1029 
   1030         AllApps3D mAllApps;
   1031         boolean mInitialize;
   1032 
   1033         class BaseAlloc {
   1034             Allocation mAlloc;
   1035             Type mType;
   1036 
   1037             void save() {
   1038                 mAlloc.data(this);
   1039             }
   1040         }
   1041 
   1042         private boolean checkClickOK() {
   1043             return (Math.abs(mAllApps.mVelocity) < 0.4f) &&
   1044                    (Math.abs(mScrollPos - Math.round(mScrollPos)) < 0.4f);
   1045         }
   1046 
   1047         void pause() {
   1048             if (sRS != null) {
   1049                 sRS.contextBindRootScript(null);
   1050             }
   1051         }
   1052 
   1053         void resume() {
   1054             if (sRS != null) {
   1055                 sRS.contextBindRootScript(mScript);
   1056             }
   1057         }
   1058 
   1059         class Params extends BaseAlloc {
   1060             Params() {
   1061                 mType = Type.createFromClass(sRS, Params.class, 1, "ParamsClass");
   1062                 mAlloc = Allocation.createTyped(sRS, mType);
   1063                 save();
   1064             }
   1065             public int bubbleWidth;
   1066             public int bubbleHeight;
   1067             public int bubbleBitmapWidth;
   1068             public int bubbleBitmapHeight;
   1069 
   1070             public int homeButtonWidth;
   1071             public int homeButtonHeight;
   1072             public int homeButtonTextureWidth;
   1073             public int homeButtonTextureHeight;
   1074         }
   1075 
   1076         class State extends BaseAlloc {
   1077             public float newPositionX;
   1078             public int newTouchDown;
   1079             public float flingVelocity;
   1080             public int iconCount;
   1081             public int selectedIconIndex = -1;
   1082             public int selectedIconTexture;
   1083             public float zoomTarget;
   1084             public int homeButtonId;
   1085             public float targetPos;
   1086 
   1087             State() {
   1088                 mType = Type.createFromClass(sRS, State.class, 1, "StateClass");
   1089                 mAlloc = Allocation.createTyped(sRS, mType);
   1090                 save();
   1091             }
   1092         }
   1093 
   1094         public RolloRS(AllApps3D allApps) {
   1095             mAllApps = allApps;
   1096         }
   1097 
   1098         public void init(Resources res, int width, int height) {
   1099             mRes = res;
   1100             mWidth = width;
   1101             mHeight = height;
   1102             initProgramVertex();
   1103             initProgramFragment();
   1104             initProgramStore();
   1105             initGl();
   1106             initData();
   1107             initRs();
   1108         }
   1109 
   1110         public void initMesh() {
   1111             SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(sRS, 2, 0);
   1112 
   1113             for (int ct=0; ct < 16; ct++) {
   1114                 float pos = (1.f / (16.f - 1)) * ct;
   1115                 tm.addVertex(0.0f, pos);
   1116                 tm.addVertex(1.0f, pos);
   1117             }
   1118             for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
   1119                 tm.addTriangle(ct, ct+1, ct+2);
   1120                 tm.addTriangle(ct+1, ct+3, ct+2);
   1121             }
   1122             mMesh = tm.create();
   1123             mMesh.setName("SMCell");
   1124         }
   1125 
   1126         void resize(int w, int h) {
   1127             mPVA.setupProjectionNormalized(w, h);
   1128             mWidth = w;
   1129             mHeight = h;
   1130         }
   1131 
   1132         private void initProgramVertex() {
   1133             mPVA = new ProgramVertex.MatrixAllocation(sRS);
   1134             resize(mWidth, mHeight);
   1135 
   1136             ProgramVertex.Builder pvb = new ProgramVertex.Builder(sRS, null, null);
   1137             pvb.setTextureMatrixEnable(true);
   1138             mPV = pvb.create();
   1139             mPV.setName("PV");
   1140             mPV.bindAllocation(mPVA);
   1141 
   1142             Element.Builder eb = new Element.Builder(sRS);
   1143             eb.add(Element.createVector(sRS, Element.DataType.FLOAT_32, 2), "ImgSize");
   1144             eb.add(Element.createVector(sRS, Element.DataType.FLOAT_32, 4), "Position");
   1145             eb.add(Element.createVector(sRS, Element.DataType.FLOAT_32, 2), "BendPos");
   1146             eb.add(Element.createVector(sRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
   1147             Element e = eb.create();
   1148 
   1149             mUniformAlloc = Allocation.createSized(sRS, e, 1);
   1150 
   1151             initMesh();
   1152             ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(sRS);
   1153             String t = "void main() {\n" +
   1154                     // Animation
   1155                     "  float ani = UNI_Position.z;\n" +
   1156 
   1157                     "  float bendY1 = UNI_BendPos.x;\n" +
   1158                     "  float bendY2 = UNI_BendPos.y;\n" +
   1159                     "  float bendAngle = 47.0 * (3.14 / 180.0);\n" +
   1160                     "  float bendDistance = bendY1 * 0.4;\n" +
   1161                     "  float distanceDimLevel = 0.6;\n" +
   1162 
   1163                     "  float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
   1164                     "  float aDy = cos(bendAngle);\n" +
   1165                     "  float aDz = sin(bendAngle);\n" +
   1166 
   1167                     "  float scale = (2.0 / 480.0);\n" +
   1168                     "  float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
   1169                     "  float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
   1170                     "  float y = 0.0;\n" +
   1171                     "  float z = 0.0;\n" +
   1172                     "  float lum = 1.0;\n" +
   1173 
   1174                     "  float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
   1175                     "  y += cv * aDy;\n" +
   1176                     "  z += -cv * aDz;\n" +
   1177                     "  cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" +  // curve range
   1178                     "  lum += cv / bendDistance * distanceDimLevel;\n" +
   1179                     "  y += cv * cos(cv * bendStep);\n" +
   1180                     "  z += cv * sin(cv * bendStep);\n" +
   1181 
   1182                     "  cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
   1183                     "  y += cv * aDy;\n" +
   1184                     "  z += cv * aDz;\n" +
   1185                     "  cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
   1186                     "  lum -= cv / bendDistance * distanceDimLevel;\n" +
   1187                     "  y += cv * cos(cv * bendStep);\n" +
   1188                     "  z += cv * sin(cv * bendStep);\n" +
   1189 
   1190                     "  y += clamp(ys, bendY1, bendY2);\n" +
   1191 
   1192                     "  vec4 pos;\n" +
   1193                     "  pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
   1194                     "  pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
   1195                     "  pos.z = z * UNI_ScaleOffset.x;\n" +
   1196                     "  pos.w = 1.0;\n" +
   1197 
   1198                     "  pos.x *= 1.0 + ani * 4.0;\n" +
   1199                     "  pos.y *= 1.0 + ani * 4.0;\n" +
   1200                     "  pos.z -= ani * 1.5;\n" +
   1201                     "  lum *= 1.0 - ani;\n" +
   1202 
   1203                     "  gl_Position = UNI_MVP * pos;\n" +
   1204                     "  varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
   1205                     "  varTex0.xy = ATTRIB_position;\n" +
   1206                     "  varTex0.y = 1.0 - varTex0.y;\n" +
   1207                     "  varTex0.zw = vec2(0.0, 0.0);\n" +
   1208                     "}\n";
   1209             sb.setShader(t);
   1210             sb.addConstant(mUniformAlloc.getType());
   1211             sb.addInput(mMesh.getVertexType(0).getElement());
   1212             mPVCurve = sb.create();
   1213             mPVCurve.setName("PVCurve");
   1214             mPVCurve.bindAllocation(mPVA);
   1215             mPVCurve.bindConstants(mUniformAlloc, 1);
   1216 
   1217             sRS.contextBindProgramVertex(mPV);
   1218         }
   1219 
   1220         private void initProgramFragment() {
   1221             Sampler.Builder sb = new Sampler.Builder(sRS);
   1222             sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
   1223             sb.setMag(Sampler.Value.NEAREST);
   1224             sb.setWrapS(Sampler.Value.CLAMP);
   1225             sb.setWrapT(Sampler.Value.CLAMP);
   1226             Sampler linear = sb.create();
   1227 
   1228             sb.setMin(Sampler.Value.NEAREST);
   1229             sb.setMag(Sampler.Value.NEAREST);
   1230             Sampler nearest = sb.create();
   1231 
   1232             ProgramFragment.Builder bf = new ProgramFragment.Builder(sRS);
   1233             bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
   1234                           ProgramFragment.Builder.Format.RGBA, 0);
   1235             mPFTexMip = bf.create();
   1236             mPFTexMip.setName("PFTexMip");
   1237             mPFTexMip.bindSampler(linear, 0);
   1238 
   1239             mPFTexNearest = bf.create();
   1240             mPFTexNearest.setName("PFTexNearest");
   1241             mPFTexNearest.bindSampler(nearest, 0);
   1242 
   1243             bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
   1244                           ProgramFragment.Builder.Format.ALPHA, 0);
   1245             mPFTexMipAlpha = bf.create();
   1246             mPFTexMipAlpha.setName("PFTexMipAlpha");
   1247             mPFTexMipAlpha.bindSampler(linear, 0);
   1248 
   1249         }
   1250 
   1251         private void initProgramStore() {
   1252             ProgramStore.Builder bs = new ProgramStore.Builder(sRS, null, null);
   1253             bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
   1254             bs.setColorMask(true,true,true,false);
   1255             bs.setDitherEnable(true);
   1256             bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
   1257                             ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
   1258             mPSIcons = bs.create();
   1259             mPSIcons.setName("PSIcons");
   1260         }
   1261 
   1262         private void initGl() {
   1263         }
   1264 
   1265         private void initData() {
   1266             mParams = new Params();
   1267             mState = new State();
   1268 
   1269             final Utilities.BubbleText bubble = new Utilities.BubbleText(mAllApps.getContext());
   1270 
   1271             mParams.bubbleWidth = bubble.getBubbleWidth();
   1272             mParams.bubbleHeight = bubble.getMaxBubbleHeight();
   1273             mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
   1274             mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
   1275 
   1276             mHomeButtonNormal = Allocation.createFromBitmapResource(sRS, mRes,
   1277                     R.drawable.home_button_normal, Element.RGBA_8888(sRS), false);
   1278             mHomeButtonNormal.uploadToTexture(0);
   1279             mHomeButtonFocused = Allocation.createFromBitmapResource(sRS, mRes,
   1280                     R.drawable.home_button_focused, Element.RGBA_8888(sRS), false);
   1281             mHomeButtonFocused.uploadToTexture(0);
   1282             mHomeButtonPressed = Allocation.createFromBitmapResource(sRS, mRes,
   1283                     R.drawable.home_button_pressed, Element.RGBA_8888(sRS), false);
   1284             mHomeButtonPressed.uploadToTexture(0);
   1285             mParams.homeButtonWidth = 76;
   1286             mParams.homeButtonHeight = 68;
   1287             mParams.homeButtonTextureWidth = 128;
   1288             mParams.homeButtonTextureHeight = 128;
   1289 
   1290             mState.homeButtonId = mHomeButtonNormal.getID();
   1291 
   1292             mParams.save();
   1293             mState.save();
   1294 
   1295             mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
   1296                     Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
   1297             mSelectionCanvas = new Canvas(mSelectionBitmap);
   1298 
   1299             setApps(null);
   1300         }
   1301 
   1302         private void initRs() {
   1303             ScriptC.Builder sb = new ScriptC.Builder(sRS);
   1304             sb.setScript(mRes, R.raw.allapps);
   1305             sb.setRoot(true);
   1306             sb.addDefines(mAllApps.mDefines);
   1307             sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
   1308             sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
   1309             sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
   1310             mInvokeMove = sb.addInvokable("move");
   1311             mInvokeFling = sb.addInvokable("fling");
   1312             mInvokeMoveTo = sb.addInvokable("moveTo");
   1313             mInvokeResetWAR = sb.addInvokable("resetHWWar");
   1314             mInvokeSetZoom = sb.addInvokable("setZoom");
   1315             mScript = sb.create();
   1316             mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   1317             mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
   1318             mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
   1319             mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
   1320             mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
   1321             mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
   1322 
   1323             sRS.contextBindRootScript(mScript);
   1324         }
   1325 
   1326         void dirtyCheck() {
   1327             if (sZoomDirty) {
   1328                 setZoom(mAllApps.sNextZoom, mAllApps.sAnimateNextZoom);
   1329             }
   1330         }
   1331 
   1332         @SuppressWarnings({"ConstantConditions"})
   1333         private void setApps(ArrayList<ApplicationInfo> list) {
   1334             sRollo.pause();
   1335             final int count = list != null ? list.size() : 0;
   1336             int allocCount = count;
   1337             if (allocCount < 1) {
   1338                 allocCount = 1;
   1339             }
   1340 
   1341             mIcons = new Allocation[count];
   1342             mIconIds = new int[allocCount];
   1343             mAllocIconIds = Allocation.createSized(sRS, Element.USER_I32(sRS), allocCount);
   1344 
   1345             mLabels = new Allocation[count];
   1346             mLabelIds = new int[allocCount];
   1347             mAllocLabelIds = Allocation.createSized(sRS, Element.USER_I32(sRS), allocCount);
   1348 
   1349             mState.iconCount = count;
   1350             for (int i=0; i < mState.iconCount; i++) {
   1351                 createAppIconAllocations(i, list.get(i));
   1352             }
   1353             for (int i=0; i < mState.iconCount; i++) {
   1354                 uploadAppIcon(i, list.get(i));
   1355             }
   1356             saveAppsList();
   1357             sRollo.resume();
   1358         }
   1359 
   1360         private void setZoom(float zoom, boolean animate) {
   1361             if (animate) {
   1362                 sRollo.clearSelectedIcon();
   1363                 sRollo.setHomeSelected(SELECTED_NONE);
   1364             }
   1365             if (zoom > 0.001f) {
   1366                 sRollo.mState.zoomTarget = zoom;
   1367             } else {
   1368                 sRollo.mState.zoomTarget = 0;
   1369             }
   1370             sRollo.mState.save();
   1371             if (!animate) {
   1372                 sRollo.mInvokeSetZoom.execute();
   1373             }
   1374         }
   1375 
   1376         private void createAppIconAllocations(int index, ApplicationInfo item) {
   1377             mIcons[index] = Allocation.createFromBitmap(sRS, item.iconBitmap,
   1378                     Element.RGBA_8888(sRS), false);
   1379             mLabels[index] = Allocation.createFromBitmap(sRS, item.titleBitmap,
   1380                     Element.A_8(sRS), false);
   1381             mIconIds[index] = mIcons[index].getID();
   1382             mLabelIds[index] = mLabels[index].getID();
   1383         }
   1384 
   1385         private void uploadAppIcon(int index, ApplicationInfo item) {
   1386             if (mIconIds[index] != mIcons[index].getID()) {
   1387                 throw new IllegalStateException("uploadAppIcon index=" + index
   1388                     + " mIcons[index].getID=" + mIcons[index].getID()
   1389                     + " mIconsIds[index]=" + mIconIds[index]
   1390                     + " item=" + item);
   1391             }
   1392             mIcons[index].uploadToTexture(true, 0);
   1393             mLabels[index].uploadToTexture(true, 0);
   1394         }
   1395 
   1396         /**
   1397          * Puts the empty spaces at the end.  Updates mState.iconCount.  You must
   1398          * fill in the values and call saveAppsList().
   1399          */
   1400         private void reallocAppsList(int count) {
   1401             Allocation[] icons = new Allocation[count];
   1402             int[] iconIds = new int[count];
   1403             mAllocIconIds = Allocation.createSized(sRS, Element.USER_I32(sRS), count);
   1404 
   1405             Allocation[] labels = new Allocation[count];
   1406             int[] labelIds = new int[count];
   1407             mAllocLabelIds = Allocation.createSized(sRS, Element.USER_I32(sRS), count);
   1408 
   1409             final int oldCount = sRollo.mState.iconCount;
   1410 
   1411             System.arraycopy(mIcons, 0, icons, 0, oldCount);
   1412             System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
   1413             System.arraycopy(mLabels, 0, labels, 0, oldCount);
   1414             System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
   1415 
   1416             mIcons = icons;
   1417             mIconIds = iconIds;
   1418             mLabels = labels;
   1419             mLabelIds = labelIds;
   1420         }
   1421 
   1422         /**
   1423          * Handle the allocations for the new app.  Make sure you call saveAppsList when done.
   1424          */
   1425         private void addApp(int index, ApplicationInfo item) {
   1426             final int count = mState.iconCount - index;
   1427             final int dest = index + 1;
   1428 
   1429             System.arraycopy(mIcons, index, mIcons, dest, count);
   1430             System.arraycopy(mIconIds, index, mIconIds, dest, count);
   1431             System.arraycopy(mLabels, index, mLabels, dest, count);
   1432             System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
   1433 
   1434             createAppIconAllocations(index, item);
   1435             uploadAppIcon(index, item);
   1436             sRollo.mState.iconCount++;
   1437         }
   1438 
   1439         /**
   1440          * Handle the allocations for the removed app.  Make sure you call saveAppsList when done.
   1441          */
   1442         private void removeApp(int index) {
   1443             final int count = mState.iconCount - index - 1;
   1444             final int src = index + 1;
   1445 
   1446             System.arraycopy(mIcons, src, mIcons, index, count);
   1447             System.arraycopy(mIconIds, src, mIconIds, index, count);
   1448             System.arraycopy(mLabels, src, mLabels, index, count);
   1449             System.arraycopy(mLabelIds, src, mLabelIds, index, count);
   1450 
   1451             sRollo.mState.iconCount--;
   1452             final int last = mState.iconCount;
   1453 
   1454             mIcons[last] = null;
   1455             mIconIds[last] = 0;
   1456             mLabels[last] = null;
   1457             mLabelIds[last] = 0;
   1458         }
   1459 
   1460         /**
   1461          * Send the apps list structures to RS.
   1462          */
   1463         private void saveAppsList() {
   1464             // WTF: how could mScript be not null but mAllocIconIds null b/2460740.
   1465             if (mScript != null && mAllocIconIds != null) {
   1466                 mAllocIconIds.data(mIconIds);
   1467                 mAllocLabelIds.data(mLabelIds);
   1468 
   1469                 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
   1470                 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
   1471 
   1472                 mState.save();
   1473 
   1474                 // Note: mScript may be null if we haven't initialized it yet.
   1475                 // In that case, this is a no-op.
   1476                 if (mInvokeResetWAR != null) {
   1477                     mInvokeResetWAR.execute();
   1478                 }
   1479             }
   1480         }
   1481 
   1482         void fling() {
   1483             mInvokeFling.execute();
   1484         }
   1485 
   1486         void move() {
   1487             mInvokeMove.execute();
   1488         }
   1489 
   1490         void moveTo(float row) {
   1491             mState.targetPos = row;
   1492             mState.save();
   1493             mInvokeMoveTo.execute();
   1494         }
   1495 
   1496         /**
   1497          * You need to call save() on mState on your own after calling this.
   1498          *
   1499          * @return the index of the icon that was selected.
   1500          */
   1501         int selectIcon(int x, int y, int pressed) {
   1502             if (mAllApps != null) {
   1503                 final int index = mAllApps.chooseTappedIcon(x, y);
   1504                 selectIcon(index, pressed);
   1505                 return index;
   1506             } else {
   1507                 return -1;
   1508             }
   1509         }
   1510 
   1511         /**
   1512          * Select the icon at the given index.
   1513          *
   1514          * @param index The index.
   1515          * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
   1516          */
   1517         void selectIcon(int index, int pressed) {
   1518             final ArrayList<ApplicationInfo> appsList = mAllApps.mAllAppsList;
   1519             if (appsList == null || index < 0 || index >= appsList.size()) {
   1520                 if (mAllApps != null) {
   1521                     mAllApps.mRestoreFocusIndex = index;
   1522                 }
   1523                 mState.selectedIconIndex = -1;
   1524                 if (mAllApps.mLastSelection == SELECTION_ICONS) {
   1525                     mAllApps.mLastSelection = SELECTION_NONE;
   1526                 }
   1527             } else {
   1528                 if (pressed == SELECTED_FOCUSED) {
   1529                     mAllApps.mLastSelection = SELECTION_ICONS;
   1530                 }
   1531 
   1532                 int prev = mState.selectedIconIndex;
   1533                 mState.selectedIconIndex = index;
   1534 
   1535                 ApplicationInfo info = appsList.get(index);
   1536                 Bitmap selectionBitmap = mSelectionBitmap;
   1537 
   1538                 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
   1539                         selectionBitmap.getWidth(), selectionBitmap.getHeight(),
   1540                         pressed == SELECTED_PRESSED, info.iconBitmap);
   1541 
   1542                 mSelectedIcon = Allocation.createFromBitmap(sRS, selectionBitmap,
   1543                         Element.RGBA_8888(sRS), false);
   1544                 mSelectedIcon.uploadToTexture(0);
   1545                 mState.selectedIconTexture = mSelectedIcon.getID();
   1546 
   1547                 if (prev != index) {
   1548                     if (info.title != null && info.title.length() > 0) {
   1549                         //setContentDescription(info.title);
   1550                         mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
   1551                     }
   1552                 }
   1553             }
   1554         }
   1555 
   1556         /**
   1557          * You need to call save() on mState on your own after calling this.
   1558          */
   1559         void clearSelectedIcon() {
   1560             mState.selectedIconIndex = -1;
   1561         }
   1562 
   1563         void setHomeSelected(int mode) {
   1564             final int prev = mAllApps.mLastSelection;
   1565             switch (mode) {
   1566             case SELECTED_NONE:
   1567                 mState.homeButtonId = mHomeButtonNormal.getID();
   1568                 break;
   1569             case SELECTED_FOCUSED:
   1570                 mAllApps.mLastSelection = SELECTION_HOME;
   1571                 mState.homeButtonId = mHomeButtonFocused.getID();
   1572                 if (prev != SELECTION_HOME) {
   1573                     mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
   1574                 }
   1575                 break;
   1576             case SELECTED_PRESSED:
   1577                 mState.homeButtonId = mHomeButtonPressed.getID();
   1578                 break;
   1579             }
   1580         }
   1581 
   1582         public void dumpState() {
   1583             Log.d(TAG, "sRollo.mWidth=" + mWidth);
   1584             Log.d(TAG, "sRollo.mHeight=" + mHeight);
   1585             Log.d(TAG, "sRollo.mIcons=" + Arrays.toString(mIcons));
   1586             if (mIcons != null) {
   1587                 Log.d(TAG, "sRollo.mIcons.length=" + mIcons.length);
   1588             }
   1589             if (mIconIds != null) {
   1590                 Log.d(TAG, "sRollo.mIconIds.length=" + mIconIds.length);
   1591             }
   1592             Log.d(TAG, "sRollo.mIconIds=" +  Arrays.toString(mIconIds));
   1593             if (mLabelIds != null) {
   1594                 Log.d(TAG, "sRollo.mLabelIds.length=" + mLabelIds.length);
   1595             }
   1596             Log.d(TAG, "sRollo.mLabelIds=" +  Arrays.toString(mLabelIds));
   1597             Log.d(TAG, "sRollo.mState.newPositionX=" + mState.newPositionX);
   1598             Log.d(TAG, "sRollo.mState.newTouchDown=" + mState.newTouchDown);
   1599             Log.d(TAG, "sRollo.mState.flingVelocity=" + mState.flingVelocity);
   1600             Log.d(TAG, "sRollo.mState.iconCount=" + mState.iconCount);
   1601             Log.d(TAG, "sRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
   1602             Log.d(TAG, "sRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
   1603             Log.d(TAG, "sRollo.mState.zoomTarget=" + mState.zoomTarget);
   1604             Log.d(TAG, "sRollo.mState.homeButtonId=" + mState.homeButtonId);
   1605             Log.d(TAG, "sRollo.mState.targetPos=" + mState.targetPos);
   1606             Log.d(TAG, "sRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
   1607             Log.d(TAG, "sRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
   1608             Log.d(TAG, "sRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
   1609             Log.d(TAG, "sRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
   1610             Log.d(TAG, "sRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
   1611             Log.d(TAG, "sRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
   1612             Log.d(TAG, "sRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
   1613             Log.d(TAG, "sRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
   1614         }
   1615     }
   1616 
   1617     public void dumpState() {
   1618         Log.d(TAG, "sRS=" + sRS);
   1619         Log.d(TAG, "sRollo=" + sRollo);
   1620         ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
   1621         Log.d(TAG, "mTouchXBorders=" +  Arrays.toString(mTouchXBorders));
   1622         Log.d(TAG, "mTouchYBorders=" +  Arrays.toString(mTouchYBorders));
   1623         Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
   1624         Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
   1625         Log.d(TAG, "mLastSelection=" + mLastSelection);
   1626         Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
   1627         Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
   1628         Log.d(TAG, "mTouchTracking=" + mTouchTracking);
   1629         Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
   1630         Log.d(TAG, "sZoomDirty=" + sZoomDirty);
   1631         Log.d(TAG, "sAnimateNextZoom=" + sAnimateNextZoom);
   1632         Log.d(TAG, "mZoom=" + mZoom);
   1633         Log.d(TAG, "mScrollPos=" + sRollo.mScrollPos);
   1634         Log.d(TAG, "mVelocity=" + mVelocity);
   1635         Log.d(TAG, "mMessageProc=" + mMessageProc);
   1636         if (sRollo != null) {
   1637             sRollo.dumpState();
   1638         }
   1639         if (sRS != null) {
   1640             sRS.contextDump(0);
   1641         }
   1642     }
   1643 }
   1644 
   1645 
   1646