blob: 6d0655e7562dbebb8321d38098861da70663e0ce [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();
Daniel Sandler388f6792010-03-02 14:08:08 -050087 private ArrayList<ApplicationInfo> mAllAppsList;
88
Romain Guy13c2e7b2010-03-10 19:45:00 -080089 private static RenderScriptGL mRS;
90 private static RolloRS mRollo;
Jason Samsdd8cd8b2010-03-11 12:38:48 -080091
Daniel Sandler388f6792010-03-02 14:08:08 -050092 /**
93 * True when we are using arrow keys or trackball to drive navigation
94 */
95 private boolean mArrowNavigation = false;
96 private boolean mStartedScrolling;
97
98 /**
99 * Used to keep track of the selection when AllAppsView loses window focus.
100 * One of the SELECTION_ constants.
101 */
102 private int mLastSelection;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800103
Daniel Sandler388f6792010-03-02 14:08:08 -0500104 /**
105 * Used to keep track of the selection when AllAppsView loses window focus
106 */
107 private int mLastSelectedIcon;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800108
Daniel Sandler388f6792010-03-02 14:08:08 -0500109 private VelocityTracker mVelocityTracker;
110 private int mTouchTracking;
111 private int mMotionDownRawX;
112 private int mMotionDownRawY;
113 private int mDownIconIndex = -1;
114 private int mCurrentIconIndex = -1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800115
Daniel Sandler388f6792010-03-02 14:08:08 -0500116 private boolean mShouldGainFocus;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800117
Daniel Sandler388f6792010-03-02 14:08:08 -0500118 private boolean mHaveSurface = false;
119 private boolean mZoomDirty = false;
120 private boolean mAnimateNextZoom;
121 private float mNextZoom;
122 private float mZoom;
Daniel Sandler388f6792010-03-02 14:08:08 -0500123 private float mVelocity;
124 private AAMessage mMessageProc;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800125
Romain Guy060b5c82010-03-04 10:07:38 -0800126 private int mColumnsPerPage;
127 private int mRowsPerPage;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800128 private boolean mSurrendered;
129
Romain Guyc16fea72010-03-12 17:17:56 -0800130 private int mRestoreFocusIndex = -1;
131
Romain Guy060b5c82010-03-04 10:07:38 -0800132 @SuppressWarnings({"UnusedDeclaration"})
Daniel Sandler388f6792010-03-02 14:08:08 -0500133 static class Defines {
134 public static final int ALLOC_PARAMS = 0;
135 public static final int ALLOC_STATE = 1;
136 public static final int ALLOC_ICON_IDS = 3;
137 public static final int ALLOC_LABEL_IDS = 4;
138 public static final int ALLOC_VP_CONSTANTS = 5;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800139
Romain Guy060b5c82010-03-04 10:07:38 -0800140 public static final int COLUMNS_PER_PAGE_PORTRAIT = 4;
141 public static final int ROWS_PER_PAGE_PORTRAIT = 4;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800142
Romain Guy060b5c82010-03-04 10:07:38 -0800143 public static final int COLUMNS_PER_PAGE_LANDSCAPE = 6;
144 public static final int ROWS_PER_PAGE_LANDSCAPE = 3;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800145
Daniel Sandler388f6792010-03-02 14:08:08 -0500146 public static final int ICON_WIDTH_PX = 64;
147 public static final int ICON_TEXTURE_WIDTH_PX = 74;
148 public static final int SELECTION_TEXTURE_WIDTH_PX = 74 + 20;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800149
Daniel Sandler388f6792010-03-02 14:08:08 -0500150 public static final int ICON_HEIGHT_PX = 64;
151 public static final int ICON_TEXTURE_HEIGHT_PX = 74;
152 public static final int SELECTION_TEXTURE_HEIGHT_PX = 74 + 20;
153 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800154
Daniel Sandler388f6792010-03-02 14:08:08 -0500155 public AllApps3D(Context context, AttributeSet attrs) {
156 super(context, attrs);
157 setFocusable(true);
158 setSoundEffectsEnabled(false);
159 getHolder().setFormat(PixelFormat.TRANSLUCENT);
160 final ViewConfiguration config = ViewConfiguration.get(context);
161 mSlop = config.getScaledTouchSlop();
162 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800163
Daniel Sandler388f6792010-03-02 14:08:08 -0500164 setOnClickListener(this);
165 setOnLongClickListener(this);
166 setZOrderOnTop(true);
167 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800168
Romain Guy13c2e7b2010-03-10 19:45:00 -0800169 if (mRS == null) {
170 mRS = createRenderScript(true);
171 } else {
172 createRenderScript(mRS);
173 }
174
Romain Guy060b5c82010-03-04 10:07:38 -0800175 final DisplayMetrics metrics = getResources().getDisplayMetrics();
176 final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;
177 mColumnsPerPage = isPortrait ? Defines.COLUMNS_PER_PAGE_PORTRAIT :
178 Defines.COLUMNS_PER_PAGE_LANDSCAPE;
179 mRowsPerPage = isPortrait ? Defines.ROWS_PER_PAGE_PORTRAIT :
180 Defines.ROWS_PER_PAGE_LANDSCAPE;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800181
182 if (mRollo != null) {
183 mRollo.mAllApps = this;
184 mRollo.mRes = getResources();
185 mRollo.mInitialize = true;
186 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500187 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800188
Romain Guy060b5c82010-03-04 10:07:38 -0800189 @SuppressWarnings({"UnusedDeclaration"})
190 public AllApps3D(Context context, AttributeSet attrs, int defStyle) {
191 this(context, attrs);
192 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800193
Romain Guy13c2e7b2010-03-10 19:45:00 -0800194 public void surrender() {
195 mRS.contextSetSurface(0, 0, null);
196 mRS.mMessageCallback = null;
197 mSurrendered = true;
198 }
199
Daniel Sandler388f6792010-03-02 14:08:08 -0500200 /**
201 * Note that this implementation prohibits this view from ever being reattached.
202 */
203 @Override
204 protected void onDetachedFromWindow() {
Daniel Sandler388f6792010-03-02 14:08:08 -0500205 mRS.mMessageCallback = null;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800206 if (!mSurrendered) {
207 destroyRenderScript();
208 mRS = null;
209 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500210 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800211
Daniel Sandler388f6792010-03-02 14:08:08 -0500212 /**
213 * If you have an attached click listener, View always plays the click sound!?!?
214 * Deal with sound effects by hand.
215 */
216 public void reallyPlaySoundEffect(int sound) {
217 boolean old = isSoundEffectsEnabled();
218 setSoundEffectsEnabled(true);
219 playSoundEffect(sound);
220 setSoundEffectsEnabled(old);
221 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800222
Daniel Sandler388f6792010-03-02 14:08:08 -0500223 public void setLauncher(Launcher launcher) {
224 mLauncher = launcher;
225 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800226
Daniel Sandler388f6792010-03-02 14:08:08 -0500227 @Override
228 public void surfaceDestroyed(SurfaceHolder holder) {
229 super.surfaceDestroyed(holder);
230 // Without this, we leak mMessageCallback which leaks the context.
Romain Guy13c2e7b2010-03-10 19:45:00 -0800231 if (!mSurrendered) {
232 mRS.mMessageCallback = null;
233 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500234 // We may lose any callbacks that are pending, so make sure that we re-sync that
235 // on the next surfaceChanged.
236 mZoomDirty = true;
237 mHaveSurface = false;
238 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800239
Daniel Sandler388f6792010-03-02 14:08:08 -0500240 @Override
241 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
242 //long startTime = SystemClock.uptimeMillis();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800243
Daniel Sandler388f6792010-03-02 14:08:08 -0500244 super.surfaceChanged(holder, format, w, h);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800245
Romain Guy13c2e7b2010-03-10 19:45:00 -0800246 if (mSurrendered) return;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800247
Daniel Sandler388f6792010-03-02 14:08:08 -0500248 mHaveSurface = true;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800249
Daniel Sandler388f6792010-03-02 14:08:08 -0500250 if (mRollo == null) {
Romain Guy13c2e7b2010-03-10 19:45:00 -0800251 mRollo = new RolloRS(this);
Daniel Sandler388f6792010-03-02 14:08:08 -0500252 mRollo.init(getResources(), w, h);
253 if (mAllAppsList != null) {
254 mRollo.setApps(mAllAppsList);
255 }
256 if (mShouldGainFocus) {
257 gainFocus();
258 mShouldGainFocus = false;
259 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800260 } else if (mRollo.mInitialize) {
261 mRollo.initGl();
262 mRollo.initTouchState(w, h);
263 mRollo.mInitialize = false;
Daniel Sandler388f6792010-03-02 14:08:08 -0500264 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800265
Daniel Sandler388f6792010-03-02 14:08:08 -0500266 mRollo.dirtyCheck();
267 mRollo.resize(w, h);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800268
Daniel Sandler388f6792010-03-02 14:08:08 -0500269 if (mRS != null) {
270 mRS.mMessageCallback = mMessageProc = new AAMessage();
271 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800272
Daniel Sandler388f6792010-03-02 14:08:08 -0500273 if (mRollo.mUniformAlloc != null) {
274 float tf[] = new float[] {72.f, 72.f,
275 120.f, 120.f, 0.f, 0.f,
276 120.f, 680.f,
277 (2.f / 480.f), 0, -((float)w / 2) - 0.25f, -380.25f};
278 if (w > h) {
279 tf[6] = 40.f;
280 tf[7] = h - 40.f;
281 tf[9] = 1.f;
282 tf[10] = -((float)w / 2) - 0.25f;
283 tf[11] = -((float)h / 2) - 0.25f;
284 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800285
Daniel Sandler388f6792010-03-02 14:08:08 -0500286 mRollo.mUniformAlloc.data(tf);
287 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800288
Daniel Sandler388f6792010-03-02 14:08:08 -0500289 //long endTime = SystemClock.uptimeMillis();
290 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
291 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800292
Daniel Sandler388f6792010-03-02 14:08:08 -0500293 @Override
294 public void onWindowFocusChanged(boolean hasWindowFocus) {
295 super.onWindowFocusChanged(hasWindowFocus);
Romain Guy13c2e7b2010-03-10 19:45:00 -0800296
297 if (mSurrendered) return;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800298
Daniel Sandler388f6792010-03-02 14:08:08 -0500299 if (mArrowNavigation) {
300 if (!hasWindowFocus) {
301 // Clear selection when we lose window focus
302 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
303 mRollo.setHomeSelected(SELECTED_NONE);
304 mRollo.clearSelectedIcon();
305 mRollo.mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -0800306 } else {
Daniel Sandler388f6792010-03-02 14:08:08 -0500307 if (mRollo.mState.iconCount > 0) {
308 if (mLastSelection == SELECTION_ICONS) {
309 int selection = mLastSelectedIcon;
Romain Guy6a42cf32010-03-12 16:03:52 -0800310 final int firstIcon = Math.round(mRollo.mScrollPos) * mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500311 if (selection < 0 || // No selection
312 selection < firstIcon || // off the top of the screen
313 selection >= mRollo.mState.iconCount || // past last icon
314 selection >= firstIcon + // past last icon on screen
Romain Guy060b5c82010-03-04 10:07:38 -0800315 (mColumnsPerPage * mRowsPerPage)) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500316 selection = firstIcon;
317 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800318
Daniel Sandler388f6792010-03-02 14:08:08 -0500319 // Select the first icon when we gain window focus
320 mRollo.selectIcon(selection, SELECTED_FOCUSED);
321 mRollo.mState.save();
322 } else if (mLastSelection == SELECTION_HOME) {
323 mRollo.setHomeSelected(SELECTED_FOCUSED);
324 mRollo.mState.save();
325 }
326 }
327 }
328 }
329 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800330
Daniel Sandler388f6792010-03-02 14:08:08 -0500331 @Override
332 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
333 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800334
Romain Guy13c2e7b2010-03-10 19:45:00 -0800335 if (!isVisible() || mSurrendered) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500336 return;
337 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800338
Daniel Sandler388f6792010-03-02 14:08:08 -0500339 if (gainFocus) {
340 if (mRollo != null) {
341 gainFocus();
342 } else {
343 mShouldGainFocus = true;
344 }
345 } else {
346 if (mRollo != null) {
347 if (mArrowNavigation) {
348 // Clear selection when we lose focus
349 mRollo.clearSelectedIcon();
350 mRollo.setHomeSelected(SELECTED_NONE);
351 mRollo.mState.save();
352 mArrowNavigation = false;
353 }
354 } else {
355 mShouldGainFocus = false;
356 }
357 }
358 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800359
Daniel Sandler388f6792010-03-02 14:08:08 -0500360 private void gainFocus() {
361 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
362 // Select the first icon when we gain keyboard focus
363 mArrowNavigation = true;
Romain Guy6a42cf32010-03-12 16:03:52 -0800364 mRollo.selectIcon(Math.round(mRollo.mScrollPos) * mColumnsPerPage, SELECTED_FOCUSED);
Daniel Sandler388f6792010-03-02 14:08:08 -0500365 mRollo.mState.save();
366 }
367 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800368
Daniel Sandler388f6792010-03-02 14:08:08 -0500369 @Override
370 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800371
Daniel Sandler388f6792010-03-02 14:08:08 -0500372 boolean handled = false;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800373
Daniel Sandler388f6792010-03-02 14:08:08 -0500374 if (!isVisible()) {
375 return false;
376 }
377 final int iconCount = mRollo.mState.iconCount;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800378
Daniel Sandler388f6792010-03-02 14:08:08 -0500379 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
380 if (mArrowNavigation) {
381 if (mLastSelection == SELECTION_HOME) {
382 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
383 mLauncher.closeAllApps(true);
384 } else {
385 int whichApp = mRollo.mState.selectedIconIndex;
386 if (whichApp >= 0) {
387 ApplicationInfo app = mAllAppsList.get(whichApp);
388 mLauncher.startActivitySafely(app.intent);
389 handled = true;
390 }
391 }
392 }
393 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800394
Daniel Sandler388f6792010-03-02 14:08:08 -0500395 if (iconCount > 0) {
Romain Guy6a42cf32010-03-12 16:03:52 -0800396 final boolean isPortrait = getWidth() < getHeight();
397
Daniel Sandler388f6792010-03-02 14:08:08 -0500398 mArrowNavigation = true;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800399
Daniel Sandler388f6792010-03-02 14:08:08 -0500400 int currentSelection = mRollo.mState.selectedIconIndex;
Romain Guy6a42cf32010-03-12 16:03:52 -0800401 int currentTopRow = Math.round(mRollo.mScrollPos);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800402
Romain Guy060b5c82010-03-04 10:07:38 -0800403 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE_PORTRAIT-1
404 final int currentPageCol = currentSelection % mColumnsPerPage;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800405
Romain Guy060b5c82010-03-04 10:07:38 -0800406 // The row of the current selection, in the range 0..ROWS_PER_PAGE_PORTRAIT-1
Romain Guy6a42cf32010-03-12 16:03:52 -0800407 final int currentPageRow = (currentSelection - (currentTopRow * mColumnsPerPage))
Romain Guy060b5c82010-03-04 10:07:38 -0800408 / mRowsPerPage;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800409
Daniel Sandler388f6792010-03-02 14:08:08 -0500410 int newSelection = currentSelection;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800411
Daniel Sandler388f6792010-03-02 14:08:08 -0500412 switch (keyCode) {
413 case KeyEvent.KEYCODE_DPAD_UP:
414 if (mLastSelection == SELECTION_HOME) {
Romain Guy6a42cf32010-03-12 16:03:52 -0800415 if (isPortrait) {
416 mRollo.setHomeSelected(SELECTED_NONE);
417 int lastRowCount = iconCount % mColumnsPerPage;
418 if (lastRowCount == 0) {
419 lastRowCount = mColumnsPerPage;
420 }
421 newSelection = iconCount - lastRowCount + (mColumnsPerPage / 2);
422 if (newSelection >= iconCount) {
423 newSelection = iconCount-1;
424 }
425 int target = (newSelection / mColumnsPerPage) - (mRowsPerPage - 1);
426 if (target < 0) {
427 target = 0;
428 }
429 if (currentTopRow != target) {
430 mRollo.moveTo(target);
431 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500432 }
433 } else {
434 if (currentPageRow > 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800435 newSelection = currentSelection - mColumnsPerPage;
Romain Guy6a42cf32010-03-12 16:03:52 -0800436 if (currentTopRow > newSelection / mColumnsPerPage) {
437 mRollo.moveTo(newSelection / mColumnsPerPage);
438 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500439 } else if (currentTopRow > 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800440 newSelection = currentSelection - mColumnsPerPage;
441 mRollo.moveTo(newSelection / mColumnsPerPage);
Daniel Sandler388f6792010-03-02 14:08:08 -0500442 } else if (currentPageRow != 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800443 newSelection = currentTopRow * mRowsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500444 }
445 }
446 handled = true;
447 break;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800448
Daniel Sandler388f6792010-03-02 14:08:08 -0500449 case KeyEvent.KEYCODE_DPAD_DOWN: {
Romain Guy060b5c82010-03-04 10:07:38 -0800450 final int rowCount = iconCount / mColumnsPerPage
451 + (iconCount % mColumnsPerPage == 0 ? 0 : 1);
452 final int currentRow = currentSelection / mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500453 if (mLastSelection != SELECTION_HOME) {
454 if (currentRow < rowCount-1) {
455 mRollo.setHomeSelected(SELECTED_NONE);
456 if (currentSelection < 0) {
457 newSelection = 0;
458 } else {
Romain Guy060b5c82010-03-04 10:07:38 -0800459 newSelection = currentSelection + mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500460 }
461 if (newSelection >= iconCount) {
462 // Go from D to G in this arrangement:
463 // A B C D
464 // E F G
465 newSelection = iconCount - 1;
466 }
Romain Guy060b5c82010-03-04 10:07:38 -0800467 if (currentPageRow >= mRowsPerPage - 1) {
Romain Guy6a42cf32010-03-12 16:03:52 -0800468 mRollo.moveTo((newSelection / mColumnsPerPage) - mRowsPerPage + 1);
Daniel Sandler388f6792010-03-02 14:08:08 -0500469 }
Romain Guy6a42cf32010-03-12 16:03:52 -0800470 } else if (isPortrait) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500471 newSelection = -1;
472 mRollo.setHomeSelected(SELECTED_FOCUSED);
473 }
474 }
475 handled = true;
476 break;
477 }
478 case KeyEvent.KEYCODE_DPAD_LEFT:
479 if (mLastSelection != SELECTION_HOME) {
480 if (currentPageCol > 0) {
481 newSelection = currentSelection - 1;
482 }
Romain Guy6a42cf32010-03-12 16:03:52 -0800483 } else if (!isPortrait) {
484 newSelection = ((int) (mRollo.mScrollPos) * mColumnsPerPage) +
485 (mRowsPerPage / 2 * mColumnsPerPage) + mColumnsPerPage - 1;
486 mRollo.setHomeSelected(SELECTED_NONE);
Daniel Sandler388f6792010-03-02 14:08:08 -0500487 }
488 handled = true;
489 break;
490 case KeyEvent.KEYCODE_DPAD_RIGHT:
491 if (mLastSelection != SELECTION_HOME) {
Romain Guy6a42cf32010-03-12 16:03:52 -0800492 if (!isPortrait && (currentPageCol == mColumnsPerPage - 1 ||
493 currentSelection == iconCount - 1)) {
494 newSelection = -1;
495 mRollo.setHomeSelected(SELECTED_FOCUSED);
496 } else if ((currentPageCol < mColumnsPerPage - 1) &&
Daniel Sandler388f6792010-03-02 14:08:08 -0500497 (currentSelection < iconCount - 1)) {
498 newSelection = currentSelection + 1;
499 }
500 }
501 handled = true;
502 break;
503 }
504 if (newSelection != currentSelection) {
505 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
506 mRollo.mState.save();
507 }
508 }
509 return handled;
510 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800511
Daniel Sandler388f6792010-03-02 14:08:08 -0500512 @Override
513 public boolean onTouchEvent(MotionEvent ev)
514 {
515 mArrowNavigation = false;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800516
Daniel Sandler388f6792010-03-02 14:08:08 -0500517 if (!isVisible()) {
518 return true;
519 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800520
Daniel Sandler388f6792010-03-02 14:08:08 -0500521 if (mLocks != 0) {
522 return true;
523 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800524
Daniel Sandler388f6792010-03-02 14:08:08 -0500525 super.onTouchEvent(ev);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800526
Daniel Sandler388f6792010-03-02 14:08:08 -0500527 int x = (int)ev.getX();
528 int y = (int)ev.getY();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800529
Romain Guyce115852010-03-04 12:15:37 -0800530 final boolean isPortrait = getWidth() < getHeight();
Daniel Sandler388f6792010-03-02 14:08:08 -0500531 int action = ev.getAction();
532 switch (action) {
533 case MotionEvent.ACTION_DOWN:
Romain Guyce115852010-03-04 12:15:37 -0800534 if ((isPortrait && y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) ||
535 (!isPortrait && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500536 mTouchTracking = TRACKING_HOME;
537 mRollo.setHomeSelected(SELECTED_PRESSED);
538 mRollo.mState.save();
539 mCurrentIconIndex = -1;
540 } else {
541 mTouchTracking = TRACKING_FLING;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800542
Daniel Sandler388f6792010-03-02 14:08:08 -0500543 mMotionDownRawX = (int)ev.getRawX();
544 mMotionDownRawY = (int)ev.getRawY();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800545
Daniel Sandler388f6792010-03-02 14:08:08 -0500546 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
547 mRollo.mState.newTouchDown = 1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800548
Daniel Sandler388f6792010-03-02 14:08:08 -0500549 if (!mRollo.checkClickOK()) {
550 mRollo.clearSelectedIcon();
551 } else {
552 mDownIconIndex = mCurrentIconIndex
Romain Guy6a42cf32010-03-12 16:03:52 -0800553 = mRollo.selectIcon(x, y, SELECTED_PRESSED);
Daniel Sandler388f6792010-03-02 14:08:08 -0500554 if (mDownIconIndex < 0) {
555 // if nothing was selected, no long press.
556 cancelLongPress();
557 }
558 }
559 mRollo.mState.save();
560 mRollo.move();
561 mVelocityTracker = VelocityTracker.obtain();
562 mVelocityTracker.addMovement(ev);
563 mStartedScrolling = false;
564 }
565 break;
566 case MotionEvent.ACTION_MOVE:
567 case MotionEvent.ACTION_OUTSIDE:
568 if (mTouchTracking == TRACKING_HOME) {
Romain Guyce115852010-03-04 12:15:37 -0800569 mRollo.setHomeSelected((isPortrait &&
570 y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) || (!isPortrait
571 && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])
Daniel Sandler388f6792010-03-02 14:08:08 -0500572 ? SELECTED_PRESSED : SELECTED_NONE);
573 mRollo.mState.save();
574 } else if (mTouchTracking == TRACKING_FLING) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500575 int rawY = (int)ev.getRawY();
576 int slop;
577 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800578
Daniel Sandler388f6792010-03-02 14:08:08 -0500579 if (!mStartedScrolling && slop < mSlop) {
580 // don't update anything so when we do start scrolling
581 // below, we get the right delta.
Romain Guy6a42cf32010-03-12 16:03:52 -0800582 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y);
Daniel Sandler388f6792010-03-02 14:08:08 -0500583 if (mDownIconIndex != mCurrentIconIndex) {
584 // If a different icon is selected, don't allow it to be picked up.
585 // This handles off-axis dragging.
586 cancelLongPress();
587 mCurrentIconIndex = -1;
588 }
589 } else {
590 if (!mStartedScrolling) {
591 cancelLongPress();
592 mCurrentIconIndex = -1;
593 }
594 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
595 mRollo.mState.newTouchDown = 1;
596 mRollo.move();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800597
Daniel Sandler388f6792010-03-02 14:08:08 -0500598 mStartedScrolling = true;
599 mRollo.clearSelectedIcon();
600 mVelocityTracker.addMovement(ev);
601 mRollo.mState.save();
602 }
603 }
604 break;
605 case MotionEvent.ACTION_UP:
606 case MotionEvent.ACTION_CANCEL:
607 if (mTouchTracking == TRACKING_HOME) {
608 if (action == MotionEvent.ACTION_UP) {
Romain Guyce115852010-03-04 12:15:37 -0800609 if ((isPortrait && y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) ||
610 (!isPortrait && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500611 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
612 mLauncher.closeAllApps(true);
613 }
614 mRollo.setHomeSelected(SELECTED_NONE);
615 mRollo.mState.save();
616 }
617 mCurrentIconIndex = -1;
618 } else if (mTouchTracking == TRACKING_FLING) {
619 mRollo.mState.newTouchDown = 0;
620 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800621
Daniel Sandler388f6792010-03-02 14:08:08 -0500622 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
623 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
624 mRollo.clearSelectedIcon();
625 mRollo.mState.save();
626 mRollo.fling();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800627
Daniel Sandler388f6792010-03-02 14:08:08 -0500628 if (mVelocityTracker != null) {
629 mVelocityTracker.recycle();
630 mVelocityTracker = null;
631 }
632 }
633 mTouchTracking = TRACKING_NONE;
634 break;
635 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800636
Daniel Sandler388f6792010-03-02 14:08:08 -0500637 return true;
638 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800639
Daniel Sandler388f6792010-03-02 14:08:08 -0500640 public void onClick(View v) {
641 if (mLocks != 0 || !isVisible()) {
642 return;
643 }
644 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
645 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
646 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
647 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
648 mLauncher.startActivitySafely(app.intent);
649 }
650 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800651
Daniel Sandler388f6792010-03-02 14:08:08 -0500652 public boolean onLongClick(View v) {
653 if (mLocks != 0 || !isVisible()) {
654 return true;
655 }
656 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
657 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
658 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800659
Daniel Sandler388f6792010-03-02 14:08:08 -0500660 Bitmap bmp = app.iconBitmap;
661 final int w = bmp.getWidth();
662 final int h = bmp.getHeight();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800663
Daniel Sandler388f6792010-03-02 14:08:08 -0500664 // We don't really have an accurate location to use. This will do.
665 int screenX = mMotionDownRawX - (w / 2);
666 int screenY = mMotionDownRawY - h;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800667
Daniel Sandler388f6792010-03-02 14:08:08 -0500668 mDragController.startDrag(bmp, screenX, screenY,
669 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800670
Daniel Sandler388f6792010-03-02 14:08:08 -0500671 mLauncher.closeAllApps(true);
672 }
673 return true;
674 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800675
Daniel Sandler388f6792010-03-02 14:08:08 -0500676 @Override
677 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
678 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
679 if (!isVisible()) {
680 return false;
681 }
682 String text = null;
683 int index;
684 int count = mAllAppsList.size() + 1; // +1 is home
685 int pos = -1;
686 switch (mLastSelection) {
687 case SELECTION_ICONS:
688 index = mRollo.mState.selectedIconIndex;
689 if (index >= 0) {
690 ApplicationInfo info = mAllAppsList.get(index);
691 if (info.title != null) {
692 text = info.title.toString();
693 pos = index;
694 }
695 }
696 break;
697 case SELECTION_HOME:
698 text = getContext().getString(R.string.all_apps_home_button_label);
699 pos = count;
700 break;
701 }
702 if (text != null) {
703 event.setEnabled(true);
704 event.getText().add(text);
705 //event.setContentDescription(text);
706 event.setItemCount(count);
707 event.setCurrentItemIndex(pos);
708 }
709 }
710 return false;
711 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800712
Daniel Sandler388f6792010-03-02 14:08:08 -0500713 public void setDragController(DragController dragger) {
714 mDragController = dragger;
715 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800716
Daniel Sandler388f6792010-03-02 14:08:08 -0500717 public void onDropCompleted(View target, boolean success) {
718 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800719
Daniel Sandler388f6792010-03-02 14:08:08 -0500720 /**
721 * Zoom to the specifed level.
722 *
723 * @param zoom [0..1] 0 is hidden, 1 is open
724 */
725 public void zoom(float zoom, boolean animate) {
726 cancelLongPress();
727 mNextZoom = zoom;
728 mAnimateNextZoom = animate;
729 // if we do setZoom while we don't have a surface, we won't
730 // get the callbacks that actually set mZoom.
731 if (mRollo == null || !mHaveSurface) {
732 mZoomDirty = true;
733 mZoom = zoom;
Daniel Sandler388f6792010-03-02 14:08:08 -0500734 } else {
735 mRollo.setZoom(zoom, animate);
736 }
737 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800738
Daniel Sandler388f6792010-03-02 14:08:08 -0500739 public boolean isVisible() {
740 return mZoom > 0.001f;
741 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800742
Daniel Sandler388f6792010-03-02 14:08:08 -0500743 public boolean isOpaque() {
744 return mZoom > 0.999f;
745 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800746
Daniel Sandler388f6792010-03-02 14:08:08 -0500747 public void setApps(ArrayList<ApplicationInfo> list) {
748 if (mRS == null) {
749 // We've been removed from the window. Don't bother with all this.
750 return;
751 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800752
753 boolean reload = false;
754 if (mAllAppsList == null) {
755 reload = true;
756 } else if (list.size() != mAllAppsList.size()) {
757 reload = true;
758 } else {
759 final int count = list.size();
760 for (int i = 0; i < count; i++) {
761 if (list.get(i) != mAllAppsList.get(i)) {
762 reload = true;
763 break;
764 }
765 }
766 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800767
Daniel Sandler388f6792010-03-02 14:08:08 -0500768 mAllAppsList = list;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800769 if (mRollo != null && reload) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500770 mRollo.setApps(list);
771 }
Romain Guyc16fea72010-03-12 17:17:56 -0800772
773 if (hasFocus() && mRestoreFocusIndex != -1) {
774 mRollo.selectIcon(mRestoreFocusIndex, SELECTED_FOCUSED);
775 mRollo.mState.save();
776 mRestoreFocusIndex = -1;
777 }
778
Daniel Sandler388f6792010-03-02 14:08:08 -0500779 mLocks &= ~LOCK_ICONS_PENDING;
780 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800781
Daniel Sandler388f6792010-03-02 14:08:08 -0500782 public void addApps(ArrayList<ApplicationInfo> list) {
783 if (mAllAppsList == null) {
784 // Not done loading yet. We'll find out about it later.
785 return;
786 }
787 if (mRS == null) {
788 // We've been removed from the window. Don't bother with all this.
789 return;
790 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800791
Daniel Sandler388f6792010-03-02 14:08:08 -0500792 final int N = list.size();
793 if (mRollo != null) {
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800794 mRollo.pause();
Daniel Sandler388f6792010-03-02 14:08:08 -0500795 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
796 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800797
Daniel Sandler388f6792010-03-02 14:08:08 -0500798 for (int i=0; i<N; i++) {
799 final ApplicationInfo item = list.get(i);
800 int index = Collections.binarySearch(mAllAppsList, item,
801 LauncherModel.APP_NAME_COMPARATOR);
802 if (index < 0) {
803 index = -(index+1);
804 }
805 mAllAppsList.add(index, item);
806 if (mRollo != null) {
807 mRollo.addApp(index, item);
808 }
809 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800810
Daniel Sandler388f6792010-03-02 14:08:08 -0500811 if (mRollo != null) {
812 mRollo.saveAppsList();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800813 mRollo.resume();
Daniel Sandler388f6792010-03-02 14:08:08 -0500814 }
815 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800816
Daniel Sandler388f6792010-03-02 14:08:08 -0500817 public void removeApps(ArrayList<ApplicationInfo> list) {
818 if (mAllAppsList == null) {
819 // Not done loading yet. We'll find out about it later.
820 return;
821 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800822
823 if (mRollo != null) {
824 mRollo.pause();
825 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500826 final int N = list.size();
827 for (int i=0; i<N; i++) {
828 final ApplicationInfo item = list.get(i);
829 int index = findAppByComponent(mAllAppsList, item);
830 if (index >= 0) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500831 mAllAppsList.remove(index);
832 if (mRollo != null) {
833 mRollo.removeApp(index);
834 }
835 } else {
836 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
837 // Try to recover. This should keep us from crashing for now.
838 }
839 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800840
Daniel Sandler388f6792010-03-02 14:08:08 -0500841 if (mRollo != null) {
842 mRollo.saveAppsList();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800843 mRollo.resume();
Daniel Sandler388f6792010-03-02 14:08:08 -0500844 }
845 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800846
Joe Onorato64e6be72010-03-05 15:05:52 -0500847 public void updateApps(ArrayList<ApplicationInfo> list) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500848 // Just remove and add, because they may need to be re-sorted.
849 removeApps(list);
850 addApps(list);
851 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800852
Daniel Sandler388f6792010-03-02 14:08:08 -0500853 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
854 ComponentName component = item.intent.getComponent();
855 final int N = list.size();
856 for (int i=0; i<N; i++) {
857 ApplicationInfo x = list.get(i);
858 if (x.intent.getComponent().equals(component)) {
859 return i;
860 }
861 }
862 return -1;
863 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800864
Romain Guy060b5c82010-03-04 10:07:38 -0800865 /*
Daniel Sandler388f6792010-03-02 14:08:08 -0500866 private static int countPages(int iconCount) {
Romain Guy060b5c82010-03-04 10:07:38 -0800867 int iconsPerPage = getColumnsCount() * Defines.ROWS_PER_PAGE_PORTRAIT;
Daniel Sandler388f6792010-03-02 14:08:08 -0500868 int pages = iconCount / iconsPerPage;
869 if (pages*iconsPerPage != iconCount) {
870 pages++;
871 }
872 return pages;
873 }
Romain Guy060b5c82010-03-04 10:07:38 -0800874 */
Daniel Sandler388f6792010-03-02 14:08:08 -0500875
876 class AAMessage extends RenderScript.RSMessage {
877 public void run() {
Romain Guy6a42cf32010-03-12 16:03:52 -0800878 mRollo.mScrollPos = ((float)mData[0]) / (1 << 16);
Daniel Sandler388f6792010-03-02 14:08:08 -0500879 mVelocity = ((float)mData[1]) / (1 << 16);
880 mZoom = ((float)mData[2]) / (1 << 16);
881 mZoomDirty = false;
882 }
883 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800884
Romain Guy13c2e7b2010-03-10 19:45:00 -0800885 public static class RolloRS {
Daniel Sandler388f6792010-03-02 14:08:08 -0500886 // Allocations ======
887 private int mWidth;
888 private int mHeight;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800889
Daniel Sandler388f6792010-03-02 14:08:08 -0500890 private Resources mRes;
891 private Script mScript;
892 private Script.Invokable mInvokeMove;
893 private Script.Invokable mInvokeMoveTo;
894 private Script.Invokable mInvokeFling;
895 private Script.Invokable mInvokeResetWAR;
896 private Script.Invokable mInvokeSetZoom;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800897
Daniel Sandler388f6792010-03-02 14:08:08 -0500898 private ProgramStore mPSIcons;
899 private ProgramFragment mPFTexMip;
900 private ProgramFragment mPFTexMipAlpha;
901 private ProgramFragment mPFTexNearest;
902 private ProgramVertex mPV;
903 private ProgramVertex mPVCurve;
904 private SimpleMesh mMesh;
905 private ProgramVertex.MatrixAllocation mPVA;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800906
Daniel Sandler388f6792010-03-02 14:08:08 -0500907 private Allocation mUniformAlloc;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800908
Daniel Sandler388f6792010-03-02 14:08:08 -0500909 private Allocation mHomeButtonNormal;
910 private Allocation mHomeButtonFocused;
911 private Allocation mHomeButtonPressed;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800912
Daniel Sandler388f6792010-03-02 14:08:08 -0500913 private Allocation[] mIcons;
914 private int[] mIconIds;
915 private Allocation mAllocIconIds;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800916
Daniel Sandler388f6792010-03-02 14:08:08 -0500917 private Allocation[] mLabels;
918 private int[] mLabelIds;
919 private Allocation mAllocLabelIds;
920 private Allocation mSelectedIcon;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800921
Daniel Sandler388f6792010-03-02 14:08:08 -0500922 private int[] mTouchYBorders;
923 private int[] mTouchXBorders;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800924
Daniel Sandler388f6792010-03-02 14:08:08 -0500925 private Bitmap mSelectionBitmap;
926 private Canvas mSelectionCanvas;
Romain Guy6a42cf32010-03-12 16:03:52 -0800927
928 private float mScrollPos;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800929
Daniel Sandler388f6792010-03-02 14:08:08 -0500930 Params mParams;
931 State mState;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800932
Romain Guy13c2e7b2010-03-10 19:45:00 -0800933 AllApps3D mAllApps;
934 boolean mInitialize;
935
Daniel Sandler388f6792010-03-02 14:08:08 -0500936 class BaseAlloc {
937 Allocation mAlloc;
938 Type mType;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800939
Daniel Sandler388f6792010-03-02 14:08:08 -0500940 void save() {
941 mAlloc.data(this);
942 }
943 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800944
Daniel Sandler388f6792010-03-02 14:08:08 -0500945 private boolean checkClickOK() {
Romain Guy13c2e7b2010-03-10 19:45:00 -0800946 return (Math.abs(mAllApps.mVelocity) < 0.4f) &&
Romain Guy6a42cf32010-03-12 16:03:52 -0800947 (Math.abs(mScrollPos - Math.round(mScrollPos)) < 0.4f);
Daniel Sandler388f6792010-03-02 14:08:08 -0500948 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800949
950 void pause() {
951 mRS.contextBindRootScript(null);
952 }
953
954 void resume() {
955 mRS.contextBindRootScript(mScript);
956 }
957
Daniel Sandler388f6792010-03-02 14:08:08 -0500958 class Params extends BaseAlloc {
959 Params() {
960 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
961 mAlloc = Allocation.createTyped(mRS, mType);
962 save();
963 }
964 public int bubbleWidth;
965 public int bubbleHeight;
966 public int bubbleBitmapWidth;
967 public int bubbleBitmapHeight;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800968
Daniel Sandler388f6792010-03-02 14:08:08 -0500969 public int homeButtonWidth;
970 public int homeButtonHeight;
971 public int homeButtonTextureWidth;
972 public int homeButtonTextureHeight;
973 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800974
Daniel Sandler388f6792010-03-02 14:08:08 -0500975 class State extends BaseAlloc {
976 public float newPositionX;
977 public int newTouchDown;
978 public float flingVelocity;
979 public int iconCount;
980 public int selectedIconIndex = -1;
981 public int selectedIconTexture;
982 public float zoomTarget;
983 public int homeButtonId;
984 public float targetPos;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800985
Daniel Sandler388f6792010-03-02 14:08:08 -0500986 State() {
987 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
988 mAlloc = Allocation.createTyped(mRS, mType);
989 save();
990 }
991 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800992
Romain Guy13c2e7b2010-03-10 19:45:00 -0800993 public RolloRS(AllApps3D allApps) {
994 mAllApps = allApps;
Daniel Sandler388f6792010-03-02 14:08:08 -0500995 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800996
Daniel Sandler388f6792010-03-02 14:08:08 -0500997 public void init(Resources res, int width, int height) {
998 mRes = res;
999 mWidth = width;
1000 mHeight = height;
1001 initProgramVertex();
1002 initProgramFragment();
1003 initProgramStore();
1004 initGl();
1005 initData();
Romain Guy13c2e7b2010-03-10 19:45:00 -08001006 initTouchState(width, height);
Daniel Sandler388f6792010-03-02 14:08:08 -05001007 initRs();
1008 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001009
Daniel Sandler388f6792010-03-02 14:08:08 -05001010 public void initMesh() {
1011 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001012
Daniel Sandler388f6792010-03-02 14:08:08 -05001013 for (int ct=0; ct < 16; ct++) {
1014 float pos = (1.f / (16.f - 1)) * ct;
1015 tm.addVertex(0.0f, pos);
1016 tm.addVertex(1.0f, pos);
1017 }
1018 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
1019 tm.addTriangle(ct, ct+1, ct+2);
1020 tm.addTriangle(ct+1, ct+3, ct+2);
1021 }
1022 mMesh = tm.create();
1023 mMesh.setName("SMCell");
1024 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001025
Daniel Sandler388f6792010-03-02 14:08:08 -05001026 void resize(int w, int h) {
1027 mPVA.setupProjectionNormalized(w, h);
1028 mWidth = w;
1029 mHeight = h;
1030 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001031
Daniel Sandler388f6792010-03-02 14:08:08 -05001032 private void initProgramVertex() {
1033 mPVA = new ProgramVertex.MatrixAllocation(mRS);
1034 resize(mWidth, mHeight);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001035
Daniel Sandler388f6792010-03-02 14:08:08 -05001036 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
1037 pvb.setTextureMatrixEnable(true);
1038 mPV = pvb.create();
1039 mPV.setName("PV");
1040 mPV.bindAllocation(mPVA);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001041
Daniel Sandler388f6792010-03-02 14:08:08 -05001042 Element.Builder eb = new Element.Builder(mRS);
1043 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
1044 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
1045 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "BendPos");
1046 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
1047 Element e = eb.create();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001048
Daniel Sandler388f6792010-03-02 14:08:08 -05001049 mUniformAlloc = Allocation.createSized(mRS, e, 1);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001050
Daniel Sandler388f6792010-03-02 14:08:08 -05001051 initMesh();
1052 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
Romain Guy060b5c82010-03-04 10:07:38 -08001053 String t = "void main() {\n" +
1054 // Animation
1055 " float ani = UNI_Position.z;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001056
Romain Guy060b5c82010-03-04 10:07:38 -08001057 " float bendY1 = UNI_BendPos.x;\n" +
1058 " float bendY2 = UNI_BendPos.y;\n" +
1059 " float bendAngle = 47.0 * (3.14 / 180.0);\n" +
1060 " float bendDistance = bendY1 * 0.4;\n" +
1061 " float distanceDimLevel = 0.6;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001062
Romain Guy060b5c82010-03-04 10:07:38 -08001063 " float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
1064 " float aDy = cos(bendAngle);\n" +
1065 " float aDz = sin(bendAngle);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001066
Romain Guy060b5c82010-03-04 10:07:38 -08001067 " float scale = (2.0 / 480.0);\n" +
1068 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
1069 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
1070 " float y = 0.0;\n" +
1071 " float z = 0.0;\n" +
1072 " float lum = 1.0;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001073
Romain Guy060b5c82010-03-04 10:07:38 -08001074 " float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
1075 " y += cv * aDy;\n" +
1076 " z += -cv * aDz;\n" +
1077 " cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" + // curve range
1078 " lum += cv / bendDistance * distanceDimLevel;\n" +
1079 " y += cv * cos(cv * bendStep);\n" +
1080 " z += cv * sin(cv * bendStep);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001081
Romain Guy060b5c82010-03-04 10:07:38 -08001082 " cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
1083 " y += cv * aDy;\n" +
1084 " z += cv * aDz;\n" +
1085 " cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
1086 " lum -= cv / bendDistance * distanceDimLevel;\n" +
1087 " y += cv * cos(cv * bendStep);\n" +
1088 " z += cv * sin(cv * bendStep);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001089
Romain Guy060b5c82010-03-04 10:07:38 -08001090 " y += clamp(ys, bendY1, bendY2);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001091
Romain Guy060b5c82010-03-04 10:07:38 -08001092 " vec4 pos;\n" +
1093 " pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
1094 " pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
1095 " pos.z = z * UNI_ScaleOffset.x;\n" +
1096 " pos.w = 1.0;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001097
Romain Guy060b5c82010-03-04 10:07:38 -08001098 " pos.x *= 1.0 + ani * 4.0;\n" +
1099 " pos.y *= 1.0 + ani * 4.0;\n" +
1100 " pos.z -= ani * 1.5;\n" +
1101 " lum *= 1.0 - ani;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001102
Romain Guy060b5c82010-03-04 10:07:38 -08001103 " gl_Position = UNI_MVP * pos;\n" +
1104 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
1105 " varTex0.xy = ATTRIB_position;\n" +
1106 " varTex0.y = 1.0 - varTex0.y;\n" +
1107 " varTex0.zw = vec2(0.0, 0.0);\n" +
1108 "}\n";
Daniel Sandler388f6792010-03-02 14:08:08 -05001109 sb.setShader(t);
1110 sb.addConstant(mUniformAlloc.getType());
1111 sb.addInput(mMesh.getVertexType(0).getElement());
1112 mPVCurve = sb.create();
1113 mPVCurve.setName("PVCurve");
1114 mPVCurve.bindAllocation(mPVA);
1115 mPVCurve.bindConstants(mUniformAlloc, 1);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001116
Daniel Sandler388f6792010-03-02 14:08:08 -05001117 mRS.contextBindProgramVertex(mPV);
1118 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001119
Daniel Sandler388f6792010-03-02 14:08:08 -05001120 private void initProgramFragment() {
1121 Sampler.Builder sb = new Sampler.Builder(mRS);
1122 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
1123 sb.setMag(Sampler.Value.NEAREST);
1124 sb.setWrapS(Sampler.Value.CLAMP);
1125 sb.setWrapT(Sampler.Value.CLAMP);
1126 Sampler linear = sb.create();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001127
Daniel Sandler388f6792010-03-02 14:08:08 -05001128 sb.setMin(Sampler.Value.NEAREST);
1129 sb.setMag(Sampler.Value.NEAREST);
1130 Sampler nearest = sb.create();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001131
Daniel Sandler388f6792010-03-02 14:08:08 -05001132 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
1133 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1134 ProgramFragment.Builder.Format.RGBA, 0);
1135 mPFTexMip = bf.create();
1136 mPFTexMip.setName("PFTexMip");
1137 mPFTexMip.bindSampler(linear, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001138
Daniel Sandler388f6792010-03-02 14:08:08 -05001139 mPFTexNearest = bf.create();
1140 mPFTexNearest.setName("PFTexNearest");
1141 mPFTexNearest.bindSampler(nearest, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001142
Daniel Sandler388f6792010-03-02 14:08:08 -05001143 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1144 ProgramFragment.Builder.Format.ALPHA, 0);
1145 mPFTexMipAlpha = bf.create();
1146 mPFTexMipAlpha.setName("PFTexMipAlpha");
1147 mPFTexMipAlpha.bindSampler(linear, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001148
Daniel Sandler388f6792010-03-02 14:08:08 -05001149 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001150
Daniel Sandler388f6792010-03-02 14:08:08 -05001151 private void initProgramStore() {
1152 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1153 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
1154 bs.setColorMask(true,true,true,false);
1155 bs.setDitherEnable(true);
1156 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1157 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1158 mPSIcons = bs.create();
1159 mPSIcons.setName("PSIcons");
1160 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001161
Daniel Sandler388f6792010-03-02 14:08:08 -05001162 private void initGl() {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001163 mTouchXBorders = new int[mAllApps.mColumnsPerPage + 1];
1164 mTouchYBorders = new int[mAllApps.mRowsPerPage + 1];
Daniel Sandler388f6792010-03-02 14:08:08 -05001165 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001166
Daniel Sandler388f6792010-03-02 14:08:08 -05001167 private void initData() {
1168 mParams = new Params();
1169 mState = new State();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001170
Romain Guy13c2e7b2010-03-10 19:45:00 -08001171 final Utilities.BubbleText bubble = new Utilities.BubbleText(mAllApps.getContext());
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001172
Daniel Sandler388f6792010-03-02 14:08:08 -05001173 mParams.bubbleWidth = bubble.getBubbleWidth();
1174 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1175 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1176 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001177
Daniel Sandler388f6792010-03-02 14:08:08 -05001178 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1179 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1180 mHomeButtonNormal.uploadToTexture(0);
1181 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1182 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1183 mHomeButtonFocused.uploadToTexture(0);
1184 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1185 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1186 mHomeButtonPressed.uploadToTexture(0);
1187 mParams.homeButtonWidth = 76;
1188 mParams.homeButtonHeight = 68;
1189 mParams.homeButtonTextureWidth = 128;
1190 mParams.homeButtonTextureHeight = 128;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001191
Daniel Sandler388f6792010-03-02 14:08:08 -05001192 mState.homeButtonId = mHomeButtonNormal.getID();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001193
Daniel Sandler388f6792010-03-02 14:08:08 -05001194 mParams.save();
1195 mState.save();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001196
Daniel Sandler388f6792010-03-02 14:08:08 -05001197 mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1198 Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1199 mSelectionCanvas = new Canvas(mSelectionBitmap);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001200
Daniel Sandler388f6792010-03-02 14:08:08 -05001201 setApps(null);
1202 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001203
Daniel Sandler388f6792010-03-02 14:08:08 -05001204 private void initRs() {
1205 ScriptC.Builder sb = new ScriptC.Builder(mRS);
1206 sb.setScript(mRes, R.raw.allapps);
1207 sb.setRoot(true);
Romain Guy13c2e7b2010-03-10 19:45:00 -08001208 sb.addDefines(mAllApps.mDefines);
Daniel Sandler388f6792010-03-02 14:08:08 -05001209 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1210 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
1211 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
1212 mInvokeMove = sb.addInvokable("move");
1213 mInvokeFling = sb.addInvokable("fling");
1214 mInvokeMoveTo = sb.addInvokable("moveTo");
1215 mInvokeResetWAR = sb.addInvokable("resetHWWar");
1216 mInvokeSetZoom = sb.addInvokable("setZoom");
1217 mScript = sb.create();
1218 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1219 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1220 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1221 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1222 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
1223 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001224
Daniel Sandler388f6792010-03-02 14:08:08 -05001225 mRS.contextBindRootScript(mScript);
1226 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001227
Daniel Sandler388f6792010-03-02 14:08:08 -05001228 void dirtyCheck() {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001229 if (mAllApps.mZoomDirty) {
1230 setZoom(mAllApps.mNextZoom, mAllApps.mAnimateNextZoom);
Daniel Sandler388f6792010-03-02 14:08:08 -05001231 }
1232 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001233
Romain Guy060b5c82010-03-04 10:07:38 -08001234 @SuppressWarnings({"ConstantConditions"})
Daniel Sandler388f6792010-03-02 14:08:08 -05001235 private void setApps(ArrayList<ApplicationInfo> list) {
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001236 mRollo.pause();
Daniel Sandler388f6792010-03-02 14:08:08 -05001237 final int count = list != null ? list.size() : 0;
1238 int allocCount = count;
1239 if (allocCount < 1) {
1240 allocCount = 1;
1241 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001242
Daniel Sandler388f6792010-03-02 14:08:08 -05001243 mIcons = new Allocation[count];
1244 mIconIds = new int[allocCount];
1245 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001246
Daniel Sandler388f6792010-03-02 14:08:08 -05001247 mLabels = new Allocation[count];
1248 mLabelIds = new int[allocCount];
1249 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001250
Daniel Sandler388f6792010-03-02 14:08:08 -05001251 mState.iconCount = count;
1252 for (int i=0; i < mState.iconCount; i++) {
1253 createAppIconAllocations(i, list.get(i));
1254 }
1255 for (int i=0; i < mState.iconCount; i++) {
1256 uploadAppIcon(i, list.get(i));
1257 }
1258 saveAppsList();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001259 mRollo.resume();
Daniel Sandler388f6792010-03-02 14:08:08 -05001260 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001261
Daniel Sandler388f6792010-03-02 14:08:08 -05001262 private void setZoom(float zoom, boolean animate) {
Romain Guyc16fea72010-03-12 17:17:56 -08001263 if (animate) {
1264 mRollo.clearSelectedIcon();
1265 mRollo.setHomeSelected(SELECTED_NONE);
1266 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001267 if (zoom > 0.001f) {
1268 mRollo.mState.zoomTarget = zoom;
1269 } else {
1270 mRollo.mState.zoomTarget = 0;
1271 }
1272 mRollo.mState.save();
1273 if (!animate) {
1274 mRollo.mInvokeSetZoom.execute();
1275 }
1276 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001277
Daniel Sandler388f6792010-03-02 14:08:08 -05001278 private void createAppIconAllocations(int index, ApplicationInfo item) {
1279 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001280 Element.RGBA_8888(mRS), false);
Daniel Sandler388f6792010-03-02 14:08:08 -05001281 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001282 Element.A_8(mRS), false);
Daniel Sandler388f6792010-03-02 14:08:08 -05001283 mIconIds[index] = mIcons[index].getID();
1284 mLabelIds[index] = mLabels[index].getID();
1285 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001286
Daniel Sandler388f6792010-03-02 14:08:08 -05001287 private void uploadAppIcon(int index, ApplicationInfo item) {
1288 if (mIconIds[index] != mIcons[index].getID()) {
1289 throw new IllegalStateException("uploadAppIcon index=" + index
1290 + " mIcons[index].getID=" + mIcons[index].getID()
1291 + " mIconsIds[index]=" + mIconIds[index]
1292 + " item=" + item);
1293 }
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001294 mIcons[index].uploadToTexture(true, 0);
1295 mLabels[index].uploadToTexture(true, 0);
Daniel Sandler388f6792010-03-02 14:08:08 -05001296 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001297
Daniel Sandler388f6792010-03-02 14:08:08 -05001298 /**
1299 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1300 * fill in the values and call saveAppsList().
1301 */
1302 private void reallocAppsList(int count) {
1303 Allocation[] icons = new Allocation[count];
1304 int[] iconIds = new int[count];
1305 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001306
Daniel Sandler388f6792010-03-02 14:08:08 -05001307 Allocation[] labels = new Allocation[count];
1308 int[] labelIds = new int[count];
1309 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001310
Daniel Sandler388f6792010-03-02 14:08:08 -05001311 final int oldCount = mRollo.mState.iconCount;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001312
Daniel Sandler388f6792010-03-02 14:08:08 -05001313 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1314 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1315 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1316 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001317
Daniel Sandler388f6792010-03-02 14:08:08 -05001318 mIcons = icons;
1319 mIconIds = iconIds;
1320 mLabels = labels;
1321 mLabelIds = labelIds;
1322 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001323
Daniel Sandler388f6792010-03-02 14:08:08 -05001324 /**
1325 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1326 */
1327 private void addApp(int index, ApplicationInfo item) {
1328 final int count = mState.iconCount - index;
1329 final int dest = index + 1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001330
Daniel Sandler388f6792010-03-02 14:08:08 -05001331 System.arraycopy(mIcons, index, mIcons, dest, count);
1332 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1333 System.arraycopy(mLabels, index, mLabels, dest, count);
1334 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001335
Daniel Sandler388f6792010-03-02 14:08:08 -05001336 createAppIconAllocations(index, item);
1337 uploadAppIcon(index, item);
1338 mRollo.mState.iconCount++;
1339 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001340
Daniel Sandler388f6792010-03-02 14:08:08 -05001341 /**
1342 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1343 */
1344 private void removeApp(int index) {
1345 final int count = mState.iconCount - index - 1;
1346 final int src = index + 1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001347
Daniel Sandler388f6792010-03-02 14:08:08 -05001348 System.arraycopy(mIcons, src, mIcons, index, count);
1349 System.arraycopy(mIconIds, src, mIconIds, index, count);
1350 System.arraycopy(mLabels, src, mLabels, index, count);
1351 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001352
Daniel Sandler388f6792010-03-02 14:08:08 -05001353 mRollo.mState.iconCount--;
1354 final int last = mState.iconCount;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001355
Daniel Sandler388f6792010-03-02 14:08:08 -05001356 mIcons[last] = null;
1357 mIconIds[last] = 0;
1358 mLabels[last] = null;
1359 mLabelIds[last] = 0;
1360 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001361
Daniel Sandler388f6792010-03-02 14:08:08 -05001362 /**
1363 * Send the apps list structures to RS.
1364 */
1365 private void saveAppsList() {
1366 // WTF: how could mScript be not null but mAllocIconIds null b/2460740.
1367 if (mScript != null && mAllocIconIds != null) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001368 mAllocIconIds.data(mIconIds);
1369 mAllocLabelIds.data(mLabelIds);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001370
Daniel Sandler388f6792010-03-02 14:08:08 -05001371 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1372 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001373
Daniel Sandler388f6792010-03-02 14:08:08 -05001374 mState.save();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001375
Daniel Sandler388f6792010-03-02 14:08:08 -05001376 // Note: mScript may be null if we haven't initialized it yet.
1377 // In that case, this is a no-op.
1378 if (mInvokeResetWAR != null) {
1379 mInvokeResetWAR.execute();
1380 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001381 }
1382 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001383
Romain Guy13c2e7b2010-03-10 19:45:00 -08001384 void initTouchState(int width, int height) {
1385 boolean isPortrait = width < height;
1386
Romain Guy060b5c82010-03-04 10:07:38 -08001387 // TODO: Put this in a config file/define
1388 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE_PORTRAIT;
1389 if (!isPortrait) cellHeight -= 12;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001390 int centerY = (int) (mAllApps.getHeight() * (isPortrait ? 0.5f : 0.47f));
Romain Guy060b5c82010-03-04 10:07:38 -08001391 if (!isPortrait) centerY += cellHeight / 2;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001392 int half = (int) Math.floor((mAllApps.mRowsPerPage + 1) / 2);
Romain Guy060b5c82010-03-04 10:07:38 -08001393 int end = mTouchYBorders.length - (half + 1);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001394
Romain Guy060b5c82010-03-04 10:07:38 -08001395 for (int i = -half; i <= end; i++) {
1396 mTouchYBorders[i + half] = centerY + i * cellHeight;
1397 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001398
Romain Guy060b5c82010-03-04 10:07:38 -08001399 int x = 0;
1400 // TODO: Put this in a config file/define
1401 int columnWidth = 120;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001402 for (int i = 0; i < mAllApps.mColumnsPerPage + 1; i++) {
Romain Guy060b5c82010-03-04 10:07:38 -08001403 mTouchXBorders[i] = x;
1404 x += columnWidth;
1405 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001406 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001407
Daniel Sandler388f6792010-03-02 14:08:08 -05001408 void fling() {
1409 mInvokeFling.execute();
1410 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001411
Daniel Sandler388f6792010-03-02 14:08:08 -05001412 void move() {
1413 mInvokeMove.execute();
1414 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001415
Daniel Sandler388f6792010-03-02 14:08:08 -05001416 void moveTo(float row) {
1417 mState.targetPos = row;
1418 mState.save();
1419 mInvokeMoveTo.execute();
1420 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001421
Romain Guy6a42cf32010-03-12 16:03:52 -08001422 int chooseTappedIcon(int x, int y) {
1423 float pos = mScrollPos;
1424
Daniel Sandler388f6792010-03-02 14:08:08 -05001425 // Adjust for scroll position if not zero.
1426 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001427
Daniel Sandler388f6792010-03-02 14:08:08 -05001428 int col = -1;
1429 int row = -1;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001430 final int columnsCount = mAllApps.mColumnsPerPage;
Romain Guy060b5c82010-03-04 10:07:38 -08001431 for (int i=0; i< columnsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001432 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1433 col = i;
1434 break;
1435 }
1436 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001437 final int rowsCount = mAllApps.mRowsPerPage;
Romain Guy060b5c82010-03-04 10:07:38 -08001438 for (int i=0; i< rowsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001439 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1440 row = i;
1441 break;
1442 }
1443 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001444
Daniel Sandler388f6792010-03-02 14:08:08 -05001445 if (row < 0 || col < 0) {
1446 return -1;
1447 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001448
Romain Guy6a42cf32010-03-12 16:03:52 -08001449 int index = (((int) pos) * columnsCount) + (row * columnsCount) + col;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001450
Daniel Sandler388f6792010-03-02 14:08:08 -05001451 if (index >= mState.iconCount) {
1452 return -1;
1453 } else {
1454 return index;
1455 }
1456 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001457
Daniel Sandler388f6792010-03-02 14:08:08 -05001458 /**
1459 * You need to call save() on mState on your own after calling this.
1460 *
1461 * @return the index of the icon that was selected.
1462 */
Romain Guy6a42cf32010-03-12 16:03:52 -08001463 int selectIcon(int x, int y, int pressed) {
1464 final int index = chooseTappedIcon(x, y);
Daniel Sandler388f6792010-03-02 14:08:08 -05001465 selectIcon(index, pressed);
1466 return index;
1467 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001468
Daniel Sandler388f6792010-03-02 14:08:08 -05001469 /**
1470 * Select the icon at the given index.
1471 *
1472 * @param index The index.
1473 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
1474 */
1475 void selectIcon(int index, int pressed) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001476 final ArrayList<ApplicationInfo> appsList = mAllApps.mAllAppsList;
1477 if (appsList == null || index < 0 || index >= appsList.size()) {
Romain Guyc16fea72010-03-12 17:17:56 -08001478 if (mAllApps != null) {
1479 mAllApps.mRestoreFocusIndex = index;
1480 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001481 mState.selectedIconIndex = -1;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001482 if (mAllApps.mLastSelection == SELECTION_ICONS) {
1483 mAllApps.mLastSelection = SELECTION_NONE;
Daniel Sandler388f6792010-03-02 14:08:08 -05001484 }
1485 } else {
1486 if (pressed == SELECTED_FOCUSED) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001487 mAllApps.mLastSelection = SELECTION_ICONS;
Daniel Sandler388f6792010-03-02 14:08:08 -05001488 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001489
Daniel Sandler388f6792010-03-02 14:08:08 -05001490 int prev = mState.selectedIconIndex;
1491 mState.selectedIconIndex = index;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001492
Romain Guy13c2e7b2010-03-10 19:45:00 -08001493 ApplicationInfo info = appsList.get(index);
Daniel Sandler388f6792010-03-02 14:08:08 -05001494 Bitmap selectionBitmap = mSelectionBitmap;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001495
Daniel Sandler388f6792010-03-02 14:08:08 -05001496 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1497 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1498 pressed == SELECTED_PRESSED, info.iconBitmap);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001499
Daniel Sandler388f6792010-03-02 14:08:08 -05001500 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
1501 Element.RGBA_8888(mRS), false);
1502 mSelectedIcon.uploadToTexture(0);
1503 mState.selectedIconTexture = mSelectedIcon.getID();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001504
Daniel Sandler388f6792010-03-02 14:08:08 -05001505 if (prev != index) {
1506 if (info.title != null && info.title.length() > 0) {
1507 //setContentDescription(info.title);
Romain Guy13c2e7b2010-03-10 19:45:00 -08001508 mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Daniel Sandler388f6792010-03-02 14:08:08 -05001509 }
1510 }
1511 }
1512 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001513
Daniel Sandler388f6792010-03-02 14:08:08 -05001514 /**
1515 * You need to call save() on mState on your own after calling this.
1516 */
1517 void clearSelectedIcon() {
1518 mState.selectedIconIndex = -1;
1519 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001520
Daniel Sandler388f6792010-03-02 14:08:08 -05001521 void setHomeSelected(int mode) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001522 final int prev = mAllApps.mLastSelection;
Daniel Sandler388f6792010-03-02 14:08:08 -05001523 switch (mode) {
1524 case SELECTED_NONE:
1525 mState.homeButtonId = mHomeButtonNormal.getID();
1526 break;
1527 case SELECTED_FOCUSED:
Romain Guy13c2e7b2010-03-10 19:45:00 -08001528 mAllApps.mLastSelection = SELECTION_HOME;
Daniel Sandler388f6792010-03-02 14:08:08 -05001529 mState.homeButtonId = mHomeButtonFocused.getID();
1530 if (prev != SELECTION_HOME) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001531 mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Daniel Sandler388f6792010-03-02 14:08:08 -05001532 }
1533 break;
1534 case SELECTED_PRESSED:
1535 mState.homeButtonId = mHomeButtonPressed.getID();
1536 break;
1537 }
1538 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001539
Daniel Sandler388f6792010-03-02 14:08:08 -05001540 public void dumpState() {
1541 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1542 Log.d(TAG, "mRollo.mHeight=" + mHeight);
Romain Guy060b5c82010-03-04 10:07:38 -08001543 Log.d(TAG, "mRollo.mIcons=" + Arrays.toString(mIcons));
Daniel Sandler388f6792010-03-02 14:08:08 -05001544 if (mIcons != null) {
1545 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1546 }
1547 if (mIconIds != null) {
1548 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1549 }
1550 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1551 if (mLabelIds != null) {
1552 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1553 }
1554 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1555 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1556 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
1557 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1558 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1559 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1560 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1561 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1562 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1563 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1564 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1565 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1566 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1567 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1568 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1569 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1570 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1571 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1572 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1573 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1574 }
1575 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001576
Daniel Sandler388f6792010-03-02 14:08:08 -05001577 public void dumpState() {
1578 Log.d(TAG, "mRS=" + mRS);
1579 Log.d(TAG, "mRollo=" + mRollo);
1580 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1581 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1582 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1583 Log.d(TAG, "mLastSelection=" + mLastSelection);
1584 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1585 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1586 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1587 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1588 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1589 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1590 Log.d(TAG, "mZoom=" + mZoom);
Romain Guy6a42cf32010-03-12 16:03:52 -08001591 Log.d(TAG, "mScrollPos=" + mRollo.mScrollPos);
Daniel Sandler388f6792010-03-02 14:08:08 -05001592 Log.d(TAG, "mVelocity=" + mVelocity);
1593 Log.d(TAG, "mMessageProc=" + mMessageProc);
1594 if (mRollo != null) {
1595 mRollo.dumpState();
1596 }
1597 if (mRS != null) {
1598 mRS.contextDump(0);
1599 }
1600 }
1601}
1602
1603