blob: e31fe5d552eaa2b673e874db02507502cf8e0a16 [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 Onorato82ca5502009-10-15 16:59:23 -070082 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070083 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 Onorato5162ea92009-09-03 09:39:42 -070094 private int mMotionDownRawX;
95 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -070096 private int mDownIconIndex = -1;
97 private int mCurrentIconIndex = -1;
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);
Joe Onorato82ca5502009-10-15 16:59:23 -0700145 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700146 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();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700232
Jason Samsd8152b92009-10-13 17:19:10 -0700233 if (mRotateMove) {
234 mRollo.mState.newPositionX = ev.getRawY() / mDefines.SCREEN_WIDTH_PX;
235 } else {
236 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
237 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700238 mRollo.mState.newTouchDown = 1;
239
240 if (!mRollo.checkClickOK()) {
241 mRollo.clearSelectedIcon();
242 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700243 mDownIconIndex = mCurrentIconIndex
244 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
245 if (mDownIconIndex < 0) {
246 // if nothing was selected, no long press.
247 cancelLongPress();
248 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700249 }
250 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700251 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700252 mVelocity = VelocityTracker.obtain();
253 mVelocity.addMovement(ev);
254 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700255 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400256 break;
257 case MotionEvent.ACTION_MOVE:
258 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700259 if (mTouchTracking == TRACKING_HOME) {
260 // TODO: highlight?
Joe Onoratofb0ca672009-09-14 17:55:46 -0400261 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700262 int rawX = (int)ev.getRawX();
263 int rawY = (int)ev.getRawY();
264 int slop;
Jason Samsd8152b92009-10-13 17:19:10 -0700265 if (mRotateMove) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700266 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700267 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700268 slop = Math.abs(rawX - mMotionDownRawX);
Jason Samsd8152b92009-10-13 17:19:10 -0700269 }
270
Joe Onorato82ca5502009-10-15 16:59:23 -0700271 if (!mStartedScrolling && slop < mSlop) {
272 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700273 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700274 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
275 if (mDownIconIndex != mCurrentIconIndex) {
276 // If a different icon is selected, don't allow it to be picked up.
277 // This handles off-axis dragging.
278 cancelLongPress();
279 mCurrentIconIndex = -1;
280 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700281 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700282 if (!mStartedScrolling) {
283 cancelLongPress();
284 mCurrentIconIndex = -1;
285 }
Jason Samsd8152b92009-10-13 17:19:10 -0700286 if (mRotateMove) {
287 mRollo.mState.newPositionX = ev.getRawY() / mDefines.SCREEN_WIDTH_PX;
288 } else {
289 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
290 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700291 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700292 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700293
Joe Onoratobcbeab82009-10-01 21:45:43 -0700294 mStartedScrolling = true;
295 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700296 mVelocity.addMovement(ev);
297 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700298 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400299 }
300 break;
301 case MotionEvent.ACTION_UP:
302 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700303 if (mTouchTracking == TRACKING_HOME) {
304 if (action == MotionEvent.ACTION_UP) {
305 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
306 mLauncher.closeAllApps(true);
307 }
308 }
Jason Sams476339d2009-09-29 18:14:38 -0700309 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700310 mRollo.mState.newTouchDown = 0;
Jason Samsd8152b92009-10-13 17:19:10 -0700311 if (mRotateMove) {
312 mRollo.mState.newPositionX = ev.getRawY() / mDefines.SCREEN_WIDTH_PX;
313 } else {
314 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
315 }
Jason Sams476339d2009-09-29 18:14:38 -0700316
Jason Sams12c14a82009-10-06 14:33:15 -0700317 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Samsd8152b92009-10-13 17:19:10 -0700318 if (mRotateMove) {
319 mRollo.mState.flingVelocityX
320 = mVelocity.getYVelocity() / mDefines.SCREEN_WIDTH_PX;
321 } else {
322 mRollo.mState.flingVelocityX
323 = mVelocity.getXVelocity() / mDefines.SCREEN_WIDTH_PX;
324 }
Jason Sams12c14a82009-10-06 14:33:15 -0700325 mRollo.clearSelectedIcon();
326 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700327 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700328
Joe Onorato539ed9d2009-10-02 10:22:14 -0700329 if (mVelocity != null) {
330 mVelocity.recycle();
331 mVelocity = null;
332 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700333 break;
334 }
Joe Onorato93839052009-08-06 20:34:32 -0700335 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700336
337 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700338 }
339
Joe Onorato6665c0f2009-09-02 15:27:24 -0700340 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700341 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400342 return;
343 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700344 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
345 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
346 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700347 mLauncher.startActivitySafely(app.intent);
348 }
349 }
350
351 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700352 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400353 return true;
354 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700355 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
356 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
357 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700358
359 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700360 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
361 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700362
Joe Onoratobcbeab82009-10-01 21:45:43 -0700363 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
364 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700365 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700366 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700367 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700368
Joe Onorato7bb17492009-09-24 17:51:01 -0700369 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700370 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700371 return true;
372 }
373
Joe Onorato5162ea92009-09-03 09:39:42 -0700374 public void setDragController(DragController dragger) {
375 mDragController = dragger;
376 }
377
378 public void onDropCompleted(View target, boolean success) {
379 }
380
Joe Onorato4db52312009-10-06 11:17:43 -0700381 /**
382 * Zoom to the specifed amount.
383 *
384 * @param amount [0..1] 0 is hidden, 1 is open
385 * @param animate Whether to animate.
386 */
Jason Sams12c14a82009-10-06 14:33:15 -0700387 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700388 if (mRollo == null) {
389 return;
390 }
391
Joe Onoratofb0ca672009-09-14 17:55:46 -0400392 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400393 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700394 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700395 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700396 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700397 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700398 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700399 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400400 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400401 }
402
403 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700404 if (mRollo == null) {
405 return false;
406 }
Jason Sams12c14a82009-10-06 14:33:15 -0700407 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400408 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700409
Joe Onorato93839052009-08-06 20:34:32 -0700410 @Override
411 public boolean onTrackballEvent(MotionEvent ev)
412 {
413 float x = ev.getX();
414 float y = ev.getY();
415 //Float tx = new Float(x);
416 //Float ty = new Float(y);
417 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
418
419
420 return true;
421 }
422
Joe Onorato9c1289c2009-08-17 11:03:03 -0400423 public void setApps(ArrayList<ApplicationInfo> list) {
424 mAllAppsList = list;
425 if (mRollo != null) {
426 mRollo.setApps(list);
427 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700428 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400429 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700430 }
431
Joe Onoratoa8138d52009-10-06 19:25:30 -0700432 public void addApps(ArrayList<ApplicationInfo> list) {
433 final int N = list.size();
434 if (mRollo != null) {
435 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
436 }
437
438 for (int i=0; i<N; i++) {
439 final ApplicationInfo item = list.get(i);
440 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
441 if (index < 0) {
442 index = -(index+1);
443 }
444 mAllAppsList.add(index, item);
445 if (mRollo != null) {
446 mRollo.addApp(index, item);
447 mRollo.mState.iconCount++;
448 }
449 }
450
451 if (mRollo != null) {
452 mRollo.saveAppsList();
453 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700454 }
455
Joe Onoratoa8138d52009-10-06 19:25:30 -0700456 public void removeApps(ArrayList<ApplicationInfo> list) {
457 final int N = list.size();
458 for (int i=0; i<N; i++) {
459 final ApplicationInfo item = list.get(i);
460 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
461 if (index >= 0) {
462 mAllAppsList.remove(index);
463 if (mRollo != null) {
464 mRollo.removeApp(index);
465 mRollo.mState.iconCount--;
466 }
467 } else {
468 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
469 // Try to recover. This should keep us from crashing for now.
470 }
471 }
472
473 if (mRollo != null) {
474 mRollo.saveAppsList();
475 }
476 }
477
478 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
479 // Just remove and add, because they may need to be re-sorted.
480 removeApps(list);
481 addApps(list);
482 }
483
484 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
485 public int compare(ApplicationInfo a, ApplicationInfo b) {
486 int result = a.title.toString().compareTo(b.toString());
487 if (result != 0) {
488 return result;
489 }
490 return a.intent.getComponent().compareTo(b.intent.getComponent());
491 }
492 };
493
494 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
495 public int compare(ApplicationInfo a, ApplicationInfo b) {
496 return a.intent.getComponent().compareTo(b.intent.getComponent());
497 }
498 };
499
Joe Onoratoc567acb2009-08-31 14:34:43 -0700500 private static int countPages(int iconCount) {
501 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
502 int pages = iconCount / iconsPerPage;
503 if (pages*iconsPerPage != iconCount) {
504 pages++;
505 }
506 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400507 }
508
Joe Onorato93839052009-08-06 20:34:32 -0700509 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700510
Joe Onorato1feb3a82009-08-08 22:32:00 -0700511 // Allocations ======
Jason Samsd8152b92009-10-13 17:19:10 -0700512 private int mViewMode = 0;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700513
Joe Onorato93839052009-08-06 20:34:32 -0700514 private int mWidth;
515 private int mHeight;
516
517 private Resources mRes;
Jason Samsd8152b92009-10-13 17:19:10 -0700518 private Script[] mScript = new Script[4];
Jason Sams86c87ed2009-09-18 13:55:55 -0700519
Jason Samsd8152b92009-10-13 17:19:10 -0700520 private Script.Invokable[] mInvokeMove = new Script.Invokable[4];
521 private Script.Invokable[] mInvokeFling = new Script.Invokable[4];
Jason Sams41b61c82009-10-15 15:40:54 -0700522 private Script.Invokable[] mInvokeResetWAR = new Script.Invokable[4];
Jason Sams86c87ed2009-09-18 13:55:55 -0700523
Jason Samscd689e12009-09-29 15:28:22 -0700524 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700525 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700526 private ProgramFragment mPFColor;
527 private ProgramFragment mPFTexLinear;
528 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700529 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700530 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700531 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700532 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700533
Joe Onoratobcbeab82009-10-01 21:45:43 -0700534 private Allocation mHomeButton;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700535
Joe Onoratobf15cb42009-08-07 14:33:40 -0700536 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700537 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700538 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700539
Joe Onoratobf15cb42009-08-07 14:33:40 -0700540 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700541 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700542 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700543 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700544
Joe Onorato6665c0f2009-09-02 15:27:24 -0700545 private int[] mTouchYBorders;
546 private Allocation mAllocTouchYBorders;
547 private int[] mTouchXBorders;
548 private Allocation mAllocTouchXBorders;
549
550 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400551 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700552
Jason Samsd8152b92009-10-13 17:19:10 -0700553
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700554 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700555 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700556
Jason Sams78aebd82009-09-15 13:06:59 -0700557 class BaseAlloc {
558 Allocation mAlloc;
559 Type mType;
560
561 void save() {
562 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700563 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700564 }
565
Jason Sams12c14a82009-10-06 14:33:15 -0700566 class AAMessage extends RenderScript.RSMessage {
567 public void run() {
568 mPosX = ((float)mData[0]) / (1 << 16);
569 mVelocity = ((float)mData[1]) / (1 << 16);
570 mZoom = ((float)mData[2]) / (1 << 16);
571 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
572 }
573 float mZoom;
574 float mPosX;
575 float mVelocity;
576 }
577 AAMessage mMessageProc;
578
Jason Sams476339d2009-09-29 18:14:38 -0700579 private boolean checkClickOK() {
580 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams12c14a82009-10-06 14:33:15 -0700581 return (Math.abs(mMessageProc.mVelocity) < 0.1f) &&
582 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.1f);
Jason Sams476339d2009-09-29 18:14:38 -0700583 }
584
Jason Sams78aebd82009-09-15 13:06:59 -0700585 class Params extends BaseAlloc {
586 Params() {
587 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
588 mAlloc = Allocation.createTyped(mRS, mType);
589 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700590 }
Jason Sams78aebd82009-09-15 13:06:59 -0700591 public int bubbleWidth;
592 public int bubbleHeight;
593 public int bubbleBitmapWidth;
594 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700595
596 public int homeButtonId;
597 public int homeButtonWidth;
598 public int homeButtonHeight;
599 public int homeButtonTextureWidth;
600 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700601 }
602
603 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700604 public float newPositionX;
605 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700606 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700607 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700608 public int selectedIconIndex = -1;
609 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700610 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700611
612 State() {
613 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
614 mAlloc = Allocation.createTyped(mRS, mType);
615 save();
616 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700617 }
618
619 public RolloRS() {
620 }
621
622 public void init(Resources res, int width, int height) {
623 mRes = res;
624 mWidth = width;
625 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700626 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700627 initProgramVertex();
628 initProgramFragment();
629 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700630 initMesh();
Jason Samsd8152b92009-10-13 17:19:10 -0700631 initMesh2();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700632 initGl();
633 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700634 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700635 initRs();
636 }
637
Jason Sams0aa71662009-10-02 18:43:18 -0700638 public void initMesh() {
639 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
640 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
641
642 for (int ct=0; ct < 450; ct++) {
643 float x = 0;
644 float z = 0;
645 float l = 1.f;
646
647 if (ct < 190) {
648 z = 0.1f + 0.05f * (190 - ct);
649 x = -1;
650 l = 0.125f + (0.125f / 190.f) * ct;
651 } else if (ct >= 190 && ct < 200) {
652 float a = (3.14f * 0.5f) * (0.1f * (ct - 200));
653 float s = (float)Math.sin(a);
654 float c = (float)Math.cos(a);
655 x = -0.9f + s * 0.1f;
656 z = 0.1f - c * 0.1f;
657 l = 0.25f + 0.075f * (ct - 190);
658 } else if (ct >= 200 && ct < 250) {
659 z = 0.f;
660 x = -0.9f + (1.8f * (ct - 200) / 50.f);
661 } else if (ct >= 250 && ct < 260) {
662 float a = (3.14f * 0.5f) * (0.1f * (ct - 250));
663 float s = (float)Math.sin(a);
664 float c = (float)Math.cos(a);
665 x = 0.9f + s * 0.1f;
666 z = 0.1f - c * 0.1f;
667 l = 0.25f + 0.075f * (260 - ct);
668 } else if (ct >= 260) {
669 z = 0.1f + 0.05f * (ct - 260);
670 x = 1;
671 l = 0.125f + (0.125f / 190.f) * (450 - ct);
672 }
673 //Log.e("rs", "ct " + Integer.toString(ct) + " x = " + Float.toString(x) + ", z = " + Float.toString(z));
674 //Log.e("rs", "ct " + Integer.toString(ct) + " l = " + Float.toString(l));
675 float s = ct * 0.1f;
676 tm.setColor(l, l, l, 0.99f);
677 tm.setTexture(s, 1);
678 tm.addVertex(x, -0.5f, z);
679 tm.setTexture(s, 0);
680 tm.addVertex(x, 0.5f, z);
681 }
682 for (int ct=0; ct < (450*2 - 2); ct+= 2) {
683 tm.addTriangle(ct, ct+1, ct+2);
684 tm.addTriangle(ct+1, ct+3, ct+2);
685 }
686 mMesh = tm.create();
687 mMesh.setName("SMMesh");
688
689 }
690
691
Jason Samsd8152b92009-10-13 17:19:10 -0700692 public void initMesh2() {
693 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
694 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
695
696 float y = 0;
697 float z = 0;
698 for (int ct=0; ct < 200; ct++) {
699 float angle = 0;
700 float maxAngle = 3.14f * 0.16f;
701 float l = 1.f;
702
703 l = 1 - ((ct-5) * 0.10f);
704 if (ct > 7) {
705 angle = maxAngle * (ct - 7) * 0.2f;
706 angle = Math.min(angle, maxAngle);
707 }
708 l = Math.max(0.3f, l);
709 l = Math.min(1.0f, l);
710
711 y += 0.1f * Math.cos(angle);
712 z += 0.1f * Math.sin(angle);
713
714 float t = 0.1f * ct;
715 float ds = 0.08f;
716 tm.setColor(l, l, l, 0.99f);
717 tm.setTexture(ds, t);
718 tm.addVertex(-0.5f, y, z);
719 tm.setTexture(1 - ds, t);
720 tm.addVertex(0.5f, y, z);
721 }
722 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
723 tm.addTriangle(ct, ct+1, ct+2);
724 tm.addTriangle(ct+1, ct+3, ct+2);
725 }
726 mMesh2 = tm.create();
727 mMesh2.setName("SMMesh2");
728 }
729
Jason Samscd689e12009-09-29 15:28:22 -0700730 private void initProgramVertex() {
731 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
732 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700733
734 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700735 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700736 mPV = pvb.create();
737 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700738 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700739
Jason Samscd689e12009-09-29 15:28:22 -0700740 pva = new ProgramVertex.MatrixAllocation(mRS);
741 pva.setupOrthoWindow(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700742 pvb.setTextureMatrixEnable(true);
743 mPVOrtho = pvb.create();
744 mPVOrtho.setName("PVOrtho");
Jason Samscd689e12009-09-29 15:28:22 -0700745 mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700746
747 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700748 }
Joe Onorato93839052009-08-06 20:34:32 -0700749
Jason Samscd689e12009-09-29 15:28:22 -0700750 private void initProgramFragment() {
751 Sampler.Builder sb = new Sampler.Builder(mRS);
752 sb.setMin(Sampler.Value.LINEAR);
753 sb.setMag(Sampler.Value.LINEAR);
754 sb.setWrapS(Sampler.Value.CLAMP);
755 sb.setWrapT(Sampler.Value.CLAMP);
756 Sampler linear = sb.create();
757
758 sb.setMin(Sampler.Value.NEAREST);
759 sb.setMag(Sampler.Value.NEAREST);
760 Sampler nearest = sb.create();
761
762 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
763 mPFColor = bf.create();
764 mPFColor.setName("PFColor");
765
766 bf.setTexEnable(true, 0);
767 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
768 mPFTexLinear = bf.create();
769 mPFTexLinear.setName("PFTexLinear");
770 mPFTexLinear.bindSampler(linear, 0);
771
772 mPFTexNearest = bf.create();
773 mPFTexNearest.setName("PFTexNearest");
774 mPFTexNearest.bindSampler(nearest, 0);
775 }
776
777 private void initProgramStore() {
778 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
779 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700780 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700781 bs.setDitherEnable(true);
782 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
783 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
784 mPSIcons = bs.create();
785 mPSIcons.setName("PSIcons");
786
787 //bs.setDitherEnable(false);
788 //mPSText = bs.create();
789 //mPSText.setName("PSText");
790 }
791
792 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700793 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700794 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700795 mTouchXBorders.length);
796 mAllocTouchXBorders.data(mTouchXBorders);
797
798 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700799 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700800 mTouchYBorders.length);
801 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700802 }
Jason Sams78aebd82009-09-15 13:06:59 -0700803
Joe Onorato1feb3a82009-08-08 22:32:00 -0700804 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700805 mParams = new Params();
806 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700807
Joe Onoratobf15cb42009-08-07 14:33:40 -0700808 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700809
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700810 mParams.bubbleWidth = bubble.getBubbleWidth();
811 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
812 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
813 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700814
Joe Onoratobcbeab82009-10-01 21:45:43 -0700815 mHomeButton = Allocation.createFromBitmapResource(mRS, mRes,
816 R.drawable.home_button, Element.RGBA_8888(mRS), false);
817 mHomeButton.uploadToTexture(0);
818 mParams.homeButtonId = mHomeButton.getID();
819 mParams.homeButtonWidth = 76;
820 mParams.homeButtonHeight = 68;
821 mParams.homeButtonTextureWidth = 128;
822 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700823
Joe Onorato1feb3a82009-08-08 22:32:00 -0700824 mParams.save();
825 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400826
Joe Onorato1291a8c2009-09-15 15:07:25 -0400827 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
828 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
829 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700830
Joe Onorato9c1289c2009-08-17 11:03:03 -0400831 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700832 }
833
Jason Samsd8152b92009-10-13 17:19:10 -0700834 private void initScript(int idx, int id) {
Joe Onorato93839052009-08-06 20:34:32 -0700835 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Samsd8152b92009-10-13 17:19:10 -0700836 sb.setScript(mRes, id);
Joe Onorato93839052009-08-06 20:34:32 -0700837 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700838 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700839 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
840 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Samsd8152b92009-10-13 17:19:10 -0700841 mInvokeMove[idx] = sb.addInvokable("move");
842 mInvokeFling[idx] = sb.addInvokable("fling");
Jason Sams41b61c82009-10-15 15:40:54 -0700843 mInvokeResetWAR[idx] = sb.addInvokable("resetHWWar");
Jason Samsd8152b92009-10-13 17:19:10 -0700844 mScript[idx] = sb.create();
845 mScript[idx].setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
846 mScript[idx].bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
847 mScript[idx].bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
848 mScript[idx].bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
849 mScript[idx].bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
850 mScript[idx].bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
851 mScript[idx].bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
852 }
Joe Onorato93839052009-08-06 20:34:32 -0700853
Jason Samsd8152b92009-10-13 17:19:10 -0700854 private void initRs() {
855 mViewMode = 0;
856 initScript(0, R.raw.rollo3);
857 initScript(1, R.raw.rollo2);
858 initScript(2, R.raw.rollo);
859 initScript(3, R.raw.rollo4);
Joe Onorato93839052009-08-06 20:34:32 -0700860
Jason Sams12c14a82009-10-06 14:33:15 -0700861 mMessageProc = new AAMessage();
862 mRS.mMessageCallback = mMessageProc;
Jason Samsd8152b92009-10-13 17:19:10 -0700863 mRS.contextBindRootScript(mScript[mViewMode]);
Joe Onorato93839052009-08-06 20:34:32 -0700864 }
Joe Onorato93839052009-08-06 20:34:32 -0700865
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866 private void setApps(ArrayList<ApplicationInfo> list) {
867 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700868 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700869 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700870 allocCount = 1;
871 }
872
Joe Onorato9c1289c2009-08-17 11:03:03 -0400873 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700874 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700875 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400876
877 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700878 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700879 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700881 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400882
883 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
884
885 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700886 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400887 }
888
Joe Onorato9c1289c2009-08-17 11:03:03 -0400889 mState.iconCount = count;
890
Joe Onoratoa8138d52009-10-06 19:25:30 -0700891 saveAppsList();
892 }
893
894 private void uploadAppIcon(int index, ApplicationInfo item) {
895 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
896 Element.RGBA_8888(mRS), false);
897 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
898 Element.RGBA_8888(mRS), false);
899
900 mIcons[index].uploadToTexture(0);
901 mLabels[index].uploadToTexture(0);
902
903 mIconIds[index] = mIcons[index].getID();
904 mLabelIds[index] = mLabels[index].getID();
905 }
906
907 /**
908 * Puts the empty spaces at the end. Updates mState.iconCount. You must
909 * fill in the values and call saveAppsList().
910 */
911 private void reallocAppsList(int count) {
912 Allocation[] icons = new Allocation[count];
913 int[] iconIds = new int[count];
914 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
915
916 Allocation[] labels = new Allocation[count];
917 int[] labelIds = new int[count];
918 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
919
920 final int oldCount = mIcons.length;
921
922 System.arraycopy(mIcons, 0, icons, 0, oldCount);
923 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
924 System.arraycopy(mLabels, 0, labels, 0, oldCount);
925 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
926
927 mIcons = icons;
928 mIconIds = iconIds;
929 mLabels = labels;
930 mLabelIds = labelIds;
931 }
932
933 /**
934 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
935 */
936 private void addApp(int index, ApplicationInfo item) {
937 final int count = mState.iconCount - index;
938 final int dest = index + 1;
939
940 System.arraycopy(mIcons, index, mIcons, dest, count);
941 System.arraycopy(mIconIds, index, mIconIds, dest, count);
942 System.arraycopy(mLabels, index, mLabels, dest, count);
943 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
944
945 uploadAppIcon(index, item);
946 }
947
948 /**
949 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
950 */
951 private void removeApp(int index) {
952 final int count = mState.iconCount - index - 1;
953 final int src = index + 1;
954
955 System.arraycopy(mIcons, src, mIcons, index, count);
956 System.arraycopy(mIconIds, src, mIconIds, index, count);
957 System.arraycopy(mLabels, src, mLabels, index, count);
958 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
959
960 final int last = mState.iconCount - 1;
961 mIcons[last] = null;
962 mIconIds[last] = 0;
963 mLabels[last] = null;
964 mLabelIds[last] = 0;
965 }
966
967 /**
968 * Send the apps list structures to RS.
969 */
970 private void saveAppsList() {
971 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -0700972
Joe Onoratoa8138d52009-10-06 19:25:30 -0700973 mAllocIconIds.data(mIconIds);
974 mAllocLabelIds.data(mLabelIds);
975
Jason Samsd8152b92009-10-13 17:19:10 -0700976 if (mScript[0] != null) { // this happens when we init it
977 for (int ct=0; ct < 4; ct++) {
978 mScript[ct].bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
979 mScript[ct].bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
980 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 }
982
983 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -0700984
985 // Note: mScript may be null if we haven't initialized it yet.
986 // In that case, this is a no-op.
Jason Sams41b61c82009-10-15 15:40:54 -0700987 if (mInvokeResetWAR != null &&
988 mInvokeResetWAR[mViewMode] != null) {
989 mInvokeResetWAR[mViewMode].execute();
990 }
Jason Samsd8152b92009-10-13 17:19:10 -0700991 mRS.contextBindRootScript(mScript[mViewMode]);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700993
994 void initTouchState() {
995 int width = getWidth();
996 int height = getHeight();
997
998 int iconsSize;
999 if (width < height) {
1000 iconsSize = width;
1001 } else {
1002 iconsSize = height;
1003 }
1004 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
1005 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
1006
Joe Onorato56848b02009-09-25 13:59:59 -07001007 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
1008 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
1009 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001010 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -07001011 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
1012 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001013
1014 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -07001015
Joe Onorato6665c0f2009-09-02 15:27:24 -07001016 int centerX = (width / 2);
1017 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -07001018 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001019 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -07001020 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001021 mTouchXBorders[4] = centerX + (2 * cellWidth);
1022
1023 mAllocTouchXBorders.data(mTouchXBorders);
1024 }
1025
Jason Samsb52dfa02009-10-14 20:16:14 -07001026 int chooseTappedIconHorz(int x, int y, float page) {
Jason Sams86c87ed2009-09-18 13:55:55 -07001027 int currentPage = (int)page;
1028
Joe Onorato6665c0f2009-09-02 15:27:24 -07001029 int col = -1;
1030 int row = -1;
1031
1032 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1033 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1034 col = i;
1035 break;
1036 }
1037 }
1038 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1039 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1040 row = i;
1041 break;
1042 }
1043 }
1044
1045 if (row < 0 || col < 0) {
1046 return -1;
1047 }
1048
Jason Sams78aebd82009-09-15 13:06:59 -07001049 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001050 + (row * Defines.ROWS_PER_PAGE) + col;
1051 }
1052
Jason Samsb52dfa02009-10-14 20:16:14 -07001053 int chooseTappedIconVert(int x, int y, float pos) {
1054 int ydead = (getHeight() - 4 * 145) / 2;
1055 if (y < ydead || y > (getHeight() - ydead)) {
1056 return -1;
1057 }
1058
1059 y -= ydead;
1060 y += pos * 145;
1061 int row = y / 145;
1062 int col = x / 120;
1063
1064 return row * 4 + col;
1065 }
1066
Joe Onorato82ca5502009-10-15 16:59:23 -07001067 int chooseTappedIcon(int x, int y, float pos) {
1068 int index;
1069 if (mViewMode != 0) {
1070 index = chooseTappedIconHorz(x, y, pos);
1071 } else {
1072 index = chooseTappedIconVert(x, y, pos);
1073 }
1074 final int iconCount = mAllAppsList.size();
1075 if (index >= iconCount) {
1076 index = -1;
1077 }
1078 return index;
1079 }
1080
Jason Samsd8152b92009-10-13 17:19:10 -07001081 boolean setView(int v) {
1082 mViewMode = v;
1083 mRS.contextBindRootScript(mScript[mViewMode]);
1084 return (v == 0);
1085 }
1086
1087 void fling() {
1088 mInvokeFling[mViewMode].execute();
1089 }
1090
1091 void move() {
1092 mInvokeMove[mViewMode].execute();
1093 }
1094
Joe Onorato6665c0f2009-09-02 15:27:24 -07001095 /**
1096 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001097 *
1098 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001099 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001100 int selectIcon(int x, int y, float pos) {
1101 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001102 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001103 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001104 }
1105
1106 void selectIcon(int index) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001107 if (index < 0) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001108 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001109 } else {
1110 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001111
1112 Bitmap selectionBitmap = mSelectionBitmap;
1113
1114 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1115 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1116 mAllAppsList.get(index).iconBitmap);
1117
1118 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001119 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001120 mSelectedIcon.uploadToTexture(0);
1121 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001122 }
1123 }
1124
1125 /**
1126 * You need to call save() on mState on your own after calling this.
1127 */
1128 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001129 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001130 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001131
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 }
Joe Onorato93839052009-08-06 20:34:32 -07001133}
1134
1135