blob: 89d11d1ddcf9bb05f5391533a32822ab5845235b [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);
161
162 destroyRenderScript();
163 mRS = null;
164 mRollo = null;
165 }
166
167 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700168 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500169 Log.d(TAG, "starting surfaceChanged");
Joe Onorato6665c0f2009-09-02 15:27:24 -0700170 long startTime = SystemClock.uptimeMillis();
171
Joe Onorato080d9b62009-11-02 12:01:11 -0500172 super.surfaceChanged(holder, format, w, h);
173
Jason Sams81134792009-10-27 15:38:42 -0700174 if (mRS != null) {
175 destroyRenderScript();
176 mRS = null;
177 mRollo = null;
178 }
179
Jason Sams05de32a2009-09-27 14:01:40 -0700180 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700181 mRollo = new RolloRS();
182 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 if (mAllAppsList != null) {
184 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700185 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700187
188 Resources res = getContext().getResources();
189 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700190
191 long endTime = SystemClock.uptimeMillis();
192 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700193 }
Jason Sams2e19c052009-10-20 18:19:55 -0700194
Joe Onorato93839052009-08-06 20:34:32 -0700195 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800196 public void onWindowFocusChanged(boolean hasWindowFocus) {
197 super.onWindowFocusChanged(hasWindowFocus);
198 if (mArrowNavigation) {
199 if (!hasWindowFocus) {
200 // Clear selection when we lose window focus
201 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
202 mRollo.clearSelectedIcon();
203 mRollo.mState.save();
204 } else if (hasWindowFocus) {
205 if (mRollo.mState.iconCount > 0) {
206 int selection = mLastSelectedIcon;
207 final int firstIcon = Math.round(mRollo.mMessageProc.mPosX) *
208 Defines.COLUMNS_PER_PAGE;
209 if (selection < 0 || // No selection
210 selection < firstIcon || // off the top of the screen
211 selection >= mRollo.mState.iconCount || // past last icon
212 selection >= firstIcon + // past last icon on screen
213 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
214 selection = firstIcon;
215 }
216
217 // Select the first icon when we gain window focus
218 mRollo.selectIcon(selection);
219 mRollo.mState.save();
220 }
221 }
222 }
223 }
224
225 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700226 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
227 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400228
229 if (!isVisible()) {
230 return;
231 }
232
Mike Cleron7d5d7462009-10-20 14:06:00 -0700233 if (gainFocus) {
234 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
235 // Select the first icon when we gain keyboard focus
236 mArrowNavigation = true;
237 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE);
238 mRollo.mState.save();
239 }
240 } else {
241 if (mArrowNavigation) {
242 // Clear selection when we lose focus
243 mRollo.clearSelectedIcon();
244 mRollo.mState.save();
245 mArrowNavigation = false;
246 }
247 }
248 }
249
250 @Override
251 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700252
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800253 boolean handled = false;
254
Joe Onorato859b3a72009-10-28 15:17:01 -0400255 if (!isVisible()) {
256 return false;
257 }
258
Joe Onoratoa13f5742009-11-02 17:15:19 -0500259 final int iconCount = mRollo.mState.iconCount;
260
Mike Cleron7d5d7462009-10-20 14:06:00 -0700261 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
262 if (mArrowNavigation) {
263 int whichApp = mRollo.mState.selectedIconIndex;
264 if (whichApp >= 0) {
265 ApplicationInfo app = mAllAppsList.get(whichApp);
266 mLauncher.startActivitySafely(app.intent);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800267 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700268 }
269 }
270 }
Jason Sams2e19c052009-10-20 18:19:55 -0700271
Joe Onoratoa13f5742009-11-02 17:15:19 -0500272 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700273 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700274
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800275 int currentSelection = mRollo.mState.selectedIconIndex;
276 int currentTopRow = Math.round(mRollo.mMessageProc.mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700277
Mike Cleron7d5d7462009-10-20 14:06:00 -0700278 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500279 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700280
Mike Cleron7d5d7462009-10-20 14:06:00 -0700281 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500282 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700283 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700284
Mike Cleron7d5d7462009-10-20 14:06:00 -0700285 int newSelection = currentSelection;
286
287 switch (keyCode) {
288 case KeyEvent.KEYCODE_DPAD_UP:
289 if (currentPageRow > 0) {
290 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
291 } else if (currentTopRow > 0) {
Jason Sams2e19c052009-10-20 18:19:55 -0700292 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800293 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700294 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800295 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700296 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800297
Joe Onoratoa13f5742009-11-02 17:15:19 -0500298 case KeyEvent.KEYCODE_DPAD_DOWN: {
299 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
300 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
301 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
302 if (currentRow < rowCount-1) {
303 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
304 if (newSelection >= iconCount) {
305 // Go from D to G in this arrangement:
306 // A B C D
307 // E F G
308 newSelection = iconCount - 1;
309 }
310 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800311 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
312 Defines.ROWS_PER_PAGE + 1);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700313 }
314 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800315 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700316 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500317 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700318 case KeyEvent.KEYCODE_DPAD_LEFT:
319 if (currentPageCol > 0) {
320 newSelection = currentSelection - 1;
321 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800322 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700323 break;
324 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700325 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500326 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700327 newSelection = currentSelection + 1;
328 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800329 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700330 break;
331 }
332 if (newSelection != currentSelection) {
333 mRollo.selectIcon(newSelection);
334 mRollo.mState.save();
335 }
336 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800337 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700338 }
339
Joe Onorato93839052009-08-06 20:34:32 -0700340 @Override
341 public boolean onTouchEvent(MotionEvent ev)
342 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700343 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700344
Joe Onorato7bb17492009-09-24 17:51:01 -0700345 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700346 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700347 }
348
Joe Onoratofb0ca672009-09-14 17:55:46 -0400349 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700350 return true;
351 }
352
353 super.onTouchEvent(ev);
354
Joe Onoratofb0ca672009-09-14 17:55:46 -0400355 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700356 int y = (int)ev.getY();
357
Joe Onoratobcbeab82009-10-01 21:45:43 -0700358 int action = ev.getAction();
359 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400360 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700361 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
362 mTouchTracking = TRACKING_HOME;
Joe Onoratod63458b2009-10-15 21:19:09 -0700363 mRollo.setHomeSelected(true);
364 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700365 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700366 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700367 mTouchTracking = TRACKING_FLING;
368
369 mMotionDownRawX = (int)ev.getRawX();
370 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700371
Mike Cleron7d5d7462009-10-20 14:06:00 -0700372 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700373 mRollo.mState.newTouchDown = 1;
374
375 if (!mRollo.checkClickOK()) {
376 mRollo.clearSelectedIcon();
377 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700378 mDownIconIndex = mCurrentIconIndex
379 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
380 if (mDownIconIndex < 0) {
381 // if nothing was selected, no long press.
382 cancelLongPress();
383 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700384 }
385 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700386 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700387 mVelocity = VelocityTracker.obtain();
388 mVelocity.addMovement(ev);
389 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700390 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400391 break;
392 case MotionEvent.ACTION_MOVE:
393 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700394 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratod63458b2009-10-15 21:19:09 -0700395 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]);
396 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700397 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700398 int rawX = (int)ev.getRawX();
399 int rawY = (int)ev.getRawY();
400 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700401 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700402
Joe Onorato82ca5502009-10-15 16:59:23 -0700403 if (!mStartedScrolling && slop < mSlop) {
404 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700405 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700406 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
407 if (mDownIconIndex != mCurrentIconIndex) {
408 // If a different icon is selected, don't allow it to be picked up.
409 // This handles off-axis dragging.
410 cancelLongPress();
411 mCurrentIconIndex = -1;
412 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700413 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700414 if (!mStartedScrolling) {
415 cancelLongPress();
416 mCurrentIconIndex = -1;
417 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700418 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700419 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700420 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700421
Joe Onoratobcbeab82009-10-01 21:45:43 -0700422 mStartedScrolling = true;
423 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700424 mVelocity.addMovement(ev);
425 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700426 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400427 }
428 break;
429 case MotionEvent.ACTION_UP:
430 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700431 if (mTouchTracking == TRACKING_HOME) {
432 if (action == MotionEvent.ACTION_UP) {
433 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400434 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700435 mLauncher.closeAllApps(true);
436 }
Joe Onoratod63458b2009-10-15 21:19:09 -0700437 mRollo.setHomeSelected(false);
438 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700439 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700440 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700441 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700442 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700443 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700444
Jason Sams12c14a82009-10-06 14:33:15 -0700445 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700446 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700447 mRollo.clearSelectedIcon();
448 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700449 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700450
Joe Onorato539ed9d2009-10-02 10:22:14 -0700451 if (mVelocity != null) {
452 mVelocity.recycle();
453 mVelocity = null;
454 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700455 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700456 mTouchTracking = TRACKING_NONE;
457 break;
Joe Onorato93839052009-08-06 20:34:32 -0700458 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700459
460 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700461 }
462
Joe Onorato6665c0f2009-09-02 15:27:24 -0700463 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700464 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400465 return;
466 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700467 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
468 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400469 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700470 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700471 mLauncher.startActivitySafely(app.intent);
472 }
473 }
474
475 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700476 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400477 return true;
478 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700479 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
480 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
481 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700482
483 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700484 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
485 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700486
Joe Onoratobcbeab82009-10-01 21:45:43 -0700487 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
488 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700489 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700490 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700491 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700492
Joe Onorato7bb17492009-09-24 17:51:01 -0700493 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700494 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700495 return true;
496 }
497
Joe Onorato5162ea92009-09-03 09:39:42 -0700498 public void setDragController(DragController dragger) {
499 mDragController = dragger;
500 }
501
502 public void onDropCompleted(View target, boolean success) {
503 }
504
Joe Onorato4db52312009-10-06 11:17:43 -0700505 /**
506 * Zoom to the specifed amount.
507 *
508 * @param amount [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700509 */
Jason Sams12c14a82009-10-06 14:33:15 -0700510 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700511 if (mRollo == null) {
512 return;
513 }
514
Joe Onoratofb0ca672009-09-14 17:55:46 -0400515 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400516 mRollo.clearSelectedIcon();
Joe Onorato1d708e72009-10-15 21:29:21 -0700517 mRollo.setHomeSelected(false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700518 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700519 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700520 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700521 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700522 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700523 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400524 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400525 }
526
527 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700528 if (mRollo == null) {
529 return false;
530 }
Jason Sams12c14a82009-10-06 14:33:15 -0700531 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400532 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700533
Mike Cleronb6082fa02009-10-19 17:03:36 -0700534 /*
Joe Onorato93839052009-08-06 20:34:32 -0700535 @Override
536 public boolean onTrackballEvent(MotionEvent ev)
537 {
538 float x = ev.getX();
539 float y = ev.getY();
540 //Float tx = new Float(x);
541 //Float ty = new Float(y);
542 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
543
544
545 return true;
546 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700547 */
Joe Onorato93839052009-08-06 20:34:32 -0700548
Joe Onorato9c1289c2009-08-17 11:03:03 -0400549 public void setApps(ArrayList<ApplicationInfo> list) {
550 mAllAppsList = list;
551 if (mRollo != null) {
552 mRollo.setApps(list);
553 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400554 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700555 }
556
Joe Onoratoa8138d52009-10-06 19:25:30 -0700557 public void addApps(ArrayList<ApplicationInfo> list) {
558 final int N = list.size();
559 if (mRollo != null) {
560 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
561 }
562
563 for (int i=0; i<N; i++) {
564 final ApplicationInfo item = list.get(i);
565 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
566 if (index < 0) {
567 index = -(index+1);
568 }
569 mAllAppsList.add(index, item);
570 if (mRollo != null) {
571 mRollo.addApp(index, item);
572 mRollo.mState.iconCount++;
573 }
574 }
575
576 if (mRollo != null) {
577 mRollo.saveAppsList();
578 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700579 }
580
Joe Onoratoa8138d52009-10-06 19:25:30 -0700581 public void removeApps(ArrayList<ApplicationInfo> list) {
582 final int N = list.size();
583 for (int i=0; i<N; i++) {
584 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400585 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700586 if (index >= 0) {
587 mAllAppsList.remove(index);
588 if (mRollo != null) {
589 mRollo.removeApp(index);
590 mRollo.mState.iconCount--;
591 }
592 } else {
593 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
594 // Try to recover. This should keep us from crashing for now.
595 }
596 }
597
598 if (mRollo != null) {
599 mRollo.saveAppsList();
600 }
601 }
602
603 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
604 // Just remove and add, because they may need to be re-sorted.
605 removeApps(list);
606 addApps(list);
607 }
608
609 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
610 public int compare(ApplicationInfo a, ApplicationInfo b) {
611 int result = a.title.toString().compareTo(b.toString());
612 if (result != 0) {
613 return result;
614 }
615 return a.intent.getComponent().compareTo(b.intent.getComponent());
616 }
617 };
618
Joe Onoratocb9f7982009-10-31 16:32:02 -0400619 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
620 ComponentName component = item.intent.getComponent();
621 final int N = list.size();
622 for (int i=0; i<N; i++) {
623 ApplicationInfo x = list.get(i);
624 if (x.intent.getComponent().equals(component)) {
625 return i;
626 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700627 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400628 return -1;
629 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700630
Joe Onoratoc567acb2009-08-31 14:34:43 -0700631 private static int countPages(int iconCount) {
632 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
633 int pages = iconCount / iconsPerPage;
634 if (pages*iconsPerPage != iconCount) {
635 pages++;
636 }
637 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 }
639
Joe Onorato93839052009-08-06 20:34:32 -0700640 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700641
Joe Onorato1feb3a82009-08-08 22:32:00 -0700642 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700643 private int mWidth;
644 private int mHeight;
645
646 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700647 private Script mScript;
648 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700649 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700650 private Script.Invokable mInvokeFling;
651 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700652
Jason Samsc1c521e2009-10-19 14:45:45 -0700653
Jason Samscd689e12009-09-29 15:28:22 -0700654 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700655 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700656 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700657 private ProgramFragment mPFTexMip;
658 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700659 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700660 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700661 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700662 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700663
Joe Onoratod63458b2009-10-15 21:19:09 -0700664 private Allocation mHomeButtonNormal;
665 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700666
Joe Onoratobf15cb42009-08-07 14:33:40 -0700667 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700668 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700669 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700670
Joe Onoratobf15cb42009-08-07 14:33:40 -0700671 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700672 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700673 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700674 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700675
Joe Onorato6665c0f2009-09-02 15:27:24 -0700676 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700677 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700678
679 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400680 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700681
Jason Samsd8152b92009-10-13 17:19:10 -0700682
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700683 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700684 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700685
Jason Sams78aebd82009-09-15 13:06:59 -0700686 class BaseAlloc {
687 Allocation mAlloc;
688 Type mType;
689
690 void save() {
691 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700692 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700693 }
694
Jason Sams12c14a82009-10-06 14:33:15 -0700695 class AAMessage extends RenderScript.RSMessage {
696 public void run() {
697 mPosX = ((float)mData[0]) / (1 << 16);
698 mVelocity = ((float)mData[1]) / (1 << 16);
699 mZoom = ((float)mData[2]) / (1 << 16);
700 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
701 }
702 float mZoom;
703 float mPosX;
704 float mVelocity;
705 }
706 AAMessage mMessageProc;
707
Jason Sams476339d2009-09-29 18:14:38 -0700708 private boolean checkClickOK() {
709 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams2e19c052009-10-20 18:19:55 -0700710 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
711 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700712 }
713
Jason Sams78aebd82009-09-15 13:06:59 -0700714 class Params extends BaseAlloc {
715 Params() {
716 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
717 mAlloc = Allocation.createTyped(mRS, mType);
718 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700719 }
Jason Sams78aebd82009-09-15 13:06:59 -0700720 public int bubbleWidth;
721 public int bubbleHeight;
722 public int bubbleBitmapWidth;
723 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700724
Joe Onoratobcbeab82009-10-01 21:45:43 -0700725 public int homeButtonWidth;
726 public int homeButtonHeight;
727 public int homeButtonTextureWidth;
728 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700729 }
730
731 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700732 public float newPositionX;
733 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700734 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700735 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700736 public int selectedIconIndex = -1;
737 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700738 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700739 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700740 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700741
742 State() {
743 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
744 mAlloc = Allocation.createTyped(mRS, mType);
745 save();
746 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700747 }
748
749 public RolloRS() {
750 }
751
752 public void init(Resources res, int width, int height) {
753 mRes = res;
754 mWidth = width;
755 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700756 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700757 initProgramVertex();
758 initProgramFragment();
759 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700760 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700761 initGl();
762 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700763 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700764 initRs();
765 }
766
Jason Sams0aa71662009-10-02 18:43:18 -0700767 public void initMesh() {
768 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
769 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
770
Jason Samsd8152b92009-10-13 17:19:10 -0700771 float y = 0;
772 float z = 0;
773 for (int ct=0; ct < 200; ct++) {
774 float angle = 0;
775 float maxAngle = 3.14f * 0.16f;
776 float l = 1.f;
777
778 l = 1 - ((ct-5) * 0.10f);
779 if (ct > 7) {
780 angle = maxAngle * (ct - 7) * 0.2f;
781 angle = Math.min(angle, maxAngle);
782 }
Jason Samsc8514792009-10-29 14:27:29 -0700783 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700784 l = Math.min(1.0f, l);
785
786 y += 0.1f * Math.cos(angle);
787 z += 0.1f * Math.sin(angle);
788
789 float t = 0.1f * ct;
790 float ds = 0.08f;
791 tm.setColor(l, l, l, 0.99f);
792 tm.setTexture(ds, t);
793 tm.addVertex(-0.5f, y, z);
794 tm.setTexture(1 - ds, t);
795 tm.addVertex(0.5f, y, z);
796 }
797 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
798 tm.addTriangle(ct, ct+1, ct+2);
799 tm.addTriangle(ct+1, ct+3, ct+2);
800 }
801 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700802 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700803 }
804
Jason Samscd689e12009-09-29 15:28:22 -0700805 private void initProgramVertex() {
806 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
807 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700808
809 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700810 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700811 mPV = pvb.create();
812 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700813 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700814
Jason Sams37e7c2b2009-10-19 12:55:43 -0700815 //pva = new ProgramVertex.MatrixAllocation(mRS);
816 //pva.setupOrthoWindow(mWidth, mHeight);
817 //pvb.setTextureMatrixEnable(true);
818 //mPVOrtho = pvb.create();
819 //mPVOrtho.setName("PVOrtho");
820 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700821
822 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700823 }
Joe Onorato93839052009-08-06 20:34:32 -0700824
Jason Samscd689e12009-09-29 15:28:22 -0700825 private void initProgramFragment() {
826 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700827 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700828 sb.setMag(Sampler.Value.LINEAR);
829 sb.setWrapS(Sampler.Value.CLAMP);
830 sb.setWrapT(Sampler.Value.CLAMP);
831 Sampler linear = sb.create();
832
833 sb.setMin(Sampler.Value.NEAREST);
834 sb.setMag(Sampler.Value.NEAREST);
835 Sampler nearest = sb.create();
836
837 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700838 //mPFColor = bf.create();
839 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700840
841 bf.setTexEnable(true, 0);
842 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700843 mPFTexMip = bf.create();
844 mPFTexMip.setName("PFTexMip");
845 mPFTexMip.bindSampler(linear, 0);
846
847 mPFTexNearest = bf.create();
848 mPFTexNearest.setName("PFTexNearest");
849 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700850 }
851
852 private void initProgramStore() {
853 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
854 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700855 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700856 bs.setDitherEnable(true);
857 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
858 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
859 mPSIcons = bs.create();
860 mPSIcons.setName("PSIcons");
861
862 //bs.setDitherEnable(false);
863 //mPSText = bs.create();
864 //mPSText.setName("PSText");
865 }
866
867 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700868 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700869 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700870 }
Jason Sams78aebd82009-09-15 13:06:59 -0700871
Joe Onorato1feb3a82009-08-08 22:32:00 -0700872 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700873 mParams = new Params();
874 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700875
Joe Onoratobf15cb42009-08-07 14:33:40 -0700876 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700877
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700878 mParams.bubbleWidth = bubble.getBubbleWidth();
879 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
880 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
881 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700882
Joe Onoratod63458b2009-10-15 21:19:09 -0700883 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
884 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
885 mHomeButtonNormal.uploadToTexture(0);
886 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
887 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
888 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700889 mParams.homeButtonWidth = 76;
890 mParams.homeButtonHeight = 68;
891 mParams.homeButtonTextureWidth = 128;
892 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700893
Joe Onoratod63458b2009-10-15 21:19:09 -0700894 mState.homeButtonId = mHomeButtonNormal.getID();
895
Joe Onorato1feb3a82009-08-08 22:32:00 -0700896 mParams.save();
897 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400898
Joe Onorato1291a8c2009-09-15 15:07:25 -0400899 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
900 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
901 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700902
Joe Onorato9c1289c2009-08-17 11:03:03 -0400903 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700904 }
905
Jason Sams37e7c2b2009-10-19 12:55:43 -0700906 private void initScript(int id) {
907 }
908
909 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700910 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700911 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700912 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700913 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700914 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
915 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700916 mInvokeMove = sb.addInvokable("move");
917 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700918 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700919 mInvokeResetWAR = sb.addInvokable("resetHWWar");
920 mScript = sb.create();
921 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
922 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
923 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
924 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
925 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700926
Jason Sams12c14a82009-10-06 14:33:15 -0700927 mMessageProc = new AAMessage();
928 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700929 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700930 }
Joe Onorato93839052009-08-06 20:34:32 -0700931
Joe Onorato9c1289c2009-08-17 11:03:03 -0400932 private void setApps(ArrayList<ApplicationInfo> list) {
933 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700934 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700935 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700936 allocCount = 1;
937 }
938
Joe Onorato9c1289c2009-08-17 11:03:03 -0400939 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700940 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700941 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942
943 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700944 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700945 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400946
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700947 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948
949 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
950
951 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700952 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400953 }
954
Joe Onorato9c1289c2009-08-17 11:03:03 -0400955 mState.iconCount = count;
956
Joe Onoratoa8138d52009-10-06 19:25:30 -0700957 saveAppsList();
958 }
959
Jason Samsc8514792009-10-29 14:27:29 -0700960 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
961 int black[] = new int[w > h ? w : h];
962 Allocation.Adapter2D a = alloc.createAdapter2D();
963 int mip = 0;
964 while (w > 1 || h > 1) {
965 a.subData(0, 0, 1, h, black);
966 a.subData(w-1, 0, 1, h, black);
967 a.subData(0, 0, w, 1, black);
968 a.subData(0, h-1, w, 1, black);
969 mip++;
970 w = (w + 1) >> 1;
971 h = (h + 1) >> 1;
972 a.setConstraint(Dimension.LOD, mip);
973 }
974 a.subData(0, 0, 1, 1, black);
975 }
976
Joe Onoratoa8138d52009-10-06 19:25:30 -0700977 private void uploadAppIcon(int index, ApplicationInfo item) {
978 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -0700979 Element.RGBA_8888(mRS), true);
980 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
981
Joe Onoratoa8138d52009-10-06 19:25:30 -0700982 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -0700983 Element.RGBA_8888(mRS), true);
984 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -0700985
986 mIcons[index].uploadToTexture(0);
987 mLabels[index].uploadToTexture(0);
988
989 mIconIds[index] = mIcons[index].getID();
990 mLabelIds[index] = mLabels[index].getID();
991 }
992
993 /**
994 * Puts the empty spaces at the end. Updates mState.iconCount. You must
995 * fill in the values and call saveAppsList().
996 */
997 private void reallocAppsList(int count) {
998 Allocation[] icons = new Allocation[count];
999 int[] iconIds = new int[count];
1000 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1001
1002 Allocation[] labels = new Allocation[count];
1003 int[] labelIds = new int[count];
1004 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1005
1006 final int oldCount = mIcons.length;
1007
1008 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1009 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1010 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1011 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1012
1013 mIcons = icons;
1014 mIconIds = iconIds;
1015 mLabels = labels;
1016 mLabelIds = labelIds;
1017 }
1018
1019 /**
1020 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1021 */
1022 private void addApp(int index, ApplicationInfo item) {
1023 final int count = mState.iconCount - index;
1024 final int dest = index + 1;
1025
1026 System.arraycopy(mIcons, index, mIcons, dest, count);
1027 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1028 System.arraycopy(mLabels, index, mLabels, dest, count);
1029 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1030
1031 uploadAppIcon(index, item);
1032 }
1033
1034 /**
1035 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1036 */
1037 private void removeApp(int index) {
1038 final int count = mState.iconCount - index - 1;
1039 final int src = index + 1;
1040
1041 System.arraycopy(mIcons, src, mIcons, index, count);
1042 System.arraycopy(mIconIds, src, mIconIds, index, count);
1043 System.arraycopy(mLabels, src, mLabels, index, count);
1044 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1045
1046 final int last = mState.iconCount - 1;
1047 mIcons[last] = null;
1048 mIconIds[last] = 0;
1049 mLabels[last] = null;
1050 mLabelIds[last] = 0;
1051 }
1052
1053 /**
1054 * Send the apps list structures to RS.
1055 */
1056 private void saveAppsList() {
1057 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001058
Joe Onoratoa8138d52009-10-06 19:25:30 -07001059 mAllocIconIds.data(mIconIds);
1060 mAllocLabelIds.data(mLabelIds);
1061
Jason Sams37e7c2b2009-10-19 12:55:43 -07001062 if (mScript != null) { // this happens when we init it
1063 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1064 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065 }
1066
1067 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001068
1069 // Note: mScript may be null if we haven't initialized it yet.
1070 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001071 if (mInvokeResetWAR != null) {
1072 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001073 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001074 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001076
1077 void initTouchState() {
1078 int width = getWidth();
1079 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001080 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1081 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001082
Jason Sams37e7c2b2009-10-19 12:55:43 -07001083 int centerY = (height / 2);
1084 mTouchYBorders[0] = centerY - (cellHeight * 2);
1085 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001086 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001087 mTouchYBorders[3] = centerY + cellHeight;
1088 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001089
Joe Onorato6665c0f2009-09-02 15:27:24 -07001090 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001091 mTouchXBorders[0] = 0;
1092 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001093 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001094 mTouchXBorders[3] = centerX + (width / 4);
1095 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001096 }
1097
Joe Onorato664457d2009-10-28 16:30:34 -04001098 void fling() {
1099 mInvokeFling.execute();
1100 }
1101
1102 void move() {
1103 mInvokeMove.execute();
1104 }
1105
1106 void moveTo(float row) {
1107 mState.targetPos = row;
1108 mState.save();
1109 mInvokeMoveTo.execute();
1110 }
1111
Jason Sams37e7c2b2009-10-19 12:55:43 -07001112 int chooseTappedIcon(int x, int y, float pos) {
1113 // Adjust for scroll position if not zero.
1114 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001115
Joe Onorato6665c0f2009-09-02 15:27:24 -07001116 int col = -1;
1117 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001118 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1119 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1120 col = i;
1121 break;
1122 }
1123 }
1124 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1125 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1126 row = i;
1127 break;
1128 }
1129 }
1130
1131 if (row < 0 || col < 0) {
1132 return -1;
1133 }
1134
Joe Onorato664457d2009-10-28 16:30:34 -04001135 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001136 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001137
Joe Onorato664457d2009-10-28 16:30:34 -04001138 if (index >= mState.iconCount) {
1139 return -1;
1140 } else {
1141 return index;
1142 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001143 }
1144
Joe Onorato6665c0f2009-09-02 15:27:24 -07001145 /**
1146 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001147 *
1148 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001149 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001150 int selectIcon(int x, int y, float pos) {
1151 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001152 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001153 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001154 }
1155
1156 void selectIcon(int index) {
Jason Samsc8514792009-10-29 14:27:29 -07001157 if (index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001158 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001159 } else {
1160 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001161
1162 Bitmap selectionBitmap = mSelectionBitmap;
1163
1164 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1165 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1166 mAllAppsList.get(index).iconBitmap);
1167
1168 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001169 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001170 mSelectedIcon.uploadToTexture(0);
1171 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001172 }
1173 }
1174
1175 /**
1176 * You need to call save() on mState on your own after calling this.
1177 */
1178 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001179 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001180 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001181
Joe Onoratod63458b2009-10-15 21:19:09 -07001182 void setHomeSelected(boolean pressed) {
1183 if (pressed) {
1184 mState.homeButtonId = mHomeButtonPressed.getID();
1185 } else {
1186 mState.homeButtonId = mHomeButtonNormal.getID();
1187 }
1188 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001189 }
Joe Onorato93839052009-08-06 20:34:32 -07001190}
1191
1192