blob: bf439f5c91b4bbfab75ae814633c3bd827212b87 [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;
37
38import android.content.Context;
39import android.content.res.Resources;
Joe Onorato7c312c12009-08-13 21:36:53 -070040import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070041import android.graphics.Bitmap;
42import android.graphics.BitmapFactory;
43import android.graphics.Canvas;
44import android.graphics.Paint;
Joe Onorato93839052009-08-06 20:34:32 -070045import android.graphics.drawable.BitmapDrawable;
46import android.graphics.drawable.Drawable;
47import android.os.Handler;
48import 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 Onorato6665c0f2009-09-02 15:27:24 -070070 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070071 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040072
73 /** When this is 0, modifications are allowed, when it's not, they're not.
74 * TODO: What about scrolling? */
75 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070076
Joe Onorato1feb3a82009-08-08 22:32:00 -070077 private RenderScript mRS;
78 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040079 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070080
Joe Onoratod769a632009-08-11 17:09:02 -070081 private ViewConfiguration mConfig;
Joe Onoratoc567acb2009-08-31 14:34:43 -070082 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070083 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070084 private VelocityTracker mVelocity;
Joe Onorato1feb3a82009-08-08 22:32:00 -070085 private int mLastMotionX;
Joe Onorato5162ea92009-09-03 09:39:42 -070086 private int mMotionDownRawX;
87 private int mMotionDownRawY;
Joe Onoratoc567acb2009-08-31 14:34:43 -070088 private int mScrollHandleTop;
Jason Sams86c87ed2009-09-18 13:55:55 -070089 private long mTouchTime;
Joe Onorato360d0352009-09-28 14:37:53 -040090 private boolean mZoomSwipeInProgress;
Joe Onorato1feb3a82009-08-08 22:32:00 -070091
Joe Onorato6665c0f2009-09-02 15:27:24 -070092 static class Defines {
93 private static float farSize(float sizeAt0) {
94 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
95 }
96
Joe Onoratoc567acb2009-08-31 14:34:43 -070097 public static final int ALLOC_PARAMS = 0;
98 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -070099 public static final int ALLOC_READBACK = 2;
100 public static final int ALLOC_ICON_IDS = 3;
101 public static final int ALLOC_LABEL_IDS = 4;
102 public static final int ALLOC_X_BORDERS = 5;
103 public static final int ALLOC_Y_BORDERS = 6;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700104
105 public static final int COLUMNS_PER_PAGE = 4;
106 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700107
Joe Onorato6665c0f2009-09-02 15:27:24 -0700108 public static final float RADIUS = 4.0f;
109
110 public static final int SCREEN_WIDTH_PX = 480;
111 public static final int SCREEN_HEIGHT_PX = 854;
112
113 public static final int ICON_WIDTH_PX = 64;
114 public static final int ICON_TEXTURE_WIDTH_PX = 128;
115
116 public static final int ICON_HEIGHT_PX = 64;
117 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
118 public static final float ICON_TOP_OFFSET = 0.2f;
119
120 public static final float CAMERA_Z = -2;
121 public static final float FAR_ICON_SIZE
122 = farSize(2 * ICON_WIDTH_PX / (float)SCREEN_WIDTH_PX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700123 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700124
125 public AllAppsView(Context context, AttributeSet attrs) {
126 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700127 setFocusable(true);
128 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratod769a632009-08-11 17:09:02 -0700129 mConfig = ViewConfiguration.get(context);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700130 setOnClickListener(this);
131 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700132 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700133 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700134 }
135
Joe Onorato7c312c12009-08-13 21:36:53 -0700136 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
137 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700138 }
139
Joe Onorato6665c0f2009-09-02 15:27:24 -0700140 public void setLauncher(Launcher launcher) {
141 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700142 }
143
Joe Onorato1feb3a82009-08-08 22:32:00 -0700144 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700145 public void surfaceDestroyed(SurfaceHolder holder) {
146 super.surfaceDestroyed(holder);
147
148 destroyRenderScript();
149 mRS = null;
150 mRollo = null;
151 }
152
153 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700154 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
155 super.surfaceChanged(holder, format, w, h);
156
Joe Onorato6665c0f2009-09-02 15:27:24 -0700157 long startTime = SystemClock.uptimeMillis();
158
Jason Sams05de32a2009-09-27 14:01:40 -0700159 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700160 mRollo = new RolloRS();
161 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400162 if (mAllAppsList != null) {
163 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700164 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400165 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700166
167 Resources res = getContext().getResources();
168 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
169 mScrollHandleTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700170
171 long endTime = SystemClock.uptimeMillis();
172 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700173 }
174
175 @Override
176 public boolean onKeyDown(int keyCode, KeyEvent event)
177 {
Joe Onorato93839052009-08-06 20:34:32 -0700178 // this method doesn't work when 'extends View' include 'extends ScrollView'.
179 return super.onKeyDown(keyCode, event);
180 }
181
Joe Onorato93839052009-08-06 20:34:32 -0700182 @Override
183 public boolean onTouchEvent(MotionEvent ev)
184 {
Joe Onorato7bb17492009-09-24 17:51:01 -0700185 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700186 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700187 }
188
Joe Onoratofb0ca672009-09-14 17:55:46 -0400189 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700190 return true;
191 }
192
193 super.onTouchEvent(ev);
194
Joe Onoratofb0ca672009-09-14 17:55:46 -0400195 int x = (int)ev.getX();
196 int deltaX;
197 switch (ev.getAction()) {
198 case MotionEvent.ACTION_DOWN:
199 mMotionDownRawX = (int)ev.getRawX();
200 mMotionDownRawY = (int)ev.getRawY();
201 mLastMotionX = x;
Joe Onorato7bb17492009-09-24 17:51:01 -0700202 mRollo.mReadback.read();
Jason Sams86c87ed2009-09-18 13:55:55 -0700203
204 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
205 mRollo.mState.newTouchDown = 1;
206
Jason Sams476339d2009-09-29 18:14:38 -0700207 if (!mRollo.checkClickOK()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400208 mRollo.clearSelectedIcon();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700209 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700210 mRollo.selectIcon(x, (int)ev.getY(), mRollo.mReadback.posX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700211 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400212 mRollo.mState.save();
Jason Sams86c87ed2009-09-18 13:55:55 -0700213 mRollo.mInvokeMove.execute();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400214 mVelocity = VelocityTracker.obtain();
215 mVelocity.addMovement(ev);
216 mStartedScrolling = false;
217 break;
218 case MotionEvent.ACTION_MOVE:
219 case MotionEvent.ACTION_OUTSIDE:
220 int slop = Math.abs(x - mLastMotionX);
221 if (!mStartedScrolling && slop < mConfig.getScaledTouchSlop()) {
222 // don't update mLastMotionX so slop is right and when we do start scrolling
223 // below, we get the right delta.
224 } else {
Jason Sams86c87ed2009-09-18 13:55:55 -0700225
226 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
227 mRollo.mState.newTouchDown = 1;
228 mRollo.mInvokeMove.execute();
229
Joe Onoratofb0ca672009-09-14 17:55:46 -0400230 mStartedScrolling = true;
231 mRollo.clearSelectedIcon();
232 deltaX = x - mLastMotionX;
233 mVelocity.addMovement(ev);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400234 mRollo.mState.save();
235 mLastMotionX = x;
236 }
237 break;
238 case MotionEvent.ACTION_UP:
239 case MotionEvent.ACTION_CANCEL:
Jason Sams476339d2009-09-29 18:14:38 -0700240 mRollo.mState.newTouchDown = 0;
241 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
Jason Sams86c87ed2009-09-18 13:55:55 -0700242
Jason Sams476339d2009-09-29 18:14:38 -0700243 if (!mZoomSwipeInProgress) {
Joe Onorato360d0352009-09-28 14:37:53 -0400244 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
245 mConfig.getScaledMaximumFlingVelocity());
246 mRollo.mState.flingVelocityX = mVelocity.getXVelocity() / Defines.SCREEN_WIDTH_PX;
247 mRollo.clearSelectedIcon();
248 mRollo.mState.save();
249 mRollo.mInvokeFling.execute();
Jason Sams476339d2009-09-29 18:14:38 -0700250 } else {
251 mRollo.mState.save();
252 mRollo.mInvokeMove.execute();
Joe Onorato360d0352009-09-28 14:37:53 -0400253 }
Jason Sams476339d2009-09-29 18:14:38 -0700254
Joe Onoratofb0ca672009-09-14 17:55:46 -0400255 mLastMotionX = -10000;
256 mVelocity.recycle();
257 mVelocity = null;
258 break;
Joe Onorato93839052009-08-06 20:34:32 -0700259 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700260
261 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700262 }
263
Joe Onorato6665c0f2009-09-02 15:27:24 -0700264 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700265 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400266 return;
267 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700268 int index = mRollo.mState.selectedIconIndex;
Jason Sams476339d2009-09-29 18:14:38 -0700269 if (mRollo.checkClickOK() && index >= 0 && index < mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700270 ApplicationInfo app = mAllAppsList.get(index);
271 mLauncher.startActivitySafely(app.intent);
272 }
273 }
274
275 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700276 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400277 return true;
278 }
Joe Onorato5162ea92009-09-03 09:39:42 -0700279 int index = mRollo.mState.selectedIconIndex;
Jason Sams476339d2009-09-29 18:14:38 -0700280 Log.d(TAG, "long click! velocity=" + mRollo.mReadback.velocity + " index=" + index);
281 if (mRollo.checkClickOK() && index >= 0 && index < mAllAppsList.size()) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700282 ApplicationInfo app = mAllAppsList.get(index);
283
284 // We don't really have an accurate location to use. This will do.
285 int screenX = mMotionDownRawX - (Defines.ICON_WIDTH_PX / 2);
286 int screenY = mMotionDownRawY - Defines.ICON_HEIGHT_PX;
287
288 int left = (Defines.ICON_TEXTURE_WIDTH_PX - Defines.ICON_WIDTH_PX) / 2;
289 int top = (Defines.ICON_TEXTURE_HEIGHT_PX - Defines.ICON_HEIGHT_PX) / 2;
290 mDragController.startDrag(app.iconBitmap, screenX, screenY,
291 left, top, Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
292 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700293
Joe Onorato7bb17492009-09-24 17:51:01 -0700294 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700295 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700296 return true;
297 }
298
Joe Onorato5162ea92009-09-03 09:39:42 -0700299 public void setDragController(DragController dragger) {
300 mDragController = dragger;
301 }
302
303 public void onDropCompleted(View target, boolean success) {
304 }
305
Joe Onorato360d0352009-09-28 14:37:53 -0400306 public void setZoomSwipeInProgress(boolean swiping, boolean touchStillDown) {
307 mZoomSwipeInProgress = swiping;
308 if (!touchStillDown) {
309 mRollo.mState.newTouchDown = 0;
310 mRollo.mState.save();
311 mRollo.mInvokeTouchUp.execute();
312 }
313 }
314
Joe Onorato7bb17492009-09-24 17:51:01 -0700315 public void setZoomTarget(float amount) {
316 zoom(amount, true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700317 }
318
Joe Onorato7bb17492009-09-24 17:51:01 -0700319 public void setZoom(float amount) {
320 zoom(amount, false);
321 }
322
323 private void zoom(float amount, boolean animate) {
324 if (mRollo == null) {
325 return;
326 }
327
Joe Onoratofb0ca672009-09-14 17:55:46 -0400328 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400329 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700330 if (amount > 0.001f) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700331 mRollo.mState.zoomTarget = amount;
332 if (!animate) {
333 // set in readback, so we're correct even before the next frame
334 mRollo.mReadback.zoom = mRollo.mState.zoom = amount;
335 mRollo.mReadback.save();
336 }
Joe Onorato85a02a82009-09-08 12:34:22 -0700337 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700338 mRollo.mState.zoomTarget = 0;
339 if (!animate) {
340 mRollo.mReadback.zoom = mRollo.mState.zoom = 0;
341 mRollo.mReadback.save();
342 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700343 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400344 mRollo.mState.save();
Joe Onorato7bb17492009-09-24 17:51:01 -0700345 if (!animate) {
346 mRollo.mInvokeSetZoom.execute();
347 } else {
348 mRollo.mInvokeSetZoomTarget.execute();
349 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700350 mReadZoom.removeMessages(1);
351 mReadZoom.sendEmptyMessageDelayed(1, 1000);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400352 }
353
Joe Onorato8e099e22009-09-25 17:30:47 -0700354 Handler mReadZoom = new Handler() {
355 public void handleMessage(Message msg) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700356 if(mRollo != null && mRollo.mReadback != null) {
357 // FIXME: These checks may indicate other problems.
358 mRollo.mReadback.read();
359 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700360 }
361 };
362
Joe Onoratofb0ca672009-09-14 17:55:46 -0400363 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700364 if (mRollo == null) {
365 return false;
366 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700367 //mRollo.mReadback.read();
Joe Onorato7bb17492009-09-24 17:51:01 -0700368 return mRollo.mReadback.zoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400369 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700370
Joe Onorato93839052009-08-06 20:34:32 -0700371 @Override
372 public boolean onTrackballEvent(MotionEvent ev)
373 {
374 float x = ev.getX();
375 float y = ev.getY();
376 //Float tx = new Float(x);
377 //Float ty = new Float(y);
378 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
379
380
381 return true;
382 }
383
Joe Onorato9c1289c2009-08-17 11:03:03 -0400384 public void setApps(ArrayList<ApplicationInfo> list) {
385 mAllAppsList = list;
386 if (mRollo != null) {
387 mRollo.setApps(list);
388 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700389 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700390 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400391 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700392 }
393
394 private void invokeIcon(int index) {
395 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700396 }
397
398 private static int countPages(int iconCount) {
399 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
400 int pages = iconCount / iconsPerPage;
401 if (pages*iconsPerPage != iconCount) {
402 pages++;
403 }
404 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400405 }
406
Joe Onorato93839052009-08-06 20:34:32 -0700407 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700408
Joe Onorato1feb3a82009-08-08 22:32:00 -0700409 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700410
Joe Onorato93839052009-08-06 20:34:32 -0700411 private int mWidth;
412 private int mHeight;
413
414 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700415 private Script mScript;
Jason Sams86c87ed2009-09-18 13:55:55 -0700416
417 private Script.Invokable mInvokeMove;
418 private Script.Invokable mInvokeFling;
Joe Onorato7bb17492009-09-24 17:51:01 -0700419 private Script.Invokable mInvokeSetZoomTarget;
420 private Script.Invokable mInvokeSetZoom;
Joe Onorato360d0352009-09-28 14:37:53 -0400421 private Script.Invokable mInvokeTouchUp;
Jason Sams86c87ed2009-09-18 13:55:55 -0700422
Jason Samscd689e12009-09-29 15:28:22 -0700423 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700424 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700425 private ProgramFragment mPFColor;
426 private ProgramFragment mPFTexLinear;
427 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700428 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700429 private ProgramVertex mPVOrtho;
Joe Onorato93839052009-08-06 20:34:32 -0700430
Joe Onoratoc567acb2009-08-31 14:34:43 -0700431 private Allocation mScrollHandle;
432
Joe Onoratobf15cb42009-08-07 14:33:40 -0700433 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700434 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700435 private Allocation mAllocIconID;
436
Joe Onoratobf15cb42009-08-07 14:33:40 -0700437 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700438 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700439 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700440 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700441
Joe Onorato6665c0f2009-09-02 15:27:24 -0700442 private int[] mTouchYBorders;
443 private Allocation mAllocTouchYBorders;
444 private int[] mTouchXBorders;
445 private Allocation mAllocTouchXBorders;
446
447 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400448 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700449
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700450 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700451 State mState;
Joe Onorato7bb17492009-09-24 17:51:01 -0700452 Readback mReadback;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700453
Jason Sams78aebd82009-09-15 13:06:59 -0700454 class BaseAlloc {
455 Allocation mAlloc;
456 Type mType;
457
458 void save() {
459 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700460 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700461 }
462
Jason Sams476339d2009-09-29 18:14:38 -0700463 private boolean checkClickOK() {
464 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
465 return (Math.abs(mReadback.velocity) < 0.1f) &&
466 (Math.abs(mReadback.posX - Math.round(mReadback.posX)) < 0.1f);
467 }
468
Jason Sams78aebd82009-09-15 13:06:59 -0700469 class Params extends BaseAlloc {
470 Params() {
471 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
472 mAlloc = Allocation.createTyped(mRS, mType);
473 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700474 }
Jason Sams78aebd82009-09-15 13:06:59 -0700475 public int bubbleWidth;
476 public int bubbleHeight;
477 public int bubbleBitmapWidth;
478 public int bubbleBitmapHeight;
479 public int scrollHandleId;
480 public int scrollHandleTextureWidth;
481 public int scrollHandleTextureHeight;
482 }
483
484 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700485 public float newPositionX;
486 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700487 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700488 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700489 public int selectedIconIndex = -1;
490 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700491 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700492 public float zoom;
493
494 State() {
495 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
496 mAlloc = Allocation.createTyped(mRS, mType);
497 save();
498 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700499 }
500
Joe Onorato7bb17492009-09-24 17:51:01 -0700501 class Readback extends BaseAlloc {
502 public float posX;
503 public float velocity;
504 public float zoom;
505
506 Readback() {
507 mType = Type.createFromClass(mRS, Readback.class, 1, "ReadbackClass");
508 mAlloc = Allocation.createTyped(mRS, mType);
509 save();
510 }
511
512 void read() {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700513 if(mAlloc != null) {
514 mAlloc.read(this);
515 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700516 }
517 }
518
Joe Onorato1feb3a82009-08-08 22:32:00 -0700519 public RolloRS() {
520 }
521
522 public void init(Resources res, int width, int height) {
523 mRes = res;
524 mWidth = width;
525 mHeight = height;
Jason Samscd689e12009-09-29 15:28:22 -0700526 initProgramVertex();
527 initProgramFragment();
528 initProgramStore();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700529 initGl();
530 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700531 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700532 initRs();
533 }
534
Jason Samscd689e12009-09-29 15:28:22 -0700535 private void initProgramVertex() {
536 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
537 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700538
539 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
540 mPV = pvb.create();
541 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700542 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700543
Jason Samscd689e12009-09-29 15:28:22 -0700544 pva = new ProgramVertex.MatrixAllocation(mRS);
545 pva.setupOrthoWindow(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700546 pvb.setTextureMatrixEnable(true);
547 mPVOrtho = pvb.create();
548 mPVOrtho.setName("PVOrtho");
Jason Samscd689e12009-09-29 15:28:22 -0700549 mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700550
551 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700552 }
Joe Onorato93839052009-08-06 20:34:32 -0700553
Jason Samscd689e12009-09-29 15:28:22 -0700554 private void initProgramFragment() {
555 Sampler.Builder sb = new Sampler.Builder(mRS);
556 sb.setMin(Sampler.Value.LINEAR);
557 sb.setMag(Sampler.Value.LINEAR);
558 sb.setWrapS(Sampler.Value.CLAMP);
559 sb.setWrapT(Sampler.Value.CLAMP);
560 Sampler linear = sb.create();
561
562 sb.setMin(Sampler.Value.NEAREST);
563 sb.setMag(Sampler.Value.NEAREST);
564 Sampler nearest = sb.create();
565
566 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
567 mPFColor = bf.create();
568 mPFColor.setName("PFColor");
569
570 bf.setTexEnable(true, 0);
571 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
572 mPFTexLinear = bf.create();
573 mPFTexLinear.setName("PFTexLinear");
574 mPFTexLinear.bindSampler(linear, 0);
575
576 mPFTexNearest = bf.create();
577 mPFTexNearest.setName("PFTexNearest");
578 mPFTexNearest.bindSampler(nearest, 0);
579 }
580
581 private void initProgramStore() {
582 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
583 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
584 bs.setDitherEnable(true);
585 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
586 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
587 mPSIcons = bs.create();
588 mPSIcons.setName("PSIcons");
589
590 //bs.setDitherEnable(false);
591 //mPSText = bs.create();
592 //mPSText.setName("PSText");
593 }
594
595 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700596 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700597 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700598 mTouchXBorders.length);
599 mAllocTouchXBorders.data(mTouchXBorders);
600
601 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700602 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700603 mTouchYBorders.length);
604 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700605 }
Jason Sams78aebd82009-09-15 13:06:59 -0700606
Joe Onorato1feb3a82009-08-08 22:32:00 -0700607 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700608 mParams = new Params();
609 mState = new State();
Joe Onorato7bb17492009-09-24 17:51:01 -0700610 mReadback = new Readback();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700611
Joe Onoratobf15cb42009-08-07 14:33:40 -0700612 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700613
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700614 mParams.bubbleWidth = bubble.getBubbleWidth();
615 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
616 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
617 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700618
Joe Onoratoc567acb2009-08-31 14:34:43 -0700619 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700620 R.drawable.all_apps_button_pow2, Element.RGBA_8888(mRS), false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700621 mScrollHandle.uploadToTexture(0);
622 mParams.scrollHandleId = mScrollHandle.getID();
623 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
624 mParams.scrollHandleTextureWidth = 128;
625 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700626
Joe Onoratoc567acb2009-08-31 14:34:43 -0700627
Joe Onorato1feb3a82009-08-08 22:32:00 -0700628 mParams.save();
629 mState.save();
Joe Onorato7bb17492009-09-24 17:51:01 -0700630 mReadback.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631
Joe Onorato1291a8c2009-09-15 15:07:25 -0400632 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
633 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
634 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700635
636 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700638 }
639
Joe Onorato1feb3a82009-08-08 22:32:00 -0700640 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700641 ScriptC.Builder sb = new ScriptC.Builder(mRS);
642 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700643 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700644 sb.addDefines(Defines.class);
Jason Sams78aebd82009-09-15 13:06:59 -0700645 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
646 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700647 sb.setType(mReadback.mType, "readback", Defines.ALLOC_READBACK);
Jason Sams86c87ed2009-09-18 13:55:55 -0700648 mInvokeMove = sb.addInvokable("move");
649 mInvokeFling = sb.addInvokable("fling");
Joe Onorato7bb17492009-09-24 17:51:01 -0700650 mInvokeSetZoomTarget = sb.addInvokable("setZoomTarget");
651 mInvokeSetZoom = sb.addInvokable("setZoom");
Joe Onorato360d0352009-09-28 14:37:53 -0400652 mInvokeTouchUp = sb.addInvokable("touchUp");
Joe Onorato93839052009-08-06 20:34:32 -0700653 mScript = sb.create();
654 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
655
Jason Sams78aebd82009-09-15 13:06:59 -0700656 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
657 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700658 mScript.bindAllocation(mReadback.mAlloc, Defines.ALLOC_READBACK);
Joe Onoratod769a632009-08-11 17:09:02 -0700659 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700660 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700661 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
662 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700663
664 mRS.contextBindRootScript(mScript);
665 }
Joe Onorato93839052009-08-06 20:34:32 -0700666
Joe Onorato9c1289c2009-08-17 11:03:03 -0400667 private void setApps(ArrayList<ApplicationInfo> list) {
668 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700669 int allocCount = count;
670 if(allocCount < 1) {
671 allocCount = 1;
672 }
673
Joe Onorato9c1289c2009-08-17 11:03:03 -0400674 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700675 mIconIds = new int[allocCount];
676 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400677
678 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700679 mLabelIds = new int[allocCount];
680 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400681
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700682 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400683
684 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
685
686 for (int i=0; i<count; i++) {
687 final ApplicationInfo item = list.get(i);
688
689 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700690 Element.RGBA_8888(mRS), false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700692 Element.RGBA_8888(mRS), false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400693
694 mIcons[i].uploadToTexture(0);
695 mLabels[i].uploadToTexture(0);
696
Joe Onorato6665c0f2009-09-02 15:27:24 -0700697 mIconIds[i] = mIcons[i].getID();
698 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400699 }
700
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700701 mAllocIconID.data(mIconIds);
702 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703
704 mState.iconCount = count;
705
Joe Onorato9c1289c2009-08-17 11:03:03 -0400706 if (mScript != null) { // wtf
707 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
708 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
709 }
710
711 mState.save();
712 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700713
714 void initTouchState() {
715 int width = getWidth();
716 int height = getHeight();
717
718 int iconsSize;
719 if (width < height) {
720 iconsSize = width;
721 } else {
722 iconsSize = height;
723 }
724 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
725 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
726
Joe Onorato56848b02009-09-25 13:59:59 -0700727 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
728 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
729 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700730 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -0700731 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
732 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700733
734 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700735
Joe Onorato6665c0f2009-09-02 15:27:24 -0700736 int centerX = (width / 2);
737 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -0700738 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700739 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -0700740 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700741 mTouchXBorders[4] = centerX + (2 * cellWidth);
742
743 mAllocTouchXBorders.data(mTouchXBorders);
744 }
745
Jason Sams86c87ed2009-09-18 13:55:55 -0700746 int chooseTappedIcon(int x, int y, float page) {
747 int currentPage = (int)page;
748
Joe Onorato6665c0f2009-09-02 15:27:24 -0700749 int col = -1;
750 int row = -1;
751
752 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
753 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
754 col = i;
755 break;
756 }
757 }
758 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
759 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
760 row = i;
761 break;
762 }
763 }
764
765 if (row < 0 || col < 0) {
766 return -1;
767 }
768
Jason Sams78aebd82009-09-15 13:06:59 -0700769 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700770 + (row * Defines.ROWS_PER_PAGE) + col;
771 }
772
773 /**
774 * You need to call save() on mState on your own after calling this.
775 */
Jason Sams86c87ed2009-09-18 13:55:55 -0700776 void selectIcon(int x, int y, float pos) {
777 int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400778 selectIcon(index);
779 }
780
781 void selectIcon(int index) {
782 Log.d(TAG, "selectIcon index=" + index);
783 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700784 if (index < 0 || index >= iconCount) {
785 mState.selectedIconIndex = -1;
786 return;
787 } else {
788 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400789
790 Bitmap selectionBitmap = mSelectionBitmap;
791
792 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
793 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
794 mAllAppsList.get(index).iconBitmap);
795
796 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700797 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400798 mSelectedIcon.uploadToTexture(0);
799 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700800 }
801 }
802
803 /**
804 * You need to call save() on mState on your own after calling this.
805 */
806 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400807 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700808 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400809 }
Joe Onorato93839052009-08-06 20:34:32 -0700810}
811
812