blob: f7908b6b5a29062cdaba399d55a1683a95d0c56a [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);
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;
203 mRollo.mState.adjustedDeceleration = 0;
204 mRollo.mState.save();
205 mVelocity = VelocityTracker.obtain();
206 mVelocity.addMovement(ev);
207 mStartedScrolling = false;
208 break;
209 case MotionEvent.ACTION_MOVE:
210 case MotionEvent.ACTION_OUTSIDE:
211 int slop = Math.abs(x - mLastMotionX);
212 if (!mStartedScrolling && slop < mConfig.getScaledTouchSlop()) {
213 // don't update mLastMotionX so slop is right and when we do start scrolling
214 // below, we get the right delta.
215 } else {
216 mStartedScrolling = true;
217 mRollo.clearSelectedIcon();
218 deltaX = x - mLastMotionX;
219 mVelocity.addMovement(ev);
220 mRollo.mState.currentScrollX = mLastScrollX;
221 mLastScrollX += deltaX;
222 mRollo.mState.scrollX = mLastScrollX;
223 mRollo.mState.save();
224 mLastMotionX = x;
225 }
226 break;
227 case MotionEvent.ACTION_UP:
228 case MotionEvent.ACTION_CANCEL:
229 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
230 mConfig.getScaledMaximumFlingVelocity());
231 mRollo.mState.flingTimeMs = (int)SystemClock.uptimeMillis(); // TODO: use long
232 mRollo.mState.flingVelocityX = (int)mVelocity.getXVelocity();
233 mRollo.clearSelectedIcon();
234 mRollo.mState.save();
235 mLastMotionX = -10000;
236 mVelocity.recycle();
237 mVelocity = null;
238 break;
Joe Onorato93839052009-08-06 20:34:32 -0700239 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700240
241 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700242 }
243
Joe Onorato6665c0f2009-09-02 15:27:24 -0700244 public void onClick(View v) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400245 if (mLocks != 0) {
246 return;
247 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700248 int index = mRollo.mState.selectedIconIndex;
249 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
250 ApplicationInfo app = mAllAppsList.get(index);
251 mLauncher.startActivitySafely(app.intent);
252 }
253 }
254
255 public boolean onLongClick(View v) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400256 if (mLocks != 0) {
257 return true;
258 }
Joe Onorato5162ea92009-09-03 09:39:42 -0700259 int index = mRollo.mState.selectedIconIndex;
260 Log.d(TAG, "long click! velocity=" + mRollo.mState.flingVelocityX + " index=" + index);
261 if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) {
262 ApplicationInfo app = mAllAppsList.get(index);
263
264 // We don't really have an accurate location to use. This will do.
265 int screenX = mMotionDownRawX - (Defines.ICON_WIDTH_PX / 2);
266 int screenY = mMotionDownRawY - Defines.ICON_HEIGHT_PX;
267
268 int left = (Defines.ICON_TEXTURE_WIDTH_PX - Defines.ICON_WIDTH_PX) / 2;
269 int top = (Defines.ICON_TEXTURE_HEIGHT_PX - Defines.ICON_HEIGHT_PX) / 2;
270 mDragController.startDrag(app.iconBitmap, screenX, screenY,
271 left, top, Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
272 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700273
Joe Onoratofb0ca672009-09-14 17:55:46 -0400274 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700275 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700276 return true;
277 }
278
Joe Onorato5162ea92009-09-03 09:39:42 -0700279 public void setDragController(DragController dragger) {
280 mDragController = dragger;
281 }
282
283 public void onDropCompleted(View target, boolean success) {
284 }
285
Joe Onorato85a02a82009-09-08 12:34:22 -0700286 private static final int SCALE_SCALE = 100000;
287
Joe Onorato85a02a82009-09-08 12:34:22 -0700288 public void setScale(float amount) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400289 cancelLongPress();
Joe Onorato85a02a82009-09-08 12:34:22 -0700290 mRollo.mState.read();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400291 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700292 if (amount > 0.001f) {
293 mRollo.mState.visible = 1;
294 mRollo.mState.zoom = (int)(SCALE_SCALE*amount);
295 } else {
296 mRollo.mState.visible = 0;
297 mRollo.mState.zoom = 0;
298 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400299 if (amount > 0.001f && amount < 0.999f) {
300 mLocks |= LOCK_ZOOMING;
301 } else {
302 mLocks &= ~LOCK_ZOOMING;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700303 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400304 mRollo.mState.save();
305 }
306
307 public boolean isZooming() {
308 return (mLocks & LOCK_ZOOMING) != 0;
309 }
310
311 public boolean isVisible() {
312 return mRollo != null && mRollo.mState.visible != 0;
313 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700314
Joe Onorato93839052009-08-06 20:34:32 -0700315 @Override
316 public boolean onTrackballEvent(MotionEvent ev)
317 {
318 float x = ev.getX();
319 float y = ev.getY();
320 //Float tx = new Float(x);
321 //Float ty = new Float(y);
322 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
323
324
325 return true;
326 }
327
Joe Onorato9c1289c2009-08-17 11:03:03 -0400328 public void setApps(ArrayList<ApplicationInfo> list) {
329 mAllAppsList = list;
330 if (mRollo != null) {
331 mRollo.setApps(list);
332 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700333 mPageCount = countPages(list.size());
Joe Onorato6665c0f2009-09-02 15:27:24 -0700334 Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list);
Joe Onoratofb0ca672009-09-14 17:55:46 -0400335 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700336 }
337
338 private void invokeIcon(int index) {
339 Log.d(TAG, "launch it!!!! index=" + index);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700340 }
341
342 private static int countPages(int iconCount) {
343 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
344 int pages = iconCount / iconsPerPage;
345 if (pages*iconsPerPage != iconCount) {
346 pages++;
347 }
348 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400349 }
350
Joe Onorato93839052009-08-06 20:34:32 -0700351 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700352
Joe Onorato1feb3a82009-08-08 22:32:00 -0700353 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700354
Joe Onorato93839052009-08-06 20:34:32 -0700355 private int mWidth;
356 private int mHeight;
357
358 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700359 private Script mScript;
360 private Sampler mSampler;
361 private Sampler mSamplerText;
362 private ProgramStore mPSBackground;
363 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700364 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700365 private ProgramFragment mPFImages;
366 private ProgramFragment mPFText;
367 private ProgramVertex mPV;
368 private ProgramVertex.MatrixAllocation mPVAlloc;
369 private ProgramVertex mPVOrtho;
370 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700371
Joe Onoratoc567acb2009-08-31 14:34:43 -0700372 private Allocation mScrollHandle;
373
Joe Onoratobf15cb42009-08-07 14:33:40 -0700374 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700375 private int[] mIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700376 private Allocation mAllocIconID;
377
Joe Onoratobf15cb42009-08-07 14:33:40 -0700378 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700379 private int[] mLabelIds;
Joe Onorato93839052009-08-06 20:34:32 -0700380 private Allocation mAllocLabelID;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700381 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700382
Joe Onorato6665c0f2009-09-02 15:27:24 -0700383 private int[] mTouchYBorders;
384 private Allocation mAllocTouchYBorders;
385 private int[] mTouchXBorders;
386 private Allocation mAllocTouchXBorders;
387
388 private Bitmap mSelectionBitmap;
Joe Onorato93839052009-08-06 20:34:32 -0700389
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700390 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700391 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700392
393 class Params extends IntAllocation {
394 Params(RenderScript rs) {
395 super(rs);
396 }
397 @AllocationIndex(0) public int bubbleWidth;
398 @AllocationIndex(1) public int bubbleHeight;
399 @AllocationIndex(2) public int bubbleBitmapWidth;
400 @AllocationIndex(3) public int bubbleBitmapHeight;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700401 @AllocationIndex(4) public int scrollHandleId;
402 @AllocationIndex(5) public int scrollHandleTextureWidth;
403 @AllocationIndex(6) public int scrollHandleTextureHeight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700404 }
405
Joe Onorato1feb3a82009-08-08 22:32:00 -0700406 class State extends IntAllocation {
407 State(RenderScript rs) {
408 super(rs);
409 }
410 @AllocationIndex(0) public int iconCount;
411 @AllocationIndex(1) public int scrollX;
Joe Onoratod769a632009-08-11 17:09:02 -0700412 @AllocationIndex(2) public int flingTimeMs;
413 @AllocationIndex(3) public int flingVelocityX;
414 @AllocationIndex(4) public int adjustedDeceleration;
415 @AllocationIndex(5) public int currentScrollX;
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700416 @AllocationIndex(6) public int flingDuration;
417 @AllocationIndex(7) public int flingEndPos;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700418 @AllocationIndex(8) public int startScrollX;
419 @AllocationIndex(9) public int selectedIconIndex = -1;
420 @AllocationIndex(10) public int selectedIconTexture;
Joe Onorato006b25f2009-09-03 11:38:43 -0700421 @AllocationIndex(11) public int visible;
Joe Onorato85a02a82009-09-08 12:34:22 -0700422 @AllocationIndex(12) public int zoom;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700423 }
424
425 public RolloRS() {
426 }
427
428 public void init(Resources res, int width, int height) {
429 mRes = res;
430 mWidth = width;
431 mHeight = height;
432 initGl();
433 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700434 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700435 initRs();
436 }
437
438 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700439 Sampler.Builder sb = new Sampler.Builder(mRS);
440 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
441 sb.setMag(Sampler.Value.LINEAR);
442 sb.setWrapS(Sampler.Value.CLAMP);
443 sb.setWrapT(Sampler.Value.CLAMP);
444 mSampler = sb.create();
445
446 sb.setMin(Sampler.Value.NEAREST);
447 sb.setMag(Sampler.Value.NEAREST);
448 mSamplerText = sb.create();
449
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700450 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
451 mPFDebug = dbg.create();
452 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700453
454 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
455 bf.setTexEnable(true, 0);
456 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
457 mPFImages = bf.create();
458 mPFImages.setName("PF");
459 mPFImages.bindSampler(mSampler, 0);
460
461 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700462 //mPFText = bf.create();
463 mPFText = (new ProgramFragment.Builder(mRS, null, null)).create();
Joe Onorato93839052009-08-06 20:34:32 -0700464 mPFText.setName("PFText");
465 mPFText.bindSampler(mSamplerText, 0);
466
467 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700468 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700469 bs.setDitherEnable(false);
470 bs.setDepthMask(true);
471 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
472 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
473 mPSBackground = bs.create();
474 mPSBackground.setName("PFS");
475
476 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
477 bs.setDepthMask(false);
478 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
479 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
480 mPSText = bs.create();
481 mPSText.setName("PFSText");
482
483 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
484 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
485
486 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
487 mPV = pvb.create();
488 mPV.setName("PV");
489 mPV.bindAllocation(mPVAlloc);
490
491 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
492 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
493
494 pvb.setTextureMatrixEnable(true);
495 mPVOrtho = pvb.create();
496 mPVOrtho.setName("PVOrtho");
497 mPVOrtho.bindAllocation(mPVOrthoAlloc);
498
499 mRS.contextBindProgramVertex(mPV);
500
Joe Onorato6665c0f2009-09-02 15:27:24 -0700501 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
502 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32,
503 mTouchXBorders.length);
504 mAllocTouchXBorders.data(mTouchXBorders);
505
506 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
507 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32,
508 mTouchYBorders.length);
509 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onorato93839052009-08-06 20:34:32 -0700510
511 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700512 }
513
Joe Onorato1feb3a82009-08-08 22:32:00 -0700514 private void initData() {
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700515 mParams = new Params(mRS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700516 mState = new State(mRS);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700517
Joe Onoratobf15cb42009-08-07 14:33:40 -0700518 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700519
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700520 mParams.bubbleWidth = bubble.getBubbleWidth();
521 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
522 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
523 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700524
Joe Onoratoc567acb2009-08-31 14:34:43 -0700525 mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700526 R.drawable.all_apps_button_pow2, Element.RGBA_8888, false);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700527 mScrollHandle.uploadToTexture(0);
528 mParams.scrollHandleId = mScrollHandle.getID();
529 Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId);
530 mParams.scrollHandleTextureWidth = 128;
531 mParams.scrollHandleTextureHeight = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700532
Joe Onoratoc567acb2009-08-31 14:34:43 -0700533
Joe Onorato1feb3a82009-08-08 22:32:00 -0700534 mParams.save();
535 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400536
Joe Onorato6665c0f2009-09-02 15:27:24 -0700537 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX,
538 Bitmap.Config.ARGB_8888);
539 Bitmap selectionBitmap = mSelectionBitmap;
540 Paint paint = new Paint();
541 float radius = 12 * getContext().getResources().getDisplayMetrics().density;
542 //paint.setMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.OUTER));
543 Canvas canvas = new Canvas(selectionBitmap);
544 canvas.drawColor(0xffff0000);
545
546 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
547 Element.RGBA_8888, false);
548 mSelectedIcon.uploadToTexture(0);
549
550 mState.selectedIconTexture = mSelectedIcon.getID();
551
552 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400553 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700554 }
555
Joe Onorato1feb3a82009-08-08 22:32:00 -0700556 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700557 ScriptC.Builder sb = new ScriptC.Builder(mRS);
558 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700559 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700560 sb.addDefines(Defines.class);
Joe Onorato93839052009-08-06 20:34:32 -0700561 mScript = sb.create();
562 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
563
Joe Onoratod769a632009-08-11 17:09:02 -0700564 mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
565 mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
566 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700567 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700568 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
569 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700570
571 mRS.contextBindRootScript(mScript);
572 }
Joe Onorato93839052009-08-06 20:34:32 -0700573
Joe Onorato9c1289c2009-08-17 11:03:03 -0400574 private void setApps(ArrayList<ApplicationInfo> list) {
575 final int count = list != null ? list.size() : 0;
576 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700577 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400578 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
579
580 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700581 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400582 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
583
584 Element ie8888 = Element.RGBA_8888;
585
586 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
587
588 for (int i=0; i<count; i++) {
589 final ApplicationInfo item = list.get(i);
590
591 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700592 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400593 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700594 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400595
596 mIcons[i].uploadToTexture(0);
597 mLabels[i].uploadToTexture(0);
598
Joe Onorato6665c0f2009-09-02 15:27:24 -0700599 mIconIds[i] = mIcons[i].getID();
600 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400601 }
602
Joe Onorato6665c0f2009-09-02 15:27:24 -0700603 mAllocIconID.data(mIconIds);
604 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400605
606 mState.iconCount = count;
607
608 Log.d("AllAppsView", "mScript=" + mScript + " mAllocIconID=" + mAllocIconID);
609
610 if (mScript != null) { // wtf
611 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
612 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
613 }
614
615 mState.save();
616 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700617
618 void initTouchState() {
619 int width = getWidth();
620 int height = getHeight();
621
622 int iconsSize;
623 if (width < height) {
624 iconsSize = width;
625 } else {
626 iconsSize = height;
627 }
628 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
629 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
630
631 int centerY = (height / 2) - (int)(cellHeight * 0.35f);
632 mTouchYBorders[0] = centerY - (int)(2.4f * cellHeight);
633 mTouchYBorders[1] = centerY - (int)(1.15f * cellHeight);
634 mTouchYBorders[2] = centerY;
635 mTouchYBorders[3] = centerY + (int)(1.15f * cellHeight);;
636 mTouchYBorders[4] = centerY + (int)(2.4f * cellHeight);
637
638 mAllocTouchYBorders.data(mTouchYBorders);
639
640 int centerX = (width / 2);
641 mTouchXBorders[0] = centerX - (2 * cellWidth);
642 mTouchXBorders[1] = centerX - (int)(0.83f * cellWidth);;
643 mTouchXBorders[2] = centerX;
644 mTouchXBorders[3] = centerX + (int)(0.83f * cellWidth);;
645 mTouchXBorders[4] = centerX + (2 * cellWidth);
646
647 mAllocTouchXBorders.data(mTouchXBorders);
648 }
649
650 int chooseTappedIcon(int x, int y, int scrollX, int currentPage) {
651 int col = -1;
652 int row = -1;
653
654 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
655 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
656 col = i;
657 break;
658 }
659 }
660 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
661 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
662 row = i;
663 break;
664 }
665 }
666
667 if (row < 0 || col < 0) {
668 return -1;
669 }
670
671 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
672 + (row * Defines.ROWS_PER_PAGE) + col;
673 }
674
675 /**
676 * You need to call save() on mState on your own after calling this.
677 */
678 void selectIcon(int x, int y, int scrollX, int currentPage) {
679 int iconCount = mAllAppsList.size();
680 int index = chooseTappedIcon(x, y, scrollX, currentPage);
681 if (index < 0 || index >= iconCount) {
682 mState.selectedIconIndex = -1;
683 return;
684 } else {
685 mState.selectedIconIndex = index;
686 }
687 }
688
689 /**
690 * You need to call save() on mState on your own after calling this.
691 */
692 void clearSelectedIcon() {
693 mState.selectedIconIndex = -1;
694 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400695 }
Joe Onorato93839052009-08-06 20:34:32 -0700696}
697
698