blob: dcaaaa118235557ef26b3e900d8f473ec5ea6dbb [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;
31import android.renderscript.Script;
32import android.renderscript.ScriptC;
33import android.renderscript.ProgramFragment;
34import android.renderscript.ProgramStore;
35import android.renderscript.Sampler;
36
37import android.content.Context;
38import android.content.res.Resources;
Joe Onorato7c312c12009-08-13 21:36:53 -070039import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070040import android.graphics.Bitmap;
41import android.graphics.BitmapFactory;
Joe Onorato6665c0f2009-09-02 15:27:24 -070042import android.graphics.BlurMaskFilter;
Joe Onorato93839052009-08-06 20:34:32 -070043import 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 Onorato6665c0f2009-09-02 15:27:24 -070067 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070068 private DragController mDragController;
Joe Onorato6665c0f2009-09-02 15:27:24 -070069
Joe Onorato1feb3a82009-08-08 22:32:00 -070070 private RenderScript mRS;
71 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040072 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070073
Joe Onoratod769a632009-08-11 17:09:02 -070074 private ViewConfiguration mConfig;
Joe Onoratoc567acb2009-08-31 14:34:43 -070075 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070076 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070077 private VelocityTracker mVelocity;
78 private int mLastScrollX;
Joe Onorato1feb3a82009-08-08 22:32:00 -070079 private int mLastMotionX;
Joe Onorato5162ea92009-09-03 09:39:42 -070080 private int mMotionDownRawX;
81 private int mMotionDownRawY;
Joe Onoratoc567acb2009-08-31 14:34:43 -070082 private TouchHandler mTouchHandler;
83 private int mScrollHandleTop;
Joe Onorato1feb3a82009-08-08 22:32:00 -070084
Joe Onorato6665c0f2009-09-02 15:27:24 -070085 static class Defines {
86 private static float farSize(float sizeAt0) {
87 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
88 }
89
Joe Onoratoc567acb2009-08-31 14:34:43 -070090 public static final int ALLOC_PARAMS = 0;
91 public static final int ALLOC_STATE = 1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070092 public static final int ALLOC_ICON_IDS = 2;
93 public static final int ALLOC_LABEL_IDS = 3;
94 public static final int ALLOC_X_BORDERS = 4;
95 public static final int ALLOC_Y_BORDERS = 5;
Joe Onoratoc567acb2009-08-31 14:34:43 -070096
97 public static final int COLUMNS_PER_PAGE = 4;
98 public static final int ROWS_PER_PAGE = 4;
99
Joe Onorato6665c0f2009-09-02 15:27:24 -0700100 public static final float RADIUS = 4.0f;
101
102 public static final int SCREEN_WIDTH_PX = 480;
103 public static final int SCREEN_HEIGHT_PX = 854;
104
105 public static final int ICON_WIDTH_PX = 64;
106 public static final int ICON_TEXTURE_WIDTH_PX = 128;
107
108 public static final int ICON_HEIGHT_PX = 64;
109 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
110 public static final float ICON_TOP_OFFSET = 0.2f;
111
112 public static final float CAMERA_Z = -2;
113 public static final float FAR_ICON_SIZE
114 = farSize(2 * ICON_WIDTH_PX / (float)SCREEN_WIDTH_PX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700115 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700116
117 public AllAppsView(Context context, AttributeSet attrs) {
118 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700119 setFocusable(true);
120 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratod769a632009-08-11 17:09:02 -0700121 mConfig = ViewConfiguration.get(context);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700122 setOnClickListener(this);
123 setOnLongClickListener(this);
Joe Onorato93839052009-08-06 20:34:32 -0700124 }
125
Joe Onorato7c312c12009-08-13 21:36:53 -0700126 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
127 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700128 }
129
Joe Onorato6665c0f2009-09-02 15:27:24 -0700130 public void setLauncher(Launcher launcher) {
131 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700132 }
133
Joe Onorato1feb3a82009-08-08 22:32:00 -0700134 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700135 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
136 super.surfaceChanged(holder, format, w, h);
137
Joe Onorato6665c0f2009-09-02 15:27:24 -0700138 long startTime = SystemClock.uptimeMillis();
139
Jason Samsb58cbdc2009-08-21 16:56:58 -0700140 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700141 mRollo = new RolloRS();
142 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400143 if (mAllAppsList != null) {
144 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700145 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400146 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700147
148 Resources res = getContext().getResources();
149 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
150 mScrollHandleTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700151
152 long endTime = SystemClock.uptimeMillis();
153 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700154 }
155
156 @Override
157 public boolean onKeyDown(int keyCode, KeyEvent event)
158 {
Joe Onorato93839052009-08-06 20:34:32 -0700159 // this method doesn't work when 'extends View' include 'extends ScrollView'.
160 return super.onKeyDown(keyCode, event);
161 }
162
Joe Onorato93839052009-08-06 20:34:32 -0700163 @Override
164 public boolean onTouchEvent(MotionEvent ev)
165 {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700166 super.onTouchEvent(ev);
167
Joe Onoratoe3406a22009-09-03 14:36:25 -0700168 if (mRollo.mState.visible == 0) {
169 return false;
170 }
171
Joe Onoratoc567acb2009-08-31 14:34:43 -0700172 mTouchHandler = mFlingHandler;
173 /*
174 int action = ev.getAction();
175 if (action == MotionEvent.ACTION_DOWN) {
176 if (ev.getY() > mScrollHandleTop) {
177 mTouchHandler = mScrollHandler;
178 } else {
179 mTouchHandler = mFlingHandler;
180 }
Joe Onorato93839052009-08-06 20:34:32 -0700181 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700182 */
Joe Onorato6665c0f2009-09-02 15:27:24 -0700183 mTouchHandler.onTouchEvent(ev);
184
185 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700186 }
187
Joe Onoratoc567acb2009-08-31 14:34:43 -0700188 private abstract class TouchHandler {
189 abstract boolean onTouchEvent(MotionEvent ev);
190 };
191
192 private TouchHandler mFlingHandler = new TouchHandler() {
193 @Override
194 public boolean onTouchEvent(MotionEvent ev)
195 {
196 int x = (int)ev.getX();
197 int deltaX;
198 switch (ev.getAction()) {
199 case MotionEvent.ACTION_DOWN:
Joe Onorato5162ea92009-09-03 09:39:42 -0700200 mMotionDownRawX = (int)ev.getRawX();
201 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700202 mLastMotionX = x;
203 mRollo.mState.read();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700204 mRollo.mState.startScrollX = mRollo.mState.scrollX = mLastScrollX
205 = mRollo.mState.currentScrollX;
206 if (mRollo.mState.flingVelocityX != 0) {
207 mRollo.clearSelectedIcon();
208 } else {
209 mRollo.selectIcon(x, (int)ev.getY(), mRollo.mState.startScrollX,
210 (-mRollo.mState.startScrollX / Defines.SCREEN_WIDTH_PX));
211 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700212 mRollo.mState.flingVelocityX = 0;
213 mRollo.mState.adjustedDeceleration = 0;
214 mRollo.mState.save();
215 mVelocity = VelocityTracker.obtain();
216 mVelocity.addMovement(ev);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700217 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700218 break;
219 case MotionEvent.ACTION_MOVE:
220 case MotionEvent.ACTION_OUTSIDE:
Joe Onorato6665c0f2009-09-02 15:27:24 -0700221 int slop = Math.abs(x - mLastMotionX);
222 if (!mStartedScrolling && slop < mConfig.getScaledTouchSlop()) {
223 // don't update mLastMotionX so slop is right and when we do start scrolling
224 // below, we get the right delta.
225 } else {
226 mStartedScrolling = true;
227 mRollo.clearSelectedIcon();
228 deltaX = x - mLastMotionX;
229 mVelocity.addMovement(ev);
230 mRollo.mState.currentScrollX = mLastScrollX;
231 mLastScrollX += deltaX;
232 mRollo.mState.scrollX = mLastScrollX;
233 mRollo.mState.save();
234 mLastMotionX = x;
235 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700236 break;
237 case MotionEvent.ACTION_UP:
238 case MotionEvent.ACTION_CANCEL:
239 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
240 mConfig.getScaledMaximumFlingVelocity());
241 mRollo.mState.flingTimeMs = (int)SystemClock.uptimeMillis(); // TODO: use long
242 mRollo.mState.flingVelocityX = (int)mVelocity.getXVelocity();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700243 mRollo.clearSelectedIcon();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700244 mRollo.mState.save();
245 mLastMotionX = -10000;
246 mVelocity.recycle();
247 mVelocity = null;
248 break;
249 }
250 return true;
251 }
252 };
253
Joe Onorato6665c0f2009-09-02 15:27:24 -0700254 public void onClick(View v) {
255 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 Onorato5162ea92009-09-03 09:39:42 -0700263 int index = mRollo.mState.selectedIconIndex;
264 Log.d(TAG, "long click! velocity=" + mRollo.mState.flingVelocityX + " index=" + index);
265 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
266 ApplicationInfo app = mAllAppsList.get(index);
267
268 // We don't really have an accurate location to use. This will do.
269 int screenX = mMotionDownRawX - (Defines.ICON_WIDTH_PX / 2);
270 int screenY = mMotionDownRawY - Defines.ICON_HEIGHT_PX;
271
272 int left = (Defines.ICON_TEXTURE_WIDTH_PX - Defines.ICON_WIDTH_PX) / 2;
273 int top = (Defines.ICON_TEXTURE_HEIGHT_PX - Defines.ICON_HEIGHT_PX) / 2;
274 mDragController.startDrag(app.iconBitmap, screenX, screenY,
275 left, top, Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
276 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700277
278 mLauncher.closeAllAppsDialog(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700279 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700280 return true;
281 }
282
Joe Onorato5162ea92009-09-03 09:39:42 -0700283 public void setDragController(DragController dragger) {
284 mDragController = dragger;
285 }
286
287 public void onDropCompleted(View target, boolean success) {
288 }
289
Joe Onorato006b25f2009-09-03 11:38:43 -0700290 public void show() {
291 mRollo.mState.read();
292 mRollo.mState.visible = 1;
293 mRollo.mState.save();
294 }
295
296 public void hide(boolean animate) {
297 mRollo.mState.read();
298 mRollo.mState.visible = 0;
299 mRollo.mState.save();
300 }
301
Joe Onoratoc567acb2009-08-31 14:34:43 -0700302 /*
303 private TouchHandler mScrollHandler = new TouchHandler() {
304 @Override
305 public boolean onTouchEvent(MotionEvent ev)
306 {
307 int x = (int)ev.getX();
308 int w = getWidth();
309
310 float percent = x / (float)w;
311
312 mRollo.mState.read();
313
314 mRollo.mState.scrollX = mLastScrollX = -(int)(mPageCount * w * percent);
315 mRollo.mState.flingVelocityX = 0;
316 mRollo.mState.adjustedDeceleration = 0;
317 mRollo.mState.save();
318
319 return true;
320 }
321 };
322 */
323
Joe Onorato93839052009-08-06 20:34:32 -0700324 @Override
325 public boolean onTrackballEvent(MotionEvent ev)
326 {
327 float x = ev.getX();
328 float y = ev.getY();
329 //Float tx = new Float(x);
330 //Float ty = new Float(y);
331 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
332
333
334 return true;
335 }
336
Joe Onorato9c1289c2009-08-17 11:03:03 -0400337 public void setApps(ArrayList<ApplicationInfo> list) {
338 mAllAppsList = list;
339 if (mRollo != null) {
340 mRollo.setApps(list);
341 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700342 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700343 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
344 }
345
346 private void invokeIcon(int index) {
347 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700348 }
349
350 private static int countPages(int iconCount) {
351 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
352 int pages = iconCount / iconsPerPage;
353 if (pages*iconsPerPage != iconCount) {
354 pages++;
355 }
356 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400357 }
358
Joe Onorato93839052009-08-06 20:34:32 -0700359 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700360
Joe Onorato1feb3a82009-08-08 22:32:00 -0700361 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700362
Joe Onorato93839052009-08-06 20:34:32 -0700363 private int mWidth;
364 private int mHeight;
365
366 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700367 private Script mScript;
368 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 Onorato93839052009-08-06 20:34:32 -0700397
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700398 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700399 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700400
401 class Params extends IntAllocation {
402 Params(RenderScript rs) {
403 super(rs);
404 }
405 @AllocationIndex(0) public int bubbleWidth;
406 @AllocationIndex(1) public int bubbleHeight;
407 @AllocationIndex(2) public int bubbleBitmapWidth;
408 @AllocationIndex(3) public int bubbleBitmapHeight;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700409 @AllocationIndex(4) public int scrollHandleId;
410 @AllocationIndex(5) public int scrollHandleTextureWidth;
411 @AllocationIndex(6) public int scrollHandleTextureHeight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700412 }
413
Joe Onorato1feb3a82009-08-08 22:32:00 -0700414 class State extends IntAllocation {
415 State(RenderScript rs) {
416 super(rs);
417 }
418 @AllocationIndex(0) public int iconCount;
419 @AllocationIndex(1) public int scrollX;
Joe Onoratod769a632009-08-11 17:09:02 -0700420 @AllocationIndex(2) public int flingTimeMs;
421 @AllocationIndex(3) public int flingVelocityX;
422 @AllocationIndex(4) public int adjustedDeceleration;
423 @AllocationIndex(5) public int currentScrollX;
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700424 @AllocationIndex(6) public int flingDuration;
425 @AllocationIndex(7) public int flingEndPos;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700426 @AllocationIndex(8) public int startScrollX;
427 @AllocationIndex(9) public int selectedIconIndex = -1;
428 @AllocationIndex(10) public int selectedIconTexture;
Joe Onorato006b25f2009-09-03 11:38:43 -0700429 @AllocationIndex(11) public int visible;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700430 }
431
432 public RolloRS() {
433 }
434
435 public void init(Resources res, int width, int height) {
436 mRes = res;
437 mWidth = width;
438 mHeight = height;
439 initGl();
440 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700441 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700442 initRs();
443 }
444
445 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700446 Sampler.Builder sb = new Sampler.Builder(mRS);
447 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
448 sb.setMag(Sampler.Value.LINEAR);
449 sb.setWrapS(Sampler.Value.CLAMP);
450 sb.setWrapT(Sampler.Value.CLAMP);
451 mSampler = sb.create();
452
453 sb.setMin(Sampler.Value.NEAREST);
454 sb.setMag(Sampler.Value.NEAREST);
455 mSamplerText = sb.create();
456
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700457 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
458 mPFDebug = dbg.create();
459 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700460
461 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
462 bf.setTexEnable(true, 0);
463 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
464 mPFImages = bf.create();
465 mPFImages.setName("PF");
466 mPFImages.bindSampler(mSampler, 0);
467
468 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700469 //mPFText = bf.create();
470 mPFText = (new ProgramFragment.Builder(mRS, null, null)).create();
Joe Onorato93839052009-08-06 20:34:32 -0700471 mPFText.setName("PFText");
472 mPFText.bindSampler(mSamplerText, 0);
473
474 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700475 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700476 bs.setDitherEnable(false);
477 bs.setDepthMask(true);
478 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
479 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
480 mPSBackground = bs.create();
481 mPSBackground.setName("PFS");
482
483 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
484 bs.setDepthMask(false);
485 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
486 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
487 mPSText = bs.create();
488 mPSText.setName("PFSText");
489
490 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
491 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
492
493 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
494 mPV = pvb.create();
495 mPV.setName("PV");
496 mPV.bindAllocation(mPVAlloc);
497
498 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
499 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
500
501 pvb.setTextureMatrixEnable(true);
502 mPVOrtho = pvb.create();
503 mPVOrtho.setName("PVOrtho");
504 mPVOrtho.bindAllocation(mPVOrthoAlloc);
505
506 mRS.contextBindProgramVertex(mPV);
507
Joe Onorato6665c0f2009-09-02 15:27:24 -0700508 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
509 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32,
510 mTouchXBorders.length);
511 mAllocTouchXBorders.data(mTouchXBorders);
512
513 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
514 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32,
515 mTouchYBorders.length);
516 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onorato93839052009-08-06 20:34:32 -0700517
518 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700519 }
520
Joe Onorato1feb3a82009-08-08 22:32:00 -0700521 private void initData() {
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700522 mParams = new Params(mRS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700523 mState = new State(mRS);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700524
Joe Onoratobf15cb42009-08-07 14:33:40 -0700525 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700526
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700527 mParams.bubbleWidth = bubble.getBubbleWidth();
528 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
529 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
530 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700531
Joe Onoratoc567acb2009-08-31 14:34:43 -0700532 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700533 R.drawable.all_apps_button_pow2, Element.RGBA_8888, false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700534 mScrollHandle.uploadToTexture(0);
535 mParams.scrollHandleId = mScrollHandle.getID();
536 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
537 mParams.scrollHandleTextureWidth = 128;
538 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700539
Joe Onoratoc567acb2009-08-31 14:34:43 -0700540
Joe Onorato1feb3a82009-08-08 22:32:00 -0700541 mParams.save();
542 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400543
Joe Onorato6665c0f2009-09-02 15:27:24 -0700544 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
545 Bitmap.Config.ARGB_8888);
546 Bitmap selectionBitmap = mSelectionBitmap;
547 Paint paint = new Paint();
548 float radius = 12 * getContext().getResources().getDisplayMetrics().density;
549 //paint.setMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.OUTER));
550 Canvas canvas = new Canvas(selectionBitmap);
551 canvas.drawColor(0xffff0000);
552
553 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
554 Element.RGBA_8888, false);
555 mSelectedIcon.uploadToTexture(0);
556
557 mState.selectedIconTexture = mSelectedIcon.getID();
558
559 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700561 }
562
Joe Onorato1feb3a82009-08-08 22:32:00 -0700563 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700564 ScriptC.Builder sb = new ScriptC.Builder(mRS);
565 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700566 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700567 sb.addDefines(Defines.class);
Joe Onorato93839052009-08-06 20:34:32 -0700568 mScript = sb.create();
569 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
570
Joe Onoratod769a632009-08-11 17:09:02 -0700571 mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
572 mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
573 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700574 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700575 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
576 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700577
578 mRS.contextBindRootScript(mScript);
579 }
Joe Onorato93839052009-08-06 20:34:32 -0700580
Joe Onorato9c1289c2009-08-17 11:03:03 -0400581 private void setApps(ArrayList<ApplicationInfo> list) {
582 final int count = list != null ? list.size() : 0;
583 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700584 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400585 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
586
587 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700588 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400589 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
590
591 Element ie8888 = Element.RGBA_8888;
592
593 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
594
595 for (int i=0; i<count; i++) {
596 final ApplicationInfo item = list.get(i);
597
598 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700599 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400600 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700601 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400602
603 mIcons[i].uploadToTexture(0);
604 mLabels[i].uploadToTexture(0);
605
Joe Onorato6665c0f2009-09-02 15:27:24 -0700606 mIconIds[i] = mIcons[i].getID();
607 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400608 }
609
Joe Onorato6665c0f2009-09-02 15:27:24 -0700610 mAllocIconID.data(mIconIds);
611 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400612
613 mState.iconCount = count;
614
615 Log.d("AllAppsView", "mScript=" + mScript + " mAllocIconID=" + mAllocIconID);
616
617 if (mScript != null) { // wtf
618 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
619 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
620 }
621
622 mState.save();
623 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700624
625 void initTouchState() {
626 int width = getWidth();
627 int height = getHeight();
628
629 int iconsSize;
630 if (width < height) {
631 iconsSize = width;
632 } else {
633 iconsSize = height;
634 }
635 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
636 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
637
638 int centerY = (height / 2) - (int)(cellHeight * 0.35f);
639 mTouchYBorders[0] = centerY - (int)(2.4f * cellHeight);
640 mTouchYBorders[1] = centerY - (int)(1.15f * cellHeight);
641 mTouchYBorders[2] = centerY;
642 mTouchYBorders[3] = centerY + (int)(1.15f * cellHeight);;
643 mTouchYBorders[4] = centerY + (int)(2.4f * cellHeight);
644
645 mAllocTouchYBorders.data(mTouchYBorders);
646
647 int centerX = (width / 2);
648 mTouchXBorders[0] = centerX - (2 * cellWidth);
649 mTouchXBorders[1] = centerX - (int)(0.83f * cellWidth);;
650 mTouchXBorders[2] = centerX;
651 mTouchXBorders[3] = centerX + (int)(0.83f * cellWidth);;
652 mTouchXBorders[4] = centerX + (2 * cellWidth);
653
654 mAllocTouchXBorders.data(mTouchXBorders);
655 }
656
657 int chooseTappedIcon(int x, int y, int scrollX, int currentPage) {
658 int col = -1;
659 int row = -1;
660
661 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
662 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
663 col = i;
664 break;
665 }
666 }
667 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
668 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
669 row = i;
670 break;
671 }
672 }
673
674 if (row < 0 || col < 0) {
675 return -1;
676 }
677
678 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
679 + (row * Defines.ROWS_PER_PAGE) + col;
680 }
681
682 /**
683 * You need to call save() on mState on your own after calling this.
684 */
685 void selectIcon(int x, int y, int scrollX, int currentPage) {
686 int iconCount = mAllAppsList.size();
687 int index = chooseTappedIcon(x, y, scrollX, currentPage);
688 if (index < 0 || index >= iconCount) {
689 mState.selectedIconIndex = -1;
690 return;
691 } else {
692 mState.selectedIconIndex = index;
693 }
694 }
695
696 /**
697 * You need to call save() on mState on your own after calling this.
698 */
699 void clearSelectedIcon() {
700 mState.selectedIconIndex = -1;
701 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400702 }
Joe Onorato93839052009-08-06 20:34:32 -0700703}
704
705