blob: ac4b52cfa58414b16a0a0d919a7404af9abf6602 [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
Romain Guyce115852010-03-04 12:15:37 -0800484 final boolean isPortrait = getWidth() < getHeight();
Daniel Sandler388f6792010-03-02 14:08:08 -0500485 int action = ev.getAction();
486 switch (action) {
487 case MotionEvent.ACTION_DOWN:
Romain Guyce115852010-03-04 12:15:37 -0800488 if ((isPortrait && y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) ||
489 (!isPortrait && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500490 mTouchTracking = TRACKING_HOME;
491 mRollo.setHomeSelected(SELECTED_PRESSED);
492 mRollo.mState.save();
493 mCurrentIconIndex = -1;
494 } else {
495 mTouchTracking = TRACKING_FLING;
Romain Guy060b5c82010-03-04 10:07:38 -0800496
Daniel Sandler388f6792010-03-02 14:08:08 -0500497 mMotionDownRawX = (int)ev.getRawX();
498 mMotionDownRawY = (int)ev.getRawY();
Romain Guy060b5c82010-03-04 10:07:38 -0800499
Daniel Sandler388f6792010-03-02 14:08:08 -0500500 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
501 mRollo.mState.newTouchDown = 1;
Romain Guy060b5c82010-03-04 10:07:38 -0800502
Daniel Sandler388f6792010-03-02 14:08:08 -0500503 if (!mRollo.checkClickOK()) {
504 mRollo.clearSelectedIcon();
505 } else {
506 mDownIconIndex = mCurrentIconIndex
507 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
508 if (mDownIconIndex < 0) {
509 // if nothing was selected, no long press.
510 cancelLongPress();
511 }
512 }
513 mRollo.mState.save();
514 mRollo.move();
515 mVelocityTracker = VelocityTracker.obtain();
516 mVelocityTracker.addMovement(ev);
517 mStartedScrolling = false;
518 }
519 break;
520 case MotionEvent.ACTION_MOVE:
521 case MotionEvent.ACTION_OUTSIDE:
522 if (mTouchTracking == TRACKING_HOME) {
Romain Guyce115852010-03-04 12:15:37 -0800523 mRollo.setHomeSelected((isPortrait &&
524 y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) || (!isPortrait
525 && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])
Daniel Sandler388f6792010-03-02 14:08:08 -0500526 ? SELECTED_PRESSED : SELECTED_NONE);
527 mRollo.mState.save();
528 } else if (mTouchTracking == TRACKING_FLING) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500529 int rawY = (int)ev.getRawY();
530 int slop;
531 slop = Math.abs(rawY - mMotionDownRawY);
Romain Guy060b5c82010-03-04 10:07:38 -0800532
Daniel Sandler388f6792010-03-02 14:08:08 -0500533 if (!mStartedScrolling && slop < mSlop) {
534 // don't update anything so when we do start scrolling
535 // below, we get the right delta.
536 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
537 if (mDownIconIndex != mCurrentIconIndex) {
538 // If a different icon is selected, don't allow it to be picked up.
539 // This handles off-axis dragging.
540 cancelLongPress();
541 mCurrentIconIndex = -1;
542 }
543 } else {
544 if (!mStartedScrolling) {
545 cancelLongPress();
546 mCurrentIconIndex = -1;
547 }
548 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
549 mRollo.mState.newTouchDown = 1;
550 mRollo.move();
Romain Guy060b5c82010-03-04 10:07:38 -0800551
Daniel Sandler388f6792010-03-02 14:08:08 -0500552 mStartedScrolling = true;
553 mRollo.clearSelectedIcon();
554 mVelocityTracker.addMovement(ev);
555 mRollo.mState.save();
556 }
557 }
558 break;
559 case MotionEvent.ACTION_UP:
560 case MotionEvent.ACTION_CANCEL:
561 if (mTouchTracking == TRACKING_HOME) {
562 if (action == MotionEvent.ACTION_UP) {
Romain Guyce115852010-03-04 12:15:37 -0800563 if ((isPortrait && y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) ||
564 (!isPortrait && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500565 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
566 mLauncher.closeAllApps(true);
567 }
568 mRollo.setHomeSelected(SELECTED_NONE);
569 mRollo.mState.save();
570 }
571 mCurrentIconIndex = -1;
572 } else if (mTouchTracking == TRACKING_FLING) {
573 mRollo.mState.newTouchDown = 0;
574 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Romain Guy060b5c82010-03-04 10:07:38 -0800575
Daniel Sandler388f6792010-03-02 14:08:08 -0500576 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
577 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
578 mRollo.clearSelectedIcon();
579 mRollo.mState.save();
580 mRollo.fling();
Romain Guy060b5c82010-03-04 10:07:38 -0800581
Daniel Sandler388f6792010-03-02 14:08:08 -0500582 if (mVelocityTracker != null) {
583 mVelocityTracker.recycle();
584 mVelocityTracker = null;
585 }
586 }
587 mTouchTracking = TRACKING_NONE;
588 break;
589 }
Romain Guy060b5c82010-03-04 10:07:38 -0800590
Daniel Sandler388f6792010-03-02 14:08:08 -0500591 return true;
592 }
Romain Guy060b5c82010-03-04 10:07:38 -0800593
Daniel Sandler388f6792010-03-02 14:08:08 -0500594 public void onClick(View v) {
595 if (mLocks != 0 || !isVisible()) {
596 return;
597 }
598 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
599 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
600 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
601 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
602 mLauncher.startActivitySafely(app.intent);
603 }
604 }
Romain Guy060b5c82010-03-04 10:07:38 -0800605
Daniel Sandler388f6792010-03-02 14:08:08 -0500606 public boolean onLongClick(View v) {
607 if (mLocks != 0 || !isVisible()) {
608 return true;
609 }
610 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
611 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
612 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Romain Guy060b5c82010-03-04 10:07:38 -0800613
Daniel Sandler388f6792010-03-02 14:08:08 -0500614 Bitmap bmp = app.iconBitmap;
615 final int w = bmp.getWidth();
616 final int h = bmp.getHeight();
Romain Guy060b5c82010-03-04 10:07:38 -0800617
Daniel Sandler388f6792010-03-02 14:08:08 -0500618 // We don't really have an accurate location to use. This will do.
619 int screenX = mMotionDownRawX - (w / 2);
620 int screenY = mMotionDownRawY - h;
Romain Guy060b5c82010-03-04 10:07:38 -0800621
Daniel Sandler388f6792010-03-02 14:08:08 -0500622 mDragController.startDrag(bmp, screenX, screenY,
623 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Romain Guy060b5c82010-03-04 10:07:38 -0800624
Daniel Sandler388f6792010-03-02 14:08:08 -0500625 mLauncher.closeAllApps(true);
626 }
627 return true;
628 }
Romain Guy060b5c82010-03-04 10:07:38 -0800629
Daniel Sandler388f6792010-03-02 14:08:08 -0500630 @Override
631 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
632 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
633 if (!isVisible()) {
634 return false;
635 }
636 String text = null;
637 int index;
638 int count = mAllAppsList.size() + 1; // +1 is home
639 int pos = -1;
640 switch (mLastSelection) {
641 case SELECTION_ICONS:
642 index = mRollo.mState.selectedIconIndex;
643 if (index >= 0) {
644 ApplicationInfo info = mAllAppsList.get(index);
645 if (info.title != null) {
646 text = info.title.toString();
647 pos = index;
648 }
649 }
650 break;
651 case SELECTION_HOME:
652 text = getContext().getString(R.string.all_apps_home_button_label);
653 pos = count;
654 break;
655 }
656 if (text != null) {
657 event.setEnabled(true);
658 event.getText().add(text);
659 //event.setContentDescription(text);
660 event.setItemCount(count);
661 event.setCurrentItemIndex(pos);
662 }
663 }
664 return false;
665 }
Romain Guy060b5c82010-03-04 10:07:38 -0800666
Daniel Sandler388f6792010-03-02 14:08:08 -0500667 public void setDragController(DragController dragger) {
668 mDragController = dragger;
669 }
Romain Guy060b5c82010-03-04 10:07:38 -0800670
Daniel Sandler388f6792010-03-02 14:08:08 -0500671 public void onDropCompleted(View target, boolean success) {
672 }
Romain Guy060b5c82010-03-04 10:07:38 -0800673
Daniel Sandler388f6792010-03-02 14:08:08 -0500674 /**
675 * Zoom to the specifed level.
676 *
677 * @param zoom [0..1] 0 is hidden, 1 is open
678 */
679 public void zoom(float zoom, boolean animate) {
680 cancelLongPress();
681 mNextZoom = zoom;
682 mAnimateNextZoom = animate;
683 // if we do setZoom while we don't have a surface, we won't
684 // get the callbacks that actually set mZoom.
685 if (mRollo == null || !mHaveSurface) {
686 mZoomDirty = true;
687 mZoom = zoom;
Daniel Sandler388f6792010-03-02 14:08:08 -0500688 } else {
689 mRollo.setZoom(zoom, animate);
690 }
691 }
Romain Guy060b5c82010-03-04 10:07:38 -0800692
Daniel Sandler388f6792010-03-02 14:08:08 -0500693 public boolean isVisible() {
694 return mZoom > 0.001f;
695 }
Romain Guy060b5c82010-03-04 10:07:38 -0800696
Daniel Sandler388f6792010-03-02 14:08:08 -0500697 public boolean isOpaque() {
698 return mZoom > 0.999f;
699 }
Romain Guy060b5c82010-03-04 10:07:38 -0800700
Daniel Sandler388f6792010-03-02 14:08:08 -0500701 public void setApps(ArrayList<ApplicationInfo> list) {
702 if (mRS == null) {
703 // We've been removed from the window. Don't bother with all this.
704 return;
705 }
Romain Guy060b5c82010-03-04 10:07:38 -0800706
Daniel Sandler388f6792010-03-02 14:08:08 -0500707 mAllAppsList = list;
708 if (mRollo != null) {
709 mRollo.setApps(list);
710 }
711 mLocks &= ~LOCK_ICONS_PENDING;
712 }
Romain Guy060b5c82010-03-04 10:07:38 -0800713
Daniel Sandler388f6792010-03-02 14:08:08 -0500714 public void addApps(ArrayList<ApplicationInfo> list) {
715 if (mAllAppsList == null) {
716 // Not done loading yet. We'll find out about it later.
717 return;
718 }
719 if (mRS == null) {
720 // We've been removed from the window. Don't bother with all this.
721 return;
722 }
Romain Guy060b5c82010-03-04 10:07:38 -0800723
Daniel Sandler388f6792010-03-02 14:08:08 -0500724 final int N = list.size();
725 if (mRollo != null) {
726 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
727 }
Romain Guy060b5c82010-03-04 10:07:38 -0800728
Daniel Sandler388f6792010-03-02 14:08:08 -0500729 for (int i=0; i<N; i++) {
730 final ApplicationInfo item = list.get(i);
731 int index = Collections.binarySearch(mAllAppsList, item,
732 LauncherModel.APP_NAME_COMPARATOR);
733 if (index < 0) {
734 index = -(index+1);
735 }
736 mAllAppsList.add(index, item);
737 if (mRollo != null) {
738 mRollo.addApp(index, item);
739 }
740 }
Romain Guy060b5c82010-03-04 10:07:38 -0800741
Daniel Sandler388f6792010-03-02 14:08:08 -0500742 if (mRollo != null) {
743 mRollo.saveAppsList();
744 }
745 }
Romain Guy060b5c82010-03-04 10:07:38 -0800746
Daniel Sandler388f6792010-03-02 14:08:08 -0500747 public void removeApps(ArrayList<ApplicationInfo> list) {
748 if (mAllAppsList == null) {
749 // Not done loading yet. We'll find out about it later.
750 return;
751 }
Romain Guy060b5c82010-03-04 10:07:38 -0800752
Daniel Sandler388f6792010-03-02 14:08:08 -0500753 final int N = list.size();
754 for (int i=0; i<N; i++) {
755 final ApplicationInfo item = list.get(i);
756 int index = findAppByComponent(mAllAppsList, item);
757 if (index >= 0) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500758 mAllAppsList.remove(index);
759 if (mRollo != null) {
760 mRollo.removeApp(index);
761 }
762 } else {
763 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
764 // Try to recover. This should keep us from crashing for now.
765 }
766 }
Romain Guy060b5c82010-03-04 10:07:38 -0800767
Daniel Sandler388f6792010-03-02 14:08:08 -0500768 if (mRollo != null) {
769 mRollo.saveAppsList();
770 }
771 }
Romain Guy060b5c82010-03-04 10:07:38 -0800772
Daniel Sandler388f6792010-03-02 14:08:08 -0500773 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
774 // Just remove and add, because they may need to be re-sorted.
775 removeApps(list);
776 addApps(list);
777 }
Romain Guy060b5c82010-03-04 10:07:38 -0800778
Daniel Sandler388f6792010-03-02 14:08:08 -0500779 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
780 ComponentName component = item.intent.getComponent();
781 final int N = list.size();
782 for (int i=0; i<N; i++) {
783 ApplicationInfo x = list.get(i);
784 if (x.intent.getComponent().equals(component)) {
785 return i;
786 }
787 }
788 return -1;
789 }
Romain Guy060b5c82010-03-04 10:07:38 -0800790
791 /*
Daniel Sandler388f6792010-03-02 14:08:08 -0500792 private static int countPages(int iconCount) {
Romain Guy060b5c82010-03-04 10:07:38 -0800793 int iconsPerPage = getColumnsCount() * Defines.ROWS_PER_PAGE_PORTRAIT;
Daniel Sandler388f6792010-03-02 14:08:08 -0500794 int pages = iconCount / iconsPerPage;
795 if (pages*iconsPerPage != iconCount) {
796 pages++;
797 }
798 return pages;
799 }
Romain Guy060b5c82010-03-04 10:07:38 -0800800 */
Daniel Sandler388f6792010-03-02 14:08:08 -0500801
802 class AAMessage extends RenderScript.RSMessage {
803 public void run() {
804 mPosX = ((float)mData[0]) / (1 << 16);
805 mVelocity = ((float)mData[1]) / (1 << 16);
806 mZoom = ((float)mData[2]) / (1 << 16);
807 mZoomDirty = false;
808 }
809 }
Romain Guy060b5c82010-03-04 10:07:38 -0800810
Daniel Sandler388f6792010-03-02 14:08:08 -0500811 public class RolloRS {
Romain Guy060b5c82010-03-04 10:07:38 -0800812
Daniel Sandler388f6792010-03-02 14:08:08 -0500813 // Allocations ======
814 private int mWidth;
815 private int mHeight;
Romain Guy060b5c82010-03-04 10:07:38 -0800816
Daniel Sandler388f6792010-03-02 14:08:08 -0500817 private Resources mRes;
818 private Script mScript;
819 private Script.Invokable mInvokeMove;
820 private Script.Invokable mInvokeMoveTo;
821 private Script.Invokable mInvokeFling;
822 private Script.Invokable mInvokeResetWAR;
823 private Script.Invokable mInvokeSetZoom;
Romain Guy060b5c82010-03-04 10:07:38 -0800824
Daniel Sandler388f6792010-03-02 14:08:08 -0500825 private ProgramStore mPSIcons;
826 private ProgramFragment mPFTexMip;
827 private ProgramFragment mPFTexMipAlpha;
828 private ProgramFragment mPFTexNearest;
829 private ProgramVertex mPV;
830 private ProgramVertex mPVCurve;
831 private SimpleMesh mMesh;
832 private ProgramVertex.MatrixAllocation mPVA;
Romain Guy060b5c82010-03-04 10:07:38 -0800833
Daniel Sandler388f6792010-03-02 14:08:08 -0500834 private Allocation mUniformAlloc;
Romain Guy060b5c82010-03-04 10:07:38 -0800835
Daniel Sandler388f6792010-03-02 14:08:08 -0500836 private Allocation mHomeButtonNormal;
837 private Allocation mHomeButtonFocused;
838 private Allocation mHomeButtonPressed;
Romain Guy060b5c82010-03-04 10:07:38 -0800839
Daniel Sandler388f6792010-03-02 14:08:08 -0500840 private Allocation[] mIcons;
841 private int[] mIconIds;
842 private Allocation mAllocIconIds;
Romain Guy060b5c82010-03-04 10:07:38 -0800843
Daniel Sandler388f6792010-03-02 14:08:08 -0500844 private Allocation[] mLabels;
845 private int[] mLabelIds;
846 private Allocation mAllocLabelIds;
847 private Allocation mSelectedIcon;
Romain Guy060b5c82010-03-04 10:07:38 -0800848
Daniel Sandler388f6792010-03-02 14:08:08 -0500849 private int[] mTouchYBorders;
850 private int[] mTouchXBorders;
Romain Guy060b5c82010-03-04 10:07:38 -0800851
Daniel Sandler388f6792010-03-02 14:08:08 -0500852 private Bitmap mSelectionBitmap;
853 private Canvas mSelectionCanvas;
Romain Guy060b5c82010-03-04 10:07:38 -0800854
Daniel Sandler388f6792010-03-02 14:08:08 -0500855 Params mParams;
856 State mState;
Romain Guy060b5c82010-03-04 10:07:38 -0800857
Daniel Sandler388f6792010-03-02 14:08:08 -0500858 class BaseAlloc {
859 Allocation mAlloc;
860 Type mType;
Romain Guy060b5c82010-03-04 10:07:38 -0800861
Daniel Sandler388f6792010-03-02 14:08:08 -0500862 void save() {
863 mAlloc.data(this);
864 }
865 }
Romain Guy060b5c82010-03-04 10:07:38 -0800866
Daniel Sandler388f6792010-03-02 14:08:08 -0500867 private boolean checkClickOK() {
868 return (Math.abs(mVelocity) < 0.4f) &&
869 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
870 }
Romain Guy060b5c82010-03-04 10:07:38 -0800871
Daniel Sandler388f6792010-03-02 14:08:08 -0500872 class Params extends BaseAlloc {
873 Params() {
874 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
875 mAlloc = Allocation.createTyped(mRS, mType);
876 save();
877 }
878 public int bubbleWidth;
879 public int bubbleHeight;
880 public int bubbleBitmapWidth;
881 public int bubbleBitmapHeight;
Romain Guy060b5c82010-03-04 10:07:38 -0800882
Daniel Sandler388f6792010-03-02 14:08:08 -0500883 public int homeButtonWidth;
884 public int homeButtonHeight;
885 public int homeButtonTextureWidth;
886 public int homeButtonTextureHeight;
887 }
Romain Guy060b5c82010-03-04 10:07:38 -0800888
Daniel Sandler388f6792010-03-02 14:08:08 -0500889 class State extends BaseAlloc {
890 public float newPositionX;
891 public int newTouchDown;
892 public float flingVelocity;
893 public int iconCount;
894 public int selectedIconIndex = -1;
895 public int selectedIconTexture;
896 public float zoomTarget;
897 public int homeButtonId;
898 public float targetPos;
Romain Guy060b5c82010-03-04 10:07:38 -0800899
Daniel Sandler388f6792010-03-02 14:08:08 -0500900 State() {
901 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
902 mAlloc = Allocation.createTyped(mRS, mType);
903 save();
904 }
905 }
Romain Guy060b5c82010-03-04 10:07:38 -0800906
Daniel Sandler388f6792010-03-02 14:08:08 -0500907 public RolloRS() {
908 }
Romain Guy060b5c82010-03-04 10:07:38 -0800909
Daniel Sandler388f6792010-03-02 14:08:08 -0500910 public void init(Resources res, int width, int height) {
911 mRes = res;
912 mWidth = width;
913 mHeight = height;
914 initProgramVertex();
915 initProgramFragment();
916 initProgramStore();
917 initGl();
918 initData();
919 initTouchState();
920 initRs();
921 }
Romain Guy060b5c82010-03-04 10:07:38 -0800922
Daniel Sandler388f6792010-03-02 14:08:08 -0500923 public void initMesh() {
924 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Romain Guy060b5c82010-03-04 10:07:38 -0800925
Daniel Sandler388f6792010-03-02 14:08:08 -0500926 for (int ct=0; ct < 16; ct++) {
927 float pos = (1.f / (16.f - 1)) * ct;
928 tm.addVertex(0.0f, pos);
929 tm.addVertex(1.0f, pos);
930 }
931 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
932 tm.addTriangle(ct, ct+1, ct+2);
933 tm.addTriangle(ct+1, ct+3, ct+2);
934 }
935 mMesh = tm.create();
936 mMesh.setName("SMCell");
937 }
Romain Guy060b5c82010-03-04 10:07:38 -0800938
Daniel Sandler388f6792010-03-02 14:08:08 -0500939 void resize(int w, int h) {
940 mPVA.setupProjectionNormalized(w, h);
941 mWidth = w;
942 mHeight = h;
943 }
Romain Guy060b5c82010-03-04 10:07:38 -0800944
Daniel Sandler388f6792010-03-02 14:08:08 -0500945 private void initProgramVertex() {
946 mPVA = new ProgramVertex.MatrixAllocation(mRS);
947 resize(mWidth, mHeight);
Romain Guy060b5c82010-03-04 10:07:38 -0800948
Daniel Sandler388f6792010-03-02 14:08:08 -0500949 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
950 pvb.setTextureMatrixEnable(true);
951 mPV = pvb.create();
952 mPV.setName("PV");
953 mPV.bindAllocation(mPVA);
Romain Guy060b5c82010-03-04 10:07:38 -0800954
Daniel Sandler388f6792010-03-02 14:08:08 -0500955 Element.Builder eb = new Element.Builder(mRS);
956 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
957 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
958 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "BendPos");
959 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
960 Element e = eb.create();
Romain Guy060b5c82010-03-04 10:07:38 -0800961
Daniel Sandler388f6792010-03-02 14:08:08 -0500962 mUniformAlloc = Allocation.createSized(mRS, e, 1);
Romain Guy060b5c82010-03-04 10:07:38 -0800963
Daniel Sandler388f6792010-03-02 14:08:08 -0500964 initMesh();
965 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
Romain Guy060b5c82010-03-04 10:07:38 -0800966 String t = "void main() {\n" +
967 // Animation
968 " float ani = UNI_Position.z;\n" +
969
970 " float bendY1 = UNI_BendPos.x;\n" +
971 " float bendY2 = UNI_BendPos.y;\n" +
972 " float bendAngle = 47.0 * (3.14 / 180.0);\n" +
973 " float bendDistance = bendY1 * 0.4;\n" +
974 " float distanceDimLevel = 0.6;\n" +
975
976 " float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
977 " float aDy = cos(bendAngle);\n" +
978 " float aDz = sin(bendAngle);\n" +
979
980 " float scale = (2.0 / 480.0);\n" +
981 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
982 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
983 " float y = 0.0;\n" +
984 " float z = 0.0;\n" +
985 " float lum = 1.0;\n" +
986
987 " float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
988 " y += cv * aDy;\n" +
989 " z += -cv * aDz;\n" +
990 " cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" + // curve range
991 " lum += cv / bendDistance * distanceDimLevel;\n" +
992 " y += cv * cos(cv * bendStep);\n" +
993 " z += cv * sin(cv * bendStep);\n" +
994
995 " cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
996 " y += cv * aDy;\n" +
997 " z += cv * aDz;\n" +
998 " cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
999 " lum -= cv / bendDistance * distanceDimLevel;\n" +
1000 " y += cv * cos(cv * bendStep);\n" +
1001 " z += cv * sin(cv * bendStep);\n" +
1002
1003 " y += clamp(ys, bendY1, bendY2);\n" +
1004
1005 " vec4 pos;\n" +
1006 " pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
1007 " pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
1008 " pos.z = z * UNI_ScaleOffset.x;\n" +
1009 " pos.w = 1.0;\n" +
1010
1011 " pos.x *= 1.0 + ani * 4.0;\n" +
1012 " pos.y *= 1.0 + ani * 4.0;\n" +
1013 " pos.z -= ani * 1.5;\n" +
1014 " lum *= 1.0 - ani;\n" +
1015
1016 " gl_Position = UNI_MVP * pos;\n" +
1017 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
1018 " varTex0.xy = ATTRIB_position;\n" +
1019 " varTex0.y = 1.0 - varTex0.y;\n" +
1020 " varTex0.zw = vec2(0.0, 0.0);\n" +
1021 "}\n";
Daniel Sandler388f6792010-03-02 14:08:08 -05001022 sb.setShader(t);
1023 sb.addConstant(mUniformAlloc.getType());
1024 sb.addInput(mMesh.getVertexType(0).getElement());
1025 mPVCurve = sb.create();
1026 mPVCurve.setName("PVCurve");
1027 mPVCurve.bindAllocation(mPVA);
1028 mPVCurve.bindConstants(mUniformAlloc, 1);
Romain Guy060b5c82010-03-04 10:07:38 -08001029
Daniel Sandler388f6792010-03-02 14:08:08 -05001030 mRS.contextBindProgramVertex(mPV);
1031 }
Romain Guy060b5c82010-03-04 10:07:38 -08001032
Daniel Sandler388f6792010-03-02 14:08:08 -05001033 private void initProgramFragment() {
1034 Sampler.Builder sb = new Sampler.Builder(mRS);
1035 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
1036 sb.setMag(Sampler.Value.NEAREST);
1037 sb.setWrapS(Sampler.Value.CLAMP);
1038 sb.setWrapT(Sampler.Value.CLAMP);
1039 Sampler linear = sb.create();
Romain Guy060b5c82010-03-04 10:07:38 -08001040
Daniel Sandler388f6792010-03-02 14:08:08 -05001041 sb.setMin(Sampler.Value.NEAREST);
1042 sb.setMag(Sampler.Value.NEAREST);
1043 Sampler nearest = sb.create();
Romain Guy060b5c82010-03-04 10:07:38 -08001044
Daniel Sandler388f6792010-03-02 14:08:08 -05001045 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
1046 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1047 ProgramFragment.Builder.Format.RGBA, 0);
1048 mPFTexMip = bf.create();
1049 mPFTexMip.setName("PFTexMip");
1050 mPFTexMip.bindSampler(linear, 0);
Romain Guy060b5c82010-03-04 10:07:38 -08001051
Daniel Sandler388f6792010-03-02 14:08:08 -05001052 mPFTexNearest = bf.create();
1053 mPFTexNearest.setName("PFTexNearest");
1054 mPFTexNearest.bindSampler(nearest, 0);
Romain Guy060b5c82010-03-04 10:07:38 -08001055
Daniel Sandler388f6792010-03-02 14:08:08 -05001056 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1057 ProgramFragment.Builder.Format.ALPHA, 0);
1058 mPFTexMipAlpha = bf.create();
1059 mPFTexMipAlpha.setName("PFTexMipAlpha");
1060 mPFTexMipAlpha.bindSampler(linear, 0);
Romain Guy060b5c82010-03-04 10:07:38 -08001061
Daniel Sandler388f6792010-03-02 14:08:08 -05001062 }
Romain Guy060b5c82010-03-04 10:07:38 -08001063
Daniel Sandler388f6792010-03-02 14:08:08 -05001064 private void initProgramStore() {
1065 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1066 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
1067 bs.setColorMask(true,true,true,false);
1068 bs.setDitherEnable(true);
1069 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1070 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1071 mPSIcons = bs.create();
1072 mPSIcons.setName("PSIcons");
1073 }
Romain Guy060b5c82010-03-04 10:07:38 -08001074
Daniel Sandler388f6792010-03-02 14:08:08 -05001075 private void initGl() {
Romain Guy060b5c82010-03-04 10:07:38 -08001076 mTouchXBorders = new int[mColumnsPerPage +1];
1077 mTouchYBorders = new int[mRowsPerPage +1];
Daniel Sandler388f6792010-03-02 14:08:08 -05001078 }
Romain Guy060b5c82010-03-04 10:07:38 -08001079
Daniel Sandler388f6792010-03-02 14:08:08 -05001080 private void initData() {
1081 mParams = new Params();
1082 mState = new State();
Romain Guy060b5c82010-03-04 10:07:38 -08001083
Daniel Sandler388f6792010-03-02 14:08:08 -05001084 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Romain Guy060b5c82010-03-04 10:07:38 -08001085
Daniel Sandler388f6792010-03-02 14:08:08 -05001086 mParams.bubbleWidth = bubble.getBubbleWidth();
1087 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1088 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1089 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Romain Guy060b5c82010-03-04 10:07:38 -08001090
Daniel Sandler388f6792010-03-02 14:08:08 -05001091 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1092 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1093 mHomeButtonNormal.uploadToTexture(0);
1094 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1095 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1096 mHomeButtonFocused.uploadToTexture(0);
1097 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1098 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1099 mHomeButtonPressed.uploadToTexture(0);
1100 mParams.homeButtonWidth = 76;
1101 mParams.homeButtonHeight = 68;
1102 mParams.homeButtonTextureWidth = 128;
1103 mParams.homeButtonTextureHeight = 128;
Romain Guy060b5c82010-03-04 10:07:38 -08001104
Daniel Sandler388f6792010-03-02 14:08:08 -05001105 mState.homeButtonId = mHomeButtonNormal.getID();
Romain Guy060b5c82010-03-04 10:07:38 -08001106
Daniel Sandler388f6792010-03-02 14:08:08 -05001107 mParams.save();
1108 mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -08001109
Daniel Sandler388f6792010-03-02 14:08:08 -05001110 mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1111 Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1112 mSelectionCanvas = new Canvas(mSelectionBitmap);
Romain Guy060b5c82010-03-04 10:07:38 -08001113
Daniel Sandler388f6792010-03-02 14:08:08 -05001114 setApps(null);
1115 }
Romain Guy060b5c82010-03-04 10:07:38 -08001116
Daniel Sandler388f6792010-03-02 14:08:08 -05001117 private void initRs() {
1118 ScriptC.Builder sb = new ScriptC.Builder(mRS);
1119 sb.setScript(mRes, R.raw.allapps);
1120 sb.setRoot(true);
1121 sb.addDefines(mDefines);
1122 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1123 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
1124 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
1125 mInvokeMove = sb.addInvokable("move");
1126 mInvokeFling = sb.addInvokable("fling");
1127 mInvokeMoveTo = sb.addInvokable("moveTo");
1128 mInvokeResetWAR = sb.addInvokable("resetHWWar");
1129 mInvokeSetZoom = sb.addInvokable("setZoom");
1130 mScript = sb.create();
1131 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1132 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1133 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1134 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1135 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
1136 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Romain Guy060b5c82010-03-04 10:07:38 -08001137
Daniel Sandler388f6792010-03-02 14:08:08 -05001138 mRS.contextBindRootScript(mScript);
1139 }
Romain Guy060b5c82010-03-04 10:07:38 -08001140
Daniel Sandler388f6792010-03-02 14:08:08 -05001141 void dirtyCheck() {
1142 if (mZoomDirty) {
1143 setZoom(mNextZoom, mAnimateNextZoom);
1144 }
1145 }
Romain Guy060b5c82010-03-04 10:07:38 -08001146
1147 @SuppressWarnings({"ConstantConditions"})
Daniel Sandler388f6792010-03-02 14:08:08 -05001148 private void setApps(ArrayList<ApplicationInfo> list) {
1149 final int count = list != null ? list.size() : 0;
1150 int allocCount = count;
1151 if (allocCount < 1) {
1152 allocCount = 1;
1153 }
Romain Guy060b5c82010-03-04 10:07:38 -08001154
Daniel Sandler388f6792010-03-02 14:08:08 -05001155 mIcons = new Allocation[count];
1156 mIconIds = new int[allocCount];
1157 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Romain Guy060b5c82010-03-04 10:07:38 -08001158
Daniel Sandler388f6792010-03-02 14:08:08 -05001159 mLabels = new Allocation[count];
1160 mLabelIds = new int[allocCount];
1161 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Romain Guy060b5c82010-03-04 10:07:38 -08001162
Daniel Sandler388f6792010-03-02 14:08:08 -05001163 mState.iconCount = count;
1164 for (int i=0; i < mState.iconCount; i++) {
1165 createAppIconAllocations(i, list.get(i));
1166 }
1167 for (int i=0; i < mState.iconCount; i++) {
1168 uploadAppIcon(i, list.get(i));
1169 }
1170 saveAppsList();
1171 }
Romain Guy060b5c82010-03-04 10:07:38 -08001172
Daniel Sandler388f6792010-03-02 14:08:08 -05001173 private void setZoom(float zoom, boolean animate) {
1174 mRollo.clearSelectedIcon();
1175 mRollo.setHomeSelected(SELECTED_NONE);
1176 if (zoom > 0.001f) {
1177 mRollo.mState.zoomTarget = zoom;
1178 } else {
1179 mRollo.mState.zoomTarget = 0;
1180 }
1181 mRollo.mState.save();
1182 if (!animate) {
1183 mRollo.mInvokeSetZoom.execute();
1184 }
1185 }
Romain Guy060b5c82010-03-04 10:07:38 -08001186
Daniel Sandler388f6792010-03-02 14:08:08 -05001187 private void createAppIconAllocations(int index, ApplicationInfo item) {
1188 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
1189 Element.RGBA_8888(mRS), true);
1190 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
1191 Element.A_8(mRS), true);
1192 mIconIds[index] = mIcons[index].getID();
1193 mLabelIds[index] = mLabels[index].getID();
1194 }
Romain Guy060b5c82010-03-04 10:07:38 -08001195
Daniel Sandler388f6792010-03-02 14:08:08 -05001196 private void uploadAppIcon(int index, ApplicationInfo item) {
1197 if (mIconIds[index] != mIcons[index].getID()) {
1198 throw new IllegalStateException("uploadAppIcon index=" + index
1199 + " mIcons[index].getID=" + mIcons[index].getID()
1200 + " mIconsIds[index]=" + mIconIds[index]
1201 + " item=" + item);
1202 }
1203 mIcons[index].uploadToTexture(0);
1204 mLabels[index].uploadToTexture(0);
1205 }
Romain Guy060b5c82010-03-04 10:07:38 -08001206
Daniel Sandler388f6792010-03-02 14:08:08 -05001207 /**
1208 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1209 * fill in the values and call saveAppsList().
1210 */
1211 private void reallocAppsList(int count) {
1212 Allocation[] icons = new Allocation[count];
1213 int[] iconIds = new int[count];
1214 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Romain Guy060b5c82010-03-04 10:07:38 -08001215
Daniel Sandler388f6792010-03-02 14:08:08 -05001216 Allocation[] labels = new Allocation[count];
1217 int[] labelIds = new int[count];
1218 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Romain Guy060b5c82010-03-04 10:07:38 -08001219
Daniel Sandler388f6792010-03-02 14:08:08 -05001220 final int oldCount = mRollo.mState.iconCount;
Romain Guy060b5c82010-03-04 10:07:38 -08001221
Daniel Sandler388f6792010-03-02 14:08:08 -05001222 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1223 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1224 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1225 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
Romain Guy060b5c82010-03-04 10:07:38 -08001226
Daniel Sandler388f6792010-03-02 14:08:08 -05001227 mIcons = icons;
1228 mIconIds = iconIds;
1229 mLabels = labels;
1230 mLabelIds = labelIds;
1231 }
Romain Guy060b5c82010-03-04 10:07:38 -08001232
Daniel Sandler388f6792010-03-02 14:08:08 -05001233 /**
1234 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1235 */
1236 private void addApp(int index, ApplicationInfo item) {
1237 final int count = mState.iconCount - index;
1238 final int dest = index + 1;
Romain Guy060b5c82010-03-04 10:07:38 -08001239
Daniel Sandler388f6792010-03-02 14:08:08 -05001240 System.arraycopy(mIcons, index, mIcons, dest, count);
1241 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1242 System.arraycopy(mLabels, index, mLabels, dest, count);
1243 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
Romain Guy060b5c82010-03-04 10:07:38 -08001244
Daniel Sandler388f6792010-03-02 14:08:08 -05001245 createAppIconAllocations(index, item);
1246 uploadAppIcon(index, item);
1247 mRollo.mState.iconCount++;
1248 }
Romain Guy060b5c82010-03-04 10:07:38 -08001249
Daniel Sandler388f6792010-03-02 14:08:08 -05001250 /**
1251 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1252 */
1253 private void removeApp(int index) {
1254 final int count = mState.iconCount - index - 1;
1255 final int src = index + 1;
Romain Guy060b5c82010-03-04 10:07:38 -08001256
Daniel Sandler388f6792010-03-02 14:08:08 -05001257 System.arraycopy(mIcons, src, mIcons, index, count);
1258 System.arraycopy(mIconIds, src, mIconIds, index, count);
1259 System.arraycopy(mLabels, src, mLabels, index, count);
1260 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
Romain Guy060b5c82010-03-04 10:07:38 -08001261
Daniel Sandler388f6792010-03-02 14:08:08 -05001262 mRollo.mState.iconCount--;
1263 final int last = mState.iconCount;
Romain Guy060b5c82010-03-04 10:07:38 -08001264
Daniel Sandler388f6792010-03-02 14:08:08 -05001265 mIcons[last] = null;
1266 mIconIds[last] = 0;
1267 mLabels[last] = null;
1268 mLabelIds[last] = 0;
1269 }
Romain Guy060b5c82010-03-04 10:07:38 -08001270
Daniel Sandler388f6792010-03-02 14:08:08 -05001271 /**
1272 * Send the apps list structures to RS.
1273 */
1274 private void saveAppsList() {
1275 // WTF: how could mScript be not null but mAllocIconIds null b/2460740.
1276 if (mScript != null && mAllocIconIds != null) {
1277 mRS.contextBindRootScript(null);
Romain Guy060b5c82010-03-04 10:07:38 -08001278
Daniel Sandler388f6792010-03-02 14:08:08 -05001279 mAllocIconIds.data(mIconIds);
1280 mAllocLabelIds.data(mLabelIds);
Romain Guy060b5c82010-03-04 10:07:38 -08001281
Daniel Sandler388f6792010-03-02 14:08:08 -05001282 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1283 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Romain Guy060b5c82010-03-04 10:07:38 -08001284
Daniel Sandler388f6792010-03-02 14:08:08 -05001285 mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -08001286
Daniel Sandler388f6792010-03-02 14:08:08 -05001287 // Note: mScript may be null if we haven't initialized it yet.
1288 // In that case, this is a no-op.
1289 if (mInvokeResetWAR != null) {
1290 mInvokeResetWAR.execute();
1291 }
Romain Guy060b5c82010-03-04 10:07:38 -08001292
Daniel Sandler388f6792010-03-02 14:08:08 -05001293 mRS.contextBindRootScript(mScript);
1294 }
1295 }
Romain Guy060b5c82010-03-04 10:07:38 -08001296
Daniel Sandler388f6792010-03-02 14:08:08 -05001297 void initTouchState() {
Romain Guy060b5c82010-03-04 10:07:38 -08001298 boolean isPortrait = getWidth() < getHeight();
1299 // TODO: Put this in a config file/define
1300 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE_PORTRAIT;
1301 if (!isPortrait) cellHeight -= 12;
1302 int centerY = (int) (getHeight() * (isPortrait ? 0.5f : 0.47f));
1303 if (!isPortrait) centerY += cellHeight / 2;
1304 int half = (int) Math.floor((mRowsPerPage + 1) / 2);
1305 int end = mTouchYBorders.length - (half + 1);
1306
1307 for (int i = -half; i <= end; i++) {
1308 mTouchYBorders[i + half] = centerY + i * cellHeight;
1309 }
1310
1311 int x = 0;
1312 // TODO: Put this in a config file/define
1313 int columnWidth = 120;
1314 for (int i = 0; i < mColumnsPerPage + 1; i++) {
1315 mTouchXBorders[i] = x;
1316 x += columnWidth;
1317 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001318 }
Romain Guy060b5c82010-03-04 10:07:38 -08001319
Daniel Sandler388f6792010-03-02 14:08:08 -05001320 void fling() {
1321 mInvokeFling.execute();
1322 }
Romain Guy060b5c82010-03-04 10:07:38 -08001323
Daniel Sandler388f6792010-03-02 14:08:08 -05001324 void move() {
1325 mInvokeMove.execute();
1326 }
Romain Guy060b5c82010-03-04 10:07:38 -08001327
Daniel Sandler388f6792010-03-02 14:08:08 -05001328 void moveTo(float row) {
1329 mState.targetPos = row;
1330 mState.save();
1331 mInvokeMoveTo.execute();
1332 }
Romain Guy060b5c82010-03-04 10:07:38 -08001333
Daniel Sandler388f6792010-03-02 14:08:08 -05001334 int chooseTappedIcon(int x, int y, float pos) {
1335 // Adjust for scroll position if not zero.
1336 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Romain Guy060b5c82010-03-04 10:07:38 -08001337
Daniel Sandler388f6792010-03-02 14:08:08 -05001338 int col = -1;
1339 int row = -1;
Romain Guy060b5c82010-03-04 10:07:38 -08001340 final int columnsCount = mColumnsPerPage;
1341 for (int i=0; i< columnsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001342 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1343 col = i;
1344 break;
1345 }
1346 }
Romain Guy060b5c82010-03-04 10:07:38 -08001347 final int rowsCount = mRowsPerPage;
1348 for (int i=0; i< rowsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001349 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1350 row = i;
1351 break;
1352 }
1353 }
Romain Guy060b5c82010-03-04 10:07:38 -08001354
Daniel Sandler388f6792010-03-02 14:08:08 -05001355 if (row < 0 || col < 0) {
1356 return -1;
1357 }
Romain Guy060b5c82010-03-04 10:07:38 -08001358
1359 int index = (((int)pos) * columnsCount) + (row * columnsCount) + col;
1360
Daniel Sandler388f6792010-03-02 14:08:08 -05001361 if (index >= mState.iconCount) {
1362 return -1;
1363 } else {
1364 return index;
1365 }
1366 }
Romain Guy060b5c82010-03-04 10:07:38 -08001367
Daniel Sandler388f6792010-03-02 14:08:08 -05001368 /**
1369 * You need to call save() on mState on your own after calling this.
1370 *
1371 * @return the index of the icon that was selected.
1372 */
1373 int selectIcon(int x, int y, float pos, int pressed) {
1374 final int index = chooseTappedIcon(x, y, pos);
1375 selectIcon(index, pressed);
1376 return index;
1377 }
Romain Guy060b5c82010-03-04 10:07:38 -08001378
Daniel Sandler388f6792010-03-02 14:08:08 -05001379 /**
1380 * Select the icon at the given index.
1381 *
1382 * @param index The index.
1383 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
1384 */
1385 void selectIcon(int index, int pressed) {
1386 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
1387 mState.selectedIconIndex = -1;
1388 if (mLastSelection == SELECTION_ICONS) {
1389 mLastSelection = SELECTION_NONE;
1390 }
1391 } else {
1392 if (pressed == SELECTED_FOCUSED) {
1393 mLastSelection = SELECTION_ICONS;
1394 }
Romain Guy060b5c82010-03-04 10:07:38 -08001395
Daniel Sandler388f6792010-03-02 14:08:08 -05001396 int prev = mState.selectedIconIndex;
1397 mState.selectedIconIndex = index;
Romain Guy060b5c82010-03-04 10:07:38 -08001398
Daniel Sandler388f6792010-03-02 14:08:08 -05001399 ApplicationInfo info = mAllAppsList.get(index);
1400 Bitmap selectionBitmap = mSelectionBitmap;
Romain Guy060b5c82010-03-04 10:07:38 -08001401
Daniel Sandler388f6792010-03-02 14:08:08 -05001402 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1403 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1404 pressed == SELECTED_PRESSED, info.iconBitmap);
Romain Guy060b5c82010-03-04 10:07:38 -08001405
Daniel Sandler388f6792010-03-02 14:08:08 -05001406 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
1407 Element.RGBA_8888(mRS), false);
1408 mSelectedIcon.uploadToTexture(0);
1409 mState.selectedIconTexture = mSelectedIcon.getID();
Romain Guy060b5c82010-03-04 10:07:38 -08001410
Daniel Sandler388f6792010-03-02 14:08:08 -05001411 if (prev != index) {
1412 if (info.title != null && info.title.length() > 0) {
1413 //setContentDescription(info.title);
1414 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1415 }
1416 }
1417 }
1418 }
Romain Guy060b5c82010-03-04 10:07:38 -08001419
Daniel Sandler388f6792010-03-02 14:08:08 -05001420 /**
1421 * You need to call save() on mState on your own after calling this.
1422 */
1423 void clearSelectedIcon() {
1424 mState.selectedIconIndex = -1;
1425 }
Romain Guy060b5c82010-03-04 10:07:38 -08001426
Daniel Sandler388f6792010-03-02 14:08:08 -05001427 void setHomeSelected(int mode) {
1428 final int prev = mLastSelection;
1429 switch (mode) {
1430 case SELECTED_NONE:
1431 mState.homeButtonId = mHomeButtonNormal.getID();
1432 break;
1433 case SELECTED_FOCUSED:
1434 mLastSelection = SELECTION_HOME;
1435 mState.homeButtonId = mHomeButtonFocused.getID();
1436 if (prev != SELECTION_HOME) {
1437 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1438 }
1439 break;
1440 case SELECTED_PRESSED:
1441 mState.homeButtonId = mHomeButtonPressed.getID();
1442 break;
1443 }
1444 }
Romain Guy060b5c82010-03-04 10:07:38 -08001445
Daniel Sandler388f6792010-03-02 14:08:08 -05001446 public void dumpState() {
1447 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1448 Log.d(TAG, "mRollo.mHeight=" + mHeight);
Romain Guy060b5c82010-03-04 10:07:38 -08001449 Log.d(TAG, "mRollo.mIcons=" + Arrays.toString(mIcons));
Daniel Sandler388f6792010-03-02 14:08:08 -05001450 if (mIcons != null) {
1451 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1452 }
1453 if (mIconIds != null) {
1454 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1455 }
1456 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1457 if (mLabelIds != null) {
1458 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1459 }
1460 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1461 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1462 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
1463 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1464 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1465 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1466 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1467 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1468 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1469 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1470 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1471 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1472 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1473 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1474 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1475 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1476 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1477 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1478 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1479 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1480 }
1481 }
Romain Guy060b5c82010-03-04 10:07:38 -08001482
Daniel Sandler388f6792010-03-02 14:08:08 -05001483 public void dumpState() {
1484 Log.d(TAG, "mRS=" + mRS);
1485 Log.d(TAG, "mRollo=" + mRollo);
1486 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1487 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1488 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1489 Log.d(TAG, "mLastSelection=" + mLastSelection);
1490 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1491 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1492 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1493 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1494 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1495 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1496 Log.d(TAG, "mZoom=" + mZoom);
1497 Log.d(TAG, "mPosX=" + mPosX);
1498 Log.d(TAG, "mVelocity=" + mVelocity);
1499 Log.d(TAG, "mMessageProc=" + mMessageProc);
1500 if (mRollo != null) {
1501 mRollo.dumpState();
1502 }
1503 if (mRS != null) {
1504 mRS.contextDump(0);
1505 }
1506 }
1507}
1508
1509