blob: d977885682869296c544e35cb1f314bdcf333b08 [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;
123 private float mPosX;
124 private float mVelocity;
125 private AAMessage mMessageProc;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800126
Romain Guy060b5c82010-03-04 10:07:38 -0800127 private int mColumnsPerPage;
128 private int mRowsPerPage;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800129 private boolean mSurrendered;
130
Romain Guy060b5c82010-03-04 10:07:38 -0800131 @SuppressWarnings({"UnusedDeclaration"})
Daniel Sandler388f6792010-03-02 14:08:08 -0500132 static class Defines {
133 public static final int ALLOC_PARAMS = 0;
134 public static final int ALLOC_STATE = 1;
135 public static final int ALLOC_ICON_IDS = 3;
136 public static final int ALLOC_LABEL_IDS = 4;
137 public static final int ALLOC_VP_CONSTANTS = 5;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800138
Romain Guy060b5c82010-03-04 10:07:38 -0800139 public static final int COLUMNS_PER_PAGE_PORTRAIT = 4;
140 public static final int ROWS_PER_PAGE_PORTRAIT = 4;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800141
Romain Guy060b5c82010-03-04 10:07:38 -0800142 public static final int COLUMNS_PER_PAGE_LANDSCAPE = 6;
143 public static final int ROWS_PER_PAGE_LANDSCAPE = 3;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800144
Daniel Sandler388f6792010-03-02 14:08:08 -0500145 public static final int ICON_WIDTH_PX = 64;
146 public static final int ICON_TEXTURE_WIDTH_PX = 74;
147 public static final int SELECTION_TEXTURE_WIDTH_PX = 74 + 20;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800148
Daniel Sandler388f6792010-03-02 14:08:08 -0500149 public static final int ICON_HEIGHT_PX = 64;
150 public static final int ICON_TEXTURE_HEIGHT_PX = 74;
151 public static final int SELECTION_TEXTURE_HEIGHT_PX = 74 + 20;
152 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800153
Daniel Sandler388f6792010-03-02 14:08:08 -0500154 public AllApps3D(Context context, AttributeSet attrs) {
155 super(context, attrs);
156 setFocusable(true);
157 setSoundEffectsEnabled(false);
158 getHolder().setFormat(PixelFormat.TRANSLUCENT);
159 final ViewConfiguration config = ViewConfiguration.get(context);
160 mSlop = config.getScaledTouchSlop();
161 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800162
Daniel Sandler388f6792010-03-02 14:08:08 -0500163 setOnClickListener(this);
164 setOnLongClickListener(this);
165 setZOrderOnTop(true);
166 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800167
Romain Guy13c2e7b2010-03-10 19:45:00 -0800168 if (mRS == null) {
169 mRS = createRenderScript(true);
170 } else {
171 createRenderScript(mRS);
172 }
173
Romain Guy060b5c82010-03-04 10:07:38 -0800174 final DisplayMetrics metrics = getResources().getDisplayMetrics();
175 final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;
176 mColumnsPerPage = isPortrait ? Defines.COLUMNS_PER_PAGE_PORTRAIT :
177 Defines.COLUMNS_PER_PAGE_LANDSCAPE;
178 mRowsPerPage = isPortrait ? Defines.ROWS_PER_PAGE_PORTRAIT :
179 Defines.ROWS_PER_PAGE_LANDSCAPE;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800180
181 if (mRollo != null) {
182 mRollo.mAllApps = this;
183 mRollo.mRes = getResources();
184 mRollo.mInitialize = true;
185 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500186 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800187
Romain Guy060b5c82010-03-04 10:07:38 -0800188 @SuppressWarnings({"UnusedDeclaration"})
189 public AllApps3D(Context context, AttributeSet attrs, int defStyle) {
190 this(context, attrs);
191 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800192
Romain Guy13c2e7b2010-03-10 19:45:00 -0800193 public void surrender() {
194 mRS.contextSetSurface(0, 0, null);
195 mRS.mMessageCallback = null;
196 mSurrendered = true;
197 }
198
Daniel Sandler388f6792010-03-02 14:08:08 -0500199 /**
200 * Note that this implementation prohibits this view from ever being reattached.
201 */
202 @Override
203 protected void onDetachedFromWindow() {
Daniel Sandler388f6792010-03-02 14:08:08 -0500204 mRS.mMessageCallback = null;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800205 if (!mSurrendered) {
206 destroyRenderScript();
207 mRS = null;
208 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500209 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800210
Daniel Sandler388f6792010-03-02 14:08:08 -0500211 /**
212 * If you have an attached click listener, View always plays the click sound!?!?
213 * Deal with sound effects by hand.
214 */
215 public void reallyPlaySoundEffect(int sound) {
216 boolean old = isSoundEffectsEnabled();
217 setSoundEffectsEnabled(true);
218 playSoundEffect(sound);
219 setSoundEffectsEnabled(old);
220 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800221
Daniel Sandler388f6792010-03-02 14:08:08 -0500222 public void setLauncher(Launcher launcher) {
223 mLauncher = launcher;
224 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800225
Daniel Sandler388f6792010-03-02 14:08:08 -0500226 @Override
227 public void surfaceDestroyed(SurfaceHolder holder) {
228 super.surfaceDestroyed(holder);
229 // Without this, we leak mMessageCallback which leaks the context.
Romain Guy13c2e7b2010-03-10 19:45:00 -0800230 if (!mSurrendered) {
231 mRS.mMessageCallback = null;
232 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500233 // We may lose any callbacks that are pending, so make sure that we re-sync that
234 // on the next surfaceChanged.
235 mZoomDirty = true;
236 mHaveSurface = false;
237 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800238
Daniel Sandler388f6792010-03-02 14:08:08 -0500239 @Override
240 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
241 //long startTime = SystemClock.uptimeMillis();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800242
Daniel Sandler388f6792010-03-02 14:08:08 -0500243 super.surfaceChanged(holder, format, w, h);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800244
Romain Guy13c2e7b2010-03-10 19:45:00 -0800245 if (mSurrendered) return;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800246
Daniel Sandler388f6792010-03-02 14:08:08 -0500247 mHaveSurface = true;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800248
Daniel Sandler388f6792010-03-02 14:08:08 -0500249 if (mRollo == null) {
Romain Guy13c2e7b2010-03-10 19:45:00 -0800250 mRollo = new RolloRS(this);
Daniel Sandler388f6792010-03-02 14:08:08 -0500251 mRollo.init(getResources(), w, h);
252 if (mAllAppsList != null) {
253 mRollo.setApps(mAllAppsList);
254 }
255 if (mShouldGainFocus) {
256 gainFocus();
257 mShouldGainFocus = false;
258 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800259 } else if (mRollo.mInitialize) {
260 mRollo.initGl();
261 mRollo.initTouchState(w, h);
262 mRollo.mInitialize = false;
Daniel Sandler388f6792010-03-02 14:08:08 -0500263 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800264
Daniel Sandler388f6792010-03-02 14:08:08 -0500265 mRollo.dirtyCheck();
266 mRollo.resize(w, h);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800267
Daniel Sandler388f6792010-03-02 14:08:08 -0500268 if (mRS != null) {
269 mRS.mMessageCallback = mMessageProc = new AAMessage();
270 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800271
Daniel Sandler388f6792010-03-02 14:08:08 -0500272 if (mRollo.mUniformAlloc != null) {
273 float tf[] = new float[] {72.f, 72.f,
274 120.f, 120.f, 0.f, 0.f,
275 120.f, 680.f,
276 (2.f / 480.f), 0, -((float)w / 2) - 0.25f, -380.25f};
277 if (w > h) {
278 tf[6] = 40.f;
279 tf[7] = h - 40.f;
280 tf[9] = 1.f;
281 tf[10] = -((float)w / 2) - 0.25f;
282 tf[11] = -((float)h / 2) - 0.25f;
283 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800284
Daniel Sandler388f6792010-03-02 14:08:08 -0500285 mRollo.mUniformAlloc.data(tf);
286 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800287
Daniel Sandler388f6792010-03-02 14:08:08 -0500288 //long endTime = SystemClock.uptimeMillis();
289 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
290 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800291
Daniel Sandler388f6792010-03-02 14:08:08 -0500292 @Override
293 public void onWindowFocusChanged(boolean hasWindowFocus) {
294 super.onWindowFocusChanged(hasWindowFocus);
Romain Guy13c2e7b2010-03-10 19:45:00 -0800295
296 if (mSurrendered) return;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800297
Daniel Sandler388f6792010-03-02 14:08:08 -0500298 if (mArrowNavigation) {
299 if (!hasWindowFocus) {
300 // Clear selection when we lose window focus
301 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
302 mRollo.setHomeSelected(SELECTED_NONE);
303 mRollo.clearSelectedIcon();
304 mRollo.mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -0800305 } else {
Daniel Sandler388f6792010-03-02 14:08:08 -0500306 if (mRollo.mState.iconCount > 0) {
307 if (mLastSelection == SELECTION_ICONS) {
308 int selection = mLastSelectedIcon;
309 final int firstIcon = Math.round(mPosX) *
Romain Guy060b5c82010-03-04 10:07:38 -0800310 mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500311 if (selection < 0 || // No selection
312 selection < firstIcon || // off the top of the screen
313 selection >= mRollo.mState.iconCount || // past last icon
314 selection >= firstIcon + // past last icon on screen
Romain Guy060b5c82010-03-04 10:07:38 -0800315 (mColumnsPerPage * mRowsPerPage)) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500316 selection = firstIcon;
317 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800318
Daniel Sandler388f6792010-03-02 14:08:08 -0500319 // Select the first icon when we gain window focus
320 mRollo.selectIcon(selection, SELECTED_FOCUSED);
321 mRollo.mState.save();
322 } else if (mLastSelection == SELECTION_HOME) {
323 mRollo.setHomeSelected(SELECTED_FOCUSED);
324 mRollo.mState.save();
325 }
326 }
327 }
328 }
329 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800330
Daniel Sandler388f6792010-03-02 14:08:08 -0500331 @Override
332 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
333 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800334
Romain Guy13c2e7b2010-03-10 19:45:00 -0800335 if (!isVisible() || mSurrendered) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500336 return;
337 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800338
Daniel Sandler388f6792010-03-02 14:08:08 -0500339 if (gainFocus) {
340 if (mRollo != null) {
341 gainFocus();
342 } else {
343 mShouldGainFocus = true;
344 }
345 } else {
346 if (mRollo != null) {
347 if (mArrowNavigation) {
348 // Clear selection when we lose focus
349 mRollo.clearSelectedIcon();
350 mRollo.setHomeSelected(SELECTED_NONE);
351 mRollo.mState.save();
352 mArrowNavigation = false;
353 }
354 } else {
355 mShouldGainFocus = false;
356 }
357 }
358 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800359
Daniel Sandler388f6792010-03-02 14:08:08 -0500360 private void gainFocus() {
361 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
362 // Select the first icon when we gain keyboard focus
363 mArrowNavigation = true;
Romain Guy060b5c82010-03-04 10:07:38 -0800364 mRollo.selectIcon(Math.round(mPosX) * mColumnsPerPage,
Daniel Sandler388f6792010-03-02 14:08:08 -0500365 SELECTED_FOCUSED);
366 mRollo.mState.save();
367 }
368 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800369
Daniel Sandler388f6792010-03-02 14:08:08 -0500370 @Override
371 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800372
Daniel Sandler388f6792010-03-02 14:08:08 -0500373 boolean handled = false;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800374
Daniel Sandler388f6792010-03-02 14:08:08 -0500375 if (!isVisible()) {
376 return false;
377 }
378 final int iconCount = mRollo.mState.iconCount;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800379
Daniel Sandler388f6792010-03-02 14:08:08 -0500380 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
381 if (mArrowNavigation) {
382 if (mLastSelection == SELECTION_HOME) {
383 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
384 mLauncher.closeAllApps(true);
385 } else {
386 int whichApp = mRollo.mState.selectedIconIndex;
387 if (whichApp >= 0) {
388 ApplicationInfo app = mAllAppsList.get(whichApp);
389 mLauncher.startActivitySafely(app.intent);
390 handled = true;
391 }
392 }
393 }
394 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800395
Daniel Sandler388f6792010-03-02 14:08:08 -0500396 if (iconCount > 0) {
397 mArrowNavigation = true;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800398
Daniel Sandler388f6792010-03-02 14:08:08 -0500399 int currentSelection = mRollo.mState.selectedIconIndex;
400 int currentTopRow = Math.round(mPosX);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800401
Romain Guy060b5c82010-03-04 10:07:38 -0800402 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE_PORTRAIT-1
403 final int currentPageCol = currentSelection % mColumnsPerPage;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800404
Romain Guy060b5c82010-03-04 10:07:38 -0800405 // The row of the current selection, in the range 0..ROWS_PER_PAGE_PORTRAIT-1
406 final int currentPageRow = (currentSelection - (currentTopRow* mColumnsPerPage))
407 / mRowsPerPage;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800408
Daniel Sandler388f6792010-03-02 14:08:08 -0500409 int newSelection = currentSelection;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800410
Daniel Sandler388f6792010-03-02 14:08:08 -0500411 switch (keyCode) {
412 case KeyEvent.KEYCODE_DPAD_UP:
413 if (mLastSelection == SELECTION_HOME) {
414 mRollo.setHomeSelected(SELECTED_NONE);
Romain Guy060b5c82010-03-04 10:07:38 -0800415 int lastRowCount = iconCount % mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500416 if (lastRowCount == 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800417 lastRowCount = mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500418 }
Romain Guy060b5c82010-03-04 10:07:38 -0800419 newSelection = iconCount - lastRowCount + (mColumnsPerPage / 2);
Daniel Sandler388f6792010-03-02 14:08:08 -0500420 if (newSelection >= iconCount) {
421 newSelection = iconCount-1;
422 }
Romain Guy060b5c82010-03-04 10:07:38 -0800423 int target = (newSelection / mColumnsPerPage)
424 - (mRowsPerPage - 1);
Daniel Sandler388f6792010-03-02 14:08:08 -0500425 if (target < 0) {
426 target = 0;
427 }
428 if (currentTopRow != target) {
429 mRollo.moveTo(target);
430 }
431 } else {
432 if (currentPageRow > 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800433 newSelection = currentSelection - mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500434 } else if (currentTopRow > 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800435 newSelection = currentSelection - mColumnsPerPage;
436 mRollo.moveTo(newSelection / mColumnsPerPage);
Daniel Sandler388f6792010-03-02 14:08:08 -0500437 } else if (currentPageRow != 0) {
Romain Guy060b5c82010-03-04 10:07:38 -0800438 newSelection = currentTopRow * mRowsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500439 }
440 }
441 handled = true;
442 break;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800443
Daniel Sandler388f6792010-03-02 14:08:08 -0500444 case KeyEvent.KEYCODE_DPAD_DOWN: {
Romain Guy060b5c82010-03-04 10:07:38 -0800445 final int rowCount = iconCount / mColumnsPerPage
446 + (iconCount % mColumnsPerPage == 0 ? 0 : 1);
447 final int currentRow = currentSelection / mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500448 if (mLastSelection != SELECTION_HOME) {
449 if (currentRow < rowCount-1) {
450 mRollo.setHomeSelected(SELECTED_NONE);
451 if (currentSelection < 0) {
452 newSelection = 0;
453 } else {
Romain Guy060b5c82010-03-04 10:07:38 -0800454 newSelection = currentSelection + mColumnsPerPage;
Daniel Sandler388f6792010-03-02 14:08:08 -0500455 }
456 if (newSelection >= iconCount) {
457 // Go from D to G in this arrangement:
458 // A B C D
459 // E F G
460 newSelection = iconCount - 1;
461 }
Romain Guy060b5c82010-03-04 10:07:38 -0800462 if (currentPageRow >= mRowsPerPage - 1) {
463 mRollo.moveTo((newSelection / mColumnsPerPage) -
464 mRowsPerPage + 1);
Daniel Sandler388f6792010-03-02 14:08:08 -0500465 }
466 } else {
467 newSelection = -1;
468 mRollo.setHomeSelected(SELECTED_FOCUSED);
469 }
470 }
471 handled = true;
472 break;
473 }
474 case KeyEvent.KEYCODE_DPAD_LEFT:
475 if (mLastSelection != SELECTION_HOME) {
476 if (currentPageCol > 0) {
477 newSelection = currentSelection - 1;
478 }
479 }
480 handled = true;
481 break;
482 case KeyEvent.KEYCODE_DPAD_RIGHT:
483 if (mLastSelection != SELECTION_HOME) {
Romain Guy060b5c82010-03-04 10:07:38 -0800484 if ((currentPageCol < mColumnsPerPage - 1) &&
Daniel Sandler388f6792010-03-02 14:08:08 -0500485 (currentSelection < iconCount - 1)) {
486 newSelection = currentSelection + 1;
487 }
488 }
489 handled = true;
490 break;
491 }
492 if (newSelection != currentSelection) {
493 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
494 mRollo.mState.save();
495 }
496 }
497 return handled;
498 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800499
Daniel Sandler388f6792010-03-02 14:08:08 -0500500 @Override
501 public boolean onTouchEvent(MotionEvent ev)
502 {
503 mArrowNavigation = false;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800504
Daniel Sandler388f6792010-03-02 14:08:08 -0500505 if (!isVisible()) {
506 return true;
507 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800508
Daniel Sandler388f6792010-03-02 14:08:08 -0500509 if (mLocks != 0) {
510 return true;
511 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800512
Daniel Sandler388f6792010-03-02 14:08:08 -0500513 super.onTouchEvent(ev);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800514
Daniel Sandler388f6792010-03-02 14:08:08 -0500515 int x = (int)ev.getX();
516 int y = (int)ev.getY();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800517
Romain Guyce115852010-03-04 12:15:37 -0800518 final boolean isPortrait = getWidth() < getHeight();
Daniel Sandler388f6792010-03-02 14:08:08 -0500519 int action = ev.getAction();
520 switch (action) {
521 case MotionEvent.ACTION_DOWN:
Romain Guyce115852010-03-04 12:15:37 -0800522 if ((isPortrait && y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) ||
523 (!isPortrait && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500524 mTouchTracking = TRACKING_HOME;
525 mRollo.setHomeSelected(SELECTED_PRESSED);
526 mRollo.mState.save();
527 mCurrentIconIndex = -1;
528 } else {
529 mTouchTracking = TRACKING_FLING;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800530
Daniel Sandler388f6792010-03-02 14:08:08 -0500531 mMotionDownRawX = (int)ev.getRawX();
532 mMotionDownRawY = (int)ev.getRawY();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800533
Daniel Sandler388f6792010-03-02 14:08:08 -0500534 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
535 mRollo.mState.newTouchDown = 1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800536
Daniel Sandler388f6792010-03-02 14:08:08 -0500537 if (!mRollo.checkClickOK()) {
538 mRollo.clearSelectedIcon();
539 } else {
540 mDownIconIndex = mCurrentIconIndex
541 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
542 if (mDownIconIndex < 0) {
543 // if nothing was selected, no long press.
544 cancelLongPress();
545 }
546 }
547 mRollo.mState.save();
548 mRollo.move();
549 mVelocityTracker = VelocityTracker.obtain();
550 mVelocityTracker.addMovement(ev);
551 mStartedScrolling = false;
552 }
553 break;
554 case MotionEvent.ACTION_MOVE:
555 case MotionEvent.ACTION_OUTSIDE:
556 if (mTouchTracking == TRACKING_HOME) {
Romain Guyce115852010-03-04 12:15:37 -0800557 mRollo.setHomeSelected((isPortrait &&
558 y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) || (!isPortrait
559 && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])
Daniel Sandler388f6792010-03-02 14:08:08 -0500560 ? SELECTED_PRESSED : SELECTED_NONE);
561 mRollo.mState.save();
562 } else if (mTouchTracking == TRACKING_FLING) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500563 int rawY = (int)ev.getRawY();
564 int slop;
565 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800566
Daniel Sandler388f6792010-03-02 14:08:08 -0500567 if (!mStartedScrolling && slop < mSlop) {
568 // don't update anything so when we do start scrolling
569 // below, we get the right delta.
570 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
571 if (mDownIconIndex != mCurrentIconIndex) {
572 // If a different icon is selected, don't allow it to be picked up.
573 // This handles off-axis dragging.
574 cancelLongPress();
575 mCurrentIconIndex = -1;
576 }
577 } else {
578 if (!mStartedScrolling) {
579 cancelLongPress();
580 mCurrentIconIndex = -1;
581 }
582 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
583 mRollo.mState.newTouchDown = 1;
584 mRollo.move();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800585
Daniel Sandler388f6792010-03-02 14:08:08 -0500586 mStartedScrolling = true;
587 mRollo.clearSelectedIcon();
588 mVelocityTracker.addMovement(ev);
589 mRollo.mState.save();
590 }
591 }
592 break;
593 case MotionEvent.ACTION_UP:
594 case MotionEvent.ACTION_CANCEL:
595 if (mTouchTracking == TRACKING_HOME) {
596 if (action == MotionEvent.ACTION_UP) {
Romain Guyce115852010-03-04 12:15:37 -0800597 if ((isPortrait && y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) ||
598 (!isPortrait && x > mRollo.mTouchXBorders[mRollo.mTouchXBorders.length-1])) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500599 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
600 mLauncher.closeAllApps(true);
601 }
602 mRollo.setHomeSelected(SELECTED_NONE);
603 mRollo.mState.save();
604 }
605 mCurrentIconIndex = -1;
606 } else if (mTouchTracking == TRACKING_FLING) {
607 mRollo.mState.newTouchDown = 0;
608 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800609
Daniel Sandler388f6792010-03-02 14:08:08 -0500610 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
611 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
612 mRollo.clearSelectedIcon();
613 mRollo.mState.save();
614 mRollo.fling();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800615
Daniel Sandler388f6792010-03-02 14:08:08 -0500616 if (mVelocityTracker != null) {
617 mVelocityTracker.recycle();
618 mVelocityTracker = null;
619 }
620 }
621 mTouchTracking = TRACKING_NONE;
622 break;
623 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800624
Daniel Sandler388f6792010-03-02 14:08:08 -0500625 return true;
626 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800627
Daniel Sandler388f6792010-03-02 14:08:08 -0500628 public void onClick(View v) {
629 if (mLocks != 0 || !isVisible()) {
630 return;
631 }
632 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
633 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
634 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
635 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
636 mLauncher.startActivitySafely(app.intent);
637 }
638 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800639
Daniel Sandler388f6792010-03-02 14:08:08 -0500640 public boolean onLongClick(View v) {
641 if (mLocks != 0 || !isVisible()) {
642 return true;
643 }
644 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
645 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
646 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800647
Daniel Sandler388f6792010-03-02 14:08:08 -0500648 Bitmap bmp = app.iconBitmap;
649 final int w = bmp.getWidth();
650 final int h = bmp.getHeight();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800651
Daniel Sandler388f6792010-03-02 14:08:08 -0500652 // We don't really have an accurate location to use. This will do.
653 int screenX = mMotionDownRawX - (w / 2);
654 int screenY = mMotionDownRawY - h;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800655
Daniel Sandler388f6792010-03-02 14:08:08 -0500656 mDragController.startDrag(bmp, screenX, screenY,
657 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800658
Daniel Sandler388f6792010-03-02 14:08:08 -0500659 mLauncher.closeAllApps(true);
660 }
661 return true;
662 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800663
Daniel Sandler388f6792010-03-02 14:08:08 -0500664 @Override
665 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
666 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
667 if (!isVisible()) {
668 return false;
669 }
670 String text = null;
671 int index;
672 int count = mAllAppsList.size() + 1; // +1 is home
673 int pos = -1;
674 switch (mLastSelection) {
675 case SELECTION_ICONS:
676 index = mRollo.mState.selectedIconIndex;
677 if (index >= 0) {
678 ApplicationInfo info = mAllAppsList.get(index);
679 if (info.title != null) {
680 text = info.title.toString();
681 pos = index;
682 }
683 }
684 break;
685 case SELECTION_HOME:
686 text = getContext().getString(R.string.all_apps_home_button_label);
687 pos = count;
688 break;
689 }
690 if (text != null) {
691 event.setEnabled(true);
692 event.getText().add(text);
693 //event.setContentDescription(text);
694 event.setItemCount(count);
695 event.setCurrentItemIndex(pos);
696 }
697 }
698 return false;
699 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800700
Daniel Sandler388f6792010-03-02 14:08:08 -0500701 public void setDragController(DragController dragger) {
702 mDragController = dragger;
703 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800704
Daniel Sandler388f6792010-03-02 14:08:08 -0500705 public void onDropCompleted(View target, boolean success) {
706 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800707
Daniel Sandler388f6792010-03-02 14:08:08 -0500708 /**
709 * Zoom to the specifed level.
710 *
711 * @param zoom [0..1] 0 is hidden, 1 is open
712 */
713 public void zoom(float zoom, boolean animate) {
714 cancelLongPress();
715 mNextZoom = zoom;
716 mAnimateNextZoom = animate;
717 // if we do setZoom while we don't have a surface, we won't
718 // get the callbacks that actually set mZoom.
719 if (mRollo == null || !mHaveSurface) {
720 mZoomDirty = true;
721 mZoom = zoom;
Daniel Sandler388f6792010-03-02 14:08:08 -0500722 } else {
723 mRollo.setZoom(zoom, animate);
724 }
725 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800726
Daniel Sandler388f6792010-03-02 14:08:08 -0500727 public boolean isVisible() {
728 return mZoom > 0.001f;
729 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800730
Daniel Sandler388f6792010-03-02 14:08:08 -0500731 public boolean isOpaque() {
732 return mZoom > 0.999f;
733 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800734
Daniel Sandler388f6792010-03-02 14:08:08 -0500735 public void setApps(ArrayList<ApplicationInfo> list) {
736 if (mRS == null) {
737 // We've been removed from the window. Don't bother with all this.
738 return;
739 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800740
741 boolean reload = false;
742 if (mAllAppsList == null) {
743 reload = true;
744 } else if (list.size() != mAllAppsList.size()) {
745 reload = true;
746 } else {
747 final int count = list.size();
748 for (int i = 0; i < count; i++) {
749 if (list.get(i) != mAllAppsList.get(i)) {
750 reload = true;
751 break;
752 }
753 }
754 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800755
Daniel Sandler388f6792010-03-02 14:08:08 -0500756 mAllAppsList = list;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800757 if (mRollo != null && reload) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500758 mRollo.setApps(list);
759 }
760 mLocks &= ~LOCK_ICONS_PENDING;
761 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800762
Daniel Sandler388f6792010-03-02 14:08:08 -0500763 public void addApps(ArrayList<ApplicationInfo> list) {
764 if (mAllAppsList == null) {
765 // Not done loading yet. We'll find out about it later.
766 return;
767 }
768 if (mRS == null) {
769 // We've been removed from the window. Don't bother with all this.
770 return;
771 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800772
Daniel Sandler388f6792010-03-02 14:08:08 -0500773 final int N = list.size();
774 if (mRollo != null) {
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800775 mRollo.pause();
Daniel Sandler388f6792010-03-02 14:08:08 -0500776 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
777 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800778
Daniel Sandler388f6792010-03-02 14:08:08 -0500779 for (int i=0; i<N; i++) {
780 final ApplicationInfo item = list.get(i);
781 int index = Collections.binarySearch(mAllAppsList, item,
782 LauncherModel.APP_NAME_COMPARATOR);
783 if (index < 0) {
784 index = -(index+1);
785 }
786 mAllAppsList.add(index, item);
787 if (mRollo != null) {
788 mRollo.addApp(index, item);
789 }
790 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800791
Daniel Sandler388f6792010-03-02 14:08:08 -0500792 if (mRollo != null) {
793 mRollo.saveAppsList();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800794 mRollo.resume();
Daniel Sandler388f6792010-03-02 14:08:08 -0500795 }
796 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800797
Daniel Sandler388f6792010-03-02 14:08:08 -0500798 public void removeApps(ArrayList<ApplicationInfo> list) {
799 if (mAllAppsList == null) {
800 // Not done loading yet. We'll find out about it later.
801 return;
802 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800803
804 if (mRollo != null) {
805 mRollo.pause();
806 }
Daniel Sandler388f6792010-03-02 14:08:08 -0500807 final int N = list.size();
808 for (int i=0; i<N; i++) {
809 final ApplicationInfo item = list.get(i);
810 int index = findAppByComponent(mAllAppsList, item);
811 if (index >= 0) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500812 mAllAppsList.remove(index);
813 if (mRollo != null) {
814 mRollo.removeApp(index);
815 }
816 } else {
817 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
818 // Try to recover. This should keep us from crashing for now.
819 }
820 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800821
Daniel Sandler388f6792010-03-02 14:08:08 -0500822 if (mRollo != null) {
823 mRollo.saveAppsList();
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800824 mRollo.resume();
Daniel Sandler388f6792010-03-02 14:08:08 -0500825 }
826 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800827
Joe Onorato64e6be72010-03-05 15:05:52 -0500828 public void updateApps(ArrayList<ApplicationInfo> list) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500829 // Just remove and add, because they may need to be re-sorted.
830 removeApps(list);
831 addApps(list);
832 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800833
Daniel Sandler388f6792010-03-02 14:08:08 -0500834 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
835 ComponentName component = item.intent.getComponent();
836 final int N = list.size();
837 for (int i=0; i<N; i++) {
838 ApplicationInfo x = list.get(i);
839 if (x.intent.getComponent().equals(component)) {
840 return i;
841 }
842 }
843 return -1;
844 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800845
Romain Guy060b5c82010-03-04 10:07:38 -0800846 /*
Daniel Sandler388f6792010-03-02 14:08:08 -0500847 private static int countPages(int iconCount) {
Romain Guy060b5c82010-03-04 10:07:38 -0800848 int iconsPerPage = getColumnsCount() * Defines.ROWS_PER_PAGE_PORTRAIT;
Daniel Sandler388f6792010-03-02 14:08:08 -0500849 int pages = iconCount / iconsPerPage;
850 if (pages*iconsPerPage != iconCount) {
851 pages++;
852 }
853 return pages;
854 }
Romain Guy060b5c82010-03-04 10:07:38 -0800855 */
Daniel Sandler388f6792010-03-02 14:08:08 -0500856
857 class AAMessage extends RenderScript.RSMessage {
858 public void run() {
859 mPosX = ((float)mData[0]) / (1 << 16);
860 mVelocity = ((float)mData[1]) / (1 << 16);
861 mZoom = ((float)mData[2]) / (1 << 16);
862 mZoomDirty = false;
863 }
864 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800865
Romain Guy13c2e7b2010-03-10 19:45:00 -0800866 public static class RolloRS {
Daniel Sandler388f6792010-03-02 14:08:08 -0500867 // Allocations ======
868 private int mWidth;
869 private int mHeight;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800870
Daniel Sandler388f6792010-03-02 14:08:08 -0500871 private Resources mRes;
872 private Script mScript;
873 private Script.Invokable mInvokeMove;
874 private Script.Invokable mInvokeMoveTo;
875 private Script.Invokable mInvokeFling;
876 private Script.Invokable mInvokeResetWAR;
877 private Script.Invokable mInvokeSetZoom;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800878
Daniel Sandler388f6792010-03-02 14:08:08 -0500879 private ProgramStore mPSIcons;
880 private ProgramFragment mPFTexMip;
881 private ProgramFragment mPFTexMipAlpha;
882 private ProgramFragment mPFTexNearest;
883 private ProgramVertex mPV;
884 private ProgramVertex mPVCurve;
885 private SimpleMesh mMesh;
886 private ProgramVertex.MatrixAllocation mPVA;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800887
Daniel Sandler388f6792010-03-02 14:08:08 -0500888 private Allocation mUniformAlloc;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800889
Daniel Sandler388f6792010-03-02 14:08:08 -0500890 private Allocation mHomeButtonNormal;
891 private Allocation mHomeButtonFocused;
892 private Allocation mHomeButtonPressed;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800893
Daniel Sandler388f6792010-03-02 14:08:08 -0500894 private Allocation[] mIcons;
895 private int[] mIconIds;
896 private Allocation mAllocIconIds;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800897
Daniel Sandler388f6792010-03-02 14:08:08 -0500898 private Allocation[] mLabels;
899 private int[] mLabelIds;
900 private Allocation mAllocLabelIds;
901 private Allocation mSelectedIcon;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800902
Daniel Sandler388f6792010-03-02 14:08:08 -0500903 private int[] mTouchYBorders;
904 private int[] mTouchXBorders;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800905
Daniel Sandler388f6792010-03-02 14:08:08 -0500906 private Bitmap mSelectionBitmap;
907 private Canvas mSelectionCanvas;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800908
Daniel Sandler388f6792010-03-02 14:08:08 -0500909 Params mParams;
910 State mState;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800911
Romain Guy13c2e7b2010-03-10 19:45:00 -0800912 AllApps3D mAllApps;
913 boolean mInitialize;
914
Daniel Sandler388f6792010-03-02 14:08:08 -0500915 class BaseAlloc {
916 Allocation mAlloc;
917 Type mType;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800918
Daniel Sandler388f6792010-03-02 14:08:08 -0500919 void save() {
920 mAlloc.data(this);
921 }
922 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800923
Daniel Sandler388f6792010-03-02 14:08:08 -0500924 private boolean checkClickOK() {
Romain Guy13c2e7b2010-03-10 19:45:00 -0800925 return (Math.abs(mAllApps.mVelocity) < 0.4f) &&
926 (Math.abs(mAllApps.mPosX - Math.round(mAllApps.mPosX)) < 0.4f);
Daniel Sandler388f6792010-03-02 14:08:08 -0500927 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800928
929 void pause() {
930 mRS.contextBindRootScript(null);
931 }
932
933 void resume() {
934 mRS.contextBindRootScript(mScript);
935 }
936
Daniel Sandler388f6792010-03-02 14:08:08 -0500937 class Params extends BaseAlloc {
938 Params() {
939 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
940 mAlloc = Allocation.createTyped(mRS, mType);
941 save();
942 }
943 public int bubbleWidth;
944 public int bubbleHeight;
945 public int bubbleBitmapWidth;
946 public int bubbleBitmapHeight;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800947
Daniel Sandler388f6792010-03-02 14:08:08 -0500948 public int homeButtonWidth;
949 public int homeButtonHeight;
950 public int homeButtonTextureWidth;
951 public int homeButtonTextureHeight;
952 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800953
Daniel Sandler388f6792010-03-02 14:08:08 -0500954 class State extends BaseAlloc {
955 public float newPositionX;
956 public int newTouchDown;
957 public float flingVelocity;
958 public int iconCount;
959 public int selectedIconIndex = -1;
960 public int selectedIconTexture;
961 public float zoomTarget;
962 public int homeButtonId;
963 public float targetPos;
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800964
Daniel Sandler388f6792010-03-02 14:08:08 -0500965 State() {
966 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
967 mAlloc = Allocation.createTyped(mRS, mType);
968 save();
969 }
970 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800971
Romain Guy13c2e7b2010-03-10 19:45:00 -0800972 public RolloRS(AllApps3D allApps) {
973 mAllApps = allApps;
Daniel Sandler388f6792010-03-02 14:08:08 -0500974 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800975
Daniel Sandler388f6792010-03-02 14:08:08 -0500976 public void init(Resources res, int width, int height) {
977 mRes = res;
978 mWidth = width;
979 mHeight = height;
980 initProgramVertex();
981 initProgramFragment();
982 initProgramStore();
983 initGl();
984 initData();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800985 initTouchState(width, height);
Daniel Sandler388f6792010-03-02 14:08:08 -0500986 initRs();
987 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800988
Daniel Sandler388f6792010-03-02 14:08:08 -0500989 public void initMesh() {
990 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -0800991
Daniel Sandler388f6792010-03-02 14:08:08 -0500992 for (int ct=0; ct < 16; ct++) {
993 float pos = (1.f / (16.f - 1)) * ct;
994 tm.addVertex(0.0f, pos);
995 tm.addVertex(1.0f, pos);
996 }
997 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
998 tm.addTriangle(ct, ct+1, ct+2);
999 tm.addTriangle(ct+1, ct+3, ct+2);
1000 }
1001 mMesh = tm.create();
1002 mMesh.setName("SMCell");
1003 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001004
Daniel Sandler388f6792010-03-02 14:08:08 -05001005 void resize(int w, int h) {
1006 mPVA.setupProjectionNormalized(w, h);
1007 mWidth = w;
1008 mHeight = h;
1009 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001010
Daniel Sandler388f6792010-03-02 14:08:08 -05001011 private void initProgramVertex() {
1012 mPVA = new ProgramVertex.MatrixAllocation(mRS);
1013 resize(mWidth, mHeight);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001014
Daniel Sandler388f6792010-03-02 14:08:08 -05001015 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
1016 pvb.setTextureMatrixEnable(true);
1017 mPV = pvb.create();
1018 mPV.setName("PV");
1019 mPV.bindAllocation(mPVA);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001020
Daniel Sandler388f6792010-03-02 14:08:08 -05001021 Element.Builder eb = new Element.Builder(mRS);
1022 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
1023 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
1024 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "BendPos");
1025 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
1026 Element e = eb.create();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001027
Daniel Sandler388f6792010-03-02 14:08:08 -05001028 mUniformAlloc = Allocation.createSized(mRS, e, 1);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001029
Daniel Sandler388f6792010-03-02 14:08:08 -05001030 initMesh();
1031 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
Romain Guy060b5c82010-03-04 10:07:38 -08001032 String t = "void main() {\n" +
1033 // Animation
1034 " float ani = UNI_Position.z;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001035
Romain Guy060b5c82010-03-04 10:07:38 -08001036 " float bendY1 = UNI_BendPos.x;\n" +
1037 " float bendY2 = UNI_BendPos.y;\n" +
1038 " float bendAngle = 47.0 * (3.14 / 180.0);\n" +
1039 " float bendDistance = bendY1 * 0.4;\n" +
1040 " float distanceDimLevel = 0.6;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001041
Romain Guy060b5c82010-03-04 10:07:38 -08001042 " float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
1043 " float aDy = cos(bendAngle);\n" +
1044 " float aDz = sin(bendAngle);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001045
Romain Guy060b5c82010-03-04 10:07:38 -08001046 " float scale = (2.0 / 480.0);\n" +
1047 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
1048 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
1049 " float y = 0.0;\n" +
1050 " float z = 0.0;\n" +
1051 " float lum = 1.0;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001052
Romain Guy060b5c82010-03-04 10:07:38 -08001053 " float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
1054 " y += cv * aDy;\n" +
1055 " z += -cv * aDz;\n" +
1056 " cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" + // curve range
1057 " lum += cv / bendDistance * distanceDimLevel;\n" +
1058 " y += cv * cos(cv * bendStep);\n" +
1059 " z += cv * sin(cv * bendStep);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001060
Romain Guy060b5c82010-03-04 10:07:38 -08001061 " cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
1062 " y += cv * aDy;\n" +
1063 " z += cv * aDz;\n" +
1064 " cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
1065 " lum -= cv / bendDistance * distanceDimLevel;\n" +
1066 " y += cv * cos(cv * bendStep);\n" +
1067 " z += cv * sin(cv * bendStep);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001068
Romain Guy060b5c82010-03-04 10:07:38 -08001069 " y += clamp(ys, bendY1, bendY2);\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001070
Romain Guy060b5c82010-03-04 10:07:38 -08001071 " vec4 pos;\n" +
1072 " pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
1073 " pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
1074 " pos.z = z * UNI_ScaleOffset.x;\n" +
1075 " pos.w = 1.0;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001076
Romain Guy060b5c82010-03-04 10:07:38 -08001077 " pos.x *= 1.0 + ani * 4.0;\n" +
1078 " pos.y *= 1.0 + ani * 4.0;\n" +
1079 " pos.z -= ani * 1.5;\n" +
1080 " lum *= 1.0 - ani;\n" +
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001081
Romain Guy060b5c82010-03-04 10:07:38 -08001082 " gl_Position = UNI_MVP * pos;\n" +
1083 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
1084 " varTex0.xy = ATTRIB_position;\n" +
1085 " varTex0.y = 1.0 - varTex0.y;\n" +
1086 " varTex0.zw = vec2(0.0, 0.0);\n" +
1087 "}\n";
Daniel Sandler388f6792010-03-02 14:08:08 -05001088 sb.setShader(t);
1089 sb.addConstant(mUniformAlloc.getType());
1090 sb.addInput(mMesh.getVertexType(0).getElement());
1091 mPVCurve = sb.create();
1092 mPVCurve.setName("PVCurve");
1093 mPVCurve.bindAllocation(mPVA);
1094 mPVCurve.bindConstants(mUniformAlloc, 1);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001095
Daniel Sandler388f6792010-03-02 14:08:08 -05001096 mRS.contextBindProgramVertex(mPV);
1097 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001098
Daniel Sandler388f6792010-03-02 14:08:08 -05001099 private void initProgramFragment() {
1100 Sampler.Builder sb = new Sampler.Builder(mRS);
1101 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
1102 sb.setMag(Sampler.Value.NEAREST);
1103 sb.setWrapS(Sampler.Value.CLAMP);
1104 sb.setWrapT(Sampler.Value.CLAMP);
1105 Sampler linear = sb.create();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001106
Daniel Sandler388f6792010-03-02 14:08:08 -05001107 sb.setMin(Sampler.Value.NEAREST);
1108 sb.setMag(Sampler.Value.NEAREST);
1109 Sampler nearest = sb.create();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001110
Daniel Sandler388f6792010-03-02 14:08:08 -05001111 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
1112 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1113 ProgramFragment.Builder.Format.RGBA, 0);
1114 mPFTexMip = bf.create();
1115 mPFTexMip.setName("PFTexMip");
1116 mPFTexMip.bindSampler(linear, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001117
Daniel Sandler388f6792010-03-02 14:08:08 -05001118 mPFTexNearest = bf.create();
1119 mPFTexNearest.setName("PFTexNearest");
1120 mPFTexNearest.bindSampler(nearest, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001121
Daniel Sandler388f6792010-03-02 14:08:08 -05001122 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1123 ProgramFragment.Builder.Format.ALPHA, 0);
1124 mPFTexMipAlpha = bf.create();
1125 mPFTexMipAlpha.setName("PFTexMipAlpha");
1126 mPFTexMipAlpha.bindSampler(linear, 0);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001127
Daniel Sandler388f6792010-03-02 14:08:08 -05001128 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001129
Daniel Sandler388f6792010-03-02 14:08:08 -05001130 private void initProgramStore() {
1131 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1132 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
1133 bs.setColorMask(true,true,true,false);
1134 bs.setDitherEnable(true);
1135 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1136 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1137 mPSIcons = bs.create();
1138 mPSIcons.setName("PSIcons");
1139 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001140
Daniel Sandler388f6792010-03-02 14:08:08 -05001141 private void initGl() {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001142 mTouchXBorders = new int[mAllApps.mColumnsPerPage + 1];
1143 mTouchYBorders = new int[mAllApps.mRowsPerPage + 1];
Daniel Sandler388f6792010-03-02 14:08:08 -05001144 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001145
Daniel Sandler388f6792010-03-02 14:08:08 -05001146 private void initData() {
1147 mParams = new Params();
1148 mState = new State();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001149
Romain Guy13c2e7b2010-03-10 19:45:00 -08001150 final Utilities.BubbleText bubble = new Utilities.BubbleText(mAllApps.getContext());
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001151
Daniel Sandler388f6792010-03-02 14:08:08 -05001152 mParams.bubbleWidth = bubble.getBubbleWidth();
1153 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1154 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1155 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001156
Daniel Sandler388f6792010-03-02 14:08:08 -05001157 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1158 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1159 mHomeButtonNormal.uploadToTexture(0);
1160 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1161 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1162 mHomeButtonFocused.uploadToTexture(0);
1163 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1164 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1165 mHomeButtonPressed.uploadToTexture(0);
1166 mParams.homeButtonWidth = 76;
1167 mParams.homeButtonHeight = 68;
1168 mParams.homeButtonTextureWidth = 128;
1169 mParams.homeButtonTextureHeight = 128;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001170
Daniel Sandler388f6792010-03-02 14:08:08 -05001171 mState.homeButtonId = mHomeButtonNormal.getID();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001172
Daniel Sandler388f6792010-03-02 14:08:08 -05001173 mParams.save();
1174 mState.save();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001175
Daniel Sandler388f6792010-03-02 14:08:08 -05001176 mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1177 Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1178 mSelectionCanvas = new Canvas(mSelectionBitmap);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001179
Daniel Sandler388f6792010-03-02 14:08:08 -05001180 setApps(null);
1181 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001182
Daniel Sandler388f6792010-03-02 14:08:08 -05001183 private void initRs() {
1184 ScriptC.Builder sb = new ScriptC.Builder(mRS);
1185 sb.setScript(mRes, R.raw.allapps);
1186 sb.setRoot(true);
Romain Guy13c2e7b2010-03-10 19:45:00 -08001187 sb.addDefines(mAllApps.mDefines);
Daniel Sandler388f6792010-03-02 14:08:08 -05001188 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1189 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
1190 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
1191 mInvokeMove = sb.addInvokable("move");
1192 mInvokeFling = sb.addInvokable("fling");
1193 mInvokeMoveTo = sb.addInvokable("moveTo");
1194 mInvokeResetWAR = sb.addInvokable("resetHWWar");
1195 mInvokeSetZoom = sb.addInvokable("setZoom");
1196 mScript = sb.create();
1197 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1198 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1199 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1200 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1201 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
1202 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001203
Daniel Sandler388f6792010-03-02 14:08:08 -05001204 mRS.contextBindRootScript(mScript);
1205 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001206
Daniel Sandler388f6792010-03-02 14:08:08 -05001207 void dirtyCheck() {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001208 if (mAllApps.mZoomDirty) {
1209 setZoom(mAllApps.mNextZoom, mAllApps.mAnimateNextZoom);
Daniel Sandler388f6792010-03-02 14:08:08 -05001210 }
1211 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001212
Romain Guy060b5c82010-03-04 10:07:38 -08001213 @SuppressWarnings({"ConstantConditions"})
Daniel Sandler388f6792010-03-02 14:08:08 -05001214 private void setApps(ArrayList<ApplicationInfo> list) {
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001215 mRollo.pause();
Daniel Sandler388f6792010-03-02 14:08:08 -05001216 final int count = list != null ? list.size() : 0;
1217 int allocCount = count;
1218 if (allocCount < 1) {
1219 allocCount = 1;
1220 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001221
Daniel Sandler388f6792010-03-02 14:08:08 -05001222 mIcons = new Allocation[count];
1223 mIconIds = new int[allocCount];
1224 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001225
Daniel Sandler388f6792010-03-02 14:08:08 -05001226 mLabels = new Allocation[count];
1227 mLabelIds = new int[allocCount];
1228 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001229
Daniel Sandler388f6792010-03-02 14:08:08 -05001230 mState.iconCount = count;
1231 for (int i=0; i < mState.iconCount; i++) {
1232 createAppIconAllocations(i, list.get(i));
1233 }
1234 for (int i=0; i < mState.iconCount; i++) {
1235 uploadAppIcon(i, list.get(i));
1236 }
1237 saveAppsList();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001238 mRollo.resume();
Daniel Sandler388f6792010-03-02 14:08:08 -05001239 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001240
Daniel Sandler388f6792010-03-02 14:08:08 -05001241 private void setZoom(float zoom, boolean animate) {
1242 mRollo.clearSelectedIcon();
1243 mRollo.setHomeSelected(SELECTED_NONE);
1244 if (zoom > 0.001f) {
1245 mRollo.mState.zoomTarget = zoom;
1246 } else {
1247 mRollo.mState.zoomTarget = 0;
1248 }
1249 mRollo.mState.save();
1250 if (!animate) {
1251 mRollo.mInvokeSetZoom.execute();
1252 }
1253 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001254
Daniel Sandler388f6792010-03-02 14:08:08 -05001255 private void createAppIconAllocations(int index, ApplicationInfo item) {
1256 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001257 Element.RGBA_8888(mRS), false);
Daniel Sandler388f6792010-03-02 14:08:08 -05001258 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001259 Element.A_8(mRS), false);
Daniel Sandler388f6792010-03-02 14:08:08 -05001260 mIconIds[index] = mIcons[index].getID();
1261 mLabelIds[index] = mLabels[index].getID();
1262 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001263
Daniel Sandler388f6792010-03-02 14:08:08 -05001264 private void uploadAppIcon(int index, ApplicationInfo item) {
1265 if (mIconIds[index] != mIcons[index].getID()) {
1266 throw new IllegalStateException("uploadAppIcon index=" + index
1267 + " mIcons[index].getID=" + mIcons[index].getID()
1268 + " mIconsIds[index]=" + mIconIds[index]
1269 + " item=" + item);
1270 }
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001271 mIcons[index].uploadToTexture(true, 0);
1272 mLabels[index].uploadToTexture(true, 0);
Daniel Sandler388f6792010-03-02 14:08:08 -05001273 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001274
Daniel Sandler388f6792010-03-02 14:08:08 -05001275 /**
1276 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1277 * fill in the values and call saveAppsList().
1278 */
1279 private void reallocAppsList(int count) {
1280 Allocation[] icons = new Allocation[count];
1281 int[] iconIds = new int[count];
1282 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001283
Daniel Sandler388f6792010-03-02 14:08:08 -05001284 Allocation[] labels = new Allocation[count];
1285 int[] labelIds = new int[count];
1286 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001287
Daniel Sandler388f6792010-03-02 14:08:08 -05001288 final int oldCount = mRollo.mState.iconCount;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001289
Daniel Sandler388f6792010-03-02 14:08:08 -05001290 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1291 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1292 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1293 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001294
Daniel Sandler388f6792010-03-02 14:08:08 -05001295 mIcons = icons;
1296 mIconIds = iconIds;
1297 mLabels = labels;
1298 mLabelIds = labelIds;
1299 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001300
Daniel Sandler388f6792010-03-02 14:08:08 -05001301 /**
1302 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1303 */
1304 private void addApp(int index, ApplicationInfo item) {
1305 final int count = mState.iconCount - index;
1306 final int dest = index + 1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001307
Daniel Sandler388f6792010-03-02 14:08:08 -05001308 System.arraycopy(mIcons, index, mIcons, dest, count);
1309 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1310 System.arraycopy(mLabels, index, mLabels, dest, count);
1311 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001312
Daniel Sandler388f6792010-03-02 14:08:08 -05001313 createAppIconAllocations(index, item);
1314 uploadAppIcon(index, item);
1315 mRollo.mState.iconCount++;
1316 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001317
Daniel Sandler388f6792010-03-02 14:08:08 -05001318 /**
1319 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1320 */
1321 private void removeApp(int index) {
1322 final int count = mState.iconCount - index - 1;
1323 final int src = index + 1;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001324
Daniel Sandler388f6792010-03-02 14:08:08 -05001325 System.arraycopy(mIcons, src, mIcons, index, count);
1326 System.arraycopy(mIconIds, src, mIconIds, index, count);
1327 System.arraycopy(mLabels, src, mLabels, index, count);
1328 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001329
Daniel Sandler388f6792010-03-02 14:08:08 -05001330 mRollo.mState.iconCount--;
1331 final int last = mState.iconCount;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001332
Daniel Sandler388f6792010-03-02 14:08:08 -05001333 mIcons[last] = null;
1334 mIconIds[last] = 0;
1335 mLabels[last] = null;
1336 mLabelIds[last] = 0;
1337 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001338
Daniel Sandler388f6792010-03-02 14:08:08 -05001339 /**
1340 * Send the apps list structures to RS.
1341 */
1342 private void saveAppsList() {
1343 // WTF: how could mScript be not null but mAllocIconIds null b/2460740.
1344 if (mScript != null && mAllocIconIds != null) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001345 mAllocIconIds.data(mIconIds);
1346 mAllocLabelIds.data(mLabelIds);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001347
Daniel Sandler388f6792010-03-02 14:08:08 -05001348 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1349 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001350
Daniel Sandler388f6792010-03-02 14:08:08 -05001351 mState.save();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001352
Daniel Sandler388f6792010-03-02 14:08:08 -05001353 // Note: mScript may be null if we haven't initialized it yet.
1354 // In that case, this is a no-op.
1355 if (mInvokeResetWAR != null) {
1356 mInvokeResetWAR.execute();
1357 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001358 }
1359 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001360
Romain Guy13c2e7b2010-03-10 19:45:00 -08001361 void initTouchState(int width, int height) {
1362 boolean isPortrait = width < height;
1363
Romain Guy060b5c82010-03-04 10:07:38 -08001364 // TODO: Put this in a config file/define
1365 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE_PORTRAIT;
1366 if (!isPortrait) cellHeight -= 12;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001367 int centerY = (int) (mAllApps.getHeight() * (isPortrait ? 0.5f : 0.47f));
Romain Guy060b5c82010-03-04 10:07:38 -08001368 if (!isPortrait) centerY += cellHeight / 2;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001369 int half = (int) Math.floor((mAllApps.mRowsPerPage + 1) / 2);
Romain Guy060b5c82010-03-04 10:07:38 -08001370 int end = mTouchYBorders.length - (half + 1);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001371
Romain Guy060b5c82010-03-04 10:07:38 -08001372 for (int i = -half; i <= end; i++) {
1373 mTouchYBorders[i + half] = centerY + i * cellHeight;
1374 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001375
Romain Guy060b5c82010-03-04 10:07:38 -08001376 int x = 0;
1377 // TODO: Put this in a config file/define
1378 int columnWidth = 120;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001379 for (int i = 0; i < mAllApps.mColumnsPerPage + 1; i++) {
Romain Guy060b5c82010-03-04 10:07:38 -08001380 mTouchXBorders[i] = x;
1381 x += columnWidth;
1382 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001383 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001384
Daniel Sandler388f6792010-03-02 14:08:08 -05001385 void fling() {
1386 mInvokeFling.execute();
1387 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001388
Daniel Sandler388f6792010-03-02 14:08:08 -05001389 void move() {
1390 mInvokeMove.execute();
1391 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001392
Daniel Sandler388f6792010-03-02 14:08:08 -05001393 void moveTo(float row) {
1394 mState.targetPos = row;
1395 mState.save();
1396 mInvokeMoveTo.execute();
1397 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001398
Daniel Sandler388f6792010-03-02 14:08:08 -05001399 int chooseTappedIcon(int x, int y, float pos) {
1400 // Adjust for scroll position if not zero.
1401 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001402
Daniel Sandler388f6792010-03-02 14:08:08 -05001403 int col = -1;
1404 int row = -1;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001405 final int columnsCount = mAllApps.mColumnsPerPage;
Romain Guy060b5c82010-03-04 10:07:38 -08001406 for (int i=0; i< columnsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001407 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1408 col = i;
1409 break;
1410 }
1411 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001412 final int rowsCount = mAllApps.mRowsPerPage;
Romain Guy060b5c82010-03-04 10:07:38 -08001413 for (int i=0; i< rowsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001414 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1415 row = i;
1416 break;
1417 }
1418 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001419
Daniel Sandler388f6792010-03-02 14:08:08 -05001420 if (row < 0 || col < 0) {
1421 return -1;
1422 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001423
Romain Guy060b5c82010-03-04 10:07:38 -08001424 int index = (((int)pos) * columnsCount) + (row * columnsCount) + col;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001425
Daniel Sandler388f6792010-03-02 14:08:08 -05001426 if (index >= mState.iconCount) {
1427 return -1;
1428 } else {
1429 return index;
1430 }
1431 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001432
Daniel Sandler388f6792010-03-02 14:08:08 -05001433 /**
1434 * You need to call save() on mState on your own after calling this.
1435 *
1436 * @return the index of the icon that was selected.
1437 */
1438 int selectIcon(int x, int y, float pos, int pressed) {
1439 final int index = chooseTappedIcon(x, y, pos);
1440 selectIcon(index, pressed);
1441 return index;
1442 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001443
Daniel Sandler388f6792010-03-02 14:08:08 -05001444 /**
1445 * Select the icon at the given index.
1446 *
1447 * @param index The index.
1448 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
1449 */
1450 void selectIcon(int index, int pressed) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001451 final ArrayList<ApplicationInfo> appsList = mAllApps.mAllAppsList;
1452 if (appsList == null || index < 0 || index >= appsList.size()) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001453 mState.selectedIconIndex = -1;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001454 if (mAllApps.mLastSelection == SELECTION_ICONS) {
1455 mAllApps.mLastSelection = SELECTION_NONE;
Daniel Sandler388f6792010-03-02 14:08:08 -05001456 }
1457 } else {
1458 if (pressed == SELECTED_FOCUSED) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001459 mAllApps.mLastSelection = SELECTION_ICONS;
Daniel Sandler388f6792010-03-02 14:08:08 -05001460 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001461
Daniel Sandler388f6792010-03-02 14:08:08 -05001462 int prev = mState.selectedIconIndex;
1463 mState.selectedIconIndex = index;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001464
Romain Guy13c2e7b2010-03-10 19:45:00 -08001465 ApplicationInfo info = appsList.get(index);
Daniel Sandler388f6792010-03-02 14:08:08 -05001466 Bitmap selectionBitmap = mSelectionBitmap;
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001467
Daniel Sandler388f6792010-03-02 14:08:08 -05001468 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1469 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1470 pressed == SELECTED_PRESSED, info.iconBitmap);
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001471
Daniel Sandler388f6792010-03-02 14:08:08 -05001472 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
1473 Element.RGBA_8888(mRS), false);
1474 mSelectedIcon.uploadToTexture(0);
1475 mState.selectedIconTexture = mSelectedIcon.getID();
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001476
Daniel Sandler388f6792010-03-02 14:08:08 -05001477 if (prev != index) {
1478 if (info.title != null && info.title.length() > 0) {
1479 //setContentDescription(info.title);
Romain Guy13c2e7b2010-03-10 19:45:00 -08001480 mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Daniel Sandler388f6792010-03-02 14:08:08 -05001481 }
1482 }
1483 }
1484 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001485
Daniel Sandler388f6792010-03-02 14:08:08 -05001486 /**
1487 * You need to call save() on mState on your own after calling this.
1488 */
1489 void clearSelectedIcon() {
1490 mState.selectedIconIndex = -1;
1491 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001492
Daniel Sandler388f6792010-03-02 14:08:08 -05001493 void setHomeSelected(int mode) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001494 final int prev = mAllApps.mLastSelection;
Daniel Sandler388f6792010-03-02 14:08:08 -05001495 switch (mode) {
1496 case SELECTED_NONE:
1497 mState.homeButtonId = mHomeButtonNormal.getID();
1498 break;
1499 case SELECTED_FOCUSED:
Romain Guy13c2e7b2010-03-10 19:45:00 -08001500 mAllApps.mLastSelection = SELECTION_HOME;
Daniel Sandler388f6792010-03-02 14:08:08 -05001501 mState.homeButtonId = mHomeButtonFocused.getID();
1502 if (prev != SELECTION_HOME) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001503 mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Daniel Sandler388f6792010-03-02 14:08:08 -05001504 }
1505 break;
1506 case SELECTED_PRESSED:
1507 mState.homeButtonId = mHomeButtonPressed.getID();
1508 break;
1509 }
1510 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001511
Daniel Sandler388f6792010-03-02 14:08:08 -05001512 public void dumpState() {
1513 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1514 Log.d(TAG, "mRollo.mHeight=" + mHeight);
Romain Guy060b5c82010-03-04 10:07:38 -08001515 Log.d(TAG, "mRollo.mIcons=" + Arrays.toString(mIcons));
Daniel Sandler388f6792010-03-02 14:08:08 -05001516 if (mIcons != null) {
1517 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1518 }
1519 if (mIconIds != null) {
1520 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1521 }
1522 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1523 if (mLabelIds != null) {
1524 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1525 }
1526 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1527 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1528 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
1529 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1530 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1531 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1532 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1533 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1534 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1535 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1536 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1537 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1538 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1539 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1540 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1541 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1542 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1543 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1544 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1545 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1546 }
1547 }
Jason Samsdd8cd8b2010-03-11 12:38:48 -08001548
Daniel Sandler388f6792010-03-02 14:08:08 -05001549 public void dumpState() {
1550 Log.d(TAG, "mRS=" + mRS);
1551 Log.d(TAG, "mRollo=" + mRollo);
1552 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1553 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1554 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1555 Log.d(TAG, "mLastSelection=" + mLastSelection);
1556 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1557 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1558 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1559 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1560 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1561 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1562 Log.d(TAG, "mZoom=" + mZoom);
1563 Log.d(TAG, "mPosX=" + mPosX);
1564 Log.d(TAG, "mVelocity=" + mVelocity);
1565 Log.d(TAG, "mMessageProc=" + mMessageProc);
1566 if (mRollo != null) {
1567 mRollo.dumpState();
1568 }
1569 if (mRS != null) {
1570 mRS.contextDump(0);
1571 }
1572 }
1573}
1574
1575