blob: df438149815dc1ae5ebf2e795ea8da3491e771ea [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;
Jason Samscd689e12009-09-29 15:28:22 -0700265 if (mRollo.mReadback.velocity < 1 && index >= 0 && index < mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700266 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
Jason Samscd689e12009-09-29 15:28:22 -0700419 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700420 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700421 private ProgramFragment mPFColor;
422 private ProgramFragment mPFTexLinear;
423 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700424 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700425 private ProgramVertex mPVOrtho;
Joe Onorato93839052009-08-06 20:34:32 -0700426
Joe Onoratoc567acb2009-08-31 14:34:43 -0700427 private Allocation mScrollHandle;
428
Joe Onoratobf15cb42009-08-07 14:33:40 -0700429 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700430 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700431 private Allocation mAllocIconID;
432
Joe Onoratobf15cb42009-08-07 14:33:40 -0700433 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700434 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700435 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700436 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700437
Joe Onorato6665c0f2009-09-02 15:27:24 -0700438 private int[] mTouchYBorders;
439 private Allocation mAllocTouchYBorders;
440 private int[] mTouchXBorders;
441 private Allocation mAllocTouchXBorders;
442
443 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400444 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700445
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700446 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700447 State mState;
Joe Onorato7bb17492009-09-24 17:51:01 -0700448 Readback mReadback;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700449
Jason Sams78aebd82009-09-15 13:06:59 -0700450 class BaseAlloc {
451 Allocation mAlloc;
452 Type mType;
453
454 void save() {
455 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700456 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700457 }
458
Jason Sams78aebd82009-09-15 13:06:59 -0700459 class Params extends BaseAlloc {
460 Params() {
461 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
462 mAlloc = Allocation.createTyped(mRS, mType);
463 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700464 }
Jason Sams78aebd82009-09-15 13:06:59 -0700465 public int bubbleWidth;
466 public int bubbleHeight;
467 public int bubbleBitmapWidth;
468 public int bubbleBitmapHeight;
469 public int scrollHandleId;
470 public int scrollHandleTextureWidth;
471 public int scrollHandleTextureHeight;
472 }
473
474 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700475 public float newPositionX;
476 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700477 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700478 public int iconCount;
479 public int scrollX;
Jason Sams78aebd82009-09-15 13:06:59 -0700480 public int selectedIconIndex = -1;
481 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700482 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700483 public float zoom;
484
485 State() {
486 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
487 mAlloc = Allocation.createTyped(mRS, mType);
488 save();
489 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700490 }
491
Joe Onorato7bb17492009-09-24 17:51:01 -0700492 class Readback extends BaseAlloc {
493 public float posX;
494 public float velocity;
495 public float zoom;
496
497 Readback() {
498 mType = Type.createFromClass(mRS, Readback.class, 1, "ReadbackClass");
499 mAlloc = Allocation.createTyped(mRS, mType);
500 save();
501 }
502
503 void read() {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700504 if(mAlloc != null) {
505 mAlloc.read(this);
506 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700507 }
508 }
509
Joe Onorato1feb3a82009-08-08 22:32:00 -0700510 public RolloRS() {
511 }
512
513 public void init(Resources res, int width, int height) {
514 mRes = res;
515 mWidth = width;
516 mHeight = height;
Jason Samscd689e12009-09-29 15:28:22 -0700517 initProgramVertex();
518 initProgramFragment();
519 initProgramStore();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700520 initGl();
521 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700522 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700523 initRs();
524 }
525
Jason Samscd689e12009-09-29 15:28:22 -0700526 private void initProgramVertex() {
527 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
528 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700529
530 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
531 mPV = pvb.create();
532 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700533 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700534
Jason Samscd689e12009-09-29 15:28:22 -0700535 pva = new ProgramVertex.MatrixAllocation(mRS);
536 pva.setupOrthoWindow(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700537 pvb.setTextureMatrixEnable(true);
538 mPVOrtho = pvb.create();
539 mPVOrtho.setName("PVOrtho");
Jason Samscd689e12009-09-29 15:28:22 -0700540 mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700541
542 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700543 }
Joe Onorato93839052009-08-06 20:34:32 -0700544
Jason Samscd689e12009-09-29 15:28:22 -0700545 private void initProgramFragment() {
546 Sampler.Builder sb = new Sampler.Builder(mRS);
547 sb.setMin(Sampler.Value.LINEAR);
548 sb.setMag(Sampler.Value.LINEAR);
549 sb.setWrapS(Sampler.Value.CLAMP);
550 sb.setWrapT(Sampler.Value.CLAMP);
551 Sampler linear = sb.create();
552
553 sb.setMin(Sampler.Value.NEAREST);
554 sb.setMag(Sampler.Value.NEAREST);
555 Sampler nearest = sb.create();
556
557 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
558 mPFColor = bf.create();
559 mPFColor.setName("PFColor");
560
561 bf.setTexEnable(true, 0);
562 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
563 mPFTexLinear = bf.create();
564 mPFTexLinear.setName("PFTexLinear");
565 mPFTexLinear.bindSampler(linear, 0);
566
567 mPFTexNearest = bf.create();
568 mPFTexNearest.setName("PFTexNearest");
569 mPFTexNearest.bindSampler(nearest, 0);
570 }
571
572 private void initProgramStore() {
573 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
574 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
575 bs.setDitherEnable(true);
576 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
577 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
578 mPSIcons = bs.create();
579 mPSIcons.setName("PSIcons");
580
581 //bs.setDitherEnable(false);
582 //mPSText = bs.create();
583 //mPSText.setName("PSText");
584 }
585
586 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700587 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700588 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700589 mTouchXBorders.length);
590 mAllocTouchXBorders.data(mTouchXBorders);
591
592 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700593 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700594 mTouchYBorders.length);
595 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700596 }
Jason Sams78aebd82009-09-15 13:06:59 -0700597
Joe Onorato1feb3a82009-08-08 22:32:00 -0700598 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700599 mParams = new Params();
600 mState = new State();
Joe Onorato7bb17492009-09-24 17:51:01 -0700601 mReadback = new Readback();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700602
Joe Onoratobf15cb42009-08-07 14:33:40 -0700603 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700604
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700605 mParams.bubbleWidth = bubble.getBubbleWidth();
606 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
607 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
608 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700609
Joe Onoratoc567acb2009-08-31 14:34:43 -0700610 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700611 R.drawable.all_apps_button_pow2, Element.RGBA_8888(mRS), false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700612 mScrollHandle.uploadToTexture(0);
613 mParams.scrollHandleId = mScrollHandle.getID();
614 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
615 mParams.scrollHandleTextureWidth = 128;
616 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700617
Joe Onoratoc567acb2009-08-31 14:34:43 -0700618
Joe Onorato1feb3a82009-08-08 22:32:00 -0700619 mParams.save();
620 mState.save();
Joe Onorato7bb17492009-09-24 17:51:01 -0700621 mReadback.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400622
Joe Onorato1291a8c2009-09-15 15:07:25 -0400623 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
624 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
625 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700626
627 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400628 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700629 }
630
Joe Onorato1feb3a82009-08-08 22:32:00 -0700631 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700632 ScriptC.Builder sb = new ScriptC.Builder(mRS);
633 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700634 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700635 sb.addDefines(Defines.class);
Jason Sams78aebd82009-09-15 13:06:59 -0700636 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
637 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700638 sb.setType(mReadback.mType, "readback", Defines.ALLOC_READBACK);
Jason Sams86c87ed2009-09-18 13:55:55 -0700639 mInvokeMove = sb.addInvokable("move");
640 mInvokeFling = sb.addInvokable("fling");
Joe Onorato7bb17492009-09-24 17:51:01 -0700641 mInvokeSetZoomTarget = sb.addInvokable("setZoomTarget");
642 mInvokeSetZoom = sb.addInvokable("setZoom");
Joe Onorato360d0352009-09-28 14:37:53 -0400643 mInvokeTouchUp = sb.addInvokable("touchUp");
Joe Onorato93839052009-08-06 20:34:32 -0700644 mScript = sb.create();
645 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
646
Jason Sams78aebd82009-09-15 13:06:59 -0700647 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
648 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
Joe Onorato7bb17492009-09-24 17:51:01 -0700649 mScript.bindAllocation(mReadback.mAlloc, Defines.ALLOC_READBACK);
Joe Onoratod769a632009-08-11 17:09:02 -0700650 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700651 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700652 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
653 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700654
655 mRS.contextBindRootScript(mScript);
656 }
Joe Onorato93839052009-08-06 20:34:32 -0700657
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 private void setApps(ArrayList<ApplicationInfo> list) {
659 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700660 int allocCount = count;
661 if(allocCount < 1) {
662 allocCount = 1;
663 }
664
Joe Onorato9c1289c2009-08-17 11:03:03 -0400665 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700666 mIconIds = new int[allocCount];
667 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400668
669 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700670 mLabelIds = new int[allocCount];
671 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400672
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700673 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400674
675 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
676
677 for (int i=0; i<count; i++) {
678 final ApplicationInfo item = list.get(i);
679
680 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700681 Element.RGBA_8888(mRS), false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400682 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700683 Element.RGBA_8888(mRS), false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684
685 mIcons[i].uploadToTexture(0);
686 mLabels[i].uploadToTexture(0);
687
Joe Onorato6665c0f2009-09-02 15:27:24 -0700688 mIconIds[i] = mIcons[i].getID();
689 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690 }
691
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700692 mAllocIconID.data(mIconIds);
693 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400694
695 mState.iconCount = count;
696
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697 if (mScript != null) { // wtf
698 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
699 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
700 }
701
702 mState.save();
703 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700704
705 void initTouchState() {
706 int width = getWidth();
707 int height = getHeight();
708
709 int iconsSize;
710 if (width < height) {
711 iconsSize = width;
712 } else {
713 iconsSize = height;
714 }
715 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
716 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
717
Joe Onorato56848b02009-09-25 13:59:59 -0700718 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
719 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
720 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700721 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -0700722 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
723 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700724
725 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700726
Joe Onorato6665c0f2009-09-02 15:27:24 -0700727 int centerX = (width / 2);
728 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -0700729 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700730 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -0700731 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700732 mTouchXBorders[4] = centerX + (2 * cellWidth);
733
734 mAllocTouchXBorders.data(mTouchXBorders);
735 }
736
Jason Sams86c87ed2009-09-18 13:55:55 -0700737 int chooseTappedIcon(int x, int y, float page) {
738 int currentPage = (int)page;
739
Joe Onorato6665c0f2009-09-02 15:27:24 -0700740 int col = -1;
741 int row = -1;
742
743 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
744 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
745 col = i;
746 break;
747 }
748 }
749 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
750 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
751 row = i;
752 break;
753 }
754 }
755
756 if (row < 0 || col < 0) {
757 return -1;
758 }
759
Jason Sams78aebd82009-09-15 13:06:59 -0700760 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700761 + (row * Defines.ROWS_PER_PAGE) + col;
762 }
763
764 /**
765 * You need to call save() on mState on your own after calling this.
766 */
Jason Sams86c87ed2009-09-18 13:55:55 -0700767 void selectIcon(int x, int y, float pos) {
768 int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400769 selectIcon(index);
770 }
771
772 void selectIcon(int index) {
773 Log.d(TAG, "selectIcon index=" + index);
774 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700775 if (index < 0 || index >= iconCount) {
776 mState.selectedIconIndex = -1;
777 return;
778 } else {
779 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400780
781 Bitmap selectionBitmap = mSelectionBitmap;
782
783 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
784 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
785 mAllAppsList.get(index).iconBitmap);
786
787 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700788 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400789 mSelectedIcon.uploadToTexture(0);
790 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700791 }
792 }
793
794 /**
795 * You need to call save() on mState on your own after calling this.
796 */
797 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400798 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700799 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400800 }
Joe Onorato93839052009-08-06 20:34:32 -0700801}
802
803