blob: 6b4eab4efe0f6401bfc65da93a34f470ffb02044 [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 Onorato85a02a82009-09-08 12:34:22 -0700401 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700402 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700403 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700404 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700405 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700406 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400407 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400408 }
409
410 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700411 if (mRollo == null) {
412 return false;
413 }
Jason Sams12c14a82009-10-06 14:33:15 -0700414 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400415 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700416
Joe Onorato93839052009-08-06 20:34:32 -0700417 @Override
418 public boolean onTrackballEvent(MotionEvent ev)
419 {
420 float x = ev.getX();
421 float y = ev.getY();
422 //Float tx = new Float(x);
423 //Float ty = new Float(y);
424 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
425
426
427 return true;
428 }
429
Joe Onorato9c1289c2009-08-17 11:03:03 -0400430 public void setApps(ArrayList<ApplicationInfo> list) {
431 mAllAppsList = list;
432 if (mRollo != null) {
433 mRollo.setApps(list);
434 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700435 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400436 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700437 }
438
Joe Onoratoa8138d52009-10-06 19:25:30 -0700439 public void addApps(ArrayList<ApplicationInfo> list) {
440 final int N = list.size();
441 if (mRollo != null) {
442 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
443 }
444
445 for (int i=0; i<N; i++) {
446 final ApplicationInfo item = list.get(i);
447 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
448 if (index < 0) {
449 index = -(index+1);
450 }
451 mAllAppsList.add(index, item);
452 if (mRollo != null) {
453 mRollo.addApp(index, item);
454 mRollo.mState.iconCount++;
455 }
456 }
457
458 if (mRollo != null) {
459 mRollo.saveAppsList();
460 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700461 }
462
Joe Onoratoa8138d52009-10-06 19:25:30 -0700463 public void removeApps(ArrayList<ApplicationInfo> list) {
464 final int N = list.size();
465 for (int i=0; i<N; i++) {
466 final ApplicationInfo item = list.get(i);
467 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
468 if (index >= 0) {
469 mAllAppsList.remove(index);
470 if (mRollo != null) {
471 mRollo.removeApp(index);
472 mRollo.mState.iconCount--;
473 }
474 } else {
475 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
476 // Try to recover. This should keep us from crashing for now.
477 }
478 }
479
480 if (mRollo != null) {
481 mRollo.saveAppsList();
482 }
483 }
484
485 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
486 // Just remove and add, because they may need to be re-sorted.
487 removeApps(list);
488 addApps(list);
489 }
490
491 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
492 public int compare(ApplicationInfo a, ApplicationInfo b) {
493 int result = a.title.toString().compareTo(b.toString());
494 if (result != 0) {
495 return result;
496 }
497 return a.intent.getComponent().compareTo(b.intent.getComponent());
498 }
499 };
500
501 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
502 public int compare(ApplicationInfo a, ApplicationInfo b) {
503 return a.intent.getComponent().compareTo(b.intent.getComponent());
504 }
505 };
506
Joe Onoratoc567acb2009-08-31 14:34:43 -0700507 private static int countPages(int iconCount) {
508 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
509 int pages = iconCount / iconsPerPage;
510 if (pages*iconsPerPage != iconCount) {
511 pages++;
512 }
513 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400514 }
515
Joe Onorato93839052009-08-06 20:34:32 -0700516 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700517
Joe Onorato1feb3a82009-08-08 22:32:00 -0700518 // Allocations ======
Jason Samsd8152b92009-10-13 17:19:10 -0700519 private int mViewMode = 0;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700520
Joe Onorato93839052009-08-06 20:34:32 -0700521 private int mWidth;
522 private int mHeight;
523
524 private Resources mRes;
Jason Samsd8152b92009-10-13 17:19:10 -0700525 private Script[] mScript = new Script[4];
Jason Sams86c87ed2009-09-18 13:55:55 -0700526
Jason Samsd8152b92009-10-13 17:19:10 -0700527 private Script.Invokable[] mInvokeMove = new Script.Invokable[4];
528 private Script.Invokable[] mInvokeFling = new Script.Invokable[4];
Jason Sams41b61c82009-10-15 15:40:54 -0700529 private Script.Invokable[] mInvokeResetWAR = new Script.Invokable[4];
Jason Sams86c87ed2009-09-18 13:55:55 -0700530
Jason Samscd689e12009-09-29 15:28:22 -0700531 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700532 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700533 private ProgramFragment mPFColor;
534 private ProgramFragment mPFTexLinear;
535 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700536 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700537 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700538 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700539 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700540
Joe Onoratod63458b2009-10-15 21:19:09 -0700541 private Allocation mHomeButtonNormal;
542 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700543
Joe Onoratobf15cb42009-08-07 14:33:40 -0700544 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700545 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700546 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700547
Joe Onoratobf15cb42009-08-07 14:33:40 -0700548 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700549 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700550 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700551 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700552
Joe Onorato6665c0f2009-09-02 15:27:24 -0700553 private int[] mTouchYBorders;
554 private Allocation mAllocTouchYBorders;
555 private int[] mTouchXBorders;
556 private Allocation mAllocTouchXBorders;
557
558 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400559 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700560
Jason Samsd8152b92009-10-13 17:19:10 -0700561
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700562 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700563 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700564
Jason Sams78aebd82009-09-15 13:06:59 -0700565 class BaseAlloc {
566 Allocation mAlloc;
567 Type mType;
568
569 void save() {
570 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700571 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700572 }
573
Jason Sams12c14a82009-10-06 14:33:15 -0700574 class AAMessage extends RenderScript.RSMessage {
575 public void run() {
576 mPosX = ((float)mData[0]) / (1 << 16);
577 mVelocity = ((float)mData[1]) / (1 << 16);
578 mZoom = ((float)mData[2]) / (1 << 16);
579 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
580 }
581 float mZoom;
582 float mPosX;
583 float mVelocity;
584 }
585 AAMessage mMessageProc;
586
Jason Sams476339d2009-09-29 18:14:38 -0700587 private boolean checkClickOK() {
588 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams12c14a82009-10-06 14:33:15 -0700589 return (Math.abs(mMessageProc.mVelocity) < 0.1f) &&
590 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.1f);
Jason Sams476339d2009-09-29 18:14:38 -0700591 }
592
Jason Sams78aebd82009-09-15 13:06:59 -0700593 class Params extends BaseAlloc {
594 Params() {
595 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
596 mAlloc = Allocation.createTyped(mRS, mType);
597 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700598 }
Jason Sams78aebd82009-09-15 13:06:59 -0700599 public int bubbleWidth;
600 public int bubbleHeight;
601 public int bubbleBitmapWidth;
602 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700603
Joe Onoratobcbeab82009-10-01 21:45:43 -0700604 public int homeButtonWidth;
605 public int homeButtonHeight;
606 public int homeButtonTextureWidth;
607 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700608 }
609
610 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700611 public float newPositionX;
612 public int newTouchDown;
Jason Sams86c87ed2009-09-18 13:55:55 -0700613 public float flingVelocityX;
Jason Sams78aebd82009-09-15 13:06:59 -0700614 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700615 public int selectedIconIndex = -1;
616 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700617 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700618 public int homeButtonId;
Jason Sams78aebd82009-09-15 13:06:59 -0700619
620 State() {
621 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
622 mAlloc = Allocation.createTyped(mRS, mType);
623 save();
624 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700625 }
626
627 public RolloRS() {
628 }
629
630 public void init(Resources res, int width, int height) {
631 mRes = res;
632 mWidth = width;
633 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700634 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700635 initProgramVertex();
636 initProgramFragment();
637 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700638 initMesh();
Jason Samsd8152b92009-10-13 17:19:10 -0700639 initMesh2();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700640 initGl();
641 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700642 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700643 initRs();
644 }
645
Jason Sams0aa71662009-10-02 18:43:18 -0700646 public void initMesh() {
647 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
648 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
649
650 for (int ct=0; ct < 450; ct++) {
651 float x = 0;
652 float z = 0;
653 float l = 1.f;
654
655 if (ct < 190) {
656 z = 0.1f + 0.05f * (190 - ct);
657 x = -1;
658 l = 0.125f + (0.125f / 190.f) * ct;
659 } else if (ct >= 190 && ct < 200) {
660 float a = (3.14f * 0.5f) * (0.1f * (ct - 200));
661 float s = (float)Math.sin(a);
662 float c = (float)Math.cos(a);
663 x = -0.9f + s * 0.1f;
664 z = 0.1f - c * 0.1f;
665 l = 0.25f + 0.075f * (ct - 190);
666 } else if (ct >= 200 && ct < 250) {
667 z = 0.f;
668 x = -0.9f + (1.8f * (ct - 200) / 50.f);
669 } else if (ct >= 250 && ct < 260) {
670 float a = (3.14f * 0.5f) * (0.1f * (ct - 250));
671 float s = (float)Math.sin(a);
672 float c = (float)Math.cos(a);
673 x = 0.9f + s * 0.1f;
674 z = 0.1f - c * 0.1f;
675 l = 0.25f + 0.075f * (260 - ct);
676 } else if (ct >= 260) {
677 z = 0.1f + 0.05f * (ct - 260);
678 x = 1;
679 l = 0.125f + (0.125f / 190.f) * (450 - ct);
680 }
681 //Log.e("rs", "ct " + Integer.toString(ct) + " x = " + Float.toString(x) + ", z = " + Float.toString(z));
682 //Log.e("rs", "ct " + Integer.toString(ct) + " l = " + Float.toString(l));
683 float s = ct * 0.1f;
684 tm.setColor(l, l, l, 0.99f);
685 tm.setTexture(s, 1);
686 tm.addVertex(x, -0.5f, z);
687 tm.setTexture(s, 0);
688 tm.addVertex(x, 0.5f, z);
689 }
690 for (int ct=0; ct < (450*2 - 2); ct+= 2) {
691 tm.addTriangle(ct, ct+1, ct+2);
692 tm.addTriangle(ct+1, ct+3, ct+2);
693 }
694 mMesh = tm.create();
695 mMesh.setName("SMMesh");
696
697 }
698
699
Jason Samsd8152b92009-10-13 17:19:10 -0700700 public void initMesh2() {
701 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
702 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
703
704 float y = 0;
705 float z = 0;
706 for (int ct=0; ct < 200; ct++) {
707 float angle = 0;
708 float maxAngle = 3.14f * 0.16f;
709 float l = 1.f;
710
711 l = 1 - ((ct-5) * 0.10f);
712 if (ct > 7) {
713 angle = maxAngle * (ct - 7) * 0.2f;
714 angle = Math.min(angle, maxAngle);
715 }
716 l = Math.max(0.3f, l);
717 l = Math.min(1.0f, l);
718
719 y += 0.1f * Math.cos(angle);
720 z += 0.1f * Math.sin(angle);
721
722 float t = 0.1f * ct;
723 float ds = 0.08f;
724 tm.setColor(l, l, l, 0.99f);
725 tm.setTexture(ds, t);
726 tm.addVertex(-0.5f, y, z);
727 tm.setTexture(1 - ds, t);
728 tm.addVertex(0.5f, y, z);
729 }
730 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
731 tm.addTriangle(ct, ct+1, ct+2);
732 tm.addTriangle(ct+1, ct+3, ct+2);
733 }
734 mMesh2 = tm.create();
735 mMesh2.setName("SMMesh2");
736 }
737
Jason Samscd689e12009-09-29 15:28:22 -0700738 private void initProgramVertex() {
739 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
740 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700741
742 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700743 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700744 mPV = pvb.create();
745 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700746 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700747
Jason Samscd689e12009-09-29 15:28:22 -0700748 pva = new ProgramVertex.MatrixAllocation(mRS);
749 pva.setupOrthoWindow(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700750 pvb.setTextureMatrixEnable(true);
751 mPVOrtho = pvb.create();
752 mPVOrtho.setName("PVOrtho");
Jason Samscd689e12009-09-29 15:28:22 -0700753 mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700754
755 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700756 }
Joe Onorato93839052009-08-06 20:34:32 -0700757
Jason Samscd689e12009-09-29 15:28:22 -0700758 private void initProgramFragment() {
759 Sampler.Builder sb = new Sampler.Builder(mRS);
760 sb.setMin(Sampler.Value.LINEAR);
761 sb.setMag(Sampler.Value.LINEAR);
762 sb.setWrapS(Sampler.Value.CLAMP);
763 sb.setWrapT(Sampler.Value.CLAMP);
764 Sampler linear = sb.create();
765
766 sb.setMin(Sampler.Value.NEAREST);
767 sb.setMag(Sampler.Value.NEAREST);
768 Sampler nearest = sb.create();
769
770 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
771 mPFColor = bf.create();
772 mPFColor.setName("PFColor");
773
774 bf.setTexEnable(true, 0);
775 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
776 mPFTexLinear = bf.create();
777 mPFTexLinear.setName("PFTexLinear");
778 mPFTexLinear.bindSampler(linear, 0);
779
780 mPFTexNearest = bf.create();
781 mPFTexNearest.setName("PFTexNearest");
782 mPFTexNearest.bindSampler(nearest, 0);
783 }
784
785 private void initProgramStore() {
786 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
787 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700788 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700789 bs.setDitherEnable(true);
790 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
791 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
792 mPSIcons = bs.create();
793 mPSIcons.setName("PSIcons");
794
795 //bs.setDitherEnable(false);
796 //mPSText = bs.create();
797 //mPSText.setName("PSText");
798 }
799
800 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700801 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700802 mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700803 mTouchXBorders.length);
804 mAllocTouchXBorders.data(mTouchXBorders);
805
806 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700807 mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32(mRS),
Joe Onorato6665c0f2009-09-02 15:27:24 -0700808 mTouchYBorders.length);
809 mAllocTouchYBorders.data(mTouchYBorders);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700810 }
Jason Sams78aebd82009-09-15 13:06:59 -0700811
Joe Onorato1feb3a82009-08-08 22:32:00 -0700812 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700813 mParams = new Params();
814 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700815
Joe Onoratobf15cb42009-08-07 14:33:40 -0700816 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700817
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700818 mParams.bubbleWidth = bubble.getBubbleWidth();
819 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
820 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
821 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700822
Joe Onoratod63458b2009-10-15 21:19:09 -0700823 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
824 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
825 mHomeButtonNormal.uploadToTexture(0);
826 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
827 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
828 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700829 mParams.homeButtonWidth = 76;
830 mParams.homeButtonHeight = 68;
831 mParams.homeButtonTextureWidth = 128;
832 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700833
Joe Onoratod63458b2009-10-15 21:19:09 -0700834 mState.homeButtonId = mHomeButtonNormal.getID();
835
Joe Onorato1feb3a82009-08-08 22:32:00 -0700836 mParams.save();
837 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400838
Joe Onorato1291a8c2009-09-15 15:07:25 -0400839 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
840 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
841 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700842
Joe Onorato9c1289c2009-08-17 11:03:03 -0400843 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700844 }
845
Jason Samsd8152b92009-10-13 17:19:10 -0700846 private void initScript(int idx, int id) {
Joe Onorato93839052009-08-06 20:34:32 -0700847 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Samsd8152b92009-10-13 17:19:10 -0700848 sb.setScript(mRes, id);
Joe Onorato93839052009-08-06 20:34:32 -0700849 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700850 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700851 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
852 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Samsd8152b92009-10-13 17:19:10 -0700853 mInvokeMove[idx] = sb.addInvokable("move");
854 mInvokeFling[idx] = sb.addInvokable("fling");
Jason Sams41b61c82009-10-15 15:40:54 -0700855 mInvokeResetWAR[idx] = sb.addInvokable("resetHWWar");
Jason Samsd8152b92009-10-13 17:19:10 -0700856 mScript[idx] = sb.create();
857 mScript[idx].setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
858 mScript[idx].bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
859 mScript[idx].bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
860 mScript[idx].bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
861 mScript[idx].bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
862 mScript[idx].bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS);
863 mScript[idx].bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS);
864 }
Joe Onorato93839052009-08-06 20:34:32 -0700865
Jason Samsd8152b92009-10-13 17:19:10 -0700866 private void initRs() {
867 mViewMode = 0;
868 initScript(0, R.raw.rollo3);
869 initScript(1, R.raw.rollo2);
870 initScript(2, R.raw.rollo);
871 initScript(3, R.raw.rollo4);
Joe Onorato93839052009-08-06 20:34:32 -0700872
Jason Sams12c14a82009-10-06 14:33:15 -0700873 mMessageProc = new AAMessage();
874 mRS.mMessageCallback = mMessageProc;
Jason Samsd8152b92009-10-13 17:19:10 -0700875 mRS.contextBindRootScript(mScript[mViewMode]);
Joe Onorato93839052009-08-06 20:34:32 -0700876 }
Joe Onorato93839052009-08-06 20:34:32 -0700877
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 private void setApps(ArrayList<ApplicationInfo> list) {
879 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700880 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700881 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700882 allocCount = 1;
883 }
884
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700886 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700887 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400888
889 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700890 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700891 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700893 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894
895 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
896
897 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700898 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899 }
900
Joe Onorato9c1289c2009-08-17 11:03:03 -0400901 mState.iconCount = count;
902
Joe Onoratoa8138d52009-10-06 19:25:30 -0700903 saveAppsList();
904 }
905
906 private void uploadAppIcon(int index, ApplicationInfo item) {
907 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
908 Element.RGBA_8888(mRS), false);
909 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
910 Element.RGBA_8888(mRS), false);
911
912 mIcons[index].uploadToTexture(0);
913 mLabels[index].uploadToTexture(0);
914
915 mIconIds[index] = mIcons[index].getID();
916 mLabelIds[index] = mLabels[index].getID();
917 }
918
919 /**
920 * Puts the empty spaces at the end. Updates mState.iconCount. You must
921 * fill in the values and call saveAppsList().
922 */
923 private void reallocAppsList(int count) {
924 Allocation[] icons = new Allocation[count];
925 int[] iconIds = new int[count];
926 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
927
928 Allocation[] labels = new Allocation[count];
929 int[] labelIds = new int[count];
930 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
931
932 final int oldCount = mIcons.length;
933
934 System.arraycopy(mIcons, 0, icons, 0, oldCount);
935 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
936 System.arraycopy(mLabels, 0, labels, 0, oldCount);
937 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
938
939 mIcons = icons;
940 mIconIds = iconIds;
941 mLabels = labels;
942 mLabelIds = labelIds;
943 }
944
945 /**
946 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
947 */
948 private void addApp(int index, ApplicationInfo item) {
949 final int count = mState.iconCount - index;
950 final int dest = index + 1;
951
952 System.arraycopy(mIcons, index, mIcons, dest, count);
953 System.arraycopy(mIconIds, index, mIconIds, dest, count);
954 System.arraycopy(mLabels, index, mLabels, dest, count);
955 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
956
957 uploadAppIcon(index, item);
958 }
959
960 /**
961 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
962 */
963 private void removeApp(int index) {
964 final int count = mState.iconCount - index - 1;
965 final int src = index + 1;
966
967 System.arraycopy(mIcons, src, mIcons, index, count);
968 System.arraycopy(mIconIds, src, mIconIds, index, count);
969 System.arraycopy(mLabels, src, mLabels, index, count);
970 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
971
972 final int last = mState.iconCount - 1;
973 mIcons[last] = null;
974 mIconIds[last] = 0;
975 mLabels[last] = null;
976 mLabelIds[last] = 0;
977 }
978
979 /**
980 * Send the apps list structures to RS.
981 */
982 private void saveAppsList() {
983 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -0700984
Joe Onoratoa8138d52009-10-06 19:25:30 -0700985 mAllocIconIds.data(mIconIds);
986 mAllocLabelIds.data(mLabelIds);
987
Jason Samsd8152b92009-10-13 17:19:10 -0700988 if (mScript[0] != null) { // this happens when we init it
989 for (int ct=0; ct < 4; ct++) {
990 mScript[ct].bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
991 mScript[ct].bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
992 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400993 }
994
995 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -0700996
997 // Note: mScript may be null if we haven't initialized it yet.
998 // In that case, this is a no-op.
Jason Sams41b61c82009-10-15 15:40:54 -0700999 if (mInvokeResetWAR != null &&
1000 mInvokeResetWAR[mViewMode] != null) {
1001 mInvokeResetWAR[mViewMode].execute();
1002 }
Jason Samsd8152b92009-10-13 17:19:10 -07001003 mRS.contextBindRootScript(mScript[mViewMode]);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001004 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001005
1006 void initTouchState() {
1007 int width = getWidth();
1008 int height = getHeight();
1009
1010 int iconsSize;
1011 if (width < height) {
1012 iconsSize = width;
1013 } else {
1014 iconsSize = height;
1015 }
1016 int cellHeight = iconsSize / Defines.ROWS_PER_PAGE;
1017 int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE;
1018
Joe Onorato56848b02009-09-25 13:59:59 -07001019 int centerY = (height / 2) - (int)(cellHeight * 0.2f);
1020 mTouchYBorders[0] = centerY - (int)(2.8f * cellHeight);
1021 mTouchYBorders[1] = centerY - (int)(1.25f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001022 mTouchYBorders[2] = centerY;
Joe Onorato56848b02009-09-25 13:59:59 -07001023 mTouchYBorders[3] = centerY + (int)(1.25f * cellHeight);;
1024 mTouchYBorders[4] = centerY + (int)(2.6f * cellHeight);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001025
1026 mAllocTouchYBorders.data(mTouchYBorders);
Jason Sams78aebd82009-09-15 13:06:59 -07001027
Joe Onorato6665c0f2009-09-02 15:27:24 -07001028 int centerX = (width / 2);
1029 mTouchXBorders[0] = centerX - (2 * cellWidth);
Joe Onorato56848b02009-09-25 13:59:59 -07001030 mTouchXBorders[1] = centerX - (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001031 mTouchXBorders[2] = centerX;
Joe Onorato56848b02009-09-25 13:59:59 -07001032 mTouchXBorders[3] = centerX + (int)(0.85f * cellWidth);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001033 mTouchXBorders[4] = centerX + (2 * cellWidth);
1034
1035 mAllocTouchXBorders.data(mTouchXBorders);
1036 }
1037
Jason Samsb52dfa02009-10-14 20:16:14 -07001038 int chooseTappedIconHorz(int x, int y, float page) {
Jason Sams86c87ed2009-09-18 13:55:55 -07001039 int currentPage = (int)page;
1040
Joe Onorato6665c0f2009-09-02 15:27:24 -07001041 int col = -1;
1042 int row = -1;
1043
1044 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1045 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1046 col = i;
1047 break;
1048 }
1049 }
1050 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1051 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1052 row = i;
1053 break;
1054 }
1055 }
1056
1057 if (row < 0 || col < 0) {
1058 return -1;
1059 }
1060
Jason Sams78aebd82009-09-15 13:06:59 -07001061 return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001062 + (row * Defines.ROWS_PER_PAGE) + col;
1063 }
1064
Jason Samsb52dfa02009-10-14 20:16:14 -07001065 int chooseTappedIconVert(int x, int y, float pos) {
1066 int ydead = (getHeight() - 4 * 145) / 2;
1067 if (y < ydead || y > (getHeight() - ydead)) {
1068 return -1;
1069 }
1070
1071 y -= ydead;
1072 y += pos * 145;
1073 int row = y / 145;
1074 int col = x / 120;
1075
1076 return row * 4 + col;
1077 }
1078
Joe Onorato82ca5502009-10-15 16:59:23 -07001079 int chooseTappedIcon(int x, int y, float pos) {
1080 int index;
1081 if (mViewMode != 0) {
1082 index = chooseTappedIconHorz(x, y, pos);
1083 } else {
1084 index = chooseTappedIconVert(x, y, pos);
1085 }
1086 final int iconCount = mAllAppsList.size();
1087 if (index >= iconCount) {
1088 index = -1;
1089 }
1090 return index;
1091 }
1092
Jason Samsd8152b92009-10-13 17:19:10 -07001093 boolean setView(int v) {
1094 mViewMode = v;
1095 mRS.contextBindRootScript(mScript[mViewMode]);
1096 return (v == 0);
1097 }
1098
1099 void fling() {
1100 mInvokeFling[mViewMode].execute();
1101 }
1102
1103 void move() {
1104 mInvokeMove[mViewMode].execute();
1105 }
1106
Joe Onorato6665c0f2009-09-02 15:27:24 -07001107 /**
1108 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001109 *
1110 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001111 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001112 int selectIcon(int x, int y, float pos) {
1113 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001114 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001115 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001116 }
1117
1118 void selectIcon(int index) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001119 if (index < 0) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001120 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001121 } else {
1122 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001123
1124 Bitmap selectionBitmap = mSelectionBitmap;
1125
1126 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1127 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1128 mAllAppsList.get(index).iconBitmap);
1129
1130 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001131 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001132 mSelectedIcon.uploadToTexture(0);
1133 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001134 }
1135 }
1136
1137 /**
1138 * You need to call save() on mState on your own after calling this.
1139 */
1140 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001141 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001142 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001143
Joe Onoratod63458b2009-10-15 21:19:09 -07001144 void setHomeSelected(boolean pressed) {
1145 if (pressed) {
1146 mState.homeButtonId = mHomeButtonPressed.getID();
1147 } else {
1148 mState.homeButtonId = mHomeButtonNormal.getID();
1149 }
1150 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001151 }
Joe Onorato93839052009-08-06 20:34:32 -07001152}
1153
1154