blob: 7ee3026a9b0f6130dbe24642acc6ef9ebb9630a1 [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
Joe Onorato93839052009-08-06 20:34:32 -0700377 @Override
378 public boolean onTrackballEvent(MotionEvent ev)
379 {
380 float x = ev.getX();
381 float y = ev.getY();
382 //Float tx = new Float(x);
383 //Float ty = new Float(y);
384 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
385
386
387 return true;
388 }
389
Joe Onorato9c1289c2009-08-17 11:03:03 -0400390 public void setApps(ArrayList<ApplicationInfo> list) {
391 mAllAppsList = list;
392 if (mRollo != null) {
393 mRollo.setApps(list);
394 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700395 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400396 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700397 }
398
Joe Onoratoa8138d52009-10-06 19:25:30 -0700399 public void addApps(ArrayList<ApplicationInfo> list) {
400 final int N = list.size();
401 if (mRollo != null) {
402 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
403 }
404
405 for (int i=0; i<N; i++) {
406 final ApplicationInfo item = list.get(i);
407 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
408 if (index < 0) {
409 index = -(index+1);
410 }
411 mAllAppsList.add(index, item);
412 if (mRollo != null) {
413 mRollo.addApp(index, item);
414 mRollo.mState.iconCount++;
415 }
416 }
417
418 if (mRollo != null) {
419 mRollo.saveAppsList();
420 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700421 }
422
Joe Onoratoa8138d52009-10-06 19:25:30 -0700423 public void removeApps(ArrayList<ApplicationInfo> list) {
424 final int N = list.size();
425 for (int i=0; i<N; i++) {
426 final ApplicationInfo item = list.get(i);
427 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
428 if (index >= 0) {
429 mAllAppsList.remove(index);
430 if (mRollo != null) {
431 mRollo.removeApp(index);
432 mRollo.mState.iconCount--;
433 }
434 } else {
435 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
436 // Try to recover. This should keep us from crashing for now.
437 }
438 }
439
440 if (mRollo != null) {
441 mRollo.saveAppsList();
442 }
443 }
444
445 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
446 // Just remove and add, because they may need to be re-sorted.
447 removeApps(list);
448 addApps(list);
449 }
450
451 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
452 public int compare(ApplicationInfo a, ApplicationInfo b) {
453 int result = a.title.toString().compareTo(b.toString());
454 if (result != 0) {
455 return result;
456 }
457 return a.intent.getComponent().compareTo(b.intent.getComponent());
458 }
459 };
460
461 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
462 public int compare(ApplicationInfo a, ApplicationInfo b) {
463 return a.intent.getComponent().compareTo(b.intent.getComponent());
464 }
465 };
466
Joe Onoratoc567acb2009-08-31 14:34:43 -0700467 private static int countPages(int iconCount) {
468 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
469 int pages = iconCount / iconsPerPage;
470 if (pages*iconsPerPage != iconCount) {
471 pages++;
472 }
473 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400474 }
475
Joe Onorato93839052009-08-06 20:34:32 -0700476 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700477
Joe Onorato1feb3a82009-08-08 22:32:00 -0700478 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700479 private int mWidth;
480 private int mHeight;
481
482 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700483 private Script mScript;
484 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700485 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700486 private Script.Invokable mInvokeFling;
487 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700488
Jason Samsc1c521e2009-10-19 14:45:45 -0700489
Jason Samscd689e12009-09-29 15:28:22 -0700490 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700491 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700492 private ProgramFragment mPFColor;
493 private ProgramFragment mPFTexLinear;
Joe Onorato93839052009-08-06 20:34:32 -0700494 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700495 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700496 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700497 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700498
Joe Onoratod63458b2009-10-15 21:19:09 -0700499 private Allocation mHomeButtonNormal;
500 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700501
Joe Onoratobf15cb42009-08-07 14:33:40 -0700502 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700503 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700504 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700505
Joe Onoratobf15cb42009-08-07 14:33:40 -0700506 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700507 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700508 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700509 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700510
Joe Onorato6665c0f2009-09-02 15:27:24 -0700511 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700512 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700513
514 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400515 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700516
Jason Samsd8152b92009-10-13 17:19:10 -0700517
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700518 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700519 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700520
Jason Sams78aebd82009-09-15 13:06:59 -0700521 class BaseAlloc {
522 Allocation mAlloc;
523 Type mType;
524
525 void save() {
526 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700527 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700528 }
529
Jason Sams12c14a82009-10-06 14:33:15 -0700530 class AAMessage extends RenderScript.RSMessage {
531 public void run() {
532 mPosX = ((float)mData[0]) / (1 << 16);
533 mVelocity = ((float)mData[1]) / (1 << 16);
534 mZoom = ((float)mData[2]) / (1 << 16);
535 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
536 }
537 float mZoom;
538 float mPosX;
539 float mVelocity;
540 }
541 AAMessage mMessageProc;
542
Jason Sams476339d2009-09-29 18:14:38 -0700543 private boolean checkClickOK() {
544 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams12c14a82009-10-06 14:33:15 -0700545 return (Math.abs(mMessageProc.mVelocity) < 0.1f) &&
546 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.1f);
Jason Sams476339d2009-09-29 18:14:38 -0700547 }
548
Jason Sams78aebd82009-09-15 13:06:59 -0700549 class Params extends BaseAlloc {
550 Params() {
551 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
552 mAlloc = Allocation.createTyped(mRS, mType);
553 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700554 }
Jason Sams78aebd82009-09-15 13:06:59 -0700555 public int bubbleWidth;
556 public int bubbleHeight;
557 public int bubbleBitmapWidth;
558 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700559
Joe Onoratobcbeab82009-10-01 21:45:43 -0700560 public int homeButtonWidth;
561 public int homeButtonHeight;
562 public int homeButtonTextureWidth;
563 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700564 }
565
566 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700567 public float newPositionX;
568 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700569 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700570 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700571 public int selectedIconIndex = -1;
572 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700573 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700574 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700575 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700576
577 State() {
578 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
579 mAlloc = Allocation.createTyped(mRS, mType);
580 save();
581 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700582 }
583
584 public RolloRS() {
585 }
586
587 public void init(Resources res, int width, int height) {
588 mRes = res;
589 mWidth = width;
590 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700591 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700592 initProgramVertex();
593 initProgramFragment();
594 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700595 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700596 initGl();
597 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700598 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700599 initRs();
600 }
601
Jason Sams0aa71662009-10-02 18:43:18 -0700602 public void initMesh() {
603 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
604 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
605
Jason Samsd8152b92009-10-13 17:19:10 -0700606 float y = 0;
607 float z = 0;
608 for (int ct=0; ct < 200; ct++) {
609 float angle = 0;
610 float maxAngle = 3.14f * 0.16f;
611 float l = 1.f;
612
613 l = 1 - ((ct-5) * 0.10f);
614 if (ct > 7) {
615 angle = maxAngle * (ct - 7) * 0.2f;
616 angle = Math.min(angle, maxAngle);
617 }
618 l = Math.max(0.3f, l);
619 l = Math.min(1.0f, l);
620
621 y += 0.1f * Math.cos(angle);
622 z += 0.1f * Math.sin(angle);
623
624 float t = 0.1f * ct;
625 float ds = 0.08f;
626 tm.setColor(l, l, l, 0.99f);
627 tm.setTexture(ds, t);
628 tm.addVertex(-0.5f, y, z);
629 tm.setTexture(1 - ds, t);
630 tm.addVertex(0.5f, y, z);
631 }
632 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
633 tm.addTriangle(ct, ct+1, ct+2);
634 tm.addTriangle(ct+1, ct+3, ct+2);
635 }
636 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700637 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700638 }
639
Jason Samscd689e12009-09-29 15:28:22 -0700640 private void initProgramVertex() {
641 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
642 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700643
644 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700645 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700646 mPV = pvb.create();
647 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700648 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700649
Jason Sams37e7c2b2009-10-19 12:55:43 -0700650 //pva = new ProgramVertex.MatrixAllocation(mRS);
651 //pva.setupOrthoWindow(mWidth, mHeight);
652 //pvb.setTextureMatrixEnable(true);
653 //mPVOrtho = pvb.create();
654 //mPVOrtho.setName("PVOrtho");
655 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700656
657 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700658 }
Joe Onorato93839052009-08-06 20:34:32 -0700659
Jason Samscd689e12009-09-29 15:28:22 -0700660 private void initProgramFragment() {
661 Sampler.Builder sb = new Sampler.Builder(mRS);
662 sb.setMin(Sampler.Value.LINEAR);
663 sb.setMag(Sampler.Value.LINEAR);
664 sb.setWrapS(Sampler.Value.CLAMP);
665 sb.setWrapT(Sampler.Value.CLAMP);
666 Sampler linear = sb.create();
667
668 sb.setMin(Sampler.Value.NEAREST);
669 sb.setMag(Sampler.Value.NEAREST);
670 Sampler nearest = sb.create();
671
672 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700673 //mPFColor = bf.create();
674 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700675
676 bf.setTexEnable(true, 0);
677 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
678 mPFTexLinear = bf.create();
679 mPFTexLinear.setName("PFTexLinear");
680 mPFTexLinear.bindSampler(linear, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700681 }
682
683 private void initProgramStore() {
684 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
685 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700686 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700687 bs.setDitherEnable(true);
688 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
689 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
690 mPSIcons = bs.create();
691 mPSIcons.setName("PSIcons");
692
693 //bs.setDitherEnable(false);
694 //mPSText = bs.create();
695 //mPSText.setName("PSText");
696 }
697
698 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700699 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700700 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700701 }
Jason Sams78aebd82009-09-15 13:06:59 -0700702
Joe Onorato1feb3a82009-08-08 22:32:00 -0700703 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700704 mParams = new Params();
705 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700706
Joe Onoratobf15cb42009-08-07 14:33:40 -0700707 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700708
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700709 mParams.bubbleWidth = bubble.getBubbleWidth();
710 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
711 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
712 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700713
Joe Onoratod63458b2009-10-15 21:19:09 -0700714 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
715 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
716 mHomeButtonNormal.uploadToTexture(0);
717 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
718 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
719 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700720 mParams.homeButtonWidth = 76;
721 mParams.homeButtonHeight = 68;
722 mParams.homeButtonTextureWidth = 128;
723 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700724
Joe Onoratod63458b2009-10-15 21:19:09 -0700725 mState.homeButtonId = mHomeButtonNormal.getID();
726
Joe Onorato1feb3a82009-08-08 22:32:00 -0700727 mParams.save();
728 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400729
Joe Onorato1291a8c2009-09-15 15:07:25 -0400730 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
731 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
732 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700733
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700735 }
736
Jason Sams37e7c2b2009-10-19 12:55:43 -0700737 private void initScript(int id) {
738 }
739
740 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700741 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700742 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700743 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700744 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700745 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
746 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700747 mInvokeMove = sb.addInvokable("move");
748 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700749 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700750 mInvokeResetWAR = sb.addInvokable("resetHWWar");
751 mScript = sb.create();
752 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
753 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
754 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
755 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
756 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700757
Jason Sams12c14a82009-10-06 14:33:15 -0700758 mMessageProc = new AAMessage();
759 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700760 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700761 }
Joe Onorato93839052009-08-06 20:34:32 -0700762
Joe Onorato9c1289c2009-08-17 11:03:03 -0400763 private void setApps(ArrayList<ApplicationInfo> list) {
764 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700765 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700766 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700767 allocCount = 1;
768 }
769
Joe Onorato9c1289c2009-08-17 11:03:03 -0400770 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700771 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700772 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400773
774 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700775 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700776 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400777
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700778 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779
780 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
781
782 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700783 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400784 }
785
Joe Onorato9c1289c2009-08-17 11:03:03 -0400786 mState.iconCount = count;
787
Joe Onoratoa8138d52009-10-06 19:25:30 -0700788 saveAppsList();
789 }
790
791 private void uploadAppIcon(int index, ApplicationInfo item) {
792 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
793 Element.RGBA_8888(mRS), false);
794 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
795 Element.RGBA_8888(mRS), false);
796
797 mIcons[index].uploadToTexture(0);
798 mLabels[index].uploadToTexture(0);
799
800 mIconIds[index] = mIcons[index].getID();
801 mLabelIds[index] = mLabels[index].getID();
802 }
803
804 /**
805 * Puts the empty spaces at the end. Updates mState.iconCount. You must
806 * fill in the values and call saveAppsList().
807 */
808 private void reallocAppsList(int count) {
809 Allocation[] icons = new Allocation[count];
810 int[] iconIds = new int[count];
811 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
812
813 Allocation[] labels = new Allocation[count];
814 int[] labelIds = new int[count];
815 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
816
817 final int oldCount = mIcons.length;
818
819 System.arraycopy(mIcons, 0, icons, 0, oldCount);
820 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
821 System.arraycopy(mLabels, 0, labels, 0, oldCount);
822 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
823
824 mIcons = icons;
825 mIconIds = iconIds;
826 mLabels = labels;
827 mLabelIds = labelIds;
828 }
829
830 /**
831 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
832 */
833 private void addApp(int index, ApplicationInfo item) {
834 final int count = mState.iconCount - index;
835 final int dest = index + 1;
836
837 System.arraycopy(mIcons, index, mIcons, dest, count);
838 System.arraycopy(mIconIds, index, mIconIds, dest, count);
839 System.arraycopy(mLabels, index, mLabels, dest, count);
840 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
841
842 uploadAppIcon(index, item);
843 }
844
845 /**
846 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
847 */
848 private void removeApp(int index) {
849 final int count = mState.iconCount - index - 1;
850 final int src = index + 1;
851
852 System.arraycopy(mIcons, src, mIcons, index, count);
853 System.arraycopy(mIconIds, src, mIconIds, index, count);
854 System.arraycopy(mLabels, src, mLabels, index, count);
855 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
856
857 final int last = mState.iconCount - 1;
858 mIcons[last] = null;
859 mIconIds[last] = 0;
860 mLabels[last] = null;
861 mLabelIds[last] = 0;
862 }
863
864 /**
865 * Send the apps list structures to RS.
866 */
867 private void saveAppsList() {
868 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -0700869
Joe Onoratoa8138d52009-10-06 19:25:30 -0700870 mAllocIconIds.data(mIconIds);
871 mAllocLabelIds.data(mLabelIds);
872
Jason Sams37e7c2b2009-10-19 12:55:43 -0700873 if (mScript != null) { // this happens when we init it
874 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
875 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400876 }
877
878 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -0700879
880 // Note: mScript may be null if we haven't initialized it yet.
881 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -0700882 if (mInvokeResetWAR != null) {
883 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -0700884 }
Jason Sams37e7c2b2009-10-19 12:55:43 -0700885 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400886 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700887
888 void initTouchState() {
889 int width = getWidth();
890 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700891 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
892 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700893
Jason Sams37e7c2b2009-10-19 12:55:43 -0700894 int centerY = (height / 2);
895 mTouchYBorders[0] = centerY - (cellHeight * 2);
896 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700897 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700898 mTouchYBorders[3] = centerY + cellHeight;
899 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -0700900
Joe Onorato6665c0f2009-09-02 15:27:24 -0700901 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700902 mTouchXBorders[0] = 0;
903 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700904 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700905 mTouchXBorders[3] = centerX + (width / 4);
906 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700907 }
908
Jason Sams37e7c2b2009-10-19 12:55:43 -0700909 int chooseTappedIcon(int x, int y, float pos) {
910 // Adjust for scroll position if not zero.
911 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -0700912
Joe Onorato6665c0f2009-09-02 15:27:24 -0700913 int col = -1;
914 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700915 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
916 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
917 col = i;
918 break;
919 }
920 }
921 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
922 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
923 row = i;
924 break;
925 }
926 }
927
928 if (row < 0 || col < 0) {
929 return -1;
930 }
931
Jason Sams37e7c2b2009-10-19 12:55:43 -0700932 return (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -0700933 + (row * Defines.ROWS_PER_PAGE) + col;
934 }
935
Jason Samsd8152b92009-10-13 17:19:10 -0700936 void fling() {
Jason Sams37e7c2b2009-10-19 12:55:43 -0700937 mInvokeFling.execute();
Jason Samsd8152b92009-10-13 17:19:10 -0700938 }
939
940 void move() {
Jason Sams37e7c2b2009-10-19 12:55:43 -0700941 mInvokeMove.execute();
Jason Samsd8152b92009-10-13 17:19:10 -0700942 }
943
Jason Samsc1c521e2009-10-19 14:45:45 -0700944 void moveTo(float row) {
945 mState.targetPos = row;
946 mState.save();
947 mInvokeMoveTo.execute();
948 }
949
Joe Onorato6665c0f2009-09-02 15:27:24 -0700950 /**
951 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -0700952 *
953 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -0700954 */
Joe Onorato82ca5502009-10-15 16:59:23 -0700955 int selectIcon(int x, int y, float pos) {
956 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400957 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -0700958 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400959 }
960
961 void selectIcon(int index) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700962 if (index < 0) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700963 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700964 } else {
965 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400966
967 Bitmap selectionBitmap = mSelectionBitmap;
968
969 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
970 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
971 mAllAppsList.get(index).iconBitmap);
972
973 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700974 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400975 mSelectedIcon.uploadToTexture(0);
976 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700977 }
978 }
979
980 /**
981 * You need to call save() on mState on your own after calling this.
982 */
983 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -0400984 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700985 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700986
Joe Onoratod63458b2009-10-15 21:19:09 -0700987 void setHomeSelected(boolean pressed) {
988 if (pressed) {
989 mState.homeButtonId = mHomeButtonPressed.getID();
990 } else {
991 mState.homeButtonId = mHomeButtonNormal.getID();
992 }
993 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 }
Joe Onorato93839052009-08-06 20:34:32 -0700995}
996
997