blob: 4cd017b0bb3e290898c824c8a4da18368c87712d [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;
23
24import android.renderscript.RSSurfaceView;
25import android.renderscript.RenderScript;
26
27import android.renderscript.RenderScript;
28import android.renderscript.ProgramVertex;
29import android.renderscript.Element;
30import android.renderscript.Allocation;
Jason Sams78aebd82009-09-15 13:06:59 -070031import android.renderscript.Type;
Joe Onorato93839052009-08-06 20:34:32 -070032import android.renderscript.Script;
33import android.renderscript.ScriptC;
34import android.renderscript.ProgramFragment;
35import android.renderscript.ProgramStore;
36import android.renderscript.Sampler;
Jason Sams0aa71662009-10-02 18:43:18 -070037import android.renderscript.SimpleMesh;
Joe Onorato93839052009-08-06 20:34:32 -070038
39import android.content.Context;
40import android.content.res.Resources;
Joe Onorato7c312c12009-08-13 21:36:53 -070041import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070042import android.graphics.Bitmap;
43import android.graphics.BitmapFactory;
44import android.graphics.Canvas;
45import android.graphics.Paint;
Joe Onorato93839052009-08-06 20:34:32 -070046import android.graphics.drawable.BitmapDrawable;
47import android.graphics.drawable.Drawable;
Joe Onorato93839052009-08-06 20:34:32 -070048import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070049import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070050import android.util.AttributeSet;
51import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070052import android.view.KeyEvent;
53import android.view.MotionEvent;
Joe Onorato93839052009-08-06 20:34:32 -070054import android.view.Surface;
55import android.view.SurfaceHolder;
56import android.view.SurfaceView;
Joe Onorato6665c0f2009-09-02 15:27:24 -070057import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070058import android.view.VelocityTracker;
59import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070060import android.graphics.PixelFormat;
61
62
Joe Onorato6665c0f2009-09-02 15:27:24 -070063public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070064 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040065 private static final String TAG = "Launcher.AllAppsView";
66
Joe Onoratofb0ca672009-09-14 17:55:46 -040067 /** Bit for mLocks for when there are icons being loaded. */
68 private static final int LOCK_ICONS_PENDING = 1;
69
Joe Onoratobcbeab82009-10-01 21:45:43 -070070 private static final int TRACKING_FLING = 0;
71 private static final int TRACKING_HOME = 1;
72
Joe Onorato6665c0f2009-09-02 15:27:24 -070073 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070074 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040075
76 /** When this is 0, modifications are allowed, when it's not, they're not.
77 * TODO: What about scrolling? */
78 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070079
Joe Onoratof7b0e012009-10-01 14:09:15 -070080 private int mSlopX;
81 private int mMaxFlingVelocity;
82
Joe Onoratobcbeab82009-10-01 21:45:43 -070083 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070084 private RenderScript mRS;
85 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040086 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070087
Joe Onoratoc567acb2009-08-31 14:34:43 -070088 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070089 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070090 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -070091 private int mTouchTracking;
Joe Onorato1feb3a82009-08-08 22:32:00 -070092 private int mLastMotionX;
Joe Onorato5162ea92009-09-03 09:39:42 -070093 private int mMotionDownRawX;
94 private int mMotionDownRawY;
Joe Onoratobcbeab82009-10-01 21:45:43 -070095 private int mHomeButtonTop;
Jason Sams86c87ed2009-09-18 13:55:55 -070096 private long mTouchTime;
Joe Onorato1feb3a82009-08-08 22:32:00 -070097
Joe Onorato6665c0f2009-09-02 15:27:24 -070098 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -070099 public static final int ALLOC_PARAMS = 0;
100 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700101 public static final int ALLOC_ICON_IDS = 3;
102 public static final int ALLOC_LABEL_IDS = 4;
103 public static final int ALLOC_X_BORDERS = 5;
104 public static final int ALLOC_Y_BORDERS = 6;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700105
106 public static final int COLUMNS_PER_PAGE = 4;
107 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700108
Joe Onorato6665c0f2009-09-02 15:27:24 -0700109 public static final float RADIUS = 4.0f;
110
Joe Onorato6665c0f2009-09-02 15:27:24 -0700111 public static final int ICON_WIDTH_PX = 64;
112 public static final int ICON_TEXTURE_WIDTH_PX = 128;
113
114 public static final int ICON_HEIGHT_PX = 64;
115 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
116 public static final float ICON_TOP_OFFSET = 0.2f;
117
118 public static final float CAMERA_Z = -2;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700119
120 public int SCREEN_WIDTH_PX;
121 public int SCREEN_HEIGHT_PX;
122
123 public float FAR_ICON_SIZE;
124
125 public void recompute(int w, int h) {
126 SCREEN_WIDTH_PX = 480;
127 SCREEN_HEIGHT_PX = 800;
128 FAR_ICON_SIZE = farSize(2 * ICON_WIDTH_PX / (float)w);
129 }
130
131 private static float farSize(float sizeAt0) {
132 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
133 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700134 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700135
136 public AllAppsView(Context context, AttributeSet attrs) {
137 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700138 setFocusable(true);
139 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700140 final ViewConfiguration config = ViewConfiguration.get(context);
141 mSlopX = config.getScaledTouchSlop();
142 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
143
Joe Onorato6665c0f2009-09-02 15:27:24 -0700144 setOnClickListener(this);
145 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700146 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700147 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700148 }
149
Joe Onorato7c312c12009-08-13 21:36:53 -0700150 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
151 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700152 }
153
Joe Onorato6665c0f2009-09-02 15:27:24 -0700154 public void setLauncher(Launcher launcher) {
155 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700156 }
157
Joe Onorato1feb3a82009-08-08 22:32:00 -0700158 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700159 public void surfaceDestroyed(SurfaceHolder holder) {
160 super.surfaceDestroyed(holder);
161
162 destroyRenderScript();
163 mRS = null;
164 mRollo = null;
165 }
166
167 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700168 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
169 super.surfaceChanged(holder, format, w, h);
170
Joe Onorato6665c0f2009-09-02 15:27:24 -0700171 long startTime = SystemClock.uptimeMillis();
172
Jason Sams05de32a2009-09-27 14:01:40 -0700173 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700174 mRollo = new RolloRS();
175 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 if (mAllAppsList != null) {
177 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700178 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700180
181 Resources res = getContext().getResources();
182 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700183 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700184
185 long endTime = SystemClock.uptimeMillis();
186 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700187 }
188
189 @Override
190 public boolean onKeyDown(int keyCode, KeyEvent event)
191 {
Joe Onorato93839052009-08-06 20:34:32 -0700192 // this method doesn't work when 'extends View' include 'extends ScrollView'.
193 return super.onKeyDown(keyCode, event);
194 }
195
Joe Onorato93839052009-08-06 20:34:32 -0700196 @Override
197 public boolean onTouchEvent(MotionEvent ev)
198 {
Joe Onorato7bb17492009-09-24 17:51:01 -0700199 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700200 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700201 }
202
Joe Onoratofb0ca672009-09-14 17:55:46 -0400203 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700204 return true;
205 }
206
207 super.onTouchEvent(ev);
208
Joe Onoratofb0ca672009-09-14 17:55:46 -0400209 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700210 int y = (int)ev.getY();
211
Joe Onoratofb0ca672009-09-14 17:55:46 -0400212 int deltaX;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700213 int action = ev.getAction();
214 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400215 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700216 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
217 mTouchTracking = TRACKING_HOME;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700218 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700219 mTouchTracking = TRACKING_FLING;
220
221 mMotionDownRawX = (int)ev.getRawX();
222 mMotionDownRawY = (int)ev.getRawY();
223 mLastMotionX = x;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700224
225 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
226 mRollo.mState.newTouchDown = 1;
227
228 if (!mRollo.checkClickOK()) {
229 mRollo.clearSelectedIcon();
230 } else {
Jason Sams12c14a82009-10-06 14:33:15 -0700231 mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700232 }
233 mRollo.mState.save();
234 mRollo.mInvokeMove.execute();
235 mVelocity = VelocityTracker.obtain();
236 mVelocity.addMovement(ev);
237 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700238 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400239 break;
240 case MotionEvent.ACTION_MOVE:
241 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700242 if (mTouchTracking == TRACKING_HOME) {
243 // TODO: highlight?
Joe Onoratofb0ca672009-09-14 17:55:46 -0400244 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700245 int slopX = Math.abs(x - mLastMotionX);
246 if (!mStartedScrolling && slopX < mSlopX) {
247 // don't update mLastMotionX so slopX is right and when we do start scrolling
248 // below, we get the right delta.
249 } else {
250 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
251 mRollo.mState.newTouchDown = 1;
252 mRollo.mInvokeMove.execute();
Jason Sams86c87ed2009-09-18 13:55:55 -0700253
Joe Onoratobcbeab82009-10-01 21:45:43 -0700254 mStartedScrolling = true;
255 mRollo.clearSelectedIcon();
256 deltaX = x - mLastMotionX;
257 mVelocity.addMovement(ev);
258 mRollo.mState.save();
259 mLastMotionX = x;
260 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400261 }
262 break;
263 case MotionEvent.ACTION_UP:
264 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700265 if (mTouchTracking == TRACKING_HOME) {
266 if (action == MotionEvent.ACTION_UP) {
267 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
268 mLauncher.closeAllApps(true);
269 }
270 }
Jason Sams476339d2009-09-29 18:14:38 -0700271 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700272 mRollo.mState.newTouchDown = 0;
273 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
Jason Sams476339d2009-09-29 18:14:38 -0700274
Jason Sams12c14a82009-10-06 14:33:15 -0700275 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
276 mRollo.mState.flingVelocityX
277 = mVelocity.getXVelocity() / mDefines.SCREEN_WIDTH_PX;
278 mRollo.clearSelectedIcon();
279 mRollo.mState.save();
280 mRollo.mInvokeFling.execute();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700281
282 mLastMotionX = -10000;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700283 if (mVelocity != null) {
284 mVelocity.recycle();
285 mVelocity = null;
286 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700287 break;
288 }
Joe Onorato93839052009-08-06 20:34:32 -0700289 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700290
291 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700292 }
293
Joe Onorato6665c0f2009-09-02 15:27:24 -0700294 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700295 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400296 return;
297 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700298 int index = mRollo.mState.selectedIconIndex;
Jason Sams476339d2009-09-29 18:14:38 -0700299 if (mRollo.checkClickOK() && index >= 0 && index < mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700300 ApplicationInfo app = mAllAppsList.get(index);
301 mLauncher.startActivitySafely(app.intent);
302 }
303 }
304
305 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700306 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400307 return true;
308 }
Joe Onorato5162ea92009-09-03 09:39:42 -0700309 int index = mRollo.mState.selectedIconIndex;
Jason Sams12c14a82009-10-06 14:33:15 -0700310 Log.d(TAG, "long click! velocity=" + mRollo.mMessageProc.mVelocity + " index=" + index);
Jason Sams476339d2009-09-29 18:14:38 -0700311 if (mRollo.checkClickOK() && index >= 0 && index < mAllAppsList.size()) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700312 ApplicationInfo app = mAllAppsList.get(index);
313
314 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700315 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
316 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700317
Joe Onoratobcbeab82009-10-01 21:45:43 -0700318 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
319 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700320 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700321 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700322 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700323
Joe Onorato7bb17492009-09-24 17:51:01 -0700324 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700325 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700326 return true;
327 }
328
Joe Onorato5162ea92009-09-03 09:39:42 -0700329 public void setDragController(DragController dragger) {
330 mDragController = dragger;
331 }
332
333 public void onDropCompleted(View target, boolean success) {
334 }
335
Joe Onorato4db52312009-10-06 11:17:43 -0700336 /**
337 * Zoom to the specifed amount.
338 *
339 * @param amount [0..1] 0 is hidden, 1 is open
340 * @param animate Whether to animate.
341 */
Jason Sams12c14a82009-10-06 14:33:15 -0700342 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700343 if (mRollo == null) {
344 return;
345 }
346
Joe Onoratofb0ca672009-09-14 17:55:46 -0400347 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400348 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700349 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700350 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700351 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700352 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700353 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700354 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400355 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400356 }
357
358 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700359 if (mRollo == null) {
360 return false;
361 }
Jason Sams12c14a82009-10-06 14:33:15 -0700362 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400363 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700364
Joe Onorato93839052009-08-06 20:34:32 -0700365 @Override
366 public boolean onTrackballEvent(MotionEvent ev)
367 {
368 float x = ev.getX();
369 float y = ev.getY();
370 //Float tx = new Float(x);
371 //Float ty = new Float(y);
372 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
373
374
375 return true;
376 }
377
Joe Onorato9c1289c2009-08-17 11:03:03 -0400378 public void setApps(ArrayList<ApplicationInfo> list) {
379 mAllAppsList = list;
380 if (mRollo != null) {
381 mRollo.setApps(list);
382 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700383 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700384 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400385 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700386 }
387
388 private void invokeIcon(int index) {
389 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700390 }
391
392 private static int countPages(int iconCount) {
393 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
394 int pages = iconCount / iconsPerPage;
395 if (pages*iconsPerPage != iconCount) {
396 pages++;
397 }
398 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400399 }
400
Joe Onorato93839052009-08-06 20:34:32 -0700401 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700402
Joe Onorato1feb3a82009-08-08 22:32:00 -0700403 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700404
Joe Onorato93839052009-08-06 20:34:32 -0700405 private int mWidth;
406 private int mHeight;
407
408 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700409 private Script mScript;
Jason Sams86c87ed2009-09-18 13:55:55 -0700410
411 private Script.Invokable mInvokeMove;
412 private Script.Invokable mInvokeFling;
Joe Onorato360d0352009-09-28 14:37:53 -0400413 private Script.Invokable mInvokeTouchUp;
Jason Sams86c87ed2009-09-18 13:55:55 -0700414
Jason Samscd689e12009-09-29 15:28:22 -0700415 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700416 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700417 private ProgramFragment mPFColor;
418 private ProgramFragment mPFTexLinear;
419 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700420 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700421 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700422 private SimpleMesh mMesh;
Joe Onorato93839052009-08-06 20:34:32 -0700423
Joe Onoratobcbeab82009-10-01 21:45:43 -0700424 private Allocation mHomeButton;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700425
Joe Onoratobf15cb42009-08-07 14:33:40 -0700426 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700427 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700428 private Allocation mAllocIconID;
429
Joe Onoratobf15cb42009-08-07 14:33:40 -0700430 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700431 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700432 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700433 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700434
Joe Onorato6665c0f2009-09-02 15:27:24 -0700435 private int[] mTouchYBorders;
436 private Allocation mAllocTouchYBorders;
437 private int[] mTouchXBorders;
438 private Allocation mAllocTouchXBorders;
439
440 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400441 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700442
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700443 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700444 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700445
Jason Sams78aebd82009-09-15 13:06:59 -0700446 class BaseAlloc {
447 Allocation mAlloc;
448 Type mType;
449
450 void save() {
451 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700452 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700453 }
454
Jason Sams12c14a82009-10-06 14:33:15 -0700455 class AAMessage extends RenderScript.RSMessage {
456 public void run() {
457 mPosX = ((float)mData[0]) / (1 << 16);
458 mVelocity = ((float)mData[1]) / (1 << 16);
459 mZoom = ((float)mData[2]) / (1 << 16);
460 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
461 }
462 float mZoom;
463 float mPosX;
464 float mVelocity;
465 }
466 AAMessage mMessageProc;
467
Jason Sams476339d2009-09-29 18:14:38 -0700468 private boolean checkClickOK() {
469 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams12c14a82009-10-06 14:33:15 -0700470 return (Math.abs(mMessageProc.mVelocity) < 0.1f) &&
471 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.1f);
Jason Sams476339d2009-09-29 18:14:38 -0700472 }
473
Jason Sams78aebd82009-09-15 13:06:59 -0700474 class Params extends BaseAlloc {
475 Params() {
476 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
477 mAlloc = Allocation.createTyped(mRS, mType);
478 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700479 }
Jason Sams78aebd82009-09-15 13:06:59 -0700480 public int bubbleWidth;
481 public int bubbleHeight;
482 public int bubbleBitmapWidth;
483 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700484
485 public int homeButtonId;
486 public int homeButtonWidth;
487 public int homeButtonHeight;
488 public int homeButtonTextureWidth;
489 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700490 }
491
492 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700493 public float newPositionX;
494 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700495 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700496 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700497 public int selectedIconIndex = -1;
498 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700499 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700500
501 State() {
502 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
503 mAlloc = Allocation.createTyped(mRS, mType);
504 save();
505 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700506 }
507
508 public RolloRS() {
509 }
510
511 public void init(Resources res, int width, int height) {
512 mRes = res;
513 mWidth = width;
514 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700515 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700516 initProgramVertex();
517 initProgramFragment();
518 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700519 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700520 initGl();
521 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700522 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700523 initRs();
524 }
525
Jason Sams0aa71662009-10-02 18:43:18 -0700526 public void initMesh() {
527 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
528 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
529
530 for (int ct=0; ct < 450; ct++) {
531 float x = 0;
532 float z = 0;
533 float l = 1.f;
534
535 if (ct < 190) {
536 z = 0.1f + 0.05f * (190 - ct);
537 x = -1;
538 l = 0.125f + (0.125f / 190.f) * ct;
539 } else if (ct >= 190 && ct < 200) {
540 float a = (3.14f * 0.5f) * (0.1f * (ct - 200));
541 float s = (float)Math.sin(a);
542 float c = (float)Math.cos(a);
543 x = -0.9f + s * 0.1f;
544 z = 0.1f - c * 0.1f;
545 l = 0.25f + 0.075f * (ct - 190);
546 } else if (ct >= 200 && ct < 250) {
547 z = 0.f;
548 x = -0.9f + (1.8f * (ct - 200) / 50.f);
549 } else if (ct >= 250 && ct < 260) {
550 float a = (3.14f * 0.5f) * (0.1f * (ct - 250));
551 float s = (float)Math.sin(a);
552 float c = (float)Math.cos(a);
553 x = 0.9f + s * 0.1f;
554 z = 0.1f - c * 0.1f;
555 l = 0.25f + 0.075f * (260 - ct);
556 } else if (ct >= 260) {
557 z = 0.1f + 0.05f * (ct - 260);
558 x = 1;
559 l = 0.125f + (0.125f / 190.f) * (450 - ct);
560 }
561 //Log.e("rs", "ct " + Integer.toString(ct) + " x = " + Float.toString(x) + ", z = " + Float.toString(z));
562 //Log.e("rs", "ct " + Integer.toString(ct) + " l = " + Float.toString(l));
563 float s = ct * 0.1f;
564 tm.setColor(l, l, l, 0.99f);
565 tm.setTexture(s, 1);
566 tm.addVertex(x, -0.5f, z);
567 tm.setTexture(s, 0);
568 tm.addVertex(x, 0.5f, z);
569 }
570 for (int ct=0; ct < (450*2 - 2); ct+= 2) {
571 tm.addTriangle(ct, ct+1, ct+2);
572 tm.addTriangle(ct+1, ct+3, ct+2);
573 }
574 mMesh = tm.create();
575 mMesh.setName("SMMesh");
576
577 }
578
579
Jason Samscd689e12009-09-29 15:28:22 -0700580 private void initProgramVertex() {
581 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
582 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700583
584 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700585 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700586 mPV = pvb.create();
587 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700588 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700589
Jason Samscd689e12009-09-29 15:28:22 -0700590 pva = new ProgramVertex.MatrixAllocation(mRS);
591 pva.setupOrthoWindow(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700592 pvb.setTextureMatrixEnable(true);
593 mPVOrtho = pvb.create();
594 mPVOrtho.setName("PVOrtho");
Jason Samscd689e12009-09-29 15:28:22 -0700595 mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700596
597 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700598 }
Joe Onorato93839052009-08-06 20:34:32 -0700599
Jason Samscd689e12009-09-29 15:28:22 -0700600 private void initProgramFragment() {
601 Sampler.Builder sb = new Sampler.Builder(mRS);
602 sb.setMin(Sampler.Value.LINEAR);
603 sb.setMag(Sampler.Value.LINEAR);
604 sb.setWrapS(Sampler.Value.CLAMP);
605 sb.setWrapT(Sampler.Value.CLAMP);
606 Sampler linear = sb.create();
607
608 sb.setMin(Sampler.Value.NEAREST);
609 sb.setMag(Sampler.Value.NEAREST);
610 Sampler nearest = sb.create();
611
612 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
613 mPFColor = bf.create();
614 mPFColor.setName("PFColor");
615
616 bf.setTexEnable(true, 0);
617 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
618 mPFTexLinear = bf.create();
619 mPFTexLinear.setName("PFTexLinear");
620 mPFTexLinear.bindSampler(linear, 0);
621
622 mPFTexNearest = bf.create();
623 mPFTexNearest.setName("PFTexNearest");
624 mPFTexNearest.bindSampler(nearest, 0);
625 }
626
627 private void initProgramStore() {
628 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
629 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700630 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700631 bs.setDitherEnable(true);
632 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
633 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
634 mPSIcons = bs.create();
635 mPSIcons.setName("PSIcons");
636
637 //bs.setDitherEnable(false);
638 //mPSText = bs.create();
639 //mPSText.setName("PSText");
640 }
641
642 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700643 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700644 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700645 mTouchXBorders.length);
646 mAllocTouchXBorders.data(mTouchXBorders);
647
648 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700649 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700650 mTouchYBorders.length);
651 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700652 }
Jason Sams78aebd82009-09-15 13:06:59 -0700653
Joe Onorato1feb3a82009-08-08 22:32:00 -0700654 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700655 mParams = new Params();
656 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700657
Joe Onoratobf15cb42009-08-07 14:33:40 -0700658 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700659
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700660 mParams.bubbleWidth = bubble.getBubbleWidth();
661 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
662 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
663 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700664
Joe Onoratobcbeab82009-10-01 21:45:43 -0700665 mHomeButton = Allocation.createFromBitmapResource(mRS, mRes,
666 R.drawable.home_button, Element.RGBA_8888(mRS), false);
667 mHomeButton.uploadToTexture(0);
668 mParams.homeButtonId = mHomeButton.getID();
669 mParams.homeButtonWidth = 76;
670 mParams.homeButtonHeight = 68;
671 mParams.homeButtonTextureWidth = 128;
672 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700673
Joe Onorato1feb3a82009-08-08 22:32:00 -0700674 mParams.save();
675 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400676
Joe Onorato1291a8c2009-09-15 15:07:25 -0400677 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
678 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
679 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700680
681 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400682 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700683 }
684
Joe Onorato1feb3a82009-08-08 22:32:00 -0700685 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700686 ScriptC.Builder sb = new ScriptC.Builder(mRS);
687 sb.setScript(mRes, R.raw.rollo);
Jason Sams0aa71662009-10-02 18:43:18 -0700688 //sb.setScript(mRes, R.raw.rollo2);
Joe Onorato93839052009-08-06 20:34:32 -0700689 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700690 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700691 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
692 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams86c87ed2009-09-18 13:55:55 -0700693 mInvokeMove = sb.addInvokable("move");
694 mInvokeFling = sb.addInvokable("fling");
Joe Onorato360d0352009-09-28 14:37:53 -0400695 mInvokeTouchUp = sb.addInvokable("touchUp");
Joe Onorato93839052009-08-06 20:34:32 -0700696 mScript = sb.create();
697 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
698
Jason Sams78aebd82009-09-15 13:06:59 -0700699 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
700 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
Joe Onoratod769a632009-08-11 17:09:02 -0700701 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700702 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700703 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
704 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700705
Jason Sams12c14a82009-10-06 14:33:15 -0700706 mMessageProc = new AAMessage();
707 mRS.mMessageCallback = mMessageProc;
Joe Onorato93839052009-08-06 20:34:32 -0700708 mRS.contextBindRootScript(mScript);
709 }
Joe Onorato93839052009-08-06 20:34:32 -0700710
Joe Onorato9c1289c2009-08-17 11:03:03 -0400711 private void setApps(ArrayList<ApplicationInfo> list) {
712 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700713 int allocCount = count;
714 if(allocCount < 1) {
715 allocCount = 1;
716 }
717
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700719 mIconIds = new int[allocCount];
720 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400721
722 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700723 mLabelIds = new int[allocCount];
724 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400725
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700726 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400727
728 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
729
730 for (int i=0; i<count; i++) {
731 final ApplicationInfo item = list.get(i);
732
733 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700734 Element.RGBA_8888(mRS), false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700736 Element.RGBA_8888(mRS), false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737
738 mIcons[i].uploadToTexture(0);
739 mLabels[i].uploadToTexture(0);
740
Joe Onorato6665c0f2009-09-02 15:27:24 -0700741 mIconIds[i] = mIcons[i].getID();
742 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743 }
744
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700745 mAllocIconID.data(mIconIds);
746 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747
748 mState.iconCount = count;
749
Joe Onorato9c1289c2009-08-17 11:03:03 -0400750 if (mScript != null) { // wtf
751 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
752 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
753 }
754
755 mState.save();
756 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700757
758 void initTouchState() {
759 int width = getWidth();
760 int height = getHeight();
761
762 int iconsSize;
763 if (width < height) {
764 iconsSize = width;
765 } else {
766 iconsSize = height;
767 }
768 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
769 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
770
Joe Onorato56848b02009-09-25 13:59:59 -0700771 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
772 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
773 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700774 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -0700775 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
776 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700777
778 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700779
Joe Onorato6665c0f2009-09-02 15:27:24 -0700780 int centerX = (width / 2);
781 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -0700782 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700783 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -0700784 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700785 mTouchXBorders[4] = centerX + (2 * cellWidth);
786
787 mAllocTouchXBorders.data(mTouchXBorders);
788 }
789
Jason Sams86c87ed2009-09-18 13:55:55 -0700790 int chooseTappedIcon(int x, int y, float page) {
791 int currentPage = (int)page;
792
Joe Onorato6665c0f2009-09-02 15:27:24 -0700793 int col = -1;
794 int row = -1;
795
796 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
797 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
798 col = i;
799 break;
800 }
801 }
802 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
803 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
804 row = i;
805 break;
806 }
807 }
808
809 if (row < 0 || col < 0) {
810 return -1;
811 }
812
Jason Sams78aebd82009-09-15 13:06:59 -0700813 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700814 + (row * Defines.ROWS_PER_PAGE) + col;
815 }
816
817 /**
818 * You need to call save() on mState on your own after calling this.
819 */
Jason Sams86c87ed2009-09-18 13:55:55 -0700820 void selectIcon(int x, int y, float pos) {
821 int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400822 selectIcon(index);
823 }
824
825 void selectIcon(int index) {
826 Log.d(TAG, "selectIcon index=" + index);
827 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700828 if (index < 0 || index >= iconCount) {
829 mState.selectedIconIndex = -1;
830 return;
831 } else {
832 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400833
834 Bitmap selectionBitmap = mSelectionBitmap;
835
836 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
837 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
838 mAllAppsList.get(index).iconBitmap);
839
840 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700841 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400842 mSelectedIcon.uploadToTexture(0);
843 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700844 }
845 }
846
847 /**
848 * You need to call save() on mState on your own after calling this.
849 */
850 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400851 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700852 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400853 }
Joe Onorato93839052009-08-06 20:34:32 -0700854}
855
856