blob: 2199432d1059170a0bf98e5e947adad00fd908dd [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;
42import android.graphics.Canvas;
43import android.graphics.Paint;
Joe Onorato93839052009-08-06 20:34:32 -070044import android.graphics.drawable.BitmapDrawable;
45import android.graphics.drawable.Drawable;
46import android.os.Handler;
47import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070048import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070049import android.util.AttributeSet;
50import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070051import android.view.KeyEvent;
52import android.view.MotionEvent;
Joe Onorato93839052009-08-06 20:34:32 -070053import android.view.Surface;
54import android.view.SurfaceHolder;
55import android.view.SurfaceView;
Joe Onorato6665c0f2009-09-02 15:27:24 -070056import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070057import android.view.VelocityTracker;
58import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070059import android.graphics.PixelFormat;
60
61
Joe Onorato6665c0f2009-09-02 15:27:24 -070062public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070063 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040064 private static final String TAG = "Launcher.AllAppsView";
65
Joe Onoratofb0ca672009-09-14 17:55:46 -040066 /** Bit for mLocks for when there are icons being loaded. */
67 private static final int LOCK_ICONS_PENDING = 1;
68
69 /** Bit for mLocks for when the enter/exit is going. */
70 private static final int LOCK_ZOOMING = 2;
71
Joe Onorato6665c0f2009-09-02 15:27:24 -070072 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070073 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040074
75 /** When this is 0, modifications are allowed, when it's not, they're not.
76 * TODO: What about scrolling? */
77 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070078
Joe Onorato1feb3a82009-08-08 22:32:00 -070079 private RenderScript mRS;
80 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040081 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070082
Joe Onoratod769a632009-08-11 17:09:02 -070083 private ViewConfiguration mConfig;
Joe Onoratoc567acb2009-08-31 14:34:43 -070084 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070085 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070086 private VelocityTracker mVelocity;
87 private int mLastScrollX;
Joe Onorato1feb3a82009-08-08 22:32:00 -070088 private int mLastMotionX;
Joe Onorato5162ea92009-09-03 09:39:42 -070089 private int mMotionDownRawX;
90 private int mMotionDownRawY;
Joe Onoratoc567acb2009-08-31 14:34:43 -070091 private int mScrollHandleTop;
Joe Onorato1feb3a82009-08-08 22:32:00 -070092
Joe Onorato6665c0f2009-09-02 15:27:24 -070093 static class Defines {
94 private static float farSize(float sizeAt0) {
95 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
96 }
97
Joe Onoratoc567acb2009-08-31 14:34:43 -070098 public static final int ALLOC_PARAMS = 0;
99 public static final int ALLOC_STATE = 1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700100 public static final int ALLOC_ICON_IDS = 2;
101 public static final int ALLOC_LABEL_IDS = 3;
102 public static final int ALLOC_X_BORDERS = 4;
103 public static final int ALLOC_Y_BORDERS = 5;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700104
105 public static final int COLUMNS_PER_PAGE = 4;
106 public static final int ROWS_PER_PAGE = 4;
107
Joe Onorato6665c0f2009-09-02 15:27:24 -0700108 public static final float RADIUS = 4.0f;
109
110 public static final int SCREEN_WIDTH_PX = 480;
111 public static final int SCREEN_HEIGHT_PX = 854;
112
113 public static final int ICON_WIDTH_PX = 64;
114 public static final int ICON_TEXTURE_WIDTH_PX = 128;
115
116 public static final int ICON_HEIGHT_PX = 64;
117 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
118 public static final float ICON_TOP_OFFSET = 0.2f;
119
120 public static final float CAMERA_Z = -2;
121 public static final float FAR_ICON_SIZE
122 = farSize(2 * ICON_WIDTH_PX / (float)SCREEN_WIDTH_PX);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700123 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700124
125 public AllAppsView(Context context, AttributeSet attrs) {
126 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700127 setFocusable(true);
128 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratod769a632009-08-11 17:09:02 -0700129 mConfig = ViewConfiguration.get(context);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700130 setOnClickListener(this);
131 setOnLongClickListener(this);
Dianne Hackbornb8898d52009-09-14 22:30:18 -0700132 setOnTop(true);
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 Onorato1291a8c2009-09-15 15:07:25 -0400388 private Canvas mSelectionCanvas;
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 Onorato1291a8c2009-09-15 15:07:25 -0400537 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
538 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
539 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700540
541 Log.d(TAG, "initData calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700543 }
544
Joe Onorato1feb3a82009-08-08 22:32:00 -0700545 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700546 ScriptC.Builder sb = new ScriptC.Builder(mRS);
547 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700548 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700549 sb.addDefines(Defines.class);
Joe Onorato93839052009-08-06 20:34:32 -0700550 mScript = sb.create();
551 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
552
Joe Onoratod769a632009-08-11 17:09:02 -0700553 mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
554 mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
555 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
Joe Onoratod769a632009-08-11 17:09:02 -0700556 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700557 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
558 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700559
560 mRS.contextBindRootScript(mScript);
561 }
Joe Onorato93839052009-08-06 20:34:32 -0700562
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563 private void setApps(ArrayList<ApplicationInfo> list) {
564 final int count = list != null ? list.size() : 0;
565 mIcons = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700566 mIconIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400567 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
568
569 mLabels = new Allocation[count];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700570 mLabelIds = new int[count];
Joe Onorato9c1289c2009-08-17 11:03:03 -0400571 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
572
573 Element ie8888 = Element.RGBA_8888;
574
575 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
576
577 for (int i=0; i<count; i++) {
578 final ApplicationInfo item = list.get(i);
579
580 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700581 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400582 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Joe Onorato6665c0f2009-09-02 15:27:24 -0700583 Element.RGBA_8888, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400584
585 mIcons[i].uploadToTexture(0);
586 mLabels[i].uploadToTexture(0);
587
Joe Onorato6665c0f2009-09-02 15:27:24 -0700588 mIconIds[i] = mIcons[i].getID();
589 mLabelIds[i] = mLabels[i].getID();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400590 }
591
Joe Onorato6665c0f2009-09-02 15:27:24 -0700592 mAllocIconID.data(mIconIds);
593 mAllocLabelID.data(mLabelIds);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400594
595 mState.iconCount = count;
596
597 Log.d("AllAppsView", "mScript=" + mScript + " mAllocIconID=" + mAllocIconID);
598
599 if (mScript != null) { // wtf
600 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
601 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
602 }
603
Joe Onorato1291a8c2009-09-15 15:07:25 -0400604 if (mAllAppsList != null) {
605 selectIcon(0); // TODO remove
606 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400607 mState.save();
608 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700609
610 void initTouchState() {
611 int width = getWidth();
612 int height = getHeight();
613
614 int iconsSize;
615 if (width < height) {
616 iconsSize = width;
617 } else {
618 iconsSize = height;
619 }
620 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
621 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
622
623 int centerY = (height / 2) - (int)(cellHeight * 0.35f);
624 mTouchYBorders[0] = centerY - (int)(2.4f * cellHeight);
625 mTouchYBorders[1] = centerY - (int)(1.15f * cellHeight);
626 mTouchYBorders[2] = centerY;
627 mTouchYBorders[3] = centerY + (int)(1.15f * cellHeight);;
628 mTouchYBorders[4] = centerY + (int)(2.4f * cellHeight);
629
630 mAllocTouchYBorders.data(mTouchYBorders);
631
632 int centerX = (width / 2);
633 mTouchXBorders[0] = centerX - (2 * cellWidth);
634 mTouchXBorders[1] = centerX - (int)(0.83f * cellWidth);;
635 mTouchXBorders[2] = centerX;
636 mTouchXBorders[3] = centerX + (int)(0.83f * cellWidth);;
637 mTouchXBorders[4] = centerX + (2 * cellWidth);
638
639 mAllocTouchXBorders.data(mTouchXBorders);
640 }
641
642 int chooseTappedIcon(int x, int y, int scrollX, int currentPage) {
643 int col = -1;
644 int row = -1;
645
646 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
647 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
648 col = i;
649 break;
650 }
651 }
652 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
653 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
654 row = i;
655 break;
656 }
657 }
658
659 if (row < 0 || col < 0) {
660 return -1;
661 }
662
663 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
664 + (row * Defines.ROWS_PER_PAGE) + col;
665 }
666
667 /**
668 * You need to call save() on mState on your own after calling this.
669 */
670 void selectIcon(int x, int y, int scrollX, int currentPage) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700671 int index = chooseTappedIcon(x, y, scrollX, currentPage);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400672 selectIcon(index);
673 }
674
675 void selectIcon(int index) {
676 Log.d(TAG, "selectIcon index=" + index);
677 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700678 if (index < 0 || index >= iconCount) {
679 mState.selectedIconIndex = -1;
680 return;
681 } else {
682 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400683
684 Bitmap selectionBitmap = mSelectionBitmap;
685
686 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
687 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
688 mAllAppsList.get(index).iconBitmap);
689
690 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
691 Element.RGBA_8888, false);
692 mSelectedIcon.uploadToTexture(0);
693 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700694 }
695 }
696
697 /**
698 * You need to call save() on mState on your own after calling this.
699 */
700 void clearSelectedIcon() {
Joe Onorato1291a8c2009-09-15 15:07:25 -0400701 //mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700702 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400703 }
Joe Onorato93839052009-08-06 20:34:32 -0700704}
705
706