blob: 7bb6c8fd00556322b7ec25cd3c161683c708de01 [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 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;
108
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);
Joe Onorato93839052009-08-06 20:34:32 -0700133 }
134
Joe Onorato7c312c12009-08-13 21:36:53 -0700135 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
136 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700137 }
138
Joe Onorato6665c0f2009-09-02 15:27:24 -0700139 public void setLauncher(Launcher launcher) {
140 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700141 }
142
Joe Onorato1feb3a82009-08-08 22:32:00 -0700143 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700144 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
145 super.surfaceChanged(holder, format, w, h);
146
Joe Onorato6665c0f2009-09-02 15:27:24 -0700147 long startTime = SystemClock.uptimeMillis();
148
Jason Samsb58cbdc2009-08-21 16:56:58 -0700149 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700150 mRollo = new RolloRS();
151 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400152 if (mAllAppsList != null) {
153 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700154 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700156
157 Resources res = getContext().getResources();
158 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
159 mScrollHandleTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700160
161 long endTime = SystemClock.uptimeMillis();
162 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700163 }
164
165 @Override
166 public boolean onKeyDown(int keyCode, KeyEvent event)
167 {
Joe Onorato93839052009-08-06 20:34:32 -0700168 // this method doesn't work when 'extends View' include 'extends ScrollView'.
169 return super.onKeyDown(keyCode, event);
170 }
171
Joe Onorato93839052009-08-06 20:34:32 -0700172 @Override
173 public boolean onTouchEvent(MotionEvent ev)
174 {
Joe Onoratoe3406a22009-09-03 14:36:25 -0700175 if (mRollo.mState.visible == 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700176 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700177 }
178
Joe Onoratofb0ca672009-09-14 17:55:46 -0400179 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700180 return true;
181 }
182
183 super.onTouchEvent(ev);
184
Joe Onoratofb0ca672009-09-14 17:55:46 -0400185 int x = (int)ev.getX();
186 int deltaX;
187 switch (ev.getAction()) {
188 case MotionEvent.ACTION_DOWN:
189 mMotionDownRawX = (int)ev.getRawX();
190 mMotionDownRawY = (int)ev.getRawY();
191 mLastMotionX = x;
192 mRollo.mState.read();
193 mRollo.mState.startScrollX = mRollo.mState.scrollX = mLastScrollX
194 = mRollo.mState.currentScrollX;
195 if (mRollo.mState.flingVelocityX != 0) {
196 mRollo.clearSelectedIcon();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700197 } else {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400198 mRollo.selectIcon(x, (int)ev.getY(), mRollo.mState.startScrollX,
199 (-mRollo.mState.startScrollX / Defines.SCREEN_WIDTH_PX));
Joe Onoratoc567acb2009-08-31 14:34:43 -0700200 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400201 mRollo.mState.flingVelocityX = 0;
202 mRollo.mState.adjustedDeceleration = 0;
203 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 private static final int SCALE_SCALE = 100000;
286
Joe Onorato85a02a82009-09-08 12:34:22 -0700287 public void setScale(float amount) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400288 cancelLongPress();
Joe Onorato85a02a82009-09-08 12:34:22 -0700289 mRollo.mState.read();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400290 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700291 if (amount > 0.001f) {
292 mRollo.mState.visible = 1;
293 mRollo.mState.zoom = (int)(SCALE_SCALE*amount);
294 } else {
295 mRollo.mState.visible = 0;
296 mRollo.mState.zoom = 0;
297 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400298 if (amount > 0.001f && amount < 0.999f) {
299 mLocks |= LOCK_ZOOMING;
300 } else {
301 mLocks &= ~LOCK_ZOOMING;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700302 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400303 mRollo.mState.save();
304 }
305
306 public boolean isZooming() {
307 return (mLocks & LOCK_ZOOMING) != 0;
308 }
309
310 public boolean isVisible() {
311 return mRollo != null && mRollo.mState.visible != 0;
312 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700313
Joe Onorato93839052009-08-06 20:34:32 -0700314 @Override
315 public boolean onTrackballEvent(MotionEvent ev)
316 {
317 float x = ev.getX();
318 float y = ev.getY();
319 //Float tx = new Float(x);
320 //Float ty = new Float(y);
321 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
322
323
324 return true;
325 }
326
Joe Onorato9c1289c2009-08-17 11:03:03 -0400327 public void setApps(ArrayList<ApplicationInfo> list) {
328 mAllAppsList = list;
329 if (mRollo != null) {
330 mRollo.setApps(list);
331 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700332 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700333 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400334 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700335 }
336
337 private void invokeIcon(int index) {
338 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700339 }
340
341 private static int countPages(int iconCount) {
342 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
343 int pages = iconCount / iconsPerPage;
344 if (pages*iconsPerPage != iconCount) {
345 pages++;
346 }
347 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400348 }
349
Joe Onorato93839052009-08-06 20:34:32 -0700350 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700351
Joe Onorato1feb3a82009-08-08 22:32:00 -0700352 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700353
Joe Onorato93839052009-08-06 20:34:32 -0700354 private int mWidth;
355 private int mHeight;
356
357 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700358 private Script mScript;
359 private Sampler mSampler;
360 private Sampler mSamplerText;
361 private ProgramStore mPSBackground;
362 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700363 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700364 private ProgramFragment mPFImages;
365 private ProgramFragment mPFText;
366 private ProgramVertex mPV;
367 private ProgramVertex.MatrixAllocation mPVAlloc;
368 private ProgramVertex mPVOrtho;
369 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700370
Joe Onoratoc567acb2009-08-31 14:34:43 -0700371 private Allocation mScrollHandle;
372
Joe Onoratobf15cb42009-08-07 14:33:40 -0700373 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700374 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700375 private Allocation mAllocIconID;
376
Joe Onoratobf15cb42009-08-07 14:33:40 -0700377 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700378 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700379 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700380 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700381
Joe Onorato6665c0f2009-09-02 15:27:24 -0700382 private int[] mTouchYBorders;
383 private Allocation mAllocTouchYBorders;
384 private int[] mTouchXBorders;
385 private Allocation mAllocTouchXBorders;
386
387 private Bitmap mSelectionBitmap;
Joe Onorato93839052009-08-06 20:34:32 -0700388
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700389 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700390 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700391
392 class Params extends IntAllocation {
393 Params(RenderScript rs) {
394 super(rs);
395 }
396 @AllocationIndex(0) public int bubbleWidth;
397 @AllocationIndex(1) public int bubbleHeight;
398 @AllocationIndex(2) public int bubbleBitmapWidth;
399 @AllocationIndex(3) public int bubbleBitmapHeight;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700400 @AllocationIndex(4) public int scrollHandleId;
401 @AllocationIndex(5) public int scrollHandleTextureWidth;
402 @AllocationIndex(6) public int scrollHandleTextureHeight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700403 }
404
Joe Onorato1feb3a82009-08-08 22:32:00 -0700405 class State extends IntAllocation {
406 State(RenderScript rs) {
407 super(rs);
408 }
409 @AllocationIndex(0) public int iconCount;
410 @AllocationIndex(1) public int scrollX;
Joe Onoratod769a632009-08-11 17:09:02 -0700411 @AllocationIndex(2) public int flingTimeMs;
412 @AllocationIndex(3) public int flingVelocityX;
413 @AllocationIndex(4) public int adjustedDeceleration;
414 @AllocationIndex(5) public int currentScrollX;
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700415 @AllocationIndex(6) public int flingDuration;
416 @AllocationIndex(7) public int flingEndPos;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700417 @AllocationIndex(8) public int startScrollX;
418 @AllocationIndex(9) public int selectedIconIndex = -1;
419 @AllocationIndex(10) public int selectedIconTexture;
Joe Onorato006b25f2009-09-03 11:38:43 -0700420 @AllocationIndex(11) public int visible;
Joe Onorato85a02a82009-09-08 12:34:22 -0700421 @AllocationIndex(12) public int zoom;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700422 }
423
424 public RolloRS() {
425 }
426
427 public void init(Resources res, int width, int height) {
428 mRes = res;
429 mWidth = width;
430 mHeight = height;
431 initGl();
432 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700433 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700434 initRs();
435 }
436
437 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700438 Sampler.Builder sb = new Sampler.Builder(mRS);
439 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
440 sb.setMag(Sampler.Value.LINEAR);
441 sb.setWrapS(Sampler.Value.CLAMP);
442 sb.setWrapT(Sampler.Value.CLAMP);
443 mSampler = sb.create();
444
445 sb.setMin(Sampler.Value.NEAREST);
446 sb.setMag(Sampler.Value.NEAREST);
447 mSamplerText = sb.create();
448
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700449 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
450 mPFDebug = dbg.create();
451 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700452
453 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
454 bf.setTexEnable(true, 0);
455 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
456 mPFImages = bf.create();
457 mPFImages.setName("PF");
458 mPFImages.bindSampler(mSampler, 0);
459
460 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700461 //mPFText = bf.create();
462 mPFText = (new ProgramFragment.Builder(mRS, null, null)).create();
Joe Onorato93839052009-08-06 20:34:32 -0700463 mPFText.setName("PFText");
464 mPFText.bindSampler(mSamplerText, 0);
465
466 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700467 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700468 bs.setDitherEnable(false);
469 bs.setDepthMask(true);
470 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
471 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
472 mPSBackground = bs.create();
473 mPSBackground.setName("PFS");
474
475 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
476 bs.setDepthMask(false);
477 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
478 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
479 mPSText = bs.create();
480 mPSText.setName("PFSText");
481
482 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
483 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
484
485 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
486 mPV = pvb.create();
487 mPV.setName("PV");
488 mPV.bindAllocation(mPVAlloc);
489
490 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
491 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
492
493 pvb.setTextureMatrixEnable(true);
494 mPVOrtho = pvb.create();
495 mPVOrtho.setName("PVOrtho");
496 mPVOrtho.bindAllocation(mPVOrthoAlloc);
497
498 mRS.contextBindProgramVertex(mPV);
499
Joe Onorato6665c0f2009-09-02 15:27:24 -0700500 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
501 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32,
502 mTouchXBorders.length);
503 mAllocTouchXBorders.data(mTouchXBorders);
504
505 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
506 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32,
507 mTouchYBorders.length);
508 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onorato93839052009-08-06 20:34:32 -0700509
510 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700511 }
512
Joe Onorato1feb3a82009-08-08 22:32:00 -0700513 private void initData() {
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700514 mParams = new Params(mRS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700515 mState = new State(mRS);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700516
Joe Onoratobf15cb42009-08-07 14:33:40 -0700517 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700518
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700519 mParams.bubbleWidth = bubble.getBubbleWidth();
520 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
521 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
522 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700523
Joe Onoratoc567acb2009-08-31 14:34:43 -0700524 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700525 R.drawable.all_apps_button_pow2, Element.RGBA_8888, false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700526 mScrollHandle.uploadToTexture(0);
527 mParams.scrollHandleId = mScrollHandle.getID();
528 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
529 mParams.scrollHandleTextureWidth = 128;
530 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700531
Joe Onoratoc567acb2009-08-31 14:34:43 -0700532
Joe Onorato1feb3a82009-08-08 22:32:00 -0700533 mParams.save();
534 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400535
Joe Onorato6665c0f2009-09-02 15:27:24 -0700536 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
537 Bitmap.Config.ARGB_8888);
538 Bitmap selectionBitmap = mSelectionBitmap;
539 Paint paint = new Paint();
540 float radius = 12 * getContext().getResources().getDisplayMetrics().density;
541 //paint.setMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.OUTER));
542 Canvas canvas = new Canvas(selectionBitmap);
543 canvas.drawColor(0xffff0000);
544
545 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
546 Element.RGBA_8888, false);
547 mSelectedIcon.uploadToTexture(0);
548
549 mState.selectedIconTexture = mSelectedIcon.getID();
550
551 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400552 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700553 }
554
Joe Onorato1feb3a82009-08-08 22:32:00 -0700555 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700556 ScriptC.Builder sb = new ScriptC.Builder(mRS);
557 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700558 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700559 sb.addDefines(Defines.class);
Joe Onorato93839052009-08-06 20:34:32 -0700560 mScript = sb.create();
561 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
562
Joe Onoratod769a632009-08-11 17:09:02 -0700563 mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
564 mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
565 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700566 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700567 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
568 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700569
570 mRS.contextBindRootScript(mScript);
571 }
Joe Onorato93839052009-08-06 20:34:32 -0700572
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573 private void setApps(ArrayList<ApplicationInfo> list) {
574 final int count = list != null ? list.size() : 0;
575 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700576 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400577 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
578
579 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700580 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400581 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
582
583 Element ie8888 = Element.RGBA_8888;
584
585 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
586
587 for (int i=0; i<count; i++) {
588 final ApplicationInfo item = list.get(i);
589
590 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700591 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400592 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700593 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400594
595 mIcons[i].uploadToTexture(0);
596 mLabels[i].uploadToTexture(0);
597
Joe Onorato6665c0f2009-09-02 15:27:24 -0700598 mIconIds[i] = mIcons[i].getID();
599 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400600 }
601
Joe Onorato6665c0f2009-09-02 15:27:24 -0700602 mAllocIconID.data(mIconIds);
603 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400604
605 mState.iconCount = count;
606
607 Log.d("AllAppsView", "mScript=" + mScript + " mAllocIconID=" + mAllocIconID);
608
609 if (mScript != null) { // wtf
610 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
611 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
612 }
613
614 mState.save();
615 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700616
617 void initTouchState() {
618 int width = getWidth();
619 int height = getHeight();
620
621 int iconsSize;
622 if (width < height) {
623 iconsSize = width;
624 } else {
625 iconsSize = height;
626 }
627 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
628 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
629
630 int centerY = (height / 2) - (int)(cellHeight * 0.35f);
631 mTouchYBorders[0] = centerY - (int)(2.4f * cellHeight);
632 mTouchYBorders[1] = centerY - (int)(1.15f * cellHeight);
633 mTouchYBorders[2] = centerY;
634 mTouchYBorders[3] = centerY + (int)(1.15f * cellHeight);;
635 mTouchYBorders[4] = centerY + (int)(2.4f * cellHeight);
636
637 mAllocTouchYBorders.data(mTouchYBorders);
638
639 int centerX = (width / 2);
640 mTouchXBorders[0] = centerX - (2 * cellWidth);
641 mTouchXBorders[1] = centerX - (int)(0.83f * cellWidth);;
642 mTouchXBorders[2] = centerX;
643 mTouchXBorders[3] = centerX + (int)(0.83f * cellWidth);;
644 mTouchXBorders[4] = centerX + (2 * cellWidth);
645
646 mAllocTouchXBorders.data(mTouchXBorders);
647 }
648
649 int chooseTappedIcon(int x, int y, int scrollX, int currentPage) {
650 int col = -1;
651 int row = -1;
652
653 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
654 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
655 col = i;
656 break;
657 }
658 }
659 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
660 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
661 row = i;
662 break;
663 }
664 }
665
666 if (row < 0 || col < 0) {
667 return -1;
668 }
669
670 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
671 + (row * Defines.ROWS_PER_PAGE) + col;
672 }
673
674 /**
675 * You need to call save() on mState on your own after calling this.
676 */
677 void selectIcon(int x, int y, int scrollX, int currentPage) {
678 int iconCount = mAllAppsList.size();
679 int index = chooseTappedIcon(x, y, scrollX, currentPage);
680 if (index < 0 || index >= iconCount) {
681 mState.selectedIconIndex = -1;
682 return;
683 } else {
684 mState.selectedIconIndex = index;
685 }
686 }
687
688 /**
689 * You need to call save() on mState on your own after calling this.
690 */
691 void clearSelectedIcon() {
692 mState.selectedIconIndex = -1;
693 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400694 }
Joe Onorato93839052009-08-06 20:34:32 -0700695}
696
697