blob: 73ee4b93e3b15f43aef78eed6f748a1a70c49432 [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;
Joe Onorato52a653f2009-11-11 14:52:11 -080049import android.view.accessibility.AccessibilityEvent;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080050
51import java.util.ArrayList;
52import java.util.Collections;
53import java.util.Comparator;
Joe Onorato93839052009-08-06 20:34:32 -070054
55
Joe Onorato6665c0f2009-09-02 15:27:24 -070056public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070057 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040058 private static final String TAG = "Launcher.AllAppsView";
59
Joe Onoratofb0ca672009-09-14 17:55:46 -040060 /** Bit for mLocks for when there are icons being loaded. */
61 private static final int LOCK_ICONS_PENDING = 1;
62
Joe Onorato68ffd102009-10-15 17:59:43 -070063 private static final int TRACKING_NONE = 0;
64 private static final int TRACKING_FLING = 1;
65 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070066
Joe Onoratoeb8325a2009-11-08 13:20:30 -050067 private static final int SELECTED_NONE = 0;
68 private static final int SELECTED_FOCUSED = 1;
69 private static final int SELECTED_PRESSED = 2;
70
71 private static final int SELECTION_NONE = 0;
72 private static final int SELECTION_ICONS = 1;
73 private static final int SELECTION_HOME = 2;
74
Joe Onorato6665c0f2009-09-02 15:27:24 -070075 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070076 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040077
78 /** When this is 0, modifications are allowed, when it's not, they're not.
79 * TODO: What about scrolling? */
80 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070081
Joe Onorato82ca5502009-10-15 16:59:23 -070082 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070083 private int mMaxFlingVelocity;
84
Joe Onoratobcbeab82009-10-01 21:45:43 -070085 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070086 private RenderScript mRS;
87 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040088 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070089
Mike Cleron7d5d7462009-10-20 14:06:00 -070090 /**
91 * True when we are using arrow keys or trackball to drive navigation
92 */
93 private boolean mArrowNavigation = false;
Joe Onoratoeb8325a2009-11-08 13:20:30 -050094 private boolean mStartedScrolling;
95
96 /**
97 * Used to keep track of the selection when AllAppsView loses window focus.
98 * One of the SELECTION_ constants.
99 */
100 private int mLastSelection;
Jason Sams2e19c052009-10-20 18:19:55 -0700101
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800102 /**
103 * Used to keep track of the selection when AllAppsView loses window focus
104 */
105 private int mLastSelectedIcon;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500106
Joe Onoratod769a632009-08-11 17:09:02 -0700107 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700108 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700109 private int mMotionDownRawX;
110 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700111 private int mDownIconIndex = -1;
112 private int mCurrentIconIndex = -1;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800113
Mike Cleronb64b67a2009-11-08 14:56:25 -0800114 private boolean mShouldGainFocus;
115
Joe Onorato3a8820b2009-11-10 15:06:42 -0800116 private boolean mZoomDirty = false;
117 private float mNextZoom;
118 private boolean mNextAnimate;
Joe Onoratocb75f362009-11-12 13:04:07 -0800119 AAMessage mMessageProc;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700120
Joe Onorato6665c0f2009-09-02 15:27:24 -0700121 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700122 public static final int ALLOC_PARAMS = 0;
123 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700124 public static final int ALLOC_ICON_IDS = 3;
125 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700126
127 public static final int COLUMNS_PER_PAGE = 4;
128 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700129
Joe Onorato6665c0f2009-09-02 15:27:24 -0700130 public static final int ICON_WIDTH_PX = 64;
131 public static final int ICON_TEXTURE_WIDTH_PX = 128;
132
133 public static final int ICON_HEIGHT_PX = 64;
134 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700135
136 public int SCREEN_WIDTH_PX;
137 public int SCREEN_HEIGHT_PX;
138
Joe Onoratobcbeab82009-10-01 21:45:43 -0700139 public void recompute(int w, int h) {
140 SCREEN_WIDTH_PX = 480;
141 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700142 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700143 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700144
145 public AllAppsView(Context context, AttributeSet attrs) {
146 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700147 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400148 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700149 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700150 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700151 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700152 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
153
Joe Onorato6665c0f2009-09-02 15:27:24 -0700154 setOnClickListener(this);
155 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700156 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700157 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700158 }
159
Joe Onoratob39e51a2009-10-28 15:47:49 -0400160 /**
161 * If you have an attached click listener, View always plays the click sound!?!?
162 * Deal with sound effects by hand.
163 */
164 public void reallyPlaySoundEffect(int sound) {
165 boolean old = isSoundEffectsEnabled();
166 setSoundEffectsEnabled(true);
167 playSoundEffect(sound);
168 setSoundEffectsEnabled(old);
169 }
170
Joe Onorato7c312c12009-08-13 21:36:53 -0700171 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
172 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700173 }
174
Joe Onorato6665c0f2009-09-02 15:27:24 -0700175 public void setLauncher(Launcher launcher) {
176 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700177 }
178
Joe Onorato1feb3a82009-08-08 22:32:00 -0700179 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700180 public void surfaceDestroyed(SurfaceHolder holder) {
181 super.surfaceDestroyed(holder);
Jason Sams20df7c72009-11-05 12:30:24 -0800182 mRollo.mHasSurface = false;
Joe Onoratofab74402009-11-11 16:05:23 -0800183 // Without this, we leak mMessageCallback which leaks the context.
184 mRS.mMessageCallback = null;
Joe Onorato7bb17492009-09-24 17:51:01 -0700185 }
186
187 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700188 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800189 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700190
Joe Onorato080d9b62009-11-02 12:01:11 -0500191 super.surfaceChanged(holder, format, w, h);
192
Jason Sams90396672009-11-03 13:59:34 -0800193 if (mRS == null) {
Jason Sams90396672009-11-03 13:59:34 -0800194 mRS = createRenderScript(true);
195 mRollo = new RolloRS();
Jason Sams20df7c72009-11-05 12:30:24 -0800196 mRollo.mHasSurface = true;
Jason Sams90396672009-11-03 13:59:34 -0800197 mRollo.init(getResources(), w, h);
198 if (mAllAppsList != null) {
199 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800200 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800201 if (mShouldGainFocus) {
202 gainFocus();
203 mShouldGainFocus = false;
204 }
Joe Onorato3a8820b2009-11-10 15:06:42 -0800205 mRollo.dirtyCheck();
Jason Sams20df7c72009-11-05 12:30:24 -0800206 } else {
207 mRollo.mHasSurface = true;
208 mRollo.dirtyCheck();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700210
Joe Onoratocb75f362009-11-12 13:04:07 -0800211 mRS.mMessageCallback = mMessageProc = new AAMessage();
212
Joe Onoratoc567acb2009-08-31 14:34:43 -0700213 Resources res = getContext().getResources();
214 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700215
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800216 //long endTime = SystemClock.uptimeMillis();
217 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700218 }
Jason Sams2e19c052009-10-20 18:19:55 -0700219
Joe Onorato93839052009-08-06 20:34:32 -0700220 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800221 public void onWindowFocusChanged(boolean hasWindowFocus) {
222 super.onWindowFocusChanged(hasWindowFocus);
223 if (mArrowNavigation) {
224 if (!hasWindowFocus) {
225 // Clear selection when we lose window focus
226 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500227 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800228 mRollo.clearSelectedIcon();
229 mRollo.mState.save();
230 } else if (hasWindowFocus) {
231 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500232 if (mLastSelection == SELECTION_ICONS) {
233 int selection = mLastSelectedIcon;
Joe Onoratocb75f362009-11-12 13:04:07 -0800234 final int firstIcon = Math.round(mMessageProc.mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500235 Defines.COLUMNS_PER_PAGE;
236 if (selection < 0 || // No selection
237 selection < firstIcon || // off the top of the screen
238 selection >= mRollo.mState.iconCount || // past last icon
239 selection >= firstIcon + // past last icon on screen
240 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
241 selection = firstIcon;
242 }
243
244 // Select the first icon when we gain window focus
245 mRollo.selectIcon(selection, SELECTED_FOCUSED);
246 mRollo.mState.save();
247 } else if (mLastSelection == SELECTION_HOME) {
248 mRollo.setHomeSelected(SELECTED_FOCUSED);
249 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800250 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800251 }
252 }
253 }
254 }
255
256 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700257 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
258 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400259
260 if (!isVisible()) {
261 return;
262 }
263
Mike Cleron7d5d7462009-10-20 14:06:00 -0700264 if (gainFocus) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800265 if (mRollo != null) {
266 gainFocus();
267 } else {
268 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700269 }
270 } else {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800271 if (mRollo != null) {
272 if (mArrowNavigation) {
273 // Clear selection when we lose focus
274 mRollo.clearSelectedIcon();
275 mRollo.setHomeSelected(SELECTED_NONE);
276 mRollo.mState.save();
277 mArrowNavigation = false;
278 }
279 } else {
280 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700281 }
282 }
283 }
284
Mike Cleronb64b67a2009-11-08 14:56:25 -0800285 private void gainFocus() {
286 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
287 // Select the first icon when we gain keyboard focus
288 mArrowNavigation = true;
Joe Onoratocb75f362009-11-12 13:04:07 -0800289 mRollo.selectIcon(Math.round(mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800290 SELECTED_FOCUSED);
291 mRollo.mState.save();
292 }
293 }
294
Mike Cleron7d5d7462009-10-20 14:06:00 -0700295 @Override
296 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700297
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800298 boolean handled = false;
299
Joe Onorato859b3a72009-10-28 15:17:01 -0400300 if (!isVisible()) {
301 return false;
302 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500303 final int iconCount = mRollo.mState.iconCount;
304
Mike Cleron7d5d7462009-10-20 14:06:00 -0700305 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
306 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500307 if (mLastSelection == SELECTION_HOME) {
308 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
309 mLauncher.closeAllApps(true);
310 } else {
311 int whichApp = mRollo.mState.selectedIconIndex;
312 if (whichApp >= 0) {
313 ApplicationInfo app = mAllAppsList.get(whichApp);
314 mLauncher.startActivitySafely(app.intent);
315 handled = true;
316 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700317 }
318 }
319 }
Jason Sams2e19c052009-10-20 18:19:55 -0700320
Joe Onoratoa13f5742009-11-02 17:15:19 -0500321 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700322 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700323
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800324 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onoratocb75f362009-11-12 13:04:07 -0800325 int currentTopRow = Math.round(mMessageProc.mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700326
Mike Cleron7d5d7462009-10-20 14:06:00 -0700327 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500328 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700329
Mike Cleron7d5d7462009-10-20 14:06:00 -0700330 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500331 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700332 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700333
Mike Cleron7d5d7462009-10-20 14:06:00 -0700334 int newSelection = currentSelection;
335
336 switch (keyCode) {
337 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500338 if (mLastSelection == SELECTION_HOME) {
339 mRollo.setHomeSelected(SELECTED_NONE);
340 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
341 if (lastRowCount == 0) {
342 lastRowCount = Defines.COLUMNS_PER_PAGE;
343 }
344 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
345 if (newSelection >= iconCount) {
346 newSelection = iconCount-1;
347 }
348 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
349 - (Defines.ROWS_PER_PAGE - 1);
350 if (target < 0) {
351 target = 0;
352 }
353 if (currentTopRow != target) {
354 mRollo.moveTo(target);
355 }
356 } else {
357 if (currentPageRow > 0) {
358 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
359 } else if (currentTopRow > 0) {
360 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
361 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
362 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700363 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800364 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700365 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800366
Joe Onoratoa13f5742009-11-02 17:15:19 -0500367 case KeyEvent.KEYCODE_DPAD_DOWN: {
368 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
369 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
370 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500371 if (mLastSelection != SELECTION_HOME) {
372 if (currentRow < rowCount-1) {
373 mRollo.setHomeSelected(SELECTED_NONE);
374 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
375 if (newSelection >= iconCount) {
376 // Go from D to G in this arrangement:
377 // A B C D
378 // E F G
379 newSelection = iconCount - 1;
380 }
381 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
382 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
383 Defines.ROWS_PER_PAGE + 1);
384 }
385 } else {
386 newSelection = -1;
387 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700388 }
389 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800390 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700391 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500392 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700393 case KeyEvent.KEYCODE_DPAD_LEFT:
394 if (currentPageCol > 0) {
395 newSelection = currentSelection - 1;
396 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800397 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700398 break;
399 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700400 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500401 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700402 newSelection = currentSelection + 1;
403 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800404 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700405 break;
406 }
407 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500408 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700409 mRollo.mState.save();
410 }
411 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800412 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700413 }
414
Joe Onorato93839052009-08-06 20:34:32 -0700415 @Override
416 public boolean onTouchEvent(MotionEvent ev)
417 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700418 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700419
Joe Onorato7bb17492009-09-24 17:51:01 -0700420 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700421 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700422 }
423
Joe Onoratofb0ca672009-09-14 17:55:46 -0400424 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700425 return true;
426 }
427
428 super.onTouchEvent(ev);
429
Joe Onoratofb0ca672009-09-14 17:55:46 -0400430 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700431 int y = (int)ev.getY();
432
Joe Onoratobcbeab82009-10-01 21:45:43 -0700433 int action = ev.getAction();
434 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400435 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700436 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
437 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500438 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700439 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700440 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700441 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700442 mTouchTracking = TRACKING_FLING;
443
444 mMotionDownRawX = (int)ev.getRawX();
445 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700446
Mike Cleron7d5d7462009-10-20 14:06:00 -0700447 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700448 mRollo.mState.newTouchDown = 1;
449
450 if (!mRollo.checkClickOK()) {
451 mRollo.clearSelectedIcon();
452 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700453 mDownIconIndex = mCurrentIconIndex
Joe Onoratocb75f362009-11-12 13:04:07 -0800454 = mRollo.selectIcon(x, y, mMessageProc.mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700455 if (mDownIconIndex < 0) {
456 // if nothing was selected, no long press.
457 cancelLongPress();
458 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700459 }
460 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700461 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700462 mVelocity = VelocityTracker.obtain();
463 mVelocity.addMovement(ev);
464 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700465 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400466 break;
467 case MotionEvent.ACTION_MOVE:
468 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700469 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500470 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
471 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700472 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700473 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700474 int rawX = (int)ev.getRawX();
475 int rawY = (int)ev.getRawY();
476 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700477 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700478
Joe Onorato82ca5502009-10-15 16:59:23 -0700479 if (!mStartedScrolling && slop < mSlop) {
480 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700481 // below, we get the right delta.
Joe Onoratocb75f362009-11-12 13:04:07 -0800482 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mMessageProc.mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700483 if (mDownIconIndex != mCurrentIconIndex) {
484 // If a different icon is selected, don't allow it to be picked up.
485 // This handles off-axis dragging.
486 cancelLongPress();
487 mCurrentIconIndex = -1;
488 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700489 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700490 if (!mStartedScrolling) {
491 cancelLongPress();
492 mCurrentIconIndex = -1;
493 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700494 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700495 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700496 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700497
Joe Onoratobcbeab82009-10-01 21:45:43 -0700498 mStartedScrolling = true;
499 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700500 mVelocity.addMovement(ev);
501 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700502 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400503 }
504 break;
505 case MotionEvent.ACTION_UP:
506 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700507 if (mTouchTracking == TRACKING_HOME) {
508 if (action == MotionEvent.ACTION_UP) {
509 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400510 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700511 mLauncher.closeAllApps(true);
512 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500513 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700514 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700515 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700516 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700517 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700518 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700519 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700520
Jason Sams12c14a82009-10-06 14:33:15 -0700521 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700522 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700523 mRollo.clearSelectedIcon();
524 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700525 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700526
Joe Onorato539ed9d2009-10-02 10:22:14 -0700527 if (mVelocity != null) {
528 mVelocity.recycle();
529 mVelocity = null;
530 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700531 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700532 mTouchTracking = TRACKING_NONE;
533 break;
Joe Onorato93839052009-08-06 20:34:32 -0700534 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700535
536 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700537 }
538
Joe Onorato6665c0f2009-09-02 15:27:24 -0700539 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700540 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400541 return;
542 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700543 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
544 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400545 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700546 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700547 mLauncher.startActivitySafely(app.intent);
548 }
549 }
550
551 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700552 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400553 return true;
554 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700555 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
556 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
557 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700558
559 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700560 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
561 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700562
Joe Onoratobcbeab82009-10-01 21:45:43 -0700563 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
564 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700565 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700566 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700567 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700568
Joe Onorato7bb17492009-09-24 17:51:01 -0700569 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700570 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700571 return true;
572 }
573
Joe Onorato52a653f2009-11-11 14:52:11 -0800574 @Override
575 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
576 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
577 if (!isVisible()) {
578 return false;
579 }
580 String text = null;
581 int index;
582 int count = mAllAppsList.size() + 1; // +1 is home
583 int pos = -1;
584 switch (mLastSelection) {
585 case SELECTION_ICONS:
586 index = mRollo.mState.selectedIconIndex;
587 if (index >= 0) {
588 ApplicationInfo info = mAllAppsList.get(index);
589 if (info.title != null) {
590 text = info.title.toString();
591 pos = index;
592 }
593 }
594 break;
595 case SELECTION_HOME:
596 text = getContext().getString(R.string.all_apps_home_button_label);
597 pos = count;
598 break;
599 }
600 if (text != null) {
601 Log.d(TAG, "dispatchPopulateAccessibilityEvent setting text=" + text);
602 event.setEnabled(true);
603 event.getText().add(text);
604 //event.setContentDescription(text);
605 event.setItemCount(count);
606 event.setCurrentItemIndex(pos);
607 }
608 }
609 return false;
610 }
611
Joe Onorato5162ea92009-09-03 09:39:42 -0700612 public void setDragController(DragController dragger) {
613 mDragController = dragger;
614 }
615
616 public void onDropCompleted(View target, boolean success) {
617 }
618
Joe Onorato4db52312009-10-06 11:17:43 -0700619 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800620 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700621 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800622 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700623 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800624 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400625 cancelLongPress();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800626 if (mRollo == null || !mRollo.mHasSurface) {
627 mZoomDirty = true;
628 mNextZoom = zoom;
629 mNextAnimate = animate;
630 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700631 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800632 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700633 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400634 }
635
636 public boolean isVisible() {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800637 if (mZoomDirty) {
638 return mNextZoom > 0.001f;
639 } else {
Joe Onoratocb75f362009-11-12 13:04:07 -0800640 if (mMessageProc == null) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800641 return false;
642 } else {
Joe Onoratocb75f362009-11-12 13:04:07 -0800643 return mMessageProc.mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800644 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700645 }
Joe Onorato3a8820b2009-11-10 15:06:42 -0800646 }
647
648 public boolean isOpaque() {
649 if (mZoomDirty) {
650 return mNextZoom > 0.999f;
651 } else {
Joe Onoratocb75f362009-11-12 13:04:07 -0800652 if (mMessageProc == null) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800653 return false;
654 } else {
Joe Onoratocb75f362009-11-12 13:04:07 -0800655 return mMessageProc.mZoom > 0.999f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800656 }
657 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400658 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700659
Joe Onorato9c1289c2009-08-17 11:03:03 -0400660 public void setApps(ArrayList<ApplicationInfo> list) {
661 mAllAppsList = list;
662 if (mRollo != null) {
663 mRollo.setApps(list);
664 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400665 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700666 }
667
Joe Onoratoa8138d52009-10-06 19:25:30 -0700668 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500669 if (mAllAppsList == null) {
670 // Not done loading yet. We'll find out about it later.
671 return;
672 }
673
Joe Onoratoa8138d52009-10-06 19:25:30 -0700674 final int N = list.size();
675 if (mRollo != null) {
676 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
677 }
678
679 for (int i=0; i<N; i++) {
680 final ApplicationInfo item = list.get(i);
681 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
682 if (index < 0) {
683 index = -(index+1);
684 }
685 mAllAppsList.add(index, item);
686 if (mRollo != null) {
687 mRollo.addApp(index, item);
688 mRollo.mState.iconCount++;
689 }
690 }
691
692 if (mRollo != null) {
693 mRollo.saveAppsList();
694 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700695 }
696
Joe Onoratoa8138d52009-10-06 19:25:30 -0700697 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500698 if (mAllAppsList == null) {
699 // Not done loading yet. We'll find out about it later.
700 return;
701 }
702
Joe Onoratoa8138d52009-10-06 19:25:30 -0700703 final int N = list.size();
704 for (int i=0; i<N; i++) {
705 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400706 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700707 if (index >= 0) {
708 mAllAppsList.remove(index);
709 if (mRollo != null) {
710 mRollo.removeApp(index);
711 mRollo.mState.iconCount--;
712 }
713 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800714 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700715 // Try to recover. This should keep us from crashing for now.
716 }
717 }
718
719 if (mRollo != null) {
720 mRollo.saveAppsList();
721 }
722 }
723
724 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
725 // Just remove and add, because they may need to be re-sorted.
726 removeApps(list);
727 addApps(list);
728 }
729
730 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
731 public int compare(ApplicationInfo a, ApplicationInfo b) {
732 int result = a.title.toString().compareTo(b.toString());
733 if (result != 0) {
734 return result;
735 }
736 return a.intent.getComponent().compareTo(b.intent.getComponent());
737 }
738 };
739
Joe Onoratocb9f7982009-10-31 16:32:02 -0400740 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
741 ComponentName component = item.intent.getComponent();
742 final int N = list.size();
743 for (int i=0; i<N; i++) {
744 ApplicationInfo x = list.get(i);
745 if (x.intent.getComponent().equals(component)) {
746 return i;
747 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700748 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400749 return -1;
750 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700751
Joe Onoratoc567acb2009-08-31 14:34:43 -0700752 private static int countPages(int iconCount) {
753 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
754 int pages = iconCount / iconsPerPage;
755 if (pages*iconsPerPage != iconCount) {
756 pages++;
757 }
758 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 }
760
Joe Onoratocb75f362009-11-12 13:04:07 -0800761 class AAMessage extends RenderScript.RSMessage {
762 public void run() {
763 mPosX = ((float)mData[0]) / (1 << 16);
764 mVelocity = ((float)mData[1]) / (1 << 16);
765 mZoom = ((float)mData[2]) / (1 << 16);
766 mZoomDirty = false;
767 }
768 float mZoom;
769 float mPosX;
770 float mVelocity;
771 }
772
Joe Onorato93839052009-08-06 20:34:32 -0700773 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700774
Joe Onorato1feb3a82009-08-08 22:32:00 -0700775 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700776 private int mWidth;
777 private int mHeight;
778
779 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700780 private Script mScript;
781 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700782 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700783 private Script.Invokable mInvokeFling;
784 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800785 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700786
Jason Samscd689e12009-09-29 15:28:22 -0700787 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700788 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700789 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700790 private ProgramFragment mPFTexMip;
791 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700792 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700793 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700794 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700795 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700796
Joe Onoratod63458b2009-10-15 21:19:09 -0700797 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500798 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700799 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700800
Joe Onoratobf15cb42009-08-07 14:33:40 -0700801 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700802 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700803 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700804
Joe Onoratobf15cb42009-08-07 14:33:40 -0700805 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700806 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700807 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700808 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700809
Joe Onorato6665c0f2009-09-02 15:27:24 -0700810 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700811 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700812
813 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400814 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700815
Jason Sams20df7c72009-11-05 12:30:24 -0800816 boolean mHasSurface = false;
817 private boolean mAppsDirty = false;
Jason Samsd8152b92009-10-13 17:19:10 -0700818
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700819 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700820 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700821
Jason Sams78aebd82009-09-15 13:06:59 -0700822 class BaseAlloc {
823 Allocation mAlloc;
824 Type mType;
825
826 void save() {
827 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700828 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700829 }
830
Jason Sams476339d2009-09-29 18:14:38 -0700831 private boolean checkClickOK() {
Jason Sams2e19c052009-10-20 18:19:55 -0700832 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
833 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700834 }
835
Jason Sams78aebd82009-09-15 13:06:59 -0700836 class Params extends BaseAlloc {
837 Params() {
838 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
839 mAlloc = Allocation.createTyped(mRS, mType);
840 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700841 }
Jason Sams78aebd82009-09-15 13:06:59 -0700842 public int bubbleWidth;
843 public int bubbleHeight;
844 public int bubbleBitmapWidth;
845 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700846
Joe Onoratobcbeab82009-10-01 21:45:43 -0700847 public int homeButtonWidth;
848 public int homeButtonHeight;
849 public int homeButtonTextureWidth;
850 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700851 }
852
853 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700854 public float newPositionX;
855 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700856 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700857 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700858 public int selectedIconIndex = -1;
859 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700860 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700861 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700862 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700863
864 State() {
865 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
866 mAlloc = Allocation.createTyped(mRS, mType);
867 save();
868 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700869 }
870
871 public RolloRS() {
872 }
873
874 public void init(Resources res, int width, int height) {
875 mRes = res;
876 mWidth = width;
877 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700878 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700879 initProgramVertex();
880 initProgramFragment();
881 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700882 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700883 initGl();
884 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700885 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700886 initRs();
887 }
888
Jason Sams0aa71662009-10-02 18:43:18 -0700889 public void initMesh() {
890 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
891 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
892
Jason Samsd8152b92009-10-13 17:19:10 -0700893 float y = 0;
894 float z = 0;
895 for (int ct=0; ct < 200; ct++) {
896 float angle = 0;
897 float maxAngle = 3.14f * 0.16f;
898 float l = 1.f;
899
900 l = 1 - ((ct-5) * 0.10f);
901 if (ct > 7) {
902 angle = maxAngle * (ct - 7) * 0.2f;
903 angle = Math.min(angle, maxAngle);
904 }
Jason Samsc8514792009-10-29 14:27:29 -0700905 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700906 l = Math.min(1.0f, l);
907
908 y += 0.1f * Math.cos(angle);
909 z += 0.1f * Math.sin(angle);
910
911 float t = 0.1f * ct;
912 float ds = 0.08f;
913 tm.setColor(l, l, l, 0.99f);
914 tm.setTexture(ds, t);
915 tm.addVertex(-0.5f, y, z);
916 tm.setTexture(1 - ds, t);
917 tm.addVertex(0.5f, y, z);
918 }
919 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
920 tm.addTriangle(ct, ct+1, ct+2);
921 tm.addTriangle(ct+1, ct+3, ct+2);
922 }
923 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700924 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700925 }
926
Jason Samscd689e12009-09-29 15:28:22 -0700927 private void initProgramVertex() {
928 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
929 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700930
931 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700932 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700933 mPV = pvb.create();
934 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700935 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700936
Jason Sams37e7c2b2009-10-19 12:55:43 -0700937 //pva = new ProgramVertex.MatrixAllocation(mRS);
938 //pva.setupOrthoWindow(mWidth, mHeight);
939 //pvb.setTextureMatrixEnable(true);
940 //mPVOrtho = pvb.create();
941 //mPVOrtho.setName("PVOrtho");
942 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700943
944 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700945 }
Joe Onorato93839052009-08-06 20:34:32 -0700946
Jason Samscd689e12009-09-29 15:28:22 -0700947 private void initProgramFragment() {
948 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700949 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700950 sb.setMag(Sampler.Value.LINEAR);
951 sb.setWrapS(Sampler.Value.CLAMP);
952 sb.setWrapT(Sampler.Value.CLAMP);
953 Sampler linear = sb.create();
954
955 sb.setMin(Sampler.Value.NEAREST);
956 sb.setMag(Sampler.Value.NEAREST);
957 Sampler nearest = sb.create();
958
959 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700960 //mPFColor = bf.create();
961 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700962
963 bf.setTexEnable(true, 0);
964 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700965 mPFTexMip = bf.create();
966 mPFTexMip.setName("PFTexMip");
967 mPFTexMip.bindSampler(linear, 0);
968
969 mPFTexNearest = bf.create();
970 mPFTexNearest.setName("PFTexNearest");
971 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700972 }
973
974 private void initProgramStore() {
975 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
976 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700977 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700978 bs.setDitherEnable(true);
979 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
980 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
981 mPSIcons = bs.create();
982 mPSIcons.setName("PSIcons");
983
984 //bs.setDitherEnable(false);
985 //mPSText = bs.create();
986 //mPSText.setName("PSText");
987 }
988
989 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700990 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700991 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700992 }
Jason Sams78aebd82009-09-15 13:06:59 -0700993
Joe Onorato1feb3a82009-08-08 22:32:00 -0700994 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700995 mParams = new Params();
996 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700997
Joe Onoratobf15cb42009-08-07 14:33:40 -0700998 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700999
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001000 mParams.bubbleWidth = bubble.getBubbleWidth();
1001 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1002 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1003 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001004
Joe Onoratod63458b2009-10-15 21:19:09 -07001005 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1006 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1007 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001008 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1009 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1010 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001011 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1012 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1013 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001014 mParams.homeButtonWidth = 76;
1015 mParams.homeButtonHeight = 68;
1016 mParams.homeButtonTextureWidth = 128;
1017 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001018
Joe Onoratod63458b2009-10-15 21:19:09 -07001019 mState.homeButtonId = mHomeButtonNormal.getID();
1020
Joe Onorato1feb3a82009-08-08 22:32:00 -07001021 mParams.save();
1022 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023
Joe Onorato1291a8c2009-09-15 15:07:25 -04001024 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1025 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1026 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001027
Joe Onorato9c1289c2009-08-17 11:03:03 -04001028 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001029 }
1030
Jason Sams37e7c2b2009-10-19 12:55:43 -07001031 private void initScript(int id) {
1032 }
1033
1034 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001035 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001036 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -07001037 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001038 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001039 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1040 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001041 mInvokeMove = sb.addInvokable("move");
1042 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001043 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001044 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001045 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001046 mScript = sb.create();
1047 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1048 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1049 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1050 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1051 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001052
Jason Sams37e7c2b2009-10-19 12:55:43 -07001053 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001054 }
Joe Onorato93839052009-08-06 20:34:32 -07001055
Jason Sams20df7c72009-11-05 12:30:24 -08001056 private void uploadApps(ArrayList<ApplicationInfo> list) {
1057 for (int i=0; i < mState.iconCount; i++) {
1058 uploadAppIcon(i, list.get(i));
1059 }
1060 }
1061
1062 void dirtyCheck() {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001063 if (mHasSurface) {
1064 if (mAppsDirty) {
1065 uploadApps(mAllAppsList);
1066 saveAppsList();
1067 mAppsDirty = false;
1068 }
1069 if (mZoomDirty) {
1070 setZoom(mNextZoom, mNextAnimate);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001071 }
Jason Sams20df7c72009-11-05 12:30:24 -08001072 }
1073 }
1074
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075 private void setApps(ArrayList<ApplicationInfo> list) {
1076 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001077 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001078 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001079 allocCount = 1;
1080 }
1081
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001083 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001084 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085
1086 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001087 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001088 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001089
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001090 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001091
1092 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1093
Joe Onorato9c1289c2009-08-17 11:03:03 -04001094 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001095 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001096 saveAppsList();
1097 }
1098
Joe Onorato3a8820b2009-11-10 15:06:42 -08001099 private void setZoom(float zoom, boolean animate) {
1100 mRollo.clearSelectedIcon();
1101 mRollo.setHomeSelected(SELECTED_NONE);
1102 if (zoom > 0.001f) {
1103 mRollo.mState.zoomTarget = zoom;
1104 } else {
1105 mRollo.mState.zoomTarget = 0;
1106 }
1107 mRollo.mState.save();
1108 if (!animate) {
1109 mRollo.mInvokeSetZoom.execute();
1110 }
1111 }
1112
Jason Samsc8514792009-10-29 14:27:29 -07001113 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1114 int black[] = new int[w > h ? w : h];
1115 Allocation.Adapter2D a = alloc.createAdapter2D();
1116 int mip = 0;
1117 while (w > 1 || h > 1) {
1118 a.subData(0, 0, 1, h, black);
1119 a.subData(w-1, 0, 1, h, black);
1120 a.subData(0, 0, w, 1, black);
1121 a.subData(0, h-1, w, 1, black);
1122 mip++;
1123 w = (w + 1) >> 1;
1124 h = (h + 1) >> 1;
1125 a.setConstraint(Dimension.LOD, mip);
1126 }
1127 a.subData(0, 0, 1, 1, black);
1128 }
1129
Joe Onoratoa8138d52009-10-06 19:25:30 -07001130 private void uploadAppIcon(int index, ApplicationInfo item) {
1131 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001132 Element.RGBA_8888(mRS), true);
1133 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1134
Joe Onoratoa8138d52009-10-06 19:25:30 -07001135 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001136 Element.RGBA_8888(mRS), true);
1137 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001138
1139 mIcons[index].uploadToTexture(0);
1140 mLabels[index].uploadToTexture(0);
1141
1142 mIconIds[index] = mIcons[index].getID();
1143 mLabelIds[index] = mLabels[index].getID();
1144 }
1145
1146 /**
1147 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1148 * fill in the values and call saveAppsList().
1149 */
1150 private void reallocAppsList(int count) {
1151 Allocation[] icons = new Allocation[count];
1152 int[] iconIds = new int[count];
1153 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1154
1155 Allocation[] labels = new Allocation[count];
1156 int[] labelIds = new int[count];
1157 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1158
1159 final int oldCount = mIcons.length;
1160
1161 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1162 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1163 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1164 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1165
1166 mIcons = icons;
1167 mIconIds = iconIds;
1168 mLabels = labels;
1169 mLabelIds = labelIds;
1170 }
1171
1172 /**
1173 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1174 */
1175 private void addApp(int index, ApplicationInfo item) {
1176 final int count = mState.iconCount - index;
1177 final int dest = index + 1;
1178
1179 System.arraycopy(mIcons, index, mIcons, dest, count);
1180 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1181 System.arraycopy(mLabels, index, mLabels, dest, count);
1182 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1183
Jason Sams20df7c72009-11-05 12:30:24 -08001184 if (mHasSurface ) {
1185 uploadAppIcon(index, item);
1186 } else {
1187 mAppsDirty = true;
1188 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001189 }
1190
1191 /**
1192 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1193 */
1194 private void removeApp(int index) {
1195 final int count = mState.iconCount - index - 1;
1196 final int src = index + 1;
1197
1198 System.arraycopy(mIcons, src, mIcons, index, count);
1199 System.arraycopy(mIconIds, src, mIconIds, index, count);
1200 System.arraycopy(mLabels, src, mLabels, index, count);
1201 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1202
1203 final int last = mState.iconCount - 1;
1204 mIcons[last] = null;
1205 mIconIds[last] = 0;
1206 mLabels[last] = null;
1207 mLabelIds[last] = 0;
1208 }
1209
1210 /**
1211 * Send the apps list structures to RS.
1212 */
1213 private void saveAppsList() {
1214 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001215
Joe Onoratoa8138d52009-10-06 19:25:30 -07001216 mAllocIconIds.data(mIconIds);
1217 mAllocLabelIds.data(mLabelIds);
1218
Jason Sams37e7c2b2009-10-19 12:55:43 -07001219 if (mScript != null) { // this happens when we init it
1220 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1221 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001222 }
1223
1224 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001225
1226 // Note: mScript may be null if we haven't initialized it yet.
1227 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001228 if (mInvokeResetWAR != null) {
1229 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001230 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001231 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001233
1234 void initTouchState() {
1235 int width = getWidth();
1236 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001237 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1238 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001239
Jason Sams37e7c2b2009-10-19 12:55:43 -07001240 int centerY = (height / 2);
1241 mTouchYBorders[0] = centerY - (cellHeight * 2);
1242 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001243 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001244 mTouchYBorders[3] = centerY + cellHeight;
1245 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001246
Joe Onorato6665c0f2009-09-02 15:27:24 -07001247 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001248 mTouchXBorders[0] = 0;
1249 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001250 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001251 mTouchXBorders[3] = centerX + (width / 4);
1252 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001253 }
1254
Joe Onorato664457d2009-10-28 16:30:34 -04001255 void fling() {
1256 mInvokeFling.execute();
1257 }
1258
1259 void move() {
1260 mInvokeMove.execute();
1261 }
1262
1263 void moveTo(float row) {
1264 mState.targetPos = row;
1265 mState.save();
1266 mInvokeMoveTo.execute();
1267 }
1268
Jason Sams37e7c2b2009-10-19 12:55:43 -07001269 int chooseTappedIcon(int x, int y, float pos) {
1270 // Adjust for scroll position if not zero.
1271 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001272
Joe Onorato6665c0f2009-09-02 15:27:24 -07001273 int col = -1;
1274 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001275 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1276 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1277 col = i;
1278 break;
1279 }
1280 }
1281 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1282 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1283 row = i;
1284 break;
1285 }
1286 }
1287
1288 if (row < 0 || col < 0) {
1289 return -1;
1290 }
1291
Joe Onorato664457d2009-10-28 16:30:34 -04001292 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001293 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001294
Joe Onorato664457d2009-10-28 16:30:34 -04001295 if (index >= mState.iconCount) {
1296 return -1;
1297 } else {
1298 return index;
1299 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001300 }
1301
Joe Onorato6665c0f2009-09-02 15:27:24 -07001302 /**
1303 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001304 *
1305 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001306 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001307 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001308 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001309 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001310 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001311 }
1312
Joe Onoratoc61cff92009-11-08 11:54:39 -05001313 /**
1314 * Select the icon at the given index.
1315 *
1316 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001317 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001318 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001319 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001320 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001321 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001322 if (mLastSelection == SELECTION_ICONS) {
1323 mLastSelection = SELECTION_NONE;
1324 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001325 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001326 if (pressed == SELECTED_FOCUSED) {
1327 mLastSelection = SELECTION_ICONS;
1328 }
1329
Joe Onorato52a653f2009-11-11 14:52:11 -08001330 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001331 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001332
Joe Onorato52a653f2009-11-11 14:52:11 -08001333 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001334 Bitmap selectionBitmap = mSelectionBitmap;
1335
1336 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001337 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001338 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001339
1340 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001341 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001342 mSelectedIcon.uploadToTexture(0);
1343 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001344
1345 if (prev != index) {
1346 if (info.title != null && info.title.length() > 0) {
1347 Log.d(TAG, "sendAccessibilityEvent SELECTION_ICONS " + info.title);
1348 //setContentDescription(info.title);
1349 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1350 }
1351 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001352 }
1353 }
1354
1355 /**
1356 * You need to call save() on mState on your own after calling this.
1357 */
1358 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001359 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001360 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001361
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001362 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001363 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001364 switch (mode) {
1365 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001366 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001367 break;
1368 case SELECTED_FOCUSED:
1369 mLastSelection = SELECTION_HOME;
1370 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001371 if (prev != SELECTION_HOME) {
1372 Log.d(TAG, "sendAccessibilityEvent SELECTION_HOME");
1373 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1374 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001375 break;
1376 case SELECTED_PRESSED:
1377 mState.homeButtonId = mHomeButtonPressed.getID();
1378 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001379 }
1380 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001381 }
Joe Onorato93839052009-08-06 20:34:32 -07001382}
1383
1384