blob: 9b521b9717747d43368092ff7e8a7c55009e86aa [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;
Joe Onoratobe386092009-11-17 17:32:16 -080052import java.util.Arrays;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080053import java.util.Collections;
54import java.util.Comparator;
Joe Onorato93839052009-08-06 20:34:32 -070055
56
Joe Onorato6665c0f2009-09-02 15:27:24 -070057public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070058 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040059 private static final String TAG = "Launcher.AllAppsView";
60
Joe Onoratofb0ca672009-09-14 17:55:46 -040061 /** Bit for mLocks for when there are icons being loaded. */
62 private static final int LOCK_ICONS_PENDING = 1;
63
Joe Onorato68ffd102009-10-15 17:59:43 -070064 private static final int TRACKING_NONE = 0;
65 private static final int TRACKING_FLING = 1;
66 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070067
Joe Onoratoeb8325a2009-11-08 13:20:30 -050068 private static final int SELECTED_NONE = 0;
69 private static final int SELECTED_FOCUSED = 1;
70 private static final int SELECTED_PRESSED = 2;
71
72 private static final int SELECTION_NONE = 0;
73 private static final int SELECTION_ICONS = 1;
74 private static final int SELECTION_HOME = 2;
75
Joe Onorato6665c0f2009-09-02 15:27:24 -070076 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070077 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040078
79 /** When this is 0, modifications are allowed, when it's not, they're not.
80 * TODO: What about scrolling? */
81 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070082
Joe Onorato82ca5502009-10-15 16:59:23 -070083 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070084 private int mMaxFlingVelocity;
85
Joe Onoratobcbeab82009-10-01 21:45:43 -070086 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070087 private RenderScript mRS;
88 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070090
Mike Cleron7d5d7462009-10-20 14:06:00 -070091 /**
92 * True when we are using arrow keys or trackball to drive navigation
93 */
94 private boolean mArrowNavigation = false;
Joe Onoratoeb8325a2009-11-08 13:20:30 -050095 private boolean mStartedScrolling;
96
97 /**
98 * Used to keep track of the selection when AllAppsView loses window focus.
99 * One of the SELECTION_ constants.
100 */
101 private int mLastSelection;
Jason Sams2e19c052009-10-20 18:19:55 -0700102
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800103 /**
104 * Used to keep track of the selection when AllAppsView loses window focus
105 */
106 private int mLastSelectedIcon;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500107
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800108 private VelocityTracker mVelocityTracker;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700109 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700110 private int mMotionDownRawX;
111 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700112 private int mDownIconIndex = -1;
113 private int mCurrentIconIndex = -1;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800114
Mike Cleronb64b67a2009-11-08 14:56:25 -0800115 private boolean mShouldGainFocus;
116
Joe Onorato3a8820b2009-11-10 15:06:42 -0800117 private boolean mZoomDirty = false;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800118 private boolean mAnimateNextZoom;
119 private float mZoom;
120 private float mPosX;
121 private float mVelocity;
122 private AAMessage mMessageProc;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700123
Joe Onorato6665c0f2009-09-02 15:27:24 -0700124 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700125 public static final int ALLOC_PARAMS = 0;
126 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700127 public static final int ALLOC_ICON_IDS = 3;
128 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700129
130 public static final int COLUMNS_PER_PAGE = 4;
131 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700132
Joe Onorato6665c0f2009-09-02 15:27:24 -0700133 public static final int ICON_WIDTH_PX = 64;
134 public static final int ICON_TEXTURE_WIDTH_PX = 128;
135
136 public static final int ICON_HEIGHT_PX = 64;
137 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700138
139 public int SCREEN_WIDTH_PX;
140 public int SCREEN_HEIGHT_PX;
141
Joe Onoratobcbeab82009-10-01 21:45:43 -0700142 public void recompute(int w, int h) {
143 SCREEN_WIDTH_PX = 480;
144 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700145 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700146 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700147
148 public AllAppsView(Context context, AttributeSet attrs) {
149 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700150 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400151 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700152 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700153 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700154 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700155 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
156
Joe Onorato6665c0f2009-09-02 15:27:24 -0700157 setOnClickListener(this);
158 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700159 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700160 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samse26d9fc2009-11-12 14:00:43 -0800161
162 mRS = createRenderScript(true);
163 }
164
165 @Override
166 protected void onDetachedFromWindow() {
167 destroyRenderScript();
Joe Onorato93839052009-08-06 20:34:32 -0700168 }
169
Joe Onoratob39e51a2009-10-28 15:47:49 -0400170 /**
171 * If you have an attached click listener, View always plays the click sound!?!?
172 * Deal with sound effects by hand.
173 */
174 public void reallyPlaySoundEffect(int sound) {
175 boolean old = isSoundEffectsEnabled();
176 setSoundEffectsEnabled(true);
177 playSoundEffect(sound);
178 setSoundEffectsEnabled(old);
179 }
180
Joe Onorato7c312c12009-08-13 21:36:53 -0700181 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
182 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700183 }
184
Joe Onorato6665c0f2009-09-02 15:27:24 -0700185 public void setLauncher(Launcher launcher) {
186 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700187 }
188
Joe Onorato1feb3a82009-08-08 22:32:00 -0700189 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700190 public void surfaceDestroyed(SurfaceHolder holder) {
191 super.surfaceDestroyed(holder);
Jason Sams20df7c72009-11-05 12:30:24 -0800192 mRollo.mHasSurface = false;
Joe Onoratofab74402009-11-11 16:05:23 -0800193 // Without this, we leak mMessageCallback which leaks the context.
194 mRS.mMessageCallback = null;
Joe Onorato7bb17492009-09-24 17:51:01 -0700195 }
196
197 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700198 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800199 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700200
Joe Onorato080d9b62009-11-02 12:01:11 -0500201 super.surfaceChanged(holder, format, w, h);
202
Jason Samse26d9fc2009-11-12 14:00:43 -0800203 if (mRollo == null) {
Jason Sams90396672009-11-03 13:59:34 -0800204 mRollo = new RolloRS();
Jason Sams20df7c72009-11-05 12:30:24 -0800205 mRollo.mHasSurface = true;
Jason Sams90396672009-11-03 13:59:34 -0800206 mRollo.init(getResources(), w, h);
207 if (mAllAppsList != null) {
208 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800209 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800210 if (mShouldGainFocus) {
211 gainFocus();
212 mShouldGainFocus = false;
213 }
Jason Sams20df7c72009-11-05 12:30:24 -0800214 } else {
215 mRollo.mHasSurface = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 }
Jason Samse26d9fc2009-11-12 14:00:43 -0800217 mRollo.dirtyCheck();
Jason Sams5612e432009-11-16 14:18:07 -0800218 mRollo.resize(w, h);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700219
Joe Onoratocb75f362009-11-12 13:04:07 -0800220 mRS.mMessageCallback = mMessageProc = new AAMessage();
221
Joe Onoratoc567acb2009-08-31 14:34:43 -0700222 Resources res = getContext().getResources();
223 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700224
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800225 //long endTime = SystemClock.uptimeMillis();
226 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700227 }
Jason Sams2e19c052009-10-20 18:19:55 -0700228
Joe Onorato93839052009-08-06 20:34:32 -0700229 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800230 public void onWindowFocusChanged(boolean hasWindowFocus) {
231 super.onWindowFocusChanged(hasWindowFocus);
232 if (mArrowNavigation) {
233 if (!hasWindowFocus) {
234 // Clear selection when we lose window focus
235 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500236 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800237 mRollo.clearSelectedIcon();
238 mRollo.mState.save();
239 } else if (hasWindowFocus) {
240 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500241 if (mLastSelection == SELECTION_ICONS) {
242 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800243 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500244 Defines.COLUMNS_PER_PAGE;
245 if (selection < 0 || // No selection
246 selection < firstIcon || // off the top of the screen
247 selection >= mRollo.mState.iconCount || // past last icon
248 selection >= firstIcon + // past last icon on screen
249 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
250 selection = firstIcon;
251 }
252
253 // Select the first icon when we gain window focus
254 mRollo.selectIcon(selection, SELECTED_FOCUSED);
255 mRollo.mState.save();
256 } else if (mLastSelection == SELECTION_HOME) {
257 mRollo.setHomeSelected(SELECTED_FOCUSED);
258 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800259 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800260 }
261 }
262 }
263 }
264
265 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700266 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
267 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400268
269 if (!isVisible()) {
270 return;
271 }
272
Mike Cleron7d5d7462009-10-20 14:06:00 -0700273 if (gainFocus) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800274 if (mRollo != null) {
275 gainFocus();
276 } else {
277 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700278 }
279 } else {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800280 if (mRollo != null) {
281 if (mArrowNavigation) {
282 // Clear selection when we lose focus
283 mRollo.clearSelectedIcon();
284 mRollo.setHomeSelected(SELECTED_NONE);
285 mRollo.mState.save();
286 mArrowNavigation = false;
287 }
288 } else {
289 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700290 }
291 }
292 }
293
Mike Cleronb64b67a2009-11-08 14:56:25 -0800294 private void gainFocus() {
295 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
296 // Select the first icon when we gain keyboard focus
297 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800298 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800299 SELECTED_FOCUSED);
300 mRollo.mState.save();
301 }
302 }
303
Mike Cleron7d5d7462009-10-20 14:06:00 -0700304 @Override
305 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700306
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800307 boolean handled = false;
Jason Samse26d9fc2009-11-12 14:00:43 -0800308
Joe Onorato859b3a72009-10-28 15:17:01 -0400309 if (!isVisible()) {
310 return false;
311 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500312 final int iconCount = mRollo.mState.iconCount;
313
Mike Cleron7d5d7462009-10-20 14:06:00 -0700314 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
315 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500316 if (mLastSelection == SELECTION_HOME) {
317 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
318 mLauncher.closeAllApps(true);
319 } else {
320 int whichApp = mRollo.mState.selectedIconIndex;
321 if (whichApp >= 0) {
322 ApplicationInfo app = mAllAppsList.get(whichApp);
323 mLauncher.startActivitySafely(app.intent);
324 handled = true;
325 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700326 }
327 }
328 }
Jason Sams2e19c052009-10-20 18:19:55 -0700329
Joe Onorato478730f2009-11-16 18:54:43 -0800330 if (iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700331 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700332
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800333 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800334 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700335
Mike Cleron7d5d7462009-10-20 14:06:00 -0700336 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500337 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700338
Mike Cleron7d5d7462009-10-20 14:06:00 -0700339 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500340 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700341 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700342
Mike Cleron7d5d7462009-10-20 14:06:00 -0700343 int newSelection = currentSelection;
344
345 switch (keyCode) {
346 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500347 if (mLastSelection == SELECTION_HOME) {
348 mRollo.setHomeSelected(SELECTED_NONE);
349 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
350 if (lastRowCount == 0) {
351 lastRowCount = Defines.COLUMNS_PER_PAGE;
352 }
353 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
354 if (newSelection >= iconCount) {
355 newSelection = iconCount-1;
356 }
357 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
358 - (Defines.ROWS_PER_PAGE - 1);
359 if (target < 0) {
360 target = 0;
361 }
362 if (currentTopRow != target) {
363 mRollo.moveTo(target);
364 }
365 } else {
366 if (currentPageRow > 0) {
367 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
368 } else if (currentTopRow > 0) {
369 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
370 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
Joe Onorato478730f2009-11-16 18:54:43 -0800371 } else {
372 newSelection = Defines.COLUMNS_PER_PAGE * (Defines.ROWS_PER_PAGE-1);
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500373 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700374 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800375 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700376 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800377
Joe Onoratoa13f5742009-11-02 17:15:19 -0500378 case KeyEvent.KEYCODE_DPAD_DOWN: {
379 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
380 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
381 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500382 if (mLastSelection != SELECTION_HOME) {
383 if (currentRow < rowCount-1) {
384 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato478730f2009-11-16 18:54:43 -0800385 if (currentSelection < 0) {
386 newSelection = 0;
387 } else {
388 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
389 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500390 if (newSelection >= iconCount) {
391 // Go from D to G in this arrangement:
392 // A B C D
393 // E F G
394 newSelection = iconCount - 1;
395 }
396 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
397 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
398 Defines.ROWS_PER_PAGE + 1);
399 }
400 } else {
401 newSelection = -1;
402 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700403 }
404 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800405 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700406 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500407 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700408 case KeyEvent.KEYCODE_DPAD_LEFT:
409 if (currentPageCol > 0) {
410 newSelection = currentSelection - 1;
411 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800412 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700413 break;
414 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700415 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500416 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700417 newSelection = currentSelection + 1;
418 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800419 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700420 break;
421 }
422 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500423 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700424 mRollo.mState.save();
425 }
426 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800427 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700428 }
429
Joe Onorato93839052009-08-06 20:34:32 -0700430 @Override
431 public boolean onTouchEvent(MotionEvent ev)
432 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700433 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700434
Joe Onorato7bb17492009-09-24 17:51:01 -0700435 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700436 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700437 }
438
Joe Onoratofb0ca672009-09-14 17:55:46 -0400439 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700440 return true;
441 }
442
443 super.onTouchEvent(ev);
444
Joe Onoratofb0ca672009-09-14 17:55:46 -0400445 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700446 int y = (int)ev.getY();
447
Joe Onoratobcbeab82009-10-01 21:45:43 -0700448 int action = ev.getAction();
449 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400450 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700451 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
452 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500453 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700454 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700455 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700456 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700457 mTouchTracking = TRACKING_FLING;
458
459 mMotionDownRawX = (int)ev.getRawX();
460 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700461
Mike Cleron7d5d7462009-10-20 14:06:00 -0700462 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700463 mRollo.mState.newTouchDown = 1;
464
465 if (!mRollo.checkClickOK()) {
466 mRollo.clearSelectedIcon();
467 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700468 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800469 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700470 if (mDownIconIndex < 0) {
471 // if nothing was selected, no long press.
472 cancelLongPress();
473 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700474 }
475 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700476 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800477 mVelocityTracker = VelocityTracker.obtain();
478 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700479 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700480 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400481 break;
482 case MotionEvent.ACTION_MOVE:
483 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700484 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500485 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
486 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700487 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700488 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700489 int rawX = (int)ev.getRawX();
490 int rawY = (int)ev.getRawY();
491 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700492 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700493
Joe Onorato82ca5502009-10-15 16:59:23 -0700494 if (!mStartedScrolling && slop < mSlop) {
495 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700496 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800497 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700498 if (mDownIconIndex != mCurrentIconIndex) {
499 // If a different icon is selected, don't allow it to be picked up.
500 // This handles off-axis dragging.
501 cancelLongPress();
502 mCurrentIconIndex = -1;
503 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700504 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700505 if (!mStartedScrolling) {
506 cancelLongPress();
507 mCurrentIconIndex = -1;
508 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700509 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700510 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700511 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700512
Joe Onoratobcbeab82009-10-01 21:45:43 -0700513 mStartedScrolling = true;
514 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800515 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700516 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700517 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400518 }
519 break;
520 case MotionEvent.ACTION_UP:
521 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700522 if (mTouchTracking == TRACKING_HOME) {
523 if (action == MotionEvent.ACTION_UP) {
524 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400525 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700526 mLauncher.closeAllApps(true);
527 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500528 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700529 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700530 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700531 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700532 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700533 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700534 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700535
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800536 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
537 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700538 mRollo.clearSelectedIcon();
539 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700540 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700541
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800542 if (mVelocityTracker != null) {
543 mVelocityTracker.recycle();
544 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700545 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700546 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700547 mTouchTracking = TRACKING_NONE;
548 break;
Joe Onorato93839052009-08-06 20:34:32 -0700549 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700550
551 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700552 }
553
Joe Onorato6665c0f2009-09-02 15:27:24 -0700554 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700555 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400556 return;
557 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700558 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
559 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400560 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700561 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700562 mLauncher.startActivitySafely(app.intent);
563 }
564 }
565
566 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700567 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400568 return true;
569 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700570 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
571 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
572 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700573
574 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700575 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
576 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700577
Joe Onoratobcbeab82009-10-01 21:45:43 -0700578 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
579 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700580 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700581 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700582 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700583
Joe Onorato7bb17492009-09-24 17:51:01 -0700584 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700585 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700586 return true;
587 }
588
Joe Onorato52a653f2009-11-11 14:52:11 -0800589 @Override
590 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
591 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
592 if (!isVisible()) {
593 return false;
594 }
595 String text = null;
596 int index;
597 int count = mAllAppsList.size() + 1; // +1 is home
598 int pos = -1;
599 switch (mLastSelection) {
600 case SELECTION_ICONS:
601 index = mRollo.mState.selectedIconIndex;
602 if (index >= 0) {
603 ApplicationInfo info = mAllAppsList.get(index);
604 if (info.title != null) {
605 text = info.title.toString();
606 pos = index;
607 }
608 }
609 break;
610 case SELECTION_HOME:
611 text = getContext().getString(R.string.all_apps_home_button_label);
612 pos = count;
613 break;
614 }
615 if (text != null) {
Joe Onorato52a653f2009-11-11 14:52:11 -0800616 event.setEnabled(true);
617 event.getText().add(text);
618 //event.setContentDescription(text);
619 event.setItemCount(count);
620 event.setCurrentItemIndex(pos);
621 }
622 }
623 return false;
624 }
625
Joe Onorato5162ea92009-09-03 09:39:42 -0700626 public void setDragController(DragController dragger) {
627 mDragController = dragger;
628 }
629
630 public void onDropCompleted(View target, boolean success) {
631 }
632
Joe Onorato4db52312009-10-06 11:17:43 -0700633 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800634 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700635 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800636 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700637 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800638 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400639 cancelLongPress();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800640 if (mRollo == null || !mRollo.mHasSurface) {
641 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800642 mZoom = zoom;
643 mAnimateNextZoom = animate;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800644 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700645 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800646 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700647 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400648 }
649
650 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800651 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800652 }
653
654 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800655 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400656 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700657
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 public void setApps(ArrayList<ApplicationInfo> list) {
659 mAllAppsList = list;
660 if (mRollo != null) {
661 mRollo.setApps(list);
662 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400663 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700664 }
665
Joe Onoratoa8138d52009-10-06 19:25:30 -0700666 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500667 if (mAllAppsList == null) {
668 // Not done loading yet. We'll find out about it later.
669 return;
670 }
671
Joe Onoratoa8138d52009-10-06 19:25:30 -0700672 final int N = list.size();
673 if (mRollo != null) {
674 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
675 }
676
677 for (int i=0; i<N; i++) {
678 final ApplicationInfo item = list.get(i);
679 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
680 if (index < 0) {
681 index = -(index+1);
682 }
683 mAllAppsList.add(index, item);
684 if (mRollo != null) {
685 mRollo.addApp(index, item);
686 mRollo.mState.iconCount++;
687 }
688 }
689
690 if (mRollo != null) {
691 mRollo.saveAppsList();
692 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700693 }
694
Joe Onoratoa8138d52009-10-06 19:25:30 -0700695 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500696 if (mAllAppsList == null) {
697 // Not done loading yet. We'll find out about it later.
698 return;
699 }
700
Joe Onoratoa8138d52009-10-06 19:25:30 -0700701 final int N = list.size();
702 for (int i=0; i<N; i++) {
703 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400704 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700705 if (index >= 0) {
706 mAllAppsList.remove(index);
707 if (mRollo != null) {
708 mRollo.removeApp(index);
709 mRollo.mState.iconCount--;
710 }
711 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800712 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700713 // Try to recover. This should keep us from crashing for now.
714 }
715 }
716
717 if (mRollo != null) {
718 mRollo.saveAppsList();
719 }
720 }
721
722 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
723 // Just remove and add, because they may need to be re-sorted.
724 removeApps(list);
725 addApps(list);
726 }
727
728 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
729 public int compare(ApplicationInfo a, ApplicationInfo b) {
730 int result = a.title.toString().compareTo(b.toString());
731 if (result != 0) {
732 return result;
733 }
734 return a.intent.getComponent().compareTo(b.intent.getComponent());
735 }
736 };
737
Joe Onoratocb9f7982009-10-31 16:32:02 -0400738 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
739 ComponentName component = item.intent.getComponent();
740 final int N = list.size();
741 for (int i=0; i<N; i++) {
742 ApplicationInfo x = list.get(i);
743 if (x.intent.getComponent().equals(component)) {
744 return i;
745 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700746 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400747 return -1;
748 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700749
Joe Onoratoc567acb2009-08-31 14:34:43 -0700750 private static int countPages(int iconCount) {
751 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
752 int pages = iconCount / iconsPerPage;
753 if (pages*iconsPerPage != iconCount) {
754 pages++;
755 }
756 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757 }
758
Joe Onoratocb75f362009-11-12 13:04:07 -0800759 class AAMessage extends RenderScript.RSMessage {
760 public void run() {
761 mPosX = ((float)mData[0]) / (1 << 16);
762 mVelocity = ((float)mData[1]) / (1 << 16);
763 mZoom = ((float)mData[2]) / (1 << 16);
764 mZoomDirty = false;
765 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800766 }
767
Joe Onorato93839052009-08-06 20:34:32 -0700768 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700769
Joe Onorato1feb3a82009-08-08 22:32:00 -0700770 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700771 private int mWidth;
772 private int mHeight;
773
774 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700775 private Script mScript;
776 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700777 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700778 private Script.Invokable mInvokeFling;
779 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800780 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700781
Jason Samscd689e12009-09-29 15:28:22 -0700782 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700783 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700784 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700785 private ProgramFragment mPFTexMip;
786 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700787 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700788 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700789 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700790 private SimpleMesh mMesh2;
Jason Sams5612e432009-11-16 14:18:07 -0800791 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700792
Joe Onoratod63458b2009-10-15 21:19:09 -0700793 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500794 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700795 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700796
Joe Onoratobf15cb42009-08-07 14:33:40 -0700797 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700798 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700799 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700800
Joe Onoratobf15cb42009-08-07 14:33:40 -0700801 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700802 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700803 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700804 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700805
Joe Onorato6665c0f2009-09-02 15:27:24 -0700806 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700807 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700808
809 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400810 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700811
Jason Sams20df7c72009-11-05 12:30:24 -0800812 boolean mHasSurface = false;
813 private boolean mAppsDirty = false;
Jason Samsd8152b92009-10-13 17:19:10 -0700814
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700815 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700816 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700817
Jason Sams78aebd82009-09-15 13:06:59 -0700818 class BaseAlloc {
819 Allocation mAlloc;
820 Type mType;
821
822 void save() {
823 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700824 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700825 }
826
Jason Sams476339d2009-09-29 18:14:38 -0700827 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800828 return (Math.abs(mVelocity) < 0.4f) &&
829 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700830 }
831
Jason Sams78aebd82009-09-15 13:06:59 -0700832 class Params extends BaseAlloc {
833 Params() {
834 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
835 mAlloc = Allocation.createTyped(mRS, mType);
836 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700837 }
Jason Sams78aebd82009-09-15 13:06:59 -0700838 public int bubbleWidth;
839 public int bubbleHeight;
840 public int bubbleBitmapWidth;
841 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700842
Joe Onoratobcbeab82009-10-01 21:45:43 -0700843 public int homeButtonWidth;
844 public int homeButtonHeight;
845 public int homeButtonTextureWidth;
846 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700847 }
848
849 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700850 public float newPositionX;
851 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700852 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700853 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700854 public int selectedIconIndex = -1;
855 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700856 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700857 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700858 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700859
860 State() {
861 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
862 mAlloc = Allocation.createTyped(mRS, mType);
863 save();
864 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700865 }
866
867 public RolloRS() {
868 }
869
870 public void init(Resources res, int width, int height) {
871 mRes = res;
872 mWidth = width;
873 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700874 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700875 initProgramVertex();
876 initProgramFragment();
877 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700878 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700879 initGl();
880 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700881 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700882 initRs();
883 }
884
Jason Sams0aa71662009-10-02 18:43:18 -0700885 public void initMesh() {
886 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
887 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
888
Jason Samsd8152b92009-10-13 17:19:10 -0700889 float y = 0;
890 float z = 0;
891 for (int ct=0; ct < 200; ct++) {
892 float angle = 0;
893 float maxAngle = 3.14f * 0.16f;
894 float l = 1.f;
895
896 l = 1 - ((ct-5) * 0.10f);
897 if (ct > 7) {
898 angle = maxAngle * (ct - 7) * 0.2f;
899 angle = Math.min(angle, maxAngle);
900 }
Jason Samsc8514792009-10-29 14:27:29 -0700901 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700902 l = Math.min(1.0f, l);
903
904 y += 0.1f * Math.cos(angle);
905 z += 0.1f * Math.sin(angle);
906
907 float t = 0.1f * ct;
908 float ds = 0.08f;
909 tm.setColor(l, l, l, 0.99f);
910 tm.setTexture(ds, t);
911 tm.addVertex(-0.5f, y, z);
912 tm.setTexture(1 - ds, t);
913 tm.addVertex(0.5f, y, z);
914 }
915 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
916 tm.addTriangle(ct, ct+1, ct+2);
917 tm.addTriangle(ct+1, ct+3, ct+2);
918 }
919 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700920 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700921 }
922
Jason Sams5612e432009-11-16 14:18:07 -0800923 void resize(int w, int h) {
924 mPVA.setupProjectionNormalized(w, h);
925 mWidth = w;
926 mHeight = h;
927 }
928
Jason Samscd689e12009-09-29 15:28:22 -0700929 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800930 mPVA = new ProgramVertex.MatrixAllocation(mRS);
931 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700932
933 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700934 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700935 mPV = pvb.create();
936 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800937 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700938
Jason Sams37e7c2b2009-10-19 12:55:43 -0700939 //pva = new ProgramVertex.MatrixAllocation(mRS);
940 //pva.setupOrthoWindow(mWidth, mHeight);
941 //pvb.setTextureMatrixEnable(true);
942 //mPVOrtho = pvb.create();
943 //mPVOrtho.setName("PVOrtho");
944 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700945
946 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700947 }
Joe Onorato93839052009-08-06 20:34:32 -0700948
Jason Samscd689e12009-09-29 15:28:22 -0700949 private void initProgramFragment() {
950 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700951 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700952 sb.setMag(Sampler.Value.LINEAR);
953 sb.setWrapS(Sampler.Value.CLAMP);
954 sb.setWrapT(Sampler.Value.CLAMP);
955 Sampler linear = sb.create();
956
957 sb.setMin(Sampler.Value.NEAREST);
958 sb.setMag(Sampler.Value.NEAREST);
959 Sampler nearest = sb.create();
960
961 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700962 //mPFColor = bf.create();
963 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700964
965 bf.setTexEnable(true, 0);
966 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700967 mPFTexMip = bf.create();
968 mPFTexMip.setName("PFTexMip");
969 mPFTexMip.bindSampler(linear, 0);
970
971 mPFTexNearest = bf.create();
972 mPFTexNearest.setName("PFTexNearest");
973 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700974 }
975
976 private void initProgramStore() {
977 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
978 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700979 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700980 bs.setDitherEnable(true);
981 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
982 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
983 mPSIcons = bs.create();
984 mPSIcons.setName("PSIcons");
985
986 //bs.setDitherEnable(false);
987 //mPSText = bs.create();
988 //mPSText.setName("PSText");
989 }
990
991 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700992 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700993 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700994 }
Jason Sams78aebd82009-09-15 13:06:59 -0700995
Joe Onorato1feb3a82009-08-08 22:32:00 -0700996 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700997 mParams = new Params();
998 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700999
Joe Onoratobf15cb42009-08-07 14:33:40 -07001000 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -07001001
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001002 mParams.bubbleWidth = bubble.getBubbleWidth();
1003 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1004 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1005 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001006
Joe Onoratod63458b2009-10-15 21:19:09 -07001007 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1008 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1009 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001010 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1011 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1012 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001013 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1014 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1015 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001016 mParams.homeButtonWidth = 76;
1017 mParams.homeButtonHeight = 68;
1018 mParams.homeButtonTextureWidth = 128;
1019 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001020
Joe Onoratod63458b2009-10-15 21:19:09 -07001021 mState.homeButtonId = mHomeButtonNormal.getID();
1022
Joe Onorato1feb3a82009-08-08 22:32:00 -07001023 mParams.save();
1024 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025
Joe Onorato1291a8c2009-09-15 15:07:25 -04001026 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1027 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1028 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001029
Joe Onorato9c1289c2009-08-17 11:03:03 -04001030 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001031 }
1032
Jason Sams37e7c2b2009-10-19 12:55:43 -07001033 private void initScript(int id) {
1034 }
1035
1036 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001037 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001038 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -07001039 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001040 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001041 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1042 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001043 mInvokeMove = sb.addInvokable("move");
1044 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001045 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001046 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001047 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001048 mScript = sb.create();
1049 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1050 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1051 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1052 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1053 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001054
Jason Sams37e7c2b2009-10-19 12:55:43 -07001055 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001056 }
Joe Onorato93839052009-08-06 20:34:32 -07001057
Jason Sams20df7c72009-11-05 12:30:24 -08001058 private void uploadApps(ArrayList<ApplicationInfo> list) {
1059 for (int i=0; i < mState.iconCount; i++) {
1060 uploadAppIcon(i, list.get(i));
1061 }
1062 }
1063
1064 void dirtyCheck() {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001065 if (mHasSurface) {
1066 if (mAppsDirty) {
1067 uploadApps(mAllAppsList);
1068 saveAppsList();
1069 mAppsDirty = false;
1070 }
1071 if (mZoomDirty) {
Joe Onorato68ba5ca2009-11-12 14:23:43 -08001072 setZoom(mZoom, mAnimateNextZoom);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001073 }
Jason Sams20df7c72009-11-05 12:30:24 -08001074 }
1075 }
1076
Joe Onorato9c1289c2009-08-17 11:03:03 -04001077 private void setApps(ArrayList<ApplicationInfo> list) {
1078 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001079 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001080 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001081 allocCount = 1;
1082 }
1083
Joe Onorato9c1289c2009-08-17 11:03:03 -04001084 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001085 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001086 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087
1088 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001089 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001090 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001091
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001092 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001093
1094 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1095
Joe Onorato9c1289c2009-08-17 11:03:03 -04001096 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001097 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001098 saveAppsList();
1099 }
1100
Joe Onorato3a8820b2009-11-10 15:06:42 -08001101 private void setZoom(float zoom, boolean animate) {
1102 mRollo.clearSelectedIcon();
1103 mRollo.setHomeSelected(SELECTED_NONE);
1104 if (zoom > 0.001f) {
1105 mRollo.mState.zoomTarget = zoom;
1106 } else {
1107 mRollo.mState.zoomTarget = 0;
1108 }
1109 mRollo.mState.save();
1110 if (!animate) {
1111 mRollo.mInvokeSetZoom.execute();
1112 }
1113 }
1114
Jason Samsc8514792009-10-29 14:27:29 -07001115 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1116 int black[] = new int[w > h ? w : h];
1117 Allocation.Adapter2D a = alloc.createAdapter2D();
1118 int mip = 0;
1119 while (w > 1 || h > 1) {
1120 a.subData(0, 0, 1, h, black);
1121 a.subData(w-1, 0, 1, h, black);
1122 a.subData(0, 0, w, 1, black);
1123 a.subData(0, h-1, w, 1, black);
1124 mip++;
1125 w = (w + 1) >> 1;
1126 h = (h + 1) >> 1;
1127 a.setConstraint(Dimension.LOD, mip);
1128 }
1129 a.subData(0, 0, 1, 1, black);
1130 }
1131
Joe Onoratoa8138d52009-10-06 19:25:30 -07001132 private void uploadAppIcon(int index, ApplicationInfo item) {
1133 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001134 Element.RGBA_8888(mRS), true);
1135 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1136
Joe Onoratoa8138d52009-10-06 19:25:30 -07001137 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001138 Element.RGBA_8888(mRS), true);
1139 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001140
1141 mIcons[index].uploadToTexture(0);
1142 mLabels[index].uploadToTexture(0);
1143
1144 mIconIds[index] = mIcons[index].getID();
1145 mLabelIds[index] = mLabels[index].getID();
1146 }
1147
1148 /**
1149 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1150 * fill in the values and call saveAppsList().
1151 */
1152 private void reallocAppsList(int count) {
1153 Allocation[] icons = new Allocation[count];
1154 int[] iconIds = new int[count];
1155 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1156
1157 Allocation[] labels = new Allocation[count];
1158 int[] labelIds = new int[count];
1159 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1160
1161 final int oldCount = mIcons.length;
1162
1163 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1164 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1165 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1166 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1167
1168 mIcons = icons;
1169 mIconIds = iconIds;
1170 mLabels = labels;
1171 mLabelIds = labelIds;
1172 }
1173
1174 /**
1175 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1176 */
1177 private void addApp(int index, ApplicationInfo item) {
1178 final int count = mState.iconCount - index;
1179 final int dest = index + 1;
1180
1181 System.arraycopy(mIcons, index, mIcons, dest, count);
1182 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1183 System.arraycopy(mLabels, index, mLabels, dest, count);
1184 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1185
Jason Sams20df7c72009-11-05 12:30:24 -08001186 if (mHasSurface ) {
1187 uploadAppIcon(index, item);
1188 } else {
1189 mAppsDirty = true;
1190 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001191 }
1192
1193 /**
1194 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1195 */
1196 private void removeApp(int index) {
1197 final int count = mState.iconCount - index - 1;
1198 final int src = index + 1;
1199
1200 System.arraycopy(mIcons, src, mIcons, index, count);
1201 System.arraycopy(mIconIds, src, mIconIds, index, count);
1202 System.arraycopy(mLabels, src, mLabels, index, count);
1203 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1204
1205 final int last = mState.iconCount - 1;
1206 mIcons[last] = null;
1207 mIconIds[last] = 0;
1208 mLabels[last] = null;
1209 mLabelIds[last] = 0;
1210 }
1211
1212 /**
1213 * Send the apps list structures to RS.
1214 */
1215 private void saveAppsList() {
1216 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001217
Joe Onoratoa8138d52009-10-06 19:25:30 -07001218 mAllocIconIds.data(mIconIds);
1219 mAllocLabelIds.data(mLabelIds);
1220
Jason Sams37e7c2b2009-10-19 12:55:43 -07001221 if (mScript != null) { // this happens when we init it
1222 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1223 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001224 }
1225
1226 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001227
1228 // Note: mScript may be null if we haven't initialized it yet.
1229 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001230 if (mInvokeResetWAR != null) {
1231 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001232 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001233 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001234 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001235
1236 void initTouchState() {
1237 int width = getWidth();
1238 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001239 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1240 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001241
Jason Sams37e7c2b2009-10-19 12:55:43 -07001242 int centerY = (height / 2);
1243 mTouchYBorders[0] = centerY - (cellHeight * 2);
1244 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001245 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001246 mTouchYBorders[3] = centerY + cellHeight;
1247 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001248
Joe Onorato6665c0f2009-09-02 15:27:24 -07001249 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001250 mTouchXBorders[0] = 0;
1251 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001252 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001253 mTouchXBorders[3] = centerX + (width / 4);
1254 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001255 }
1256
Joe Onorato664457d2009-10-28 16:30:34 -04001257 void fling() {
1258 mInvokeFling.execute();
1259 }
1260
1261 void move() {
1262 mInvokeMove.execute();
1263 }
1264
1265 void moveTo(float row) {
1266 mState.targetPos = row;
1267 mState.save();
1268 mInvokeMoveTo.execute();
1269 }
1270
Jason Sams37e7c2b2009-10-19 12:55:43 -07001271 int chooseTappedIcon(int x, int y, float pos) {
1272 // Adjust for scroll position if not zero.
1273 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001274
Joe Onorato6665c0f2009-09-02 15:27:24 -07001275 int col = -1;
1276 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001277 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1278 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1279 col = i;
1280 break;
1281 }
1282 }
1283 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1284 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1285 row = i;
1286 break;
1287 }
1288 }
1289
1290 if (row < 0 || col < 0) {
1291 return -1;
1292 }
1293
Joe Onorato664457d2009-10-28 16:30:34 -04001294 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001295 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001296
Joe Onorato664457d2009-10-28 16:30:34 -04001297 if (index >= mState.iconCount) {
1298 return -1;
1299 } else {
1300 return index;
1301 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001302 }
1303
Joe Onorato6665c0f2009-09-02 15:27:24 -07001304 /**
1305 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001306 *
1307 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001308 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001309 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001310 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001311 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001312 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001313 }
1314
Joe Onoratoc61cff92009-11-08 11:54:39 -05001315 /**
1316 * Select the icon at the given index.
1317 *
1318 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001319 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001320 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001321 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001322 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001323 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001324 if (mLastSelection == SELECTION_ICONS) {
1325 mLastSelection = SELECTION_NONE;
1326 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001327 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001328 if (pressed == SELECTED_FOCUSED) {
1329 mLastSelection = SELECTION_ICONS;
1330 }
1331
Joe Onorato52a653f2009-11-11 14:52:11 -08001332 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001333 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001334
Joe Onorato52a653f2009-11-11 14:52:11 -08001335 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001336 Bitmap selectionBitmap = mSelectionBitmap;
1337
1338 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001339 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001340 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001341
1342 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001343 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001344 mSelectedIcon.uploadToTexture(0);
1345 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001346
1347 if (prev != index) {
1348 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001349 //setContentDescription(info.title);
1350 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1351 }
1352 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001353 }
1354 }
1355
1356 /**
1357 * You need to call save() on mState on your own after calling this.
1358 */
1359 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001360 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001361 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001362
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001363 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001364 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001365 switch (mode) {
1366 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001367 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001368 break;
1369 case SELECTED_FOCUSED:
1370 mLastSelection = SELECTION_HOME;
1371 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001372 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001373 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1374 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001375 break;
1376 case SELECTED_PRESSED:
1377 mState.homeButtonId = mHomeButtonPressed.getID();
1378 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001379 }
1380 }
Joe Onoratobe386092009-11-17 17:32:16 -08001381
1382 public void dumpState() {
1383 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1384 Log.d(TAG, "mRollo.mHeight=" + mHeight);
1385 Log.d(TAG, "mRollo.mIcons=" + mIcons);
1386 if (mIcons != null) {
1387 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1388 }
1389 if (mIconIds != null) {
1390 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1391 }
1392 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1393 if (mLabelIds != null) {
1394 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1395 }
1396 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1397 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1398 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
1399 Log.d(TAG, "mRollo.mHasSurface=" + mHasSurface);
1400 Log.d(TAG, "mRollo.mAppsDirty=" + mAppsDirty);
1401 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1402 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1403 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1404 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1405 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1406 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1407 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1408 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1409 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1410 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1411 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1412 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1413 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1414 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1415 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1416 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1417 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1418 }
1419 }
1420
1421 public void dumpState() {
1422 Log.d(TAG, "mRS=" + mRS);
1423 Log.d(TAG, "mRollo=" + mRollo);
1424 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1425 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1426 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1427 Log.d(TAG, "mLastSelection=" + mLastSelection);
1428 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1429 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1430 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1431 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1432 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1433 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1434 Log.d(TAG, "mZoom=" + mZoom);
1435 Log.d(TAG, "mPosX=" + mPosX);
1436 Log.d(TAG, "mVelocity=" + mVelocity);
1437 Log.d(TAG, "mMessageProc=" + mMessageProc);
1438 if (mRollo != null) {
1439 mRollo.dumpState();
1440 }
1441 if (mRS != null) {
1442 mRS.contextDump(0);
1443 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001444 }
Joe Onorato93839052009-08-06 20:34:32 -07001445}
1446
1447