blob: baa717489580d8fd8e660eba7a7423de43b652ad [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();
Joe Onoratoc567acb2009-08-31 14:34:43 -0700217
Joe Onoratocb75f362009-11-12 13:04:07 -0800218 mRS.mMessageCallback = mMessageProc = new AAMessage();
219
Joe Onoratoc567acb2009-08-31 14:34:43 -0700220 Resources res = getContext().getResources();
221 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700222
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800223 //long endTime = SystemClock.uptimeMillis();
224 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700225 }
Jason Sams2e19c052009-10-20 18:19:55 -0700226
Joe Onorato93839052009-08-06 20:34:32 -0700227 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800228 public void onWindowFocusChanged(boolean hasWindowFocus) {
229 super.onWindowFocusChanged(hasWindowFocus);
230 if (mArrowNavigation) {
231 if (!hasWindowFocus) {
232 // Clear selection when we lose window focus
233 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500234 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800235 mRollo.clearSelectedIcon();
236 mRollo.mState.save();
237 } else if (hasWindowFocus) {
238 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500239 if (mLastSelection == SELECTION_ICONS) {
240 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800241 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500242 Defines.COLUMNS_PER_PAGE;
243 if (selection < 0 || // No selection
244 selection < firstIcon || // off the top of the screen
245 selection >= mRollo.mState.iconCount || // past last icon
246 selection >= firstIcon + // past last icon on screen
247 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
248 selection = firstIcon;
249 }
250
251 // Select the first icon when we gain window focus
252 mRollo.selectIcon(selection, SELECTED_FOCUSED);
253 mRollo.mState.save();
254 } else if (mLastSelection == SELECTION_HOME) {
255 mRollo.setHomeSelected(SELECTED_FOCUSED);
256 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800257 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800258 }
259 }
260 }
261 }
262
263 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700264 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
265 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400266
267 if (!isVisible()) {
268 return;
269 }
270
Mike Cleron7d5d7462009-10-20 14:06:00 -0700271 if (gainFocus) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800272 if (mRollo != null) {
273 gainFocus();
274 } else {
275 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700276 }
277 } else {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800278 if (mRollo != null) {
279 if (mArrowNavigation) {
280 // Clear selection when we lose focus
281 mRollo.clearSelectedIcon();
282 mRollo.setHomeSelected(SELECTED_NONE);
283 mRollo.mState.save();
284 mArrowNavigation = false;
285 }
286 } else {
287 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700288 }
289 }
290 }
291
Mike Cleronb64b67a2009-11-08 14:56:25 -0800292 private void gainFocus() {
293 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
294 // Select the first icon when we gain keyboard focus
295 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800296 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800297 SELECTED_FOCUSED);
298 mRollo.mState.save();
299 }
300 }
301
Mike Cleron7d5d7462009-10-20 14:06:00 -0700302 @Override
303 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700304
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800305 boolean handled = false;
Jason Samse26d9fc2009-11-12 14:00:43 -0800306
Joe Onorato859b3a72009-10-28 15:17:01 -0400307 if (!isVisible()) {
308 return false;
309 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500310 final int iconCount = mRollo.mState.iconCount;
311
Mike Cleron7d5d7462009-10-20 14:06:00 -0700312 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
313 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500314 if (mLastSelection == SELECTION_HOME) {
315 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
316 mLauncher.closeAllApps(true);
317 } else {
318 int whichApp = mRollo.mState.selectedIconIndex;
319 if (whichApp >= 0) {
320 ApplicationInfo app = mAllAppsList.get(whichApp);
321 mLauncher.startActivitySafely(app.intent);
322 handled = true;
323 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700324 }
325 }
326 }
Jason Sams2e19c052009-10-20 18:19:55 -0700327
Joe Onoratoa13f5742009-11-02 17:15:19 -0500328 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700329 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700330
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800331 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800332 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700333
Mike Cleron7d5d7462009-10-20 14:06:00 -0700334 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500335 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700336
Mike Cleron7d5d7462009-10-20 14:06:00 -0700337 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500338 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700339 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700340
Mike Cleron7d5d7462009-10-20 14:06:00 -0700341 int newSelection = currentSelection;
342
343 switch (keyCode) {
344 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500345 if (mLastSelection == SELECTION_HOME) {
346 mRollo.setHomeSelected(SELECTED_NONE);
347 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
348 if (lastRowCount == 0) {
349 lastRowCount = Defines.COLUMNS_PER_PAGE;
350 }
351 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
352 if (newSelection >= iconCount) {
353 newSelection = iconCount-1;
354 }
355 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
356 - (Defines.ROWS_PER_PAGE - 1);
357 if (target < 0) {
358 target = 0;
359 }
360 if (currentTopRow != target) {
361 mRollo.moveTo(target);
362 }
363 } else {
364 if (currentPageRow > 0) {
365 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
366 } else if (currentTopRow > 0) {
367 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
368 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
369 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700370 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800371 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700372 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800373
Joe Onoratoa13f5742009-11-02 17:15:19 -0500374 case KeyEvent.KEYCODE_DPAD_DOWN: {
375 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
376 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
377 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500378 if (mLastSelection != SELECTION_HOME) {
379 if (currentRow < rowCount-1) {
380 mRollo.setHomeSelected(SELECTED_NONE);
381 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
382 if (newSelection >= iconCount) {
383 // Go from D to G in this arrangement:
384 // A B C D
385 // E F G
386 newSelection = iconCount - 1;
387 }
388 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
389 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
390 Defines.ROWS_PER_PAGE + 1);
391 }
392 } else {
393 newSelection = -1;
394 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700395 }
396 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800397 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700398 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500399 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700400 case KeyEvent.KEYCODE_DPAD_LEFT:
401 if (currentPageCol > 0) {
402 newSelection = currentSelection - 1;
403 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800404 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700405 break;
406 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700407 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500408 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700409 newSelection = currentSelection + 1;
410 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800411 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700412 break;
413 }
414 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500415 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700416 mRollo.mState.save();
417 }
418 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800419 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700420 }
421
Joe Onorato93839052009-08-06 20:34:32 -0700422 @Override
423 public boolean onTouchEvent(MotionEvent ev)
424 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700425 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700426
Joe Onorato7bb17492009-09-24 17:51:01 -0700427 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700428 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700429 }
430
Joe Onoratofb0ca672009-09-14 17:55:46 -0400431 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700432 return true;
433 }
434
435 super.onTouchEvent(ev);
436
Joe Onoratofb0ca672009-09-14 17:55:46 -0400437 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700438 int y = (int)ev.getY();
439
Joe Onoratobcbeab82009-10-01 21:45:43 -0700440 int action = ev.getAction();
441 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400442 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700443 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
444 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500445 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700446 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700447 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700448 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700449 mTouchTracking = TRACKING_FLING;
450
451 mMotionDownRawX = (int)ev.getRawX();
452 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700453
Mike Cleron7d5d7462009-10-20 14:06:00 -0700454 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700455 mRollo.mState.newTouchDown = 1;
456
457 if (!mRollo.checkClickOK()) {
458 mRollo.clearSelectedIcon();
459 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700460 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800461 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700462 if (mDownIconIndex < 0) {
463 // if nothing was selected, no long press.
464 cancelLongPress();
465 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700466 }
467 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700468 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800469 mVelocityTracker = VelocityTracker.obtain();
470 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700471 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700472 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400473 break;
474 case MotionEvent.ACTION_MOVE:
475 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700476 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500477 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
478 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700479 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700480 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700481 int rawX = (int)ev.getRawX();
482 int rawY = (int)ev.getRawY();
483 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700484 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700485
Joe Onorato82ca5502009-10-15 16:59:23 -0700486 if (!mStartedScrolling && slop < mSlop) {
487 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700488 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800489 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700490 if (mDownIconIndex != mCurrentIconIndex) {
491 // If a different icon is selected, don't allow it to be picked up.
492 // This handles off-axis dragging.
493 cancelLongPress();
494 mCurrentIconIndex = -1;
495 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700496 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700497 if (!mStartedScrolling) {
498 cancelLongPress();
499 mCurrentIconIndex = -1;
500 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700501 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700502 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700503 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700504
Joe Onoratobcbeab82009-10-01 21:45:43 -0700505 mStartedScrolling = true;
506 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800507 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700508 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700509 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400510 }
511 break;
512 case MotionEvent.ACTION_UP:
513 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700514 if (mTouchTracking == TRACKING_HOME) {
515 if (action == MotionEvent.ACTION_UP) {
516 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400517 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700518 mLauncher.closeAllApps(true);
519 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500520 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700521 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700522 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700523 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700524 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700525 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700526 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700527
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800528 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
529 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700530 mRollo.clearSelectedIcon();
531 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700532 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700533
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800534 if (mVelocityTracker != null) {
535 mVelocityTracker.recycle();
536 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700537 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700538 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700539 mTouchTracking = TRACKING_NONE;
540 break;
Joe Onorato93839052009-08-06 20:34:32 -0700541 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700542
543 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700544 }
545
Joe Onorato6665c0f2009-09-02 15:27:24 -0700546 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700547 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400548 return;
549 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700550 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
551 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400552 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700553 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700554 mLauncher.startActivitySafely(app.intent);
555 }
556 }
557
558 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700559 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400560 return true;
561 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700562 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
563 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
564 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700565
566 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700567 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
568 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700569
Joe Onoratobcbeab82009-10-01 21:45:43 -0700570 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
571 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700572 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700573 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700574 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700575
Joe Onorato7bb17492009-09-24 17:51:01 -0700576 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700577 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700578 return true;
579 }
580
Joe Onorato52a653f2009-11-11 14:52:11 -0800581 @Override
582 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
583 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
584 if (!isVisible()) {
585 return false;
586 }
587 String text = null;
588 int index;
589 int count = mAllAppsList.size() + 1; // +1 is home
590 int pos = -1;
591 switch (mLastSelection) {
592 case SELECTION_ICONS:
593 index = mRollo.mState.selectedIconIndex;
594 if (index >= 0) {
595 ApplicationInfo info = mAllAppsList.get(index);
596 if (info.title != null) {
597 text = info.title.toString();
598 pos = index;
599 }
600 }
601 break;
602 case SELECTION_HOME:
603 text = getContext().getString(R.string.all_apps_home_button_label);
604 pos = count;
605 break;
606 }
607 if (text != null) {
608 Log.d(TAG, "dispatchPopulateAccessibilityEvent setting text=" + text);
609 event.setEnabled(true);
610 event.getText().add(text);
611 //event.setContentDescription(text);
612 event.setItemCount(count);
613 event.setCurrentItemIndex(pos);
614 }
615 }
616 return false;
617 }
618
Joe Onorato5162ea92009-09-03 09:39:42 -0700619 public void setDragController(DragController dragger) {
620 mDragController = dragger;
621 }
622
623 public void onDropCompleted(View target, boolean success) {
624 }
625
Joe Onorato4db52312009-10-06 11:17:43 -0700626 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800627 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700628 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800629 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700630 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800631 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400632 cancelLongPress();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800633 if (mRollo == null || !mRollo.mHasSurface) {
634 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800635 mZoom = zoom;
636 mAnimateNextZoom = animate;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800637 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700638 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800639 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700640 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400641 }
642
643 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800644 Log.d(TAG, "isVisible mZoomDirty=" + mZoomDirty
645 + " mMessageProc=" + (mMessageProc == null ? "null" : mZoom));
646 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800647 }
648
649 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800650 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400651 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700652
Joe Onorato9c1289c2009-08-17 11:03:03 -0400653 public void setApps(ArrayList<ApplicationInfo> list) {
654 mAllAppsList = list;
655 if (mRollo != null) {
656 mRollo.setApps(list);
657 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400658 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700659 }
660
Joe Onoratoa8138d52009-10-06 19:25:30 -0700661 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500662 if (mAllAppsList == null) {
663 // Not done loading yet. We'll find out about it later.
664 return;
665 }
666
Joe Onoratoa8138d52009-10-06 19:25:30 -0700667 final int N = list.size();
668 if (mRollo != null) {
669 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
670 }
671
672 for (int i=0; i<N; i++) {
673 final ApplicationInfo item = list.get(i);
674 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
675 if (index < 0) {
676 index = -(index+1);
677 }
678 mAllAppsList.add(index, item);
679 if (mRollo != null) {
680 mRollo.addApp(index, item);
681 mRollo.mState.iconCount++;
682 }
683 }
684
685 if (mRollo != null) {
686 mRollo.saveAppsList();
687 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700688 }
689
Joe Onoratoa8138d52009-10-06 19:25:30 -0700690 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500691 if (mAllAppsList == null) {
692 // Not done loading yet. We'll find out about it later.
693 return;
694 }
695
Joe Onoratoa8138d52009-10-06 19:25:30 -0700696 final int N = list.size();
697 for (int i=0; i<N; i++) {
698 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400699 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700700 if (index >= 0) {
701 mAllAppsList.remove(index);
702 if (mRollo != null) {
703 mRollo.removeApp(index);
704 mRollo.mState.iconCount--;
705 }
706 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800707 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700708 // Try to recover. This should keep us from crashing for now.
709 }
710 }
711
712 if (mRollo != null) {
713 mRollo.saveAppsList();
714 }
715 }
716
717 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
718 // Just remove and add, because they may need to be re-sorted.
719 removeApps(list);
720 addApps(list);
721 }
722
723 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
724 public int compare(ApplicationInfo a, ApplicationInfo b) {
725 int result = a.title.toString().compareTo(b.toString());
726 if (result != 0) {
727 return result;
728 }
729 return a.intent.getComponent().compareTo(b.intent.getComponent());
730 }
731 };
732
Joe Onoratocb9f7982009-10-31 16:32:02 -0400733 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
734 ComponentName component = item.intent.getComponent();
735 final int N = list.size();
736 for (int i=0; i<N; i++) {
737 ApplicationInfo x = list.get(i);
738 if (x.intent.getComponent().equals(component)) {
739 return i;
740 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700741 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400742 return -1;
743 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700744
Joe Onoratoc567acb2009-08-31 14:34:43 -0700745 private static int countPages(int iconCount) {
746 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
747 int pages = iconCount / iconsPerPage;
748 if (pages*iconsPerPage != iconCount) {
749 pages++;
750 }
751 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 }
753
Joe Onoratocb75f362009-11-12 13:04:07 -0800754 class AAMessage extends RenderScript.RSMessage {
755 public void run() {
756 mPosX = ((float)mData[0]) / (1 << 16);
757 mVelocity = ((float)mData[1]) / (1 << 16);
758 mZoom = ((float)mData[2]) / (1 << 16);
759 mZoomDirty = false;
760 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800761 }
762
Joe Onorato93839052009-08-06 20:34:32 -0700763 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700764
Joe Onorato1feb3a82009-08-08 22:32:00 -0700765 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700766 private int mWidth;
767 private int mHeight;
768
769 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700770 private Script mScript;
771 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700772 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700773 private Script.Invokable mInvokeFling;
774 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800775 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700776
Jason Samscd689e12009-09-29 15:28:22 -0700777 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700778 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700779 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700780 private ProgramFragment mPFTexMip;
781 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700782 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700783 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700784 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700785 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700786
Joe Onoratod63458b2009-10-15 21:19:09 -0700787 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500788 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700789 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700790
Joe Onoratobf15cb42009-08-07 14:33:40 -0700791 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700792 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700793 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700794
Joe Onoratobf15cb42009-08-07 14:33:40 -0700795 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700796 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700797 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700798 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700799
Joe Onorato6665c0f2009-09-02 15:27:24 -0700800 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700801 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700802
803 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400804 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700805
Jason Sams20df7c72009-11-05 12:30:24 -0800806 boolean mHasSurface = false;
807 private boolean mAppsDirty = false;
Jason Samsd8152b92009-10-13 17:19:10 -0700808
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700809 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700810 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700811
Jason Sams78aebd82009-09-15 13:06:59 -0700812 class BaseAlloc {
813 Allocation mAlloc;
814 Type mType;
815
816 void save() {
817 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700818 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700819 }
820
Jason Sams476339d2009-09-29 18:14:38 -0700821 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800822 return (Math.abs(mVelocity) < 0.4f) &&
823 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700824 }
825
Jason Sams78aebd82009-09-15 13:06:59 -0700826 class Params extends BaseAlloc {
827 Params() {
828 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
829 mAlloc = Allocation.createTyped(mRS, mType);
830 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700831 }
Jason Sams78aebd82009-09-15 13:06:59 -0700832 public int bubbleWidth;
833 public int bubbleHeight;
834 public int bubbleBitmapWidth;
835 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700836
Joe Onoratobcbeab82009-10-01 21:45:43 -0700837 public int homeButtonWidth;
838 public int homeButtonHeight;
839 public int homeButtonTextureWidth;
840 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700841 }
842
843 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700844 public float newPositionX;
845 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700846 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700847 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700848 public int selectedIconIndex = -1;
849 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700850 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700851 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700852 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700853
854 State() {
855 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
856 mAlloc = Allocation.createTyped(mRS, mType);
857 save();
858 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700859 }
860
861 public RolloRS() {
862 }
863
864 public void init(Resources res, int width, int height) {
865 mRes = res;
866 mWidth = width;
867 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700868 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700869 initProgramVertex();
870 initProgramFragment();
871 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700872 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700873 initGl();
874 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700875 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700876 initRs();
877 }
878
Jason Sams0aa71662009-10-02 18:43:18 -0700879 public void initMesh() {
880 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
881 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
882
Jason Samsd8152b92009-10-13 17:19:10 -0700883 float y = 0;
884 float z = 0;
885 for (int ct=0; ct < 200; ct++) {
886 float angle = 0;
887 float maxAngle = 3.14f * 0.16f;
888 float l = 1.f;
889
890 l = 1 - ((ct-5) * 0.10f);
891 if (ct > 7) {
892 angle = maxAngle * (ct - 7) * 0.2f;
893 angle = Math.min(angle, maxAngle);
894 }
Jason Samsc8514792009-10-29 14:27:29 -0700895 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700896 l = Math.min(1.0f, l);
897
898 y += 0.1f * Math.cos(angle);
899 z += 0.1f * Math.sin(angle);
900
901 float t = 0.1f * ct;
902 float ds = 0.08f;
903 tm.setColor(l, l, l, 0.99f);
904 tm.setTexture(ds, t);
905 tm.addVertex(-0.5f, y, z);
906 tm.setTexture(1 - ds, t);
907 tm.addVertex(0.5f, y, z);
908 }
909 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
910 tm.addTriangle(ct, ct+1, ct+2);
911 tm.addTriangle(ct+1, ct+3, ct+2);
912 }
913 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700914 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700915 }
916
Jason Samscd689e12009-09-29 15:28:22 -0700917 private void initProgramVertex() {
918 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
919 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700920
921 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700922 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700923 mPV = pvb.create();
924 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700925 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700926
Jason Sams37e7c2b2009-10-19 12:55:43 -0700927 //pva = new ProgramVertex.MatrixAllocation(mRS);
928 //pva.setupOrthoWindow(mWidth, mHeight);
929 //pvb.setTextureMatrixEnable(true);
930 //mPVOrtho = pvb.create();
931 //mPVOrtho.setName("PVOrtho");
932 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700933
934 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700935 }
Joe Onorato93839052009-08-06 20:34:32 -0700936
Jason Samscd689e12009-09-29 15:28:22 -0700937 private void initProgramFragment() {
938 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700939 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700940 sb.setMag(Sampler.Value.LINEAR);
941 sb.setWrapS(Sampler.Value.CLAMP);
942 sb.setWrapT(Sampler.Value.CLAMP);
943 Sampler linear = sb.create();
944
945 sb.setMin(Sampler.Value.NEAREST);
946 sb.setMag(Sampler.Value.NEAREST);
947 Sampler nearest = sb.create();
948
949 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700950 //mPFColor = bf.create();
951 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700952
953 bf.setTexEnable(true, 0);
954 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700955 mPFTexMip = bf.create();
956 mPFTexMip.setName("PFTexMip");
957 mPFTexMip.bindSampler(linear, 0);
958
959 mPFTexNearest = bf.create();
960 mPFTexNearest.setName("PFTexNearest");
961 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700962 }
963
964 private void initProgramStore() {
965 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
966 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700967 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700968 bs.setDitherEnable(true);
969 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
970 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
971 mPSIcons = bs.create();
972 mPSIcons.setName("PSIcons");
973
974 //bs.setDitherEnable(false);
975 //mPSText = bs.create();
976 //mPSText.setName("PSText");
977 }
978
979 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700980 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700981 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700982 }
Jason Sams78aebd82009-09-15 13:06:59 -0700983
Joe Onorato1feb3a82009-08-08 22:32:00 -0700984 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700985 mParams = new Params();
986 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700987
Joe Onoratobf15cb42009-08-07 14:33:40 -0700988 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700989
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700990 mParams.bubbleWidth = bubble.getBubbleWidth();
991 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
992 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
993 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700994
Joe Onoratod63458b2009-10-15 21:19:09 -0700995 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
996 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
997 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500998 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
999 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1000 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001001 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1002 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1003 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001004 mParams.homeButtonWidth = 76;
1005 mParams.homeButtonHeight = 68;
1006 mParams.homeButtonTextureWidth = 128;
1007 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001008
Joe Onoratod63458b2009-10-15 21:19:09 -07001009 mState.homeButtonId = mHomeButtonNormal.getID();
1010
Joe Onorato1feb3a82009-08-08 22:32:00 -07001011 mParams.save();
1012 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001013
Joe Onorato1291a8c2009-09-15 15:07:25 -04001014 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1015 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1016 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001017
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001019 }
1020
Jason Sams37e7c2b2009-10-19 12:55:43 -07001021 private void initScript(int id) {
1022 }
1023
1024 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001025 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001026 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -07001027 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001028 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001029 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1030 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001031 mInvokeMove = sb.addInvokable("move");
1032 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001033 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001034 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001035 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001036 mScript = sb.create();
1037 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1038 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1039 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1040 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1041 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001042
Jason Sams37e7c2b2009-10-19 12:55:43 -07001043 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001044 }
Joe Onorato93839052009-08-06 20:34:32 -07001045
Jason Sams20df7c72009-11-05 12:30:24 -08001046 private void uploadApps(ArrayList<ApplicationInfo> list) {
1047 for (int i=0; i < mState.iconCount; i++) {
1048 uploadAppIcon(i, list.get(i));
1049 }
1050 }
1051
1052 void dirtyCheck() {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001053 if (mHasSurface) {
1054 if (mAppsDirty) {
1055 uploadApps(mAllAppsList);
1056 saveAppsList();
1057 mAppsDirty = false;
1058 }
1059 if (mZoomDirty) {
Joe Onorato68ba5ca2009-11-12 14:23:43 -08001060 setZoom(mZoom, mAnimateNextZoom);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001061 }
Jason Sams20df7c72009-11-05 12:30:24 -08001062 }
1063 }
1064
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065 private void setApps(ArrayList<ApplicationInfo> list) {
1066 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001067 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001068 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001069 allocCount = 1;
1070 }
1071
Joe Onorato9c1289c2009-08-17 11:03:03 -04001072 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001073 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001074 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075
1076 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001077 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001078 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001079
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001080 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001081
1082 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1083
Joe Onorato9c1289c2009-08-17 11:03:03 -04001084 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001085 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001086 saveAppsList();
1087 }
1088
Joe Onorato3a8820b2009-11-10 15:06:42 -08001089 private void setZoom(float zoom, boolean animate) {
1090 mRollo.clearSelectedIcon();
1091 mRollo.setHomeSelected(SELECTED_NONE);
1092 if (zoom > 0.001f) {
1093 mRollo.mState.zoomTarget = zoom;
1094 } else {
1095 mRollo.mState.zoomTarget = 0;
1096 }
1097 mRollo.mState.save();
1098 if (!animate) {
1099 mRollo.mInvokeSetZoom.execute();
1100 }
1101 }
1102
Jason Samsc8514792009-10-29 14:27:29 -07001103 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1104 int black[] = new int[w > h ? w : h];
1105 Allocation.Adapter2D a = alloc.createAdapter2D();
1106 int mip = 0;
1107 while (w > 1 || h > 1) {
1108 a.subData(0, 0, 1, h, black);
1109 a.subData(w-1, 0, 1, h, black);
1110 a.subData(0, 0, w, 1, black);
1111 a.subData(0, h-1, w, 1, black);
1112 mip++;
1113 w = (w + 1) >> 1;
1114 h = (h + 1) >> 1;
1115 a.setConstraint(Dimension.LOD, mip);
1116 }
1117 a.subData(0, 0, 1, 1, black);
1118 }
1119
Joe Onoratoa8138d52009-10-06 19:25:30 -07001120 private void uploadAppIcon(int index, ApplicationInfo item) {
1121 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001122 Element.RGBA_8888(mRS), true);
1123 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1124
Joe Onoratoa8138d52009-10-06 19:25:30 -07001125 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001126 Element.RGBA_8888(mRS), true);
1127 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001128
1129 mIcons[index].uploadToTexture(0);
1130 mLabels[index].uploadToTexture(0);
1131
1132 mIconIds[index] = mIcons[index].getID();
1133 mLabelIds[index] = mLabels[index].getID();
1134 }
1135
1136 /**
1137 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1138 * fill in the values and call saveAppsList().
1139 */
1140 private void reallocAppsList(int count) {
1141 Allocation[] icons = new Allocation[count];
1142 int[] iconIds = new int[count];
1143 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1144
1145 Allocation[] labels = new Allocation[count];
1146 int[] labelIds = new int[count];
1147 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1148
1149 final int oldCount = mIcons.length;
1150
1151 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1152 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1153 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1154 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1155
1156 mIcons = icons;
1157 mIconIds = iconIds;
1158 mLabels = labels;
1159 mLabelIds = labelIds;
1160 }
1161
1162 /**
1163 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1164 */
1165 private void addApp(int index, ApplicationInfo item) {
1166 final int count = mState.iconCount - index;
1167 final int dest = index + 1;
1168
1169 System.arraycopy(mIcons, index, mIcons, dest, count);
1170 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1171 System.arraycopy(mLabels, index, mLabels, dest, count);
1172 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1173
Jason Sams20df7c72009-11-05 12:30:24 -08001174 if (mHasSurface ) {
1175 uploadAppIcon(index, item);
1176 } else {
1177 mAppsDirty = true;
1178 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001179 }
1180
1181 /**
1182 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1183 */
1184 private void removeApp(int index) {
1185 final int count = mState.iconCount - index - 1;
1186 final int src = index + 1;
1187
1188 System.arraycopy(mIcons, src, mIcons, index, count);
1189 System.arraycopy(mIconIds, src, mIconIds, index, count);
1190 System.arraycopy(mLabels, src, mLabels, index, count);
1191 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1192
1193 final int last = mState.iconCount - 1;
1194 mIcons[last] = null;
1195 mIconIds[last] = 0;
1196 mLabels[last] = null;
1197 mLabelIds[last] = 0;
1198 }
1199
1200 /**
1201 * Send the apps list structures to RS.
1202 */
1203 private void saveAppsList() {
1204 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001205
Joe Onoratoa8138d52009-10-06 19:25:30 -07001206 mAllocIconIds.data(mIconIds);
1207 mAllocLabelIds.data(mLabelIds);
1208
Jason Sams37e7c2b2009-10-19 12:55:43 -07001209 if (mScript != null) { // this happens when we init it
1210 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1211 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 }
1213
1214 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001215
1216 // Note: mScript may be null if we haven't initialized it yet.
1217 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001218 if (mInvokeResetWAR != null) {
1219 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001220 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001221 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001222 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001223
1224 void initTouchState() {
1225 int width = getWidth();
1226 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001227 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1228 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001229
Jason Sams37e7c2b2009-10-19 12:55:43 -07001230 int centerY = (height / 2);
1231 mTouchYBorders[0] = centerY - (cellHeight * 2);
1232 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001233 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001234 mTouchYBorders[3] = centerY + cellHeight;
1235 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001236
Joe Onorato6665c0f2009-09-02 15:27:24 -07001237 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001238 mTouchXBorders[0] = 0;
1239 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001240 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001241 mTouchXBorders[3] = centerX + (width / 4);
1242 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001243 }
1244
Joe Onorato664457d2009-10-28 16:30:34 -04001245 void fling() {
1246 mInvokeFling.execute();
1247 }
1248
1249 void move() {
1250 mInvokeMove.execute();
1251 }
1252
1253 void moveTo(float row) {
1254 mState.targetPos = row;
1255 mState.save();
1256 mInvokeMoveTo.execute();
1257 }
1258
Jason Sams37e7c2b2009-10-19 12:55:43 -07001259 int chooseTappedIcon(int x, int y, float pos) {
1260 // Adjust for scroll position if not zero.
1261 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001262
Joe Onorato6665c0f2009-09-02 15:27:24 -07001263 int col = -1;
1264 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001265 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1266 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1267 col = i;
1268 break;
1269 }
1270 }
1271 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1272 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1273 row = i;
1274 break;
1275 }
1276 }
1277
1278 if (row < 0 || col < 0) {
1279 return -1;
1280 }
1281
Joe Onorato664457d2009-10-28 16:30:34 -04001282 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001283 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001284
Joe Onorato664457d2009-10-28 16:30:34 -04001285 if (index >= mState.iconCount) {
1286 return -1;
1287 } else {
1288 return index;
1289 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001290 }
1291
Joe Onorato6665c0f2009-09-02 15:27:24 -07001292 /**
1293 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001294 *
1295 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001296 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001297 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001298 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001299 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001300 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001301 }
1302
Joe Onoratoc61cff92009-11-08 11:54:39 -05001303 /**
1304 * Select the icon at the given index.
1305 *
1306 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001307 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001308 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001309 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001310 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001311 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001312 if (mLastSelection == SELECTION_ICONS) {
1313 mLastSelection = SELECTION_NONE;
1314 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001315 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001316 if (pressed == SELECTED_FOCUSED) {
1317 mLastSelection = SELECTION_ICONS;
1318 }
1319
Joe Onorato52a653f2009-11-11 14:52:11 -08001320 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001321 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001322
Joe Onorato52a653f2009-11-11 14:52:11 -08001323 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001324 Bitmap selectionBitmap = mSelectionBitmap;
1325
1326 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001327 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001328 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001329
1330 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001331 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001332 mSelectedIcon.uploadToTexture(0);
1333 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001334
1335 if (prev != index) {
1336 if (info.title != null && info.title.length() > 0) {
1337 Log.d(TAG, "sendAccessibilityEvent SELECTION_ICONS " + info.title);
1338 //setContentDescription(info.title);
1339 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1340 }
1341 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001342 }
1343 }
1344
1345 /**
1346 * You need to call save() on mState on your own after calling this.
1347 */
1348 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001349 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001350 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001351
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001352 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001353 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001354 switch (mode) {
1355 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001356 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001357 break;
1358 case SELECTED_FOCUSED:
1359 mLastSelection = SELECTION_HOME;
1360 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001361 if (prev != SELECTION_HOME) {
1362 Log.d(TAG, "sendAccessibilityEvent SELECTION_HOME");
1363 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1364 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001365 break;
1366 case SELECTED_PRESSED:
1367 mState.homeButtonId = mHomeButtonPressed.getID();
1368 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001369 }
1370 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001371 }
Joe Onorato93839052009-08-06 20:34:32 -07001372}
1373
1374