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