blob: 9967332dee7caaad494f6cce78c80a4f76536097 [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 Onoratoc567acb2009-08-31 14:34:43 -0700168 mTouchHandler = mFlingHandler;
169 /*
170 int action = ev.getAction();
171 if (action == MotionEvent.ACTION_DOWN) {
172 if (ev.getY() > mScrollHandleTop) {
173 mTouchHandler = mScrollHandler;
174 } else {
175 mTouchHandler = mFlingHandler;
176 }
Joe Onorato93839052009-08-06 20:34:32 -0700177 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700178 */
Joe Onorato6665c0f2009-09-02 15:27:24 -0700179 mTouchHandler.onTouchEvent(ev);
180
181 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700182 }
183
Joe Onoratoc567acb2009-08-31 14:34:43 -0700184 private abstract class TouchHandler {
185 abstract boolean onTouchEvent(MotionEvent ev);
186 };
187
188 private TouchHandler mFlingHandler = new TouchHandler() {
189 @Override
190 public boolean onTouchEvent(MotionEvent ev)
191 {
192 int x = (int)ev.getX();
193 int deltaX;
194 switch (ev.getAction()) {
195 case MotionEvent.ACTION_DOWN:
Joe Onorato5162ea92009-09-03 09:39:42 -0700196 mMotionDownRawX = (int)ev.getRawX();
197 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700198 mLastMotionX = x;
199 mRollo.mState.read();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700200 mRollo.mState.startScrollX = mRollo.mState.scrollX = mLastScrollX
201 = mRollo.mState.currentScrollX;
202 if (mRollo.mState.flingVelocityX != 0) {
203 mRollo.clearSelectedIcon();
204 } else {
205 mRollo.selectIcon(x, (int)ev.getY(), mRollo.mState.startScrollX,
206 (-mRollo.mState.startScrollX / Defines.SCREEN_WIDTH_PX));
207 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700208 mRollo.mState.flingVelocityX = 0;
209 mRollo.mState.adjustedDeceleration = 0;
210 mRollo.mState.save();
211 mVelocity = VelocityTracker.obtain();
212 mVelocity.addMovement(ev);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700213 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700214 break;
215 case MotionEvent.ACTION_MOVE:
216 case MotionEvent.ACTION_OUTSIDE:
Joe Onorato6665c0f2009-09-02 15:27:24 -0700217 int slop = Math.abs(x - mLastMotionX);
218 if (!mStartedScrolling && slop < mConfig.getScaledTouchSlop()) {
219 // don't update mLastMotionX so slop is right and when we do start scrolling
220 // below, we get the right delta.
221 } else {
222 mStartedScrolling = true;
223 mRollo.clearSelectedIcon();
224 deltaX = x - mLastMotionX;
225 mVelocity.addMovement(ev);
226 mRollo.mState.currentScrollX = mLastScrollX;
227 mLastScrollX += deltaX;
228 mRollo.mState.scrollX = mLastScrollX;
229 mRollo.mState.save();
230 mLastMotionX = x;
231 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700232 break;
233 case MotionEvent.ACTION_UP:
234 case MotionEvent.ACTION_CANCEL:
235 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
236 mConfig.getScaledMaximumFlingVelocity());
237 mRollo.mState.flingTimeMs = (int)SystemClock.uptimeMillis(); // TODO: use long
238 mRollo.mState.flingVelocityX = (int)mVelocity.getXVelocity();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700239 mRollo.clearSelectedIcon();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700240 mRollo.mState.save();
241 mLastMotionX = -10000;
242 mVelocity.recycle();
243 mVelocity = null;
244 break;
245 }
246 return true;
247 }
248 };
249
Joe Onorato6665c0f2009-09-02 15:27:24 -0700250 public void onClick(View v) {
251 int index = mRollo.mState.selectedIconIndex;
252 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
253 ApplicationInfo app = mAllAppsList.get(index);
254 mLauncher.startActivitySafely(app.intent);
255 }
256 }
257
258 public boolean onLongClick(View v) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700259 int index = mRollo.mState.selectedIconIndex;
260 Log.d(TAG, "long click! velocity=" + mRollo.mState.flingVelocityX + " index=" + index);
261 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
262 ApplicationInfo app = mAllAppsList.get(index);
263
264 // We don't really have an accurate location to use. This will do.
265 int screenX = mMotionDownRawX - (Defines.ICON_WIDTH_PX / 2);
266 int screenY = mMotionDownRawY - Defines.ICON_HEIGHT_PX;
267
268 int left = (Defines.ICON_TEXTURE_WIDTH_PX - Defines.ICON_WIDTH_PX) / 2;
269 int top = (Defines.ICON_TEXTURE_HEIGHT_PX - Defines.ICON_HEIGHT_PX) / 2;
270 mDragController.startDrag(app.iconBitmap, screenX, screenY,
271 left, top, Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
272 this, app, DragController.DRAG_ACTION_COPY);
273 // close me!
274 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700275 return true;
276 }
277
Joe Onorato5162ea92009-09-03 09:39:42 -0700278 public void setDragController(DragController dragger) {
279 mDragController = dragger;
280 }
281
282 public void onDropCompleted(View target, boolean success) {
283 }
284
Joe Onorato006b25f2009-09-03 11:38:43 -0700285 public void show() {
286 mRollo.mState.read();
287 mRollo.mState.visible = 1;
288 mRollo.mState.save();
289 }
290
291 public void hide(boolean animate) {
292 mRollo.mState.read();
293 mRollo.mState.visible = 0;
294 mRollo.mState.save();
295 }
296
Joe Onoratoc567acb2009-08-31 14:34:43 -0700297 /*
298 private TouchHandler mScrollHandler = new TouchHandler() {
299 @Override
300 public boolean onTouchEvent(MotionEvent ev)
301 {
302 int x = (int)ev.getX();
303 int w = getWidth();
304
305 float percent = x / (float)w;
306
307 mRollo.mState.read();
308
309 mRollo.mState.scrollX = mLastScrollX = -(int)(mPageCount * w * percent);
310 mRollo.mState.flingVelocityX = 0;
311 mRollo.mState.adjustedDeceleration = 0;
312 mRollo.mState.save();
313
314 return true;
315 }
316 };
317 */
318
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);
339 }
340
341 private void invokeIcon(int index) {
342 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700343 }
344
345 private static int countPages(int iconCount) {
346 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
347 int pages = iconCount / iconsPerPage;
348 if (pages*iconsPerPage != iconCount) {
349 pages++;
350 }
351 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400352 }
353
Joe Onorato93839052009-08-06 20:34:32 -0700354 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700355
Joe Onorato1feb3a82009-08-08 22:32:00 -0700356 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700357
Joe Onorato93839052009-08-06 20:34:32 -0700358 private int mWidth;
359 private int mHeight;
360
361 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700362 private Script mScript;
363 private Sampler mSampler;
364 private Sampler mSamplerText;
365 private ProgramStore mPSBackground;
366 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700367 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700368 private ProgramFragment mPFImages;
369 private ProgramFragment mPFText;
370 private ProgramVertex mPV;
371 private ProgramVertex.MatrixAllocation mPVAlloc;
372 private ProgramVertex mPVOrtho;
373 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700374
Joe Onoratoc567acb2009-08-31 14:34:43 -0700375 private Allocation mScrollHandle;
376
Joe Onoratobf15cb42009-08-07 14:33:40 -0700377 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700378 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700379 private Allocation mAllocIconID;
380
Joe Onoratobf15cb42009-08-07 14:33:40 -0700381 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700382 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700383 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700384 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700385
Joe Onorato6665c0f2009-09-02 15:27:24 -0700386 private int[] mTouchYBorders;
387 private Allocation mAllocTouchYBorders;
388 private int[] mTouchXBorders;
389 private Allocation mAllocTouchXBorders;
390
391 private Bitmap mSelectionBitmap;
Joe Onorato93839052009-08-06 20:34:32 -0700392
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700393 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700394 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700395
396 class Params extends IntAllocation {
397 Params(RenderScript rs) {
398 super(rs);
399 }
400 @AllocationIndex(0) public int bubbleWidth;
401 @AllocationIndex(1) public int bubbleHeight;
402 @AllocationIndex(2) public int bubbleBitmapWidth;
403 @AllocationIndex(3) public int bubbleBitmapHeight;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700404 @AllocationIndex(4) public int scrollHandleId;
405 @AllocationIndex(5) public int scrollHandleTextureWidth;
406 @AllocationIndex(6) public int scrollHandleTextureHeight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700407 }
408
Joe Onorato1feb3a82009-08-08 22:32:00 -0700409 class State extends IntAllocation {
410 State(RenderScript rs) {
411 super(rs);
412 }
413 @AllocationIndex(0) public int iconCount;
414 @AllocationIndex(1) public int scrollX;
Joe Onoratod769a632009-08-11 17:09:02 -0700415 @AllocationIndex(2) public int flingTimeMs;
416 @AllocationIndex(3) public int flingVelocityX;
417 @AllocationIndex(4) public int adjustedDeceleration;
418 @AllocationIndex(5) public int currentScrollX;
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700419 @AllocationIndex(6) public int flingDuration;
420 @AllocationIndex(7) public int flingEndPos;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700421 @AllocationIndex(8) public int startScrollX;
422 @AllocationIndex(9) public int selectedIconIndex = -1;
423 @AllocationIndex(10) public int selectedIconTexture;
Joe Onorato006b25f2009-09-03 11:38:43 -0700424 @AllocationIndex(11) public int visible;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700425 }
426
427 public RolloRS() {
428 }
429
430 public void init(Resources res, int width, int height) {
431 mRes = res;
432 mWidth = width;
433 mHeight = height;
434 initGl();
435 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700436 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700437 initRs();
438 }
439
440 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700441 Sampler.Builder sb = new Sampler.Builder(mRS);
442 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
443 sb.setMag(Sampler.Value.LINEAR);
444 sb.setWrapS(Sampler.Value.CLAMP);
445 sb.setWrapT(Sampler.Value.CLAMP);
446 mSampler = sb.create();
447
448 sb.setMin(Sampler.Value.NEAREST);
449 sb.setMag(Sampler.Value.NEAREST);
450 mSamplerText = sb.create();
451
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700452 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
453 mPFDebug = dbg.create();
454 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700455
456 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
457 bf.setTexEnable(true, 0);
458 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
459 mPFImages = bf.create();
460 mPFImages.setName("PF");
461 mPFImages.bindSampler(mSampler, 0);
462
463 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700464 //mPFText = bf.create();
465 mPFText = (new ProgramFragment.Builder(mRS, null, null)).create();
Joe Onorato93839052009-08-06 20:34:32 -0700466 mPFText.setName("PFText");
467 mPFText.bindSampler(mSamplerText, 0);
468
469 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700470 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700471 bs.setDitherEnable(false);
472 bs.setDepthMask(true);
473 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
474 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
475 mPSBackground = bs.create();
476 mPSBackground.setName("PFS");
477
478 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
479 bs.setDepthMask(false);
480 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
481 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
482 mPSText = bs.create();
483 mPSText.setName("PFSText");
484
485 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
486 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
487
488 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
489 mPV = pvb.create();
490 mPV.setName("PV");
491 mPV.bindAllocation(mPVAlloc);
492
493 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
494 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
495
496 pvb.setTextureMatrixEnable(true);
497 mPVOrtho = pvb.create();
498 mPVOrtho.setName("PVOrtho");
499 mPVOrtho.bindAllocation(mPVOrthoAlloc);
500
501 mRS.contextBindProgramVertex(mPV);
502
Joe Onorato6665c0f2009-09-02 15:27:24 -0700503 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
504 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32,
505 mTouchXBorders.length);
506 mAllocTouchXBorders.data(mTouchXBorders);
507
508 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
509 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32,
510 mTouchYBorders.length);
511 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onorato93839052009-08-06 20:34:32 -0700512
513 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700514 }
515
Joe Onorato1feb3a82009-08-08 22:32:00 -0700516 private void initData() {
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700517 mParams = new Params(mRS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700518 mState = new State(mRS);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700519
Joe Onoratobf15cb42009-08-07 14:33:40 -0700520 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700521
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700522 mParams.bubbleWidth = bubble.getBubbleWidth();
523 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
524 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
525 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700526
Joe Onoratoc567acb2009-08-31 14:34:43 -0700527 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700528 R.drawable.all_apps_button_pow2, Element.RGBA_8888, false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700529 mScrollHandle.uploadToTexture(0);
530 mParams.scrollHandleId = mScrollHandle.getID();
531 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
532 mParams.scrollHandleTextureWidth = 128;
533 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700534
Joe Onoratoc567acb2009-08-31 14:34:43 -0700535
Joe Onorato1feb3a82009-08-08 22:32:00 -0700536 mParams.save();
537 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538
Joe Onorato6665c0f2009-09-02 15:27:24 -0700539 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
540 Bitmap.Config.ARGB_8888);
541 Bitmap selectionBitmap = mSelectionBitmap;
542 Paint paint = new Paint();
543 float radius = 12 * getContext().getResources().getDisplayMetrics().density;
544 //paint.setMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.OUTER));
545 Canvas canvas = new Canvas(selectionBitmap);
546 canvas.drawColor(0xffff0000);
547
548 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
549 Element.RGBA_8888, false);
550 mSelectedIcon.uploadToTexture(0);
551
552 mState.selectedIconTexture = mSelectedIcon.getID();
553
554 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400555 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700556 }
557
Joe Onorato1feb3a82009-08-08 22:32:00 -0700558 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700559 ScriptC.Builder sb = new ScriptC.Builder(mRS);
560 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700561 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700562 sb.addDefines(Defines.class);
Joe Onorato93839052009-08-06 20:34:32 -0700563 mScript = sb.create();
564 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
565
Joe Onoratod769a632009-08-11 17:09:02 -0700566 mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
567 mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
568 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700569 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700570 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
571 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700572
573 mRS.contextBindRootScript(mScript);
574 }
Joe Onorato93839052009-08-06 20:34:32 -0700575
Joe Onorato9c1289c2009-08-17 11:03:03 -0400576 private void setApps(ArrayList<ApplicationInfo> list) {
577 final int count = list != null ? list.size() : 0;
578 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700579 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400580 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
581
582 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700583 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400584 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
585
586 Element ie8888 = Element.RGBA_8888;
587
588 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
589
590 for (int i=0; i<count; i++) {
591 final ApplicationInfo item = list.get(i);
592
593 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700594 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400595 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700596 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400597
598 mIcons[i].uploadToTexture(0);
599 mLabels[i].uploadToTexture(0);
600
Joe Onorato6665c0f2009-09-02 15:27:24 -0700601 mIconIds[i] = mIcons[i].getID();
602 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400603 }
604
Joe Onorato6665c0f2009-09-02 15:27:24 -0700605 mAllocIconID.data(mIconIds);
606 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400607
608 mState.iconCount = count;
609
610 Log.d("AllAppsView", "mScript=" + mScript + " mAllocIconID=" + mAllocIconID);
611
612 if (mScript != null) { // wtf
613 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
614 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
615 }
616
617 mState.save();
618 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700619
620 void initTouchState() {
621 int width = getWidth();
622 int height = getHeight();
623
624 int iconsSize;
625 if (width < height) {
626 iconsSize = width;
627 } else {
628 iconsSize = height;
629 }
630 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
631 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
632
633 int centerY = (height / 2) - (int)(cellHeight * 0.35f);
634 mTouchYBorders[0] = centerY - (int)(2.4f * cellHeight);
635 mTouchYBorders[1] = centerY - (int)(1.15f * cellHeight);
636 mTouchYBorders[2] = centerY;
637 mTouchYBorders[3] = centerY + (int)(1.15f * cellHeight);;
638 mTouchYBorders[4] = centerY + (int)(2.4f * cellHeight);
639
640 mAllocTouchYBorders.data(mTouchYBorders);
641
642 int centerX = (width / 2);
643 mTouchXBorders[0] = centerX - (2 * cellWidth);
644 mTouchXBorders[1] = centerX - (int)(0.83f * cellWidth);;
645 mTouchXBorders[2] = centerX;
646 mTouchXBorders[3] = centerX + (int)(0.83f * cellWidth);;
647 mTouchXBorders[4] = centerX + (2 * cellWidth);
648
649 mAllocTouchXBorders.data(mTouchXBorders);
650 }
651
652 int chooseTappedIcon(int x, int y, int scrollX, int currentPage) {
653 int col = -1;
654 int row = -1;
655
656 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
657 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
658 col = i;
659 break;
660 }
661 }
662 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
663 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
664 row = i;
665 break;
666 }
667 }
668
669 if (row < 0 || col < 0) {
670 return -1;
671 }
672
673 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
674 + (row * Defines.ROWS_PER_PAGE) + col;
675 }
676
677 /**
678 * You need to call save() on mState on your own after calling this.
679 */
680 void selectIcon(int x, int y, int scrollX, int currentPage) {
681 int iconCount = mAllAppsList.size();
682 int index = chooseTappedIcon(x, y, scrollX, currentPage);
683 if (index < 0 || index >= iconCount) {
684 mState.selectedIconIndex = -1;
685 return;
686 } else {
687 mState.selectedIconIndex = index;
688 }
689 }
690
691 /**
692 * You need to call save() on mState on your own after calling this.
693 */
694 void clearSelectedIcon() {
695 mState.selectedIconIndex = -1;
696 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697 }
Joe Onorato93839052009-08-06 20:34:32 -0700698}
699
700