blob: fdbc8415957c8b3b4882f40f8c83060d2b5e2a43 [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) {
Jason Samsb52dfa02009-10-14 20:16:14 -0700222 //mRotateMove = mRollo.setView((++mRSMode) & 3);
Jason Samsd8152b92009-10-13 17:19:10 -0700223 }
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 Sams41b61c82009-10-15 15:40:54 -0700510 private Script.Invokable[] mInvokeResetWAR = new Script.Invokable[4];
Jason Sams86c87ed2009-09-18 13:55:55 -0700511
Jason Samscd689e12009-09-29 15:28:22 -0700512 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700513 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700514 private ProgramFragment mPFColor;
515 private ProgramFragment mPFTexLinear;
516 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700517 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700518 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700519 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700520 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700521
Joe Onoratobcbeab82009-10-01 21:45:43 -0700522 private Allocation mHomeButton;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700523
Joe Onoratobf15cb42009-08-07 14:33:40 -0700524 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700525 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700526 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700527
Joe Onoratobf15cb42009-08-07 14:33:40 -0700528 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700529 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700530 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700531 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700532
Joe Onorato6665c0f2009-09-02 15:27:24 -0700533 private int[] mTouchYBorders;
534 private Allocation mAllocTouchYBorders;
535 private int[] mTouchXBorders;
536 private Allocation mAllocTouchXBorders;
537
538 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400539 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700540
Jason Samsd8152b92009-10-13 17:19:10 -0700541
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700542 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700543 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700544
Jason Sams78aebd82009-09-15 13:06:59 -0700545 class BaseAlloc {
546 Allocation mAlloc;
547 Type mType;
548
549 void save() {
550 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700551 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700552 }
553
Jason Sams12c14a82009-10-06 14:33:15 -0700554 class AAMessage extends RenderScript.RSMessage {
555 public void run() {
556 mPosX = ((float)mData[0]) / (1 << 16);
557 mVelocity = ((float)mData[1]) / (1 << 16);
558 mZoom = ((float)mData[2]) / (1 << 16);
559 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
560 }
561 float mZoom;
562 float mPosX;
563 float mVelocity;
564 }
565 AAMessage mMessageProc;
566
Jason Sams476339d2009-09-29 18:14:38 -0700567 private boolean checkClickOK() {
568 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams12c14a82009-10-06 14:33:15 -0700569 return (Math.abs(mMessageProc.mVelocity) < 0.1f) &&
570 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.1f);
Jason Sams476339d2009-09-29 18:14:38 -0700571 }
572
Jason Sams78aebd82009-09-15 13:06:59 -0700573 class Params extends BaseAlloc {
574 Params() {
575 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
576 mAlloc = Allocation.createTyped(mRS, mType);
577 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700578 }
Jason Sams78aebd82009-09-15 13:06:59 -0700579 public int bubbleWidth;
580 public int bubbleHeight;
581 public int bubbleBitmapWidth;
582 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700583
584 public int homeButtonId;
585 public int homeButtonWidth;
586 public int homeButtonHeight;
587 public int homeButtonTextureWidth;
588 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700589 }
590
591 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700592 public float newPositionX;
593 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700594 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700595 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700596 public int selectedIconIndex = -1;
597 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700598 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700599
600 State() {
601 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
602 mAlloc = Allocation.createTyped(mRS, mType);
603 save();
604 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700605 }
606
607 public RolloRS() {
608 }
609
610 public void init(Resources res, int width, int height) {
611 mRes = res;
612 mWidth = width;
613 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700614 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700615 initProgramVertex();
616 initProgramFragment();
617 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700618 initMesh();
Jason Samsd8152b92009-10-13 17:19:10 -0700619 initMesh2();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700620 initGl();
621 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700622 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700623 initRs();
624 }
625
Jason Sams0aa71662009-10-02 18:43:18 -0700626 public void initMesh() {
627 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
628 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
629
630 for (int ct=0; ct < 450; ct++) {
631 float x = 0;
632 float z = 0;
633 float l = 1.f;
634
635 if (ct < 190) {
636 z = 0.1f + 0.05f * (190 - ct);
637 x = -1;
638 l = 0.125f + (0.125f / 190.f) * ct;
639 } else if (ct >= 190 && ct < 200) {
640 float a = (3.14f * 0.5f) * (0.1f * (ct - 200));
641 float s = (float)Math.sin(a);
642 float c = (float)Math.cos(a);
643 x = -0.9f + s * 0.1f;
644 z = 0.1f - c * 0.1f;
645 l = 0.25f + 0.075f * (ct - 190);
646 } else if (ct >= 200 && ct < 250) {
647 z = 0.f;
648 x = -0.9f + (1.8f * (ct - 200) / 50.f);
649 } else if (ct >= 250 && ct < 260) {
650 float a = (3.14f * 0.5f) * (0.1f * (ct - 250));
651 float s = (float)Math.sin(a);
652 float c = (float)Math.cos(a);
653 x = 0.9f + s * 0.1f;
654 z = 0.1f - c * 0.1f;
655 l = 0.25f + 0.075f * (260 - ct);
656 } else if (ct >= 260) {
657 z = 0.1f + 0.05f * (ct - 260);
658 x = 1;
659 l = 0.125f + (0.125f / 190.f) * (450 - ct);
660 }
661 //Log.e("rs", "ct " + Integer.toString(ct) + " x = " + Float.toString(x) + ", z = " + Float.toString(z));
662 //Log.e("rs", "ct " + Integer.toString(ct) + " l = " + Float.toString(l));
663 float s = ct * 0.1f;
664 tm.setColor(l, l, l, 0.99f);
665 tm.setTexture(s, 1);
666 tm.addVertex(x, -0.5f, z);
667 tm.setTexture(s, 0);
668 tm.addVertex(x, 0.5f, z);
669 }
670 for (int ct=0; ct < (450*2 - 2); ct+= 2) {
671 tm.addTriangle(ct, ct+1, ct+2);
672 tm.addTriangle(ct+1, ct+3, ct+2);
673 }
674 mMesh = tm.create();
675 mMesh.setName("SMMesh");
676
677 }
678
679
Jason Samsd8152b92009-10-13 17:19:10 -0700680 public void initMesh2() {
681 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
682 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
683
684 float y = 0;
685 float z = 0;
686 for (int ct=0; ct < 200; ct++) {
687 float angle = 0;
688 float maxAngle = 3.14f * 0.16f;
689 float l = 1.f;
690
691 l = 1 - ((ct-5) * 0.10f);
692 if (ct > 7) {
693 angle = maxAngle * (ct - 7) * 0.2f;
694 angle = Math.min(angle, maxAngle);
695 }
696 l = Math.max(0.3f, l);
697 l = Math.min(1.0f, l);
698
699 y += 0.1f * Math.cos(angle);
700 z += 0.1f * Math.sin(angle);
701
702 float t = 0.1f * ct;
703 float ds = 0.08f;
704 tm.setColor(l, l, l, 0.99f);
705 tm.setTexture(ds, t);
706 tm.addVertex(-0.5f, y, z);
707 tm.setTexture(1 - ds, t);
708 tm.addVertex(0.5f, y, z);
709 }
710 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
711 tm.addTriangle(ct, ct+1, ct+2);
712 tm.addTriangle(ct+1, ct+3, ct+2);
713 }
714 mMesh2 = tm.create();
715 mMesh2.setName("SMMesh2");
716 }
717
Jason Samscd689e12009-09-29 15:28:22 -0700718 private void initProgramVertex() {
719 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
720 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700721
722 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700723 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700724 mPV = pvb.create();
725 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700726 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700727
Jason Samscd689e12009-09-29 15:28:22 -0700728 pva = new ProgramVertex.MatrixAllocation(mRS);
729 pva.setupOrthoWindow(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700730 pvb.setTextureMatrixEnable(true);
731 mPVOrtho = pvb.create();
732 mPVOrtho.setName("PVOrtho");
Jason Samscd689e12009-09-29 15:28:22 -0700733 mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700734
735 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700736 }
Joe Onorato93839052009-08-06 20:34:32 -0700737
Jason Samscd689e12009-09-29 15:28:22 -0700738 private void initProgramFragment() {
739 Sampler.Builder sb = new Sampler.Builder(mRS);
740 sb.setMin(Sampler.Value.LINEAR);
741 sb.setMag(Sampler.Value.LINEAR);
742 sb.setWrapS(Sampler.Value.CLAMP);
743 sb.setWrapT(Sampler.Value.CLAMP);
744 Sampler linear = sb.create();
745
746 sb.setMin(Sampler.Value.NEAREST);
747 sb.setMag(Sampler.Value.NEAREST);
748 Sampler nearest = sb.create();
749
750 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
751 mPFColor = bf.create();
752 mPFColor.setName("PFColor");
753
754 bf.setTexEnable(true, 0);
755 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
756 mPFTexLinear = bf.create();
757 mPFTexLinear.setName("PFTexLinear");
758 mPFTexLinear.bindSampler(linear, 0);
759
760 mPFTexNearest = bf.create();
761 mPFTexNearest.setName("PFTexNearest");
762 mPFTexNearest.bindSampler(nearest, 0);
763 }
764
765 private void initProgramStore() {
766 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
767 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700768 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700769 bs.setDitherEnable(true);
770 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
771 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
772 mPSIcons = bs.create();
773 mPSIcons.setName("PSIcons");
774
775 //bs.setDitherEnable(false);
776 //mPSText = bs.create();
777 //mPSText.setName("PSText");
778 }
779
780 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700781 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700782 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700783 mTouchXBorders.length);
784 mAllocTouchXBorders.data(mTouchXBorders);
785
786 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700787 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700788 mTouchYBorders.length);
789 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700790 }
Jason Sams78aebd82009-09-15 13:06:59 -0700791
Joe Onorato1feb3a82009-08-08 22:32:00 -0700792 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700793 mParams = new Params();
794 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700795
Joe Onoratobf15cb42009-08-07 14:33:40 -0700796 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700797
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700798 mParams.bubbleWidth = bubble.getBubbleWidth();
799 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
800 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
801 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700802
Joe Onoratobcbeab82009-10-01 21:45:43 -0700803 mHomeButton = Allocation.createFromBitmapResource(mRS, mRes,
804 R.drawable.home_button, Element.RGBA_8888(mRS), false);
805 mHomeButton.uploadToTexture(0);
806 mParams.homeButtonId = mHomeButton.getID();
807 mParams.homeButtonWidth = 76;
808 mParams.homeButtonHeight = 68;
809 mParams.homeButtonTextureWidth = 128;
810 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700811
Joe Onorato1feb3a82009-08-08 22:32:00 -0700812 mParams.save();
813 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400814
Joe Onorato1291a8c2009-09-15 15:07:25 -0400815 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
816 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
817 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700818
Joe Onorato9c1289c2009-08-17 11:03:03 -0400819 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700820 }
821
Jason Samsd8152b92009-10-13 17:19:10 -0700822 private void initScript(int idx, int id) {
Joe Onorato93839052009-08-06 20:34:32 -0700823 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Samsd8152b92009-10-13 17:19:10 -0700824 sb.setScript(mRes, id);
Joe Onorato93839052009-08-06 20:34:32 -0700825 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700826 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700827 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
828 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Samsd8152b92009-10-13 17:19:10 -0700829 mInvokeMove[idx] = sb.addInvokable("move");
830 mInvokeFling[idx] = sb.addInvokable("fling");
Jason Sams41b61c82009-10-15 15:40:54 -0700831 mInvokeResetWAR[idx] = sb.addInvokable("resetHWWar");
Jason Samsd8152b92009-10-13 17:19:10 -0700832 mScript[idx] = sb.create();
833 mScript[idx].setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
834 mScript[idx].bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
835 mScript[idx].bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
836 mScript[idx].bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
837 mScript[idx].bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
838 mScript[idx].bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
839 mScript[idx].bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
840 }
Joe Onorato93839052009-08-06 20:34:32 -0700841
Jason Samsd8152b92009-10-13 17:19:10 -0700842 private void initRs() {
843 mViewMode = 0;
844 initScript(0, R.raw.rollo3);
845 initScript(1, R.raw.rollo2);
846 initScript(2, R.raw.rollo);
847 initScript(3, R.raw.rollo4);
Joe Onorato93839052009-08-06 20:34:32 -0700848
Jason Sams12c14a82009-10-06 14:33:15 -0700849 mMessageProc = new AAMessage();
850 mRS.mMessageCallback = mMessageProc;
Jason Samsd8152b92009-10-13 17:19:10 -0700851 mRS.contextBindRootScript(mScript[mViewMode]);
Joe Onorato93839052009-08-06 20:34:32 -0700852 }
Joe Onorato93839052009-08-06 20:34:32 -0700853
Joe Onorato9c1289c2009-08-17 11:03:03 -0400854 private void setApps(ArrayList<ApplicationInfo> list) {
855 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700856 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700857 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700858 allocCount = 1;
859 }
860
Joe Onorato9c1289c2009-08-17 11:03:03 -0400861 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700862 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700863 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400864
865 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700866 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700867 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400868
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700869 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870
871 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
872
873 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700874 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875 }
876
Joe Onorato9c1289c2009-08-17 11:03:03 -0400877 mState.iconCount = count;
878
Joe Onoratoa8138d52009-10-06 19:25:30 -0700879 saveAppsList();
880 }
881
882 private void uploadAppIcon(int index, ApplicationInfo item) {
883 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
884 Element.RGBA_8888(mRS), false);
885 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
886 Element.RGBA_8888(mRS), false);
887
888 mIcons[index].uploadToTexture(0);
889 mLabels[index].uploadToTexture(0);
890
891 mIconIds[index] = mIcons[index].getID();
892 mLabelIds[index] = mLabels[index].getID();
893 }
894
895 /**
896 * Puts the empty spaces at the end. Updates mState.iconCount. You must
897 * fill in the values and call saveAppsList().
898 */
899 private void reallocAppsList(int count) {
900 Allocation[] icons = new Allocation[count];
901 int[] iconIds = new int[count];
902 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
903
904 Allocation[] labels = new Allocation[count];
905 int[] labelIds = new int[count];
906 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
907
908 final int oldCount = mIcons.length;
909
910 System.arraycopy(mIcons, 0, icons, 0, oldCount);
911 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
912 System.arraycopy(mLabels, 0, labels, 0, oldCount);
913 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
914
915 mIcons = icons;
916 mIconIds = iconIds;
917 mLabels = labels;
918 mLabelIds = labelIds;
919 }
920
921 /**
922 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
923 */
924 private void addApp(int index, ApplicationInfo item) {
925 final int count = mState.iconCount - index;
926 final int dest = index + 1;
927
928 System.arraycopy(mIcons, index, mIcons, dest, count);
929 System.arraycopy(mIconIds, index, mIconIds, dest, count);
930 System.arraycopy(mLabels, index, mLabels, dest, count);
931 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
932
933 uploadAppIcon(index, item);
934 }
935
936 /**
937 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
938 */
939 private void removeApp(int index) {
940 final int count = mState.iconCount - index - 1;
941 final int src = index + 1;
942
943 System.arraycopy(mIcons, src, mIcons, index, count);
944 System.arraycopy(mIconIds, src, mIconIds, index, count);
945 System.arraycopy(mLabels, src, mLabels, index, count);
946 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
947
948 final int last = mState.iconCount - 1;
949 mIcons[last] = null;
950 mIconIds[last] = 0;
951 mLabels[last] = null;
952 mLabelIds[last] = 0;
953 }
954
955 /**
956 * Send the apps list structures to RS.
957 */
958 private void saveAppsList() {
959 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -0700960
Joe Onoratoa8138d52009-10-06 19:25:30 -0700961 mAllocIconIds.data(mIconIds);
962 mAllocLabelIds.data(mLabelIds);
963
Jason Samsd8152b92009-10-13 17:19:10 -0700964 if (mScript[0] != null) { // this happens when we init it
965 for (int ct=0; ct < 4; ct++) {
966 mScript[ct].bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
967 mScript[ct].bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
968 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 }
970
971 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -0700972
973 // Note: mScript may be null if we haven't initialized it yet.
974 // In that case, this is a no-op.
Jason Sams41b61c82009-10-15 15:40:54 -0700975 if (mInvokeResetWAR != null &&
976 mInvokeResetWAR[mViewMode] != null) {
977 mInvokeResetWAR[mViewMode].execute();
978 }
Jason Samsd8152b92009-10-13 17:19:10 -0700979 mRS.contextBindRootScript(mScript[mViewMode]);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700981
982 void initTouchState() {
983 int width = getWidth();
984 int height = getHeight();
985
986 int iconsSize;
987 if (width < height) {
988 iconsSize = width;
989 } else {
990 iconsSize = height;
991 }
992 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
993 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
994
Joe Onorato56848b02009-09-25 13:59:59 -0700995 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
996 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
997 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700998 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -0700999 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
1000 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001001
1002 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -07001003
Joe Onorato6665c0f2009-09-02 15:27:24 -07001004 int centerX = (width / 2);
1005 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -07001006 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001007 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -07001008 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001009 mTouchXBorders[4] = centerX + (2 * cellWidth);
1010
1011 mAllocTouchXBorders.data(mTouchXBorders);
1012 }
1013
Jason Samsb52dfa02009-10-14 20:16:14 -07001014 int chooseTappedIconHorz(int x, int y, float page) {
Jason Sams86c87ed2009-09-18 13:55:55 -07001015 int currentPage = (int)page;
1016
Joe Onorato6665c0f2009-09-02 15:27:24 -07001017 int col = -1;
1018 int row = -1;
1019
1020 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1021 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1022 col = i;
1023 break;
1024 }
1025 }
1026 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1027 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1028 row = i;
1029 break;
1030 }
1031 }
1032
1033 if (row < 0 || col < 0) {
1034 return -1;
1035 }
1036
Jason Sams78aebd82009-09-15 13:06:59 -07001037 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001038 + (row * Defines.ROWS_PER_PAGE) + col;
1039 }
1040
Jason Samsb52dfa02009-10-14 20:16:14 -07001041 int chooseTappedIconVert(int x, int y, float pos) {
1042 int ydead = (getHeight() - 4 * 145) / 2;
1043 if (y < ydead || y > (getHeight() - ydead)) {
1044 return -1;
1045 }
1046
1047 y -= ydead;
1048 y += pos * 145;
1049 int row = y / 145;
1050 int col = x / 120;
1051
1052 return row * 4 + col;
1053 }
1054
Jason Samsd8152b92009-10-13 17:19:10 -07001055 boolean setView(int v) {
1056 mViewMode = v;
1057 mRS.contextBindRootScript(mScript[mViewMode]);
1058 return (v == 0);
1059 }
1060
1061 void fling() {
1062 mInvokeFling[mViewMode].execute();
1063 }
1064
1065 void move() {
1066 mInvokeMove[mViewMode].execute();
1067 }
1068
Joe Onorato6665c0f2009-09-02 15:27:24 -07001069 /**
1070 * You need to call save() on mState on your own after calling this.
1071 */
Jason Sams86c87ed2009-09-18 13:55:55 -07001072 void selectIcon(int x, int y, float pos) {
Jason Samsb52dfa02009-10-14 20:16:14 -07001073 int index;
1074 //Log.e("rs", "select " + x + ", " + y + ", " + pos + ", " + mViewMode);
1075 if (mViewMode != 0) {
1076 index = chooseTappedIconHorz(x, y, pos);
1077 } else {
1078 index = chooseTappedIconVert(x, y, pos);
1079 }
Joe Onorato1291a8c2009-09-15 15:07:25 -04001080 selectIcon(index);
1081 }
1082
1083 void selectIcon(int index) {
Joe Onorato1291a8c2009-09-15 15:07:25 -04001084 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001085 if (index < 0 || index >= iconCount) {
1086 mState.selectedIconIndex = -1;
1087 return;
1088 } else {
1089 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001090
1091 Bitmap selectionBitmap = mSelectionBitmap;
1092
1093 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1094 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1095 mAllAppsList.get(index).iconBitmap);
1096
1097 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001098 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001099 mSelectedIcon.uploadToTexture(0);
1100 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001101 }
1102 }
1103
1104 /**
1105 * You need to call save() on mState on your own after calling this.
1106 */
1107 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001108 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001109 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001110
Joe Onorato9c1289c2009-08-17 11:03:03 -04001111 }
Joe Onorato93839052009-08-06 20:34:32 -07001112}
1113
1114