blob: a7a1e6421bf476c9dcaab520c5a9c03015f2647b [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 Onoratoeb8325a2009-11-08 13:20:30 -050066 private static final int SELECTED_NONE = 0;
67 private static final int SELECTED_FOCUSED = 1;
68 private static final int SELECTED_PRESSED = 2;
69
70 private static final int SELECTION_NONE = 0;
71 private static final int SELECTION_ICONS = 1;
72 private static final int SELECTION_HOME = 2;
73
Joe Onorato6665c0f2009-09-02 15:27:24 -070074 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070075 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040076
77 /** When this is 0, modifications are allowed, when it's not, they're not.
78 * TODO: What about scrolling? */
79 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070080
Joe Onorato82ca5502009-10-15 16:59:23 -070081 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070082 private int mMaxFlingVelocity;
83
Joe Onoratobcbeab82009-10-01 21:45:43 -070084 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070085 private RenderScript mRS;
86 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040087 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070088
Mike Cleron7d5d7462009-10-20 14:06:00 -070089 /**
90 * True when we are using arrow keys or trackball to drive navigation
91 */
92 private boolean mArrowNavigation = false;
Joe Onoratoeb8325a2009-11-08 13:20:30 -050093 private boolean mStartedScrolling;
94
95 /**
96 * Used to keep track of the selection when AllAppsView loses window focus.
97 * One of the SELECTION_ constants.
98 */
99 private int mLastSelection;
Jason Sams2e19c052009-10-20 18:19:55 -0700100
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800101 /**
102 * Used to keep track of the selection when AllAppsView loses window focus
103 */
104 private int mLastSelectedIcon;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500105
Joe Onoratod769a632009-08-11 17:09:02 -0700106 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700107 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700108 private int mMotionDownRawX;
109 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700110 private int mDownIconIndex = -1;
111 private int mCurrentIconIndex = -1;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800112
Mike Cleronb64b67a2009-11-08 14:56:25 -0800113 private boolean mShouldGainFocus;
114
Joe Onorato3a8820b2009-11-10 15:06:42 -0800115 private boolean mZoomDirty = false;
116 private float mNextZoom;
117 private boolean mNextAnimate;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700118
Joe Onorato6665c0f2009-09-02 15:27:24 -0700119 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700120 public static final int ALLOC_PARAMS = 0;
121 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700122 public static final int ALLOC_ICON_IDS = 3;
123 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700124
125 public static final int COLUMNS_PER_PAGE = 4;
126 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700127
Joe Onorato6665c0f2009-09-02 15:27:24 -0700128 public static final int ICON_WIDTH_PX = 64;
129 public static final int ICON_TEXTURE_WIDTH_PX = 128;
130
131 public static final int ICON_HEIGHT_PX = 64;
132 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700133
134 public int SCREEN_WIDTH_PX;
135 public int SCREEN_HEIGHT_PX;
136
Joe Onoratobcbeab82009-10-01 21:45:43 -0700137 public void recompute(int w, int h) {
138 SCREEN_WIDTH_PX = 480;
139 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700140 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700141 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700142
143 public AllAppsView(Context context, AttributeSet attrs) {
144 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700145 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400146 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700147 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700148 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700149 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700150 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
151
Joe Onorato6665c0f2009-09-02 15:27:24 -0700152 setOnClickListener(this);
153 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700154 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700155 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700156 }
157
Joe Onoratob39e51a2009-10-28 15:47:49 -0400158 /**
159 * If you have an attached click listener, View always plays the click sound!?!?
160 * Deal with sound effects by hand.
161 */
162 public void reallyPlaySoundEffect(int sound) {
163 boolean old = isSoundEffectsEnabled();
164 setSoundEffectsEnabled(true);
165 playSoundEffect(sound);
166 setSoundEffectsEnabled(old);
167 }
168
Joe Onorato7c312c12009-08-13 21:36:53 -0700169 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
170 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700171 }
172
Joe Onorato6665c0f2009-09-02 15:27:24 -0700173 public void setLauncher(Launcher launcher) {
174 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700175 }
176
Joe Onorato1feb3a82009-08-08 22:32:00 -0700177 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700178 public void surfaceDestroyed(SurfaceHolder holder) {
179 super.surfaceDestroyed(holder);
Jason Sams20df7c72009-11-05 12:30:24 -0800180 mRollo.mHasSurface = false;
Joe Onorato7bb17492009-09-24 17:51:01 -0700181 }
182
183 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700184 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500185 Log.d(TAG, "starting surfaceChanged");
Joe Onorato6665c0f2009-09-02 15:27:24 -0700186 long startTime = SystemClock.uptimeMillis();
187
Joe Onorato080d9b62009-11-02 12:01:11 -0500188 super.surfaceChanged(holder, format, w, h);
189
Jason Sams90396672009-11-03 13:59:34 -0800190 if (mRS == null) {
Jason Sams90396672009-11-03 13:59:34 -0800191 mRS = createRenderScript(true);
192 mRollo = new RolloRS();
Jason Sams20df7c72009-11-05 12:30:24 -0800193 mRollo.mHasSurface = true;
Jason Sams90396672009-11-03 13:59:34 -0800194 mRollo.init(getResources(), w, h);
195 if (mAllAppsList != null) {
196 mRollo.setApps(mAllAppsList);
197 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
198 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800199 if (mShouldGainFocus) {
200 gainFocus();
201 mShouldGainFocus = false;
202 }
Joe Onorato3a8820b2009-11-10 15:06:42 -0800203 mRollo.dirtyCheck();
Jason Sams20df7c72009-11-05 12:30:24 -0800204 } else {
205 mRollo.mHasSurface = true;
206 mRollo.dirtyCheck();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700208
209 Resources res = getContext().getResources();
210 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700211
212 long endTime = SystemClock.uptimeMillis();
213 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700214 }
Jason Sams2e19c052009-10-20 18:19:55 -0700215
Joe Onorato93839052009-08-06 20:34:32 -0700216 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800217 public void onWindowFocusChanged(boolean hasWindowFocus) {
218 super.onWindowFocusChanged(hasWindowFocus);
219 if (mArrowNavigation) {
220 if (!hasWindowFocus) {
221 // Clear selection when we lose window focus
222 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500223 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800224 mRollo.clearSelectedIcon();
225 mRollo.mState.save();
226 } else if (hasWindowFocus) {
227 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500228 if (mLastSelection == SELECTION_ICONS) {
229 int selection = mLastSelectedIcon;
230 final int firstIcon = Math.round(mRollo.mMessageProc.mPosX) *
231 Defines.COLUMNS_PER_PAGE;
232 if (selection < 0 || // No selection
233 selection < firstIcon || // off the top of the screen
234 selection >= mRollo.mState.iconCount || // past last icon
235 selection >= firstIcon + // past last icon on screen
236 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
237 selection = firstIcon;
238 }
239
240 // Select the first icon when we gain window focus
241 mRollo.selectIcon(selection, SELECTED_FOCUSED);
242 mRollo.mState.save();
243 } else if (mLastSelection == SELECTION_HOME) {
244 mRollo.setHomeSelected(SELECTED_FOCUSED);
245 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800246 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800247 }
248 }
249 }
250 }
251
252 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700253 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
254 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400255
256 if (!isVisible()) {
257 return;
258 }
259
Mike Cleron7d5d7462009-10-20 14:06:00 -0700260 if (gainFocus) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800261 if (mRollo != null) {
262 gainFocus();
263 } else {
264 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700265 }
266 } else {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800267 if (mRollo != null) {
268 if (mArrowNavigation) {
269 // Clear selection when we lose focus
270 mRollo.clearSelectedIcon();
271 mRollo.setHomeSelected(SELECTED_NONE);
272 mRollo.mState.save();
273 mArrowNavigation = false;
274 }
275 } else {
276 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700277 }
278 }
279 }
280
Mike Cleronb64b67a2009-11-08 14:56:25 -0800281 private void gainFocus() {
282 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
283 // Select the first icon when we gain keyboard focus
284 mArrowNavigation = true;
285 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE,
286 SELECTED_FOCUSED);
287 mRollo.mState.save();
288 }
289 }
290
Mike Cleron7d5d7462009-10-20 14:06:00 -0700291 @Override
292 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700293
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800294 boolean handled = false;
295
Joe Onorato859b3a72009-10-28 15:17:01 -0400296 if (!isVisible()) {
297 return false;
298 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500299 final int iconCount = mRollo.mState.iconCount;
300
Mike Cleron7d5d7462009-10-20 14:06:00 -0700301 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
302 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500303 if (mLastSelection == SELECTION_HOME) {
304 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
305 mLauncher.closeAllApps(true);
306 } else {
307 int whichApp = mRollo.mState.selectedIconIndex;
308 if (whichApp >= 0) {
309 ApplicationInfo app = mAllAppsList.get(whichApp);
310 mLauncher.startActivitySafely(app.intent);
311 handled = true;
312 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700313 }
314 }
315 }
Jason Sams2e19c052009-10-20 18:19:55 -0700316
Joe Onoratoa13f5742009-11-02 17:15:19 -0500317 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700318 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700319
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800320 int currentSelection = mRollo.mState.selectedIconIndex;
321 int currentTopRow = Math.round(mRollo.mMessageProc.mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700322
Mike Cleron7d5d7462009-10-20 14:06:00 -0700323 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500324 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700325
Mike Cleron7d5d7462009-10-20 14:06:00 -0700326 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500327 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700328 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700329
Mike Cleron7d5d7462009-10-20 14:06:00 -0700330 int newSelection = currentSelection;
331
332 switch (keyCode) {
333 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500334 if (mLastSelection == SELECTION_HOME) {
335 mRollo.setHomeSelected(SELECTED_NONE);
336 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
337 if (lastRowCount == 0) {
338 lastRowCount = Defines.COLUMNS_PER_PAGE;
339 }
340 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
341 if (newSelection >= iconCount) {
342 newSelection = iconCount-1;
343 }
344 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
345 - (Defines.ROWS_PER_PAGE - 1);
346 if (target < 0) {
347 target = 0;
348 }
349 if (currentTopRow != target) {
350 mRollo.moveTo(target);
351 }
352 } else {
353 if (currentPageRow > 0) {
354 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
355 } else if (currentTopRow > 0) {
356 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
357 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
358 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700359 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800360 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700361 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800362
Joe Onoratoa13f5742009-11-02 17:15:19 -0500363 case KeyEvent.KEYCODE_DPAD_DOWN: {
364 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
365 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
366 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500367 if (mLastSelection != SELECTION_HOME) {
368 if (currentRow < rowCount-1) {
369 mRollo.setHomeSelected(SELECTED_NONE);
370 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
371 if (newSelection >= iconCount) {
372 // Go from D to G in this arrangement:
373 // A B C D
374 // E F G
375 newSelection = iconCount - 1;
376 }
377 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
378 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
379 Defines.ROWS_PER_PAGE + 1);
380 }
381 } else {
382 newSelection = -1;
383 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700384 }
385 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800386 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700387 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500388 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700389 case KeyEvent.KEYCODE_DPAD_LEFT:
390 if (currentPageCol > 0) {
391 newSelection = currentSelection - 1;
392 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800393 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700394 break;
395 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700396 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500397 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700398 newSelection = currentSelection + 1;
399 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800400 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700401 break;
402 }
403 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500404 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700405 mRollo.mState.save();
406 }
407 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800408 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700409 }
410
Joe Onorato93839052009-08-06 20:34:32 -0700411 @Override
412 public boolean onTouchEvent(MotionEvent ev)
413 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700414 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700415
Joe Onorato7bb17492009-09-24 17:51:01 -0700416 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700417 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700418 }
419
Joe Onoratofb0ca672009-09-14 17:55:46 -0400420 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700421 return true;
422 }
423
424 super.onTouchEvent(ev);
425
Joe Onoratofb0ca672009-09-14 17:55:46 -0400426 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700427 int y = (int)ev.getY();
428
Joe Onoratobcbeab82009-10-01 21:45:43 -0700429 int action = ev.getAction();
430 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400431 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700432 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
433 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500434 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700435 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700436 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700437 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700438 mTouchTracking = TRACKING_FLING;
439
440 mMotionDownRawX = (int)ev.getRawX();
441 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700442
Mike Cleron7d5d7462009-10-20 14:06:00 -0700443 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700444 mRollo.mState.newTouchDown = 1;
445
446 if (!mRollo.checkClickOK()) {
447 mRollo.clearSelectedIcon();
448 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700449 mDownIconIndex = mCurrentIconIndex
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500450 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700451 if (mDownIconIndex < 0) {
452 // if nothing was selected, no long press.
453 cancelLongPress();
454 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700455 }
456 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700457 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700458 mVelocity = VelocityTracker.obtain();
459 mVelocity.addMovement(ev);
460 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700461 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400462 break;
463 case MotionEvent.ACTION_MOVE:
464 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700465 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500466 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
467 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700468 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700469 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700470 int rawX = (int)ev.getRawX();
471 int rawY = (int)ev.getRawY();
472 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700473 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700474
Joe Onorato82ca5502009-10-15 16:59:23 -0700475 if (!mStartedScrolling && slop < mSlop) {
476 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700477 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700478 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
479 if (mDownIconIndex != mCurrentIconIndex) {
480 // If a different icon is selected, don't allow it to be picked up.
481 // This handles off-axis dragging.
482 cancelLongPress();
483 mCurrentIconIndex = -1;
484 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700485 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700486 if (!mStartedScrolling) {
487 cancelLongPress();
488 mCurrentIconIndex = -1;
489 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700490 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700491 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700492 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700493
Joe Onoratobcbeab82009-10-01 21:45:43 -0700494 mStartedScrolling = true;
495 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700496 mVelocity.addMovement(ev);
497 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700498 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400499 }
500 break;
501 case MotionEvent.ACTION_UP:
502 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700503 if (mTouchTracking == TRACKING_HOME) {
504 if (action == MotionEvent.ACTION_UP) {
505 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400506 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700507 mLauncher.closeAllApps(true);
508 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500509 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700510 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700511 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700512 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700513 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700514 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700515 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700516
Jason Sams12c14a82009-10-06 14:33:15 -0700517 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700518 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700519 mRollo.clearSelectedIcon();
520 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700521 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700522
Joe Onorato539ed9d2009-10-02 10:22:14 -0700523 if (mVelocity != null) {
524 mVelocity.recycle();
525 mVelocity = null;
526 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700527 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700528 mTouchTracking = TRACKING_NONE;
529 break;
Joe Onorato93839052009-08-06 20:34:32 -0700530 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700531
532 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700533 }
534
Joe Onorato6665c0f2009-09-02 15:27:24 -0700535 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700536 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400537 return;
538 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700539 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
540 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400541 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700542 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700543 mLauncher.startActivitySafely(app.intent);
544 }
545 }
546
547 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700548 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400549 return true;
550 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700551 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
552 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
553 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700554
555 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700556 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
557 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700558
Joe Onoratobcbeab82009-10-01 21:45:43 -0700559 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
560 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700561 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700562 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700563 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700564
Joe Onorato7bb17492009-09-24 17:51:01 -0700565 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700566 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700567 return true;
568 }
569
Joe Onorato5162ea92009-09-03 09:39:42 -0700570 public void setDragController(DragController dragger) {
571 mDragController = dragger;
572 }
573
574 public void onDropCompleted(View target, boolean success) {
575 }
576
Joe Onorato4db52312009-10-06 11:17:43 -0700577 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800578 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700579 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800580 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700581 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800582 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400583 cancelLongPress();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800584 if (mRollo == null || !mRollo.mHasSurface) {
585 mZoomDirty = true;
586 mNextZoom = zoom;
587 mNextAnimate = animate;
588 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700589 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800590 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700591 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400592 }
593
594 public boolean isVisible() {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800595 if (mZoomDirty) {
596 return mNextZoom > 0.001f;
597 } else {
598 if (mRollo == null) {
599 return false;
600 } else {
601 return mRollo.mMessageProc.mZoom > 0.001f;
602 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700603 }
Joe Onorato3a8820b2009-11-10 15:06:42 -0800604 }
605
606 public boolean isOpaque() {
607 if (mZoomDirty) {
608 return mNextZoom > 0.999f;
609 } else {
610 if (mRollo == null) {
611 return false;
612 } else {
613 return mRollo.mMessageProc.mZoom > 0.999f;
614 }
615 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400616 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700617
Mike Cleronb6082fa02009-10-19 17:03:36 -0700618 /*
Joe Onorato93839052009-08-06 20:34:32 -0700619 @Override
620 public boolean onTrackballEvent(MotionEvent ev)
621 {
622 float x = ev.getX();
623 float y = ev.getY();
624 //Float tx = new Float(x);
625 //Float ty = new Float(y);
626 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
627
628
629 return true;
630 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700631 */
Joe Onorato93839052009-08-06 20:34:32 -0700632
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 public void setApps(ArrayList<ApplicationInfo> list) {
634 mAllAppsList = list;
635 if (mRollo != null) {
636 mRollo.setApps(list);
637 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400638 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700639 }
640
Joe Onoratoa8138d52009-10-06 19:25:30 -0700641 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500642 if (mAllAppsList == null) {
643 // Not done loading yet. We'll find out about it later.
644 return;
645 }
646
Joe Onoratoa8138d52009-10-06 19:25:30 -0700647 final int N = list.size();
648 if (mRollo != null) {
649 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
650 }
651
652 for (int i=0; i<N; i++) {
653 final ApplicationInfo item = list.get(i);
654 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
655 if (index < 0) {
656 index = -(index+1);
657 }
Joe Onorato0ace11a2009-11-05 15:41:27 -0500658 Log.d(TAG, "Adding app '" + item + "' at index " + index + " mRollo=" + mRollo);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700659 mAllAppsList.add(index, item);
660 if (mRollo != null) {
661 mRollo.addApp(index, item);
662 mRollo.mState.iconCount++;
663 }
664 }
665
666 if (mRollo != null) {
667 mRollo.saveAppsList();
668 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700669 }
670
Joe Onoratoa8138d52009-10-06 19:25:30 -0700671 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500672 if (mAllAppsList == null) {
673 // Not done loading yet. We'll find out about it later.
674 return;
675 }
676
Joe Onoratoa8138d52009-10-06 19:25:30 -0700677 final int N = list.size();
678 for (int i=0; i<N; i++) {
679 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400680 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700681 if (index >= 0) {
682 mAllAppsList.remove(index);
683 if (mRollo != null) {
684 mRollo.removeApp(index);
685 mRollo.mState.iconCount--;
686 }
687 } else {
688 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
689 // Try to recover. This should keep us from crashing for now.
690 }
691 }
692
693 if (mRollo != null) {
694 mRollo.saveAppsList();
695 }
696 }
697
698 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
699 // Just remove and add, because they may need to be re-sorted.
700 removeApps(list);
701 addApps(list);
702 }
703
704 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
705 public int compare(ApplicationInfo a, ApplicationInfo b) {
706 int result = a.title.toString().compareTo(b.toString());
707 if (result != 0) {
708 return result;
709 }
710 return a.intent.getComponent().compareTo(b.intent.getComponent());
711 }
712 };
713
Joe Onoratocb9f7982009-10-31 16:32:02 -0400714 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
715 ComponentName component = item.intent.getComponent();
716 final int N = list.size();
717 for (int i=0; i<N; i++) {
718 ApplicationInfo x = list.get(i);
719 if (x.intent.getComponent().equals(component)) {
720 return i;
721 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700722 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400723 return -1;
724 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700725
Joe Onoratoc567acb2009-08-31 14:34:43 -0700726 private static int countPages(int iconCount) {
727 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
728 int pages = iconCount / iconsPerPage;
729 if (pages*iconsPerPage != iconCount) {
730 pages++;
731 }
732 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733 }
734
Joe Onorato93839052009-08-06 20:34:32 -0700735 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700736
Joe Onorato1feb3a82009-08-08 22:32:00 -0700737 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700738 private int mWidth;
739 private int mHeight;
740
741 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700742 private Script mScript;
743 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700744 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700745 private Script.Invokable mInvokeFling;
746 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800747 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700748
Jason Samscd689e12009-09-29 15:28:22 -0700749 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700750 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700751 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700752 private ProgramFragment mPFTexMip;
753 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700754 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700755 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700756 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700757 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700758
Joe Onoratod63458b2009-10-15 21:19:09 -0700759 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500760 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700761 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700762
Joe Onoratobf15cb42009-08-07 14:33:40 -0700763 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700764 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700765 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700766
Joe Onoratobf15cb42009-08-07 14:33:40 -0700767 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700768 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700769 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700770 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700771
Joe Onorato6665c0f2009-09-02 15:27:24 -0700772 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700773 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700774
775 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400776 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700777
Jason Sams20df7c72009-11-05 12:30:24 -0800778 boolean mHasSurface = false;
779 private boolean mAppsDirty = false;
Jason Samsd8152b92009-10-13 17:19:10 -0700780
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700781 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700782 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700783
Jason Sams78aebd82009-09-15 13:06:59 -0700784 class BaseAlloc {
785 Allocation mAlloc;
786 Type mType;
787
788 void save() {
789 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700790 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700791 }
792
Jason Sams12c14a82009-10-06 14:33:15 -0700793 class AAMessage extends RenderScript.RSMessage {
794 public void run() {
795 mPosX = ((float)mData[0]) / (1 << 16);
796 mVelocity = ((float)mData[1]) / (1 << 16);
797 mZoom = ((float)mData[2]) / (1 << 16);
798 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
799 }
800 float mZoom;
801 float mPosX;
802 float mVelocity;
803 }
804 AAMessage mMessageProc;
805
Jason Sams476339d2009-09-29 18:14:38 -0700806 private boolean checkClickOK() {
807 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams2e19c052009-10-20 18:19:55 -0700808 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
809 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700810 }
811
Jason Sams78aebd82009-09-15 13:06:59 -0700812 class Params extends BaseAlloc {
813 Params() {
814 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
815 mAlloc = Allocation.createTyped(mRS, mType);
816 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700817 }
Jason Sams78aebd82009-09-15 13:06:59 -0700818 public int bubbleWidth;
819 public int bubbleHeight;
820 public int bubbleBitmapWidth;
821 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700822
Joe Onoratobcbeab82009-10-01 21:45:43 -0700823 public int homeButtonWidth;
824 public int homeButtonHeight;
825 public int homeButtonTextureWidth;
826 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700827 }
828
829 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700830 public float newPositionX;
831 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700832 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700833 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700834 public int selectedIconIndex = -1;
835 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700836 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700837 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700838 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700839
840 State() {
841 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
842 mAlloc = Allocation.createTyped(mRS, mType);
843 save();
844 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700845 }
846
847 public RolloRS() {
848 }
849
850 public void init(Resources res, int width, int height) {
851 mRes = res;
852 mWidth = width;
853 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700854 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700855 initProgramVertex();
856 initProgramFragment();
857 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700858 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700859 initGl();
860 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700861 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700862 initRs();
863 }
864
Jason Sams0aa71662009-10-02 18:43:18 -0700865 public void initMesh() {
866 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
867 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
868
Jason Samsd8152b92009-10-13 17:19:10 -0700869 float y = 0;
870 float z = 0;
871 for (int ct=0; ct < 200; ct++) {
872 float angle = 0;
873 float maxAngle = 3.14f * 0.16f;
874 float l = 1.f;
875
876 l = 1 - ((ct-5) * 0.10f);
877 if (ct > 7) {
878 angle = maxAngle * (ct - 7) * 0.2f;
879 angle = Math.min(angle, maxAngle);
880 }
Jason Samsc8514792009-10-29 14:27:29 -0700881 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700882 l = Math.min(1.0f, l);
883
884 y += 0.1f * Math.cos(angle);
885 z += 0.1f * Math.sin(angle);
886
887 float t = 0.1f * ct;
888 float ds = 0.08f;
889 tm.setColor(l, l, l, 0.99f);
890 tm.setTexture(ds, t);
891 tm.addVertex(-0.5f, y, z);
892 tm.setTexture(1 - ds, t);
893 tm.addVertex(0.5f, y, z);
894 }
895 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
896 tm.addTriangle(ct, ct+1, ct+2);
897 tm.addTriangle(ct+1, ct+3, ct+2);
898 }
899 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700900 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700901 }
902
Jason Samscd689e12009-09-29 15:28:22 -0700903 private void initProgramVertex() {
904 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
905 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700906
907 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700908 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700909 mPV = pvb.create();
910 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700911 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700912
Jason Sams37e7c2b2009-10-19 12:55:43 -0700913 //pva = new ProgramVertex.MatrixAllocation(mRS);
914 //pva.setupOrthoWindow(mWidth, mHeight);
915 //pvb.setTextureMatrixEnable(true);
916 //mPVOrtho = pvb.create();
917 //mPVOrtho.setName("PVOrtho");
918 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700919
920 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700921 }
Joe Onorato93839052009-08-06 20:34:32 -0700922
Jason Samscd689e12009-09-29 15:28:22 -0700923 private void initProgramFragment() {
924 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700925 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700926 sb.setMag(Sampler.Value.LINEAR);
927 sb.setWrapS(Sampler.Value.CLAMP);
928 sb.setWrapT(Sampler.Value.CLAMP);
929 Sampler linear = sb.create();
930
931 sb.setMin(Sampler.Value.NEAREST);
932 sb.setMag(Sampler.Value.NEAREST);
933 Sampler nearest = sb.create();
934
935 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700936 //mPFColor = bf.create();
937 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700938
939 bf.setTexEnable(true, 0);
940 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700941 mPFTexMip = bf.create();
942 mPFTexMip.setName("PFTexMip");
943 mPFTexMip.bindSampler(linear, 0);
944
945 mPFTexNearest = bf.create();
946 mPFTexNearest.setName("PFTexNearest");
947 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700948 }
949
950 private void initProgramStore() {
951 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
952 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700953 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700954 bs.setDitherEnable(true);
955 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
956 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
957 mPSIcons = bs.create();
958 mPSIcons.setName("PSIcons");
959
960 //bs.setDitherEnable(false);
961 //mPSText = bs.create();
962 //mPSText.setName("PSText");
963 }
964
965 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700966 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700967 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700968 }
Jason Sams78aebd82009-09-15 13:06:59 -0700969
Joe Onorato1feb3a82009-08-08 22:32:00 -0700970 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700971 mParams = new Params();
972 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700973
Joe Onoratobf15cb42009-08-07 14:33:40 -0700974 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700975
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700976 mParams.bubbleWidth = bubble.getBubbleWidth();
977 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
978 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
979 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700980
Joe Onoratod63458b2009-10-15 21:19:09 -0700981 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
982 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
983 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500984 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
985 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
986 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -0700987 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
988 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
989 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700990 mParams.homeButtonWidth = 76;
991 mParams.homeButtonHeight = 68;
992 mParams.homeButtonTextureWidth = 128;
993 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700994
Joe Onoratod63458b2009-10-15 21:19:09 -0700995 mState.homeButtonId = mHomeButtonNormal.getID();
996
Joe Onorato1feb3a82009-08-08 22:32:00 -0700997 mParams.save();
998 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400999
Joe Onorato1291a8c2009-09-15 15:07:25 -04001000 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1001 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1002 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001003
Joe Onorato9c1289c2009-08-17 11:03:03 -04001004 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001005 }
1006
Jason Sams37e7c2b2009-10-19 12:55:43 -07001007 private void initScript(int id) {
1008 }
1009
1010 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001011 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001012 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -07001013 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001014 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001015 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1016 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001017 mInvokeMove = sb.addInvokable("move");
1018 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001019 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001020 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001021 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001022 mScript = sb.create();
1023 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1024 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1025 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1026 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1027 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001028
Jason Sams12c14a82009-10-06 14:33:15 -07001029 mMessageProc = new AAMessage();
1030 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001031 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001032 }
Joe Onorato93839052009-08-06 20:34:32 -07001033
Jason Sams20df7c72009-11-05 12:30:24 -08001034 private void uploadApps(ArrayList<ApplicationInfo> list) {
1035 for (int i=0; i < mState.iconCount; i++) {
1036 uploadAppIcon(i, list.get(i));
1037 }
1038 }
1039
1040 void dirtyCheck() {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001041 if (mHasSurface) {
1042 if (mAppsDirty) {
1043 uploadApps(mAllAppsList);
1044 saveAppsList();
1045 mAppsDirty = false;
1046 }
1047 if (mZoomDirty) {
1048 setZoom(mNextZoom, mNextAnimate);
1049 mZoomDirty = false;
1050 }
Jason Sams20df7c72009-11-05 12:30:24 -08001051 }
1052 }
1053
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054 private void setApps(ArrayList<ApplicationInfo> list) {
1055 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001056 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001057 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001058 allocCount = 1;
1059 }
1060
Joe Onorato9c1289c2009-08-17 11:03:03 -04001061 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001062 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001063 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064
1065 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001066 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001067 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001068
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001069 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001070
1071 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1072
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001074 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001075 saveAppsList();
1076 }
1077
Joe Onorato3a8820b2009-11-10 15:06:42 -08001078 private void setZoom(float zoom, boolean animate) {
1079 mRollo.clearSelectedIcon();
1080 mRollo.setHomeSelected(SELECTED_NONE);
1081 if (zoom > 0.001f) {
1082 mRollo.mState.zoomTarget = zoom;
1083 } else {
1084 mRollo.mState.zoomTarget = 0;
1085 }
1086 mRollo.mState.save();
1087 if (!animate) {
1088 mRollo.mInvokeSetZoom.execute();
1089 }
1090 }
1091
Jason Samsc8514792009-10-29 14:27:29 -07001092 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1093 int black[] = new int[w > h ? w : h];
1094 Allocation.Adapter2D a = alloc.createAdapter2D();
1095 int mip = 0;
1096 while (w > 1 || h > 1) {
1097 a.subData(0, 0, 1, h, black);
1098 a.subData(w-1, 0, 1, h, black);
1099 a.subData(0, 0, w, 1, black);
1100 a.subData(0, h-1, w, 1, black);
1101 mip++;
1102 w = (w + 1) >> 1;
1103 h = (h + 1) >> 1;
1104 a.setConstraint(Dimension.LOD, mip);
1105 }
1106 a.subData(0, 0, 1, 1, black);
1107 }
1108
Joe Onoratoa8138d52009-10-06 19:25:30 -07001109 private void uploadAppIcon(int index, ApplicationInfo item) {
1110 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001111 Element.RGBA_8888(mRS), true);
1112 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1113
Joe Onoratoa8138d52009-10-06 19:25:30 -07001114 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001115 Element.RGBA_8888(mRS), true);
1116 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001117
1118 mIcons[index].uploadToTexture(0);
1119 mLabels[index].uploadToTexture(0);
1120
1121 mIconIds[index] = mIcons[index].getID();
1122 mLabelIds[index] = mLabels[index].getID();
1123 }
1124
1125 /**
1126 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1127 * fill in the values and call saveAppsList().
1128 */
1129 private void reallocAppsList(int count) {
1130 Allocation[] icons = new Allocation[count];
1131 int[] iconIds = new int[count];
1132 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1133
1134 Allocation[] labels = new Allocation[count];
1135 int[] labelIds = new int[count];
1136 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1137
1138 final int oldCount = mIcons.length;
1139
1140 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1141 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1142 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1143 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1144
1145 mIcons = icons;
1146 mIconIds = iconIds;
1147 mLabels = labels;
1148 mLabelIds = labelIds;
1149 }
1150
1151 /**
1152 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1153 */
1154 private void addApp(int index, ApplicationInfo item) {
1155 final int count = mState.iconCount - index;
1156 final int dest = index + 1;
1157
1158 System.arraycopy(mIcons, index, mIcons, dest, count);
1159 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1160 System.arraycopy(mLabels, index, mLabels, dest, count);
1161 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1162
Jason Sams20df7c72009-11-05 12:30:24 -08001163 if (mHasSurface ) {
1164 uploadAppIcon(index, item);
1165 } else {
1166 mAppsDirty = true;
1167 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001168 }
1169
1170 /**
1171 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1172 */
1173 private void removeApp(int index) {
1174 final int count = mState.iconCount - index - 1;
1175 final int src = index + 1;
1176
1177 System.arraycopy(mIcons, src, mIcons, index, count);
1178 System.arraycopy(mIconIds, src, mIconIds, index, count);
1179 System.arraycopy(mLabels, src, mLabels, index, count);
1180 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1181
1182 final int last = mState.iconCount - 1;
1183 mIcons[last] = null;
1184 mIconIds[last] = 0;
1185 mLabels[last] = null;
1186 mLabelIds[last] = 0;
1187 }
1188
1189 /**
1190 * Send the apps list structures to RS.
1191 */
1192 private void saveAppsList() {
1193 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001194
Joe Onoratoa8138d52009-10-06 19:25:30 -07001195 mAllocIconIds.data(mIconIds);
1196 mAllocLabelIds.data(mLabelIds);
1197
Jason Sams37e7c2b2009-10-19 12:55:43 -07001198 if (mScript != null) { // this happens when we init it
1199 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1200 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 }
1202
1203 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001204
1205 // Note: mScript may be null if we haven't initialized it yet.
1206 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001207 if (mInvokeResetWAR != null) {
1208 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001209 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001210 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001211 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001212
1213 void initTouchState() {
1214 int width = getWidth();
1215 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001216 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1217 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001218
Jason Sams37e7c2b2009-10-19 12:55:43 -07001219 int centerY = (height / 2);
1220 mTouchYBorders[0] = centerY - (cellHeight * 2);
1221 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001222 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001223 mTouchYBorders[3] = centerY + cellHeight;
1224 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001225
Joe Onorato6665c0f2009-09-02 15:27:24 -07001226 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001227 mTouchXBorders[0] = 0;
1228 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001229 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001230 mTouchXBorders[3] = centerX + (width / 4);
1231 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001232 }
1233
Joe Onorato664457d2009-10-28 16:30:34 -04001234 void fling() {
1235 mInvokeFling.execute();
1236 }
1237
1238 void move() {
1239 mInvokeMove.execute();
1240 }
1241
1242 void moveTo(float row) {
1243 mState.targetPos = row;
1244 mState.save();
1245 mInvokeMoveTo.execute();
1246 }
1247
Jason Sams37e7c2b2009-10-19 12:55:43 -07001248 int chooseTappedIcon(int x, int y, float pos) {
1249 // Adjust for scroll position if not zero.
1250 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001251
Joe Onorato6665c0f2009-09-02 15:27:24 -07001252 int col = -1;
1253 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001254 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1255 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1256 col = i;
1257 break;
1258 }
1259 }
1260 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1261 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1262 row = i;
1263 break;
1264 }
1265 }
1266
1267 if (row < 0 || col < 0) {
1268 return -1;
1269 }
1270
Joe Onorato664457d2009-10-28 16:30:34 -04001271 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001272 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001273
Joe Onorato664457d2009-10-28 16:30:34 -04001274 if (index >= mState.iconCount) {
1275 return -1;
1276 } else {
1277 return index;
1278 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001279 }
1280
Joe Onorato6665c0f2009-09-02 15:27:24 -07001281 /**
1282 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001283 *
1284 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001285 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001286 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001287 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001288 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001289 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001290 }
1291
Joe Onoratoc61cff92009-11-08 11:54:39 -05001292 /**
1293 * Select the icon at the given index.
1294 *
1295 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001296 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001297 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001298 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001299 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001300 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001301 if (mLastSelection == SELECTION_ICONS) {
1302 mLastSelection = SELECTION_NONE;
1303 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001304 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001305 if (pressed == SELECTED_FOCUSED) {
1306 mLastSelection = SELECTION_ICONS;
1307 }
1308
Joe Onorato6665c0f2009-09-02 15:27:24 -07001309 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001310
1311 Bitmap selectionBitmap = mSelectionBitmap;
1312
1313 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001314 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1315 pressed == SELECTED_PRESSED,
Joe Onorato1291a8c2009-09-15 15:07:25 -04001316 mAllAppsList.get(index).iconBitmap);
1317
1318 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001319 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001320 mSelectedIcon.uploadToTexture(0);
1321 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001322 }
1323 }
1324
1325 /**
1326 * You need to call save() on mState on your own after calling this.
1327 */
1328 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001329 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001330 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001331
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001332 void setHomeSelected(int mode) {
1333 switch (mode) {
1334 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001335 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001336 break;
1337 case SELECTED_FOCUSED:
1338 mLastSelection = SELECTION_HOME;
1339 mState.homeButtonId = mHomeButtonFocused.getID();
1340 break;
1341 case SELECTED_PRESSED:
1342 mState.homeButtonId = mHomeButtonPressed.getID();
1343 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001344 }
1345 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001346 }
Joe Onorato93839052009-08-06 20:34:32 -07001347}
1348
1349