blob: 319c1231ab6f08af364108592bb92b8ac3c40d0e [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 Onorato68ba5ca2009-11-12 14:23:43 -0800107 private VelocityTracker mVelocityTracker;
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;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800117 private boolean mAnimateNextZoom;
118 private float mZoom;
119 private float mPosX;
120 private float mVelocity;
121 private AAMessage mMessageProc;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700122
Joe Onorato6665c0f2009-09-02 15:27:24 -0700123 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700124 public static final int ALLOC_PARAMS = 0;
125 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700126 public static final int ALLOC_ICON_IDS = 3;
127 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700128
129 public static final int COLUMNS_PER_PAGE = 4;
130 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700131
Joe Onorato6665c0f2009-09-02 15:27:24 -0700132 public static final int ICON_WIDTH_PX = 64;
133 public static final int ICON_TEXTURE_WIDTH_PX = 128;
134
135 public static final int ICON_HEIGHT_PX = 64;
136 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700137
138 public int SCREEN_WIDTH_PX;
139 public int SCREEN_HEIGHT_PX;
140
Joe Onoratobcbeab82009-10-01 21:45:43 -0700141 public void recompute(int w, int h) {
142 SCREEN_WIDTH_PX = 480;
143 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700144 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700145 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700146
147 public AllAppsView(Context context, AttributeSet attrs) {
148 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700149 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400150 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700151 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700152 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700153 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700154 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
155
Joe Onorato6665c0f2009-09-02 15:27:24 -0700156 setOnClickListener(this);
157 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700158 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700159 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samse26d9fc2009-11-12 14:00:43 -0800160
161 mRS = createRenderScript(true);
162 }
163
164 @Override
165 protected void onDetachedFromWindow() {
166 destroyRenderScript();
Joe Onorato93839052009-08-06 20:34:32 -0700167 }
168
Joe Onoratob39e51a2009-10-28 15:47:49 -0400169 /**
170 * If you have an attached click listener, View always plays the click sound!?!?
171 * Deal with sound effects by hand.
172 */
173 public void reallyPlaySoundEffect(int sound) {
174 boolean old = isSoundEffectsEnabled();
175 setSoundEffectsEnabled(true);
176 playSoundEffect(sound);
177 setSoundEffectsEnabled(old);
178 }
179
Joe Onorato7c312c12009-08-13 21:36:53 -0700180 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
181 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700182 }
183
Joe Onorato6665c0f2009-09-02 15:27:24 -0700184 public void setLauncher(Launcher launcher) {
185 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700186 }
187
Joe Onorato1feb3a82009-08-08 22:32:00 -0700188 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700189 public void surfaceDestroyed(SurfaceHolder holder) {
190 super.surfaceDestroyed(holder);
Jason Sams20df7c72009-11-05 12:30:24 -0800191 mRollo.mHasSurface = false;
Joe Onoratofab74402009-11-11 16:05:23 -0800192 // Without this, we leak mMessageCallback which leaks the context.
193 mRS.mMessageCallback = null;
Joe Onorato7bb17492009-09-24 17:51:01 -0700194 }
195
196 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700197 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800198 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700199
Joe Onorato080d9b62009-11-02 12:01:11 -0500200 super.surfaceChanged(holder, format, w, h);
201
Jason Samse26d9fc2009-11-12 14:00:43 -0800202 if (mRollo == null) {
Jason Sams90396672009-11-03 13:59:34 -0800203 mRollo = new RolloRS();
Jason Sams20df7c72009-11-05 12:30:24 -0800204 mRollo.mHasSurface = true;
Jason Sams90396672009-11-03 13:59:34 -0800205 mRollo.init(getResources(), w, h);
206 if (mAllAppsList != null) {
207 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800208 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800209 if (mShouldGainFocus) {
210 gainFocus();
211 mShouldGainFocus = false;
212 }
Jason Sams20df7c72009-11-05 12:30:24 -0800213 } else {
214 mRollo.mHasSurface = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 }
Jason Samse26d9fc2009-11-12 14:00:43 -0800216 mRollo.dirtyCheck();
Jason Sams5612e432009-11-16 14:18:07 -0800217 mRollo.resize(w, h);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700218
Joe Onoratocb75f362009-11-12 13:04:07 -0800219 mRS.mMessageCallback = mMessageProc = new AAMessage();
220
Joe Onoratoc567acb2009-08-31 14:34:43 -0700221 Resources res = getContext().getResources();
222 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700223
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800224 //long endTime = SystemClock.uptimeMillis();
225 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700226 }
Jason Sams2e19c052009-10-20 18:19:55 -0700227
Joe Onorato93839052009-08-06 20:34:32 -0700228 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800229 public void onWindowFocusChanged(boolean hasWindowFocus) {
230 super.onWindowFocusChanged(hasWindowFocus);
231 if (mArrowNavigation) {
232 if (!hasWindowFocus) {
233 // Clear selection when we lose window focus
234 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500235 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800236 mRollo.clearSelectedIcon();
237 mRollo.mState.save();
238 } else if (hasWindowFocus) {
239 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500240 if (mLastSelection == SELECTION_ICONS) {
241 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800242 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500243 Defines.COLUMNS_PER_PAGE;
244 if (selection < 0 || // No selection
245 selection < firstIcon || // off the top of the screen
246 selection >= mRollo.mState.iconCount || // past last icon
247 selection >= firstIcon + // past last icon on screen
248 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
249 selection = firstIcon;
250 }
251
252 // Select the first icon when we gain window focus
253 mRollo.selectIcon(selection, SELECTED_FOCUSED);
254 mRollo.mState.save();
255 } else if (mLastSelection == SELECTION_HOME) {
256 mRollo.setHomeSelected(SELECTED_FOCUSED);
257 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800258 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800259 }
260 }
261 }
262 }
263
264 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700265 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
266 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400267
268 if (!isVisible()) {
269 return;
270 }
271
Mike Cleron7d5d7462009-10-20 14:06:00 -0700272 if (gainFocus) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800273 if (mRollo != null) {
274 gainFocus();
275 } else {
276 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700277 }
278 } else {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800279 if (mRollo != null) {
280 if (mArrowNavigation) {
281 // Clear selection when we lose focus
282 mRollo.clearSelectedIcon();
283 mRollo.setHomeSelected(SELECTED_NONE);
284 mRollo.mState.save();
285 mArrowNavigation = false;
286 }
287 } else {
288 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700289 }
290 }
291 }
292
Mike Cleronb64b67a2009-11-08 14:56:25 -0800293 private void gainFocus() {
294 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
295 // Select the first icon when we gain keyboard focus
296 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800297 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800298 SELECTED_FOCUSED);
299 mRollo.mState.save();
300 }
301 }
302
Mike Cleron7d5d7462009-10-20 14:06:00 -0700303 @Override
304 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700305
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800306 boolean handled = false;
Jason Samse26d9fc2009-11-12 14:00:43 -0800307
Joe Onorato859b3a72009-10-28 15:17:01 -0400308 if (!isVisible()) {
309 return false;
310 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500311 final int iconCount = mRollo.mState.iconCount;
312
Mike Cleron7d5d7462009-10-20 14:06:00 -0700313 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
314 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500315 if (mLastSelection == SELECTION_HOME) {
316 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
317 mLauncher.closeAllApps(true);
318 } else {
319 int whichApp = mRollo.mState.selectedIconIndex;
320 if (whichApp >= 0) {
321 ApplicationInfo app = mAllAppsList.get(whichApp);
322 mLauncher.startActivitySafely(app.intent);
323 handled = true;
324 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700325 }
326 }
327 }
Jason Sams2e19c052009-10-20 18:19:55 -0700328
Joe Onoratoa13f5742009-11-02 17:15:19 -0500329 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700330 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700331
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800332 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800333 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700334
Mike Cleron7d5d7462009-10-20 14:06:00 -0700335 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500336 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700337
Mike Cleron7d5d7462009-10-20 14:06:00 -0700338 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500339 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700340 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700341
Mike Cleron7d5d7462009-10-20 14:06:00 -0700342 int newSelection = currentSelection;
343
344 switch (keyCode) {
345 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500346 if (mLastSelection == SELECTION_HOME) {
347 mRollo.setHomeSelected(SELECTED_NONE);
348 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
349 if (lastRowCount == 0) {
350 lastRowCount = Defines.COLUMNS_PER_PAGE;
351 }
352 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
353 if (newSelection >= iconCount) {
354 newSelection = iconCount-1;
355 }
356 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
357 - (Defines.ROWS_PER_PAGE - 1);
358 if (target < 0) {
359 target = 0;
360 }
361 if (currentTopRow != target) {
362 mRollo.moveTo(target);
363 }
364 } else {
365 if (currentPageRow > 0) {
366 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
367 } else if (currentTopRow > 0) {
368 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
369 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
370 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700371 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800372 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700373 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800374
Joe Onoratoa13f5742009-11-02 17:15:19 -0500375 case KeyEvent.KEYCODE_DPAD_DOWN: {
376 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
377 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
378 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500379 if (mLastSelection != SELECTION_HOME) {
380 if (currentRow < rowCount-1) {
381 mRollo.setHomeSelected(SELECTED_NONE);
382 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
383 if (newSelection >= iconCount) {
384 // Go from D to G in this arrangement:
385 // A B C D
386 // E F G
387 newSelection = iconCount - 1;
388 }
389 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
390 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
391 Defines.ROWS_PER_PAGE + 1);
392 }
393 } else {
394 newSelection = -1;
395 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700396 }
397 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800398 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700399 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500400 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700401 case KeyEvent.KEYCODE_DPAD_LEFT:
402 if (currentPageCol > 0) {
403 newSelection = currentSelection - 1;
404 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800405 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700406 break;
407 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700408 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500409 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700410 newSelection = currentSelection + 1;
411 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800412 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700413 break;
414 }
415 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500416 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700417 mRollo.mState.save();
418 }
419 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800420 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700421 }
422
Joe Onorato93839052009-08-06 20:34:32 -0700423 @Override
424 public boolean onTouchEvent(MotionEvent ev)
425 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700426 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700427
Joe Onorato7bb17492009-09-24 17:51:01 -0700428 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700429 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700430 }
431
Joe Onoratofb0ca672009-09-14 17:55:46 -0400432 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700433 return true;
434 }
435
436 super.onTouchEvent(ev);
437
Joe Onoratofb0ca672009-09-14 17:55:46 -0400438 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700439 int y = (int)ev.getY();
440
Joe Onoratobcbeab82009-10-01 21:45:43 -0700441 int action = ev.getAction();
442 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400443 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700444 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
445 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500446 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700447 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700448 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700449 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700450 mTouchTracking = TRACKING_FLING;
451
452 mMotionDownRawX = (int)ev.getRawX();
453 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700454
Mike Cleron7d5d7462009-10-20 14:06:00 -0700455 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700456 mRollo.mState.newTouchDown = 1;
457
458 if (!mRollo.checkClickOK()) {
459 mRollo.clearSelectedIcon();
460 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700461 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800462 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700463 if (mDownIconIndex < 0) {
464 // if nothing was selected, no long press.
465 cancelLongPress();
466 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700467 }
468 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700469 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800470 mVelocityTracker = VelocityTracker.obtain();
471 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700472 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700473 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400474 break;
475 case MotionEvent.ACTION_MOVE:
476 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700477 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500478 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
479 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700480 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700481 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700482 int rawX = (int)ev.getRawX();
483 int rawY = (int)ev.getRawY();
484 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700485 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700486
Joe Onorato82ca5502009-10-15 16:59:23 -0700487 if (!mStartedScrolling && slop < mSlop) {
488 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700489 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800490 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700491 if (mDownIconIndex != mCurrentIconIndex) {
492 // If a different icon is selected, don't allow it to be picked up.
493 // This handles off-axis dragging.
494 cancelLongPress();
495 mCurrentIconIndex = -1;
496 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700497 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700498 if (!mStartedScrolling) {
499 cancelLongPress();
500 mCurrentIconIndex = -1;
501 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700502 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700503 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700504 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700505
Joe Onoratobcbeab82009-10-01 21:45:43 -0700506 mStartedScrolling = true;
507 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800508 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700509 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700510 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400511 }
512 break;
513 case MotionEvent.ACTION_UP:
514 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700515 if (mTouchTracking == TRACKING_HOME) {
516 if (action == MotionEvent.ACTION_UP) {
517 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400518 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700519 mLauncher.closeAllApps(true);
520 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500521 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700522 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700523 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700524 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700525 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700526 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700527 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700528
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800529 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
530 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700531 mRollo.clearSelectedIcon();
532 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700533 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700534
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800535 if (mVelocityTracker != null) {
536 mVelocityTracker.recycle();
537 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700538 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700539 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700540 mTouchTracking = TRACKING_NONE;
541 break;
Joe Onorato93839052009-08-06 20:34:32 -0700542 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700543
544 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700545 }
546
Joe Onorato6665c0f2009-09-02 15:27:24 -0700547 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700548 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400549 return;
550 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700551 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
552 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400553 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700554 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700555 mLauncher.startActivitySafely(app.intent);
556 }
557 }
558
559 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700560 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400561 return true;
562 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700563 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
564 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
565 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700566
567 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700568 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
569 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700570
Joe Onoratobcbeab82009-10-01 21:45:43 -0700571 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
572 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700573 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700574 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700575 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700576
Joe Onorato7bb17492009-09-24 17:51:01 -0700577 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700578 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700579 return true;
580 }
581
Joe Onorato52a653f2009-11-11 14:52:11 -0800582 @Override
583 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
584 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
585 if (!isVisible()) {
586 return false;
587 }
588 String text = null;
589 int index;
590 int count = mAllAppsList.size() + 1; // +1 is home
591 int pos = -1;
592 switch (mLastSelection) {
593 case SELECTION_ICONS:
594 index = mRollo.mState.selectedIconIndex;
595 if (index >= 0) {
596 ApplicationInfo info = mAllAppsList.get(index);
597 if (info.title != null) {
598 text = info.title.toString();
599 pos = index;
600 }
601 }
602 break;
603 case SELECTION_HOME:
604 text = getContext().getString(R.string.all_apps_home_button_label);
605 pos = count;
606 break;
607 }
608 if (text != null) {
609 Log.d(TAG, "dispatchPopulateAccessibilityEvent setting text=" + text);
610 event.setEnabled(true);
611 event.getText().add(text);
612 //event.setContentDescription(text);
613 event.setItemCount(count);
614 event.setCurrentItemIndex(pos);
615 }
616 }
617 return false;
618 }
619
Joe Onorato5162ea92009-09-03 09:39:42 -0700620 public void setDragController(DragController dragger) {
621 mDragController = dragger;
622 }
623
624 public void onDropCompleted(View target, boolean success) {
625 }
626
Joe Onorato4db52312009-10-06 11:17:43 -0700627 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800628 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700629 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800630 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700631 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800632 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400633 cancelLongPress();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800634 if (mRollo == null || !mRollo.mHasSurface) {
635 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800636 mZoom = zoom;
637 mAnimateNextZoom = animate;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800638 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700639 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800640 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700641 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400642 }
643
644 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800645 Log.d(TAG, "isVisible mZoomDirty=" + mZoomDirty
646 + " mMessageProc=" + (mMessageProc == null ? "null" : mZoom));
647 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800648 }
649
650 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800651 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400652 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700653
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654 public void setApps(ArrayList<ApplicationInfo> list) {
655 mAllAppsList = list;
656 if (mRollo != null) {
657 mRollo.setApps(list);
658 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400659 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700660 }
661
Joe Onoratoa8138d52009-10-06 19:25:30 -0700662 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500663 if (mAllAppsList == null) {
664 // Not done loading yet. We'll find out about it later.
665 return;
666 }
667
Joe Onoratoa8138d52009-10-06 19:25:30 -0700668 final int N = list.size();
669 if (mRollo != null) {
670 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
671 }
672
673 for (int i=0; i<N; i++) {
674 final ApplicationInfo item = list.get(i);
675 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
676 if (index < 0) {
677 index = -(index+1);
678 }
679 mAllAppsList.add(index, item);
680 if (mRollo != null) {
681 mRollo.addApp(index, item);
682 mRollo.mState.iconCount++;
683 }
684 }
685
686 if (mRollo != null) {
687 mRollo.saveAppsList();
688 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700689 }
690
Joe Onoratoa8138d52009-10-06 19:25:30 -0700691 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500692 if (mAllAppsList == null) {
693 // Not done loading yet. We'll find out about it later.
694 return;
695 }
696
Joe Onoratoa8138d52009-10-06 19:25:30 -0700697 final int N = list.size();
698 for (int i=0; i<N; i++) {
699 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400700 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700701 if (index >= 0) {
702 mAllAppsList.remove(index);
703 if (mRollo != null) {
704 mRollo.removeApp(index);
705 mRollo.mState.iconCount--;
706 }
707 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800708 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700709 // Try to recover. This should keep us from crashing for now.
710 }
711 }
712
713 if (mRollo != null) {
714 mRollo.saveAppsList();
715 }
716 }
717
718 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
719 // Just remove and add, because they may need to be re-sorted.
720 removeApps(list);
721 addApps(list);
722 }
723
724 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
725 public int compare(ApplicationInfo a, ApplicationInfo b) {
726 int result = a.title.toString().compareTo(b.toString());
727 if (result != 0) {
728 return result;
729 }
730 return a.intent.getComponent().compareTo(b.intent.getComponent());
731 }
732 };
733
Joe Onoratocb9f7982009-10-31 16:32:02 -0400734 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
735 ComponentName component = item.intent.getComponent();
736 final int N = list.size();
737 for (int i=0; i<N; i++) {
738 ApplicationInfo x = list.get(i);
739 if (x.intent.getComponent().equals(component)) {
740 return i;
741 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700742 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400743 return -1;
744 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700745
Joe Onoratoc567acb2009-08-31 14:34:43 -0700746 private static int countPages(int iconCount) {
747 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
748 int pages = iconCount / iconsPerPage;
749 if (pages*iconsPerPage != iconCount) {
750 pages++;
751 }
752 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 }
754
Joe Onoratocb75f362009-11-12 13:04:07 -0800755 class AAMessage extends RenderScript.RSMessage {
756 public void run() {
757 mPosX = ((float)mData[0]) / (1 << 16);
758 mVelocity = ((float)mData[1]) / (1 << 16);
759 mZoom = ((float)mData[2]) / (1 << 16);
760 mZoomDirty = false;
761 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800762 }
763
Joe Onorato93839052009-08-06 20:34:32 -0700764 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700765
Joe Onorato1feb3a82009-08-08 22:32:00 -0700766 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700767 private int mWidth;
768 private int mHeight;
769
770 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700771 private Script mScript;
772 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700773 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700774 private Script.Invokable mInvokeFling;
775 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800776 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700777
Jason Samscd689e12009-09-29 15:28:22 -0700778 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700779 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700780 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700781 private ProgramFragment mPFTexMip;
782 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700783 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700784 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700785 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700786 private SimpleMesh mMesh2;
Jason Sams5612e432009-11-16 14:18:07 -0800787 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700788
Joe Onoratod63458b2009-10-15 21:19:09 -0700789 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500790 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700791 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700792
Joe Onoratobf15cb42009-08-07 14:33:40 -0700793 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700794 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700795 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700796
Joe Onoratobf15cb42009-08-07 14:33:40 -0700797 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700798 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700799 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700800 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700801
Joe Onorato6665c0f2009-09-02 15:27:24 -0700802 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700803 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700804
805 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400806 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700807
Jason Sams20df7c72009-11-05 12:30:24 -0800808 boolean mHasSurface = false;
809 private boolean mAppsDirty = false;
Jason Samsd8152b92009-10-13 17:19:10 -0700810
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700811 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700812 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700813
Jason Sams78aebd82009-09-15 13:06:59 -0700814 class BaseAlloc {
815 Allocation mAlloc;
816 Type mType;
817
818 void save() {
819 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700820 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700821 }
822
Jason Sams476339d2009-09-29 18:14:38 -0700823 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800824 return (Math.abs(mVelocity) < 0.4f) &&
825 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700826 }
827
Jason Sams78aebd82009-09-15 13:06:59 -0700828 class Params extends BaseAlloc {
829 Params() {
830 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
831 mAlloc = Allocation.createTyped(mRS, mType);
832 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700833 }
Jason Sams78aebd82009-09-15 13:06:59 -0700834 public int bubbleWidth;
835 public int bubbleHeight;
836 public int bubbleBitmapWidth;
837 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700838
Joe Onoratobcbeab82009-10-01 21:45:43 -0700839 public int homeButtonWidth;
840 public int homeButtonHeight;
841 public int homeButtonTextureWidth;
842 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700843 }
844
845 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700846 public float newPositionX;
847 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700848 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700849 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700850 public int selectedIconIndex = -1;
851 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700852 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700853 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700854 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700855
856 State() {
857 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
858 mAlloc = Allocation.createTyped(mRS, mType);
859 save();
860 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700861 }
862
863 public RolloRS() {
864 }
865
866 public void init(Resources res, int width, int height) {
867 mRes = res;
868 mWidth = width;
869 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700870 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700871 initProgramVertex();
872 initProgramFragment();
873 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700874 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700875 initGl();
876 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700877 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700878 initRs();
879 }
880
Jason Sams0aa71662009-10-02 18:43:18 -0700881 public void initMesh() {
882 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
883 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
884
Jason Samsd8152b92009-10-13 17:19:10 -0700885 float y = 0;
886 float z = 0;
887 for (int ct=0; ct < 200; ct++) {
888 float angle = 0;
889 float maxAngle = 3.14f * 0.16f;
890 float l = 1.f;
891
892 l = 1 - ((ct-5) * 0.10f);
893 if (ct > 7) {
894 angle = maxAngle * (ct - 7) * 0.2f;
895 angle = Math.min(angle, maxAngle);
896 }
Jason Samsc8514792009-10-29 14:27:29 -0700897 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700898 l = Math.min(1.0f, l);
899
900 y += 0.1f * Math.cos(angle);
901 z += 0.1f * Math.sin(angle);
902
903 float t = 0.1f * ct;
904 float ds = 0.08f;
905 tm.setColor(l, l, l, 0.99f);
906 tm.setTexture(ds, t);
907 tm.addVertex(-0.5f, y, z);
908 tm.setTexture(1 - ds, t);
909 tm.addVertex(0.5f, y, z);
910 }
911 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
912 tm.addTriangle(ct, ct+1, ct+2);
913 tm.addTriangle(ct+1, ct+3, ct+2);
914 }
915 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700916 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700917 }
918
Jason Sams5612e432009-11-16 14:18:07 -0800919 void resize(int w, int h) {
920 mPVA.setupProjectionNormalized(w, h);
921 mWidth = w;
922 mHeight = h;
923 }
924
Jason Samscd689e12009-09-29 15:28:22 -0700925 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800926 mPVA = new ProgramVertex.MatrixAllocation(mRS);
927 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700928
929 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700930 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700931 mPV = pvb.create();
932 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800933 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700934
Jason Sams37e7c2b2009-10-19 12:55:43 -0700935 //pva = new ProgramVertex.MatrixAllocation(mRS);
936 //pva.setupOrthoWindow(mWidth, mHeight);
937 //pvb.setTextureMatrixEnable(true);
938 //mPVOrtho = pvb.create();
939 //mPVOrtho.setName("PVOrtho");
940 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700941
942 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700943 }
Joe Onorato93839052009-08-06 20:34:32 -0700944
Jason Samscd689e12009-09-29 15:28:22 -0700945 private void initProgramFragment() {
946 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700947 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700948 sb.setMag(Sampler.Value.LINEAR);
949 sb.setWrapS(Sampler.Value.CLAMP);
950 sb.setWrapT(Sampler.Value.CLAMP);
951 Sampler linear = sb.create();
952
953 sb.setMin(Sampler.Value.NEAREST);
954 sb.setMag(Sampler.Value.NEAREST);
955 Sampler nearest = sb.create();
956
957 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700958 //mPFColor = bf.create();
959 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700960
961 bf.setTexEnable(true, 0);
962 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700963 mPFTexMip = bf.create();
964 mPFTexMip.setName("PFTexMip");
965 mPFTexMip.bindSampler(linear, 0);
966
967 mPFTexNearest = bf.create();
968 mPFTexNearest.setName("PFTexNearest");
969 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700970 }
971
972 private void initProgramStore() {
973 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
974 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700975 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700976 bs.setDitherEnable(true);
977 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
978 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
979 mPSIcons = bs.create();
980 mPSIcons.setName("PSIcons");
981
982 //bs.setDitherEnable(false);
983 //mPSText = bs.create();
984 //mPSText.setName("PSText");
985 }
986
987 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700988 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700989 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700990 }
Jason Sams78aebd82009-09-15 13:06:59 -0700991
Joe Onorato1feb3a82009-08-08 22:32:00 -0700992 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700993 mParams = new Params();
994 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700995
Joe Onoratobf15cb42009-08-07 14:33:40 -0700996 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700997
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700998 mParams.bubbleWidth = bubble.getBubbleWidth();
999 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1000 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1001 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001002
Joe Onoratod63458b2009-10-15 21:19:09 -07001003 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1004 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1005 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001006 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1007 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1008 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001009 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1010 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1011 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001012 mParams.homeButtonWidth = 76;
1013 mParams.homeButtonHeight = 68;
1014 mParams.homeButtonTextureWidth = 128;
1015 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001016
Joe Onoratod63458b2009-10-15 21:19:09 -07001017 mState.homeButtonId = mHomeButtonNormal.getID();
1018
Joe Onorato1feb3a82009-08-08 22:32:00 -07001019 mParams.save();
1020 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021
Joe Onorato1291a8c2009-09-15 15:07:25 -04001022 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1023 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1024 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001025
Joe Onorato9c1289c2009-08-17 11:03:03 -04001026 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001027 }
1028
Jason Sams37e7c2b2009-10-19 12:55:43 -07001029 private void initScript(int id) {
1030 }
1031
1032 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001033 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001034 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -07001035 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001036 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001037 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1038 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001039 mInvokeMove = sb.addInvokable("move");
1040 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001041 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001042 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001043 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001044 mScript = sb.create();
1045 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1046 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1047 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1048 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1049 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001050
Jason Sams37e7c2b2009-10-19 12:55:43 -07001051 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001052 }
Joe Onorato93839052009-08-06 20:34:32 -07001053
Jason Sams20df7c72009-11-05 12:30:24 -08001054 private void uploadApps(ArrayList<ApplicationInfo> list) {
1055 for (int i=0; i < mState.iconCount; i++) {
1056 uploadAppIcon(i, list.get(i));
1057 }
1058 }
1059
1060 void dirtyCheck() {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001061 if (mHasSurface) {
1062 if (mAppsDirty) {
1063 uploadApps(mAllAppsList);
1064 saveAppsList();
1065 mAppsDirty = false;
1066 }
1067 if (mZoomDirty) {
Joe Onorato68ba5ca2009-11-12 14:23:43 -08001068 setZoom(mZoom, mAnimateNextZoom);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001069 }
Jason Sams20df7c72009-11-05 12:30:24 -08001070 }
1071 }
1072
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073 private void setApps(ArrayList<ApplicationInfo> list) {
1074 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001075 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001076 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001077 allocCount = 1;
1078 }
1079
Joe Onorato9c1289c2009-08-17 11:03:03 -04001080 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001081 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001082 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001083
1084 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001085 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001086 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001088 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001089
1090 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1091
Joe Onorato9c1289c2009-08-17 11:03:03 -04001092 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001093 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001094 saveAppsList();
1095 }
1096
Joe Onorato3a8820b2009-11-10 15:06:42 -08001097 private void setZoom(float zoom, boolean animate) {
1098 mRollo.clearSelectedIcon();
1099 mRollo.setHomeSelected(SELECTED_NONE);
1100 if (zoom > 0.001f) {
1101 mRollo.mState.zoomTarget = zoom;
1102 } else {
1103 mRollo.mState.zoomTarget = 0;
1104 }
1105 mRollo.mState.save();
1106 if (!animate) {
1107 mRollo.mInvokeSetZoom.execute();
1108 }
1109 }
1110
Jason Samsc8514792009-10-29 14:27:29 -07001111 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1112 int black[] = new int[w > h ? w : h];
1113 Allocation.Adapter2D a = alloc.createAdapter2D();
1114 int mip = 0;
1115 while (w > 1 || h > 1) {
1116 a.subData(0, 0, 1, h, black);
1117 a.subData(w-1, 0, 1, h, black);
1118 a.subData(0, 0, w, 1, black);
1119 a.subData(0, h-1, w, 1, black);
1120 mip++;
1121 w = (w + 1) >> 1;
1122 h = (h + 1) >> 1;
1123 a.setConstraint(Dimension.LOD, mip);
1124 }
1125 a.subData(0, 0, 1, 1, black);
1126 }
1127
Joe Onoratoa8138d52009-10-06 19:25:30 -07001128 private void uploadAppIcon(int index, ApplicationInfo item) {
1129 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001130 Element.RGBA_8888(mRS), true);
1131 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1132
Joe Onoratoa8138d52009-10-06 19:25:30 -07001133 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001134 Element.RGBA_8888(mRS), true);
1135 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001136
1137 mIcons[index].uploadToTexture(0);
1138 mLabels[index].uploadToTexture(0);
1139
1140 mIconIds[index] = mIcons[index].getID();
1141 mLabelIds[index] = mLabels[index].getID();
1142 }
1143
1144 /**
1145 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1146 * fill in the values and call saveAppsList().
1147 */
1148 private void reallocAppsList(int count) {
1149 Allocation[] icons = new Allocation[count];
1150 int[] iconIds = new int[count];
1151 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1152
1153 Allocation[] labels = new Allocation[count];
1154 int[] labelIds = new int[count];
1155 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1156
1157 final int oldCount = mIcons.length;
1158
1159 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1160 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1161 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1162 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1163
1164 mIcons = icons;
1165 mIconIds = iconIds;
1166 mLabels = labels;
1167 mLabelIds = labelIds;
1168 }
1169
1170 /**
1171 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1172 */
1173 private void addApp(int index, ApplicationInfo item) {
1174 final int count = mState.iconCount - index;
1175 final int dest = index + 1;
1176
1177 System.arraycopy(mIcons, index, mIcons, dest, count);
1178 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1179 System.arraycopy(mLabels, index, mLabels, dest, count);
1180 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1181
Jason Sams20df7c72009-11-05 12:30:24 -08001182 if (mHasSurface ) {
1183 uploadAppIcon(index, item);
1184 } else {
1185 mAppsDirty = true;
1186 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001187 }
1188
1189 /**
1190 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1191 */
1192 private void removeApp(int index) {
1193 final int count = mState.iconCount - index - 1;
1194 final int src = index + 1;
1195
1196 System.arraycopy(mIcons, src, mIcons, index, count);
1197 System.arraycopy(mIconIds, src, mIconIds, index, count);
1198 System.arraycopy(mLabels, src, mLabels, index, count);
1199 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1200
1201 final int last = mState.iconCount - 1;
1202 mIcons[last] = null;
1203 mIconIds[last] = 0;
1204 mLabels[last] = null;
1205 mLabelIds[last] = 0;
1206 }
1207
1208 /**
1209 * Send the apps list structures to RS.
1210 */
1211 private void saveAppsList() {
1212 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001213
Joe Onoratoa8138d52009-10-06 19:25:30 -07001214 mAllocIconIds.data(mIconIds);
1215 mAllocLabelIds.data(mLabelIds);
1216
Jason Sams37e7c2b2009-10-19 12:55:43 -07001217 if (mScript != null) { // this happens when we init it
1218 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1219 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001220 }
1221
1222 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001223
1224 // Note: mScript may be null if we haven't initialized it yet.
1225 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001226 if (mInvokeResetWAR != null) {
1227 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001228 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001229 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001230 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001231
1232 void initTouchState() {
1233 int width = getWidth();
1234 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001235 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1236 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001237
Jason Sams37e7c2b2009-10-19 12:55:43 -07001238 int centerY = (height / 2);
1239 mTouchYBorders[0] = centerY - (cellHeight * 2);
1240 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001241 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001242 mTouchYBorders[3] = centerY + cellHeight;
1243 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001244
Joe Onorato6665c0f2009-09-02 15:27:24 -07001245 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001246 mTouchXBorders[0] = 0;
1247 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001248 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001249 mTouchXBorders[3] = centerX + (width / 4);
1250 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001251 }
1252
Joe Onorato664457d2009-10-28 16:30:34 -04001253 void fling() {
1254 mInvokeFling.execute();
1255 }
1256
1257 void move() {
1258 mInvokeMove.execute();
1259 }
1260
1261 void moveTo(float row) {
1262 mState.targetPos = row;
1263 mState.save();
1264 mInvokeMoveTo.execute();
1265 }
1266
Jason Sams37e7c2b2009-10-19 12:55:43 -07001267 int chooseTappedIcon(int x, int y, float pos) {
1268 // Adjust for scroll position if not zero.
1269 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001270
Joe Onorato6665c0f2009-09-02 15:27:24 -07001271 int col = -1;
1272 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001273 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1274 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1275 col = i;
1276 break;
1277 }
1278 }
1279 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1280 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1281 row = i;
1282 break;
1283 }
1284 }
1285
1286 if (row < 0 || col < 0) {
1287 return -1;
1288 }
1289
Joe Onorato664457d2009-10-28 16:30:34 -04001290 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001291 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001292
Joe Onorato664457d2009-10-28 16:30:34 -04001293 if (index >= mState.iconCount) {
1294 return -1;
1295 } else {
1296 return index;
1297 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001298 }
1299
Joe Onorato6665c0f2009-09-02 15:27:24 -07001300 /**
1301 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001302 *
1303 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001304 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001305 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001306 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001307 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001308 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001309 }
1310
Joe Onoratoc61cff92009-11-08 11:54:39 -05001311 /**
1312 * Select the icon at the given index.
1313 *
1314 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001315 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001316 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001317 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001318 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001319 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001320 if (mLastSelection == SELECTION_ICONS) {
1321 mLastSelection = SELECTION_NONE;
1322 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001323 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001324 if (pressed == SELECTED_FOCUSED) {
1325 mLastSelection = SELECTION_ICONS;
1326 }
1327
Joe Onorato52a653f2009-11-11 14:52:11 -08001328 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001329 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001330
Joe Onorato52a653f2009-11-11 14:52:11 -08001331 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001332 Bitmap selectionBitmap = mSelectionBitmap;
1333
1334 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001335 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001336 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001337
1338 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001339 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001340 mSelectedIcon.uploadToTexture(0);
1341 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001342
1343 if (prev != index) {
1344 if (info.title != null && info.title.length() > 0) {
1345 Log.d(TAG, "sendAccessibilityEvent SELECTION_ICONS " + info.title);
1346 //setContentDescription(info.title);
1347 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1348 }
1349 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001350 }
1351 }
1352
1353 /**
1354 * You need to call save() on mState on your own after calling this.
1355 */
1356 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001357 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001358 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001359
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001360 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001361 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001362 switch (mode) {
1363 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001364 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001365 break;
1366 case SELECTED_FOCUSED:
1367 mLastSelection = SELECTION_HOME;
1368 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001369 if (prev != SELECTION_HOME) {
1370 Log.d(TAG, "sendAccessibilityEvent SELECTION_HOME");
1371 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1372 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001373 break;
1374 case SELECTED_PRESSED:
1375 mState.homeButtonId = mHomeButtonPressed.getID();
1376 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001377 }
1378 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001379 }
Joe Onorato93839052009-08-06 20:34:32 -07001380}
1381
1382