blob: 732bce33c948166bea5fb3f7dad64f44acbdb150 [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 Onoratobcbeab82009-10-01 21:45:43 -070072 private static final int TRACKING_FLING = 0;
73 private static final int TRACKING_HOME = 1;
74
Joe Onorato6665c0f2009-09-02 15:27:24 -070075 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070076 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040077
78 /** When this is 0, modifications are allowed, when it's not, they're not.
79 * TODO: What about scrolling? */
80 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070081
Joe Onoratof7b0e012009-10-01 14:09:15 -070082 private int mSlopX;
83 private int mMaxFlingVelocity;
84
Joe Onoratobcbeab82009-10-01 21:45:43 -070085 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070086 private RenderScript mRS;
87 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040088 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070089
Joe Onoratoc567acb2009-08-31 14:34:43 -070090 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070091 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070092 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -070093 private int mTouchTracking;
Joe Onorato1feb3a82009-08-08 22:32:00 -070094 private int mLastMotionX;
Joe Onorato5162ea92009-09-03 09:39:42 -070095 private int mMotionDownRawX;
96 private int mMotionDownRawY;
Joe Onoratobcbeab82009-10-01 21:45:43 -070097 private int mHomeButtonTop;
Jason Sams86c87ed2009-09-18 13:55:55 -070098 private long mTouchTime;
Joe Onorato1feb3a82009-08-08 22:32:00 -070099
Joe Onorato6665c0f2009-09-02 15:27:24 -0700100 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700101 public static final int ALLOC_PARAMS = 0;
102 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700103 public static final int ALLOC_ICON_IDS = 3;
104 public static final int ALLOC_LABEL_IDS = 4;
105 public static final int ALLOC_X_BORDERS = 5;
106 public static final int ALLOC_Y_BORDERS = 6;
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 float RADIUS = 4.0f;
112
Joe Onorato6665c0f2009-09-02 15:27:24 -0700113 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;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700121
122 public int SCREEN_WIDTH_PX;
123 public int SCREEN_HEIGHT_PX;
124
125 public float FAR_ICON_SIZE;
126
127 public void recompute(int w, int h) {
128 SCREEN_WIDTH_PX = 480;
129 SCREEN_HEIGHT_PX = 800;
130 FAR_ICON_SIZE = farSize(2 * ICON_WIDTH_PX / (float)w);
131 }
132
133 private static float farSize(float sizeAt0) {
134 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
135 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700136 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700137
138 public AllAppsView(Context context, AttributeSet attrs) {
139 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700140 setFocusable(true);
141 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700142 final ViewConfiguration config = ViewConfiguration.get(context);
143 mSlopX = config.getScaledTouchSlop();
144 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
145
Joe Onorato6665c0f2009-09-02 15:27:24 -0700146 setOnClickListener(this);
147 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700148 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700149 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700150 }
151
Joe Onorato7c312c12009-08-13 21:36:53 -0700152 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
153 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700154 }
155
Joe Onorato6665c0f2009-09-02 15:27:24 -0700156 public void setLauncher(Launcher launcher) {
157 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700158 }
159
Joe Onorato1feb3a82009-08-08 22:32:00 -0700160 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700161 public void surfaceDestroyed(SurfaceHolder holder) {
162 super.surfaceDestroyed(holder);
163
164 destroyRenderScript();
165 mRS = null;
166 mRollo = null;
167 }
168
169 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700170 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
171 super.surfaceChanged(holder, format, w, h);
172
Joe Onorato6665c0f2009-09-02 15:27:24 -0700173 long startTime = SystemClock.uptimeMillis();
174
Jason Sams05de32a2009-09-27 14:01:40 -0700175 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700176 mRollo = new RolloRS();
177 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 if (mAllAppsList != null) {
179 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700180 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700182
183 Resources res = getContext().getResources();
184 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700185 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700186
187 long endTime = SystemClock.uptimeMillis();
188 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700189 }
190
191 @Override
192 public boolean onKeyDown(int keyCode, KeyEvent event)
193 {
Joe Onorato93839052009-08-06 20:34:32 -0700194 // this method doesn't work when 'extends View' include 'extends ScrollView'.
195 return super.onKeyDown(keyCode, event);
196 }
197
Joe Onorato93839052009-08-06 20:34:32 -0700198 @Override
199 public boolean onTouchEvent(MotionEvent ev)
200 {
Joe Onorato7bb17492009-09-24 17:51:01 -0700201 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700202 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700203 }
204
Joe Onoratofb0ca672009-09-14 17:55:46 -0400205 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700206 return true;
207 }
208
209 super.onTouchEvent(ev);
210
Joe Onoratofb0ca672009-09-14 17:55:46 -0400211 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700212 int y = (int)ev.getY();
213
Joe Onoratofb0ca672009-09-14 17:55:46 -0400214 int deltaX;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700215 int action = ev.getAction();
216 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400217 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700218 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
219 mTouchTracking = TRACKING_HOME;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700220 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700221 mTouchTracking = TRACKING_FLING;
222
223 mMotionDownRawX = (int)ev.getRawX();
224 mMotionDownRawY = (int)ev.getRawY();
225 mLastMotionX = x;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700226
227 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
228 mRollo.mState.newTouchDown = 1;
229
230 if (!mRollo.checkClickOK()) {
231 mRollo.clearSelectedIcon();
232 } else {
Jason Sams12c14a82009-10-06 14:33:15 -0700233 mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700234 }
235 mRollo.mState.save();
236 mRollo.mInvokeMove.execute();
237 mVelocity = VelocityTracker.obtain();
238 mVelocity.addMovement(ev);
239 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700240 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400241 break;
242 case MotionEvent.ACTION_MOVE:
243 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700244 if (mTouchTracking == TRACKING_HOME) {
245 // TODO: highlight?
Joe Onoratofb0ca672009-09-14 17:55:46 -0400246 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700247 int slopX = Math.abs(x - mLastMotionX);
248 if (!mStartedScrolling && slopX < mSlopX) {
249 // don't update mLastMotionX so slopX is right and when we do start scrolling
250 // below, we get the right delta.
251 } else {
252 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
253 mRollo.mState.newTouchDown = 1;
254 mRollo.mInvokeMove.execute();
Jason Sams86c87ed2009-09-18 13:55:55 -0700255
Joe Onoratobcbeab82009-10-01 21:45:43 -0700256 mStartedScrolling = true;
257 mRollo.clearSelectedIcon();
258 deltaX = x - mLastMotionX;
259 mVelocity.addMovement(ev);
260 mRollo.mState.save();
261 mLastMotionX = x;
262 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400263 }
264 break;
265 case MotionEvent.ACTION_UP:
266 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700267 if (mTouchTracking == TRACKING_HOME) {
268 if (action == MotionEvent.ACTION_UP) {
269 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
270 mLauncher.closeAllApps(true);
271 }
272 }
Jason Sams476339d2009-09-29 18:14:38 -0700273 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700274 mRollo.mState.newTouchDown = 0;
275 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
Jason Sams476339d2009-09-29 18:14:38 -0700276
Jason Sams12c14a82009-10-06 14:33:15 -0700277 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
278 mRollo.mState.flingVelocityX
279 = mVelocity.getXVelocity() / mDefines.SCREEN_WIDTH_PX;
280 mRollo.clearSelectedIcon();
281 mRollo.mState.save();
282 mRollo.mInvokeFling.execute();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700283
284 mLastMotionX = -10000;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700285 if (mVelocity != null) {
286 mVelocity.recycle();
287 mVelocity = null;
288 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700289 break;
290 }
Joe Onorato93839052009-08-06 20:34:32 -0700291 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700292
293 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700294 }
295
Joe Onorato6665c0f2009-09-02 15:27:24 -0700296 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700297 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400298 return;
299 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700300 int index = mRollo.mState.selectedIconIndex;
Jason Sams476339d2009-09-29 18:14:38 -0700301 if (mRollo.checkClickOK() && index >= 0 && index < mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700302 ApplicationInfo app = mAllAppsList.get(index);
303 mLauncher.startActivitySafely(app.intent);
304 }
305 }
306
307 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700308 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400309 return true;
310 }
Joe Onorato5162ea92009-09-03 09:39:42 -0700311 int index = mRollo.mState.selectedIconIndex;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700312
Jason Sams476339d2009-09-29 18:14:38 -0700313 if (mRollo.checkClickOK() && index >= 0 && index < mAllAppsList.size()) {
Joe Onorato5162ea92009-09-03 09:39:42 -0700314 ApplicationInfo app = mAllAppsList.get(index);
315
316 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700317 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
318 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700319
Joe Onoratobcbeab82009-10-01 21:45:43 -0700320 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
321 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700322 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700323 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700324 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700325
Joe Onorato7bb17492009-09-24 17:51:01 -0700326 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700327 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700328 return true;
329 }
330
Joe Onorato5162ea92009-09-03 09:39:42 -0700331 public void setDragController(DragController dragger) {
332 mDragController = dragger;
333 }
334
335 public void onDropCompleted(View target, boolean success) {
336 }
337
Joe Onorato4db52312009-10-06 11:17:43 -0700338 /**
339 * Zoom to the specifed amount.
340 *
341 * @param amount [0..1] 0 is hidden, 1 is open
342 * @param animate Whether to animate.
343 */
Jason Sams12c14a82009-10-06 14:33:15 -0700344 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700345 if (mRollo == null) {
346 return;
347 }
348
Joe Onoratofb0ca672009-09-14 17:55:46 -0400349 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400350 mRollo.clearSelectedIcon();
Joe Onorato85a02a82009-09-08 12:34:22 -0700351 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700352 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700353 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700354 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700355 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700356 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400357 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400358 }
359
360 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700361 if (mRollo == null) {
362 return false;
363 }
Jason Sams12c14a82009-10-06 14:33:15 -0700364 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400365 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700366
Joe Onorato93839052009-08-06 20:34:32 -0700367 @Override
368 public boolean onTrackballEvent(MotionEvent ev)
369 {
370 float x = ev.getX();
371 float y = ev.getY();
372 //Float tx = new Float(x);
373 //Float ty = new Float(y);
374 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
375
376
377 return true;
378 }
379
Joe Onorato9c1289c2009-08-17 11:03:03 -0400380 public void setApps(ArrayList<ApplicationInfo> list) {
381 mAllAppsList = list;
382 if (mRollo != null) {
383 mRollo.setApps(list);
384 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700385 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400386 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700387 }
388
Joe Onoratoa8138d52009-10-06 19:25:30 -0700389 public void addApps(ArrayList<ApplicationInfo> list) {
390 final int N = list.size();
391 if (mRollo != null) {
392 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
393 }
394
395 for (int i=0; i<N; i++) {
396 final ApplicationInfo item = list.get(i);
397 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
398 if (index < 0) {
399 index = -(index+1);
400 }
401 mAllAppsList.add(index, item);
402 if (mRollo != null) {
403 mRollo.addApp(index, item);
404 mRollo.mState.iconCount++;
405 }
406 }
407
408 if (mRollo != null) {
409 mRollo.saveAppsList();
410 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700411 }
412
Joe Onoratoa8138d52009-10-06 19:25:30 -0700413 public void removeApps(ArrayList<ApplicationInfo> list) {
414 final int N = list.size();
415 for (int i=0; i<N; i++) {
416 final ApplicationInfo item = list.get(i);
417 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
418 if (index >= 0) {
419 mAllAppsList.remove(index);
420 if (mRollo != null) {
421 mRollo.removeApp(index);
422 mRollo.mState.iconCount--;
423 }
424 } else {
425 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
426 // Try to recover. This should keep us from crashing for now.
427 }
428 }
429
430 if (mRollo != null) {
431 mRollo.saveAppsList();
432 }
433 }
434
435 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
436 // Just remove and add, because they may need to be re-sorted.
437 removeApps(list);
438 addApps(list);
439 }
440
441 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
442 public int compare(ApplicationInfo a, ApplicationInfo b) {
443 int result = a.title.toString().compareTo(b.toString());
444 if (result != 0) {
445 return result;
446 }
447 return a.intent.getComponent().compareTo(b.intent.getComponent());
448 }
449 };
450
451 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
452 public int compare(ApplicationInfo a, ApplicationInfo b) {
453 return a.intent.getComponent().compareTo(b.intent.getComponent());
454 }
455 };
456
Joe Onoratoc567acb2009-08-31 14:34:43 -0700457 private static int countPages(int iconCount) {
458 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
459 int pages = iconCount / iconsPerPage;
460 if (pages*iconsPerPage != iconCount) {
461 pages++;
462 }
463 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400464 }
465
Joe Onorato93839052009-08-06 20:34:32 -0700466 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700467
Joe Onorato1feb3a82009-08-08 22:32:00 -0700468 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700469
Joe Onorato93839052009-08-06 20:34:32 -0700470 private int mWidth;
471 private int mHeight;
472
473 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700474 private Script mScript;
Jason Sams86c87ed2009-09-18 13:55:55 -0700475
476 private Script.Invokable mInvokeMove;
477 private Script.Invokable mInvokeFling;
Joe Onorato360d0352009-09-28 14:37:53 -0400478 private Script.Invokable mInvokeTouchUp;
Jason Sams86c87ed2009-09-18 13:55:55 -0700479
Jason Samscd689e12009-09-29 15:28:22 -0700480 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700481 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700482 private ProgramFragment mPFColor;
483 private ProgramFragment mPFTexLinear;
484 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700485 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700486 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700487 private SimpleMesh mMesh;
Joe Onorato93839052009-08-06 20:34:32 -0700488
Joe Onoratobcbeab82009-10-01 21:45:43 -0700489 private Allocation mHomeButton;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700490
Joe Onoratobf15cb42009-08-07 14:33:40 -0700491 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700492 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700493 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700494
Joe Onoratobf15cb42009-08-07 14:33:40 -0700495 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700496 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700497 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700498 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700499
Joe Onorato6665c0f2009-09-02 15:27:24 -0700500 private int[] mTouchYBorders;
501 private Allocation mAllocTouchYBorders;
502 private int[] mTouchXBorders;
503 private Allocation mAllocTouchXBorders;
504
505 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400506 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700507
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700508 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700509 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700510
Jason Sams78aebd82009-09-15 13:06:59 -0700511 class BaseAlloc {
512 Allocation mAlloc;
513 Type mType;
514
515 void save() {
516 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700517 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700518 }
519
Jason Sams12c14a82009-10-06 14:33:15 -0700520 class AAMessage extends RenderScript.RSMessage {
521 public void run() {
522 mPosX = ((float)mData[0]) / (1 << 16);
523 mVelocity = ((float)mData[1]) / (1 << 16);
524 mZoom = ((float)mData[2]) / (1 << 16);
525 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
526 }
527 float mZoom;
528 float mPosX;
529 float mVelocity;
530 }
531 AAMessage mMessageProc;
532
Jason Sams476339d2009-09-29 18:14:38 -0700533 private boolean checkClickOK() {
534 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams12c14a82009-10-06 14:33:15 -0700535 return (Math.abs(mMessageProc.mVelocity) < 0.1f) &&
536 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.1f);
Jason Sams476339d2009-09-29 18:14:38 -0700537 }
538
Jason Sams78aebd82009-09-15 13:06:59 -0700539 class Params extends BaseAlloc {
540 Params() {
541 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
542 mAlloc = Allocation.createTyped(mRS, mType);
543 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700544 }
Jason Sams78aebd82009-09-15 13:06:59 -0700545 public int bubbleWidth;
546 public int bubbleHeight;
547 public int bubbleBitmapWidth;
548 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700549
550 public int homeButtonId;
551 public int homeButtonWidth;
552 public int homeButtonHeight;
553 public int homeButtonTextureWidth;
554 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700555 }
556
557 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700558 public float newPositionX;
559 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700560 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700561 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700562 public int selectedIconIndex = -1;
563 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700564 public float zoomTarget;
Jason Sams78aebd82009-09-15 13:06:59 -0700565
566 State() {
567 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
568 mAlloc = Allocation.createTyped(mRS, mType);
569 save();
570 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700571 }
572
573 public RolloRS() {
574 }
575
576 public void init(Resources res, int width, int height) {
577 mRes = res;
578 mWidth = width;
579 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700580 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700581 initProgramVertex();
582 initProgramFragment();
583 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700584 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700585 initGl();
586 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700587 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700588 initRs();
589 }
590
Jason Sams0aa71662009-10-02 18:43:18 -0700591 public void initMesh() {
592 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
593 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
594
595 for (int ct=0; ct < 450; ct++) {
596 float x = 0;
597 float z = 0;
598 float l = 1.f;
599
600 if (ct < 190) {
601 z = 0.1f + 0.05f * (190 - ct);
602 x = -1;
603 l = 0.125f + (0.125f / 190.f) * ct;
604 } else if (ct >= 190 && ct < 200) {
605 float a = (3.14f * 0.5f) * (0.1f * (ct - 200));
606 float s = (float)Math.sin(a);
607 float c = (float)Math.cos(a);
608 x = -0.9f + s * 0.1f;
609 z = 0.1f - c * 0.1f;
610 l = 0.25f + 0.075f * (ct - 190);
611 } else if (ct >= 200 && ct < 250) {
612 z = 0.f;
613 x = -0.9f + (1.8f * (ct - 200) / 50.f);
614 } else if (ct >= 250 && ct < 260) {
615 float a = (3.14f * 0.5f) * (0.1f * (ct - 250));
616 float s = (float)Math.sin(a);
617 float c = (float)Math.cos(a);
618 x = 0.9f + s * 0.1f;
619 z = 0.1f - c * 0.1f;
620 l = 0.25f + 0.075f * (260 - ct);
621 } else if (ct >= 260) {
622 z = 0.1f + 0.05f * (ct - 260);
623 x = 1;
624 l = 0.125f + (0.125f / 190.f) * (450 - ct);
625 }
626 //Log.e("rs", "ct " + Integer.toString(ct) + " x = " + Float.toString(x) + ", z = " + Float.toString(z));
627 //Log.e("rs", "ct " + Integer.toString(ct) + " l = " + Float.toString(l));
628 float s = ct * 0.1f;
629 tm.setColor(l, l, l, 0.99f);
630 tm.setTexture(s, 1);
631 tm.addVertex(x, -0.5f, z);
632 tm.setTexture(s, 0);
633 tm.addVertex(x, 0.5f, z);
634 }
635 for (int ct=0; ct < (450*2 - 2); ct+= 2) {
636 tm.addTriangle(ct, ct+1, ct+2);
637 tm.addTriangle(ct+1, ct+3, ct+2);
638 }
639 mMesh = tm.create();
640 mMesh.setName("SMMesh");
641
642 }
643
644
Jason Samscd689e12009-09-29 15:28:22 -0700645 private void initProgramVertex() {
646 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
647 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700648
649 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700650 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700651 mPV = pvb.create();
652 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700653 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700654
Jason Samscd689e12009-09-29 15:28:22 -0700655 pva = new ProgramVertex.MatrixAllocation(mRS);
656 pva.setupOrthoWindow(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700657 pvb.setTextureMatrixEnable(true);
658 mPVOrtho = pvb.create();
659 mPVOrtho.setName("PVOrtho");
Jason Samscd689e12009-09-29 15:28:22 -0700660 mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700661
662 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700663 }
Joe Onorato93839052009-08-06 20:34:32 -0700664
Jason Samscd689e12009-09-29 15:28:22 -0700665 private void initProgramFragment() {
666 Sampler.Builder sb = new Sampler.Builder(mRS);
667 sb.setMin(Sampler.Value.LINEAR);
668 sb.setMag(Sampler.Value.LINEAR);
669 sb.setWrapS(Sampler.Value.CLAMP);
670 sb.setWrapT(Sampler.Value.CLAMP);
671 Sampler linear = sb.create();
672
673 sb.setMin(Sampler.Value.NEAREST);
674 sb.setMag(Sampler.Value.NEAREST);
675 Sampler nearest = sb.create();
676
677 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
678 mPFColor = bf.create();
679 mPFColor.setName("PFColor");
680
681 bf.setTexEnable(true, 0);
682 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
683 mPFTexLinear = bf.create();
684 mPFTexLinear.setName("PFTexLinear");
685 mPFTexLinear.bindSampler(linear, 0);
686
687 mPFTexNearest = bf.create();
688 mPFTexNearest.setName("PFTexNearest");
689 mPFTexNearest.bindSampler(nearest, 0);
690 }
691
692 private void initProgramStore() {
693 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
694 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700695 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700696 bs.setDitherEnable(true);
697 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
698 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
699 mPSIcons = bs.create();
700 mPSIcons.setName("PSIcons");
701
702 //bs.setDitherEnable(false);
703 //mPSText = bs.create();
704 //mPSText.setName("PSText");
705 }
706
707 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700708 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700709 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700710 mTouchXBorders.length);
711 mAllocTouchXBorders.data(mTouchXBorders);
712
713 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700714 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700715 mTouchYBorders.length);
716 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700717 }
Jason Sams78aebd82009-09-15 13:06:59 -0700718
Joe Onorato1feb3a82009-08-08 22:32:00 -0700719 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700720 mParams = new Params();
721 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700722
Joe Onoratobf15cb42009-08-07 14:33:40 -0700723 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700724
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700725 mParams.bubbleWidth = bubble.getBubbleWidth();
726 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
727 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
728 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700729
Joe Onoratobcbeab82009-10-01 21:45:43 -0700730 mHomeButton = Allocation.createFromBitmapResource(mRS, mRes,
731 R.drawable.home_button, Element.RGBA_8888(mRS), false);
732 mHomeButton.uploadToTexture(0);
733 mParams.homeButtonId = mHomeButton.getID();
734 mParams.homeButtonWidth = 76;
735 mParams.homeButtonHeight = 68;
736 mParams.homeButtonTextureWidth = 128;
737 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700738
Joe Onorato1feb3a82009-08-08 22:32:00 -0700739 mParams.save();
740 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400741
Joe Onorato1291a8c2009-09-15 15:07:25 -0400742 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
743 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
744 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700745
Joe Onorato9c1289c2009-08-17 11:03:03 -0400746 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700747 }
748
Joe Onorato1feb3a82009-08-08 22:32:00 -0700749 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700750 ScriptC.Builder sb = new ScriptC.Builder(mRS);
751 sb.setScript(mRes, R.raw.rollo);
Jason Sams0aa71662009-10-02 18:43:18 -0700752 //sb.setScript(mRes, R.raw.rollo2);
Joe Onorato93839052009-08-06 20:34:32 -0700753 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700754 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700755 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
756 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams86c87ed2009-09-18 13:55:55 -0700757 mInvokeMove = sb.addInvokable("move");
758 mInvokeFling = sb.addInvokable("fling");
Joe Onorato360d0352009-09-28 14:37:53 -0400759 mInvokeTouchUp = sb.addInvokable("touchUp");
Joe Onorato93839052009-08-06 20:34:32 -0700760 mScript = sb.create();
761 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
762
Jason Sams78aebd82009-09-15 13:06:59 -0700763 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
764 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700765 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
766 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700767 mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
768 mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
Joe Onorato93839052009-08-06 20:34:32 -0700769
Jason Sams12c14a82009-10-06 14:33:15 -0700770 mMessageProc = new AAMessage();
771 mRS.mMessageCallback = mMessageProc;
Joe Onorato93839052009-08-06 20:34:32 -0700772 mRS.contextBindRootScript(mScript);
773 }
Joe Onorato93839052009-08-06 20:34:32 -0700774
Joe Onorato9c1289c2009-08-17 11:03:03 -0400775 private void setApps(ArrayList<ApplicationInfo> list) {
776 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700777 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700778 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700779 allocCount = 1;
780 }
781
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700783 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700784 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400785
786 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700787 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700788 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400789
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700790 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400791
792 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
793
794 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700795 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400796 }
797
Joe Onorato9c1289c2009-08-17 11:03:03 -0400798 mState.iconCount = count;
799
Joe Onoratoa8138d52009-10-06 19:25:30 -0700800 saveAppsList();
801 }
802
803 private void uploadAppIcon(int index, ApplicationInfo item) {
804 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
805 Element.RGBA_8888(mRS), false);
806 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
807 Element.RGBA_8888(mRS), false);
808
809 mIcons[index].uploadToTexture(0);
810 mLabels[index].uploadToTexture(0);
811
812 mIconIds[index] = mIcons[index].getID();
813 mLabelIds[index] = mLabels[index].getID();
814 }
815
816 /**
817 * Puts the empty spaces at the end. Updates mState.iconCount. You must
818 * fill in the values and call saveAppsList().
819 */
820 private void reallocAppsList(int count) {
821 Allocation[] icons = new Allocation[count];
822 int[] iconIds = new int[count];
823 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
824
825 Allocation[] labels = new Allocation[count];
826 int[] labelIds = new int[count];
827 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
828
829 final int oldCount = mIcons.length;
830
831 System.arraycopy(mIcons, 0, icons, 0, oldCount);
832 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
833 System.arraycopy(mLabels, 0, labels, 0, oldCount);
834 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
835
836 mIcons = icons;
837 mIconIds = iconIds;
838 mLabels = labels;
839 mLabelIds = labelIds;
840 }
841
842 /**
843 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
844 */
845 private void addApp(int index, ApplicationInfo item) {
846 final int count = mState.iconCount - index;
847 final int dest = index + 1;
848
849 System.arraycopy(mIcons, index, mIcons, dest, count);
850 System.arraycopy(mIconIds, index, mIconIds, dest, count);
851 System.arraycopy(mLabels, index, mLabels, dest, count);
852 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
853
854 uploadAppIcon(index, item);
855 }
856
857 /**
858 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
859 */
860 private void removeApp(int index) {
861 final int count = mState.iconCount - index - 1;
862 final int src = index + 1;
863
864 System.arraycopy(mIcons, src, mIcons, index, count);
865 System.arraycopy(mIconIds, src, mIconIds, index, count);
866 System.arraycopy(mLabels, src, mLabels, index, count);
867 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
868
869 final int last = mState.iconCount - 1;
870 mIcons[last] = null;
871 mIconIds[last] = 0;
872 mLabels[last] = null;
873 mLabelIds[last] = 0;
874 }
875
876 /**
877 * Send the apps list structures to RS.
878 */
879 private void saveAppsList() {
880 mRS.contextBindRootScript(null);
881
882 mAllocIconIds.data(mIconIds);
883 mAllocLabelIds.data(mLabelIds);
884
885 if (mScript != null) { // this happens when we init it
886 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
887 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400888 }
889
890 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -0700891
892 // Note: mScript may be null if we haven't initialized it yet.
893 // In that case, this is a no-op.
894 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400895 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700896
897 void initTouchState() {
898 int width = getWidth();
899 int height = getHeight();
900
901 int iconsSize;
902 if (width < height) {
903 iconsSize = width;
904 } else {
905 iconsSize = height;
906 }
907 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
908 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
909
Joe Onorato56848b02009-09-25 13:59:59 -0700910 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
911 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
912 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700913 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -0700914 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
915 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700916
917 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -0700918
Joe Onorato6665c0f2009-09-02 15:27:24 -0700919 int centerX = (width / 2);
920 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -0700921 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700922 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -0700923 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700924 mTouchXBorders[4] = centerX + (2 * cellWidth);
925
926 mAllocTouchXBorders.data(mTouchXBorders);
927 }
928
Jason Sams86c87ed2009-09-18 13:55:55 -0700929 int chooseTappedIcon(int x, int y, float page) {
930 int currentPage = (int)page;
931
Joe Onorato6665c0f2009-09-02 15:27:24 -0700932 int col = -1;
933 int row = -1;
934
935 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
936 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
937 col = i;
938 break;
939 }
940 }
941 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
942 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
943 row = i;
944 break;
945 }
946 }
947
948 if (row < 0 || col < 0) {
949 return -1;
950 }
951
Jason Sams78aebd82009-09-15 13:06:59 -0700952 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700953 + (row * Defines.ROWS_PER_PAGE) + col;
954 }
955
956 /**
957 * You need to call save() on mState on your own after calling this.
958 */
Jason Sams86c87ed2009-09-18 13:55:55 -0700959 void selectIcon(int x, int y, float pos) {
960 int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400961 selectIcon(index);
962 }
963
964 void selectIcon(int index) {
Joe Onorato1291a8c2009-09-15 15:07:25 -0400965 int iconCount = mAllAppsList.size();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700966 if (index < 0 || index >= iconCount) {
967 mState.selectedIconIndex = -1;
968 return;
969 } else {
970 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400971
972 Bitmap selectionBitmap = mSelectionBitmap;
973
974 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
975 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
976 mAllAppsList.get(index).iconBitmap);
977
978 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700979 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400980 mSelectedIcon.uploadToTexture(0);
981 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700982 }
983 }
984
985 /**
986 * You need to call save() on mState on your own after calling this.
987 */
988 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400989 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700990 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700991
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 }
Joe Onorato93839052009-08-06 20:34:32 -0700993}
994
995