blob: 333569d6d2875efb7f2ed4e7b9db385aa15d312a [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 Onorato1feb3a82009-08-08 22:32:00 -070090
Joe Onorato6665c0f2009-09-02 15:27:24 -070091 static class Defines {
92 private static float farSize(float sizeAt0) {
93 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
94 }
95
Joe Onoratoc567acb2009-08-31 14:34:43 -070096 public static final int ALLOC_PARAMS = 0;
97 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -070098 public static final int ALLOC_READBACK = 2;
99 public static final int ALLOC_ICON_IDS = 3;
100 public static final int ALLOC_LABEL_IDS = 4;
101 public static final int ALLOC_X_BORDERS = 5;
102 public static final int ALLOC_Y_BORDERS = 6;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700103
104 public static final int COLUMNS_PER_PAGE = 4;
105 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700106
Joe Onorato6665c0f2009-09-02 15:27:24 -0700107 public static final float RADIUS = 4.0f;
108
109 public static final int SCREEN_WIDTH_PX = 480;
110 public static final int SCREEN_HEIGHT_PX = 854;
111
112 public static final int ICON_WIDTH_PX = 64;
113 public static final int ICON_TEXTURE_WIDTH_PX = 128;
114
115 public static final int ICON_HEIGHT_PX = 64;
116 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
117 public static final float ICON_TOP_OFFSET = 0.2f;
118
119 public static final float CAMERA_Z = -2;
120 public static final float FAR_ICON_SIZE
121 = farSize(2 * ICON_WIDTH_PX / (float)SCREEN_WIDTH_PX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700122 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700123
124 public AllAppsView(Context context, AttributeSet attrs) {
125 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700126 setFocusable(true);
127 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratod769a632009-08-11 17:09:02 -0700128 mConfig = ViewConfiguration.get(context);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700129 setOnClickListener(this);
130 setOnLongClickListener(this);
Dianne Hackbornb8898d52009-09-14 22:30:18 -0700131 setOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700132 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700133 }
134
Joe Onorato7c312c12009-08-13 21:36:53 -0700135 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
136 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700137 }
138
Joe Onorato6665c0f2009-09-02 15:27:24 -0700139 public void setLauncher(Launcher launcher) {
140 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700141 }
142
Joe Onorato1feb3a82009-08-08 22:32:00 -0700143 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700144 public void surfaceDestroyed(SurfaceHolder holder) {
145 super.surfaceDestroyed(holder);
146
147 destroyRenderScript();
148 mRS = null;
149 mRollo = null;
150 }
151
152 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700153 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
154 super.surfaceChanged(holder, format, w, h);
155
Joe Onorato6665c0f2009-09-02 15:27:24 -0700156 long startTime = SystemClock.uptimeMillis();
157
Joe Onorato2df5a572009-09-23 14:43:22 -0700158 mRS = createRenderScript(true, true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700159 mRollo = new RolloRS();
160 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 if (mAllAppsList != null) {
162 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700163 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400164 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700165
166 Resources res = getContext().getResources();
167 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
168 mScrollHandleTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700169
170 long endTime = SystemClock.uptimeMillis();
171 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700172 }
173
174 @Override
175 public boolean onKeyDown(int keyCode, KeyEvent event)
176 {
Joe Onorato93839052009-08-06 20:34:32 -0700177 // this method doesn't work when 'extends View' include 'extends ScrollView'.
178 return super.onKeyDown(keyCode, event);
179 }
180
Joe Onorato93839052009-08-06 20:34:32 -0700181 @Override
182 public boolean onTouchEvent(MotionEvent ev)
183 {
Joe Onorato7bb17492009-09-24 17:51:01 -0700184 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700185 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700186 }
187
Joe Onoratofb0ca672009-09-14 17:55:46 -0400188 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700189 return true;
190 }
191
192 super.onTouchEvent(ev);
193
Joe Onoratofb0ca672009-09-14 17:55:46 -0400194 int x = (int)ev.getX();
195 int deltaX;
196 switch (ev.getAction()) {
197 case MotionEvent.ACTION_DOWN:
198 mMotionDownRawX = (int)ev.getRawX();
199 mMotionDownRawY = (int)ev.getRawY();
200 mLastMotionX = x;
Joe Onorato7bb17492009-09-24 17:51:01 -0700201 mRollo.mReadback.read();
Jason Sams86c87ed2009-09-18 13:55:55 -0700202
203 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
204 mRollo.mState.newTouchDown = 1;
205
Joe Onorato7bb17492009-09-24 17:51:01 -0700206 if (mRollo.mReadback.velocity != 0) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400207 mRollo.clearSelectedIcon();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700208 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700209 mRollo.selectIcon(x, (int)ev.getY(), mRollo.mReadback.posX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700210 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400211 mRollo.mState.save();
Jason Sams86c87ed2009-09-18 13:55:55 -0700212 mRollo.mInvokeMove.execute();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400213 mVelocity = VelocityTracker.obtain();
214 mVelocity.addMovement(ev);
215 mStartedScrolling = false;
216 break;
217 case MotionEvent.ACTION_MOVE:
218 case MotionEvent.ACTION_OUTSIDE:
219 int slop = Math.abs(x - mLastMotionX);
220 if (!mStartedScrolling && slop < mConfig.getScaledTouchSlop()) {
221 // don't update mLastMotionX so slop is right and when we do start scrolling
222 // below, we get the right delta.
223 } else {
Jason Sams86c87ed2009-09-18 13:55:55 -0700224
225 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
226 mRollo.mState.newTouchDown = 1;
227 mRollo.mInvokeMove.execute();
228
Joe Onoratofb0ca672009-09-14 17:55:46 -0400229 mStartedScrolling = true;
230 mRollo.clearSelectedIcon();
231 deltaX = x - mLastMotionX;
232 mVelocity.addMovement(ev);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400233 mRollo.mState.save();
234 mLastMotionX = x;
235 }
236 break;
237 case MotionEvent.ACTION_UP:
238 case MotionEvent.ACTION_CANCEL:
Jason Sams86c87ed2009-09-18 13:55:55 -0700239
240 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
241 mRollo.mState.newTouchDown = 0;
242
Joe Onoratofb0ca672009-09-14 17:55:46 -0400243 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
244 mConfig.getScaledMaximumFlingVelocity());
Jason Sams86c87ed2009-09-18 13:55:55 -0700245 mRollo.mState.flingVelocityX = mVelocity.getXVelocity() / Defines.SCREEN_WIDTH_PX;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400246 mRollo.clearSelectedIcon();
247 mRollo.mState.save();
Jason Sams86c87ed2009-09-18 13:55:55 -0700248 mRollo.mInvokeFling.execute();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400249 mLastMotionX = -10000;
250 mVelocity.recycle();
251 mVelocity = null;
252 break;
Joe Onorato93839052009-08-06 20:34:32 -0700253 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700254
255 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700256 }
257
Joe Onorato6665c0f2009-09-02 15:27:24 -0700258 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700259 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400260 return;
261 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700262 int index = mRollo.mState.selectedIconIndex;
263 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
264 ApplicationInfo app = mAllAppsList.get(index);
265 mLauncher.startActivitySafely(app.intent);
266 }
267 }
268
269 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700270 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400271 return true;
272 }
Joe Onorato5162ea92009-09-03 09:39:42 -0700273 int index = mRollo.mState.selectedIconIndex;
274 Log.d(TAG, "long click! velocity=" + mRollo.mState.flingVelocityX + " index=" + index);
275 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
276 ApplicationInfo app = mAllAppsList.get(index);
277
278 // We don't really have an accurate location to use. This will do.
279 int screenX = mMotionDownRawX - (Defines.ICON_WIDTH_PX / 2);
280 int screenY = mMotionDownRawY - Defines.ICON_HEIGHT_PX;
281
282 int left = (Defines.ICON_TEXTURE_WIDTH_PX - Defines.ICON_WIDTH_PX) / 2;
283 int top = (Defines.ICON_TEXTURE_HEIGHT_PX - Defines.ICON_HEIGHT_PX) / 2;
284 mDragController.startDrag(app.iconBitmap, screenX, screenY,
285 left, top, Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
286 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700287
Joe Onorato7bb17492009-09-24 17:51:01 -0700288 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700289 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700290 return true;
291 }
292
Joe Onorato5162ea92009-09-03 09:39:42 -0700293 public void setDragController(DragController dragger) {
294 mDragController = dragger;
295 }
296
297 public void onDropCompleted(View target, boolean success) {
298 }
299
Joe Onorato7bb17492009-09-24 17:51:01 -0700300 public void setZoomTarget(float amount) {
301 zoom(amount, true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700302 }
303
Joe Onorato7bb17492009-09-24 17:51:01 -0700304 public void setZoom(float amount) {
305 zoom(amount, false);
306 }
307
308 private void zoom(float amount, boolean animate) {
309 if (mRollo == null) {
310 return;
311 }
312
Joe Onoratofb0ca672009-09-14 17:55:46 -0400313 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400314 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700315 if (amount > 0.001f) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700316 mRollo.mState.zoomTarget = amount;
317 if (!animate) {
318 // set in readback, so we're correct even before the next frame
319 mRollo.mReadback.zoom = mRollo.mState.zoom = amount;
320 mRollo.mReadback.save();
321 }
Joe Onorato85a02a82009-09-08 12:34:22 -0700322 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700323 mRollo.mState.zoomTarget = 0;
324 if (!animate) {
325 mRollo.mReadback.zoom = mRollo.mState.zoom = 0;
326 mRollo.mReadback.save();
327 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700328 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400329 mRollo.mState.save();
Joe Onorato7bb17492009-09-24 17:51:01 -0700330 if (!animate) {
331 mRollo.mInvokeSetZoom.execute();
332 } else {
333 mRollo.mInvokeSetZoomTarget.execute();
334 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700335 mReadZoom.removeMessages(1);
336 mReadZoom.sendEmptyMessageDelayed(1, 1000);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400337 }
338
Joe Onorato8e099e22009-09-25 17:30:47 -0700339 Handler mReadZoom = new Handler() {
340 public void handleMessage(Message msg) {
341 mRollo.mReadback.read();
342 }
343 };
344
Joe Onoratofb0ca672009-09-14 17:55:46 -0400345 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700346 if (mRollo == null) {
347 return false;
348 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700349 //mRollo.mReadback.read();
Joe Onorato7bb17492009-09-24 17:51:01 -0700350 return mRollo.mReadback.zoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400351 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700352
Joe Onorato93839052009-08-06 20:34:32 -0700353 @Override
354 public boolean onTrackballEvent(MotionEvent ev)
355 {
356 float x = ev.getX();
357 float y = ev.getY();
358 //Float tx = new Float(x);
359 //Float ty = new Float(y);
360 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
361
362
363 return true;
364 }
365
Joe Onorato9c1289c2009-08-17 11:03:03 -0400366 public void setApps(ArrayList<ApplicationInfo> list) {
367 mAllAppsList = list;
368 if (mRollo != null) {
369 mRollo.setApps(list);
370 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700371 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700372 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400373 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700374 }
375
376 private void invokeIcon(int index) {
377 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700378 }
379
380 private static int countPages(int iconCount) {
381 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
382 int pages = iconCount / iconsPerPage;
383 if (pages*iconsPerPage != iconCount) {
384 pages++;
385 }
386 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400387 }
388
Joe Onorato93839052009-08-06 20:34:32 -0700389 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700390
Joe Onorato1feb3a82009-08-08 22:32:00 -0700391 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700392
Joe Onorato93839052009-08-06 20:34:32 -0700393 private int mWidth;
394 private int mHeight;
395
396 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700397 private Script mScript;
Jason Sams86c87ed2009-09-18 13:55:55 -0700398
399 private Script.Invokable mInvokeMove;
400 private Script.Invokable mInvokeFling;
Joe Onorato7bb17492009-09-24 17:51:01 -0700401 private Script.Invokable mInvokeSetZoomTarget;
402 private Script.Invokable mInvokeSetZoom;
Jason Sams86c87ed2009-09-18 13:55:55 -0700403
Joe Onorato93839052009-08-06 20:34:32 -0700404 private Sampler mSampler;
405 private Sampler mSamplerText;
406 private ProgramStore mPSBackground;
407 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700408 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700409 private ProgramFragment mPFImages;
Joe Onorato56848b02009-09-25 13:59:59 -0700410 private ProgramFragment mPFOrtho;
Joe Onorato93839052009-08-06 20:34:32 -0700411 private ProgramVertex mPV;
412 private ProgramVertex.MatrixAllocation mPVAlloc;
413 private ProgramVertex mPVOrtho;
414 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700415
Joe Onoratoc567acb2009-08-31 14:34:43 -0700416 private Allocation mScrollHandle;
417
Joe Onoratobf15cb42009-08-07 14:33:40 -0700418 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700419 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700420 private Allocation mAllocIconID;
421
Joe Onoratobf15cb42009-08-07 14:33:40 -0700422 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700423 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700424 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700425 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700426
Joe Onorato6665c0f2009-09-02 15:27:24 -0700427 private int[] mTouchYBorders;
428 private Allocation mAllocTouchYBorders;
429 private int[] mTouchXBorders;
430 private Allocation mAllocTouchXBorders;
431
432 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400433 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700434
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700435 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700436 State mState;
Joe Onorato7bb17492009-09-24 17:51:01 -0700437 Readback mReadback;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700438
Jason Sams78aebd82009-09-15 13:06:59 -0700439 class BaseAlloc {
440 Allocation mAlloc;
441 Type mType;
442
443 void save() {
444 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700445 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700446 }
447
Jason Sams78aebd82009-09-15 13:06:59 -0700448 class Params extends BaseAlloc {
449 Params() {
450 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
451 mAlloc = Allocation.createTyped(mRS, mType);
452 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700453 }
Jason Sams78aebd82009-09-15 13:06:59 -0700454 public int bubbleWidth;
455 public int bubbleHeight;
456 public int bubbleBitmapWidth;
457 public int bubbleBitmapHeight;
458 public int scrollHandleId;
459 public int scrollHandleTextureWidth;
460 public int scrollHandleTextureHeight;
461 }
462
463 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700464 public float newPositionX;
465 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700466 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700467 public int iconCount;
468 public int scrollX;
Jason Sams78aebd82009-09-15 13:06:59 -0700469 public int selectedIconIndex = -1;
470 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700471 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700472 public float zoom;
473
474 State() {
475 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
476 mAlloc = Allocation.createTyped(mRS, mType);
477 save();
478 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700479 }
480
Joe Onorato7bb17492009-09-24 17:51:01 -0700481 class Readback extends BaseAlloc {
482 public float posX;
483 public float velocity;
484 public float zoom;
485
486 Readback() {
487 mType = Type.createFromClass(mRS, Readback.class, 1, "ReadbackClass");
488 mAlloc = Allocation.createTyped(mRS, mType);
489 save();
490 }
491
492 void read() {
493 mAlloc.read(this);
494 }
495 }
496
Joe Onorato1feb3a82009-08-08 22:32:00 -0700497 public RolloRS() {
498 }
499
500 public void init(Resources res, int width, int height) {
501 mRes = res;
502 mWidth = width;
503 mHeight = height;
504 initGl();
505 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700506 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700507 initRs();
508 }
509
510 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700511 Sampler.Builder sb = new Sampler.Builder(mRS);
512 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
513 sb.setMag(Sampler.Value.LINEAR);
514 sb.setWrapS(Sampler.Value.CLAMP);
515 sb.setWrapT(Sampler.Value.CLAMP);
516 mSampler = sb.create();
517
518 sb.setMin(Sampler.Value.NEAREST);
519 sb.setMag(Sampler.Value.NEAREST);
520 mSamplerText = sb.create();
521
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700522 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
523 mPFDebug = dbg.create();
524 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700525
526 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
527 bf.setTexEnable(true, 0);
528 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
529 mPFImages = bf.create();
530 mPFImages.setName("PF");
531 mPFImages.bindSampler(mSampler, 0);
532
533 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato56848b02009-09-25 13:59:59 -0700534 //mPFOrtho = bf.create();
535 mPFOrtho = (new ProgramFragment.Builder(mRS, null, null)).create();
536 mPFOrtho.setName("PFOrtho");
537 mPFOrtho.bindSampler(mSamplerText, 0);
Joe Onorato93839052009-08-06 20:34:32 -0700538
539 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700540 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700541 bs.setDitherEnable(false);
542 bs.setDepthMask(true);
543 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
544 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
545 mPSBackground = bs.create();
546 mPSBackground.setName("PFS");
547
548 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
549 bs.setDepthMask(false);
550 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
551 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
552 mPSText = bs.create();
553 mPSText.setName("PFSText");
554
555 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
556 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
557
558 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
559 mPV = pvb.create();
560 mPV.setName("PV");
561 mPV.bindAllocation(mPVAlloc);
562
563 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
564 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
565
566 pvb.setTextureMatrixEnable(true);
567 mPVOrtho = pvb.create();
568 mPVOrtho.setName("PVOrtho");
569 mPVOrtho.bindAllocation(mPVOrthoAlloc);
570
571 mRS.contextBindProgramVertex(mPV);
572
Joe Onorato6665c0f2009-09-02 15:27:24 -0700573 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
574 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32,
575 mTouchXBorders.length);
576 mAllocTouchXBorders.data(mTouchXBorders);
577
578 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
579 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32,
580 mTouchYBorders.length);
581 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onorato93839052009-08-06 20:34:32 -0700582
583 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700584 }
Jason Sams78aebd82009-09-15 13:06:59 -0700585
Joe Onorato1feb3a82009-08-08 22:32:00 -0700586 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700587 mParams = new Params();
588 mState = new State();
Joe Onorato7bb17492009-09-24 17:51:01 -0700589 mReadback = new Readback();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700590
Joe Onoratobf15cb42009-08-07 14:33:40 -0700591 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700592
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700593 mParams.bubbleWidth = bubble.getBubbleWidth();
594 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
595 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
596 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700597
Joe Onoratoc567acb2009-08-31 14:34:43 -0700598 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700599 R.drawable.all_apps_button_pow2, Element.RGBA_8888, false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700600 mScrollHandle.uploadToTexture(0);
601 mParams.scrollHandleId = mScrollHandle.getID();
602 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
603 mParams.scrollHandleTextureWidth = 128;
604 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700605
Joe Onoratoc567acb2009-08-31 14:34:43 -0700606
Joe Onorato1feb3a82009-08-08 22:32:00 -0700607 mParams.save();
608 mState.save();
Joe Onorato7bb17492009-09-24 17:51:01 -0700609 mReadback.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400610
Joe Onorato1291a8c2009-09-15 15:07:25 -0400611 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
612 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
613 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700614
615 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700617 }
618
Joe Onorato1feb3a82009-08-08 22:32:00 -0700619 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700620 ScriptC.Builder sb = new ScriptC.Builder(mRS);
621 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700622 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700623 sb.addDefines(Defines.class);
Jason Sams78aebd82009-09-15 13:06:59 -0700624 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
625 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700626 sb.setType(mReadback.mType, "readback", Defines.ALLOC_READBACK);
Jason Sams86c87ed2009-09-18 13:55:55 -0700627 mInvokeMove = sb.addInvokable("move");
628 mInvokeFling = sb.addInvokable("fling");
Joe Onorato7bb17492009-09-24 17:51:01 -0700629 mInvokeSetZoomTarget = sb.addInvokable("setZoomTarget");
630 mInvokeSetZoom = sb.addInvokable("setZoom");
Joe Onorato93839052009-08-06 20:34:32 -0700631 mScript = sb.create();
632 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
633
Jason Sams78aebd82009-09-15 13:06:59 -0700634 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
635 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700636 mScript.bindAllocation(mReadback.mAlloc, Defines.ALLOC_READBACK);
Joe Onoratod769a632009-08-11 17:09:02 -0700637 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700638 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700639 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
640 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700641
642 mRS.contextBindRootScript(mScript);
643 }
Joe Onorato93839052009-08-06 20:34:32 -0700644
Joe Onorato9c1289c2009-08-17 11:03:03 -0400645 private void setApps(ArrayList<ApplicationInfo> list) {
646 final int count = list != null ? list.size() : 0;
647 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700648 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
650
651 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700652 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
654
655 Element ie8888 = Element.RGBA_8888;
656
657 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
658
659 for (int i=0; i<count; i++) {
660 final ApplicationInfo item = list.get(i);
661
662 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700663 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400664 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700665 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666
667 mIcons[i].uploadToTexture(0);
668 mLabels[i].uploadToTexture(0);
669
Joe Onorato6665c0f2009-09-02 15:27:24 -0700670 mIconIds[i] = mIcons[i].getID();
671 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400672 }
673
Jason Samsbea62772009-09-21 19:34:13 -0700674 if(count > 0) {
675 mAllocIconID.data(mIconIds);
676 mAllocLabelID.data(mLabelIds);
677 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400678
679 mState.iconCount = count;
680
Joe Onorato9c1289c2009-08-17 11:03:03 -0400681 if (mScript != null) { // wtf
682 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
683 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
684 }
685
686 mState.save();
687 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700688
689 void initTouchState() {
690 int width = getWidth();
691 int height = getHeight();
692
693 int iconsSize;
694 if (width < height) {
695 iconsSize = width;
696 } else {
697 iconsSize = height;
698 }
699 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
700 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
701
Joe Onorato56848b02009-09-25 13:59:59 -0700702 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
703 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
704 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700705 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -0700706 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
707 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700708
709 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700710
Joe Onorato6665c0f2009-09-02 15:27:24 -0700711 int centerX = (width / 2);
712 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -0700713 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700714 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -0700715 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700716 mTouchXBorders[4] = centerX + (2 * cellWidth);
717
718 mAllocTouchXBorders.data(mTouchXBorders);
719 }
720
Jason Sams86c87ed2009-09-18 13:55:55 -0700721 int chooseTappedIcon(int x, int y, float page) {
722 int currentPage = (int)page;
723
Joe Onorato6665c0f2009-09-02 15:27:24 -0700724 int col = -1;
725 int row = -1;
726
727 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
728 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
729 col = i;
730 break;
731 }
732 }
733 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
734 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
735 row = i;
736 break;
737 }
738 }
739
740 if (row < 0 || col < 0) {
741 return -1;
742 }
743
Jason Sams78aebd82009-09-15 13:06:59 -0700744 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700745 + (row * Defines.ROWS_PER_PAGE) + col;
746 }
747
748 /**
749 * You need to call save() on mState on your own after calling this.
750 */
Jason Sams86c87ed2009-09-18 13:55:55 -0700751 void selectIcon(int x, int y, float pos) {
752 int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400753 selectIcon(index);
754 }
755
756 void selectIcon(int index) {
757 Log.d(TAG, "selectIcon index=" + index);
758 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700759 if (index < 0 || index >= iconCount) {
760 mState.selectedIconIndex = -1;
761 return;
762 } else {
763 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400764
765 Bitmap selectionBitmap = mSelectionBitmap;
766
767 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
768 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
769 mAllAppsList.get(index).iconBitmap);
770
771 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
772 Element.RGBA_8888, false);
773 mSelectedIcon.uploadToTexture(0);
774 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700775 }
776 }
777
778 /**
779 * You need to call save() on mState on your own after calling this.
780 */
781 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400782 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700783 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400784 }
Joe Onorato93839052009-08-06 20:34:32 -0700785}
786
787