blob: 687d41668a681e0a8d2b948e4c22931fd5c41ba3 [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 Onorato478730f2009-11-16 18:54:43 -0800329 if (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);
Joe Onorato478730f2009-11-16 18:54:43 -0800370 } else {
371 newSelection = Defines.COLUMNS_PER_PAGE * (Defines.ROWS_PER_PAGE-1);
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500372 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700373 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800374 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700375 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800376
Joe Onoratoa13f5742009-11-02 17:15:19 -0500377 case KeyEvent.KEYCODE_DPAD_DOWN: {
378 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
379 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
380 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500381 if (mLastSelection != SELECTION_HOME) {
382 if (currentRow < rowCount-1) {
383 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato478730f2009-11-16 18:54:43 -0800384 if (currentSelection < 0) {
385 newSelection = 0;
386 } else {
387 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
388 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500389 if (newSelection >= iconCount) {
390 // Go from D to G in this arrangement:
391 // A B C D
392 // E F G
393 newSelection = iconCount - 1;
394 }
395 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
396 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
397 Defines.ROWS_PER_PAGE + 1);
398 }
399 } else {
400 newSelection = -1;
401 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700402 }
403 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800404 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700405 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500406 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700407 case KeyEvent.KEYCODE_DPAD_LEFT:
408 if (currentPageCol > 0) {
409 newSelection = currentSelection - 1;
410 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800411 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700412 break;
413 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700414 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500415 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700416 newSelection = currentSelection + 1;
417 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800418 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700419 break;
420 }
421 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500422 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700423 mRollo.mState.save();
424 }
425 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800426 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700427 }
428
Joe Onorato93839052009-08-06 20:34:32 -0700429 @Override
430 public boolean onTouchEvent(MotionEvent ev)
431 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700432 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700433
Joe Onorato7bb17492009-09-24 17:51:01 -0700434 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700435 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700436 }
437
Joe Onoratofb0ca672009-09-14 17:55:46 -0400438 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700439 return true;
440 }
441
442 super.onTouchEvent(ev);
443
Joe Onoratofb0ca672009-09-14 17:55:46 -0400444 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700445 int y = (int)ev.getY();
446
Joe Onoratobcbeab82009-10-01 21:45:43 -0700447 int action = ev.getAction();
448 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400449 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700450 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
451 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500452 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700453 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700454 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700455 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700456 mTouchTracking = TRACKING_FLING;
457
458 mMotionDownRawX = (int)ev.getRawX();
459 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700460
Mike Cleron7d5d7462009-10-20 14:06:00 -0700461 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700462 mRollo.mState.newTouchDown = 1;
463
464 if (!mRollo.checkClickOK()) {
465 mRollo.clearSelectedIcon();
466 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700467 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800468 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700469 if (mDownIconIndex < 0) {
470 // if nothing was selected, no long press.
471 cancelLongPress();
472 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700473 }
474 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700475 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800476 mVelocityTracker = VelocityTracker.obtain();
477 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700478 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700479 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400480 break;
481 case MotionEvent.ACTION_MOVE:
482 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700483 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500484 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
485 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700486 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700487 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700488 int rawX = (int)ev.getRawX();
489 int rawY = (int)ev.getRawY();
490 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700491 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700492
Joe Onorato82ca5502009-10-15 16:59:23 -0700493 if (!mStartedScrolling && slop < mSlop) {
494 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700495 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800496 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700497 if (mDownIconIndex != mCurrentIconIndex) {
498 // If a different icon is selected, don't allow it to be picked up.
499 // This handles off-axis dragging.
500 cancelLongPress();
501 mCurrentIconIndex = -1;
502 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700503 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700504 if (!mStartedScrolling) {
505 cancelLongPress();
506 mCurrentIconIndex = -1;
507 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700508 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700509 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700510 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700511
Joe Onoratobcbeab82009-10-01 21:45:43 -0700512 mStartedScrolling = true;
513 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800514 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700515 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700516 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400517 }
518 break;
519 case MotionEvent.ACTION_UP:
520 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700521 if (mTouchTracking == TRACKING_HOME) {
522 if (action == MotionEvent.ACTION_UP) {
523 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400524 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700525 mLauncher.closeAllApps(true);
526 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500527 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700528 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700529 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700530 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700531 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700532 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700533 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700534
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800535 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
536 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700537 mRollo.clearSelectedIcon();
538 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700539 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700540
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800541 if (mVelocityTracker != null) {
542 mVelocityTracker.recycle();
543 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700544 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700545 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700546 mTouchTracking = TRACKING_NONE;
547 break;
Joe Onorato93839052009-08-06 20:34:32 -0700548 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700549
550 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700551 }
552
Joe Onorato6665c0f2009-09-02 15:27:24 -0700553 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700554 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400555 return;
556 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700557 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
558 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400559 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700560 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700561 mLauncher.startActivitySafely(app.intent);
562 }
563 }
564
565 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700566 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400567 return true;
568 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700569 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
570 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
571 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700572
573 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700574 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
575 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700576
Joe Onoratobcbeab82009-10-01 21:45:43 -0700577 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
578 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700579 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700580 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700581 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700582
Joe Onorato7bb17492009-09-24 17:51:01 -0700583 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700584 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700585 return true;
586 }
587
Joe Onorato52a653f2009-11-11 14:52:11 -0800588 @Override
589 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
590 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
591 if (!isVisible()) {
592 return false;
593 }
594 String text = null;
595 int index;
596 int count = mAllAppsList.size() + 1; // +1 is home
597 int pos = -1;
598 switch (mLastSelection) {
599 case SELECTION_ICONS:
600 index = mRollo.mState.selectedIconIndex;
601 if (index >= 0) {
602 ApplicationInfo info = mAllAppsList.get(index);
603 if (info.title != null) {
604 text = info.title.toString();
605 pos = index;
606 }
607 }
608 break;
609 case SELECTION_HOME:
610 text = getContext().getString(R.string.all_apps_home_button_label);
611 pos = count;
612 break;
613 }
614 if (text != null) {
Joe Onorato52a653f2009-11-11 14:52:11 -0800615 event.setEnabled(true);
616 event.getText().add(text);
617 //event.setContentDescription(text);
618 event.setItemCount(count);
619 event.setCurrentItemIndex(pos);
620 }
621 }
622 return false;
623 }
624
Joe Onorato5162ea92009-09-03 09:39:42 -0700625 public void setDragController(DragController dragger) {
626 mDragController = dragger;
627 }
628
629 public void onDropCompleted(View target, boolean success) {
630 }
631
Joe Onorato4db52312009-10-06 11:17:43 -0700632 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800633 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700634 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800635 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700636 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800637 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400638 cancelLongPress();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800639 if (mRollo == null || !mRollo.mHasSurface) {
640 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800641 mZoom = zoom;
642 mAnimateNextZoom = animate;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800643 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700644 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800645 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700646 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400647 }
648
649 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800650 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800651 }
652
653 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800654 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400655 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700656
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 public void setApps(ArrayList<ApplicationInfo> list) {
658 mAllAppsList = list;
659 if (mRollo != null) {
660 mRollo.setApps(list);
661 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400662 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700663 }
664
Joe Onoratoa8138d52009-10-06 19:25:30 -0700665 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500666 if (mAllAppsList == null) {
667 // Not done loading yet. We'll find out about it later.
668 return;
669 }
670
Joe Onoratoa8138d52009-10-06 19:25:30 -0700671 final int N = list.size();
672 if (mRollo != null) {
673 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
674 }
675
676 for (int i=0; i<N; i++) {
677 final ApplicationInfo item = list.get(i);
678 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
679 if (index < 0) {
680 index = -(index+1);
681 }
682 mAllAppsList.add(index, item);
683 if (mRollo != null) {
684 mRollo.addApp(index, item);
685 mRollo.mState.iconCount++;
686 }
687 }
688
689 if (mRollo != null) {
690 mRollo.saveAppsList();
691 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700692 }
693
Joe Onoratoa8138d52009-10-06 19:25:30 -0700694 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500695 if (mAllAppsList == null) {
696 // Not done loading yet. We'll find out about it later.
697 return;
698 }
699
Joe Onoratoa8138d52009-10-06 19:25:30 -0700700 final int N = list.size();
701 for (int i=0; i<N; i++) {
702 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400703 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700704 if (index >= 0) {
705 mAllAppsList.remove(index);
706 if (mRollo != null) {
707 mRollo.removeApp(index);
708 mRollo.mState.iconCount--;
709 }
710 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800711 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700712 // Try to recover. This should keep us from crashing for now.
713 }
714 }
715
716 if (mRollo != null) {
717 mRollo.saveAppsList();
718 }
719 }
720
721 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
722 // Just remove and add, because they may need to be re-sorted.
723 removeApps(list);
724 addApps(list);
725 }
726
727 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
728 public int compare(ApplicationInfo a, ApplicationInfo b) {
729 int result = a.title.toString().compareTo(b.toString());
730 if (result != 0) {
731 return result;
732 }
733 return a.intent.getComponent().compareTo(b.intent.getComponent());
734 }
735 };
736
Joe Onoratocb9f7982009-10-31 16:32:02 -0400737 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
738 ComponentName component = item.intent.getComponent();
739 final int N = list.size();
740 for (int i=0; i<N; i++) {
741 ApplicationInfo x = list.get(i);
742 if (x.intent.getComponent().equals(component)) {
743 return i;
744 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700745 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400746 return -1;
747 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700748
Joe Onoratoc567acb2009-08-31 14:34:43 -0700749 private static int countPages(int iconCount) {
750 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
751 int pages = iconCount / iconsPerPage;
752 if (pages*iconsPerPage != iconCount) {
753 pages++;
754 }
755 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400756 }
757
Joe Onoratocb75f362009-11-12 13:04:07 -0800758 class AAMessage extends RenderScript.RSMessage {
759 public void run() {
760 mPosX = ((float)mData[0]) / (1 << 16);
761 mVelocity = ((float)mData[1]) / (1 << 16);
762 mZoom = ((float)mData[2]) / (1 << 16);
763 mZoomDirty = false;
764 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800765 }
766
Joe Onorato93839052009-08-06 20:34:32 -0700767 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700768
Joe Onorato1feb3a82009-08-08 22:32:00 -0700769 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700770 private int mWidth;
771 private int mHeight;
772
773 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700774 private Script mScript;
775 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700776 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700777 private Script.Invokable mInvokeFling;
778 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800779 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700780
Jason Samscd689e12009-09-29 15:28:22 -0700781 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700782 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700783 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700784 private ProgramFragment mPFTexMip;
785 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700786 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700787 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700788 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700789 private SimpleMesh mMesh2;
Jason Sams5612e432009-11-16 14:18:07 -0800790 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700791
Joe Onoratod63458b2009-10-15 21:19:09 -0700792 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500793 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700794 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700795
Joe Onoratobf15cb42009-08-07 14:33:40 -0700796 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700797 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700798 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700799
Joe Onoratobf15cb42009-08-07 14:33:40 -0700800 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700801 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700802 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700803 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700804
Joe Onorato6665c0f2009-09-02 15:27:24 -0700805 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700806 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700807
808 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400809 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700810
Jason Sams20df7c72009-11-05 12:30:24 -0800811 boolean mHasSurface = false;
812 private boolean mAppsDirty = false;
Jason Samsd8152b92009-10-13 17:19:10 -0700813
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700814 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700815 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700816
Jason Sams78aebd82009-09-15 13:06:59 -0700817 class BaseAlloc {
818 Allocation mAlloc;
819 Type mType;
820
821 void save() {
822 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700823 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700824 }
825
Jason Sams476339d2009-09-29 18:14:38 -0700826 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800827 return (Math.abs(mVelocity) < 0.4f) &&
828 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700829 }
830
Jason Sams78aebd82009-09-15 13:06:59 -0700831 class Params extends BaseAlloc {
832 Params() {
833 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
834 mAlloc = Allocation.createTyped(mRS, mType);
835 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700836 }
Jason Sams78aebd82009-09-15 13:06:59 -0700837 public int bubbleWidth;
838 public int bubbleHeight;
839 public int bubbleBitmapWidth;
840 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700841
Joe Onoratobcbeab82009-10-01 21:45:43 -0700842 public int homeButtonWidth;
843 public int homeButtonHeight;
844 public int homeButtonTextureWidth;
845 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700846 }
847
848 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700849 public float newPositionX;
850 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700851 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700852 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700853 public int selectedIconIndex = -1;
854 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700855 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700856 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700857 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700858
859 State() {
860 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
861 mAlloc = Allocation.createTyped(mRS, mType);
862 save();
863 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700864 }
865
866 public RolloRS() {
867 }
868
869 public void init(Resources res, int width, int height) {
870 mRes = res;
871 mWidth = width;
872 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700873 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700874 initProgramVertex();
875 initProgramFragment();
876 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700877 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700878 initGl();
879 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700880 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700881 initRs();
882 }
883
Jason Sams0aa71662009-10-02 18:43:18 -0700884 public void initMesh() {
885 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
886 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
887
Jason Samsd8152b92009-10-13 17:19:10 -0700888 float y = 0;
889 float z = 0;
890 for (int ct=0; ct < 200; ct++) {
891 float angle = 0;
892 float maxAngle = 3.14f * 0.16f;
893 float l = 1.f;
894
895 l = 1 - ((ct-5) * 0.10f);
896 if (ct > 7) {
897 angle = maxAngle * (ct - 7) * 0.2f;
898 angle = Math.min(angle, maxAngle);
899 }
Jason Samsc8514792009-10-29 14:27:29 -0700900 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700901 l = Math.min(1.0f, l);
902
903 y += 0.1f * Math.cos(angle);
904 z += 0.1f * Math.sin(angle);
905
906 float t = 0.1f * ct;
907 float ds = 0.08f;
908 tm.setColor(l, l, l, 0.99f);
909 tm.setTexture(ds, t);
910 tm.addVertex(-0.5f, y, z);
911 tm.setTexture(1 - ds, t);
912 tm.addVertex(0.5f, y, z);
913 }
914 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
915 tm.addTriangle(ct, ct+1, ct+2);
916 tm.addTriangle(ct+1, ct+3, ct+2);
917 }
918 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700919 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700920 }
921
Jason Sams5612e432009-11-16 14:18:07 -0800922 void resize(int w, int h) {
923 mPVA.setupProjectionNormalized(w, h);
924 mWidth = w;
925 mHeight = h;
926 }
927
Jason Samscd689e12009-09-29 15:28:22 -0700928 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800929 mPVA = new ProgramVertex.MatrixAllocation(mRS);
930 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700931
932 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700933 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700934 mPV = pvb.create();
935 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800936 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700937
Jason Sams37e7c2b2009-10-19 12:55:43 -0700938 //pva = new ProgramVertex.MatrixAllocation(mRS);
939 //pva.setupOrthoWindow(mWidth, mHeight);
940 //pvb.setTextureMatrixEnable(true);
941 //mPVOrtho = pvb.create();
942 //mPVOrtho.setName("PVOrtho");
943 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700944
945 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700946 }
Joe Onorato93839052009-08-06 20:34:32 -0700947
Jason Samscd689e12009-09-29 15:28:22 -0700948 private void initProgramFragment() {
949 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700950 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700951 sb.setMag(Sampler.Value.LINEAR);
952 sb.setWrapS(Sampler.Value.CLAMP);
953 sb.setWrapT(Sampler.Value.CLAMP);
954 Sampler linear = sb.create();
955
956 sb.setMin(Sampler.Value.NEAREST);
957 sb.setMag(Sampler.Value.NEAREST);
958 Sampler nearest = sb.create();
959
960 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700961 //mPFColor = bf.create();
962 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700963
964 bf.setTexEnable(true, 0);
965 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700966 mPFTexMip = bf.create();
967 mPFTexMip.setName("PFTexMip");
968 mPFTexMip.bindSampler(linear, 0);
969
970 mPFTexNearest = bf.create();
971 mPFTexNearest.setName("PFTexNearest");
972 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700973 }
974
975 private void initProgramStore() {
976 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
977 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700978 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700979 bs.setDitherEnable(true);
980 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
981 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
982 mPSIcons = bs.create();
983 mPSIcons.setName("PSIcons");
984
985 //bs.setDitherEnable(false);
986 //mPSText = bs.create();
987 //mPSText.setName("PSText");
988 }
989
990 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700991 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700992 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700993 }
Jason Sams78aebd82009-09-15 13:06:59 -0700994
Joe Onorato1feb3a82009-08-08 22:32:00 -0700995 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700996 mParams = new Params();
997 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700998
Joe Onoratobf15cb42009-08-07 14:33:40 -0700999 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -07001000
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001001 mParams.bubbleWidth = bubble.getBubbleWidth();
1002 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1003 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1004 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001005
Joe Onoratod63458b2009-10-15 21:19:09 -07001006 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1007 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1008 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001009 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1010 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1011 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001012 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1013 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1014 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001015 mParams.homeButtonWidth = 76;
1016 mParams.homeButtonHeight = 68;
1017 mParams.homeButtonTextureWidth = 128;
1018 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001019
Joe Onoratod63458b2009-10-15 21:19:09 -07001020 mState.homeButtonId = mHomeButtonNormal.getID();
1021
Joe Onorato1feb3a82009-08-08 22:32:00 -07001022 mParams.save();
1023 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024
Joe Onorato1291a8c2009-09-15 15:07:25 -04001025 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1026 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1027 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001028
Joe Onorato9c1289c2009-08-17 11:03:03 -04001029 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001030 }
1031
Jason Sams37e7c2b2009-10-19 12:55:43 -07001032 private void initScript(int id) {
1033 }
1034
1035 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001036 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001037 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -07001038 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001039 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001040 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1041 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001042 mInvokeMove = sb.addInvokable("move");
1043 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001044 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001045 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001046 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001047 mScript = sb.create();
1048 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1049 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1050 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1051 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1052 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001053
Jason Sams37e7c2b2009-10-19 12:55:43 -07001054 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001055 }
Joe Onorato93839052009-08-06 20:34:32 -07001056
Jason Sams20df7c72009-11-05 12:30:24 -08001057 private void uploadApps(ArrayList<ApplicationInfo> list) {
1058 for (int i=0; i < mState.iconCount; i++) {
1059 uploadAppIcon(i, list.get(i));
1060 }
1061 }
1062
1063 void dirtyCheck() {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001064 if (mHasSurface) {
1065 if (mAppsDirty) {
1066 uploadApps(mAllAppsList);
1067 saveAppsList();
1068 mAppsDirty = false;
1069 }
1070 if (mZoomDirty) {
Joe Onorato68ba5ca2009-11-12 14:23:43 -08001071 setZoom(mZoom, mAnimateNextZoom);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001072 }
Jason Sams20df7c72009-11-05 12:30:24 -08001073 }
1074 }
1075
Joe Onorato9c1289c2009-08-17 11:03:03 -04001076 private void setApps(ArrayList<ApplicationInfo> list) {
1077 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001078 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001079 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001080 allocCount = 1;
1081 }
1082
Joe Onorato9c1289c2009-08-17 11:03:03 -04001083 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001084 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001085 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001086
1087 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001088 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001089 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001091 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001092
1093 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1094
Joe Onorato9c1289c2009-08-17 11:03:03 -04001095 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001096 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001097 saveAppsList();
1098 }
1099
Joe Onorato3a8820b2009-11-10 15:06:42 -08001100 private void setZoom(float zoom, boolean animate) {
1101 mRollo.clearSelectedIcon();
1102 mRollo.setHomeSelected(SELECTED_NONE);
1103 if (zoom > 0.001f) {
1104 mRollo.mState.zoomTarget = zoom;
1105 } else {
1106 mRollo.mState.zoomTarget = 0;
1107 }
1108 mRollo.mState.save();
1109 if (!animate) {
1110 mRollo.mInvokeSetZoom.execute();
1111 }
1112 }
1113
Jason Samsc8514792009-10-29 14:27:29 -07001114 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1115 int black[] = new int[w > h ? w : h];
1116 Allocation.Adapter2D a = alloc.createAdapter2D();
1117 int mip = 0;
1118 while (w > 1 || h > 1) {
1119 a.subData(0, 0, 1, h, black);
1120 a.subData(w-1, 0, 1, h, black);
1121 a.subData(0, 0, w, 1, black);
1122 a.subData(0, h-1, w, 1, black);
1123 mip++;
1124 w = (w + 1) >> 1;
1125 h = (h + 1) >> 1;
1126 a.setConstraint(Dimension.LOD, mip);
1127 }
1128 a.subData(0, 0, 1, 1, black);
1129 }
1130
Joe Onoratoa8138d52009-10-06 19:25:30 -07001131 private void uploadAppIcon(int index, ApplicationInfo item) {
1132 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001133 Element.RGBA_8888(mRS), true);
1134 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1135
Joe Onoratoa8138d52009-10-06 19:25:30 -07001136 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001137 Element.RGBA_8888(mRS), true);
1138 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001139
1140 mIcons[index].uploadToTexture(0);
1141 mLabels[index].uploadToTexture(0);
1142
1143 mIconIds[index] = mIcons[index].getID();
1144 mLabelIds[index] = mLabels[index].getID();
1145 }
1146
1147 /**
1148 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1149 * fill in the values and call saveAppsList().
1150 */
1151 private void reallocAppsList(int count) {
1152 Allocation[] icons = new Allocation[count];
1153 int[] iconIds = new int[count];
1154 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1155
1156 Allocation[] labels = new Allocation[count];
1157 int[] labelIds = new int[count];
1158 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1159
1160 final int oldCount = mIcons.length;
1161
1162 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1163 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1164 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1165 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1166
1167 mIcons = icons;
1168 mIconIds = iconIds;
1169 mLabels = labels;
1170 mLabelIds = labelIds;
1171 }
1172
1173 /**
1174 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1175 */
1176 private void addApp(int index, ApplicationInfo item) {
1177 final int count = mState.iconCount - index;
1178 final int dest = index + 1;
1179
1180 System.arraycopy(mIcons, index, mIcons, dest, count);
1181 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1182 System.arraycopy(mLabels, index, mLabels, dest, count);
1183 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1184
Jason Sams20df7c72009-11-05 12:30:24 -08001185 if (mHasSurface ) {
1186 uploadAppIcon(index, item);
1187 } else {
1188 mAppsDirty = true;
1189 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001190 }
1191
1192 /**
1193 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1194 */
1195 private void removeApp(int index) {
1196 final int count = mState.iconCount - index - 1;
1197 final int src = index + 1;
1198
1199 System.arraycopy(mIcons, src, mIcons, index, count);
1200 System.arraycopy(mIconIds, src, mIconIds, index, count);
1201 System.arraycopy(mLabels, src, mLabels, index, count);
1202 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1203
1204 final int last = mState.iconCount - 1;
1205 mIcons[last] = null;
1206 mIconIds[last] = 0;
1207 mLabels[last] = null;
1208 mLabelIds[last] = 0;
1209 }
1210
1211 /**
1212 * Send the apps list structures to RS.
1213 */
1214 private void saveAppsList() {
1215 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001216
Joe Onoratoa8138d52009-10-06 19:25:30 -07001217 mAllocIconIds.data(mIconIds);
1218 mAllocLabelIds.data(mLabelIds);
1219
Jason Sams37e7c2b2009-10-19 12:55:43 -07001220 if (mScript != null) { // this happens when we init it
1221 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1222 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001223 }
1224
1225 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001226
1227 // Note: mScript may be null if we haven't initialized it yet.
1228 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001229 if (mInvokeResetWAR != null) {
1230 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001231 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001232 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001233 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001234
1235 void initTouchState() {
1236 int width = getWidth();
1237 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001238 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1239 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001240
Jason Sams37e7c2b2009-10-19 12:55:43 -07001241 int centerY = (height / 2);
1242 mTouchYBorders[0] = centerY - (cellHeight * 2);
1243 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001244 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001245 mTouchYBorders[3] = centerY + cellHeight;
1246 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001247
Joe Onorato6665c0f2009-09-02 15:27:24 -07001248 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001249 mTouchXBorders[0] = 0;
1250 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001251 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001252 mTouchXBorders[3] = centerX + (width / 4);
1253 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001254 }
1255
Joe Onorato664457d2009-10-28 16:30:34 -04001256 void fling() {
1257 mInvokeFling.execute();
1258 }
1259
1260 void move() {
1261 mInvokeMove.execute();
1262 }
1263
1264 void moveTo(float row) {
1265 mState.targetPos = row;
1266 mState.save();
1267 mInvokeMoveTo.execute();
1268 }
1269
Jason Sams37e7c2b2009-10-19 12:55:43 -07001270 int chooseTappedIcon(int x, int y, float pos) {
1271 // Adjust for scroll position if not zero.
1272 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001273
Joe Onorato6665c0f2009-09-02 15:27:24 -07001274 int col = -1;
1275 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001276 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1277 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1278 col = i;
1279 break;
1280 }
1281 }
1282 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1283 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1284 row = i;
1285 break;
1286 }
1287 }
1288
1289 if (row < 0 || col < 0) {
1290 return -1;
1291 }
1292
Joe Onorato664457d2009-10-28 16:30:34 -04001293 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001294 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001295
Joe Onorato664457d2009-10-28 16:30:34 -04001296 if (index >= mState.iconCount) {
1297 return -1;
1298 } else {
1299 return index;
1300 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001301 }
1302
Joe Onorato6665c0f2009-09-02 15:27:24 -07001303 /**
1304 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001305 *
1306 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001307 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001308 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001309 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001310 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001311 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001312 }
1313
Joe Onoratoc61cff92009-11-08 11:54:39 -05001314 /**
1315 * Select the icon at the given index.
1316 *
1317 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001318 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001319 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001320 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001321 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001322 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001323 if (mLastSelection == SELECTION_ICONS) {
1324 mLastSelection = SELECTION_NONE;
1325 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001326 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001327 if (pressed == SELECTED_FOCUSED) {
1328 mLastSelection = SELECTION_ICONS;
1329 }
1330
Joe Onorato52a653f2009-11-11 14:52:11 -08001331 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001332 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001333
Joe Onorato52a653f2009-11-11 14:52:11 -08001334 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001335 Bitmap selectionBitmap = mSelectionBitmap;
1336
1337 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001338 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001339 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001340
1341 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001342 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001343 mSelectedIcon.uploadToTexture(0);
1344 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001345
1346 if (prev != index) {
1347 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001348 //setContentDescription(info.title);
1349 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1350 }
1351 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001352 }
1353 }
1354
1355 /**
1356 * You need to call save() on mState on your own after calling this.
1357 */
1358 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001359 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001360 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001361
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001362 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001363 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001364 switch (mode) {
1365 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001366 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001367 break;
1368 case SELECTED_FOCUSED:
1369 mLastSelection = SELECTION_HOME;
1370 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001371 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001372 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1373 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001374 break;
1375 case SELECTED_PRESSED:
1376 mState.homeButtonId = mHomeButtonPressed.getID();
1377 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001378 }
1379 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380 }
Joe Onorato93839052009-08-06 20:34:32 -07001381}
1382
1383