blob: 8d6820cfe9af8af6cad4078816c3cfb03b4db9e6 [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
Joe Onoratocb9f7982009-10-31 16:32:02 -040019import android.content.ComponentName;
Joe Onorato93839052009-08-06 20:34:32 -070020import android.content.Context;
21import android.content.res.Resources;
22import android.graphics.Bitmap;
Joe Onorato93839052009-08-06 20:34:32 -070023import android.graphics.Canvas;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080024import android.graphics.PixelFormat;
Mike Cleron7d5d7462009-10-20 14:06:00 -070025import android.graphics.Rect;
Joe Onoratod769a632009-08-11 17:09:02 -070026import android.os.SystemClock;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080027import android.renderscript.Allocation;
28import android.renderscript.Dimension;
29import android.renderscript.Element;
30import android.renderscript.ProgramFragment;
31import android.renderscript.ProgramStore;
32import android.renderscript.ProgramVertex;
33import android.renderscript.RSSurfaceView;
34import android.renderscript.RenderScript;
35import android.renderscript.Sampler;
36import android.renderscript.Script;
37import android.renderscript.ScriptC;
38import android.renderscript.SimpleMesh;
39import android.renderscript.Type;
Joe Onorato93839052009-08-06 20:34:32 -070040import android.util.AttributeSet;
41import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070042import android.view.KeyEvent;
43import android.view.MotionEvent;
Joe Onoratob39e51a2009-10-28 15:47:49 -040044import android.view.SoundEffectConstants;
Joe Onorato93839052009-08-06 20:34:32 -070045import android.view.SurfaceHolder;
Joe Onoratod769a632009-08-11 17:09:02 -070046import android.view.VelocityTracker;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080047import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070048import android.view.ViewConfiguration;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080049
50import java.util.ArrayList;
51import java.util.Collections;
52import java.util.Comparator;
Joe Onorato93839052009-08-06 20:34:32 -070053
54
Joe Onorato6665c0f2009-09-02 15:27:24 -070055public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070056 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040057 private static final String TAG = "Launcher.AllAppsView";
58
Joe Onoratofb0ca672009-09-14 17:55:46 -040059 /** Bit for mLocks for when there are icons being loaded. */
60 private static final int LOCK_ICONS_PENDING = 1;
61
Joe Onorato68ffd102009-10-15 17:59:43 -070062 private static final int TRACKING_NONE = 0;
63 private static final int TRACKING_FLING = 1;
64 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070065
Joe Onorato6665c0f2009-09-02 15:27:24 -070066 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070067 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040068
69 /** When this is 0, modifications are allowed, when it's not, they're not.
70 * TODO: What about scrolling? */
71 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070072
Joe Onorato82ca5502009-10-15 16:59:23 -070073 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070074 private int mMaxFlingVelocity;
75
Joe Onoratobcbeab82009-10-01 21:45:43 -070076 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070077 private RenderScript mRS;
78 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040079 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070080
Mike Cleron7d5d7462009-10-20 14:06:00 -070081 /**
82 * True when we are using arrow keys or trackball to drive navigation
83 */
84 private boolean mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -070085
Mike Cleron4a5c1e12009-11-03 10:17:05 -080086 /**
87 * Used to keep track of the selection when AllAppsView loses window focus
88 */
89 private int mLastSelectedIcon;
90
Joe Onorato6665c0f2009-09-02 15:27:24 -070091 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070092 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -070093 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -070094 private int mMotionDownRawX;
95 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -070096 private int mDownIconIndex = -1;
97 private int mCurrentIconIndex = -1;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080098
Joe Onorato1feb3a82009-08-08 22:32:00 -070099
Joe Onorato6665c0f2009-09-02 15:27:24 -0700100 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700101 public static final int ALLOC_PARAMS = 0;
102 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700103 public static final int ALLOC_ICON_IDS = 3;
104 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700105
106 public static final int COLUMNS_PER_PAGE = 4;
107 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700108
Joe Onorato6665c0f2009-09-02 15:27:24 -0700109 public static final int ICON_WIDTH_PX = 64;
110 public static final int ICON_TEXTURE_WIDTH_PX = 128;
111
112 public static final int ICON_HEIGHT_PX = 64;
113 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700114
115 public int SCREEN_WIDTH_PX;
116 public int SCREEN_HEIGHT_PX;
117
Joe Onoratobcbeab82009-10-01 21:45:43 -0700118 public void recompute(int w, int h) {
119 SCREEN_WIDTH_PX = 480;
120 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700121 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700122 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700123
124 public AllAppsView(Context context, AttributeSet attrs) {
125 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700126 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400127 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700128 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700129 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700130 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700131 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
132
Joe Onorato6665c0f2009-09-02 15:27:24 -0700133 setOnClickListener(this);
134 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700135 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700136 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700137 }
138
Joe Onoratob39e51a2009-10-28 15:47:49 -0400139 /**
140 * If you have an attached click listener, View always plays the click sound!?!?
141 * Deal with sound effects by hand.
142 */
143 public void reallyPlaySoundEffect(int sound) {
144 boolean old = isSoundEffectsEnabled();
145 setSoundEffectsEnabled(true);
146 playSoundEffect(sound);
147 setSoundEffectsEnabled(old);
148 }
149
Joe Onorato7c312c12009-08-13 21:36:53 -0700150 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
151 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700152 }
153
Joe Onorato6665c0f2009-09-02 15:27:24 -0700154 public void setLauncher(Launcher launcher) {
155 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700156 }
157
Joe Onorato1feb3a82009-08-08 22:32:00 -0700158 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700159 public void surfaceDestroyed(SurfaceHolder holder) {
160 super.surfaceDestroyed(holder);
Joe Onorato7bb17492009-09-24 17:51:01 -0700161 }
162
163 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700164 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500165 Log.d(TAG, "starting surfaceChanged");
Joe Onorato6665c0f2009-09-02 15:27:24 -0700166 long startTime = SystemClock.uptimeMillis();
167
Joe Onorato080d9b62009-11-02 12:01:11 -0500168 super.surfaceChanged(holder, format, w, h);
169
Jason Sams90396672009-11-03 13:59:34 -0800170 if (mRS == null) {
Jason Sams81134792009-10-27 15:38:42 -0700171
Jason Sams90396672009-11-03 13:59:34 -0800172 mRS = createRenderScript(true);
173 mRollo = new RolloRS();
174 mRollo.init(getResources(), w, h);
175 if (mAllAppsList != null) {
176 mRollo.setApps(mAllAppsList);
177 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
178 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400179 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700180
181 Resources res = getContext().getResources();
182 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700183
184 long endTime = SystemClock.uptimeMillis();
185 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700186 }
Jason Sams2e19c052009-10-20 18:19:55 -0700187
Joe Onorato93839052009-08-06 20:34:32 -0700188 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800189 public void onWindowFocusChanged(boolean hasWindowFocus) {
190 super.onWindowFocusChanged(hasWindowFocus);
191 if (mArrowNavigation) {
192 if (!hasWindowFocus) {
193 // Clear selection when we lose window focus
194 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
195 mRollo.clearSelectedIcon();
196 mRollo.mState.save();
197 } else if (hasWindowFocus) {
198 if (mRollo.mState.iconCount > 0) {
199 int selection = mLastSelectedIcon;
200 final int firstIcon = Math.round(mRollo.mMessageProc.mPosX) *
201 Defines.COLUMNS_PER_PAGE;
202 if (selection < 0 || // No selection
203 selection < firstIcon || // off the top of the screen
204 selection >= mRollo.mState.iconCount || // past last icon
205 selection >= firstIcon + // past last icon on screen
206 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
207 selection = firstIcon;
208 }
209
210 // Select the first icon when we gain window focus
211 mRollo.selectIcon(selection);
212 mRollo.mState.save();
213 }
214 }
215 }
216 }
217
218 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700219 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
220 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400221
222 if (!isVisible()) {
223 return;
224 }
225
Mike Cleron7d5d7462009-10-20 14:06:00 -0700226 if (gainFocus) {
227 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
228 // Select the first icon when we gain keyboard focus
229 mArrowNavigation = true;
230 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE);
231 mRollo.mState.save();
232 }
233 } else {
234 if (mArrowNavigation) {
235 // Clear selection when we lose focus
236 mRollo.clearSelectedIcon();
237 mRollo.mState.save();
238 mArrowNavigation = false;
239 }
240 }
241 }
242
243 @Override
244 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700245
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800246 boolean handled = false;
247
Joe Onorato859b3a72009-10-28 15:17:01 -0400248 if (!isVisible()) {
249 return false;
250 }
251
Joe Onoratoa13f5742009-11-02 17:15:19 -0500252 final int iconCount = mRollo.mState.iconCount;
253
Mike Cleron7d5d7462009-10-20 14:06:00 -0700254 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
255 if (mArrowNavigation) {
256 int whichApp = mRollo.mState.selectedIconIndex;
257 if (whichApp >= 0) {
258 ApplicationInfo app = mAllAppsList.get(whichApp);
259 mLauncher.startActivitySafely(app.intent);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800260 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700261 }
262 }
263 }
Jason Sams2e19c052009-10-20 18:19:55 -0700264
Joe Onoratoa13f5742009-11-02 17:15:19 -0500265 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700266 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700267
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800268 int currentSelection = mRollo.mState.selectedIconIndex;
269 int currentTopRow = Math.round(mRollo.mMessageProc.mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700270
Mike Cleron7d5d7462009-10-20 14:06:00 -0700271 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500272 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700273
Mike Cleron7d5d7462009-10-20 14:06:00 -0700274 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500275 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700276 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700277
Mike Cleron7d5d7462009-10-20 14:06:00 -0700278 int newSelection = currentSelection;
279
280 switch (keyCode) {
281 case KeyEvent.KEYCODE_DPAD_UP:
282 if (currentPageRow > 0) {
283 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
284 } else if (currentTopRow > 0) {
Jason Sams2e19c052009-10-20 18:19:55 -0700285 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800286 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700287 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800288 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700289 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800290
Joe Onoratoa13f5742009-11-02 17:15:19 -0500291 case KeyEvent.KEYCODE_DPAD_DOWN: {
292 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
293 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
294 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
295 if (currentRow < rowCount-1) {
296 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
297 if (newSelection >= iconCount) {
298 // Go from D to G in this arrangement:
299 // A B C D
300 // E F G
301 newSelection = iconCount - 1;
302 }
303 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800304 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
305 Defines.ROWS_PER_PAGE + 1);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700306 }
307 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800308 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700309 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500310 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700311 case KeyEvent.KEYCODE_DPAD_LEFT:
312 if (currentPageCol > 0) {
313 newSelection = currentSelection - 1;
314 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800315 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700316 break;
317 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700318 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500319 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700320 newSelection = currentSelection + 1;
321 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800322 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700323 break;
324 }
325 if (newSelection != currentSelection) {
326 mRollo.selectIcon(newSelection);
327 mRollo.mState.save();
328 }
329 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800330 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700331 }
332
Joe Onorato93839052009-08-06 20:34:32 -0700333 @Override
334 public boolean onTouchEvent(MotionEvent ev)
335 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700336 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700337
Joe Onorato7bb17492009-09-24 17:51:01 -0700338 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700339 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700340 }
341
Joe Onoratofb0ca672009-09-14 17:55:46 -0400342 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700343 return true;
344 }
345
346 super.onTouchEvent(ev);
347
Joe Onoratofb0ca672009-09-14 17:55:46 -0400348 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700349 int y = (int)ev.getY();
350
Joe Onoratobcbeab82009-10-01 21:45:43 -0700351 int action = ev.getAction();
352 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400353 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700354 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
355 mTouchTracking = TRACKING_HOME;
Joe Onoratod63458b2009-10-15 21:19:09 -0700356 mRollo.setHomeSelected(true);
357 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700358 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700359 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700360 mTouchTracking = TRACKING_FLING;
361
362 mMotionDownRawX = (int)ev.getRawX();
363 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700364
Mike Cleron7d5d7462009-10-20 14:06:00 -0700365 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700366 mRollo.mState.newTouchDown = 1;
367
368 if (!mRollo.checkClickOK()) {
369 mRollo.clearSelectedIcon();
370 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700371 mDownIconIndex = mCurrentIconIndex
372 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
373 if (mDownIconIndex < 0) {
374 // if nothing was selected, no long press.
375 cancelLongPress();
376 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700377 }
378 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700379 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700380 mVelocity = VelocityTracker.obtain();
381 mVelocity.addMovement(ev);
382 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700383 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400384 break;
385 case MotionEvent.ACTION_MOVE:
386 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700387 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratod63458b2009-10-15 21:19:09 -0700388 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]);
389 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700390 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700391 int rawX = (int)ev.getRawX();
392 int rawY = (int)ev.getRawY();
393 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700394 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700395
Joe Onorato82ca5502009-10-15 16:59:23 -0700396 if (!mStartedScrolling && slop < mSlop) {
397 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700398 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700399 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
400 if (mDownIconIndex != mCurrentIconIndex) {
401 // If a different icon is selected, don't allow it to be picked up.
402 // This handles off-axis dragging.
403 cancelLongPress();
404 mCurrentIconIndex = -1;
405 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700406 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700407 if (!mStartedScrolling) {
408 cancelLongPress();
409 mCurrentIconIndex = -1;
410 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700411 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700412 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700413 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700414
Joe Onoratobcbeab82009-10-01 21:45:43 -0700415 mStartedScrolling = true;
416 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700417 mVelocity.addMovement(ev);
418 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700419 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400420 }
421 break;
422 case MotionEvent.ACTION_UP:
423 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700424 if (mTouchTracking == TRACKING_HOME) {
425 if (action == MotionEvent.ACTION_UP) {
426 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400427 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700428 mLauncher.closeAllApps(true);
429 }
Joe Onoratod63458b2009-10-15 21:19:09 -0700430 mRollo.setHomeSelected(false);
431 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700432 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700433 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700434 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700435 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700436 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700437
Jason Sams12c14a82009-10-06 14:33:15 -0700438 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700439 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700440 mRollo.clearSelectedIcon();
441 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700442 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700443
Joe Onorato539ed9d2009-10-02 10:22:14 -0700444 if (mVelocity != null) {
445 mVelocity.recycle();
446 mVelocity = null;
447 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700448 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700449 mTouchTracking = TRACKING_NONE;
450 break;
Joe Onorato93839052009-08-06 20:34:32 -0700451 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700452
453 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700454 }
455
Joe Onorato6665c0f2009-09-02 15:27:24 -0700456 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700457 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400458 return;
459 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700460 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
461 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400462 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700463 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700464 mLauncher.startActivitySafely(app.intent);
465 }
466 }
467
468 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700469 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400470 return true;
471 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700472 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
473 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
474 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700475
476 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700477 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
478 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700479
Joe Onoratobcbeab82009-10-01 21:45:43 -0700480 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
481 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700482 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700483 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700484 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700485
Joe Onorato7bb17492009-09-24 17:51:01 -0700486 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700487 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700488 return true;
489 }
490
Joe Onorato5162ea92009-09-03 09:39:42 -0700491 public void setDragController(DragController dragger) {
492 mDragController = dragger;
493 }
494
495 public void onDropCompleted(View target, boolean success) {
496 }
497
Joe Onorato4db52312009-10-06 11:17:43 -0700498 /**
499 * Zoom to the specifed amount.
500 *
501 * @param amount [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700502 */
Jason Sams12c14a82009-10-06 14:33:15 -0700503 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700504 if (mRollo == null) {
505 return;
506 }
507
Joe Onoratofb0ca672009-09-14 17:55:46 -0400508 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400509 mRollo.clearSelectedIcon();
Joe Onorato1d708e72009-10-15 21:29:21 -0700510 mRollo.setHomeSelected(false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700511 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700512 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700513 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700514 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700515 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700516 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400517 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400518 }
519
520 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700521 if (mRollo == null) {
522 return false;
523 }
Jason Sams12c14a82009-10-06 14:33:15 -0700524 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400525 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700526
Mike Cleronb6082fa02009-10-19 17:03:36 -0700527 /*
Joe Onorato93839052009-08-06 20:34:32 -0700528 @Override
529 public boolean onTrackballEvent(MotionEvent ev)
530 {
531 float x = ev.getX();
532 float y = ev.getY();
533 //Float tx = new Float(x);
534 //Float ty = new Float(y);
535 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
536
537
538 return true;
539 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700540 */
Joe Onorato93839052009-08-06 20:34:32 -0700541
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542 public void setApps(ArrayList<ApplicationInfo> list) {
543 mAllAppsList = list;
544 if (mRollo != null) {
545 mRollo.setApps(list);
546 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400547 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700548 }
549
Joe Onoratoa8138d52009-10-06 19:25:30 -0700550 public void addApps(ArrayList<ApplicationInfo> list) {
551 final int N = list.size();
552 if (mRollo != null) {
553 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
554 }
555
556 for (int i=0; i<N; i++) {
557 final ApplicationInfo item = list.get(i);
558 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
559 if (index < 0) {
560 index = -(index+1);
561 }
562 mAllAppsList.add(index, item);
563 if (mRollo != null) {
564 mRollo.addApp(index, item);
565 mRollo.mState.iconCount++;
566 }
567 }
568
569 if (mRollo != null) {
570 mRollo.saveAppsList();
571 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700572 }
573
Joe Onoratoa8138d52009-10-06 19:25:30 -0700574 public void removeApps(ArrayList<ApplicationInfo> list) {
575 final int N = list.size();
576 for (int i=0; i<N; i++) {
577 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400578 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700579 if (index >= 0) {
580 mAllAppsList.remove(index);
581 if (mRollo != null) {
582 mRollo.removeApp(index);
583 mRollo.mState.iconCount--;
584 }
585 } else {
586 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
587 // Try to recover. This should keep us from crashing for now.
588 }
589 }
590
591 if (mRollo != null) {
592 mRollo.saveAppsList();
593 }
594 }
595
596 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
597 // Just remove and add, because they may need to be re-sorted.
598 removeApps(list);
599 addApps(list);
600 }
601
602 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
603 public int compare(ApplicationInfo a, ApplicationInfo b) {
604 int result = a.title.toString().compareTo(b.toString());
605 if (result != 0) {
606 return result;
607 }
608 return a.intent.getComponent().compareTo(b.intent.getComponent());
609 }
610 };
611
Joe Onoratocb9f7982009-10-31 16:32:02 -0400612 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
613 ComponentName component = item.intent.getComponent();
614 final int N = list.size();
615 for (int i=0; i<N; i++) {
616 ApplicationInfo x = list.get(i);
617 if (x.intent.getComponent().equals(component)) {
618 return i;
619 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700620 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400621 return -1;
622 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700623
Joe Onoratoc567acb2009-08-31 14:34:43 -0700624 private static int countPages(int iconCount) {
625 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
626 int pages = iconCount / iconsPerPage;
627 if (pages*iconsPerPage != iconCount) {
628 pages++;
629 }
630 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 }
632
Joe Onorato93839052009-08-06 20:34:32 -0700633 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700634
Joe Onorato1feb3a82009-08-08 22:32:00 -0700635 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700636 private int mWidth;
637 private int mHeight;
638
639 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700640 private Script mScript;
641 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700642 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700643 private Script.Invokable mInvokeFling;
644 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700645
Jason Samsc1c521e2009-10-19 14:45:45 -0700646
Jason Samscd689e12009-09-29 15:28:22 -0700647 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700648 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700649 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700650 private ProgramFragment mPFTexMip;
651 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700652 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700653 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700654 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700655 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700656
Joe Onoratod63458b2009-10-15 21:19:09 -0700657 private Allocation mHomeButtonNormal;
658 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700659
Joe Onoratobf15cb42009-08-07 14:33:40 -0700660 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700661 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700662 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700663
Joe Onoratobf15cb42009-08-07 14:33:40 -0700664 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700665 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700666 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700667 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700668
Joe Onorato6665c0f2009-09-02 15:27:24 -0700669 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700670 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700671
672 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400673 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700674
Jason Samsd8152b92009-10-13 17:19:10 -0700675
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700676 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700677 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700678
Jason Sams78aebd82009-09-15 13:06:59 -0700679 class BaseAlloc {
680 Allocation mAlloc;
681 Type mType;
682
683 void save() {
684 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700685 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700686 }
687
Jason Sams12c14a82009-10-06 14:33:15 -0700688 class AAMessage extends RenderScript.RSMessage {
689 public void run() {
690 mPosX = ((float)mData[0]) / (1 << 16);
691 mVelocity = ((float)mData[1]) / (1 << 16);
692 mZoom = ((float)mData[2]) / (1 << 16);
693 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
694 }
695 float mZoom;
696 float mPosX;
697 float mVelocity;
698 }
699 AAMessage mMessageProc;
700
Jason Sams476339d2009-09-29 18:14:38 -0700701 private boolean checkClickOK() {
702 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams2e19c052009-10-20 18:19:55 -0700703 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
704 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700705 }
706
Jason Sams78aebd82009-09-15 13:06:59 -0700707 class Params extends BaseAlloc {
708 Params() {
709 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
710 mAlloc = Allocation.createTyped(mRS, mType);
711 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700712 }
Jason Sams78aebd82009-09-15 13:06:59 -0700713 public int bubbleWidth;
714 public int bubbleHeight;
715 public int bubbleBitmapWidth;
716 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700717
Joe Onoratobcbeab82009-10-01 21:45:43 -0700718 public int homeButtonWidth;
719 public int homeButtonHeight;
720 public int homeButtonTextureWidth;
721 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700722 }
723
724 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700725 public float newPositionX;
726 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700727 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700728 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700729 public int selectedIconIndex = -1;
730 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700731 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700732 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700733 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700734
735 State() {
736 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
737 mAlloc = Allocation.createTyped(mRS, mType);
738 save();
739 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700740 }
741
742 public RolloRS() {
743 }
744
745 public void init(Resources res, int width, int height) {
746 mRes = res;
747 mWidth = width;
748 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700749 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700750 initProgramVertex();
751 initProgramFragment();
752 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700753 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700754 initGl();
755 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700756 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700757 initRs();
758 }
759
Jason Sams0aa71662009-10-02 18:43:18 -0700760 public void initMesh() {
761 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
762 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
763
Jason Samsd8152b92009-10-13 17:19:10 -0700764 float y = 0;
765 float z = 0;
766 for (int ct=0; ct < 200; ct++) {
767 float angle = 0;
768 float maxAngle = 3.14f * 0.16f;
769 float l = 1.f;
770
771 l = 1 - ((ct-5) * 0.10f);
772 if (ct > 7) {
773 angle = maxAngle * (ct - 7) * 0.2f;
774 angle = Math.min(angle, maxAngle);
775 }
Jason Samsc8514792009-10-29 14:27:29 -0700776 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700777 l = Math.min(1.0f, l);
778
779 y += 0.1f * Math.cos(angle);
780 z += 0.1f * Math.sin(angle);
781
782 float t = 0.1f * ct;
783 float ds = 0.08f;
784 tm.setColor(l, l, l, 0.99f);
785 tm.setTexture(ds, t);
786 tm.addVertex(-0.5f, y, z);
787 tm.setTexture(1 - ds, t);
788 tm.addVertex(0.5f, y, z);
789 }
790 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
791 tm.addTriangle(ct, ct+1, ct+2);
792 tm.addTriangle(ct+1, ct+3, ct+2);
793 }
794 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700795 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700796 }
797
Jason Samscd689e12009-09-29 15:28:22 -0700798 private void initProgramVertex() {
799 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
800 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700801
802 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700803 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700804 mPV = pvb.create();
805 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700806 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700807
Jason Sams37e7c2b2009-10-19 12:55:43 -0700808 //pva = new ProgramVertex.MatrixAllocation(mRS);
809 //pva.setupOrthoWindow(mWidth, mHeight);
810 //pvb.setTextureMatrixEnable(true);
811 //mPVOrtho = pvb.create();
812 //mPVOrtho.setName("PVOrtho");
813 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700814
815 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700816 }
Joe Onorato93839052009-08-06 20:34:32 -0700817
Jason Samscd689e12009-09-29 15:28:22 -0700818 private void initProgramFragment() {
819 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700820 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700821 sb.setMag(Sampler.Value.LINEAR);
822 sb.setWrapS(Sampler.Value.CLAMP);
823 sb.setWrapT(Sampler.Value.CLAMP);
824 Sampler linear = sb.create();
825
826 sb.setMin(Sampler.Value.NEAREST);
827 sb.setMag(Sampler.Value.NEAREST);
828 Sampler nearest = sb.create();
829
830 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700831 //mPFColor = bf.create();
832 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700833
834 bf.setTexEnable(true, 0);
835 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700836 mPFTexMip = bf.create();
837 mPFTexMip.setName("PFTexMip");
838 mPFTexMip.bindSampler(linear, 0);
839
840 mPFTexNearest = bf.create();
841 mPFTexNearest.setName("PFTexNearest");
842 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700843 }
844
845 private void initProgramStore() {
846 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
847 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700848 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700849 bs.setDitherEnable(true);
850 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
851 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
852 mPSIcons = bs.create();
853 mPSIcons.setName("PSIcons");
854
855 //bs.setDitherEnable(false);
856 //mPSText = bs.create();
857 //mPSText.setName("PSText");
858 }
859
860 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700861 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700862 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700863 }
Jason Sams78aebd82009-09-15 13:06:59 -0700864
Joe Onorato1feb3a82009-08-08 22:32:00 -0700865 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700866 mParams = new Params();
867 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700868
Joe Onoratobf15cb42009-08-07 14:33:40 -0700869 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700870
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700871 mParams.bubbleWidth = bubble.getBubbleWidth();
872 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
873 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
874 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700875
Joe Onoratod63458b2009-10-15 21:19:09 -0700876 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
877 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
878 mHomeButtonNormal.uploadToTexture(0);
879 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
880 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
881 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700882 mParams.homeButtonWidth = 76;
883 mParams.homeButtonHeight = 68;
884 mParams.homeButtonTextureWidth = 128;
885 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700886
Joe Onoratod63458b2009-10-15 21:19:09 -0700887 mState.homeButtonId = mHomeButtonNormal.getID();
888
Joe Onorato1feb3a82009-08-08 22:32:00 -0700889 mParams.save();
890 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400891
Joe Onorato1291a8c2009-09-15 15:07:25 -0400892 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
893 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
894 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700895
Joe Onorato9c1289c2009-08-17 11:03:03 -0400896 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700897 }
898
Jason Sams37e7c2b2009-10-19 12:55:43 -0700899 private void initScript(int id) {
900 }
901
902 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700903 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700904 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700905 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700906 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700907 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
908 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700909 mInvokeMove = sb.addInvokable("move");
910 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700911 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700912 mInvokeResetWAR = sb.addInvokable("resetHWWar");
913 mScript = sb.create();
914 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
915 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
916 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
917 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
918 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700919
Jason Sams12c14a82009-10-06 14:33:15 -0700920 mMessageProc = new AAMessage();
921 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700922 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700923 }
Joe Onorato93839052009-08-06 20:34:32 -0700924
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925 private void setApps(ArrayList<ApplicationInfo> list) {
926 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700927 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700928 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700929 allocCount = 1;
930 }
931
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700933 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700934 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400935
936 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700937 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700938 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700940 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941
942 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
943
944 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700945 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946 }
947
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 mState.iconCount = count;
949
Joe Onoratoa8138d52009-10-06 19:25:30 -0700950 saveAppsList();
951 }
952
Jason Samsc8514792009-10-29 14:27:29 -0700953 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
954 int black[] = new int[w > h ? w : h];
955 Allocation.Adapter2D a = alloc.createAdapter2D();
956 int mip = 0;
957 while (w > 1 || h > 1) {
958 a.subData(0, 0, 1, h, black);
959 a.subData(w-1, 0, 1, h, black);
960 a.subData(0, 0, w, 1, black);
961 a.subData(0, h-1, w, 1, black);
962 mip++;
963 w = (w + 1) >> 1;
964 h = (h + 1) >> 1;
965 a.setConstraint(Dimension.LOD, mip);
966 }
967 a.subData(0, 0, 1, 1, black);
968 }
969
Joe Onoratoa8138d52009-10-06 19:25:30 -0700970 private void uploadAppIcon(int index, ApplicationInfo item) {
971 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -0700972 Element.RGBA_8888(mRS), true);
973 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
974
Joe Onoratoa8138d52009-10-06 19:25:30 -0700975 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -0700976 Element.RGBA_8888(mRS), true);
977 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -0700978
979 mIcons[index].uploadToTexture(0);
980 mLabels[index].uploadToTexture(0);
981
982 mIconIds[index] = mIcons[index].getID();
983 mLabelIds[index] = mLabels[index].getID();
984 }
985
986 /**
987 * Puts the empty spaces at the end. Updates mState.iconCount. You must
988 * fill in the values and call saveAppsList().
989 */
990 private void reallocAppsList(int count) {
991 Allocation[] icons = new Allocation[count];
992 int[] iconIds = new int[count];
993 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
994
995 Allocation[] labels = new Allocation[count];
996 int[] labelIds = new int[count];
997 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
998
999 final int oldCount = mIcons.length;
1000
1001 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1002 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1003 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1004 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1005
1006 mIcons = icons;
1007 mIconIds = iconIds;
1008 mLabels = labels;
1009 mLabelIds = labelIds;
1010 }
1011
1012 /**
1013 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1014 */
1015 private void addApp(int index, ApplicationInfo item) {
1016 final int count = mState.iconCount - index;
1017 final int dest = index + 1;
1018
1019 System.arraycopy(mIcons, index, mIcons, dest, count);
1020 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1021 System.arraycopy(mLabels, index, mLabels, dest, count);
1022 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1023
1024 uploadAppIcon(index, item);
1025 }
1026
1027 /**
1028 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1029 */
1030 private void removeApp(int index) {
1031 final int count = mState.iconCount - index - 1;
1032 final int src = index + 1;
1033
1034 System.arraycopy(mIcons, src, mIcons, index, count);
1035 System.arraycopy(mIconIds, src, mIconIds, index, count);
1036 System.arraycopy(mLabels, src, mLabels, index, count);
1037 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1038
1039 final int last = mState.iconCount - 1;
1040 mIcons[last] = null;
1041 mIconIds[last] = 0;
1042 mLabels[last] = null;
1043 mLabelIds[last] = 0;
1044 }
1045
1046 /**
1047 * Send the apps list structures to RS.
1048 */
1049 private void saveAppsList() {
1050 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001051
Joe Onoratoa8138d52009-10-06 19:25:30 -07001052 mAllocIconIds.data(mIconIds);
1053 mAllocLabelIds.data(mLabelIds);
1054
Jason Sams37e7c2b2009-10-19 12:55:43 -07001055 if (mScript != null) { // this happens when we init it
1056 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1057 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001058 }
1059
1060 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001061
1062 // Note: mScript may be null if we haven't initialized it yet.
1063 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001064 if (mInvokeResetWAR != null) {
1065 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001066 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001067 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001068 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001069
1070 void initTouchState() {
1071 int width = getWidth();
1072 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001073 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1074 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001075
Jason Sams37e7c2b2009-10-19 12:55:43 -07001076 int centerY = (height / 2);
1077 mTouchYBorders[0] = centerY - (cellHeight * 2);
1078 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001079 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001080 mTouchYBorders[3] = centerY + cellHeight;
1081 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001082
Joe Onorato6665c0f2009-09-02 15:27:24 -07001083 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001084 mTouchXBorders[0] = 0;
1085 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001086 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001087 mTouchXBorders[3] = centerX + (width / 4);
1088 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001089 }
1090
Joe Onorato664457d2009-10-28 16:30:34 -04001091 void fling() {
1092 mInvokeFling.execute();
1093 }
1094
1095 void move() {
1096 mInvokeMove.execute();
1097 }
1098
1099 void moveTo(float row) {
1100 mState.targetPos = row;
1101 mState.save();
1102 mInvokeMoveTo.execute();
1103 }
1104
Jason Sams37e7c2b2009-10-19 12:55:43 -07001105 int chooseTappedIcon(int x, int y, float pos) {
1106 // Adjust for scroll position if not zero.
1107 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001108
Joe Onorato6665c0f2009-09-02 15:27:24 -07001109 int col = -1;
1110 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001111 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1112 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1113 col = i;
1114 break;
1115 }
1116 }
1117 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1118 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1119 row = i;
1120 break;
1121 }
1122 }
1123
1124 if (row < 0 || col < 0) {
1125 return -1;
1126 }
1127
Joe Onorato664457d2009-10-28 16:30:34 -04001128 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001129 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001130
Joe Onorato664457d2009-10-28 16:30:34 -04001131 if (index >= mState.iconCount) {
1132 return -1;
1133 } else {
1134 return index;
1135 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001136 }
1137
Joe Onorato6665c0f2009-09-02 15:27:24 -07001138 /**
1139 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001140 *
1141 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001142 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001143 int selectIcon(int x, int y, float pos) {
1144 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001145 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001146 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001147 }
1148
1149 void selectIcon(int index) {
Jason Samsc8514792009-10-29 14:27:29 -07001150 if (index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001151 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001152 } else {
1153 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001154
1155 Bitmap selectionBitmap = mSelectionBitmap;
1156
1157 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1158 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1159 mAllAppsList.get(index).iconBitmap);
1160
1161 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001162 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001163 mSelectedIcon.uploadToTexture(0);
1164 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001165 }
1166 }
1167
1168 /**
1169 * You need to call save() on mState on your own after calling this.
1170 */
1171 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001172 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001173 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001174
Joe Onoratod63458b2009-10-15 21:19:09 -07001175 void setHomeSelected(boolean pressed) {
1176 if (pressed) {
1177 mState.homeButtonId = mHomeButtonPressed.getID();
1178 } else {
1179 mState.homeButtonId = mHomeButtonNormal.getID();
1180 }
1181 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001182 }
Joe Onorato93839052009-08-06 20:34:32 -07001183}
1184
1185