blob: b9ec2cfb3ff1097a2d186726cd11e58059720055 [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
64 implements View.OnClickListener, View.OnLongClickListener {
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;
68
Joe Onorato1feb3a82009-08-08 22:32:00 -070069 private RenderScript mRS;
70 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040071 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070072
Joe Onoratod769a632009-08-11 17:09:02 -070073 private ViewConfiguration mConfig;
Joe Onoratoc567acb2009-08-31 14:34:43 -070074 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070075 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070076 private VelocityTracker mVelocity;
77 private int mLastScrollX;
Joe Onorato1feb3a82009-08-08 22:32:00 -070078 private int mLastMotionX;
Joe Onoratoc567acb2009-08-31 14:34:43 -070079 private TouchHandler mTouchHandler;
80 private int mScrollHandleTop;
Joe Onorato1feb3a82009-08-08 22:32:00 -070081
Joe Onorato6665c0f2009-09-02 15:27:24 -070082 static class Defines {
83 private static float farSize(float sizeAt0) {
84 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
85 }
86
Joe Onoratoc567acb2009-08-31 14:34:43 -070087 public static final int ALLOC_PARAMS = 0;
88 public static final int ALLOC_STATE = 1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070089 public static final int ALLOC_ICON_IDS = 2;
90 public static final int ALLOC_LABEL_IDS = 3;
91 public static final int ALLOC_X_BORDERS = 4;
92 public static final int ALLOC_Y_BORDERS = 5;
Joe Onoratoc567acb2009-08-31 14:34:43 -070093
94 public static final int COLUMNS_PER_PAGE = 4;
95 public static final int ROWS_PER_PAGE = 4;
96
Joe Onorato6665c0f2009-09-02 15:27:24 -070097 public static final float RADIUS = 4.0f;
98
99 public static final int SCREEN_WIDTH_PX = 480;
100 public static final int SCREEN_HEIGHT_PX = 854;
101
102 public static final int ICON_WIDTH_PX = 64;
103 public static final int ICON_TEXTURE_WIDTH_PX = 128;
104
105 public static final int ICON_HEIGHT_PX = 64;
106 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
107 public static final float ICON_TOP_OFFSET = 0.2f;
108
109 public static final float CAMERA_Z = -2;
110 public static final float FAR_ICON_SIZE
111 = farSize(2 * ICON_WIDTH_PX / (float)SCREEN_WIDTH_PX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700112 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700113
114 public AllAppsView(Context context, AttributeSet attrs) {
115 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700116 setFocusable(true);
117 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratod769a632009-08-11 17:09:02 -0700118 mConfig = ViewConfiguration.get(context);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700119 setOnClickListener(this);
120 setOnLongClickListener(this);
Joe Onorato93839052009-08-06 20:34:32 -0700121 }
122
Joe Onorato7c312c12009-08-13 21:36:53 -0700123 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
124 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700125 }
126
Joe Onorato6665c0f2009-09-02 15:27:24 -0700127 public void setLauncher(Launcher launcher) {
128 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700129 }
130
Joe Onorato1feb3a82009-08-08 22:32:00 -0700131 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700132 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
133 super.surfaceChanged(holder, format, w, h);
134
Joe Onorato6665c0f2009-09-02 15:27:24 -0700135 long startTime = SystemClock.uptimeMillis();
136
Jason Samsb58cbdc2009-08-21 16:56:58 -0700137 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700138 mRollo = new RolloRS();
139 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400140 if (mAllAppsList != null) {
141 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700142 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400143 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700144
145 Resources res = getContext().getResources();
146 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
147 mScrollHandleTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700148
149 long endTime = SystemClock.uptimeMillis();
150 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700151 }
152
153 @Override
154 public boolean onKeyDown(int keyCode, KeyEvent event)
155 {
Joe Onorato93839052009-08-06 20:34:32 -0700156 // this method doesn't work when 'extends View' include 'extends ScrollView'.
157 return super.onKeyDown(keyCode, event);
158 }
159
Joe Onorato93839052009-08-06 20:34:32 -0700160 @Override
161 public boolean onTouchEvent(MotionEvent ev)
162 {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700163 super.onTouchEvent(ev);
164
Joe Onoratoc567acb2009-08-31 14:34:43 -0700165 mTouchHandler = mFlingHandler;
166 /*
167 int action = ev.getAction();
168 if (action == MotionEvent.ACTION_DOWN) {
169 if (ev.getY() > mScrollHandleTop) {
170 mTouchHandler = mScrollHandler;
171 } else {
172 mTouchHandler = mFlingHandler;
173 }
Joe Onorato93839052009-08-06 20:34:32 -0700174 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700175 */
Joe Onorato6665c0f2009-09-02 15:27:24 -0700176 mTouchHandler.onTouchEvent(ev);
177
178 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700179 }
180
Joe Onoratoc567acb2009-08-31 14:34:43 -0700181 private abstract class TouchHandler {
182 abstract boolean onTouchEvent(MotionEvent ev);
183 };
184
185 private TouchHandler mFlingHandler = new TouchHandler() {
186 @Override
187 public boolean onTouchEvent(MotionEvent ev)
188 {
189 int x = (int)ev.getX();
190 int deltaX;
191 switch (ev.getAction()) {
192 case MotionEvent.ACTION_DOWN:
193 mLastMotionX = x;
194 mRollo.mState.read();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700195 mRollo.mState.startScrollX = mRollo.mState.scrollX = mLastScrollX
196 = mRollo.mState.currentScrollX;
197 if (mRollo.mState.flingVelocityX != 0) {
198 mRollo.clearSelectedIcon();
199 } else {
200 mRollo.selectIcon(x, (int)ev.getY(), mRollo.mState.startScrollX,
201 (-mRollo.mState.startScrollX / Defines.SCREEN_WIDTH_PX));
202 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700203 mRollo.mState.flingVelocityX = 0;
204 mRollo.mState.adjustedDeceleration = 0;
205 mRollo.mState.save();
206 mVelocity = VelocityTracker.obtain();
207 mVelocity.addMovement(ev);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700208 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700209 break;
210 case MotionEvent.ACTION_MOVE:
211 case MotionEvent.ACTION_OUTSIDE:
Joe Onorato6665c0f2009-09-02 15:27:24 -0700212 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 {
217 mStartedScrolling = true;
218 mRollo.clearSelectedIcon();
219 deltaX = x - mLastMotionX;
220 mVelocity.addMovement(ev);
221 mRollo.mState.currentScrollX = mLastScrollX;
222 mLastScrollX += deltaX;
223 mRollo.mState.scrollX = mLastScrollX;
224 mRollo.mState.save();
225 mLastMotionX = x;
226 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700227 break;
228 case MotionEvent.ACTION_UP:
229 case MotionEvent.ACTION_CANCEL:
230 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
231 mConfig.getScaledMaximumFlingVelocity());
232 mRollo.mState.flingTimeMs = (int)SystemClock.uptimeMillis(); // TODO: use long
233 mRollo.mState.flingVelocityX = (int)mVelocity.getXVelocity();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700234 mRollo.clearSelectedIcon();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700235 mRollo.mState.save();
236 mLastMotionX = -10000;
237 mVelocity.recycle();
238 mVelocity = null;
239 break;
240 }
241 return true;
242 }
243 };
244
Joe Onorato6665c0f2009-09-02 15:27:24 -0700245 public void onClick(View v) {
246 int index = mRollo.mState.selectedIconIndex;
247 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
248 ApplicationInfo app = mAllAppsList.get(index);
249 mLauncher.startActivitySafely(app.intent);
250 }
251 }
252
253 public boolean onLongClick(View v) {
254 Log.d(TAG, "long click! velocity=" + mRollo.mState.flingVelocityX + " index="
255 + mRollo.mState.selectedIconIndex);
256 return true;
257 }
258
Joe Onoratoc567acb2009-08-31 14:34:43 -0700259 /*
260 private TouchHandler mScrollHandler = new TouchHandler() {
261 @Override
262 public boolean onTouchEvent(MotionEvent ev)
263 {
264 int x = (int)ev.getX();
265 int w = getWidth();
266
267 float percent = x / (float)w;
268
269 mRollo.mState.read();
270
271 mRollo.mState.scrollX = mLastScrollX = -(int)(mPageCount * w * percent);
272 mRollo.mState.flingVelocityX = 0;
273 mRollo.mState.adjustedDeceleration = 0;
274 mRollo.mState.save();
275
276 return true;
277 }
278 };
279 */
280
Joe Onorato93839052009-08-06 20:34:32 -0700281 @Override
282 public boolean onTrackballEvent(MotionEvent ev)
283 {
284 float x = ev.getX();
285 float y = ev.getY();
286 //Float tx = new Float(x);
287 //Float ty = new Float(y);
288 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
289
290
291 return true;
292 }
293
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294 public void setApps(ArrayList<ApplicationInfo> list) {
295 mAllAppsList = list;
296 if (mRollo != null) {
297 mRollo.setApps(list);
298 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700299 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700300 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
301 }
302
303 private void invokeIcon(int index) {
304 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700305 }
306
307 private static int countPages(int iconCount) {
308 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
309 int pages = iconCount / iconsPerPage;
310 if (pages*iconsPerPage != iconCount) {
311 pages++;
312 }
313 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400314 }
315
Joe Onorato93839052009-08-06 20:34:32 -0700316 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700317
Joe Onorato1feb3a82009-08-08 22:32:00 -0700318 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700319
Joe Onorato93839052009-08-06 20:34:32 -0700320 private int mWidth;
321 private int mHeight;
322
323 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700324 private Script mScript;
325 private Sampler mSampler;
326 private Sampler mSamplerText;
327 private ProgramStore mPSBackground;
328 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700329 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700330 private ProgramFragment mPFImages;
331 private ProgramFragment mPFText;
332 private ProgramVertex mPV;
333 private ProgramVertex.MatrixAllocation mPVAlloc;
334 private ProgramVertex mPVOrtho;
335 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700336
Joe Onoratoc567acb2009-08-31 14:34:43 -0700337 private Allocation mScrollHandle;
338
Joe Onoratobf15cb42009-08-07 14:33:40 -0700339 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700340 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700341 private Allocation mAllocIconID;
342
Joe Onoratobf15cb42009-08-07 14:33:40 -0700343 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700344 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700345 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700346 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700347
Joe Onorato6665c0f2009-09-02 15:27:24 -0700348 private int[] mTouchYBorders;
349 private Allocation mAllocTouchYBorders;
350 private int[] mTouchXBorders;
351 private Allocation mAllocTouchXBorders;
352
353 private Bitmap mSelectionBitmap;
Joe Onorato93839052009-08-06 20:34:32 -0700354
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700355 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700356 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700357
358 class Params extends IntAllocation {
359 Params(RenderScript rs) {
360 super(rs);
361 }
362 @AllocationIndex(0) public int bubbleWidth;
363 @AllocationIndex(1) public int bubbleHeight;
364 @AllocationIndex(2) public int bubbleBitmapWidth;
365 @AllocationIndex(3) public int bubbleBitmapHeight;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700366 @AllocationIndex(4) public int scrollHandleId;
367 @AllocationIndex(5) public int scrollHandleTextureWidth;
368 @AllocationIndex(6) public int scrollHandleTextureHeight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700369 }
370
Joe Onorato1feb3a82009-08-08 22:32:00 -0700371 class State extends IntAllocation {
372 State(RenderScript rs) {
373 super(rs);
374 }
375 @AllocationIndex(0) public int iconCount;
376 @AllocationIndex(1) public int scrollX;
Joe Onoratod769a632009-08-11 17:09:02 -0700377 @AllocationIndex(2) public int flingTimeMs;
378 @AllocationIndex(3) public int flingVelocityX;
379 @AllocationIndex(4) public int adjustedDeceleration;
380 @AllocationIndex(5) public int currentScrollX;
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700381 @AllocationIndex(6) public int flingDuration;
382 @AllocationIndex(7) public int flingEndPos;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700383 @AllocationIndex(8) public int startScrollX;
384 @AllocationIndex(9) public int selectedIconIndex = -1;
385 @AllocationIndex(10) public int selectedIconTexture;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700386 }
387
388 public RolloRS() {
389 }
390
391 public void init(Resources res, int width, int height) {
392 mRes = res;
393 mWidth = width;
394 mHeight = height;
395 initGl();
396 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700397 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700398 initRs();
399 }
400
401 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700402 Sampler.Builder sb = new Sampler.Builder(mRS);
403 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
404 sb.setMag(Sampler.Value.LINEAR);
405 sb.setWrapS(Sampler.Value.CLAMP);
406 sb.setWrapT(Sampler.Value.CLAMP);
407 mSampler = sb.create();
408
409 sb.setMin(Sampler.Value.NEAREST);
410 sb.setMag(Sampler.Value.NEAREST);
411 mSamplerText = sb.create();
412
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700413 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
414 mPFDebug = dbg.create();
415 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700416
417 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
418 bf.setTexEnable(true, 0);
419 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
420 mPFImages = bf.create();
421 mPFImages.setName("PF");
422 mPFImages.bindSampler(mSampler, 0);
423
424 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700425 //mPFText = bf.create();
426 mPFText = (new ProgramFragment.Builder(mRS, null, null)).create();
Joe Onorato93839052009-08-06 20:34:32 -0700427 mPFText.setName("PFText");
428 mPFText.bindSampler(mSamplerText, 0);
429
430 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700431 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700432 bs.setDitherEnable(false);
433 bs.setDepthMask(true);
434 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
435 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
436 mPSBackground = bs.create();
437 mPSBackground.setName("PFS");
438
439 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
440 bs.setDepthMask(false);
441 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
442 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
443 mPSText = bs.create();
444 mPSText.setName("PFSText");
445
446 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
447 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
448
449 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
450 mPV = pvb.create();
451 mPV.setName("PV");
452 mPV.bindAllocation(mPVAlloc);
453
454 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
455 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
456
457 pvb.setTextureMatrixEnable(true);
458 mPVOrtho = pvb.create();
459 mPVOrtho.setName("PVOrtho");
460 mPVOrtho.bindAllocation(mPVOrthoAlloc);
461
462 mRS.contextBindProgramVertex(mPV);
463
Joe Onorato6665c0f2009-09-02 15:27:24 -0700464 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
465 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32,
466 mTouchXBorders.length);
467 mAllocTouchXBorders.data(mTouchXBorders);
468
469 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
470 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32,
471 mTouchYBorders.length);
472 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onorato93839052009-08-06 20:34:32 -0700473
474 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700475 }
476
Joe Onorato1feb3a82009-08-08 22:32:00 -0700477 private void initData() {
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700478 mParams = new Params(mRS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700479 mState = new State(mRS);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700480
Joe Onoratobf15cb42009-08-07 14:33:40 -0700481 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700482
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700483 mParams.bubbleWidth = bubble.getBubbleWidth();
484 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
485 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
486 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700487
Joe Onoratoc567acb2009-08-31 14:34:43 -0700488 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700489 R.drawable.all_apps_button_pow2, Element.RGBA_8888, false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700490 mScrollHandle.uploadToTexture(0);
491 mParams.scrollHandleId = mScrollHandle.getID();
492 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
493 mParams.scrollHandleTextureWidth = 128;
494 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700495
Joe Onoratoc567acb2009-08-31 14:34:43 -0700496
Joe Onorato1feb3a82009-08-08 22:32:00 -0700497 mParams.save();
498 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400499
Joe Onorato6665c0f2009-09-02 15:27:24 -0700500 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
501 Bitmap.Config.ARGB_8888);
502 Bitmap selectionBitmap = mSelectionBitmap;
503 Paint paint = new Paint();
504 float radius = 12 * getContext().getResources().getDisplayMetrics().density;
505 //paint.setMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.OUTER));
506 Canvas canvas = new Canvas(selectionBitmap);
507 canvas.drawColor(0xffff0000);
508
509 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
510 Element.RGBA_8888, false);
511 mSelectedIcon.uploadToTexture(0);
512
513 mState.selectedIconTexture = mSelectedIcon.getID();
514
515 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700517 }
518
Joe Onorato1feb3a82009-08-08 22:32:00 -0700519 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700520 ScriptC.Builder sb = new ScriptC.Builder(mRS);
521 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700522 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700523 sb.addDefines(Defines.class);
Joe Onorato93839052009-08-06 20:34:32 -0700524 mScript = sb.create();
525 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
526
Joe Onoratod769a632009-08-11 17:09:02 -0700527 mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
528 mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
529 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700530 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700531 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
532 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700533
534 mRS.contextBindRootScript(mScript);
535 }
Joe Onorato93839052009-08-06 20:34:32 -0700536
Joe Onorato9c1289c2009-08-17 11:03:03 -0400537 private void setApps(ArrayList<ApplicationInfo> list) {
538 final int count = list != null ? list.size() : 0;
539 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700540 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400541 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
542
543 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700544 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400545 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
546
547 Element ie8888 = Element.RGBA_8888;
548
549 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
550
551 for (int i=0; i<count; i++) {
552 final ApplicationInfo item = list.get(i);
553
554 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700555 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400556 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700557 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400558
559 mIcons[i].uploadToTexture(0);
560 mLabels[i].uploadToTexture(0);
561
Joe Onorato6665c0f2009-09-02 15:27:24 -0700562 mIconIds[i] = mIcons[i].getID();
563 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400564 }
565
Joe Onorato6665c0f2009-09-02 15:27:24 -0700566 mAllocIconID.data(mIconIds);
567 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400568
569 mState.iconCount = count;
570
571 Log.d("AllAppsView", "mScript=" + mScript + " mAllocIconID=" + mAllocIconID);
572
573 if (mScript != null) { // wtf
574 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
575 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
576 }
577
578 mState.save();
579 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700580
581 void initTouchState() {
582 int width = getWidth();
583 int height = getHeight();
584
585 int iconsSize;
586 if (width < height) {
587 iconsSize = width;
588 } else {
589 iconsSize = height;
590 }
591 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
592 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
593
594 int centerY = (height / 2) - (int)(cellHeight * 0.35f);
595 mTouchYBorders[0] = centerY - (int)(2.4f * cellHeight);
596 mTouchYBorders[1] = centerY - (int)(1.15f * cellHeight);
597 mTouchYBorders[2] = centerY;
598 mTouchYBorders[3] = centerY + (int)(1.15f * cellHeight);;
599 mTouchYBorders[4] = centerY + (int)(2.4f * cellHeight);
600
601 mAllocTouchYBorders.data(mTouchYBorders);
602
603 int centerX = (width / 2);
604 mTouchXBorders[0] = centerX - (2 * cellWidth);
605 mTouchXBorders[1] = centerX - (int)(0.83f * cellWidth);;
606 mTouchXBorders[2] = centerX;
607 mTouchXBorders[3] = centerX + (int)(0.83f * cellWidth);;
608 mTouchXBorders[4] = centerX + (2 * cellWidth);
609
610 mAllocTouchXBorders.data(mTouchXBorders);
611 }
612
613 int chooseTappedIcon(int x, int y, int scrollX, int currentPage) {
614 int col = -1;
615 int row = -1;
616
617 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
618 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
619 col = i;
620 break;
621 }
622 }
623 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
624 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
625 row = i;
626 break;
627 }
628 }
629
630 if (row < 0 || col < 0) {
631 return -1;
632 }
633
634 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
635 + (row * Defines.ROWS_PER_PAGE) + col;
636 }
637
638 /**
639 * You need to call save() on mState on your own after calling this.
640 */
641 void selectIcon(int x, int y, int scrollX, int currentPage) {
642 int iconCount = mAllAppsList.size();
643 int index = chooseTappedIcon(x, y, scrollX, currentPage);
644 if (index < 0 || index >= iconCount) {
645 mState.selectedIconIndex = -1;
646 return;
647 } else {
648 mState.selectedIconIndex = index;
649 }
650 }
651
652 /**
653 * You need to call save() on mState on your own after calling this.
654 */
655 void clearSelectedIcon() {
656 mState.selectedIconIndex = -1;
657 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 }
Joe Onorato93839052009-08-06 20:34:32 -0700659}
660
661