blob: 608556051cca88adf7d6a1b4c9590b2f0b7a0d35 [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;
Jason Samsd8152b92009-10-13 17:19:10 -0700101 private boolean mRotateMove = true;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700102
Joe Onorato6665c0f2009-09-02 15:27:24 -0700103 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700104 public static final int ALLOC_PARAMS = 0;
105 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700106 public static final int ALLOC_ICON_IDS = 3;
107 public static final int ALLOC_LABEL_IDS = 4;
108 public static final int ALLOC_X_BORDERS = 5;
109 public static final int ALLOC_Y_BORDERS = 6;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700110
111 public static final int COLUMNS_PER_PAGE = 4;
112 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700113
Joe Onorato6665c0f2009-09-02 15:27:24 -0700114 public static final float RADIUS = 4.0f;
115
Joe Onorato6665c0f2009-09-02 15:27:24 -0700116 public static final int ICON_WIDTH_PX = 64;
117 public static final int ICON_TEXTURE_WIDTH_PX = 128;
118
119 public static final int ICON_HEIGHT_PX = 64;
120 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
121 public static final float ICON_TOP_OFFSET = 0.2f;
122
123 public static final float CAMERA_Z = -2;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700124
125 public int SCREEN_WIDTH_PX;
126 public int SCREEN_HEIGHT_PX;
127
128 public float FAR_ICON_SIZE;
129
130 public void recompute(int w, int h) {
131 SCREEN_WIDTH_PX = 480;
132 SCREEN_HEIGHT_PX = 800;
133 FAR_ICON_SIZE = farSize(2 * ICON_WIDTH_PX / (float)w);
134 }
135
136 private static float farSize(float sizeAt0) {
137 return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z;
138 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700139 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700140
141 public AllAppsView(Context context, AttributeSet attrs) {
142 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700143 setFocusable(true);
144 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700145 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700146 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700147 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
148
Joe Onorato6665c0f2009-09-02 15:27:24 -0700149 setOnClickListener(this);
150 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700151 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700152 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700153 }
154
Joe Onorato7c312c12009-08-13 21:36:53 -0700155 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
156 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700157 }
158
Joe Onorato6665c0f2009-09-02 15:27:24 -0700159 public void setLauncher(Launcher launcher) {
160 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700161 }
162
Joe Onorato1feb3a82009-08-08 22:32:00 -0700163 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700164 public void surfaceDestroyed(SurfaceHolder holder) {
165 super.surfaceDestroyed(holder);
166
167 destroyRenderScript();
168 mRS = null;
169 mRollo = null;
170 }
171
172 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700173 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
174 super.surfaceChanged(holder, format, w, h);
175
Joe Onorato6665c0f2009-09-02 15:27:24 -0700176 long startTime = SystemClock.uptimeMillis();
177
Jason Sams05de32a2009-09-27 14:01:40 -0700178 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700179 mRollo = new RolloRS();
180 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 if (mAllAppsList != null) {
182 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700183 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700185
186 Resources res = getContext().getResources();
187 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700188 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700189
190 long endTime = SystemClock.uptimeMillis();
191 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700192 }
193
194 @Override
195 public boolean onKeyDown(int keyCode, KeyEvent event)
196 {
Joe Onorato93839052009-08-06 20:34:32 -0700197 // this method doesn't work when 'extends View' include 'extends ScrollView'.
198 return super.onKeyDown(keyCode, event);
199 }
200
Jason Samsd8152b92009-10-13 17:19:10 -0700201 private int mRSMode = 0;
202
Joe Onorato93839052009-08-06 20:34:32 -0700203 @Override
204 public boolean onTouchEvent(MotionEvent ev)
205 {
Joe Onorato7bb17492009-09-24 17:51:01 -0700206 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700207 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700208 }
209
Joe Onoratofb0ca672009-09-14 17:55:46 -0400210 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700211 return true;
212 }
213
214 super.onTouchEvent(ev);
215
Joe Onoratofb0ca672009-09-14 17:55:46 -0400216 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700217 int y = (int)ev.getY();
218
Joe Onoratobcbeab82009-10-01 21:45:43 -0700219 int action = ev.getAction();
220 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400221 case MotionEvent.ACTION_DOWN:
Jason Samsd8152b92009-10-13 17:19:10 -0700222 if (x < 60 && y > 700) {
Jason Samsb52dfa02009-10-14 20:16:14 -0700223 //mRotateMove = mRollo.setView((++mRSMode) & 3);
Jason Samsd8152b92009-10-13 17:19:10 -0700224 }
225
Joe Onoratobcbeab82009-10-01 21:45:43 -0700226 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
227 mTouchTracking = TRACKING_HOME;
Joe Onoratod63458b2009-10-15 21:19:09 -0700228 mRollo.setHomeSelected(true);
229 mRollo.mState.save();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700230 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700231 mTouchTracking = TRACKING_FLING;
232
233 mMotionDownRawX = (int)ev.getRawX();
234 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700235
Jason Samsd8152b92009-10-13 17:19:10 -0700236 if (mRotateMove) {
237 mRollo.mState.newPositionX = ev.getRawY() / mDefines.SCREEN_WIDTH_PX;
238 } else {
239 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
240 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700241 mRollo.mState.newTouchDown = 1;
242
243 if (!mRollo.checkClickOK()) {
244 mRollo.clearSelectedIcon();
245 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700246 mDownIconIndex = mCurrentIconIndex
247 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
248 if (mDownIconIndex < 0) {
249 // if nothing was selected, no long press.
250 cancelLongPress();
251 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700252 }
253 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700254 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700255 mVelocity = VelocityTracker.obtain();
256 mVelocity.addMovement(ev);
257 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700258 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400259 break;
260 case MotionEvent.ACTION_MOVE:
261 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700262 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratod63458b2009-10-15 21:19:09 -0700263 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]);
264 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700265 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700266 int rawX = (int)ev.getRawX();
267 int rawY = (int)ev.getRawY();
268 int slop;
Jason Samsd8152b92009-10-13 17:19:10 -0700269 if (mRotateMove) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700270 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700271 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700272 slop = Math.abs(rawX - mMotionDownRawX);
Jason Samsd8152b92009-10-13 17:19:10 -0700273 }
274
Joe Onorato82ca5502009-10-15 16:59:23 -0700275 if (!mStartedScrolling && slop < mSlop) {
276 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700277 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700278 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
279 if (mDownIconIndex != mCurrentIconIndex) {
280 // If a different icon is selected, don't allow it to be picked up.
281 // This handles off-axis dragging.
282 cancelLongPress();
283 mCurrentIconIndex = -1;
284 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700285 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700286 if (!mStartedScrolling) {
287 cancelLongPress();
288 mCurrentIconIndex = -1;
289 }
Jason Samsd8152b92009-10-13 17:19:10 -0700290 if (mRotateMove) {
291 mRollo.mState.newPositionX = ev.getRawY() / mDefines.SCREEN_WIDTH_PX;
292 } else {
293 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
294 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700295 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700296 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700297
Joe Onoratobcbeab82009-10-01 21:45:43 -0700298 mStartedScrolling = true;
299 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700300 mVelocity.addMovement(ev);
301 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700302 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400303 }
304 break;
305 case MotionEvent.ACTION_UP:
306 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700307 if (mTouchTracking == TRACKING_HOME) {
308 if (action == MotionEvent.ACTION_UP) {
309 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
310 mLauncher.closeAllApps(true);
311 }
Joe Onoratod63458b2009-10-15 21:19:09 -0700312 mRollo.setHomeSelected(false);
313 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700314 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700315 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700316 mRollo.mState.newTouchDown = 0;
Jason Samsd8152b92009-10-13 17:19:10 -0700317 if (mRotateMove) {
318 mRollo.mState.newPositionX = ev.getRawY() / mDefines.SCREEN_WIDTH_PX;
319 } else {
320 mRollo.mState.newPositionX = ev.getRawX() / mDefines.SCREEN_WIDTH_PX;
321 }
Jason Sams476339d2009-09-29 18:14:38 -0700322
Jason Sams12c14a82009-10-06 14:33:15 -0700323 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Samsd8152b92009-10-13 17:19:10 -0700324 if (mRotateMove) {
325 mRollo.mState.flingVelocityX
326 = mVelocity.getYVelocity() / mDefines.SCREEN_WIDTH_PX;
327 } else {
328 mRollo.mState.flingVelocityX
329 = mVelocity.getXVelocity() / mDefines.SCREEN_WIDTH_PX;
330 }
Jason Sams12c14a82009-10-06 14:33:15 -0700331 mRollo.clearSelectedIcon();
332 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700333 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700334
Joe Onorato539ed9d2009-10-02 10:22:14 -0700335 if (mVelocity != null) {
336 mVelocity.recycle();
337 mVelocity = null;
338 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700339 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700340 mTouchTracking = TRACKING_NONE;
341 break;
Joe Onorato93839052009-08-06 20:34:32 -0700342 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700343
344 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700345 }
346
Joe Onorato6665c0f2009-09-02 15:27:24 -0700347 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700348 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400349 return;
350 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700351 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
352 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
353 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700354 mLauncher.startActivitySafely(app.intent);
355 }
356 }
357
358 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700359 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400360 return true;
361 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700362 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
363 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
364 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700365
366 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700367 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
368 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700369
Joe Onoratobcbeab82009-10-01 21:45:43 -0700370 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
371 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700372 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700373 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700374 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700375
Joe Onorato7bb17492009-09-24 17:51:01 -0700376 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700377 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700378 return true;
379 }
380
Joe Onorato5162ea92009-09-03 09:39:42 -0700381 public void setDragController(DragController dragger) {
382 mDragController = dragger;
383 }
384
385 public void onDropCompleted(View target, boolean success) {
386 }
387
Joe Onorato4db52312009-10-06 11:17:43 -0700388 /**
389 * Zoom to the specifed amount.
390 *
391 * @param amount [0..1] 0 is hidden, 1 is open
392 * @param animate Whether to animate.
393 */
Jason Sams12c14a82009-10-06 14:33:15 -0700394 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700395 if (mRollo == null) {
396 return;
397 }
398
Joe Onoratofb0ca672009-09-14 17:55:46 -0400399 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400400 mRollo.clearSelectedIcon();
Joe Onorato1d708e72009-10-15 21:29:21 -0700401 mRollo.setHomeSelected(false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700402 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700403 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700404 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700405 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700406 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700407 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400408 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400409 }
410
411 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700412 if (mRollo == null) {
413 return false;
414 }
Jason Sams12c14a82009-10-06 14:33:15 -0700415 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400416 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700417
Joe Onorato93839052009-08-06 20:34:32 -0700418 @Override
419 public boolean onTrackballEvent(MotionEvent ev)
420 {
421 float x = ev.getX();
422 float y = ev.getY();
423 //Float tx = new Float(x);
424 //Float ty = new Float(y);
425 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
426
427
428 return true;
429 }
430
Joe Onorato9c1289c2009-08-17 11:03:03 -0400431 public void setApps(ArrayList<ApplicationInfo> list) {
432 mAllAppsList = list;
433 if (mRollo != null) {
434 mRollo.setApps(list);
435 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700436 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400437 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700438 }
439
Joe Onoratoa8138d52009-10-06 19:25:30 -0700440 public void addApps(ArrayList<ApplicationInfo> list) {
441 final int N = list.size();
442 if (mRollo != null) {
443 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
444 }
445
446 for (int i=0; i<N; i++) {
447 final ApplicationInfo item = list.get(i);
448 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
449 if (index < 0) {
450 index = -(index+1);
451 }
452 mAllAppsList.add(index, item);
453 if (mRollo != null) {
454 mRollo.addApp(index, item);
455 mRollo.mState.iconCount++;
456 }
457 }
458
459 if (mRollo != null) {
460 mRollo.saveAppsList();
461 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700462 }
463
Joe Onoratoa8138d52009-10-06 19:25:30 -0700464 public void removeApps(ArrayList<ApplicationInfo> list) {
465 final int N = list.size();
466 for (int i=0; i<N; i++) {
467 final ApplicationInfo item = list.get(i);
468 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
469 if (index >= 0) {
470 mAllAppsList.remove(index);
471 if (mRollo != null) {
472 mRollo.removeApp(index);
473 mRollo.mState.iconCount--;
474 }
475 } else {
476 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
477 // Try to recover. This should keep us from crashing for now.
478 }
479 }
480
481 if (mRollo != null) {
482 mRollo.saveAppsList();
483 }
484 }
485
486 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
487 // Just remove and add, because they may need to be re-sorted.
488 removeApps(list);
489 addApps(list);
490 }
491
492 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
493 public int compare(ApplicationInfo a, ApplicationInfo b) {
494 int result = a.title.toString().compareTo(b.toString());
495 if (result != 0) {
496 return result;
497 }
498 return a.intent.getComponent().compareTo(b.intent.getComponent());
499 }
500 };
501
502 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
503 public int compare(ApplicationInfo a, ApplicationInfo b) {
504 return a.intent.getComponent().compareTo(b.intent.getComponent());
505 }
506 };
507
Joe Onoratoc567acb2009-08-31 14:34:43 -0700508 private static int countPages(int iconCount) {
509 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
510 int pages = iconCount / iconsPerPage;
511 if (pages*iconsPerPage != iconCount) {
512 pages++;
513 }
514 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400515 }
516
Joe Onorato93839052009-08-06 20:34:32 -0700517 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700518
Joe Onorato1feb3a82009-08-08 22:32:00 -0700519 // Allocations ======
Jason Samsd8152b92009-10-13 17:19:10 -0700520 private int mViewMode = 0;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700521
Joe Onorato93839052009-08-06 20:34:32 -0700522 private int mWidth;
523 private int mHeight;
524
525 private Resources mRes;
Jason Samsd8152b92009-10-13 17:19:10 -0700526 private Script[] mScript = new Script[4];
Jason Sams86c87ed2009-09-18 13:55:55 -0700527
Jason Samsd8152b92009-10-13 17:19:10 -0700528 private Script.Invokable[] mInvokeMove = new Script.Invokable[4];
529 private Script.Invokable[] mInvokeFling = new Script.Invokable[4];
Jason Sams41b61c82009-10-15 15:40:54 -0700530 private Script.Invokable[] mInvokeResetWAR = new Script.Invokable[4];
Jason Sams86c87ed2009-09-18 13:55:55 -0700531
Jason Samscd689e12009-09-29 15:28:22 -0700532 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700533 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700534 private ProgramFragment mPFColor;
535 private ProgramFragment mPFTexLinear;
536 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700537 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700538 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700539 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700540 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700541
Joe Onoratod63458b2009-10-15 21:19:09 -0700542 private Allocation mHomeButtonNormal;
543 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700544
Joe Onoratobf15cb42009-08-07 14:33:40 -0700545 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700546 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700547 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700548
Joe Onoratobf15cb42009-08-07 14:33:40 -0700549 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700550 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700551 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700552 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700553
Joe Onorato6665c0f2009-09-02 15:27:24 -0700554 private int[] mTouchYBorders;
555 private Allocation mAllocTouchYBorders;
556 private int[] mTouchXBorders;
557 private Allocation mAllocTouchXBorders;
558
559 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400560 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700561
Jason Samsd8152b92009-10-13 17:19:10 -0700562
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700563 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700564 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700565
Jason Sams78aebd82009-09-15 13:06:59 -0700566 class BaseAlloc {
567 Allocation mAlloc;
568 Type mType;
569
570 void save() {
571 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700572 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700573 }
574
Jason Sams12c14a82009-10-06 14:33:15 -0700575 class AAMessage extends RenderScript.RSMessage {
576 public void run() {
577 mPosX = ((float)mData[0]) / (1 << 16);
578 mVelocity = ((float)mData[1]) / (1 << 16);
579 mZoom = ((float)mData[2]) / (1 << 16);
580 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
581 }
582 float mZoom;
583 float mPosX;
584 float mVelocity;
585 }
586 AAMessage mMessageProc;
587
Jason Sams476339d2009-09-29 18:14:38 -0700588 private boolean checkClickOK() {
589 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams12c14a82009-10-06 14:33:15 -0700590 return (Math.abs(mMessageProc.mVelocity) < 0.1f) &&
591 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.1f);
Jason Sams476339d2009-09-29 18:14:38 -0700592 }
593
Jason Sams78aebd82009-09-15 13:06:59 -0700594 class Params extends BaseAlloc {
595 Params() {
596 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
597 mAlloc = Allocation.createTyped(mRS, mType);
598 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700599 }
Jason Sams78aebd82009-09-15 13:06:59 -0700600 public int bubbleWidth;
601 public int bubbleHeight;
602 public int bubbleBitmapWidth;
603 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700604
Joe Onoratobcbeab82009-10-01 21:45:43 -0700605 public int homeButtonWidth;
606 public int homeButtonHeight;
607 public int homeButtonTextureWidth;
608 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700609 }
610
611 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700612 public float newPositionX;
613 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700614 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700615 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700616 public int selectedIconIndex = -1;
617 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700618 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700619 public int homeButtonId;
Jason Sams78aebd82009-09-15 13:06:59 -0700620
621 State() {
622 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
623 mAlloc = Allocation.createTyped(mRS, mType);
624 save();
625 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700626 }
627
628 public RolloRS() {
629 }
630
631 public void init(Resources res, int width, int height) {
632 mRes = res;
633 mWidth = width;
634 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700635 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700636 initProgramVertex();
637 initProgramFragment();
638 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700639 initMesh();
Jason Samsd8152b92009-10-13 17:19:10 -0700640 initMesh2();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700641 initGl();
642 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700643 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700644 initRs();
645 }
646
Jason Sams0aa71662009-10-02 18:43:18 -0700647 public void initMesh() {
648 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
649 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
650
651 for (int ct=0; ct < 450; ct++) {
652 float x = 0;
653 float z = 0;
654 float l = 1.f;
655
656 if (ct < 190) {
657 z = 0.1f + 0.05f * (190 - ct);
658 x = -1;
659 l = 0.125f + (0.125f / 190.f) * ct;
660 } else if (ct >= 190 && ct < 200) {
661 float a = (3.14f * 0.5f) * (0.1f * (ct - 200));
662 float s = (float)Math.sin(a);
663 float c = (float)Math.cos(a);
664 x = -0.9f + s * 0.1f;
665 z = 0.1f - c * 0.1f;
666 l = 0.25f + 0.075f * (ct - 190);
667 } else if (ct >= 200 && ct < 250) {
668 z = 0.f;
669 x = -0.9f + (1.8f * (ct - 200) / 50.f);
670 } else if (ct >= 250 && ct < 260) {
671 float a = (3.14f * 0.5f) * (0.1f * (ct - 250));
672 float s = (float)Math.sin(a);
673 float c = (float)Math.cos(a);
674 x = 0.9f + s * 0.1f;
675 z = 0.1f - c * 0.1f;
676 l = 0.25f + 0.075f * (260 - ct);
677 } else if (ct >= 260) {
678 z = 0.1f + 0.05f * (ct - 260);
679 x = 1;
680 l = 0.125f + (0.125f / 190.f) * (450 - ct);
681 }
682 //Log.e("rs", "ct " + Integer.toString(ct) + " x = " + Float.toString(x) + ", z = " + Float.toString(z));
683 //Log.e("rs", "ct " + Integer.toString(ct) + " l = " + Float.toString(l));
684 float s = ct * 0.1f;
685 tm.setColor(l, l, l, 0.99f);
686 tm.setTexture(s, 1);
687 tm.addVertex(x, -0.5f, z);
688 tm.setTexture(s, 0);
689 tm.addVertex(x, 0.5f, z);
690 }
691 for (int ct=0; ct < (450*2 - 2); ct+= 2) {
692 tm.addTriangle(ct, ct+1, ct+2);
693 tm.addTriangle(ct+1, ct+3, ct+2);
694 }
695 mMesh = tm.create();
696 mMesh.setName("SMMesh");
697
698 }
699
700
Jason Samsd8152b92009-10-13 17:19:10 -0700701 public void initMesh2() {
702 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
703 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
704
705 float y = 0;
706 float z = 0;
707 for (int ct=0; ct < 200; ct++) {
708 float angle = 0;
709 float maxAngle = 3.14f * 0.16f;
710 float l = 1.f;
711
712 l = 1 - ((ct-5) * 0.10f);
713 if (ct > 7) {
714 angle = maxAngle * (ct - 7) * 0.2f;
715 angle = Math.min(angle, maxAngle);
716 }
717 l = Math.max(0.3f, l);
718 l = Math.min(1.0f, l);
719
720 y += 0.1f * Math.cos(angle);
721 z += 0.1f * Math.sin(angle);
722
723 float t = 0.1f * ct;
724 float ds = 0.08f;
725 tm.setColor(l, l, l, 0.99f);
726 tm.setTexture(ds, t);
727 tm.addVertex(-0.5f, y, z);
728 tm.setTexture(1 - ds, t);
729 tm.addVertex(0.5f, y, z);
730 }
731 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
732 tm.addTriangle(ct, ct+1, ct+2);
733 tm.addTriangle(ct+1, ct+3, ct+2);
734 }
735 mMesh2 = tm.create();
736 mMesh2.setName("SMMesh2");
737 }
738
Jason Samscd689e12009-09-29 15:28:22 -0700739 private void initProgramVertex() {
740 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
741 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700742
743 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700744 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700745 mPV = pvb.create();
746 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700747 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700748
Jason Samscd689e12009-09-29 15:28:22 -0700749 pva = new ProgramVertex.MatrixAllocation(mRS);
750 pva.setupOrthoWindow(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700751 pvb.setTextureMatrixEnable(true);
752 mPVOrtho = pvb.create();
753 mPVOrtho.setName("PVOrtho");
Jason Samscd689e12009-09-29 15:28:22 -0700754 mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700755
756 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700757 }
Joe Onorato93839052009-08-06 20:34:32 -0700758
Jason Samscd689e12009-09-29 15:28:22 -0700759 private void initProgramFragment() {
760 Sampler.Builder sb = new Sampler.Builder(mRS);
761 sb.setMin(Sampler.Value.LINEAR);
762 sb.setMag(Sampler.Value.LINEAR);
763 sb.setWrapS(Sampler.Value.CLAMP);
764 sb.setWrapT(Sampler.Value.CLAMP);
765 Sampler linear = sb.create();
766
767 sb.setMin(Sampler.Value.NEAREST);
768 sb.setMag(Sampler.Value.NEAREST);
769 Sampler nearest = sb.create();
770
771 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
772 mPFColor = bf.create();
773 mPFColor.setName("PFColor");
774
775 bf.setTexEnable(true, 0);
776 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
777 mPFTexLinear = bf.create();
778 mPFTexLinear.setName("PFTexLinear");
779 mPFTexLinear.bindSampler(linear, 0);
780
781 mPFTexNearest = bf.create();
782 mPFTexNearest.setName("PFTexNearest");
783 mPFTexNearest.bindSampler(nearest, 0);
784 }
785
786 private void initProgramStore() {
787 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
788 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700789 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700790 bs.setDitherEnable(true);
791 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
792 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
793 mPSIcons = bs.create();
794 mPSIcons.setName("PSIcons");
795
796 //bs.setDitherEnable(false);
797 //mPSText = bs.create();
798 //mPSText.setName("PSText");
799 }
800
801 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700802 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700803 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700804 mTouchXBorders.length);
805 mAllocTouchXBorders.data(mTouchXBorders);
806
807 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700808 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700809 mTouchYBorders.length);
810 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700811 }
Jason Sams78aebd82009-09-15 13:06:59 -0700812
Joe Onorato1feb3a82009-08-08 22:32:00 -0700813 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700814 mParams = new Params();
815 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700816
Joe Onoratobf15cb42009-08-07 14:33:40 -0700817 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700818
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700819 mParams.bubbleWidth = bubble.getBubbleWidth();
820 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
821 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
822 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700823
Joe Onoratod63458b2009-10-15 21:19:09 -0700824 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
825 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
826 mHomeButtonNormal.uploadToTexture(0);
827 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
828 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
829 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700830 mParams.homeButtonWidth = 76;
831 mParams.homeButtonHeight = 68;
832 mParams.homeButtonTextureWidth = 128;
833 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700834
Joe Onoratod63458b2009-10-15 21:19:09 -0700835 mState.homeButtonId = mHomeButtonNormal.getID();
836
Joe Onorato1feb3a82009-08-08 22:32:00 -0700837 mParams.save();
838 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400839
Joe Onorato1291a8c2009-09-15 15:07:25 -0400840 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
841 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
842 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700843
Joe Onorato9c1289c2009-08-17 11:03:03 -0400844 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700845 }
846
Jason Samsd8152b92009-10-13 17:19:10 -0700847 private void initScript(int idx, int id) {
Joe Onorato93839052009-08-06 20:34:32 -0700848 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Samsd8152b92009-10-13 17:19:10 -0700849 sb.setScript(mRes, id);
Joe Onorato93839052009-08-06 20:34:32 -0700850 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700851 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700852 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
853 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Samsd8152b92009-10-13 17:19:10 -0700854 mInvokeMove[idx] = sb.addInvokable("move");
855 mInvokeFling[idx] = sb.addInvokable("fling");
Jason Sams41b61c82009-10-15 15:40:54 -0700856 mInvokeResetWAR[idx] = sb.addInvokable("resetHWWar");
Jason Samsd8152b92009-10-13 17:19:10 -0700857 mScript[idx] = sb.create();
858 mScript[idx].setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
859 mScript[idx].bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
860 mScript[idx].bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
861 mScript[idx].bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
862 mScript[idx].bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
863 mScript[idx].bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
864 mScript[idx].bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
865 }
Joe Onorato93839052009-08-06 20:34:32 -0700866
Jason Samsd8152b92009-10-13 17:19:10 -0700867 private void initRs() {
868 mViewMode = 0;
869 initScript(0, R.raw.rollo3);
870 initScript(1, R.raw.rollo2);
871 initScript(2, R.raw.rollo);
872 initScript(3, R.raw.rollo4);
Joe Onorato93839052009-08-06 20:34:32 -0700873
Jason Sams12c14a82009-10-06 14:33:15 -0700874 mMessageProc = new AAMessage();
875 mRS.mMessageCallback = mMessageProc;
Jason Samsd8152b92009-10-13 17:19:10 -0700876 mRS.contextBindRootScript(mScript[mViewMode]);
Joe Onorato93839052009-08-06 20:34:32 -0700877 }
Joe Onorato93839052009-08-06 20:34:32 -0700878
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 private void setApps(ArrayList<ApplicationInfo> list) {
880 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700881 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700882 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700883 allocCount = 1;
884 }
885
Joe Onorato9c1289c2009-08-17 11:03:03 -0400886 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700887 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700888 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400889
890 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700891 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700892 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400893
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700894 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400895
896 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
897
898 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700899 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400900 }
901
Joe Onorato9c1289c2009-08-17 11:03:03 -0400902 mState.iconCount = count;
903
Joe Onoratoa8138d52009-10-06 19:25:30 -0700904 saveAppsList();
905 }
906
907 private void uploadAppIcon(int index, ApplicationInfo item) {
908 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
909 Element.RGBA_8888(mRS), false);
910 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
911 Element.RGBA_8888(mRS), false);
912
913 mIcons[index].uploadToTexture(0);
914 mLabels[index].uploadToTexture(0);
915
916 mIconIds[index] = mIcons[index].getID();
917 mLabelIds[index] = mLabels[index].getID();
918 }
919
920 /**
921 * Puts the empty spaces at the end. Updates mState.iconCount. You must
922 * fill in the values and call saveAppsList().
923 */
924 private void reallocAppsList(int count) {
925 Allocation[] icons = new Allocation[count];
926 int[] iconIds = new int[count];
927 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
928
929 Allocation[] labels = new Allocation[count];
930 int[] labelIds = new int[count];
931 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
932
933 final int oldCount = mIcons.length;
934
935 System.arraycopy(mIcons, 0, icons, 0, oldCount);
936 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
937 System.arraycopy(mLabels, 0, labels, 0, oldCount);
938 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
939
940 mIcons = icons;
941 mIconIds = iconIds;
942 mLabels = labels;
943 mLabelIds = labelIds;
944 }
945
946 /**
947 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
948 */
949 private void addApp(int index, ApplicationInfo item) {
950 final int count = mState.iconCount - index;
951 final int dest = index + 1;
952
953 System.arraycopy(mIcons, index, mIcons, dest, count);
954 System.arraycopy(mIconIds, index, mIconIds, dest, count);
955 System.arraycopy(mLabels, index, mLabels, dest, count);
956 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
957
958 uploadAppIcon(index, item);
959 }
960
961 /**
962 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
963 */
964 private void removeApp(int index) {
965 final int count = mState.iconCount - index - 1;
966 final int src = index + 1;
967
968 System.arraycopy(mIcons, src, mIcons, index, count);
969 System.arraycopy(mIconIds, src, mIconIds, index, count);
970 System.arraycopy(mLabels, src, mLabels, index, count);
971 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
972
973 final int last = mState.iconCount - 1;
974 mIcons[last] = null;
975 mIconIds[last] = 0;
976 mLabels[last] = null;
977 mLabelIds[last] = 0;
978 }
979
980 /**
981 * Send the apps list structures to RS.
982 */
983 private void saveAppsList() {
984 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -0700985
Joe Onoratoa8138d52009-10-06 19:25:30 -0700986 mAllocIconIds.data(mIconIds);
987 mAllocLabelIds.data(mLabelIds);
988
Jason Samsd8152b92009-10-13 17:19:10 -0700989 if (mScript[0] != null) { // this happens when we init it
990 for (int ct=0; ct < 4; ct++) {
991 mScript[ct].bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
992 mScript[ct].bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
993 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 }
995
996 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -0700997
998 // Note: mScript may be null if we haven't initialized it yet.
999 // In that case, this is a no-op.
Jason Sams41b61c82009-10-15 15:40:54 -07001000 if (mInvokeResetWAR != null &&
1001 mInvokeResetWAR[mViewMode] != null) {
1002 mInvokeResetWAR[mViewMode].execute();
1003 }
Jason Samsd8152b92009-10-13 17:19:10 -07001004 mRS.contextBindRootScript(mScript[mViewMode]);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001005 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001006
1007 void initTouchState() {
1008 int width = getWidth();
1009 int height = getHeight();
1010
1011 int iconsSize;
1012 if (width < height) {
1013 iconsSize = width;
1014 } else {
1015 iconsSize = height;
1016 }
1017 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
1018 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
1019
Joe Onorato56848b02009-09-25 13:59:59 -07001020 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
1021 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
1022 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001023 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -07001024 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
1025 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001026
1027 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -07001028
Joe Onorato6665c0f2009-09-02 15:27:24 -07001029 int centerX = (width / 2);
1030 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -07001031 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001032 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -07001033 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001034 mTouchXBorders[4] = centerX + (2 * cellWidth);
1035
1036 mAllocTouchXBorders.data(mTouchXBorders);
1037 }
1038
Jason Samsb52dfa02009-10-14 20:16:14 -07001039 int chooseTappedIconHorz(int x, int y, float page) {
Jason Sams86c87ed2009-09-18 13:55:55 -07001040 int currentPage = (int)page;
1041
Joe Onorato6665c0f2009-09-02 15:27:24 -07001042 int col = -1;
1043 int row = -1;
1044
1045 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1046 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1047 col = i;
1048 break;
1049 }
1050 }
1051 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1052 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1053 row = i;
1054 break;
1055 }
1056 }
1057
1058 if (row < 0 || col < 0) {
1059 return -1;
1060 }
1061
Jason Sams78aebd82009-09-15 13:06:59 -07001062 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001063 + (row * Defines.ROWS_PER_PAGE) + col;
1064 }
1065
Jason Samsb52dfa02009-10-14 20:16:14 -07001066 int chooseTappedIconVert(int x, int y, float pos) {
1067 int ydead = (getHeight() - 4 * 145) / 2;
1068 if (y < ydead || y > (getHeight() - ydead)) {
1069 return -1;
1070 }
1071
1072 y -= ydead;
1073 y += pos * 145;
1074 int row = y / 145;
1075 int col = x / 120;
1076
1077 return row * 4 + col;
1078 }
1079
Joe Onorato82ca5502009-10-15 16:59:23 -07001080 int chooseTappedIcon(int x, int y, float pos) {
1081 int index;
1082 if (mViewMode != 0) {
1083 index = chooseTappedIconHorz(x, y, pos);
1084 } else {
1085 index = chooseTappedIconVert(x, y, pos);
1086 }
1087 final int iconCount = mAllAppsList.size();
1088 if (index >= iconCount) {
1089 index = -1;
1090 }
1091 return index;
1092 }
1093
Jason Samsd8152b92009-10-13 17:19:10 -07001094 boolean setView(int v) {
1095 mViewMode = v;
1096 mRS.contextBindRootScript(mScript[mViewMode]);
1097 return (v == 0);
1098 }
1099
1100 void fling() {
1101 mInvokeFling[mViewMode].execute();
1102 }
1103
1104 void move() {
1105 mInvokeMove[mViewMode].execute();
1106 }
1107
Joe Onorato6665c0f2009-09-02 15:27:24 -07001108 /**
1109 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001110 *
1111 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001112 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001113 int selectIcon(int x, int y, float pos) {
1114 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001115 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001116 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001117 }
1118
1119 void selectIcon(int index) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001120 if (index < 0) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001121 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001122 } else {
1123 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001124
1125 Bitmap selectionBitmap = mSelectionBitmap;
1126
1127 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1128 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1129 mAllAppsList.get(index).iconBitmap);
1130
1131 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001132 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001133 mSelectedIcon.uploadToTexture(0);
1134 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001135 }
1136 }
1137
1138 /**
1139 * You need to call save() on mState on your own after calling this.
1140 */
1141 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001142 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001143 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001144
Joe Onoratod63458b2009-10-15 21:19:09 -07001145 void setHomeSelected(boolean pressed) {
1146 if (pressed) {
1147 mState.homeButtonId = mHomeButtonPressed.getID();
1148 } else {
1149 mState.homeButtonId = mHomeButtonNormal.getID();
1150 }
1151 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001152 }
Joe Onorato93839052009-08-06 20:34:32 -07001153}
1154
1155