blob: 854a7c6ede0a6dfa98bb3b78b675b70b62cd904d [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
Joe Onoratocb9f7982009-10-31 16:32:02 -040042import android.content.ComponentName;
Joe Onorato93839052009-08-06 20:34:32 -070043import android.content.Context;
44import android.content.res.Resources;
Joe Onorato7c312c12009-08-13 21:36:53 -070045import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070046import android.graphics.Bitmap;
47import android.graphics.BitmapFactory;
48import android.graphics.Canvas;
49import android.graphics.Paint;
Mike Cleron7d5d7462009-10-20 14:06:00 -070050import android.graphics.Rect;
Joe Onorato93839052009-08-06 20:34:32 -070051import android.graphics.drawable.BitmapDrawable;
52import android.graphics.drawable.Drawable;
Joe Onorato93839052009-08-06 20:34:32 -070053import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070054import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070055import android.util.AttributeSet;
56import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070057import android.view.KeyEvent;
58import android.view.MotionEvent;
Joe Onoratob39e51a2009-10-28 15:47:49 -040059import android.view.SoundEffectConstants;
Joe Onorato93839052009-08-06 20:34:32 -070060import android.view.Surface;
61import android.view.SurfaceHolder;
62import android.view.SurfaceView;
Joe Onorato6665c0f2009-09-02 15:27:24 -070063import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070064import android.view.VelocityTracker;
65import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070066import android.graphics.PixelFormat;
67
68
Joe Onorato6665c0f2009-09-02 15:27:24 -070069public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070070 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040071 private static final String TAG = "Launcher.AllAppsView";
72
Joe Onoratofb0ca672009-09-14 17:55:46 -040073 /** Bit for mLocks for when there are icons being loaded. */
74 private static final int LOCK_ICONS_PENDING = 1;
75
Joe Onorato68ffd102009-10-15 17:59:43 -070076 private static final int TRACKING_NONE = 0;
77 private static final int TRACKING_FLING = 1;
78 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070079
Joe Onorato6665c0f2009-09-02 15:27:24 -070080 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070081 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040082
83 /** When this is 0, modifications are allowed, when it's not, they're not.
84 * TODO: What about scrolling? */
85 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070086
Joe Onorato82ca5502009-10-15 16:59:23 -070087 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070088 private int mMaxFlingVelocity;
89
Joe Onoratobcbeab82009-10-01 21:45:43 -070090 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070091 private RenderScript mRS;
92 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070094
Mike Cleron7d5d7462009-10-20 14:06:00 -070095 /**
96 * True when we are using arrow keys or trackball to drive navigation
97 */
98 private boolean mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -070099
Joe Onoratoc567acb2009-08-31 14:34:43 -0700100 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700101 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -0700102 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700103 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700104 private int mMotionDownRawX;
105 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700106 private int mDownIconIndex = -1;
107 private int mCurrentIconIndex = -1;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700108 private int mHomeButtonTop;
Jason Sams86c87ed2009-09-18 13:55:55 -0700109 private long mTouchTime;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700110
Joe Onorato6665c0f2009-09-02 15:27:24 -0700111 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700112 public static final int ALLOC_PARAMS = 0;
113 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700114 public static final int ALLOC_ICON_IDS = 3;
115 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700116
117 public static final int COLUMNS_PER_PAGE = 4;
118 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700119
Joe Onorato6665c0f2009-09-02 15:27:24 -0700120 public static final int ICON_WIDTH_PX = 64;
121 public static final int ICON_TEXTURE_WIDTH_PX = 128;
122
123 public static final int ICON_HEIGHT_PX = 64;
124 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700125
126 public int SCREEN_WIDTH_PX;
127 public int SCREEN_HEIGHT_PX;
128
Joe Onoratobcbeab82009-10-01 21:45:43 -0700129 public void recompute(int w, int h) {
130 SCREEN_WIDTH_PX = 480;
131 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700132 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700133 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700134
135 public AllAppsView(Context context, AttributeSet attrs) {
136 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700137 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400138 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700139 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700140 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700141 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700142 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
143
Joe Onorato6665c0f2009-09-02 15:27:24 -0700144 setOnClickListener(this);
145 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700146 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700147 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700148 }
149
Joe Onoratob39e51a2009-10-28 15:47:49 -0400150 /**
151 * If you have an attached click listener, View always plays the click sound!?!?
152 * Deal with sound effects by hand.
153 */
154 public void reallyPlaySoundEffect(int sound) {
155 boolean old = isSoundEffectsEnabled();
156 setSoundEffectsEnabled(true);
157 playSoundEffect(sound);
158 setSoundEffectsEnabled(old);
159 }
160
Joe Onorato7c312c12009-08-13 21:36:53 -0700161 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
162 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700163 }
164
Joe Onorato6665c0f2009-09-02 15:27:24 -0700165 public void setLauncher(Launcher launcher) {
166 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700167 }
168
Joe Onorato1feb3a82009-08-08 22:32:00 -0700169 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700170 public void surfaceDestroyed(SurfaceHolder holder) {
171 super.surfaceDestroyed(holder);
172
173 destroyRenderScript();
174 mRS = null;
175 mRollo = null;
176 }
177
178 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700179 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500180 Log.d(TAG, "starting surfaceChanged");
Joe Onorato6665c0f2009-09-02 15:27:24 -0700181 long startTime = SystemClock.uptimeMillis();
182
Joe Onorato080d9b62009-11-02 12:01:11 -0500183 super.surfaceChanged(holder, format, w, h);
184
Jason Sams81134792009-10-27 15:38:42 -0700185 if (mRS != null) {
186 destroyRenderScript();
187 mRS = null;
188 mRollo = null;
189 }
190
Jason Sams05de32a2009-09-27 14:01:40 -0700191 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700192 mRollo = new RolloRS();
193 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 if (mAllAppsList != null) {
195 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700196 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700198
199 Resources res = getContext().getResources();
200 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700201 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700202
203 long endTime = SystemClock.uptimeMillis();
204 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700205 }
Jason Sams2e19c052009-10-20 18:19:55 -0700206
Joe Onorato93839052009-08-06 20:34:32 -0700207 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700208 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
209 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400210
211 if (!isVisible()) {
212 return;
213 }
214
Mike Cleron7d5d7462009-10-20 14:06:00 -0700215 if (gainFocus) {
216 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
217 // Select the first icon when we gain keyboard focus
218 mArrowNavigation = true;
219 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE);
220 mRollo.mState.save();
221 }
222 } else {
223 if (mArrowNavigation) {
224 // Clear selection when we lose focus
225 mRollo.clearSelectedIcon();
226 mRollo.mState.save();
227 mArrowNavigation = false;
228 }
229 }
230 }
231
232 @Override
233 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700234
Joe Onorato859b3a72009-10-28 15:17:01 -0400235 if (!isVisible()) {
236 return false;
237 }
238
Joe Onoratoa13f5742009-11-02 17:15:19 -0500239 final int iconCount = mRollo.mState.iconCount;
240
Mike Cleron7d5d7462009-10-20 14:06:00 -0700241 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
242 if (mArrowNavigation) {
243 int whichApp = mRollo.mState.selectedIconIndex;
244 if (whichApp >= 0) {
245 ApplicationInfo app = mAllAppsList.get(whichApp);
246 mLauncher.startActivitySafely(app.intent);
247 }
248 }
249 }
Jason Sams2e19c052009-10-20 18:19:55 -0700250
Joe Onoratoa13f5742009-11-02 17:15:19 -0500251 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700252 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700253
Joe Onoratoa13f5742009-11-02 17:15:19 -0500254 final int currentSelection = mRollo.mState.selectedIconIndex;
255 final int currentTopRow = (int) mRollo.mMessageProc.mPosX;
Jason Sams2e19c052009-10-20 18:19:55 -0700256
Mike Cleron7d5d7462009-10-20 14:06:00 -0700257 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500258 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700259
Mike Cleron7d5d7462009-10-20 14:06:00 -0700260 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500261 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700262 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700263
Mike Cleron7d5d7462009-10-20 14:06:00 -0700264 int newSelection = currentSelection;
265
266 switch (keyCode) {
267 case KeyEvent.KEYCODE_DPAD_UP:
268 if (currentPageRow > 0) {
269 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
270 } else if (currentTopRow > 0) {
271 mRollo.moveTo(currentTopRow - 1);
Jason Sams2e19c052009-10-20 18:19:55 -0700272 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700273 }
274 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500275 case KeyEvent.KEYCODE_DPAD_DOWN: {
276 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
277 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
278 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
279 if (currentRow < rowCount-1) {
280 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
281 if (newSelection >= iconCount) {
282 // Go from D to G in this arrangement:
283 // A B C D
284 // E F G
285 newSelection = iconCount - 1;
286 }
287 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700288 mRollo.moveTo(currentTopRow + 1);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700289 }
290 }
291 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500292 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700293 case KeyEvent.KEYCODE_DPAD_LEFT:
294 if (currentPageCol > 0) {
295 newSelection = currentSelection - 1;
296 }
297 break;
298 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700299 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500300 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700301 newSelection = currentSelection + 1;
302 }
303 break;
304 }
305 if (newSelection != currentSelection) {
306 mRollo.selectIcon(newSelection);
307 mRollo.mState.save();
308 }
309 }
310 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700311 }
312
Jason Samsd8152b92009-10-13 17:19:10 -0700313 private int mRSMode = 0;
314
Joe Onorato93839052009-08-06 20:34:32 -0700315 @Override
316 public boolean onTouchEvent(MotionEvent ev)
317 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700318 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700319
Joe Onorato7bb17492009-09-24 17:51:01 -0700320 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700321 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700322 }
323
Joe Onoratofb0ca672009-09-14 17:55:46 -0400324 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700325 return true;
326 }
327
328 super.onTouchEvent(ev);
329
Joe Onoratofb0ca672009-09-14 17:55:46 -0400330 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700331 int y = (int)ev.getY();
332
Joe Onoratobcbeab82009-10-01 21:45:43 -0700333 int action = ev.getAction();
334 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400335 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700336 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
337 mTouchTracking = TRACKING_HOME;
Joe Onoratod63458b2009-10-15 21:19:09 -0700338 mRollo.setHomeSelected(true);
339 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700340 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700341 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700342 mTouchTracking = TRACKING_FLING;
343
344 mMotionDownRawX = (int)ev.getRawX();
345 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700346
Mike Cleron7d5d7462009-10-20 14:06:00 -0700347 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700348 mRollo.mState.newTouchDown = 1;
349
350 if (!mRollo.checkClickOK()) {
351 mRollo.clearSelectedIcon();
352 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700353 mDownIconIndex = mCurrentIconIndex
354 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
355 if (mDownIconIndex < 0) {
356 // if nothing was selected, no long press.
357 cancelLongPress();
358 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700359 }
360 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700361 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700362 mVelocity = VelocityTracker.obtain();
363 mVelocity.addMovement(ev);
364 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700365 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400366 break;
367 case MotionEvent.ACTION_MOVE:
368 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700369 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratod63458b2009-10-15 21:19:09 -0700370 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]);
371 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700372 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700373 int rawX = (int)ev.getRawX();
374 int rawY = (int)ev.getRawY();
375 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700376 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700377
Joe Onorato82ca5502009-10-15 16:59:23 -0700378 if (!mStartedScrolling && slop < mSlop) {
379 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700380 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700381 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
382 if (mDownIconIndex != mCurrentIconIndex) {
383 // If a different icon is selected, don't allow it to be picked up.
384 // This handles off-axis dragging.
385 cancelLongPress();
386 mCurrentIconIndex = -1;
387 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700388 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700389 if (!mStartedScrolling) {
390 cancelLongPress();
391 mCurrentIconIndex = -1;
392 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700393 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700394 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700395 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700396
Joe Onoratobcbeab82009-10-01 21:45:43 -0700397 mStartedScrolling = true;
398 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700399 mVelocity.addMovement(ev);
400 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700401 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400402 }
403 break;
404 case MotionEvent.ACTION_UP:
405 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700406 if (mTouchTracking == TRACKING_HOME) {
407 if (action == MotionEvent.ACTION_UP) {
408 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400409 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700410 mLauncher.closeAllApps(true);
411 }
Joe Onoratod63458b2009-10-15 21:19:09 -0700412 mRollo.setHomeSelected(false);
413 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700414 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700415 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700416 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700417 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700418 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700419
Jason Sams12c14a82009-10-06 14:33:15 -0700420 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700421 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700422 mRollo.clearSelectedIcon();
423 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700424 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700425
Joe Onorato539ed9d2009-10-02 10:22:14 -0700426 if (mVelocity != null) {
427 mVelocity.recycle();
428 mVelocity = null;
429 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700430 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700431 mTouchTracking = TRACKING_NONE;
432 break;
Joe Onorato93839052009-08-06 20:34:32 -0700433 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700434
435 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700436 }
437
Joe Onorato6665c0f2009-09-02 15:27:24 -0700438 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700439 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400440 return;
441 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700442 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
443 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400444 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700445 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700446 mLauncher.startActivitySafely(app.intent);
447 }
448 }
449
450 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700451 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400452 return true;
453 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700454 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
455 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
456 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700457
458 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700459 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
460 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700461
Joe Onoratobcbeab82009-10-01 21:45:43 -0700462 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
463 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700464 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700465 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700466 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700467
Joe Onorato7bb17492009-09-24 17:51:01 -0700468 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700469 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700470 return true;
471 }
472
Joe Onorato5162ea92009-09-03 09:39:42 -0700473 public void setDragController(DragController dragger) {
474 mDragController = dragger;
475 }
476
477 public void onDropCompleted(View target, boolean success) {
478 }
479
Joe Onorato4db52312009-10-06 11:17:43 -0700480 /**
481 * Zoom to the specifed amount.
482 *
483 * @param amount [0..1] 0 is hidden, 1 is open
484 * @param animate Whether to animate.
485 */
Jason Sams12c14a82009-10-06 14:33:15 -0700486 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700487 if (mRollo == null) {
488 return;
489 }
490
Joe Onoratofb0ca672009-09-14 17:55:46 -0400491 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400492 mRollo.clearSelectedIcon();
Joe Onorato1d708e72009-10-15 21:29:21 -0700493 mRollo.setHomeSelected(false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700494 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700495 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700496 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700497 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700498 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700499 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400500 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400501 }
502
503 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700504 if (mRollo == null) {
505 return false;
506 }
Jason Sams12c14a82009-10-06 14:33:15 -0700507 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400508 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700509
Mike Cleronb6082fa02009-10-19 17:03:36 -0700510 /*
Joe Onorato93839052009-08-06 20:34:32 -0700511 @Override
512 public boolean onTrackballEvent(MotionEvent ev)
513 {
514 float x = ev.getX();
515 float y = ev.getY();
516 //Float tx = new Float(x);
517 //Float ty = new Float(y);
518 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
519
520
521 return true;
522 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700523 */
Joe Onorato93839052009-08-06 20:34:32 -0700524
Joe Onorato9c1289c2009-08-17 11:03:03 -0400525 public void setApps(ArrayList<ApplicationInfo> list) {
526 mAllAppsList = list;
527 if (mRollo != null) {
528 mRollo.setApps(list);
529 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700530 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400531 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700532 }
533
Joe Onoratoa8138d52009-10-06 19:25:30 -0700534 public void addApps(ArrayList<ApplicationInfo> list) {
535 final int N = list.size();
536 if (mRollo != null) {
537 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
538 }
539
540 for (int i=0; i<N; i++) {
541 final ApplicationInfo item = list.get(i);
542 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
543 if (index < 0) {
544 index = -(index+1);
545 }
546 mAllAppsList.add(index, item);
547 if (mRollo != null) {
548 mRollo.addApp(index, item);
549 mRollo.mState.iconCount++;
550 }
551 }
552
553 if (mRollo != null) {
554 mRollo.saveAppsList();
555 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700556 }
557
Joe Onoratoa8138d52009-10-06 19:25:30 -0700558 public void removeApps(ArrayList<ApplicationInfo> list) {
559 final int N = list.size();
560 for (int i=0; i<N; i++) {
561 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400562 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700563 if (index >= 0) {
564 mAllAppsList.remove(index);
565 if (mRollo != null) {
566 mRollo.removeApp(index);
567 mRollo.mState.iconCount--;
568 }
569 } else {
570 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
571 // Try to recover. This should keep us from crashing for now.
572 }
573 }
574
575 if (mRollo != null) {
576 mRollo.saveAppsList();
577 }
578 }
579
580 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
581 // Just remove and add, because they may need to be re-sorted.
582 removeApps(list);
583 addApps(list);
584 }
585
586 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
587 public int compare(ApplicationInfo a, ApplicationInfo b) {
588 int result = a.title.toString().compareTo(b.toString());
589 if (result != 0) {
590 return result;
591 }
592 return a.intent.getComponent().compareTo(b.intent.getComponent());
593 }
594 };
595
Joe Onoratocb9f7982009-10-31 16:32:02 -0400596 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
597 ComponentName component = item.intent.getComponent();
598 final int N = list.size();
599 for (int i=0; i<N; i++) {
600 ApplicationInfo x = list.get(i);
601 if (x.intent.getComponent().equals(component)) {
602 return i;
603 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700604 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400605 return -1;
606 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700607
Joe Onoratoc567acb2009-08-31 14:34:43 -0700608 private static int countPages(int iconCount) {
609 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
610 int pages = iconCount / iconsPerPage;
611 if (pages*iconsPerPage != iconCount) {
612 pages++;
613 }
614 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400615 }
616
Joe Onorato93839052009-08-06 20:34:32 -0700617 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700618
Joe Onorato1feb3a82009-08-08 22:32:00 -0700619 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700620 private int mWidth;
621 private int mHeight;
622
623 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700624 private Script mScript;
625 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700626 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700627 private Script.Invokable mInvokeFling;
628 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700629
Jason Samsc1c521e2009-10-19 14:45:45 -0700630
Jason Samscd689e12009-09-29 15:28:22 -0700631 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700632 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700633 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700634 private ProgramFragment mPFTexMip;
635 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700636 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700637 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700638 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700639 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700640
Joe Onoratod63458b2009-10-15 21:19:09 -0700641 private Allocation mHomeButtonNormal;
642 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700643
Joe Onoratobf15cb42009-08-07 14:33:40 -0700644 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700645 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700646 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700647
Joe Onoratobf15cb42009-08-07 14:33:40 -0700648 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700649 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700650 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700651 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700652
Joe Onorato6665c0f2009-09-02 15:27:24 -0700653 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700654 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700655
656 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400657 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700658
Jason Samsd8152b92009-10-13 17:19:10 -0700659
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700660 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700661 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700662
Jason Sams78aebd82009-09-15 13:06:59 -0700663 class BaseAlloc {
664 Allocation mAlloc;
665 Type mType;
666
667 void save() {
668 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700669 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700670 }
671
Jason Sams12c14a82009-10-06 14:33:15 -0700672 class AAMessage extends RenderScript.RSMessage {
673 public void run() {
674 mPosX = ((float)mData[0]) / (1 << 16);
675 mVelocity = ((float)mData[1]) / (1 << 16);
676 mZoom = ((float)mData[2]) / (1 << 16);
677 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
678 }
679 float mZoom;
680 float mPosX;
681 float mVelocity;
682 }
683 AAMessage mMessageProc;
684
Jason Sams476339d2009-09-29 18:14:38 -0700685 private boolean checkClickOK() {
686 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams2e19c052009-10-20 18:19:55 -0700687 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
688 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700689 }
690
Jason Sams78aebd82009-09-15 13:06:59 -0700691 class Params extends BaseAlloc {
692 Params() {
693 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
694 mAlloc = Allocation.createTyped(mRS, mType);
695 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700696 }
Jason Sams78aebd82009-09-15 13:06:59 -0700697 public int bubbleWidth;
698 public int bubbleHeight;
699 public int bubbleBitmapWidth;
700 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700701
Joe Onoratobcbeab82009-10-01 21:45:43 -0700702 public int homeButtonWidth;
703 public int homeButtonHeight;
704 public int homeButtonTextureWidth;
705 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700706 }
707
708 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700709 public float newPositionX;
710 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700711 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700712 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700713 public int selectedIconIndex = -1;
714 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700715 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700716 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700717 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700718
719 State() {
720 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
721 mAlloc = Allocation.createTyped(mRS, mType);
722 save();
723 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700724 }
725
726 public RolloRS() {
727 }
728
729 public void init(Resources res, int width, int height) {
730 mRes = res;
731 mWidth = width;
732 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700733 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700734 initProgramVertex();
735 initProgramFragment();
736 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700737 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700738 initGl();
739 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700740 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700741 initRs();
742 }
743
Jason Sams0aa71662009-10-02 18:43:18 -0700744 public void initMesh() {
745 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
746 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
747
Jason Samsd8152b92009-10-13 17:19:10 -0700748 float y = 0;
749 float z = 0;
750 for (int ct=0; ct < 200; ct++) {
751 float angle = 0;
752 float maxAngle = 3.14f * 0.16f;
753 float l = 1.f;
754
755 l = 1 - ((ct-5) * 0.10f);
756 if (ct > 7) {
757 angle = maxAngle * (ct - 7) * 0.2f;
758 angle = Math.min(angle, maxAngle);
759 }
Jason Samsc8514792009-10-29 14:27:29 -0700760 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700761 l = Math.min(1.0f, l);
762
763 y += 0.1f * Math.cos(angle);
764 z += 0.1f * Math.sin(angle);
765
766 float t = 0.1f * ct;
767 float ds = 0.08f;
768 tm.setColor(l, l, l, 0.99f);
769 tm.setTexture(ds, t);
770 tm.addVertex(-0.5f, y, z);
771 tm.setTexture(1 - ds, t);
772 tm.addVertex(0.5f, y, z);
773 }
774 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
775 tm.addTriangle(ct, ct+1, ct+2);
776 tm.addTriangle(ct+1, ct+3, ct+2);
777 }
778 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700779 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700780 }
781
Jason Samscd689e12009-09-29 15:28:22 -0700782 private void initProgramVertex() {
783 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
784 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700785
786 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700787 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700788 mPV = pvb.create();
789 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700790 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700791
Jason Sams37e7c2b2009-10-19 12:55:43 -0700792 //pva = new ProgramVertex.MatrixAllocation(mRS);
793 //pva.setupOrthoWindow(mWidth, mHeight);
794 //pvb.setTextureMatrixEnable(true);
795 //mPVOrtho = pvb.create();
796 //mPVOrtho.setName("PVOrtho");
797 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700798
799 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700800 }
Joe Onorato93839052009-08-06 20:34:32 -0700801
Jason Samscd689e12009-09-29 15:28:22 -0700802 private void initProgramFragment() {
803 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700804 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700805 sb.setMag(Sampler.Value.LINEAR);
806 sb.setWrapS(Sampler.Value.CLAMP);
807 sb.setWrapT(Sampler.Value.CLAMP);
808 Sampler linear = sb.create();
809
810 sb.setMin(Sampler.Value.NEAREST);
811 sb.setMag(Sampler.Value.NEAREST);
812 Sampler nearest = sb.create();
813
814 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700815 //mPFColor = bf.create();
816 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700817
818 bf.setTexEnable(true, 0);
819 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700820 mPFTexMip = bf.create();
821 mPFTexMip.setName("PFTexMip");
822 mPFTexMip.bindSampler(linear, 0);
823
824 mPFTexNearest = bf.create();
825 mPFTexNearest.setName("PFTexNearest");
826 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700827 }
828
829 private void initProgramStore() {
830 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
831 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700832 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700833 bs.setDitherEnable(true);
834 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
835 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
836 mPSIcons = bs.create();
837 mPSIcons.setName("PSIcons");
838
839 //bs.setDitherEnable(false);
840 //mPSText = bs.create();
841 //mPSText.setName("PSText");
842 }
843
844 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700845 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700846 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700847 }
Jason Sams78aebd82009-09-15 13:06:59 -0700848
Joe Onorato1feb3a82009-08-08 22:32:00 -0700849 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700850 mParams = new Params();
851 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700852
Joe Onoratobf15cb42009-08-07 14:33:40 -0700853 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700854
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700855 mParams.bubbleWidth = bubble.getBubbleWidth();
856 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
857 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
858 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700859
Joe Onoratod63458b2009-10-15 21:19:09 -0700860 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
861 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
862 mHomeButtonNormal.uploadToTexture(0);
863 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
864 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
865 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700866 mParams.homeButtonWidth = 76;
867 mParams.homeButtonHeight = 68;
868 mParams.homeButtonTextureWidth = 128;
869 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700870
Joe Onoratod63458b2009-10-15 21:19:09 -0700871 mState.homeButtonId = mHomeButtonNormal.getID();
872
Joe Onorato1feb3a82009-08-08 22:32:00 -0700873 mParams.save();
874 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400875
Joe Onorato1291a8c2009-09-15 15:07:25 -0400876 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
877 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
878 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700879
Joe Onorato9c1289c2009-08-17 11:03:03 -0400880 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700881 }
882
Jason Sams37e7c2b2009-10-19 12:55:43 -0700883 private void initScript(int id) {
884 }
885
886 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700887 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700888 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700889 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700890 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700891 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
892 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700893 mInvokeMove = sb.addInvokable("move");
894 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700895 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700896 mInvokeResetWAR = sb.addInvokable("resetHWWar");
897 mScript = sb.create();
898 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
899 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
900 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
901 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
902 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700903
Jason Sams12c14a82009-10-06 14:33:15 -0700904 mMessageProc = new AAMessage();
905 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700906 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700907 }
Joe Onorato93839052009-08-06 20:34:32 -0700908
Joe Onorato9c1289c2009-08-17 11:03:03 -0400909 private void setApps(ArrayList<ApplicationInfo> list) {
910 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700911 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700912 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700913 allocCount = 1;
914 }
915
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700917 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700918 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919
920 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700921 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700922 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400923
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700924 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925
926 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
927
928 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700929 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400930 }
931
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 mState.iconCount = count;
933
Joe Onoratoa8138d52009-10-06 19:25:30 -0700934 saveAppsList();
935 }
936
Jason Samsc8514792009-10-29 14:27:29 -0700937 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
938 int black[] = new int[w > h ? w : h];
939 Allocation.Adapter2D a = alloc.createAdapter2D();
940 int mip = 0;
941 while (w > 1 || h > 1) {
942 a.subData(0, 0, 1, h, black);
943 a.subData(w-1, 0, 1, h, black);
944 a.subData(0, 0, w, 1, black);
945 a.subData(0, h-1, w, 1, black);
946 mip++;
947 w = (w + 1) >> 1;
948 h = (h + 1) >> 1;
949 a.setConstraint(Dimension.LOD, mip);
950 }
951 a.subData(0, 0, 1, 1, black);
952 }
953
Joe Onoratoa8138d52009-10-06 19:25:30 -0700954 private void uploadAppIcon(int index, ApplicationInfo item) {
955 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -0700956 Element.RGBA_8888(mRS), true);
957 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
958
Joe Onoratoa8138d52009-10-06 19:25:30 -0700959 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -0700960 Element.RGBA_8888(mRS), true);
961 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -0700962
963 mIcons[index].uploadToTexture(0);
964 mLabels[index].uploadToTexture(0);
965
966 mIconIds[index] = mIcons[index].getID();
967 mLabelIds[index] = mLabels[index].getID();
968 }
969
970 /**
971 * Puts the empty spaces at the end. Updates mState.iconCount. You must
972 * fill in the values and call saveAppsList().
973 */
974 private void reallocAppsList(int count) {
975 Allocation[] icons = new Allocation[count];
976 int[] iconIds = new int[count];
977 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
978
979 Allocation[] labels = new Allocation[count];
980 int[] labelIds = new int[count];
981 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
982
983 final int oldCount = mIcons.length;
984
985 System.arraycopy(mIcons, 0, icons, 0, oldCount);
986 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
987 System.arraycopy(mLabels, 0, labels, 0, oldCount);
988 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
989
990 mIcons = icons;
991 mIconIds = iconIds;
992 mLabels = labels;
993 mLabelIds = labelIds;
994 }
995
996 /**
997 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
998 */
999 private void addApp(int index, ApplicationInfo item) {
1000 final int count = mState.iconCount - index;
1001 final int dest = index + 1;
1002
1003 System.arraycopy(mIcons, index, mIcons, dest, count);
1004 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1005 System.arraycopy(mLabels, index, mLabels, dest, count);
1006 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1007
1008 uploadAppIcon(index, item);
1009 }
1010
1011 /**
1012 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1013 */
1014 private void removeApp(int index) {
1015 final int count = mState.iconCount - index - 1;
1016 final int src = index + 1;
1017
1018 System.arraycopy(mIcons, src, mIcons, index, count);
1019 System.arraycopy(mIconIds, src, mIconIds, index, count);
1020 System.arraycopy(mLabels, src, mLabels, index, count);
1021 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1022
1023 final int last = mState.iconCount - 1;
1024 mIcons[last] = null;
1025 mIconIds[last] = 0;
1026 mLabels[last] = null;
1027 mLabelIds[last] = 0;
1028 }
1029
1030 /**
1031 * Send the apps list structures to RS.
1032 */
1033 private void saveAppsList() {
1034 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001035
Joe Onoratoa8138d52009-10-06 19:25:30 -07001036 mAllocIconIds.data(mIconIds);
1037 mAllocLabelIds.data(mLabelIds);
1038
Jason Sams37e7c2b2009-10-19 12:55:43 -07001039 if (mScript != null) { // this happens when we init it
1040 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1041 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042 }
1043
1044 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001045
1046 // Note: mScript may be null if we haven't initialized it yet.
1047 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001048 if (mInvokeResetWAR != null) {
1049 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001050 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001051 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001052 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001053
1054 void initTouchState() {
1055 int width = getWidth();
1056 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001057 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1058 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001059
Jason Sams37e7c2b2009-10-19 12:55:43 -07001060 int centerY = (height / 2);
1061 mTouchYBorders[0] = centerY - (cellHeight * 2);
1062 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001063 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001064 mTouchYBorders[3] = centerY + cellHeight;
1065 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001066
Joe Onorato6665c0f2009-09-02 15:27:24 -07001067 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001068 mTouchXBorders[0] = 0;
1069 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001070 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001071 mTouchXBorders[3] = centerX + (width / 4);
1072 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001073 }
1074
Joe Onorato664457d2009-10-28 16:30:34 -04001075 void fling() {
1076 mInvokeFling.execute();
1077 }
1078
1079 void move() {
1080 mInvokeMove.execute();
1081 }
1082
1083 void moveTo(float row) {
1084 mState.targetPos = row;
1085 mState.save();
1086 mInvokeMoveTo.execute();
1087 }
1088
Jason Sams37e7c2b2009-10-19 12:55:43 -07001089 int chooseTappedIcon(int x, int y, float pos) {
1090 // Adjust for scroll position if not zero.
1091 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001092
Joe Onorato6665c0f2009-09-02 15:27:24 -07001093 int col = -1;
1094 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001095 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1096 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1097 col = i;
1098 break;
1099 }
1100 }
1101 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1102 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1103 row = i;
1104 break;
1105 }
1106 }
1107
1108 if (row < 0 || col < 0) {
1109 return -1;
1110 }
1111
Joe Onorato664457d2009-10-28 16:30:34 -04001112 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001113 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001114
Joe Onorato664457d2009-10-28 16:30:34 -04001115 if (index >= mState.iconCount) {
1116 return -1;
1117 } else {
1118 return index;
1119 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001120 }
1121
Joe Onorato6665c0f2009-09-02 15:27:24 -07001122 /**
1123 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001124 *
1125 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001126 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001127 int selectIcon(int x, int y, float pos) {
1128 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001129 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001130 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001131 }
1132
1133 void selectIcon(int index) {
Jason Samsc8514792009-10-29 14:27:29 -07001134 if (index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001135 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001136 } else {
1137 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001138
1139 Bitmap selectionBitmap = mSelectionBitmap;
1140
1141 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1142 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1143 mAllAppsList.get(index).iconBitmap);
1144
1145 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001146 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001147 mSelectedIcon.uploadToTexture(0);
1148 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001149 }
1150 }
1151
1152 /**
1153 * You need to call save() on mState on your own after calling this.
1154 */
1155 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001156 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001157 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001158
Joe Onoratod63458b2009-10-15 21:19:09 -07001159 void setHomeSelected(boolean pressed) {
1160 if (pressed) {
1161 mState.homeButtonId = mHomeButtonPressed.getID();
1162 } else {
1163 mState.homeButtonId = mHomeButtonNormal.getID();
1164 }
1165 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001166 }
Joe Onorato93839052009-08-06 20:34:32 -07001167}
1168
1169