blob: a1ce8f6073263b0652aa657b63db83c6fe9fa465 [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;
Jason Samsc8514792009-10-29 14:27:29 -070032import android.renderscript.Dimension;
Joe Onorato93839052009-08-06 20:34:32 -070033import android.renderscript.Allocation;
Jason Sams78aebd82009-09-15 13:06:59 -070034import android.renderscript.Type;
Joe Onorato93839052009-08-06 20:34:32 -070035import android.renderscript.Script;
36import android.renderscript.ScriptC;
37import android.renderscript.ProgramFragment;
38import android.renderscript.ProgramStore;
39import android.renderscript.Sampler;
Jason Sams0aa71662009-10-02 18:43:18 -070040import android.renderscript.SimpleMesh;
Joe Onorato93839052009-08-06 20:34:32 -070041
42import android.content.Context;
43import android.content.res.Resources;
Joe Onorato7c312c12009-08-13 21:36:53 -070044import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070045import android.graphics.Bitmap;
46import android.graphics.BitmapFactory;
47import android.graphics.Canvas;
48import android.graphics.Paint;
Mike Cleron7d5d7462009-10-20 14:06:00 -070049import android.graphics.Rect;
Joe Onorato93839052009-08-06 20:34:32 -070050import android.graphics.drawable.BitmapDrawable;
51import android.graphics.drawable.Drawable;
Joe Onorato93839052009-08-06 20:34:32 -070052import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070053import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070054import android.util.AttributeSet;
55import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070056import android.view.KeyEvent;
57import android.view.MotionEvent;
Joe Onoratob39e51a2009-10-28 15:47:49 -040058import android.view.SoundEffectConstants;
Joe Onorato93839052009-08-06 20:34:32 -070059import android.view.Surface;
60import android.view.SurfaceHolder;
61import android.view.SurfaceView;
Joe Onorato6665c0f2009-09-02 15:27:24 -070062import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070063import android.view.VelocityTracker;
64import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070065import android.graphics.PixelFormat;
66
67
Joe Onorato6665c0f2009-09-02 15:27:24 -070068public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070069 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040070 private static final String TAG = "Launcher.AllAppsView";
71
Joe Onoratofb0ca672009-09-14 17:55:46 -040072 /** Bit for mLocks for when there are icons being loaded. */
73 private static final int LOCK_ICONS_PENDING = 1;
74
Joe Onorato68ffd102009-10-15 17:59:43 -070075 private static final int TRACKING_NONE = 0;
76 private static final int TRACKING_FLING = 1;
77 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070078
Joe Onorato6665c0f2009-09-02 15:27:24 -070079 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070080 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040081
82 /** When this is 0, modifications are allowed, when it's not, they're not.
83 * TODO: What about scrolling? */
84 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070085
Joe Onorato82ca5502009-10-15 16:59:23 -070086 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070087 private int mMaxFlingVelocity;
88
Joe Onoratobcbeab82009-10-01 21:45:43 -070089 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070090 private RenderScript mRS;
91 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040092 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070093
Mike Cleron7d5d7462009-10-20 14:06:00 -070094 /**
95 * True when we are using arrow keys or trackball to drive navigation
96 */
97 private boolean mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -070098
Joe Onoratoc567acb2009-08-31 14:34:43 -070099 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700100 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -0700101 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700102 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700103 private int mMotionDownRawX;
104 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700105 private int mDownIconIndex = -1;
106 private int mCurrentIconIndex = -1;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700107 private int mHomeButtonTop;
Jason Sams86c87ed2009-09-18 13:55:55 -0700108 private long mTouchTime;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700109
Joe Onorato6665c0f2009-09-02 15:27:24 -0700110 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700111 public static final int ALLOC_PARAMS = 0;
112 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700113 public static final int ALLOC_ICON_IDS = 3;
114 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700115
116 public static final int COLUMNS_PER_PAGE = 4;
117 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700118
Joe Onorato6665c0f2009-09-02 15:27:24 -0700119 public static final int ICON_WIDTH_PX = 64;
120 public static final int ICON_TEXTURE_WIDTH_PX = 128;
121
122 public static final int ICON_HEIGHT_PX = 64;
123 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700124
125 public int SCREEN_WIDTH_PX;
126 public int SCREEN_HEIGHT_PX;
127
Joe Onoratobcbeab82009-10-01 21:45:43 -0700128 public void recompute(int w, int h) {
129 SCREEN_WIDTH_PX = 480;
130 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700131 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700132 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700133
134 public AllAppsView(Context context, AttributeSet attrs) {
135 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700136 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400137 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700138 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700139 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700140 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700141 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
142
Joe Onorato6665c0f2009-09-02 15:27:24 -0700143 setOnClickListener(this);
144 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700145 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700146 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700147 }
148
Joe Onoratob39e51a2009-10-28 15:47:49 -0400149 /**
150 * If you have an attached click listener, View always plays the click sound!?!?
151 * Deal with sound effects by hand.
152 */
153 public void reallyPlaySoundEffect(int sound) {
154 boolean old = isSoundEffectsEnabled();
155 setSoundEffectsEnabled(true);
156 playSoundEffect(sound);
157 setSoundEffectsEnabled(old);
158 }
159
Joe Onorato7c312c12009-08-13 21:36:53 -0700160 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
161 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700162 }
163
Joe Onorato6665c0f2009-09-02 15:27:24 -0700164 public void setLauncher(Launcher launcher) {
165 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700166 }
167
Joe Onorato1feb3a82009-08-08 22:32:00 -0700168 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700169 public void surfaceDestroyed(SurfaceHolder holder) {
170 super.surfaceDestroyed(holder);
171
172 destroyRenderScript();
173 mRS = null;
174 mRollo = null;
175 }
176
177 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700178 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
179 super.surfaceChanged(holder, format, w, h);
180
Joe Onorato6665c0f2009-09-02 15:27:24 -0700181 long startTime = SystemClock.uptimeMillis();
182
Jason Sams81134792009-10-27 15:38:42 -0700183 if (mRS != null) {
184 destroyRenderScript();
185 mRS = null;
186 mRollo = null;
187 }
188
Jason Sams05de32a2009-09-27 14:01:40 -0700189 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700190 mRollo = new RolloRS();
191 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 if (mAllAppsList != null) {
193 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700194 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700196
197 Resources res = getContext().getResources();
198 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700199 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700200
201 long endTime = SystemClock.uptimeMillis();
202 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700203 }
Jason Sams2e19c052009-10-20 18:19:55 -0700204
Joe Onorato93839052009-08-06 20:34:32 -0700205 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700206 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
207 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400208
209 if (!isVisible()) {
210 return;
211 }
212
Mike Cleron7d5d7462009-10-20 14:06:00 -0700213 if (gainFocus) {
214 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
215 // Select the first icon when we gain keyboard focus
216 mArrowNavigation = true;
217 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE);
218 mRollo.mState.save();
219 }
220 } else {
221 if (mArrowNavigation) {
222 // Clear selection when we lose focus
223 mRollo.clearSelectedIcon();
224 mRollo.mState.save();
225 mArrowNavigation = false;
226 }
227 }
228 }
229
230 @Override
231 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700232
Joe Onorato859b3a72009-10-28 15:17:01 -0400233 if (!isVisible()) {
234 return false;
235 }
236
Mike Cleron7d5d7462009-10-20 14:06:00 -0700237 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
238 if (mArrowNavigation) {
239 int whichApp = mRollo.mState.selectedIconIndex;
240 if (whichApp >= 0) {
241 ApplicationInfo app = mAllAppsList.get(whichApp);
242 mLauncher.startActivitySafely(app.intent);
243 }
244 }
245 }
Jason Sams2e19c052009-10-20 18:19:55 -0700246
Mike Cleron7d5d7462009-10-20 14:06:00 -0700247 if (mArrowNavigation && mRollo.mState.iconCount > 0) {
248 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700249
Mike Cleron7d5d7462009-10-20 14:06:00 -0700250 int currentSelection = mRollo.mState.selectedIconIndex;
251 int currentTopRow = (int) mRollo.mMessageProc.mPosX;
Jason Sams2e19c052009-10-20 18:19:55 -0700252
Mike Cleron7d5d7462009-10-20 14:06:00 -0700253 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
254 int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700255
Mike Cleron7d5d7462009-10-20 14:06:00 -0700256 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
257 int currentPageRow = (currentSelection - (currentTopRow * Defines.COLUMNS_PER_PAGE))
258 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700259
Mike Cleron7d5d7462009-10-20 14:06:00 -0700260 int newSelection = currentSelection;
261
262 switch (keyCode) {
263 case KeyEvent.KEYCODE_DPAD_UP:
264 if (currentPageRow > 0) {
265 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
266 } else if (currentTopRow > 0) {
267 mRollo.moveTo(currentTopRow - 1);
Jason Sams2e19c052009-10-20 18:19:55 -0700268 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700269 }
270 break;
271 case KeyEvent.KEYCODE_DPAD_DOWN:
272 if (currentSelection < mRollo.mState.iconCount - Defines.COLUMNS_PER_PAGE) {
273 if (currentPageRow < Defines.ROWS_PER_PAGE - 1) {
274 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
275 } else {
276 mRollo.moveTo(currentTopRow + 1);
277 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
278 }
279 }
280 break;
281 case KeyEvent.KEYCODE_DPAD_LEFT:
282 if (currentPageCol > 0) {
283 newSelection = currentSelection - 1;
284 }
285 break;
286 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700287 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Mike Cleron7d5d7462009-10-20 14:06:00 -0700288 (currentSelection < mRollo.mState.iconCount - 1)) {
289 newSelection = currentSelection + 1;
290 }
291 break;
292 }
293 if (newSelection != currentSelection) {
294 mRollo.selectIcon(newSelection);
295 mRollo.mState.save();
296 }
297 }
298 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700299 }
300
Jason Samsd8152b92009-10-13 17:19:10 -0700301 private int mRSMode = 0;
302
Joe Onorato93839052009-08-06 20:34:32 -0700303 @Override
304 public boolean onTouchEvent(MotionEvent ev)
305 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700306 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700307
Joe Onorato7bb17492009-09-24 17:51:01 -0700308 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700309 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700310 }
311
Joe Onoratofb0ca672009-09-14 17:55:46 -0400312 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700313 return true;
314 }
315
316 super.onTouchEvent(ev);
317
Joe Onoratofb0ca672009-09-14 17:55:46 -0400318 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700319 int y = (int)ev.getY();
320
Joe Onoratobcbeab82009-10-01 21:45:43 -0700321 int action = ev.getAction();
322 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400323 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700324 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
325 mTouchTracking = TRACKING_HOME;
Joe Onoratod63458b2009-10-15 21:19:09 -0700326 mRollo.setHomeSelected(true);
327 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700328 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700329 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700330 mTouchTracking = TRACKING_FLING;
331
332 mMotionDownRawX = (int)ev.getRawX();
333 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700334
Mike Cleron7d5d7462009-10-20 14:06:00 -0700335 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700336 mRollo.mState.newTouchDown = 1;
337
338 if (!mRollo.checkClickOK()) {
339 mRollo.clearSelectedIcon();
340 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700341 mDownIconIndex = mCurrentIconIndex
342 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
343 if (mDownIconIndex < 0) {
344 // if nothing was selected, no long press.
345 cancelLongPress();
346 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700347 }
348 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700349 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700350 mVelocity = VelocityTracker.obtain();
351 mVelocity.addMovement(ev);
352 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700353 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400354 break;
355 case MotionEvent.ACTION_MOVE:
356 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700357 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratod63458b2009-10-15 21:19:09 -0700358 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]);
359 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700360 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700361 int rawX = (int)ev.getRawX();
362 int rawY = (int)ev.getRawY();
363 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700364 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700365
Joe Onorato82ca5502009-10-15 16:59:23 -0700366 if (!mStartedScrolling && slop < mSlop) {
367 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700368 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700369 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
370 if (mDownIconIndex != mCurrentIconIndex) {
371 // If a different icon is selected, don't allow it to be picked up.
372 // This handles off-axis dragging.
373 cancelLongPress();
374 mCurrentIconIndex = -1;
375 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700376 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700377 if (!mStartedScrolling) {
378 cancelLongPress();
379 mCurrentIconIndex = -1;
380 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700381 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700382 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700383 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700384
Joe Onoratobcbeab82009-10-01 21:45:43 -0700385 mStartedScrolling = true;
386 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700387 mVelocity.addMovement(ev);
388 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700389 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400390 }
391 break;
392 case MotionEvent.ACTION_UP:
393 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700394 if (mTouchTracking == TRACKING_HOME) {
395 if (action == MotionEvent.ACTION_UP) {
396 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400397 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700398 mLauncher.closeAllApps(true);
399 }
Joe Onoratod63458b2009-10-15 21:19:09 -0700400 mRollo.setHomeSelected(false);
401 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700402 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700403 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700404 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700405 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700406 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700407
Jason Sams12c14a82009-10-06 14:33:15 -0700408 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700409 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700410 mRollo.clearSelectedIcon();
411 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700412 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700413
Joe Onorato539ed9d2009-10-02 10:22:14 -0700414 if (mVelocity != null) {
415 mVelocity.recycle();
416 mVelocity = null;
417 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700418 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700419 mTouchTracking = TRACKING_NONE;
420 break;
Joe Onorato93839052009-08-06 20:34:32 -0700421 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700422
423 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700424 }
425
Joe Onorato6665c0f2009-09-02 15:27:24 -0700426 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700427 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400428 return;
429 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700430 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
431 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400432 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700433 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700434 mLauncher.startActivitySafely(app.intent);
435 }
436 }
437
438 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700439 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400440 return true;
441 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700442 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
443 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
444 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700445
446 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700447 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
448 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700449
Joe Onoratobcbeab82009-10-01 21:45:43 -0700450 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
451 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700452 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700453 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700454 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700455
Joe Onorato7bb17492009-09-24 17:51:01 -0700456 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700457 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700458 return true;
459 }
460
Joe Onorato5162ea92009-09-03 09:39:42 -0700461 public void setDragController(DragController dragger) {
462 mDragController = dragger;
463 }
464
465 public void onDropCompleted(View target, boolean success) {
466 }
467
Joe Onorato4db52312009-10-06 11:17:43 -0700468 /**
469 * Zoom to the specifed amount.
470 *
471 * @param amount [0..1] 0 is hidden, 1 is open
472 * @param animate Whether to animate.
473 */
Jason Sams12c14a82009-10-06 14:33:15 -0700474 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700475 if (mRollo == null) {
476 return;
477 }
478
Joe Onoratofb0ca672009-09-14 17:55:46 -0400479 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400480 mRollo.clearSelectedIcon();
Joe Onorato1d708e72009-10-15 21:29:21 -0700481 mRollo.setHomeSelected(false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700482 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700483 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700484 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700485 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700486 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700487 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400488 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400489 }
490
491 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700492 if (mRollo == null) {
493 return false;
494 }
Jason Sams12c14a82009-10-06 14:33:15 -0700495 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400496 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700497
Mike Cleronb6082fa02009-10-19 17:03:36 -0700498 /*
Joe Onorato93839052009-08-06 20:34:32 -0700499 @Override
500 public boolean onTrackballEvent(MotionEvent ev)
501 {
502 float x = ev.getX();
503 float y = ev.getY();
504 //Float tx = new Float(x);
505 //Float ty = new Float(y);
506 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
507
508
509 return true;
510 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700511 */
Joe Onorato93839052009-08-06 20:34:32 -0700512
Joe Onorato9c1289c2009-08-17 11:03:03 -0400513 public void setApps(ArrayList<ApplicationInfo> list) {
514 mAllAppsList = list;
515 if (mRollo != null) {
516 mRollo.setApps(list);
517 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700518 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400519 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700520 }
521
Joe Onoratoa8138d52009-10-06 19:25:30 -0700522 public void addApps(ArrayList<ApplicationInfo> list) {
523 final int N = list.size();
524 if (mRollo != null) {
525 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
526 }
527
528 for (int i=0; i<N; i++) {
529 final ApplicationInfo item = list.get(i);
530 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
531 if (index < 0) {
532 index = -(index+1);
533 }
534 mAllAppsList.add(index, item);
535 if (mRollo != null) {
536 mRollo.addApp(index, item);
537 mRollo.mState.iconCount++;
538 }
539 }
540
541 if (mRollo != null) {
542 mRollo.saveAppsList();
543 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700544 }
545
Joe Onoratoa8138d52009-10-06 19:25:30 -0700546 public void removeApps(ArrayList<ApplicationInfo> list) {
547 final int N = list.size();
548 for (int i=0; i<N; i++) {
549 final ApplicationInfo item = list.get(i);
550 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
551 if (index >= 0) {
552 mAllAppsList.remove(index);
553 if (mRollo != null) {
554 mRollo.removeApp(index);
555 mRollo.mState.iconCount--;
556 }
557 } else {
558 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
559 // Try to recover. This should keep us from crashing for now.
560 }
561 }
562
563 if (mRollo != null) {
564 mRollo.saveAppsList();
565 }
566 }
567
568 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
569 // Just remove and add, because they may need to be re-sorted.
570 removeApps(list);
571 addApps(list);
572 }
573
574 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
575 public int compare(ApplicationInfo a, ApplicationInfo b) {
576 int result = a.title.toString().compareTo(b.toString());
577 if (result != 0) {
578 return result;
579 }
580 return a.intent.getComponent().compareTo(b.intent.getComponent());
581 }
582 };
583
584 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
585 public int compare(ApplicationInfo a, ApplicationInfo b) {
586 return a.intent.getComponent().compareTo(b.intent.getComponent());
587 }
588 };
589
Joe Onoratoc567acb2009-08-31 14:34:43 -0700590 private static int countPages(int iconCount) {
591 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
592 int pages = iconCount / iconsPerPage;
593 if (pages*iconsPerPage != iconCount) {
594 pages++;
595 }
596 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400597 }
598
Joe Onorato93839052009-08-06 20:34:32 -0700599 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700600
Joe Onorato1feb3a82009-08-08 22:32:00 -0700601 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700602 private int mWidth;
603 private int mHeight;
604
605 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700606 private Script mScript;
607 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700608 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700609 private Script.Invokable mInvokeFling;
610 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700611
Jason Samsc1c521e2009-10-19 14:45:45 -0700612
Jason Samscd689e12009-09-29 15:28:22 -0700613 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700614 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700615 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700616 private ProgramFragment mPFTexMip;
617 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700618 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700619 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700620 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700621 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700622
Joe Onoratod63458b2009-10-15 21:19:09 -0700623 private Allocation mHomeButtonNormal;
624 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700625
Joe Onoratobf15cb42009-08-07 14:33:40 -0700626 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700627 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700628 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700629
Joe Onoratobf15cb42009-08-07 14:33:40 -0700630 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700631 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700632 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700633 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700634
Joe Onorato6665c0f2009-09-02 15:27:24 -0700635 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700636 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700637
638 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400639 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700640
Jason Samsd8152b92009-10-13 17:19:10 -0700641
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700642 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700643 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700644
Jason Sams78aebd82009-09-15 13:06:59 -0700645 class BaseAlloc {
646 Allocation mAlloc;
647 Type mType;
648
649 void save() {
650 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700651 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700652 }
653
Jason Sams12c14a82009-10-06 14:33:15 -0700654 class AAMessage extends RenderScript.RSMessage {
655 public void run() {
656 mPosX = ((float)mData[0]) / (1 << 16);
657 mVelocity = ((float)mData[1]) / (1 << 16);
658 mZoom = ((float)mData[2]) / (1 << 16);
659 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
660 }
661 float mZoom;
662 float mPosX;
663 float mVelocity;
664 }
665 AAMessage mMessageProc;
666
Jason Sams476339d2009-09-29 18:14:38 -0700667 private boolean checkClickOK() {
668 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams2e19c052009-10-20 18:19:55 -0700669 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
670 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700671 }
672
Jason Sams78aebd82009-09-15 13:06:59 -0700673 class Params extends BaseAlloc {
674 Params() {
675 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
676 mAlloc = Allocation.createTyped(mRS, mType);
677 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700678 }
Jason Sams78aebd82009-09-15 13:06:59 -0700679 public int bubbleWidth;
680 public int bubbleHeight;
681 public int bubbleBitmapWidth;
682 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700683
Joe Onoratobcbeab82009-10-01 21:45:43 -0700684 public int homeButtonWidth;
685 public int homeButtonHeight;
686 public int homeButtonTextureWidth;
687 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700688 }
689
690 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700691 public float newPositionX;
692 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700693 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700694 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700695 public int selectedIconIndex = -1;
696 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700697 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700698 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700699 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700700
701 State() {
702 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
703 mAlloc = Allocation.createTyped(mRS, mType);
704 save();
705 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700706 }
707
708 public RolloRS() {
709 }
710
711 public void init(Resources res, int width, int height) {
712 mRes = res;
713 mWidth = width;
714 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700715 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700716 initProgramVertex();
717 initProgramFragment();
718 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700719 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700720 initGl();
721 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700722 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700723 initRs();
724 }
725
Jason Sams0aa71662009-10-02 18:43:18 -0700726 public void initMesh() {
727 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
728 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
729
Jason Samsd8152b92009-10-13 17:19:10 -0700730 float y = 0;
731 float z = 0;
732 for (int ct=0; ct < 200; ct++) {
733 float angle = 0;
734 float maxAngle = 3.14f * 0.16f;
735 float l = 1.f;
736
737 l = 1 - ((ct-5) * 0.10f);
738 if (ct > 7) {
739 angle = maxAngle * (ct - 7) * 0.2f;
740 angle = Math.min(angle, maxAngle);
741 }
Jason Samsc8514792009-10-29 14:27:29 -0700742 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700743 l = Math.min(1.0f, l);
744
745 y += 0.1f * Math.cos(angle);
746 z += 0.1f * Math.sin(angle);
747
748 float t = 0.1f * ct;
749 float ds = 0.08f;
750 tm.setColor(l, l, l, 0.99f);
751 tm.setTexture(ds, t);
752 tm.addVertex(-0.5f, y, z);
753 tm.setTexture(1 - ds, t);
754 tm.addVertex(0.5f, y, z);
755 }
756 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
757 tm.addTriangle(ct, ct+1, ct+2);
758 tm.addTriangle(ct+1, ct+3, ct+2);
759 }
760 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700761 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700762 }
763
Jason Samscd689e12009-09-29 15:28:22 -0700764 private void initProgramVertex() {
765 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
766 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700767
768 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700769 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700770 mPV = pvb.create();
771 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700772 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700773
Jason Sams37e7c2b2009-10-19 12:55:43 -0700774 //pva = new ProgramVertex.MatrixAllocation(mRS);
775 //pva.setupOrthoWindow(mWidth, mHeight);
776 //pvb.setTextureMatrixEnable(true);
777 //mPVOrtho = pvb.create();
778 //mPVOrtho.setName("PVOrtho");
779 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700780
781 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700782 }
Joe Onorato93839052009-08-06 20:34:32 -0700783
Jason Samscd689e12009-09-29 15:28:22 -0700784 private void initProgramFragment() {
785 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700786 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700787 sb.setMag(Sampler.Value.LINEAR);
788 sb.setWrapS(Sampler.Value.CLAMP);
789 sb.setWrapT(Sampler.Value.CLAMP);
790 Sampler linear = sb.create();
791
792 sb.setMin(Sampler.Value.NEAREST);
793 sb.setMag(Sampler.Value.NEAREST);
794 Sampler nearest = sb.create();
795
796 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700797 //mPFColor = bf.create();
798 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700799
800 bf.setTexEnable(true, 0);
801 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700802 mPFTexMip = bf.create();
803 mPFTexMip.setName("PFTexMip");
804 mPFTexMip.bindSampler(linear, 0);
805
806 mPFTexNearest = bf.create();
807 mPFTexNearest.setName("PFTexNearest");
808 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700809 }
810
811 private void initProgramStore() {
812 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
813 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700814 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700815 bs.setDitherEnable(true);
816 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
817 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
818 mPSIcons = bs.create();
819 mPSIcons.setName("PSIcons");
820
821 //bs.setDitherEnable(false);
822 //mPSText = bs.create();
823 //mPSText.setName("PSText");
824 }
825
826 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700827 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700828 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700829 }
Jason Sams78aebd82009-09-15 13:06:59 -0700830
Joe Onorato1feb3a82009-08-08 22:32:00 -0700831 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700832 mParams = new Params();
833 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700834
Joe Onoratobf15cb42009-08-07 14:33:40 -0700835 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700836
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700837 mParams.bubbleWidth = bubble.getBubbleWidth();
838 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
839 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
840 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700841
Joe Onoratod63458b2009-10-15 21:19:09 -0700842 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
843 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
844 mHomeButtonNormal.uploadToTexture(0);
845 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
846 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
847 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700848 mParams.homeButtonWidth = 76;
849 mParams.homeButtonHeight = 68;
850 mParams.homeButtonTextureWidth = 128;
851 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700852
Joe Onoratod63458b2009-10-15 21:19:09 -0700853 mState.homeButtonId = mHomeButtonNormal.getID();
854
Joe Onorato1feb3a82009-08-08 22:32:00 -0700855 mParams.save();
856 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400857
Joe Onorato1291a8c2009-09-15 15:07:25 -0400858 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
859 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
860 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700861
Joe Onorato9c1289c2009-08-17 11:03:03 -0400862 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700863 }
864
Jason Sams37e7c2b2009-10-19 12:55:43 -0700865 private void initScript(int id) {
866 }
867
868 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700869 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700870 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700871 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700872 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700873 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
874 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700875 mInvokeMove = sb.addInvokable("move");
876 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700877 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700878 mInvokeResetWAR = sb.addInvokable("resetHWWar");
879 mScript = sb.create();
880 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
881 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
882 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
883 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
884 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700885
Jason Sams12c14a82009-10-06 14:33:15 -0700886 mMessageProc = new AAMessage();
887 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700888 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700889 }
Joe Onorato93839052009-08-06 20:34:32 -0700890
Joe Onorato9c1289c2009-08-17 11:03:03 -0400891 private void setApps(ArrayList<ApplicationInfo> list) {
892 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700893 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700894 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700895 allocCount = 1;
896 }
897
Joe Onorato9c1289c2009-08-17 11:03:03 -0400898 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700899 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700900 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400901
902 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700903 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700904 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700906 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400907
908 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
909
910 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700911 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400912 }
913
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 mState.iconCount = count;
915
Joe Onoratoa8138d52009-10-06 19:25:30 -0700916 saveAppsList();
917 }
918
Jason Samsc8514792009-10-29 14:27:29 -0700919 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
920 int black[] = new int[w > h ? w : h];
921 Allocation.Adapter2D a = alloc.createAdapter2D();
922 int mip = 0;
923 while (w > 1 || h > 1) {
924 a.subData(0, 0, 1, h, black);
925 a.subData(w-1, 0, 1, h, black);
926 a.subData(0, 0, w, 1, black);
927 a.subData(0, h-1, w, 1, black);
928 mip++;
929 w = (w + 1) >> 1;
930 h = (h + 1) >> 1;
931 a.setConstraint(Dimension.LOD, mip);
932 }
933 a.subData(0, 0, 1, 1, black);
934 }
935
Joe Onoratoa8138d52009-10-06 19:25:30 -0700936 private void uploadAppIcon(int index, ApplicationInfo item) {
937 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -0700938 Element.RGBA_8888(mRS), true);
939 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
940
Joe Onoratoa8138d52009-10-06 19:25:30 -0700941 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -0700942 Element.RGBA_8888(mRS), true);
943 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -0700944
945 mIcons[index].uploadToTexture(0);
946 mLabels[index].uploadToTexture(0);
947
948 mIconIds[index] = mIcons[index].getID();
949 mLabelIds[index] = mLabels[index].getID();
950 }
951
952 /**
953 * Puts the empty spaces at the end. Updates mState.iconCount. You must
954 * fill in the values and call saveAppsList().
955 */
956 private void reallocAppsList(int count) {
957 Allocation[] icons = new Allocation[count];
958 int[] iconIds = new int[count];
959 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
960
961 Allocation[] labels = new Allocation[count];
962 int[] labelIds = new int[count];
963 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
964
965 final int oldCount = mIcons.length;
966
967 System.arraycopy(mIcons, 0, icons, 0, oldCount);
968 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
969 System.arraycopy(mLabels, 0, labels, 0, oldCount);
970 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
971
972 mIcons = icons;
973 mIconIds = iconIds;
974 mLabels = labels;
975 mLabelIds = labelIds;
976 }
977
978 /**
979 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
980 */
981 private void addApp(int index, ApplicationInfo item) {
982 final int count = mState.iconCount - index;
983 final int dest = index + 1;
984
985 System.arraycopy(mIcons, index, mIcons, dest, count);
986 System.arraycopy(mIconIds, index, mIconIds, dest, count);
987 System.arraycopy(mLabels, index, mLabels, dest, count);
988 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
989
990 uploadAppIcon(index, item);
991 }
992
993 /**
994 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
995 */
996 private void removeApp(int index) {
997 final int count = mState.iconCount - index - 1;
998 final int src = index + 1;
999
1000 System.arraycopy(mIcons, src, mIcons, index, count);
1001 System.arraycopy(mIconIds, src, mIconIds, index, count);
1002 System.arraycopy(mLabels, src, mLabels, index, count);
1003 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1004
1005 final int last = mState.iconCount - 1;
1006 mIcons[last] = null;
1007 mIconIds[last] = 0;
1008 mLabels[last] = null;
1009 mLabelIds[last] = 0;
1010 }
1011
1012 /**
1013 * Send the apps list structures to RS.
1014 */
1015 private void saveAppsList() {
1016 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001017
Joe Onoratoa8138d52009-10-06 19:25:30 -07001018 mAllocIconIds.data(mIconIds);
1019 mAllocLabelIds.data(mLabelIds);
1020
Jason Sams37e7c2b2009-10-19 12:55:43 -07001021 if (mScript != null) { // this happens when we init it
1022 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1023 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024 }
1025
1026 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001027
1028 // Note: mScript may be null if we haven't initialized it yet.
1029 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001030 if (mInvokeResetWAR != null) {
1031 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001032 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001033 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001034 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001035
1036 void initTouchState() {
1037 int width = getWidth();
1038 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001039 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1040 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001041
Jason Sams37e7c2b2009-10-19 12:55:43 -07001042 int centerY = (height / 2);
1043 mTouchYBorders[0] = centerY - (cellHeight * 2);
1044 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001045 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001046 mTouchYBorders[3] = centerY + cellHeight;
1047 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001048
Joe Onorato6665c0f2009-09-02 15:27:24 -07001049 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001050 mTouchXBorders[0] = 0;
1051 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001052 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001053 mTouchXBorders[3] = centerX + (width / 4);
1054 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001055 }
1056
Joe Onorato664457d2009-10-28 16:30:34 -04001057 void fling() {
1058 mInvokeFling.execute();
1059 }
1060
1061 void move() {
1062 mInvokeMove.execute();
1063 }
1064
1065 void moveTo(float row) {
1066 mState.targetPos = row;
1067 mState.save();
1068 mInvokeMoveTo.execute();
1069 }
1070
Jason Sams37e7c2b2009-10-19 12:55:43 -07001071 int chooseTappedIcon(int x, int y, float pos) {
1072 // Adjust for scroll position if not zero.
1073 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001074
Joe Onorato6665c0f2009-09-02 15:27:24 -07001075 int col = -1;
1076 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001077 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1078 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1079 col = i;
1080 break;
1081 }
1082 }
1083 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1084 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1085 row = i;
1086 break;
1087 }
1088 }
1089
1090 if (row < 0 || col < 0) {
1091 return -1;
1092 }
1093
Joe Onorato664457d2009-10-28 16:30:34 -04001094 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001095 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001096
Joe Onorato664457d2009-10-28 16:30:34 -04001097 if (index >= mState.iconCount) {
1098 return -1;
1099 } else {
1100 return index;
1101 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001102 }
1103
Joe Onorato6665c0f2009-09-02 15:27:24 -07001104 /**
1105 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001106 *
1107 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001108 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001109 int selectIcon(int x, int y, float pos) {
1110 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001111 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001112 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001113 }
1114
1115 void selectIcon(int index) {
Jason Samsc8514792009-10-29 14:27:29 -07001116 if (index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001117 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001118 } else {
1119 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001120
1121 Bitmap selectionBitmap = mSelectionBitmap;
1122
1123 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1124 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1125 mAllAppsList.get(index).iconBitmap);
1126
1127 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001128 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001129 mSelectedIcon.uploadToTexture(0);
1130 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001131 }
1132 }
1133
1134 /**
1135 * You need to call save() on mState on your own after calling this.
1136 */
1137 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001138 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001139 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001140
Joe Onoratod63458b2009-10-15 21:19:09 -07001141 void setHomeSelected(boolean pressed) {
1142 if (pressed) {
1143 mState.homeButtonId = mHomeButtonPressed.getID();
1144 } else {
1145 mState.homeButtonId = mHomeButtonNormal.getID();
1146 }
1147 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001148 }
Joe Onorato93839052009-08-06 20:34:32 -07001149}
1150
1151