blob: 4d340cab9632c720d630b170828e179786ef0027 [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 Onoratobcbeab82009-10-01 21:45:43 -070072 private static final int TRACKING_FLING = 0;
73 private static final int TRACKING_HOME = 1;
74
Joe Onorato6665c0f2009-09-02 15:27:24 -070075 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070076 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040077
78 /** When this is 0, modifications are allowed, when it's not, they're not.
79 * TODO: What about scrolling? */
80 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070081
Joe Onoratof7b0e012009-10-01 14:09:15 -070082 private int mSlopX;
83 private int mMaxFlingVelocity;
84
Joe Onoratobcbeab82009-10-01 21:45:43 -070085 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070086 private RenderScript mRS;
87 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040088 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070089
Joe Onoratoc567acb2009-08-31 14:34:43 -070090 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070091 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070092 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -070093 private int mTouchTracking;
Joe Onorato1feb3a82009-08-08 22:32:00 -070094 private int mLastMotionX;
Jason Samsd8152b92009-10-13 17:19:10 -070095 private int mLastMotionY;
Joe Onorato5162ea92009-09-03 09:39:42 -070096 private int mMotionDownRawX;
97 private int mMotionDownRawY;
Joe Onoratobcbeab82009-10-01 21:45:43 -070098 private int mHomeButtonTop;
Jason Sams86c87ed2009-09-18 13:55:55 -070099 private long mTouchTime;
Jason Samsd8152b92009-10-13 17:19:10 -0700100 private boolean mRotateMove = true;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700101
Joe Onorato6665c0f2009-09-02 15:27:24 -0700102 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700103 public static final int ALLOC_PARAMS = 0;
104 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700105 public static final int ALLOC_ICON_IDS = 3;
106 public static final int ALLOC_LABEL_IDS = 4;
107 public static final int ALLOC_X_BORDERS = 5;
108 public static final int ALLOC_Y_BORDERS = 6;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700109
110 public static final int COLUMNS_PER_PAGE = 4;
111 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700112
Joe Onorato6665c0f2009-09-02 15:27:24 -0700113 public static final float RADIUS = 4.0f;
114
Joe Onorato6665c0f2009-09-02 15:27:24 -0700115 public static final int ICON_WIDTH_PX = 64;
116 public static final int ICON_TEXTURE_WIDTH_PX = 128;
117
118 public static final int ICON_HEIGHT_PX = 64;
119 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
120 public static final float ICON_TOP_OFFSET = 0.2f;
121
122 public static final float CAMERA_Z = -2;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700123
124 public int SCREEN_WIDTH_PX;
125 public int SCREEN_HEIGHT_PX;
126
127 public float FAR_ICON_SIZE;
128
129 public void recompute(int w, int h) {
130 SCREEN_WIDTH_PX = 480;
131 SCREEN_HEIGHT_PX = 800;
132 FAR_ICON_SIZE = farSize(2 * ICON_WIDTH_PX / (float)w);
133 }
134
135 private static float farSize(float sizeAt0) {
136 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
137 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700138 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700139
140 public AllAppsView(Context context, AttributeSet attrs) {
141 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700142 setFocusable(true);
143 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700144 final ViewConfiguration config = ViewConfiguration.get(context);
145 mSlopX = config.getScaledTouchSlop();
146 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
147
Joe Onorato6665c0f2009-09-02 15:27:24 -0700148 setOnClickListener(this);
149 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700150 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700151 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700152 }
153
Joe Onorato7c312c12009-08-13 21:36:53 -0700154 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
155 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700156 }
157
Joe Onorato6665c0f2009-09-02 15:27:24 -0700158 public void setLauncher(Launcher launcher) {
159 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700160 }
161
Joe Onorato1feb3a82009-08-08 22:32:00 -0700162 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700163 public void surfaceDestroyed(SurfaceHolder holder) {
164 super.surfaceDestroyed(holder);
165
166 destroyRenderScript();
167 mRS = null;
168 mRollo = null;
169 }
170
171 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700172 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
173 super.surfaceChanged(holder, format, w, h);
174
Joe Onorato6665c0f2009-09-02 15:27:24 -0700175 long startTime = SystemClock.uptimeMillis();
176
Jason Sams05de32a2009-09-27 14:01:40 -0700177 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700178 mRollo = new RolloRS();
179 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400180 if (mAllAppsList != null) {
181 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700182 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700184
185 Resources res = getContext().getResources();
186 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700187 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700188
189 long endTime = SystemClock.uptimeMillis();
190 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700191 }
192
193 @Override
194 public boolean onKeyDown(int keyCode, KeyEvent event)
195 {
Joe Onorato93839052009-08-06 20:34:32 -0700196 // this method doesn't work when 'extends View' include 'extends ScrollView'.
197 return super.onKeyDown(keyCode, event);
198 }
199
Jason Samsd8152b92009-10-13 17:19:10 -0700200 private int mRSMode = 0;
201
Joe Onorato93839052009-08-06 20:34:32 -0700202 @Override
203 public boolean onTouchEvent(MotionEvent ev)
204 {
Joe Onorato7bb17492009-09-24 17:51:01 -0700205 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700206 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700207 }
208
Joe Onoratofb0ca672009-09-14 17:55:46 -0400209 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700210 return true;
211 }
212
213 super.onTouchEvent(ev);
214
Joe Onoratofb0ca672009-09-14 17:55:46 -0400215 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700216 int y = (int)ev.getY();
217
Joe Onoratobcbeab82009-10-01 21:45:43 -0700218 int action = ev.getAction();
219 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400220 case MotionEvent.ACTION_DOWN:
Jason Samsd8152b92009-10-13 17:19:10 -0700221 if (x < 60 && y > 700) {
222 mRotateMove = mRollo.setView((++mRSMode) & 3);
223 }
224
Joe Onoratobcbeab82009-10-01 21:45:43 -0700225 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
226 mTouchTracking = TRACKING_HOME;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700227 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700228 mTouchTracking = TRACKING_FLING;
229
230 mMotionDownRawX = (int)ev.getRawX();
231 mMotionDownRawY = (int)ev.getRawY();
232 mLastMotionX = x;
Jason Samsd8152b92009-10-13 17:19:10 -0700233 mLastMotionY = y;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700234
Jason Samsd8152b92009-10-13 17:19:10 -0700235 if (mRotateMove) {
236 mRollo.mState.newPositionX = ev.getRawY() / mDefines.SCREEN_WIDTH_PX;
237 } else {
238 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
239 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700240 mRollo.mState.newTouchDown = 1;
241
242 if (!mRollo.checkClickOK()) {
243 mRollo.clearSelectedIcon();
244 } else {
Jason Sams12c14a82009-10-06 14:33:15 -0700245 mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700246 }
247 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700248 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700249 mVelocity = VelocityTracker.obtain();
250 mVelocity.addMovement(ev);
251 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700252 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400253 break;
254 case MotionEvent.ACTION_MOVE:
255 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700256 if (mTouchTracking == TRACKING_HOME) {
257 // TODO: highlight?
Joe Onoratofb0ca672009-09-14 17:55:46 -0400258 } else {
Jason Samsd8152b92009-10-13 17:19:10 -0700259 int slopX;
260 if (mRotateMove) {
261 slopX = Math.abs(y - mLastMotionY);
262 } else {
263 slopX = Math.abs(x - mLastMotionX);
264 }
265
Joe Onoratobcbeab82009-10-01 21:45:43 -0700266 if (!mStartedScrolling && slopX < mSlopX) {
267 // don't update mLastMotionX so slopX is right and when we do start scrolling
268 // below, we get the right delta.
269 } else {
Jason Samsd8152b92009-10-13 17:19:10 -0700270 if (mRotateMove) {
271 mRollo.mState.newPositionX = ev.getRawY() / mDefines.SCREEN_WIDTH_PX;
272 } else {
273 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
274 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700275 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700276 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700277
Joe Onoratobcbeab82009-10-01 21:45:43 -0700278 mStartedScrolling = true;
279 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700280 mVelocity.addMovement(ev);
281 mRollo.mState.save();
282 mLastMotionX = x;
Jason Samsd8152b92009-10-13 17:19:10 -0700283 mLastMotionY = y;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700284 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400285 }
286 break;
287 case MotionEvent.ACTION_UP:
288 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700289 if (mTouchTracking == TRACKING_HOME) {
290 if (action == MotionEvent.ACTION_UP) {
291 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
292 mLauncher.closeAllApps(true);
293 }
294 }
Jason Sams476339d2009-09-29 18:14:38 -0700295 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700296 mRollo.mState.newTouchDown = 0;
Jason Samsd8152b92009-10-13 17:19:10 -0700297 if (mRotateMove) {
298 mRollo.mState.newPositionX = ev.getRawY() / mDefines.SCREEN_WIDTH_PX;
299 } else {
300 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
301 }
Jason Sams476339d2009-09-29 18:14:38 -0700302
Jason Sams12c14a82009-10-06 14:33:15 -0700303 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Samsd8152b92009-10-13 17:19:10 -0700304 if (mRotateMove) {
305 mRollo.mState.flingVelocityX
306 = mVelocity.getYVelocity() / mDefines.SCREEN_WIDTH_PX;
307 } else {
308 mRollo.mState.flingVelocityX
309 = mVelocity.getXVelocity() / mDefines.SCREEN_WIDTH_PX;
310 }
Jason Sams12c14a82009-10-06 14:33:15 -0700311 mRollo.clearSelectedIcon();
312 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700313 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700314
315 mLastMotionX = -10000;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700316 if (mVelocity != null) {
317 mVelocity.recycle();
318 mVelocity = null;
319 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700320 break;
321 }
Joe Onorato93839052009-08-06 20:34:32 -0700322 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700323
324 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700325 }
326
Joe Onorato6665c0f2009-09-02 15:27:24 -0700327 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700328 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400329 return;
330 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700331 int index = mRollo.mState.selectedIconIndex;
Jason Sams476339d2009-09-29 18:14:38 -0700332 if (mRollo.checkClickOK() && index >= 0 && index < mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700333 ApplicationInfo app = mAllAppsList.get(index);
334 mLauncher.startActivitySafely(app.intent);
335 }
336 }
337
338 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700339 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400340 return true;
341 }
Joe Onorato5162ea92009-09-03 09:39:42 -0700342 int index = mRollo.mState.selectedIconIndex;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700343
Jason Sams476339d2009-09-29 18:14:38 -0700344 if (mRollo.checkClickOK() && index >= 0 && index < mAllAppsList.size()) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700345 ApplicationInfo app = mAllAppsList.get(index);
346
347 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700348 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
349 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700350
Joe Onoratobcbeab82009-10-01 21:45:43 -0700351 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
352 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700353 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700354 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700355 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700356
Joe Onorato7bb17492009-09-24 17:51:01 -0700357 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700358 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700359 return true;
360 }
361
Joe Onorato5162ea92009-09-03 09:39:42 -0700362 public void setDragController(DragController dragger) {
363 mDragController = dragger;
364 }
365
366 public void onDropCompleted(View target, boolean success) {
367 }
368
Joe Onorato4db52312009-10-06 11:17:43 -0700369 /**
370 * Zoom to the specifed amount.
371 *
372 * @param amount [0..1] 0 is hidden, 1 is open
373 * @param animate Whether to animate.
374 */
Jason Sams12c14a82009-10-06 14:33:15 -0700375 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700376 if (mRollo == null) {
377 return;
378 }
379
Joe Onoratofb0ca672009-09-14 17:55:46 -0400380 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400381 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700382 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700383 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700384 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700385 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700386 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700387 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400388 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400389 }
390
391 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700392 if (mRollo == null) {
393 return false;
394 }
Jason Sams12c14a82009-10-06 14:33:15 -0700395 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400396 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700397
Joe Onorato93839052009-08-06 20:34:32 -0700398 @Override
399 public boolean onTrackballEvent(MotionEvent ev)
400 {
401 float x = ev.getX();
402 float y = ev.getY();
403 //Float tx = new Float(x);
404 //Float ty = new Float(y);
405 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
406
407
408 return true;
409 }
410
Joe Onorato9c1289c2009-08-17 11:03:03 -0400411 public void setApps(ArrayList<ApplicationInfo> list) {
412 mAllAppsList = list;
413 if (mRollo != null) {
414 mRollo.setApps(list);
415 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700416 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400417 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700418 }
419
Joe Onoratoa8138d52009-10-06 19:25:30 -0700420 public void addApps(ArrayList<ApplicationInfo> list) {
421 final int N = list.size();
422 if (mRollo != null) {
423 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
424 }
425
426 for (int i=0; i<N; i++) {
427 final ApplicationInfo item = list.get(i);
428 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
429 if (index < 0) {
430 index = -(index+1);
431 }
432 mAllAppsList.add(index, item);
433 if (mRollo != null) {
434 mRollo.addApp(index, item);
435 mRollo.mState.iconCount++;
436 }
437 }
438
439 if (mRollo != null) {
440 mRollo.saveAppsList();
441 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700442 }
443
Joe Onoratoa8138d52009-10-06 19:25:30 -0700444 public void removeApps(ArrayList<ApplicationInfo> list) {
445 final int N = list.size();
446 for (int i=0; i<N; i++) {
447 final ApplicationInfo item = list.get(i);
448 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
449 if (index >= 0) {
450 mAllAppsList.remove(index);
451 if (mRollo != null) {
452 mRollo.removeApp(index);
453 mRollo.mState.iconCount--;
454 }
455 } else {
456 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
457 // Try to recover. This should keep us from crashing for now.
458 }
459 }
460
461 if (mRollo != null) {
462 mRollo.saveAppsList();
463 }
464 }
465
466 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
467 // Just remove and add, because they may need to be re-sorted.
468 removeApps(list);
469 addApps(list);
470 }
471
472 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
473 public int compare(ApplicationInfo a, ApplicationInfo b) {
474 int result = a.title.toString().compareTo(b.toString());
475 if (result != 0) {
476 return result;
477 }
478 return a.intent.getComponent().compareTo(b.intent.getComponent());
479 }
480 };
481
482 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
483 public int compare(ApplicationInfo a, ApplicationInfo b) {
484 return a.intent.getComponent().compareTo(b.intent.getComponent());
485 }
486 };
487
Joe Onoratoc567acb2009-08-31 14:34:43 -0700488 private static int countPages(int iconCount) {
489 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
490 int pages = iconCount / iconsPerPage;
491 if (pages*iconsPerPage != iconCount) {
492 pages++;
493 }
494 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400495 }
496
Joe Onorato93839052009-08-06 20:34:32 -0700497 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700498
Joe Onorato1feb3a82009-08-08 22:32:00 -0700499 // Allocations ======
Jason Samsd8152b92009-10-13 17:19:10 -0700500 private int mViewMode = 0;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700501
Joe Onorato93839052009-08-06 20:34:32 -0700502 private int mWidth;
503 private int mHeight;
504
505 private Resources mRes;
Jason Samsd8152b92009-10-13 17:19:10 -0700506 private Script[] mScript = new Script[4];
Jason Sams86c87ed2009-09-18 13:55:55 -0700507
Jason Samsd8152b92009-10-13 17:19:10 -0700508 private Script.Invokable[] mInvokeMove = new Script.Invokable[4];
509 private Script.Invokable[] mInvokeFling = new Script.Invokable[4];
Jason Sams86c87ed2009-09-18 13:55:55 -0700510
Jason Samscd689e12009-09-29 15:28:22 -0700511 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700512 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700513 private ProgramFragment mPFColor;
514 private ProgramFragment mPFTexLinear;
515 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700516 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700517 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700518 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700519 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700520
Joe Onoratobcbeab82009-10-01 21:45:43 -0700521 private Allocation mHomeButton;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700522
Joe Onoratobf15cb42009-08-07 14:33:40 -0700523 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700524 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700525 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700526
Joe Onoratobf15cb42009-08-07 14:33:40 -0700527 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700528 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700529 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700530 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700531
Joe Onorato6665c0f2009-09-02 15:27:24 -0700532 private int[] mTouchYBorders;
533 private Allocation mAllocTouchYBorders;
534 private int[] mTouchXBorders;
535 private Allocation mAllocTouchXBorders;
536
537 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400538 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700539
Jason Samsd8152b92009-10-13 17:19:10 -0700540
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700541 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700542 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700543
Jason Sams78aebd82009-09-15 13:06:59 -0700544 class BaseAlloc {
545 Allocation mAlloc;
546 Type mType;
547
548 void save() {
549 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700550 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700551 }
552
Jason Sams12c14a82009-10-06 14:33:15 -0700553 class AAMessage extends RenderScript.RSMessage {
554 public void run() {
555 mPosX = ((float)mData[0]) / (1 << 16);
556 mVelocity = ((float)mData[1]) / (1 << 16);
557 mZoom = ((float)mData[2]) / (1 << 16);
558 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
559 }
560 float mZoom;
561 float mPosX;
562 float mVelocity;
563 }
564 AAMessage mMessageProc;
565
Jason Sams476339d2009-09-29 18:14:38 -0700566 private boolean checkClickOK() {
567 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams12c14a82009-10-06 14:33:15 -0700568 return (Math.abs(mMessageProc.mVelocity) < 0.1f) &&
569 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.1f);
Jason Sams476339d2009-09-29 18:14:38 -0700570 }
571
Jason Sams78aebd82009-09-15 13:06:59 -0700572 class Params extends BaseAlloc {
573 Params() {
574 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
575 mAlloc = Allocation.createTyped(mRS, mType);
576 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700577 }
Jason Sams78aebd82009-09-15 13:06:59 -0700578 public int bubbleWidth;
579 public int bubbleHeight;
580 public int bubbleBitmapWidth;
581 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700582
583 public int homeButtonId;
584 public int homeButtonWidth;
585 public int homeButtonHeight;
586 public int homeButtonTextureWidth;
587 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700588 }
589
590 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700591 public float newPositionX;
592 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700593 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700594 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700595 public int selectedIconIndex = -1;
596 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700597 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700598
599 State() {
600 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
601 mAlloc = Allocation.createTyped(mRS, mType);
602 save();
603 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700604 }
605
606 public RolloRS() {
607 }
608
609 public void init(Resources res, int width, int height) {
610 mRes = res;
611 mWidth = width;
612 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700613 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700614 initProgramVertex();
615 initProgramFragment();
616 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700617 initMesh();
Jason Samsd8152b92009-10-13 17:19:10 -0700618 initMesh2();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700619 initGl();
620 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700621 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700622 initRs();
623 }
624
Jason Sams0aa71662009-10-02 18:43:18 -0700625 public void initMesh() {
626 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
627 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
628
629 for (int ct=0; ct < 450; ct++) {
630 float x = 0;
631 float z = 0;
632 float l = 1.f;
633
634 if (ct < 190) {
635 z = 0.1f + 0.05f * (190 - ct);
636 x = -1;
637 l = 0.125f + (0.125f / 190.f) * ct;
638 } else if (ct >= 190 && ct < 200) {
639 float a = (3.14f * 0.5f) * (0.1f * (ct - 200));
640 float s = (float)Math.sin(a);
641 float c = (float)Math.cos(a);
642 x = -0.9f + s * 0.1f;
643 z = 0.1f - c * 0.1f;
644 l = 0.25f + 0.075f * (ct - 190);
645 } else if (ct >= 200 && ct < 250) {
646 z = 0.f;
647 x = -0.9f + (1.8f * (ct - 200) / 50.f);
648 } else if (ct >= 250 && ct < 260) {
649 float a = (3.14f * 0.5f) * (0.1f * (ct - 250));
650 float s = (float)Math.sin(a);
651 float c = (float)Math.cos(a);
652 x = 0.9f + s * 0.1f;
653 z = 0.1f - c * 0.1f;
654 l = 0.25f + 0.075f * (260 - ct);
655 } else if (ct >= 260) {
656 z = 0.1f + 0.05f * (ct - 260);
657 x = 1;
658 l = 0.125f + (0.125f / 190.f) * (450 - ct);
659 }
660 //Log.e("rs", "ct " + Integer.toString(ct) + " x = " + Float.toString(x) + ", z = " + Float.toString(z));
661 //Log.e("rs", "ct " + Integer.toString(ct) + " l = " + Float.toString(l));
662 float s = ct * 0.1f;
663 tm.setColor(l, l, l, 0.99f);
664 tm.setTexture(s, 1);
665 tm.addVertex(x, -0.5f, z);
666 tm.setTexture(s, 0);
667 tm.addVertex(x, 0.5f, z);
668 }
669 for (int ct=0; ct < (450*2 - 2); ct+= 2) {
670 tm.addTriangle(ct, ct+1, ct+2);
671 tm.addTriangle(ct+1, ct+3, ct+2);
672 }
673 mMesh = tm.create();
674 mMesh.setName("SMMesh");
675
676 }
677
678
Jason Samsd8152b92009-10-13 17:19:10 -0700679 public void initMesh2() {
680 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
681 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
682
683 float y = 0;
684 float z = 0;
685 for (int ct=0; ct < 200; ct++) {
686 float angle = 0;
687 float maxAngle = 3.14f * 0.16f;
688 float l = 1.f;
689
690 l = 1 - ((ct-5) * 0.10f);
691 if (ct > 7) {
692 angle = maxAngle * (ct - 7) * 0.2f;
693 angle = Math.min(angle, maxAngle);
694 }
695 l = Math.max(0.3f, l);
696 l = Math.min(1.0f, l);
697
698 y += 0.1f * Math.cos(angle);
699 z += 0.1f * Math.sin(angle);
700
701 float t = 0.1f * ct;
702 float ds = 0.08f;
703 tm.setColor(l, l, l, 0.99f);
704 tm.setTexture(ds, t);
705 tm.addVertex(-0.5f, y, z);
706 tm.setTexture(1 - ds, t);
707 tm.addVertex(0.5f, y, z);
708 }
709 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
710 tm.addTriangle(ct, ct+1, ct+2);
711 tm.addTriangle(ct+1, ct+3, ct+2);
712 }
713 mMesh2 = tm.create();
714 mMesh2.setName("SMMesh2");
715 }
716
Jason Samscd689e12009-09-29 15:28:22 -0700717 private void initProgramVertex() {
718 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
719 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700720
721 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700722 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700723 mPV = pvb.create();
724 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700725 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700726
Jason Samscd689e12009-09-29 15:28:22 -0700727 pva = new ProgramVertex.MatrixAllocation(mRS);
728 pva.setupOrthoWindow(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700729 pvb.setTextureMatrixEnable(true);
730 mPVOrtho = pvb.create();
731 mPVOrtho.setName("PVOrtho");
Jason Samscd689e12009-09-29 15:28:22 -0700732 mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700733
734 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700735 }
Joe Onorato93839052009-08-06 20:34:32 -0700736
Jason Samscd689e12009-09-29 15:28:22 -0700737 private void initProgramFragment() {
738 Sampler.Builder sb = new Sampler.Builder(mRS);
739 sb.setMin(Sampler.Value.LINEAR);
740 sb.setMag(Sampler.Value.LINEAR);
741 sb.setWrapS(Sampler.Value.CLAMP);
742 sb.setWrapT(Sampler.Value.CLAMP);
743 Sampler linear = sb.create();
744
745 sb.setMin(Sampler.Value.NEAREST);
746 sb.setMag(Sampler.Value.NEAREST);
747 Sampler nearest = sb.create();
748
749 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
750 mPFColor = bf.create();
751 mPFColor.setName("PFColor");
752
753 bf.setTexEnable(true, 0);
754 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
755 mPFTexLinear = bf.create();
756 mPFTexLinear.setName("PFTexLinear");
757 mPFTexLinear.bindSampler(linear, 0);
758
759 mPFTexNearest = bf.create();
760 mPFTexNearest.setName("PFTexNearest");
761 mPFTexNearest.bindSampler(nearest, 0);
762 }
763
764 private void initProgramStore() {
765 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
766 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700767 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700768 bs.setDitherEnable(true);
769 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
770 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
771 mPSIcons = bs.create();
772 mPSIcons.setName("PSIcons");
773
774 //bs.setDitherEnable(false);
775 //mPSText = bs.create();
776 //mPSText.setName("PSText");
777 }
778
779 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700780 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700781 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700782 mTouchXBorders.length);
783 mAllocTouchXBorders.data(mTouchXBorders);
784
785 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700786 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700787 mTouchYBorders.length);
788 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700789 }
Jason Sams78aebd82009-09-15 13:06:59 -0700790
Joe Onorato1feb3a82009-08-08 22:32:00 -0700791 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700792 mParams = new Params();
793 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700794
Joe Onoratobf15cb42009-08-07 14:33:40 -0700795 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700796
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700797 mParams.bubbleWidth = bubble.getBubbleWidth();
798 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
799 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
800 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700801
Joe Onoratobcbeab82009-10-01 21:45:43 -0700802 mHomeButton = Allocation.createFromBitmapResource(mRS, mRes,
803 R.drawable.home_button, Element.RGBA_8888(mRS), false);
804 mHomeButton.uploadToTexture(0);
805 mParams.homeButtonId = mHomeButton.getID();
806 mParams.homeButtonWidth = 76;
807 mParams.homeButtonHeight = 68;
808 mParams.homeButtonTextureWidth = 128;
809 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700810
Joe Onorato1feb3a82009-08-08 22:32:00 -0700811 mParams.save();
812 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400813
Joe Onorato1291a8c2009-09-15 15:07:25 -0400814 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
815 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
816 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700817
Joe Onorato9c1289c2009-08-17 11:03:03 -0400818 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700819 }
820
Jason Samsd8152b92009-10-13 17:19:10 -0700821 private void initScript(int idx, int id) {
Joe Onorato93839052009-08-06 20:34:32 -0700822 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Samsd8152b92009-10-13 17:19:10 -0700823 sb.setScript(mRes, id);
Joe Onorato93839052009-08-06 20:34:32 -0700824 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700825 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700826 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
827 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Samsd8152b92009-10-13 17:19:10 -0700828 mInvokeMove[idx] = sb.addInvokable("move");
829 mInvokeFling[idx] = sb.addInvokable("fling");
830 mScript[idx] = sb.create();
831 mScript[idx].setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
832 mScript[idx].bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
833 mScript[idx].bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
834 mScript[idx].bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
835 mScript[idx].bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
836 mScript[idx].bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
837 mScript[idx].bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
838 }
Joe Onorato93839052009-08-06 20:34:32 -0700839
Jason Samsd8152b92009-10-13 17:19:10 -0700840 private void initRs() {
841 mViewMode = 0;
842 initScript(0, R.raw.rollo3);
843 initScript(1, R.raw.rollo2);
844 initScript(2, R.raw.rollo);
845 initScript(3, R.raw.rollo4);
Joe Onorato93839052009-08-06 20:34:32 -0700846
Jason Sams12c14a82009-10-06 14:33:15 -0700847 mMessageProc = new AAMessage();
848 mRS.mMessageCallback = mMessageProc;
Jason Samsd8152b92009-10-13 17:19:10 -0700849 mRS.contextBindRootScript(mScript[mViewMode]);
Joe Onorato93839052009-08-06 20:34:32 -0700850 }
Joe Onorato93839052009-08-06 20:34:32 -0700851
Joe Onorato9c1289c2009-08-17 11:03:03 -0400852 private void setApps(ArrayList<ApplicationInfo> list) {
853 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700854 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700855 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700856 allocCount = 1;
857 }
858
Joe Onorato9c1289c2009-08-17 11:03:03 -0400859 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700860 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700861 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400862
863 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700864 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700865 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700867 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400868
869 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
870
871 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700872 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873 }
874
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875 mState.iconCount = count;
876
Joe Onoratoa8138d52009-10-06 19:25:30 -0700877 saveAppsList();
878 }
879
880 private void uploadAppIcon(int index, ApplicationInfo item) {
881 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
882 Element.RGBA_8888(mRS), false);
883 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
884 Element.RGBA_8888(mRS), false);
885
886 mIcons[index].uploadToTexture(0);
887 mLabels[index].uploadToTexture(0);
888
889 mIconIds[index] = mIcons[index].getID();
890 mLabelIds[index] = mLabels[index].getID();
891 }
892
893 /**
894 * Puts the empty spaces at the end. Updates mState.iconCount. You must
895 * fill in the values and call saveAppsList().
896 */
897 private void reallocAppsList(int count) {
898 Allocation[] icons = new Allocation[count];
899 int[] iconIds = new int[count];
900 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
901
902 Allocation[] labels = new Allocation[count];
903 int[] labelIds = new int[count];
904 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
905
906 final int oldCount = mIcons.length;
907
908 System.arraycopy(mIcons, 0, icons, 0, oldCount);
909 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
910 System.arraycopy(mLabels, 0, labels, 0, oldCount);
911 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
912
913 mIcons = icons;
914 mIconIds = iconIds;
915 mLabels = labels;
916 mLabelIds = labelIds;
917 }
918
919 /**
920 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
921 */
922 private void addApp(int index, ApplicationInfo item) {
923 final int count = mState.iconCount - index;
924 final int dest = index + 1;
925
926 System.arraycopy(mIcons, index, mIcons, dest, count);
927 System.arraycopy(mIconIds, index, mIconIds, dest, count);
928 System.arraycopy(mLabels, index, mLabels, dest, count);
929 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
930
931 uploadAppIcon(index, item);
932 }
933
934 /**
935 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
936 */
937 private void removeApp(int index) {
938 final int count = mState.iconCount - index - 1;
939 final int src = index + 1;
940
941 System.arraycopy(mIcons, src, mIcons, index, count);
942 System.arraycopy(mIconIds, src, mIconIds, index, count);
943 System.arraycopy(mLabels, src, mLabels, index, count);
944 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
945
946 final int last = mState.iconCount - 1;
947 mIcons[last] = null;
948 mIconIds[last] = 0;
949 mLabels[last] = null;
950 mLabelIds[last] = 0;
951 }
952
953 /**
954 * Send the apps list structures to RS.
955 */
956 private void saveAppsList() {
957 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -0700958
Joe Onoratoa8138d52009-10-06 19:25:30 -0700959 mAllocIconIds.data(mIconIds);
960 mAllocLabelIds.data(mLabelIds);
961
Jason Samsd8152b92009-10-13 17:19:10 -0700962 if (mScript[0] != null) { // this happens when we init it
963 for (int ct=0; ct < 4; ct++) {
964 mScript[ct].bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
965 mScript[ct].bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
966 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967 }
968
969 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -0700970
971 // Note: mScript may be null if we haven't initialized it yet.
972 // In that case, this is a no-op.
Jason Samsd8152b92009-10-13 17:19:10 -0700973 mRS.contextBindRootScript(mScript[mViewMode]);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700975
976 void initTouchState() {
977 int width = getWidth();
978 int height = getHeight();
979
980 int iconsSize;
981 if (width < height) {
982 iconsSize = width;
983 } else {
984 iconsSize = height;
985 }
986 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
987 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
988
Joe Onorato56848b02009-09-25 13:59:59 -0700989 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
990 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
991 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700992 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -0700993 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
994 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700995
996 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700997
Joe Onorato6665c0f2009-09-02 15:27:24 -0700998 int centerX = (width / 2);
999 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -07001000 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001001 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -07001002 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001003 mTouchXBorders[4] = centerX + (2 * cellWidth);
1004
1005 mAllocTouchXBorders.data(mTouchXBorders);
1006 }
1007
Jason Sams86c87ed2009-09-18 13:55:55 -07001008 int chooseTappedIcon(int x, int y, float page) {
1009 int currentPage = (int)page;
1010
Joe Onorato6665c0f2009-09-02 15:27:24 -07001011 int col = -1;
1012 int row = -1;
1013
1014 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1015 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1016 col = i;
1017 break;
1018 }
1019 }
1020 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1021 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1022 row = i;
1023 break;
1024 }
1025 }
1026
1027 if (row < 0 || col < 0) {
1028 return -1;
1029 }
1030
Jason Sams78aebd82009-09-15 13:06:59 -07001031 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001032 + (row * Defines.ROWS_PER_PAGE) + col;
1033 }
1034
Jason Samsd8152b92009-10-13 17:19:10 -07001035 boolean setView(int v) {
1036 mViewMode = v;
1037 mRS.contextBindRootScript(mScript[mViewMode]);
1038 return (v == 0);
1039 }
1040
1041 void fling() {
1042 mInvokeFling[mViewMode].execute();
1043 }
1044
1045 void move() {
1046 mInvokeMove[mViewMode].execute();
1047 }
1048
Joe Onorato6665c0f2009-09-02 15:27:24 -07001049 /**
1050 * You need to call save() on mState on your own after calling this.
1051 */
Jason Sams86c87ed2009-09-18 13:55:55 -07001052 void selectIcon(int x, int y, float pos) {
1053 int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001054 selectIcon(index);
1055 }
1056
1057 void selectIcon(int index) {
Joe Onorato1291a8c2009-09-15 15:07:25 -04001058 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001059 if (index < 0 || index >= iconCount) {
1060 mState.selectedIconIndex = -1;
1061 return;
1062 } else {
1063 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001064
1065 Bitmap selectionBitmap = mSelectionBitmap;
1066
1067 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1068 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1069 mAllAppsList.get(index).iconBitmap);
1070
1071 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001072 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001073 mSelectedIcon.uploadToTexture(0);
1074 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001075 }
1076 }
1077
1078 /**
1079 * You need to call save() on mState on your own after calling this.
1080 */
1081 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001082 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001083 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001084
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085 }
Joe Onorato93839052009-08-06 20:34:32 -07001086}
1087
1088