blob: 0b236628cd7abc3b728063585a088745633f1ed8 [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
Romain Guyedcce092010-03-04 13:03:17 -080055import com.android.launcher.R;
56
Daniel Sandler388f6792010-03-02 14:08:08 -050057public class AllApps3D extends RSSurfaceView
58 implements AllAppsView, View.OnClickListener, View.OnLongClickListener, DragSource {
59 private static final String TAG = "Launcher.AllApps3D";
60
61 /** Bit for mLocks for when there are icons being loaded. */
62 private static final int LOCK_ICONS_PENDING = 1;
63
64 private static final int TRACKING_NONE = 0;
65 private static final int TRACKING_FLING = 1;
66 private static final int TRACKING_HOME = 2;
67
68 private static final int SELECTED_NONE = 0;
69 private static final int SELECTED_FOCUSED = 1;
70 private static final int SELECTED_PRESSED = 2;
71
72 private static final int SELECTION_NONE = 0;
73 private static final int SELECTION_ICONS = 1;
74 private static final int SELECTION_HOME = 2;
75
76 private Launcher mLauncher;
77 private DragController mDragController;
78
79 /** When this is 0, modifications are allowed, when it's not, they're not.
80 * TODO: What about scrolling? */
81 private int mLocks = LOCK_ICONS_PENDING;
82
83 private int mSlop;
84 private int mMaxFlingVelocity;
85
86 private Defines mDefines = new Defines();
87 private RenderScriptGL mRS;
88 private RolloRS mRollo;
89 private ArrayList<ApplicationInfo> mAllAppsList;
90
91 /**
92 * True when we are using arrow keys or trackball to drive navigation
93 */
94 private boolean mArrowNavigation = false;
95 private boolean mStartedScrolling;
96
97 /**
98 * Used to keep track of the selection when AllAppsView loses window focus.
99 * One of the SELECTION_ constants.
100 */
101 private int mLastSelection;
Romain Guy060b5c82010-03-04 10:07:38 -0800102
Daniel Sandler388f6792010-03-02 14:08:08 -0500103 /**
104 * Used to keep track of the selection when AllAppsView loses window focus
105 */
106 private int mLastSelectedIcon;
Romain Guy060b5c82010-03-04 10:07:38 -0800107
Daniel Sandler388f6792010-03-02 14:08:08 -0500108 private VelocityTracker mVelocityTracker;
109 private int mTouchTracking;
110 private int mMotionDownRawX;
111 private int mMotionDownRawY;
112 private int mDownIconIndex = -1;
113 private int mCurrentIconIndex = -1;
Romain Guy060b5c82010-03-04 10:07:38 -0800114
Daniel Sandler388f6792010-03-02 14:08:08 -0500115 private boolean mShouldGainFocus;
Romain Guy060b5c82010-03-04 10:07:38 -0800116
Daniel Sandler388f6792010-03-02 14:08:08 -0500117 private boolean mHaveSurface = false;
118 private boolean mZoomDirty = false;
119 private boolean mAnimateNextZoom;
120 private float mNextZoom;
121 private float mZoom;
122 private float mPosX;
123 private float mVelocity;
124 private AAMessage mMessageProc;
Romain Guy060b5c82010-03-04 10:07:38 -0800125
126 private int mColumnsPerPage;
127 private int mRowsPerPage;
128
129 @SuppressWarnings({"UnusedDeclaration"})
Daniel Sandler388f6792010-03-02 14:08:08 -0500130 static class Defines {
131 public static final int ALLOC_PARAMS = 0;
132 public static final int ALLOC_STATE = 1;
133 public static final int ALLOC_ICON_IDS = 3;
134 public static final int ALLOC_LABEL_IDS = 4;
135 public static final int ALLOC_VP_CONSTANTS = 5;
Romain Guy060b5c82010-03-04 10:07:38 -0800136
137 public static final int COLUMNS_PER_PAGE_PORTRAIT = 4;
138 public static final int ROWS_PER_PAGE_PORTRAIT = 4;
139
140 public static final int COLUMNS_PER_PAGE_LANDSCAPE = 6;
141 public static final int ROWS_PER_PAGE_LANDSCAPE = 3;
142
Daniel Sandler388f6792010-03-02 14:08:08 -0500143 public static final int ICON_WIDTH_PX = 64;
144 public static final int ICON_TEXTURE_WIDTH_PX = 74;
145 public static final int SELECTION_TEXTURE_WIDTH_PX = 74 + 20;
Romain Guy060b5c82010-03-04 10:07:38 -0800146
Daniel Sandler388f6792010-03-02 14:08:08 -0500147 public static final int ICON_HEIGHT_PX = 64;
148 public static final int ICON_TEXTURE_HEIGHT_PX = 74;
149 public static final int SELECTION_TEXTURE_HEIGHT_PX = 74 + 20;
150 }
Romain Guy060b5c82010-03-04 10:07:38 -0800151
Daniel Sandler388f6792010-03-02 14:08:08 -0500152 public AllApps3D(Context context, AttributeSet attrs) {
153 super(context, attrs);
154 setFocusable(true);
155 setSoundEffectsEnabled(false);
156 getHolder().setFormat(PixelFormat.TRANSLUCENT);
157 final ViewConfiguration config = ViewConfiguration.get(context);
158 mSlop = config.getScaledTouchSlop();
159 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
Romain Guy060b5c82010-03-04 10:07:38 -0800160
Daniel Sandler388f6792010-03-02 14:08:08 -0500161 setOnClickListener(this);
162 setOnLongClickListener(this);
163 setZOrderOnTop(true);
164 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Romain Guy060b5c82010-03-04 10:07:38 -0800165
Daniel Sandler388f6792010-03-02 14:08:08 -0500166 mRS = createRenderScript(true);
Romain Guy060b5c82010-03-04 10:07:38 -0800167
168 final DisplayMetrics metrics = getResources().getDisplayMetrics();
169 final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;
170 mColumnsPerPage = isPortrait ? Defines.COLUMNS_PER_PAGE_PORTRAIT :
171 Defines.COLUMNS_PER_PAGE_LANDSCAPE;
172 mRowsPerPage = isPortrait ? Defines.ROWS_PER_PAGE_PORTRAIT :
173 Defines.ROWS_PER_PAGE_LANDSCAPE;
Daniel Sandler388f6792010-03-02 14:08:08 -0500174 }
Romain Guy060b5c82010-03-04 10:07:38 -0800175
176 @SuppressWarnings({"UnusedDeclaration"})
177 public AllApps3D(Context context, AttributeSet attrs, int defStyle) {
178 this(context, attrs);
179 }
180
Daniel Sandler388f6792010-03-02 14:08:08 -0500181 /**
182 * Note that this implementation prohibits this view from ever being reattached.
183 */
184 @Override
185 protected void onDetachedFromWindow() {
186 destroyRenderScript();
187 mRS.mMessageCallback = null;
188 mRS = null;
189 }
Romain Guy060b5c82010-03-04 10:07:38 -0800190
Daniel Sandler388f6792010-03-02 14:08:08 -0500191 /**
192 * If you have an attached click listener, View always plays the click sound!?!?
193 * Deal with sound effects by hand.
194 */
195 public void reallyPlaySoundEffect(int sound) {
196 boolean old = isSoundEffectsEnabled();
197 setSoundEffectsEnabled(true);
198 playSoundEffect(sound);
199 setSoundEffectsEnabled(old);
200 }
Romain Guy060b5c82010-03-04 10:07:38 -0800201
Daniel Sandler388f6792010-03-02 14:08:08 -0500202 public void setLauncher(Launcher launcher) {
203 mLauncher = launcher;
204 }
Romain Guy060b5c82010-03-04 10:07:38 -0800205
Daniel Sandler388f6792010-03-02 14:08:08 -0500206 @Override
207 public void surfaceDestroyed(SurfaceHolder holder) {
208 super.surfaceDestroyed(holder);
209 // Without this, we leak mMessageCallback which leaks the context.
210 mRS.mMessageCallback = null;
211 // We may lose any callbacks that are pending, so make sure that we re-sync that
212 // on the next surfaceChanged.
213 mZoomDirty = true;
214 mHaveSurface = false;
215 }
Romain Guy060b5c82010-03-04 10:07:38 -0800216
Daniel Sandler388f6792010-03-02 14:08:08 -0500217 @Override
218 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
219 //long startTime = SystemClock.uptimeMillis();
Romain Guy060b5c82010-03-04 10:07:38 -0800220
Daniel Sandler388f6792010-03-02 14:08:08 -0500221 super.surfaceChanged(holder, format, w, h);
Romain Guy060b5c82010-03-04 10:07:38 -0800222
Daniel Sandler388f6792010-03-02 14:08:08 -0500223 mHaveSurface = true;
Romain Guy060b5c82010-03-04 10:07:38 -0800224
Daniel Sandler388f6792010-03-02 14:08:08 -0500225 if (mRollo == null) {
226 mRollo = new RolloRS();
227 mRollo.init(getResources(), w, h);
228 if (mAllAppsList != null) {
229 mRollo.setApps(mAllAppsList);
230 }
231 if (mShouldGainFocus) {
232 gainFocus();
233 mShouldGainFocus = false;
234 }
235 }
236 mRollo.dirtyCheck();
237 mRollo.resize(w, h);
Romain Guy060b5c82010-03-04 10:07:38 -0800238
Daniel Sandler388f6792010-03-02 14:08:08 -0500239 if (mRS != null) {
240 mRS.mMessageCallback = mMessageProc = new AAMessage();
241 }
Romain Guy060b5c82010-03-04 10:07:38 -0800242
Daniel Sandler388f6792010-03-02 14:08:08 -0500243 if (mRollo.mUniformAlloc != null) {
244 float tf[] = new float[] {72.f, 72.f,
245 120.f, 120.f, 0.f, 0.f,
246 120.f, 680.f,
247 (2.f / 480.f), 0, -((float)w / 2) - 0.25f, -380.25f};
248 if (w > h) {
249 tf[6] = 40.f;
250 tf[7] = h - 40.f;
251 tf[9] = 1.f;
252 tf[10] = -((float)w / 2) - 0.25f;
253 tf[11] = -((float)h / 2) - 0.25f;
254 }
Romain Guy060b5c82010-03-04 10:07:38 -0800255
Daniel Sandler388f6792010-03-02 14:08:08 -0500256 mRollo.mUniformAlloc.data(tf);
257 }
Romain Guy060b5c82010-03-04 10:07:38 -0800258
Daniel Sandler388f6792010-03-02 14:08:08 -0500259 //long endTime = SystemClock.uptimeMillis();
260 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
261 }
Romain Guy060b5c82010-03-04 10:07:38 -0800262
Daniel Sandler388f6792010-03-02 14:08:08 -0500263 @Override
264 public void onWindowFocusChanged(boolean hasWindowFocus) {
265 super.onWindowFocusChanged(hasWindowFocus);
266 if (mArrowNavigation) {
267 if (!hasWindowFocus) {
268 // Clear selection when we lose window focus
269 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
270 mRollo.setHomeSelected(SELECTED_NONE);
271 mRollo.clearSelectedIcon();
272 mRollo.mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -0800273 } else {
Daniel Sandler388f6792010-03-02 14:08:08 -0500274 if (mRollo.mState.iconCount > 0) {
275 if (mLastSelection == SELECTION_ICONS) {
276 int selection = mLastSelectedIcon;
277 final int firstIcon = Math.round(mPosX) *
Romain Guy060b5c82010-03-04 10:07:38 -0800278 mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500279 if (selection < 0 || // No selection
280 selection < firstIcon || // off the top of the screen
281 selection >= mRollo.mState.iconCount || // past last icon
282 selection >= firstIcon + // past last icon on screen
Romain Guy060b5c82010-03-04 10:07:38 -0800283 (mColumnsPerPage * mRowsPerPage)) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500284 selection = firstIcon;
285 }
Romain Guy060b5c82010-03-04 10:07:38 -0800286
Daniel Sandler388f6792010-03-02 14:08:08 -0500287 // Select the first icon when we gain window focus
288 mRollo.selectIcon(selection, SELECTED_FOCUSED);
289 mRollo.mState.save();
290 } else if (mLastSelection == SELECTION_HOME) {
291 mRollo.setHomeSelected(SELECTED_FOCUSED);
292 mRollo.mState.save();
293 }
294 }
295 }
296 }
297 }
Romain Guy060b5c82010-03-04 10:07:38 -0800298
Daniel Sandler388f6792010-03-02 14:08:08 -0500299 @Override
300 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
301 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Romain Guy060b5c82010-03-04 10:07:38 -0800302
Daniel Sandler388f6792010-03-02 14:08:08 -0500303 if (!isVisible()) {
304 return;
305 }
Romain Guy060b5c82010-03-04 10:07:38 -0800306
Daniel Sandler388f6792010-03-02 14:08:08 -0500307 if (gainFocus) {
308 if (mRollo != null) {
309 gainFocus();
310 } else {
311 mShouldGainFocus = true;
312 }
313 } else {
314 if (mRollo != null) {
315 if (mArrowNavigation) {
316 // Clear selection when we lose focus
317 mRollo.clearSelectedIcon();
318 mRollo.setHomeSelected(SELECTED_NONE);
319 mRollo.mState.save();
320 mArrowNavigation = false;
321 }
322 } else {
323 mShouldGainFocus = false;
324 }
325 }
326 }
Romain Guy060b5c82010-03-04 10:07:38 -0800327
Daniel Sandler388f6792010-03-02 14:08:08 -0500328 private void gainFocus() {
329 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
330 // Select the first icon when we gain keyboard focus
331 mArrowNavigation = true;
Romain Guy060b5c82010-03-04 10:07:38 -0800332 mRollo.selectIcon(Math.round(mPosX) * mColumnsPerPage,
Daniel Sandler388f6792010-03-02 14:08:08 -0500333 SELECTED_FOCUSED);
334 mRollo.mState.save();
335 }
336 }
Romain Guy060b5c82010-03-04 10:07:38 -0800337
Daniel Sandler388f6792010-03-02 14:08:08 -0500338 @Override
339 public boolean onKeyDown(int keyCode, KeyEvent event) {
Romain Guy060b5c82010-03-04 10:07:38 -0800340
Daniel Sandler388f6792010-03-02 14:08:08 -0500341 boolean handled = false;
Romain Guy060b5c82010-03-04 10:07:38 -0800342
Daniel Sandler388f6792010-03-02 14:08:08 -0500343 if (!isVisible()) {
344 return false;
345 }
346 final int iconCount = mRollo.mState.iconCount;
Romain Guy060b5c82010-03-04 10:07:38 -0800347
Daniel Sandler388f6792010-03-02 14:08:08 -0500348 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
349 if (mArrowNavigation) {
350 if (mLastSelection == SELECTION_HOME) {
351 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
352 mLauncher.closeAllApps(true);
353 } else {
354 int whichApp = mRollo.mState.selectedIconIndex;
355 if (whichApp >= 0) {
356 ApplicationInfo app = mAllAppsList.get(whichApp);
357 mLauncher.startActivitySafely(app.intent);
358 handled = true;
359 }
360 }
361 }
362 }
Romain Guy060b5c82010-03-04 10:07:38 -0800363
Daniel Sandler388f6792010-03-02 14:08:08 -0500364 if (iconCount > 0) {
365 mArrowNavigation = true;
Romain Guy060b5c82010-03-04 10:07:38 -0800366
Daniel Sandler388f6792010-03-02 14:08:08 -0500367 int currentSelection = mRollo.mState.selectedIconIndex;
368 int currentTopRow = Math.round(mPosX);
Romain Guy060b5c82010-03-04 10:07:38 -0800369
370 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE_PORTRAIT-1
371 final int currentPageCol = currentSelection % mColumnsPerPage;
372
373 // The row of the current selection, in the range 0..ROWS_PER_PAGE_PORTRAIT-1
374 final int currentPageRow = (currentSelection - (currentTopRow* mColumnsPerPage))
375 / mRowsPerPage;
376
Daniel Sandler388f6792010-03-02 14:08:08 -0500377 int newSelection = currentSelection;
Romain Guy060b5c82010-03-04 10:07:38 -0800378
Daniel Sandler388f6792010-03-02 14:08:08 -0500379 switch (keyCode) {
380 case KeyEvent.KEYCODE_DPAD_UP:
381 if (mLastSelection == SELECTION_HOME) {
382 mRollo.setHomeSelected(SELECTED_NONE);
Romain Guy060b5c82010-03-04 10:07:38 -0800383 int lastRowCount = iconCount % mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500384 if (lastRowCount == 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800385 lastRowCount = mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500386 }
Romain Guy060b5c82010-03-04 10:07:38 -0800387 newSelection = iconCount - lastRowCount + (mColumnsPerPage / 2);
Daniel Sandler388f6792010-03-02 14:08:08 -0500388 if (newSelection >= iconCount) {
389 newSelection = iconCount-1;
390 }
Romain Guy060b5c82010-03-04 10:07:38 -0800391 int target = (newSelection / mColumnsPerPage)
392 - (mRowsPerPage - 1);
Daniel Sandler388f6792010-03-02 14:08:08 -0500393 if (target < 0) {
394 target = 0;
395 }
396 if (currentTopRow != target) {
397 mRollo.moveTo(target);
398 }
399 } else {
400 if (currentPageRow > 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800401 newSelection = currentSelection - mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500402 } else if (currentTopRow > 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800403 newSelection = currentSelection - mColumnsPerPage;
404 mRollo.moveTo(newSelection / mColumnsPerPage);
Daniel Sandler388f6792010-03-02 14:08:08 -0500405 } else if (currentPageRow != 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800406 newSelection = currentTopRow * mRowsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500407 }
408 }
409 handled = true;
410 break;
Romain Guy060b5c82010-03-04 10:07:38 -0800411
Daniel Sandler388f6792010-03-02 14:08:08 -0500412 case KeyEvent.KEYCODE_DPAD_DOWN: {
Romain Guy060b5c82010-03-04 10:07:38 -0800413 final int rowCount = iconCount / mColumnsPerPage
414 + (iconCount % mColumnsPerPage == 0 ? 0 : 1);
415 final int currentRow = currentSelection / mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500416 if (mLastSelection != SELECTION_HOME) {
417 if (currentRow < rowCount-1) {
418 mRollo.setHomeSelected(SELECTED_NONE);
419 if (currentSelection < 0) {
420 newSelection = 0;
421 } else {
Romain Guy060b5c82010-03-04 10:07:38 -0800422 newSelection = currentSelection + mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500423 }
424 if (newSelection >= iconCount) {
425 // Go from D to G in this arrangement:
426 // A B C D
427 // E F G
428 newSelection = iconCount - 1;
429 }
Romain Guy060b5c82010-03-04 10:07:38 -0800430 if (currentPageRow >= mRowsPerPage - 1) {
431 mRollo.moveTo((newSelection / mColumnsPerPage) -
432 mRowsPerPage + 1);
Daniel Sandler388f6792010-03-02 14:08:08 -0500433 }
434 } else {
435 newSelection = -1;
436 mRollo.setHomeSelected(SELECTED_FOCUSED);
437 }
438 }
439 handled = true;
440 break;
441 }
442 case KeyEvent.KEYCODE_DPAD_LEFT:
443 if (mLastSelection != SELECTION_HOME) {
444 if (currentPageCol > 0) {
445 newSelection = currentSelection - 1;
446 }
447 }
448 handled = true;
449 break;
450 case KeyEvent.KEYCODE_DPAD_RIGHT:
451 if (mLastSelection != SELECTION_HOME) {
Romain Guy060b5c82010-03-04 10:07:38 -0800452 if ((currentPageCol < mColumnsPerPage - 1) &&
Daniel Sandler388f6792010-03-02 14:08:08 -0500453 (currentSelection < iconCount - 1)) {
454 newSelection = currentSelection + 1;
455 }
456 }
457 handled = true;
458 break;
459 }
460 if (newSelection != currentSelection) {
461 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
462 mRollo.mState.save();
463 }
464 }
465 return handled;
466 }
Romain Guy060b5c82010-03-04 10:07:38 -0800467
Daniel Sandler388f6792010-03-02 14:08:08 -0500468 @Override
469 public boolean onTouchEvent(MotionEvent ev)
470 {
471 mArrowNavigation = false;
Romain Guy060b5c82010-03-04 10:07:38 -0800472
Daniel Sandler388f6792010-03-02 14:08:08 -0500473 if (!isVisible()) {
474 return true;
475 }
Romain Guy060b5c82010-03-04 10:07:38 -0800476
Daniel Sandler388f6792010-03-02 14:08:08 -0500477 if (mLocks != 0) {
478 return true;
479 }
Romain Guy060b5c82010-03-04 10:07:38 -0800480
Daniel Sandler388f6792010-03-02 14:08:08 -0500481 super.onTouchEvent(ev);
Romain Guy060b5c82010-03-04 10:07:38 -0800482
Daniel Sandler388f6792010-03-02 14:08:08 -0500483 int x = (int)ev.getX();
484 int y = (int)ev.getY();
Romain Guy060b5c82010-03-04 10:07:38 -0800485
Romain Guyce115852010-03-04 12:15:37 -0800486 final boolean isPortrait = getWidth() < getHeight();
Daniel Sandler388f6792010-03-02 14:08:08 -0500487 int action = ev.getAction();
488 switch (action) {
489 case MotionEvent.ACTION_DOWN:
Romain Guyce115852010-03-04 12:15:37 -0800490 if ((isPortrait && y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) ||
491 (!isPortrait && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500492 mTouchTracking = TRACKING_HOME;
493 mRollo.setHomeSelected(SELECTED_PRESSED);
494 mRollo.mState.save();
495 mCurrentIconIndex = -1;
496 } else {
497 mTouchTracking = TRACKING_FLING;
Romain Guy060b5c82010-03-04 10:07:38 -0800498
Daniel Sandler388f6792010-03-02 14:08:08 -0500499 mMotionDownRawX = (int)ev.getRawX();
500 mMotionDownRawY = (int)ev.getRawY();
Romain Guy060b5c82010-03-04 10:07:38 -0800501
Daniel Sandler388f6792010-03-02 14:08:08 -0500502 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
503 mRollo.mState.newTouchDown = 1;
Romain Guy060b5c82010-03-04 10:07:38 -0800504
Daniel Sandler388f6792010-03-02 14:08:08 -0500505 if (!mRollo.checkClickOK()) {
506 mRollo.clearSelectedIcon();
507 } else {
508 mDownIconIndex = mCurrentIconIndex
509 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
510 if (mDownIconIndex < 0) {
511 // if nothing was selected, no long press.
512 cancelLongPress();
513 }
514 }
515 mRollo.mState.save();
516 mRollo.move();
517 mVelocityTracker = VelocityTracker.obtain();
518 mVelocityTracker.addMovement(ev);
519 mStartedScrolling = false;
520 }
521 break;
522 case MotionEvent.ACTION_MOVE:
523 case MotionEvent.ACTION_OUTSIDE:
524 if (mTouchTracking == TRACKING_HOME) {
Romain Guyce115852010-03-04 12:15:37 -0800525 mRollo.setHomeSelected((isPortrait &&
526 y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) || (!isPortrait
527 && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])
Daniel Sandler388f6792010-03-02 14:08:08 -0500528 ? SELECTED_PRESSED : SELECTED_NONE);
529 mRollo.mState.save();
530 } else if (mTouchTracking == TRACKING_FLING) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500531 int rawY = (int)ev.getRawY();
532 int slop;
533 slop = Math.abs(rawY - mMotionDownRawY);
Romain Guy060b5c82010-03-04 10:07:38 -0800534
Daniel Sandler388f6792010-03-02 14:08:08 -0500535 if (!mStartedScrolling && slop < mSlop) {
536 // don't update anything so when we do start scrolling
537 // below, we get the right delta.
538 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
539 if (mDownIconIndex != mCurrentIconIndex) {
540 // If a different icon is selected, don't allow it to be picked up.
541 // This handles off-axis dragging.
542 cancelLongPress();
543 mCurrentIconIndex = -1;
544 }
545 } else {
546 if (!mStartedScrolling) {
547 cancelLongPress();
548 mCurrentIconIndex = -1;
549 }
550 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
551 mRollo.mState.newTouchDown = 1;
552 mRollo.move();
Romain Guy060b5c82010-03-04 10:07:38 -0800553
Daniel Sandler388f6792010-03-02 14:08:08 -0500554 mStartedScrolling = true;
555 mRollo.clearSelectedIcon();
556 mVelocityTracker.addMovement(ev);
557 mRollo.mState.save();
558 }
559 }
560 break;
561 case MotionEvent.ACTION_UP:
562 case MotionEvent.ACTION_CANCEL:
563 if (mTouchTracking == TRACKING_HOME) {
564 if (action == MotionEvent.ACTION_UP) {
Romain Guyce115852010-03-04 12:15:37 -0800565 if ((isPortrait && y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) ||
566 (!isPortrait && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500567 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
568 mLauncher.closeAllApps(true);
569 }
570 mRollo.setHomeSelected(SELECTED_NONE);
571 mRollo.mState.save();
572 }
573 mCurrentIconIndex = -1;
574 } else if (mTouchTracking == TRACKING_FLING) {
575 mRollo.mState.newTouchDown = 0;
576 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Romain Guy060b5c82010-03-04 10:07:38 -0800577
Daniel Sandler388f6792010-03-02 14:08:08 -0500578 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
579 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
580 mRollo.clearSelectedIcon();
581 mRollo.mState.save();
582 mRollo.fling();
Romain Guy060b5c82010-03-04 10:07:38 -0800583
Daniel Sandler388f6792010-03-02 14:08:08 -0500584 if (mVelocityTracker != null) {
585 mVelocityTracker.recycle();
586 mVelocityTracker = null;
587 }
588 }
589 mTouchTracking = TRACKING_NONE;
590 break;
591 }
Romain Guy060b5c82010-03-04 10:07:38 -0800592
Daniel Sandler388f6792010-03-02 14:08:08 -0500593 return true;
594 }
Romain Guy060b5c82010-03-04 10:07:38 -0800595
Daniel Sandler388f6792010-03-02 14:08:08 -0500596 public void onClick(View v) {
597 if (mLocks != 0 || !isVisible()) {
598 return;
599 }
600 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
601 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
602 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
603 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
604 mLauncher.startActivitySafely(app.intent);
605 }
606 }
Romain Guy060b5c82010-03-04 10:07:38 -0800607
Daniel Sandler388f6792010-03-02 14:08:08 -0500608 public boolean onLongClick(View v) {
609 if (mLocks != 0 || !isVisible()) {
610 return true;
611 }
612 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
613 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
614 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Romain Guy060b5c82010-03-04 10:07:38 -0800615
Daniel Sandler388f6792010-03-02 14:08:08 -0500616 Bitmap bmp = app.iconBitmap;
617 final int w = bmp.getWidth();
618 final int h = bmp.getHeight();
Romain Guy060b5c82010-03-04 10:07:38 -0800619
Daniel Sandler388f6792010-03-02 14:08:08 -0500620 // We don't really have an accurate location to use. This will do.
621 int screenX = mMotionDownRawX - (w / 2);
622 int screenY = mMotionDownRawY - h;
Romain Guy060b5c82010-03-04 10:07:38 -0800623
Daniel Sandler388f6792010-03-02 14:08:08 -0500624 mDragController.startDrag(bmp, screenX, screenY,
625 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Romain Guy060b5c82010-03-04 10:07:38 -0800626
Daniel Sandler388f6792010-03-02 14:08:08 -0500627 mLauncher.closeAllApps(true);
628 }
629 return true;
630 }
Romain Guy060b5c82010-03-04 10:07:38 -0800631
Daniel Sandler388f6792010-03-02 14:08:08 -0500632 @Override
633 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
634 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
635 if (!isVisible()) {
636 return false;
637 }
638 String text = null;
639 int index;
640 int count = mAllAppsList.size() + 1; // +1 is home
641 int pos = -1;
642 switch (mLastSelection) {
643 case SELECTION_ICONS:
644 index = mRollo.mState.selectedIconIndex;
645 if (index >= 0) {
646 ApplicationInfo info = mAllAppsList.get(index);
647 if (info.title != null) {
648 text = info.title.toString();
649 pos = index;
650 }
651 }
652 break;
653 case SELECTION_HOME:
654 text = getContext().getString(R.string.all_apps_home_button_label);
655 pos = count;
656 break;
657 }
658 if (text != null) {
659 event.setEnabled(true);
660 event.getText().add(text);
661 //event.setContentDescription(text);
662 event.setItemCount(count);
663 event.setCurrentItemIndex(pos);
664 }
665 }
666 return false;
667 }
Romain Guy060b5c82010-03-04 10:07:38 -0800668
Daniel Sandler388f6792010-03-02 14:08:08 -0500669 public void setDragController(DragController dragger) {
670 mDragController = dragger;
671 }
Romain Guy060b5c82010-03-04 10:07:38 -0800672
Daniel Sandler388f6792010-03-02 14:08:08 -0500673 public void onDropCompleted(View target, boolean success) {
674 }
Romain Guy060b5c82010-03-04 10:07:38 -0800675
Daniel Sandler388f6792010-03-02 14:08:08 -0500676 /**
677 * Zoom to the specifed level.
678 *
679 * @param zoom [0..1] 0 is hidden, 1 is open
680 */
681 public void zoom(float zoom, boolean animate) {
682 cancelLongPress();
683 mNextZoom = zoom;
684 mAnimateNextZoom = animate;
685 // if we do setZoom while we don't have a surface, we won't
686 // get the callbacks that actually set mZoom.
687 if (mRollo == null || !mHaveSurface) {
688 mZoomDirty = true;
689 mZoom = zoom;
Daniel Sandler388f6792010-03-02 14:08:08 -0500690 } else {
691 mRollo.setZoom(zoom, animate);
692 }
693 }
Romain Guy060b5c82010-03-04 10:07:38 -0800694
Daniel Sandler388f6792010-03-02 14:08:08 -0500695 public boolean isVisible() {
696 return mZoom > 0.001f;
697 }
Romain Guy060b5c82010-03-04 10:07:38 -0800698
Daniel Sandler388f6792010-03-02 14:08:08 -0500699 public boolean isOpaque() {
700 return mZoom > 0.999f;
701 }
Romain Guy060b5c82010-03-04 10:07:38 -0800702
Daniel Sandler388f6792010-03-02 14:08:08 -0500703 public void setApps(ArrayList<ApplicationInfo> list) {
704 if (mRS == null) {
705 // We've been removed from the window. Don't bother with all this.
706 return;
707 }
Romain Guy060b5c82010-03-04 10:07:38 -0800708
Daniel Sandler388f6792010-03-02 14:08:08 -0500709 mAllAppsList = list;
710 if (mRollo != null) {
711 mRollo.setApps(list);
712 }
713 mLocks &= ~LOCK_ICONS_PENDING;
714 }
Romain Guy060b5c82010-03-04 10:07:38 -0800715
Daniel Sandler388f6792010-03-02 14:08:08 -0500716 public void addApps(ArrayList<ApplicationInfo> list) {
717 if (mAllAppsList == null) {
718 // Not done loading yet. We'll find out about it later.
719 return;
720 }
721 if (mRS == null) {
722 // We've been removed from the window. Don't bother with all this.
723 return;
724 }
Romain Guy060b5c82010-03-04 10:07:38 -0800725
Daniel Sandler388f6792010-03-02 14:08:08 -0500726 final int N = list.size();
727 if (mRollo != null) {
728 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
729 }
Romain Guy060b5c82010-03-04 10:07:38 -0800730
Daniel Sandler388f6792010-03-02 14:08:08 -0500731 for (int i=0; i<N; i++) {
732 final ApplicationInfo item = list.get(i);
733 int index = Collections.binarySearch(mAllAppsList, item,
734 LauncherModel.APP_NAME_COMPARATOR);
735 if (index < 0) {
736 index = -(index+1);
737 }
738 mAllAppsList.add(index, item);
739 if (mRollo != null) {
740 mRollo.addApp(index, item);
741 }
742 }
Romain Guy060b5c82010-03-04 10:07:38 -0800743
Daniel Sandler388f6792010-03-02 14:08:08 -0500744 if (mRollo != null) {
745 mRollo.saveAppsList();
746 }
747 }
Romain Guy060b5c82010-03-04 10:07:38 -0800748
Daniel Sandler388f6792010-03-02 14:08:08 -0500749 public void removeApps(ArrayList<ApplicationInfo> list) {
750 if (mAllAppsList == null) {
751 // Not done loading yet. We'll find out about it later.
752 return;
753 }
Romain Guy060b5c82010-03-04 10:07:38 -0800754
Daniel Sandler388f6792010-03-02 14:08:08 -0500755 final int N = list.size();
756 for (int i=0; i<N; i++) {
757 final ApplicationInfo item = list.get(i);
758 int index = findAppByComponent(mAllAppsList, item);
759 if (index >= 0) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500760 mAllAppsList.remove(index);
761 if (mRollo != null) {
762 mRollo.removeApp(index);
763 }
764 } else {
765 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
766 // Try to recover. This should keep us from crashing for now.
767 }
768 }
Romain Guy060b5c82010-03-04 10:07:38 -0800769
Daniel Sandler388f6792010-03-02 14:08:08 -0500770 if (mRollo != null) {
771 mRollo.saveAppsList();
772 }
773 }
Romain Guy060b5c82010-03-04 10:07:38 -0800774
Daniel Sandler388f6792010-03-02 14:08:08 -0500775 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
776 // Just remove and add, because they may need to be re-sorted.
777 removeApps(list);
778 addApps(list);
779 }
Romain Guy060b5c82010-03-04 10:07:38 -0800780
Daniel Sandler388f6792010-03-02 14:08:08 -0500781 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
782 ComponentName component = item.intent.getComponent();
783 final int N = list.size();
784 for (int i=0; i<N; i++) {
785 ApplicationInfo x = list.get(i);
786 if (x.intent.getComponent().equals(component)) {
787 return i;
788 }
789 }
790 return -1;
791 }
Romain Guy060b5c82010-03-04 10:07:38 -0800792
793 /*
Daniel Sandler388f6792010-03-02 14:08:08 -0500794 private static int countPages(int iconCount) {
Romain Guy060b5c82010-03-04 10:07:38 -0800795 int iconsPerPage = getColumnsCount() * Defines.ROWS_PER_PAGE_PORTRAIT;
Daniel Sandler388f6792010-03-02 14:08:08 -0500796 int pages = iconCount / iconsPerPage;
797 if (pages*iconsPerPage != iconCount) {
798 pages++;
799 }
800 return pages;
801 }
Romain Guy060b5c82010-03-04 10:07:38 -0800802 */
Daniel Sandler388f6792010-03-02 14:08:08 -0500803
804 class AAMessage extends RenderScript.RSMessage {
805 public void run() {
806 mPosX = ((float)mData[0]) / (1 << 16);
807 mVelocity = ((float)mData[1]) / (1 << 16);
808 mZoom = ((float)mData[2]) / (1 << 16);
809 mZoomDirty = false;
810 }
811 }
Romain Guy060b5c82010-03-04 10:07:38 -0800812
Daniel Sandler388f6792010-03-02 14:08:08 -0500813 public class RolloRS {
Romain Guy060b5c82010-03-04 10:07:38 -0800814
Daniel Sandler388f6792010-03-02 14:08:08 -0500815 // Allocations ======
816 private int mWidth;
817 private int mHeight;
Romain Guy060b5c82010-03-04 10:07:38 -0800818
Daniel Sandler388f6792010-03-02 14:08:08 -0500819 private Resources mRes;
820 private Script mScript;
821 private Script.Invokable mInvokeMove;
822 private Script.Invokable mInvokeMoveTo;
823 private Script.Invokable mInvokeFling;
824 private Script.Invokable mInvokeResetWAR;
825 private Script.Invokable mInvokeSetZoom;
Romain Guy060b5c82010-03-04 10:07:38 -0800826
Daniel Sandler388f6792010-03-02 14:08:08 -0500827 private ProgramStore mPSIcons;
828 private ProgramFragment mPFTexMip;
829 private ProgramFragment mPFTexMipAlpha;
830 private ProgramFragment mPFTexNearest;
831 private ProgramVertex mPV;
832 private ProgramVertex mPVCurve;
833 private SimpleMesh mMesh;
834 private ProgramVertex.MatrixAllocation mPVA;
Romain Guy060b5c82010-03-04 10:07:38 -0800835
Daniel Sandler388f6792010-03-02 14:08:08 -0500836 private Allocation mUniformAlloc;
Romain Guy060b5c82010-03-04 10:07:38 -0800837
Daniel Sandler388f6792010-03-02 14:08:08 -0500838 private Allocation mHomeButtonNormal;
839 private Allocation mHomeButtonFocused;
840 private Allocation mHomeButtonPressed;
Romain Guy060b5c82010-03-04 10:07:38 -0800841
Daniel Sandler388f6792010-03-02 14:08:08 -0500842 private Allocation[] mIcons;
843 private int[] mIconIds;
844 private Allocation mAllocIconIds;
Romain Guy060b5c82010-03-04 10:07:38 -0800845
Daniel Sandler388f6792010-03-02 14:08:08 -0500846 private Allocation[] mLabels;
847 private int[] mLabelIds;
848 private Allocation mAllocLabelIds;
849 private Allocation mSelectedIcon;
Romain Guy060b5c82010-03-04 10:07:38 -0800850
Daniel Sandler388f6792010-03-02 14:08:08 -0500851 private int[] mTouchYBorders;
852 private int[] mTouchXBorders;
Romain Guy060b5c82010-03-04 10:07:38 -0800853
Daniel Sandler388f6792010-03-02 14:08:08 -0500854 private Bitmap mSelectionBitmap;
855 private Canvas mSelectionCanvas;
Romain Guy060b5c82010-03-04 10:07:38 -0800856
Daniel Sandler388f6792010-03-02 14:08:08 -0500857 Params mParams;
858 State mState;
Romain Guy060b5c82010-03-04 10:07:38 -0800859
Daniel Sandler388f6792010-03-02 14:08:08 -0500860 class BaseAlloc {
861 Allocation mAlloc;
862 Type mType;
Romain Guy060b5c82010-03-04 10:07:38 -0800863
Daniel Sandler388f6792010-03-02 14:08:08 -0500864 void save() {
865 mAlloc.data(this);
866 }
867 }
Romain Guy060b5c82010-03-04 10:07:38 -0800868
Daniel Sandler388f6792010-03-02 14:08:08 -0500869 private boolean checkClickOK() {
870 return (Math.abs(mVelocity) < 0.4f) &&
871 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
872 }
Romain Guy060b5c82010-03-04 10:07:38 -0800873
Daniel Sandler388f6792010-03-02 14:08:08 -0500874 class Params extends BaseAlloc {
875 Params() {
876 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
877 mAlloc = Allocation.createTyped(mRS, mType);
878 save();
879 }
880 public int bubbleWidth;
881 public int bubbleHeight;
882 public int bubbleBitmapWidth;
883 public int bubbleBitmapHeight;
Romain Guy060b5c82010-03-04 10:07:38 -0800884
Daniel Sandler388f6792010-03-02 14:08:08 -0500885 public int homeButtonWidth;
886 public int homeButtonHeight;
887 public int homeButtonTextureWidth;
888 public int homeButtonTextureHeight;
889 }
Romain Guy060b5c82010-03-04 10:07:38 -0800890
Daniel Sandler388f6792010-03-02 14:08:08 -0500891 class State extends BaseAlloc {
892 public float newPositionX;
893 public int newTouchDown;
894 public float flingVelocity;
895 public int iconCount;
896 public int selectedIconIndex = -1;
897 public int selectedIconTexture;
898 public float zoomTarget;
899 public int homeButtonId;
900 public float targetPos;
Romain Guy060b5c82010-03-04 10:07:38 -0800901
Daniel Sandler388f6792010-03-02 14:08:08 -0500902 State() {
903 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
904 mAlloc = Allocation.createTyped(mRS, mType);
905 save();
906 }
907 }
Romain Guy060b5c82010-03-04 10:07:38 -0800908
Daniel Sandler388f6792010-03-02 14:08:08 -0500909 public RolloRS() {
910 }
Romain Guy060b5c82010-03-04 10:07:38 -0800911
Daniel Sandler388f6792010-03-02 14:08:08 -0500912 public void init(Resources res, int width, int height) {
913 mRes = res;
914 mWidth = width;
915 mHeight = height;
916 initProgramVertex();
917 initProgramFragment();
918 initProgramStore();
919 initGl();
920 initData();
921 initTouchState();
922 initRs();
923 }
Romain Guy060b5c82010-03-04 10:07:38 -0800924
Daniel Sandler388f6792010-03-02 14:08:08 -0500925 public void initMesh() {
926 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Romain Guy060b5c82010-03-04 10:07:38 -0800927
Daniel Sandler388f6792010-03-02 14:08:08 -0500928 for (int ct=0; ct < 16; ct++) {
929 float pos = (1.f / (16.f - 1)) * ct;
930 tm.addVertex(0.0f, pos);
931 tm.addVertex(1.0f, pos);
932 }
933 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
934 tm.addTriangle(ct, ct+1, ct+2);
935 tm.addTriangle(ct+1, ct+3, ct+2);
936 }
937 mMesh = tm.create();
938 mMesh.setName("SMCell");
939 }
Romain Guy060b5c82010-03-04 10:07:38 -0800940
Daniel Sandler388f6792010-03-02 14:08:08 -0500941 void resize(int w, int h) {
942 mPVA.setupProjectionNormalized(w, h);
943 mWidth = w;
944 mHeight = h;
945 }
Romain Guy060b5c82010-03-04 10:07:38 -0800946
Daniel Sandler388f6792010-03-02 14:08:08 -0500947 private void initProgramVertex() {
948 mPVA = new ProgramVertex.MatrixAllocation(mRS);
949 resize(mWidth, mHeight);
Romain Guy060b5c82010-03-04 10:07:38 -0800950
Daniel Sandler388f6792010-03-02 14:08:08 -0500951 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
952 pvb.setTextureMatrixEnable(true);
953 mPV = pvb.create();
954 mPV.setName("PV");
955 mPV.bindAllocation(mPVA);
Romain Guy060b5c82010-03-04 10:07:38 -0800956
Daniel Sandler388f6792010-03-02 14:08:08 -0500957 Element.Builder eb = new Element.Builder(mRS);
958 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
959 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
960 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "BendPos");
961 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
962 Element e = eb.create();
Romain Guy060b5c82010-03-04 10:07:38 -0800963
Daniel Sandler388f6792010-03-02 14:08:08 -0500964 mUniformAlloc = Allocation.createSized(mRS, e, 1);
Romain Guy060b5c82010-03-04 10:07:38 -0800965
Daniel Sandler388f6792010-03-02 14:08:08 -0500966 initMesh();
967 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
Romain Guy060b5c82010-03-04 10:07:38 -0800968 String t = "void main() {\n" +
969 // Animation
970 " float ani = UNI_Position.z;\n" +
971
972 " float bendY1 = UNI_BendPos.x;\n" +
973 " float bendY2 = UNI_BendPos.y;\n" +
974 " float bendAngle = 47.0 * (3.14 / 180.0);\n" +
975 " float bendDistance = bendY1 * 0.4;\n" +
976 " float distanceDimLevel = 0.6;\n" +
977
978 " float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
979 " float aDy = cos(bendAngle);\n" +
980 " float aDz = sin(bendAngle);\n" +
981
982 " float scale = (2.0 / 480.0);\n" +
983 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
984 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
985 " float y = 0.0;\n" +
986 " float z = 0.0;\n" +
987 " float lum = 1.0;\n" +
988
989 " float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
990 " y += cv * aDy;\n" +
991 " z += -cv * aDz;\n" +
992 " cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" + // curve range
993 " lum += cv / bendDistance * distanceDimLevel;\n" +
994 " y += cv * cos(cv * bendStep);\n" +
995 " z += cv * sin(cv * bendStep);\n" +
996
997 " cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
998 " y += cv * aDy;\n" +
999 " z += cv * aDz;\n" +
1000 " cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
1001 " lum -= cv / bendDistance * distanceDimLevel;\n" +
1002 " y += cv * cos(cv * bendStep);\n" +
1003 " z += cv * sin(cv * bendStep);\n" +
1004
1005 " y += clamp(ys, bendY1, bendY2);\n" +
1006
1007 " vec4 pos;\n" +
1008 " pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
1009 " pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
1010 " pos.z = z * UNI_ScaleOffset.x;\n" +
1011 " pos.w = 1.0;\n" +
1012
1013 " pos.x *= 1.0 + ani * 4.0;\n" +
1014 " pos.y *= 1.0 + ani * 4.0;\n" +
1015 " pos.z -= ani * 1.5;\n" +
1016 " lum *= 1.0 - ani;\n" +
1017
1018 " gl_Position = UNI_MVP * pos;\n" +
1019 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
1020 " varTex0.xy = ATTRIB_position;\n" +
1021 " varTex0.y = 1.0 - varTex0.y;\n" +
1022 " varTex0.zw = vec2(0.0, 0.0);\n" +
1023 "}\n";
Daniel Sandler388f6792010-03-02 14:08:08 -05001024 sb.setShader(t);
1025 sb.addConstant(mUniformAlloc.getType());
1026 sb.addInput(mMesh.getVertexType(0).getElement());
1027 mPVCurve = sb.create();
1028 mPVCurve.setName("PVCurve");
1029 mPVCurve.bindAllocation(mPVA);
1030 mPVCurve.bindConstants(mUniformAlloc, 1);
Romain Guy060b5c82010-03-04 10:07:38 -08001031
Daniel Sandler388f6792010-03-02 14:08:08 -05001032 mRS.contextBindProgramVertex(mPV);
1033 }
Romain Guy060b5c82010-03-04 10:07:38 -08001034
Daniel Sandler388f6792010-03-02 14:08:08 -05001035 private void initProgramFragment() {
1036 Sampler.Builder sb = new Sampler.Builder(mRS);
1037 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
1038 sb.setMag(Sampler.Value.NEAREST);
1039 sb.setWrapS(Sampler.Value.CLAMP);
1040 sb.setWrapT(Sampler.Value.CLAMP);
1041 Sampler linear = sb.create();
Romain Guy060b5c82010-03-04 10:07:38 -08001042
Daniel Sandler388f6792010-03-02 14:08:08 -05001043 sb.setMin(Sampler.Value.NEAREST);
1044 sb.setMag(Sampler.Value.NEAREST);
1045 Sampler nearest = sb.create();
Romain Guy060b5c82010-03-04 10:07:38 -08001046
Daniel Sandler388f6792010-03-02 14:08:08 -05001047 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
1048 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1049 ProgramFragment.Builder.Format.RGBA, 0);
1050 mPFTexMip = bf.create();
1051 mPFTexMip.setName("PFTexMip");
1052 mPFTexMip.bindSampler(linear, 0);
Romain Guy060b5c82010-03-04 10:07:38 -08001053
Daniel Sandler388f6792010-03-02 14:08:08 -05001054 mPFTexNearest = bf.create();
1055 mPFTexNearest.setName("PFTexNearest");
1056 mPFTexNearest.bindSampler(nearest, 0);
Romain Guy060b5c82010-03-04 10:07:38 -08001057
Daniel Sandler388f6792010-03-02 14:08:08 -05001058 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1059 ProgramFragment.Builder.Format.ALPHA, 0);
1060 mPFTexMipAlpha = bf.create();
1061 mPFTexMipAlpha.setName("PFTexMipAlpha");
1062 mPFTexMipAlpha.bindSampler(linear, 0);
Romain Guy060b5c82010-03-04 10:07:38 -08001063
Daniel Sandler388f6792010-03-02 14:08:08 -05001064 }
Romain Guy060b5c82010-03-04 10:07:38 -08001065
Daniel Sandler388f6792010-03-02 14:08:08 -05001066 private void initProgramStore() {
1067 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1068 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
1069 bs.setColorMask(true,true,true,false);
1070 bs.setDitherEnable(true);
1071 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1072 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1073 mPSIcons = bs.create();
1074 mPSIcons.setName("PSIcons");
1075 }
Romain Guy060b5c82010-03-04 10:07:38 -08001076
Daniel Sandler388f6792010-03-02 14:08:08 -05001077 private void initGl() {
Romain Guy060b5c82010-03-04 10:07:38 -08001078 mTouchXBorders = new int[mColumnsPerPage +1];
1079 mTouchYBorders = new int[mRowsPerPage +1];
Daniel Sandler388f6792010-03-02 14:08:08 -05001080 }
Romain Guy060b5c82010-03-04 10:07:38 -08001081
Daniel Sandler388f6792010-03-02 14:08:08 -05001082 private void initData() {
1083 mParams = new Params();
1084 mState = new State();
Romain Guy060b5c82010-03-04 10:07:38 -08001085
Daniel Sandler388f6792010-03-02 14:08:08 -05001086 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Romain Guy060b5c82010-03-04 10:07:38 -08001087
Daniel Sandler388f6792010-03-02 14:08:08 -05001088 mParams.bubbleWidth = bubble.getBubbleWidth();
1089 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1090 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1091 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Romain Guy060b5c82010-03-04 10:07:38 -08001092
Daniel Sandler388f6792010-03-02 14:08:08 -05001093 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1094 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1095 mHomeButtonNormal.uploadToTexture(0);
1096 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1097 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1098 mHomeButtonFocused.uploadToTexture(0);
1099 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1100 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1101 mHomeButtonPressed.uploadToTexture(0);
1102 mParams.homeButtonWidth = 76;
1103 mParams.homeButtonHeight = 68;
1104 mParams.homeButtonTextureWidth = 128;
1105 mParams.homeButtonTextureHeight = 128;
Romain Guy060b5c82010-03-04 10:07:38 -08001106
Daniel Sandler388f6792010-03-02 14:08:08 -05001107 mState.homeButtonId = mHomeButtonNormal.getID();
Romain Guy060b5c82010-03-04 10:07:38 -08001108
Daniel Sandler388f6792010-03-02 14:08:08 -05001109 mParams.save();
1110 mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -08001111
Daniel Sandler388f6792010-03-02 14:08:08 -05001112 mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1113 Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1114 mSelectionCanvas = new Canvas(mSelectionBitmap);
Romain Guy060b5c82010-03-04 10:07:38 -08001115
Daniel Sandler388f6792010-03-02 14:08:08 -05001116 setApps(null);
1117 }
Romain Guy060b5c82010-03-04 10:07:38 -08001118
Daniel Sandler388f6792010-03-02 14:08:08 -05001119 private void initRs() {
1120 ScriptC.Builder sb = new ScriptC.Builder(mRS);
1121 sb.setScript(mRes, R.raw.allapps);
1122 sb.setRoot(true);
1123 sb.addDefines(mDefines);
1124 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1125 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
1126 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
1127 mInvokeMove = sb.addInvokable("move");
1128 mInvokeFling = sb.addInvokable("fling");
1129 mInvokeMoveTo = sb.addInvokable("moveTo");
1130 mInvokeResetWAR = sb.addInvokable("resetHWWar");
1131 mInvokeSetZoom = sb.addInvokable("setZoom");
1132 mScript = sb.create();
1133 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1134 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1135 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1136 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1137 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
1138 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Romain Guy060b5c82010-03-04 10:07:38 -08001139
Daniel Sandler388f6792010-03-02 14:08:08 -05001140 mRS.contextBindRootScript(mScript);
1141 }
Romain Guy060b5c82010-03-04 10:07:38 -08001142
Daniel Sandler388f6792010-03-02 14:08:08 -05001143 void dirtyCheck() {
1144 if (mZoomDirty) {
1145 setZoom(mNextZoom, mAnimateNextZoom);
1146 }
1147 }
Romain Guy060b5c82010-03-04 10:07:38 -08001148
1149 @SuppressWarnings({"ConstantConditions"})
Daniel Sandler388f6792010-03-02 14:08:08 -05001150 private void setApps(ArrayList<ApplicationInfo> list) {
1151 final int count = list != null ? list.size() : 0;
1152 int allocCount = count;
1153 if (allocCount < 1) {
1154 allocCount = 1;
1155 }
Romain Guy060b5c82010-03-04 10:07:38 -08001156
Daniel Sandler388f6792010-03-02 14:08:08 -05001157 mIcons = new Allocation[count];
1158 mIconIds = new int[allocCount];
1159 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Romain Guy060b5c82010-03-04 10:07:38 -08001160
Daniel Sandler388f6792010-03-02 14:08:08 -05001161 mLabels = new Allocation[count];
1162 mLabelIds = new int[allocCount];
1163 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Romain Guy060b5c82010-03-04 10:07:38 -08001164
Daniel Sandler388f6792010-03-02 14:08:08 -05001165 mState.iconCount = count;
1166 for (int i=0; i < mState.iconCount; i++) {
1167 createAppIconAllocations(i, list.get(i));
1168 }
1169 for (int i=0; i < mState.iconCount; i++) {
1170 uploadAppIcon(i, list.get(i));
1171 }
1172 saveAppsList();
1173 }
Romain Guy060b5c82010-03-04 10:07:38 -08001174
Daniel Sandler388f6792010-03-02 14:08:08 -05001175 private void setZoom(float zoom, boolean animate) {
1176 mRollo.clearSelectedIcon();
1177 mRollo.setHomeSelected(SELECTED_NONE);
1178 if (zoom > 0.001f) {
1179 mRollo.mState.zoomTarget = zoom;
1180 } else {
1181 mRollo.mState.zoomTarget = 0;
1182 }
1183 mRollo.mState.save();
1184 if (!animate) {
1185 mRollo.mInvokeSetZoom.execute();
1186 }
1187 }
Romain Guy060b5c82010-03-04 10:07:38 -08001188
Daniel Sandler388f6792010-03-02 14:08:08 -05001189 private void createAppIconAllocations(int index, ApplicationInfo item) {
1190 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
1191 Element.RGBA_8888(mRS), true);
1192 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
1193 Element.A_8(mRS), true);
1194 mIconIds[index] = mIcons[index].getID();
1195 mLabelIds[index] = mLabels[index].getID();
1196 }
Romain Guy060b5c82010-03-04 10:07:38 -08001197
Daniel Sandler388f6792010-03-02 14:08:08 -05001198 private void uploadAppIcon(int index, ApplicationInfo item) {
1199 if (mIconIds[index] != mIcons[index].getID()) {
1200 throw new IllegalStateException("uploadAppIcon index=" + index
1201 + " mIcons[index].getID=" + mIcons[index].getID()
1202 + " mIconsIds[index]=" + mIconIds[index]
1203 + " item=" + item);
1204 }
1205 mIcons[index].uploadToTexture(0);
1206 mLabels[index].uploadToTexture(0);
1207 }
Romain Guy060b5c82010-03-04 10:07:38 -08001208
Daniel Sandler388f6792010-03-02 14:08:08 -05001209 /**
1210 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1211 * fill in the values and call saveAppsList().
1212 */
1213 private void reallocAppsList(int count) {
1214 Allocation[] icons = new Allocation[count];
1215 int[] iconIds = new int[count];
1216 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Romain Guy060b5c82010-03-04 10:07:38 -08001217
Daniel Sandler388f6792010-03-02 14:08:08 -05001218 Allocation[] labels = new Allocation[count];
1219 int[] labelIds = new int[count];
1220 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Romain Guy060b5c82010-03-04 10:07:38 -08001221
Daniel Sandler388f6792010-03-02 14:08:08 -05001222 final int oldCount = mRollo.mState.iconCount;
Romain Guy060b5c82010-03-04 10:07:38 -08001223
Daniel Sandler388f6792010-03-02 14:08:08 -05001224 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1225 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1226 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1227 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
Romain Guy060b5c82010-03-04 10:07:38 -08001228
Daniel Sandler388f6792010-03-02 14:08:08 -05001229 mIcons = icons;
1230 mIconIds = iconIds;
1231 mLabels = labels;
1232 mLabelIds = labelIds;
1233 }
Romain Guy060b5c82010-03-04 10:07:38 -08001234
Daniel Sandler388f6792010-03-02 14:08:08 -05001235 /**
1236 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1237 */
1238 private void addApp(int index, ApplicationInfo item) {
1239 final int count = mState.iconCount - index;
1240 final int dest = index + 1;
Romain Guy060b5c82010-03-04 10:07:38 -08001241
Daniel Sandler388f6792010-03-02 14:08:08 -05001242 System.arraycopy(mIcons, index, mIcons, dest, count);
1243 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1244 System.arraycopy(mLabels, index, mLabels, dest, count);
1245 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
Romain Guy060b5c82010-03-04 10:07:38 -08001246
Daniel Sandler388f6792010-03-02 14:08:08 -05001247 createAppIconAllocations(index, item);
1248 uploadAppIcon(index, item);
1249 mRollo.mState.iconCount++;
1250 }
Romain Guy060b5c82010-03-04 10:07:38 -08001251
Daniel Sandler388f6792010-03-02 14:08:08 -05001252 /**
1253 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1254 */
1255 private void removeApp(int index) {
1256 final int count = mState.iconCount - index - 1;
1257 final int src = index + 1;
Romain Guy060b5c82010-03-04 10:07:38 -08001258
Daniel Sandler388f6792010-03-02 14:08:08 -05001259 System.arraycopy(mIcons, src, mIcons, index, count);
1260 System.arraycopy(mIconIds, src, mIconIds, index, count);
1261 System.arraycopy(mLabels, src, mLabels, index, count);
1262 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
Romain Guy060b5c82010-03-04 10:07:38 -08001263
Daniel Sandler388f6792010-03-02 14:08:08 -05001264 mRollo.mState.iconCount--;
1265 final int last = mState.iconCount;
Romain Guy060b5c82010-03-04 10:07:38 -08001266
Daniel Sandler388f6792010-03-02 14:08:08 -05001267 mIcons[last] = null;
1268 mIconIds[last] = 0;
1269 mLabels[last] = null;
1270 mLabelIds[last] = 0;
1271 }
Romain Guy060b5c82010-03-04 10:07:38 -08001272
Daniel Sandler388f6792010-03-02 14:08:08 -05001273 /**
1274 * Send the apps list structures to RS.
1275 */
1276 private void saveAppsList() {
1277 // WTF: how could mScript be not null but mAllocIconIds null b/2460740.
1278 if (mScript != null && mAllocIconIds != null) {
1279 mRS.contextBindRootScript(null);
Romain Guy060b5c82010-03-04 10:07:38 -08001280
Daniel Sandler388f6792010-03-02 14:08:08 -05001281 mAllocIconIds.data(mIconIds);
1282 mAllocLabelIds.data(mLabelIds);
Romain Guy060b5c82010-03-04 10:07:38 -08001283
Daniel Sandler388f6792010-03-02 14:08:08 -05001284 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1285 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Romain Guy060b5c82010-03-04 10:07:38 -08001286
Daniel Sandler388f6792010-03-02 14:08:08 -05001287 mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -08001288
Daniel Sandler388f6792010-03-02 14:08:08 -05001289 // Note: mScript may be null if we haven't initialized it yet.
1290 // In that case, this is a no-op.
1291 if (mInvokeResetWAR != null) {
1292 mInvokeResetWAR.execute();
1293 }
Romain Guy060b5c82010-03-04 10:07:38 -08001294
Daniel Sandler388f6792010-03-02 14:08:08 -05001295 mRS.contextBindRootScript(mScript);
1296 }
1297 }
Romain Guy060b5c82010-03-04 10:07:38 -08001298
Daniel Sandler388f6792010-03-02 14:08:08 -05001299 void initTouchState() {
Romain Guy060b5c82010-03-04 10:07:38 -08001300 boolean isPortrait = getWidth() < getHeight();
1301 // TODO: Put this in a config file/define
1302 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE_PORTRAIT;
1303 if (!isPortrait) cellHeight -= 12;
1304 int centerY = (int) (getHeight() * (isPortrait ? 0.5f : 0.47f));
1305 if (!isPortrait) centerY += cellHeight / 2;
1306 int half = (int) Math.floor((mRowsPerPage + 1) / 2);
1307 int end = mTouchYBorders.length - (half + 1);
1308
1309 for (int i = -half; i <= end; i++) {
1310 mTouchYBorders[i + half] = centerY + i * cellHeight;
1311 }
1312
1313 int x = 0;
1314 // TODO: Put this in a config file/define
1315 int columnWidth = 120;
1316 for (int i = 0; i < mColumnsPerPage + 1; i++) {
1317 mTouchXBorders[i] = x;
1318 x += columnWidth;
1319 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001320 }
Romain Guy060b5c82010-03-04 10:07:38 -08001321
Daniel Sandler388f6792010-03-02 14:08:08 -05001322 void fling() {
1323 mInvokeFling.execute();
1324 }
Romain Guy060b5c82010-03-04 10:07:38 -08001325
Daniel Sandler388f6792010-03-02 14:08:08 -05001326 void move() {
1327 mInvokeMove.execute();
1328 }
Romain Guy060b5c82010-03-04 10:07:38 -08001329
Daniel Sandler388f6792010-03-02 14:08:08 -05001330 void moveTo(float row) {
1331 mState.targetPos = row;
1332 mState.save();
1333 mInvokeMoveTo.execute();
1334 }
Romain Guy060b5c82010-03-04 10:07:38 -08001335
Daniel Sandler388f6792010-03-02 14:08:08 -05001336 int chooseTappedIcon(int x, int y, float pos) {
1337 // Adjust for scroll position if not zero.
1338 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Romain Guy060b5c82010-03-04 10:07:38 -08001339
Daniel Sandler388f6792010-03-02 14:08:08 -05001340 int col = -1;
1341 int row = -1;
Romain Guy060b5c82010-03-04 10:07:38 -08001342 final int columnsCount = mColumnsPerPage;
1343 for (int i=0; i< columnsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001344 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1345 col = i;
1346 break;
1347 }
1348 }
Romain Guy060b5c82010-03-04 10:07:38 -08001349 final int rowsCount = mRowsPerPage;
1350 for (int i=0; i< rowsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001351 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1352 row = i;
1353 break;
1354 }
1355 }
Romain Guy060b5c82010-03-04 10:07:38 -08001356
Daniel Sandler388f6792010-03-02 14:08:08 -05001357 if (row < 0 || col < 0) {
1358 return -1;
1359 }
Romain Guy060b5c82010-03-04 10:07:38 -08001360
1361 int index = (((int)pos) * columnsCount) + (row * columnsCount) + col;
1362
Daniel Sandler388f6792010-03-02 14:08:08 -05001363 if (index >= mState.iconCount) {
1364 return -1;
1365 } else {
1366 return index;
1367 }
1368 }
Romain Guy060b5c82010-03-04 10:07:38 -08001369
Daniel Sandler388f6792010-03-02 14:08:08 -05001370 /**
1371 * You need to call save() on mState on your own after calling this.
1372 *
1373 * @return the index of the icon that was selected.
1374 */
1375 int selectIcon(int x, int y, float pos, int pressed) {
1376 final int index = chooseTappedIcon(x, y, pos);
1377 selectIcon(index, pressed);
1378 return index;
1379 }
Romain Guy060b5c82010-03-04 10:07:38 -08001380
Daniel Sandler388f6792010-03-02 14:08:08 -05001381 /**
1382 * Select the icon at the given index.
1383 *
1384 * @param index The index.
1385 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
1386 */
1387 void selectIcon(int index, int pressed) {
1388 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
1389 mState.selectedIconIndex = -1;
1390 if (mLastSelection == SELECTION_ICONS) {
1391 mLastSelection = SELECTION_NONE;
1392 }
1393 } else {
1394 if (pressed == SELECTED_FOCUSED) {
1395 mLastSelection = SELECTION_ICONS;
1396 }
Romain Guy060b5c82010-03-04 10:07:38 -08001397
Daniel Sandler388f6792010-03-02 14:08:08 -05001398 int prev = mState.selectedIconIndex;
1399 mState.selectedIconIndex = index;
Romain Guy060b5c82010-03-04 10:07:38 -08001400
Daniel Sandler388f6792010-03-02 14:08:08 -05001401 ApplicationInfo info = mAllAppsList.get(index);
1402 Bitmap selectionBitmap = mSelectionBitmap;
Romain Guy060b5c82010-03-04 10:07:38 -08001403
Daniel Sandler388f6792010-03-02 14:08:08 -05001404 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1405 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1406 pressed == SELECTED_PRESSED, info.iconBitmap);
Romain Guy060b5c82010-03-04 10:07:38 -08001407
Daniel Sandler388f6792010-03-02 14:08:08 -05001408 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
1409 Element.RGBA_8888(mRS), false);
1410 mSelectedIcon.uploadToTexture(0);
1411 mState.selectedIconTexture = mSelectedIcon.getID();
Romain Guy060b5c82010-03-04 10:07:38 -08001412
Daniel Sandler388f6792010-03-02 14:08:08 -05001413 if (prev != index) {
1414 if (info.title != null && info.title.length() > 0) {
1415 //setContentDescription(info.title);
1416 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1417 }
1418 }
1419 }
1420 }
Romain Guy060b5c82010-03-04 10:07:38 -08001421
Daniel Sandler388f6792010-03-02 14:08:08 -05001422 /**
1423 * You need to call save() on mState on your own after calling this.
1424 */
1425 void clearSelectedIcon() {
1426 mState.selectedIconIndex = -1;
1427 }
Romain Guy060b5c82010-03-04 10:07:38 -08001428
Daniel Sandler388f6792010-03-02 14:08:08 -05001429 void setHomeSelected(int mode) {
1430 final int prev = mLastSelection;
1431 switch (mode) {
1432 case SELECTED_NONE:
1433 mState.homeButtonId = mHomeButtonNormal.getID();
1434 break;
1435 case SELECTED_FOCUSED:
1436 mLastSelection = SELECTION_HOME;
1437 mState.homeButtonId = mHomeButtonFocused.getID();
1438 if (prev != SELECTION_HOME) {
1439 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1440 }
1441 break;
1442 case SELECTED_PRESSED:
1443 mState.homeButtonId = mHomeButtonPressed.getID();
1444 break;
1445 }
1446 }
Romain Guy060b5c82010-03-04 10:07:38 -08001447
Daniel Sandler388f6792010-03-02 14:08:08 -05001448 public void dumpState() {
1449 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1450 Log.d(TAG, "mRollo.mHeight=" + mHeight);
Romain Guy060b5c82010-03-04 10:07:38 -08001451 Log.d(TAG, "mRollo.mIcons=" + Arrays.toString(mIcons));
Daniel Sandler388f6792010-03-02 14:08:08 -05001452 if (mIcons != null) {
1453 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1454 }
1455 if (mIconIds != null) {
1456 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1457 }
1458 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1459 if (mLabelIds != null) {
1460 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1461 }
1462 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1463 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1464 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
1465 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1466 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1467 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1468 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1469 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1470 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1471 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1472 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1473 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1474 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1475 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1476 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1477 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1478 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1479 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1480 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1481 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1482 }
1483 }
Romain Guy060b5c82010-03-04 10:07:38 -08001484
Daniel Sandler388f6792010-03-02 14:08:08 -05001485 public void dumpState() {
1486 Log.d(TAG, "mRS=" + mRS);
1487 Log.d(TAG, "mRollo=" + mRollo);
1488 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1489 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1490 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1491 Log.d(TAG, "mLastSelection=" + mLastSelection);
1492 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1493 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1494 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1495 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1496 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1497 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1498 Log.d(TAG, "mZoom=" + mZoom);
1499 Log.d(TAG, "mPosX=" + mPosX);
1500 Log.d(TAG, "mVelocity=" + mVelocity);
1501 Log.d(TAG, "mMessageProc=" + mMessageProc);
1502 if (mRollo != null) {
1503 mRollo.dumpState();
1504 }
1505 if (mRS != null) {
1506 mRS.contextDump(0);
1507 }
1508 }
1509}
1510
1511