blob: e2f3be3bf25d7b9313cb308c145be52f39030d41 [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);
Jason Samsfd22dac2009-09-20 17:24:16 -0700134 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700135 }
136
Joe Onorato7c312c12009-08-13 21:36:53 -0700137 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
138 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700139 }
140
Joe Onorato6665c0f2009-09-02 15:27:24 -0700141 public void setLauncher(Launcher launcher) {
142 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700143 }
144
Joe Onorato1feb3a82009-08-08 22:32:00 -0700145 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700146 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
147 super.surfaceChanged(holder, format, w, h);
148
Joe Onorato6665c0f2009-09-02 15:27:24 -0700149 long startTime = SystemClock.uptimeMillis();
150
Joe Onorato2df5a572009-09-23 14:43:22 -0700151 mRS = createRenderScript(true, true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700152 mRollo = new RolloRS();
153 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400154 if (mAllAppsList != null) {
155 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700156 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400157 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700158
159 Resources res = getContext().getResources();
160 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
161 mScrollHandleTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700162
163 long endTime = SystemClock.uptimeMillis();
164 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700165 }
166
167 @Override
168 public boolean onKeyDown(int keyCode, KeyEvent event)
169 {
Joe Onorato93839052009-08-06 20:34:32 -0700170 // this method doesn't work when 'extends View' include 'extends ScrollView'.
171 return super.onKeyDown(keyCode, event);
172 }
173
Joe Onorato93839052009-08-06 20:34:32 -0700174 @Override
175 public boolean onTouchEvent(MotionEvent ev)
176 {
Joe Onoratoe3406a22009-09-03 14:36:25 -0700177 if (mRollo.mState.visible == 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700178 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700179 }
180
Joe Onoratofb0ca672009-09-14 17:55:46 -0400181 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700182 return true;
183 }
184
185 super.onTouchEvent(ev);
186
Joe Onoratofb0ca672009-09-14 17:55:46 -0400187 int x = (int)ev.getX();
188 int deltaX;
189 switch (ev.getAction()) {
190 case MotionEvent.ACTION_DOWN:
191 mMotionDownRawX = (int)ev.getRawX();
192 mMotionDownRawY = (int)ev.getRawY();
193 mLastMotionX = x;
194 mRollo.mState.read();
Jason Sams86c87ed2009-09-18 13:55:55 -0700195
196 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
197 mRollo.mState.newTouchDown = 1;
198
199 if (mRollo.mState.readVel != 0) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400200 mRollo.clearSelectedIcon();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700201 } else {
Jason Sams86c87ed2009-09-18 13:55:55 -0700202 mRollo.selectIcon(x, (int)ev.getY(), mRollo.mState.readPosX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700203 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400204 mRollo.mState.save();
Jason Sams86c87ed2009-09-18 13:55:55 -0700205 mRollo.mInvokeMove.execute();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400206 mVelocity = VelocityTracker.obtain();
207 mVelocity.addMovement(ev);
208 mStartedScrolling = false;
209 break;
210 case MotionEvent.ACTION_MOVE:
211 case MotionEvent.ACTION_OUTSIDE:
212 int slop = Math.abs(x - mLastMotionX);
213 if (!mStartedScrolling && slop < mConfig.getScaledTouchSlop()) {
214 // don't update mLastMotionX so slop is right and when we do start scrolling
215 // below, we get the right delta.
216 } else {
Jason Sams86c87ed2009-09-18 13:55:55 -0700217
218 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
219 mRollo.mState.newTouchDown = 1;
220 mRollo.mInvokeMove.execute();
221
Joe Onoratofb0ca672009-09-14 17:55:46 -0400222 mStartedScrolling = true;
223 mRollo.clearSelectedIcon();
224 deltaX = x - mLastMotionX;
225 mVelocity.addMovement(ev);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400226 mRollo.mState.save();
227 mLastMotionX = x;
228 }
229 break;
230 case MotionEvent.ACTION_UP:
231 case MotionEvent.ACTION_CANCEL:
Jason Sams86c87ed2009-09-18 13:55:55 -0700232
233 mRollo.mState.newPositionX = ev.getRawX() / Defines.SCREEN_WIDTH_PX;
234 mRollo.mState.newTouchDown = 0;
235
Joe Onoratofb0ca672009-09-14 17:55:46 -0400236 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
237 mConfig.getScaledMaximumFlingVelocity());
Jason Sams86c87ed2009-09-18 13:55:55 -0700238 mRollo.mState.flingVelocityX = mVelocity.getXVelocity() / Defines.SCREEN_WIDTH_PX;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400239 mRollo.clearSelectedIcon();
240 mRollo.mState.save();
Jason Sams86c87ed2009-09-18 13:55:55 -0700241 mRollo.mInvokeFling.execute();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400242 mLastMotionX = -10000;
243 mVelocity.recycle();
244 mVelocity = null;
245 break;
Joe Onorato93839052009-08-06 20:34:32 -0700246 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700247
248 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700249 }
250
Joe Onorato6665c0f2009-09-02 15:27:24 -0700251 public void onClick(View v) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400252 if (mLocks != 0) {
253 return;
254 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700255 int index = mRollo.mState.selectedIconIndex;
256 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
257 ApplicationInfo app = mAllAppsList.get(index);
258 mLauncher.startActivitySafely(app.intent);
259 }
260 }
261
262 public boolean onLongClick(View v) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400263 if (mLocks != 0) {
264 return true;
265 }
Joe Onorato5162ea92009-09-03 09:39:42 -0700266 int index = mRollo.mState.selectedIconIndex;
267 Log.d(TAG, "long click! velocity=" + mRollo.mState.flingVelocityX + " index=" + index);
268 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
269 ApplicationInfo app = mAllAppsList.get(index);
270
271 // We don't really have an accurate location to use. This will do.
272 int screenX = mMotionDownRawX - (Defines.ICON_WIDTH_PX / 2);
273 int screenY = mMotionDownRawY - Defines.ICON_HEIGHT_PX;
274
275 int left = (Defines.ICON_TEXTURE_WIDTH_PX - Defines.ICON_WIDTH_PX) / 2;
276 int top = (Defines.ICON_TEXTURE_HEIGHT_PX - Defines.ICON_HEIGHT_PX) / 2;
277 mDragController.startDrag(app.iconBitmap, screenX, screenY,
278 left, top, Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
279 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700280
Jason Samsfd22dac2009-09-20 17:24:16 -0700281 mLauncher.closeAllApps();
Joe Onorato5162ea92009-09-03 09:39:42 -0700282 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700283 return true;
284 }
285
Joe Onorato5162ea92009-09-03 09:39:42 -0700286 public void setDragController(DragController dragger) {
287 mDragController = dragger;
288 }
289
290 public void onDropCompleted(View target, boolean success) {
291 }
292
Jason Samsfd22dac2009-09-20 17:24:16 -0700293 public void setZoom(float v) {
294 mRollo.mState.zoom = v;
295 mRollo.mState.save();
296 mRollo.mInvokeZoom.execute();
297 }
298
Joe Onorato85a02a82009-09-08 12:34:22 -0700299 public void setScale(float amount) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400300 cancelLongPress();
Joe Onorato85a02a82009-09-08 12:34:22 -0700301 mRollo.mState.read();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400302 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700303 if (amount > 0.001f) {
304 mRollo.mState.visible = 1;
Jason Sams78aebd82009-09-15 13:06:59 -0700305 mRollo.mState.zoom = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700306 } else {
307 mRollo.mState.visible = 0;
308 mRollo.mState.zoom = 0;
309 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400310 if (amount > 0.001f && amount < 0.999f) {
311 mLocks |= LOCK_ZOOMING;
312 } else {
313 mLocks &= ~LOCK_ZOOMING;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700314 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400315 mRollo.mState.save();
Jason Samsfd22dac2009-09-20 17:24:16 -0700316 mRollo.mInvokeZoom.execute();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400317 }
318
319 public boolean isZooming() {
320 return (mLocks & LOCK_ZOOMING) != 0;
321 }
322
323 public boolean isVisible() {
324 return mRollo != null && mRollo.mState.visible != 0;
325 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700326
Joe Onorato93839052009-08-06 20:34:32 -0700327 @Override
328 public boolean onTrackballEvent(MotionEvent ev)
329 {
330 float x = ev.getX();
331 float y = ev.getY();
332 //Float tx = new Float(x);
333 //Float ty = new Float(y);
334 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
335
336
337 return true;
338 }
339
Joe Onorato9c1289c2009-08-17 11:03:03 -0400340 public void setApps(ArrayList<ApplicationInfo> list) {
341 mAllAppsList = list;
342 if (mRollo != null) {
343 mRollo.setApps(list);
344 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700345 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700346 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400347 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700348 }
349
350 private void invokeIcon(int index) {
351 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700352 }
353
354 private static int countPages(int iconCount) {
355 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
356 int pages = iconCount / iconsPerPage;
357 if (pages*iconsPerPage != iconCount) {
358 pages++;
359 }
360 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400361 }
362
Joe Onorato93839052009-08-06 20:34:32 -0700363 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700364
Joe Onorato1feb3a82009-08-08 22:32:00 -0700365 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700366
Joe Onorato93839052009-08-06 20:34:32 -0700367 private int mWidth;
368 private int mHeight;
369
370 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700371 private Script mScript;
Jason Sams86c87ed2009-09-18 13:55:55 -0700372
373 private Script.Invokable mInvokeMove;
374 private Script.Invokable mInvokeFling;
Jason Samsfd22dac2009-09-20 17:24:16 -0700375 private Script.Invokable mInvokeZoom;
Jason Sams86c87ed2009-09-18 13:55:55 -0700376
Joe Onorato93839052009-08-06 20:34:32 -0700377 private Sampler mSampler;
378 private Sampler mSamplerText;
379 private ProgramStore mPSBackground;
380 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700381 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700382 private ProgramFragment mPFImages;
383 private ProgramFragment mPFText;
384 private ProgramVertex mPV;
385 private ProgramVertex.MatrixAllocation mPVAlloc;
386 private ProgramVertex mPVOrtho;
387 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700388
Joe Onoratoc567acb2009-08-31 14:34:43 -0700389 private Allocation mScrollHandle;
390
Joe Onoratobf15cb42009-08-07 14:33:40 -0700391 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700392 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700393 private Allocation mAllocIconID;
394
Joe Onoratobf15cb42009-08-07 14:33:40 -0700395 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700396 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700397 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700398 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700399
Joe Onorato6665c0f2009-09-02 15:27:24 -0700400 private int[] mTouchYBorders;
401 private Allocation mAllocTouchYBorders;
402 private int[] mTouchXBorders;
403 private Allocation mAllocTouchXBorders;
404
405 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400406 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700407
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700408 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700409 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700410
Jason Sams78aebd82009-09-15 13:06:59 -0700411 class BaseAlloc {
412 Allocation mAlloc;
413 Type mType;
414
415 void save() {
416 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700417 }
Jason Sams78aebd82009-09-15 13:06:59 -0700418
419 void read() {
420 mAlloc.read(this);
421 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700422 }
423
Jason Sams78aebd82009-09-15 13:06:59 -0700424 class Params extends BaseAlloc {
425 Params() {
426 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
427 mAlloc = Allocation.createTyped(mRS, mType);
428 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700429 }
Jason Sams78aebd82009-09-15 13:06:59 -0700430 public int bubbleWidth;
431 public int bubbleHeight;
432 public int bubbleBitmapWidth;
433 public int bubbleBitmapHeight;
434 public int scrollHandleId;
435 public int scrollHandleTextureWidth;
436 public int scrollHandleTextureHeight;
437 }
438
439 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700440 public float newPositionX;
441 public int newTouchDown;
442 public float readPosX;
443 public float readVel;
444 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700445 public int iconCount;
446 public int scrollX;
Jason Sams78aebd82009-09-15 13:06:59 -0700447 public int selectedIconIndex = -1;
448 public int selectedIconTexture;
449 public int visible;
450 public float zoom;
451
452 State() {
453 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
454 mAlloc = Allocation.createTyped(mRS, mType);
455 save();
456 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700457 }
458
459 public RolloRS() {
460 }
461
462 public void init(Resources res, int width, int height) {
463 mRes = res;
464 mWidth = width;
465 mHeight = height;
466 initGl();
467 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700468 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700469 initRs();
470 }
471
472 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700473 Sampler.Builder sb = new Sampler.Builder(mRS);
474 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
475 sb.setMag(Sampler.Value.LINEAR);
476 sb.setWrapS(Sampler.Value.CLAMP);
477 sb.setWrapT(Sampler.Value.CLAMP);
478 mSampler = sb.create();
479
480 sb.setMin(Sampler.Value.NEAREST);
481 sb.setMag(Sampler.Value.NEAREST);
482 mSamplerText = sb.create();
483
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700484 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
485 mPFDebug = dbg.create();
486 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700487
488 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
489 bf.setTexEnable(true, 0);
490 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
491 mPFImages = bf.create();
492 mPFImages.setName("PF");
493 mPFImages.bindSampler(mSampler, 0);
494
495 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700496 //mPFText = bf.create();
497 mPFText = (new ProgramFragment.Builder(mRS, null, null)).create();
Joe Onorato93839052009-08-06 20:34:32 -0700498 mPFText.setName("PFText");
499 mPFText.bindSampler(mSamplerText, 0);
500
501 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700502 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700503 bs.setDitherEnable(false);
504 bs.setDepthMask(true);
505 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
506 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
507 mPSBackground = bs.create();
508 mPSBackground.setName("PFS");
509
510 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
511 bs.setDepthMask(false);
512 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
513 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
514 mPSText = bs.create();
515 mPSText.setName("PFSText");
516
517 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
518 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
519
520 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
521 mPV = pvb.create();
522 mPV.setName("PV");
523 mPV.bindAllocation(mPVAlloc);
524
525 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
526 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
527
528 pvb.setTextureMatrixEnable(true);
529 mPVOrtho = pvb.create();
530 mPVOrtho.setName("PVOrtho");
531 mPVOrtho.bindAllocation(mPVOrthoAlloc);
532
533 mRS.contextBindProgramVertex(mPV);
534
Joe Onorato6665c0f2009-09-02 15:27:24 -0700535 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
536 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32,
537 mTouchXBorders.length);
538 mAllocTouchXBorders.data(mTouchXBorders);
539
540 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
541 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32,
542 mTouchYBorders.length);
543 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onorato93839052009-08-06 20:34:32 -0700544
545 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700546 }
Jason Sams78aebd82009-09-15 13:06:59 -0700547
Joe Onorato1feb3a82009-08-08 22:32:00 -0700548 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700549 mParams = new Params();
550 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700551
Joe Onoratobf15cb42009-08-07 14:33:40 -0700552 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700553
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700554 mParams.bubbleWidth = bubble.getBubbleWidth();
555 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
556 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
557 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700558
Joe Onoratoc567acb2009-08-31 14:34:43 -0700559 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700560 R.drawable.all_apps_button_pow2, Element.RGBA_8888, false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700561 mScrollHandle.uploadToTexture(0);
562 mParams.scrollHandleId = mScrollHandle.getID();
563 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
564 mParams.scrollHandleTextureWidth = 128;
565 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700566
Joe Onoratoc567acb2009-08-31 14:34:43 -0700567
Joe Onorato1feb3a82009-08-08 22:32:00 -0700568 mParams.save();
569 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400570
Joe Onorato1291a8c2009-09-15 15:07:25 -0400571 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
572 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
573 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700574
575 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400576 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700577 }
578
Joe Onorato1feb3a82009-08-08 22:32:00 -0700579 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700580 ScriptC.Builder sb = new ScriptC.Builder(mRS);
581 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700582 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700583 sb.addDefines(Defines.class);
Jason Sams78aebd82009-09-15 13:06:59 -0700584 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
585 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams86c87ed2009-09-18 13:55:55 -0700586 mInvokeMove = sb.addInvokable("move");
587 mInvokeFling = sb.addInvokable("fling");
Jason Samsfd22dac2009-09-20 17:24:16 -0700588 mInvokeZoom = sb.addInvokable("setZoomTarget");
Joe Onorato93839052009-08-06 20:34:32 -0700589 mScript = sb.create();
590 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
591
Jason Sams78aebd82009-09-15 13:06:59 -0700592 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
593 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
Joe Onoratod769a632009-08-11 17:09:02 -0700594 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700595 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700596 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
597 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700598
599 mRS.contextBindRootScript(mScript);
600 }
Joe Onorato93839052009-08-06 20:34:32 -0700601
Joe Onorato9c1289c2009-08-17 11:03:03 -0400602 private void setApps(ArrayList<ApplicationInfo> list) {
603 final int count = list != null ? list.size() : 0;
604 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700605 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400606 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
607
608 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700609 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400610 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
611
612 Element ie8888 = Element.RGBA_8888;
613
614 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
615
616 for (int i=0; i<count; i++) {
617 final ApplicationInfo item = list.get(i);
618
619 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700620 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400621 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700622 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400623
624 mIcons[i].uploadToTexture(0);
625 mLabels[i].uploadToTexture(0);
626
Joe Onorato6665c0f2009-09-02 15:27:24 -0700627 mIconIds[i] = mIcons[i].getID();
628 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400629 }
630
Jason Samsbea62772009-09-21 19:34:13 -0700631 if(count > 0) {
632 mAllocIconID.data(mIconIds);
633 mAllocLabelID.data(mLabelIds);
634 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635
636 mState.iconCount = count;
637
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 if (mScript != null) { // wtf
639 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
640 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
641 }
642
643 mState.save();
644 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700645
646 void initTouchState() {
647 int width = getWidth();
648 int height = getHeight();
649
650 int iconsSize;
651 if (width < height) {
652 iconsSize = width;
653 } else {
654 iconsSize = height;
655 }
656 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
657 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
658
659 int centerY = (height / 2) - (int)(cellHeight * 0.35f);
660 mTouchYBorders[0] = centerY - (int)(2.4f * cellHeight);
661 mTouchYBorders[1] = centerY - (int)(1.15f * cellHeight);
662 mTouchYBorders[2] = centerY;
663 mTouchYBorders[3] = centerY + (int)(1.15f * cellHeight);;
664 mTouchYBorders[4] = centerY + (int)(2.4f * cellHeight);
665
666 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700667
Joe Onorato6665c0f2009-09-02 15:27:24 -0700668 int centerX = (width / 2);
669 mTouchXBorders[0] = centerX - (2 * cellWidth);
670 mTouchXBorders[1] = centerX - (int)(0.83f * cellWidth);;
671 mTouchXBorders[2] = centerX;
672 mTouchXBorders[3] = centerX + (int)(0.83f * cellWidth);;
673 mTouchXBorders[4] = centerX + (2 * cellWidth);
674
675 mAllocTouchXBorders.data(mTouchXBorders);
676 }
677
Jason Sams86c87ed2009-09-18 13:55:55 -0700678 int chooseTappedIcon(int x, int y, float page) {
679 int currentPage = (int)page;
680
Joe Onorato6665c0f2009-09-02 15:27:24 -0700681 int col = -1;
682 int row = -1;
683
684 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
685 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
686 col = i;
687 break;
688 }
689 }
690 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
691 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
692 row = i;
693 break;
694 }
695 }
696
697 if (row < 0 || col < 0) {
698 return -1;
699 }
700
Jason Sams78aebd82009-09-15 13:06:59 -0700701 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700702 + (row * Defines.ROWS_PER_PAGE) + col;
703 }
704
705 /**
706 * You need to call save() on mState on your own after calling this.
707 */
Jason Sams86c87ed2009-09-18 13:55:55 -0700708 void selectIcon(int x, int y, float pos) {
709 int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400710 selectIcon(index);
711 }
712
713 void selectIcon(int index) {
714 Log.d(TAG, "selectIcon index=" + index);
715 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700716 if (index < 0 || index >= iconCount) {
717 mState.selectedIconIndex = -1;
718 return;
719 } else {
720 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400721
722 Bitmap selectionBitmap = mSelectionBitmap;
723
724 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
725 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
726 mAllAppsList.get(index).iconBitmap);
727
728 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
729 Element.RGBA_8888, false);
730 mSelectedIcon.uploadToTexture(0);
731 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700732 }
733 }
734
735 /**
736 * You need to call save() on mState on your own after calling this.
737 */
738 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400739 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700740 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741 }
Joe Onorato93839052009-08-06 20:34:32 -0700742}
743
744