blob: 795f4906757401409fa483a8a12db1884105b4ff [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;
Mike Cleron7d5d7462009-10-20 14:06:00 -070048import android.graphics.Rect;
Joe Onorato93839052009-08-06 20:34:32 -070049import android.graphics.drawable.BitmapDrawable;
50import android.graphics.drawable.Drawable;
Joe Onorato93839052009-08-06 20:34:32 -070051import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070052import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070053import android.util.AttributeSet;
54import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070055import android.view.KeyEvent;
56import android.view.MotionEvent;
Joe Onoratob39e51a2009-10-28 15:47:49 -040057import android.view.SoundEffectConstants;
Joe Onorato93839052009-08-06 20:34:32 -070058import android.view.Surface;
59import android.view.SurfaceHolder;
60import android.view.SurfaceView;
Joe Onorato6665c0f2009-09-02 15:27:24 -070061import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070062import android.view.VelocityTracker;
63import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070064import android.graphics.PixelFormat;
65
66
Joe Onorato6665c0f2009-09-02 15:27:24 -070067public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070068 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040069 private static final String TAG = "Launcher.AllAppsView";
70
Joe Onoratofb0ca672009-09-14 17:55:46 -040071 /** Bit for mLocks for when there are icons being loaded. */
72 private static final int LOCK_ICONS_PENDING = 1;
73
Joe Onorato68ffd102009-10-15 17:59:43 -070074 private static final int TRACKING_NONE = 0;
75 private static final int TRACKING_FLING = 1;
76 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070077
Joe Onorato6665c0f2009-09-02 15:27:24 -070078 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070079 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040080
81 /** When this is 0, modifications are allowed, when it's not, they're not.
82 * TODO: What about scrolling? */
83 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070084
Joe Onorato82ca5502009-10-15 16:59:23 -070085 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070086 private int mMaxFlingVelocity;
87
Joe Onoratobcbeab82009-10-01 21:45:43 -070088 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070089 private RenderScript mRS;
90 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040091 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070092
Mike Cleron7d5d7462009-10-20 14:06:00 -070093 /**
94 * True when we are using arrow keys or trackball to drive navigation
95 */
96 private boolean mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -070097
Joe Onoratoc567acb2009-08-31 14:34:43 -070098 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070099 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -0700100 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700101 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700102 private int mMotionDownRawX;
103 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700104 private int mDownIconIndex = -1;
105 private int mCurrentIconIndex = -1;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700106 private int mHomeButtonTop;
Jason Sams86c87ed2009-09-18 13:55:55 -0700107 private long mTouchTime;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700108
Joe Onorato6665c0f2009-09-02 15:27:24 -0700109 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700110 public static final int ALLOC_PARAMS = 0;
111 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700112 public static final int ALLOC_ICON_IDS = 3;
113 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700114
115 public static final int COLUMNS_PER_PAGE = 4;
116 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700117
Joe Onorato6665c0f2009-09-02 15:27:24 -0700118 public static final int ICON_WIDTH_PX = 64;
119 public static final int ICON_TEXTURE_WIDTH_PX = 128;
120
121 public static final int ICON_HEIGHT_PX = 64;
122 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700123
124 public int SCREEN_WIDTH_PX;
125 public int SCREEN_HEIGHT_PX;
126
Joe Onoratobcbeab82009-10-01 21:45:43 -0700127 public void recompute(int w, int h) {
128 SCREEN_WIDTH_PX = 480;
129 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700130 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700131 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700132
133 public AllAppsView(Context context, AttributeSet attrs) {
134 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700135 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400136 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700137 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700138 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700139 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700140 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
141
Joe Onorato6665c0f2009-09-02 15:27:24 -0700142 setOnClickListener(this);
143 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700144 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700145 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700146 }
147
Joe Onoratob39e51a2009-10-28 15:47:49 -0400148 /**
149 * If you have an attached click listener, View always plays the click sound!?!?
150 * Deal with sound effects by hand.
151 */
152 public void reallyPlaySoundEffect(int sound) {
153 boolean old = isSoundEffectsEnabled();
154 setSoundEffectsEnabled(true);
155 playSoundEffect(sound);
156 setSoundEffectsEnabled(old);
157 }
158
Joe Onorato7c312c12009-08-13 21:36:53 -0700159 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
160 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700161 }
162
Joe Onorato6665c0f2009-09-02 15:27:24 -0700163 public void setLauncher(Launcher launcher) {
164 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700165 }
166
Joe Onorato1feb3a82009-08-08 22:32:00 -0700167 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700168 public void surfaceDestroyed(SurfaceHolder holder) {
169 super.surfaceDestroyed(holder);
170
171 destroyRenderScript();
172 mRS = null;
173 mRollo = null;
174 }
175
176 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700177 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
178 super.surfaceChanged(holder, format, w, h);
179
Joe Onorato6665c0f2009-09-02 15:27:24 -0700180 long startTime = SystemClock.uptimeMillis();
181
Jason Sams81134792009-10-27 15:38:42 -0700182 if (mRS != null) {
183 destroyRenderScript();
184 mRS = null;
185 mRollo = null;
186 }
187
Jason Sams05de32a2009-09-27 14:01:40 -0700188 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700189 mRollo = new RolloRS();
190 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 if (mAllAppsList != null) {
192 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700193 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700195
196 Resources res = getContext().getResources();
197 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700198 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700199
200 long endTime = SystemClock.uptimeMillis();
201 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700202 }
Jason Sams2e19c052009-10-20 18:19:55 -0700203
Joe Onorato93839052009-08-06 20:34:32 -0700204 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700205 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
206 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400207
208 if (!isVisible()) {
209 return;
210 }
211
Mike Cleron7d5d7462009-10-20 14:06:00 -0700212 if (gainFocus) {
213 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
214 // Select the first icon when we gain keyboard focus
215 mArrowNavigation = true;
216 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE);
217 mRollo.mState.save();
218 }
219 } else {
220 if (mArrowNavigation) {
221 // Clear selection when we lose focus
222 mRollo.clearSelectedIcon();
223 mRollo.mState.save();
224 mArrowNavigation = false;
225 }
226 }
227 }
228
229 @Override
230 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700231
Joe Onorato859b3a72009-10-28 15:17:01 -0400232 if (!isVisible()) {
233 return false;
234 }
235
Mike Cleron7d5d7462009-10-20 14:06:00 -0700236 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
237 if (mArrowNavigation) {
238 int whichApp = mRollo.mState.selectedIconIndex;
239 if (whichApp >= 0) {
240 ApplicationInfo app = mAllAppsList.get(whichApp);
241 mLauncher.startActivitySafely(app.intent);
242 }
243 }
244 }
Jason Sams2e19c052009-10-20 18:19:55 -0700245
Mike Cleron7d5d7462009-10-20 14:06:00 -0700246 if (mArrowNavigation && mRollo.mState.iconCount > 0) {
247 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700248
Mike Cleron7d5d7462009-10-20 14:06:00 -0700249 int currentSelection = mRollo.mState.selectedIconIndex;
250 int currentTopRow = (int) mRollo.mMessageProc.mPosX;
Jason Sams2e19c052009-10-20 18:19:55 -0700251
Mike Cleron7d5d7462009-10-20 14:06:00 -0700252 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
253 int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700254
Mike Cleron7d5d7462009-10-20 14:06:00 -0700255 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
256 int currentPageRow = (currentSelection - (currentTopRow * Defines.COLUMNS_PER_PAGE))
257 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700258
Mike Cleron7d5d7462009-10-20 14:06:00 -0700259 int newSelection = currentSelection;
260
261 switch (keyCode) {
262 case KeyEvent.KEYCODE_DPAD_UP:
263 if (currentPageRow > 0) {
264 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
265 } else if (currentTopRow > 0) {
266 mRollo.moveTo(currentTopRow - 1);
Jason Sams2e19c052009-10-20 18:19:55 -0700267 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700268 }
269 break;
270 case KeyEvent.KEYCODE_DPAD_DOWN:
271 if (currentSelection < mRollo.mState.iconCount - Defines.COLUMNS_PER_PAGE) {
272 if (currentPageRow < Defines.ROWS_PER_PAGE - 1) {
273 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
274 } else {
275 mRollo.moveTo(currentTopRow + 1);
276 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
277 }
278 }
279 break;
280 case KeyEvent.KEYCODE_DPAD_LEFT:
281 if (currentPageCol > 0) {
282 newSelection = currentSelection - 1;
283 }
284 break;
285 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700286 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Mike Cleron7d5d7462009-10-20 14:06:00 -0700287 (currentSelection < mRollo.mState.iconCount - 1)) {
288 newSelection = currentSelection + 1;
289 }
290 break;
291 }
292 if (newSelection != currentSelection) {
293 mRollo.selectIcon(newSelection);
294 mRollo.mState.save();
295 }
296 }
297 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700298 }
299
Jason Samsd8152b92009-10-13 17:19:10 -0700300 private int mRSMode = 0;
301
Joe Onorato93839052009-08-06 20:34:32 -0700302 @Override
303 public boolean onTouchEvent(MotionEvent ev)
304 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700305 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700306
Joe Onorato7bb17492009-09-24 17:51:01 -0700307 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700308 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700309 }
310
Joe Onoratofb0ca672009-09-14 17:55:46 -0400311 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700312 return true;
313 }
314
315 super.onTouchEvent(ev);
316
Joe Onoratofb0ca672009-09-14 17:55:46 -0400317 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700318 int y = (int)ev.getY();
319
Joe Onoratobcbeab82009-10-01 21:45:43 -0700320 int action = ev.getAction();
321 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400322 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700323 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
324 mTouchTracking = TRACKING_HOME;
Joe Onoratod63458b2009-10-15 21:19:09 -0700325 mRollo.setHomeSelected(true);
326 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700327 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700328 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700329 mTouchTracking = TRACKING_FLING;
330
331 mMotionDownRawX = (int)ev.getRawX();
332 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700333
Mike Cleron7d5d7462009-10-20 14:06:00 -0700334 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700335 mRollo.mState.newTouchDown = 1;
336
337 if (!mRollo.checkClickOK()) {
338 mRollo.clearSelectedIcon();
339 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700340 mDownIconIndex = mCurrentIconIndex
341 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
342 if (mDownIconIndex < 0) {
343 // if nothing was selected, no long press.
344 cancelLongPress();
345 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700346 }
347 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700348 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700349 mVelocity = VelocityTracker.obtain();
350 mVelocity.addMovement(ev);
351 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700352 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400353 break;
354 case MotionEvent.ACTION_MOVE:
355 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700356 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratod63458b2009-10-15 21:19:09 -0700357 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]);
358 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700359 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700360 int rawX = (int)ev.getRawX();
361 int rawY = (int)ev.getRawY();
362 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700363 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700364
Joe Onorato82ca5502009-10-15 16:59:23 -0700365 if (!mStartedScrolling && slop < mSlop) {
366 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700367 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700368 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
369 if (mDownIconIndex != mCurrentIconIndex) {
370 // If a different icon is selected, don't allow it to be picked up.
371 // This handles off-axis dragging.
372 cancelLongPress();
373 mCurrentIconIndex = -1;
374 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700375 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700376 if (!mStartedScrolling) {
377 cancelLongPress();
378 mCurrentIconIndex = -1;
379 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700380 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700381 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700382 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700383
Joe Onoratobcbeab82009-10-01 21:45:43 -0700384 mStartedScrolling = true;
385 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700386 mVelocity.addMovement(ev);
387 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700388 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400389 }
390 break;
391 case MotionEvent.ACTION_UP:
392 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700393 if (mTouchTracking == TRACKING_HOME) {
394 if (action == MotionEvent.ACTION_UP) {
395 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400396 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700397 mLauncher.closeAllApps(true);
398 }
Joe Onoratod63458b2009-10-15 21:19:09 -0700399 mRollo.setHomeSelected(false);
400 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700401 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700402 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700403 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700404 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700405 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700406
Jason Sams12c14a82009-10-06 14:33:15 -0700407 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700408 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700409 mRollo.clearSelectedIcon();
410 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700411 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700412
Joe Onorato539ed9d2009-10-02 10:22:14 -0700413 if (mVelocity != null) {
414 mVelocity.recycle();
415 mVelocity = null;
416 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700417 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700418 mTouchTracking = TRACKING_NONE;
419 break;
Joe Onorato93839052009-08-06 20:34:32 -0700420 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700421
422 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700423 }
424
Joe Onorato6665c0f2009-09-02 15:27:24 -0700425 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700426 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400427 return;
428 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700429 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
430 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400431 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700432 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700433 mLauncher.startActivitySafely(app.intent);
434 }
435 }
436
437 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700438 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400439 return true;
440 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700441 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
442 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
443 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700444
445 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700446 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
447 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700448
Joe Onoratobcbeab82009-10-01 21:45:43 -0700449 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
450 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700451 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700452 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700453 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700454
Joe Onorato7bb17492009-09-24 17:51:01 -0700455 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700456 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700457 return true;
458 }
459
Joe Onorato5162ea92009-09-03 09:39:42 -0700460 public void setDragController(DragController dragger) {
461 mDragController = dragger;
462 }
463
464 public void onDropCompleted(View target, boolean success) {
465 }
466
Joe Onorato4db52312009-10-06 11:17:43 -0700467 /**
468 * Zoom to the specifed amount.
469 *
470 * @param amount [0..1] 0 is hidden, 1 is open
471 * @param animate Whether to animate.
472 */
Jason Sams12c14a82009-10-06 14:33:15 -0700473 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700474 if (mRollo == null) {
475 return;
476 }
477
Joe Onoratofb0ca672009-09-14 17:55:46 -0400478 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400479 mRollo.clearSelectedIcon();
Joe Onorato1d708e72009-10-15 21:29:21 -0700480 mRollo.setHomeSelected(false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700481 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700482 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700483 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700484 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700485 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700486 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400487 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400488 }
489
490 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700491 if (mRollo == null) {
492 return false;
493 }
Jason Sams12c14a82009-10-06 14:33:15 -0700494 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400495 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700496
Mike Cleronb6082fa02009-10-19 17:03:36 -0700497 /*
Joe Onorato93839052009-08-06 20:34:32 -0700498 @Override
499 public boolean onTrackballEvent(MotionEvent ev)
500 {
501 float x = ev.getX();
502 float y = ev.getY();
503 //Float tx = new Float(x);
504 //Float ty = new Float(y);
505 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
506
507
508 return true;
509 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700510 */
Joe Onorato93839052009-08-06 20:34:32 -0700511
Joe Onorato9c1289c2009-08-17 11:03:03 -0400512 public void setApps(ArrayList<ApplicationInfo> list) {
513 mAllAppsList = list;
514 if (mRollo != null) {
515 mRollo.setApps(list);
516 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700517 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400518 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700519 }
520
Joe Onoratoa8138d52009-10-06 19:25:30 -0700521 public void addApps(ArrayList<ApplicationInfo> list) {
522 final int N = list.size();
523 if (mRollo != null) {
524 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
525 }
526
527 for (int i=0; i<N; i++) {
528 final ApplicationInfo item = list.get(i);
529 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
530 if (index < 0) {
531 index = -(index+1);
532 }
533 mAllAppsList.add(index, item);
534 if (mRollo != null) {
535 mRollo.addApp(index, item);
536 mRollo.mState.iconCount++;
537 }
538 }
539
540 if (mRollo != null) {
541 mRollo.saveAppsList();
542 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700543 }
544
Joe Onoratoa8138d52009-10-06 19:25:30 -0700545 public void removeApps(ArrayList<ApplicationInfo> list) {
546 final int N = list.size();
547 for (int i=0; i<N; i++) {
548 final ApplicationInfo item = list.get(i);
549 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
550 if (index >= 0) {
551 mAllAppsList.remove(index);
552 if (mRollo != null) {
553 mRollo.removeApp(index);
554 mRollo.mState.iconCount--;
555 }
556 } else {
557 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
558 // Try to recover. This should keep us from crashing for now.
559 }
560 }
561
562 if (mRollo != null) {
563 mRollo.saveAppsList();
564 }
565 }
566
567 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
568 // Just remove and add, because they may need to be re-sorted.
569 removeApps(list);
570 addApps(list);
571 }
572
573 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
574 public int compare(ApplicationInfo a, ApplicationInfo b) {
575 int result = a.title.toString().compareTo(b.toString());
576 if (result != 0) {
577 return result;
578 }
579 return a.intent.getComponent().compareTo(b.intent.getComponent());
580 }
581 };
582
583 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
584 public int compare(ApplicationInfo a, ApplicationInfo b) {
585 return a.intent.getComponent().compareTo(b.intent.getComponent());
586 }
587 };
588
Joe Onoratoc567acb2009-08-31 14:34:43 -0700589 private static int countPages(int iconCount) {
590 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
591 int pages = iconCount / iconsPerPage;
592 if (pages*iconsPerPage != iconCount) {
593 pages++;
594 }
595 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400596 }
597
Joe Onorato93839052009-08-06 20:34:32 -0700598 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700599
Joe Onorato1feb3a82009-08-08 22:32:00 -0700600 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700601 private int mWidth;
602 private int mHeight;
603
604 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700605 private Script mScript;
606 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700607 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700608 private Script.Invokable mInvokeFling;
609 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700610
Jason Samsc1c521e2009-10-19 14:45:45 -0700611
Jason Samscd689e12009-09-29 15:28:22 -0700612 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700613 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700614 private ProgramFragment mPFColor;
615 private ProgramFragment mPFTexLinear;
Joe Onorato93839052009-08-06 20:34:32 -0700616 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700617 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700618 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700619 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700620
Joe Onoratod63458b2009-10-15 21:19:09 -0700621 private Allocation mHomeButtonNormal;
622 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700623
Joe Onoratobf15cb42009-08-07 14:33:40 -0700624 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700625 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700626 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700627
Joe Onoratobf15cb42009-08-07 14:33:40 -0700628 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700629 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700630 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700631 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700632
Joe Onorato6665c0f2009-09-02 15:27:24 -0700633 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700634 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700635
636 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400637 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700638
Jason Samsd8152b92009-10-13 17:19:10 -0700639
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700640 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700641 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700642
Jason Sams78aebd82009-09-15 13:06:59 -0700643 class BaseAlloc {
644 Allocation mAlloc;
645 Type mType;
646
647 void save() {
648 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700649 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700650 }
651
Jason Sams12c14a82009-10-06 14:33:15 -0700652 class AAMessage extends RenderScript.RSMessage {
653 public void run() {
654 mPosX = ((float)mData[0]) / (1 << 16);
655 mVelocity = ((float)mData[1]) / (1 << 16);
656 mZoom = ((float)mData[2]) / (1 << 16);
657 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
658 }
659 float mZoom;
660 float mPosX;
661 float mVelocity;
662 }
663 AAMessage mMessageProc;
664
Jason Sams476339d2009-09-29 18:14:38 -0700665 private boolean checkClickOK() {
666 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams2e19c052009-10-20 18:19:55 -0700667 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
668 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700669 }
670
Jason Sams78aebd82009-09-15 13:06:59 -0700671 class Params extends BaseAlloc {
672 Params() {
673 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
674 mAlloc = Allocation.createTyped(mRS, mType);
675 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700676 }
Jason Sams78aebd82009-09-15 13:06:59 -0700677 public int bubbleWidth;
678 public int bubbleHeight;
679 public int bubbleBitmapWidth;
680 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700681
Joe Onoratobcbeab82009-10-01 21:45:43 -0700682 public int homeButtonWidth;
683 public int homeButtonHeight;
684 public int homeButtonTextureWidth;
685 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700686 }
687
688 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700689 public float newPositionX;
690 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700691 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700692 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700693 public int selectedIconIndex = -1;
694 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700695 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700696 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700697 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700698
699 State() {
700 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
701 mAlloc = Allocation.createTyped(mRS, mType);
702 save();
703 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700704 }
705
706 public RolloRS() {
707 }
708
709 public void init(Resources res, int width, int height) {
710 mRes = res;
711 mWidth = width;
712 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700713 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700714 initProgramVertex();
715 initProgramFragment();
716 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700717 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700718 initGl();
719 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700720 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700721 initRs();
722 }
723
Jason Sams0aa71662009-10-02 18:43:18 -0700724 public void initMesh() {
725 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
726 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
727
Jason Samsd8152b92009-10-13 17:19:10 -0700728 float y = 0;
729 float z = 0;
730 for (int ct=0; ct < 200; ct++) {
731 float angle = 0;
732 float maxAngle = 3.14f * 0.16f;
733 float l = 1.f;
734
735 l = 1 - ((ct-5) * 0.10f);
736 if (ct > 7) {
737 angle = maxAngle * (ct - 7) * 0.2f;
738 angle = Math.min(angle, maxAngle);
739 }
740 l = Math.max(0.3f, l);
741 l = Math.min(1.0f, l);
742
743 y += 0.1f * Math.cos(angle);
744 z += 0.1f * Math.sin(angle);
745
746 float t = 0.1f * ct;
747 float ds = 0.08f;
748 tm.setColor(l, l, l, 0.99f);
749 tm.setTexture(ds, t);
750 tm.addVertex(-0.5f, y, z);
751 tm.setTexture(1 - ds, t);
752 tm.addVertex(0.5f, y, z);
753 }
754 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
755 tm.addTriangle(ct, ct+1, ct+2);
756 tm.addTriangle(ct+1, ct+3, ct+2);
757 }
758 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700759 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700760 }
761
Jason Samscd689e12009-09-29 15:28:22 -0700762 private void initProgramVertex() {
763 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
764 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700765
766 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700767 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700768 mPV = pvb.create();
769 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700770 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700771
Jason Sams37e7c2b2009-10-19 12:55:43 -0700772 //pva = new ProgramVertex.MatrixAllocation(mRS);
773 //pva.setupOrthoWindow(mWidth, mHeight);
774 //pvb.setTextureMatrixEnable(true);
775 //mPVOrtho = pvb.create();
776 //mPVOrtho.setName("PVOrtho");
777 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700778
779 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700780 }
Joe Onorato93839052009-08-06 20:34:32 -0700781
Jason Samscd689e12009-09-29 15:28:22 -0700782 private void initProgramFragment() {
783 Sampler.Builder sb = new Sampler.Builder(mRS);
784 sb.setMin(Sampler.Value.LINEAR);
785 sb.setMag(Sampler.Value.LINEAR);
786 sb.setWrapS(Sampler.Value.CLAMP);
787 sb.setWrapT(Sampler.Value.CLAMP);
788 Sampler linear = sb.create();
789
790 sb.setMin(Sampler.Value.NEAREST);
791 sb.setMag(Sampler.Value.NEAREST);
792 Sampler nearest = sb.create();
793
794 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700795 //mPFColor = bf.create();
796 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700797
798 bf.setTexEnable(true, 0);
799 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
800 mPFTexLinear = bf.create();
801 mPFTexLinear.setName("PFTexLinear");
802 mPFTexLinear.bindSampler(linear, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700803 }
804
805 private void initProgramStore() {
806 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
807 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700808 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700809 bs.setDitherEnable(true);
810 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
811 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
812 mPSIcons = bs.create();
813 mPSIcons.setName("PSIcons");
814
815 //bs.setDitherEnable(false);
816 //mPSText = bs.create();
817 //mPSText.setName("PSText");
818 }
819
820 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700821 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700822 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700823 }
Jason Sams78aebd82009-09-15 13:06:59 -0700824
Joe Onorato1feb3a82009-08-08 22:32:00 -0700825 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700826 mParams = new Params();
827 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700828
Joe Onoratobf15cb42009-08-07 14:33:40 -0700829 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700830
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700831 mParams.bubbleWidth = bubble.getBubbleWidth();
832 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
833 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
834 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700835
Joe Onoratod63458b2009-10-15 21:19:09 -0700836 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
837 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
838 mHomeButtonNormal.uploadToTexture(0);
839 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
840 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
841 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700842 mParams.homeButtonWidth = 76;
843 mParams.homeButtonHeight = 68;
844 mParams.homeButtonTextureWidth = 128;
845 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700846
Joe Onoratod63458b2009-10-15 21:19:09 -0700847 mState.homeButtonId = mHomeButtonNormal.getID();
848
Joe Onorato1feb3a82009-08-08 22:32:00 -0700849 mParams.save();
850 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400851
Joe Onorato1291a8c2009-09-15 15:07:25 -0400852 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
853 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
854 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700855
Joe Onorato9c1289c2009-08-17 11:03:03 -0400856 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700857 }
858
Jason Sams37e7c2b2009-10-19 12:55:43 -0700859 private void initScript(int id) {
860 }
861
862 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700863 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700864 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700865 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700866 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700867 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
868 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700869 mInvokeMove = sb.addInvokable("move");
870 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700871 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700872 mInvokeResetWAR = sb.addInvokable("resetHWWar");
873 mScript = sb.create();
874 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
875 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
876 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
877 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
878 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700879
Jason Sams12c14a82009-10-06 14:33:15 -0700880 mMessageProc = new AAMessage();
881 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700882 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700883 }
Joe Onorato93839052009-08-06 20:34:32 -0700884
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885 private void setApps(ArrayList<ApplicationInfo> list) {
886 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700887 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700888 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700889 allocCount = 1;
890 }
891
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700893 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700894 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400895
896 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700897 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700898 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700900 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400901
902 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
903
904 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700905 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400906 }
907
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 mState.iconCount = count;
909
Joe Onoratoa8138d52009-10-06 19:25:30 -0700910 saveAppsList();
911 }
912
913 private void uploadAppIcon(int index, ApplicationInfo item) {
914 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
915 Element.RGBA_8888(mRS), false);
916 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
917 Element.RGBA_8888(mRS), false);
918
919 mIcons[index].uploadToTexture(0);
920 mLabels[index].uploadToTexture(0);
921
922 mIconIds[index] = mIcons[index].getID();
923 mLabelIds[index] = mLabels[index].getID();
924 }
925
926 /**
927 * Puts the empty spaces at the end. Updates mState.iconCount. You must
928 * fill in the values and call saveAppsList().
929 */
930 private void reallocAppsList(int count) {
931 Allocation[] icons = new Allocation[count];
932 int[] iconIds = new int[count];
933 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
934
935 Allocation[] labels = new Allocation[count];
936 int[] labelIds = new int[count];
937 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
938
939 final int oldCount = mIcons.length;
940
941 System.arraycopy(mIcons, 0, icons, 0, oldCount);
942 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
943 System.arraycopy(mLabels, 0, labels, 0, oldCount);
944 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
945
946 mIcons = icons;
947 mIconIds = iconIds;
948 mLabels = labels;
949 mLabelIds = labelIds;
950 }
951
952 /**
953 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
954 */
955 private void addApp(int index, ApplicationInfo item) {
956 final int count = mState.iconCount - index;
957 final int dest = index + 1;
958
959 System.arraycopy(mIcons, index, mIcons, dest, count);
960 System.arraycopy(mIconIds, index, mIconIds, dest, count);
961 System.arraycopy(mLabels, index, mLabels, dest, count);
962 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
963
964 uploadAppIcon(index, item);
965 }
966
967 /**
968 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
969 */
970 private void removeApp(int index) {
971 final int count = mState.iconCount - index - 1;
972 final int src = index + 1;
973
974 System.arraycopy(mIcons, src, mIcons, index, count);
975 System.arraycopy(mIconIds, src, mIconIds, index, count);
976 System.arraycopy(mLabels, src, mLabels, index, count);
977 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
978
979 final int last = mState.iconCount - 1;
980 mIcons[last] = null;
981 mIconIds[last] = 0;
982 mLabels[last] = null;
983 mLabelIds[last] = 0;
984 }
985
986 /**
987 * Send the apps list structures to RS.
988 */
989 private void saveAppsList() {
990 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -0700991
Joe Onoratoa8138d52009-10-06 19:25:30 -0700992 mAllocIconIds.data(mIconIds);
993 mAllocLabelIds.data(mLabelIds);
994
Jason Sams37e7c2b2009-10-19 12:55:43 -0700995 if (mScript != null) { // this happens when we init it
996 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
997 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998 }
999
1000 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001001
1002 // Note: mScript may be null if we haven't initialized it yet.
1003 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001004 if (mInvokeResetWAR != null) {
1005 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001006 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001007 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001009
1010 void initTouchState() {
1011 int width = getWidth();
1012 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001013 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1014 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001015
Jason Sams37e7c2b2009-10-19 12:55:43 -07001016 int centerY = (height / 2);
1017 mTouchYBorders[0] = centerY - (cellHeight * 2);
1018 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001019 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001020 mTouchYBorders[3] = centerY + cellHeight;
1021 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001022
Joe Onorato6665c0f2009-09-02 15:27:24 -07001023 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001024 mTouchXBorders[0] = 0;
1025 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001026 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001027 mTouchXBorders[3] = centerX + (width / 4);
1028 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001029 }
1030
Joe Onorato664457d2009-10-28 16:30:34 -04001031 void fling() {
1032 mInvokeFling.execute();
1033 }
1034
1035 void move() {
1036 mInvokeMove.execute();
1037 }
1038
1039 void moveTo(float row) {
1040 mState.targetPos = row;
1041 mState.save();
1042 mInvokeMoveTo.execute();
1043 }
1044
Jason Sams37e7c2b2009-10-19 12:55:43 -07001045 int chooseTappedIcon(int x, int y, float pos) {
1046 // Adjust for scroll position if not zero.
1047 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001048
Joe Onorato6665c0f2009-09-02 15:27:24 -07001049 int col = -1;
1050 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001051 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1052 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1053 col = i;
1054 break;
1055 }
1056 }
1057 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1058 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1059 row = i;
1060 break;
1061 }
1062 }
1063
1064 if (row < 0 || col < 0) {
1065 return -1;
1066 }
1067
Joe Onorato664457d2009-10-28 16:30:34 -04001068 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001069 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001070
Joe Onorato664457d2009-10-28 16:30:34 -04001071 if (index >= mState.iconCount) {
1072 return -1;
1073 } else {
1074 return index;
1075 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001076 }
1077
Joe Onorato6665c0f2009-09-02 15:27:24 -07001078 /**
1079 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001080 *
1081 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001082 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001083 int selectIcon(int x, int y, float pos) {
1084 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001085 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001086 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001087 }
1088
1089 void selectIcon(int index) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001090 if (index < 0) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001091 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001092 } else {
1093 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001094
1095 Bitmap selectionBitmap = mSelectionBitmap;
1096
1097 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1098 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1099 mAllAppsList.get(index).iconBitmap);
1100
1101 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001102 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001103 mSelectedIcon.uploadToTexture(0);
1104 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001105 }
1106 }
1107
1108 /**
1109 * You need to call save() on mState on your own after calling this.
1110 */
1111 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001112 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001113 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001114
Joe Onoratod63458b2009-10-15 21:19:09 -07001115 void setHomeSelected(boolean pressed) {
1116 if (pressed) {
1117 mState.homeButtonId = mHomeButtonPressed.getID();
1118 } else {
1119 mState.homeButtonId = mHomeButtonNormal.getID();
1120 }
1121 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001122 }
Joe Onorato93839052009-08-06 20:34:32 -07001123}
1124
1125