blob: e59fa6cb191c55014694f811a95c0a2d9d68c4a7 [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;
91
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;
Romain Guy060b5c82010-03-04 10:07:38 -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;
Romain Guy060b5c82010-03-04 10:07:38 -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;
Romain Guy060b5c82010-03-04 10:07:38 -0800115
Daniel Sandler388f6792010-03-02 14:08:08 -0500116 private boolean mShouldGainFocus;
Romain Guy060b5c82010-03-04 10:07:38 -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;
Romain Guy060b5c82010-03-04 10:07:38 -0800126
127 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;
Romain Guy060b5c82010-03-04 10:07:38 -0800138
139 public static final int COLUMNS_PER_PAGE_PORTRAIT = 4;
140 public static final int ROWS_PER_PAGE_PORTRAIT = 4;
141
142 public static final int COLUMNS_PER_PAGE_LANDSCAPE = 6;
143 public static final int ROWS_PER_PAGE_LANDSCAPE = 3;
144
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;
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -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();
Romain Guy060b5c82010-03-04 10:07:38 -0800162
Daniel Sandler388f6792010-03-02 14:08:08 -0500163 setOnClickListener(this);
164 setOnLongClickListener(this);
165 setZOrderOnTop(true);
166 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -0800187
188 @SuppressWarnings({"UnusedDeclaration"})
189 public AllApps3D(Context context, AttributeSet attrs, int defStyle) {
190 this(context, attrs);
191 }
192
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 }
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -0800221
Daniel Sandler388f6792010-03-02 14:08:08 -0500222 public void setLauncher(Launcher launcher) {
223 mLauncher = launcher;
224 }
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -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();
Romain Guy060b5c82010-03-04 10:07:38 -0800242
Daniel Sandler388f6792010-03-02 14:08:08 -0500243 super.surfaceChanged(holder, format, w, h);
Romain Guy13c2e7b2010-03-10 19:45:00 -0800244
245 if (mSurrendered) return;
Romain Guy060b5c82010-03-04 10:07:38 -0800246
Daniel Sandler388f6792010-03-02 14:08:08 -0500247 mHaveSurface = true;
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800264
Daniel Sandler388f6792010-03-02 14:08:08 -0500265 mRollo.dirtyCheck();
266 mRollo.resize(w, h);
Romain Guy060b5c82010-03-04 10:07:38 -0800267
Daniel Sandler388f6792010-03-02 14:08:08 -0500268 if (mRS != null) {
269 mRS.mMessageCallback = mMessageProc = new AAMessage();
270 }
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -0800284
Daniel Sandler388f6792010-03-02 14:08:08 -0500285 mRollo.mUniformAlloc.data(tf);
286 }
Romain Guy060b5c82010-03-04 10:07:38 -0800287
Daniel Sandler388f6792010-03-02 14:08:08 -0500288 //long endTime = SystemClock.uptimeMillis();
289 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
290 }
Romain Guy060b5c82010-03-04 10:07:38 -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;
297
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 }
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -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);
Romain Guy060b5c82010-03-04 10:07:38 -0800334
Romain Guy13c2e7b2010-03-10 19:45:00 -0800335 if (!isVisible() || mSurrendered) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500336 return;
337 }
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -0800369
Daniel Sandler388f6792010-03-02 14:08:08 -0500370 @Override
371 public boolean onKeyDown(int keyCode, KeyEvent event) {
Romain Guy060b5c82010-03-04 10:07:38 -0800372
Daniel Sandler388f6792010-03-02 14:08:08 -0500373 boolean handled = false;
Romain Guy060b5c82010-03-04 10:07:38 -0800374
Daniel Sandler388f6792010-03-02 14:08:08 -0500375 if (!isVisible()) {
376 return false;
377 }
378 final int iconCount = mRollo.mState.iconCount;
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -0800395
Daniel Sandler388f6792010-03-02 14:08:08 -0500396 if (iconCount > 0) {
397 mArrowNavigation = true;
Romain Guy060b5c82010-03-04 10:07:38 -0800398
Daniel Sandler388f6792010-03-02 14:08:08 -0500399 int currentSelection = mRollo.mState.selectedIconIndex;
400 int currentTopRow = Math.round(mPosX);
Romain Guy060b5c82010-03-04 10:07:38 -0800401
402 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE_PORTRAIT-1
403 final int currentPageCol = currentSelection % mColumnsPerPage;
404
405 // The row of the current selection, in the range 0..ROWS_PER_PAGE_PORTRAIT-1
406 final int currentPageRow = (currentSelection - (currentTopRow* mColumnsPerPage))
407 / mRowsPerPage;
408
Daniel Sandler388f6792010-03-02 14:08:08 -0500409 int newSelection = currentSelection;
Romain Guy060b5c82010-03-04 10:07:38 -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;
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -0800499
Daniel Sandler388f6792010-03-02 14:08:08 -0500500 @Override
501 public boolean onTouchEvent(MotionEvent ev)
502 {
503 mArrowNavigation = false;
Romain Guy060b5c82010-03-04 10:07:38 -0800504
Daniel Sandler388f6792010-03-02 14:08:08 -0500505 if (!isVisible()) {
506 return true;
507 }
Romain Guy060b5c82010-03-04 10:07:38 -0800508
Daniel Sandler388f6792010-03-02 14:08:08 -0500509 if (mLocks != 0) {
510 return true;
511 }
Romain Guy060b5c82010-03-04 10:07:38 -0800512
Daniel Sandler388f6792010-03-02 14:08:08 -0500513 super.onTouchEvent(ev);
Romain Guy060b5c82010-03-04 10:07:38 -0800514
Daniel Sandler388f6792010-03-02 14:08:08 -0500515 int x = (int)ev.getX();
516 int y = (int)ev.getY();
Romain Guy060b5c82010-03-04 10:07:38 -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;
Romain Guy060b5c82010-03-04 10:07:38 -0800530
Daniel Sandler388f6792010-03-02 14:08:08 -0500531 mMotionDownRawX = (int)ev.getRawX();
532 mMotionDownRawY = (int)ev.getRawY();
Romain Guy060b5c82010-03-04 10:07:38 -0800533
Daniel Sandler388f6792010-03-02 14:08:08 -0500534 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
535 mRollo.mState.newTouchDown = 1;
Romain Guy060b5c82010-03-04 10:07:38 -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);
Romain Guy060b5c82010-03-04 10:07:38 -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();
Romain Guy060b5c82010-03-04 10:07:38 -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();
Romain Guy060b5c82010-03-04 10:07:38 -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();
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -0800624
Daniel Sandler388f6792010-03-02 14:08:08 -0500625 return true;
626 }
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -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);
Romain Guy060b5c82010-03-04 10:07:38 -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();
Romain Guy060b5c82010-03-04 10:07:38 -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;
Romain Guy060b5c82010-03-04 10:07:38 -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);
Romain Guy060b5c82010-03-04 10:07:38 -0800658
Daniel Sandler388f6792010-03-02 14:08:08 -0500659 mLauncher.closeAllApps(true);
660 }
661 return true;
662 }
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -0800700
Daniel Sandler388f6792010-03-02 14:08:08 -0500701 public void setDragController(DragController dragger) {
702 mDragController = dragger;
703 }
Romain Guy060b5c82010-03-04 10:07:38 -0800704
Daniel Sandler388f6792010-03-02 14:08:08 -0500705 public void onDropCompleted(View target, boolean success) {
706 }
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -0800726
Daniel Sandler388f6792010-03-02 14:08:08 -0500727 public boolean isVisible() {
728 return mZoom > 0.001f;
729 }
Romain Guy060b5c82010-03-04 10:07:38 -0800730
Daniel Sandler388f6792010-03-02 14:08:08 -0500731 public boolean isOpaque() {
732 return mZoom > 0.999f;
733 }
Romain Guy060b5c82010-03-04 10:07:38 -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 }
755
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 }
Romain Guy060b5c82010-03-04 10:07:38 -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 }
Romain Guy060b5c82010-03-04 10:07:38 -0800772
Daniel Sandler388f6792010-03-02 14:08:08 -0500773 final int N = list.size();
774 if (mRollo != null) {
775 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
776 }
Romain Guy060b5c82010-03-04 10:07:38 -0800777
Daniel Sandler388f6792010-03-02 14:08:08 -0500778 for (int i=0; i<N; i++) {
779 final ApplicationInfo item = list.get(i);
780 int index = Collections.binarySearch(mAllAppsList, item,
781 LauncherModel.APP_NAME_COMPARATOR);
782 if (index < 0) {
783 index = -(index+1);
784 }
785 mAllAppsList.add(index, item);
786 if (mRollo != null) {
787 mRollo.addApp(index, item);
788 }
789 }
Romain Guy060b5c82010-03-04 10:07:38 -0800790
Daniel Sandler388f6792010-03-02 14:08:08 -0500791 if (mRollo != null) {
792 mRollo.saveAppsList();
793 }
794 }
Romain Guy060b5c82010-03-04 10:07:38 -0800795
Daniel Sandler388f6792010-03-02 14:08:08 -0500796 public void removeApps(ArrayList<ApplicationInfo> list) {
797 if (mAllAppsList == null) {
798 // Not done loading yet. We'll find out about it later.
799 return;
800 }
Romain Guy060b5c82010-03-04 10:07:38 -0800801
Daniel Sandler388f6792010-03-02 14:08:08 -0500802 final int N = list.size();
803 for (int i=0; i<N; i++) {
804 final ApplicationInfo item = list.get(i);
805 int index = findAppByComponent(mAllAppsList, item);
806 if (index >= 0) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500807 mAllAppsList.remove(index);
808 if (mRollo != null) {
809 mRollo.removeApp(index);
810 }
811 } else {
812 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
813 // Try to recover. This should keep us from crashing for now.
814 }
815 }
Romain Guy060b5c82010-03-04 10:07:38 -0800816
Daniel Sandler388f6792010-03-02 14:08:08 -0500817 if (mRollo != null) {
818 mRollo.saveAppsList();
819 }
820 }
Romain Guy060b5c82010-03-04 10:07:38 -0800821
Joe Onorato64e6be72010-03-05 15:05:52 -0500822 public void updateApps(ArrayList<ApplicationInfo> list) {
Daniel Sandler388f6792010-03-02 14:08:08 -0500823 // Just remove and add, because they may need to be re-sorted.
824 removeApps(list);
825 addApps(list);
826 }
Romain Guy060b5c82010-03-04 10:07:38 -0800827
Daniel Sandler388f6792010-03-02 14:08:08 -0500828 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
829 ComponentName component = item.intent.getComponent();
830 final int N = list.size();
831 for (int i=0; i<N; i++) {
832 ApplicationInfo x = list.get(i);
833 if (x.intent.getComponent().equals(component)) {
834 return i;
835 }
836 }
837 return -1;
838 }
Romain Guy060b5c82010-03-04 10:07:38 -0800839
840 /*
Daniel Sandler388f6792010-03-02 14:08:08 -0500841 private static int countPages(int iconCount) {
Romain Guy060b5c82010-03-04 10:07:38 -0800842 int iconsPerPage = getColumnsCount() * Defines.ROWS_PER_PAGE_PORTRAIT;
Daniel Sandler388f6792010-03-02 14:08:08 -0500843 int pages = iconCount / iconsPerPage;
844 if (pages*iconsPerPage != iconCount) {
845 pages++;
846 }
847 return pages;
848 }
Romain Guy060b5c82010-03-04 10:07:38 -0800849 */
Daniel Sandler388f6792010-03-02 14:08:08 -0500850
851 class AAMessage extends RenderScript.RSMessage {
852 public void run() {
853 mPosX = ((float)mData[0]) / (1 << 16);
854 mVelocity = ((float)mData[1]) / (1 << 16);
855 mZoom = ((float)mData[2]) / (1 << 16);
856 mZoomDirty = false;
857 }
858 }
Romain Guy060b5c82010-03-04 10:07:38 -0800859
Romain Guy13c2e7b2010-03-10 19:45:00 -0800860 public static class RolloRS {
Daniel Sandler388f6792010-03-02 14:08:08 -0500861 // Allocations ======
862 private int mWidth;
863 private int mHeight;
Romain Guy060b5c82010-03-04 10:07:38 -0800864
Daniel Sandler388f6792010-03-02 14:08:08 -0500865 private Resources mRes;
866 private Script mScript;
867 private Script.Invokable mInvokeMove;
868 private Script.Invokable mInvokeMoveTo;
869 private Script.Invokable mInvokeFling;
870 private Script.Invokable mInvokeResetWAR;
871 private Script.Invokable mInvokeSetZoom;
Romain Guy060b5c82010-03-04 10:07:38 -0800872
Daniel Sandler388f6792010-03-02 14:08:08 -0500873 private ProgramStore mPSIcons;
874 private ProgramFragment mPFTexMip;
875 private ProgramFragment mPFTexMipAlpha;
876 private ProgramFragment mPFTexNearest;
877 private ProgramVertex mPV;
878 private ProgramVertex mPVCurve;
879 private SimpleMesh mMesh;
880 private ProgramVertex.MatrixAllocation mPVA;
Romain Guy060b5c82010-03-04 10:07:38 -0800881
Daniel Sandler388f6792010-03-02 14:08:08 -0500882 private Allocation mUniformAlloc;
Romain Guy060b5c82010-03-04 10:07:38 -0800883
Daniel Sandler388f6792010-03-02 14:08:08 -0500884 private Allocation mHomeButtonNormal;
885 private Allocation mHomeButtonFocused;
886 private Allocation mHomeButtonPressed;
Romain Guy060b5c82010-03-04 10:07:38 -0800887
Daniel Sandler388f6792010-03-02 14:08:08 -0500888 private Allocation[] mIcons;
889 private int[] mIconIds;
890 private Allocation mAllocIconIds;
Romain Guy060b5c82010-03-04 10:07:38 -0800891
Daniel Sandler388f6792010-03-02 14:08:08 -0500892 private Allocation[] mLabels;
893 private int[] mLabelIds;
894 private Allocation mAllocLabelIds;
895 private Allocation mSelectedIcon;
Romain Guy060b5c82010-03-04 10:07:38 -0800896
Daniel Sandler388f6792010-03-02 14:08:08 -0500897 private int[] mTouchYBorders;
898 private int[] mTouchXBorders;
Romain Guy060b5c82010-03-04 10:07:38 -0800899
Daniel Sandler388f6792010-03-02 14:08:08 -0500900 private Bitmap mSelectionBitmap;
901 private Canvas mSelectionCanvas;
Romain Guy060b5c82010-03-04 10:07:38 -0800902
Daniel Sandler388f6792010-03-02 14:08:08 -0500903 Params mParams;
904 State mState;
Romain Guy13c2e7b2010-03-10 19:45:00 -0800905
906 AllApps3D mAllApps;
907 boolean mInitialize;
908
Daniel Sandler388f6792010-03-02 14:08:08 -0500909 class BaseAlloc {
910 Allocation mAlloc;
911 Type mType;
Romain Guy060b5c82010-03-04 10:07:38 -0800912
Daniel Sandler388f6792010-03-02 14:08:08 -0500913 void save() {
914 mAlloc.data(this);
915 }
916 }
Romain Guy060b5c82010-03-04 10:07:38 -0800917
Daniel Sandler388f6792010-03-02 14:08:08 -0500918 private boolean checkClickOK() {
Romain Guy13c2e7b2010-03-10 19:45:00 -0800919 return (Math.abs(mAllApps.mVelocity) < 0.4f) &&
920 (Math.abs(mAllApps.mPosX - Math.round(mAllApps.mPosX)) < 0.4f);
Daniel Sandler388f6792010-03-02 14:08:08 -0500921 }
Romain Guy060b5c82010-03-04 10:07:38 -0800922
Daniel Sandler388f6792010-03-02 14:08:08 -0500923 class Params extends BaseAlloc {
924 Params() {
925 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
926 mAlloc = Allocation.createTyped(mRS, mType);
927 save();
928 }
929 public int bubbleWidth;
930 public int bubbleHeight;
931 public int bubbleBitmapWidth;
932 public int bubbleBitmapHeight;
Romain Guy060b5c82010-03-04 10:07:38 -0800933
Daniel Sandler388f6792010-03-02 14:08:08 -0500934 public int homeButtonWidth;
935 public int homeButtonHeight;
936 public int homeButtonTextureWidth;
937 public int homeButtonTextureHeight;
938 }
Romain Guy060b5c82010-03-04 10:07:38 -0800939
Daniel Sandler388f6792010-03-02 14:08:08 -0500940 class State extends BaseAlloc {
941 public float newPositionX;
942 public int newTouchDown;
943 public float flingVelocity;
944 public int iconCount;
945 public int selectedIconIndex = -1;
946 public int selectedIconTexture;
947 public float zoomTarget;
948 public int homeButtonId;
949 public float targetPos;
Romain Guy060b5c82010-03-04 10:07:38 -0800950
Daniel Sandler388f6792010-03-02 14:08:08 -0500951 State() {
952 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
953 mAlloc = Allocation.createTyped(mRS, mType);
954 save();
955 }
956 }
Romain Guy060b5c82010-03-04 10:07:38 -0800957
Romain Guy13c2e7b2010-03-10 19:45:00 -0800958 public RolloRS(AllApps3D allApps) {
959 mAllApps = allApps;
Daniel Sandler388f6792010-03-02 14:08:08 -0500960 }
Romain Guy060b5c82010-03-04 10:07:38 -0800961
Daniel Sandler388f6792010-03-02 14:08:08 -0500962 public void init(Resources res, int width, int height) {
963 mRes = res;
964 mWidth = width;
965 mHeight = height;
966 initProgramVertex();
967 initProgramFragment();
968 initProgramStore();
969 initGl();
970 initData();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800971 initTouchState(width, height);
Daniel Sandler388f6792010-03-02 14:08:08 -0500972 initRs();
973 }
Romain Guy060b5c82010-03-04 10:07:38 -0800974
Daniel Sandler388f6792010-03-02 14:08:08 -0500975 public void initMesh() {
976 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Romain Guy060b5c82010-03-04 10:07:38 -0800977
Daniel Sandler388f6792010-03-02 14:08:08 -0500978 for (int ct=0; ct < 16; ct++) {
979 float pos = (1.f / (16.f - 1)) * ct;
980 tm.addVertex(0.0f, pos);
981 tm.addVertex(1.0f, pos);
982 }
983 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
984 tm.addTriangle(ct, ct+1, ct+2);
985 tm.addTriangle(ct+1, ct+3, ct+2);
986 }
987 mMesh = tm.create();
988 mMesh.setName("SMCell");
989 }
Romain Guy060b5c82010-03-04 10:07:38 -0800990
Daniel Sandler388f6792010-03-02 14:08:08 -0500991 void resize(int w, int h) {
992 mPVA.setupProjectionNormalized(w, h);
993 mWidth = w;
994 mHeight = h;
995 }
Romain Guy060b5c82010-03-04 10:07:38 -0800996
Daniel Sandler388f6792010-03-02 14:08:08 -0500997 private void initProgramVertex() {
998 mPVA = new ProgramVertex.MatrixAllocation(mRS);
999 resize(mWidth, mHeight);
Romain Guy060b5c82010-03-04 10:07:38 -08001000
Daniel Sandler388f6792010-03-02 14:08:08 -05001001 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
1002 pvb.setTextureMatrixEnable(true);
1003 mPV = pvb.create();
1004 mPV.setName("PV");
1005 mPV.bindAllocation(mPVA);
Romain Guy060b5c82010-03-04 10:07:38 -08001006
Daniel Sandler388f6792010-03-02 14:08:08 -05001007 Element.Builder eb = new Element.Builder(mRS);
1008 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
1009 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
1010 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "BendPos");
1011 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
1012 Element e = eb.create();
Romain Guy060b5c82010-03-04 10:07:38 -08001013
Daniel Sandler388f6792010-03-02 14:08:08 -05001014 mUniformAlloc = Allocation.createSized(mRS, e, 1);
Romain Guy060b5c82010-03-04 10:07:38 -08001015
Daniel Sandler388f6792010-03-02 14:08:08 -05001016 initMesh();
1017 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
Romain Guy060b5c82010-03-04 10:07:38 -08001018 String t = "void main() {\n" +
1019 // Animation
1020 " float ani = UNI_Position.z;\n" +
1021
1022 " float bendY1 = UNI_BendPos.x;\n" +
1023 " float bendY2 = UNI_BendPos.y;\n" +
1024 " float bendAngle = 47.0 * (3.14 / 180.0);\n" +
1025 " float bendDistance = bendY1 * 0.4;\n" +
1026 " float distanceDimLevel = 0.6;\n" +
1027
1028 " float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
1029 " float aDy = cos(bendAngle);\n" +
1030 " float aDz = sin(bendAngle);\n" +
1031
1032 " float scale = (2.0 / 480.0);\n" +
1033 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
1034 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
1035 " float y = 0.0;\n" +
1036 " float z = 0.0;\n" +
1037 " float lum = 1.0;\n" +
1038
1039 " float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
1040 " y += cv * aDy;\n" +
1041 " z += -cv * aDz;\n" +
1042 " cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" + // curve range
1043 " lum += cv / bendDistance * distanceDimLevel;\n" +
1044 " y += cv * cos(cv * bendStep);\n" +
1045 " z += cv * sin(cv * bendStep);\n" +
1046
1047 " cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
1048 " y += cv * aDy;\n" +
1049 " z += cv * aDz;\n" +
1050 " cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
1051 " lum -= cv / bendDistance * distanceDimLevel;\n" +
1052 " y += cv * cos(cv * bendStep);\n" +
1053 " z += cv * sin(cv * bendStep);\n" +
1054
1055 " y += clamp(ys, bendY1, bendY2);\n" +
1056
1057 " vec4 pos;\n" +
1058 " pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
1059 " pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
1060 " pos.z = z * UNI_ScaleOffset.x;\n" +
1061 " pos.w = 1.0;\n" +
1062
1063 " pos.x *= 1.0 + ani * 4.0;\n" +
1064 " pos.y *= 1.0 + ani * 4.0;\n" +
1065 " pos.z -= ani * 1.5;\n" +
1066 " lum *= 1.0 - ani;\n" +
1067
1068 " gl_Position = UNI_MVP * pos;\n" +
1069 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
1070 " varTex0.xy = ATTRIB_position;\n" +
1071 " varTex0.y = 1.0 - varTex0.y;\n" +
1072 " varTex0.zw = vec2(0.0, 0.0);\n" +
1073 "}\n";
Daniel Sandler388f6792010-03-02 14:08:08 -05001074 sb.setShader(t);
1075 sb.addConstant(mUniformAlloc.getType());
1076 sb.addInput(mMesh.getVertexType(0).getElement());
1077 mPVCurve = sb.create();
1078 mPVCurve.setName("PVCurve");
1079 mPVCurve.bindAllocation(mPVA);
1080 mPVCurve.bindConstants(mUniformAlloc, 1);
Romain Guy060b5c82010-03-04 10:07:38 -08001081
Daniel Sandler388f6792010-03-02 14:08:08 -05001082 mRS.contextBindProgramVertex(mPV);
1083 }
Romain Guy060b5c82010-03-04 10:07:38 -08001084
Daniel Sandler388f6792010-03-02 14:08:08 -05001085 private void initProgramFragment() {
1086 Sampler.Builder sb = new Sampler.Builder(mRS);
1087 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
1088 sb.setMag(Sampler.Value.NEAREST);
1089 sb.setWrapS(Sampler.Value.CLAMP);
1090 sb.setWrapT(Sampler.Value.CLAMP);
1091 Sampler linear = sb.create();
Romain Guy060b5c82010-03-04 10:07:38 -08001092
Daniel Sandler388f6792010-03-02 14:08:08 -05001093 sb.setMin(Sampler.Value.NEAREST);
1094 sb.setMag(Sampler.Value.NEAREST);
1095 Sampler nearest = sb.create();
Romain Guy060b5c82010-03-04 10:07:38 -08001096
Daniel Sandler388f6792010-03-02 14:08:08 -05001097 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
1098 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1099 ProgramFragment.Builder.Format.RGBA, 0);
1100 mPFTexMip = bf.create();
1101 mPFTexMip.setName("PFTexMip");
1102 mPFTexMip.bindSampler(linear, 0);
Romain Guy060b5c82010-03-04 10:07:38 -08001103
Daniel Sandler388f6792010-03-02 14:08:08 -05001104 mPFTexNearest = bf.create();
1105 mPFTexNearest.setName("PFTexNearest");
1106 mPFTexNearest.bindSampler(nearest, 0);
Romain Guy060b5c82010-03-04 10:07:38 -08001107
Daniel Sandler388f6792010-03-02 14:08:08 -05001108 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1109 ProgramFragment.Builder.Format.ALPHA, 0);
1110 mPFTexMipAlpha = bf.create();
1111 mPFTexMipAlpha.setName("PFTexMipAlpha");
1112 mPFTexMipAlpha.bindSampler(linear, 0);
Romain Guy060b5c82010-03-04 10:07:38 -08001113
Daniel Sandler388f6792010-03-02 14:08:08 -05001114 }
Romain Guy060b5c82010-03-04 10:07:38 -08001115
Daniel Sandler388f6792010-03-02 14:08:08 -05001116 private void initProgramStore() {
1117 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1118 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
1119 bs.setColorMask(true,true,true,false);
1120 bs.setDitherEnable(true);
1121 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1122 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1123 mPSIcons = bs.create();
1124 mPSIcons.setName("PSIcons");
1125 }
Romain Guy060b5c82010-03-04 10:07:38 -08001126
Daniel Sandler388f6792010-03-02 14:08:08 -05001127 private void initGl() {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001128 mTouchXBorders = new int[mAllApps.mColumnsPerPage + 1];
1129 mTouchYBorders = new int[mAllApps.mRowsPerPage + 1];
Daniel Sandler388f6792010-03-02 14:08:08 -05001130 }
Romain Guy060b5c82010-03-04 10:07:38 -08001131
Daniel Sandler388f6792010-03-02 14:08:08 -05001132 private void initData() {
1133 mParams = new Params();
1134 mState = new State();
Romain Guy060b5c82010-03-04 10:07:38 -08001135
Romain Guy13c2e7b2010-03-10 19:45:00 -08001136 final Utilities.BubbleText bubble = new Utilities.BubbleText(mAllApps.getContext());
Romain Guy060b5c82010-03-04 10:07:38 -08001137
Daniel Sandler388f6792010-03-02 14:08:08 -05001138 mParams.bubbleWidth = bubble.getBubbleWidth();
1139 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1140 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1141 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Romain Guy060b5c82010-03-04 10:07:38 -08001142
Daniel Sandler388f6792010-03-02 14:08:08 -05001143 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1144 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1145 mHomeButtonNormal.uploadToTexture(0);
1146 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1147 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1148 mHomeButtonFocused.uploadToTexture(0);
1149 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1150 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1151 mHomeButtonPressed.uploadToTexture(0);
1152 mParams.homeButtonWidth = 76;
1153 mParams.homeButtonHeight = 68;
1154 mParams.homeButtonTextureWidth = 128;
1155 mParams.homeButtonTextureHeight = 128;
Romain Guy060b5c82010-03-04 10:07:38 -08001156
Daniel Sandler388f6792010-03-02 14:08:08 -05001157 mState.homeButtonId = mHomeButtonNormal.getID();
Romain Guy060b5c82010-03-04 10:07:38 -08001158
Daniel Sandler388f6792010-03-02 14:08:08 -05001159 mParams.save();
1160 mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -08001161
Daniel Sandler388f6792010-03-02 14:08:08 -05001162 mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1163 Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1164 mSelectionCanvas = new Canvas(mSelectionBitmap);
Romain Guy060b5c82010-03-04 10:07:38 -08001165
Daniel Sandler388f6792010-03-02 14:08:08 -05001166 setApps(null);
1167 }
Romain Guy060b5c82010-03-04 10:07:38 -08001168
Daniel Sandler388f6792010-03-02 14:08:08 -05001169 private void initRs() {
1170 ScriptC.Builder sb = new ScriptC.Builder(mRS);
1171 sb.setScript(mRes, R.raw.allapps);
1172 sb.setRoot(true);
Romain Guy13c2e7b2010-03-10 19:45:00 -08001173 sb.addDefines(mAllApps.mDefines);
Daniel Sandler388f6792010-03-02 14:08:08 -05001174 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1175 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
1176 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
1177 mInvokeMove = sb.addInvokable("move");
1178 mInvokeFling = sb.addInvokable("fling");
1179 mInvokeMoveTo = sb.addInvokable("moveTo");
1180 mInvokeResetWAR = sb.addInvokable("resetHWWar");
1181 mInvokeSetZoom = sb.addInvokable("setZoom");
1182 mScript = sb.create();
1183 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1184 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1185 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1186 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1187 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
1188 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Romain Guy060b5c82010-03-04 10:07:38 -08001189
Daniel Sandler388f6792010-03-02 14:08:08 -05001190 mRS.contextBindRootScript(mScript);
1191 }
Romain Guy060b5c82010-03-04 10:07:38 -08001192
Daniel Sandler388f6792010-03-02 14:08:08 -05001193 void dirtyCheck() {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001194 if (mAllApps.mZoomDirty) {
1195 setZoom(mAllApps.mNextZoom, mAllApps.mAnimateNextZoom);
Daniel Sandler388f6792010-03-02 14:08:08 -05001196 }
1197 }
Romain Guy060b5c82010-03-04 10:07:38 -08001198
1199 @SuppressWarnings({"ConstantConditions"})
Daniel Sandler388f6792010-03-02 14:08:08 -05001200 private void setApps(ArrayList<ApplicationInfo> list) {
1201 final int count = list != null ? list.size() : 0;
1202 int allocCount = count;
1203 if (allocCount < 1) {
1204 allocCount = 1;
1205 }
Romain Guy060b5c82010-03-04 10:07:38 -08001206
Daniel Sandler388f6792010-03-02 14:08:08 -05001207 mIcons = new Allocation[count];
1208 mIconIds = new int[allocCount];
1209 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Romain Guy060b5c82010-03-04 10:07:38 -08001210
Daniel Sandler388f6792010-03-02 14:08:08 -05001211 mLabels = new Allocation[count];
1212 mLabelIds = new int[allocCount];
1213 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Romain Guy060b5c82010-03-04 10:07:38 -08001214
Daniel Sandler388f6792010-03-02 14:08:08 -05001215 mState.iconCount = count;
1216 for (int i=0; i < mState.iconCount; i++) {
1217 createAppIconAllocations(i, list.get(i));
1218 }
1219 for (int i=0; i < mState.iconCount; i++) {
1220 uploadAppIcon(i, list.get(i));
1221 }
1222 saveAppsList();
1223 }
Romain Guy060b5c82010-03-04 10:07:38 -08001224
Daniel Sandler388f6792010-03-02 14:08:08 -05001225 private void setZoom(float zoom, boolean animate) {
1226 mRollo.clearSelectedIcon();
1227 mRollo.setHomeSelected(SELECTED_NONE);
1228 if (zoom > 0.001f) {
1229 mRollo.mState.zoomTarget = zoom;
1230 } else {
1231 mRollo.mState.zoomTarget = 0;
1232 }
1233 mRollo.mState.save();
1234 if (!animate) {
1235 mRollo.mInvokeSetZoom.execute();
1236 }
1237 }
Romain Guy060b5c82010-03-04 10:07:38 -08001238
Daniel Sandler388f6792010-03-02 14:08:08 -05001239 private void createAppIconAllocations(int index, ApplicationInfo item) {
1240 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001241 Element.RGBA_8888(mRS), false);
Daniel Sandler388f6792010-03-02 14:08:08 -05001242 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001243 Element.A_8(mRS), false);
Daniel Sandler388f6792010-03-02 14:08:08 -05001244 mIconIds[index] = mIcons[index].getID();
1245 mLabelIds[index] = mLabels[index].getID();
1246 }
Romain Guy060b5c82010-03-04 10:07:38 -08001247
Daniel Sandler388f6792010-03-02 14:08:08 -05001248 private void uploadAppIcon(int index, ApplicationInfo item) {
1249 if (mIconIds[index] != mIcons[index].getID()) {
1250 throw new IllegalStateException("uploadAppIcon index=" + index
1251 + " mIcons[index].getID=" + mIcons[index].getID()
1252 + " mIconsIds[index]=" + mIconIds[index]
1253 + " item=" + item);
1254 }
Jason Samsd1e2e1d2010-03-05 13:24:31 -08001255 mIcons[index].uploadToTexture(true, 0);
1256 mLabels[index].uploadToTexture(true, 0);
Daniel Sandler388f6792010-03-02 14:08:08 -05001257 }
Romain Guy060b5c82010-03-04 10:07:38 -08001258
Daniel Sandler388f6792010-03-02 14:08:08 -05001259 /**
1260 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1261 * fill in the values and call saveAppsList().
1262 */
1263 private void reallocAppsList(int count) {
1264 Allocation[] icons = new Allocation[count];
1265 int[] iconIds = new int[count];
1266 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Romain Guy060b5c82010-03-04 10:07:38 -08001267
Daniel Sandler388f6792010-03-02 14:08:08 -05001268 Allocation[] labels = new Allocation[count];
1269 int[] labelIds = new int[count];
1270 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
Romain Guy060b5c82010-03-04 10:07:38 -08001271
Daniel Sandler388f6792010-03-02 14:08:08 -05001272 final int oldCount = mRollo.mState.iconCount;
Romain Guy060b5c82010-03-04 10:07:38 -08001273
Daniel Sandler388f6792010-03-02 14:08:08 -05001274 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1275 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1276 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1277 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
Romain Guy060b5c82010-03-04 10:07:38 -08001278
Daniel Sandler388f6792010-03-02 14:08:08 -05001279 mIcons = icons;
1280 mIconIds = iconIds;
1281 mLabels = labels;
1282 mLabelIds = labelIds;
1283 }
Romain Guy060b5c82010-03-04 10:07:38 -08001284
Daniel Sandler388f6792010-03-02 14:08:08 -05001285 /**
1286 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1287 */
1288 private void addApp(int index, ApplicationInfo item) {
1289 final int count = mState.iconCount - index;
1290 final int dest = index + 1;
Romain Guy060b5c82010-03-04 10:07:38 -08001291
Daniel Sandler388f6792010-03-02 14:08:08 -05001292 System.arraycopy(mIcons, index, mIcons, dest, count);
1293 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1294 System.arraycopy(mLabels, index, mLabels, dest, count);
1295 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
Romain Guy060b5c82010-03-04 10:07:38 -08001296
Daniel Sandler388f6792010-03-02 14:08:08 -05001297 createAppIconAllocations(index, item);
1298 uploadAppIcon(index, item);
1299 mRollo.mState.iconCount++;
1300 }
Romain Guy060b5c82010-03-04 10:07:38 -08001301
Daniel Sandler388f6792010-03-02 14:08:08 -05001302 /**
1303 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1304 */
1305 private void removeApp(int index) {
1306 final int count = mState.iconCount - index - 1;
1307 final int src = index + 1;
Romain Guy060b5c82010-03-04 10:07:38 -08001308
Daniel Sandler388f6792010-03-02 14:08:08 -05001309 System.arraycopy(mIcons, src, mIcons, index, count);
1310 System.arraycopy(mIconIds, src, mIconIds, index, count);
1311 System.arraycopy(mLabels, src, mLabels, index, count);
1312 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
Romain Guy060b5c82010-03-04 10:07:38 -08001313
Daniel Sandler388f6792010-03-02 14:08:08 -05001314 mRollo.mState.iconCount--;
1315 final int last = mState.iconCount;
Romain Guy060b5c82010-03-04 10:07:38 -08001316
Daniel Sandler388f6792010-03-02 14:08:08 -05001317 mIcons[last] = null;
1318 mIconIds[last] = 0;
1319 mLabels[last] = null;
1320 mLabelIds[last] = 0;
1321 }
Romain Guy060b5c82010-03-04 10:07:38 -08001322
Daniel Sandler388f6792010-03-02 14:08:08 -05001323 /**
1324 * Send the apps list structures to RS.
1325 */
1326 private void saveAppsList() {
1327 // WTF: how could mScript be not null but mAllocIconIds null b/2460740.
1328 if (mScript != null && mAllocIconIds != null) {
1329 mRS.contextBindRootScript(null);
Romain Guy060b5c82010-03-04 10:07:38 -08001330
Daniel Sandler388f6792010-03-02 14:08:08 -05001331 mAllocIconIds.data(mIconIds);
1332 mAllocLabelIds.data(mLabelIds);
Romain Guy060b5c82010-03-04 10:07:38 -08001333
Daniel Sandler388f6792010-03-02 14:08:08 -05001334 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1335 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Romain Guy060b5c82010-03-04 10:07:38 -08001336
Daniel Sandler388f6792010-03-02 14:08:08 -05001337 mState.save();
Romain Guy060b5c82010-03-04 10:07:38 -08001338
Daniel Sandler388f6792010-03-02 14:08:08 -05001339 // Note: mScript may be null if we haven't initialized it yet.
1340 // In that case, this is a no-op.
1341 if (mInvokeResetWAR != null) {
1342 mInvokeResetWAR.execute();
1343 }
Romain Guy060b5c82010-03-04 10:07:38 -08001344
Daniel Sandler388f6792010-03-02 14:08:08 -05001345 mRS.contextBindRootScript(mScript);
1346 }
1347 }
Romain Guy060b5c82010-03-04 10:07:38 -08001348
Romain Guy13c2e7b2010-03-10 19:45:00 -08001349 void initTouchState(int width, int height) {
1350 boolean isPortrait = width < height;
1351
Romain Guy060b5c82010-03-04 10:07:38 -08001352 // TODO: Put this in a config file/define
1353 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE_PORTRAIT;
1354 if (!isPortrait) cellHeight -= 12;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001355 int centerY = (int) (mAllApps.getHeight() * (isPortrait ? 0.5f : 0.47f));
Romain Guy060b5c82010-03-04 10:07:38 -08001356 if (!isPortrait) centerY += cellHeight / 2;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001357 int half = (int) Math.floor((mAllApps.mRowsPerPage + 1) / 2);
Romain Guy060b5c82010-03-04 10:07:38 -08001358 int end = mTouchYBorders.length - (half + 1);
1359
1360 for (int i = -half; i <= end; i++) {
1361 mTouchYBorders[i + half] = centerY + i * cellHeight;
1362 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001363
Romain Guy060b5c82010-03-04 10:07:38 -08001364 int x = 0;
1365 // TODO: Put this in a config file/define
1366 int columnWidth = 120;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001367 for (int i = 0; i < mAllApps.mColumnsPerPage + 1; i++) {
Romain Guy060b5c82010-03-04 10:07:38 -08001368 mTouchXBorders[i] = x;
1369 x += columnWidth;
1370 }
Daniel Sandler388f6792010-03-02 14:08:08 -05001371 }
Romain Guy060b5c82010-03-04 10:07:38 -08001372
Daniel Sandler388f6792010-03-02 14:08:08 -05001373 void fling() {
1374 mInvokeFling.execute();
1375 }
Romain Guy060b5c82010-03-04 10:07:38 -08001376
Daniel Sandler388f6792010-03-02 14:08:08 -05001377 void move() {
1378 mInvokeMove.execute();
1379 }
Romain Guy060b5c82010-03-04 10:07:38 -08001380
Daniel Sandler388f6792010-03-02 14:08:08 -05001381 void moveTo(float row) {
1382 mState.targetPos = row;
1383 mState.save();
1384 mInvokeMoveTo.execute();
1385 }
Romain Guy060b5c82010-03-04 10:07:38 -08001386
Daniel Sandler388f6792010-03-02 14:08:08 -05001387 int chooseTappedIcon(int x, int y, float pos) {
1388 // Adjust for scroll position if not zero.
1389 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Romain Guy060b5c82010-03-04 10:07:38 -08001390
Daniel Sandler388f6792010-03-02 14:08:08 -05001391 int col = -1;
1392 int row = -1;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001393 final int columnsCount = mAllApps.mColumnsPerPage;
Romain Guy060b5c82010-03-04 10:07:38 -08001394 for (int i=0; i< columnsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001395 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1396 col = i;
1397 break;
1398 }
1399 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001400 final int rowsCount = mAllApps.mRowsPerPage;
Romain Guy060b5c82010-03-04 10:07:38 -08001401 for (int i=0; i< rowsCount; i++) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001402 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1403 row = i;
1404 break;
1405 }
1406 }
Romain Guy060b5c82010-03-04 10:07:38 -08001407
Daniel Sandler388f6792010-03-02 14:08:08 -05001408 if (row < 0 || col < 0) {
1409 return -1;
1410 }
Romain Guy060b5c82010-03-04 10:07:38 -08001411
1412 int index = (((int)pos) * columnsCount) + (row * columnsCount) + col;
1413
Daniel Sandler388f6792010-03-02 14:08:08 -05001414 if (index >= mState.iconCount) {
1415 return -1;
1416 } else {
1417 return index;
1418 }
1419 }
Romain Guy060b5c82010-03-04 10:07:38 -08001420
Daniel Sandler388f6792010-03-02 14:08:08 -05001421 /**
1422 * You need to call save() on mState on your own after calling this.
1423 *
1424 * @return the index of the icon that was selected.
1425 */
1426 int selectIcon(int x, int y, float pos, int pressed) {
1427 final int index = chooseTappedIcon(x, y, pos);
1428 selectIcon(index, pressed);
1429 return index;
1430 }
Romain Guy060b5c82010-03-04 10:07:38 -08001431
Daniel Sandler388f6792010-03-02 14:08:08 -05001432 /**
1433 * Select the icon at the given index.
1434 *
1435 * @param index The index.
1436 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
1437 */
1438 void selectIcon(int index, int pressed) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001439 final ArrayList<ApplicationInfo> appsList = mAllApps.mAllAppsList;
1440 if (appsList == null || index < 0 || index >= appsList.size()) {
Daniel Sandler388f6792010-03-02 14:08:08 -05001441 mState.selectedIconIndex = -1;
Romain Guy13c2e7b2010-03-10 19:45:00 -08001442 if (mAllApps.mLastSelection == SELECTION_ICONS) {
1443 mAllApps.mLastSelection = SELECTION_NONE;
Daniel Sandler388f6792010-03-02 14:08:08 -05001444 }
1445 } else {
1446 if (pressed == SELECTED_FOCUSED) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001447 mAllApps.mLastSelection = SELECTION_ICONS;
Daniel Sandler388f6792010-03-02 14:08:08 -05001448 }
Romain Guy060b5c82010-03-04 10:07:38 -08001449
Daniel Sandler388f6792010-03-02 14:08:08 -05001450 int prev = mState.selectedIconIndex;
1451 mState.selectedIconIndex = index;
Romain Guy060b5c82010-03-04 10:07:38 -08001452
Romain Guy13c2e7b2010-03-10 19:45:00 -08001453 ApplicationInfo info = appsList.get(index);
Daniel Sandler388f6792010-03-02 14:08:08 -05001454 Bitmap selectionBitmap = mSelectionBitmap;
Romain Guy060b5c82010-03-04 10:07:38 -08001455
Daniel Sandler388f6792010-03-02 14:08:08 -05001456 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1457 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1458 pressed == SELECTED_PRESSED, info.iconBitmap);
Romain Guy060b5c82010-03-04 10:07:38 -08001459
Daniel Sandler388f6792010-03-02 14:08:08 -05001460 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
1461 Element.RGBA_8888(mRS), false);
1462 mSelectedIcon.uploadToTexture(0);
1463 mState.selectedIconTexture = mSelectedIcon.getID();
Romain Guy060b5c82010-03-04 10:07:38 -08001464
Daniel Sandler388f6792010-03-02 14:08:08 -05001465 if (prev != index) {
1466 if (info.title != null && info.title.length() > 0) {
1467 //setContentDescription(info.title);
Romain Guy13c2e7b2010-03-10 19:45:00 -08001468 mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Daniel Sandler388f6792010-03-02 14:08:08 -05001469 }
1470 }
1471 }
1472 }
Romain Guy060b5c82010-03-04 10:07:38 -08001473
Daniel Sandler388f6792010-03-02 14:08:08 -05001474 /**
1475 * You need to call save() on mState on your own after calling this.
1476 */
1477 void clearSelectedIcon() {
1478 mState.selectedIconIndex = -1;
1479 }
Romain Guy060b5c82010-03-04 10:07:38 -08001480
Daniel Sandler388f6792010-03-02 14:08:08 -05001481 void setHomeSelected(int mode) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001482 final int prev = mAllApps.mLastSelection;
Daniel Sandler388f6792010-03-02 14:08:08 -05001483 switch (mode) {
1484 case SELECTED_NONE:
1485 mState.homeButtonId = mHomeButtonNormal.getID();
1486 break;
1487 case SELECTED_FOCUSED:
Romain Guy13c2e7b2010-03-10 19:45:00 -08001488 mAllApps.mLastSelection = SELECTION_HOME;
Daniel Sandler388f6792010-03-02 14:08:08 -05001489 mState.homeButtonId = mHomeButtonFocused.getID();
1490 if (prev != SELECTION_HOME) {
Romain Guy13c2e7b2010-03-10 19:45:00 -08001491 mAllApps.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Daniel Sandler388f6792010-03-02 14:08:08 -05001492 }
1493 break;
1494 case SELECTED_PRESSED:
1495 mState.homeButtonId = mHomeButtonPressed.getID();
1496 break;
1497 }
1498 }
Romain Guy060b5c82010-03-04 10:07:38 -08001499
Daniel Sandler388f6792010-03-02 14:08:08 -05001500 public void dumpState() {
1501 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1502 Log.d(TAG, "mRollo.mHeight=" + mHeight);
Romain Guy060b5c82010-03-04 10:07:38 -08001503 Log.d(TAG, "mRollo.mIcons=" + Arrays.toString(mIcons));
Daniel Sandler388f6792010-03-02 14:08:08 -05001504 if (mIcons != null) {
1505 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1506 }
1507 if (mIconIds != null) {
1508 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1509 }
1510 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1511 if (mLabelIds != null) {
1512 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1513 }
1514 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1515 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1516 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
1517 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1518 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1519 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1520 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1521 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1522 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1523 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1524 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1525 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1526 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1527 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1528 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1529 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1530 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1531 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1532 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1533 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1534 }
1535 }
Romain Guy060b5c82010-03-04 10:07:38 -08001536
Daniel Sandler388f6792010-03-02 14:08:08 -05001537 public void dumpState() {
1538 Log.d(TAG, "mRS=" + mRS);
1539 Log.d(TAG, "mRollo=" + mRollo);
1540 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1541 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1542 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1543 Log.d(TAG, "mLastSelection=" + mLastSelection);
1544 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1545 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1546 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1547 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1548 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1549 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1550 Log.d(TAG, "mZoom=" + mZoom);
1551 Log.d(TAG, "mPosX=" + mPosX);
1552 Log.d(TAG, "mVelocity=" + mVelocity);
1553 Log.d(TAG, "mMessageProc=" + mMessageProc);
1554 if (mRollo != null) {
1555 mRollo.dumpState();
1556 }
1557 if (mRS != null) {
1558 mRS.contextDump(0);
1559 }
1560 }
1561}
1562
1563