blob: 17d9a78f70d79b894e1cb72d4c09e23b34081ae6 [file] [log] [blame]
Joe Onorato93839052009-08-06 20:34:32 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
19import java.io.Writer;
20import java.util.ArrayList;
21import java.util.concurrent.Semaphore;
22import java.lang.Float;
23
24import android.renderscript.RSSurfaceView;
25import android.renderscript.RenderScript;
26
27import android.renderscript.RenderScript;
28import android.renderscript.ProgramVertex;
29import android.renderscript.Element;
30import android.renderscript.Allocation;
Jason Sams78aebd82009-09-15 13:06:59 -070031import android.renderscript.Type;
Joe Onorato93839052009-08-06 20:34:32 -070032import android.renderscript.Script;
33import android.renderscript.ScriptC;
34import android.renderscript.ProgramFragment;
35import android.renderscript.ProgramStore;
36import android.renderscript.Sampler;
37
38import android.content.Context;
39import android.content.res.Resources;
Joe Onorato7c312c12009-08-13 21:36:53 -070040import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070041import android.graphics.Bitmap;
42import android.graphics.BitmapFactory;
43import android.graphics.Canvas;
44import android.graphics.Paint;
Joe Onorato93839052009-08-06 20:34:32 -070045import android.graphics.drawable.BitmapDrawable;
46import android.graphics.drawable.Drawable;
47import android.os.Handler;
48import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070049import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070050import android.util.AttributeSet;
51import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070052import android.view.KeyEvent;
53import android.view.MotionEvent;
Joe Onorato93839052009-08-06 20:34:32 -070054import android.view.Surface;
55import android.view.SurfaceHolder;
56import android.view.SurfaceView;
Joe Onorato6665c0f2009-09-02 15:27:24 -070057import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070058import android.view.VelocityTracker;
59import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070060import android.graphics.PixelFormat;
61
62
Joe Onorato6665c0f2009-09-02 15:27:24 -070063public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070064 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040065 private static final String TAG = "Launcher.AllAppsView";
66
Joe Onoratofb0ca672009-09-14 17:55:46 -040067 /** Bit for mLocks for when there are icons being loaded. */
68 private static final int LOCK_ICONS_PENDING = 1;
69
Joe Onorato6665c0f2009-09-02 15:27:24 -070070 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070071 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040072
73 /** When this is 0, modifications are allowed, when it's not, they're not.
74 * TODO: What about scrolling? */
75 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070076
Joe Onorato1feb3a82009-08-08 22:32:00 -070077 private RenderScript mRS;
78 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040079 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070080
Joe Onoratod769a632009-08-11 17:09:02 -070081 private ViewConfiguration mConfig;
Joe Onoratoc567acb2009-08-31 14:34:43 -070082 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070083 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070084 private VelocityTracker mVelocity;
Joe Onorato1feb3a82009-08-08 22:32:00 -070085 private int mLastMotionX;
Joe Onorato5162ea92009-09-03 09:39:42 -070086 private int mMotionDownRawX;
87 private int mMotionDownRawY;
Joe Onoratoc567acb2009-08-31 14:34:43 -070088 private int mScrollHandleTop;
Jason Sams86c87ed2009-09-18 13:55:55 -070089 private long mTouchTime;
Joe Onorato360d0352009-09-28 14:37:53 -040090 private boolean mZoomSwipeInProgress;
Joe Onorato1feb3a82009-08-08 22:32:00 -070091
Joe Onorato6665c0f2009-09-02 15:27:24 -070092 static class Defines {
93 private static float farSize(float sizeAt0) {
94 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
95 }
96
Joe Onoratoc567acb2009-08-31 14:34:43 -070097 public static final int ALLOC_PARAMS = 0;
98 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -070099 public static final int ALLOC_READBACK = 2;
100 public static final int ALLOC_ICON_IDS = 3;
101 public static final int ALLOC_LABEL_IDS = 4;
102 public static final int ALLOC_X_BORDERS = 5;
103 public static final int ALLOC_Y_BORDERS = 6;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700104
105 public static final int COLUMNS_PER_PAGE = 4;
106 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700107
Joe Onorato6665c0f2009-09-02 15:27:24 -0700108 public static final float RADIUS = 4.0f;
109
110 public static final int SCREEN_WIDTH_PX = 480;
111 public static final int SCREEN_HEIGHT_PX = 854;
112
113 public static final int ICON_WIDTH_PX = 64;
114 public static final int ICON_TEXTURE_WIDTH_PX = 128;
115
116 public static final int ICON_HEIGHT_PX = 64;
117 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
118 public static final float ICON_TOP_OFFSET = 0.2f;
119
120 public static final float CAMERA_Z = -2;
121 public static final float FAR_ICON_SIZE
122 = farSize(2 * ICON_WIDTH_PX / (float)SCREEN_WIDTH_PX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700123 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700124
125 public AllAppsView(Context context, AttributeSet attrs) {
126 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700127 setFocusable(true);
128 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratod769a632009-08-11 17:09:02 -0700129 mConfig = ViewConfiguration.get(context);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700130 setOnClickListener(this);
131 setOnLongClickListener(this);
Dianne Hackbornb8898d52009-09-14 22:30:18 -0700132 setOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700133 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700134 }
135
Joe Onorato7c312c12009-08-13 21:36:53 -0700136 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
137 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700138 }
139
Joe Onorato6665c0f2009-09-02 15:27:24 -0700140 public void setLauncher(Launcher launcher) {
141 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700142 }
143
Joe Onorato1feb3a82009-08-08 22:32:00 -0700144 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700145 public void surfaceDestroyed(SurfaceHolder holder) {
146 super.surfaceDestroyed(holder);
147
148 destroyRenderScript();
149 mRS = null;
150 mRollo = null;
151 }
152
153 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700154 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
155 super.surfaceChanged(holder, format, w, h);
156
Joe Onorato6665c0f2009-09-02 15:27:24 -0700157 long startTime = SystemClock.uptimeMillis();
158
Jason Sams05de32a2009-09-27 14:01:40 -0700159 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700160 mRollo = new RolloRS();
161 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400162 if (mAllAppsList != null) {
163 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700164 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400165 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700166
167 Resources res = getContext().getResources();
168 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
169 mScrollHandleTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700170
171 long endTime = SystemClock.uptimeMillis();
172 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700173 }
174
175 @Override
176 public boolean onKeyDown(int keyCode, KeyEvent event)
177 {
Joe Onorato93839052009-08-06 20:34:32 -0700178 // this method doesn't work when 'extends View' include 'extends ScrollView'.
179 return super.onKeyDown(keyCode, event);
180 }
181
Joe Onorato93839052009-08-06 20:34:32 -0700182 @Override
183 public boolean onTouchEvent(MotionEvent ev)
184 {
Joe Onorato7bb17492009-09-24 17:51:01 -0700185 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700186 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700187 }
188
Joe Onoratofb0ca672009-09-14 17:55:46 -0400189 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700190 return true;
191 }
192
193 super.onTouchEvent(ev);
194
Joe Onoratofb0ca672009-09-14 17:55:46 -0400195 int x = (int)ev.getX();
196 int deltaX;
197 switch (ev.getAction()) {
198 case MotionEvent.ACTION_DOWN:
199 mMotionDownRawX = (int)ev.getRawX();
200 mMotionDownRawY = (int)ev.getRawY();
201 mLastMotionX = x;
Joe Onorato7bb17492009-09-24 17:51:01 -0700202 mRollo.mReadback.read();
Jason Sams86c87ed2009-09-18 13:55:55 -0700203
204 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
205 mRollo.mState.newTouchDown = 1;
206
Joe Onorato7bb17492009-09-24 17:51:01 -0700207 if (mRollo.mReadback.velocity != 0) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400208 mRollo.clearSelectedIcon();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700209 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700210 mRollo.selectIcon(x, (int)ev.getY(), mRollo.mReadback.posX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700211 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400212 mRollo.mState.save();
Jason Sams86c87ed2009-09-18 13:55:55 -0700213 mRollo.mInvokeMove.execute();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400214 mVelocity = VelocityTracker.obtain();
215 mVelocity.addMovement(ev);
216 mStartedScrolling = false;
217 break;
218 case MotionEvent.ACTION_MOVE:
219 case MotionEvent.ACTION_OUTSIDE:
220 int slop = Math.abs(x - mLastMotionX);
221 if (!mStartedScrolling && slop < mConfig.getScaledTouchSlop()) {
222 // don't update mLastMotionX so slop is right and when we do start scrolling
223 // below, we get the right delta.
224 } else {
Jason Sams86c87ed2009-09-18 13:55:55 -0700225
226 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
227 mRollo.mState.newTouchDown = 1;
228 mRollo.mInvokeMove.execute();
229
Joe Onoratofb0ca672009-09-14 17:55:46 -0400230 mStartedScrolling = true;
231 mRollo.clearSelectedIcon();
232 deltaX = x - mLastMotionX;
233 mVelocity.addMovement(ev);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400234 mRollo.mState.save();
235 mLastMotionX = x;
236 }
237 break;
238 case MotionEvent.ACTION_UP:
239 case MotionEvent.ACTION_CANCEL:
Joe Onorato360d0352009-09-28 14:37:53 -0400240 if (!mZoomSwipeInProgress) {
241 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
242 mRollo.mState.newTouchDown = 0;
Jason Sams86c87ed2009-09-18 13:55:55 -0700243
Joe Onorato360d0352009-09-28 14:37:53 -0400244 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
245 mConfig.getScaledMaximumFlingVelocity());
246 mRollo.mState.flingVelocityX = mVelocity.getXVelocity() / Defines.SCREEN_WIDTH_PX;
247 mRollo.clearSelectedIcon();
248 mRollo.mState.save();
249 mRollo.mInvokeFling.execute();
250 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400251 mLastMotionX = -10000;
252 mVelocity.recycle();
253 mVelocity = null;
254 break;
Joe Onorato93839052009-08-06 20:34:32 -0700255 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700256
257 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700258 }
259
Joe Onorato6665c0f2009-09-02 15:27:24 -0700260 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700261 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400262 return;
263 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700264 int index = mRollo.mState.selectedIconIndex;
265 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
266 ApplicationInfo app = mAllAppsList.get(index);
267 mLauncher.startActivitySafely(app.intent);
268 }
269 }
270
271 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700272 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400273 return true;
274 }
Joe Onorato5162ea92009-09-03 09:39:42 -0700275 int index = mRollo.mState.selectedIconIndex;
276 Log.d(TAG, "long click! velocity=" + mRollo.mState.flingVelocityX + " index=" + index);
277 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
278 ApplicationInfo app = mAllAppsList.get(index);
279
280 // We don't really have an accurate location to use. This will do.
281 int screenX = mMotionDownRawX - (Defines.ICON_WIDTH_PX / 2);
282 int screenY = mMotionDownRawY - Defines.ICON_HEIGHT_PX;
283
284 int left = (Defines.ICON_TEXTURE_WIDTH_PX - Defines.ICON_WIDTH_PX) / 2;
285 int top = (Defines.ICON_TEXTURE_HEIGHT_PX - Defines.ICON_HEIGHT_PX) / 2;
286 mDragController.startDrag(app.iconBitmap, screenX, screenY,
287 left, top, Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
288 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700289
Joe Onorato7bb17492009-09-24 17:51:01 -0700290 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700291 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700292 return true;
293 }
294
Joe Onorato5162ea92009-09-03 09:39:42 -0700295 public void setDragController(DragController dragger) {
296 mDragController = dragger;
297 }
298
299 public void onDropCompleted(View target, boolean success) {
300 }
301
Joe Onorato360d0352009-09-28 14:37:53 -0400302 public void setZoomSwipeInProgress(boolean swiping, boolean touchStillDown) {
303 mZoomSwipeInProgress = swiping;
304 if (!touchStillDown) {
305 mRollo.mState.newTouchDown = 0;
306 mRollo.mState.save();
307 mRollo.mInvokeTouchUp.execute();
308 }
309 }
310
Joe Onorato7bb17492009-09-24 17:51:01 -0700311 public void setZoomTarget(float amount) {
312 zoom(amount, true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700313 }
314
Joe Onorato7bb17492009-09-24 17:51:01 -0700315 public void setZoom(float amount) {
316 zoom(amount, false);
317 }
318
319 private void zoom(float amount, boolean animate) {
320 if (mRollo == null) {
321 return;
322 }
323
Joe Onoratofb0ca672009-09-14 17:55:46 -0400324 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400325 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700326 if (amount > 0.001f) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700327 mRollo.mState.zoomTarget = amount;
328 if (!animate) {
329 // set in readback, so we're correct even before the next frame
330 mRollo.mReadback.zoom = mRollo.mState.zoom = amount;
331 mRollo.mReadback.save();
332 }
Joe Onorato85a02a82009-09-08 12:34:22 -0700333 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700334 mRollo.mState.zoomTarget = 0;
335 if (!animate) {
336 mRollo.mReadback.zoom = mRollo.mState.zoom = 0;
337 mRollo.mReadback.save();
338 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700339 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400340 mRollo.mState.save();
Joe Onorato7bb17492009-09-24 17:51:01 -0700341 if (!animate) {
342 mRollo.mInvokeSetZoom.execute();
343 } else {
344 mRollo.mInvokeSetZoomTarget.execute();
345 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700346 mReadZoom.removeMessages(1);
347 mReadZoom.sendEmptyMessageDelayed(1, 1000);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400348 }
349
Joe Onorato8e099e22009-09-25 17:30:47 -0700350 Handler mReadZoom = new Handler() {
351 public void handleMessage(Message msg) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700352 if(mRollo != null && mRollo.mReadback != null) {
353 // FIXME: These checks may indicate other problems.
354 mRollo.mReadback.read();
355 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700356 }
357 };
358
Joe Onoratofb0ca672009-09-14 17:55:46 -0400359 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700360 if (mRollo == null) {
361 return false;
362 }
Joe Onorato8e099e22009-09-25 17:30:47 -0700363 //mRollo.mReadback.read();
Joe Onorato7bb17492009-09-24 17:51:01 -0700364 return mRollo.mReadback.zoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400365 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700366
Joe Onorato93839052009-08-06 20:34:32 -0700367 @Override
368 public boolean onTrackballEvent(MotionEvent ev)
369 {
370 float x = ev.getX();
371 float y = ev.getY();
372 //Float tx = new Float(x);
373 //Float ty = new Float(y);
374 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
375
376
377 return true;
378 }
379
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 public void setApps(ArrayList<ApplicationInfo> list) {
381 mAllAppsList = list;
382 if (mRollo != null) {
383 mRollo.setApps(list);
384 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700385 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700386 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400387 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700388 }
389
390 private void invokeIcon(int index) {
391 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700392 }
393
394 private static int countPages(int iconCount) {
395 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
396 int pages = iconCount / iconsPerPage;
397 if (pages*iconsPerPage != iconCount) {
398 pages++;
399 }
400 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400401 }
402
Joe Onorato93839052009-08-06 20:34:32 -0700403 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700404
Joe Onorato1feb3a82009-08-08 22:32:00 -0700405 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700406
Joe Onorato93839052009-08-06 20:34:32 -0700407 private int mWidth;
408 private int mHeight;
409
410 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700411 private Script mScript;
Jason Sams86c87ed2009-09-18 13:55:55 -0700412
413 private Script.Invokable mInvokeMove;
414 private Script.Invokable mInvokeFling;
Joe Onorato7bb17492009-09-24 17:51:01 -0700415 private Script.Invokable mInvokeSetZoomTarget;
416 private Script.Invokable mInvokeSetZoom;
Joe Onorato360d0352009-09-28 14:37:53 -0400417 private Script.Invokable mInvokeTouchUp;
Jason Sams86c87ed2009-09-18 13:55:55 -0700418
Joe Onorato93839052009-08-06 20:34:32 -0700419 private Sampler mSampler;
420 private Sampler mSamplerText;
421 private ProgramStore mPSBackground;
422 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700423 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700424 private ProgramFragment mPFImages;
Joe Onorato56848b02009-09-25 13:59:59 -0700425 private ProgramFragment mPFOrtho;
Joe Onorato93839052009-08-06 20:34:32 -0700426 private ProgramVertex mPV;
427 private ProgramVertex.MatrixAllocation mPVAlloc;
428 private ProgramVertex mPVOrtho;
429 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700430
Joe Onoratoc567acb2009-08-31 14:34:43 -0700431 private Allocation mScrollHandle;
432
Joe Onoratobf15cb42009-08-07 14:33:40 -0700433 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700434 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700435 private Allocation mAllocIconID;
436
Joe Onoratobf15cb42009-08-07 14:33:40 -0700437 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700438 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700439 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700440 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700441
Joe Onorato6665c0f2009-09-02 15:27:24 -0700442 private int[] mTouchYBorders;
443 private Allocation mAllocTouchYBorders;
444 private int[] mTouchXBorders;
445 private Allocation mAllocTouchXBorders;
446
447 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400448 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700449
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700450 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700451 State mState;
Joe Onorato7bb17492009-09-24 17:51:01 -0700452 Readback mReadback;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700453
Jason Sams78aebd82009-09-15 13:06:59 -0700454 class BaseAlloc {
455 Allocation mAlloc;
456 Type mType;
457
458 void save() {
459 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700460 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700461 }
462
Jason Sams78aebd82009-09-15 13:06:59 -0700463 class Params extends BaseAlloc {
464 Params() {
465 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
466 mAlloc = Allocation.createTyped(mRS, mType);
467 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700468 }
Jason Sams78aebd82009-09-15 13:06:59 -0700469 public int bubbleWidth;
470 public int bubbleHeight;
471 public int bubbleBitmapWidth;
472 public int bubbleBitmapHeight;
473 public int scrollHandleId;
474 public int scrollHandleTextureWidth;
475 public int scrollHandleTextureHeight;
476 }
477
478 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700479 public float newPositionX;
480 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700481 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700482 public int iconCount;
483 public int scrollX;
Jason Sams78aebd82009-09-15 13:06:59 -0700484 public int selectedIconIndex = -1;
485 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700486 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700487 public float zoom;
488
489 State() {
490 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
491 mAlloc = Allocation.createTyped(mRS, mType);
492 save();
493 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700494 }
495
Joe Onorato7bb17492009-09-24 17:51:01 -0700496 class Readback extends BaseAlloc {
497 public float posX;
498 public float velocity;
499 public float zoom;
500
501 Readback() {
502 mType = Type.createFromClass(mRS, Readback.class, 1, "ReadbackClass");
503 mAlloc = Allocation.createTyped(mRS, mType);
504 save();
505 }
506
507 void read() {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700508 if(mAlloc != null) {
509 mAlloc.read(this);
510 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700511 }
512 }
513
Joe Onorato1feb3a82009-08-08 22:32:00 -0700514 public RolloRS() {
515 }
516
517 public void init(Resources res, int width, int height) {
518 mRes = res;
519 mWidth = width;
520 mHeight = height;
521 initGl();
522 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700523 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700524 initRs();
525 }
526
527 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700528 Sampler.Builder sb = new Sampler.Builder(mRS);
529 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
530 sb.setMag(Sampler.Value.LINEAR);
531 sb.setWrapS(Sampler.Value.CLAMP);
532 sb.setWrapT(Sampler.Value.CLAMP);
533 mSampler = sb.create();
534
535 sb.setMin(Sampler.Value.NEAREST);
536 sb.setMag(Sampler.Value.NEAREST);
537 mSamplerText = sb.create();
538
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700539 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
540 mPFDebug = dbg.create();
541 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700542
543 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
544 bf.setTexEnable(true, 0);
545 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
546 mPFImages = bf.create();
547 mPFImages.setName("PF");
548 mPFImages.bindSampler(mSampler, 0);
549
550 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato56848b02009-09-25 13:59:59 -0700551 //mPFOrtho = bf.create();
552 mPFOrtho = (new ProgramFragment.Builder(mRS, null, null)).create();
553 mPFOrtho.setName("PFOrtho");
554 mPFOrtho.bindSampler(mSamplerText, 0);
Joe Onorato93839052009-08-06 20:34:32 -0700555
556 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700557 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700558 bs.setDitherEnable(false);
559 bs.setDepthMask(true);
560 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
561 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
562 mPSBackground = bs.create();
563 mPSBackground.setName("PFS");
564
565 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
566 bs.setDepthMask(false);
567 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
568 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
569 mPSText = bs.create();
570 mPSText.setName("PFSText");
571
572 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
573 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
574
575 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
576 mPV = pvb.create();
577 mPV.setName("PV");
578 mPV.bindAllocation(mPVAlloc);
579
580 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
581 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
582
583 pvb.setTextureMatrixEnable(true);
584 mPVOrtho = pvb.create();
585 mPVOrtho.setName("PVOrtho");
586 mPVOrtho.bindAllocation(mPVOrthoAlloc);
587
588 mRS.contextBindProgramVertex(mPV);
589
Joe Onorato6665c0f2009-09-02 15:27:24 -0700590 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700591 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700592 mTouchXBorders.length);
593 mAllocTouchXBorders.data(mTouchXBorders);
594
595 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700596 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700597 mTouchYBorders.length);
598 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700599 }
Jason Sams78aebd82009-09-15 13:06:59 -0700600
Joe Onorato1feb3a82009-08-08 22:32:00 -0700601 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700602 mParams = new Params();
603 mState = new State();
Joe Onorato7bb17492009-09-24 17:51:01 -0700604 mReadback = new Readback();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700605
Joe Onoratobf15cb42009-08-07 14:33:40 -0700606 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700607
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700608 mParams.bubbleWidth = bubble.getBubbleWidth();
609 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
610 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
611 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700612
Joe Onoratoc567acb2009-08-31 14:34:43 -0700613 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700614 R.drawable.all_apps_button_pow2, Element.RGBA_8888(mRS), false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700615 mScrollHandle.uploadToTexture(0);
616 mParams.scrollHandleId = mScrollHandle.getID();
617 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
618 mParams.scrollHandleTextureWidth = 128;
619 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700620
Joe Onoratoc567acb2009-08-31 14:34:43 -0700621
Joe Onorato1feb3a82009-08-08 22:32:00 -0700622 mParams.save();
623 mState.save();
Joe Onorato7bb17492009-09-24 17:51:01 -0700624 mReadback.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625
Joe Onorato1291a8c2009-09-15 15:07:25 -0400626 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
627 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
628 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700629
630 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700632 }
633
Joe Onorato1feb3a82009-08-08 22:32:00 -0700634 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700635 ScriptC.Builder sb = new ScriptC.Builder(mRS);
636 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700637 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700638 sb.addDefines(Defines.class);
Jason Sams78aebd82009-09-15 13:06:59 -0700639 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
640 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700641 sb.setType(mReadback.mType, "readback", Defines.ALLOC_READBACK);
Jason Sams86c87ed2009-09-18 13:55:55 -0700642 mInvokeMove = sb.addInvokable("move");
643 mInvokeFling = sb.addInvokable("fling");
Joe Onorato7bb17492009-09-24 17:51:01 -0700644 mInvokeSetZoomTarget = sb.addInvokable("setZoomTarget");
645 mInvokeSetZoom = sb.addInvokable("setZoom");
Joe Onorato360d0352009-09-28 14:37:53 -0400646 mInvokeTouchUp = sb.addInvokable("touchUp");
Joe Onorato93839052009-08-06 20:34:32 -0700647 mScript = sb.create();
648 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
649
Jason Sams78aebd82009-09-15 13:06:59 -0700650 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
651 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700652 mScript.bindAllocation(mReadback.mAlloc, Defines.ALLOC_READBACK);
Joe Onoratod769a632009-08-11 17:09:02 -0700653 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700654 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700655 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
656 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700657
658 mRS.contextBindRootScript(mScript);
659 }
Joe Onorato93839052009-08-06 20:34:32 -0700660
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661 private void setApps(ArrayList<ApplicationInfo> list) {
662 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700663 int allocCount = count;
664 if(allocCount < 1) {
665 allocCount = 1;
666 }
667
Joe Onorato9c1289c2009-08-17 11:03:03 -0400668 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700669 mIconIds = new int[allocCount];
670 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400671
672 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700673 mLabelIds = new int[allocCount];
674 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400675
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700676 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400677
678 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
679
680 for (int i=0; i<count; i++) {
681 final ApplicationInfo item = list.get(i);
682
683 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700684 Element.RGBA_8888(mRS), false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400685 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700686 Element.RGBA_8888(mRS), false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400687
688 mIcons[i].uploadToTexture(0);
689 mLabels[i].uploadToTexture(0);
690
Joe Onorato6665c0f2009-09-02 15:27:24 -0700691 mIconIds[i] = mIcons[i].getID();
692 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400693 }
694
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700695 mAllocIconID.data(mIconIds);
696 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697
698 mState.iconCount = count;
699
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 if (mScript != null) { // wtf
701 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
702 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
703 }
704
705 mState.save();
706 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700707
708 void initTouchState() {
709 int width = getWidth();
710 int height = getHeight();
711
712 int iconsSize;
713 if (width < height) {
714 iconsSize = width;
715 } else {
716 iconsSize = height;
717 }
718 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
719 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
720
Joe Onorato56848b02009-09-25 13:59:59 -0700721 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
722 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
723 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700724 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -0700725 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
726 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700727
728 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700729
Joe Onorato6665c0f2009-09-02 15:27:24 -0700730 int centerX = (width / 2);
731 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -0700732 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700733 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -0700734 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700735 mTouchXBorders[4] = centerX + (2 * cellWidth);
736
737 mAllocTouchXBorders.data(mTouchXBorders);
738 }
739
Jason Sams86c87ed2009-09-18 13:55:55 -0700740 int chooseTappedIcon(int x, int y, float page) {
741 int currentPage = (int)page;
742
Joe Onorato6665c0f2009-09-02 15:27:24 -0700743 int col = -1;
744 int row = -1;
745
746 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
747 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
748 col = i;
749 break;
750 }
751 }
752 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
753 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
754 row = i;
755 break;
756 }
757 }
758
759 if (row < 0 || col < 0) {
760 return -1;
761 }
762
Jason Sams78aebd82009-09-15 13:06:59 -0700763 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700764 + (row * Defines.ROWS_PER_PAGE) + col;
765 }
766
767 /**
768 * You need to call save() on mState on your own after calling this.
769 */
Jason Sams86c87ed2009-09-18 13:55:55 -0700770 void selectIcon(int x, int y, float pos) {
771 int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400772 selectIcon(index);
773 }
774
775 void selectIcon(int index) {
776 Log.d(TAG, "selectIcon index=" + index);
777 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700778 if (index < 0 || index >= iconCount) {
779 mState.selectedIconIndex = -1;
780 return;
781 } else {
782 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400783
784 Bitmap selectionBitmap = mSelectionBitmap;
785
786 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
787 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
788 mAllAppsList.get(index).iconBitmap);
789
790 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700791 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400792 mSelectedIcon.uploadToTexture(0);
793 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700794 }
795 }
796
797 /**
798 * You need to call save() on mState on your own after calling this.
799 */
800 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400801 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700802 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400803 }
Joe Onorato93839052009-08-06 20:34:32 -0700804}
805
806