blob: fe267bae15f2e30f9ea26729dd1eb04be2cf97c0 [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) {
Joe Onorato96da4012010-03-17 20:03:24 -0700207 Log.i(TAG, "onDetachedFromWindow");
Romain Guy13c2e7b2010-03-10 19:45:00 -0800208 destroyRenderScript();
209 mRS = null;
Joe Onorato96da4012010-03-17 20:03:24 -0700210 mRollo = null;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800211 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500212 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800213
Daniel Sandler388f6792010-03-02 14:08:08 -0500214 /**
215 * If you have an attached click listener, View always plays the click sound!?!?
216 * Deal with sound effects by hand.
217 */
218 public void reallyPlaySoundEffect(int sound) {
219 boolean old = isSoundEffectsEnabled();
220 setSoundEffectsEnabled(true);
221 playSoundEffect(sound);
222 setSoundEffectsEnabled(old);
223 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800224
Daniel Sandler388f6792010-03-02 14:08:08 -0500225 public void setLauncher(Launcher launcher) {
226 mLauncher = launcher;
227 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800228
Daniel Sandler388f6792010-03-02 14:08:08 -0500229 @Override
230 public void surfaceDestroyed(SurfaceHolder holder) {
231 super.surfaceDestroyed(holder);
232 // Without this, we leak mMessageCallback which leaks the context.
Romain Guy13c2e7b2010-03-10 19:45:00 -0800233 if (!mSurrendered) {
234 mRS.mMessageCallback = null;
235 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500236 // We may lose any callbacks that are pending, so make sure that we re-sync that
237 // on the next surfaceChanged.
238 mZoomDirty = true;
239 mHaveSurface = false;
240 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800241
Daniel Sandler388f6792010-03-02 14:08:08 -0500242 @Override
243 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
244 //long startTime = SystemClock.uptimeMillis();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800245
Daniel Sandler388f6792010-03-02 14:08:08 -0500246 super.surfaceChanged(holder, format, w, h);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800247
Romain Guy13c2e7b2010-03-10 19:45:00 -0800248 if (mSurrendered) return;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800249
Daniel Sandler388f6792010-03-02 14:08:08 -0500250 mHaveSurface = true;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800251
Daniel Sandler388f6792010-03-02 14:08:08 -0500252 if (mRollo == null) {
Romain Guy13c2e7b2010-03-10 19:45:00 -0800253 mRollo = new RolloRS(this);
Daniel Sandler388f6792010-03-02 14:08:08 -0500254 mRollo.init(getResources(), w, h);
255 if (mAllAppsList != null) {
256 mRollo.setApps(mAllAppsList);
257 }
258 if (mShouldGainFocus) {
259 gainFocus();
260 mShouldGainFocus = false;
261 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800262 } else if (mRollo.mInitialize) {
263 mRollo.initGl();
264 mRollo.initTouchState(w, h);
265 mRollo.mInitialize = false;
Daniel Sandler388f6792010-03-02 14:08:08 -0500266 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800267
Daniel Sandler388f6792010-03-02 14:08:08 -0500268 mRollo.dirtyCheck();
269 mRollo.resize(w, h);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800270
Daniel Sandler388f6792010-03-02 14:08:08 -0500271 if (mRS != null) {
272 mRS.mMessageCallback = mMessageProc = new AAMessage();
273 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800274
Daniel Sandler388f6792010-03-02 14:08:08 -0500275 if (mRollo.mUniformAlloc != null) {
276 float tf[] = new float[] {72.f, 72.f,
277 120.f, 120.f, 0.f, 0.f,
278 120.f, 680.f,
279 (2.f / 480.f), 0, -((float)w / 2) - 0.25f, -380.25f};
280 if (w > h) {
281 tf[6] = 40.f;
282 tf[7] = h - 40.f;
283 tf[9] = 1.f;
284 tf[10] = -((float)w / 2) - 0.25f;
285 tf[11] = -((float)h / 2) - 0.25f;
286 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800287
Daniel Sandler388f6792010-03-02 14:08:08 -0500288 mRollo.mUniformAlloc.data(tf);
289 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800290
Daniel Sandler388f6792010-03-02 14:08:08 -0500291 //long endTime = SystemClock.uptimeMillis();
292 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
293 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800294
Daniel Sandler388f6792010-03-02 14:08:08 -0500295 @Override
296 public void onWindowFocusChanged(boolean hasWindowFocus) {
297 super.onWindowFocusChanged(hasWindowFocus);
Romain Guy13c2e7b2010-03-10 19:45:00 -0800298
299 if (mSurrendered) return;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800300
Daniel Sandler388f6792010-03-02 14:08:08 -0500301 if (mArrowNavigation) {
302 if (!hasWindowFocus) {
303 // Clear selection when we lose window focus
304 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
305 mRollo.setHomeSelected(SELECTED_NONE);
306 mRollo.clearSelectedIcon();
307 mRollo.mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -0800308 } else {
Daniel Sandler388f6792010-03-02 14:08:08 -0500309 if (mRollo.mState.iconCount > 0) {
310 if (mLastSelection == SELECTION_ICONS) {
311 int selection = mLastSelectedIcon;
Romain Guy6a42cf32010-03-12 16:03:52 -0800312 final int firstIcon = Math.round(mRollo.mScrollPos) * mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500313 if (selection < 0 || // No selection
314 selection < firstIcon || // off the top of the screen
315 selection >= mRollo.mState.iconCount || // past last icon
316 selection >= firstIcon + // past last icon on screen
Romain Guy060b5c82010-03-04 10:07:38 -0800317 (mColumnsPerPage * mRowsPerPage)) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500318 selection = firstIcon;
319 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800320
Daniel Sandler388f6792010-03-02 14:08:08 -0500321 // Select the first icon when we gain window focus
322 mRollo.selectIcon(selection, SELECTED_FOCUSED);
323 mRollo.mState.save();
324 } else if (mLastSelection == SELECTION_HOME) {
325 mRollo.setHomeSelected(SELECTED_FOCUSED);
326 mRollo.mState.save();
327 }
328 }
329 }
330 }
331 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800332
Daniel Sandler388f6792010-03-02 14:08:08 -0500333 @Override
334 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
335 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800336
Romain Guy13c2e7b2010-03-10 19:45:00 -0800337 if (!isVisible() || mSurrendered) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500338 return;
339 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800340
Daniel Sandler388f6792010-03-02 14:08:08 -0500341 if (gainFocus) {
342 if (mRollo != null) {
343 gainFocus();
344 } else {
345 mShouldGainFocus = true;
346 }
347 } else {
348 if (mRollo != null) {
349 if (mArrowNavigation) {
350 // Clear selection when we lose focus
351 mRollo.clearSelectedIcon();
352 mRollo.setHomeSelected(SELECTED_NONE);
353 mRollo.mState.save();
354 mArrowNavigation = false;
355 }
356 } else {
357 mShouldGainFocus = false;
358 }
359 }
360 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800361
Daniel Sandler388f6792010-03-02 14:08:08 -0500362 private void gainFocus() {
363 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
364 // Select the first icon when we gain keyboard focus
365 mArrowNavigation = true;
Romain Guy6a42cf32010-03-12 16:03:52 -0800366 mRollo.selectIcon(Math.round(mRollo.mScrollPos) * mColumnsPerPage, SELECTED_FOCUSED);
Daniel Sandler388f6792010-03-02 14:08:08 -0500367 mRollo.mState.save();
368 }
369 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800370
Daniel Sandler388f6792010-03-02 14:08:08 -0500371 @Override
372 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800373
Daniel Sandler388f6792010-03-02 14:08:08 -0500374 boolean handled = false;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800375
Daniel Sandler388f6792010-03-02 14:08:08 -0500376 if (!isVisible()) {
377 return false;
378 }
379 final int iconCount = mRollo.mState.iconCount;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800380
Daniel Sandler388f6792010-03-02 14:08:08 -0500381 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
382 if (mArrowNavigation) {
383 if (mLastSelection == SELECTION_HOME) {
384 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
385 mLauncher.closeAllApps(true);
386 } else {
387 int whichApp = mRollo.mState.selectedIconIndex;
388 if (whichApp >= 0) {
389 ApplicationInfo app = mAllAppsList.get(whichApp);
390 mLauncher.startActivitySafely(app.intent);
391 handled = true;
392 }
393 }
394 }
395 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800396
Daniel Sandler388f6792010-03-02 14:08:08 -0500397 if (iconCount > 0) {
Romain Guy6a42cf32010-03-12 16:03:52 -0800398 final boolean isPortrait = getWidth() < getHeight();
399
Daniel Sandler388f6792010-03-02 14:08:08 -0500400 mArrowNavigation = true;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800401
Daniel Sandler388f6792010-03-02 14:08:08 -0500402 int currentSelection = mRollo.mState.selectedIconIndex;
Romain Guy6a42cf32010-03-12 16:03:52 -0800403 int currentTopRow = Math.round(mRollo.mScrollPos);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800404
Romain Guy060b5c82010-03-04 10:07:38 -0800405 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE_PORTRAIT-1
406 final int currentPageCol = currentSelection % mColumnsPerPage;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800407
Romain Guy060b5c82010-03-04 10:07:38 -0800408 // The row of the current selection, in the range 0..ROWS_PER_PAGE_PORTRAIT-1
Romain Guy6a42cf32010-03-12 16:03:52 -0800409 final int currentPageRow = (currentSelection - (currentTopRow * mColumnsPerPage))
Romain Guy060b5c82010-03-04 10:07:38 -0800410 / mRowsPerPage;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800411
Daniel Sandler388f6792010-03-02 14:08:08 -0500412 int newSelection = currentSelection;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800413
Daniel Sandler388f6792010-03-02 14:08:08 -0500414 switch (keyCode) {
415 case KeyEvent.KEYCODE_DPAD_UP:
416 if (mLastSelection == SELECTION_HOME) {
Romain Guy6a42cf32010-03-12 16:03:52 -0800417 if (isPortrait) {
418 mRollo.setHomeSelected(SELECTED_NONE);
419 int lastRowCount = iconCount % mColumnsPerPage;
420 if (lastRowCount == 0) {
421 lastRowCount = mColumnsPerPage;
422 }
423 newSelection = iconCount - lastRowCount + (mColumnsPerPage / 2);
424 if (newSelection >= iconCount) {
425 newSelection = iconCount-1;
426 }
427 int target = (newSelection / mColumnsPerPage) - (mRowsPerPage - 1);
428 if (target < 0) {
429 target = 0;
430 }
431 if (currentTopRow != target) {
432 mRollo.moveTo(target);
433 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500434 }
435 } else {
436 if (currentPageRow > 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800437 newSelection = currentSelection - mColumnsPerPage;
Romain Guy6a42cf32010-03-12 16:03:52 -0800438 if (currentTopRow > newSelection / mColumnsPerPage) {
439 mRollo.moveTo(newSelection / mColumnsPerPage);
440 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500441 } else if (currentTopRow > 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800442 newSelection = currentSelection - mColumnsPerPage;
443 mRollo.moveTo(newSelection / mColumnsPerPage);
Daniel Sandler388f6792010-03-02 14:08:08 -0500444 } else if (currentPageRow != 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800445 newSelection = currentTopRow * mRowsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500446 }
447 }
448 handled = true;
449 break;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800450
Daniel Sandler388f6792010-03-02 14:08:08 -0500451 case KeyEvent.KEYCODE_DPAD_DOWN: {
Romain Guy060b5c82010-03-04 10:07:38 -0800452 final int rowCount = iconCount / mColumnsPerPage
453 + (iconCount % mColumnsPerPage == 0 ? 0 : 1);
454 final int currentRow = currentSelection / mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500455 if (mLastSelection != SELECTION_HOME) {
456 if (currentRow < rowCount-1) {
457 mRollo.setHomeSelected(SELECTED_NONE);
458 if (currentSelection < 0) {
459 newSelection = 0;
460 } else {
Romain Guy060b5c82010-03-04 10:07:38 -0800461 newSelection = currentSelection + mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500462 }
463 if (newSelection >= iconCount) {
464 // Go from D to G in this arrangement:
465 // A B C D
466 // E F G
467 newSelection = iconCount - 1;
468 }
Romain Guy060b5c82010-03-04 10:07:38 -0800469 if (currentPageRow >= mRowsPerPage - 1) {
Romain Guy6a42cf32010-03-12 16:03:52 -0800470 mRollo.moveTo((newSelection / mColumnsPerPage) - mRowsPerPage + 1);
Daniel Sandler388f6792010-03-02 14:08:08 -0500471 }
Romain Guy6a42cf32010-03-12 16:03:52 -0800472 } else if (isPortrait) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500473 newSelection = -1;
474 mRollo.setHomeSelected(SELECTED_FOCUSED);
475 }
476 }
477 handled = true;
478 break;
479 }
480 case KeyEvent.KEYCODE_DPAD_LEFT:
481 if (mLastSelection != SELECTION_HOME) {
482 if (currentPageCol > 0) {
483 newSelection = currentSelection - 1;
484 }
Romain Guy6a42cf32010-03-12 16:03:52 -0800485 } else if (!isPortrait) {
486 newSelection = ((int) (mRollo.mScrollPos) * mColumnsPerPage) +
487 (mRowsPerPage / 2 * mColumnsPerPage) + mColumnsPerPage - 1;
488 mRollo.setHomeSelected(SELECTED_NONE);
Daniel Sandler388f6792010-03-02 14:08:08 -0500489 }
490 handled = true;
491 break;
492 case KeyEvent.KEYCODE_DPAD_RIGHT:
493 if (mLastSelection != SELECTION_HOME) {
Romain Guy6a42cf32010-03-12 16:03:52 -0800494 if (!isPortrait && (currentPageCol == mColumnsPerPage - 1 ||
495 currentSelection == iconCount - 1)) {
496 newSelection = -1;
497 mRollo.setHomeSelected(SELECTED_FOCUSED);
498 } else if ((currentPageCol < mColumnsPerPage - 1) &&
Daniel Sandler388f6792010-03-02 14:08:08 -0500499 (currentSelection < iconCount - 1)) {
500 newSelection = currentSelection + 1;
501 }
502 }
503 handled = true;
504 break;
505 }
506 if (newSelection != currentSelection) {
507 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
508 mRollo.mState.save();
509 }
510 }
511 return handled;
512 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800513
Daniel Sandler388f6792010-03-02 14:08:08 -0500514 @Override
515 public boolean onTouchEvent(MotionEvent ev)
516 {
517 mArrowNavigation = false;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800518
Daniel Sandler388f6792010-03-02 14:08:08 -0500519 if (!isVisible()) {
520 return true;
521 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800522
Daniel Sandler388f6792010-03-02 14:08:08 -0500523 if (mLocks != 0) {
524 return true;
525 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800526
Daniel Sandler388f6792010-03-02 14:08:08 -0500527 super.onTouchEvent(ev);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800528
Daniel Sandler388f6792010-03-02 14:08:08 -0500529 int x = (int)ev.getX();
530 int y = (int)ev.getY();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800531
Romain Guyce115852010-03-04 12:15:37 -0800532 final boolean isPortrait = getWidth() < getHeight();
Daniel Sandler388f6792010-03-02 14:08:08 -0500533 int action = ev.getAction();
534 switch (action) {
535 case MotionEvent.ACTION_DOWN:
Romain Guyce115852010-03-04 12:15:37 -0800536 if ((isPortrait && y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) ||
537 (!isPortrait && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500538 mTouchTracking = TRACKING_HOME;
539 mRollo.setHomeSelected(SELECTED_PRESSED);
540 mRollo.mState.save();
541 mCurrentIconIndex = -1;
542 } else {
543 mTouchTracking = TRACKING_FLING;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800544
Daniel Sandler388f6792010-03-02 14:08:08 -0500545 mMotionDownRawX = (int)ev.getRawX();
546 mMotionDownRawY = (int)ev.getRawY();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800547
Daniel Sandler388f6792010-03-02 14:08:08 -0500548 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
549 mRollo.mState.newTouchDown = 1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800550
Daniel Sandler388f6792010-03-02 14:08:08 -0500551 if (!mRollo.checkClickOK()) {
552 mRollo.clearSelectedIcon();
553 } else {
554 mDownIconIndex = mCurrentIconIndex
Romain Guy6a42cf32010-03-12 16:03:52 -0800555 = mRollo.selectIcon(x, y, SELECTED_PRESSED);
Daniel Sandler388f6792010-03-02 14:08:08 -0500556 if (mDownIconIndex < 0) {
557 // if nothing was selected, no long press.
558 cancelLongPress();
559 }
560 }
561 mRollo.mState.save();
562 mRollo.move();
563 mVelocityTracker = VelocityTracker.obtain();
564 mVelocityTracker.addMovement(ev);
565 mStartedScrolling = false;
566 }
567 break;
568 case MotionEvent.ACTION_MOVE:
569 case MotionEvent.ACTION_OUTSIDE:
570 if (mTouchTracking == TRACKING_HOME) {
Romain Guyce115852010-03-04 12:15:37 -0800571 mRollo.setHomeSelected((isPortrait &&
572 y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) || (!isPortrait
573 && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])
Daniel Sandler388f6792010-03-02 14:08:08 -0500574 ? SELECTED_PRESSED : SELECTED_NONE);
575 mRollo.mState.save();
576 } else if (mTouchTracking == TRACKING_FLING) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500577 int rawY = (int)ev.getRawY();
578 int slop;
579 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800580
Daniel Sandler388f6792010-03-02 14:08:08 -0500581 if (!mStartedScrolling && slop < mSlop) {
582 // don't update anything so when we do start scrolling
583 // below, we get the right delta.
Romain Guy6a42cf32010-03-12 16:03:52 -0800584 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y);
Daniel Sandler388f6792010-03-02 14:08:08 -0500585 if (mDownIconIndex != mCurrentIconIndex) {
586 // If a different icon is selected, don't allow it to be picked up.
587 // This handles off-axis dragging.
588 cancelLongPress();
589 mCurrentIconIndex = -1;
590 }
591 } else {
592 if (!mStartedScrolling) {
593 cancelLongPress();
594 mCurrentIconIndex = -1;
595 }
596 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
597 mRollo.mState.newTouchDown = 1;
598 mRollo.move();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800599
Daniel Sandler388f6792010-03-02 14:08:08 -0500600 mStartedScrolling = true;
601 mRollo.clearSelectedIcon();
602 mVelocityTracker.addMovement(ev);
603 mRollo.mState.save();
604 }
605 }
606 break;
607 case MotionEvent.ACTION_UP:
608 case MotionEvent.ACTION_CANCEL:
609 if (mTouchTracking == TRACKING_HOME) {
610 if (action == MotionEvent.ACTION_UP) {
Romain Guyce115852010-03-04 12:15:37 -0800611 if ((isPortrait && y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) ||
612 (!isPortrait && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500613 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
614 mLauncher.closeAllApps(true);
615 }
616 mRollo.setHomeSelected(SELECTED_NONE);
617 mRollo.mState.save();
618 }
619 mCurrentIconIndex = -1;
620 } else if (mTouchTracking == TRACKING_FLING) {
621 mRollo.mState.newTouchDown = 0;
622 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800623
Daniel Sandler388f6792010-03-02 14:08:08 -0500624 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
625 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
626 mRollo.clearSelectedIcon();
627 mRollo.mState.save();
628 mRollo.fling();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800629
Daniel Sandler388f6792010-03-02 14:08:08 -0500630 if (mVelocityTracker != null) {
631 mVelocityTracker.recycle();
632 mVelocityTracker = null;
633 }
634 }
635 mTouchTracking = TRACKING_NONE;
636 break;
637 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800638
Daniel Sandler388f6792010-03-02 14:08:08 -0500639 return true;
640 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800641
Daniel Sandler388f6792010-03-02 14:08:08 -0500642 public void onClick(View v) {
643 if (mLocks != 0 || !isVisible()) {
644 return;
645 }
646 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
647 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
648 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
649 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
650 mLauncher.startActivitySafely(app.intent);
651 }
652 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800653
Daniel Sandler388f6792010-03-02 14:08:08 -0500654 public boolean onLongClick(View v) {
655 if (mLocks != 0 || !isVisible()) {
656 return true;
657 }
658 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
659 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
660 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800661
Daniel Sandler388f6792010-03-02 14:08:08 -0500662 Bitmap bmp = app.iconBitmap;
663 final int w = bmp.getWidth();
664 final int h = bmp.getHeight();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800665
Daniel Sandler388f6792010-03-02 14:08:08 -0500666 // We don't really have an accurate location to use. This will do.
667 int screenX = mMotionDownRawX - (w / 2);
668 int screenY = mMotionDownRawY - h;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800669
Daniel Sandler388f6792010-03-02 14:08:08 -0500670 mDragController.startDrag(bmp, screenX, screenY,
671 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800672
Daniel Sandler388f6792010-03-02 14:08:08 -0500673 mLauncher.closeAllApps(true);
674 }
675 return true;
676 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800677
Daniel Sandler388f6792010-03-02 14:08:08 -0500678 @Override
679 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
680 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
681 if (!isVisible()) {
682 return false;
683 }
684 String text = null;
685 int index;
686 int count = mAllAppsList.size() + 1; // +1 is home
687 int pos = -1;
688 switch (mLastSelection) {
689 case SELECTION_ICONS:
690 index = mRollo.mState.selectedIconIndex;
691 if (index >= 0) {
692 ApplicationInfo info = mAllAppsList.get(index);
693 if (info.title != null) {
694 text = info.title.toString();
695 pos = index;
696 }
697 }
698 break;
699 case SELECTION_HOME:
700 text = getContext().getString(R.string.all_apps_home_button_label);
701 pos = count;
702 break;
703 }
704 if (text != null) {
705 event.setEnabled(true);
706 event.getText().add(text);
707 //event.setContentDescription(text);
708 event.setItemCount(count);
709 event.setCurrentItemIndex(pos);
710 }
711 }
712 return false;
713 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800714
Daniel Sandler388f6792010-03-02 14:08:08 -0500715 public void setDragController(DragController dragger) {
716 mDragController = dragger;
717 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800718
Daniel Sandler388f6792010-03-02 14:08:08 -0500719 public void onDropCompleted(View target, boolean success) {
720 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800721
Daniel Sandler388f6792010-03-02 14:08:08 -0500722 /**
723 * Zoom to the specifed level.
724 *
725 * @param zoom [0..1] 0 is hidden, 1 is open
726 */
727 public void zoom(float zoom, boolean animate) {
728 cancelLongPress();
729 mNextZoom = zoom;
730 mAnimateNextZoom = animate;
731 // if we do setZoom while we don't have a surface, we won't
732 // get the callbacks that actually set mZoom.
733 if (mRollo == null || !mHaveSurface) {
734 mZoomDirty = true;
735 mZoom = zoom;
Daniel Sandler388f6792010-03-02 14:08:08 -0500736 } else {
737 mRollo.setZoom(zoom, animate);
738 }
739 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800740
Daniel Sandler388f6792010-03-02 14:08:08 -0500741 public boolean isVisible() {
742 return mZoom > 0.001f;
743 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800744
Daniel Sandler388f6792010-03-02 14:08:08 -0500745 public boolean isOpaque() {
746 return mZoom > 0.999f;
747 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800748
Daniel Sandler388f6792010-03-02 14:08:08 -0500749 public void setApps(ArrayList<ApplicationInfo> list) {
750 if (mRS == null) {
751 // We've been removed from the window. Don't bother with all this.
752 return;
753 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800754
755 boolean reload = false;
756 if (mAllAppsList == null) {
757 reload = true;
758 } else if (list.size() != mAllAppsList.size()) {
759 reload = true;
760 } else {
761 final int count = list.size();
762 for (int i = 0; i < count; i++) {
763 if (list.get(i) != mAllAppsList.get(i)) {
764 reload = true;
765 break;
766 }
767 }
768 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800769
Daniel Sandler388f6792010-03-02 14:08:08 -0500770 mAllAppsList = list;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800771 if (mRollo != null && reload) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500772 mRollo.setApps(list);
773 }
Romain Guyc16fea72010-03-12 17:17:56 -0800774
775 if (hasFocus() && mRestoreFocusIndex != -1) {
776 mRollo.selectIcon(mRestoreFocusIndex, SELECTED_FOCUSED);
777 mRollo.mState.save();
778 mRestoreFocusIndex = -1;
779 }
780
Daniel Sandler388f6792010-03-02 14:08:08 -0500781 mLocks &= ~LOCK_ICONS_PENDING;
782 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800783
Daniel Sandler388f6792010-03-02 14:08:08 -0500784 public void addApps(ArrayList<ApplicationInfo> list) {
785 if (mAllAppsList == null) {
786 // Not done loading yet. We'll find out about it later.
787 return;
788 }
789 if (mRS == null) {
790 // We've been removed from the window. Don't bother with all this.
791 return;
792 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800793
Daniel Sandler388f6792010-03-02 14:08:08 -0500794 final int N = list.size();
795 if (mRollo != null) {
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800796 mRollo.pause();
Daniel Sandler388f6792010-03-02 14:08:08 -0500797 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
798 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800799
Daniel Sandler388f6792010-03-02 14:08:08 -0500800 for (int i=0; i<N; i++) {
801 final ApplicationInfo item = list.get(i);
802 int index = Collections.binarySearch(mAllAppsList, item,
803 LauncherModel.APP_NAME_COMPARATOR);
804 if (index < 0) {
805 index = -(index+1);
806 }
807 mAllAppsList.add(index, item);
808 if (mRollo != null) {
809 mRollo.addApp(index, item);
810 }
811 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800812
Daniel Sandler388f6792010-03-02 14:08:08 -0500813 if (mRollo != null) {
814 mRollo.saveAppsList();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800815 mRollo.resume();
Daniel Sandler388f6792010-03-02 14:08:08 -0500816 }
817 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800818
Daniel Sandler388f6792010-03-02 14:08:08 -0500819 public void removeApps(ArrayList<ApplicationInfo> list) {
820 if (mAllAppsList == null) {
821 // Not done loading yet. We'll find out about it later.
822 return;
823 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800824
825 if (mRollo != null) {
826 mRollo.pause();
827 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500828 final int N = list.size();
829 for (int i=0; i<N; i++) {
830 final ApplicationInfo item = list.get(i);
831 int index = findAppByComponent(mAllAppsList, item);
832 if (index >= 0) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500833 mAllAppsList.remove(index);
834 if (mRollo != null) {
835 mRollo.removeApp(index);
836 }
837 } else {
838 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
839 // Try to recover. This should keep us from crashing for now.
840 }
841 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800842
Daniel Sandler388f6792010-03-02 14:08:08 -0500843 if (mRollo != null) {
844 mRollo.saveAppsList();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800845 mRollo.resume();
Daniel Sandler388f6792010-03-02 14:08:08 -0500846 }
847 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800848
Joe Onorato64e6be72010-03-05 15:05:52 -0500849 public void updateApps(ArrayList<ApplicationInfo> list) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500850 // Just remove and add, because they may need to be re-sorted.
851 removeApps(list);
852 addApps(list);
853 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800854
Daniel Sandler388f6792010-03-02 14:08:08 -0500855 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
856 ComponentName component = item.intent.getComponent();
857 final int N = list.size();
858 for (int i=0; i<N; i++) {
859 ApplicationInfo x = list.get(i);
860 if (x.intent.getComponent().equals(component)) {
861 return i;
862 }
863 }
864 return -1;
865 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800866
Romain Guy060b5c82010-03-04 10:07:38 -0800867 /*
Daniel Sandler388f6792010-03-02 14:08:08 -0500868 private static int countPages(int iconCount) {
Romain Guy060b5c82010-03-04 10:07:38 -0800869 int iconsPerPage = getColumnsCount() * Defines.ROWS_PER_PAGE_PORTRAIT;
Daniel Sandler388f6792010-03-02 14:08:08 -0500870 int pages = iconCount / iconsPerPage;
871 if (pages*iconsPerPage != iconCount) {
872 pages++;
873 }
874 return pages;
875 }
Romain Guy060b5c82010-03-04 10:07:38 -0800876 */
Daniel Sandler388f6792010-03-02 14:08:08 -0500877
878 class AAMessage extends RenderScript.RSMessage {
879 public void run() {
Romain Guy6a42cf32010-03-12 16:03:52 -0800880 mRollo.mScrollPos = ((float)mData[0]) / (1 << 16);
Daniel Sandler388f6792010-03-02 14:08:08 -0500881 mVelocity = ((float)mData[1]) / (1 << 16);
882 mZoom = ((float)mData[2]) / (1 << 16);
883 mZoomDirty = false;
884 }
885 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800886
Romain Guy13c2e7b2010-03-10 19:45:00 -0800887 public static class RolloRS {
Daniel Sandler388f6792010-03-02 14:08:08 -0500888 // Allocations ======
889 private int mWidth;
890 private int mHeight;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800891
Daniel Sandler388f6792010-03-02 14:08:08 -0500892 private Resources mRes;
893 private Script mScript;
894 private Script.Invokable mInvokeMove;
895 private Script.Invokable mInvokeMoveTo;
896 private Script.Invokable mInvokeFling;
897 private Script.Invokable mInvokeResetWAR;
898 private Script.Invokable mInvokeSetZoom;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800899
Daniel Sandler388f6792010-03-02 14:08:08 -0500900 private ProgramStore mPSIcons;
901 private ProgramFragment mPFTexMip;
902 private ProgramFragment mPFTexMipAlpha;
903 private ProgramFragment mPFTexNearest;
904 private ProgramVertex mPV;
905 private ProgramVertex mPVCurve;
906 private SimpleMesh mMesh;
907 private ProgramVertex.MatrixAllocation mPVA;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800908
Daniel Sandler388f6792010-03-02 14:08:08 -0500909 private Allocation mUniformAlloc;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800910
Daniel Sandler388f6792010-03-02 14:08:08 -0500911 private Allocation mHomeButtonNormal;
912 private Allocation mHomeButtonFocused;
913 private Allocation mHomeButtonPressed;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800914
Daniel Sandler388f6792010-03-02 14:08:08 -0500915 private Allocation[] mIcons;
916 private int[] mIconIds;
917 private Allocation mAllocIconIds;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800918
Daniel Sandler388f6792010-03-02 14:08:08 -0500919 private Allocation[] mLabels;
920 private int[] mLabelIds;
921 private Allocation mAllocLabelIds;
922 private Allocation mSelectedIcon;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800923
Daniel Sandler388f6792010-03-02 14:08:08 -0500924 private int[] mTouchYBorders;
925 private int[] mTouchXBorders;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800926
Daniel Sandler388f6792010-03-02 14:08:08 -0500927 private Bitmap mSelectionBitmap;
928 private Canvas mSelectionCanvas;
Romain Guy6a42cf32010-03-12 16:03:52 -0800929
930 private float mScrollPos;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800931
Daniel Sandler388f6792010-03-02 14:08:08 -0500932 Params mParams;
933 State mState;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800934
Romain Guy13c2e7b2010-03-10 19:45:00 -0800935 AllApps3D mAllApps;
936 boolean mInitialize;
937
Daniel Sandler388f6792010-03-02 14:08:08 -0500938 class BaseAlloc {
939 Allocation mAlloc;
940 Type mType;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800941
Daniel Sandler388f6792010-03-02 14:08:08 -0500942 void save() {
943 mAlloc.data(this);
944 }
945 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800946
Daniel Sandler388f6792010-03-02 14:08:08 -0500947 private boolean checkClickOK() {
Romain Guy13c2e7b2010-03-10 19:45:00 -0800948 return (Math.abs(mAllApps.mVelocity) < 0.4f) &&
Romain Guy6a42cf32010-03-12 16:03:52 -0800949 (Math.abs(mScrollPos - Math.round(mScrollPos)) < 0.4f);
Daniel Sandler388f6792010-03-02 14:08:08 -0500950 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800951
952 void pause() {
953 mRS.contextBindRootScript(null);
954 }
955
956 void resume() {
957 mRS.contextBindRootScript(mScript);
958 }
959
Daniel Sandler388f6792010-03-02 14:08:08 -0500960 class Params extends BaseAlloc {
961 Params() {
962 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
963 mAlloc = Allocation.createTyped(mRS, mType);
964 save();
965 }
966 public int bubbleWidth;
967 public int bubbleHeight;
968 public int bubbleBitmapWidth;
969 public int bubbleBitmapHeight;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800970
Daniel Sandler388f6792010-03-02 14:08:08 -0500971 public int homeButtonWidth;
972 public int homeButtonHeight;
973 public int homeButtonTextureWidth;
974 public int homeButtonTextureHeight;
975 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800976
Daniel Sandler388f6792010-03-02 14:08:08 -0500977 class State extends BaseAlloc {
978 public float newPositionX;
979 public int newTouchDown;
980 public float flingVelocity;
981 public int iconCount;
982 public int selectedIconIndex = -1;
983 public int selectedIconTexture;
984 public float zoomTarget;
985 public int homeButtonId;
986 public float targetPos;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800987
Daniel Sandler388f6792010-03-02 14:08:08 -0500988 State() {
989 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
990 mAlloc = Allocation.createTyped(mRS, mType);
991 save();
992 }
993 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800994
Romain Guy13c2e7b2010-03-10 19:45:00 -0800995 public RolloRS(AllApps3D allApps) {
996 mAllApps = allApps;
Daniel Sandler388f6792010-03-02 14:08:08 -0500997 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800998
Daniel Sandler388f6792010-03-02 14:08:08 -0500999 public void init(Resources res, int width, int height) {
1000 mRes = res;
1001 mWidth = width;
1002 mHeight = height;
1003 initProgramVertex();
1004 initProgramFragment();
1005 initProgramStore();
1006 initGl();
1007 initData();
Romain Guy13c2e7b2010-03-10 19:45:00 -08001008 initTouchState(width, height);
Daniel Sandler388f6792010-03-02 14:08:08 -05001009 initRs();
1010 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001011
Daniel Sandler388f6792010-03-02 14:08:08 -05001012 public void initMesh() {
1013 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001014
Daniel Sandler388f6792010-03-02 14:08:08 -05001015 for (int ct=0; ct < 16; ct++) {
1016 float pos = (1.f / (16.f - 1)) * ct;
1017 tm.addVertex(0.0f, pos);
1018 tm.addVertex(1.0f, pos);
1019 }
1020 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
1021 tm.addTriangle(ct, ct+1, ct+2);
1022 tm.addTriangle(ct+1, ct+3, ct+2);
1023 }
1024 mMesh = tm.create();
1025 mMesh.setName("SMCell");
1026 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001027
Daniel Sandler388f6792010-03-02 14:08:08 -05001028 void resize(int w, int h) {
1029 mPVA.setupProjectionNormalized(w, h);
1030 mWidth = w;
1031 mHeight = h;
1032 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001033
Daniel Sandler388f6792010-03-02 14:08:08 -05001034 private void initProgramVertex() {
1035 mPVA = new ProgramVertex.MatrixAllocation(mRS);
1036 resize(mWidth, mHeight);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001037
Daniel Sandler388f6792010-03-02 14:08:08 -05001038 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
1039 pvb.setTextureMatrixEnable(true);
1040 mPV = pvb.create();
1041 mPV.setName("PV");
1042 mPV.bindAllocation(mPVA);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001043
Daniel Sandler388f6792010-03-02 14:08:08 -05001044 Element.Builder eb = new Element.Builder(mRS);
1045 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
1046 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
1047 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "BendPos");
1048 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
1049 Element e = eb.create();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001050
Daniel Sandler388f6792010-03-02 14:08:08 -05001051 mUniformAlloc = Allocation.createSized(mRS, e, 1);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001052
Daniel Sandler388f6792010-03-02 14:08:08 -05001053 initMesh();
1054 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
Romain Guy060b5c82010-03-04 10:07:38 -08001055 String t = "void main() {\n" +
1056 // Animation
1057 " float ani = UNI_Position.z;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001058
Romain Guy060b5c82010-03-04 10:07:38 -08001059 " float bendY1 = UNI_BendPos.x;\n" +
1060 " float bendY2 = UNI_BendPos.y;\n" +
1061 " float bendAngle = 47.0 * (3.14 / 180.0);\n" +
1062 " float bendDistance = bendY1 * 0.4;\n" +
1063 " float distanceDimLevel = 0.6;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001064
Romain Guy060b5c82010-03-04 10:07:38 -08001065 " float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
1066 " float aDy = cos(bendAngle);\n" +
1067 " float aDz = sin(bendAngle);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001068
Romain Guy060b5c82010-03-04 10:07:38 -08001069 " float scale = (2.0 / 480.0);\n" +
1070 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
1071 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
1072 " float y = 0.0;\n" +
1073 " float z = 0.0;\n" +
1074 " float lum = 1.0;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001075
Romain Guy060b5c82010-03-04 10:07:38 -08001076 " float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
1077 " y += cv * aDy;\n" +
1078 " z += -cv * aDz;\n" +
1079 " cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" + // curve range
1080 " lum += cv / bendDistance * distanceDimLevel;\n" +
1081 " y += cv * cos(cv * bendStep);\n" +
1082 " z += cv * sin(cv * bendStep);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001083
Romain Guy060b5c82010-03-04 10:07:38 -08001084 " cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
1085 " y += cv * aDy;\n" +
1086 " z += cv * aDz;\n" +
1087 " cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
1088 " lum -= cv / bendDistance * distanceDimLevel;\n" +
1089 " y += cv * cos(cv * bendStep);\n" +
1090 " z += cv * sin(cv * bendStep);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001091
Romain Guy060b5c82010-03-04 10:07:38 -08001092 " y += clamp(ys, bendY1, bendY2);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001093
Romain Guy060b5c82010-03-04 10:07:38 -08001094 " vec4 pos;\n" +
1095 " pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
1096 " pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
1097 " pos.z = z * UNI_ScaleOffset.x;\n" +
1098 " pos.w = 1.0;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001099
Romain Guy060b5c82010-03-04 10:07:38 -08001100 " pos.x *= 1.0 + ani * 4.0;\n" +
1101 " pos.y *= 1.0 + ani * 4.0;\n" +
1102 " pos.z -= ani * 1.5;\n" +
1103 " lum *= 1.0 - ani;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001104
Romain Guy060b5c82010-03-04 10:07:38 -08001105 " gl_Position = UNI_MVP * pos;\n" +
1106 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
1107 " varTex0.xy = ATTRIB_position;\n" +
1108 " varTex0.y = 1.0 - varTex0.y;\n" +
1109 " varTex0.zw = vec2(0.0, 0.0);\n" +
1110 "}\n";
Daniel Sandler388f6792010-03-02 14:08:08 -05001111 sb.setShader(t);
1112 sb.addConstant(mUniformAlloc.getType());
1113 sb.addInput(mMesh.getVertexType(0).getElement());
1114 mPVCurve = sb.create();
1115 mPVCurve.setName("PVCurve");
1116 mPVCurve.bindAllocation(mPVA);
1117 mPVCurve.bindConstants(mUniformAlloc, 1);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001118
Daniel Sandler388f6792010-03-02 14:08:08 -05001119 mRS.contextBindProgramVertex(mPV);
1120 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001121
Daniel Sandler388f6792010-03-02 14:08:08 -05001122 private void initProgramFragment() {
1123 Sampler.Builder sb = new Sampler.Builder(mRS);
1124 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
1125 sb.setMag(Sampler.Value.NEAREST);
1126 sb.setWrapS(Sampler.Value.CLAMP);
1127 sb.setWrapT(Sampler.Value.CLAMP);
1128 Sampler linear = sb.create();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001129
Daniel Sandler388f6792010-03-02 14:08:08 -05001130 sb.setMin(Sampler.Value.NEAREST);
1131 sb.setMag(Sampler.Value.NEAREST);
1132 Sampler nearest = sb.create();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001133
Daniel Sandler388f6792010-03-02 14:08:08 -05001134 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
1135 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1136 ProgramFragment.Builder.Format.RGBA, 0);
1137 mPFTexMip = bf.create();
1138 mPFTexMip.setName("PFTexMip");
1139 mPFTexMip.bindSampler(linear, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001140
Daniel Sandler388f6792010-03-02 14:08:08 -05001141 mPFTexNearest = bf.create();
1142 mPFTexNearest.setName("PFTexNearest");
1143 mPFTexNearest.bindSampler(nearest, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001144
Daniel Sandler388f6792010-03-02 14:08:08 -05001145 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1146 ProgramFragment.Builder.Format.ALPHA, 0);
1147 mPFTexMipAlpha = bf.create();
1148 mPFTexMipAlpha.setName("PFTexMipAlpha");
1149 mPFTexMipAlpha.bindSampler(linear, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001150
Daniel Sandler388f6792010-03-02 14:08:08 -05001151 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001152
Daniel Sandler388f6792010-03-02 14:08:08 -05001153 private void initProgramStore() {
1154 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1155 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
1156 bs.setColorMask(true,true,true,false);
1157 bs.setDitherEnable(true);
1158 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1159 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1160 mPSIcons = bs.create();
1161 mPSIcons.setName("PSIcons");
1162 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001163
Daniel Sandler388f6792010-03-02 14:08:08 -05001164 private void initGl() {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001165 mTouchXBorders = new int[mAllApps.mColumnsPerPage + 1];
1166 mTouchYBorders = new int[mAllApps.mRowsPerPage + 1];
Daniel Sandler388f6792010-03-02 14:08:08 -05001167 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001168
Daniel Sandler388f6792010-03-02 14:08:08 -05001169 private void initData() {
1170 mParams = new Params();
1171 mState = new State();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001172
Romain Guy13c2e7b2010-03-10 19:45:00 -08001173 final Utilities.BubbleText bubble = new Utilities.BubbleText(mAllApps.getContext());
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001174
Daniel Sandler388f6792010-03-02 14:08:08 -05001175 mParams.bubbleWidth = bubble.getBubbleWidth();
1176 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1177 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1178 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001179
Daniel Sandler388f6792010-03-02 14:08:08 -05001180 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1181 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1182 mHomeButtonNormal.uploadToTexture(0);
1183 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1184 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1185 mHomeButtonFocused.uploadToTexture(0);
1186 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1187 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1188 mHomeButtonPressed.uploadToTexture(0);
1189 mParams.homeButtonWidth = 76;
1190 mParams.homeButtonHeight = 68;
1191 mParams.homeButtonTextureWidth = 128;
1192 mParams.homeButtonTextureHeight = 128;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001193
Daniel Sandler388f6792010-03-02 14:08:08 -05001194 mState.homeButtonId = mHomeButtonNormal.getID();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001195
Daniel Sandler388f6792010-03-02 14:08:08 -05001196 mParams.save();
1197 mState.save();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001198
Daniel Sandler388f6792010-03-02 14:08:08 -05001199 mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1200 Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1201 mSelectionCanvas = new Canvas(mSelectionBitmap);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001202
Daniel Sandler388f6792010-03-02 14:08:08 -05001203 setApps(null);
1204 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001205
Daniel Sandler388f6792010-03-02 14:08:08 -05001206 private void initRs() {
1207 ScriptC.Builder sb = new ScriptC.Builder(mRS);
1208 sb.setScript(mRes, R.raw.allapps);
1209 sb.setRoot(true);
Romain Guy13c2e7b2010-03-10 19:45:00 -08001210 sb.addDefines(mAllApps.mDefines);
Daniel Sandler388f6792010-03-02 14:08:08 -05001211 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1212 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
1213 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
1214 mInvokeMove = sb.addInvokable("move");
1215 mInvokeFling = sb.addInvokable("fling");
1216 mInvokeMoveTo = sb.addInvokable("moveTo");
1217 mInvokeResetWAR = sb.addInvokable("resetHWWar");
1218 mInvokeSetZoom = sb.addInvokable("setZoom");
1219 mScript = sb.create();
1220 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1221 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1222 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1223 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1224 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
1225 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001226
Daniel Sandler388f6792010-03-02 14:08:08 -05001227 mRS.contextBindRootScript(mScript);
1228 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001229
Daniel Sandler388f6792010-03-02 14:08:08 -05001230 void dirtyCheck() {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001231 if (mAllApps.mZoomDirty) {
1232 setZoom(mAllApps.mNextZoom, mAllApps.mAnimateNextZoom);
Daniel Sandler388f6792010-03-02 14:08:08 -05001233 }
1234 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001235
Romain Guy060b5c82010-03-04 10:07:38 -08001236 @SuppressWarnings({"ConstantConditions"})
Daniel Sandler388f6792010-03-02 14:08:08 -05001237 private void setApps(ArrayList<ApplicationInfo> list) {
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001238 mRollo.pause();
Daniel Sandler388f6792010-03-02 14:08:08 -05001239 final int count = list != null ? list.size() : 0;
1240 int allocCount = count;
1241 if (allocCount < 1) {
1242 allocCount = 1;
1243 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001244
Daniel Sandler388f6792010-03-02 14:08:08 -05001245 mIcons = new Allocation[count];
1246 mIconIds = new int[allocCount];
1247 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001248
Daniel Sandler388f6792010-03-02 14:08:08 -05001249 mLabels = new Allocation[count];
1250 mLabelIds = new int[allocCount];
1251 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001252
Daniel Sandler388f6792010-03-02 14:08:08 -05001253 mState.iconCount = count;
1254 for (int i=0; i < mState.iconCount; i++) {
1255 createAppIconAllocations(i, list.get(i));
1256 }
1257 for (int i=0; i < mState.iconCount; i++) {
1258 uploadAppIcon(i, list.get(i));
1259 }
1260 saveAppsList();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001261 mRollo.resume();
Daniel Sandler388f6792010-03-02 14:08:08 -05001262 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001263
Daniel Sandler388f6792010-03-02 14:08:08 -05001264 private void setZoom(float zoom, boolean animate) {
Romain Guyc16fea72010-03-12 17:17:56 -08001265 if (animate) {
1266 mRollo.clearSelectedIcon();
1267 mRollo.setHomeSelected(SELECTED_NONE);
1268 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001269 if (zoom > 0.001f) {
1270 mRollo.mState.zoomTarget = zoom;
1271 } else {
1272 mRollo.mState.zoomTarget = 0;
1273 }
1274 mRollo.mState.save();
1275 if (!animate) {
1276 mRollo.mInvokeSetZoom.execute();
1277 }
1278 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001279
Daniel Sandler388f6792010-03-02 14:08:08 -05001280 private void createAppIconAllocations(int index, ApplicationInfo item) {
1281 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001282 Element.RGBA_8888(mRS), false);
Daniel Sandler388f6792010-03-02 14:08:08 -05001283 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001284 Element.A_8(mRS), false);
Daniel Sandler388f6792010-03-02 14:08:08 -05001285 mIconIds[index] = mIcons[index].getID();
1286 mLabelIds[index] = mLabels[index].getID();
1287 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001288
Daniel Sandler388f6792010-03-02 14:08:08 -05001289 private void uploadAppIcon(int index, ApplicationInfo item) {
1290 if (mIconIds[index] != mIcons[index].getID()) {
1291 throw new IllegalStateException("uploadAppIcon index=" + index
1292 + " mIcons[index].getID=" + mIcons[index].getID()
1293 + " mIconsIds[index]=" + mIconIds[index]
1294 + " item=" + item);
1295 }
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001296 mIcons[index].uploadToTexture(true, 0);
1297 mLabels[index].uploadToTexture(true, 0);
Daniel Sandler388f6792010-03-02 14:08:08 -05001298 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001299
Daniel Sandler388f6792010-03-02 14:08:08 -05001300 /**
1301 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1302 * fill in the values and call saveAppsList().
1303 */
1304 private void reallocAppsList(int count) {
1305 Allocation[] icons = new Allocation[count];
1306 int[] iconIds = new int[count];
1307 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001308
Daniel Sandler388f6792010-03-02 14:08:08 -05001309 Allocation[] labels = new Allocation[count];
1310 int[] labelIds = new int[count];
1311 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001312
Daniel Sandler388f6792010-03-02 14:08:08 -05001313 final int oldCount = mRollo.mState.iconCount;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001314
Daniel Sandler388f6792010-03-02 14:08:08 -05001315 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1316 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1317 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1318 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001319
Daniel Sandler388f6792010-03-02 14:08:08 -05001320 mIcons = icons;
1321 mIconIds = iconIds;
1322 mLabels = labels;
1323 mLabelIds = labelIds;
1324 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001325
Daniel Sandler388f6792010-03-02 14:08:08 -05001326 /**
1327 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1328 */
1329 private void addApp(int index, ApplicationInfo item) {
1330 final int count = mState.iconCount - index;
1331 final int dest = index + 1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001332
Daniel Sandler388f6792010-03-02 14:08:08 -05001333 System.arraycopy(mIcons, index, mIcons, dest, count);
1334 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1335 System.arraycopy(mLabels, index, mLabels, dest, count);
1336 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001337
Daniel Sandler388f6792010-03-02 14:08:08 -05001338 createAppIconAllocations(index, item);
1339 uploadAppIcon(index, item);
1340 mRollo.mState.iconCount++;
1341 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001342
Daniel Sandler388f6792010-03-02 14:08:08 -05001343 /**
1344 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1345 */
1346 private void removeApp(int index) {
1347 final int count = mState.iconCount - index - 1;
1348 final int src = index + 1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001349
Daniel Sandler388f6792010-03-02 14:08:08 -05001350 System.arraycopy(mIcons, src, mIcons, index, count);
1351 System.arraycopy(mIconIds, src, mIconIds, index, count);
1352 System.arraycopy(mLabels, src, mLabels, index, count);
1353 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001354
Daniel Sandler388f6792010-03-02 14:08:08 -05001355 mRollo.mState.iconCount--;
1356 final int last = mState.iconCount;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001357
Daniel Sandler388f6792010-03-02 14:08:08 -05001358 mIcons[last] = null;
1359 mIconIds[last] = 0;
1360 mLabels[last] = null;
1361 mLabelIds[last] = 0;
1362 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001363
Daniel Sandler388f6792010-03-02 14:08:08 -05001364 /**
1365 * Send the apps list structures to RS.
1366 */
1367 private void saveAppsList() {
1368 // WTF: how could mScript be not null but mAllocIconIds null b/2460740.
1369 if (mScript != null && mAllocIconIds != null) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001370 mAllocIconIds.data(mIconIds);
1371 mAllocLabelIds.data(mLabelIds);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001372
Daniel Sandler388f6792010-03-02 14:08:08 -05001373 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1374 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001375
Daniel Sandler388f6792010-03-02 14:08:08 -05001376 mState.save();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001377
Daniel Sandler388f6792010-03-02 14:08:08 -05001378 // Note: mScript may be null if we haven't initialized it yet.
1379 // In that case, this is a no-op.
1380 if (mInvokeResetWAR != null) {
1381 mInvokeResetWAR.execute();
1382 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001383 }
1384 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001385
Romain Guy13c2e7b2010-03-10 19:45:00 -08001386 void initTouchState(int width, int height) {
1387 boolean isPortrait = width < height;
1388
Romain Guy060b5c82010-03-04 10:07:38 -08001389 // TODO: Put this in a config file/define
1390 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE_PORTRAIT;
1391 if (!isPortrait) cellHeight -= 12;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001392 int centerY = (int) (mAllApps.getHeight() * (isPortrait ? 0.5f : 0.47f));
Romain Guy060b5c82010-03-04 10:07:38 -08001393 if (!isPortrait) centerY += cellHeight / 2;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001394 int half = (int) Math.floor((mAllApps.mRowsPerPage + 1) / 2);
Romain Guy060b5c82010-03-04 10:07:38 -08001395 int end = mTouchYBorders.length - (half + 1);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001396
Romain Guy060b5c82010-03-04 10:07:38 -08001397 for (int i = -half; i <= end; i++) {
1398 mTouchYBorders[i + half] = centerY + i * cellHeight;
1399 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001400
Romain Guy060b5c82010-03-04 10:07:38 -08001401 int x = 0;
1402 // TODO: Put this in a config file/define
1403 int columnWidth = 120;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001404 for (int i = 0; i < mAllApps.mColumnsPerPage + 1; i++) {
Romain Guy060b5c82010-03-04 10:07:38 -08001405 mTouchXBorders[i] = x;
1406 x += columnWidth;
1407 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001408 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001409
Daniel Sandler388f6792010-03-02 14:08:08 -05001410 void fling() {
1411 mInvokeFling.execute();
1412 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001413
Daniel Sandler388f6792010-03-02 14:08:08 -05001414 void move() {
1415 mInvokeMove.execute();
1416 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001417
Daniel Sandler388f6792010-03-02 14:08:08 -05001418 void moveTo(float row) {
1419 mState.targetPos = row;
1420 mState.save();
1421 mInvokeMoveTo.execute();
1422 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001423
Romain Guy6a42cf32010-03-12 16:03:52 -08001424 int chooseTappedIcon(int x, int y) {
1425 float pos = mScrollPos;
1426
Daniel Sandler388f6792010-03-02 14:08:08 -05001427 // Adjust for scroll position if not zero.
1428 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001429
Daniel Sandler388f6792010-03-02 14:08:08 -05001430 int col = -1;
1431 int row = -1;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001432 final int columnsCount = mAllApps.mColumnsPerPage;
Romain Guy060b5c82010-03-04 10:07:38 -08001433 for (int i=0; i< columnsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001434 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1435 col = i;
1436 break;
1437 }
1438 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001439 final int rowsCount = mAllApps.mRowsPerPage;
Romain Guy060b5c82010-03-04 10:07:38 -08001440 for (int i=0; i< rowsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001441 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1442 row = i;
1443 break;
1444 }
1445 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001446
Daniel Sandler388f6792010-03-02 14:08:08 -05001447 if (row < 0 || col < 0) {
1448 return -1;
1449 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001450
Romain Guy6a42cf32010-03-12 16:03:52 -08001451 int index = (((int) pos) * columnsCount) + (row * columnsCount) + col;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001452
Daniel Sandler388f6792010-03-02 14:08:08 -05001453 if (index >= mState.iconCount) {
1454 return -1;
1455 } else {
1456 return index;
1457 }
1458 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001459
Daniel Sandler388f6792010-03-02 14:08:08 -05001460 /**
1461 * You need to call save() on mState on your own after calling this.
1462 *
1463 * @return the index of the icon that was selected.
1464 */
Romain Guy6a42cf32010-03-12 16:03:52 -08001465 int selectIcon(int x, int y, int pressed) {
1466 final int index = chooseTappedIcon(x, y);
Daniel Sandler388f6792010-03-02 14:08:08 -05001467 selectIcon(index, pressed);
1468 return index;
1469 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001470
Daniel Sandler388f6792010-03-02 14:08:08 -05001471 /**
1472 * Select the icon at the given index.
1473 *
1474 * @param index The index.
1475 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
1476 */
1477 void selectIcon(int index, int pressed) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001478 final ArrayList<ApplicationInfo> appsList = mAllApps.mAllAppsList;
1479 if (appsList == null || index < 0 || index >= appsList.size()) {
Romain Guyc16fea72010-03-12 17:17:56 -08001480 if (mAllApps != null) {
1481 mAllApps.mRestoreFocusIndex = index;
1482 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001483 mState.selectedIconIndex = -1;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001484 if (mAllApps.mLastSelection == SELECTION_ICONS) {
1485 mAllApps.mLastSelection = SELECTION_NONE;
Daniel Sandler388f6792010-03-02 14:08:08 -05001486 }
1487 } else {
1488 if (pressed == SELECTED_FOCUSED) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001489 mAllApps.mLastSelection = SELECTION_ICONS;
Daniel Sandler388f6792010-03-02 14:08:08 -05001490 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001491
Daniel Sandler388f6792010-03-02 14:08:08 -05001492 int prev = mState.selectedIconIndex;
1493 mState.selectedIconIndex = index;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001494
Romain Guy13c2e7b2010-03-10 19:45:00 -08001495 ApplicationInfo info = appsList.get(index);
Daniel Sandler388f6792010-03-02 14:08:08 -05001496 Bitmap selectionBitmap = mSelectionBitmap;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001497
Daniel Sandler388f6792010-03-02 14:08:08 -05001498 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1499 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1500 pressed == SELECTED_PRESSED, info.iconBitmap);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001501
Daniel Sandler388f6792010-03-02 14:08:08 -05001502 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
1503 Element.RGBA_8888(mRS), false);
1504 mSelectedIcon.uploadToTexture(0);
1505 mState.selectedIconTexture = mSelectedIcon.getID();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001506
Daniel Sandler388f6792010-03-02 14:08:08 -05001507 if (prev != index) {
1508 if (info.title != null && info.title.length() > 0) {
1509 //setContentDescription(info.title);
Romain Guy13c2e7b2010-03-10 19:45:00 -08001510 mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Daniel Sandler388f6792010-03-02 14:08:08 -05001511 }
1512 }
1513 }
1514 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001515
Daniel Sandler388f6792010-03-02 14:08:08 -05001516 /**
1517 * You need to call save() on mState on your own after calling this.
1518 */
1519 void clearSelectedIcon() {
1520 mState.selectedIconIndex = -1;
1521 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001522
Daniel Sandler388f6792010-03-02 14:08:08 -05001523 void setHomeSelected(int mode) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001524 final int prev = mAllApps.mLastSelection;
Daniel Sandler388f6792010-03-02 14:08:08 -05001525 switch (mode) {
1526 case SELECTED_NONE:
1527 mState.homeButtonId = mHomeButtonNormal.getID();
1528 break;
1529 case SELECTED_FOCUSED:
Romain Guy13c2e7b2010-03-10 19:45:00 -08001530 mAllApps.mLastSelection = SELECTION_HOME;
Daniel Sandler388f6792010-03-02 14:08:08 -05001531 mState.homeButtonId = mHomeButtonFocused.getID();
1532 if (prev != SELECTION_HOME) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001533 mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Daniel Sandler388f6792010-03-02 14:08:08 -05001534 }
1535 break;
1536 case SELECTED_PRESSED:
1537 mState.homeButtonId = mHomeButtonPressed.getID();
1538 break;
1539 }
1540 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001541
Daniel Sandler388f6792010-03-02 14:08:08 -05001542 public void dumpState() {
1543 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1544 Log.d(TAG, "mRollo.mHeight=" + mHeight);
Romain Guy060b5c82010-03-04 10:07:38 -08001545 Log.d(TAG, "mRollo.mIcons=" + Arrays.toString(mIcons));
Daniel Sandler388f6792010-03-02 14:08:08 -05001546 if (mIcons != null) {
1547 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1548 }
1549 if (mIconIds != null) {
1550 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1551 }
1552 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1553 if (mLabelIds != null) {
1554 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1555 }
1556 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1557 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1558 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
1559 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1560 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1561 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1562 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1563 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1564 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1565 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1566 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1567 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1568 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1569 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1570 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1571 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1572 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1573 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1574 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1575 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1576 }
1577 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001578
Daniel Sandler388f6792010-03-02 14:08:08 -05001579 public void dumpState() {
1580 Log.d(TAG, "mRS=" + mRS);
1581 Log.d(TAG, "mRollo=" + mRollo);
1582 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1583 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1584 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1585 Log.d(TAG, "mLastSelection=" + mLastSelection);
1586 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1587 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1588 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1589 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1590 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1591 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1592 Log.d(TAG, "mZoom=" + mZoom);
Romain Guy6a42cf32010-03-12 16:03:52 -08001593 Log.d(TAG, "mScrollPos=" + mRollo.mScrollPos);
Daniel Sandler388f6792010-03-02 14:08:08 -05001594 Log.d(TAG, "mVelocity=" + mVelocity);
1595 Log.d(TAG, "mMessageProc=" + mMessageProc);
1596 if (mRollo != null) {
1597 mRollo.dumpState();
1598 }
1599 if (mRS != null) {
1600 mRS.contextDump(0);
1601 }
1602 }
1603}
1604
1605