blob: d96268cdd226924f5bf177a23a23cb5a087bd04b [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
Joe Onorato1feb3a82009-08-08 22:32:00 -0700113
Joe Onorato6665c0f2009-09-02 15:27:24 -0700114 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700115 public static final int ALLOC_PARAMS = 0;
116 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700117 public static final int ALLOC_ICON_IDS = 3;
118 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700119
120 public static final int COLUMNS_PER_PAGE = 4;
121 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700122
Joe Onorato6665c0f2009-09-02 15:27:24 -0700123 public static final int ICON_WIDTH_PX = 64;
124 public static final int ICON_TEXTURE_WIDTH_PX = 128;
125
126 public static final int ICON_HEIGHT_PX = 64;
127 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700128
129 public int SCREEN_WIDTH_PX;
130 public int SCREEN_HEIGHT_PX;
131
Joe Onoratobcbeab82009-10-01 21:45:43 -0700132 public void recompute(int w, int h) {
133 SCREEN_WIDTH_PX = 480;
134 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700135 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700136 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700137
138 public AllAppsView(Context context, AttributeSet attrs) {
139 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700140 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400141 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700142 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700143 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700144 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700145 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
146
Joe Onorato6665c0f2009-09-02 15:27:24 -0700147 setOnClickListener(this);
148 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700149 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700150 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700151 }
152
Joe Onoratob39e51a2009-10-28 15:47:49 -0400153 /**
154 * If you have an attached click listener, View always plays the click sound!?!?
155 * Deal with sound effects by hand.
156 */
157 public void reallyPlaySoundEffect(int sound) {
158 boolean old = isSoundEffectsEnabled();
159 setSoundEffectsEnabled(true);
160 playSoundEffect(sound);
161 setSoundEffectsEnabled(old);
162 }
163
Joe Onorato7c312c12009-08-13 21:36:53 -0700164 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
165 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700166 }
167
Joe Onorato6665c0f2009-09-02 15:27:24 -0700168 public void setLauncher(Launcher launcher) {
169 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700170 }
171
Joe Onorato1feb3a82009-08-08 22:32:00 -0700172 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700173 public void surfaceDestroyed(SurfaceHolder holder) {
174 super.surfaceDestroyed(holder);
Jason Sams20df7c72009-11-05 12:30:24 -0800175 mRollo.mHasSurface = false;
Joe Onorato7bb17492009-09-24 17:51:01 -0700176 }
177
178 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700179 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500180 Log.d(TAG, "starting surfaceChanged");
Joe Onorato6665c0f2009-09-02 15:27:24 -0700181 long startTime = SystemClock.uptimeMillis();
182
Joe Onorato080d9b62009-11-02 12:01:11 -0500183 super.surfaceChanged(holder, format, w, h);
184
Jason Sams90396672009-11-03 13:59:34 -0800185 if (mRS == null) {
Jason Sams81134792009-10-27 15:38:42 -0700186
Jason Sams90396672009-11-03 13:59:34 -0800187 mRS = createRenderScript(true);
188 mRollo = new RolloRS();
Jason Sams20df7c72009-11-05 12:30:24 -0800189 mRollo.mHasSurface = true;
Jason Sams90396672009-11-03 13:59:34 -0800190 mRollo.init(getResources(), w, h);
191 if (mAllAppsList != null) {
192 mRollo.setApps(mAllAppsList);
193 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
194 }
Jason Sams20df7c72009-11-05 12:30:24 -0800195 } else {
196 mRollo.mHasSurface = true;
197 mRollo.dirtyCheck();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400198 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700199
200 Resources res = getContext().getResources();
201 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700202
203 long endTime = SystemClock.uptimeMillis();
204 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700205 }
Jason Sams2e19c052009-10-20 18:19:55 -0700206
Joe Onorato93839052009-08-06 20:34:32 -0700207 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800208 public void onWindowFocusChanged(boolean hasWindowFocus) {
209 super.onWindowFocusChanged(hasWindowFocus);
210 if (mArrowNavigation) {
211 if (!hasWindowFocus) {
212 // Clear selection when we lose window focus
213 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500214 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800215 mRollo.clearSelectedIcon();
216 mRollo.mState.save();
217 } else if (hasWindowFocus) {
218 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500219 if (mLastSelection == SELECTION_ICONS) {
220 int selection = mLastSelectedIcon;
221 final int firstIcon = Math.round(mRollo.mMessageProc.mPosX) *
222 Defines.COLUMNS_PER_PAGE;
223 if (selection < 0 || // No selection
224 selection < firstIcon || // off the top of the screen
225 selection >= mRollo.mState.iconCount || // past last icon
226 selection >= firstIcon + // past last icon on screen
227 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
228 selection = firstIcon;
229 }
230
231 // Select the first icon when we gain window focus
232 mRollo.selectIcon(selection, SELECTED_FOCUSED);
233 mRollo.mState.save();
234 } else if (mLastSelection == SELECTION_HOME) {
235 mRollo.setHomeSelected(SELECTED_FOCUSED);
236 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800237 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800238 }
239 }
240 }
241 }
242
243 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700244 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
245 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400246
247 if (!isVisible()) {
248 return;
249 }
250
Mike Cleron7d5d7462009-10-20 14:06:00 -0700251 if (gainFocus) {
252 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
253 // Select the first icon when we gain keyboard focus
254 mArrowNavigation = true;
Joe Onoratoc61cff92009-11-08 11:54:39 -0500255 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE,
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500256 SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700257 mRollo.mState.save();
258 }
259 } else {
260 if (mArrowNavigation) {
261 // Clear selection when we lose focus
262 mRollo.clearSelectedIcon();
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500263 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700264 mRollo.mState.save();
265 mArrowNavigation = false;
266 }
267 }
268 }
269
270 @Override
271 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700272
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800273 boolean handled = false;
274
Joe Onorato859b3a72009-10-28 15:17:01 -0400275 if (!isVisible()) {
276 return false;
277 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500278 final int iconCount = mRollo.mState.iconCount;
279
Mike Cleron7d5d7462009-10-20 14:06:00 -0700280 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
281 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500282 if (mLastSelection == SELECTION_HOME) {
283 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
284 mLauncher.closeAllApps(true);
285 } else {
286 int whichApp = mRollo.mState.selectedIconIndex;
287 if (whichApp >= 0) {
288 ApplicationInfo app = mAllAppsList.get(whichApp);
289 mLauncher.startActivitySafely(app.intent);
290 handled = true;
291 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700292 }
293 }
294 }
Jason Sams2e19c052009-10-20 18:19:55 -0700295
Joe Onoratoa13f5742009-11-02 17:15:19 -0500296 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700297 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700298
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800299 int currentSelection = mRollo.mState.selectedIconIndex;
300 int currentTopRow = Math.round(mRollo.mMessageProc.mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700301
Mike Cleron7d5d7462009-10-20 14:06:00 -0700302 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500303 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700304
Mike Cleron7d5d7462009-10-20 14:06:00 -0700305 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500306 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700307 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700308
Mike Cleron7d5d7462009-10-20 14:06:00 -0700309 int newSelection = currentSelection;
310
311 switch (keyCode) {
312 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500313 if (mLastSelection == SELECTION_HOME) {
314 mRollo.setHomeSelected(SELECTED_NONE);
315 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
316 if (lastRowCount == 0) {
317 lastRowCount = Defines.COLUMNS_PER_PAGE;
318 }
319 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
320 if (newSelection >= iconCount) {
321 newSelection = iconCount-1;
322 }
323 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
324 - (Defines.ROWS_PER_PAGE - 1);
325 if (target < 0) {
326 target = 0;
327 }
328 if (currentTopRow != target) {
329 mRollo.moveTo(target);
330 }
331 } else {
332 if (currentPageRow > 0) {
333 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
334 } else if (currentTopRow > 0) {
335 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
336 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
337 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700338 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800339 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700340 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800341
Joe Onoratoa13f5742009-11-02 17:15:19 -0500342 case KeyEvent.KEYCODE_DPAD_DOWN: {
343 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
344 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
345 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500346 if (mLastSelection != SELECTION_HOME) {
347 if (currentRow < rowCount-1) {
348 mRollo.setHomeSelected(SELECTED_NONE);
349 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
350 if (newSelection >= iconCount) {
351 // Go from D to G in this arrangement:
352 // A B C D
353 // E F G
354 newSelection = iconCount - 1;
355 }
356 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
357 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
358 Defines.ROWS_PER_PAGE + 1);
359 }
360 } else {
361 newSelection = -1;
362 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700363 }
364 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800365 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700366 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500367 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700368 case KeyEvent.KEYCODE_DPAD_LEFT:
369 if (currentPageCol > 0) {
370 newSelection = currentSelection - 1;
371 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800372 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700373 break;
374 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700375 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500376 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700377 newSelection = currentSelection + 1;
378 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800379 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700380 break;
381 }
382 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500383 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700384 mRollo.mState.save();
385 }
386 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800387 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700388 }
389
Joe Onorato93839052009-08-06 20:34:32 -0700390 @Override
391 public boolean onTouchEvent(MotionEvent ev)
392 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700393 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700394
Joe Onorato7bb17492009-09-24 17:51:01 -0700395 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700396 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700397 }
398
Joe Onoratofb0ca672009-09-14 17:55:46 -0400399 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700400 return true;
401 }
402
403 super.onTouchEvent(ev);
404
Joe Onoratofb0ca672009-09-14 17:55:46 -0400405 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700406 int y = (int)ev.getY();
407
Joe Onoratobcbeab82009-10-01 21:45:43 -0700408 int action = ev.getAction();
409 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400410 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700411 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
412 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500413 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700414 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700415 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700416 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700417 mTouchTracking = TRACKING_FLING;
418
419 mMotionDownRawX = (int)ev.getRawX();
420 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700421
Mike Cleron7d5d7462009-10-20 14:06:00 -0700422 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700423 mRollo.mState.newTouchDown = 1;
424
425 if (!mRollo.checkClickOK()) {
426 mRollo.clearSelectedIcon();
427 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700428 mDownIconIndex = mCurrentIconIndex
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500429 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700430 if (mDownIconIndex < 0) {
431 // if nothing was selected, no long press.
432 cancelLongPress();
433 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700434 }
435 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700436 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700437 mVelocity = VelocityTracker.obtain();
438 mVelocity.addMovement(ev);
439 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700440 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400441 break;
442 case MotionEvent.ACTION_MOVE:
443 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700444 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500445 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
446 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700447 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700448 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700449 int rawX = (int)ev.getRawX();
450 int rawY = (int)ev.getRawY();
451 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700452 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700453
Joe Onorato82ca5502009-10-15 16:59:23 -0700454 if (!mStartedScrolling && slop < mSlop) {
455 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700456 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700457 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
458 if (mDownIconIndex != mCurrentIconIndex) {
459 // If a different icon is selected, don't allow it to be picked up.
460 // This handles off-axis dragging.
461 cancelLongPress();
462 mCurrentIconIndex = -1;
463 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700464 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700465 if (!mStartedScrolling) {
466 cancelLongPress();
467 mCurrentIconIndex = -1;
468 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700469 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700470 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700471 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700472
Joe Onoratobcbeab82009-10-01 21:45:43 -0700473 mStartedScrolling = true;
474 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700475 mVelocity.addMovement(ev);
476 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700477 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400478 }
479 break;
480 case MotionEvent.ACTION_UP:
481 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700482 if (mTouchTracking == TRACKING_HOME) {
483 if (action == MotionEvent.ACTION_UP) {
484 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400485 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700486 mLauncher.closeAllApps(true);
487 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500488 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700489 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700490 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700491 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700492 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700493 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700494 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700495
Jason Sams12c14a82009-10-06 14:33:15 -0700496 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700497 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700498 mRollo.clearSelectedIcon();
499 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700500 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700501
Joe Onorato539ed9d2009-10-02 10:22:14 -0700502 if (mVelocity != null) {
503 mVelocity.recycle();
504 mVelocity = null;
505 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700506 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700507 mTouchTracking = TRACKING_NONE;
508 break;
Joe Onorato93839052009-08-06 20:34:32 -0700509 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700510
511 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700512 }
513
Joe Onorato6665c0f2009-09-02 15:27:24 -0700514 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700515 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400516 return;
517 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700518 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
519 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400520 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700521 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700522 mLauncher.startActivitySafely(app.intent);
523 }
524 }
525
526 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700527 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400528 return true;
529 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700530 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
531 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
532 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700533
534 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700535 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
536 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700537
Joe Onoratobcbeab82009-10-01 21:45:43 -0700538 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
539 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700540 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700541 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700542 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700543
Joe Onorato7bb17492009-09-24 17:51:01 -0700544 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700545 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700546 return true;
547 }
548
Joe Onorato5162ea92009-09-03 09:39:42 -0700549 public void setDragController(DragController dragger) {
550 mDragController = dragger;
551 }
552
553 public void onDropCompleted(View target, boolean success) {
554 }
555
Joe Onorato4db52312009-10-06 11:17:43 -0700556 /**
557 * Zoom to the specifed amount.
558 *
559 * @param amount [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700560 */
Jason Sams12c14a82009-10-06 14:33:15 -0700561 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700562 if (mRollo == null) {
563 return;
564 }
565
Joe Onoratofb0ca672009-09-14 17:55:46 -0400566 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400567 mRollo.clearSelectedIcon();
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500568 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato85a02a82009-09-08 12:34:22 -0700569 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700570 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700571 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700572 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700573 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700574 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400575 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400576 }
577
578 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700579 if (mRollo == null) {
580 return false;
581 }
Jason Sams12c14a82009-10-06 14:33:15 -0700582 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400583 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700584
Mike Cleronb6082fa02009-10-19 17:03:36 -0700585 /*
Joe Onorato93839052009-08-06 20:34:32 -0700586 @Override
587 public boolean onTrackballEvent(MotionEvent ev)
588 {
589 float x = ev.getX();
590 float y = ev.getY();
591 //Float tx = new Float(x);
592 //Float ty = new Float(y);
593 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
594
595
596 return true;
597 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700598 */
Joe Onorato93839052009-08-06 20:34:32 -0700599
Joe Onorato9c1289c2009-08-17 11:03:03 -0400600 public void setApps(ArrayList<ApplicationInfo> list) {
601 mAllAppsList = list;
602 if (mRollo != null) {
603 mRollo.setApps(list);
604 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400605 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700606 }
607
Joe Onoratoa8138d52009-10-06 19:25:30 -0700608 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500609 if (mAllAppsList == null) {
610 // Not done loading yet. We'll find out about it later.
611 return;
612 }
613
Joe Onoratoa8138d52009-10-06 19:25:30 -0700614 final int N = list.size();
615 if (mRollo != null) {
616 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
617 }
618
619 for (int i=0; i<N; i++) {
620 final ApplicationInfo item = list.get(i);
621 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
622 if (index < 0) {
623 index = -(index+1);
624 }
Joe Onorato0ace11a2009-11-05 15:41:27 -0500625 Log.d(TAG, "Adding app '" + item + "' at index " + index + " mRollo=" + mRollo);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700626 mAllAppsList.add(index, item);
627 if (mRollo != null) {
628 mRollo.addApp(index, item);
629 mRollo.mState.iconCount++;
630 }
631 }
632
633 if (mRollo != null) {
634 mRollo.saveAppsList();
635 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700636 }
637
Joe Onoratoa8138d52009-10-06 19:25:30 -0700638 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500639 if (mAllAppsList == null) {
640 // Not done loading yet. We'll find out about it later.
641 return;
642 }
643
Joe Onoratoa8138d52009-10-06 19:25:30 -0700644 final int N = list.size();
645 for (int i=0; i<N; i++) {
646 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400647 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700648 if (index >= 0) {
649 mAllAppsList.remove(index);
650 if (mRollo != null) {
651 mRollo.removeApp(index);
652 mRollo.mState.iconCount--;
653 }
654 } else {
655 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
656 // Try to recover. This should keep us from crashing for now.
657 }
658 }
659
660 if (mRollo != null) {
661 mRollo.saveAppsList();
662 }
663 }
664
665 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
666 // Just remove and add, because they may need to be re-sorted.
667 removeApps(list);
668 addApps(list);
669 }
670
671 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
672 public int compare(ApplicationInfo a, ApplicationInfo b) {
673 int result = a.title.toString().compareTo(b.toString());
674 if (result != 0) {
675 return result;
676 }
677 return a.intent.getComponent().compareTo(b.intent.getComponent());
678 }
679 };
680
Joe Onoratocb9f7982009-10-31 16:32:02 -0400681 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
682 ComponentName component = item.intent.getComponent();
683 final int N = list.size();
684 for (int i=0; i<N; i++) {
685 ApplicationInfo x = list.get(i);
686 if (x.intent.getComponent().equals(component)) {
687 return i;
688 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700689 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400690 return -1;
691 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700692
Joe Onoratoc567acb2009-08-31 14:34:43 -0700693 private static int countPages(int iconCount) {
694 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
695 int pages = iconCount / iconsPerPage;
696 if (pages*iconsPerPage != iconCount) {
697 pages++;
698 }
699 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 }
701
Joe Onorato93839052009-08-06 20:34:32 -0700702 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700703
Joe Onorato1feb3a82009-08-08 22:32:00 -0700704 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700705 private int mWidth;
706 private int mHeight;
707
708 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700709 private Script mScript;
710 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700711 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700712 private Script.Invokable mInvokeFling;
713 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700714
Jason Samsc1c521e2009-10-19 14:45:45 -0700715
Jason Samscd689e12009-09-29 15:28:22 -0700716 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700717 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700718 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700719 private ProgramFragment mPFTexMip;
720 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700721 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700722 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700723 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700724 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700725
Joe Onoratod63458b2009-10-15 21:19:09 -0700726 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500727 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700728 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700729
Joe Onoratobf15cb42009-08-07 14:33:40 -0700730 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700731 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700732 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700733
Joe Onoratobf15cb42009-08-07 14:33:40 -0700734 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700735 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700736 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700737 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700738
Joe Onorato6665c0f2009-09-02 15:27:24 -0700739 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700740 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700741
742 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400743 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700744
Jason Sams20df7c72009-11-05 12:30:24 -0800745 boolean mHasSurface = false;
746 private boolean mAppsDirty = false;
Jason Samsd8152b92009-10-13 17:19:10 -0700747
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700748 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700749 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700750
Jason Sams78aebd82009-09-15 13:06:59 -0700751 class BaseAlloc {
752 Allocation mAlloc;
753 Type mType;
754
755 void save() {
756 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700757 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700758 }
759
Jason Sams12c14a82009-10-06 14:33:15 -0700760 class AAMessage extends RenderScript.RSMessage {
761 public void run() {
762 mPosX = ((float)mData[0]) / (1 << 16);
763 mVelocity = ((float)mData[1]) / (1 << 16);
764 mZoom = ((float)mData[2]) / (1 << 16);
765 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
766 }
767 float mZoom;
768 float mPosX;
769 float mVelocity;
770 }
771 AAMessage mMessageProc;
772
Jason Sams476339d2009-09-29 18:14:38 -0700773 private boolean checkClickOK() {
774 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams2e19c052009-10-20 18:19:55 -0700775 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
776 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700777 }
778
Jason Sams78aebd82009-09-15 13:06:59 -0700779 class Params extends BaseAlloc {
780 Params() {
781 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
782 mAlloc = Allocation.createTyped(mRS, mType);
783 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700784 }
Jason Sams78aebd82009-09-15 13:06:59 -0700785 public int bubbleWidth;
786 public int bubbleHeight;
787 public int bubbleBitmapWidth;
788 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700789
Joe Onoratobcbeab82009-10-01 21:45:43 -0700790 public int homeButtonWidth;
791 public int homeButtonHeight;
792 public int homeButtonTextureWidth;
793 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700794 }
795
796 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700797 public float newPositionX;
798 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700799 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700800 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700801 public int selectedIconIndex = -1;
802 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700803 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700804 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700805 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700806
807 State() {
808 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
809 mAlloc = Allocation.createTyped(mRS, mType);
810 save();
811 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700812 }
813
814 public RolloRS() {
815 }
816
817 public void init(Resources res, int width, int height) {
818 mRes = res;
819 mWidth = width;
820 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700821 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700822 initProgramVertex();
823 initProgramFragment();
824 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700825 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700826 initGl();
827 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700828 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700829 initRs();
830 }
831
Jason Sams0aa71662009-10-02 18:43:18 -0700832 public void initMesh() {
833 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
834 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
835
Jason Samsd8152b92009-10-13 17:19:10 -0700836 float y = 0;
837 float z = 0;
838 for (int ct=0; ct < 200; ct++) {
839 float angle = 0;
840 float maxAngle = 3.14f * 0.16f;
841 float l = 1.f;
842
843 l = 1 - ((ct-5) * 0.10f);
844 if (ct > 7) {
845 angle = maxAngle * (ct - 7) * 0.2f;
846 angle = Math.min(angle, maxAngle);
847 }
Jason Samsc8514792009-10-29 14:27:29 -0700848 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700849 l = Math.min(1.0f, l);
850
851 y += 0.1f * Math.cos(angle);
852 z += 0.1f * Math.sin(angle);
853
854 float t = 0.1f * ct;
855 float ds = 0.08f;
856 tm.setColor(l, l, l, 0.99f);
857 tm.setTexture(ds, t);
858 tm.addVertex(-0.5f, y, z);
859 tm.setTexture(1 - ds, t);
860 tm.addVertex(0.5f, y, z);
861 }
862 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
863 tm.addTriangle(ct, ct+1, ct+2);
864 tm.addTriangle(ct+1, ct+3, ct+2);
865 }
866 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700867 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700868 }
869
Jason Samscd689e12009-09-29 15:28:22 -0700870 private void initProgramVertex() {
871 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
872 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700873
874 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700875 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700876 mPV = pvb.create();
877 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700878 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700879
Jason Sams37e7c2b2009-10-19 12:55:43 -0700880 //pva = new ProgramVertex.MatrixAllocation(mRS);
881 //pva.setupOrthoWindow(mWidth, mHeight);
882 //pvb.setTextureMatrixEnable(true);
883 //mPVOrtho = pvb.create();
884 //mPVOrtho.setName("PVOrtho");
885 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700886
887 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700888 }
Joe Onorato93839052009-08-06 20:34:32 -0700889
Jason Samscd689e12009-09-29 15:28:22 -0700890 private void initProgramFragment() {
891 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700892 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700893 sb.setMag(Sampler.Value.LINEAR);
894 sb.setWrapS(Sampler.Value.CLAMP);
895 sb.setWrapT(Sampler.Value.CLAMP);
896 Sampler linear = sb.create();
897
898 sb.setMin(Sampler.Value.NEAREST);
899 sb.setMag(Sampler.Value.NEAREST);
900 Sampler nearest = sb.create();
901
902 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700903 //mPFColor = bf.create();
904 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700905
906 bf.setTexEnable(true, 0);
907 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700908 mPFTexMip = bf.create();
909 mPFTexMip.setName("PFTexMip");
910 mPFTexMip.bindSampler(linear, 0);
911
912 mPFTexNearest = bf.create();
913 mPFTexNearest.setName("PFTexNearest");
914 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700915 }
916
917 private void initProgramStore() {
918 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
919 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700920 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700921 bs.setDitherEnable(true);
922 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
923 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
924 mPSIcons = bs.create();
925 mPSIcons.setName("PSIcons");
926
927 //bs.setDitherEnable(false);
928 //mPSText = bs.create();
929 //mPSText.setName("PSText");
930 }
931
932 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700933 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700934 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700935 }
Jason Sams78aebd82009-09-15 13:06:59 -0700936
Joe Onorato1feb3a82009-08-08 22:32:00 -0700937 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700938 mParams = new Params();
939 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700940
Joe Onoratobf15cb42009-08-07 14:33:40 -0700941 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700942
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700943 mParams.bubbleWidth = bubble.getBubbleWidth();
944 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
945 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
946 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700947
Joe Onoratod63458b2009-10-15 21:19:09 -0700948 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
949 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
950 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500951 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
952 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
953 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -0700954 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
955 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
956 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700957 mParams.homeButtonWidth = 76;
958 mParams.homeButtonHeight = 68;
959 mParams.homeButtonTextureWidth = 128;
960 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700961
Joe Onoratod63458b2009-10-15 21:19:09 -0700962 mState.homeButtonId = mHomeButtonNormal.getID();
963
Joe Onorato1feb3a82009-08-08 22:32:00 -0700964 mParams.save();
965 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966
Joe Onorato1291a8c2009-09-15 15:07:25 -0400967 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
968 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
969 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700970
Joe Onorato9c1289c2009-08-17 11:03:03 -0400971 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700972 }
973
Jason Sams37e7c2b2009-10-19 12:55:43 -0700974 private void initScript(int id) {
975 }
976
977 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700978 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700979 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700980 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700981 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700982 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
983 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700984 mInvokeMove = sb.addInvokable("move");
985 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700986 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700987 mInvokeResetWAR = sb.addInvokable("resetHWWar");
988 mScript = sb.create();
989 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
990 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
991 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
992 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
993 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700994
Jason Sams12c14a82009-10-06 14:33:15 -0700995 mMessageProc = new AAMessage();
996 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700997 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700998 }
Joe Onorato93839052009-08-06 20:34:32 -0700999
Jason Sams20df7c72009-11-05 12:30:24 -08001000 private void uploadApps(ArrayList<ApplicationInfo> list) {
1001 for (int i=0; i < mState.iconCount; i++) {
1002 uploadAppIcon(i, list.get(i));
1003 }
1004 }
1005
1006 void dirtyCheck() {
1007 if (mAppsDirty && mHasSurface) {
1008 uploadApps(mAllAppsList);
1009 saveAppsList();
1010 mAppsDirty = false;
1011 }
1012 }
1013
Joe Onorato9c1289c2009-08-17 11:03:03 -04001014 private void setApps(ArrayList<ApplicationInfo> list) {
1015 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001016 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001017 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001018 allocCount = 1;
1019 }
1020
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001022 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001023 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024
1025 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001026 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001027 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001028
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001029 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001030
1031 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1032
Joe Onorato9c1289c2009-08-17 11:03:03 -04001033 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001034 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001035 saveAppsList();
1036 }
1037
Jason Samsc8514792009-10-29 14:27:29 -07001038 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1039 int black[] = new int[w > h ? w : h];
1040 Allocation.Adapter2D a = alloc.createAdapter2D();
1041 int mip = 0;
1042 while (w > 1 || h > 1) {
1043 a.subData(0, 0, 1, h, black);
1044 a.subData(w-1, 0, 1, h, black);
1045 a.subData(0, 0, w, 1, black);
1046 a.subData(0, h-1, w, 1, black);
1047 mip++;
1048 w = (w + 1) >> 1;
1049 h = (h + 1) >> 1;
1050 a.setConstraint(Dimension.LOD, mip);
1051 }
1052 a.subData(0, 0, 1, 1, black);
1053 }
1054
Joe Onoratoa8138d52009-10-06 19:25:30 -07001055 private void uploadAppIcon(int index, ApplicationInfo item) {
1056 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001057 Element.RGBA_8888(mRS), true);
1058 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1059
Joe Onoratoa8138d52009-10-06 19:25:30 -07001060 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001061 Element.RGBA_8888(mRS), true);
1062 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001063
1064 mIcons[index].uploadToTexture(0);
1065 mLabels[index].uploadToTexture(0);
1066
1067 mIconIds[index] = mIcons[index].getID();
1068 mLabelIds[index] = mLabels[index].getID();
1069 }
1070
1071 /**
1072 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1073 * fill in the values and call saveAppsList().
1074 */
1075 private void reallocAppsList(int count) {
1076 Allocation[] icons = new Allocation[count];
1077 int[] iconIds = new int[count];
1078 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1079
1080 Allocation[] labels = new Allocation[count];
1081 int[] labelIds = new int[count];
1082 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1083
1084 final int oldCount = mIcons.length;
1085
1086 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1087 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1088 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1089 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1090
1091 mIcons = icons;
1092 mIconIds = iconIds;
1093 mLabels = labels;
1094 mLabelIds = labelIds;
1095 }
1096
1097 /**
1098 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1099 */
1100 private void addApp(int index, ApplicationInfo item) {
1101 final int count = mState.iconCount - index;
1102 final int dest = index + 1;
1103
1104 System.arraycopy(mIcons, index, mIcons, dest, count);
1105 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1106 System.arraycopy(mLabels, index, mLabels, dest, count);
1107 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1108
Jason Sams20df7c72009-11-05 12:30:24 -08001109 if (mHasSurface ) {
1110 uploadAppIcon(index, item);
1111 } else {
1112 mAppsDirty = true;
1113 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001114 }
1115
1116 /**
1117 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1118 */
1119 private void removeApp(int index) {
1120 final int count = mState.iconCount - index - 1;
1121 final int src = index + 1;
1122
1123 System.arraycopy(mIcons, src, mIcons, index, count);
1124 System.arraycopy(mIconIds, src, mIconIds, index, count);
1125 System.arraycopy(mLabels, src, mLabels, index, count);
1126 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1127
1128 final int last = mState.iconCount - 1;
1129 mIcons[last] = null;
1130 mIconIds[last] = 0;
1131 mLabels[last] = null;
1132 mLabelIds[last] = 0;
1133 }
1134
1135 /**
1136 * Send the apps list structures to RS.
1137 */
1138 private void saveAppsList() {
1139 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001140
Joe Onoratoa8138d52009-10-06 19:25:30 -07001141 mAllocIconIds.data(mIconIds);
1142 mAllocLabelIds.data(mLabelIds);
1143
Jason Sams37e7c2b2009-10-19 12:55:43 -07001144 if (mScript != null) { // this happens when we init it
1145 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1146 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001147 }
1148
1149 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001150
1151 // Note: mScript may be null if we haven't initialized it yet.
1152 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001153 if (mInvokeResetWAR != null) {
1154 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001155 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001156 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001157 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001158
1159 void initTouchState() {
1160 int width = getWidth();
1161 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001162 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1163 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001164
Jason Sams37e7c2b2009-10-19 12:55:43 -07001165 int centerY = (height / 2);
1166 mTouchYBorders[0] = centerY - (cellHeight * 2);
1167 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001168 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001169 mTouchYBorders[3] = centerY + cellHeight;
1170 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001171
Joe Onorato6665c0f2009-09-02 15:27:24 -07001172 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001173 mTouchXBorders[0] = 0;
1174 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001175 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001176 mTouchXBorders[3] = centerX + (width / 4);
1177 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001178 }
1179
Joe Onorato664457d2009-10-28 16:30:34 -04001180 void fling() {
1181 mInvokeFling.execute();
1182 }
1183
1184 void move() {
1185 mInvokeMove.execute();
1186 }
1187
1188 void moveTo(float row) {
1189 mState.targetPos = row;
1190 mState.save();
1191 mInvokeMoveTo.execute();
1192 }
1193
Jason Sams37e7c2b2009-10-19 12:55:43 -07001194 int chooseTappedIcon(int x, int y, float pos) {
1195 // Adjust for scroll position if not zero.
1196 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001197
Joe Onorato6665c0f2009-09-02 15:27:24 -07001198 int col = -1;
1199 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001200 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1201 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1202 col = i;
1203 break;
1204 }
1205 }
1206 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1207 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1208 row = i;
1209 break;
1210 }
1211 }
1212
1213 if (row < 0 || col < 0) {
1214 return -1;
1215 }
1216
Joe Onorato664457d2009-10-28 16:30:34 -04001217 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001218 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001219
Joe Onorato664457d2009-10-28 16:30:34 -04001220 if (index >= mState.iconCount) {
1221 return -1;
1222 } else {
1223 return index;
1224 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001225 }
1226
Joe Onorato6665c0f2009-09-02 15:27:24 -07001227 /**
1228 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001229 *
1230 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001231 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001232 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001233 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001234 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001235 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001236 }
1237
Joe Onoratoc61cff92009-11-08 11:54:39 -05001238 /**
1239 * Select the icon at the given index.
1240 *
1241 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001242 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001243 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001244 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001245 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001246 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001247 if (mLastSelection == SELECTION_ICONS) {
1248 mLastSelection = SELECTION_NONE;
1249 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001250 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001251 if (pressed == SELECTED_FOCUSED) {
1252 mLastSelection = SELECTION_ICONS;
1253 }
1254
Joe Onorato6665c0f2009-09-02 15:27:24 -07001255 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001256
1257 Bitmap selectionBitmap = mSelectionBitmap;
1258
1259 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001260 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1261 pressed == SELECTED_PRESSED,
Joe Onorato1291a8c2009-09-15 15:07:25 -04001262 mAllAppsList.get(index).iconBitmap);
1263
1264 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001265 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001266 mSelectedIcon.uploadToTexture(0);
1267 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001268 }
1269 }
1270
1271 /**
1272 * You need to call save() on mState on your own after calling this.
1273 */
1274 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001275 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001276 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001277
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001278 void setHomeSelected(int mode) {
1279 switch (mode) {
1280 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001281 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001282 break;
1283 case SELECTED_FOCUSED:
1284 mLastSelection = SELECTION_HOME;
1285 mState.homeButtonId = mHomeButtonFocused.getID();
1286 break;
1287 case SELECTED_PRESSED:
1288 mState.homeButtonId = mHomeButtonPressed.getID();
1289 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001290 }
1291 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001292 }
Joe Onorato93839052009-08-06 20:34:32 -07001293}
1294
1295