blob: 722213d424db4e21ab5340baa95ac7c7e852ed72 [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 Onorato1feb3a82009-08-08 22:32:00 -0700115
Joe Onorato6665c0f2009-09-02 15:27:24 -0700116 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700117 public static final int ALLOC_PARAMS = 0;
118 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700119 public static final int ALLOC_ICON_IDS = 3;
120 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700121
122 public static final int COLUMNS_PER_PAGE = 4;
123 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700124
Joe Onorato6665c0f2009-09-02 15:27:24 -0700125 public static final int ICON_WIDTH_PX = 64;
126 public static final int ICON_TEXTURE_WIDTH_PX = 128;
127
128 public static final int ICON_HEIGHT_PX = 64;
129 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700130
131 public int SCREEN_WIDTH_PX;
132 public int SCREEN_HEIGHT_PX;
133
Joe Onoratobcbeab82009-10-01 21:45:43 -0700134 public void recompute(int w, int h) {
135 SCREEN_WIDTH_PX = 480;
136 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700137 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700138 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700139
140 public AllAppsView(Context context, AttributeSet attrs) {
141 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700142 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400143 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700144 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700145 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700146 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700147 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
148
Joe Onorato6665c0f2009-09-02 15:27:24 -0700149 setOnClickListener(this);
150 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700151 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700152 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700153 }
154
Joe Onoratob39e51a2009-10-28 15:47:49 -0400155 /**
156 * If you have an attached click listener, View always plays the click sound!?!?
157 * Deal with sound effects by hand.
158 */
159 public void reallyPlaySoundEffect(int sound) {
160 boolean old = isSoundEffectsEnabled();
161 setSoundEffectsEnabled(true);
162 playSoundEffect(sound);
163 setSoundEffectsEnabled(old);
164 }
165
Joe Onorato7c312c12009-08-13 21:36:53 -0700166 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
167 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700168 }
169
Joe Onorato6665c0f2009-09-02 15:27:24 -0700170 public void setLauncher(Launcher launcher) {
171 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700172 }
173
Joe Onorato1feb3a82009-08-08 22:32:00 -0700174 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700175 public void surfaceDestroyed(SurfaceHolder holder) {
176 super.surfaceDestroyed(holder);
Jason Sams20df7c72009-11-05 12:30:24 -0800177 mRollo.mHasSurface = false;
Joe Onorato7bb17492009-09-24 17:51:01 -0700178 }
179
180 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700181 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500182 Log.d(TAG, "starting surfaceChanged");
Joe Onorato6665c0f2009-09-02 15:27:24 -0700183 long startTime = SystemClock.uptimeMillis();
184
Joe Onorato080d9b62009-11-02 12:01:11 -0500185 super.surfaceChanged(holder, format, w, h);
186
Jason Sams90396672009-11-03 13:59:34 -0800187 if (mRS == null) {
Jason Sams81134792009-10-27 15:38:42 -0700188
Jason Sams90396672009-11-03 13:59:34 -0800189 mRS = createRenderScript(true);
190 mRollo = new RolloRS();
Jason Sams20df7c72009-11-05 12:30:24 -0800191 mRollo.mHasSurface = true;
Jason Sams90396672009-11-03 13:59:34 -0800192 mRollo.init(getResources(), w, h);
193 if (mAllAppsList != null) {
194 mRollo.setApps(mAllAppsList);
195 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
196 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800197 if (mShouldGainFocus) {
198 gainFocus();
199 mShouldGainFocus = false;
200 }
Jason Sams20df7c72009-11-05 12:30:24 -0800201 } else {
202 mRollo.mHasSurface = true;
203 mRollo.dirtyCheck();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700205
206 Resources res = getContext().getResources();
207 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700208
209 long endTime = SystemClock.uptimeMillis();
210 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700211 }
Jason Sams2e19c052009-10-20 18:19:55 -0700212
Joe Onorato93839052009-08-06 20:34:32 -0700213 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800214 public void onWindowFocusChanged(boolean hasWindowFocus) {
215 super.onWindowFocusChanged(hasWindowFocus);
216 if (mArrowNavigation) {
217 if (!hasWindowFocus) {
218 // Clear selection when we lose window focus
219 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500220 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800221 mRollo.clearSelectedIcon();
222 mRollo.mState.save();
223 } else if (hasWindowFocus) {
224 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500225 if (mLastSelection == SELECTION_ICONS) {
226 int selection = mLastSelectedIcon;
227 final int firstIcon = Math.round(mRollo.mMessageProc.mPosX) *
228 Defines.COLUMNS_PER_PAGE;
229 if (selection < 0 || // No selection
230 selection < firstIcon || // off the top of the screen
231 selection >= mRollo.mState.iconCount || // past last icon
232 selection >= firstIcon + // past last icon on screen
233 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
234 selection = firstIcon;
235 }
236
237 // Select the first icon when we gain window focus
238 mRollo.selectIcon(selection, SELECTED_FOCUSED);
239 mRollo.mState.save();
240 } else if (mLastSelection == SELECTION_HOME) {
241 mRollo.setHomeSelected(SELECTED_FOCUSED);
242 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800243 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800244 }
245 }
246 }
247 }
248
249 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700250 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
251 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400252
253 if (!isVisible()) {
254 return;
255 }
256
Mike Cleron7d5d7462009-10-20 14:06:00 -0700257 if (gainFocus) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800258 if (mRollo != null) {
259 gainFocus();
260 } else {
261 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700262 }
263 } else {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800264 if (mRollo != null) {
265 if (mArrowNavigation) {
266 // Clear selection when we lose focus
267 mRollo.clearSelectedIcon();
268 mRollo.setHomeSelected(SELECTED_NONE);
269 mRollo.mState.save();
270 mArrowNavigation = false;
271 }
272 } else {
273 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700274 }
275 }
276 }
277
Mike Cleronb64b67a2009-11-08 14:56:25 -0800278 private void gainFocus() {
279 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
280 // Select the first icon when we gain keyboard focus
281 mArrowNavigation = true;
282 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE,
283 SELECTED_FOCUSED);
284 mRollo.mState.save();
285 }
286 }
287
Mike Cleron7d5d7462009-10-20 14:06:00 -0700288 @Override
289 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700290
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800291 boolean handled = false;
292
Joe Onorato859b3a72009-10-28 15:17:01 -0400293 if (!isVisible()) {
294 return false;
295 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500296 final int iconCount = mRollo.mState.iconCount;
297
Mike Cleron7d5d7462009-10-20 14:06:00 -0700298 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
299 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500300 if (mLastSelection == SELECTION_HOME) {
301 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
302 mLauncher.closeAllApps(true);
303 } else {
304 int whichApp = mRollo.mState.selectedIconIndex;
305 if (whichApp >= 0) {
306 ApplicationInfo app = mAllAppsList.get(whichApp);
307 mLauncher.startActivitySafely(app.intent);
308 handled = true;
309 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700310 }
311 }
312 }
Jason Sams2e19c052009-10-20 18:19:55 -0700313
Joe Onoratoa13f5742009-11-02 17:15:19 -0500314 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700315 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700316
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800317 int currentSelection = mRollo.mState.selectedIconIndex;
318 int currentTopRow = Math.round(mRollo.mMessageProc.mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700319
Mike Cleron7d5d7462009-10-20 14:06:00 -0700320 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500321 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700322
Mike Cleron7d5d7462009-10-20 14:06:00 -0700323 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500324 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700325 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700326
Mike Cleron7d5d7462009-10-20 14:06:00 -0700327 int newSelection = currentSelection;
328
329 switch (keyCode) {
330 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500331 if (mLastSelection == SELECTION_HOME) {
332 mRollo.setHomeSelected(SELECTED_NONE);
333 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
334 if (lastRowCount == 0) {
335 lastRowCount = Defines.COLUMNS_PER_PAGE;
336 }
337 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
338 if (newSelection >= iconCount) {
339 newSelection = iconCount-1;
340 }
341 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
342 - (Defines.ROWS_PER_PAGE - 1);
343 if (target < 0) {
344 target = 0;
345 }
346 if (currentTopRow != target) {
347 mRollo.moveTo(target);
348 }
349 } else {
350 if (currentPageRow > 0) {
351 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
352 } else if (currentTopRow > 0) {
353 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
354 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
355 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700356 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800357 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700358 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800359
Joe Onoratoa13f5742009-11-02 17:15:19 -0500360 case KeyEvent.KEYCODE_DPAD_DOWN: {
361 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
362 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
363 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500364 if (mLastSelection != SELECTION_HOME) {
365 if (currentRow < rowCount-1) {
366 mRollo.setHomeSelected(SELECTED_NONE);
367 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
368 if (newSelection >= iconCount) {
369 // Go from D to G in this arrangement:
370 // A B C D
371 // E F G
372 newSelection = iconCount - 1;
373 }
374 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
375 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
376 Defines.ROWS_PER_PAGE + 1);
377 }
378 } else {
379 newSelection = -1;
380 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700381 }
382 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800383 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700384 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500385 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700386 case KeyEvent.KEYCODE_DPAD_LEFT:
387 if (currentPageCol > 0) {
388 newSelection = currentSelection - 1;
389 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800390 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700391 break;
392 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700393 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500394 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700395 newSelection = currentSelection + 1;
396 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800397 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700398 break;
399 }
400 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500401 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700402 mRollo.mState.save();
403 }
404 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800405 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700406 }
407
Joe Onorato93839052009-08-06 20:34:32 -0700408 @Override
409 public boolean onTouchEvent(MotionEvent ev)
410 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700411 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700412
Joe Onorato7bb17492009-09-24 17:51:01 -0700413 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700414 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700415 }
416
Joe Onoratofb0ca672009-09-14 17:55:46 -0400417 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700418 return true;
419 }
420
421 super.onTouchEvent(ev);
422
Joe Onoratofb0ca672009-09-14 17:55:46 -0400423 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700424 int y = (int)ev.getY();
425
Joe Onoratobcbeab82009-10-01 21:45:43 -0700426 int action = ev.getAction();
427 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400428 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700429 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
430 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500431 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700432 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700433 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700434 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700435 mTouchTracking = TRACKING_FLING;
436
437 mMotionDownRawX = (int)ev.getRawX();
438 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700439
Mike Cleron7d5d7462009-10-20 14:06:00 -0700440 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700441 mRollo.mState.newTouchDown = 1;
442
443 if (!mRollo.checkClickOK()) {
444 mRollo.clearSelectedIcon();
445 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700446 mDownIconIndex = mCurrentIconIndex
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500447 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700448 if (mDownIconIndex < 0) {
449 // if nothing was selected, no long press.
450 cancelLongPress();
451 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700452 }
453 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700454 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700455 mVelocity = VelocityTracker.obtain();
456 mVelocity.addMovement(ev);
457 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700458 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400459 break;
460 case MotionEvent.ACTION_MOVE:
461 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700462 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500463 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
464 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700465 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700466 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700467 int rawX = (int)ev.getRawX();
468 int rawY = (int)ev.getRawY();
469 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700470 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700471
Joe Onorato82ca5502009-10-15 16:59:23 -0700472 if (!mStartedScrolling && slop < mSlop) {
473 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700474 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700475 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
476 if (mDownIconIndex != mCurrentIconIndex) {
477 // If a different icon is selected, don't allow it to be picked up.
478 // This handles off-axis dragging.
479 cancelLongPress();
480 mCurrentIconIndex = -1;
481 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700482 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700483 if (!mStartedScrolling) {
484 cancelLongPress();
485 mCurrentIconIndex = -1;
486 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700487 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700488 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700489 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700490
Joe Onoratobcbeab82009-10-01 21:45:43 -0700491 mStartedScrolling = true;
492 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700493 mVelocity.addMovement(ev);
494 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700495 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400496 }
497 break;
498 case MotionEvent.ACTION_UP:
499 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700500 if (mTouchTracking == TRACKING_HOME) {
501 if (action == MotionEvent.ACTION_UP) {
502 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400503 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700504 mLauncher.closeAllApps(true);
505 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500506 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700507 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700508 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700509 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700510 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700511 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700512 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700513
Jason Sams12c14a82009-10-06 14:33:15 -0700514 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700515 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700516 mRollo.clearSelectedIcon();
517 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700518 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700519
Joe Onorato539ed9d2009-10-02 10:22:14 -0700520 if (mVelocity != null) {
521 mVelocity.recycle();
522 mVelocity = null;
523 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700524 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700525 mTouchTracking = TRACKING_NONE;
526 break;
Joe Onorato93839052009-08-06 20:34:32 -0700527 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700528
529 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700530 }
531
Joe Onorato6665c0f2009-09-02 15:27:24 -0700532 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700533 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400534 return;
535 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700536 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
537 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400538 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700539 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700540 mLauncher.startActivitySafely(app.intent);
541 }
542 }
543
544 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700545 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400546 return true;
547 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700548 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
549 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
550 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700551
552 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700553 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
554 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700555
Joe Onoratobcbeab82009-10-01 21:45:43 -0700556 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
557 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700558 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700559 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700560 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700561
Joe Onorato7bb17492009-09-24 17:51:01 -0700562 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700563 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700564 return true;
565 }
566
Joe Onorato5162ea92009-09-03 09:39:42 -0700567 public void setDragController(DragController dragger) {
568 mDragController = dragger;
569 }
570
571 public void onDropCompleted(View target, boolean success) {
572 }
573
Joe Onorato4db52312009-10-06 11:17:43 -0700574 /**
575 * Zoom to the specifed amount.
576 *
577 * @param amount [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700578 */
Jason Sams12c14a82009-10-06 14:33:15 -0700579 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700580 if (mRollo == null) {
581 return;
582 }
583
Joe Onoratofb0ca672009-09-14 17:55:46 -0400584 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400585 mRollo.clearSelectedIcon();
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500586 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato85a02a82009-09-08 12:34:22 -0700587 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700588 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700589 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700590 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700591 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700592 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400593 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400594 }
595
596 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700597 if (mRollo == null) {
598 return false;
599 }
Jason Sams12c14a82009-10-06 14:33:15 -0700600 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400601 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700602
Mike Cleronb6082fa02009-10-19 17:03:36 -0700603 /*
Joe Onorato93839052009-08-06 20:34:32 -0700604 @Override
605 public boolean onTrackballEvent(MotionEvent ev)
606 {
607 float x = ev.getX();
608 float y = ev.getY();
609 //Float tx = new Float(x);
610 //Float ty = new Float(y);
611 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
612
613
614 return true;
615 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700616 */
Joe Onorato93839052009-08-06 20:34:32 -0700617
Joe Onorato9c1289c2009-08-17 11:03:03 -0400618 public void setApps(ArrayList<ApplicationInfo> list) {
619 mAllAppsList = list;
620 if (mRollo != null) {
621 mRollo.setApps(list);
622 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400623 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700624 }
625
Joe Onoratoa8138d52009-10-06 19:25:30 -0700626 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500627 if (mAllAppsList == null) {
628 // Not done loading yet. We'll find out about it later.
629 return;
630 }
631
Joe Onoratoa8138d52009-10-06 19:25:30 -0700632 final int N = list.size();
633 if (mRollo != null) {
634 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
635 }
636
637 for (int i=0; i<N; i++) {
638 final ApplicationInfo item = list.get(i);
639 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
640 if (index < 0) {
641 index = -(index+1);
642 }
Joe Onorato0ace11a2009-11-05 15:41:27 -0500643 Log.d(TAG, "Adding app '" + item + "' at index " + index + " mRollo=" + mRollo);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700644 mAllAppsList.add(index, item);
645 if (mRollo != null) {
646 mRollo.addApp(index, item);
647 mRollo.mState.iconCount++;
648 }
649 }
650
651 if (mRollo != null) {
652 mRollo.saveAppsList();
653 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700654 }
655
Joe Onoratoa8138d52009-10-06 19:25:30 -0700656 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500657 if (mAllAppsList == null) {
658 // Not done loading yet. We'll find out about it later.
659 return;
660 }
661
Joe Onoratoa8138d52009-10-06 19:25:30 -0700662 final int N = list.size();
663 for (int i=0; i<N; i++) {
664 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400665 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700666 if (index >= 0) {
667 mAllAppsList.remove(index);
668 if (mRollo != null) {
669 mRollo.removeApp(index);
670 mRollo.mState.iconCount--;
671 }
672 } else {
673 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
674 // Try to recover. This should keep us from crashing for now.
675 }
676 }
677
678 if (mRollo != null) {
679 mRollo.saveAppsList();
680 }
681 }
682
683 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
684 // Just remove and add, because they may need to be re-sorted.
685 removeApps(list);
686 addApps(list);
687 }
688
689 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
690 public int compare(ApplicationInfo a, ApplicationInfo b) {
691 int result = a.title.toString().compareTo(b.toString());
692 if (result != 0) {
693 return result;
694 }
695 return a.intent.getComponent().compareTo(b.intent.getComponent());
696 }
697 };
698
Joe Onoratocb9f7982009-10-31 16:32:02 -0400699 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
700 ComponentName component = item.intent.getComponent();
701 final int N = list.size();
702 for (int i=0; i<N; i++) {
703 ApplicationInfo x = list.get(i);
704 if (x.intent.getComponent().equals(component)) {
705 return i;
706 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700707 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400708 return -1;
709 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700710
Joe Onoratoc567acb2009-08-31 14:34:43 -0700711 private static int countPages(int iconCount) {
712 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
713 int pages = iconCount / iconsPerPage;
714 if (pages*iconsPerPage != iconCount) {
715 pages++;
716 }
717 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400718 }
719
Joe Onorato93839052009-08-06 20:34:32 -0700720 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700721
Joe Onorato1feb3a82009-08-08 22:32:00 -0700722 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700723 private int mWidth;
724 private int mHeight;
725
726 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700727 private Script mScript;
728 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700729 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700730 private Script.Invokable mInvokeFling;
731 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700732
Jason Samsc1c521e2009-10-19 14:45:45 -0700733
Jason Samscd689e12009-09-29 15:28:22 -0700734 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700735 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700736 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700737 private ProgramFragment mPFTexMip;
738 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700739 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700740 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700741 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700742 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700743
Joe Onoratod63458b2009-10-15 21:19:09 -0700744 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500745 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700746 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700747
Joe Onoratobf15cb42009-08-07 14:33:40 -0700748 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700749 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700750 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700751
Joe Onoratobf15cb42009-08-07 14:33:40 -0700752 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700753 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700754 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700755 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700756
Joe Onorato6665c0f2009-09-02 15:27:24 -0700757 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700758 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700759
760 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400761 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700762
Jason Sams20df7c72009-11-05 12:30:24 -0800763 boolean mHasSurface = false;
764 private boolean mAppsDirty = false;
Jason Samsd8152b92009-10-13 17:19:10 -0700765
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700766 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700767 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700768
Jason Sams78aebd82009-09-15 13:06:59 -0700769 class BaseAlloc {
770 Allocation mAlloc;
771 Type mType;
772
773 void save() {
774 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700775 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700776 }
777
Jason Sams12c14a82009-10-06 14:33:15 -0700778 class AAMessage extends RenderScript.RSMessage {
779 public void run() {
780 mPosX = ((float)mData[0]) / (1 << 16);
781 mVelocity = ((float)mData[1]) / (1 << 16);
782 mZoom = ((float)mData[2]) / (1 << 16);
783 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
784 }
785 float mZoom;
786 float mPosX;
787 float mVelocity;
788 }
789 AAMessage mMessageProc;
790
Jason Sams476339d2009-09-29 18:14:38 -0700791 private boolean checkClickOK() {
792 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams2e19c052009-10-20 18:19:55 -0700793 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
794 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700795 }
796
Jason Sams78aebd82009-09-15 13:06:59 -0700797 class Params extends BaseAlloc {
798 Params() {
799 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
800 mAlloc = Allocation.createTyped(mRS, mType);
801 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700802 }
Jason Sams78aebd82009-09-15 13:06:59 -0700803 public int bubbleWidth;
804 public int bubbleHeight;
805 public int bubbleBitmapWidth;
806 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700807
Joe Onoratobcbeab82009-10-01 21:45:43 -0700808 public int homeButtonWidth;
809 public int homeButtonHeight;
810 public int homeButtonTextureWidth;
811 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700812 }
813
814 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700815 public float newPositionX;
816 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700817 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700818 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700819 public int selectedIconIndex = -1;
820 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700821 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700822 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700823 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700824
825 State() {
826 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
827 mAlloc = Allocation.createTyped(mRS, mType);
828 save();
829 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700830 }
831
832 public RolloRS() {
833 }
834
835 public void init(Resources res, int width, int height) {
836 mRes = res;
837 mWidth = width;
838 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700839 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700840 initProgramVertex();
841 initProgramFragment();
842 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700843 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700844 initGl();
845 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700846 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700847 initRs();
848 }
849
Jason Sams0aa71662009-10-02 18:43:18 -0700850 public void initMesh() {
851 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
852 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
853
Jason Samsd8152b92009-10-13 17:19:10 -0700854 float y = 0;
855 float z = 0;
856 for (int ct=0; ct < 200; ct++) {
857 float angle = 0;
858 float maxAngle = 3.14f * 0.16f;
859 float l = 1.f;
860
861 l = 1 - ((ct-5) * 0.10f);
862 if (ct > 7) {
863 angle = maxAngle * (ct - 7) * 0.2f;
864 angle = Math.min(angle, maxAngle);
865 }
Jason Samsc8514792009-10-29 14:27:29 -0700866 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700867 l = Math.min(1.0f, l);
868
869 y += 0.1f * Math.cos(angle);
870 z += 0.1f * Math.sin(angle);
871
872 float t = 0.1f * ct;
873 float ds = 0.08f;
874 tm.setColor(l, l, l, 0.99f);
875 tm.setTexture(ds, t);
876 tm.addVertex(-0.5f, y, z);
877 tm.setTexture(1 - ds, t);
878 tm.addVertex(0.5f, y, z);
879 }
880 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
881 tm.addTriangle(ct, ct+1, ct+2);
882 tm.addTriangle(ct+1, ct+3, ct+2);
883 }
884 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700885 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700886 }
887
Jason Samscd689e12009-09-29 15:28:22 -0700888 private void initProgramVertex() {
889 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
890 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700891
892 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700893 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700894 mPV = pvb.create();
895 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700896 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700897
Jason Sams37e7c2b2009-10-19 12:55:43 -0700898 //pva = new ProgramVertex.MatrixAllocation(mRS);
899 //pva.setupOrthoWindow(mWidth, mHeight);
900 //pvb.setTextureMatrixEnable(true);
901 //mPVOrtho = pvb.create();
902 //mPVOrtho.setName("PVOrtho");
903 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700904
905 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700906 }
Joe Onorato93839052009-08-06 20:34:32 -0700907
Jason Samscd689e12009-09-29 15:28:22 -0700908 private void initProgramFragment() {
909 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700910 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700911 sb.setMag(Sampler.Value.LINEAR);
912 sb.setWrapS(Sampler.Value.CLAMP);
913 sb.setWrapT(Sampler.Value.CLAMP);
914 Sampler linear = sb.create();
915
916 sb.setMin(Sampler.Value.NEAREST);
917 sb.setMag(Sampler.Value.NEAREST);
918 Sampler nearest = sb.create();
919
920 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700921 //mPFColor = bf.create();
922 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700923
924 bf.setTexEnable(true, 0);
925 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700926 mPFTexMip = bf.create();
927 mPFTexMip.setName("PFTexMip");
928 mPFTexMip.bindSampler(linear, 0);
929
930 mPFTexNearest = bf.create();
931 mPFTexNearest.setName("PFTexNearest");
932 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700933 }
934
935 private void initProgramStore() {
936 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
937 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700938 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700939 bs.setDitherEnable(true);
940 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
941 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
942 mPSIcons = bs.create();
943 mPSIcons.setName("PSIcons");
944
945 //bs.setDitherEnable(false);
946 //mPSText = bs.create();
947 //mPSText.setName("PSText");
948 }
949
950 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700951 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700952 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700953 }
Jason Sams78aebd82009-09-15 13:06:59 -0700954
Joe Onorato1feb3a82009-08-08 22:32:00 -0700955 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700956 mParams = new Params();
957 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700958
Joe Onoratobf15cb42009-08-07 14:33:40 -0700959 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700960
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700961 mParams.bubbleWidth = bubble.getBubbleWidth();
962 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
963 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
964 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700965
Joe Onoratod63458b2009-10-15 21:19:09 -0700966 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
967 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
968 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500969 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
970 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
971 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -0700972 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
973 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
974 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700975 mParams.homeButtonWidth = 76;
976 mParams.homeButtonHeight = 68;
977 mParams.homeButtonTextureWidth = 128;
978 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700979
Joe Onoratod63458b2009-10-15 21:19:09 -0700980 mState.homeButtonId = mHomeButtonNormal.getID();
981
Joe Onorato1feb3a82009-08-08 22:32:00 -0700982 mParams.save();
983 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984
Joe Onorato1291a8c2009-09-15 15:07:25 -0400985 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
986 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
987 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700988
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700990 }
991
Jason Sams37e7c2b2009-10-19 12:55:43 -0700992 private void initScript(int id) {
993 }
994
995 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700996 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700997 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700998 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700999 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001000 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1001 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001002 mInvokeMove = sb.addInvokable("move");
1003 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001004 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001005 mInvokeResetWAR = sb.addInvokable("resetHWWar");
1006 mScript = sb.create();
1007 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1008 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1009 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1010 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1011 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001012
Jason Sams12c14a82009-10-06 14:33:15 -07001013 mMessageProc = new AAMessage();
1014 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001015 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001016 }
Joe Onorato93839052009-08-06 20:34:32 -07001017
Jason Sams20df7c72009-11-05 12:30:24 -08001018 private void uploadApps(ArrayList<ApplicationInfo> list) {
1019 for (int i=0; i < mState.iconCount; i++) {
1020 uploadAppIcon(i, list.get(i));
1021 }
1022 }
1023
1024 void dirtyCheck() {
1025 if (mAppsDirty && mHasSurface) {
1026 uploadApps(mAllAppsList);
1027 saveAppsList();
1028 mAppsDirty = false;
1029 }
1030 }
1031
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 private void setApps(ArrayList<ApplicationInfo> list) {
1033 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001034 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001035 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001036 allocCount = 1;
1037 }
1038
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001040 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001041 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042
1043 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001044 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001045 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001047 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048
1049 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1050
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001052 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001053 saveAppsList();
1054 }
1055
Jason Samsc8514792009-10-29 14:27:29 -07001056 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1057 int black[] = new int[w > h ? w : h];
1058 Allocation.Adapter2D a = alloc.createAdapter2D();
1059 int mip = 0;
1060 while (w > 1 || h > 1) {
1061 a.subData(0, 0, 1, h, black);
1062 a.subData(w-1, 0, 1, h, black);
1063 a.subData(0, 0, w, 1, black);
1064 a.subData(0, h-1, w, 1, black);
1065 mip++;
1066 w = (w + 1) >> 1;
1067 h = (h + 1) >> 1;
1068 a.setConstraint(Dimension.LOD, mip);
1069 }
1070 a.subData(0, 0, 1, 1, black);
1071 }
1072
Joe Onoratoa8138d52009-10-06 19:25:30 -07001073 private void uploadAppIcon(int index, ApplicationInfo item) {
1074 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001075 Element.RGBA_8888(mRS), true);
1076 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1077
Joe Onoratoa8138d52009-10-06 19:25:30 -07001078 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001079 Element.RGBA_8888(mRS), true);
1080 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001081
1082 mIcons[index].uploadToTexture(0);
1083 mLabels[index].uploadToTexture(0);
1084
1085 mIconIds[index] = mIcons[index].getID();
1086 mLabelIds[index] = mLabels[index].getID();
1087 }
1088
1089 /**
1090 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1091 * fill in the values and call saveAppsList().
1092 */
1093 private void reallocAppsList(int count) {
1094 Allocation[] icons = new Allocation[count];
1095 int[] iconIds = new int[count];
1096 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1097
1098 Allocation[] labels = new Allocation[count];
1099 int[] labelIds = new int[count];
1100 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1101
1102 final int oldCount = mIcons.length;
1103
1104 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1105 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1106 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1107 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1108
1109 mIcons = icons;
1110 mIconIds = iconIds;
1111 mLabels = labels;
1112 mLabelIds = labelIds;
1113 }
1114
1115 /**
1116 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1117 */
1118 private void addApp(int index, ApplicationInfo item) {
1119 final int count = mState.iconCount - index;
1120 final int dest = index + 1;
1121
1122 System.arraycopy(mIcons, index, mIcons, dest, count);
1123 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1124 System.arraycopy(mLabels, index, mLabels, dest, count);
1125 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1126
Jason Sams20df7c72009-11-05 12:30:24 -08001127 if (mHasSurface ) {
1128 uploadAppIcon(index, item);
1129 } else {
1130 mAppsDirty = true;
1131 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001132 }
1133
1134 /**
1135 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1136 */
1137 private void removeApp(int index) {
1138 final int count = mState.iconCount - index - 1;
1139 final int src = index + 1;
1140
1141 System.arraycopy(mIcons, src, mIcons, index, count);
1142 System.arraycopy(mIconIds, src, mIconIds, index, count);
1143 System.arraycopy(mLabels, src, mLabels, index, count);
1144 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1145
1146 final int last = mState.iconCount - 1;
1147 mIcons[last] = null;
1148 mIconIds[last] = 0;
1149 mLabels[last] = null;
1150 mLabelIds[last] = 0;
1151 }
1152
1153 /**
1154 * Send the apps list structures to RS.
1155 */
1156 private void saveAppsList() {
1157 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001158
Joe Onoratoa8138d52009-10-06 19:25:30 -07001159 mAllocIconIds.data(mIconIds);
1160 mAllocLabelIds.data(mLabelIds);
1161
Jason Sams37e7c2b2009-10-19 12:55:43 -07001162 if (mScript != null) { // this happens when we init it
1163 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1164 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001165 }
1166
1167 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001168
1169 // Note: mScript may be null if we haven't initialized it yet.
1170 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001171 if (mInvokeResetWAR != null) {
1172 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001173 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001174 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001175 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001176
1177 void initTouchState() {
1178 int width = getWidth();
1179 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001180 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1181 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001182
Jason Sams37e7c2b2009-10-19 12:55:43 -07001183 int centerY = (height / 2);
1184 mTouchYBorders[0] = centerY - (cellHeight * 2);
1185 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001186 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001187 mTouchYBorders[3] = centerY + cellHeight;
1188 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001189
Joe Onorato6665c0f2009-09-02 15:27:24 -07001190 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001191 mTouchXBorders[0] = 0;
1192 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001193 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001194 mTouchXBorders[3] = centerX + (width / 4);
1195 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001196 }
1197
Joe Onorato664457d2009-10-28 16:30:34 -04001198 void fling() {
1199 mInvokeFling.execute();
1200 }
1201
1202 void move() {
1203 mInvokeMove.execute();
1204 }
1205
1206 void moveTo(float row) {
1207 mState.targetPos = row;
1208 mState.save();
1209 mInvokeMoveTo.execute();
1210 }
1211
Jason Sams37e7c2b2009-10-19 12:55:43 -07001212 int chooseTappedIcon(int x, int y, float pos) {
1213 // Adjust for scroll position if not zero.
1214 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001215
Joe Onorato6665c0f2009-09-02 15:27:24 -07001216 int col = -1;
1217 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001218 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1219 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1220 col = i;
1221 break;
1222 }
1223 }
1224 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1225 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1226 row = i;
1227 break;
1228 }
1229 }
1230
1231 if (row < 0 || col < 0) {
1232 return -1;
1233 }
1234
Joe Onorato664457d2009-10-28 16:30:34 -04001235 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001236 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001237
Joe Onorato664457d2009-10-28 16:30:34 -04001238 if (index >= mState.iconCount) {
1239 return -1;
1240 } else {
1241 return index;
1242 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001243 }
1244
Joe Onorato6665c0f2009-09-02 15:27:24 -07001245 /**
1246 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001247 *
1248 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001249 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001250 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001251 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001252 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001253 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001254 }
1255
Joe Onoratoc61cff92009-11-08 11:54:39 -05001256 /**
1257 * Select the icon at the given index.
1258 *
1259 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001260 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001261 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001262 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001263 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001264 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001265 if (mLastSelection == SELECTION_ICONS) {
1266 mLastSelection = SELECTION_NONE;
1267 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001268 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001269 if (pressed == SELECTED_FOCUSED) {
1270 mLastSelection = SELECTION_ICONS;
1271 }
1272
Joe Onorato6665c0f2009-09-02 15:27:24 -07001273 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001274
1275 Bitmap selectionBitmap = mSelectionBitmap;
1276
1277 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001278 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1279 pressed == SELECTED_PRESSED,
Joe Onorato1291a8c2009-09-15 15:07:25 -04001280 mAllAppsList.get(index).iconBitmap);
1281
1282 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001283 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001284 mSelectedIcon.uploadToTexture(0);
1285 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001286 }
1287 }
1288
1289 /**
1290 * You need to call save() on mState on your own after calling this.
1291 */
1292 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001293 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001294 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001295
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001296 void setHomeSelected(int mode) {
1297 switch (mode) {
1298 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001299 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001300 break;
1301 case SELECTED_FOCUSED:
1302 mLastSelection = SELECTION_HOME;
1303 mState.homeButtonId = mHomeButtonFocused.getID();
1304 break;
1305 case SELECTED_PRESSED:
1306 mState.homeButtonId = mHomeButtonPressed.getID();
1307 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001308 }
1309 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001310 }
Joe Onorato93839052009-08-06 20:34:32 -07001311}
1312
1313