blob: 26d8166232670f88b5b43f771caaf306cc3018da [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 Onoratofb0ca672009-09-14 17:55:46 -0400335 }
336
337 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700338 if (mRollo == null) {
339 return false;
340 }
341 mRollo.mReadback.read();
342 return mRollo.mReadback.zoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400343 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700344
Joe Onorato93839052009-08-06 20:34:32 -0700345 @Override
346 public boolean onTrackballEvent(MotionEvent ev)
347 {
348 float x = ev.getX();
349 float y = ev.getY();
350 //Float tx = new Float(x);
351 //Float ty = new Float(y);
352 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
353
354
355 return true;
356 }
357
Joe Onorato9c1289c2009-08-17 11:03:03 -0400358 public void setApps(ArrayList<ApplicationInfo> list) {
359 mAllAppsList = list;
360 if (mRollo != null) {
361 mRollo.setApps(list);
362 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700363 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700364 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400365 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700366 }
367
368 private void invokeIcon(int index) {
369 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700370 }
371
372 private static int countPages(int iconCount) {
373 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
374 int pages = iconCount / iconsPerPage;
375 if (pages*iconsPerPage != iconCount) {
376 pages++;
377 }
378 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 }
380
Joe Onorato93839052009-08-06 20:34:32 -0700381 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700382
Joe Onorato1feb3a82009-08-08 22:32:00 -0700383 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700384
Joe Onorato93839052009-08-06 20:34:32 -0700385 private int mWidth;
386 private int mHeight;
387
388 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700389 private Script mScript;
Jason Sams86c87ed2009-09-18 13:55:55 -0700390
391 private Script.Invokable mInvokeMove;
392 private Script.Invokable mInvokeFling;
Joe Onorato7bb17492009-09-24 17:51:01 -0700393 private Script.Invokable mInvokeSetZoomTarget;
394 private Script.Invokable mInvokeSetZoom;
Jason Sams86c87ed2009-09-18 13:55:55 -0700395
Joe Onorato93839052009-08-06 20:34:32 -0700396 private Sampler mSampler;
397 private Sampler mSamplerText;
398 private ProgramStore mPSBackground;
399 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700400 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700401 private ProgramFragment mPFImages;
Joe Onorato56848b02009-09-25 13:59:59 -0700402 private ProgramFragment mPFOrtho;
Joe Onorato93839052009-08-06 20:34:32 -0700403 private ProgramVertex mPV;
404 private ProgramVertex.MatrixAllocation mPVAlloc;
405 private ProgramVertex mPVOrtho;
406 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700407
Joe Onoratoc567acb2009-08-31 14:34:43 -0700408 private Allocation mScrollHandle;
409
Joe Onoratobf15cb42009-08-07 14:33:40 -0700410 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700411 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700412 private Allocation mAllocIconID;
413
Joe Onoratobf15cb42009-08-07 14:33:40 -0700414 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700415 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700416 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700417 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700418
Joe Onorato6665c0f2009-09-02 15:27:24 -0700419 private int[] mTouchYBorders;
420 private Allocation mAllocTouchYBorders;
421 private int[] mTouchXBorders;
422 private Allocation mAllocTouchXBorders;
423
424 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400425 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700426
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700427 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700428 State mState;
Joe Onorato7bb17492009-09-24 17:51:01 -0700429 Readback mReadback;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700430
Jason Sams78aebd82009-09-15 13:06:59 -0700431 class BaseAlloc {
432 Allocation mAlloc;
433 Type mType;
434
435 void save() {
436 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700437 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700438 }
439
Jason Sams78aebd82009-09-15 13:06:59 -0700440 class Params extends BaseAlloc {
441 Params() {
442 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
443 mAlloc = Allocation.createTyped(mRS, mType);
444 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700445 }
Jason Sams78aebd82009-09-15 13:06:59 -0700446 public int bubbleWidth;
447 public int bubbleHeight;
448 public int bubbleBitmapWidth;
449 public int bubbleBitmapHeight;
450 public int scrollHandleId;
451 public int scrollHandleTextureWidth;
452 public int scrollHandleTextureHeight;
453 }
454
455 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700456 public float newPositionX;
457 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700458 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700459 public int iconCount;
460 public int scrollX;
Jason Sams78aebd82009-09-15 13:06:59 -0700461 public int selectedIconIndex = -1;
462 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700463 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700464 public float zoom;
465
466 State() {
467 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
468 mAlloc = Allocation.createTyped(mRS, mType);
469 save();
470 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700471 }
472
Joe Onorato7bb17492009-09-24 17:51:01 -0700473 class Readback extends BaseAlloc {
474 public float posX;
475 public float velocity;
476 public float zoom;
477
478 Readback() {
479 mType = Type.createFromClass(mRS, Readback.class, 1, "ReadbackClass");
480 mAlloc = Allocation.createTyped(mRS, mType);
481 save();
482 }
483
484 void read() {
485 mAlloc.read(this);
486 }
487 }
488
Joe Onorato1feb3a82009-08-08 22:32:00 -0700489 public RolloRS() {
490 }
491
492 public void init(Resources res, int width, int height) {
493 mRes = res;
494 mWidth = width;
495 mHeight = height;
496 initGl();
497 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700498 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700499 initRs();
500 }
501
502 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700503 Sampler.Builder sb = new Sampler.Builder(mRS);
504 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
505 sb.setMag(Sampler.Value.LINEAR);
506 sb.setWrapS(Sampler.Value.CLAMP);
507 sb.setWrapT(Sampler.Value.CLAMP);
508 mSampler = sb.create();
509
510 sb.setMin(Sampler.Value.NEAREST);
511 sb.setMag(Sampler.Value.NEAREST);
512 mSamplerText = sb.create();
513
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700514 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
515 mPFDebug = dbg.create();
516 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700517
518 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
519 bf.setTexEnable(true, 0);
520 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
521 mPFImages = bf.create();
522 mPFImages.setName("PF");
523 mPFImages.bindSampler(mSampler, 0);
524
525 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato56848b02009-09-25 13:59:59 -0700526 //mPFOrtho = bf.create();
527 mPFOrtho = (new ProgramFragment.Builder(mRS, null, null)).create();
528 mPFOrtho.setName("PFOrtho");
529 mPFOrtho.bindSampler(mSamplerText, 0);
Joe Onorato93839052009-08-06 20:34:32 -0700530
531 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700532 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700533 bs.setDitherEnable(false);
534 bs.setDepthMask(true);
535 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
536 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
537 mPSBackground = bs.create();
538 mPSBackground.setName("PFS");
539
540 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
541 bs.setDepthMask(false);
542 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
543 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
544 mPSText = bs.create();
545 mPSText.setName("PFSText");
546
547 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
548 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
549
550 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
551 mPV = pvb.create();
552 mPV.setName("PV");
553 mPV.bindAllocation(mPVAlloc);
554
555 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
556 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
557
558 pvb.setTextureMatrixEnable(true);
559 mPVOrtho = pvb.create();
560 mPVOrtho.setName("PVOrtho");
561 mPVOrtho.bindAllocation(mPVOrthoAlloc);
562
563 mRS.contextBindProgramVertex(mPV);
564
Joe Onorato6665c0f2009-09-02 15:27:24 -0700565 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
566 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32,
567 mTouchXBorders.length);
568 mAllocTouchXBorders.data(mTouchXBorders);
569
570 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
571 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32,
572 mTouchYBorders.length);
573 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onorato93839052009-08-06 20:34:32 -0700574
575 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700576 }
Jason Sams78aebd82009-09-15 13:06:59 -0700577
Joe Onorato1feb3a82009-08-08 22:32:00 -0700578 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700579 mParams = new Params();
580 mState = new State();
Joe Onorato7bb17492009-09-24 17:51:01 -0700581 mReadback = new Readback();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700582
Joe Onoratobf15cb42009-08-07 14:33:40 -0700583 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700584
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700585 mParams.bubbleWidth = bubble.getBubbleWidth();
586 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
587 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
588 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700589
Joe Onoratoc567acb2009-08-31 14:34:43 -0700590 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700591 R.drawable.all_apps_button_pow2, Element.RGBA_8888, false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700592 mScrollHandle.uploadToTexture(0);
593 mParams.scrollHandleId = mScrollHandle.getID();
594 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
595 mParams.scrollHandleTextureWidth = 128;
596 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700597
Joe Onoratoc567acb2009-08-31 14:34:43 -0700598
Joe Onorato1feb3a82009-08-08 22:32:00 -0700599 mParams.save();
600 mState.save();
Joe Onorato7bb17492009-09-24 17:51:01 -0700601 mReadback.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400602
Joe Onorato1291a8c2009-09-15 15:07:25 -0400603 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
604 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
605 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700606
607 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400608 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700609 }
610
Joe Onorato1feb3a82009-08-08 22:32:00 -0700611 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700612 ScriptC.Builder sb = new ScriptC.Builder(mRS);
613 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700614 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700615 sb.addDefines(Defines.class);
Jason Sams78aebd82009-09-15 13:06:59 -0700616 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
617 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700618 sb.setType(mReadback.mType, "readback", Defines.ALLOC_READBACK);
Jason Sams86c87ed2009-09-18 13:55:55 -0700619 mInvokeMove = sb.addInvokable("move");
620 mInvokeFling = sb.addInvokable("fling");
Joe Onorato7bb17492009-09-24 17:51:01 -0700621 mInvokeSetZoomTarget = sb.addInvokable("setZoomTarget");
622 mInvokeSetZoom = sb.addInvokable("setZoom");
Joe Onorato93839052009-08-06 20:34:32 -0700623 mScript = sb.create();
624 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
625
Jason Sams78aebd82009-09-15 13:06:59 -0700626 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
627 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700628 mScript.bindAllocation(mReadback.mAlloc, Defines.ALLOC_READBACK);
Joe Onoratod769a632009-08-11 17:09:02 -0700629 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700630 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700631 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
632 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700633
634 mRS.contextBindRootScript(mScript);
635 }
Joe Onorato93839052009-08-06 20:34:32 -0700636
Joe Onorato9c1289c2009-08-17 11:03:03 -0400637 private void setApps(ArrayList<ApplicationInfo> list) {
638 final int count = list != null ? list.size() : 0;
639 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700640 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400641 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
642
643 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700644 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400645 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
646
647 Element ie8888 = Element.RGBA_8888;
648
649 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
650
651 for (int i=0; i<count; i++) {
652 final ApplicationInfo item = list.get(i);
653
654 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700655 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400656 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700657 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658
659 mIcons[i].uploadToTexture(0);
660 mLabels[i].uploadToTexture(0);
661
Joe Onorato6665c0f2009-09-02 15:27:24 -0700662 mIconIds[i] = mIcons[i].getID();
663 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400664 }
665
Jason Samsbea62772009-09-21 19:34:13 -0700666 if(count > 0) {
667 mAllocIconID.data(mIconIds);
668 mAllocLabelID.data(mLabelIds);
669 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400670
671 mState.iconCount = count;
672
Joe Onorato9c1289c2009-08-17 11:03:03 -0400673 if (mScript != null) { // wtf
674 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
675 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
676 }
677
678 mState.save();
679 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700680
681 void initTouchState() {
682 int width = getWidth();
683 int height = getHeight();
684
685 int iconsSize;
686 if (width < height) {
687 iconsSize = width;
688 } else {
689 iconsSize = height;
690 }
691 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
692 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
693
Joe Onorato56848b02009-09-25 13:59:59 -0700694 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
695 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
696 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700697 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -0700698 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
699 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700700
701 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700702
Joe Onorato6665c0f2009-09-02 15:27:24 -0700703 int centerX = (width / 2);
704 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -0700705 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700706 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -0700707 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700708 mTouchXBorders[4] = centerX + (2 * cellWidth);
709
710 mAllocTouchXBorders.data(mTouchXBorders);
711 }
712
Jason Sams86c87ed2009-09-18 13:55:55 -0700713 int chooseTappedIcon(int x, int y, float page) {
714 int currentPage = (int)page;
715
Joe Onorato6665c0f2009-09-02 15:27:24 -0700716 int col = -1;
717 int row = -1;
718
719 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
720 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
721 col = i;
722 break;
723 }
724 }
725 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
726 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
727 row = i;
728 break;
729 }
730 }
731
732 if (row < 0 || col < 0) {
733 return -1;
734 }
735
Jason Sams78aebd82009-09-15 13:06:59 -0700736 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700737 + (row * Defines.ROWS_PER_PAGE) + col;
738 }
739
740 /**
741 * You need to call save() on mState on your own after calling this.
742 */
Jason Sams86c87ed2009-09-18 13:55:55 -0700743 void selectIcon(int x, int y, float pos) {
744 int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400745 selectIcon(index);
746 }
747
748 void selectIcon(int index) {
749 Log.d(TAG, "selectIcon index=" + index);
750 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700751 if (index < 0 || index >= iconCount) {
752 mState.selectedIconIndex = -1;
753 return;
754 } else {
755 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400756
757 Bitmap selectionBitmap = mSelectionBitmap;
758
759 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
760 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
761 mAllAppsList.get(index).iconBitmap);
762
763 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
764 Element.RGBA_8888, false);
765 mSelectedIcon.uploadToTexture(0);
766 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700767 }
768 }
769
770 /**
771 * You need to call save() on mState on your own after calling this.
772 */
773 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400774 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700775 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400776 }
Joe Onorato93839052009-08-06 20:34:32 -0700777}
778
779