blob: 305f4041cab483da548a9ae4c12fd93272ec532b [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;
Joe Onoratoa8138d52009-10-06 19:25:30 -070023import java.util.Collections;
24import java.util.Comparator;
Joe Onorato93839052009-08-06 20:34:32 -070025
26import android.renderscript.RSSurfaceView;
27import android.renderscript.RenderScript;
28
29import android.renderscript.RenderScript;
30import android.renderscript.ProgramVertex;
31import android.renderscript.Element;
32import android.renderscript.Allocation;
Jason Sams78aebd82009-09-15 13:06:59 -070033import android.renderscript.Type;
Joe Onorato93839052009-08-06 20:34:32 -070034import android.renderscript.Script;
35import android.renderscript.ScriptC;
36import android.renderscript.ProgramFragment;
37import android.renderscript.ProgramStore;
38import android.renderscript.Sampler;
Jason Sams0aa71662009-10-02 18:43:18 -070039import android.renderscript.SimpleMesh;
Joe Onorato93839052009-08-06 20:34:32 -070040
41import android.content.Context;
42import android.content.res.Resources;
Joe Onorato7c312c12009-08-13 21:36:53 -070043import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070044import android.graphics.Bitmap;
45import android.graphics.BitmapFactory;
46import android.graphics.Canvas;
47import android.graphics.Paint;
Joe Onorato93839052009-08-06 20:34:32 -070048import android.graphics.drawable.BitmapDrawable;
49import android.graphics.drawable.Drawable;
Joe Onorato93839052009-08-06 20:34:32 -070050import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070051import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070052import android.util.AttributeSet;
53import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070054import android.view.KeyEvent;
55import android.view.MotionEvent;
Joe Onorato93839052009-08-06 20:34:32 -070056import android.view.Surface;
57import android.view.SurfaceHolder;
58import android.view.SurfaceView;
Joe Onorato6665c0f2009-09-02 15:27:24 -070059import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070060import android.view.VelocityTracker;
61import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070062import android.graphics.PixelFormat;
63
64
Joe Onorato6665c0f2009-09-02 15:27:24 -070065public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070066 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040067 private static final String TAG = "Launcher.AllAppsView";
68
Joe Onoratofb0ca672009-09-14 17:55:46 -040069 /** Bit for mLocks for when there are icons being loaded. */
70 private static final int LOCK_ICONS_PENDING = 1;
71
Joe Onorato68ffd102009-10-15 17:59:43 -070072 private static final int TRACKING_NONE = 0;
73 private static final int TRACKING_FLING = 1;
74 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070075
Joe Onorato6665c0f2009-09-02 15:27:24 -070076 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070077 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040078
79 /** When this is 0, modifications are allowed, when it's not, they're not.
80 * TODO: What about scrolling? */
81 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070082
Joe Onorato82ca5502009-10-15 16:59:23 -070083 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070084 private int mMaxFlingVelocity;
85
Joe Onoratobcbeab82009-10-01 21:45:43 -070086 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070087 private RenderScript mRS;
88 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070090
Joe Onoratoc567acb2009-08-31 14:34:43 -070091 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070092 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070093 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -070094 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -070095 private int mMotionDownRawX;
96 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -070097 private int mDownIconIndex = -1;
98 private int mCurrentIconIndex = -1;
Joe Onoratobcbeab82009-10-01 21:45:43 -070099 private int mHomeButtonTop;
Jason Sams86c87ed2009-09-18 13:55:55 -0700100 private long mTouchTime;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700101
Joe Onorato6665c0f2009-09-02 15:27:24 -0700102 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700103 public static final int ALLOC_PARAMS = 0;
104 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700105 public static final int ALLOC_ICON_IDS = 3;
106 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700107
108 public static final int COLUMNS_PER_PAGE = 4;
109 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700110
Joe Onorato6665c0f2009-09-02 15:27:24 -0700111 public static final int ICON_WIDTH_PX = 64;
112 public static final int ICON_TEXTURE_WIDTH_PX = 128;
113
114 public static final int ICON_HEIGHT_PX = 64;
115 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700116
117 public int SCREEN_WIDTH_PX;
118 public int SCREEN_HEIGHT_PX;
119
Joe Onoratobcbeab82009-10-01 21:45:43 -0700120 public void recompute(int w, int h) {
121 SCREEN_WIDTH_PX = 480;
122 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700123 }
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 Onoratof7b0e012009-10-01 14:09:15 -0700130 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700131 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700132 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
133
Joe Onorato6665c0f2009-09-02 15:27:24 -0700134 setOnClickListener(this);
135 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700136 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700137 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700138 }
139
Joe Onorato7c312c12009-08-13 21:36:53 -0700140 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
141 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700142 }
143
Joe Onorato6665c0f2009-09-02 15:27:24 -0700144 public void setLauncher(Launcher launcher) {
145 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700146 }
147
Joe Onorato1feb3a82009-08-08 22:32:00 -0700148 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700149 public void surfaceDestroyed(SurfaceHolder holder) {
150 super.surfaceDestroyed(holder);
151
152 destroyRenderScript();
153 mRS = null;
154 mRollo = null;
155 }
156
157 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700158 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
159 super.surfaceChanged(holder, format, w, h);
160
Joe Onorato6665c0f2009-09-02 15:27:24 -0700161 long startTime = SystemClock.uptimeMillis();
162
Jason Sams05de32a2009-09-27 14:01:40 -0700163 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700164 mRollo = new RolloRS();
165 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400166 if (mAllAppsList != null) {
167 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700168 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700170
171 Resources res = getContext().getResources();
172 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700173 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700174
175 long endTime = SystemClock.uptimeMillis();
176 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700177 }
178
179 @Override
180 public boolean onKeyDown(int keyCode, KeyEvent event)
181 {
Joe Onorato93839052009-08-06 20:34:32 -0700182 // this method doesn't work when 'extends View' include 'extends ScrollView'.
183 return super.onKeyDown(keyCode, event);
184 }
185
Jason Samsd8152b92009-10-13 17:19:10 -0700186 private int mRSMode = 0;
187
Joe Onorato93839052009-08-06 20:34:32 -0700188 @Override
189 public boolean onTouchEvent(MotionEvent ev)
190 {
Joe Onorato7bb17492009-09-24 17:51:01 -0700191 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700192 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700193 }
194
Joe Onoratofb0ca672009-09-14 17:55:46 -0400195 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700196 return true;
197 }
198
199 super.onTouchEvent(ev);
200
Joe Onoratofb0ca672009-09-14 17:55:46 -0400201 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700202 int y = (int)ev.getY();
203
Joe Onoratobcbeab82009-10-01 21:45:43 -0700204 int action = ev.getAction();
205 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400206 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700207 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
208 mTouchTracking = TRACKING_HOME;
Joe Onoratod63458b2009-10-15 21:19:09 -0700209 mRollo.setHomeSelected(true);
210 mRollo.mState.save();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700211 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700212 mTouchTracking = TRACKING_FLING;
213
214 mMotionDownRawX = (int)ev.getRawX();
215 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700216
Jason Sams37e7c2b2009-10-19 12:55:43 -0700217 mRollo.mState.newPositionX = ev.getRawY() / getWidth();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700218 mRollo.mState.newTouchDown = 1;
219
220 if (!mRollo.checkClickOK()) {
221 mRollo.clearSelectedIcon();
222 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700223 mDownIconIndex = mCurrentIconIndex
224 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
225 if (mDownIconIndex < 0) {
226 // if nothing was selected, no long press.
227 cancelLongPress();
228 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700229 }
230 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700231 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700232 mVelocity = VelocityTracker.obtain();
233 mVelocity.addMovement(ev);
234 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700235 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400236 break;
237 case MotionEvent.ACTION_MOVE:
238 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700239 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratod63458b2009-10-15 21:19:09 -0700240 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]);
241 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700242 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700243 int rawX = (int)ev.getRawX();
244 int rawY = (int)ev.getRawY();
245 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700246 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700247
Joe Onorato82ca5502009-10-15 16:59:23 -0700248 if (!mStartedScrolling && slop < mSlop) {
249 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700250 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700251 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
252 if (mDownIconIndex != mCurrentIconIndex) {
253 // If a different icon is selected, don't allow it to be picked up.
254 // This handles off-axis dragging.
255 cancelLongPress();
256 mCurrentIconIndex = -1;
257 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700258 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700259 if (!mStartedScrolling) {
260 cancelLongPress();
261 mCurrentIconIndex = -1;
262 }
Jason Sams37e7c2b2009-10-19 12:55:43 -0700263 mRollo.mState.newPositionX = ev.getRawY() / getWidth();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700264 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700265 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700266
Joe Onoratobcbeab82009-10-01 21:45:43 -0700267 mStartedScrolling = true;
268 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700269 mVelocity.addMovement(ev);
270 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700271 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400272 }
273 break;
274 case MotionEvent.ACTION_UP:
275 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700276 if (mTouchTracking == TRACKING_HOME) {
277 if (action == MotionEvent.ACTION_UP) {
278 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
279 mLauncher.closeAllApps(true);
280 }
Joe Onoratod63458b2009-10-15 21:19:09 -0700281 mRollo.setHomeSelected(false);
282 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700283 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700284 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700285 mRollo.mState.newTouchDown = 0;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700286 mRollo.mState.newPositionX = ev.getRawY() / getWidth();
Jason Sams476339d2009-09-29 18:14:38 -0700287
Jason Sams12c14a82009-10-06 14:33:15 -0700288 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700289 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700290 mRollo.clearSelectedIcon();
291 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700292 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700293
Joe Onorato539ed9d2009-10-02 10:22:14 -0700294 if (mVelocity != null) {
295 mVelocity.recycle();
296 mVelocity = null;
297 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700298 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700299 mTouchTracking = TRACKING_NONE;
300 break;
Joe Onorato93839052009-08-06 20:34:32 -0700301 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700302
303 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700304 }
305
Joe Onorato6665c0f2009-09-02 15:27:24 -0700306 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700307 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400308 return;
309 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700310 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
311 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
312 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700313 mLauncher.startActivitySafely(app.intent);
314 }
315 }
316
317 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700318 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400319 return true;
320 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700321 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
322 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
323 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700324
325 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700326 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
327 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700328
Joe Onoratobcbeab82009-10-01 21:45:43 -0700329 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
330 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700331 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700332 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700333 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700334
Joe Onorato7bb17492009-09-24 17:51:01 -0700335 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700336 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700337 return true;
338 }
339
Joe Onorato5162ea92009-09-03 09:39:42 -0700340 public void setDragController(DragController dragger) {
341 mDragController = dragger;
342 }
343
344 public void onDropCompleted(View target, boolean success) {
345 }
346
Joe Onorato4db52312009-10-06 11:17:43 -0700347 /**
348 * Zoom to the specifed amount.
349 *
350 * @param amount [0..1] 0 is hidden, 1 is open
351 * @param animate Whether to animate.
352 */
Jason Sams12c14a82009-10-06 14:33:15 -0700353 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700354 if (mRollo == null) {
355 return;
356 }
357
Joe Onoratofb0ca672009-09-14 17:55:46 -0400358 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400359 mRollo.clearSelectedIcon();
Joe Onorato1d708e72009-10-15 21:29:21 -0700360 mRollo.setHomeSelected(false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700361 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700362 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700363 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700364 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700365 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700366 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400367 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400368 }
369
370 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700371 if (mRollo == null) {
372 return false;
373 }
Jason Sams12c14a82009-10-06 14:33:15 -0700374 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400375 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700376
Mike Cleronb6082fa02009-10-19 17:03:36 -0700377 /*
Joe Onorato93839052009-08-06 20:34:32 -0700378 @Override
379 public boolean onTrackballEvent(MotionEvent ev)
380 {
381 float x = ev.getX();
382 float y = ev.getY();
383 //Float tx = new Float(x);
384 //Float ty = new Float(y);
385 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
386
387
388 return true;
389 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700390 */
Joe Onorato93839052009-08-06 20:34:32 -0700391
Joe Onorato9c1289c2009-08-17 11:03:03 -0400392 public void setApps(ArrayList<ApplicationInfo> list) {
393 mAllAppsList = list;
394 if (mRollo != null) {
395 mRollo.setApps(list);
396 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700397 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400398 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700399 }
400
Joe Onoratoa8138d52009-10-06 19:25:30 -0700401 public void addApps(ArrayList<ApplicationInfo> list) {
402 final int N = list.size();
403 if (mRollo != null) {
404 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
405 }
406
407 for (int i=0; i<N; i++) {
408 final ApplicationInfo item = list.get(i);
409 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
410 if (index < 0) {
411 index = -(index+1);
412 }
413 mAllAppsList.add(index, item);
414 if (mRollo != null) {
415 mRollo.addApp(index, item);
416 mRollo.mState.iconCount++;
417 }
418 }
419
420 if (mRollo != null) {
421 mRollo.saveAppsList();
422 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700423 }
424
Joe Onoratoa8138d52009-10-06 19:25:30 -0700425 public void removeApps(ArrayList<ApplicationInfo> list) {
426 final int N = list.size();
427 for (int i=0; i<N; i++) {
428 final ApplicationInfo item = list.get(i);
429 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
430 if (index >= 0) {
431 mAllAppsList.remove(index);
432 if (mRollo != null) {
433 mRollo.removeApp(index);
434 mRollo.mState.iconCount--;
435 }
436 } else {
437 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
438 // Try to recover. This should keep us from crashing for now.
439 }
440 }
441
442 if (mRollo != null) {
443 mRollo.saveAppsList();
444 }
445 }
446
447 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
448 // Just remove and add, because they may need to be re-sorted.
449 removeApps(list);
450 addApps(list);
451 }
452
453 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
454 public int compare(ApplicationInfo a, ApplicationInfo b) {
455 int result = a.title.toString().compareTo(b.toString());
456 if (result != 0) {
457 return result;
458 }
459 return a.intent.getComponent().compareTo(b.intent.getComponent());
460 }
461 };
462
463 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
464 public int compare(ApplicationInfo a, ApplicationInfo b) {
465 return a.intent.getComponent().compareTo(b.intent.getComponent());
466 }
467 };
468
Joe Onoratoc567acb2009-08-31 14:34:43 -0700469 private static int countPages(int iconCount) {
470 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
471 int pages = iconCount / iconsPerPage;
472 if (pages*iconsPerPage != iconCount) {
473 pages++;
474 }
475 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400476 }
477
Joe Onorato93839052009-08-06 20:34:32 -0700478 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700479
Joe Onorato1feb3a82009-08-08 22:32:00 -0700480 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700481 private int mWidth;
482 private int mHeight;
483
484 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700485 private Script mScript;
486 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700487 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700488 private Script.Invokable mInvokeFling;
489 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700490
Jason Samsc1c521e2009-10-19 14:45:45 -0700491
Jason Samscd689e12009-09-29 15:28:22 -0700492 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700493 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700494 private ProgramFragment mPFColor;
495 private ProgramFragment mPFTexLinear;
Joe Onorato93839052009-08-06 20:34:32 -0700496 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700497 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700498 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700499 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700500
Joe Onoratod63458b2009-10-15 21:19:09 -0700501 private Allocation mHomeButtonNormal;
502 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700503
Joe Onoratobf15cb42009-08-07 14:33:40 -0700504 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700505 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700506 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700507
Joe Onoratobf15cb42009-08-07 14:33:40 -0700508 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700509 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700510 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700511 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700512
Joe Onorato6665c0f2009-09-02 15:27:24 -0700513 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700514 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700515
516 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400517 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700518
Jason Samsd8152b92009-10-13 17:19:10 -0700519
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700520 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700521 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700522
Jason Sams78aebd82009-09-15 13:06:59 -0700523 class BaseAlloc {
524 Allocation mAlloc;
525 Type mType;
526
527 void save() {
528 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700529 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700530 }
531
Jason Sams12c14a82009-10-06 14:33:15 -0700532 class AAMessage extends RenderScript.RSMessage {
533 public void run() {
534 mPosX = ((float)mData[0]) / (1 << 16);
535 mVelocity = ((float)mData[1]) / (1 << 16);
536 mZoom = ((float)mData[2]) / (1 << 16);
537 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
538 }
539 float mZoom;
540 float mPosX;
541 float mVelocity;
542 }
543 AAMessage mMessageProc;
544
Jason Sams476339d2009-09-29 18:14:38 -0700545 private boolean checkClickOK() {
546 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams12c14a82009-10-06 14:33:15 -0700547 return (Math.abs(mMessageProc.mVelocity) < 0.1f) &&
548 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.1f);
Jason Sams476339d2009-09-29 18:14:38 -0700549 }
550
Jason Sams78aebd82009-09-15 13:06:59 -0700551 class Params extends BaseAlloc {
552 Params() {
553 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
554 mAlloc = Allocation.createTyped(mRS, mType);
555 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700556 }
Jason Sams78aebd82009-09-15 13:06:59 -0700557 public int bubbleWidth;
558 public int bubbleHeight;
559 public int bubbleBitmapWidth;
560 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700561
Joe Onoratobcbeab82009-10-01 21:45:43 -0700562 public int homeButtonWidth;
563 public int homeButtonHeight;
564 public int homeButtonTextureWidth;
565 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700566 }
567
568 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700569 public float newPositionX;
570 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700571 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700572 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700573 public int selectedIconIndex = -1;
574 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700575 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700576 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700577 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700578
579 State() {
580 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
581 mAlloc = Allocation.createTyped(mRS, mType);
582 save();
583 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700584 }
585
586 public RolloRS() {
587 }
588
589 public void init(Resources res, int width, int height) {
590 mRes = res;
591 mWidth = width;
592 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700593 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700594 initProgramVertex();
595 initProgramFragment();
596 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700597 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700598 initGl();
599 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700600 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700601 initRs();
602 }
603
Jason Sams0aa71662009-10-02 18:43:18 -0700604 public void initMesh() {
605 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
606 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
607
Jason Samsd8152b92009-10-13 17:19:10 -0700608 float y = 0;
609 float z = 0;
610 for (int ct=0; ct < 200; ct++) {
611 float angle = 0;
612 float maxAngle = 3.14f * 0.16f;
613 float l = 1.f;
614
615 l = 1 - ((ct-5) * 0.10f);
616 if (ct > 7) {
617 angle = maxAngle * (ct - 7) * 0.2f;
618 angle = Math.min(angle, maxAngle);
619 }
620 l = Math.max(0.3f, l);
621 l = Math.min(1.0f, l);
622
623 y += 0.1f * Math.cos(angle);
624 z += 0.1f * Math.sin(angle);
625
626 float t = 0.1f * ct;
627 float ds = 0.08f;
628 tm.setColor(l, l, l, 0.99f);
629 tm.setTexture(ds, t);
630 tm.addVertex(-0.5f, y, z);
631 tm.setTexture(1 - ds, t);
632 tm.addVertex(0.5f, y, z);
633 }
634 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
635 tm.addTriangle(ct, ct+1, ct+2);
636 tm.addTriangle(ct+1, ct+3, ct+2);
637 }
638 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700639 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700640 }
641
Jason Samscd689e12009-09-29 15:28:22 -0700642 private void initProgramVertex() {
643 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
644 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700645
646 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700647 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700648 mPV = pvb.create();
649 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700650 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700651
Jason Sams37e7c2b2009-10-19 12:55:43 -0700652 //pva = new ProgramVertex.MatrixAllocation(mRS);
653 //pva.setupOrthoWindow(mWidth, mHeight);
654 //pvb.setTextureMatrixEnable(true);
655 //mPVOrtho = pvb.create();
656 //mPVOrtho.setName("PVOrtho");
657 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700658
659 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700660 }
Joe Onorato93839052009-08-06 20:34:32 -0700661
Jason Samscd689e12009-09-29 15:28:22 -0700662 private void initProgramFragment() {
663 Sampler.Builder sb = new Sampler.Builder(mRS);
664 sb.setMin(Sampler.Value.LINEAR);
665 sb.setMag(Sampler.Value.LINEAR);
666 sb.setWrapS(Sampler.Value.CLAMP);
667 sb.setWrapT(Sampler.Value.CLAMP);
668 Sampler linear = sb.create();
669
670 sb.setMin(Sampler.Value.NEAREST);
671 sb.setMag(Sampler.Value.NEAREST);
672 Sampler nearest = sb.create();
673
674 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700675 //mPFColor = bf.create();
676 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700677
678 bf.setTexEnable(true, 0);
679 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
680 mPFTexLinear = bf.create();
681 mPFTexLinear.setName("PFTexLinear");
682 mPFTexLinear.bindSampler(linear, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700683 }
684
685 private void initProgramStore() {
686 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
687 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700688 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700689 bs.setDitherEnable(true);
690 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
691 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
692 mPSIcons = bs.create();
693 mPSIcons.setName("PSIcons");
694
695 //bs.setDitherEnable(false);
696 //mPSText = bs.create();
697 //mPSText.setName("PSText");
698 }
699
700 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700701 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700702 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700703 }
Jason Sams78aebd82009-09-15 13:06:59 -0700704
Joe Onorato1feb3a82009-08-08 22:32:00 -0700705 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700706 mParams = new Params();
707 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700708
Joe Onoratobf15cb42009-08-07 14:33:40 -0700709 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700710
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700711 mParams.bubbleWidth = bubble.getBubbleWidth();
712 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
713 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
714 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700715
Joe Onoratod63458b2009-10-15 21:19:09 -0700716 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
717 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
718 mHomeButtonNormal.uploadToTexture(0);
719 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
720 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
721 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700722 mParams.homeButtonWidth = 76;
723 mParams.homeButtonHeight = 68;
724 mParams.homeButtonTextureWidth = 128;
725 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700726
Joe Onoratod63458b2009-10-15 21:19:09 -0700727 mState.homeButtonId = mHomeButtonNormal.getID();
728
Joe Onorato1feb3a82009-08-08 22:32:00 -0700729 mParams.save();
730 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731
Joe Onorato1291a8c2009-09-15 15:07:25 -0400732 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
733 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
734 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700735
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700737 }
738
Jason Sams37e7c2b2009-10-19 12:55:43 -0700739 private void initScript(int id) {
740 }
741
742 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700743 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700744 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700745 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700746 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700747 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
748 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700749 mInvokeMove = sb.addInvokable("move");
750 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700751 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700752 mInvokeResetWAR = sb.addInvokable("resetHWWar");
753 mScript = sb.create();
754 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
755 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
756 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
757 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
758 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700759
Jason Sams12c14a82009-10-06 14:33:15 -0700760 mMessageProc = new AAMessage();
761 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700762 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700763 }
Joe Onorato93839052009-08-06 20:34:32 -0700764
Joe Onorato9c1289c2009-08-17 11:03:03 -0400765 private void setApps(ArrayList<ApplicationInfo> list) {
766 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700767 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700768 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700769 allocCount = 1;
770 }
771
Joe Onorato9c1289c2009-08-17 11:03:03 -0400772 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700773 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700774 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400775
776 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700777 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700778 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700780 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400781
782 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
783
784 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700785 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400786 }
787
Joe Onorato9c1289c2009-08-17 11:03:03 -0400788 mState.iconCount = count;
789
Joe Onoratoa8138d52009-10-06 19:25:30 -0700790 saveAppsList();
791 }
792
793 private void uploadAppIcon(int index, ApplicationInfo item) {
794 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
795 Element.RGBA_8888(mRS), false);
796 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
797 Element.RGBA_8888(mRS), false);
798
799 mIcons[index].uploadToTexture(0);
800 mLabels[index].uploadToTexture(0);
801
802 mIconIds[index] = mIcons[index].getID();
803 mLabelIds[index] = mLabels[index].getID();
804 }
805
806 /**
807 * Puts the empty spaces at the end. Updates mState.iconCount. You must
808 * fill in the values and call saveAppsList().
809 */
810 private void reallocAppsList(int count) {
811 Allocation[] icons = new Allocation[count];
812 int[] iconIds = new int[count];
813 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
814
815 Allocation[] labels = new Allocation[count];
816 int[] labelIds = new int[count];
817 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
818
819 final int oldCount = mIcons.length;
820
821 System.arraycopy(mIcons, 0, icons, 0, oldCount);
822 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
823 System.arraycopy(mLabels, 0, labels, 0, oldCount);
824 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
825
826 mIcons = icons;
827 mIconIds = iconIds;
828 mLabels = labels;
829 mLabelIds = labelIds;
830 }
831
832 /**
833 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
834 */
835 private void addApp(int index, ApplicationInfo item) {
836 final int count = mState.iconCount - index;
837 final int dest = index + 1;
838
839 System.arraycopy(mIcons, index, mIcons, dest, count);
840 System.arraycopy(mIconIds, index, mIconIds, dest, count);
841 System.arraycopy(mLabels, index, mLabels, dest, count);
842 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
843
844 uploadAppIcon(index, item);
845 }
846
847 /**
848 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
849 */
850 private void removeApp(int index) {
851 final int count = mState.iconCount - index - 1;
852 final int src = index + 1;
853
854 System.arraycopy(mIcons, src, mIcons, index, count);
855 System.arraycopy(mIconIds, src, mIconIds, index, count);
856 System.arraycopy(mLabels, src, mLabels, index, count);
857 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
858
859 final int last = mState.iconCount - 1;
860 mIcons[last] = null;
861 mIconIds[last] = 0;
862 mLabels[last] = null;
863 mLabelIds[last] = 0;
864 }
865
866 /**
867 * Send the apps list structures to RS.
868 */
869 private void saveAppsList() {
870 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -0700871
Joe Onoratoa8138d52009-10-06 19:25:30 -0700872 mAllocIconIds.data(mIconIds);
873 mAllocLabelIds.data(mLabelIds);
874
Jason Sams37e7c2b2009-10-19 12:55:43 -0700875 if (mScript != null) { // this happens when we init it
876 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
877 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 }
879
880 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -0700881
882 // Note: mScript may be null if we haven't initialized it yet.
883 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -0700884 if (mInvokeResetWAR != null) {
885 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -0700886 }
Jason Sams37e7c2b2009-10-19 12:55:43 -0700887 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400888 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700889
890 void initTouchState() {
891 int width = getWidth();
892 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700893 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
894 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700895
Jason Sams37e7c2b2009-10-19 12:55:43 -0700896 int centerY = (height / 2);
897 mTouchYBorders[0] = centerY - (cellHeight * 2);
898 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700899 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700900 mTouchYBorders[3] = centerY + cellHeight;
901 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -0700902
Joe Onorato6665c0f2009-09-02 15:27:24 -0700903 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700904 mTouchXBorders[0] = 0;
905 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700906 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700907 mTouchXBorders[3] = centerX + (width / 4);
908 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700909 }
910
Jason Sams37e7c2b2009-10-19 12:55:43 -0700911 int chooseTappedIcon(int x, int y, float pos) {
912 // Adjust for scroll position if not zero.
913 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -0700914
Joe Onorato6665c0f2009-09-02 15:27:24 -0700915 int col = -1;
916 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700917 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
918 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
919 col = i;
920 break;
921 }
922 }
923 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
924 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
925 row = i;
926 break;
927 }
928 }
929
930 if (row < 0 || col < 0) {
931 return -1;
932 }
933
Jason Sams37e7c2b2009-10-19 12:55:43 -0700934 return (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700935 + (row * Defines.ROWS_PER_PAGE) + col;
936 }
937
Jason Samsd8152b92009-10-13 17:19:10 -0700938 void fling() {
Jason Sams37e7c2b2009-10-19 12:55:43 -0700939 mInvokeFling.execute();
Jason Samsd8152b92009-10-13 17:19:10 -0700940 }
941
942 void move() {
Jason Sams37e7c2b2009-10-19 12:55:43 -0700943 mInvokeMove.execute();
Jason Samsd8152b92009-10-13 17:19:10 -0700944 }
945
Jason Samsc1c521e2009-10-19 14:45:45 -0700946 void moveTo(float row) {
947 mState.targetPos = row;
948 mState.save();
949 mInvokeMoveTo.execute();
950 }
951
Joe Onorato6665c0f2009-09-02 15:27:24 -0700952 /**
953 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -0700954 *
955 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -0700956 */
Joe Onorato82ca5502009-10-15 16:59:23 -0700957 int selectIcon(int x, int y, float pos) {
958 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400959 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -0700960 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400961 }
962
963 void selectIcon(int index) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700964 if (index < 0) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700965 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700966 } else {
967 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400968
969 Bitmap selectionBitmap = mSelectionBitmap;
970
971 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
972 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
973 mAllAppsList.get(index).iconBitmap);
974
975 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700976 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400977 mSelectedIcon.uploadToTexture(0);
978 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700979 }
980 }
981
982 /**
983 * You need to call save() on mState on your own after calling this.
984 */
985 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400986 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700987 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700988
Joe Onoratod63458b2009-10-15 21:19:09 -0700989 void setHomeSelected(boolean pressed) {
990 if (pressed) {
991 mState.homeButtonId = mHomeButtonPressed.getID();
992 } else {
993 mState.homeButtonId = mHomeButtonNormal.getID();
994 }
995 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400996 }
Joe Onorato93839052009-08-06 20:34:32 -0700997}
998
999