blob: 5128db332abd3d33461547b51cc74277bf1bccda [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 Onorato85a02a82009-09-08 12:34:22 -070069 private boolean mLocked = true;
Joe Onorato6665c0f2009-09-02 15:27:24 -070070
Joe Onorato1feb3a82009-08-08 22:32:00 -070071 private RenderScript mRS;
72 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040073 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070074
Joe Onoratod769a632009-08-11 17:09:02 -070075 private ViewConfiguration mConfig;
Joe Onoratoc567acb2009-08-31 14:34:43 -070076 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070077 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070078 private VelocityTracker mVelocity;
79 private int mLastScrollX;
Joe Onorato1feb3a82009-08-08 22:32:00 -070080 private int mLastMotionX;
Joe Onorato5162ea92009-09-03 09:39:42 -070081 private int mMotionDownRawX;
82 private int mMotionDownRawY;
Joe Onoratoc567acb2009-08-31 14:34:43 -070083 private TouchHandler mTouchHandler;
84 private int mScrollHandleTop;
Joe Onorato1feb3a82009-08-08 22:32:00 -070085
Joe Onorato6665c0f2009-09-02 15:27:24 -070086 static class Defines {
87 private static float farSize(float sizeAt0) {
88 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
89 }
90
Joe Onoratoc567acb2009-08-31 14:34:43 -070091 public static final int ALLOC_PARAMS = 0;
92 public static final int ALLOC_STATE = 1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070093 public static final int ALLOC_ICON_IDS = 2;
94 public static final int ALLOC_LABEL_IDS = 3;
95 public static final int ALLOC_X_BORDERS = 4;
96 public static final int ALLOC_Y_BORDERS = 5;
Joe Onoratoc567acb2009-08-31 14:34:43 -070097
98 public static final int COLUMNS_PER_PAGE = 4;
99 public static final int ROWS_PER_PAGE = 4;
100
Joe Onorato6665c0f2009-09-02 15:27:24 -0700101 public static final float RADIUS = 4.0f;
102
103 public static final int SCREEN_WIDTH_PX = 480;
104 public static final int SCREEN_HEIGHT_PX = 854;
105
106 public static final int ICON_WIDTH_PX = 64;
107 public static final int ICON_TEXTURE_WIDTH_PX = 128;
108
109 public static final int ICON_HEIGHT_PX = 64;
110 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
111 public static final float ICON_TOP_OFFSET = 0.2f;
112
113 public static final float CAMERA_Z = -2;
114 public static final float FAR_ICON_SIZE
115 = farSize(2 * ICON_WIDTH_PX / (float)SCREEN_WIDTH_PX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700116 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700117
118 public AllAppsView(Context context, AttributeSet attrs) {
119 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700120 setFocusable(true);
121 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratod769a632009-08-11 17:09:02 -0700122 mConfig = ViewConfiguration.get(context);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700123 setOnClickListener(this);
124 setOnLongClickListener(this);
Joe Onorato93839052009-08-06 20:34:32 -0700125 }
126
Joe Onorato7c312c12009-08-13 21:36:53 -0700127 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
128 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700129 }
130
Joe Onorato6665c0f2009-09-02 15:27:24 -0700131 public void setLauncher(Launcher launcher) {
132 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700133 }
134
Joe Onorato1feb3a82009-08-08 22:32:00 -0700135 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700136 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
137 super.surfaceChanged(holder, format, w, h);
138
Joe Onorato6665c0f2009-09-02 15:27:24 -0700139 long startTime = SystemClock.uptimeMillis();
140
Jason Samsb58cbdc2009-08-21 16:56:58 -0700141 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700142 mRollo = new RolloRS();
143 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400144 if (mAllAppsList != null) {
145 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700146 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400147 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700148
149 Resources res = getContext().getResources();
150 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
151 mScrollHandleTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700152
153 long endTime = SystemClock.uptimeMillis();
154 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700155 }
156
157 @Override
158 public boolean onKeyDown(int keyCode, KeyEvent event)
159 {
Joe Onorato93839052009-08-06 20:34:32 -0700160 // this method doesn't work when 'extends View' include 'extends ScrollView'.
161 return super.onKeyDown(keyCode, event);
162 }
163
Joe Onorato93839052009-08-06 20:34:32 -0700164 @Override
165 public boolean onTouchEvent(MotionEvent ev)
166 {
Joe Onoratoe3406a22009-09-03 14:36:25 -0700167 if (mRollo.mState.visible == 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700168 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700169 }
170
Joe Onorato85a02a82009-09-08 12:34:22 -0700171 if (mLocked) {
172 return true;
173 }
174
175 super.onTouchEvent(ev);
176
Joe Onoratoc567acb2009-08-31 14:34:43 -0700177 mTouchHandler = mFlingHandler;
178 /*
179 int action = ev.getAction();
180 if (action == MotionEvent.ACTION_DOWN) {
181 if (ev.getY() > mScrollHandleTop) {
182 mTouchHandler = mScrollHandler;
183 } else {
184 mTouchHandler = mFlingHandler;
185 }
Joe Onorato93839052009-08-06 20:34:32 -0700186 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700187 */
Joe Onorato6665c0f2009-09-02 15:27:24 -0700188 mTouchHandler.onTouchEvent(ev);
189
190 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700191 }
192
Joe Onoratoc567acb2009-08-31 14:34:43 -0700193 private abstract class TouchHandler {
194 abstract boolean onTouchEvent(MotionEvent ev);
195 };
196
197 private TouchHandler mFlingHandler = new TouchHandler() {
198 @Override
199 public boolean onTouchEvent(MotionEvent ev)
200 {
201 int x = (int)ev.getX();
202 int deltaX;
203 switch (ev.getAction()) {
204 case MotionEvent.ACTION_DOWN:
Joe Onorato5162ea92009-09-03 09:39:42 -0700205 mMotionDownRawX = (int)ev.getRawX();
206 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700207 mLastMotionX = x;
208 mRollo.mState.read();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700209 mRollo.mState.startScrollX = mRollo.mState.scrollX = mLastScrollX
210 = mRollo.mState.currentScrollX;
211 if (mRollo.mState.flingVelocityX != 0) {
212 mRollo.clearSelectedIcon();
213 } else {
214 mRollo.selectIcon(x, (int)ev.getY(), mRollo.mState.startScrollX,
215 (-mRollo.mState.startScrollX / Defines.SCREEN_WIDTH_PX));
216 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700217 mRollo.mState.flingVelocityX = 0;
218 mRollo.mState.adjustedDeceleration = 0;
219 mRollo.mState.save();
220 mVelocity = VelocityTracker.obtain();
221 mVelocity.addMovement(ev);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700222 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700223 break;
224 case MotionEvent.ACTION_MOVE:
225 case MotionEvent.ACTION_OUTSIDE:
Joe Onorato6665c0f2009-09-02 15:27:24 -0700226 int slop = Math.abs(x - mLastMotionX);
227 if (!mStartedScrolling && slop < mConfig.getScaledTouchSlop()) {
228 // don't update mLastMotionX so slop is right and when we do start scrolling
229 // below, we get the right delta.
230 } else {
231 mStartedScrolling = true;
232 mRollo.clearSelectedIcon();
233 deltaX = x - mLastMotionX;
234 mVelocity.addMovement(ev);
235 mRollo.mState.currentScrollX = mLastScrollX;
236 mLastScrollX += deltaX;
237 mRollo.mState.scrollX = mLastScrollX;
238 mRollo.mState.save();
239 mLastMotionX = x;
240 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700241 break;
242 case MotionEvent.ACTION_UP:
243 case MotionEvent.ACTION_CANCEL:
244 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
245 mConfig.getScaledMaximumFlingVelocity());
246 mRollo.mState.flingTimeMs = (int)SystemClock.uptimeMillis(); // TODO: use long
247 mRollo.mState.flingVelocityX = (int)mVelocity.getXVelocity();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700248 mRollo.clearSelectedIcon();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700249 mRollo.mState.save();
250 mLastMotionX = -10000;
251 mVelocity.recycle();
252 mVelocity = null;
253 break;
254 }
255 return true;
256 }
257 };
258
Joe Onorato6665c0f2009-09-02 15:27:24 -0700259 public void onClick(View v) {
260 int index = mRollo.mState.selectedIconIndex;
261 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
262 ApplicationInfo app = mAllAppsList.get(index);
263 mLauncher.startActivitySafely(app.intent);
264 }
265 }
266
267 public boolean onLongClick(View v) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700268 int index = mRollo.mState.selectedIconIndex;
269 Log.d(TAG, "long click! velocity=" + mRollo.mState.flingVelocityX + " index=" + index);
270 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
271 ApplicationInfo app = mAllAppsList.get(index);
272
273 // We don't really have an accurate location to use. This will do.
274 int screenX = mMotionDownRawX - (Defines.ICON_WIDTH_PX / 2);
275 int screenY = mMotionDownRawY - Defines.ICON_HEIGHT_PX;
276
277 int left = (Defines.ICON_TEXTURE_WIDTH_PX - Defines.ICON_WIDTH_PX) / 2;
278 int top = (Defines.ICON_TEXTURE_HEIGHT_PX - Defines.ICON_HEIGHT_PX) / 2;
279 mDragController.startDrag(app.iconBitmap, screenX, screenY,
280 left, top, Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
281 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700282
283 mLauncher.closeAllAppsDialog(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700284 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700285 return true;
286 }
287
Joe Onorato5162ea92009-09-03 09:39:42 -0700288 public void setDragController(DragController dragger) {
289 mDragController = dragger;
290 }
291
292 public void onDropCompleted(View target, boolean success) {
293 }
294
Joe Onorato85a02a82009-09-08 12:34:22 -0700295 private static final int SCALE_SCALE = 100000;
296
Joe Onorato006b25f2009-09-03 11:38:43 -0700297 public void show() {
298 mRollo.mState.read();
299 mRollo.mState.visible = 1;
Joe Onorato85a02a82009-09-08 12:34:22 -0700300 mRollo.mState.zoom = SCALE_SCALE;
Joe Onorato006b25f2009-09-03 11:38:43 -0700301 mRollo.mState.save();
302 }
303
Joe Onorato85a02a82009-09-08 12:34:22 -0700304 public void setScale(float amount) {
305 mRollo.mState.read();
306 if (amount > 0.001f) {
307 mRollo.mState.visible = 1;
308 mRollo.mState.zoom = (int)(SCALE_SCALE*amount);
309 } else {
310 mRollo.mState.visible = 0;
311 mRollo.mState.zoom = 0;
312 }
313 mRollo.mState.save();
314 }
315
316 public void hide() {
Joe Onorato006b25f2009-09-03 11:38:43 -0700317 mRollo.mState.read();
318 mRollo.mState.visible = 0;
Joe Onorato85a02a82009-09-08 12:34:22 -0700319 mRollo.mState.zoom = 0;
Joe Onorato006b25f2009-09-03 11:38:43 -0700320 mRollo.mState.save();
321 }
322
Joe Onoratoc567acb2009-08-31 14:34:43 -0700323 /*
324 private TouchHandler mScrollHandler = new TouchHandler() {
325 @Override
326 public boolean onTouchEvent(MotionEvent ev)
327 {
328 int x = (int)ev.getX();
329 int w = getWidth();
330
331 float percent = x / (float)w;
332
333 mRollo.mState.read();
334
335 mRollo.mState.scrollX = mLastScrollX = -(int)(mPageCount * w * percent);
336 mRollo.mState.flingVelocityX = 0;
337 mRollo.mState.adjustedDeceleration = 0;
338 mRollo.mState.save();
339
340 return true;
341 }
342 };
343 */
344
Joe Onorato93839052009-08-06 20:34:32 -0700345 @Override
346 public boolean onTrackballEvent(MotionEvent ev)
347 {
348 float x = ev.getX();
349 float y = ev.getY();
350 //Float tx = new Float(x);
351 //Float ty = new Float(y);
352 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
353
354
355 return true;
356 }
357
Joe Onorato9c1289c2009-08-17 11:03:03 -0400358 public void setApps(ArrayList<ApplicationInfo> list) {
359 mAllAppsList = list;
360 if (mRollo != null) {
361 mRollo.setApps(list);
362 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700363 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700364 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onorato85a02a82009-09-08 12:34:22 -0700365 mLocked = false;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700366 }
367
368 private void invokeIcon(int index) {
369 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700370 }
371
372 private static int countPages(int iconCount) {
373 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
374 int pages = iconCount / iconsPerPage;
375 if (pages*iconsPerPage != iconCount) {
376 pages++;
377 }
378 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400379 }
380
Joe Onorato93839052009-08-06 20:34:32 -0700381 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700382
Joe Onorato1feb3a82009-08-08 22:32:00 -0700383 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700384
Joe Onorato93839052009-08-06 20:34:32 -0700385 private int mWidth;
386 private int mHeight;
387
388 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700389 private Script mScript;
390 private Sampler mSampler;
391 private Sampler mSamplerText;
392 private ProgramStore mPSBackground;
393 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700394 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700395 private ProgramFragment mPFImages;
396 private ProgramFragment mPFText;
397 private ProgramVertex mPV;
398 private ProgramVertex.MatrixAllocation mPVAlloc;
399 private ProgramVertex mPVOrtho;
400 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700401
Joe Onoratoc567acb2009-08-31 14:34:43 -0700402 private Allocation mScrollHandle;
403
Joe Onoratobf15cb42009-08-07 14:33:40 -0700404 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700405 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700406 private Allocation mAllocIconID;
407
Joe Onoratobf15cb42009-08-07 14:33:40 -0700408 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700409 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700410 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700411 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700412
Joe Onorato6665c0f2009-09-02 15:27:24 -0700413 private int[] mTouchYBorders;
414 private Allocation mAllocTouchYBorders;
415 private int[] mTouchXBorders;
416 private Allocation mAllocTouchXBorders;
417
418 private Bitmap mSelectionBitmap;
Joe Onorato93839052009-08-06 20:34:32 -0700419
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700420 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700421 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700422
423 class Params extends IntAllocation {
424 Params(RenderScript rs) {
425 super(rs);
426 }
427 @AllocationIndex(0) public int bubbleWidth;
428 @AllocationIndex(1) public int bubbleHeight;
429 @AllocationIndex(2) public int bubbleBitmapWidth;
430 @AllocationIndex(3) public int bubbleBitmapHeight;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700431 @AllocationIndex(4) public int scrollHandleId;
432 @AllocationIndex(5) public int scrollHandleTextureWidth;
433 @AllocationIndex(6) public int scrollHandleTextureHeight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700434 }
435
Joe Onorato1feb3a82009-08-08 22:32:00 -0700436 class State extends IntAllocation {
437 State(RenderScript rs) {
438 super(rs);
439 }
440 @AllocationIndex(0) public int iconCount;
441 @AllocationIndex(1) public int scrollX;
Joe Onoratod769a632009-08-11 17:09:02 -0700442 @AllocationIndex(2) public int flingTimeMs;
443 @AllocationIndex(3) public int flingVelocityX;
444 @AllocationIndex(4) public int adjustedDeceleration;
445 @AllocationIndex(5) public int currentScrollX;
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700446 @AllocationIndex(6) public int flingDuration;
447 @AllocationIndex(7) public int flingEndPos;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700448 @AllocationIndex(8) public int startScrollX;
449 @AllocationIndex(9) public int selectedIconIndex = -1;
450 @AllocationIndex(10) public int selectedIconTexture;
Joe Onorato006b25f2009-09-03 11:38:43 -0700451 @AllocationIndex(11) public int visible;
Joe Onorato85a02a82009-09-08 12:34:22 -0700452 @AllocationIndex(12) public int zoom;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700453 }
454
455 public RolloRS() {
456 }
457
458 public void init(Resources res, int width, int height) {
459 mRes = res;
460 mWidth = width;
461 mHeight = height;
462 initGl();
463 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700464 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700465 initRs();
466 }
467
468 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700469 Sampler.Builder sb = new Sampler.Builder(mRS);
470 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
471 sb.setMag(Sampler.Value.LINEAR);
472 sb.setWrapS(Sampler.Value.CLAMP);
473 sb.setWrapT(Sampler.Value.CLAMP);
474 mSampler = sb.create();
475
476 sb.setMin(Sampler.Value.NEAREST);
477 sb.setMag(Sampler.Value.NEAREST);
478 mSamplerText = sb.create();
479
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700480 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
481 mPFDebug = dbg.create();
482 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700483
484 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
485 bf.setTexEnable(true, 0);
486 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
487 mPFImages = bf.create();
488 mPFImages.setName("PF");
489 mPFImages.bindSampler(mSampler, 0);
490
491 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700492 //mPFText = bf.create();
493 mPFText = (new ProgramFragment.Builder(mRS, null, null)).create();
Joe Onorato93839052009-08-06 20:34:32 -0700494 mPFText.setName("PFText");
495 mPFText.bindSampler(mSamplerText, 0);
496
497 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700498 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700499 bs.setDitherEnable(false);
500 bs.setDepthMask(true);
501 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
502 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
503 mPSBackground = bs.create();
504 mPSBackground.setName("PFS");
505
506 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
507 bs.setDepthMask(false);
508 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
509 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
510 mPSText = bs.create();
511 mPSText.setName("PFSText");
512
513 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
514 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
515
516 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
517 mPV = pvb.create();
518 mPV.setName("PV");
519 mPV.bindAllocation(mPVAlloc);
520
521 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
522 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
523
524 pvb.setTextureMatrixEnable(true);
525 mPVOrtho = pvb.create();
526 mPVOrtho.setName("PVOrtho");
527 mPVOrtho.bindAllocation(mPVOrthoAlloc);
528
529 mRS.contextBindProgramVertex(mPV);
530
Joe Onorato6665c0f2009-09-02 15:27:24 -0700531 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
532 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32,
533 mTouchXBorders.length);
534 mAllocTouchXBorders.data(mTouchXBorders);
535
536 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
537 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32,
538 mTouchYBorders.length);
539 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onorato93839052009-08-06 20:34:32 -0700540
541 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700542 }
543
Joe Onorato1feb3a82009-08-08 22:32:00 -0700544 private void initData() {
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700545 mParams = new Params(mRS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700546 mState = new State(mRS);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700547
Joe Onoratobf15cb42009-08-07 14:33:40 -0700548 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700549
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700550 mParams.bubbleWidth = bubble.getBubbleWidth();
551 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
552 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
553 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700554
Joe Onoratoc567acb2009-08-31 14:34:43 -0700555 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700556 R.drawable.all_apps_button_pow2, Element.RGBA_8888, false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700557 mScrollHandle.uploadToTexture(0);
558 mParams.scrollHandleId = mScrollHandle.getID();
559 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
560 mParams.scrollHandleTextureWidth = 128;
561 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700562
Joe Onoratoc567acb2009-08-31 14:34:43 -0700563
Joe Onorato1feb3a82009-08-08 22:32:00 -0700564 mParams.save();
565 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566
Joe Onorato6665c0f2009-09-02 15:27:24 -0700567 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
568 Bitmap.Config.ARGB_8888);
569 Bitmap selectionBitmap = mSelectionBitmap;
570 Paint paint = new Paint();
571 float radius = 12 * getContext().getResources().getDisplayMetrics().density;
572 //paint.setMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.OUTER));
573 Canvas canvas = new Canvas(selectionBitmap);
574 canvas.drawColor(0xffff0000);
575
576 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
577 Element.RGBA_8888, false);
578 mSelectedIcon.uploadToTexture(0);
579
580 mState.selectedIconTexture = mSelectedIcon.getID();
581
582 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400583 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700584 }
585
Joe Onorato1feb3a82009-08-08 22:32:00 -0700586 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700587 ScriptC.Builder sb = new ScriptC.Builder(mRS);
588 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700589 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700590 sb.addDefines(Defines.class);
Joe Onorato93839052009-08-06 20:34:32 -0700591 mScript = sb.create();
592 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
593
Joe Onoratod769a632009-08-11 17:09:02 -0700594 mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
595 mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
596 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700597 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700598 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
599 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700600
601 mRS.contextBindRootScript(mScript);
602 }
Joe Onorato93839052009-08-06 20:34:32 -0700603
Joe Onorato9c1289c2009-08-17 11:03:03 -0400604 private void setApps(ArrayList<ApplicationInfo> list) {
605 final int count = list != null ? list.size() : 0;
606 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700607 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400608 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
609
610 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700611 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400612 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
613
614 Element ie8888 = Element.RGBA_8888;
615
616 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
617
618 for (int i=0; i<count; i++) {
619 final ApplicationInfo item = list.get(i);
620
621 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700622 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400623 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700624 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400625
626 mIcons[i].uploadToTexture(0);
627 mLabels[i].uploadToTexture(0);
628
Joe Onorato6665c0f2009-09-02 15:27:24 -0700629 mIconIds[i] = mIcons[i].getID();
630 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 }
632
Joe Onorato6665c0f2009-09-02 15:27:24 -0700633 mAllocIconID.data(mIconIds);
634 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635
636 mState.iconCount = count;
637
638 Log.d("AllAppsView", "mScript=" + mScript + " mAllocIconID=" + mAllocIconID);
639
640 if (mScript != null) { // wtf
641 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
642 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
643 }
644
645 mState.save();
646 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700647
648 void initTouchState() {
649 int width = getWidth();
650 int height = getHeight();
651
652 int iconsSize;
653 if (width < height) {
654 iconsSize = width;
655 } else {
656 iconsSize = height;
657 }
658 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
659 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
660
661 int centerY = (height / 2) - (int)(cellHeight * 0.35f);
662 mTouchYBorders[0] = centerY - (int)(2.4f * cellHeight);
663 mTouchYBorders[1] = centerY - (int)(1.15f * cellHeight);
664 mTouchYBorders[2] = centerY;
665 mTouchYBorders[3] = centerY + (int)(1.15f * cellHeight);;
666 mTouchYBorders[4] = centerY + (int)(2.4f * cellHeight);
667
668 mAllocTouchYBorders.data(mTouchYBorders);
669
670 int centerX = (width / 2);
671 mTouchXBorders[0] = centerX - (2 * cellWidth);
672 mTouchXBorders[1] = centerX - (int)(0.83f * cellWidth);;
673 mTouchXBorders[2] = centerX;
674 mTouchXBorders[3] = centerX + (int)(0.83f * cellWidth);;
675 mTouchXBorders[4] = centerX + (2 * cellWidth);
676
677 mAllocTouchXBorders.data(mTouchXBorders);
678 }
679
680 int chooseTappedIcon(int x, int y, int scrollX, int currentPage) {
681 int col = -1;
682 int row = -1;
683
684 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
685 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
686 col = i;
687 break;
688 }
689 }
690 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
691 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
692 row = i;
693 break;
694 }
695 }
696
697 if (row < 0 || col < 0) {
698 return -1;
699 }
700
701 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
702 + (row * Defines.ROWS_PER_PAGE) + col;
703 }
704
705 /**
706 * You need to call save() on mState on your own after calling this.
707 */
708 void selectIcon(int x, int y, int scrollX, int currentPage) {
709 int iconCount = mAllAppsList.size();
710 int index = chooseTappedIcon(x, y, scrollX, currentPage);
711 if (index < 0 || index >= iconCount) {
712 mState.selectedIconIndex = -1;
713 return;
714 } else {
715 mState.selectedIconIndex = index;
716 }
717 }
718
719 /**
720 * You need to call save() on mState on your own after calling this.
721 */
722 void clearSelectedIcon() {
723 mState.selectedIconIndex = -1;
724 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400725 }
Joe Onorato93839052009-08-06 20:34:32 -0700726}
727
728