• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
AllApps3D(Context context, AttributeSet attrs)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
onWindowFocusChanged(boolean hasWindowFocus)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
onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)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 
gainFocus()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
onKeyDown(int keyCode, KeyEvent event)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         // We don't accept long click events in these cases
730         // - If the workspace isn't ready to accept a drop
731         // - If we're not done loading (because we might be confused about which item
732         //   to pick up
733         // - If we're not visible
734         if (!isVisible() || mLauncher.isWorkspaceLocked() || mLocks != 0) {
735             return true;
736         }
737         if (sRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
738                 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
739             ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
740 
741             Bitmap bmp = app.iconBitmap;
742             final int w = bmp.getWidth();
743             final int h = bmp.getHeight();
744 
745             // We don't really have an accurate location to use.  This will do.
746             int screenX = mMotionDownRawX - (w / 2);
747             int screenY = mMotionDownRawY - h;
748 
749             mDragController.startDrag(bmp, screenX, screenY,
750                     0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
751 
752             mLauncher.closeAllApps(true);
753         }
754         return true;
755     }
756 
757     @Override
758     public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
759         if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
760             if (!isVisible()) {
761                 return false;
762             }
763             String text = null;
764             int index;
765             int count = mAllAppsList.size() + 1; // +1 is home
766             int pos = -1;
767             switch (mLastSelection) {
768             case SELECTION_ICONS:
769                 index = sRollo.mState.selectedIconIndex;
770                 if (index >= 0) {
771                     ApplicationInfo info = mAllAppsList.get(index);
772                     if (info.title != null) {
773                         text = info.title.toString();
774                         pos = index;
775                     }
776                 }
777                 break;
778             case SELECTION_HOME:
779                 text = getContext().getString(R.string.all_apps_home_button_label);
780                 pos = count;
781                 break;
782             }
783             if (text != null) {
784                 event.setEnabled(true);
785                 event.getText().add(text);
786                 //event.setContentDescription(text);
787                 event.setItemCount(count);
788                 event.setCurrentItemIndex(pos);
789             }
790         }
791         return false;
792     }
793 
794     public void setDragController(DragController dragger) {
795         mDragController = dragger;
796     }
797 
798     public void onDropCompleted(View target, boolean success) {
799     }
800 
801     /**
802      * Zoom to the specifed level.
803      *
804      * @param zoom [0..1] 0 is hidden, 1 is open
805      */
806     public void zoom(float zoom, boolean animate) {
807         cancelLongPress();
808         sNextZoom = zoom;
809         sAnimateNextZoom = animate;
810         // if we do setZoom while we don't have a surface, we won't
811         // get the callbacks that actually set mZoom.
812         if (sRollo == null || !mHaveSurface) {
813             sZoomDirty = true;
814             mZoom = zoom;
815         } else {
816             sRollo.setZoom(zoom, animate);
817         }
818     }
819 
820     /**
821      * If sRollo is null, then we're not visible.  This is also used to guard against
822      * sRollo being null.
823      */
824     public boolean isVisible() {
825         return sRollo != null && mZoom > 0.001f;
826     }
827 
828     public boolean isOpaque() {
829         return mZoom > 0.999f;
830     }
831 
832     public void setApps(ArrayList<ApplicationInfo> list) {
833         if (sRS == null) {
834             // We've been removed from the window.  Don't bother with all this.
835             return;
836         }
837 
838         if (list != null) {
839             Collections.sort(list, LauncherModel.APP_NAME_COMPARATOR);
840         }
841 
842         boolean reload = false;
843         if (mAllAppsList == null) {
844             reload = true;
845         } else if (list.size() != mAllAppsList.size()) {
846             reload = true;
847         } else {
848             final int count = list.size();
849             for (int i = 0; i < count; i++) {
850                 if (list.get(i) != mAllAppsList.get(i)) {
851                     reload = true;
852                     break;
853                 }
854             }
855         }
856 
857         mAllAppsList = list;
858         if (sRollo != null && reload) {
859             sRollo.setApps(list);
860         }
861 
862         if (hasFocus() && mRestoreFocusIndex != -1) {
863             sRollo.selectIcon(mRestoreFocusIndex, SELECTED_FOCUSED);
864             sRollo.mState.save();
865             mRestoreFocusIndex = -1;
866         }
867 
868         mLocks &= ~LOCK_ICONS_PENDING;
869     }
870 
871     public void addApps(ArrayList<ApplicationInfo> list) {
872         if (mAllAppsList == null) {
873             // Not done loading yet.  We'll find out about it later.
874             return;
875         }
876         if (sRS == null) {
877             // We've been removed from the window.  Don't bother with all this.
878             return;
879         }
880 
881         final int N = list.size();
882         if (sRollo != null) {
883             sRollo.pause();
884             sRollo.reallocAppsList(sRollo.mState.iconCount + N);
885         }
886 
887         for (int i=0; i<N; i++) {
888             final ApplicationInfo item = list.get(i);
889             int index = Collections.binarySearch(mAllAppsList, item,
890                     LauncherModel.APP_NAME_COMPARATOR);
891             if (index < 0) {
892                 index = -(index+1);
893             }
894             mAllAppsList.add(index, item);
895             if (sRollo != null) {
896                 sRollo.addApp(index, item);
897             }
898         }
899 
900         if (sRollo != null) {
901             sRollo.saveAppsList();
902             sRollo.resume();
903         }
904     }
905 
906     public void removeApps(ArrayList<ApplicationInfo> list) {
907         if (mAllAppsList == null) {
908             // Not done loading yet.  We'll find out about it later.
909             return;
910         }
911 
912         if (sRollo != null) {
913             sRollo.pause();
914         }
915         final int N = list.size();
916         for (int i=0; i<N; i++) {
917             final ApplicationInfo item = list.get(i);
918             int index = findAppByComponent(mAllAppsList, item);
919             if (index >= 0) {
920                 mAllAppsList.remove(index);
921                 if (sRollo != null) {
922                     sRollo.removeApp(index);
923                 }
924             } else {
925                 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
926                 // Try to recover.  This should keep us from crashing for now.
927             }
928         }
929 
930         if (sRollo != null) {
931             sRollo.saveAppsList();
932             sRollo.resume();
933         }
934     }
935 
936     public void updateApps(ArrayList<ApplicationInfo> list) {
937         // Just remove and add, because they may need to be re-sorted.
938         removeApps(list);
939         addApps(list);
940     }
941 
942     private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
943         ComponentName component = item.intent.getComponent();
944         final int N = list.size();
945         for (int i=0; i<N; i++) {
946             ApplicationInfo x = list.get(i);
947             if (x.intent.getComponent().equals(component)) {
948                 return i;
949             }
950         }
951         return -1;
952     }
953 
954     /*
955     private static int countPages(int iconCount) {
956         int iconsPerPage = getColumnsCount() * Defines.ROWS_PER_PAGE_PORTRAIT;
957         int pages = iconCount / iconsPerPage;
958         if (pages*iconsPerPage != iconCount) {
959             pages++;
960         }
961         return pages;
962     }
963     */
964 
965     class AAMessage extends RenderScript.RSMessage {
966         public void run() {
967             sRollo.mScrollPos = ((float)mData[0]) / (1 << 16);
968             mVelocity = ((float)mData[1]) / (1 << 16);
969 
970             boolean lastVisible = isVisible();
971             mZoom = ((float)mData[2]) / (1 << 16);
972 
973             final boolean visible = isVisible();
974             if (visible != lastVisible) {
975                 post(new Runnable() {
976                     public void run() {
977                         if (visible) {
978                             showSurface();
979                         } else {
980                             hideSurface();
981                         }
982                     }
983                 });
984             }
985 
986             sZoomDirty = false;
987         }
988     }
989 
990     public static class RolloRS {
991         // Allocations ======
992         private int mWidth;
993         private int mHeight;
994 
995         private Resources mRes;
996         private Script mScript;
997         private Script.Invokable mInvokeMove;
998         private Script.Invokable mInvokeMoveTo;
999         private Script.Invokable mInvokeFling;
1000         private Script.Invokable mInvokeResetWAR;
1001         private Script.Invokable mInvokeSetZoom;
1002 
1003         private ProgramStore mPSIcons;
1004         private ProgramFragment mPFTexMip;
1005         private ProgramFragment mPFTexMipAlpha;
1006         private ProgramFragment mPFTexNearest;
1007         private ProgramVertex mPV;
1008         private ProgramVertex mPVCurve;
1009         private SimpleMesh mMesh;
1010         private ProgramVertex.MatrixAllocation mPVA;
1011 
1012         private Allocation mUniformAlloc;
1013 
1014         private Allocation mHomeButtonNormal;
1015         private Allocation mHomeButtonFocused;
1016         private Allocation mHomeButtonPressed;
1017 
1018         private Allocation[] mIcons;
1019         private int[] mIconIds;
1020         private Allocation mAllocIconIds;
1021 
1022         private Allocation[] mLabels;
1023         private int[] mLabelIds;
1024         private Allocation mAllocLabelIds;
1025         private Allocation mSelectedIcon;
1026 
1027         private Bitmap mSelectionBitmap;
1028         private Canvas mSelectionCanvas;
1029 
1030         private float mScrollPos;
1031 
1032         Params mParams;
1033         State mState;
1034 
1035         AllApps3D mAllApps;
1036         boolean mInitialize;
1037 
1038         class BaseAlloc {
1039             Allocation mAlloc;
1040             Type mType;
1041 
1042             void save() {
1043                 mAlloc.data(this);
1044             }
1045         }
1046 
1047         private boolean checkClickOK() {
1048             return (Math.abs(mAllApps.mVelocity) < 0.4f) &&
1049                    (Math.abs(mScrollPos - Math.round(mScrollPos)) < 0.4f);
1050         }
1051 
1052         void pause() {
1053             if (sRS != null) {
1054                 sRS.contextBindRootScript(null);
1055             }
1056         }
1057 
1058         void resume() {
1059             if (sRS != null) {
1060                 sRS.contextBindRootScript(mScript);
1061             }
1062         }
1063 
1064         class Params extends BaseAlloc {
1065             Params() {
1066                 mType = Type.createFromClass(sRS, Params.class, 1, "ParamsClass");
1067                 mAlloc = Allocation.createTyped(sRS, mType);
1068                 save();
1069             }
1070             public int bubbleWidth;
1071             public int bubbleHeight;
1072             public int bubbleBitmapWidth;
1073             public int bubbleBitmapHeight;
1074 
1075             public int homeButtonWidth;
1076             public int homeButtonHeight;
1077             public int homeButtonTextureWidth;
1078             public int homeButtonTextureHeight;
1079         }
1080 
1081         class State extends BaseAlloc {
1082             public float newPositionX;
1083             public int newTouchDown;
1084             public float flingVelocity;
1085             public int iconCount;
1086             public int selectedIconIndex = -1;
1087             public int selectedIconTexture;
1088             public float zoomTarget;
1089             public int homeButtonId;
1090             public float targetPos;
1091 
1092             State() {
1093                 mType = Type.createFromClass(sRS, State.class, 1, "StateClass");
1094                 mAlloc = Allocation.createTyped(sRS, mType);
1095                 save();
1096             }
1097         }
1098 
1099         public RolloRS(AllApps3D allApps) {
1100             mAllApps = allApps;
1101         }
1102 
1103         public void init(Resources res, int width, int height) {
1104             mRes = res;
1105             mWidth = width;
1106             mHeight = height;
1107             initProgramVertex();
1108             initProgramFragment();
1109             initProgramStore();
1110             initGl();
1111             initData();
1112             initRs();
1113         }
1114 
1115         public void initMesh() {
1116             SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(sRS, 2, 0);
1117 
1118             for (int ct=0; ct < 16; ct++) {
1119                 float pos = (1.f / (16.f - 1)) * ct;
1120                 tm.addVertex(0.0f, pos);
1121                 tm.addVertex(1.0f, pos);
1122             }
1123             for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
1124                 tm.addTriangle(ct, ct+1, ct+2);
1125                 tm.addTriangle(ct+1, ct+3, ct+2);
1126             }
1127             mMesh = tm.create();
1128             mMesh.setName("SMCell");
1129         }
1130 
1131         void resize(int w, int h) {
1132             mPVA.setupProjectionNormalized(w, h);
1133             mWidth = w;
1134             mHeight = h;
1135         }
1136 
1137         private void initProgramVertex() {
1138             mPVA = new ProgramVertex.MatrixAllocation(sRS);
1139             resize(mWidth, mHeight);
1140 
1141             ProgramVertex.Builder pvb = new ProgramVertex.Builder(sRS, null, null);
1142             pvb.setTextureMatrixEnable(true);
1143             mPV = pvb.create();
1144             mPV.setName("PV");
1145             mPV.bindAllocation(mPVA);
1146 
1147             Element.Builder eb = new Element.Builder(sRS);
1148             eb.add(Element.createVector(sRS, Element.DataType.FLOAT_32, 2), "ImgSize");
1149             eb.add(Element.createVector(sRS, Element.DataType.FLOAT_32, 4), "Position");
1150             eb.add(Element.createVector(sRS, Element.DataType.FLOAT_32, 2), "BendPos");
1151             eb.add(Element.createVector(sRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
1152             Element e = eb.create();
1153 
1154             mUniformAlloc = Allocation.createSized(sRS, e, 1);
1155 
1156             initMesh();
1157             ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(sRS);
1158             String t = "void main() {\n" +
1159                     // Animation
1160                     "  float ani = UNI_Position.z;\n" +
1161 
1162                     "  float bendY1 = UNI_BendPos.x;\n" +
1163                     "  float bendY2 = UNI_BendPos.y;\n" +
1164                     "  float bendAngle = 47.0 * (3.14 / 180.0);\n" +
1165                     "  float bendDistance = bendY1 * 0.4;\n" +
1166                     "  float distanceDimLevel = 0.6;\n" +
1167 
1168                     "  float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
1169                     "  float aDy = cos(bendAngle);\n" +
1170                     "  float aDz = sin(bendAngle);\n" +
1171 
1172                     "  float scale = (2.0 / 480.0);\n" +
1173                     "  float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
1174                     "  float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
1175                     "  float y = 0.0;\n" +
1176                     "  float z = 0.0;\n" +
1177                     "  float lum = 1.0;\n" +
1178 
1179                     "  float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
1180                     "  y += cv * aDy;\n" +
1181                     "  z += -cv * aDz;\n" +
1182                     "  cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" +  // curve range
1183                     "  lum += cv / bendDistance * distanceDimLevel;\n" +
1184                     "  y += cv * cos(cv * bendStep);\n" +
1185                     "  z += cv * sin(cv * bendStep);\n" +
1186 
1187                     "  cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
1188                     "  y += cv * aDy;\n" +
1189                     "  z += cv * aDz;\n" +
1190                     "  cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
1191                     "  lum -= cv / bendDistance * distanceDimLevel;\n" +
1192                     "  y += cv * cos(cv * bendStep);\n" +
1193                     "  z += cv * sin(cv * bendStep);\n" +
1194 
1195                     "  y += clamp(ys, bendY1, bendY2);\n" +
1196 
1197                     "  vec4 pos;\n" +
1198                     "  pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
1199                     "  pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
1200                     "  pos.z = z * UNI_ScaleOffset.x;\n" +
1201                     "  pos.w = 1.0;\n" +
1202 
1203                     "  pos.x *= 1.0 + ani * 4.0;\n" +
1204                     "  pos.y *= 1.0 + ani * 4.0;\n" +
1205                     "  pos.z -= ani * 1.5;\n" +
1206                     "  lum *= 1.0 - ani;\n" +
1207 
1208                     "  gl_Position = UNI_MVP * pos;\n" +
1209                     "  varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
1210                     "  varTex0.xy = ATTRIB_position;\n" +
1211                     "  varTex0.y = 1.0 - varTex0.y;\n" +
1212                     "  varTex0.zw = vec2(0.0, 0.0);\n" +
1213                     "}\n";
1214             sb.setShader(t);
1215             sb.addConstant(mUniformAlloc.getType());
1216             sb.addInput(mMesh.getVertexType(0).getElement());
1217             mPVCurve = sb.create();
1218             mPVCurve.setName("PVCurve");
1219             mPVCurve.bindAllocation(mPVA);
1220             mPVCurve.bindConstants(mUniformAlloc, 1);
1221 
1222             sRS.contextBindProgramVertex(mPV);
1223         }
1224 
1225         private void initProgramFragment() {
1226             Sampler.Builder sb = new Sampler.Builder(sRS);
1227             sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
1228             sb.setMag(Sampler.Value.NEAREST);
1229             sb.setWrapS(Sampler.Value.CLAMP);
1230             sb.setWrapT(Sampler.Value.CLAMP);
1231             Sampler linear = sb.create();
1232 
1233             sb.setMin(Sampler.Value.NEAREST);
1234             sb.setMag(Sampler.Value.NEAREST);
1235             Sampler nearest = sb.create();
1236 
1237             ProgramFragment.Builder bf = new ProgramFragment.Builder(sRS);
1238             bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1239                           ProgramFragment.Builder.Format.RGBA, 0);
1240             mPFTexMip = bf.create();
1241             mPFTexMip.setName("PFTexMip");
1242             mPFTexMip.bindSampler(linear, 0);
1243 
1244             mPFTexNearest = bf.create();
1245             mPFTexNearest.setName("PFTexNearest");
1246             mPFTexNearest.bindSampler(nearest, 0);
1247 
1248             bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1249                           ProgramFragment.Builder.Format.ALPHA, 0);
1250             mPFTexMipAlpha = bf.create();
1251             mPFTexMipAlpha.setName("PFTexMipAlpha");
1252             mPFTexMipAlpha.bindSampler(linear, 0);
1253 
1254         }
1255 
1256         private void initProgramStore() {
1257             ProgramStore.Builder bs = new ProgramStore.Builder(sRS, null, null);
1258             bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
1259             bs.setColorMask(true,true,true,false);
1260             bs.setDitherEnable(true);
1261             bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1262                             ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1263             mPSIcons = bs.create();
1264             mPSIcons.setName("PSIcons");
1265         }
1266 
1267         private void initGl() {
1268         }
1269 
1270         private void initData() {
1271             mParams = new Params();
1272             mState = new State();
1273 
1274             final Utilities.BubbleText bubble = new Utilities.BubbleText(mAllApps.getContext());
1275 
1276             mParams.bubbleWidth = bubble.getBubbleWidth();
1277             mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1278             mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1279             mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
1280 
1281             mHomeButtonNormal = Allocation.createFromBitmapResource(sRS, mRes,
1282                     R.drawable.home_button_normal, Element.RGBA_8888(sRS), false);
1283             mHomeButtonNormal.uploadToTexture(0);
1284             mHomeButtonFocused = Allocation.createFromBitmapResource(sRS, mRes,
1285                     R.drawable.home_button_focused, Element.RGBA_8888(sRS), false);
1286             mHomeButtonFocused.uploadToTexture(0);
1287             mHomeButtonPressed = Allocation.createFromBitmapResource(sRS, mRes,
1288                     R.drawable.home_button_pressed, Element.RGBA_8888(sRS), false);
1289             mHomeButtonPressed.uploadToTexture(0);
1290             mParams.homeButtonWidth = 76;
1291             mParams.homeButtonHeight = 68;
1292             mParams.homeButtonTextureWidth = 128;
1293             mParams.homeButtonTextureHeight = 128;
1294 
1295             mState.homeButtonId = mHomeButtonNormal.getID();
1296 
1297             mParams.save();
1298             mState.save();
1299 
1300             mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1301                     Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1302             mSelectionCanvas = new Canvas(mSelectionBitmap);
1303 
1304             setApps(null);
1305         }
1306 
1307         private void initRs() {
1308             ScriptC.Builder sb = new ScriptC.Builder(sRS);
1309             sb.setScript(mRes, R.raw.allapps);
1310             sb.setRoot(true);
1311             sb.addDefines(mAllApps.mDefines);
1312             sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1313             sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
1314             sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
1315             mInvokeMove = sb.addInvokable("move");
1316             mInvokeFling = sb.addInvokable("fling");
1317             mInvokeMoveTo = sb.addInvokable("moveTo");
1318             mInvokeResetWAR = sb.addInvokable("resetHWWar");
1319             mInvokeSetZoom = sb.addInvokable("setZoom");
1320             mScript = sb.create();
1321             mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1322             mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1323             mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1324             mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1325             mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
1326             mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
1327 
1328             sRS.contextBindRootScript(mScript);
1329         }
1330 
1331         void dirtyCheck() {
1332             if (sZoomDirty) {
1333                 setZoom(mAllApps.sNextZoom, mAllApps.sAnimateNextZoom);
1334             }
1335         }
1336 
1337         @SuppressWarnings({"ConstantConditions"})
1338         private void setApps(ArrayList<ApplicationInfo> list) {
1339             sRollo.pause();
1340             final int count = list != null ? list.size() : 0;
1341             int allocCount = count;
1342             if (allocCount < 1) {
1343                 allocCount = 1;
1344             }
1345 
1346             mIcons = new Allocation[count];
1347             mIconIds = new int[allocCount];
1348             mAllocIconIds = Allocation.createSized(sRS, Element.USER_I32(sRS), allocCount);
1349 
1350             mLabels = new Allocation[count];
1351             mLabelIds = new int[allocCount];
1352             mAllocLabelIds = Allocation.createSized(sRS, Element.USER_I32(sRS), allocCount);
1353 
1354             mState.iconCount = count;
1355             for (int i=0; i < mState.iconCount; i++) {
1356                 createAppIconAllocations(i, list.get(i));
1357             }
1358             for (int i=0; i < mState.iconCount; i++) {
1359                 uploadAppIcon(i, list.get(i));
1360             }
1361             saveAppsList();
1362             sRollo.resume();
1363         }
1364 
1365         private void setZoom(float zoom, boolean animate) {
1366             if (animate) {
1367                 sRollo.clearSelectedIcon();
1368                 sRollo.setHomeSelected(SELECTED_NONE);
1369             }
1370             if (zoom > 0.001f) {
1371                 sRollo.mState.zoomTarget = zoom;
1372             } else {
1373                 sRollo.mState.zoomTarget = 0;
1374             }
1375             sRollo.mState.save();
1376             if (!animate) {
1377                 sRollo.mInvokeSetZoom.execute();
1378             }
1379         }
1380 
1381         private void createAppIconAllocations(int index, ApplicationInfo item) {
1382             mIcons[index] = Allocation.createFromBitmap(sRS, item.iconBitmap,
1383                     Element.RGBA_8888(sRS), false);
1384             mLabels[index] = Allocation.createFromBitmap(sRS, item.titleBitmap,
1385                     Element.A_8(sRS), false);
1386             mIconIds[index] = mIcons[index].getID();
1387             mLabelIds[index] = mLabels[index].getID();
1388         }
1389 
1390         private void uploadAppIcon(int index, ApplicationInfo item) {
1391             if (mIconIds[index] != mIcons[index].getID()) {
1392                 throw new IllegalStateException("uploadAppIcon index=" + index
1393                     + " mIcons[index].getID=" + mIcons[index].getID()
1394                     + " mIconsIds[index]=" + mIconIds[index]
1395                     + " item=" + item);
1396             }
1397             mIcons[index].uploadToTexture(true, 0);
1398             mLabels[index].uploadToTexture(true, 0);
1399         }
1400 
1401         /**
1402          * Puts the empty spaces at the end.  Updates mState.iconCount.  You must
1403          * fill in the values and call saveAppsList().
1404          */
1405         private void reallocAppsList(int count) {
1406             Allocation[] icons = new Allocation[count];
1407             int[] iconIds = new int[count];
1408             mAllocIconIds = Allocation.createSized(sRS, Element.USER_I32(sRS), count);
1409 
1410             Allocation[] labels = new Allocation[count];
1411             int[] labelIds = new int[count];
1412             mAllocLabelIds = Allocation.createSized(sRS, Element.USER_I32(sRS), count);
1413 
1414             final int oldCount = sRollo.mState.iconCount;
1415 
1416             System.arraycopy(mIcons, 0, icons, 0, oldCount);
1417             System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1418             System.arraycopy(mLabels, 0, labels, 0, oldCount);
1419             System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1420 
1421             mIcons = icons;
1422             mIconIds = iconIds;
1423             mLabels = labels;
1424             mLabelIds = labelIds;
1425         }
1426 
1427         /**
1428          * Handle the allocations for the new app.  Make sure you call saveAppsList when done.
1429          */
1430         private void addApp(int index, ApplicationInfo item) {
1431             final int count = mState.iconCount - index;
1432             final int dest = index + 1;
1433 
1434             System.arraycopy(mIcons, index, mIcons, dest, count);
1435             System.arraycopy(mIconIds, index, mIconIds, dest, count);
1436             System.arraycopy(mLabels, index, mLabels, dest, count);
1437             System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1438 
1439             createAppIconAllocations(index, item);
1440             uploadAppIcon(index, item);
1441             sRollo.mState.iconCount++;
1442         }
1443 
1444         /**
1445          * Handle the allocations for the removed app.  Make sure you call saveAppsList when done.
1446          */
1447         private void removeApp(int index) {
1448             final int count = mState.iconCount - index - 1;
1449             final int src = index + 1;
1450 
1451             System.arraycopy(mIcons, src, mIcons, index, count);
1452             System.arraycopy(mIconIds, src, mIconIds, index, count);
1453             System.arraycopy(mLabels, src, mLabels, index, count);
1454             System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1455 
1456             sRollo.mState.iconCount--;
1457             final int last = mState.iconCount;
1458 
1459             mIcons[last] = null;
1460             mIconIds[last] = 0;
1461             mLabels[last] = null;
1462             mLabelIds[last] = 0;
1463         }
1464 
1465         /**
1466          * Send the apps list structures to RS.
1467          */
1468         private void saveAppsList() {
1469             // WTF: how could mScript be not null but mAllocIconIds null b/2460740.
1470             if (mScript != null && mAllocIconIds != null) {
1471                 mAllocIconIds.data(mIconIds);
1472                 mAllocLabelIds.data(mLabelIds);
1473 
1474                 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1475                 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
1476 
1477                 mState.save();
1478 
1479                 // Note: mScript may be null if we haven't initialized it yet.
1480                 // In that case, this is a no-op.
1481                 if (mInvokeResetWAR != null) {
1482                     mInvokeResetWAR.execute();
1483                 }
1484             }
1485         }
1486 
1487         void fling() {
1488             mInvokeFling.execute();
1489         }
1490 
1491         void move() {
1492             mInvokeMove.execute();
1493         }
1494 
1495         void moveTo(float row) {
1496             mState.targetPos = row;
1497             mState.save();
1498             mInvokeMoveTo.execute();
1499         }
1500 
1501         /**
1502          * You need to call save() on mState on your own after calling this.
1503          *
1504          * @return the index of the icon that was selected.
1505          */
1506         int selectIcon(int x, int y, int pressed) {
1507             if (mAllApps != null) {
1508                 final int index = mAllApps.chooseTappedIcon(x, y);
1509                 selectIcon(index, pressed);
1510                 return index;
1511             } else {
1512                 return -1;
1513             }
1514         }
1515 
1516         /**
1517          * Select the icon at the given index.
1518          *
1519          * @param index The index.
1520          * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
1521          */
1522         void selectIcon(int index, int pressed) {
1523             final ArrayList<ApplicationInfo> appsList = mAllApps.mAllAppsList;
1524             if (appsList == null || index < 0 || index >= appsList.size()) {
1525                 if (mAllApps != null) {
1526                     mAllApps.mRestoreFocusIndex = index;
1527                 }
1528                 mState.selectedIconIndex = -1;
1529                 if (mAllApps.mLastSelection == SELECTION_ICONS) {
1530                     mAllApps.mLastSelection = SELECTION_NONE;
1531                 }
1532             } else {
1533                 if (pressed == SELECTED_FOCUSED) {
1534                     mAllApps.mLastSelection = SELECTION_ICONS;
1535                 }
1536 
1537                 int prev = mState.selectedIconIndex;
1538                 mState.selectedIconIndex = index;
1539 
1540                 ApplicationInfo info = appsList.get(index);
1541                 Bitmap selectionBitmap = mSelectionBitmap;
1542 
1543                 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1544                         selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1545                         pressed == SELECTED_PRESSED, info.iconBitmap);
1546 
1547                 mSelectedIcon = Allocation.createFromBitmap(sRS, selectionBitmap,
1548                         Element.RGBA_8888(sRS), false);
1549                 mSelectedIcon.uploadToTexture(0);
1550                 mState.selectedIconTexture = mSelectedIcon.getID();
1551 
1552                 if (prev != index) {
1553                     if (info.title != null && info.title.length() > 0) {
1554                         //setContentDescription(info.title);
1555                         mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1556                     }
1557                 }
1558             }
1559         }
1560 
1561         /**
1562          * You need to call save() on mState on your own after calling this.
1563          */
1564         void clearSelectedIcon() {
1565             mState.selectedIconIndex = -1;
1566         }
1567 
1568         void setHomeSelected(int mode) {
1569             final int prev = mAllApps.mLastSelection;
1570             switch (mode) {
1571             case SELECTED_NONE:
1572                 mState.homeButtonId = mHomeButtonNormal.getID();
1573                 break;
1574             case SELECTED_FOCUSED:
1575                 mAllApps.mLastSelection = SELECTION_HOME;
1576                 mState.homeButtonId = mHomeButtonFocused.getID();
1577                 if (prev != SELECTION_HOME) {
1578                     mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1579                 }
1580                 break;
1581             case SELECTED_PRESSED:
1582                 mState.homeButtonId = mHomeButtonPressed.getID();
1583                 break;
1584             }
1585         }
1586 
1587         public void dumpState() {
1588             Log.d(TAG, "sRollo.mWidth=" + mWidth);
1589             Log.d(TAG, "sRollo.mHeight=" + mHeight);
1590             Log.d(TAG, "sRollo.mIcons=" + Arrays.toString(mIcons));
1591             if (mIcons != null) {
1592                 Log.d(TAG, "sRollo.mIcons.length=" + mIcons.length);
1593             }
1594             if (mIconIds != null) {
1595                 Log.d(TAG, "sRollo.mIconIds.length=" + mIconIds.length);
1596             }
1597             Log.d(TAG, "sRollo.mIconIds=" +  Arrays.toString(mIconIds));
1598             if (mLabelIds != null) {
1599                 Log.d(TAG, "sRollo.mLabelIds.length=" + mLabelIds.length);
1600             }
1601             Log.d(TAG, "sRollo.mLabelIds=" +  Arrays.toString(mLabelIds));
1602             Log.d(TAG, "sRollo.mState.newPositionX=" + mState.newPositionX);
1603             Log.d(TAG, "sRollo.mState.newTouchDown=" + mState.newTouchDown);
1604             Log.d(TAG, "sRollo.mState.flingVelocity=" + mState.flingVelocity);
1605             Log.d(TAG, "sRollo.mState.iconCount=" + mState.iconCount);
1606             Log.d(TAG, "sRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1607             Log.d(TAG, "sRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1608             Log.d(TAG, "sRollo.mState.zoomTarget=" + mState.zoomTarget);
1609             Log.d(TAG, "sRollo.mState.homeButtonId=" + mState.homeButtonId);
1610             Log.d(TAG, "sRollo.mState.targetPos=" + mState.targetPos);
1611             Log.d(TAG, "sRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1612             Log.d(TAG, "sRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1613             Log.d(TAG, "sRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1614             Log.d(TAG, "sRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1615             Log.d(TAG, "sRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1616             Log.d(TAG, "sRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1617             Log.d(TAG, "sRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1618             Log.d(TAG, "sRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1619         }
1620     }
1621 
1622     public void dumpState() {
1623         Log.d(TAG, "sRS=" + sRS);
1624         Log.d(TAG, "sRollo=" + sRollo);
1625         ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1626         Log.d(TAG, "mTouchXBorders=" +  Arrays.toString(mTouchXBorders));
1627         Log.d(TAG, "mTouchYBorders=" +  Arrays.toString(mTouchYBorders));
1628         Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1629         Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1630         Log.d(TAG, "mLastSelection=" + mLastSelection);
1631         Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1632         Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1633         Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1634         Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1635         Log.d(TAG, "sZoomDirty=" + sZoomDirty);
1636         Log.d(TAG, "sAnimateNextZoom=" + sAnimateNextZoom);
1637         Log.d(TAG, "mZoom=" + mZoom);
1638         Log.d(TAG, "mScrollPos=" + sRollo.mScrollPos);
1639         Log.d(TAG, "mVelocity=" + mVelocity);
1640         Log.d(TAG, "mMessageProc=" + mMessageProc);
1641         if (sRollo != null) {
1642             sRollo.dumpState();
1643         }
1644         if (sRS != null) {
1645             sRS.contextDump(0);
1646         }
1647     }
1648 }
1649 
1650 
1651