blob: 59bda67c4838af140790ab15cd325184cd02b2cb [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;
Daniel Sandler388f6792010-03-02 14:08:08 -050026import android.renderscript.Allocation;
Daniel Sandler388f6792010-03-02 14:08:08 -050027import android.renderscript.Element;
28import android.renderscript.ProgramFragment;
29import android.renderscript.ProgramStore;
30import android.renderscript.ProgramVertex;
31import android.renderscript.RSSurfaceView;
32import android.renderscript.RenderScriptGL;
33import android.renderscript.RenderScript;
34import android.renderscript.Sampler;
35import android.renderscript.Script;
36import android.renderscript.ScriptC;
37import android.renderscript.SimpleMesh;
38import android.renderscript.Type;
39import android.util.AttributeSet;
Romain Guy060b5c82010-03-04 10:07:38 -080040import android.util.DisplayMetrics;
Daniel Sandler388f6792010-03-02 14:08:08 -050041import android.util.Log;
42import android.view.KeyEvent;
43import android.view.MotionEvent;
44import android.view.SoundEffectConstants;
45import android.view.SurfaceHolder;
46import android.view.VelocityTracker;
47import android.view.View;
48import android.view.ViewConfiguration;
49import android.view.accessibility.AccessibilityEvent;
50
51import java.util.ArrayList;
52import java.util.Arrays;
53import java.util.Collections;
Daniel Sandler388f6792010-03-02 14:08:08 -050054
55public class AllApps3D extends RSSurfaceView
56 implements AllAppsView, View.OnClickListener, View.OnLongClickListener, DragSource {
57 private static final String TAG = "Launcher.AllApps3D";
58
59 /** Bit for mLocks for when there are icons being loaded. */
60 private static final int LOCK_ICONS_PENDING = 1;
61
62 private static final int TRACKING_NONE = 0;
63 private static final int TRACKING_FLING = 1;
64 private static final int TRACKING_HOME = 2;
65
66 private static final int SELECTED_NONE = 0;
67 private static final int SELECTED_FOCUSED = 1;
68 private static final int SELECTED_PRESSED = 2;
69
70 private static final int SELECTION_NONE = 0;
71 private static final int SELECTION_ICONS = 1;
72 private static final int SELECTION_HOME = 2;
73
74 private Launcher mLauncher;
75 private DragController mDragController;
76
77 /** When this is 0, modifications are allowed, when it's not, they're not.
78 * TODO: What about scrolling? */
79 private int mLocks = LOCK_ICONS_PENDING;
80
81 private int mSlop;
82 private int mMaxFlingVelocity;
83
84 private Defines mDefines = new Defines();
85 private RenderScriptGL mRS;
86 private RolloRS mRollo;
87 private ArrayList<ApplicationInfo> mAllAppsList;
88
89 /**
90 * True when we are using arrow keys or trackball to drive navigation
91 */
92 private boolean mArrowNavigation = false;
93 private boolean mStartedScrolling;
94
95 /**
96 * Used to keep track of the selection when AllAppsView loses window focus.
97 * One of the SELECTION_ constants.
98 */
99 private int mLastSelection;
Romain Guy060b5c82010-03-04 10:07:38 -0800100
Daniel Sandler388f6792010-03-02 14:08:08 -0500101 /**
102 * Used to keep track of the selection when AllAppsView loses window focus
103 */
104 private int mLastSelectedIcon;
Romain Guy060b5c82010-03-04 10:07:38 -0800105
Daniel Sandler388f6792010-03-02 14:08:08 -0500106 private VelocityTracker mVelocityTracker;
107 private int mTouchTracking;
108 private int mMotionDownRawX;
109 private int mMotionDownRawY;
110 private int mDownIconIndex = -1;
111 private int mCurrentIconIndex = -1;
Romain Guy060b5c82010-03-04 10:07:38 -0800112
Daniel Sandler388f6792010-03-02 14:08:08 -0500113 private boolean mShouldGainFocus;
Romain Guy060b5c82010-03-04 10:07:38 -0800114
Daniel Sandler388f6792010-03-02 14:08:08 -0500115 private boolean mHaveSurface = false;
116 private boolean mZoomDirty = false;
117 private boolean mAnimateNextZoom;
118 private float mNextZoom;
119 private float mZoom;
120 private float mPosX;
121 private float mVelocity;
122 private AAMessage mMessageProc;
Romain Guy060b5c82010-03-04 10:07:38 -0800123
124 private int mColumnsPerPage;
125 private int mRowsPerPage;
126
127 @SuppressWarnings({"UnusedDeclaration"})
Daniel Sandler388f6792010-03-02 14:08:08 -0500128 static class Defines {
129 public static final int ALLOC_PARAMS = 0;
130 public static final int ALLOC_STATE = 1;
131 public static final int ALLOC_ICON_IDS = 3;
132 public static final int ALLOC_LABEL_IDS = 4;
133 public static final int ALLOC_VP_CONSTANTS = 5;
Romain Guy060b5c82010-03-04 10:07:38 -0800134
135 public static final int COLUMNS_PER_PAGE_PORTRAIT = 4;
136 public static final int ROWS_PER_PAGE_PORTRAIT = 4;
137
138 public static final int COLUMNS_PER_PAGE_LANDSCAPE = 6;
139 public static final int ROWS_PER_PAGE_LANDSCAPE = 3;
140
Daniel Sandler388f6792010-03-02 14:08:08 -0500141 public static final int ICON_WIDTH_PX = 64;
142 public static final int ICON_TEXTURE_WIDTH_PX = 74;
143 public static final int SELECTION_TEXTURE_WIDTH_PX = 74 + 20;
Romain Guy060b5c82010-03-04 10:07:38 -0800144
Daniel Sandler388f6792010-03-02 14:08:08 -0500145 public static final int ICON_HEIGHT_PX = 64;
146 public static final int ICON_TEXTURE_HEIGHT_PX = 74;
147 public static final int SELECTION_TEXTURE_HEIGHT_PX = 74 + 20;
148 }
Romain Guy060b5c82010-03-04 10:07:38 -0800149
Daniel Sandler388f6792010-03-02 14:08:08 -0500150 public AllApps3D(Context context, AttributeSet attrs) {
151 super(context, attrs);
152 setFocusable(true);
153 setSoundEffectsEnabled(false);
154 getHolder().setFormat(PixelFormat.TRANSLUCENT);
155 final ViewConfiguration config = ViewConfiguration.get(context);
156 mSlop = config.getScaledTouchSlop();
157 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
Romain Guy060b5c82010-03-04 10:07:38 -0800158
Daniel Sandler388f6792010-03-02 14:08:08 -0500159 setOnClickListener(this);
160 setOnLongClickListener(this);
161 setZOrderOnTop(true);
162 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Romain Guy060b5c82010-03-04 10:07:38 -0800163
Daniel Sandler388f6792010-03-02 14:08:08 -0500164 mRS = createRenderScript(true);
Romain Guy060b5c82010-03-04 10:07:38 -0800165
166 final DisplayMetrics metrics = getResources().getDisplayMetrics();
167 final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;
168 mColumnsPerPage = isPortrait ? Defines.COLUMNS_PER_PAGE_PORTRAIT :
169 Defines.COLUMNS_PER_PAGE_LANDSCAPE;
170 mRowsPerPage = isPortrait ? Defines.ROWS_PER_PAGE_PORTRAIT :
171 Defines.ROWS_PER_PAGE_LANDSCAPE;
Daniel Sandler388f6792010-03-02 14:08:08 -0500172 }
Romain Guy060b5c82010-03-04 10:07:38 -0800173
174 @SuppressWarnings({"UnusedDeclaration"})
175 public AllApps3D(Context context, AttributeSet attrs, int defStyle) {
176 this(context, attrs);
177 }
178
Daniel Sandler388f6792010-03-02 14:08:08 -0500179 /**
180 * Note that this implementation prohibits this view from ever being reattached.
181 */
182 @Override
183 protected void onDetachedFromWindow() {
184 destroyRenderScript();
185 mRS.mMessageCallback = null;
186 mRS = null;
187 }
Romain Guy060b5c82010-03-04 10:07:38 -0800188
Daniel Sandler388f6792010-03-02 14:08:08 -0500189 /**
190 * If you have an attached click listener, View always plays the click sound!?!?
191 * Deal with sound effects by hand.
192 */
193 public void reallyPlaySoundEffect(int sound) {
194 boolean old = isSoundEffectsEnabled();
195 setSoundEffectsEnabled(true);
196 playSoundEffect(sound);
197 setSoundEffectsEnabled(old);
198 }
Romain Guy060b5c82010-03-04 10:07:38 -0800199
Daniel Sandler388f6792010-03-02 14:08:08 -0500200 public void setLauncher(Launcher launcher) {
201 mLauncher = launcher;
202 }
Romain Guy060b5c82010-03-04 10:07:38 -0800203
Daniel Sandler388f6792010-03-02 14:08:08 -0500204 @Override
205 public void surfaceDestroyed(SurfaceHolder holder) {
206 super.surfaceDestroyed(holder);
207 // Without this, we leak mMessageCallback which leaks the context.
208 mRS.mMessageCallback = null;
209 // We may lose any callbacks that are pending, so make sure that we re-sync that
210 // on the next surfaceChanged.
211 mZoomDirty = true;
212 mHaveSurface = false;
213 }
Romain Guy060b5c82010-03-04 10:07:38 -0800214
Daniel Sandler388f6792010-03-02 14:08:08 -0500215 @Override
216 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
217 //long startTime = SystemClock.uptimeMillis();
Romain Guy060b5c82010-03-04 10:07:38 -0800218
Daniel Sandler388f6792010-03-02 14:08:08 -0500219 super.surfaceChanged(holder, format, w, h);
Romain Guy060b5c82010-03-04 10:07:38 -0800220
Daniel Sandler388f6792010-03-02 14:08:08 -0500221 mHaveSurface = true;
Romain Guy060b5c82010-03-04 10:07:38 -0800222
Daniel Sandler388f6792010-03-02 14:08:08 -0500223 if (mRollo == null) {
224 mRollo = new RolloRS();
225 mRollo.init(getResources(), w, h);
226 if (mAllAppsList != null) {
227 mRollo.setApps(mAllAppsList);
228 }
229 if (mShouldGainFocus) {
230 gainFocus();
231 mShouldGainFocus = false;
232 }
233 }
234 mRollo.dirtyCheck();
235 mRollo.resize(w, h);
Romain Guy060b5c82010-03-04 10:07:38 -0800236
Daniel Sandler388f6792010-03-02 14:08:08 -0500237 if (mRS != null) {
238 mRS.mMessageCallback = mMessageProc = new AAMessage();
239 }
Romain Guy060b5c82010-03-04 10:07:38 -0800240
Daniel Sandler388f6792010-03-02 14:08:08 -0500241 if (mRollo.mUniformAlloc != null) {
242 float tf[] = new float[] {72.f, 72.f,
243 120.f, 120.f, 0.f, 0.f,
244 120.f, 680.f,
245 (2.f / 480.f), 0, -((float)w / 2) - 0.25f, -380.25f};
246 if (w > h) {
247 tf[6] = 40.f;
248 tf[7] = h - 40.f;
249 tf[9] = 1.f;
250 tf[10] = -((float)w / 2) - 0.25f;
251 tf[11] = -((float)h / 2) - 0.25f;
252 }
Romain Guy060b5c82010-03-04 10:07:38 -0800253
Daniel Sandler388f6792010-03-02 14:08:08 -0500254 mRollo.mUniformAlloc.data(tf);
255 }
Romain Guy060b5c82010-03-04 10:07:38 -0800256
Daniel Sandler388f6792010-03-02 14:08:08 -0500257 //long endTime = SystemClock.uptimeMillis();
258 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
259 }
Romain Guy060b5c82010-03-04 10:07:38 -0800260
Daniel Sandler388f6792010-03-02 14:08:08 -0500261 @Override
262 public void onWindowFocusChanged(boolean hasWindowFocus) {
263 super.onWindowFocusChanged(hasWindowFocus);
264 if (mArrowNavigation) {
265 if (!hasWindowFocus) {
266 // Clear selection when we lose window focus
267 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
268 mRollo.setHomeSelected(SELECTED_NONE);
269 mRollo.clearSelectedIcon();
270 mRollo.mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -0800271 } else {
Daniel Sandler388f6792010-03-02 14:08:08 -0500272 if (mRollo.mState.iconCount > 0) {
273 if (mLastSelection == SELECTION_ICONS) {
274 int selection = mLastSelectedIcon;
275 final int firstIcon = Math.round(mPosX) *
Romain Guy060b5c82010-03-04 10:07:38 -0800276 mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500277 if (selection < 0 || // No selection
278 selection < firstIcon || // off the top of the screen
279 selection >= mRollo.mState.iconCount || // past last icon
280 selection >= firstIcon + // past last icon on screen
Romain Guy060b5c82010-03-04 10:07:38 -0800281 (mColumnsPerPage * mRowsPerPage)) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500282 selection = firstIcon;
283 }
Romain Guy060b5c82010-03-04 10:07:38 -0800284
Daniel Sandler388f6792010-03-02 14:08:08 -0500285 // Select the first icon when we gain window focus
286 mRollo.selectIcon(selection, SELECTED_FOCUSED);
287 mRollo.mState.save();
288 } else if (mLastSelection == SELECTION_HOME) {
289 mRollo.setHomeSelected(SELECTED_FOCUSED);
290 mRollo.mState.save();
291 }
292 }
293 }
294 }
295 }
Romain Guy060b5c82010-03-04 10:07:38 -0800296
Daniel Sandler388f6792010-03-02 14:08:08 -0500297 @Override
298 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
299 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Romain Guy060b5c82010-03-04 10:07:38 -0800300
Daniel Sandler388f6792010-03-02 14:08:08 -0500301 if (!isVisible()) {
302 return;
303 }
Romain Guy060b5c82010-03-04 10:07:38 -0800304
Daniel Sandler388f6792010-03-02 14:08:08 -0500305 if (gainFocus) {
306 if (mRollo != null) {
307 gainFocus();
308 } else {
309 mShouldGainFocus = true;
310 }
311 } else {
312 if (mRollo != null) {
313 if (mArrowNavigation) {
314 // Clear selection when we lose focus
315 mRollo.clearSelectedIcon();
316 mRollo.setHomeSelected(SELECTED_NONE);
317 mRollo.mState.save();
318 mArrowNavigation = false;
319 }
320 } else {
321 mShouldGainFocus = false;
322 }
323 }
324 }
Romain Guy060b5c82010-03-04 10:07:38 -0800325
Daniel Sandler388f6792010-03-02 14:08:08 -0500326 private void gainFocus() {
327 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
328 // Select the first icon when we gain keyboard focus
329 mArrowNavigation = true;
Romain Guy060b5c82010-03-04 10:07:38 -0800330 mRollo.selectIcon(Math.round(mPosX) * mColumnsPerPage,
Daniel Sandler388f6792010-03-02 14:08:08 -0500331 SELECTED_FOCUSED);
332 mRollo.mState.save();
333 }
334 }
Romain Guy060b5c82010-03-04 10:07:38 -0800335
Daniel Sandler388f6792010-03-02 14:08:08 -0500336 @Override
337 public boolean onKeyDown(int keyCode, KeyEvent event) {
Romain Guy060b5c82010-03-04 10:07:38 -0800338
Daniel Sandler388f6792010-03-02 14:08:08 -0500339 boolean handled = false;
Romain Guy060b5c82010-03-04 10:07:38 -0800340
Daniel Sandler388f6792010-03-02 14:08:08 -0500341 if (!isVisible()) {
342 return false;
343 }
344 final int iconCount = mRollo.mState.iconCount;
Romain Guy060b5c82010-03-04 10:07:38 -0800345
Daniel Sandler388f6792010-03-02 14:08:08 -0500346 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
347 if (mArrowNavigation) {
348 if (mLastSelection == SELECTION_HOME) {
349 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
350 mLauncher.closeAllApps(true);
351 } else {
352 int whichApp = mRollo.mState.selectedIconIndex;
353 if (whichApp >= 0) {
354 ApplicationInfo app = mAllAppsList.get(whichApp);
355 mLauncher.startActivitySafely(app.intent);
356 handled = true;
357 }
358 }
359 }
360 }
Romain Guy060b5c82010-03-04 10:07:38 -0800361
Daniel Sandler388f6792010-03-02 14:08:08 -0500362 if (iconCount > 0) {
363 mArrowNavigation = true;
Romain Guy060b5c82010-03-04 10:07:38 -0800364
Daniel Sandler388f6792010-03-02 14:08:08 -0500365 int currentSelection = mRollo.mState.selectedIconIndex;
366 int currentTopRow = Math.round(mPosX);
Romain Guy060b5c82010-03-04 10:07:38 -0800367
368 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE_PORTRAIT-1
369 final int currentPageCol = currentSelection % mColumnsPerPage;
370
371 // The row of the current selection, in the range 0..ROWS_PER_PAGE_PORTRAIT-1
372 final int currentPageRow = (currentSelection - (currentTopRow* mColumnsPerPage))
373 / mRowsPerPage;
374
Daniel Sandler388f6792010-03-02 14:08:08 -0500375 int newSelection = currentSelection;
Romain Guy060b5c82010-03-04 10:07:38 -0800376
Daniel Sandler388f6792010-03-02 14:08:08 -0500377 switch (keyCode) {
378 case KeyEvent.KEYCODE_DPAD_UP:
379 if (mLastSelection == SELECTION_HOME) {
380 mRollo.setHomeSelected(SELECTED_NONE);
Romain Guy060b5c82010-03-04 10:07:38 -0800381 int lastRowCount = iconCount % mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500382 if (lastRowCount == 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800383 lastRowCount = mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500384 }
Romain Guy060b5c82010-03-04 10:07:38 -0800385 newSelection = iconCount - lastRowCount + (mColumnsPerPage / 2);
Daniel Sandler388f6792010-03-02 14:08:08 -0500386 if (newSelection >= iconCount) {
387 newSelection = iconCount-1;
388 }
Romain Guy060b5c82010-03-04 10:07:38 -0800389 int target = (newSelection / mColumnsPerPage)
390 - (mRowsPerPage - 1);
Daniel Sandler388f6792010-03-02 14:08:08 -0500391 if (target < 0) {
392 target = 0;
393 }
394 if (currentTopRow != target) {
395 mRollo.moveTo(target);
396 }
397 } else {
398 if (currentPageRow > 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800399 newSelection = currentSelection - mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500400 } else if (currentTopRow > 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800401 newSelection = currentSelection - mColumnsPerPage;
402 mRollo.moveTo(newSelection / mColumnsPerPage);
Daniel Sandler388f6792010-03-02 14:08:08 -0500403 } else if (currentPageRow != 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800404 newSelection = currentTopRow * mRowsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500405 }
406 }
407 handled = true;
408 break;
Romain Guy060b5c82010-03-04 10:07:38 -0800409
Daniel Sandler388f6792010-03-02 14:08:08 -0500410 case KeyEvent.KEYCODE_DPAD_DOWN: {
Romain Guy060b5c82010-03-04 10:07:38 -0800411 final int rowCount = iconCount / mColumnsPerPage
412 + (iconCount % mColumnsPerPage == 0 ? 0 : 1);
413 final int currentRow = currentSelection / mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500414 if (mLastSelection != SELECTION_HOME) {
415 if (currentRow < rowCount-1) {
416 mRollo.setHomeSelected(SELECTED_NONE);
417 if (currentSelection < 0) {
418 newSelection = 0;
419 } else {
Romain Guy060b5c82010-03-04 10:07:38 -0800420 newSelection = currentSelection + mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500421 }
422 if (newSelection >= iconCount) {
423 // Go from D to G in this arrangement:
424 // A B C D
425 // E F G
426 newSelection = iconCount - 1;
427 }
Romain Guy060b5c82010-03-04 10:07:38 -0800428 if (currentPageRow >= mRowsPerPage - 1) {
429 mRollo.moveTo((newSelection / mColumnsPerPage) -
430 mRowsPerPage + 1);
Daniel Sandler388f6792010-03-02 14:08:08 -0500431 }
432 } else {
433 newSelection = -1;
434 mRollo.setHomeSelected(SELECTED_FOCUSED);
435 }
436 }
437 handled = true;
438 break;
439 }
440 case KeyEvent.KEYCODE_DPAD_LEFT:
441 if (mLastSelection != SELECTION_HOME) {
442 if (currentPageCol > 0) {
443 newSelection = currentSelection - 1;
444 }
445 }
446 handled = true;
447 break;
448 case KeyEvent.KEYCODE_DPAD_RIGHT:
449 if (mLastSelection != SELECTION_HOME) {
Romain Guy060b5c82010-03-04 10:07:38 -0800450 if ((currentPageCol < mColumnsPerPage - 1) &&
Daniel Sandler388f6792010-03-02 14:08:08 -0500451 (currentSelection < iconCount - 1)) {
452 newSelection = currentSelection + 1;
453 }
454 }
455 handled = true;
456 break;
457 }
458 if (newSelection != currentSelection) {
459 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
460 mRollo.mState.save();
461 }
462 }
463 return handled;
464 }
Romain Guy060b5c82010-03-04 10:07:38 -0800465
Daniel Sandler388f6792010-03-02 14:08:08 -0500466 @Override
467 public boolean onTouchEvent(MotionEvent ev)
468 {
469 mArrowNavigation = false;
Romain Guy060b5c82010-03-04 10:07:38 -0800470
Daniel Sandler388f6792010-03-02 14:08:08 -0500471 if (!isVisible()) {
472 return true;
473 }
Romain Guy060b5c82010-03-04 10:07:38 -0800474
Daniel Sandler388f6792010-03-02 14:08:08 -0500475 if (mLocks != 0) {
476 return true;
477 }
Romain Guy060b5c82010-03-04 10:07:38 -0800478
Daniel Sandler388f6792010-03-02 14:08:08 -0500479 super.onTouchEvent(ev);
Romain Guy060b5c82010-03-04 10:07:38 -0800480
Daniel Sandler388f6792010-03-02 14:08:08 -0500481 int x = (int)ev.getX();
482 int y = (int)ev.getY();
Romain Guy060b5c82010-03-04 10:07:38 -0800483
Daniel Sandler388f6792010-03-02 14:08:08 -0500484 int action = ev.getAction();
485 switch (action) {
486 case MotionEvent.ACTION_DOWN:
487 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
488 mTouchTracking = TRACKING_HOME;
489 mRollo.setHomeSelected(SELECTED_PRESSED);
490 mRollo.mState.save();
491 mCurrentIconIndex = -1;
492 } else {
493 mTouchTracking = TRACKING_FLING;
Romain Guy060b5c82010-03-04 10:07:38 -0800494
Daniel Sandler388f6792010-03-02 14:08:08 -0500495 mMotionDownRawX = (int)ev.getRawX();
496 mMotionDownRawY = (int)ev.getRawY();
Romain Guy060b5c82010-03-04 10:07:38 -0800497
Daniel Sandler388f6792010-03-02 14:08:08 -0500498 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
499 mRollo.mState.newTouchDown = 1;
Romain Guy060b5c82010-03-04 10:07:38 -0800500
Daniel Sandler388f6792010-03-02 14:08:08 -0500501 if (!mRollo.checkClickOK()) {
502 mRollo.clearSelectedIcon();
503 } else {
504 mDownIconIndex = mCurrentIconIndex
505 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
506 if (mDownIconIndex < 0) {
507 // if nothing was selected, no long press.
508 cancelLongPress();
509 }
510 }
511 mRollo.mState.save();
512 mRollo.move();
513 mVelocityTracker = VelocityTracker.obtain();
514 mVelocityTracker.addMovement(ev);
515 mStartedScrolling = false;
516 }
517 break;
518 case MotionEvent.ACTION_MOVE:
519 case MotionEvent.ACTION_OUTSIDE:
520 if (mTouchTracking == TRACKING_HOME) {
521 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
522 ? SELECTED_PRESSED : SELECTED_NONE);
523 mRollo.mState.save();
524 } else if (mTouchTracking == TRACKING_FLING) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500525 int rawY = (int)ev.getRawY();
526 int slop;
527 slop = Math.abs(rawY - mMotionDownRawY);
Romain Guy060b5c82010-03-04 10:07:38 -0800528
Daniel Sandler388f6792010-03-02 14:08:08 -0500529 if (!mStartedScrolling && slop < mSlop) {
530 // don't update anything so when we do start scrolling
531 // below, we get the right delta.
532 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
533 if (mDownIconIndex != mCurrentIconIndex) {
534 // If a different icon is selected, don't allow it to be picked up.
535 // This handles off-axis dragging.
536 cancelLongPress();
537 mCurrentIconIndex = -1;
538 }
539 } else {
540 if (!mStartedScrolling) {
541 cancelLongPress();
542 mCurrentIconIndex = -1;
543 }
544 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
545 mRollo.mState.newTouchDown = 1;
546 mRollo.move();
Romain Guy060b5c82010-03-04 10:07:38 -0800547
Daniel Sandler388f6792010-03-02 14:08:08 -0500548 mStartedScrolling = true;
549 mRollo.clearSelectedIcon();
550 mVelocityTracker.addMovement(ev);
551 mRollo.mState.save();
552 }
553 }
554 break;
555 case MotionEvent.ACTION_UP:
556 case MotionEvent.ACTION_CANCEL:
557 if (mTouchTracking == TRACKING_HOME) {
558 if (action == MotionEvent.ACTION_UP) {
559 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
560 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
561 mLauncher.closeAllApps(true);
562 }
563 mRollo.setHomeSelected(SELECTED_NONE);
564 mRollo.mState.save();
565 }
566 mCurrentIconIndex = -1;
567 } else if (mTouchTracking == TRACKING_FLING) {
568 mRollo.mState.newTouchDown = 0;
569 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Romain Guy060b5c82010-03-04 10:07:38 -0800570
Daniel Sandler388f6792010-03-02 14:08:08 -0500571 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
572 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
573 mRollo.clearSelectedIcon();
574 mRollo.mState.save();
575 mRollo.fling();
Romain Guy060b5c82010-03-04 10:07:38 -0800576
Daniel Sandler388f6792010-03-02 14:08:08 -0500577 if (mVelocityTracker != null) {
578 mVelocityTracker.recycle();
579 mVelocityTracker = null;
580 }
581 }
582 mTouchTracking = TRACKING_NONE;
583 break;
584 }
Romain Guy060b5c82010-03-04 10:07:38 -0800585
Daniel Sandler388f6792010-03-02 14:08:08 -0500586 return true;
587 }
Romain Guy060b5c82010-03-04 10:07:38 -0800588
Daniel Sandler388f6792010-03-02 14:08:08 -0500589 public void onClick(View v) {
590 if (mLocks != 0 || !isVisible()) {
591 return;
592 }
593 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
594 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
595 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
596 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
597 mLauncher.startActivitySafely(app.intent);
598 }
599 }
Romain Guy060b5c82010-03-04 10:07:38 -0800600
Daniel Sandler388f6792010-03-02 14:08:08 -0500601 public boolean onLongClick(View v) {
602 if (mLocks != 0 || !isVisible()) {
603 return true;
604 }
605 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
606 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
607 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Romain Guy060b5c82010-03-04 10:07:38 -0800608
Daniel Sandler388f6792010-03-02 14:08:08 -0500609 Bitmap bmp = app.iconBitmap;
610 final int w = bmp.getWidth();
611 final int h = bmp.getHeight();
Romain Guy060b5c82010-03-04 10:07:38 -0800612
Daniel Sandler388f6792010-03-02 14:08:08 -0500613 // We don't really have an accurate location to use. This will do.
614 int screenX = mMotionDownRawX - (w / 2);
615 int screenY = mMotionDownRawY - h;
Romain Guy060b5c82010-03-04 10:07:38 -0800616
Daniel Sandler388f6792010-03-02 14:08:08 -0500617 mDragController.startDrag(bmp, screenX, screenY,
618 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Romain Guy060b5c82010-03-04 10:07:38 -0800619
Daniel Sandler388f6792010-03-02 14:08:08 -0500620 mLauncher.closeAllApps(true);
621 }
622 return true;
623 }
Romain Guy060b5c82010-03-04 10:07:38 -0800624
Daniel Sandler388f6792010-03-02 14:08:08 -0500625 @Override
626 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
627 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
628 if (!isVisible()) {
629 return false;
630 }
631 String text = null;
632 int index;
633 int count = mAllAppsList.size() + 1; // +1 is home
634 int pos = -1;
635 switch (mLastSelection) {
636 case SELECTION_ICONS:
637 index = mRollo.mState.selectedIconIndex;
638 if (index >= 0) {
639 ApplicationInfo info = mAllAppsList.get(index);
640 if (info.title != null) {
641 text = info.title.toString();
642 pos = index;
643 }
644 }
645 break;
646 case SELECTION_HOME:
647 text = getContext().getString(R.string.all_apps_home_button_label);
648 pos = count;
649 break;
650 }
651 if (text != null) {
652 event.setEnabled(true);
653 event.getText().add(text);
654 //event.setContentDescription(text);
655 event.setItemCount(count);
656 event.setCurrentItemIndex(pos);
657 }
658 }
659 return false;
660 }
Romain Guy060b5c82010-03-04 10:07:38 -0800661
Daniel Sandler388f6792010-03-02 14:08:08 -0500662 public void setDragController(DragController dragger) {
663 mDragController = dragger;
664 }
Romain Guy060b5c82010-03-04 10:07:38 -0800665
Daniel Sandler388f6792010-03-02 14:08:08 -0500666 public void onDropCompleted(View target, boolean success) {
667 }
Romain Guy060b5c82010-03-04 10:07:38 -0800668
Daniel Sandler388f6792010-03-02 14:08:08 -0500669 /**
670 * Zoom to the specifed level.
671 *
672 * @param zoom [0..1] 0 is hidden, 1 is open
673 */
674 public void zoom(float zoom, boolean animate) {
675 cancelLongPress();
676 mNextZoom = zoom;
677 mAnimateNextZoom = animate;
678 // if we do setZoom while we don't have a surface, we won't
679 // get the callbacks that actually set mZoom.
680 if (mRollo == null || !mHaveSurface) {
681 mZoomDirty = true;
682 mZoom = zoom;
Daniel Sandler388f6792010-03-02 14:08:08 -0500683 } else {
684 mRollo.setZoom(zoom, animate);
685 }
686 }
Romain Guy060b5c82010-03-04 10:07:38 -0800687
Daniel Sandler388f6792010-03-02 14:08:08 -0500688 public boolean isVisible() {
689 return mZoom > 0.001f;
690 }
Romain Guy060b5c82010-03-04 10:07:38 -0800691
Daniel Sandler388f6792010-03-02 14:08:08 -0500692 public boolean isOpaque() {
693 return mZoom > 0.999f;
694 }
Romain Guy060b5c82010-03-04 10:07:38 -0800695
Daniel Sandler388f6792010-03-02 14:08:08 -0500696 public void setApps(ArrayList<ApplicationInfo> list) {
697 if (mRS == null) {
698 // We've been removed from the window. Don't bother with all this.
699 return;
700 }
Romain Guy060b5c82010-03-04 10:07:38 -0800701
Daniel Sandler388f6792010-03-02 14:08:08 -0500702 mAllAppsList = list;
703 if (mRollo != null) {
704 mRollo.setApps(list);
705 }
706 mLocks &= ~LOCK_ICONS_PENDING;
707 }
Romain Guy060b5c82010-03-04 10:07:38 -0800708
Daniel Sandler388f6792010-03-02 14:08:08 -0500709 public void addApps(ArrayList<ApplicationInfo> list) {
710 if (mAllAppsList == null) {
711 // Not done loading yet. We'll find out about it later.
712 return;
713 }
714 if (mRS == null) {
715 // We've been removed from the window. Don't bother with all this.
716 return;
717 }
Romain Guy060b5c82010-03-04 10:07:38 -0800718
Daniel Sandler388f6792010-03-02 14:08:08 -0500719 final int N = list.size();
720 if (mRollo != null) {
721 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
722 }
Romain Guy060b5c82010-03-04 10:07:38 -0800723
Daniel Sandler388f6792010-03-02 14:08:08 -0500724 for (int i=0; i<N; i++) {
725 final ApplicationInfo item = list.get(i);
726 int index = Collections.binarySearch(mAllAppsList, item,
727 LauncherModel.APP_NAME_COMPARATOR);
728 if (index < 0) {
729 index = -(index+1);
730 }
731 mAllAppsList.add(index, item);
732 if (mRollo != null) {
733 mRollo.addApp(index, item);
734 }
735 }
Romain Guy060b5c82010-03-04 10:07:38 -0800736
Daniel Sandler388f6792010-03-02 14:08:08 -0500737 if (mRollo != null) {
738 mRollo.saveAppsList();
739 }
740 }
Romain Guy060b5c82010-03-04 10:07:38 -0800741
Daniel Sandler388f6792010-03-02 14:08:08 -0500742 public void removeApps(ArrayList<ApplicationInfo> list) {
743 if (mAllAppsList == null) {
744 // Not done loading yet. We'll find out about it later.
745 return;
746 }
Romain Guy060b5c82010-03-04 10:07:38 -0800747
Daniel Sandler388f6792010-03-02 14:08:08 -0500748 final int N = list.size();
749 for (int i=0; i<N; i++) {
750 final ApplicationInfo item = list.get(i);
751 int index = findAppByComponent(mAllAppsList, item);
752 if (index >= 0) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500753 mAllAppsList.remove(index);
754 if (mRollo != null) {
755 mRollo.removeApp(index);
756 }
757 } else {
758 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
759 // Try to recover. This should keep us from crashing for now.
760 }
761 }
Romain Guy060b5c82010-03-04 10:07:38 -0800762
Daniel Sandler388f6792010-03-02 14:08:08 -0500763 if (mRollo != null) {
764 mRollo.saveAppsList();
765 }
766 }
Romain Guy060b5c82010-03-04 10:07:38 -0800767
Daniel Sandler388f6792010-03-02 14:08:08 -0500768 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
769 // Just remove and add, because they may need to be re-sorted.
770 removeApps(list);
771 addApps(list);
772 }
Romain Guy060b5c82010-03-04 10:07:38 -0800773
Daniel Sandler388f6792010-03-02 14:08:08 -0500774 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
775 ComponentName component = item.intent.getComponent();
776 final int N = list.size();
777 for (int i=0; i<N; i++) {
778 ApplicationInfo x = list.get(i);
779 if (x.intent.getComponent().equals(component)) {
780 return i;
781 }
782 }
783 return -1;
784 }
Romain Guy060b5c82010-03-04 10:07:38 -0800785
786 /*
Daniel Sandler388f6792010-03-02 14:08:08 -0500787 private static int countPages(int iconCount) {
Romain Guy060b5c82010-03-04 10:07:38 -0800788 int iconsPerPage = getColumnsCount() * Defines.ROWS_PER_PAGE_PORTRAIT;
Daniel Sandler388f6792010-03-02 14:08:08 -0500789 int pages = iconCount / iconsPerPage;
790 if (pages*iconsPerPage != iconCount) {
791 pages++;
792 }
793 return pages;
794 }
Romain Guy060b5c82010-03-04 10:07:38 -0800795 */
Daniel Sandler388f6792010-03-02 14:08:08 -0500796
797 class AAMessage extends RenderScript.RSMessage {
798 public void run() {
799 mPosX = ((float)mData[0]) / (1 << 16);
800 mVelocity = ((float)mData[1]) / (1 << 16);
801 mZoom = ((float)mData[2]) / (1 << 16);
802 mZoomDirty = false;
803 }
804 }
Romain Guy060b5c82010-03-04 10:07:38 -0800805
Daniel Sandler388f6792010-03-02 14:08:08 -0500806 public class RolloRS {
Romain Guy060b5c82010-03-04 10:07:38 -0800807
Daniel Sandler388f6792010-03-02 14:08:08 -0500808 // Allocations ======
809 private int mWidth;
810 private int mHeight;
Romain Guy060b5c82010-03-04 10:07:38 -0800811
Daniel Sandler388f6792010-03-02 14:08:08 -0500812 private Resources mRes;
813 private Script mScript;
814 private Script.Invokable mInvokeMove;
815 private Script.Invokable mInvokeMoveTo;
816 private Script.Invokable mInvokeFling;
817 private Script.Invokable mInvokeResetWAR;
818 private Script.Invokable mInvokeSetZoom;
Romain Guy060b5c82010-03-04 10:07:38 -0800819
Daniel Sandler388f6792010-03-02 14:08:08 -0500820 private ProgramStore mPSIcons;
821 private ProgramFragment mPFTexMip;
822 private ProgramFragment mPFTexMipAlpha;
823 private ProgramFragment mPFTexNearest;
824 private ProgramVertex mPV;
825 private ProgramVertex mPVCurve;
826 private SimpleMesh mMesh;
827 private ProgramVertex.MatrixAllocation mPVA;
Romain Guy060b5c82010-03-04 10:07:38 -0800828
Daniel Sandler388f6792010-03-02 14:08:08 -0500829 private Allocation mUniformAlloc;
Romain Guy060b5c82010-03-04 10:07:38 -0800830
Daniel Sandler388f6792010-03-02 14:08:08 -0500831 private Allocation mHomeButtonNormal;
832 private Allocation mHomeButtonFocused;
833 private Allocation mHomeButtonPressed;
Romain Guy060b5c82010-03-04 10:07:38 -0800834
Daniel Sandler388f6792010-03-02 14:08:08 -0500835 private Allocation[] mIcons;
836 private int[] mIconIds;
837 private Allocation mAllocIconIds;
Romain Guy060b5c82010-03-04 10:07:38 -0800838
Daniel Sandler388f6792010-03-02 14:08:08 -0500839 private Allocation[] mLabels;
840 private int[] mLabelIds;
841 private Allocation mAllocLabelIds;
842 private Allocation mSelectedIcon;
Romain Guy060b5c82010-03-04 10:07:38 -0800843
Daniel Sandler388f6792010-03-02 14:08:08 -0500844 private int[] mTouchYBorders;
845 private int[] mTouchXBorders;
Romain Guy060b5c82010-03-04 10:07:38 -0800846
Daniel Sandler388f6792010-03-02 14:08:08 -0500847 private Bitmap mSelectionBitmap;
848 private Canvas mSelectionCanvas;
Romain Guy060b5c82010-03-04 10:07:38 -0800849
Daniel Sandler388f6792010-03-02 14:08:08 -0500850 Params mParams;
851 State mState;
Romain Guy060b5c82010-03-04 10:07:38 -0800852
Daniel Sandler388f6792010-03-02 14:08:08 -0500853 class BaseAlloc {
854 Allocation mAlloc;
855 Type mType;
Romain Guy060b5c82010-03-04 10:07:38 -0800856
Daniel Sandler388f6792010-03-02 14:08:08 -0500857 void save() {
858 mAlloc.data(this);
859 }
860 }
Romain Guy060b5c82010-03-04 10:07:38 -0800861
Daniel Sandler388f6792010-03-02 14:08:08 -0500862 private boolean checkClickOK() {
863 return (Math.abs(mVelocity) < 0.4f) &&
864 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
865 }
Romain Guy060b5c82010-03-04 10:07:38 -0800866
Daniel Sandler388f6792010-03-02 14:08:08 -0500867 class Params extends BaseAlloc {
868 Params() {
869 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
870 mAlloc = Allocation.createTyped(mRS, mType);
871 save();
872 }
873 public int bubbleWidth;
874 public int bubbleHeight;
875 public int bubbleBitmapWidth;
876 public int bubbleBitmapHeight;
Romain Guy060b5c82010-03-04 10:07:38 -0800877
Daniel Sandler388f6792010-03-02 14:08:08 -0500878 public int homeButtonWidth;
879 public int homeButtonHeight;
880 public int homeButtonTextureWidth;
881 public int homeButtonTextureHeight;
882 }
Romain Guy060b5c82010-03-04 10:07:38 -0800883
Daniel Sandler388f6792010-03-02 14:08:08 -0500884 class State extends BaseAlloc {
885 public float newPositionX;
886 public int newTouchDown;
887 public float flingVelocity;
888 public int iconCount;
889 public int selectedIconIndex = -1;
890 public int selectedIconTexture;
891 public float zoomTarget;
892 public int homeButtonId;
893 public float targetPos;
Romain Guy060b5c82010-03-04 10:07:38 -0800894
Daniel Sandler388f6792010-03-02 14:08:08 -0500895 State() {
896 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
897 mAlloc = Allocation.createTyped(mRS, mType);
898 save();
899 }
900 }
Romain Guy060b5c82010-03-04 10:07:38 -0800901
Daniel Sandler388f6792010-03-02 14:08:08 -0500902 public RolloRS() {
903 }
Romain Guy060b5c82010-03-04 10:07:38 -0800904
Daniel Sandler388f6792010-03-02 14:08:08 -0500905 public void init(Resources res, int width, int height) {
906 mRes = res;
907 mWidth = width;
908 mHeight = height;
909 initProgramVertex();
910 initProgramFragment();
911 initProgramStore();
912 initGl();
913 initData();
914 initTouchState();
915 initRs();
916 }
Romain Guy060b5c82010-03-04 10:07:38 -0800917
Daniel Sandler388f6792010-03-02 14:08:08 -0500918 public void initMesh() {
919 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Romain Guy060b5c82010-03-04 10:07:38 -0800920
Daniel Sandler388f6792010-03-02 14:08:08 -0500921 for (int ct=0; ct < 16; ct++) {
922 float pos = (1.f / (16.f - 1)) * ct;
923 tm.addVertex(0.0f, pos);
924 tm.addVertex(1.0f, pos);
925 }
926 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
927 tm.addTriangle(ct, ct+1, ct+2);
928 tm.addTriangle(ct+1, ct+3, ct+2);
929 }
930 mMesh = tm.create();
931 mMesh.setName("SMCell");
932 }
Romain Guy060b5c82010-03-04 10:07:38 -0800933
Daniel Sandler388f6792010-03-02 14:08:08 -0500934 void resize(int w, int h) {
935 mPVA.setupProjectionNormalized(w, h);
936 mWidth = w;
937 mHeight = h;
938 }
Romain Guy060b5c82010-03-04 10:07:38 -0800939
Daniel Sandler388f6792010-03-02 14:08:08 -0500940 private void initProgramVertex() {
941 mPVA = new ProgramVertex.MatrixAllocation(mRS);
942 resize(mWidth, mHeight);
Romain Guy060b5c82010-03-04 10:07:38 -0800943
Daniel Sandler388f6792010-03-02 14:08:08 -0500944 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
945 pvb.setTextureMatrixEnable(true);
946 mPV = pvb.create();
947 mPV.setName("PV");
948 mPV.bindAllocation(mPVA);
Romain Guy060b5c82010-03-04 10:07:38 -0800949
Daniel Sandler388f6792010-03-02 14:08:08 -0500950 Element.Builder eb = new Element.Builder(mRS);
951 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
952 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
953 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "BendPos");
954 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
955 Element e = eb.create();
Romain Guy060b5c82010-03-04 10:07:38 -0800956
Daniel Sandler388f6792010-03-02 14:08:08 -0500957 mUniformAlloc = Allocation.createSized(mRS, e, 1);
Romain Guy060b5c82010-03-04 10:07:38 -0800958
Daniel Sandler388f6792010-03-02 14:08:08 -0500959 initMesh();
960 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
Romain Guy060b5c82010-03-04 10:07:38 -0800961 String t = "void main() {\n" +
962 // Animation
963 " float ani = UNI_Position.z;\n" +
964
965 " float bendY1 = UNI_BendPos.x;\n" +
966 " float bendY2 = UNI_BendPos.y;\n" +
967 " float bendAngle = 47.0 * (3.14 / 180.0);\n" +
968 " float bendDistance = bendY1 * 0.4;\n" +
969 " float distanceDimLevel = 0.6;\n" +
970
971 " float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
972 " float aDy = cos(bendAngle);\n" +
973 " float aDz = sin(bendAngle);\n" +
974
975 " float scale = (2.0 / 480.0);\n" +
976 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
977 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
978 " float y = 0.0;\n" +
979 " float z = 0.0;\n" +
980 " float lum = 1.0;\n" +
981
982 " float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
983 " y += cv * aDy;\n" +
984 " z += -cv * aDz;\n" +
985 " cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" + // curve range
986 " lum += cv / bendDistance * distanceDimLevel;\n" +
987 " y += cv * cos(cv * bendStep);\n" +
988 " z += cv * sin(cv * bendStep);\n" +
989
990 " cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
991 " y += cv * aDy;\n" +
992 " z += cv * aDz;\n" +
993 " cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
994 " lum -= cv / bendDistance * distanceDimLevel;\n" +
995 " y += cv * cos(cv * bendStep);\n" +
996 " z += cv * sin(cv * bendStep);\n" +
997
998 " y += clamp(ys, bendY1, bendY2);\n" +
999
1000 " vec4 pos;\n" +
1001 " pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
1002 " pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
1003 " pos.z = z * UNI_ScaleOffset.x;\n" +
1004 " pos.w = 1.0;\n" +
1005
1006 " pos.x *= 1.0 + ani * 4.0;\n" +
1007 " pos.y *= 1.0 + ani * 4.0;\n" +
1008 " pos.z -= ani * 1.5;\n" +
1009 " lum *= 1.0 - ani;\n" +
1010
1011 " gl_Position = UNI_MVP * pos;\n" +
1012 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
1013 " varTex0.xy = ATTRIB_position;\n" +
1014 " varTex0.y = 1.0 - varTex0.y;\n" +
1015 " varTex0.zw = vec2(0.0, 0.0);\n" +
1016 "}\n";
Daniel Sandler388f6792010-03-02 14:08:08 -05001017 sb.setShader(t);
1018 sb.addConstant(mUniformAlloc.getType());
1019 sb.addInput(mMesh.getVertexType(0).getElement());
1020 mPVCurve = sb.create();
1021 mPVCurve.setName("PVCurve");
1022 mPVCurve.bindAllocation(mPVA);
1023 mPVCurve.bindConstants(mUniformAlloc, 1);
Romain Guy060b5c82010-03-04 10:07:38 -08001024
Daniel Sandler388f6792010-03-02 14:08:08 -05001025 mRS.contextBindProgramVertex(mPV);
1026 }
Romain Guy060b5c82010-03-04 10:07:38 -08001027
Daniel Sandler388f6792010-03-02 14:08:08 -05001028 private void initProgramFragment() {
1029 Sampler.Builder sb = new Sampler.Builder(mRS);
1030 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
1031 sb.setMag(Sampler.Value.NEAREST);
1032 sb.setWrapS(Sampler.Value.CLAMP);
1033 sb.setWrapT(Sampler.Value.CLAMP);
1034 Sampler linear = sb.create();
Romain Guy060b5c82010-03-04 10:07:38 -08001035
Daniel Sandler388f6792010-03-02 14:08:08 -05001036 sb.setMin(Sampler.Value.NEAREST);
1037 sb.setMag(Sampler.Value.NEAREST);
1038 Sampler nearest = sb.create();
Romain Guy060b5c82010-03-04 10:07:38 -08001039
Daniel Sandler388f6792010-03-02 14:08:08 -05001040 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
1041 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1042 ProgramFragment.Builder.Format.RGBA, 0);
1043 mPFTexMip = bf.create();
1044 mPFTexMip.setName("PFTexMip");
1045 mPFTexMip.bindSampler(linear, 0);
Romain Guy060b5c82010-03-04 10:07:38 -08001046
Daniel Sandler388f6792010-03-02 14:08:08 -05001047 mPFTexNearest = bf.create();
1048 mPFTexNearest.setName("PFTexNearest");
1049 mPFTexNearest.bindSampler(nearest, 0);
Romain Guy060b5c82010-03-04 10:07:38 -08001050
Daniel Sandler388f6792010-03-02 14:08:08 -05001051 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1052 ProgramFragment.Builder.Format.ALPHA, 0);
1053 mPFTexMipAlpha = bf.create();
1054 mPFTexMipAlpha.setName("PFTexMipAlpha");
1055 mPFTexMipAlpha.bindSampler(linear, 0);
Romain Guy060b5c82010-03-04 10:07:38 -08001056
Daniel Sandler388f6792010-03-02 14:08:08 -05001057 }
Romain Guy060b5c82010-03-04 10:07:38 -08001058
Daniel Sandler388f6792010-03-02 14:08:08 -05001059 private void initProgramStore() {
1060 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1061 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
1062 bs.setColorMask(true,true,true,false);
1063 bs.setDitherEnable(true);
1064 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1065 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1066 mPSIcons = bs.create();
1067 mPSIcons.setName("PSIcons");
1068 }
Romain Guy060b5c82010-03-04 10:07:38 -08001069
Daniel Sandler388f6792010-03-02 14:08:08 -05001070 private void initGl() {
Romain Guy060b5c82010-03-04 10:07:38 -08001071 mTouchXBorders = new int[mColumnsPerPage +1];
1072 mTouchYBorders = new int[mRowsPerPage +1];
Daniel Sandler388f6792010-03-02 14:08:08 -05001073 }
Romain Guy060b5c82010-03-04 10:07:38 -08001074
Daniel Sandler388f6792010-03-02 14:08:08 -05001075 private void initData() {
1076 mParams = new Params();
1077 mState = new State();
Romain Guy060b5c82010-03-04 10:07:38 -08001078
Daniel Sandler388f6792010-03-02 14:08:08 -05001079 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Romain Guy060b5c82010-03-04 10:07:38 -08001080
Daniel Sandler388f6792010-03-02 14:08:08 -05001081 mParams.bubbleWidth = bubble.getBubbleWidth();
1082 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1083 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1084 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Romain Guy060b5c82010-03-04 10:07:38 -08001085
Daniel Sandler388f6792010-03-02 14:08:08 -05001086 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1087 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1088 mHomeButtonNormal.uploadToTexture(0);
1089 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1090 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1091 mHomeButtonFocused.uploadToTexture(0);
1092 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1093 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1094 mHomeButtonPressed.uploadToTexture(0);
1095 mParams.homeButtonWidth = 76;
1096 mParams.homeButtonHeight = 68;
1097 mParams.homeButtonTextureWidth = 128;
1098 mParams.homeButtonTextureHeight = 128;
Romain Guy060b5c82010-03-04 10:07:38 -08001099
Daniel Sandler388f6792010-03-02 14:08:08 -05001100 mState.homeButtonId = mHomeButtonNormal.getID();
Romain Guy060b5c82010-03-04 10:07:38 -08001101
Daniel Sandler388f6792010-03-02 14:08:08 -05001102 mParams.save();
1103 mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -08001104
Daniel Sandler388f6792010-03-02 14:08:08 -05001105 mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1106 Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1107 mSelectionCanvas = new Canvas(mSelectionBitmap);
Romain Guy060b5c82010-03-04 10:07:38 -08001108
Daniel Sandler388f6792010-03-02 14:08:08 -05001109 setApps(null);
1110 }
Romain Guy060b5c82010-03-04 10:07:38 -08001111
Daniel Sandler388f6792010-03-02 14:08:08 -05001112 private void initRs() {
1113 ScriptC.Builder sb = new ScriptC.Builder(mRS);
1114 sb.setScript(mRes, R.raw.allapps);
1115 sb.setRoot(true);
1116 sb.addDefines(mDefines);
1117 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1118 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
1119 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
1120 mInvokeMove = sb.addInvokable("move");
1121 mInvokeFling = sb.addInvokable("fling");
1122 mInvokeMoveTo = sb.addInvokable("moveTo");
1123 mInvokeResetWAR = sb.addInvokable("resetHWWar");
1124 mInvokeSetZoom = sb.addInvokable("setZoom");
1125 mScript = sb.create();
1126 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1127 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1128 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1129 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1130 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
1131 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Romain Guy060b5c82010-03-04 10:07:38 -08001132
Daniel Sandler388f6792010-03-02 14:08:08 -05001133 mRS.contextBindRootScript(mScript);
1134 }
Romain Guy060b5c82010-03-04 10:07:38 -08001135
Daniel Sandler388f6792010-03-02 14:08:08 -05001136 void dirtyCheck() {
1137 if (mZoomDirty) {
1138 setZoom(mNextZoom, mAnimateNextZoom);
1139 }
1140 }
Romain Guy060b5c82010-03-04 10:07:38 -08001141
1142 @SuppressWarnings({"ConstantConditions"})
Daniel Sandler388f6792010-03-02 14:08:08 -05001143 private void setApps(ArrayList<ApplicationInfo> list) {
1144 final int count = list != null ? list.size() : 0;
1145 int allocCount = count;
1146 if (allocCount < 1) {
1147 allocCount = 1;
1148 }
Romain Guy060b5c82010-03-04 10:07:38 -08001149
Daniel Sandler388f6792010-03-02 14:08:08 -05001150 mIcons = new Allocation[count];
1151 mIconIds = new int[allocCount];
1152 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Romain Guy060b5c82010-03-04 10:07:38 -08001153
Daniel Sandler388f6792010-03-02 14:08:08 -05001154 mLabels = new Allocation[count];
1155 mLabelIds = new int[allocCount];
1156 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Romain Guy060b5c82010-03-04 10:07:38 -08001157
Daniel Sandler388f6792010-03-02 14:08:08 -05001158 mState.iconCount = count;
1159 for (int i=0; i < mState.iconCount; i++) {
1160 createAppIconAllocations(i, list.get(i));
1161 }
1162 for (int i=0; i < mState.iconCount; i++) {
1163 uploadAppIcon(i, list.get(i));
1164 }
1165 saveAppsList();
1166 }
Romain Guy060b5c82010-03-04 10:07:38 -08001167
Daniel Sandler388f6792010-03-02 14:08:08 -05001168 private void setZoom(float zoom, boolean animate) {
1169 mRollo.clearSelectedIcon();
1170 mRollo.setHomeSelected(SELECTED_NONE);
1171 if (zoom > 0.001f) {
1172 mRollo.mState.zoomTarget = zoom;
1173 } else {
1174 mRollo.mState.zoomTarget = 0;
1175 }
1176 mRollo.mState.save();
1177 if (!animate) {
1178 mRollo.mInvokeSetZoom.execute();
1179 }
1180 }
Romain Guy060b5c82010-03-04 10:07:38 -08001181
Daniel Sandler388f6792010-03-02 14:08:08 -05001182 private void createAppIconAllocations(int index, ApplicationInfo item) {
1183 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
1184 Element.RGBA_8888(mRS), true);
1185 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
1186 Element.A_8(mRS), true);
1187 mIconIds[index] = mIcons[index].getID();
1188 mLabelIds[index] = mLabels[index].getID();
1189 }
Romain Guy060b5c82010-03-04 10:07:38 -08001190
Daniel Sandler388f6792010-03-02 14:08:08 -05001191 private void uploadAppIcon(int index, ApplicationInfo item) {
1192 if (mIconIds[index] != mIcons[index].getID()) {
1193 throw new IllegalStateException("uploadAppIcon index=" + index
1194 + " mIcons[index].getID=" + mIcons[index].getID()
1195 + " mIconsIds[index]=" + mIconIds[index]
1196 + " item=" + item);
1197 }
1198 mIcons[index].uploadToTexture(0);
1199 mLabels[index].uploadToTexture(0);
1200 }
Romain Guy060b5c82010-03-04 10:07:38 -08001201
Daniel Sandler388f6792010-03-02 14:08:08 -05001202 /**
1203 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1204 * fill in the values and call saveAppsList().
1205 */
1206 private void reallocAppsList(int count) {
1207 Allocation[] icons = new Allocation[count];
1208 int[] iconIds = new int[count];
1209 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Romain Guy060b5c82010-03-04 10:07:38 -08001210
Daniel Sandler388f6792010-03-02 14:08:08 -05001211 Allocation[] labels = new Allocation[count];
1212 int[] labelIds = new int[count];
1213 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Romain Guy060b5c82010-03-04 10:07:38 -08001214
Daniel Sandler388f6792010-03-02 14:08:08 -05001215 final int oldCount = mRollo.mState.iconCount;
Romain Guy060b5c82010-03-04 10:07:38 -08001216
Daniel Sandler388f6792010-03-02 14:08:08 -05001217 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1218 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1219 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1220 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
Romain Guy060b5c82010-03-04 10:07:38 -08001221
Daniel Sandler388f6792010-03-02 14:08:08 -05001222 mIcons = icons;
1223 mIconIds = iconIds;
1224 mLabels = labels;
1225 mLabelIds = labelIds;
1226 }
Romain Guy060b5c82010-03-04 10:07:38 -08001227
Daniel Sandler388f6792010-03-02 14:08:08 -05001228 /**
1229 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1230 */
1231 private void addApp(int index, ApplicationInfo item) {
1232 final int count = mState.iconCount - index;
1233 final int dest = index + 1;
Romain Guy060b5c82010-03-04 10:07:38 -08001234
Daniel Sandler388f6792010-03-02 14:08:08 -05001235 System.arraycopy(mIcons, index, mIcons, dest, count);
1236 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1237 System.arraycopy(mLabels, index, mLabels, dest, count);
1238 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
Romain Guy060b5c82010-03-04 10:07:38 -08001239
Daniel Sandler388f6792010-03-02 14:08:08 -05001240 createAppIconAllocations(index, item);
1241 uploadAppIcon(index, item);
1242 mRollo.mState.iconCount++;
1243 }
Romain Guy060b5c82010-03-04 10:07:38 -08001244
Daniel Sandler388f6792010-03-02 14:08:08 -05001245 /**
1246 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1247 */
1248 private void removeApp(int index) {
1249 final int count = mState.iconCount - index - 1;
1250 final int src = index + 1;
Romain Guy060b5c82010-03-04 10:07:38 -08001251
Daniel Sandler388f6792010-03-02 14:08:08 -05001252 System.arraycopy(mIcons, src, mIcons, index, count);
1253 System.arraycopy(mIconIds, src, mIconIds, index, count);
1254 System.arraycopy(mLabels, src, mLabels, index, count);
1255 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
Romain Guy060b5c82010-03-04 10:07:38 -08001256
Daniel Sandler388f6792010-03-02 14:08:08 -05001257 mRollo.mState.iconCount--;
1258 final int last = mState.iconCount;
Romain Guy060b5c82010-03-04 10:07:38 -08001259
Daniel Sandler388f6792010-03-02 14:08:08 -05001260 mIcons[last] = null;
1261 mIconIds[last] = 0;
1262 mLabels[last] = null;
1263 mLabelIds[last] = 0;
1264 }
Romain Guy060b5c82010-03-04 10:07:38 -08001265
Daniel Sandler388f6792010-03-02 14:08:08 -05001266 /**
1267 * Send the apps list structures to RS.
1268 */
1269 private void saveAppsList() {
1270 // WTF: how could mScript be not null but mAllocIconIds null b/2460740.
1271 if (mScript != null && mAllocIconIds != null) {
1272 mRS.contextBindRootScript(null);
Romain Guy060b5c82010-03-04 10:07:38 -08001273
Daniel Sandler388f6792010-03-02 14:08:08 -05001274 mAllocIconIds.data(mIconIds);
1275 mAllocLabelIds.data(mLabelIds);
Romain Guy060b5c82010-03-04 10:07:38 -08001276
Daniel Sandler388f6792010-03-02 14:08:08 -05001277 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1278 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Romain Guy060b5c82010-03-04 10:07:38 -08001279
Daniel Sandler388f6792010-03-02 14:08:08 -05001280 mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -08001281
Daniel Sandler388f6792010-03-02 14:08:08 -05001282 // Note: mScript may be null if we haven't initialized it yet.
1283 // In that case, this is a no-op.
1284 if (mInvokeResetWAR != null) {
1285 mInvokeResetWAR.execute();
1286 }
Romain Guy060b5c82010-03-04 10:07:38 -08001287
Daniel Sandler388f6792010-03-02 14:08:08 -05001288 mRS.contextBindRootScript(mScript);
1289 }
1290 }
Romain Guy060b5c82010-03-04 10:07:38 -08001291
Daniel Sandler388f6792010-03-02 14:08:08 -05001292 void initTouchState() {
Romain Guy060b5c82010-03-04 10:07:38 -08001293 boolean isPortrait = getWidth() < getHeight();
1294 // TODO: Put this in a config file/define
1295 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE_PORTRAIT;
1296 if (!isPortrait) cellHeight -= 12;
1297 int centerY = (int) (getHeight() * (isPortrait ? 0.5f : 0.47f));
1298 if (!isPortrait) centerY += cellHeight / 2;
1299 int half = (int) Math.floor((mRowsPerPage + 1) / 2);
1300 int end = mTouchYBorders.length - (half + 1);
1301
1302 for (int i = -half; i <= end; i++) {
1303 mTouchYBorders[i + half] = centerY + i * cellHeight;
1304 }
1305
1306 int x = 0;
1307 // TODO: Put this in a config file/define
1308 int columnWidth = 120;
1309 for (int i = 0; i < mColumnsPerPage + 1; i++) {
1310 mTouchXBorders[i] = x;
1311 x += columnWidth;
1312 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001313 }
Romain Guy060b5c82010-03-04 10:07:38 -08001314
Daniel Sandler388f6792010-03-02 14:08:08 -05001315 void fling() {
1316 mInvokeFling.execute();
1317 }
Romain Guy060b5c82010-03-04 10:07:38 -08001318
Daniel Sandler388f6792010-03-02 14:08:08 -05001319 void move() {
1320 mInvokeMove.execute();
1321 }
Romain Guy060b5c82010-03-04 10:07:38 -08001322
Daniel Sandler388f6792010-03-02 14:08:08 -05001323 void moveTo(float row) {
1324 mState.targetPos = row;
1325 mState.save();
1326 mInvokeMoveTo.execute();
1327 }
Romain Guy060b5c82010-03-04 10:07:38 -08001328
Daniel Sandler388f6792010-03-02 14:08:08 -05001329 int chooseTappedIcon(int x, int y, float pos) {
1330 // Adjust for scroll position if not zero.
1331 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Romain Guy060b5c82010-03-04 10:07:38 -08001332
Daniel Sandler388f6792010-03-02 14:08:08 -05001333 int col = -1;
1334 int row = -1;
Romain Guy060b5c82010-03-04 10:07:38 -08001335 final int columnsCount = mColumnsPerPage;
1336 for (int i=0; i< columnsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001337 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1338 col = i;
1339 break;
1340 }
1341 }
Romain Guy060b5c82010-03-04 10:07:38 -08001342 final int rowsCount = mRowsPerPage;
1343 for (int i=0; i< rowsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001344 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1345 row = i;
1346 break;
1347 }
1348 }
Romain Guy060b5c82010-03-04 10:07:38 -08001349
Daniel Sandler388f6792010-03-02 14:08:08 -05001350 if (row < 0 || col < 0) {
1351 return -1;
1352 }
Romain Guy060b5c82010-03-04 10:07:38 -08001353
1354 int index = (((int)pos) * columnsCount) + (row * columnsCount) + col;
1355
Daniel Sandler388f6792010-03-02 14:08:08 -05001356 if (index >= mState.iconCount) {
1357 return -1;
1358 } else {
1359 return index;
1360 }
1361 }
Romain Guy060b5c82010-03-04 10:07:38 -08001362
Daniel Sandler388f6792010-03-02 14:08:08 -05001363 /**
1364 * You need to call save() on mState on your own after calling this.
1365 *
1366 * @return the index of the icon that was selected.
1367 */
1368 int selectIcon(int x, int y, float pos, int pressed) {
1369 final int index = chooseTappedIcon(x, y, pos);
1370 selectIcon(index, pressed);
1371 return index;
1372 }
Romain Guy060b5c82010-03-04 10:07:38 -08001373
Daniel Sandler388f6792010-03-02 14:08:08 -05001374 /**
1375 * Select the icon at the given index.
1376 *
1377 * @param index The index.
1378 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
1379 */
1380 void selectIcon(int index, int pressed) {
1381 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
1382 mState.selectedIconIndex = -1;
1383 if (mLastSelection == SELECTION_ICONS) {
1384 mLastSelection = SELECTION_NONE;
1385 }
1386 } else {
1387 if (pressed == SELECTED_FOCUSED) {
1388 mLastSelection = SELECTION_ICONS;
1389 }
Romain Guy060b5c82010-03-04 10:07:38 -08001390
Daniel Sandler388f6792010-03-02 14:08:08 -05001391 int prev = mState.selectedIconIndex;
1392 mState.selectedIconIndex = index;
Romain Guy060b5c82010-03-04 10:07:38 -08001393
Daniel Sandler388f6792010-03-02 14:08:08 -05001394 ApplicationInfo info = mAllAppsList.get(index);
1395 Bitmap selectionBitmap = mSelectionBitmap;
Romain Guy060b5c82010-03-04 10:07:38 -08001396
Daniel Sandler388f6792010-03-02 14:08:08 -05001397 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1398 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1399 pressed == SELECTED_PRESSED, info.iconBitmap);
Romain Guy060b5c82010-03-04 10:07:38 -08001400
Daniel Sandler388f6792010-03-02 14:08:08 -05001401 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
1402 Element.RGBA_8888(mRS), false);
1403 mSelectedIcon.uploadToTexture(0);
1404 mState.selectedIconTexture = mSelectedIcon.getID();
Romain Guy060b5c82010-03-04 10:07:38 -08001405
Daniel Sandler388f6792010-03-02 14:08:08 -05001406 if (prev != index) {
1407 if (info.title != null && info.title.length() > 0) {
1408 //setContentDescription(info.title);
1409 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1410 }
1411 }
1412 }
1413 }
Romain Guy060b5c82010-03-04 10:07:38 -08001414
Daniel Sandler388f6792010-03-02 14:08:08 -05001415 /**
1416 * You need to call save() on mState on your own after calling this.
1417 */
1418 void clearSelectedIcon() {
1419 mState.selectedIconIndex = -1;
1420 }
Romain Guy060b5c82010-03-04 10:07:38 -08001421
Daniel Sandler388f6792010-03-02 14:08:08 -05001422 void setHomeSelected(int mode) {
1423 final int prev = mLastSelection;
1424 switch (mode) {
1425 case SELECTED_NONE:
1426 mState.homeButtonId = mHomeButtonNormal.getID();
1427 break;
1428 case SELECTED_FOCUSED:
1429 mLastSelection = SELECTION_HOME;
1430 mState.homeButtonId = mHomeButtonFocused.getID();
1431 if (prev != SELECTION_HOME) {
1432 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1433 }
1434 break;
1435 case SELECTED_PRESSED:
1436 mState.homeButtonId = mHomeButtonPressed.getID();
1437 break;
1438 }
1439 }
Romain Guy060b5c82010-03-04 10:07:38 -08001440
Daniel Sandler388f6792010-03-02 14:08:08 -05001441 public void dumpState() {
1442 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1443 Log.d(TAG, "mRollo.mHeight=" + mHeight);
Romain Guy060b5c82010-03-04 10:07:38 -08001444 Log.d(TAG, "mRollo.mIcons=" + Arrays.toString(mIcons));
Daniel Sandler388f6792010-03-02 14:08:08 -05001445 if (mIcons != null) {
1446 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1447 }
1448 if (mIconIds != null) {
1449 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1450 }
1451 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1452 if (mLabelIds != null) {
1453 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1454 }
1455 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1456 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1457 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
1458 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1459 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1460 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1461 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1462 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1463 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1464 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1465 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1466 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1467 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1468 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1469 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1470 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1471 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1472 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1473 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1474 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1475 }
1476 }
Romain Guy060b5c82010-03-04 10:07:38 -08001477
Daniel Sandler388f6792010-03-02 14:08:08 -05001478 public void dumpState() {
1479 Log.d(TAG, "mRS=" + mRS);
1480 Log.d(TAG, "mRollo=" + mRollo);
1481 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1482 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1483 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1484 Log.d(TAG, "mLastSelection=" + mLastSelection);
1485 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1486 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1487 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1488 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1489 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1490 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1491 Log.d(TAG, "mZoom=" + mZoom);
1492 Log.d(TAG, "mPosX=" + mPosX);
1493 Log.d(TAG, "mVelocity=" + mVelocity);
1494 Log.d(TAG, "mMessageProc=" + mMessageProc);
1495 if (mRollo != null) {
1496 mRollo.dumpState();
1497 }
1498 if (mRS != null) {
1499 mRS.contextDump(0);
1500 }
1501 }
1502}
1503
1504