blob: 95f36d6933edddf2f185400e727bae9f9314f6a6 [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;
Jason Sams78aebd82009-09-15 13:06:59 -070031import android.renderscript.Type;
Joe Onorato93839052009-08-06 20:34:32 -070032import android.renderscript.Script;
33import android.renderscript.ScriptC;
34import android.renderscript.ProgramFragment;
35import android.renderscript.ProgramStore;
36import android.renderscript.Sampler;
37
38import android.content.Context;
39import android.content.res.Resources;
Joe Onorato7c312c12009-08-13 21:36:53 -070040import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070041import android.graphics.Bitmap;
42import android.graphics.BitmapFactory;
43import 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 Onoratofb0ca672009-09-14 17:55:46 -040067 /** Bit for mLocks for when there are icons being loaded. */
68 private static final int LOCK_ICONS_PENDING = 1;
69
70 /** Bit for mLocks for when the enter/exit is going. */
71 private static final int LOCK_ZOOMING = 2;
72
Joe Onorato6665c0f2009-09-02 15:27:24 -070073 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070074 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040075
76 /** When this is 0, modifications are allowed, when it's not, they're not.
77 * TODO: What about scrolling? */
78 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070079
Joe Onorato1feb3a82009-08-08 22:32:00 -070080 private RenderScript mRS;
81 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040082 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070083
Joe Onoratod769a632009-08-11 17:09:02 -070084 private ViewConfiguration mConfig;
Joe Onoratoc567acb2009-08-31 14:34:43 -070085 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070086 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070087 private VelocityTracker mVelocity;
88 private int mLastScrollX;
Joe Onorato1feb3a82009-08-08 22:32:00 -070089 private int mLastMotionX;
Joe Onorato5162ea92009-09-03 09:39:42 -070090 private int mMotionDownRawX;
91 private int mMotionDownRawY;
Joe Onoratoc567acb2009-08-31 14:34:43 -070092 private int mScrollHandleTop;
Joe Onorato1feb3a82009-08-08 22:32:00 -070093
Joe Onorato6665c0f2009-09-02 15:27:24 -070094 static class Defines {
95 private static float farSize(float sizeAt0) {
96 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
97 }
98
Joe Onoratoc567acb2009-08-31 14:34:43 -070099 public static final int ALLOC_PARAMS = 0;
100 public static final int ALLOC_STATE = 1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700101 public static final int ALLOC_ICON_IDS = 2;
102 public static final int ALLOC_LABEL_IDS = 3;
103 public static final int ALLOC_X_BORDERS = 4;
104 public static final int ALLOC_Y_BORDERS = 5;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700105
106 public static final int COLUMNS_PER_PAGE = 4;
107 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700108
Joe Onorato6665c0f2009-09-02 15:27:24 -0700109 public static final float RADIUS = 4.0f;
110
111 public static final int SCREEN_WIDTH_PX = 480;
112 public static final int SCREEN_HEIGHT_PX = 854;
113
114 public static final int ICON_WIDTH_PX = 64;
115 public static final int ICON_TEXTURE_WIDTH_PX = 128;
116
117 public static final int ICON_HEIGHT_PX = 64;
118 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
119 public static final float ICON_TOP_OFFSET = 0.2f;
120
121 public static final float CAMERA_Z = -2;
122 public static final float FAR_ICON_SIZE
123 = farSize(2 * ICON_WIDTH_PX / (float)SCREEN_WIDTH_PX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700124 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700125
126 public AllAppsView(Context context, AttributeSet attrs) {
127 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700128 setFocusable(true);
129 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratod769a632009-08-11 17:09:02 -0700130 mConfig = ViewConfiguration.get(context);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700131 setOnClickListener(this);
132 setOnLongClickListener(this);
Dianne Hackbornb8898d52009-09-14 22:30:18 -0700133 setOnTop(true);
Joe Onorato93839052009-08-06 20:34:32 -0700134 }
135
Joe Onorato7c312c12009-08-13 21:36:53 -0700136 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
137 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700138 }
139
Joe Onorato6665c0f2009-09-02 15:27:24 -0700140 public void setLauncher(Launcher launcher) {
141 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700142 }
143
Joe Onorato1feb3a82009-08-08 22:32:00 -0700144 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700145 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
146 super.surfaceChanged(holder, format, w, h);
147
Joe Onorato6665c0f2009-09-02 15:27:24 -0700148 long startTime = SystemClock.uptimeMillis();
149
Jason Samsb58cbdc2009-08-21 16:56:58 -0700150 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700151 mRollo = new RolloRS();
152 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400153 if (mAllAppsList != null) {
154 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700155 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400156 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700157
158 Resources res = getContext().getResources();
159 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
160 mScrollHandleTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700161
162 long endTime = SystemClock.uptimeMillis();
163 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700164 }
165
166 @Override
167 public boolean onKeyDown(int keyCode, KeyEvent event)
168 {
Joe Onorato93839052009-08-06 20:34:32 -0700169 // this method doesn't work when 'extends View' include 'extends ScrollView'.
170 return super.onKeyDown(keyCode, event);
171 }
172
Joe Onorato93839052009-08-06 20:34:32 -0700173 @Override
174 public boolean onTouchEvent(MotionEvent ev)
175 {
Joe Onoratoe3406a22009-09-03 14:36:25 -0700176 if (mRollo.mState.visible == 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700177 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700178 }
179
Joe Onoratofb0ca672009-09-14 17:55:46 -0400180 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700181 return true;
182 }
183
184 super.onTouchEvent(ev);
185
Joe Onoratofb0ca672009-09-14 17:55:46 -0400186 int x = (int)ev.getX();
187 int deltaX;
188 switch (ev.getAction()) {
189 case MotionEvent.ACTION_DOWN:
190 mMotionDownRawX = (int)ev.getRawX();
191 mMotionDownRawY = (int)ev.getRawY();
192 mLastMotionX = x;
193 mRollo.mState.read();
194 mRollo.mState.startScrollX = mRollo.mState.scrollX = mLastScrollX
195 = mRollo.mState.currentScrollX;
196 if (mRollo.mState.flingVelocityX != 0) {
197 mRollo.clearSelectedIcon();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700198 } else {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400199 mRollo.selectIcon(x, (int)ev.getY(), mRollo.mState.startScrollX,
200 (-mRollo.mState.startScrollX / Defines.SCREEN_WIDTH_PX));
Joe Onoratoc567acb2009-08-31 14:34:43 -0700201 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400202 mRollo.mState.flingVelocityX = 0;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400203 mRollo.mState.save();
204 mVelocity = VelocityTracker.obtain();
205 mVelocity.addMovement(ev);
206 mStartedScrolling = false;
207 break;
208 case MotionEvent.ACTION_MOVE:
209 case MotionEvent.ACTION_OUTSIDE:
210 int slop = Math.abs(x - mLastMotionX);
211 if (!mStartedScrolling && slop < mConfig.getScaledTouchSlop()) {
212 // don't update mLastMotionX so slop is right and when we do start scrolling
213 // below, we get the right delta.
214 } else {
215 mStartedScrolling = true;
216 mRollo.clearSelectedIcon();
217 deltaX = x - mLastMotionX;
218 mVelocity.addMovement(ev);
219 mRollo.mState.currentScrollX = mLastScrollX;
220 mLastScrollX += deltaX;
221 mRollo.mState.scrollX = mLastScrollX;
222 mRollo.mState.save();
223 mLastMotionX = x;
224 }
225 break;
226 case MotionEvent.ACTION_UP:
227 case MotionEvent.ACTION_CANCEL:
228 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
229 mConfig.getScaledMaximumFlingVelocity());
230 mRollo.mState.flingTimeMs = (int)SystemClock.uptimeMillis(); // TODO: use long
231 mRollo.mState.flingVelocityX = (int)mVelocity.getXVelocity();
232 mRollo.clearSelectedIcon();
233 mRollo.mState.save();
234 mLastMotionX = -10000;
235 mVelocity.recycle();
236 mVelocity = null;
237 break;
Joe Onorato93839052009-08-06 20:34:32 -0700238 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700239
240 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700241 }
242
Joe Onorato6665c0f2009-09-02 15:27:24 -0700243 public void onClick(View v) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400244 if (mLocks != 0) {
245 return;
246 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700247 int index = mRollo.mState.selectedIconIndex;
248 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
249 ApplicationInfo app = mAllAppsList.get(index);
250 mLauncher.startActivitySafely(app.intent);
251 }
252 }
253
254 public boolean onLongClick(View v) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400255 if (mLocks != 0) {
256 return true;
257 }
Joe Onorato5162ea92009-09-03 09:39:42 -0700258 int index = mRollo.mState.selectedIconIndex;
259 Log.d(TAG, "long click! velocity=" + mRollo.mState.flingVelocityX + " index=" + index);
260 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
261 ApplicationInfo app = mAllAppsList.get(index);
262
263 // We don't really have an accurate location to use. This will do.
264 int screenX = mMotionDownRawX - (Defines.ICON_WIDTH_PX / 2);
265 int screenY = mMotionDownRawY - Defines.ICON_HEIGHT_PX;
266
267 int left = (Defines.ICON_TEXTURE_WIDTH_PX - Defines.ICON_WIDTH_PX) / 2;
268 int top = (Defines.ICON_TEXTURE_HEIGHT_PX - Defines.ICON_HEIGHT_PX) / 2;
269 mDragController.startDrag(app.iconBitmap, screenX, screenY,
270 left, top, Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
271 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700272
Joe Onoratofb0ca672009-09-14 17:55:46 -0400273 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700274 }
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 Onorato85a02a82009-09-08 12:34:22 -0700285 public void setScale(float amount) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400286 cancelLongPress();
Joe Onorato85a02a82009-09-08 12:34:22 -0700287 mRollo.mState.read();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400288 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700289 if (amount > 0.001f) {
290 mRollo.mState.visible = 1;
Jason Sams78aebd82009-09-15 13:06:59 -0700291 mRollo.mState.zoom = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700292 } else {
293 mRollo.mState.visible = 0;
294 mRollo.mState.zoom = 0;
295 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400296 if (amount > 0.001f && amount < 0.999f) {
297 mLocks |= LOCK_ZOOMING;
298 } else {
299 mLocks &= ~LOCK_ZOOMING;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700300 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400301 mRollo.mState.save();
302 }
303
304 public boolean isZooming() {
305 return (mLocks & LOCK_ZOOMING) != 0;
306 }
307
308 public boolean isVisible() {
309 return mRollo != null && mRollo.mState.visible != 0;
310 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700311
Joe Onorato93839052009-08-06 20:34:32 -0700312 @Override
313 public boolean onTrackballEvent(MotionEvent ev)
314 {
315 float x = ev.getX();
316 float y = ev.getY();
317 //Float tx = new Float(x);
318 //Float ty = new Float(y);
319 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
320
321
322 return true;
323 }
324
Joe Onorato9c1289c2009-08-17 11:03:03 -0400325 public void setApps(ArrayList<ApplicationInfo> list) {
326 mAllAppsList = list;
327 if (mRollo != null) {
328 mRollo.setApps(list);
329 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700330 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700331 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400332 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700333 }
334
335 private void invokeIcon(int index) {
336 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700337 }
338
339 private static int countPages(int iconCount) {
340 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
341 int pages = iconCount / iconsPerPage;
342 if (pages*iconsPerPage != iconCount) {
343 pages++;
344 }
345 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400346 }
347
Joe Onorato93839052009-08-06 20:34:32 -0700348 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700349
Joe Onorato1feb3a82009-08-08 22:32:00 -0700350 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700351
Joe Onorato93839052009-08-06 20:34:32 -0700352 private int mWidth;
353 private int mHeight;
354
355 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700356 private Script mScript;
357 private Sampler mSampler;
358 private Sampler mSamplerText;
359 private ProgramStore mPSBackground;
360 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700361 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700362 private ProgramFragment mPFImages;
363 private ProgramFragment mPFText;
364 private ProgramVertex mPV;
365 private ProgramVertex.MatrixAllocation mPVAlloc;
366 private ProgramVertex mPVOrtho;
367 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700368
Joe Onoratoc567acb2009-08-31 14:34:43 -0700369 private Allocation mScrollHandle;
370
Joe Onoratobf15cb42009-08-07 14:33:40 -0700371 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700372 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700373 private Allocation mAllocIconID;
374
Joe Onoratobf15cb42009-08-07 14:33:40 -0700375 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700376 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700377 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700378 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700379
Joe Onorato6665c0f2009-09-02 15:27:24 -0700380 private int[] mTouchYBorders;
381 private Allocation mAllocTouchYBorders;
382 private int[] mTouchXBorders;
383 private Allocation mAllocTouchXBorders;
384
385 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400386 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700387
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700388 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700389 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700390
Jason Sams78aebd82009-09-15 13:06:59 -0700391 class BaseAlloc {
392 Allocation mAlloc;
393 Type mType;
394
395 void save() {
396 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700397 }
Jason Sams78aebd82009-09-15 13:06:59 -0700398
399 void read() {
400 mAlloc.read(this);
401 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700402 }
403
Jason Sams78aebd82009-09-15 13:06:59 -0700404 class Params extends BaseAlloc {
405 Params() {
406 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
407 mAlloc = Allocation.createTyped(mRS, mType);
408 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700409 }
Jason Sams78aebd82009-09-15 13:06:59 -0700410 public int bubbleWidth;
411 public int bubbleHeight;
412 public int bubbleBitmapWidth;
413 public int bubbleBitmapHeight;
414 public int scrollHandleId;
415 public int scrollHandleTextureWidth;
416 public int scrollHandleTextureHeight;
417 }
418
419 class State extends BaseAlloc {
420 public int iconCount;
421 public int scrollX;
422 public int flingTimeMs;
423 public float flingVelocityX;
424 public int currentScrollX;
425 public int flingDuration;
426 public int flingEndPos;
427 public int startScrollX;
428 public int selectedIconIndex = -1;
429 public int selectedIconTexture;
430 public int visible;
431 public float zoom;
432
433 State() {
434 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
435 mAlloc = Allocation.createTyped(mRS, mType);
436 save();
437 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700438 }
439
440 public RolloRS() {
441 }
442
443 public void init(Resources res, int width, int height) {
444 mRes = res;
445 mWidth = width;
446 mHeight = height;
447 initGl();
448 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700449 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700450 initRs();
451 }
452
453 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700454 Sampler.Builder sb = new Sampler.Builder(mRS);
455 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
456 sb.setMag(Sampler.Value.LINEAR);
457 sb.setWrapS(Sampler.Value.CLAMP);
458 sb.setWrapT(Sampler.Value.CLAMP);
459 mSampler = sb.create();
460
461 sb.setMin(Sampler.Value.NEAREST);
462 sb.setMag(Sampler.Value.NEAREST);
463 mSamplerText = sb.create();
464
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700465 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
466 mPFDebug = dbg.create();
467 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700468
469 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
470 bf.setTexEnable(true, 0);
471 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
472 mPFImages = bf.create();
473 mPFImages.setName("PF");
474 mPFImages.bindSampler(mSampler, 0);
475
476 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700477 //mPFText = bf.create();
478 mPFText = (new ProgramFragment.Builder(mRS, null, null)).create();
Joe Onorato93839052009-08-06 20:34:32 -0700479 mPFText.setName("PFText");
480 mPFText.bindSampler(mSamplerText, 0);
481
482 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700483 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700484 bs.setDitherEnable(false);
485 bs.setDepthMask(true);
486 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
487 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
488 mPSBackground = bs.create();
489 mPSBackground.setName("PFS");
490
491 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
492 bs.setDepthMask(false);
493 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
494 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
495 mPSText = bs.create();
496 mPSText.setName("PFSText");
497
498 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
499 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
500
501 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
502 mPV = pvb.create();
503 mPV.setName("PV");
504 mPV.bindAllocation(mPVAlloc);
505
506 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
507 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
508
509 pvb.setTextureMatrixEnable(true);
510 mPVOrtho = pvb.create();
511 mPVOrtho.setName("PVOrtho");
512 mPVOrtho.bindAllocation(mPVOrthoAlloc);
513
514 mRS.contextBindProgramVertex(mPV);
515
Joe Onorato6665c0f2009-09-02 15:27:24 -0700516 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
517 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32,
518 mTouchXBorders.length);
519 mAllocTouchXBorders.data(mTouchXBorders);
520
521 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
522 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32,
523 mTouchYBorders.length);
524 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onorato93839052009-08-06 20:34:32 -0700525
526 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700527 }
Jason Sams78aebd82009-09-15 13:06:59 -0700528
Joe Onorato1feb3a82009-08-08 22:32:00 -0700529 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700530 mParams = new Params();
531 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700532
Joe Onoratobf15cb42009-08-07 14:33:40 -0700533 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700534
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700535 mParams.bubbleWidth = bubble.getBubbleWidth();
536 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
537 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
538 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700539
Joe Onoratoc567acb2009-08-31 14:34:43 -0700540 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700541 R.drawable.all_apps_button_pow2, Element.RGBA_8888, false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700542 mScrollHandle.uploadToTexture(0);
543 mParams.scrollHandleId = mScrollHandle.getID();
544 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
545 mParams.scrollHandleTextureWidth = 128;
546 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700547
Joe Onoratoc567acb2009-08-31 14:34:43 -0700548
Joe Onorato1feb3a82009-08-08 22:32:00 -0700549 mParams.save();
550 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400551
Joe Onorato1291a8c2009-09-15 15:07:25 -0400552 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
553 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
554 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700555
556 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400557 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700558 }
559
Joe Onorato1feb3a82009-08-08 22:32:00 -0700560 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700561 ScriptC.Builder sb = new ScriptC.Builder(mRS);
562 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700563 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700564 sb.addDefines(Defines.class);
Jason Sams78aebd82009-09-15 13:06:59 -0700565 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
566 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Joe Onorato93839052009-08-06 20:34:32 -0700567 mScript = sb.create();
568 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
569
Jason Sams78aebd82009-09-15 13:06:59 -0700570 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
571 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
Joe Onoratod769a632009-08-11 17:09:02 -0700572 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700573 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700574 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
575 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700576
577 mRS.contextBindRootScript(mScript);
578 }
Joe Onorato93839052009-08-06 20:34:32 -0700579
Joe Onorato9c1289c2009-08-17 11:03:03 -0400580 private void setApps(ArrayList<ApplicationInfo> list) {
581 final int count = list != null ? list.size() : 0;
582 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700583 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400584 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
585
586 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700587 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400588 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
589
590 Element ie8888 = Element.RGBA_8888;
591
592 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
593
594 for (int i=0; i<count; i++) {
595 final ApplicationInfo item = list.get(i);
596
597 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700598 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400599 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700600 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400601
602 mIcons[i].uploadToTexture(0);
603 mLabels[i].uploadToTexture(0);
604
Joe Onorato6665c0f2009-09-02 15:27:24 -0700605 mIconIds[i] = mIcons[i].getID();
606 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400607 }
608
Joe Onorato6665c0f2009-09-02 15:27:24 -0700609 mAllocIconID.data(mIconIds);
610 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400611
612 mState.iconCount = count;
613
614 Log.d("AllAppsView", "mScript=" + mScript + " mAllocIconID=" + mAllocIconID);
615
616 if (mScript != null) { // wtf
617 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
618 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
619 }
620
621 mState.save();
622 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700623
624 void initTouchState() {
625 int width = getWidth();
626 int height = getHeight();
627
628 int iconsSize;
629 if (width < height) {
630 iconsSize = width;
631 } else {
632 iconsSize = height;
633 }
634 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
635 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
636
637 int centerY = (height / 2) - (int)(cellHeight * 0.35f);
638 mTouchYBorders[0] = centerY - (int)(2.4f * cellHeight);
639 mTouchYBorders[1] = centerY - (int)(1.15f * cellHeight);
640 mTouchYBorders[2] = centerY;
641 mTouchYBorders[3] = centerY + (int)(1.15f * cellHeight);;
642 mTouchYBorders[4] = centerY + (int)(2.4f * cellHeight);
643
644 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700645
Joe Onorato6665c0f2009-09-02 15:27:24 -0700646 int centerX = (width / 2);
647 mTouchXBorders[0] = centerX - (2 * cellWidth);
648 mTouchXBorders[1] = centerX - (int)(0.83f * cellWidth);;
649 mTouchXBorders[2] = centerX;
650 mTouchXBorders[3] = centerX + (int)(0.83f * cellWidth);;
651 mTouchXBorders[4] = centerX + (2 * cellWidth);
652
653 mAllocTouchXBorders.data(mTouchXBorders);
654 }
655
656 int chooseTappedIcon(int x, int y, int scrollX, int currentPage) {
657 int col = -1;
658 int row = -1;
659
660 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
661 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
662 col = i;
663 break;
664 }
665 }
666 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
667 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
668 row = i;
669 break;
670 }
671 }
672
673 if (row < 0 || col < 0) {
674 return -1;
675 }
676
Jason Sams78aebd82009-09-15 13:06:59 -0700677 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700678 + (row * Defines.ROWS_PER_PAGE) + col;
679 }
680
681 /**
682 * You need to call save() on mState on your own after calling this.
683 */
684 void selectIcon(int x, int y, int scrollX, int currentPage) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700685 int index = chooseTappedIcon(x, y, scrollX, currentPage);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400686 selectIcon(index);
687 }
688
689 void selectIcon(int index) {
690 Log.d(TAG, "selectIcon index=" + index);
691 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700692 if (index < 0 || index >= iconCount) {
693 mState.selectedIconIndex = -1;
694 return;
695 } else {
696 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400697
698 Bitmap selectionBitmap = mSelectionBitmap;
699
700 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
701 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
702 mAllAppsList.get(index).iconBitmap);
703
704 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
705 Element.RGBA_8888, false);
706 mSelectedIcon.uploadToTexture(0);
707 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700708 }
709 }
710
711 /**
712 * You need to call save() on mState on your own after calling this.
713 */
714 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400715 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700716 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717 }
Joe Onorato93839052009-08-06 20:34:32 -0700718}
719
720