blob: b5074b11e38a71f109125e2d2dfbdd18da655605 [file] [log] [blame]
Joe Onorato93839052009-08-06 20:34:32 -07001/*
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 java.io.Writer;
20import java.util.ArrayList;
21import java.util.concurrent.Semaphore;
22import java.lang.Float;
Joe Onoratoa8138d52009-10-06 19:25:30 -070023import java.util.Collections;
24import java.util.Comparator;
Joe Onorato93839052009-08-06 20:34:32 -070025
26import android.renderscript.RSSurfaceView;
27import android.renderscript.RenderScript;
28
29import android.renderscript.RenderScript;
30import android.renderscript.ProgramVertex;
31import android.renderscript.Element;
32import android.renderscript.Allocation;
Jason Sams78aebd82009-09-15 13:06:59 -070033import android.renderscript.Type;
Joe Onorato93839052009-08-06 20:34:32 -070034import android.renderscript.Script;
35import android.renderscript.ScriptC;
36import android.renderscript.ProgramFragment;
37import android.renderscript.ProgramStore;
38import android.renderscript.Sampler;
Jason Sams0aa71662009-10-02 18:43:18 -070039import android.renderscript.SimpleMesh;
Joe Onorato93839052009-08-06 20:34:32 -070040
41import android.content.Context;
42import android.content.res.Resources;
Joe Onorato7c312c12009-08-13 21:36:53 -070043import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070044import android.graphics.Bitmap;
45import android.graphics.BitmapFactory;
46import android.graphics.Canvas;
47import android.graphics.Paint;
Joe Onorato93839052009-08-06 20:34:32 -070048import android.graphics.drawable.BitmapDrawable;
49import android.graphics.drawable.Drawable;
Joe Onorato93839052009-08-06 20:34:32 -070050import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070051import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070052import android.util.AttributeSet;
53import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070054import android.view.KeyEvent;
55import android.view.MotionEvent;
Joe Onorato93839052009-08-06 20:34:32 -070056import android.view.Surface;
57import android.view.SurfaceHolder;
58import android.view.SurfaceView;
Joe Onorato6665c0f2009-09-02 15:27:24 -070059import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070060import android.view.VelocityTracker;
61import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070062import android.graphics.PixelFormat;
63
64
Joe Onorato6665c0f2009-09-02 15:27:24 -070065public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070066 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040067 private static final String TAG = "Launcher.AllAppsView";
68
Joe Onoratofb0ca672009-09-14 17:55:46 -040069 /** Bit for mLocks for when there are icons being loaded. */
70 private static final int LOCK_ICONS_PENDING = 1;
71
Joe Onorato68ffd102009-10-15 17:59:43 -070072 private static final int TRACKING_NONE = 0;
73 private static final int TRACKING_FLING = 1;
74 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070075
Joe Onorato6665c0f2009-09-02 15:27:24 -070076 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070077 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040078
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;
Joe Onorato6665c0f2009-09-02 15:27:24 -070082
Joe Onorato82ca5502009-10-15 16:59:23 -070083 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070084 private int mMaxFlingVelocity;
85
Joe Onoratobcbeab82009-10-01 21:45:43 -070086 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070087 private RenderScript mRS;
88 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070090
Joe Onoratoc567acb2009-08-31 14:34:43 -070091 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070092 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070093 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -070094 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -070095 private int mMotionDownRawX;
96 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -070097 private int mDownIconIndex = -1;
98 private int mCurrentIconIndex = -1;
Joe Onoratobcbeab82009-10-01 21:45:43 -070099 private int mHomeButtonTop;
Jason Sams86c87ed2009-09-18 13:55:55 -0700100 private long mTouchTime;
Jason Samsd8152b92009-10-13 17:19:10 -0700101 private boolean mRotateMove = true;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700102
Joe Onorato6665c0f2009-09-02 15:27:24 -0700103 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700104 public static final int ALLOC_PARAMS = 0;
105 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700106 public static final int ALLOC_ICON_IDS = 3;
107 public static final int ALLOC_LABEL_IDS = 4;
108 public static final int ALLOC_X_BORDERS = 5;
109 public static final int ALLOC_Y_BORDERS = 6;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700110
111 public static final int COLUMNS_PER_PAGE = 4;
112 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700113
Joe Onorato6665c0f2009-09-02 15:27:24 -0700114 public static final float RADIUS = 4.0f;
115
Joe Onorato6665c0f2009-09-02 15:27:24 -0700116 public static final int ICON_WIDTH_PX = 64;
117 public static final int ICON_TEXTURE_WIDTH_PX = 128;
118
119 public static final int ICON_HEIGHT_PX = 64;
120 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
121 public static final float ICON_TOP_OFFSET = 0.2f;
122
123 public static final float CAMERA_Z = -2;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700124
125 public int SCREEN_WIDTH_PX;
126 public int SCREEN_HEIGHT_PX;
127
128 public float FAR_ICON_SIZE;
129
130 public void recompute(int w, int h) {
131 SCREEN_WIDTH_PX = 480;
132 SCREEN_HEIGHT_PX = 800;
133 FAR_ICON_SIZE = farSize(2 * ICON_WIDTH_PX / (float)w);
134 }
135
136 private static float farSize(float sizeAt0) {
137 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
138 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700139 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700140
141 public AllAppsView(Context context, AttributeSet attrs) {
142 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700143 setFocusable(true);
144 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700145 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700146 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700147 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
148
Joe Onorato6665c0f2009-09-02 15:27:24 -0700149 setOnClickListener(this);
150 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700151 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700152 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700153 }
154
Joe Onorato7c312c12009-08-13 21:36:53 -0700155 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
156 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700157 }
158
Joe Onorato6665c0f2009-09-02 15:27:24 -0700159 public void setLauncher(Launcher launcher) {
160 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700161 }
162
Joe Onorato1feb3a82009-08-08 22:32:00 -0700163 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700164 public void surfaceDestroyed(SurfaceHolder holder) {
165 super.surfaceDestroyed(holder);
166
167 destroyRenderScript();
168 mRS = null;
169 mRollo = null;
170 }
171
172 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700173 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
174 super.surfaceChanged(holder, format, w, h);
175
Joe Onorato6665c0f2009-09-02 15:27:24 -0700176 long startTime = SystemClock.uptimeMillis();
177
Jason Sams05de32a2009-09-27 14:01:40 -0700178 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700179 mRollo = new RolloRS();
180 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 if (mAllAppsList != null) {
182 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700183 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700185
186 Resources res = getContext().getResources();
187 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700188 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700189
190 long endTime = SystemClock.uptimeMillis();
191 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700192 }
193
194 @Override
195 public boolean onKeyDown(int keyCode, KeyEvent event)
196 {
Joe Onorato93839052009-08-06 20:34:32 -0700197 // this method doesn't work when 'extends View' include 'extends ScrollView'.
198 return super.onKeyDown(keyCode, event);
199 }
200
Jason Samsd8152b92009-10-13 17:19:10 -0700201 private int mRSMode = 0;
202
Joe Onorato93839052009-08-06 20:34:32 -0700203 @Override
204 public boolean onTouchEvent(MotionEvent ev)
205 {
Joe Onorato7bb17492009-09-24 17:51:01 -0700206 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700207 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700208 }
209
Joe Onoratofb0ca672009-09-14 17:55:46 -0400210 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700211 return true;
212 }
213
214 super.onTouchEvent(ev);
215
Joe Onoratofb0ca672009-09-14 17:55:46 -0400216 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700217 int y = (int)ev.getY();
218
Joe Onoratobcbeab82009-10-01 21:45:43 -0700219 int action = ev.getAction();
220 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400221 case MotionEvent.ACTION_DOWN:
Jason Samsd8152b92009-10-13 17:19:10 -0700222 if (x < 60 && y > 700) {
Jason Samsb52dfa02009-10-14 20:16:14 -0700223 //mRotateMove = mRollo.setView((++mRSMode) & 3);
Jason Samsd8152b92009-10-13 17:19:10 -0700224 }
225
Joe Onoratobcbeab82009-10-01 21:45:43 -0700226 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
227 mTouchTracking = TRACKING_HOME;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700228 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700229 mTouchTracking = TRACKING_FLING;
230
231 mMotionDownRawX = (int)ev.getRawX();
232 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700233
Jason Samsd8152b92009-10-13 17:19:10 -0700234 if (mRotateMove) {
235 mRollo.mState.newPositionX = ev.getRawY() / mDefines.SCREEN_WIDTH_PX;
236 } else {
237 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
238 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700239 mRollo.mState.newTouchDown = 1;
240
241 if (!mRollo.checkClickOK()) {
242 mRollo.clearSelectedIcon();
243 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700244 mDownIconIndex = mCurrentIconIndex
245 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
246 if (mDownIconIndex < 0) {
247 // if nothing was selected, no long press.
248 cancelLongPress();
249 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700250 }
251 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700252 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700253 mVelocity = VelocityTracker.obtain();
254 mVelocity.addMovement(ev);
255 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700256 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400257 break;
258 case MotionEvent.ACTION_MOVE:
259 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700260 if (mTouchTracking == TRACKING_HOME) {
261 // TODO: highlight?
Joe Onorato68ffd102009-10-15 17:59:43 -0700262 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700263 int rawX = (int)ev.getRawX();
264 int rawY = (int)ev.getRawY();
265 int slop;
Jason Samsd8152b92009-10-13 17:19:10 -0700266 if (mRotateMove) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700267 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700268 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700269 slop = Math.abs(rawX - mMotionDownRawX);
Jason Samsd8152b92009-10-13 17:19:10 -0700270 }
271
Joe Onorato82ca5502009-10-15 16:59:23 -0700272 if (!mStartedScrolling && slop < mSlop) {
273 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700274 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700275 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
276 if (mDownIconIndex != mCurrentIconIndex) {
277 // If a different icon is selected, don't allow it to be picked up.
278 // This handles off-axis dragging.
279 cancelLongPress();
280 mCurrentIconIndex = -1;
281 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700282 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700283 if (!mStartedScrolling) {
284 cancelLongPress();
285 mCurrentIconIndex = -1;
286 }
Jason Samsd8152b92009-10-13 17:19:10 -0700287 if (mRotateMove) {
288 mRollo.mState.newPositionX = ev.getRawY() / mDefines.SCREEN_WIDTH_PX;
289 } else {
290 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
291 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700292 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700293 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700294
Joe Onoratobcbeab82009-10-01 21:45:43 -0700295 mStartedScrolling = true;
296 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700297 mVelocity.addMovement(ev);
298 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700299 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400300 }
301 break;
302 case MotionEvent.ACTION_UP:
303 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700304 if (mTouchTracking == TRACKING_HOME) {
305 if (action == MotionEvent.ACTION_UP) {
306 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
307 mLauncher.closeAllApps(true);
308 }
309 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700310 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700311 mRollo.mState.newTouchDown = 0;
Jason Samsd8152b92009-10-13 17:19:10 -0700312 if (mRotateMove) {
313 mRollo.mState.newPositionX = ev.getRawY() / mDefines.SCREEN_WIDTH_PX;
314 } else {
315 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
316 }
Jason Sams476339d2009-09-29 18:14:38 -0700317
Jason Sams12c14a82009-10-06 14:33:15 -0700318 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Samsd8152b92009-10-13 17:19:10 -0700319 if (mRotateMove) {
320 mRollo.mState.flingVelocityX
321 = mVelocity.getYVelocity() / mDefines.SCREEN_WIDTH_PX;
322 } else {
323 mRollo.mState.flingVelocityX
324 = mVelocity.getXVelocity() / mDefines.SCREEN_WIDTH_PX;
325 }
Jason Sams12c14a82009-10-06 14:33:15 -0700326 mRollo.clearSelectedIcon();
327 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700328 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700329
Joe Onorato539ed9d2009-10-02 10:22:14 -0700330 if (mVelocity != null) {
331 mVelocity.recycle();
332 mVelocity = null;
333 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700334 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700335 mTouchTracking = TRACKING_NONE;
336 break;
Joe Onorato93839052009-08-06 20:34:32 -0700337 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700338
339 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700340 }
341
Joe Onorato6665c0f2009-09-02 15:27:24 -0700342 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700343 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400344 return;
345 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700346 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
347 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
348 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700349 mLauncher.startActivitySafely(app.intent);
350 }
351 }
352
353 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700354 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400355 return true;
356 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700357 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
358 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
359 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700360
361 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700362 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
363 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700364
Joe Onoratobcbeab82009-10-01 21:45:43 -0700365 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
366 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700367 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700368 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700369 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700370
Joe Onorato7bb17492009-09-24 17:51:01 -0700371 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700372 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700373 return true;
374 }
375
Joe Onorato5162ea92009-09-03 09:39:42 -0700376 public void setDragController(DragController dragger) {
377 mDragController = dragger;
378 }
379
380 public void onDropCompleted(View target, boolean success) {
381 }
382
Joe Onorato4db52312009-10-06 11:17:43 -0700383 /**
384 * Zoom to the specifed amount.
385 *
386 * @param amount [0..1] 0 is hidden, 1 is open
387 * @param animate Whether to animate.
388 */
Jason Sams12c14a82009-10-06 14:33:15 -0700389 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700390 if (mRollo == null) {
391 return;
392 }
393
Joe Onoratofb0ca672009-09-14 17:55:46 -0400394 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400395 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700396 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700397 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700398 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700399 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700400 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700401 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400402 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400403 }
404
405 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700406 if (mRollo == null) {
407 return false;
408 }
Jason Sams12c14a82009-10-06 14:33:15 -0700409 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400410 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700411
Joe Onorato93839052009-08-06 20:34:32 -0700412 @Override
413 public boolean onTrackballEvent(MotionEvent ev)
414 {
415 float x = ev.getX();
416 float y = ev.getY();
417 //Float tx = new Float(x);
418 //Float ty = new Float(y);
419 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
420
421
422 return true;
423 }
424
Joe Onorato9c1289c2009-08-17 11:03:03 -0400425 public void setApps(ArrayList<ApplicationInfo> list) {
426 mAllAppsList = list;
427 if (mRollo != null) {
428 mRollo.setApps(list);
429 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700430 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400431 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700432 }
433
Joe Onoratoa8138d52009-10-06 19:25:30 -0700434 public void addApps(ArrayList<ApplicationInfo> list) {
435 final int N = list.size();
436 if (mRollo != null) {
437 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
438 }
439
440 for (int i=0; i<N; i++) {
441 final ApplicationInfo item = list.get(i);
442 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
443 if (index < 0) {
444 index = -(index+1);
445 }
446 mAllAppsList.add(index, item);
447 if (mRollo != null) {
448 mRollo.addApp(index, item);
449 mRollo.mState.iconCount++;
450 }
451 }
452
453 if (mRollo != null) {
454 mRollo.saveAppsList();
455 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700456 }
457
Joe Onoratoa8138d52009-10-06 19:25:30 -0700458 public void removeApps(ArrayList<ApplicationInfo> list) {
459 final int N = list.size();
460 for (int i=0; i<N; i++) {
461 final ApplicationInfo item = list.get(i);
462 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
463 if (index >= 0) {
464 mAllAppsList.remove(index);
465 if (mRollo != null) {
466 mRollo.removeApp(index);
467 mRollo.mState.iconCount--;
468 }
469 } else {
470 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
471 // Try to recover. This should keep us from crashing for now.
472 }
473 }
474
475 if (mRollo != null) {
476 mRollo.saveAppsList();
477 }
478 }
479
480 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
481 // Just remove and add, because they may need to be re-sorted.
482 removeApps(list);
483 addApps(list);
484 }
485
486 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
487 public int compare(ApplicationInfo a, ApplicationInfo b) {
488 int result = a.title.toString().compareTo(b.toString());
489 if (result != 0) {
490 return result;
491 }
492 return a.intent.getComponent().compareTo(b.intent.getComponent());
493 }
494 };
495
496 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
497 public int compare(ApplicationInfo a, ApplicationInfo b) {
498 return a.intent.getComponent().compareTo(b.intent.getComponent());
499 }
500 };
501
Joe Onoratoc567acb2009-08-31 14:34:43 -0700502 private static int countPages(int iconCount) {
503 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
504 int pages = iconCount / iconsPerPage;
505 if (pages*iconsPerPage != iconCount) {
506 pages++;
507 }
508 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400509 }
510
Joe Onorato93839052009-08-06 20:34:32 -0700511 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700512
Joe Onorato1feb3a82009-08-08 22:32:00 -0700513 // Allocations ======
Jason Samsd8152b92009-10-13 17:19:10 -0700514 private int mViewMode = 0;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700515
Joe Onorato93839052009-08-06 20:34:32 -0700516 private int mWidth;
517 private int mHeight;
518
519 private Resources mRes;
Jason Samsd8152b92009-10-13 17:19:10 -0700520 private Script[] mScript = new Script[4];
Jason Sams86c87ed2009-09-18 13:55:55 -0700521
Jason Samsd8152b92009-10-13 17:19:10 -0700522 private Script.Invokable[] mInvokeMove = new Script.Invokable[4];
523 private Script.Invokable[] mInvokeFling = new Script.Invokable[4];
Jason Sams41b61c82009-10-15 15:40:54 -0700524 private Script.Invokable[] mInvokeResetWAR = new Script.Invokable[4];
Jason Sams86c87ed2009-09-18 13:55:55 -0700525
Jason Samscd689e12009-09-29 15:28:22 -0700526 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700527 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700528 private ProgramFragment mPFColor;
529 private ProgramFragment mPFTexLinear;
530 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700531 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700532 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700533 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700534 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700535
Joe Onoratobcbeab82009-10-01 21:45:43 -0700536 private Allocation mHomeButton;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700537
Joe Onoratobf15cb42009-08-07 14:33:40 -0700538 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700539 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700540 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700541
Joe Onoratobf15cb42009-08-07 14:33:40 -0700542 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700543 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700544 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700545 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700546
Joe Onorato6665c0f2009-09-02 15:27:24 -0700547 private int[] mTouchYBorders;
548 private Allocation mAllocTouchYBorders;
549 private int[] mTouchXBorders;
550 private Allocation mAllocTouchXBorders;
551
552 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400553 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700554
Jason Samsd8152b92009-10-13 17:19:10 -0700555
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700556 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700557 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700558
Jason Sams78aebd82009-09-15 13:06:59 -0700559 class BaseAlloc {
560 Allocation mAlloc;
561 Type mType;
562
563 void save() {
564 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700565 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700566 }
567
Jason Sams12c14a82009-10-06 14:33:15 -0700568 class AAMessage extends RenderScript.RSMessage {
569 public void run() {
570 mPosX = ((float)mData[0]) / (1 << 16);
571 mVelocity = ((float)mData[1]) / (1 << 16);
572 mZoom = ((float)mData[2]) / (1 << 16);
573 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
574 }
575 float mZoom;
576 float mPosX;
577 float mVelocity;
578 }
579 AAMessage mMessageProc;
580
Jason Sams476339d2009-09-29 18:14:38 -0700581 private boolean checkClickOK() {
582 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams12c14a82009-10-06 14:33:15 -0700583 return (Math.abs(mMessageProc.mVelocity) < 0.1f) &&
584 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.1f);
Jason Sams476339d2009-09-29 18:14:38 -0700585 }
586
Jason Sams78aebd82009-09-15 13:06:59 -0700587 class Params extends BaseAlloc {
588 Params() {
589 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
590 mAlloc = Allocation.createTyped(mRS, mType);
591 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700592 }
Jason Sams78aebd82009-09-15 13:06:59 -0700593 public int bubbleWidth;
594 public int bubbleHeight;
595 public int bubbleBitmapWidth;
596 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700597
598 public int homeButtonId;
599 public int homeButtonWidth;
600 public int homeButtonHeight;
601 public int homeButtonTextureWidth;
602 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700603 }
604
605 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700606 public float newPositionX;
607 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700608 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700609 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700610 public int selectedIconIndex = -1;
611 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700612 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700613
614 State() {
615 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
616 mAlloc = Allocation.createTyped(mRS, mType);
617 save();
618 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700619 }
620
621 public RolloRS() {
622 }
623
624 public void init(Resources res, int width, int height) {
625 mRes = res;
626 mWidth = width;
627 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700628 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700629 initProgramVertex();
630 initProgramFragment();
631 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700632 initMesh();
Jason Samsd8152b92009-10-13 17:19:10 -0700633 initMesh2();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700634 initGl();
635 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700636 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700637 initRs();
638 }
639
Jason Sams0aa71662009-10-02 18:43:18 -0700640 public void initMesh() {
641 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
642 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
643
644 for (int ct=0; ct < 450; ct++) {
645 float x = 0;
646 float z = 0;
647 float l = 1.f;
648
649 if (ct < 190) {
650 z = 0.1f + 0.05f * (190 - ct);
651 x = -1;
652 l = 0.125f + (0.125f / 190.f) * ct;
653 } else if (ct >= 190 && ct < 200) {
654 float a = (3.14f * 0.5f) * (0.1f * (ct - 200));
655 float s = (float)Math.sin(a);
656 float c = (float)Math.cos(a);
657 x = -0.9f + s * 0.1f;
658 z = 0.1f - c * 0.1f;
659 l = 0.25f + 0.075f * (ct - 190);
660 } else if (ct >= 200 && ct < 250) {
661 z = 0.f;
662 x = -0.9f + (1.8f * (ct - 200) / 50.f);
663 } else if (ct >= 250 && ct < 260) {
664 float a = (3.14f * 0.5f) * (0.1f * (ct - 250));
665 float s = (float)Math.sin(a);
666 float c = (float)Math.cos(a);
667 x = 0.9f + s * 0.1f;
668 z = 0.1f - c * 0.1f;
669 l = 0.25f + 0.075f * (260 - ct);
670 } else if (ct >= 260) {
671 z = 0.1f + 0.05f * (ct - 260);
672 x = 1;
673 l = 0.125f + (0.125f / 190.f) * (450 - ct);
674 }
675 //Log.e("rs", "ct " + Integer.toString(ct) + " x = " + Float.toString(x) + ", z = " + Float.toString(z));
676 //Log.e("rs", "ct " + Integer.toString(ct) + " l = " + Float.toString(l));
677 float s = ct * 0.1f;
678 tm.setColor(l, l, l, 0.99f);
679 tm.setTexture(s, 1);
680 tm.addVertex(x, -0.5f, z);
681 tm.setTexture(s, 0);
682 tm.addVertex(x, 0.5f, z);
683 }
684 for (int ct=0; ct < (450*2 - 2); ct+= 2) {
685 tm.addTriangle(ct, ct+1, ct+2);
686 tm.addTriangle(ct+1, ct+3, ct+2);
687 }
688 mMesh = tm.create();
689 mMesh.setName("SMMesh");
690
691 }
692
693
Jason Samsd8152b92009-10-13 17:19:10 -0700694 public void initMesh2() {
695 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
696 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
697
698 float y = 0;
699 float z = 0;
700 for (int ct=0; ct < 200; ct++) {
701 float angle = 0;
702 float maxAngle = 3.14f * 0.16f;
703 float l = 1.f;
704
705 l = 1 - ((ct-5) * 0.10f);
706 if (ct > 7) {
707 angle = maxAngle * (ct - 7) * 0.2f;
708 angle = Math.min(angle, maxAngle);
709 }
710 l = Math.max(0.3f, l);
711 l = Math.min(1.0f, l);
712
713 y += 0.1f * Math.cos(angle);
714 z += 0.1f * Math.sin(angle);
715
716 float t = 0.1f * ct;
717 float ds = 0.08f;
718 tm.setColor(l, l, l, 0.99f);
719 tm.setTexture(ds, t);
720 tm.addVertex(-0.5f, y, z);
721 tm.setTexture(1 - ds, t);
722 tm.addVertex(0.5f, y, z);
723 }
724 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
725 tm.addTriangle(ct, ct+1, ct+2);
726 tm.addTriangle(ct+1, ct+3, ct+2);
727 }
728 mMesh2 = tm.create();
729 mMesh2.setName("SMMesh2");
730 }
731
Jason Samscd689e12009-09-29 15:28:22 -0700732 private void initProgramVertex() {
733 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
734 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700735
736 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700737 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700738 mPV = pvb.create();
739 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700740 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700741
Jason Samscd689e12009-09-29 15:28:22 -0700742 pva = new ProgramVertex.MatrixAllocation(mRS);
743 pva.setupOrthoWindow(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700744 pvb.setTextureMatrixEnable(true);
745 mPVOrtho = pvb.create();
746 mPVOrtho.setName("PVOrtho");
Jason Samscd689e12009-09-29 15:28:22 -0700747 mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700748
749 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700750 }
Joe Onorato93839052009-08-06 20:34:32 -0700751
Jason Samscd689e12009-09-29 15:28:22 -0700752 private void initProgramFragment() {
753 Sampler.Builder sb = new Sampler.Builder(mRS);
754 sb.setMin(Sampler.Value.LINEAR);
755 sb.setMag(Sampler.Value.LINEAR);
756 sb.setWrapS(Sampler.Value.CLAMP);
757 sb.setWrapT(Sampler.Value.CLAMP);
758 Sampler linear = sb.create();
759
760 sb.setMin(Sampler.Value.NEAREST);
761 sb.setMag(Sampler.Value.NEAREST);
762 Sampler nearest = sb.create();
763
764 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
765 mPFColor = bf.create();
766 mPFColor.setName("PFColor");
767
768 bf.setTexEnable(true, 0);
769 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
770 mPFTexLinear = bf.create();
771 mPFTexLinear.setName("PFTexLinear");
772 mPFTexLinear.bindSampler(linear, 0);
773
774 mPFTexNearest = bf.create();
775 mPFTexNearest.setName("PFTexNearest");
776 mPFTexNearest.bindSampler(nearest, 0);
777 }
778
779 private void initProgramStore() {
780 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
781 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700782 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700783 bs.setDitherEnable(true);
784 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
785 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
786 mPSIcons = bs.create();
787 mPSIcons.setName("PSIcons");
788
789 //bs.setDitherEnable(false);
790 //mPSText = bs.create();
791 //mPSText.setName("PSText");
792 }
793
794 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700795 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700796 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700797 mTouchXBorders.length);
798 mAllocTouchXBorders.data(mTouchXBorders);
799
800 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700801 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700802 mTouchYBorders.length);
803 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700804 }
Jason Sams78aebd82009-09-15 13:06:59 -0700805
Joe Onorato1feb3a82009-08-08 22:32:00 -0700806 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700807 mParams = new Params();
808 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700809
Joe Onoratobf15cb42009-08-07 14:33:40 -0700810 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700811
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700812 mParams.bubbleWidth = bubble.getBubbleWidth();
813 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
814 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
815 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700816
Joe Onoratobcbeab82009-10-01 21:45:43 -0700817 mHomeButton = Allocation.createFromBitmapResource(mRS, mRes,
818 R.drawable.home_button, Element.RGBA_8888(mRS), false);
819 mHomeButton.uploadToTexture(0);
820 mParams.homeButtonId = mHomeButton.getID();
821 mParams.homeButtonWidth = 76;
822 mParams.homeButtonHeight = 68;
823 mParams.homeButtonTextureWidth = 128;
824 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700825
Joe Onorato1feb3a82009-08-08 22:32:00 -0700826 mParams.save();
827 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400828
Joe Onorato1291a8c2009-09-15 15:07:25 -0400829 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
830 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
831 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700832
Joe Onorato9c1289c2009-08-17 11:03:03 -0400833 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700834 }
835
Jason Samsd8152b92009-10-13 17:19:10 -0700836 private void initScript(int idx, int id) {
Joe Onorato93839052009-08-06 20:34:32 -0700837 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Samsd8152b92009-10-13 17:19:10 -0700838 sb.setScript(mRes, id);
Joe Onorato93839052009-08-06 20:34:32 -0700839 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700840 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700841 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
842 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Samsd8152b92009-10-13 17:19:10 -0700843 mInvokeMove[idx] = sb.addInvokable("move");
844 mInvokeFling[idx] = sb.addInvokable("fling");
Jason Sams41b61c82009-10-15 15:40:54 -0700845 mInvokeResetWAR[idx] = sb.addInvokable("resetHWWar");
Jason Samsd8152b92009-10-13 17:19:10 -0700846 mScript[idx] = sb.create();
847 mScript[idx].setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
848 mScript[idx].bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
849 mScript[idx].bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
850 mScript[idx].bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
851 mScript[idx].bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
852 mScript[idx].bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
853 mScript[idx].bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
854 }
Joe Onorato93839052009-08-06 20:34:32 -0700855
Jason Samsd8152b92009-10-13 17:19:10 -0700856 private void initRs() {
857 mViewMode = 0;
858 initScript(0, R.raw.rollo3);
859 initScript(1, R.raw.rollo2);
860 initScript(2, R.raw.rollo);
861 initScript(3, R.raw.rollo4);
Joe Onorato93839052009-08-06 20:34:32 -0700862
Jason Sams12c14a82009-10-06 14:33:15 -0700863 mMessageProc = new AAMessage();
864 mRS.mMessageCallback = mMessageProc;
Jason Samsd8152b92009-10-13 17:19:10 -0700865 mRS.contextBindRootScript(mScript[mViewMode]);
Joe Onorato93839052009-08-06 20:34:32 -0700866 }
Joe Onorato93839052009-08-06 20:34:32 -0700867
Joe Onorato9c1289c2009-08-17 11:03:03 -0400868 private void setApps(ArrayList<ApplicationInfo> list) {
869 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700870 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700871 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700872 allocCount = 1;
873 }
874
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700876 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700877 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878
879 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700880 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700881 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700883 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400884
885 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
886
887 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700888 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400889 }
890
Joe Onorato9c1289c2009-08-17 11:03:03 -0400891 mState.iconCount = count;
892
Joe Onoratoa8138d52009-10-06 19:25:30 -0700893 saveAppsList();
894 }
895
896 private void uploadAppIcon(int index, ApplicationInfo item) {
897 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
898 Element.RGBA_8888(mRS), false);
899 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
900 Element.RGBA_8888(mRS), false);
901
902 mIcons[index].uploadToTexture(0);
903 mLabels[index].uploadToTexture(0);
904
905 mIconIds[index] = mIcons[index].getID();
906 mLabelIds[index] = mLabels[index].getID();
907 }
908
909 /**
910 * Puts the empty spaces at the end. Updates mState.iconCount. You must
911 * fill in the values and call saveAppsList().
912 */
913 private void reallocAppsList(int count) {
914 Allocation[] icons = new Allocation[count];
915 int[] iconIds = new int[count];
916 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
917
918 Allocation[] labels = new Allocation[count];
919 int[] labelIds = new int[count];
920 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
921
922 final int oldCount = mIcons.length;
923
924 System.arraycopy(mIcons, 0, icons, 0, oldCount);
925 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
926 System.arraycopy(mLabels, 0, labels, 0, oldCount);
927 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
928
929 mIcons = icons;
930 mIconIds = iconIds;
931 mLabels = labels;
932 mLabelIds = labelIds;
933 }
934
935 /**
936 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
937 */
938 private void addApp(int index, ApplicationInfo item) {
939 final int count = mState.iconCount - index;
940 final int dest = index + 1;
941
942 System.arraycopy(mIcons, index, mIcons, dest, count);
943 System.arraycopy(mIconIds, index, mIconIds, dest, count);
944 System.arraycopy(mLabels, index, mLabels, dest, count);
945 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
946
947 uploadAppIcon(index, item);
948 }
949
950 /**
951 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
952 */
953 private void removeApp(int index) {
954 final int count = mState.iconCount - index - 1;
955 final int src = index + 1;
956
957 System.arraycopy(mIcons, src, mIcons, index, count);
958 System.arraycopy(mIconIds, src, mIconIds, index, count);
959 System.arraycopy(mLabels, src, mLabels, index, count);
960 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
961
962 final int last = mState.iconCount - 1;
963 mIcons[last] = null;
964 mIconIds[last] = 0;
965 mLabels[last] = null;
966 mLabelIds[last] = 0;
967 }
968
969 /**
970 * Send the apps list structures to RS.
971 */
972 private void saveAppsList() {
973 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -0700974
Joe Onoratoa8138d52009-10-06 19:25:30 -0700975 mAllocIconIds.data(mIconIds);
976 mAllocLabelIds.data(mLabelIds);
977
Jason Samsd8152b92009-10-13 17:19:10 -0700978 if (mScript[0] != null) { // this happens when we init it
979 for (int ct=0; ct < 4; ct++) {
980 mScript[ct].bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
981 mScript[ct].bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
982 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400983 }
984
985 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -0700986
987 // Note: mScript may be null if we haven't initialized it yet.
988 // In that case, this is a no-op.
Jason Sams41b61c82009-10-15 15:40:54 -0700989 if (mInvokeResetWAR != null &&
990 mInvokeResetWAR[mViewMode] != null) {
991 mInvokeResetWAR[mViewMode].execute();
992 }
Jason Samsd8152b92009-10-13 17:19:10 -0700993 mRS.contextBindRootScript(mScript[mViewMode]);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700995
996 void initTouchState() {
997 int width = getWidth();
998 int height = getHeight();
999
1000 int iconsSize;
1001 if (width < height) {
1002 iconsSize = width;
1003 } else {
1004 iconsSize = height;
1005 }
1006 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
1007 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
1008
Joe Onorato56848b02009-09-25 13:59:59 -07001009 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
1010 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
1011 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001012 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -07001013 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
1014 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001015
1016 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -07001017
Joe Onorato6665c0f2009-09-02 15:27:24 -07001018 int centerX = (width / 2);
1019 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -07001020 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001021 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -07001022 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001023 mTouchXBorders[4] = centerX + (2 * cellWidth);
1024
1025 mAllocTouchXBorders.data(mTouchXBorders);
1026 }
1027
Jason Samsb52dfa02009-10-14 20:16:14 -07001028 int chooseTappedIconHorz(int x, int y, float page) {
Jason Sams86c87ed2009-09-18 13:55:55 -07001029 int currentPage = (int)page;
1030
Joe Onorato6665c0f2009-09-02 15:27:24 -07001031 int col = -1;
1032 int row = -1;
1033
1034 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1035 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1036 col = i;
1037 break;
1038 }
1039 }
1040 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1041 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1042 row = i;
1043 break;
1044 }
1045 }
1046
1047 if (row < 0 || col < 0) {
1048 return -1;
1049 }
1050
Jason Sams78aebd82009-09-15 13:06:59 -07001051 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001052 + (row * Defines.ROWS_PER_PAGE) + col;
1053 }
1054
Jason Samsb52dfa02009-10-14 20:16:14 -07001055 int chooseTappedIconVert(int x, int y, float pos) {
1056 int ydead = (getHeight() - 4 * 145) / 2;
1057 if (y < ydead || y > (getHeight() - ydead)) {
1058 return -1;
1059 }
1060
1061 y -= ydead;
1062 y += pos * 145;
1063 int row = y / 145;
1064 int col = x / 120;
1065
1066 return row * 4 + col;
1067 }
1068
Joe Onorato82ca5502009-10-15 16:59:23 -07001069 int chooseTappedIcon(int x, int y, float pos) {
1070 int index;
1071 if (mViewMode != 0) {
1072 index = chooseTappedIconHorz(x, y, pos);
1073 } else {
1074 index = chooseTappedIconVert(x, y, pos);
1075 }
1076 final int iconCount = mAllAppsList.size();
1077 if (index >= iconCount) {
1078 index = -1;
1079 }
1080 return index;
1081 }
1082
Jason Samsd8152b92009-10-13 17:19:10 -07001083 boolean setView(int v) {
1084 mViewMode = v;
1085 mRS.contextBindRootScript(mScript[mViewMode]);
1086 return (v == 0);
1087 }
1088
1089 void fling() {
1090 mInvokeFling[mViewMode].execute();
1091 }
1092
1093 void move() {
1094 mInvokeMove[mViewMode].execute();
1095 }
1096
Joe Onorato6665c0f2009-09-02 15:27:24 -07001097 /**
1098 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001099 *
1100 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001101 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001102 int selectIcon(int x, int y, float pos) {
1103 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001104 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001105 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001106 }
1107
1108 void selectIcon(int index) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001109 if (index < 0) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001110 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001111 } else {
1112 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001113
1114 Bitmap selectionBitmap = mSelectionBitmap;
1115
1116 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1117 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1118 mAllAppsList.get(index).iconBitmap);
1119
1120 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001121 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001122 mSelectedIcon.uploadToTexture(0);
1123 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001124 }
1125 }
1126
1127 /**
1128 * You need to call save() on mState on your own after calling this.
1129 */
1130 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001131 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001132 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001133
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 }
Joe Onorato93839052009-08-06 20:34:32 -07001135}
1136
1137