blob: 4a8cc771930bebb0a1b013895a57b85e4affc902 [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;
48import android.os.Handler;
49import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070050import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070051import android.util.AttributeSet;
52import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070053import android.view.KeyEvent;
54import android.view.MotionEvent;
Joe Onorato93839052009-08-06 20:34:32 -070055import android.view.Surface;
56import android.view.SurfaceHolder;
57import android.view.SurfaceView;
Joe Onorato6665c0f2009-09-02 15:27:24 -070058import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070059import android.view.VelocityTracker;
60import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070061import android.graphics.PixelFormat;
62
63
Joe Onorato6665c0f2009-09-02 15:27:24 -070064public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070065 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040066 private static final String TAG = "Launcher.AllAppsView";
67
Joe Onoratofb0ca672009-09-14 17:55:46 -040068 /** Bit for mLocks for when there are icons being loaded. */
69 private static final int LOCK_ICONS_PENDING = 1;
70
Joe Onoratobcbeab82009-10-01 21:45:43 -070071 private static final int TRACKING_FLING = 0;
72 private static final int TRACKING_HOME = 1;
73
Joe Onorato6665c0f2009-09-02 15:27:24 -070074 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070075 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040076
77 /** When this is 0, modifications are allowed, when it's not, they're not.
78 * TODO: What about scrolling? */
79 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070080
Joe Onoratof7b0e012009-10-01 14:09:15 -070081 private int mSlopX;
82 private int mMaxFlingVelocity;
83
Joe Onoratobcbeab82009-10-01 21:45:43 -070084 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070085 private RenderScript mRS;
86 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040087 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070088
Joe Onoratoc567acb2009-08-31 14:34:43 -070089 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070090 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070091 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -070092 private int mTouchTracking;
Joe Onorato1feb3a82009-08-08 22:32:00 -070093 private int mLastMotionX;
Joe Onorato5162ea92009-09-03 09:39:42 -070094 private int mMotionDownRawX;
95 private int mMotionDownRawY;
Joe Onoratobcbeab82009-10-01 21:45:43 -070096 private int mHomeButtonTop;
Jason Sams86c87ed2009-09-18 13:55:55 -070097 private long mTouchTime;
Joe Onorato360d0352009-09-28 14:37:53 -040098 private boolean mZoomSwipeInProgress;
Joe Onorato1feb3a82009-08-08 22:32:00 -070099
Joe Onorato6665c0f2009-09-02 15:27:24 -0700100 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700101 public static final int ALLOC_PARAMS = 0;
102 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700103 public static final int ALLOC_READBACK = 2;
104 public static final int ALLOC_ICON_IDS = 3;
105 public static final int ALLOC_LABEL_IDS = 4;
106 public static final int ALLOC_X_BORDERS = 5;
107 public static final int ALLOC_Y_BORDERS = 6;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700108
109 public static final int COLUMNS_PER_PAGE = 4;
110 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700111
Joe Onorato6665c0f2009-09-02 15:27:24 -0700112 public static final float RADIUS = 4.0f;
113
Joe Onorato6665c0f2009-09-02 15:27:24 -0700114 public static final int ICON_WIDTH_PX = 64;
115 public static final int ICON_TEXTURE_WIDTH_PX = 128;
116
117 public static final int ICON_HEIGHT_PX = 64;
118 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
119 public static final float ICON_TOP_OFFSET = 0.2f;
120
121 public static final float CAMERA_Z = -2;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700122
123 public int SCREEN_WIDTH_PX;
124 public int SCREEN_HEIGHT_PX;
125
126 public float FAR_ICON_SIZE;
127
128 public void recompute(int w, int h) {
129 SCREEN_WIDTH_PX = 480;
130 SCREEN_HEIGHT_PX = 800;
131 FAR_ICON_SIZE = farSize(2 * ICON_WIDTH_PX / (float)w);
132 }
133
134 private static float farSize(float sizeAt0) {
135 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
136 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700137 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700138
139 public AllAppsView(Context context, AttributeSet attrs) {
140 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700141 setFocusable(true);
142 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700143 final ViewConfiguration config = ViewConfiguration.get(context);
144 mSlopX = config.getScaledTouchSlop();
145 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
146
Joe Onorato6665c0f2009-09-02 15:27:24 -0700147 setOnClickListener(this);
148 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700149 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700150 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700151 }
152
Joe Onorato7c312c12009-08-13 21:36:53 -0700153 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
154 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700155 }
156
Joe Onorato6665c0f2009-09-02 15:27:24 -0700157 public void setLauncher(Launcher launcher) {
158 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700159 }
160
Joe Onorato1feb3a82009-08-08 22:32:00 -0700161 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700162 public void surfaceDestroyed(SurfaceHolder holder) {
163 super.surfaceDestroyed(holder);
164
165 destroyRenderScript();
166 mRS = null;
167 mRollo = null;
168 }
169
170 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700171 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
172 super.surfaceChanged(holder, format, w, h);
173
Joe Onorato6665c0f2009-09-02 15:27:24 -0700174 long startTime = SystemClock.uptimeMillis();
175
Jason Sams05de32a2009-09-27 14:01:40 -0700176 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700177 mRollo = new RolloRS();
178 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 if (mAllAppsList != null) {
180 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700181 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700183
184 Resources res = getContext().getResources();
185 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700186 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700187
188 long endTime = SystemClock.uptimeMillis();
189 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700190 }
191
192 @Override
193 public boolean onKeyDown(int keyCode, KeyEvent event)
194 {
Joe Onorato93839052009-08-06 20:34:32 -0700195 // this method doesn't work when 'extends View' include 'extends ScrollView'.
196 return super.onKeyDown(keyCode, event);
197 }
198
Joe Onorato93839052009-08-06 20:34:32 -0700199 @Override
200 public boolean onTouchEvent(MotionEvent ev)
201 {
Joe Onorato7bb17492009-09-24 17:51:01 -0700202 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700203 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700204 }
205
Joe Onoratofb0ca672009-09-14 17:55:46 -0400206 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700207 return true;
208 }
209
210 super.onTouchEvent(ev);
211
Joe Onoratofb0ca672009-09-14 17:55:46 -0400212 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700213 int y = (int)ev.getY();
214
Joe Onoratofb0ca672009-09-14 17:55:46 -0400215 int deltaX;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700216 int action = ev.getAction();
217 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400218 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700219 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
220 mTouchTracking = TRACKING_HOME;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700221 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700222 mTouchTracking = TRACKING_FLING;
223
224 mMotionDownRawX = (int)ev.getRawX();
225 mMotionDownRawY = (int)ev.getRawY();
226 mLastMotionX = x;
227 mRollo.mReadback.read();
228
229 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
230 mRollo.mState.newTouchDown = 1;
231
232 if (!mRollo.checkClickOK()) {
233 mRollo.clearSelectedIcon();
234 } else {
235 mRollo.selectIcon(x, y, mRollo.mReadback.posX);
236 }
237 mRollo.mState.save();
238 mRollo.mInvokeMove.execute();
239 mVelocity = VelocityTracker.obtain();
240 mVelocity.addMovement(ev);
241 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700242 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400243 break;
244 case MotionEvent.ACTION_MOVE:
245 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700246 if (mTouchTracking == TRACKING_HOME) {
247 // TODO: highlight?
Joe Onoratofb0ca672009-09-14 17:55:46 -0400248 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700249 int slopX = Math.abs(x - mLastMotionX);
250 if (!mStartedScrolling && slopX < mSlopX) {
251 // don't update mLastMotionX so slopX is right and when we do start scrolling
252 // below, we get the right delta.
253 } else {
254 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
255 mRollo.mState.newTouchDown = 1;
256 mRollo.mInvokeMove.execute();
Jason Sams86c87ed2009-09-18 13:55:55 -0700257
Joe Onoratobcbeab82009-10-01 21:45:43 -0700258 mStartedScrolling = true;
259 mRollo.clearSelectedIcon();
260 deltaX = x - mLastMotionX;
261 mVelocity.addMovement(ev);
262 mRollo.mState.save();
263 mLastMotionX = x;
264 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400265 }
266 break;
267 case MotionEvent.ACTION_UP:
268 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700269 if (mTouchTracking == TRACKING_HOME) {
270 if (action == MotionEvent.ACTION_UP) {
271 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
272 mLauncher.closeAllApps(true);
273 }
274 }
Jason Sams476339d2009-09-29 18:14:38 -0700275 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700276 mRollo.mState.newTouchDown = 0;
277 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
Jason Sams476339d2009-09-29 18:14:38 -0700278
Joe Onoratobcbeab82009-10-01 21:45:43 -0700279 if (!mZoomSwipeInProgress) {
280 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
281 mRollo.mState.flingVelocityX
282 = mVelocity.getXVelocity() / mDefines.SCREEN_WIDTH_PX;
283 mRollo.clearSelectedIcon();
284 mRollo.mState.save();
285 mRollo.mInvokeFling.execute();
286 } else {
287 mRollo.mState.save();
288 mRollo.mInvokeMove.execute();
289 }
290
291 mLastMotionX = -10000;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700292 if (mVelocity != null) {
293 mVelocity.recycle();
294 mVelocity = null;
295 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700296 break;
297 }
Joe Onorato93839052009-08-06 20:34:32 -0700298 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700299
300 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700301 }
302
Joe Onorato6665c0f2009-09-02 15:27:24 -0700303 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700304 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400305 return;
306 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700307 int index = mRollo.mState.selectedIconIndex;
Jason Sams476339d2009-09-29 18:14:38 -0700308 if (mRollo.checkClickOK() && index >= 0 && index < mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700309 ApplicationInfo app = mAllAppsList.get(index);
310 mLauncher.startActivitySafely(app.intent);
311 }
312 }
313
314 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700315 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400316 return true;
317 }
Joe Onorato5162ea92009-09-03 09:39:42 -0700318 int index = mRollo.mState.selectedIconIndex;
Jason Sams476339d2009-09-29 18:14:38 -0700319 Log.d(TAG, "long click! velocity=" + mRollo.mReadback.velocity + " index=" + index);
320 if (mRollo.checkClickOK() && index >= 0 && index < mAllAppsList.size()) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700321 ApplicationInfo app = mAllAppsList.get(index);
322
323 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700324 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
325 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700326
Joe Onoratobcbeab82009-10-01 21:45:43 -0700327 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
328 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700329 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700330 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700331 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700332
Joe Onorato7bb17492009-09-24 17:51:01 -0700333 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700334 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700335 return true;
336 }
337
Joe Onorato5162ea92009-09-03 09:39:42 -0700338 public void setDragController(DragController dragger) {
339 mDragController = dragger;
340 }
341
342 public void onDropCompleted(View target, boolean success) {
343 }
344
Joe Onorato360d0352009-09-28 14:37:53 -0400345 public void setZoomSwipeInProgress(boolean swiping, boolean touchStillDown) {
346 mZoomSwipeInProgress = swiping;
347 if (!touchStillDown) {
348 mRollo.mState.newTouchDown = 0;
349 mRollo.mState.save();
350 mRollo.mInvokeTouchUp.execute();
351 }
352 }
353
Joe Onorato7bb17492009-09-24 17:51:01 -0700354 public void setZoomTarget(float amount) {
355 zoom(amount, true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700356 }
357
Joe Onorato7bb17492009-09-24 17:51:01 -0700358 public void setZoom(float amount) {
359 zoom(amount, false);
360 }
361
362 private void zoom(float amount, boolean animate) {
363 if (mRollo == null) {
364 return;
365 }
366
Joe Onoratofb0ca672009-09-14 17:55:46 -0400367 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400368 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700369 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700370 // set in readback, so we're correct even before the next frame
371 mRollo.mReadback.zoom = mRollo.mState.zoomTarget = amount;
Joe Onorato7bb17492009-09-24 17:51:01 -0700372 if (!animate) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700373 mRollo.mState.zoom = amount;
Joe Onorato7bb17492009-09-24 17:51:01 -0700374 mRollo.mReadback.save();
375 }
Joe Onorato85a02a82009-09-08 12:34:22 -0700376 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700377 mRollo.mState.zoomTarget = 0;
378 if (!animate) {
379 mRollo.mReadback.zoom = mRollo.mState.zoom = 0;
380 mRollo.mReadback.save();
381 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700382 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400383 mRollo.mState.save();
Joe Onorato7bb17492009-09-24 17:51:01 -0700384 if (!animate) {
385 mRollo.mInvokeSetZoom.execute();
386 } else {
387 mRollo.mInvokeSetZoomTarget.execute();
388 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700389 mReadZoom.removeMessages(1);
390 mReadZoom.sendEmptyMessageDelayed(1, 1000);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400391 }
392
Joe Onorato8e099e22009-09-25 17:30:47 -0700393 Handler mReadZoom = new Handler() {
394 public void handleMessage(Message msg) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700395 if(mRollo != null && mRollo.mReadback != null) {
396 // FIXME: These checks may indicate other problems.
397 mRollo.mReadback.read();
398 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700399 }
400 };
401
Joe Onoratofb0ca672009-09-14 17:55:46 -0400402 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700403 if (mRollo == null) {
404 return false;
405 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700406 //mRollo.mReadback.read();
Joe Onorato7bb17492009-09-24 17:51:01 -0700407 return mRollo.mReadback.zoom > 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 Onorato6665c0f2009-09-02 15:27:24 -0700429 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400430 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700431 }
432
433 private void invokeIcon(int index) {
434 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700435 }
436
437 private static int countPages(int iconCount) {
438 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
439 int pages = iconCount / iconsPerPage;
440 if (pages*iconsPerPage != iconCount) {
441 pages++;
442 }
443 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400444 }
445
Joe Onorato93839052009-08-06 20:34:32 -0700446 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700447
Joe Onorato1feb3a82009-08-08 22:32:00 -0700448 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700449
Joe Onorato93839052009-08-06 20:34:32 -0700450 private int mWidth;
451 private int mHeight;
452
453 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700454 private Script mScript;
Jason Sams86c87ed2009-09-18 13:55:55 -0700455
456 private Script.Invokable mInvokeMove;
457 private Script.Invokable mInvokeFling;
Joe Onorato7bb17492009-09-24 17:51:01 -0700458 private Script.Invokable mInvokeSetZoomTarget;
459 private Script.Invokable mInvokeSetZoom;
Joe Onorato360d0352009-09-28 14:37:53 -0400460 private Script.Invokable mInvokeTouchUp;
Jason Sams86c87ed2009-09-18 13:55:55 -0700461
Jason Samscd689e12009-09-29 15:28:22 -0700462 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700463 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700464 private ProgramFragment mPFColor;
465 private ProgramFragment mPFTexLinear;
466 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700467 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700468 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700469 private SimpleMesh mMesh;
Joe Onorato93839052009-08-06 20:34:32 -0700470
Joe Onoratobcbeab82009-10-01 21:45:43 -0700471 private Allocation mHomeButton;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700472
Joe Onoratobf15cb42009-08-07 14:33:40 -0700473 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700474 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700475 private Allocation mAllocIconID;
476
Joe Onoratobf15cb42009-08-07 14:33:40 -0700477 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700478 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700479 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700480 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700481
Joe Onorato6665c0f2009-09-02 15:27:24 -0700482 private int[] mTouchYBorders;
483 private Allocation mAllocTouchYBorders;
484 private int[] mTouchXBorders;
485 private Allocation mAllocTouchXBorders;
486
487 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400488 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700489
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700490 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700491 State mState;
Joe Onorato7bb17492009-09-24 17:51:01 -0700492 Readback mReadback;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700493
Jason Sams78aebd82009-09-15 13:06:59 -0700494 class BaseAlloc {
495 Allocation mAlloc;
496 Type mType;
497
498 void save() {
499 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700500 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700501 }
502
Jason Sams476339d2009-09-29 18:14:38 -0700503 private boolean checkClickOK() {
504 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
505 return (Math.abs(mReadback.velocity) < 0.1f) &&
506 (Math.abs(mReadback.posX - Math.round(mReadback.posX)) < 0.1f);
507 }
508
Jason Sams78aebd82009-09-15 13:06:59 -0700509 class Params extends BaseAlloc {
510 Params() {
511 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
512 mAlloc = Allocation.createTyped(mRS, mType);
513 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700514 }
Jason Sams78aebd82009-09-15 13:06:59 -0700515 public int bubbleWidth;
516 public int bubbleHeight;
517 public int bubbleBitmapWidth;
518 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700519
520 public int homeButtonId;
521 public int homeButtonWidth;
522 public int homeButtonHeight;
523 public int homeButtonTextureWidth;
524 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700525 }
526
527 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700528 public float newPositionX;
529 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700530 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700531 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700532 public int selectedIconIndex = -1;
533 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700534 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700535 public float zoom;
536
537 State() {
538 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
539 mAlloc = Allocation.createTyped(mRS, mType);
540 save();
541 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700542 }
543
Joe Onorato7bb17492009-09-24 17:51:01 -0700544 class Readback extends BaseAlloc {
545 public float posX;
546 public float velocity;
547 public float zoom;
548
549 Readback() {
550 mType = Type.createFromClass(mRS, Readback.class, 1, "ReadbackClass");
551 mAlloc = Allocation.createTyped(mRS, mType);
552 save();
553 }
554
555 void read() {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700556 if(mAlloc != null) {
557 mAlloc.read(this);
558 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700559 }
560 }
561
Joe Onorato1feb3a82009-08-08 22:32:00 -0700562 public RolloRS() {
563 }
564
565 public void init(Resources res, int width, int height) {
566 mRes = res;
567 mWidth = width;
568 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700569 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700570 initProgramVertex();
571 initProgramFragment();
572 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700573 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700574 initGl();
575 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700576 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700577 initRs();
578 }
579
Jason Sams0aa71662009-10-02 18:43:18 -0700580 public void initMesh() {
581 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
582 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
583
584 for (int ct=0; ct < 450; ct++) {
585 float x = 0;
586 float z = 0;
587 float l = 1.f;
588
589 if (ct < 190) {
590 z = 0.1f + 0.05f * (190 - ct);
591 x = -1;
592 l = 0.125f + (0.125f / 190.f) * ct;
593 } else if (ct >= 190 && ct < 200) {
594 float a = (3.14f * 0.5f) * (0.1f * (ct - 200));
595 float s = (float)Math.sin(a);
596 float c = (float)Math.cos(a);
597 x = -0.9f + s * 0.1f;
598 z = 0.1f - c * 0.1f;
599 l = 0.25f + 0.075f * (ct - 190);
600 } else if (ct >= 200 && ct < 250) {
601 z = 0.f;
602 x = -0.9f + (1.8f * (ct - 200) / 50.f);
603 } else if (ct >= 250 && ct < 260) {
604 float a = (3.14f * 0.5f) * (0.1f * (ct - 250));
605 float s = (float)Math.sin(a);
606 float c = (float)Math.cos(a);
607 x = 0.9f + s * 0.1f;
608 z = 0.1f - c * 0.1f;
609 l = 0.25f + 0.075f * (260 - ct);
610 } else if (ct >= 260) {
611 z = 0.1f + 0.05f * (ct - 260);
612 x = 1;
613 l = 0.125f + (0.125f / 190.f) * (450 - ct);
614 }
615 //Log.e("rs", "ct " + Integer.toString(ct) + " x = " + Float.toString(x) + ", z = " + Float.toString(z));
616 //Log.e("rs", "ct " + Integer.toString(ct) + " l = " + Float.toString(l));
617 float s = ct * 0.1f;
618 tm.setColor(l, l, l, 0.99f);
619 tm.setTexture(s, 1);
620 tm.addVertex(x, -0.5f, z);
621 tm.setTexture(s, 0);
622 tm.addVertex(x, 0.5f, z);
623 }
624 for (int ct=0; ct < (450*2 - 2); ct+= 2) {
625 tm.addTriangle(ct, ct+1, ct+2);
626 tm.addTriangle(ct+1, ct+3, ct+2);
627 }
628 mMesh = tm.create();
629 mMesh.setName("SMMesh");
630
631 }
632
633
Jason Samscd689e12009-09-29 15:28:22 -0700634 private void initProgramVertex() {
635 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
636 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700637
638 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700639 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700640 mPV = pvb.create();
641 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700642 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700643
Jason Samscd689e12009-09-29 15:28:22 -0700644 pva = new ProgramVertex.MatrixAllocation(mRS);
645 pva.setupOrthoWindow(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700646 pvb.setTextureMatrixEnable(true);
647 mPVOrtho = pvb.create();
648 mPVOrtho.setName("PVOrtho");
Jason Samscd689e12009-09-29 15:28:22 -0700649 mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700650
651 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700652 }
Joe Onorato93839052009-08-06 20:34:32 -0700653
Jason Samscd689e12009-09-29 15:28:22 -0700654 private void initProgramFragment() {
655 Sampler.Builder sb = new Sampler.Builder(mRS);
656 sb.setMin(Sampler.Value.LINEAR);
657 sb.setMag(Sampler.Value.LINEAR);
658 sb.setWrapS(Sampler.Value.CLAMP);
659 sb.setWrapT(Sampler.Value.CLAMP);
660 Sampler linear = sb.create();
661
662 sb.setMin(Sampler.Value.NEAREST);
663 sb.setMag(Sampler.Value.NEAREST);
664 Sampler nearest = sb.create();
665
666 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
667 mPFColor = bf.create();
668 mPFColor.setName("PFColor");
669
670 bf.setTexEnable(true, 0);
671 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
672 mPFTexLinear = bf.create();
673 mPFTexLinear.setName("PFTexLinear");
674 mPFTexLinear.bindSampler(linear, 0);
675
676 mPFTexNearest = bf.create();
677 mPFTexNearest.setName("PFTexNearest");
678 mPFTexNearest.bindSampler(nearest, 0);
679 }
680
681 private void initProgramStore() {
682 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
683 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
684 bs.setDitherEnable(true);
685 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
686 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
687 mPSIcons = bs.create();
688 mPSIcons.setName("PSIcons");
689
690 //bs.setDitherEnable(false);
691 //mPSText = bs.create();
692 //mPSText.setName("PSText");
693 }
694
695 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700696 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700697 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700698 mTouchXBorders.length);
699 mAllocTouchXBorders.data(mTouchXBorders);
700
701 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700702 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700703 mTouchYBorders.length);
704 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700705 }
Jason Sams78aebd82009-09-15 13:06:59 -0700706
Joe Onorato1feb3a82009-08-08 22:32:00 -0700707 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700708 mParams = new Params();
709 mState = new State();
Joe Onorato7bb17492009-09-24 17:51:01 -0700710 mReadback = new Readback();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700711
Joe Onoratobf15cb42009-08-07 14:33:40 -0700712 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700713
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700714 mParams.bubbleWidth = bubble.getBubbleWidth();
715 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
716 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
717 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700718
Joe Onoratobcbeab82009-10-01 21:45:43 -0700719 mHomeButton = Allocation.createFromBitmapResource(mRS, mRes,
720 R.drawable.home_button, Element.RGBA_8888(mRS), false);
721 mHomeButton.uploadToTexture(0);
722 mParams.homeButtonId = mHomeButton.getID();
723 mParams.homeButtonWidth = 76;
724 mParams.homeButtonHeight = 68;
725 mParams.homeButtonTextureWidth = 128;
726 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700727
Joe Onorato1feb3a82009-08-08 22:32:00 -0700728 mParams.save();
729 mState.save();
Joe Onorato7bb17492009-09-24 17:51:01 -0700730 mReadback.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731
Joe Onorato1291a8c2009-09-15 15:07:25 -0400732 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
733 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
734 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700735
736 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700738 }
739
Joe Onorato1feb3a82009-08-08 22:32:00 -0700740 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700741 ScriptC.Builder sb = new ScriptC.Builder(mRS);
742 sb.setScript(mRes, R.raw.rollo);
Jason Sams0aa71662009-10-02 18:43:18 -0700743 //sb.setScript(mRes, R.raw.rollo2);
Joe Onorato93839052009-08-06 20:34:32 -0700744 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700745 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700746 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
747 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700748 sb.setType(mReadback.mType, "readback", Defines.ALLOC_READBACK);
Jason Sams86c87ed2009-09-18 13:55:55 -0700749 mInvokeMove = sb.addInvokable("move");
750 mInvokeFling = sb.addInvokable("fling");
Joe Onorato7bb17492009-09-24 17:51:01 -0700751 mInvokeSetZoomTarget = sb.addInvokable("setZoomTarget");
752 mInvokeSetZoom = sb.addInvokable("setZoom");
Joe Onorato360d0352009-09-28 14:37:53 -0400753 mInvokeTouchUp = sb.addInvokable("touchUp");
Joe Onorato93839052009-08-06 20:34:32 -0700754 mScript = sb.create();
755 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
756
Jason Sams78aebd82009-09-15 13:06:59 -0700757 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
758 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700759 mScript.bindAllocation(mReadback.mAlloc, Defines.ALLOC_READBACK);
Joe Onoratod769a632009-08-11 17:09:02 -0700760 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700761 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700762 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
763 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700764
765 mRS.contextBindRootScript(mScript);
766 }
Joe Onorato93839052009-08-06 20:34:32 -0700767
Joe Onorato9c1289c2009-08-17 11:03:03 -0400768 private void setApps(ArrayList<ApplicationInfo> list) {
769 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700770 int allocCount = count;
771 if(allocCount < 1) {
772 allocCount = 1;
773 }
774
Joe Onorato9c1289c2009-08-17 11:03:03 -0400775 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700776 mIconIds = new int[allocCount];
777 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400778
779 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700780 mLabelIds = new int[allocCount];
781 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700783 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400784
785 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
786
787 for (int i=0; i<count; i++) {
788 final ApplicationInfo item = list.get(i);
789
790 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700791 Element.RGBA_8888(mRS), false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400792 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700793 Element.RGBA_8888(mRS), false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400794
795 mIcons[i].uploadToTexture(0);
796 mLabels[i].uploadToTexture(0);
797
Joe Onorato6665c0f2009-09-02 15:27:24 -0700798 mIconIds[i] = mIcons[i].getID();
799 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400800 }
801
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700802 mAllocIconID.data(mIconIds);
803 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400804
805 mState.iconCount = count;
806
Joe Onorato9c1289c2009-08-17 11:03:03 -0400807 if (mScript != null) { // wtf
808 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
809 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
810 }
811
812 mState.save();
813 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700814
815 void initTouchState() {
816 int width = getWidth();
817 int height = getHeight();
818
819 int iconsSize;
820 if (width < height) {
821 iconsSize = width;
822 } else {
823 iconsSize = height;
824 }
825 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
826 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
827
Joe Onorato56848b02009-09-25 13:59:59 -0700828 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
829 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
830 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700831 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -0700832 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
833 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700834
835 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700836
Joe Onorato6665c0f2009-09-02 15:27:24 -0700837 int centerX = (width / 2);
838 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -0700839 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700840 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -0700841 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700842 mTouchXBorders[4] = centerX + (2 * cellWidth);
843
844 mAllocTouchXBorders.data(mTouchXBorders);
845 }
846
Jason Sams86c87ed2009-09-18 13:55:55 -0700847 int chooseTappedIcon(int x, int y, float page) {
848 int currentPage = (int)page;
849
Joe Onorato6665c0f2009-09-02 15:27:24 -0700850 int col = -1;
851 int row = -1;
852
853 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
854 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
855 col = i;
856 break;
857 }
858 }
859 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
860 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
861 row = i;
862 break;
863 }
864 }
865
866 if (row < 0 || col < 0) {
867 return -1;
868 }
869
Jason Sams78aebd82009-09-15 13:06:59 -0700870 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700871 + (row * Defines.ROWS_PER_PAGE) + col;
872 }
873
874 /**
875 * You need to call save() on mState on your own after calling this.
876 */
Jason Sams86c87ed2009-09-18 13:55:55 -0700877 void selectIcon(int x, int y, float pos) {
878 int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400879 selectIcon(index);
880 }
881
882 void selectIcon(int index) {
883 Log.d(TAG, "selectIcon index=" + index);
884 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700885 if (index < 0 || index >= iconCount) {
886 mState.selectedIconIndex = -1;
887 return;
888 } else {
889 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400890
891 Bitmap selectionBitmap = mSelectionBitmap;
892
893 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
894 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
895 mAllAppsList.get(index).iconBitmap);
896
897 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700898 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400899 mSelectedIcon.uploadToTexture(0);
900 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700901 }
902 }
903
904 /**
905 * You need to call save() on mState on your own after calling this.
906 */
907 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400908 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400910 }
Joe Onorato93839052009-08-06 20:34:32 -0700911}
912
913