blob: ae446774977edc60816b66bff7e720f5e06e19bf [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
Jason Sams05de32a2009-09-27 14:01:40 -0700158 mRS = createRenderScript(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) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700341 if(mRollo != null && mRollo.mReadback != null) {
342 // FIXME: These checks may indicate other problems.
343 mRollo.mReadback.read();
344 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700345 }
346 };
347
Joe Onoratofb0ca672009-09-14 17:55:46 -0400348 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700349 if (mRollo == null) {
350 return false;
351 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700352 //mRollo.mReadback.read();
Joe Onorato7bb17492009-09-24 17:51:01 -0700353 return mRollo.mReadback.zoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400354 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700355
Joe Onorato93839052009-08-06 20:34:32 -0700356 @Override
357 public boolean onTrackballEvent(MotionEvent ev)
358 {
359 float x = ev.getX();
360 float y = ev.getY();
361 //Float tx = new Float(x);
362 //Float ty = new Float(y);
363 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
364
365
366 return true;
367 }
368
Joe Onorato9c1289c2009-08-17 11:03:03 -0400369 public void setApps(ArrayList<ApplicationInfo> list) {
370 mAllAppsList = list;
371 if (mRollo != null) {
372 mRollo.setApps(list);
373 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700374 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700375 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400376 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700377 }
378
379 private void invokeIcon(int index) {
380 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700381 }
382
383 private static int countPages(int iconCount) {
384 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
385 int pages = iconCount / iconsPerPage;
386 if (pages*iconsPerPage != iconCount) {
387 pages++;
388 }
389 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390 }
391
Joe Onorato93839052009-08-06 20:34:32 -0700392 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700393
Joe Onorato1feb3a82009-08-08 22:32:00 -0700394 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700395
Joe Onorato93839052009-08-06 20:34:32 -0700396 private int mWidth;
397 private int mHeight;
398
399 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700400 private Script mScript;
Jason Sams86c87ed2009-09-18 13:55:55 -0700401
402 private Script.Invokable mInvokeMove;
403 private Script.Invokable mInvokeFling;
Joe Onorato7bb17492009-09-24 17:51:01 -0700404 private Script.Invokable mInvokeSetZoomTarget;
405 private Script.Invokable mInvokeSetZoom;
Jason Sams86c87ed2009-09-18 13:55:55 -0700406
Joe Onorato93839052009-08-06 20:34:32 -0700407 private Sampler mSampler;
408 private Sampler mSamplerText;
409 private ProgramStore mPSBackground;
410 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700411 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700412 private ProgramFragment mPFImages;
Joe Onorato56848b02009-09-25 13:59:59 -0700413 private ProgramFragment mPFOrtho;
Joe Onorato93839052009-08-06 20:34:32 -0700414 private ProgramVertex mPV;
415 private ProgramVertex.MatrixAllocation mPVAlloc;
416 private ProgramVertex mPVOrtho;
417 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700418
Joe Onoratoc567acb2009-08-31 14:34:43 -0700419 private Allocation mScrollHandle;
420
Joe Onoratobf15cb42009-08-07 14:33:40 -0700421 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700422 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700423 private Allocation mAllocIconID;
424
Joe Onoratobf15cb42009-08-07 14:33:40 -0700425 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700426 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700427 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700428 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700429
Joe Onorato6665c0f2009-09-02 15:27:24 -0700430 private int[] mTouchYBorders;
431 private Allocation mAllocTouchYBorders;
432 private int[] mTouchXBorders;
433 private Allocation mAllocTouchXBorders;
434
435 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400436 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700437
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700438 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700439 State mState;
Joe Onorato7bb17492009-09-24 17:51:01 -0700440 Readback mReadback;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700441
Jason Sams78aebd82009-09-15 13:06:59 -0700442 class BaseAlloc {
443 Allocation mAlloc;
444 Type mType;
445
446 void save() {
447 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700448 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700449 }
450
Jason Sams78aebd82009-09-15 13:06:59 -0700451 class Params extends BaseAlloc {
452 Params() {
453 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
454 mAlloc = Allocation.createTyped(mRS, mType);
455 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700456 }
Jason Sams78aebd82009-09-15 13:06:59 -0700457 public int bubbleWidth;
458 public int bubbleHeight;
459 public int bubbleBitmapWidth;
460 public int bubbleBitmapHeight;
461 public int scrollHandleId;
462 public int scrollHandleTextureWidth;
463 public int scrollHandleTextureHeight;
464 }
465
466 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700467 public float newPositionX;
468 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700469 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700470 public int iconCount;
471 public int scrollX;
Jason Sams78aebd82009-09-15 13:06:59 -0700472 public int selectedIconIndex = -1;
473 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700474 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700475 public float zoom;
476
477 State() {
478 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
479 mAlloc = Allocation.createTyped(mRS, mType);
480 save();
481 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700482 }
483
Joe Onorato7bb17492009-09-24 17:51:01 -0700484 class Readback extends BaseAlloc {
485 public float posX;
486 public float velocity;
487 public float zoom;
488
489 Readback() {
490 mType = Type.createFromClass(mRS, Readback.class, 1, "ReadbackClass");
491 mAlloc = Allocation.createTyped(mRS, mType);
492 save();
493 }
494
495 void read() {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700496 if(mAlloc != null) {
497 mAlloc.read(this);
498 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700499 }
500 }
501
Joe Onorato1feb3a82009-08-08 22:32:00 -0700502 public RolloRS() {
503 }
504
505 public void init(Resources res, int width, int height) {
506 mRes = res;
507 mWidth = width;
508 mHeight = height;
509 initGl();
510 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700511 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700512 initRs();
513 }
514
515 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700516 Sampler.Builder sb = new Sampler.Builder(mRS);
517 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
518 sb.setMag(Sampler.Value.LINEAR);
519 sb.setWrapS(Sampler.Value.CLAMP);
520 sb.setWrapT(Sampler.Value.CLAMP);
521 mSampler = sb.create();
522
523 sb.setMin(Sampler.Value.NEAREST);
524 sb.setMag(Sampler.Value.NEAREST);
525 mSamplerText = sb.create();
526
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700527 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
528 mPFDebug = dbg.create();
529 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700530
531 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
532 bf.setTexEnable(true, 0);
533 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
534 mPFImages = bf.create();
535 mPFImages.setName("PF");
536 mPFImages.bindSampler(mSampler, 0);
537
538 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato56848b02009-09-25 13:59:59 -0700539 //mPFOrtho = bf.create();
540 mPFOrtho = (new ProgramFragment.Builder(mRS, null, null)).create();
541 mPFOrtho.setName("PFOrtho");
542 mPFOrtho.bindSampler(mSamplerText, 0);
Joe Onorato93839052009-08-06 20:34:32 -0700543
544 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700545 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700546 bs.setDitherEnable(false);
547 bs.setDepthMask(true);
548 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
549 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
550 mPSBackground = bs.create();
551 mPSBackground.setName("PFS");
552
553 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
554 bs.setDepthMask(false);
555 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
556 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
557 mPSText = bs.create();
558 mPSText.setName("PFSText");
559
560 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
561 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
562
563 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
564 mPV = pvb.create();
565 mPV.setName("PV");
566 mPV.bindAllocation(mPVAlloc);
567
568 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
569 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
570
571 pvb.setTextureMatrixEnable(true);
572 mPVOrtho = pvb.create();
573 mPVOrtho.setName("PVOrtho");
574 mPVOrtho.bindAllocation(mPVOrthoAlloc);
575
576 mRS.contextBindProgramVertex(mPV);
577
Joe Onorato6665c0f2009-09-02 15:27:24 -0700578 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700579 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700580 mTouchXBorders.length);
581 mAllocTouchXBorders.data(mTouchXBorders);
582
583 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700584 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700585 mTouchYBorders.length);
586 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700587 }
Jason Sams78aebd82009-09-15 13:06:59 -0700588
Joe Onorato1feb3a82009-08-08 22:32:00 -0700589 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700590 mParams = new Params();
591 mState = new State();
Joe Onorato7bb17492009-09-24 17:51:01 -0700592 mReadback = new Readback();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700593
Joe Onoratobf15cb42009-08-07 14:33:40 -0700594 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700595
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700596 mParams.bubbleWidth = bubble.getBubbleWidth();
597 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
598 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
599 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700600
Joe Onoratoc567acb2009-08-31 14:34:43 -0700601 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700602 R.drawable.all_apps_button_pow2, Element.RGBA_8888(mRS), false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700603 mScrollHandle.uploadToTexture(0);
604 mParams.scrollHandleId = mScrollHandle.getID();
605 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
606 mParams.scrollHandleTextureWidth = 128;
607 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700608
Joe Onoratoc567acb2009-08-31 14:34:43 -0700609
Joe Onorato1feb3a82009-08-08 22:32:00 -0700610 mParams.save();
611 mState.save();
Joe Onorato7bb17492009-09-24 17:51:01 -0700612 mReadback.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400613
Joe Onorato1291a8c2009-09-15 15:07:25 -0400614 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
615 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
616 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700617
618 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400619 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700620 }
621
Joe Onorato1feb3a82009-08-08 22:32:00 -0700622 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700623 ScriptC.Builder sb = new ScriptC.Builder(mRS);
624 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700625 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700626 sb.addDefines(Defines.class);
Jason Sams78aebd82009-09-15 13:06:59 -0700627 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
628 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700629 sb.setType(mReadback.mType, "readback", Defines.ALLOC_READBACK);
Jason Sams86c87ed2009-09-18 13:55:55 -0700630 mInvokeMove = sb.addInvokable("move");
631 mInvokeFling = sb.addInvokable("fling");
Joe Onorato7bb17492009-09-24 17:51:01 -0700632 mInvokeSetZoomTarget = sb.addInvokable("setZoomTarget");
633 mInvokeSetZoom = sb.addInvokable("setZoom");
Joe Onorato93839052009-08-06 20:34:32 -0700634 mScript = sb.create();
635 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
636
Jason Sams78aebd82009-09-15 13:06:59 -0700637 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
638 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700639 mScript.bindAllocation(mReadback.mAlloc, Defines.ALLOC_READBACK);
Joe Onoratod769a632009-08-11 17:09:02 -0700640 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700641 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700642 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
643 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700644
645 mRS.contextBindRootScript(mScript);
646 }
Joe Onorato93839052009-08-06 20:34:32 -0700647
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648 private void setApps(ArrayList<ApplicationInfo> list) {
649 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700650 int allocCount = count;
651 if(allocCount < 1) {
652 allocCount = 1;
653 }
654
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700656 mIconIds = new int[allocCount];
657 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658
659 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700660 mLabelIds = new int[allocCount];
661 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400662
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700663 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400664
665 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
666
667 for (int i=0; i<count; i++) {
668 final ApplicationInfo item = list.get(i);
669
670 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700671 Element.RGBA_8888(mRS), false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400672 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700673 Element.RGBA_8888(mRS), false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400674
675 mIcons[i].uploadToTexture(0);
676 mLabels[i].uploadToTexture(0);
677
Joe Onorato6665c0f2009-09-02 15:27:24 -0700678 mIconIds[i] = mIcons[i].getID();
679 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400680 }
681
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700682 mAllocIconID.data(mIconIds);
683 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684
685 mState.iconCount = count;
686
Joe Onorato9c1289c2009-08-17 11:03:03 -0400687 if (mScript != null) { // wtf
688 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
689 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
690 }
691
692 mState.save();
693 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700694
695 void initTouchState() {
696 int width = getWidth();
697 int height = getHeight();
698
699 int iconsSize;
700 if (width < height) {
701 iconsSize = width;
702 } else {
703 iconsSize = height;
704 }
705 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
706 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
707
Joe Onorato56848b02009-09-25 13:59:59 -0700708 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
709 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
710 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700711 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -0700712 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
713 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700714
715 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700716
Joe Onorato6665c0f2009-09-02 15:27:24 -0700717 int centerX = (width / 2);
718 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -0700719 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700720 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -0700721 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700722 mTouchXBorders[4] = centerX + (2 * cellWidth);
723
724 mAllocTouchXBorders.data(mTouchXBorders);
725 }
726
Jason Sams86c87ed2009-09-18 13:55:55 -0700727 int chooseTappedIcon(int x, int y, float page) {
728 int currentPage = (int)page;
729
Joe Onorato6665c0f2009-09-02 15:27:24 -0700730 int col = -1;
731 int row = -1;
732
733 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
734 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
735 col = i;
736 break;
737 }
738 }
739 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
740 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
741 row = i;
742 break;
743 }
744 }
745
746 if (row < 0 || col < 0) {
747 return -1;
748 }
749
Jason Sams78aebd82009-09-15 13:06:59 -0700750 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700751 + (row * Defines.ROWS_PER_PAGE) + col;
752 }
753
754 /**
755 * You need to call save() on mState on your own after calling this.
756 */
Jason Sams86c87ed2009-09-18 13:55:55 -0700757 void selectIcon(int x, int y, float pos) {
758 int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400759 selectIcon(index);
760 }
761
762 void selectIcon(int index) {
763 Log.d(TAG, "selectIcon index=" + index);
764 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700765 if (index < 0 || index >= iconCount) {
766 mState.selectedIconIndex = -1;
767 return;
768 } else {
769 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400770
771 Bitmap selectionBitmap = mSelectionBitmap;
772
773 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
774 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
775 mAllAppsList.get(index).iconBitmap);
776
777 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700778 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400779 mSelectedIcon.uploadToTexture(0);
780 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700781 }
782 }
783
784 /**
785 * You need to call save() on mState on your own after calling this.
786 */
787 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400788 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700789 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400790 }
Joe Onorato93839052009-08-06 20:34:32 -0700791}
792
793