blob: 03734c0dfb3b5c60c18a7fec7443ce5ffc2872e5 [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
70 /** Bit for mLocks for when the enter/exit is going. */
71 private static final int LOCK_ZOOMING = 2;
72
Joe Onorato6665c0f2009-09-02 15:27:24 -070073 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070074 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040075
76 /** When this is 0, modifications are allowed, when it's not, they're not.
77 * TODO: What about scrolling? */
78 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070079
Joe Onorato1feb3a82009-08-08 22:32:00 -070080 private RenderScript mRS;
81 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040082 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070083
Joe Onoratod769a632009-08-11 17:09:02 -070084 private ViewConfiguration mConfig;
Joe Onoratoc567acb2009-08-31 14:34:43 -070085 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070086 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070087 private VelocityTracker mVelocity;
Joe Onorato1feb3a82009-08-08 22:32:00 -070088 private int mLastMotionX;
Joe Onorato5162ea92009-09-03 09:39:42 -070089 private int mMotionDownRawX;
90 private int mMotionDownRawY;
Joe Onoratoc567acb2009-08-31 14:34:43 -070091 private int mScrollHandleTop;
Jason Sams86c87ed2009-09-18 13:55:55 -070092 private long mTouchTime;
Joe Onorato1feb3a82009-08-08 22:32:00 -070093
Joe Onorato6665c0f2009-09-02 15:27:24 -070094 static class Defines {
95 private static float farSize(float sizeAt0) {
96 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
97 }
98
Joe Onoratoc567acb2009-08-31 14:34:43 -070099 public static final int ALLOC_PARAMS = 0;
100 public static final int ALLOC_STATE = 1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700101 public static final int ALLOC_ICON_IDS = 2;
102 public static final int ALLOC_LABEL_IDS = 3;
103 public static final int ALLOC_X_BORDERS = 4;
104 public static final int ALLOC_Y_BORDERS = 5;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700105
106 public static final int COLUMNS_PER_PAGE = 4;
107 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700108
Joe Onorato6665c0f2009-09-02 15:27:24 -0700109 public static final float RADIUS = 4.0f;
110
111 public static final int SCREEN_WIDTH_PX = 480;
112 public static final int SCREEN_HEIGHT_PX = 854;
113
114 public static final int ICON_WIDTH_PX = 64;
115 public static final int ICON_TEXTURE_WIDTH_PX = 128;
116
117 public static final int ICON_HEIGHT_PX = 64;
118 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
119 public static final float ICON_TOP_OFFSET = 0.2f;
120
121 public static final float CAMERA_Z = -2;
122 public static final float FAR_ICON_SIZE
123 = farSize(2 * ICON_WIDTH_PX / (float)SCREEN_WIDTH_PX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700124 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700125
126 public AllAppsView(Context context, AttributeSet attrs) {
127 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700128 setFocusable(true);
129 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratod769a632009-08-11 17:09:02 -0700130 mConfig = ViewConfiguration.get(context);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700131 setOnClickListener(this);
132 setOnLongClickListener(this);
Dianne Hackbornb8898d52009-09-14 22:30:18 -0700133 setOnTop(true);
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 Onorato93839052009-08-06 20:34:32 -0700145 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
146 super.surfaceChanged(holder, format, w, h);
147
Joe Onorato6665c0f2009-09-02 15:27:24 -0700148 long startTime = SystemClock.uptimeMillis();
149
Jason Samsb58cbdc2009-08-21 16:56:58 -0700150 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700151 mRollo = new RolloRS();
152 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 if (mAllAppsList != null) {
154 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700155 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400156 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700157
158 Resources res = getContext().getResources();
159 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
160 mScrollHandleTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700161
162 long endTime = SystemClock.uptimeMillis();
163 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700164 }
165
166 @Override
167 public boolean onKeyDown(int keyCode, KeyEvent event)
168 {
Joe Onorato93839052009-08-06 20:34:32 -0700169 // this method doesn't work when 'extends View' include 'extends ScrollView'.
170 return super.onKeyDown(keyCode, event);
171 }
172
Joe Onorato93839052009-08-06 20:34:32 -0700173 @Override
174 public boolean onTouchEvent(MotionEvent ev)
175 {
Joe Onoratoe3406a22009-09-03 14:36:25 -0700176 if (mRollo.mState.visible == 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700177 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700178 }
179
Joe Onoratofb0ca672009-09-14 17:55:46 -0400180 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700181 return true;
182 }
183
184 super.onTouchEvent(ev);
185
Joe Onoratofb0ca672009-09-14 17:55:46 -0400186 int x = (int)ev.getX();
187 int deltaX;
188 switch (ev.getAction()) {
189 case MotionEvent.ACTION_DOWN:
190 mMotionDownRawX = (int)ev.getRawX();
191 mMotionDownRawY = (int)ev.getRawY();
192 mLastMotionX = x;
193 mRollo.mState.read();
Jason Sams86c87ed2009-09-18 13:55:55 -0700194
195 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
196 mRollo.mState.newTouchDown = 1;
197
198 if (mRollo.mState.readVel != 0) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400199 mRollo.clearSelectedIcon();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700200 } else {
Jason Sams86c87ed2009-09-18 13:55:55 -0700201 mRollo.selectIcon(x, (int)ev.getY(), mRollo.mState.readPosX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700202 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400203 mRollo.mState.save();
Jason Sams86c87ed2009-09-18 13:55:55 -0700204 mRollo.mInvokeMove.execute();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400205 mVelocity = VelocityTracker.obtain();
206 mVelocity.addMovement(ev);
207 mStartedScrolling = false;
208 break;
209 case MotionEvent.ACTION_MOVE:
210 case MotionEvent.ACTION_OUTSIDE:
211 int slop = Math.abs(x - mLastMotionX);
212 if (!mStartedScrolling && slop < mConfig.getScaledTouchSlop()) {
213 // don't update mLastMotionX so slop is right and when we do start scrolling
214 // below, we get the right delta.
215 } else {
Jason Sams86c87ed2009-09-18 13:55:55 -0700216
217 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
218 mRollo.mState.newTouchDown = 1;
219 mRollo.mInvokeMove.execute();
220
Joe Onoratofb0ca672009-09-14 17:55:46 -0400221 mStartedScrolling = true;
222 mRollo.clearSelectedIcon();
223 deltaX = x - mLastMotionX;
224 mVelocity.addMovement(ev);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400225 mRollo.mState.save();
226 mLastMotionX = x;
227 }
228 break;
229 case MotionEvent.ACTION_UP:
230 case MotionEvent.ACTION_CANCEL:
Jason Sams86c87ed2009-09-18 13:55:55 -0700231
232 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
233 mRollo.mState.newTouchDown = 0;
234
Joe Onoratofb0ca672009-09-14 17:55:46 -0400235 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
236 mConfig.getScaledMaximumFlingVelocity());
Jason Sams86c87ed2009-09-18 13:55:55 -0700237 mRollo.mState.flingVelocityX = mVelocity.getXVelocity() / Defines.SCREEN_WIDTH_PX;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400238 mRollo.clearSelectedIcon();
239 mRollo.mState.save();
Jason Sams86c87ed2009-09-18 13:55:55 -0700240 mRollo.mInvokeFling.execute();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400241 mLastMotionX = -10000;
242 mVelocity.recycle();
243 mVelocity = null;
244 break;
Joe Onorato93839052009-08-06 20:34:32 -0700245 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700246
247 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700248 }
249
Joe Onorato6665c0f2009-09-02 15:27:24 -0700250 public void onClick(View v) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400251 if (mLocks != 0) {
252 return;
253 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700254 int index = mRollo.mState.selectedIconIndex;
255 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
256 ApplicationInfo app = mAllAppsList.get(index);
257 mLauncher.startActivitySafely(app.intent);
258 }
259 }
260
261 public boolean onLongClick(View v) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400262 if (mLocks != 0) {
263 return true;
264 }
Joe Onorato5162ea92009-09-03 09:39:42 -0700265 int index = mRollo.mState.selectedIconIndex;
266 Log.d(TAG, "long click! velocity=" + mRollo.mState.flingVelocityX + " index=" + index);
267 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
268 ApplicationInfo app = mAllAppsList.get(index);
269
270 // We don't really have an accurate location to use. This will do.
271 int screenX = mMotionDownRawX - (Defines.ICON_WIDTH_PX / 2);
272 int screenY = mMotionDownRawY - Defines.ICON_HEIGHT_PX;
273
274 int left = (Defines.ICON_TEXTURE_WIDTH_PX - Defines.ICON_WIDTH_PX) / 2;
275 int top = (Defines.ICON_TEXTURE_HEIGHT_PX - Defines.ICON_HEIGHT_PX) / 2;
276 mDragController.startDrag(app.iconBitmap, screenX, screenY,
277 left, top, Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
278 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700279
Joe Onoratofb0ca672009-09-14 17:55:46 -0400280 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700281 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700282 return true;
283 }
284
Joe Onorato5162ea92009-09-03 09:39:42 -0700285 public void setDragController(DragController dragger) {
286 mDragController = dragger;
287 }
288
289 public void onDropCompleted(View target, boolean success) {
290 }
291
Joe Onorato85a02a82009-09-08 12:34:22 -0700292 public void setScale(float amount) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400293 cancelLongPress();
Joe Onorato85a02a82009-09-08 12:34:22 -0700294 mRollo.mState.read();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400295 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700296 if (amount > 0.001f) {
297 mRollo.mState.visible = 1;
Jason Sams78aebd82009-09-15 13:06:59 -0700298 mRollo.mState.zoom = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700299 } else {
300 mRollo.mState.visible = 0;
301 mRollo.mState.zoom = 0;
302 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400303 if (amount > 0.001f && amount < 0.999f) {
304 mLocks |= LOCK_ZOOMING;
305 } else {
306 mLocks &= ~LOCK_ZOOMING;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700307 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400308 mRollo.mState.save();
309 }
310
311 public boolean isZooming() {
312 return (mLocks & LOCK_ZOOMING) != 0;
313 }
314
315 public boolean isVisible() {
316 return mRollo != null && mRollo.mState.visible != 0;
317 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700318
Joe Onorato93839052009-08-06 20:34:32 -0700319 @Override
320 public boolean onTrackballEvent(MotionEvent ev)
321 {
322 float x = ev.getX();
323 float y = ev.getY();
324 //Float tx = new Float(x);
325 //Float ty = new Float(y);
326 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
327
328
329 return true;
330 }
331
Joe Onorato9c1289c2009-08-17 11:03:03 -0400332 public void setApps(ArrayList<ApplicationInfo> list) {
333 mAllAppsList = list;
334 if (mRollo != null) {
335 mRollo.setApps(list);
336 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700337 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700338 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400339 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700340 }
341
342 private void invokeIcon(int index) {
343 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700344 }
345
346 private static int countPages(int iconCount) {
347 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
348 int pages = iconCount / iconsPerPage;
349 if (pages*iconsPerPage != iconCount) {
350 pages++;
351 }
352 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400353 }
354
Joe Onorato93839052009-08-06 20:34:32 -0700355 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700356
Joe Onorato1feb3a82009-08-08 22:32:00 -0700357 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700358
Joe Onorato93839052009-08-06 20:34:32 -0700359 private int mWidth;
360 private int mHeight;
361
362 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700363 private Script mScript;
Jason Sams86c87ed2009-09-18 13:55:55 -0700364
365 private Script.Invokable mInvokeMove;
366 private Script.Invokable mInvokeFling;
367
Joe Onorato93839052009-08-06 20:34:32 -0700368 private Sampler mSampler;
369 private Sampler mSamplerText;
370 private ProgramStore mPSBackground;
371 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700372 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700373 private ProgramFragment mPFImages;
374 private ProgramFragment mPFText;
375 private ProgramVertex mPV;
376 private ProgramVertex.MatrixAllocation mPVAlloc;
377 private ProgramVertex mPVOrtho;
378 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700379
Joe Onoratoc567acb2009-08-31 14:34:43 -0700380 private Allocation mScrollHandle;
381
Joe Onoratobf15cb42009-08-07 14:33:40 -0700382 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700383 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700384 private Allocation mAllocIconID;
385
Joe Onoratobf15cb42009-08-07 14:33:40 -0700386 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700387 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700388 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700389 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700390
Joe Onorato6665c0f2009-09-02 15:27:24 -0700391 private int[] mTouchYBorders;
392 private Allocation mAllocTouchYBorders;
393 private int[] mTouchXBorders;
394 private Allocation mAllocTouchXBorders;
395
396 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400397 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700398
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700399 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700400 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700401
Jason Sams78aebd82009-09-15 13:06:59 -0700402 class BaseAlloc {
403 Allocation mAlloc;
404 Type mType;
405
406 void save() {
407 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700408 }
Jason Sams78aebd82009-09-15 13:06:59 -0700409
410 void read() {
411 mAlloc.read(this);
412 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700413 }
414
Jason Sams78aebd82009-09-15 13:06:59 -0700415 class Params extends BaseAlloc {
416 Params() {
417 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
418 mAlloc = Allocation.createTyped(mRS, mType);
419 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700420 }
Jason Sams78aebd82009-09-15 13:06:59 -0700421 public int bubbleWidth;
422 public int bubbleHeight;
423 public int bubbleBitmapWidth;
424 public int bubbleBitmapHeight;
425 public int scrollHandleId;
426 public int scrollHandleTextureWidth;
427 public int scrollHandleTextureHeight;
428 }
429
430 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700431 public float newPositionX;
432 public int newTouchDown;
433 public float readPosX;
434 public float readVel;
435 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700436 public int iconCount;
437 public int scrollX;
Jason Sams78aebd82009-09-15 13:06:59 -0700438 public int selectedIconIndex = -1;
439 public int selectedIconTexture;
440 public int visible;
441 public float zoom;
442
443 State() {
444 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
445 mAlloc = Allocation.createTyped(mRS, mType);
446 save();
447 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700448 }
449
450 public RolloRS() {
451 }
452
453 public void init(Resources res, int width, int height) {
454 mRes = res;
455 mWidth = width;
456 mHeight = height;
457 initGl();
458 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700459 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700460 initRs();
461 }
462
463 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700464 Sampler.Builder sb = new Sampler.Builder(mRS);
465 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
466 sb.setMag(Sampler.Value.LINEAR);
467 sb.setWrapS(Sampler.Value.CLAMP);
468 sb.setWrapT(Sampler.Value.CLAMP);
469 mSampler = sb.create();
470
471 sb.setMin(Sampler.Value.NEAREST);
472 sb.setMag(Sampler.Value.NEAREST);
473 mSamplerText = sb.create();
474
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700475 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
476 mPFDebug = dbg.create();
477 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700478
479 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
480 bf.setTexEnable(true, 0);
481 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
482 mPFImages = bf.create();
483 mPFImages.setName("PF");
484 mPFImages.bindSampler(mSampler, 0);
485
486 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700487 //mPFText = bf.create();
488 mPFText = (new ProgramFragment.Builder(mRS, null, null)).create();
Joe Onorato93839052009-08-06 20:34:32 -0700489 mPFText.setName("PFText");
490 mPFText.bindSampler(mSamplerText, 0);
491
492 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700493 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700494 bs.setDitherEnable(false);
495 bs.setDepthMask(true);
496 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
497 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
498 mPSBackground = bs.create();
499 mPSBackground.setName("PFS");
500
501 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
502 bs.setDepthMask(false);
503 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
504 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
505 mPSText = bs.create();
506 mPSText.setName("PFSText");
507
508 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
509 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
510
511 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
512 mPV = pvb.create();
513 mPV.setName("PV");
514 mPV.bindAllocation(mPVAlloc);
515
516 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
517 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
518
519 pvb.setTextureMatrixEnable(true);
520 mPVOrtho = pvb.create();
521 mPVOrtho.setName("PVOrtho");
522 mPVOrtho.bindAllocation(mPVOrthoAlloc);
523
524 mRS.contextBindProgramVertex(mPV);
525
Joe Onorato6665c0f2009-09-02 15:27:24 -0700526 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
527 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32,
528 mTouchXBorders.length);
529 mAllocTouchXBorders.data(mTouchXBorders);
530
531 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
532 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32,
533 mTouchYBorders.length);
534 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onorato93839052009-08-06 20:34:32 -0700535
536 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700537 }
Jason Sams78aebd82009-09-15 13:06:59 -0700538
Joe Onorato1feb3a82009-08-08 22:32:00 -0700539 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700540 mParams = new Params();
541 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700542
Joe Onoratobf15cb42009-08-07 14:33:40 -0700543 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700544
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700545 mParams.bubbleWidth = bubble.getBubbleWidth();
546 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
547 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
548 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700549
Joe Onoratoc567acb2009-08-31 14:34:43 -0700550 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700551 R.drawable.all_apps_button_pow2, Element.RGBA_8888, false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700552 mScrollHandle.uploadToTexture(0);
553 mParams.scrollHandleId = mScrollHandle.getID();
554 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
555 mParams.scrollHandleTextureWidth = 128;
556 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700557
Joe Onoratoc567acb2009-08-31 14:34:43 -0700558
Joe Onorato1feb3a82009-08-08 22:32:00 -0700559 mParams.save();
560 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400561
Joe Onorato1291a8c2009-09-15 15:07:25 -0400562 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
563 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
564 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700565
566 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400567 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700568 }
569
Joe Onorato1feb3a82009-08-08 22:32:00 -0700570 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700571 ScriptC.Builder sb = new ScriptC.Builder(mRS);
572 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700573 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700574 sb.addDefines(Defines.class);
Jason Sams78aebd82009-09-15 13:06:59 -0700575 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
576 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams86c87ed2009-09-18 13:55:55 -0700577 mInvokeMove = sb.addInvokable("move");
578 mInvokeFling = sb.addInvokable("fling");
Joe Onorato93839052009-08-06 20:34:32 -0700579 mScript = sb.create();
580 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
581
Jason Sams78aebd82009-09-15 13:06:59 -0700582 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
583 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
Joe Onoratod769a632009-08-11 17:09:02 -0700584 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700585 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700586 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
587 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700588
589 mRS.contextBindRootScript(mScript);
590 }
Joe Onorato93839052009-08-06 20:34:32 -0700591
Joe Onorato9c1289c2009-08-17 11:03:03 -0400592 private void setApps(ArrayList<ApplicationInfo> list) {
593 final int count = list != null ? list.size() : 0;
594 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700595 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400596 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
597
598 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700599 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400600 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
601
602 Element ie8888 = Element.RGBA_8888;
603
604 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
605
606 for (int i=0; i<count; i++) {
607 final ApplicationInfo item = list.get(i);
608
609 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700610 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400611 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700612 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400613
614 mIcons[i].uploadToTexture(0);
615 mLabels[i].uploadToTexture(0);
616
Joe Onorato6665c0f2009-09-02 15:27:24 -0700617 mIconIds[i] = mIcons[i].getID();
618 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400619 }
620
Joe Onorato6665c0f2009-09-02 15:27:24 -0700621 mAllocIconID.data(mIconIds);
622 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400623
624 mState.iconCount = count;
625
626 Log.d("AllAppsView", "mScript=" + mScript + " mAllocIconID=" + mAllocIconID);
627
628 if (mScript != null) { // wtf
629 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
630 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
631 }
632
633 mState.save();
634 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700635
636 void initTouchState() {
637 int width = getWidth();
638 int height = getHeight();
639
640 int iconsSize;
641 if (width < height) {
642 iconsSize = width;
643 } else {
644 iconsSize = height;
645 }
646 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
647 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
648
649 int centerY = (height / 2) - (int)(cellHeight * 0.35f);
650 mTouchYBorders[0] = centerY - (int)(2.4f * cellHeight);
651 mTouchYBorders[1] = centerY - (int)(1.15f * cellHeight);
652 mTouchYBorders[2] = centerY;
653 mTouchYBorders[3] = centerY + (int)(1.15f * cellHeight);;
654 mTouchYBorders[4] = centerY + (int)(2.4f * cellHeight);
655
656 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700657
Joe Onorato6665c0f2009-09-02 15:27:24 -0700658 int centerX = (width / 2);
659 mTouchXBorders[0] = centerX - (2 * cellWidth);
660 mTouchXBorders[1] = centerX - (int)(0.83f * cellWidth);;
661 mTouchXBorders[2] = centerX;
662 mTouchXBorders[3] = centerX + (int)(0.83f * cellWidth);;
663 mTouchXBorders[4] = centerX + (2 * cellWidth);
664
665 mAllocTouchXBorders.data(mTouchXBorders);
666 }
667
Jason Sams86c87ed2009-09-18 13:55:55 -0700668 int chooseTappedIcon(int x, int y, float page) {
669 int currentPage = (int)page;
670
Joe Onorato6665c0f2009-09-02 15:27:24 -0700671 int col = -1;
672 int row = -1;
673
674 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
675 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
676 col = i;
677 break;
678 }
679 }
680 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
681 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
682 row = i;
683 break;
684 }
685 }
686
687 if (row < 0 || col < 0) {
688 return -1;
689 }
690
Jason Sams78aebd82009-09-15 13:06:59 -0700691 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700692 + (row * Defines.ROWS_PER_PAGE) + col;
693 }
694
695 /**
696 * You need to call save() on mState on your own after calling this.
697 */
Jason Sams86c87ed2009-09-18 13:55:55 -0700698 void selectIcon(int x, int y, float pos) {
699 int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400700 selectIcon(index);
701 }
702
703 void selectIcon(int index) {
704 Log.d(TAG, "selectIcon index=" + index);
705 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700706 if (index < 0 || index >= iconCount) {
707 mState.selectedIconIndex = -1;
708 return;
709 } else {
710 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400711
712 Bitmap selectionBitmap = mSelectionBitmap;
713
714 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
715 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
716 mAllAppsList.get(index).iconBitmap);
717
718 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
719 Element.RGBA_8888, false);
720 mSelectedIcon.uploadToTexture(0);
721 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700722 }
723 }
724
725 /**
726 * You need to call save() on mState on your own after calling this.
727 */
728 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400729 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700730 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731 }
Joe Onorato93839052009-08-06 20:34:32 -0700732}
733
734