blob: aea8c80dfd371a66648e811a1cc95998dd8367a8 [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) {
180 super.surfaceChanged(holder, format, w, h);
181
Joe Onorato6665c0f2009-09-02 15:27:24 -0700182 long startTime = SystemClock.uptimeMillis();
183
Jason Sams81134792009-10-27 15:38:42 -0700184 if (mRS != null) {
185 destroyRenderScript();
186 mRS = null;
187 mRollo = null;
188 }
189
Jason Sams05de32a2009-09-27 14:01:40 -0700190 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700191 mRollo = new RolloRS();
192 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 if (mAllAppsList != null) {
194 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700195 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400196 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700197
198 Resources res = getContext().getResources();
199 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700200 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700201
202 long endTime = SystemClock.uptimeMillis();
203 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700204 }
Jason Sams2e19c052009-10-20 18:19:55 -0700205
Joe Onorato93839052009-08-06 20:34:32 -0700206 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700207 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
208 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400209
210 if (!isVisible()) {
211 return;
212 }
213
Mike Cleron7d5d7462009-10-20 14:06:00 -0700214 if (gainFocus) {
215 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
216 // Select the first icon when we gain keyboard focus
217 mArrowNavigation = true;
218 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE);
219 mRollo.mState.save();
220 }
221 } else {
222 if (mArrowNavigation) {
223 // Clear selection when we lose focus
224 mRollo.clearSelectedIcon();
225 mRollo.mState.save();
226 mArrowNavigation = false;
227 }
228 }
229 }
230
231 @Override
232 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700233
Joe Onorato859b3a72009-10-28 15:17:01 -0400234 if (!isVisible()) {
235 return false;
236 }
237
Mike Cleron7d5d7462009-10-20 14:06:00 -0700238 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
239 if (mArrowNavigation) {
240 int whichApp = mRollo.mState.selectedIconIndex;
241 if (whichApp >= 0) {
242 ApplicationInfo app = mAllAppsList.get(whichApp);
243 mLauncher.startActivitySafely(app.intent);
244 }
245 }
246 }
Jason Sams2e19c052009-10-20 18:19:55 -0700247
Mike Cleron7d5d7462009-10-20 14:06:00 -0700248 if (mArrowNavigation && mRollo.mState.iconCount > 0) {
249 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700250
Mike Cleron7d5d7462009-10-20 14:06:00 -0700251 int currentSelection = mRollo.mState.selectedIconIndex;
252 int currentTopRow = (int) mRollo.mMessageProc.mPosX;
Jason Sams2e19c052009-10-20 18:19:55 -0700253
Mike Cleron7d5d7462009-10-20 14:06:00 -0700254 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
255 int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700256
Mike Cleron7d5d7462009-10-20 14:06:00 -0700257 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
258 int currentPageRow = (currentSelection - (currentTopRow * Defines.COLUMNS_PER_PAGE))
259 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700260
Mike Cleron7d5d7462009-10-20 14:06:00 -0700261 int newSelection = currentSelection;
262
263 switch (keyCode) {
264 case KeyEvent.KEYCODE_DPAD_UP:
265 if (currentPageRow > 0) {
266 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
267 } else if (currentTopRow > 0) {
268 mRollo.moveTo(currentTopRow - 1);
Jason Sams2e19c052009-10-20 18:19:55 -0700269 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700270 }
271 break;
272 case KeyEvent.KEYCODE_DPAD_DOWN:
273 if (currentSelection < mRollo.mState.iconCount - Defines.COLUMNS_PER_PAGE) {
274 if (currentPageRow < Defines.ROWS_PER_PAGE - 1) {
275 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
276 } else {
277 mRollo.moveTo(currentTopRow + 1);
278 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
279 }
280 }
281 break;
282 case KeyEvent.KEYCODE_DPAD_LEFT:
283 if (currentPageCol > 0) {
284 newSelection = currentSelection - 1;
285 }
286 break;
287 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700288 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Mike Cleron7d5d7462009-10-20 14:06:00 -0700289 (currentSelection < mRollo.mState.iconCount - 1)) {
290 newSelection = currentSelection + 1;
291 }
292 break;
293 }
294 if (newSelection != currentSelection) {
295 mRollo.selectIcon(newSelection);
296 mRollo.mState.save();
297 }
298 }
299 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700300 }
301
Jason Samsd8152b92009-10-13 17:19:10 -0700302 private int mRSMode = 0;
303
Joe Onorato93839052009-08-06 20:34:32 -0700304 @Override
305 public boolean onTouchEvent(MotionEvent ev)
306 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700307 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700308
Joe Onorato7bb17492009-09-24 17:51:01 -0700309 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700310 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700311 }
312
Joe Onoratofb0ca672009-09-14 17:55:46 -0400313 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700314 return true;
315 }
316
317 super.onTouchEvent(ev);
318
Joe Onoratofb0ca672009-09-14 17:55:46 -0400319 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700320 int y = (int)ev.getY();
321
Joe Onoratobcbeab82009-10-01 21:45:43 -0700322 int action = ev.getAction();
323 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400324 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700325 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
326 mTouchTracking = TRACKING_HOME;
Joe Onoratod63458b2009-10-15 21:19:09 -0700327 mRollo.setHomeSelected(true);
328 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700329 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700330 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700331 mTouchTracking = TRACKING_FLING;
332
333 mMotionDownRawX = (int)ev.getRawX();
334 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700335
Mike Cleron7d5d7462009-10-20 14:06:00 -0700336 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700337 mRollo.mState.newTouchDown = 1;
338
339 if (!mRollo.checkClickOK()) {
340 mRollo.clearSelectedIcon();
341 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700342 mDownIconIndex = mCurrentIconIndex
343 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
344 if (mDownIconIndex < 0) {
345 // if nothing was selected, no long press.
346 cancelLongPress();
347 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700348 }
349 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700350 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700351 mVelocity = VelocityTracker.obtain();
352 mVelocity.addMovement(ev);
353 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700354 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400355 break;
356 case MotionEvent.ACTION_MOVE:
357 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700358 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratod63458b2009-10-15 21:19:09 -0700359 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]);
360 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700361 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700362 int rawX = (int)ev.getRawX();
363 int rawY = (int)ev.getRawY();
364 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700365 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700366
Joe Onorato82ca5502009-10-15 16:59:23 -0700367 if (!mStartedScrolling && slop < mSlop) {
368 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700369 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700370 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
371 if (mDownIconIndex != mCurrentIconIndex) {
372 // If a different icon is selected, don't allow it to be picked up.
373 // This handles off-axis dragging.
374 cancelLongPress();
375 mCurrentIconIndex = -1;
376 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700377 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700378 if (!mStartedScrolling) {
379 cancelLongPress();
380 mCurrentIconIndex = -1;
381 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700382 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700383 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700384 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700385
Joe Onoratobcbeab82009-10-01 21:45:43 -0700386 mStartedScrolling = true;
387 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700388 mVelocity.addMovement(ev);
389 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700390 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400391 }
392 break;
393 case MotionEvent.ACTION_UP:
394 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700395 if (mTouchTracking == TRACKING_HOME) {
396 if (action == MotionEvent.ACTION_UP) {
397 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400398 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700399 mLauncher.closeAllApps(true);
400 }
Joe Onoratod63458b2009-10-15 21:19:09 -0700401 mRollo.setHomeSelected(false);
402 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700403 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700404 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700405 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700406 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700407 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700408
Jason Sams12c14a82009-10-06 14:33:15 -0700409 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700410 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700411 mRollo.clearSelectedIcon();
412 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700413 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700414
Joe Onorato539ed9d2009-10-02 10:22:14 -0700415 if (mVelocity != null) {
416 mVelocity.recycle();
417 mVelocity = null;
418 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700419 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700420 mTouchTracking = TRACKING_NONE;
421 break;
Joe Onorato93839052009-08-06 20:34:32 -0700422 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700423
424 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700425 }
426
Joe Onorato6665c0f2009-09-02 15:27:24 -0700427 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700428 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400429 return;
430 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700431 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
432 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400433 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700434 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700435 mLauncher.startActivitySafely(app.intent);
436 }
437 }
438
439 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700440 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400441 return true;
442 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700443 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
444 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
445 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700446
447 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700448 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
449 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700450
Joe Onoratobcbeab82009-10-01 21:45:43 -0700451 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
452 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700453 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700454 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700455 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700456
Joe Onorato7bb17492009-09-24 17:51:01 -0700457 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700458 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700459 return true;
460 }
461
Joe Onorato5162ea92009-09-03 09:39:42 -0700462 public void setDragController(DragController dragger) {
463 mDragController = dragger;
464 }
465
466 public void onDropCompleted(View target, boolean success) {
467 }
468
Joe Onorato4db52312009-10-06 11:17:43 -0700469 /**
470 * Zoom to the specifed amount.
471 *
472 * @param amount [0..1] 0 is hidden, 1 is open
473 * @param animate Whether to animate.
474 */
Jason Sams12c14a82009-10-06 14:33:15 -0700475 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700476 if (mRollo == null) {
477 return;
478 }
479
Joe Onoratofb0ca672009-09-14 17:55:46 -0400480 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400481 mRollo.clearSelectedIcon();
Joe Onorato1d708e72009-10-15 21:29:21 -0700482 mRollo.setHomeSelected(false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700483 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700484 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700485 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700486 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700487 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700488 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400489 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400490 }
491
492 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700493 if (mRollo == null) {
494 return false;
495 }
Jason Sams12c14a82009-10-06 14:33:15 -0700496 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400497 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700498
Mike Cleronb6082fa02009-10-19 17:03:36 -0700499 /*
Joe Onorato93839052009-08-06 20:34:32 -0700500 @Override
501 public boolean onTrackballEvent(MotionEvent ev)
502 {
503 float x = ev.getX();
504 float y = ev.getY();
505 //Float tx = new Float(x);
506 //Float ty = new Float(y);
507 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
508
509
510 return true;
511 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700512 */
Joe Onorato93839052009-08-06 20:34:32 -0700513
Joe Onorato9c1289c2009-08-17 11:03:03 -0400514 public void setApps(ArrayList<ApplicationInfo> list) {
515 mAllAppsList = list;
516 if (mRollo != null) {
517 mRollo.setApps(list);
518 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700519 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400520 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700521 }
522
Joe Onoratoa8138d52009-10-06 19:25:30 -0700523 public void addApps(ArrayList<ApplicationInfo> list) {
524 final int N = list.size();
525 if (mRollo != null) {
526 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
527 }
528
529 for (int i=0; i<N; i++) {
530 final ApplicationInfo item = list.get(i);
531 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
532 if (index < 0) {
533 index = -(index+1);
534 }
535 mAllAppsList.add(index, item);
536 if (mRollo != null) {
537 mRollo.addApp(index, item);
538 mRollo.mState.iconCount++;
539 }
540 }
541
542 if (mRollo != null) {
543 mRollo.saveAppsList();
544 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700545 }
546
Joe Onoratoa8138d52009-10-06 19:25:30 -0700547 public void removeApps(ArrayList<ApplicationInfo> list) {
548 final int N = list.size();
549 for (int i=0; i<N; i++) {
550 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400551 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700552 if (index >= 0) {
553 mAllAppsList.remove(index);
554 if (mRollo != null) {
555 mRollo.removeApp(index);
556 mRollo.mState.iconCount--;
557 }
558 } else {
559 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
560 // Try to recover. This should keep us from crashing for now.
561 }
562 }
563
564 if (mRollo != null) {
565 mRollo.saveAppsList();
566 }
567 }
568
569 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
570 // Just remove and add, because they may need to be re-sorted.
571 removeApps(list);
572 addApps(list);
573 }
574
575 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
576 public int compare(ApplicationInfo a, ApplicationInfo b) {
577 int result = a.title.toString().compareTo(b.toString());
578 if (result != 0) {
579 return result;
580 }
581 return a.intent.getComponent().compareTo(b.intent.getComponent());
582 }
583 };
584
Joe Onoratocb9f7982009-10-31 16:32:02 -0400585 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
586 ComponentName component = item.intent.getComponent();
587 final int N = list.size();
588 for (int i=0; i<N; i++) {
589 ApplicationInfo x = list.get(i);
590 if (x.intent.getComponent().equals(component)) {
591 return i;
592 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700593 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400594 return -1;
595 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700596
Joe Onoratoc567acb2009-08-31 14:34:43 -0700597 private static int countPages(int iconCount) {
598 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
599 int pages = iconCount / iconsPerPage;
600 if (pages*iconsPerPage != iconCount) {
601 pages++;
602 }
603 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400604 }
605
Joe Onorato93839052009-08-06 20:34:32 -0700606 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700607
Joe Onorato1feb3a82009-08-08 22:32:00 -0700608 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700609 private int mWidth;
610 private int mHeight;
611
612 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700613 private Script mScript;
614 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700615 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700616 private Script.Invokable mInvokeFling;
617 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700618
Jason Samsc1c521e2009-10-19 14:45:45 -0700619
Jason Samscd689e12009-09-29 15:28:22 -0700620 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700621 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700622 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700623 private ProgramFragment mPFTexMip;
624 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700625 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700626 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700627 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700628 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700629
Joe Onoratod63458b2009-10-15 21:19:09 -0700630 private Allocation mHomeButtonNormal;
631 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700632
Joe Onoratobf15cb42009-08-07 14:33:40 -0700633 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700634 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700635 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700636
Joe Onoratobf15cb42009-08-07 14:33:40 -0700637 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700638 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700639 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700640 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700641
Joe Onorato6665c0f2009-09-02 15:27:24 -0700642 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700643 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700644
645 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400646 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700647
Jason Samsd8152b92009-10-13 17:19:10 -0700648
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700649 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700650 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700651
Jason Sams78aebd82009-09-15 13:06:59 -0700652 class BaseAlloc {
653 Allocation mAlloc;
654 Type mType;
655
656 void save() {
657 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700658 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700659 }
660
Jason Sams12c14a82009-10-06 14:33:15 -0700661 class AAMessage extends RenderScript.RSMessage {
662 public void run() {
663 mPosX = ((float)mData[0]) / (1 << 16);
664 mVelocity = ((float)mData[1]) / (1 << 16);
665 mZoom = ((float)mData[2]) / (1 << 16);
666 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
667 }
668 float mZoom;
669 float mPosX;
670 float mVelocity;
671 }
672 AAMessage mMessageProc;
673
Jason Sams476339d2009-09-29 18:14:38 -0700674 private boolean checkClickOK() {
675 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams2e19c052009-10-20 18:19:55 -0700676 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
677 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700678 }
679
Jason Sams78aebd82009-09-15 13:06:59 -0700680 class Params extends BaseAlloc {
681 Params() {
682 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
683 mAlloc = Allocation.createTyped(mRS, mType);
684 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700685 }
Jason Sams78aebd82009-09-15 13:06:59 -0700686 public int bubbleWidth;
687 public int bubbleHeight;
688 public int bubbleBitmapWidth;
689 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700690
Joe Onoratobcbeab82009-10-01 21:45:43 -0700691 public int homeButtonWidth;
692 public int homeButtonHeight;
693 public int homeButtonTextureWidth;
694 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700695 }
696
697 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700698 public float newPositionX;
699 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700700 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700701 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700702 public int selectedIconIndex = -1;
703 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700704 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700705 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700706 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700707
708 State() {
709 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
710 mAlloc = Allocation.createTyped(mRS, mType);
711 save();
712 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700713 }
714
715 public RolloRS() {
716 }
717
718 public void init(Resources res, int width, int height) {
719 mRes = res;
720 mWidth = width;
721 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700722 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700723 initProgramVertex();
724 initProgramFragment();
725 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700726 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700727 initGl();
728 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700729 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700730 initRs();
731 }
732
Jason Sams0aa71662009-10-02 18:43:18 -0700733 public void initMesh() {
734 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
735 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
736
Jason Samsd8152b92009-10-13 17:19:10 -0700737 float y = 0;
738 float z = 0;
739 for (int ct=0; ct < 200; ct++) {
740 float angle = 0;
741 float maxAngle = 3.14f * 0.16f;
742 float l = 1.f;
743
744 l = 1 - ((ct-5) * 0.10f);
745 if (ct > 7) {
746 angle = maxAngle * (ct - 7) * 0.2f;
747 angle = Math.min(angle, maxAngle);
748 }
Jason Samsc8514792009-10-29 14:27:29 -0700749 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700750 l = Math.min(1.0f, l);
751
752 y += 0.1f * Math.cos(angle);
753 z += 0.1f * Math.sin(angle);
754
755 float t = 0.1f * ct;
756 float ds = 0.08f;
757 tm.setColor(l, l, l, 0.99f);
758 tm.setTexture(ds, t);
759 tm.addVertex(-0.5f, y, z);
760 tm.setTexture(1 - ds, t);
761 tm.addVertex(0.5f, y, z);
762 }
763 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
764 tm.addTriangle(ct, ct+1, ct+2);
765 tm.addTriangle(ct+1, ct+3, ct+2);
766 }
767 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700768 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700769 }
770
Jason Samscd689e12009-09-29 15:28:22 -0700771 private void initProgramVertex() {
772 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
773 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700774
775 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700776 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700777 mPV = pvb.create();
778 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700779 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700780
Jason Sams37e7c2b2009-10-19 12:55:43 -0700781 //pva = new ProgramVertex.MatrixAllocation(mRS);
782 //pva.setupOrthoWindow(mWidth, mHeight);
783 //pvb.setTextureMatrixEnable(true);
784 //mPVOrtho = pvb.create();
785 //mPVOrtho.setName("PVOrtho");
786 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700787
788 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700789 }
Joe Onorato93839052009-08-06 20:34:32 -0700790
Jason Samscd689e12009-09-29 15:28:22 -0700791 private void initProgramFragment() {
792 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700793 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700794 sb.setMag(Sampler.Value.LINEAR);
795 sb.setWrapS(Sampler.Value.CLAMP);
796 sb.setWrapT(Sampler.Value.CLAMP);
797 Sampler linear = sb.create();
798
799 sb.setMin(Sampler.Value.NEAREST);
800 sb.setMag(Sampler.Value.NEAREST);
801 Sampler nearest = sb.create();
802
803 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700804 //mPFColor = bf.create();
805 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700806
807 bf.setTexEnable(true, 0);
808 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700809 mPFTexMip = bf.create();
810 mPFTexMip.setName("PFTexMip");
811 mPFTexMip.bindSampler(linear, 0);
812
813 mPFTexNearest = bf.create();
814 mPFTexNearest.setName("PFTexNearest");
815 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700816 }
817
818 private void initProgramStore() {
819 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
820 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700821 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700822 bs.setDitherEnable(true);
823 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
824 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
825 mPSIcons = bs.create();
826 mPSIcons.setName("PSIcons");
827
828 //bs.setDitherEnable(false);
829 //mPSText = bs.create();
830 //mPSText.setName("PSText");
831 }
832
833 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700834 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700835 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700836 }
Jason Sams78aebd82009-09-15 13:06:59 -0700837
Joe Onorato1feb3a82009-08-08 22:32:00 -0700838 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700839 mParams = new Params();
840 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700841
Joe Onoratobf15cb42009-08-07 14:33:40 -0700842 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700843
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700844 mParams.bubbleWidth = bubble.getBubbleWidth();
845 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
846 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
847 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700848
Joe Onoratod63458b2009-10-15 21:19:09 -0700849 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
850 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
851 mHomeButtonNormal.uploadToTexture(0);
852 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
853 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
854 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700855 mParams.homeButtonWidth = 76;
856 mParams.homeButtonHeight = 68;
857 mParams.homeButtonTextureWidth = 128;
858 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700859
Joe Onoratod63458b2009-10-15 21:19:09 -0700860 mState.homeButtonId = mHomeButtonNormal.getID();
861
Joe Onorato1feb3a82009-08-08 22:32:00 -0700862 mParams.save();
863 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400864
Joe Onorato1291a8c2009-09-15 15:07:25 -0400865 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
866 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
867 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700868
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700870 }
871
Jason Sams37e7c2b2009-10-19 12:55:43 -0700872 private void initScript(int id) {
873 }
874
875 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700876 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700877 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700878 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700879 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700880 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
881 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700882 mInvokeMove = sb.addInvokable("move");
883 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700884 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700885 mInvokeResetWAR = sb.addInvokable("resetHWWar");
886 mScript = sb.create();
887 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
888 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
889 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
890 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
891 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700892
Jason Sams12c14a82009-10-06 14:33:15 -0700893 mMessageProc = new AAMessage();
894 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700895 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700896 }
Joe Onorato93839052009-08-06 20:34:32 -0700897
Joe Onorato9c1289c2009-08-17 11:03:03 -0400898 private void setApps(ArrayList<ApplicationInfo> list) {
899 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700900 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700901 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700902 allocCount = 1;
903 }
904
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700906 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700907 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908
909 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700910 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700911 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400912
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700913 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914
915 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
916
917 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700918 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400919 }
920
Joe Onorato9c1289c2009-08-17 11:03:03 -0400921 mState.iconCount = count;
922
Joe Onoratoa8138d52009-10-06 19:25:30 -0700923 saveAppsList();
924 }
925
Jason Samsc8514792009-10-29 14:27:29 -0700926 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
927 int black[] = new int[w > h ? w : h];
928 Allocation.Adapter2D a = alloc.createAdapter2D();
929 int mip = 0;
930 while (w > 1 || h > 1) {
931 a.subData(0, 0, 1, h, black);
932 a.subData(w-1, 0, 1, h, black);
933 a.subData(0, 0, w, 1, black);
934 a.subData(0, h-1, w, 1, black);
935 mip++;
936 w = (w + 1) >> 1;
937 h = (h + 1) >> 1;
938 a.setConstraint(Dimension.LOD, mip);
939 }
940 a.subData(0, 0, 1, 1, black);
941 }
942
Joe Onoratoa8138d52009-10-06 19:25:30 -0700943 private void uploadAppIcon(int index, ApplicationInfo item) {
944 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -0700945 Element.RGBA_8888(mRS), true);
946 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
947
Joe Onoratoa8138d52009-10-06 19:25:30 -0700948 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -0700949 Element.RGBA_8888(mRS), true);
950 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -0700951
952 mIcons[index].uploadToTexture(0);
953 mLabels[index].uploadToTexture(0);
954
955 mIconIds[index] = mIcons[index].getID();
956 mLabelIds[index] = mLabels[index].getID();
957 }
958
959 /**
960 * Puts the empty spaces at the end. Updates mState.iconCount. You must
961 * fill in the values and call saveAppsList().
962 */
963 private void reallocAppsList(int count) {
964 Allocation[] icons = new Allocation[count];
965 int[] iconIds = new int[count];
966 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
967
968 Allocation[] labels = new Allocation[count];
969 int[] labelIds = new int[count];
970 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
971
972 final int oldCount = mIcons.length;
973
974 System.arraycopy(mIcons, 0, icons, 0, oldCount);
975 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
976 System.arraycopy(mLabels, 0, labels, 0, oldCount);
977 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
978
979 mIcons = icons;
980 mIconIds = iconIds;
981 mLabels = labels;
982 mLabelIds = labelIds;
983 }
984
985 /**
986 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
987 */
988 private void addApp(int index, ApplicationInfo item) {
989 final int count = mState.iconCount - index;
990 final int dest = index + 1;
991
992 System.arraycopy(mIcons, index, mIcons, dest, count);
993 System.arraycopy(mIconIds, index, mIconIds, dest, count);
994 System.arraycopy(mLabels, index, mLabels, dest, count);
995 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
996
997 uploadAppIcon(index, item);
998 }
999
1000 /**
1001 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1002 */
1003 private void removeApp(int index) {
1004 final int count = mState.iconCount - index - 1;
1005 final int src = index + 1;
1006
1007 System.arraycopy(mIcons, src, mIcons, index, count);
1008 System.arraycopy(mIconIds, src, mIconIds, index, count);
1009 System.arraycopy(mLabels, src, mLabels, index, count);
1010 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1011
1012 final int last = mState.iconCount - 1;
1013 mIcons[last] = null;
1014 mIconIds[last] = 0;
1015 mLabels[last] = null;
1016 mLabelIds[last] = 0;
1017 }
1018
1019 /**
1020 * Send the apps list structures to RS.
1021 */
1022 private void saveAppsList() {
1023 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001024
Joe Onoratoa8138d52009-10-06 19:25:30 -07001025 mAllocIconIds.data(mIconIds);
1026 mAllocLabelIds.data(mLabelIds);
1027
Jason Sams37e7c2b2009-10-19 12:55:43 -07001028 if (mScript != null) { // this happens when we init it
1029 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1030 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001031 }
1032
1033 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001034
1035 // Note: mScript may be null if we haven't initialized it yet.
1036 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001037 if (mInvokeResetWAR != null) {
1038 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001039 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001040 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001042
1043 void initTouchState() {
1044 int width = getWidth();
1045 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001046 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1047 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001048
Jason Sams37e7c2b2009-10-19 12:55:43 -07001049 int centerY = (height / 2);
1050 mTouchYBorders[0] = centerY - (cellHeight * 2);
1051 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001052 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001053 mTouchYBorders[3] = centerY + cellHeight;
1054 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001055
Joe Onorato6665c0f2009-09-02 15:27:24 -07001056 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001057 mTouchXBorders[0] = 0;
1058 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001059 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001060 mTouchXBorders[3] = centerX + (width / 4);
1061 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001062 }
1063
Joe Onorato664457d2009-10-28 16:30:34 -04001064 void fling() {
1065 mInvokeFling.execute();
1066 }
1067
1068 void move() {
1069 mInvokeMove.execute();
1070 }
1071
1072 void moveTo(float row) {
1073 mState.targetPos = row;
1074 mState.save();
1075 mInvokeMoveTo.execute();
1076 }
1077
Jason Sams37e7c2b2009-10-19 12:55:43 -07001078 int chooseTappedIcon(int x, int y, float pos) {
1079 // Adjust for scroll position if not zero.
1080 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001081
Joe Onorato6665c0f2009-09-02 15:27:24 -07001082 int col = -1;
1083 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001084 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1085 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1086 col = i;
1087 break;
1088 }
1089 }
1090 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1091 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1092 row = i;
1093 break;
1094 }
1095 }
1096
1097 if (row < 0 || col < 0) {
1098 return -1;
1099 }
1100
Joe Onorato664457d2009-10-28 16:30:34 -04001101 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001102 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001103
Joe Onorato664457d2009-10-28 16:30:34 -04001104 if (index >= mState.iconCount) {
1105 return -1;
1106 } else {
1107 return index;
1108 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001109 }
1110
Joe Onorato6665c0f2009-09-02 15:27:24 -07001111 /**
1112 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001113 *
1114 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001115 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001116 int selectIcon(int x, int y, float pos) {
1117 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001118 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001119 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001120 }
1121
1122 void selectIcon(int index) {
Jason Samsc8514792009-10-29 14:27:29 -07001123 if (index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001124 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001125 } else {
1126 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001127
1128 Bitmap selectionBitmap = mSelectionBitmap;
1129
1130 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1131 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1132 mAllAppsList.get(index).iconBitmap);
1133
1134 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001135 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001136 mSelectedIcon.uploadToTexture(0);
1137 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001138 }
1139 }
1140
1141 /**
1142 * You need to call save() on mState on your own after calling this.
1143 */
1144 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001145 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001146 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001147
Joe Onoratod63458b2009-10-15 21:19:09 -07001148 void setHomeSelected(boolean pressed) {
1149 if (pressed) {
1150 mState.homeButtonId = mHomeButtonPressed.getID();
1151 } else {
1152 mState.homeButtonId = mHomeButtonNormal.getID();
1153 }
1154 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001155 }
Joe Onorato93839052009-08-06 20:34:32 -07001156}
1157
1158