blob: 5f8e9f6d8f4d33e284ceb9321a83e02a4dbdd868 [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 Guy060b5c82010-03-04 10:07:38 -0800130 @SuppressWarnings({"UnusedDeclaration"})
Daniel Sandler388f6792010-03-02 14:08:08 -0500131 static class Defines {
132 public static final int ALLOC_PARAMS = 0;
133 public static final int ALLOC_STATE = 1;
134 public static final int ALLOC_ICON_IDS = 3;
135 public static final int ALLOC_LABEL_IDS = 4;
136 public static final int ALLOC_VP_CONSTANTS = 5;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800137
Romain Guy060b5c82010-03-04 10:07:38 -0800138 public static final int COLUMNS_PER_PAGE_PORTRAIT = 4;
139 public static final int ROWS_PER_PAGE_PORTRAIT = 4;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800140
Romain Guy060b5c82010-03-04 10:07:38 -0800141 public static final int COLUMNS_PER_PAGE_LANDSCAPE = 6;
142 public static final int ROWS_PER_PAGE_LANDSCAPE = 3;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800143
Daniel Sandler388f6792010-03-02 14:08:08 -0500144 public static final int ICON_WIDTH_PX = 64;
145 public static final int ICON_TEXTURE_WIDTH_PX = 74;
146 public static final int SELECTION_TEXTURE_WIDTH_PX = 74 + 20;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800147
Daniel Sandler388f6792010-03-02 14:08:08 -0500148 public static final int ICON_HEIGHT_PX = 64;
149 public static final int ICON_TEXTURE_HEIGHT_PX = 74;
150 public static final int SELECTION_TEXTURE_HEIGHT_PX = 74 + 20;
151 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800152
Daniel Sandler388f6792010-03-02 14:08:08 -0500153 public AllApps3D(Context context, AttributeSet attrs) {
154 super(context, attrs);
155 setFocusable(true);
156 setSoundEffectsEnabled(false);
157 getHolder().setFormat(PixelFormat.TRANSLUCENT);
158 final ViewConfiguration config = ViewConfiguration.get(context);
159 mSlop = config.getScaledTouchSlop();
160 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800161
Daniel Sandler388f6792010-03-02 14:08:08 -0500162 setOnClickListener(this);
163 setOnLongClickListener(this);
164 setZOrderOnTop(true);
165 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800166
Romain Guy13c2e7b2010-03-10 19:45:00 -0800167 if (mRS == null) {
168 mRS = createRenderScript(true);
169 } else {
170 createRenderScript(mRS);
171 }
172
Romain Guy060b5c82010-03-04 10:07:38 -0800173 final DisplayMetrics metrics = getResources().getDisplayMetrics();
174 final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;
175 mColumnsPerPage = isPortrait ? Defines.COLUMNS_PER_PAGE_PORTRAIT :
176 Defines.COLUMNS_PER_PAGE_LANDSCAPE;
177 mRowsPerPage = isPortrait ? Defines.ROWS_PER_PAGE_PORTRAIT :
178 Defines.ROWS_PER_PAGE_LANDSCAPE;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800179
180 if (mRollo != null) {
181 mRollo.mAllApps = this;
182 mRollo.mRes = getResources();
183 mRollo.mInitialize = true;
184 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500185 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800186
Romain Guy060b5c82010-03-04 10:07:38 -0800187 @SuppressWarnings({"UnusedDeclaration"})
188 public AllApps3D(Context context, AttributeSet attrs, int defStyle) {
189 this(context, attrs);
190 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800191
Romain Guy13c2e7b2010-03-10 19:45:00 -0800192 public void surrender() {
193 mRS.contextSetSurface(0, 0, null);
194 mRS.mMessageCallback = null;
195 mSurrendered = true;
196 }
197
Daniel Sandler388f6792010-03-02 14:08:08 -0500198 /**
199 * Note that this implementation prohibits this view from ever being reattached.
200 */
201 @Override
202 protected void onDetachedFromWindow() {
Daniel Sandler388f6792010-03-02 14:08:08 -0500203 mRS.mMessageCallback = null;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800204 if (!mSurrendered) {
205 destroyRenderScript();
206 mRS = null;
207 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500208 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800209
Daniel Sandler388f6792010-03-02 14:08:08 -0500210 /**
211 * If you have an attached click listener, View always plays the click sound!?!?
212 * Deal with sound effects by hand.
213 */
214 public void reallyPlaySoundEffect(int sound) {
215 boolean old = isSoundEffectsEnabled();
216 setSoundEffectsEnabled(true);
217 playSoundEffect(sound);
218 setSoundEffectsEnabled(old);
219 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800220
Daniel Sandler388f6792010-03-02 14:08:08 -0500221 public void setLauncher(Launcher launcher) {
222 mLauncher = launcher;
223 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800224
Daniel Sandler388f6792010-03-02 14:08:08 -0500225 @Override
226 public void surfaceDestroyed(SurfaceHolder holder) {
227 super.surfaceDestroyed(holder);
228 // Without this, we leak mMessageCallback which leaks the context.
Romain Guy13c2e7b2010-03-10 19:45:00 -0800229 if (!mSurrendered) {
230 mRS.mMessageCallback = null;
231 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500232 // We may lose any callbacks that are pending, so make sure that we re-sync that
233 // on the next surfaceChanged.
234 mZoomDirty = true;
235 mHaveSurface = false;
236 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800237
Daniel Sandler388f6792010-03-02 14:08:08 -0500238 @Override
239 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
240 //long startTime = SystemClock.uptimeMillis();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800241
Daniel Sandler388f6792010-03-02 14:08:08 -0500242 super.surfaceChanged(holder, format, w, h);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800243
Romain Guy13c2e7b2010-03-10 19:45:00 -0800244 if (mSurrendered) return;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800245
Daniel Sandler388f6792010-03-02 14:08:08 -0500246 mHaveSurface = true;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800247
Daniel Sandler388f6792010-03-02 14:08:08 -0500248 if (mRollo == null) {
Romain Guy13c2e7b2010-03-10 19:45:00 -0800249 mRollo = new RolloRS(this);
Daniel Sandler388f6792010-03-02 14:08:08 -0500250 mRollo.init(getResources(), w, h);
251 if (mAllAppsList != null) {
252 mRollo.setApps(mAllAppsList);
253 }
254 if (mShouldGainFocus) {
255 gainFocus();
256 mShouldGainFocus = false;
257 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800258 } else if (mRollo.mInitialize) {
259 mRollo.initGl();
260 mRollo.initTouchState(w, h);
261 mRollo.mInitialize = false;
Daniel Sandler388f6792010-03-02 14:08:08 -0500262 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800263
Daniel Sandler388f6792010-03-02 14:08:08 -0500264 mRollo.dirtyCheck();
265 mRollo.resize(w, h);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800266
Daniel Sandler388f6792010-03-02 14:08:08 -0500267 if (mRS != null) {
268 mRS.mMessageCallback = mMessageProc = new AAMessage();
269 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800270
Daniel Sandler388f6792010-03-02 14:08:08 -0500271 if (mRollo.mUniformAlloc != null) {
272 float tf[] = new float[] {72.f, 72.f,
273 120.f, 120.f, 0.f, 0.f,
274 120.f, 680.f,
275 (2.f / 480.f), 0, -((float)w / 2) - 0.25f, -380.25f};
276 if (w > h) {
277 tf[6] = 40.f;
278 tf[7] = h - 40.f;
279 tf[9] = 1.f;
280 tf[10] = -((float)w / 2) - 0.25f;
281 tf[11] = -((float)h / 2) - 0.25f;
282 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800283
Daniel Sandler388f6792010-03-02 14:08:08 -0500284 mRollo.mUniformAlloc.data(tf);
285 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800286
Daniel Sandler388f6792010-03-02 14:08:08 -0500287 //long endTime = SystemClock.uptimeMillis();
288 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
289 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800290
Daniel Sandler388f6792010-03-02 14:08:08 -0500291 @Override
292 public void onWindowFocusChanged(boolean hasWindowFocus) {
293 super.onWindowFocusChanged(hasWindowFocus);
Romain Guy13c2e7b2010-03-10 19:45:00 -0800294
295 if (mSurrendered) return;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800296
Daniel Sandler388f6792010-03-02 14:08:08 -0500297 if (mArrowNavigation) {
298 if (!hasWindowFocus) {
299 // Clear selection when we lose window focus
300 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
301 mRollo.setHomeSelected(SELECTED_NONE);
302 mRollo.clearSelectedIcon();
303 mRollo.mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -0800304 } else {
Daniel Sandler388f6792010-03-02 14:08:08 -0500305 if (mRollo.mState.iconCount > 0) {
306 if (mLastSelection == SELECTION_ICONS) {
307 int selection = mLastSelectedIcon;
Romain Guy6a42cf32010-03-12 16:03:52 -0800308 final int firstIcon = Math.round(mRollo.mScrollPos) * mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500309 if (selection < 0 || // No selection
310 selection < firstIcon || // off the top of the screen
311 selection >= mRollo.mState.iconCount || // past last icon
312 selection >= firstIcon + // past last icon on screen
Romain Guy060b5c82010-03-04 10:07:38 -0800313 (mColumnsPerPage * mRowsPerPage)) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500314 selection = firstIcon;
315 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800316
Daniel Sandler388f6792010-03-02 14:08:08 -0500317 // Select the first icon when we gain window focus
318 mRollo.selectIcon(selection, SELECTED_FOCUSED);
319 mRollo.mState.save();
320 } else if (mLastSelection == SELECTION_HOME) {
321 mRollo.setHomeSelected(SELECTED_FOCUSED);
322 mRollo.mState.save();
323 }
324 }
325 }
326 }
327 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800328
Daniel Sandler388f6792010-03-02 14:08:08 -0500329 @Override
330 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
331 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800332
Romain Guy13c2e7b2010-03-10 19:45:00 -0800333 if (!isVisible() || mSurrendered) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500334 return;
335 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800336
Daniel Sandler388f6792010-03-02 14:08:08 -0500337 if (gainFocus) {
338 if (mRollo != null) {
339 gainFocus();
340 } else {
341 mShouldGainFocus = true;
342 }
343 } else {
344 if (mRollo != null) {
345 if (mArrowNavigation) {
346 // Clear selection when we lose focus
347 mRollo.clearSelectedIcon();
348 mRollo.setHomeSelected(SELECTED_NONE);
349 mRollo.mState.save();
350 mArrowNavigation = false;
351 }
352 } else {
353 mShouldGainFocus = false;
354 }
355 }
356 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800357
Daniel Sandler388f6792010-03-02 14:08:08 -0500358 private void gainFocus() {
359 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
360 // Select the first icon when we gain keyboard focus
361 mArrowNavigation = true;
Romain Guy6a42cf32010-03-12 16:03:52 -0800362 mRollo.selectIcon(Math.round(mRollo.mScrollPos) * mColumnsPerPage, SELECTED_FOCUSED);
Daniel Sandler388f6792010-03-02 14:08:08 -0500363 mRollo.mState.save();
364 }
365 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800366
Daniel Sandler388f6792010-03-02 14:08:08 -0500367 @Override
368 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800369
Daniel Sandler388f6792010-03-02 14:08:08 -0500370 boolean handled = false;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800371
Daniel Sandler388f6792010-03-02 14:08:08 -0500372 if (!isVisible()) {
373 return false;
374 }
375 final int iconCount = mRollo.mState.iconCount;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800376
Daniel Sandler388f6792010-03-02 14:08:08 -0500377 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
378 if (mArrowNavigation) {
379 if (mLastSelection == SELECTION_HOME) {
380 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
381 mLauncher.closeAllApps(true);
382 } else {
383 int whichApp = mRollo.mState.selectedIconIndex;
384 if (whichApp >= 0) {
385 ApplicationInfo app = mAllAppsList.get(whichApp);
386 mLauncher.startActivitySafely(app.intent);
387 handled = true;
388 }
389 }
390 }
391 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800392
Daniel Sandler388f6792010-03-02 14:08:08 -0500393 if (iconCount > 0) {
Romain Guy6a42cf32010-03-12 16:03:52 -0800394 final boolean isPortrait = getWidth() < getHeight();
395
Daniel Sandler388f6792010-03-02 14:08:08 -0500396 mArrowNavigation = true;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800397
Daniel Sandler388f6792010-03-02 14:08:08 -0500398 int currentSelection = mRollo.mState.selectedIconIndex;
Romain Guy6a42cf32010-03-12 16:03:52 -0800399 int currentTopRow = Math.round(mRollo.mScrollPos);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800400
Romain Guy060b5c82010-03-04 10:07:38 -0800401 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE_PORTRAIT-1
402 final int currentPageCol = currentSelection % mColumnsPerPage;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800403
Romain Guy060b5c82010-03-04 10:07:38 -0800404 // The row of the current selection, in the range 0..ROWS_PER_PAGE_PORTRAIT-1
Romain Guy6a42cf32010-03-12 16:03:52 -0800405 final int currentPageRow = (currentSelection - (currentTopRow * mColumnsPerPage))
Romain Guy060b5c82010-03-04 10:07:38 -0800406 / mRowsPerPage;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800407
Daniel Sandler388f6792010-03-02 14:08:08 -0500408 int newSelection = currentSelection;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800409
Daniel Sandler388f6792010-03-02 14:08:08 -0500410 switch (keyCode) {
411 case KeyEvent.KEYCODE_DPAD_UP:
412 if (mLastSelection == SELECTION_HOME) {
Romain Guy6a42cf32010-03-12 16:03:52 -0800413 if (isPortrait) {
414 mRollo.setHomeSelected(SELECTED_NONE);
415 int lastRowCount = iconCount % mColumnsPerPage;
416 if (lastRowCount == 0) {
417 lastRowCount = mColumnsPerPage;
418 }
419 newSelection = iconCount - lastRowCount + (mColumnsPerPage / 2);
420 if (newSelection >= iconCount) {
421 newSelection = iconCount-1;
422 }
423 int target = (newSelection / mColumnsPerPage) - (mRowsPerPage - 1);
424 if (target < 0) {
425 target = 0;
426 }
427 if (currentTopRow != target) {
428 mRollo.moveTo(target);
429 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500430 }
431 } else {
432 if (currentPageRow > 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800433 newSelection = currentSelection - mColumnsPerPage;
Romain Guy6a42cf32010-03-12 16:03:52 -0800434 if (currentTopRow > newSelection / mColumnsPerPage) {
435 mRollo.moveTo(newSelection / mColumnsPerPage);
436 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500437 } else if (currentTopRow > 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800438 newSelection = currentSelection - mColumnsPerPage;
439 mRollo.moveTo(newSelection / mColumnsPerPage);
Daniel Sandler388f6792010-03-02 14:08:08 -0500440 } else if (currentPageRow != 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800441 newSelection = currentTopRow * mRowsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500442 }
443 }
444 handled = true;
445 break;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800446
Daniel Sandler388f6792010-03-02 14:08:08 -0500447 case KeyEvent.KEYCODE_DPAD_DOWN: {
Romain Guy060b5c82010-03-04 10:07:38 -0800448 final int rowCount = iconCount / mColumnsPerPage
449 + (iconCount % mColumnsPerPage == 0 ? 0 : 1);
450 final int currentRow = currentSelection / mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500451 if (mLastSelection != SELECTION_HOME) {
452 if (currentRow < rowCount-1) {
453 mRollo.setHomeSelected(SELECTED_NONE);
454 if (currentSelection < 0) {
455 newSelection = 0;
456 } else {
Romain Guy060b5c82010-03-04 10:07:38 -0800457 newSelection = currentSelection + mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500458 }
459 if (newSelection >= iconCount) {
460 // Go from D to G in this arrangement:
461 // A B C D
462 // E F G
463 newSelection = iconCount - 1;
464 }
Romain Guy060b5c82010-03-04 10:07:38 -0800465 if (currentPageRow >= mRowsPerPage - 1) {
Romain Guy6a42cf32010-03-12 16:03:52 -0800466 mRollo.moveTo((newSelection / mColumnsPerPage) - mRowsPerPage + 1);
Daniel Sandler388f6792010-03-02 14:08:08 -0500467 }
Romain Guy6a42cf32010-03-12 16:03:52 -0800468 } else if (isPortrait) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500469 newSelection = -1;
470 mRollo.setHomeSelected(SELECTED_FOCUSED);
471 }
472 }
473 handled = true;
474 break;
475 }
476 case KeyEvent.KEYCODE_DPAD_LEFT:
477 if (mLastSelection != SELECTION_HOME) {
478 if (currentPageCol > 0) {
479 newSelection = currentSelection - 1;
480 }
Romain Guy6a42cf32010-03-12 16:03:52 -0800481 } else if (!isPortrait) {
482 newSelection = ((int) (mRollo.mScrollPos) * mColumnsPerPage) +
483 (mRowsPerPage / 2 * mColumnsPerPage) + mColumnsPerPage - 1;
484 mRollo.setHomeSelected(SELECTED_NONE);
Daniel Sandler388f6792010-03-02 14:08:08 -0500485 }
486 handled = true;
487 break;
488 case KeyEvent.KEYCODE_DPAD_RIGHT:
489 if (mLastSelection != SELECTION_HOME) {
Romain Guy6a42cf32010-03-12 16:03:52 -0800490 if (!isPortrait && (currentPageCol == mColumnsPerPage - 1 ||
491 currentSelection == iconCount - 1)) {
492 newSelection = -1;
493 mRollo.setHomeSelected(SELECTED_FOCUSED);
494 } else if ((currentPageCol < mColumnsPerPage - 1) &&
Daniel Sandler388f6792010-03-02 14:08:08 -0500495 (currentSelection < iconCount - 1)) {
496 newSelection = currentSelection + 1;
497 }
498 }
499 handled = true;
500 break;
501 }
502 if (newSelection != currentSelection) {
503 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
504 mRollo.mState.save();
505 }
506 }
507 return handled;
508 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800509
Daniel Sandler388f6792010-03-02 14:08:08 -0500510 @Override
511 public boolean onTouchEvent(MotionEvent ev)
512 {
513 mArrowNavigation = false;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800514
Daniel Sandler388f6792010-03-02 14:08:08 -0500515 if (!isVisible()) {
516 return true;
517 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800518
Daniel Sandler388f6792010-03-02 14:08:08 -0500519 if (mLocks != 0) {
520 return true;
521 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800522
Daniel Sandler388f6792010-03-02 14:08:08 -0500523 super.onTouchEvent(ev);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800524
Daniel Sandler388f6792010-03-02 14:08:08 -0500525 int x = (int)ev.getX();
526 int y = (int)ev.getY();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800527
Romain Guyce115852010-03-04 12:15:37 -0800528 final boolean isPortrait = getWidth() < getHeight();
Daniel Sandler388f6792010-03-02 14:08:08 -0500529 int action = ev.getAction();
530 switch (action) {
531 case MotionEvent.ACTION_DOWN:
Romain Guyce115852010-03-04 12:15:37 -0800532 if ((isPortrait && y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) ||
533 (!isPortrait && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500534 mTouchTracking = TRACKING_HOME;
535 mRollo.setHomeSelected(SELECTED_PRESSED);
536 mRollo.mState.save();
537 mCurrentIconIndex = -1;
538 } else {
539 mTouchTracking = TRACKING_FLING;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800540
Daniel Sandler388f6792010-03-02 14:08:08 -0500541 mMotionDownRawX = (int)ev.getRawX();
542 mMotionDownRawY = (int)ev.getRawY();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800543
Daniel Sandler388f6792010-03-02 14:08:08 -0500544 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
545 mRollo.mState.newTouchDown = 1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800546
Daniel Sandler388f6792010-03-02 14:08:08 -0500547 if (!mRollo.checkClickOK()) {
548 mRollo.clearSelectedIcon();
549 } else {
550 mDownIconIndex = mCurrentIconIndex
Romain Guy6a42cf32010-03-12 16:03:52 -0800551 = mRollo.selectIcon(x, y, SELECTED_PRESSED);
Daniel Sandler388f6792010-03-02 14:08:08 -0500552 if (mDownIconIndex < 0) {
553 // if nothing was selected, no long press.
554 cancelLongPress();
555 }
556 }
557 mRollo.mState.save();
558 mRollo.move();
559 mVelocityTracker = VelocityTracker.obtain();
560 mVelocityTracker.addMovement(ev);
561 mStartedScrolling = false;
562 }
563 break;
564 case MotionEvent.ACTION_MOVE:
565 case MotionEvent.ACTION_OUTSIDE:
566 if (mTouchTracking == TRACKING_HOME) {
Romain Guyce115852010-03-04 12:15:37 -0800567 mRollo.setHomeSelected((isPortrait &&
568 y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) || (!isPortrait
569 && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])
Daniel Sandler388f6792010-03-02 14:08:08 -0500570 ? SELECTED_PRESSED : SELECTED_NONE);
571 mRollo.mState.save();
572 } else if (mTouchTracking == TRACKING_FLING) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500573 int rawY = (int)ev.getRawY();
574 int slop;
575 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800576
Daniel Sandler388f6792010-03-02 14:08:08 -0500577 if (!mStartedScrolling && slop < mSlop) {
578 // don't update anything so when we do start scrolling
579 // below, we get the right delta.
Romain Guy6a42cf32010-03-12 16:03:52 -0800580 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y);
Daniel Sandler388f6792010-03-02 14:08:08 -0500581 if (mDownIconIndex != mCurrentIconIndex) {
582 // If a different icon is selected, don't allow it to be picked up.
583 // This handles off-axis dragging.
584 cancelLongPress();
585 mCurrentIconIndex = -1;
586 }
587 } else {
588 if (!mStartedScrolling) {
589 cancelLongPress();
590 mCurrentIconIndex = -1;
591 }
592 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
593 mRollo.mState.newTouchDown = 1;
594 mRollo.move();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800595
Daniel Sandler388f6792010-03-02 14:08:08 -0500596 mStartedScrolling = true;
597 mRollo.clearSelectedIcon();
598 mVelocityTracker.addMovement(ev);
599 mRollo.mState.save();
600 }
601 }
602 break;
603 case MotionEvent.ACTION_UP:
604 case MotionEvent.ACTION_CANCEL:
605 if (mTouchTracking == TRACKING_HOME) {
606 if (action == MotionEvent.ACTION_UP) {
Romain Guyce115852010-03-04 12:15:37 -0800607 if ((isPortrait && y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) ||
608 (!isPortrait && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500609 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
610 mLauncher.closeAllApps(true);
611 }
612 mRollo.setHomeSelected(SELECTED_NONE);
613 mRollo.mState.save();
614 }
615 mCurrentIconIndex = -1;
616 } else if (mTouchTracking == TRACKING_FLING) {
617 mRollo.mState.newTouchDown = 0;
618 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800619
Daniel Sandler388f6792010-03-02 14:08:08 -0500620 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
621 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
622 mRollo.clearSelectedIcon();
623 mRollo.mState.save();
624 mRollo.fling();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800625
Daniel Sandler388f6792010-03-02 14:08:08 -0500626 if (mVelocityTracker != null) {
627 mVelocityTracker.recycle();
628 mVelocityTracker = null;
629 }
630 }
631 mTouchTracking = TRACKING_NONE;
632 break;
633 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800634
Daniel Sandler388f6792010-03-02 14:08:08 -0500635 return true;
636 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800637
Daniel Sandler388f6792010-03-02 14:08:08 -0500638 public void onClick(View v) {
639 if (mLocks != 0 || !isVisible()) {
640 return;
641 }
642 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
643 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
644 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
645 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
646 mLauncher.startActivitySafely(app.intent);
647 }
648 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800649
Daniel Sandler388f6792010-03-02 14:08:08 -0500650 public boolean onLongClick(View v) {
651 if (mLocks != 0 || !isVisible()) {
652 return true;
653 }
654 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
655 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
656 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800657
Daniel Sandler388f6792010-03-02 14:08:08 -0500658 Bitmap bmp = app.iconBitmap;
659 final int w = bmp.getWidth();
660 final int h = bmp.getHeight();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800661
Daniel Sandler388f6792010-03-02 14:08:08 -0500662 // We don't really have an accurate location to use. This will do.
663 int screenX = mMotionDownRawX - (w / 2);
664 int screenY = mMotionDownRawY - h;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800665
Daniel Sandler388f6792010-03-02 14:08:08 -0500666 mDragController.startDrag(bmp, screenX, screenY,
667 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800668
Daniel Sandler388f6792010-03-02 14:08:08 -0500669 mLauncher.closeAllApps(true);
670 }
671 return true;
672 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800673
Daniel Sandler388f6792010-03-02 14:08:08 -0500674 @Override
675 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
676 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
677 if (!isVisible()) {
678 return false;
679 }
680 String text = null;
681 int index;
682 int count = mAllAppsList.size() + 1; // +1 is home
683 int pos = -1;
684 switch (mLastSelection) {
685 case SELECTION_ICONS:
686 index = mRollo.mState.selectedIconIndex;
687 if (index >= 0) {
688 ApplicationInfo info = mAllAppsList.get(index);
689 if (info.title != null) {
690 text = info.title.toString();
691 pos = index;
692 }
693 }
694 break;
695 case SELECTION_HOME:
696 text = getContext().getString(R.string.all_apps_home_button_label);
697 pos = count;
698 break;
699 }
700 if (text != null) {
701 event.setEnabled(true);
702 event.getText().add(text);
703 //event.setContentDescription(text);
704 event.setItemCount(count);
705 event.setCurrentItemIndex(pos);
706 }
707 }
708 return false;
709 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800710
Daniel Sandler388f6792010-03-02 14:08:08 -0500711 public void setDragController(DragController dragger) {
712 mDragController = dragger;
713 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800714
Daniel Sandler388f6792010-03-02 14:08:08 -0500715 public void onDropCompleted(View target, boolean success) {
716 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800717
Daniel Sandler388f6792010-03-02 14:08:08 -0500718 /**
719 * Zoom to the specifed level.
720 *
721 * @param zoom [0..1] 0 is hidden, 1 is open
722 */
723 public void zoom(float zoom, boolean animate) {
724 cancelLongPress();
725 mNextZoom = zoom;
726 mAnimateNextZoom = animate;
727 // if we do setZoom while we don't have a surface, we won't
728 // get the callbacks that actually set mZoom.
729 if (mRollo == null || !mHaveSurface) {
730 mZoomDirty = true;
731 mZoom = zoom;
Daniel Sandler388f6792010-03-02 14:08:08 -0500732 } else {
733 mRollo.setZoom(zoom, animate);
734 }
735 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800736
Daniel Sandler388f6792010-03-02 14:08:08 -0500737 public boolean isVisible() {
738 return mZoom > 0.001f;
739 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800740
Daniel Sandler388f6792010-03-02 14:08:08 -0500741 public boolean isOpaque() {
742 return mZoom > 0.999f;
743 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800744
Daniel Sandler388f6792010-03-02 14:08:08 -0500745 public void setApps(ArrayList<ApplicationInfo> list) {
746 if (mRS == null) {
747 // We've been removed from the window. Don't bother with all this.
748 return;
749 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800750
751 boolean reload = false;
752 if (mAllAppsList == null) {
753 reload = true;
754 } else if (list.size() != mAllAppsList.size()) {
755 reload = true;
756 } else {
757 final int count = list.size();
758 for (int i = 0; i < count; i++) {
759 if (list.get(i) != mAllAppsList.get(i)) {
760 reload = true;
761 break;
762 }
763 }
764 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800765
Daniel Sandler388f6792010-03-02 14:08:08 -0500766 mAllAppsList = list;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800767 if (mRollo != null && reload) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500768 mRollo.setApps(list);
769 }
770 mLocks &= ~LOCK_ICONS_PENDING;
771 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800772
Daniel Sandler388f6792010-03-02 14:08:08 -0500773 public void addApps(ArrayList<ApplicationInfo> list) {
774 if (mAllAppsList == null) {
775 // Not done loading yet. We'll find out about it later.
776 return;
777 }
778 if (mRS == null) {
779 // We've been removed from the window. Don't bother with all this.
780 return;
781 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800782
Daniel Sandler388f6792010-03-02 14:08:08 -0500783 final int N = list.size();
784 if (mRollo != null) {
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800785 mRollo.pause();
Daniel Sandler388f6792010-03-02 14:08:08 -0500786 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
787 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800788
Daniel Sandler388f6792010-03-02 14:08:08 -0500789 for (int i=0; i<N; i++) {
790 final ApplicationInfo item = list.get(i);
791 int index = Collections.binarySearch(mAllAppsList, item,
792 LauncherModel.APP_NAME_COMPARATOR);
793 if (index < 0) {
794 index = -(index+1);
795 }
796 mAllAppsList.add(index, item);
797 if (mRollo != null) {
798 mRollo.addApp(index, item);
799 }
800 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800801
Daniel Sandler388f6792010-03-02 14:08:08 -0500802 if (mRollo != null) {
803 mRollo.saveAppsList();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800804 mRollo.resume();
Daniel Sandler388f6792010-03-02 14:08:08 -0500805 }
806 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800807
Daniel Sandler388f6792010-03-02 14:08:08 -0500808 public void removeApps(ArrayList<ApplicationInfo> list) {
809 if (mAllAppsList == null) {
810 // Not done loading yet. We'll find out about it later.
811 return;
812 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800813
814 if (mRollo != null) {
815 mRollo.pause();
816 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500817 final int N = list.size();
818 for (int i=0; i<N; i++) {
819 final ApplicationInfo item = list.get(i);
820 int index = findAppByComponent(mAllAppsList, item);
821 if (index >= 0) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500822 mAllAppsList.remove(index);
823 if (mRollo != null) {
824 mRollo.removeApp(index);
825 }
826 } else {
827 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
828 // Try to recover. This should keep us from crashing for now.
829 }
830 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800831
Daniel Sandler388f6792010-03-02 14:08:08 -0500832 if (mRollo != null) {
833 mRollo.saveAppsList();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800834 mRollo.resume();
Daniel Sandler388f6792010-03-02 14:08:08 -0500835 }
836 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800837
Joe Onorato64e6be72010-03-05 15:05:52 -0500838 public void updateApps(ArrayList<ApplicationInfo> list) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500839 // Just remove and add, because they may need to be re-sorted.
840 removeApps(list);
841 addApps(list);
842 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800843
Daniel Sandler388f6792010-03-02 14:08:08 -0500844 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
845 ComponentName component = item.intent.getComponent();
846 final int N = list.size();
847 for (int i=0; i<N; i++) {
848 ApplicationInfo x = list.get(i);
849 if (x.intent.getComponent().equals(component)) {
850 return i;
851 }
852 }
853 return -1;
854 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800855
Romain Guy060b5c82010-03-04 10:07:38 -0800856 /*
Daniel Sandler388f6792010-03-02 14:08:08 -0500857 private static int countPages(int iconCount) {
Romain Guy060b5c82010-03-04 10:07:38 -0800858 int iconsPerPage = getColumnsCount() * Defines.ROWS_PER_PAGE_PORTRAIT;
Daniel Sandler388f6792010-03-02 14:08:08 -0500859 int pages = iconCount / iconsPerPage;
860 if (pages*iconsPerPage != iconCount) {
861 pages++;
862 }
863 return pages;
864 }
Romain Guy060b5c82010-03-04 10:07:38 -0800865 */
Daniel Sandler388f6792010-03-02 14:08:08 -0500866
867 class AAMessage extends RenderScript.RSMessage {
868 public void run() {
Romain Guy6a42cf32010-03-12 16:03:52 -0800869 mRollo.mScrollPos = ((float)mData[0]) / (1 << 16);
Daniel Sandler388f6792010-03-02 14:08:08 -0500870 mVelocity = ((float)mData[1]) / (1 << 16);
871 mZoom = ((float)mData[2]) / (1 << 16);
872 mZoomDirty = false;
873 }
874 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800875
Romain Guy13c2e7b2010-03-10 19:45:00 -0800876 public static class RolloRS {
Daniel Sandler388f6792010-03-02 14:08:08 -0500877 // Allocations ======
878 private int mWidth;
879 private int mHeight;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800880
Daniel Sandler388f6792010-03-02 14:08:08 -0500881 private Resources mRes;
882 private Script mScript;
883 private Script.Invokable mInvokeMove;
884 private Script.Invokable mInvokeMoveTo;
885 private Script.Invokable mInvokeFling;
886 private Script.Invokable mInvokeResetWAR;
887 private Script.Invokable mInvokeSetZoom;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800888
Daniel Sandler388f6792010-03-02 14:08:08 -0500889 private ProgramStore mPSIcons;
890 private ProgramFragment mPFTexMip;
891 private ProgramFragment mPFTexMipAlpha;
892 private ProgramFragment mPFTexNearest;
893 private ProgramVertex mPV;
894 private ProgramVertex mPVCurve;
895 private SimpleMesh mMesh;
896 private ProgramVertex.MatrixAllocation mPVA;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800897
Daniel Sandler388f6792010-03-02 14:08:08 -0500898 private Allocation mUniformAlloc;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800899
Daniel Sandler388f6792010-03-02 14:08:08 -0500900 private Allocation mHomeButtonNormal;
901 private Allocation mHomeButtonFocused;
902 private Allocation mHomeButtonPressed;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800903
Daniel Sandler388f6792010-03-02 14:08:08 -0500904 private Allocation[] mIcons;
905 private int[] mIconIds;
906 private Allocation mAllocIconIds;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800907
Daniel Sandler388f6792010-03-02 14:08:08 -0500908 private Allocation[] mLabels;
909 private int[] mLabelIds;
910 private Allocation mAllocLabelIds;
911 private Allocation mSelectedIcon;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800912
Daniel Sandler388f6792010-03-02 14:08:08 -0500913 private int[] mTouchYBorders;
914 private int[] mTouchXBorders;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800915
Daniel Sandler388f6792010-03-02 14:08:08 -0500916 private Bitmap mSelectionBitmap;
917 private Canvas mSelectionCanvas;
Romain Guy6a42cf32010-03-12 16:03:52 -0800918
919 private float mScrollPos;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800920
Daniel Sandler388f6792010-03-02 14:08:08 -0500921 Params mParams;
922 State mState;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800923
Romain Guy13c2e7b2010-03-10 19:45:00 -0800924 AllApps3D mAllApps;
925 boolean mInitialize;
926
Daniel Sandler388f6792010-03-02 14:08:08 -0500927 class BaseAlloc {
928 Allocation mAlloc;
929 Type mType;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800930
Daniel Sandler388f6792010-03-02 14:08:08 -0500931 void save() {
932 mAlloc.data(this);
933 }
934 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800935
Daniel Sandler388f6792010-03-02 14:08:08 -0500936 private boolean checkClickOK() {
Romain Guy13c2e7b2010-03-10 19:45:00 -0800937 return (Math.abs(mAllApps.mVelocity) < 0.4f) &&
Romain Guy6a42cf32010-03-12 16:03:52 -0800938 (Math.abs(mScrollPos - Math.round(mScrollPos)) < 0.4f);
Daniel Sandler388f6792010-03-02 14:08:08 -0500939 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800940
941 void pause() {
942 mRS.contextBindRootScript(null);
943 }
944
945 void resume() {
946 mRS.contextBindRootScript(mScript);
947 }
948
Daniel Sandler388f6792010-03-02 14:08:08 -0500949 class Params extends BaseAlloc {
950 Params() {
951 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
952 mAlloc = Allocation.createTyped(mRS, mType);
953 save();
954 }
955 public int bubbleWidth;
956 public int bubbleHeight;
957 public int bubbleBitmapWidth;
958 public int bubbleBitmapHeight;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800959
Daniel Sandler388f6792010-03-02 14:08:08 -0500960 public int homeButtonWidth;
961 public int homeButtonHeight;
962 public int homeButtonTextureWidth;
963 public int homeButtonTextureHeight;
964 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800965
Daniel Sandler388f6792010-03-02 14:08:08 -0500966 class State extends BaseAlloc {
967 public float newPositionX;
968 public int newTouchDown;
969 public float flingVelocity;
970 public int iconCount;
971 public int selectedIconIndex = -1;
972 public int selectedIconTexture;
973 public float zoomTarget;
974 public int homeButtonId;
975 public float targetPos;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800976
Daniel Sandler388f6792010-03-02 14:08:08 -0500977 State() {
978 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
979 mAlloc = Allocation.createTyped(mRS, mType);
980 save();
981 }
982 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800983
Romain Guy13c2e7b2010-03-10 19:45:00 -0800984 public RolloRS(AllApps3D allApps) {
985 mAllApps = allApps;
Daniel Sandler388f6792010-03-02 14:08:08 -0500986 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800987
Daniel Sandler388f6792010-03-02 14:08:08 -0500988 public void init(Resources res, int width, int height) {
989 mRes = res;
990 mWidth = width;
991 mHeight = height;
992 initProgramVertex();
993 initProgramFragment();
994 initProgramStore();
995 initGl();
996 initData();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800997 initTouchState(width, height);
Daniel Sandler388f6792010-03-02 14:08:08 -0500998 initRs();
999 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001000
Daniel Sandler388f6792010-03-02 14:08:08 -05001001 public void initMesh() {
1002 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001003
Daniel Sandler388f6792010-03-02 14:08:08 -05001004 for (int ct=0; ct < 16; ct++) {
1005 float pos = (1.f / (16.f - 1)) * ct;
1006 tm.addVertex(0.0f, pos);
1007 tm.addVertex(1.0f, pos);
1008 }
1009 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
1010 tm.addTriangle(ct, ct+1, ct+2);
1011 tm.addTriangle(ct+1, ct+3, ct+2);
1012 }
1013 mMesh = tm.create();
1014 mMesh.setName("SMCell");
1015 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001016
Daniel Sandler388f6792010-03-02 14:08:08 -05001017 void resize(int w, int h) {
1018 mPVA.setupProjectionNormalized(w, h);
1019 mWidth = w;
1020 mHeight = h;
1021 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001022
Daniel Sandler388f6792010-03-02 14:08:08 -05001023 private void initProgramVertex() {
1024 mPVA = new ProgramVertex.MatrixAllocation(mRS);
1025 resize(mWidth, mHeight);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001026
Daniel Sandler388f6792010-03-02 14:08:08 -05001027 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
1028 pvb.setTextureMatrixEnable(true);
1029 mPV = pvb.create();
1030 mPV.setName("PV");
1031 mPV.bindAllocation(mPVA);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001032
Daniel Sandler388f6792010-03-02 14:08:08 -05001033 Element.Builder eb = new Element.Builder(mRS);
1034 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
1035 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
1036 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "BendPos");
1037 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
1038 Element e = eb.create();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001039
Daniel Sandler388f6792010-03-02 14:08:08 -05001040 mUniformAlloc = Allocation.createSized(mRS, e, 1);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001041
Daniel Sandler388f6792010-03-02 14:08:08 -05001042 initMesh();
1043 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
Romain Guy060b5c82010-03-04 10:07:38 -08001044 String t = "void main() {\n" +
1045 // Animation
1046 " float ani = UNI_Position.z;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001047
Romain Guy060b5c82010-03-04 10:07:38 -08001048 " float bendY1 = UNI_BendPos.x;\n" +
1049 " float bendY2 = UNI_BendPos.y;\n" +
1050 " float bendAngle = 47.0 * (3.14 / 180.0);\n" +
1051 " float bendDistance = bendY1 * 0.4;\n" +
1052 " float distanceDimLevel = 0.6;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001053
Romain Guy060b5c82010-03-04 10:07:38 -08001054 " float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
1055 " float aDy = cos(bendAngle);\n" +
1056 " float aDz = sin(bendAngle);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001057
Romain Guy060b5c82010-03-04 10:07:38 -08001058 " float scale = (2.0 / 480.0);\n" +
1059 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
1060 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
1061 " float y = 0.0;\n" +
1062 " float z = 0.0;\n" +
1063 " float lum = 1.0;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001064
Romain Guy060b5c82010-03-04 10:07:38 -08001065 " float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
1066 " y += cv * aDy;\n" +
1067 " z += -cv * aDz;\n" +
1068 " cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" + // curve range
1069 " lum += cv / bendDistance * distanceDimLevel;\n" +
1070 " y += cv * cos(cv * bendStep);\n" +
1071 " z += cv * sin(cv * bendStep);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001072
Romain Guy060b5c82010-03-04 10:07:38 -08001073 " cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
1074 " y += cv * aDy;\n" +
1075 " z += cv * aDz;\n" +
1076 " cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
1077 " lum -= cv / bendDistance * distanceDimLevel;\n" +
1078 " y += cv * cos(cv * bendStep);\n" +
1079 " z += cv * sin(cv * bendStep);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001080
Romain Guy060b5c82010-03-04 10:07:38 -08001081 " y += clamp(ys, bendY1, bendY2);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001082
Romain Guy060b5c82010-03-04 10:07:38 -08001083 " vec4 pos;\n" +
1084 " pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
1085 " pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
1086 " pos.z = z * UNI_ScaleOffset.x;\n" +
1087 " pos.w = 1.0;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001088
Romain Guy060b5c82010-03-04 10:07:38 -08001089 " pos.x *= 1.0 + ani * 4.0;\n" +
1090 " pos.y *= 1.0 + ani * 4.0;\n" +
1091 " pos.z -= ani * 1.5;\n" +
1092 " lum *= 1.0 - ani;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001093
Romain Guy060b5c82010-03-04 10:07:38 -08001094 " gl_Position = UNI_MVP * pos;\n" +
1095 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
1096 " varTex0.xy = ATTRIB_position;\n" +
1097 " varTex0.y = 1.0 - varTex0.y;\n" +
1098 " varTex0.zw = vec2(0.0, 0.0);\n" +
1099 "}\n";
Daniel Sandler388f6792010-03-02 14:08:08 -05001100 sb.setShader(t);
1101 sb.addConstant(mUniformAlloc.getType());
1102 sb.addInput(mMesh.getVertexType(0).getElement());
1103 mPVCurve = sb.create();
1104 mPVCurve.setName("PVCurve");
1105 mPVCurve.bindAllocation(mPVA);
1106 mPVCurve.bindConstants(mUniformAlloc, 1);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001107
Daniel Sandler388f6792010-03-02 14:08:08 -05001108 mRS.contextBindProgramVertex(mPV);
1109 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001110
Daniel Sandler388f6792010-03-02 14:08:08 -05001111 private void initProgramFragment() {
1112 Sampler.Builder sb = new Sampler.Builder(mRS);
1113 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
1114 sb.setMag(Sampler.Value.NEAREST);
1115 sb.setWrapS(Sampler.Value.CLAMP);
1116 sb.setWrapT(Sampler.Value.CLAMP);
1117 Sampler linear = sb.create();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001118
Daniel Sandler388f6792010-03-02 14:08:08 -05001119 sb.setMin(Sampler.Value.NEAREST);
1120 sb.setMag(Sampler.Value.NEAREST);
1121 Sampler nearest = sb.create();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001122
Daniel Sandler388f6792010-03-02 14:08:08 -05001123 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
1124 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1125 ProgramFragment.Builder.Format.RGBA, 0);
1126 mPFTexMip = bf.create();
1127 mPFTexMip.setName("PFTexMip");
1128 mPFTexMip.bindSampler(linear, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001129
Daniel Sandler388f6792010-03-02 14:08:08 -05001130 mPFTexNearest = bf.create();
1131 mPFTexNearest.setName("PFTexNearest");
1132 mPFTexNearest.bindSampler(nearest, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001133
Daniel Sandler388f6792010-03-02 14:08:08 -05001134 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1135 ProgramFragment.Builder.Format.ALPHA, 0);
1136 mPFTexMipAlpha = bf.create();
1137 mPFTexMipAlpha.setName("PFTexMipAlpha");
1138 mPFTexMipAlpha.bindSampler(linear, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001139
Daniel Sandler388f6792010-03-02 14:08:08 -05001140 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001141
Daniel Sandler388f6792010-03-02 14:08:08 -05001142 private void initProgramStore() {
1143 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1144 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
1145 bs.setColorMask(true,true,true,false);
1146 bs.setDitherEnable(true);
1147 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1148 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1149 mPSIcons = bs.create();
1150 mPSIcons.setName("PSIcons");
1151 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001152
Daniel Sandler388f6792010-03-02 14:08:08 -05001153 private void initGl() {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001154 mTouchXBorders = new int[mAllApps.mColumnsPerPage + 1];
1155 mTouchYBorders = new int[mAllApps.mRowsPerPage + 1];
Daniel Sandler388f6792010-03-02 14:08:08 -05001156 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001157
Daniel Sandler388f6792010-03-02 14:08:08 -05001158 private void initData() {
1159 mParams = new Params();
1160 mState = new State();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001161
Romain Guy13c2e7b2010-03-10 19:45:00 -08001162 final Utilities.BubbleText bubble = new Utilities.BubbleText(mAllApps.getContext());
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001163
Daniel Sandler388f6792010-03-02 14:08:08 -05001164 mParams.bubbleWidth = bubble.getBubbleWidth();
1165 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1166 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1167 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001168
Daniel Sandler388f6792010-03-02 14:08:08 -05001169 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1170 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1171 mHomeButtonNormal.uploadToTexture(0);
1172 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1173 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1174 mHomeButtonFocused.uploadToTexture(0);
1175 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1176 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1177 mHomeButtonPressed.uploadToTexture(0);
1178 mParams.homeButtonWidth = 76;
1179 mParams.homeButtonHeight = 68;
1180 mParams.homeButtonTextureWidth = 128;
1181 mParams.homeButtonTextureHeight = 128;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001182
Daniel Sandler388f6792010-03-02 14:08:08 -05001183 mState.homeButtonId = mHomeButtonNormal.getID();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001184
Daniel Sandler388f6792010-03-02 14:08:08 -05001185 mParams.save();
1186 mState.save();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001187
Daniel Sandler388f6792010-03-02 14:08:08 -05001188 mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1189 Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1190 mSelectionCanvas = new Canvas(mSelectionBitmap);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001191
Daniel Sandler388f6792010-03-02 14:08:08 -05001192 setApps(null);
1193 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001194
Daniel Sandler388f6792010-03-02 14:08:08 -05001195 private void initRs() {
1196 ScriptC.Builder sb = new ScriptC.Builder(mRS);
1197 sb.setScript(mRes, R.raw.allapps);
1198 sb.setRoot(true);
Romain Guy13c2e7b2010-03-10 19:45:00 -08001199 sb.addDefines(mAllApps.mDefines);
Daniel Sandler388f6792010-03-02 14:08:08 -05001200 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1201 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
1202 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
1203 mInvokeMove = sb.addInvokable("move");
1204 mInvokeFling = sb.addInvokable("fling");
1205 mInvokeMoveTo = sb.addInvokable("moveTo");
1206 mInvokeResetWAR = sb.addInvokable("resetHWWar");
1207 mInvokeSetZoom = sb.addInvokable("setZoom");
1208 mScript = sb.create();
1209 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1210 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1211 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1212 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1213 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
1214 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001215
Daniel Sandler388f6792010-03-02 14:08:08 -05001216 mRS.contextBindRootScript(mScript);
1217 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001218
Daniel Sandler388f6792010-03-02 14:08:08 -05001219 void dirtyCheck() {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001220 if (mAllApps.mZoomDirty) {
1221 setZoom(mAllApps.mNextZoom, mAllApps.mAnimateNextZoom);
Daniel Sandler388f6792010-03-02 14:08:08 -05001222 }
1223 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001224
Romain Guy060b5c82010-03-04 10:07:38 -08001225 @SuppressWarnings({"ConstantConditions"})
Daniel Sandler388f6792010-03-02 14:08:08 -05001226 private void setApps(ArrayList<ApplicationInfo> list) {
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001227 mRollo.pause();
Daniel Sandler388f6792010-03-02 14:08:08 -05001228 final int count = list != null ? list.size() : 0;
1229 int allocCount = count;
1230 if (allocCount < 1) {
1231 allocCount = 1;
1232 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001233
Daniel Sandler388f6792010-03-02 14:08:08 -05001234 mIcons = new Allocation[count];
1235 mIconIds = new int[allocCount];
1236 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001237
Daniel Sandler388f6792010-03-02 14:08:08 -05001238 mLabels = new Allocation[count];
1239 mLabelIds = new int[allocCount];
1240 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001241
Daniel Sandler388f6792010-03-02 14:08:08 -05001242 mState.iconCount = count;
1243 for (int i=0; i < mState.iconCount; i++) {
1244 createAppIconAllocations(i, list.get(i));
1245 }
1246 for (int i=0; i < mState.iconCount; i++) {
1247 uploadAppIcon(i, list.get(i));
1248 }
1249 saveAppsList();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001250 mRollo.resume();
Daniel Sandler388f6792010-03-02 14:08:08 -05001251 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001252
Daniel Sandler388f6792010-03-02 14:08:08 -05001253 private void setZoom(float zoom, boolean animate) {
1254 mRollo.clearSelectedIcon();
1255 mRollo.setHomeSelected(SELECTED_NONE);
1256 if (zoom > 0.001f) {
1257 mRollo.mState.zoomTarget = zoom;
1258 } else {
1259 mRollo.mState.zoomTarget = 0;
1260 }
1261 mRollo.mState.save();
1262 if (!animate) {
1263 mRollo.mInvokeSetZoom.execute();
1264 }
1265 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001266
Daniel Sandler388f6792010-03-02 14:08:08 -05001267 private void createAppIconAllocations(int index, ApplicationInfo item) {
1268 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001269 Element.RGBA_8888(mRS), false);
Daniel Sandler388f6792010-03-02 14:08:08 -05001270 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001271 Element.A_8(mRS), false);
Daniel Sandler388f6792010-03-02 14:08:08 -05001272 mIconIds[index] = mIcons[index].getID();
1273 mLabelIds[index] = mLabels[index].getID();
1274 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001275
Daniel Sandler388f6792010-03-02 14:08:08 -05001276 private void uploadAppIcon(int index, ApplicationInfo item) {
1277 if (mIconIds[index] != mIcons[index].getID()) {
1278 throw new IllegalStateException("uploadAppIcon index=" + index
1279 + " mIcons[index].getID=" + mIcons[index].getID()
1280 + " mIconsIds[index]=" + mIconIds[index]
1281 + " item=" + item);
1282 }
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001283 mIcons[index].uploadToTexture(true, 0);
1284 mLabels[index].uploadToTexture(true, 0);
Daniel Sandler388f6792010-03-02 14:08:08 -05001285 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001286
Daniel Sandler388f6792010-03-02 14:08:08 -05001287 /**
1288 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1289 * fill in the values and call saveAppsList().
1290 */
1291 private void reallocAppsList(int count) {
1292 Allocation[] icons = new Allocation[count];
1293 int[] iconIds = new int[count];
1294 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001295
Daniel Sandler388f6792010-03-02 14:08:08 -05001296 Allocation[] labels = new Allocation[count];
1297 int[] labelIds = new int[count];
1298 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001299
Daniel Sandler388f6792010-03-02 14:08:08 -05001300 final int oldCount = mRollo.mState.iconCount;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001301
Daniel Sandler388f6792010-03-02 14:08:08 -05001302 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1303 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1304 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1305 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001306
Daniel Sandler388f6792010-03-02 14:08:08 -05001307 mIcons = icons;
1308 mIconIds = iconIds;
1309 mLabels = labels;
1310 mLabelIds = labelIds;
1311 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001312
Daniel Sandler388f6792010-03-02 14:08:08 -05001313 /**
1314 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1315 */
1316 private void addApp(int index, ApplicationInfo item) {
1317 final int count = mState.iconCount - index;
1318 final int dest = index + 1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001319
Daniel Sandler388f6792010-03-02 14:08:08 -05001320 System.arraycopy(mIcons, index, mIcons, dest, count);
1321 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1322 System.arraycopy(mLabels, index, mLabels, dest, count);
1323 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001324
Daniel Sandler388f6792010-03-02 14:08:08 -05001325 createAppIconAllocations(index, item);
1326 uploadAppIcon(index, item);
1327 mRollo.mState.iconCount++;
1328 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001329
Daniel Sandler388f6792010-03-02 14:08:08 -05001330 /**
1331 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1332 */
1333 private void removeApp(int index) {
1334 final int count = mState.iconCount - index - 1;
1335 final int src = index + 1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001336
Daniel Sandler388f6792010-03-02 14:08:08 -05001337 System.arraycopy(mIcons, src, mIcons, index, count);
1338 System.arraycopy(mIconIds, src, mIconIds, index, count);
1339 System.arraycopy(mLabels, src, mLabels, index, count);
1340 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001341
Daniel Sandler388f6792010-03-02 14:08:08 -05001342 mRollo.mState.iconCount--;
1343 final int last = mState.iconCount;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001344
Daniel Sandler388f6792010-03-02 14:08:08 -05001345 mIcons[last] = null;
1346 mIconIds[last] = 0;
1347 mLabels[last] = null;
1348 mLabelIds[last] = 0;
1349 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001350
Daniel Sandler388f6792010-03-02 14:08:08 -05001351 /**
1352 * Send the apps list structures to RS.
1353 */
1354 private void saveAppsList() {
1355 // WTF: how could mScript be not null but mAllocIconIds null b/2460740.
1356 if (mScript != null && mAllocIconIds != null) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001357 mAllocIconIds.data(mIconIds);
1358 mAllocLabelIds.data(mLabelIds);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001359
Daniel Sandler388f6792010-03-02 14:08:08 -05001360 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1361 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001362
Daniel Sandler388f6792010-03-02 14:08:08 -05001363 mState.save();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001364
Daniel Sandler388f6792010-03-02 14:08:08 -05001365 // Note: mScript may be null if we haven't initialized it yet.
1366 // In that case, this is a no-op.
1367 if (mInvokeResetWAR != null) {
1368 mInvokeResetWAR.execute();
1369 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001370 }
1371 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001372
Romain Guy13c2e7b2010-03-10 19:45:00 -08001373 void initTouchState(int width, int height) {
1374 boolean isPortrait = width < height;
1375
Romain Guy060b5c82010-03-04 10:07:38 -08001376 // TODO: Put this in a config file/define
1377 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE_PORTRAIT;
1378 if (!isPortrait) cellHeight -= 12;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001379 int centerY = (int) (mAllApps.getHeight() * (isPortrait ? 0.5f : 0.47f));
Romain Guy060b5c82010-03-04 10:07:38 -08001380 if (!isPortrait) centerY += cellHeight / 2;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001381 int half = (int) Math.floor((mAllApps.mRowsPerPage + 1) / 2);
Romain Guy060b5c82010-03-04 10:07:38 -08001382 int end = mTouchYBorders.length - (half + 1);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001383
Romain Guy060b5c82010-03-04 10:07:38 -08001384 for (int i = -half; i <= end; i++) {
1385 mTouchYBorders[i + half] = centerY + i * cellHeight;
1386 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001387
Romain Guy060b5c82010-03-04 10:07:38 -08001388 int x = 0;
1389 // TODO: Put this in a config file/define
1390 int columnWidth = 120;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001391 for (int i = 0; i < mAllApps.mColumnsPerPage + 1; i++) {
Romain Guy060b5c82010-03-04 10:07:38 -08001392 mTouchXBorders[i] = x;
1393 x += columnWidth;
1394 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001395 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001396
Daniel Sandler388f6792010-03-02 14:08:08 -05001397 void fling() {
1398 mInvokeFling.execute();
1399 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001400
Daniel Sandler388f6792010-03-02 14:08:08 -05001401 void move() {
1402 mInvokeMove.execute();
1403 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001404
Daniel Sandler388f6792010-03-02 14:08:08 -05001405 void moveTo(float row) {
1406 mState.targetPos = row;
1407 mState.save();
1408 mInvokeMoveTo.execute();
1409 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001410
Romain Guy6a42cf32010-03-12 16:03:52 -08001411 int chooseTappedIcon(int x, int y) {
1412 float pos = mScrollPos;
1413
Daniel Sandler388f6792010-03-02 14:08:08 -05001414 // Adjust for scroll position if not zero.
1415 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001416
Daniel Sandler388f6792010-03-02 14:08:08 -05001417 int col = -1;
1418 int row = -1;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001419 final int columnsCount = mAllApps.mColumnsPerPage;
Romain Guy060b5c82010-03-04 10:07:38 -08001420 for (int i=0; i< columnsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001421 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1422 col = i;
1423 break;
1424 }
1425 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001426 final int rowsCount = mAllApps.mRowsPerPage;
Romain Guy060b5c82010-03-04 10:07:38 -08001427 for (int i=0; i< rowsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001428 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1429 row = i;
1430 break;
1431 }
1432 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001433
Daniel Sandler388f6792010-03-02 14:08:08 -05001434 if (row < 0 || col < 0) {
1435 return -1;
1436 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001437
Romain Guy6a42cf32010-03-12 16:03:52 -08001438 int index = (((int) pos) * columnsCount) + (row * columnsCount) + col;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001439
Daniel Sandler388f6792010-03-02 14:08:08 -05001440 if (index >= mState.iconCount) {
1441 return -1;
1442 } else {
1443 return index;
1444 }
1445 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001446
Daniel Sandler388f6792010-03-02 14:08:08 -05001447 /**
1448 * You need to call save() on mState on your own after calling this.
1449 *
1450 * @return the index of the icon that was selected.
1451 */
Romain Guy6a42cf32010-03-12 16:03:52 -08001452 int selectIcon(int x, int y, int pressed) {
1453 final int index = chooseTappedIcon(x, y);
Daniel Sandler388f6792010-03-02 14:08:08 -05001454 selectIcon(index, pressed);
1455 return index;
1456 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001457
Daniel Sandler388f6792010-03-02 14:08:08 -05001458 /**
1459 * Select the icon at the given index.
1460 *
1461 * @param index The index.
1462 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
1463 */
1464 void selectIcon(int index, int pressed) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001465 final ArrayList<ApplicationInfo> appsList = mAllApps.mAllAppsList;
1466 if (appsList == null || index < 0 || index >= appsList.size()) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001467 mState.selectedIconIndex = -1;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001468 if (mAllApps.mLastSelection == SELECTION_ICONS) {
1469 mAllApps.mLastSelection = SELECTION_NONE;
Daniel Sandler388f6792010-03-02 14:08:08 -05001470 }
1471 } else {
1472 if (pressed == SELECTED_FOCUSED) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001473 mAllApps.mLastSelection = SELECTION_ICONS;
Daniel Sandler388f6792010-03-02 14:08:08 -05001474 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001475
Daniel Sandler388f6792010-03-02 14:08:08 -05001476 int prev = mState.selectedIconIndex;
1477 mState.selectedIconIndex = index;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001478
Romain Guy13c2e7b2010-03-10 19:45:00 -08001479 ApplicationInfo info = appsList.get(index);
Daniel Sandler388f6792010-03-02 14:08:08 -05001480 Bitmap selectionBitmap = mSelectionBitmap;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001481
Daniel Sandler388f6792010-03-02 14:08:08 -05001482 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1483 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1484 pressed == SELECTED_PRESSED, info.iconBitmap);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001485
Daniel Sandler388f6792010-03-02 14:08:08 -05001486 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
1487 Element.RGBA_8888(mRS), false);
1488 mSelectedIcon.uploadToTexture(0);
1489 mState.selectedIconTexture = mSelectedIcon.getID();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001490
Daniel Sandler388f6792010-03-02 14:08:08 -05001491 if (prev != index) {
1492 if (info.title != null && info.title.length() > 0) {
1493 //setContentDescription(info.title);
Romain Guy13c2e7b2010-03-10 19:45:00 -08001494 mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Daniel Sandler388f6792010-03-02 14:08:08 -05001495 }
1496 }
1497 }
1498 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001499
Daniel Sandler388f6792010-03-02 14:08:08 -05001500 /**
1501 * You need to call save() on mState on your own after calling this.
1502 */
1503 void clearSelectedIcon() {
1504 mState.selectedIconIndex = -1;
1505 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001506
Daniel Sandler388f6792010-03-02 14:08:08 -05001507 void setHomeSelected(int mode) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001508 final int prev = mAllApps.mLastSelection;
Daniel Sandler388f6792010-03-02 14:08:08 -05001509 switch (mode) {
1510 case SELECTED_NONE:
1511 mState.homeButtonId = mHomeButtonNormal.getID();
1512 break;
1513 case SELECTED_FOCUSED:
Romain Guy13c2e7b2010-03-10 19:45:00 -08001514 mAllApps.mLastSelection = SELECTION_HOME;
Daniel Sandler388f6792010-03-02 14:08:08 -05001515 mState.homeButtonId = mHomeButtonFocused.getID();
1516 if (prev != SELECTION_HOME) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001517 mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Daniel Sandler388f6792010-03-02 14:08:08 -05001518 }
1519 break;
1520 case SELECTED_PRESSED:
1521 mState.homeButtonId = mHomeButtonPressed.getID();
1522 break;
1523 }
1524 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001525
Daniel Sandler388f6792010-03-02 14:08:08 -05001526 public void dumpState() {
1527 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1528 Log.d(TAG, "mRollo.mHeight=" + mHeight);
Romain Guy060b5c82010-03-04 10:07:38 -08001529 Log.d(TAG, "mRollo.mIcons=" + Arrays.toString(mIcons));
Daniel Sandler388f6792010-03-02 14:08:08 -05001530 if (mIcons != null) {
1531 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1532 }
1533 if (mIconIds != null) {
1534 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1535 }
1536 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1537 if (mLabelIds != null) {
1538 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1539 }
1540 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1541 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1542 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
1543 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1544 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1545 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1546 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1547 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1548 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1549 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1550 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1551 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1552 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1553 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1554 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1555 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1556 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1557 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1558 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1559 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1560 }
1561 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001562
Daniel Sandler388f6792010-03-02 14:08:08 -05001563 public void dumpState() {
1564 Log.d(TAG, "mRS=" + mRS);
1565 Log.d(TAG, "mRollo=" + mRollo);
1566 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1567 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1568 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1569 Log.d(TAG, "mLastSelection=" + mLastSelection);
1570 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1571 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1572 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1573 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1574 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1575 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1576 Log.d(TAG, "mZoom=" + mZoom);
Romain Guy6a42cf32010-03-12 16:03:52 -08001577 Log.d(TAG, "mScrollPos=" + mRollo.mScrollPos);
Daniel Sandler388f6792010-03-02 14:08:08 -05001578 Log.d(TAG, "mVelocity=" + mVelocity);
1579 Log.d(TAG, "mMessageProc=" + mMessageProc);
1580 if (mRollo != null) {
1581 mRollo.dumpState();
1582 }
1583 if (mRS != null) {
1584 mRS.contextDump(0);
1585 }
1586 }
1587}
1588
1589