blob: 8d24ea34a09ed173d63d424a86ca97c6ca4ec44f [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) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800274 if (mRollo != null && mRollo.mHasSurface) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800275 gainFocus();
276 } else {
277 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700278 }
279 } else {
Joe Onorato8eea3912009-12-09 13:01:06 -0800280 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800281 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 Onoratoaf5b4cb2009-12-08 17:51:22 -0800371 } else if (currentPageRow != 0) {
372 newSelection = currentTopRow * Defines.ROWS_PER_PAGE;
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) {
Joe Onorato3ecbd812009-12-11 13:38:54 -0800661 mRollo.setApps(list);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400662 }
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();
Joe Onorato8eea3912009-12-09 13:01:06 -0800673 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700674 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
675 }
676
677 for (int i=0; i<N; i++) {
678 final ApplicationInfo item = list.get(i);
Joe Onoratob0c27f22009-12-01 16:19:38 -0800679 int index = Collections.binarySearch(mAllAppsList, item,
680 LauncherModel.APP_NAME_COMPARATOR);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700681 if (index < 0) {
682 index = -(index+1);
683 }
684 mAllAppsList.add(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -0800685 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700686 mRollo.addApp(index, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700687 }
688 }
689
Joe Onorato8eea3912009-12-09 13:01:06 -0800690 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700691 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) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800706 int ic = mRollo != null ? mRollo.mState.iconCount : 666;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700707 mAllAppsList.remove(index);
Joe Onorato8eea3912009-12-09 13:01:06 -0800708 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700709 mRollo.removeApp(index);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700710 }
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
Joe Onorato8eea3912009-12-09 13:01:06 -0800717 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700718 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
Joe Onoratocb9f7982009-10-31 16:32:02 -0400728 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
729 ComponentName component = item.intent.getComponent();
730 final int N = list.size();
731 for (int i=0; i<N; i++) {
732 ApplicationInfo x = list.get(i);
733 if (x.intent.getComponent().equals(component)) {
734 return i;
735 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700736 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400737 return -1;
738 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700739
Joe Onoratoc567acb2009-08-31 14:34:43 -0700740 private static int countPages(int iconCount) {
741 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
742 int pages = iconCount / iconsPerPage;
743 if (pages*iconsPerPage != iconCount) {
744 pages++;
745 }
746 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 }
748
Joe Onoratocb75f362009-11-12 13:04:07 -0800749 class AAMessage extends RenderScript.RSMessage {
750 public void run() {
751 mPosX = ((float)mData[0]) / (1 << 16);
752 mVelocity = ((float)mData[1]) / (1 << 16);
753 mZoom = ((float)mData[2]) / (1 << 16);
754 mZoomDirty = false;
755 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800756 }
757
Joe Onorato93839052009-08-06 20:34:32 -0700758 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700759
Joe Onorato1feb3a82009-08-08 22:32:00 -0700760 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700761 private int mWidth;
762 private int mHeight;
763
764 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700765 private Script mScript;
766 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700767 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700768 private Script.Invokable mInvokeFling;
769 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800770 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700771
Jason Samscd689e12009-09-29 15:28:22 -0700772 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700773 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700774 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700775 private ProgramFragment mPFTexMip;
776 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700777 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700778 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700779 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700780 private SimpleMesh mMesh2;
Jason Sams5612e432009-11-16 14:18:07 -0800781 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700782
Joe Onoratod63458b2009-10-15 21:19:09 -0700783 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500784 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700785 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700786
Joe Onoratobf15cb42009-08-07 14:33:40 -0700787 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700788 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700789 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700790
Joe Onoratobf15cb42009-08-07 14:33:40 -0700791 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700792 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700793 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700794 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700795
Joe Onorato6665c0f2009-09-02 15:27:24 -0700796 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700797 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700798
799 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400800 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700801
Jason Sams20df7c72009-11-05 12:30:24 -0800802 boolean mHasSurface = false;
Joe Onorato8cf34292009-12-11 10:59:51 -0800803 private boolean mAppsDirty = true;
Jason Samsd8152b92009-10-13 17:19:10 -0700804
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700805 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700806 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700807
Jason Sams78aebd82009-09-15 13:06:59 -0700808 class BaseAlloc {
809 Allocation mAlloc;
810 Type mType;
811
812 void save() {
813 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700814 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700815 }
816
Jason Sams476339d2009-09-29 18:14:38 -0700817 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800818 return (Math.abs(mVelocity) < 0.4f) &&
819 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700820 }
821
Jason Sams78aebd82009-09-15 13:06:59 -0700822 class Params extends BaseAlloc {
823 Params() {
824 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
825 mAlloc = Allocation.createTyped(mRS, mType);
826 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700827 }
Jason Sams78aebd82009-09-15 13:06:59 -0700828 public int bubbleWidth;
829 public int bubbleHeight;
830 public int bubbleBitmapWidth;
831 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700832
Joe Onoratobcbeab82009-10-01 21:45:43 -0700833 public int homeButtonWidth;
834 public int homeButtonHeight;
835 public int homeButtonTextureWidth;
836 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700837 }
838
839 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700840 public float newPositionX;
841 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700842 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700843 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700844 public int selectedIconIndex = -1;
845 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700846 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700847 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700848 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700849
850 State() {
851 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
852 mAlloc = Allocation.createTyped(mRS, mType);
853 save();
854 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700855 }
856
857 public RolloRS() {
858 }
859
860 public void init(Resources res, int width, int height) {
861 mRes = res;
862 mWidth = width;
863 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700864 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700865 initProgramVertex();
866 initProgramFragment();
867 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700868 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700869 initGl();
870 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700871 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700872 initRs();
873 }
874
Jason Sams0aa71662009-10-02 18:43:18 -0700875 public void initMesh() {
876 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
877 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
878
Jason Samsd8152b92009-10-13 17:19:10 -0700879 float y = 0;
880 float z = 0;
881 for (int ct=0; ct < 200; ct++) {
882 float angle = 0;
883 float maxAngle = 3.14f * 0.16f;
884 float l = 1.f;
885
886 l = 1 - ((ct-5) * 0.10f);
887 if (ct > 7) {
888 angle = maxAngle * (ct - 7) * 0.2f;
889 angle = Math.min(angle, maxAngle);
890 }
Jason Samsc8514792009-10-29 14:27:29 -0700891 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700892 l = Math.min(1.0f, l);
893
894 y += 0.1f * Math.cos(angle);
895 z += 0.1f * Math.sin(angle);
896
897 float t = 0.1f * ct;
898 float ds = 0.08f;
899 tm.setColor(l, l, l, 0.99f);
900 tm.setTexture(ds, t);
901 tm.addVertex(-0.5f, y, z);
902 tm.setTexture(1 - ds, t);
903 tm.addVertex(0.5f, y, z);
904 }
905 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
906 tm.addTriangle(ct, ct+1, ct+2);
907 tm.addTriangle(ct+1, ct+3, ct+2);
908 }
909 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700910 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700911 }
912
Jason Sams5612e432009-11-16 14:18:07 -0800913 void resize(int w, int h) {
914 mPVA.setupProjectionNormalized(w, h);
915 mWidth = w;
916 mHeight = h;
917 }
918
Jason Samscd689e12009-09-29 15:28:22 -0700919 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800920 mPVA = new ProgramVertex.MatrixAllocation(mRS);
921 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700922
923 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700924 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700925 mPV = pvb.create();
926 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800927 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700928
Jason Sams37e7c2b2009-10-19 12:55:43 -0700929 //pva = new ProgramVertex.MatrixAllocation(mRS);
930 //pva.setupOrthoWindow(mWidth, mHeight);
931 //pvb.setTextureMatrixEnable(true);
932 //mPVOrtho = pvb.create();
933 //mPVOrtho.setName("PVOrtho");
934 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700935
936 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700937 }
Joe Onorato93839052009-08-06 20:34:32 -0700938
Jason Samscd689e12009-09-29 15:28:22 -0700939 private void initProgramFragment() {
940 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700941 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700942 sb.setMag(Sampler.Value.LINEAR);
943 sb.setWrapS(Sampler.Value.CLAMP);
944 sb.setWrapT(Sampler.Value.CLAMP);
945 Sampler linear = sb.create();
946
947 sb.setMin(Sampler.Value.NEAREST);
948 sb.setMag(Sampler.Value.NEAREST);
949 Sampler nearest = sb.create();
950
951 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700952 //mPFColor = bf.create();
953 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700954
955 bf.setTexEnable(true, 0);
956 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700957 mPFTexMip = bf.create();
958 mPFTexMip.setName("PFTexMip");
959 mPFTexMip.bindSampler(linear, 0);
960
961 mPFTexNearest = bf.create();
962 mPFTexNearest.setName("PFTexNearest");
963 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700964 }
965
966 private void initProgramStore() {
967 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
968 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700969 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700970 bs.setDitherEnable(true);
971 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
972 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
973 mPSIcons = bs.create();
974 mPSIcons.setName("PSIcons");
975
976 //bs.setDitherEnable(false);
977 //mPSText = bs.create();
978 //mPSText.setName("PSText");
979 }
980
981 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700982 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700983 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700984 }
Jason Sams78aebd82009-09-15 13:06:59 -0700985
Joe Onorato1feb3a82009-08-08 22:32:00 -0700986 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700987 mParams = new Params();
988 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700989
Joe Onoratobf15cb42009-08-07 14:33:40 -0700990 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700991
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700992 mParams.bubbleWidth = bubble.getBubbleWidth();
993 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
994 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
995 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700996
Joe Onoratod63458b2009-10-15 21:19:09 -0700997 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
998 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
999 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001000 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1001 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1002 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001003 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1004 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1005 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001006 mParams.homeButtonWidth = 76;
1007 mParams.homeButtonHeight = 68;
1008 mParams.homeButtonTextureWidth = 128;
1009 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001010
Joe Onoratod63458b2009-10-15 21:19:09 -07001011 mState.homeButtonId = mHomeButtonNormal.getID();
1012
Joe Onorato1feb3a82009-08-08 22:32:00 -07001013 mParams.save();
1014 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015
Joe Onorato1291a8c2009-09-15 15:07:25 -04001016 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1017 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1018 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001019
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001021 }
1022
Jason Sams37e7c2b2009-10-19 12:55:43 -07001023 private void initScript(int id) {
1024 }
1025
1026 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001027 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001028 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -07001029 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001030 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001031 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1032 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001033 mInvokeMove = sb.addInvokable("move");
1034 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001035 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001036 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001037 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001038 mScript = sb.create();
1039 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1040 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1041 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1042 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1043 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001044
Jason Sams37e7c2b2009-10-19 12:55:43 -07001045 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001046 }
Joe Onorato93839052009-08-06 20:34:32 -07001047
Jason Sams20df7c72009-11-05 12:30:24 -08001048 void dirtyCheck() {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001049 if (mHasSurface) {
Joe Onorato8cf34292009-12-11 10:59:51 -08001050 if (mAppsDirty && mAllAppsList != null) {
Joe Onorato8eea3912009-12-09 13:01:06 -08001051 for (int i=0; i < mState.iconCount; i++) {
1052 uploadAppIcon(i, mAllAppsList.get(i));
1053 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08001054 saveAppsList();
1055 mAppsDirty = false;
1056 }
1057 if (mZoomDirty) {
Joe Onorato68ba5ca2009-11-12 14:23:43 -08001058 setZoom(mZoom, mAnimateNextZoom);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001059 }
Jason Sams20df7c72009-11-05 12:30:24 -08001060 }
1061 }
1062
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063 private void setApps(ArrayList<ApplicationInfo> list) {
1064 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001065 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001066 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001067 allocCount = 1;
1068 }
1069
Joe Onorato9c1289c2009-08-17 11:03:03 -04001070 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001071 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001072 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073
1074 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001075 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001076 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001077
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001078 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001079
Joe Onorato9c1289c2009-08-17 11:03:03 -04001080 mState.iconCount = count;
Joe Onorato8eea3912009-12-09 13:01:06 -08001081 for (int i=0; i < mState.iconCount; i++) {
1082 createAppIconAllocations(i, list.get(i));
Joe Onorato3ecbd812009-12-11 13:38:54 -08001083 }
1084 if (mHasSurface) {
1085 for (int i=0; i < mState.iconCount; i++) {
1086 uploadAppIcon(i, list.get(i));
1087 }
1088 } else {
1089 mRollo.mAppsDirty = true;
Joe Onorato8eea3912009-12-09 13:01:06 -08001090 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001091 saveAppsList();
1092 }
1093
Joe Onorato3a8820b2009-11-10 15:06:42 -08001094 private void setZoom(float zoom, boolean animate) {
1095 mRollo.clearSelectedIcon();
1096 mRollo.setHomeSelected(SELECTED_NONE);
1097 if (zoom > 0.001f) {
1098 mRollo.mState.zoomTarget = zoom;
1099 } else {
1100 mRollo.mState.zoomTarget = 0;
1101 }
1102 mRollo.mState.save();
1103 if (!animate) {
1104 mRollo.mInvokeSetZoom.execute();
1105 }
1106 }
1107
Jason Samsc8514792009-10-29 14:27:29 -07001108 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1109 int black[] = new int[w > h ? w : h];
1110 Allocation.Adapter2D a = alloc.createAdapter2D();
1111 int mip = 0;
1112 while (w > 1 || h > 1) {
1113 a.subData(0, 0, 1, h, black);
1114 a.subData(w-1, 0, 1, h, black);
1115 a.subData(0, 0, w, 1, black);
1116 a.subData(0, h-1, w, 1, black);
1117 mip++;
1118 w = (w + 1) >> 1;
1119 h = (h + 1) >> 1;
1120 a.setConstraint(Dimension.LOD, mip);
1121 }
1122 a.subData(0, 0, 1, 1, black);
1123 }
1124
Joe Onorato8eea3912009-12-09 13:01:06 -08001125 private void createAppIconAllocations(int index, ApplicationInfo item) {
Joe Onoratoa8138d52009-10-06 19:25:30 -07001126 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001127 Element.RGBA_8888(mRS), true);
Joe Onorato8eea3912009-12-09 13:01:06 -08001128 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(),
1129 item.iconBitmap.getHeight());
Jason Samsc8514792009-10-29 14:27:29 -07001130
Joe Onoratoa8138d52009-10-06 19:25:30 -07001131 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001132 Element.RGBA_8888(mRS), true);
Joe Onorato8eea3912009-12-09 13:01:06 -08001133 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(),
1134 item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001135
1136 mIconIds[index] = mIcons[index].getID();
1137 mLabelIds[index] = mLabels[index].getID();
1138 }
1139
Joe Onorato8eea3912009-12-09 13:01:06 -08001140 private void uploadAppIcon(int index, ApplicationInfo item) {
1141 if (mIconIds[index] != mIcons[index].getID()) {
1142 throw new IllegalStateException("uploadAppIcon index=" + index
1143 + " mIcons[index].getID=" + mIcons[index].getID()
1144 + " mIconsIds[index]=" + mIconIds[index]
1145 + " item=" + item);
1146 }
1147 mIcons[index].uploadToTexture(0);
1148 mLabels[index].uploadToTexture(0);
1149 }
1150
Joe Onoratoa8138d52009-10-06 19:25:30 -07001151 /**
1152 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1153 * fill in the values and call saveAppsList().
1154 */
1155 private void reallocAppsList(int count) {
1156 Allocation[] icons = new Allocation[count];
1157 int[] iconIds = new int[count];
1158 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1159
1160 Allocation[] labels = new Allocation[count];
1161 int[] labelIds = new int[count];
1162 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1163
Joe Onoratobf173f12009-12-08 13:29:38 -08001164 final int oldCount = mRollo.mState.iconCount;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001165
1166 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1167 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1168 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1169 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1170
1171 mIcons = icons;
1172 mIconIds = iconIds;
1173 mLabels = labels;
1174 mLabelIds = labelIds;
1175 }
1176
1177 /**
1178 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1179 */
1180 private void addApp(int index, ApplicationInfo item) {
1181 final int count = mState.iconCount - index;
1182 final int dest = index + 1;
1183
1184 System.arraycopy(mIcons, index, mIcons, dest, count);
1185 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1186 System.arraycopy(mLabels, index, mLabels, dest, count);
1187 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1188
Joe Onorato8eea3912009-12-09 13:01:06 -08001189 createAppIconAllocations(index, item);
1190
Joe Onoratoa276fc52009-12-08 17:02:02 -08001191 if (mHasSurface) {
Jason Sams20df7c72009-11-05 12:30:24 -08001192 uploadAppIcon(index, item);
1193 } else {
1194 mAppsDirty = true;
1195 }
Joe Onorato8eea3912009-12-09 13:01:06 -08001196
1197 mRollo.mState.iconCount++;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001198 }
1199
1200 /**
1201 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1202 */
1203 private void removeApp(int index) {
1204 final int count = mState.iconCount - index - 1;
1205 final int src = index + 1;
1206
1207 System.arraycopy(mIcons, src, mIcons, index, count);
1208 System.arraycopy(mIconIds, src, mIconIds, index, count);
1209 System.arraycopy(mLabels, src, mLabels, index, count);
1210 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1211
Joe Onoratoa276fc52009-12-08 17:02:02 -08001212 mRollo.mState.iconCount--;
Joe Onorato8eea3912009-12-09 13:01:06 -08001213 final int last = mState.iconCount;
Joe Onoratoa276fc52009-12-08 17:02:02 -08001214
Joe Onoratoa8138d52009-10-06 19:25:30 -07001215 mIcons[last] = null;
1216 mIconIds[last] = 0;
1217 mLabels[last] = null;
1218 mLabelIds[last] = 0;
1219 }
1220
1221 /**
1222 * Send the apps list structures to RS.
1223 */
1224 private void saveAppsList() {
1225 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001226
Joe Onoratoa8138d52009-10-06 19:25:30 -07001227 mAllocIconIds.data(mIconIds);
1228 mAllocLabelIds.data(mLabelIds);
1229
Jason Sams37e7c2b2009-10-19 12:55:43 -07001230 if (mScript != null) { // this happens when we init it
1231 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1232 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001233 }
1234
1235 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001236
1237 // Note: mScript may be null if we haven't initialized it yet.
1238 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001239 if (mInvokeResetWAR != null) {
1240 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001241 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001242 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001243 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001244
1245 void initTouchState() {
1246 int width = getWidth();
1247 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001248 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1249 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001250
Jason Sams37e7c2b2009-10-19 12:55:43 -07001251 int centerY = (height / 2);
1252 mTouchYBorders[0] = centerY - (cellHeight * 2);
1253 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001254 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001255 mTouchYBorders[3] = centerY + cellHeight;
1256 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001257
Joe Onorato6665c0f2009-09-02 15:27:24 -07001258 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001259 mTouchXBorders[0] = 0;
1260 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001261 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001262 mTouchXBorders[3] = centerX + (width / 4);
1263 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001264 }
1265
Joe Onorato664457d2009-10-28 16:30:34 -04001266 void fling() {
1267 mInvokeFling.execute();
1268 }
1269
1270 void move() {
1271 mInvokeMove.execute();
1272 }
1273
1274 void moveTo(float row) {
1275 mState.targetPos = row;
1276 mState.save();
1277 mInvokeMoveTo.execute();
1278 }
1279
Jason Sams37e7c2b2009-10-19 12:55:43 -07001280 int chooseTappedIcon(int x, int y, float pos) {
1281 // Adjust for scroll position if not zero.
1282 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001283
Joe Onorato6665c0f2009-09-02 15:27:24 -07001284 int col = -1;
1285 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001286 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1287 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1288 col = i;
1289 break;
1290 }
1291 }
1292 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1293 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1294 row = i;
1295 break;
1296 }
1297 }
1298
1299 if (row < 0 || col < 0) {
1300 return -1;
1301 }
1302
Joe Onorato664457d2009-10-28 16:30:34 -04001303 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001304 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001305
Joe Onorato664457d2009-10-28 16:30:34 -04001306 if (index >= mState.iconCount) {
1307 return -1;
1308 } else {
1309 return index;
1310 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001311 }
1312
Joe Onorato6665c0f2009-09-02 15:27:24 -07001313 /**
1314 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001315 *
1316 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001317 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001318 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001319 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001320 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001321 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001322 }
1323
Joe Onoratoc61cff92009-11-08 11:54:39 -05001324 /**
1325 * Select the icon at the given index.
1326 *
1327 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001328 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001329 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001330 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001331 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001332 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001333 if (mLastSelection == SELECTION_ICONS) {
1334 mLastSelection = SELECTION_NONE;
1335 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001336 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001337 if (pressed == SELECTED_FOCUSED) {
1338 mLastSelection = SELECTION_ICONS;
1339 }
1340
Joe Onorato52a653f2009-11-11 14:52:11 -08001341 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001342 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001343
Joe Onorato52a653f2009-11-11 14:52:11 -08001344 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001345 Bitmap selectionBitmap = mSelectionBitmap;
1346
1347 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001348 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001349 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001350
1351 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001352 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001353 mSelectedIcon.uploadToTexture(0);
1354 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001355
1356 if (prev != index) {
1357 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001358 //setContentDescription(info.title);
1359 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1360 }
1361 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001362 }
1363 }
1364
1365 /**
1366 * You need to call save() on mState on your own after calling this.
1367 */
1368 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001369 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001370 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001371
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001372 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001373 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001374 switch (mode) {
1375 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001376 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001377 break;
1378 case SELECTED_FOCUSED:
1379 mLastSelection = SELECTION_HOME;
1380 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001381 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001382 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1383 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001384 break;
1385 case SELECTED_PRESSED:
1386 mState.homeButtonId = mHomeButtonPressed.getID();
1387 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001388 }
1389 }
Joe Onoratobe386092009-11-17 17:32:16 -08001390
1391 public void dumpState() {
1392 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1393 Log.d(TAG, "mRollo.mHeight=" + mHeight);
1394 Log.d(TAG, "mRollo.mIcons=" + mIcons);
1395 if (mIcons != null) {
1396 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1397 }
1398 if (mIconIds != null) {
1399 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1400 }
1401 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1402 if (mLabelIds != null) {
1403 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1404 }
1405 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1406 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1407 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
1408 Log.d(TAG, "mRollo.mHasSurface=" + mHasSurface);
1409 Log.d(TAG, "mRollo.mAppsDirty=" + mAppsDirty);
1410 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1411 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1412 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1413 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1414 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1415 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1416 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1417 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1418 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1419 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1420 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1421 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1422 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1423 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1424 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1425 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1426 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1427 }
1428 }
1429
1430 public void dumpState() {
1431 Log.d(TAG, "mRS=" + mRS);
1432 Log.d(TAG, "mRollo=" + mRollo);
1433 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1434 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1435 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1436 Log.d(TAG, "mLastSelection=" + mLastSelection);
1437 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1438 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1439 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1440 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1441 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1442 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1443 Log.d(TAG, "mZoom=" + mZoom);
1444 Log.d(TAG, "mPosX=" + mPosX);
1445 Log.d(TAG, "mVelocity=" + mVelocity);
1446 Log.d(TAG, "mMessageProc=" + mMessageProc);
1447 if (mRollo != null) {
1448 mRollo.dumpState();
1449 }
1450 if (mRS != null) {
1451 mRS.contextDump(0);
1452 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001453 }
Joe Onorato93839052009-08-06 20:34:32 -07001454}
1455
1456