blob: 375fc12c713260d63360e31965cedf68e31ed794 [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 Onoratoc567acb2009-08-31 14:34:43 -0700285 /*
286 private TouchHandler mScrollHandler = new TouchHandler() {
287 @Override
288 public boolean onTouchEvent(MotionEvent ev)
289 {
290 int x = (int)ev.getX();
291 int w = getWidth();
292
293 float percent = x / (float)w;
294
295 mRollo.mState.read();
296
297 mRollo.mState.scrollX = mLastScrollX = -(int)(mPageCount * w * percent);
298 mRollo.mState.flingVelocityX = 0;
299 mRollo.mState.adjustedDeceleration = 0;
300 mRollo.mState.save();
301
302 return true;
303 }
304 };
305 */
306
Joe Onorato93839052009-08-06 20:34:32 -0700307 @Override
308 public boolean onTrackballEvent(MotionEvent ev)
309 {
310 float x = ev.getX();
311 float y = ev.getY();
312 //Float tx = new Float(x);
313 //Float ty = new Float(y);
314 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
315
316
317 return true;
318 }
319
Joe Onorato9c1289c2009-08-17 11:03:03 -0400320 public void setApps(ArrayList<ApplicationInfo> list) {
321 mAllAppsList = list;
322 if (mRollo != null) {
323 mRollo.setApps(list);
324 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700325 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700326 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
327 }
328
329 private void invokeIcon(int index) {
330 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700331 }
332
333 private static int countPages(int iconCount) {
334 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
335 int pages = iconCount / iconsPerPage;
336 if (pages*iconsPerPage != iconCount) {
337 pages++;
338 }
339 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400340 }
341
Joe Onorato93839052009-08-06 20:34:32 -0700342 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700343
Joe Onorato1feb3a82009-08-08 22:32:00 -0700344 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700345
Joe Onorato93839052009-08-06 20:34:32 -0700346 private int mWidth;
347 private int mHeight;
348
349 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700350 private Script mScript;
351 private Sampler mSampler;
352 private Sampler mSamplerText;
353 private ProgramStore mPSBackground;
354 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700355 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700356 private ProgramFragment mPFImages;
357 private ProgramFragment mPFText;
358 private ProgramVertex mPV;
359 private ProgramVertex.MatrixAllocation mPVAlloc;
360 private ProgramVertex mPVOrtho;
361 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700362
Joe Onoratoc567acb2009-08-31 14:34:43 -0700363 private Allocation mScrollHandle;
364
Joe Onoratobf15cb42009-08-07 14:33:40 -0700365 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700366 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700367 private Allocation mAllocIconID;
368
Joe Onoratobf15cb42009-08-07 14:33:40 -0700369 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700370 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700371 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700372 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700373
Joe Onorato6665c0f2009-09-02 15:27:24 -0700374 private int[] mTouchYBorders;
375 private Allocation mAllocTouchYBorders;
376 private int[] mTouchXBorders;
377 private Allocation mAllocTouchXBorders;
378
379 private Bitmap mSelectionBitmap;
Joe Onorato93839052009-08-06 20:34:32 -0700380
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700381 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700382 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700383
384 class Params extends IntAllocation {
385 Params(RenderScript rs) {
386 super(rs);
387 }
388 @AllocationIndex(0) public int bubbleWidth;
389 @AllocationIndex(1) public int bubbleHeight;
390 @AllocationIndex(2) public int bubbleBitmapWidth;
391 @AllocationIndex(3) public int bubbleBitmapHeight;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700392 @AllocationIndex(4) public int scrollHandleId;
393 @AllocationIndex(5) public int scrollHandleTextureWidth;
394 @AllocationIndex(6) public int scrollHandleTextureHeight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700395 }
396
Joe Onorato1feb3a82009-08-08 22:32:00 -0700397 class State extends IntAllocation {
398 State(RenderScript rs) {
399 super(rs);
400 }
401 @AllocationIndex(0) public int iconCount;
402 @AllocationIndex(1) public int scrollX;
Joe Onoratod769a632009-08-11 17:09:02 -0700403 @AllocationIndex(2) public int flingTimeMs;
404 @AllocationIndex(3) public int flingVelocityX;
405 @AllocationIndex(4) public int adjustedDeceleration;
406 @AllocationIndex(5) public int currentScrollX;
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700407 @AllocationIndex(6) public int flingDuration;
408 @AllocationIndex(7) public int flingEndPos;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700409 @AllocationIndex(8) public int startScrollX;
410 @AllocationIndex(9) public int selectedIconIndex = -1;
411 @AllocationIndex(10) public int selectedIconTexture;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700412 }
413
414 public RolloRS() {
415 }
416
417 public void init(Resources res, int width, int height) {
418 mRes = res;
419 mWidth = width;
420 mHeight = height;
421 initGl();
422 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700423 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700424 initRs();
425 }
426
427 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700428 Sampler.Builder sb = new Sampler.Builder(mRS);
429 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
430 sb.setMag(Sampler.Value.LINEAR);
431 sb.setWrapS(Sampler.Value.CLAMP);
432 sb.setWrapT(Sampler.Value.CLAMP);
433 mSampler = sb.create();
434
435 sb.setMin(Sampler.Value.NEAREST);
436 sb.setMag(Sampler.Value.NEAREST);
437 mSamplerText = sb.create();
438
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700439 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
440 mPFDebug = dbg.create();
441 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700442
443 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
444 bf.setTexEnable(true, 0);
445 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
446 mPFImages = bf.create();
447 mPFImages.setName("PF");
448 mPFImages.bindSampler(mSampler, 0);
449
450 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700451 //mPFText = bf.create();
452 mPFText = (new ProgramFragment.Builder(mRS, null, null)).create();
Joe Onorato93839052009-08-06 20:34:32 -0700453 mPFText.setName("PFText");
454 mPFText.bindSampler(mSamplerText, 0);
455
456 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700457 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700458 bs.setDitherEnable(false);
459 bs.setDepthMask(true);
460 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
461 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
462 mPSBackground = bs.create();
463 mPSBackground.setName("PFS");
464
465 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
466 bs.setDepthMask(false);
467 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
468 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
469 mPSText = bs.create();
470 mPSText.setName("PFSText");
471
472 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
473 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
474
475 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
476 mPV = pvb.create();
477 mPV.setName("PV");
478 mPV.bindAllocation(mPVAlloc);
479
480 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
481 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
482
483 pvb.setTextureMatrixEnable(true);
484 mPVOrtho = pvb.create();
485 mPVOrtho.setName("PVOrtho");
486 mPVOrtho.bindAllocation(mPVOrthoAlloc);
487
488 mRS.contextBindProgramVertex(mPV);
489
Joe Onorato6665c0f2009-09-02 15:27:24 -0700490 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
491 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32,
492 mTouchXBorders.length);
493 mAllocTouchXBorders.data(mTouchXBorders);
494
495 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
496 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32,
497 mTouchYBorders.length);
498 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onorato93839052009-08-06 20:34:32 -0700499
500 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700501 }
502
Joe Onorato1feb3a82009-08-08 22:32:00 -0700503 private void initData() {
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700504 mParams = new Params(mRS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700505 mState = new State(mRS);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700506
Joe Onoratobf15cb42009-08-07 14:33:40 -0700507 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700508
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700509 mParams.bubbleWidth = bubble.getBubbleWidth();
510 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
511 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
512 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700513
Joe Onoratoc567acb2009-08-31 14:34:43 -0700514 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700515 R.drawable.all_apps_button_pow2, Element.RGBA_8888, false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700516 mScrollHandle.uploadToTexture(0);
517 mParams.scrollHandleId = mScrollHandle.getID();
518 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
519 mParams.scrollHandleTextureWidth = 128;
520 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700521
Joe Onoratoc567acb2009-08-31 14:34:43 -0700522
Joe Onorato1feb3a82009-08-08 22:32:00 -0700523 mParams.save();
524 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400525
Joe Onorato6665c0f2009-09-02 15:27:24 -0700526 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
527 Bitmap.Config.ARGB_8888);
528 Bitmap selectionBitmap = mSelectionBitmap;
529 Paint paint = new Paint();
530 float radius = 12 * getContext().getResources().getDisplayMetrics().density;
531 //paint.setMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.OUTER));
532 Canvas canvas = new Canvas(selectionBitmap);
533 canvas.drawColor(0xffff0000);
534
535 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
536 Element.RGBA_8888, false);
537 mSelectedIcon.uploadToTexture(0);
538
539 mState.selectedIconTexture = mSelectedIcon.getID();
540
541 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700543 }
544
Joe Onorato1feb3a82009-08-08 22:32:00 -0700545 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700546 ScriptC.Builder sb = new ScriptC.Builder(mRS);
547 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700548 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700549 sb.addDefines(Defines.class);
Joe Onorato93839052009-08-06 20:34:32 -0700550 mScript = sb.create();
551 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
552
Joe Onoratod769a632009-08-11 17:09:02 -0700553 mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
554 mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
555 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700556 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700557 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
558 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700559
560 mRS.contextBindRootScript(mScript);
561 }
Joe Onorato93839052009-08-06 20:34:32 -0700562
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563 private void setApps(ArrayList<ApplicationInfo> list) {
564 final int count = list != null ? list.size() : 0;
565 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700566 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400567 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
568
569 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700570 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
572
573 Element ie8888 = Element.RGBA_8888;
574
575 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
576
577 for (int i=0; i<count; i++) {
578 final ApplicationInfo item = list.get(i);
579
580 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700581 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400582 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700583 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400584
585 mIcons[i].uploadToTexture(0);
586 mLabels[i].uploadToTexture(0);
587
Joe Onorato6665c0f2009-09-02 15:27:24 -0700588 mIconIds[i] = mIcons[i].getID();
589 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400590 }
591
Joe Onorato6665c0f2009-09-02 15:27:24 -0700592 mAllocIconID.data(mIconIds);
593 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400594
595 mState.iconCount = count;
596
597 Log.d("AllAppsView", "mScript=" + mScript + " mAllocIconID=" + mAllocIconID);
598
599 if (mScript != null) { // wtf
600 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
601 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
602 }
603
604 mState.save();
605 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700606
607 void initTouchState() {
608 int width = getWidth();
609 int height = getHeight();
610
611 int iconsSize;
612 if (width < height) {
613 iconsSize = width;
614 } else {
615 iconsSize = height;
616 }
617 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
618 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
619
620 int centerY = (height / 2) - (int)(cellHeight * 0.35f);
621 mTouchYBorders[0] = centerY - (int)(2.4f * cellHeight);
622 mTouchYBorders[1] = centerY - (int)(1.15f * cellHeight);
623 mTouchYBorders[2] = centerY;
624 mTouchYBorders[3] = centerY + (int)(1.15f * cellHeight);;
625 mTouchYBorders[4] = centerY + (int)(2.4f * cellHeight);
626
627 mAllocTouchYBorders.data(mTouchYBorders);
628
629 int centerX = (width / 2);
630 mTouchXBorders[0] = centerX - (2 * cellWidth);
631 mTouchXBorders[1] = centerX - (int)(0.83f * cellWidth);;
632 mTouchXBorders[2] = centerX;
633 mTouchXBorders[3] = centerX + (int)(0.83f * cellWidth);;
634 mTouchXBorders[4] = centerX + (2 * cellWidth);
635
636 mAllocTouchXBorders.data(mTouchXBorders);
637 }
638
639 int chooseTappedIcon(int x, int y, int scrollX, int currentPage) {
640 int col = -1;
641 int row = -1;
642
643 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
644 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
645 col = i;
646 break;
647 }
648 }
649 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
650 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
651 row = i;
652 break;
653 }
654 }
655
656 if (row < 0 || col < 0) {
657 return -1;
658 }
659
660 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
661 + (row * Defines.ROWS_PER_PAGE) + col;
662 }
663
664 /**
665 * You need to call save() on mState on your own after calling this.
666 */
667 void selectIcon(int x, int y, int scrollX, int currentPage) {
668 int iconCount = mAllAppsList.size();
669 int index = chooseTappedIcon(x, y, scrollX, currentPage);
670 if (index < 0 || index >= iconCount) {
671 mState.selectedIconIndex = -1;
672 return;
673 } else {
674 mState.selectedIconIndex = index;
675 }
676 }
677
678 /**
679 * You need to call save() on mState on your own after calling this.
680 */
681 void clearSelectedIcon() {
682 mState.selectedIconIndex = -1;
683 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684 }
Joe Onorato93839052009-08-06 20:34:32 -0700685}
686
687