blob: b3d8c3e9919e656384757fdb0e9fffd2a9e2a531 [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);
Joe Onoratofab74402009-11-11 16:05:23 -0800192 // Without this, we leak mMessageCallback which leaks the context.
193 mRS.mMessageCallback = null;
Joe Onorato7bb17492009-09-24 17:51:01 -0700194 }
195
196 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700197 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800198 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700199
Joe Onorato080d9b62009-11-02 12:01:11 -0500200 super.surfaceChanged(holder, format, w, h);
201
Jason Samse26d9fc2009-11-12 14:00:43 -0800202 if (mRollo == null) {
Jason Sams90396672009-11-03 13:59:34 -0800203 mRollo = new RolloRS();
204 mRollo.init(getResources(), w, h);
205 if (mAllAppsList != null) {
206 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800207 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800208 if (mShouldGainFocus) {
209 gainFocus();
210 mShouldGainFocus = false;
211 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 }
Jason Samse26d9fc2009-11-12 14:00:43 -0800213 mRollo.dirtyCheck();
Jason Sams5612e432009-11-16 14:18:07 -0800214 mRollo.resize(w, h);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700215
Joe Onoratocb75f362009-11-12 13:04:07 -0800216 mRS.mMessageCallback = mMessageProc = new AAMessage();
217
Joe Onoratoc567acb2009-08-31 14:34:43 -0700218 Resources res = getContext().getResources();
219 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700220
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800221 //long endTime = SystemClock.uptimeMillis();
222 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700223 }
Jason Sams2e19c052009-10-20 18:19:55 -0700224
Joe Onorato93839052009-08-06 20:34:32 -0700225 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800226 public void onWindowFocusChanged(boolean hasWindowFocus) {
227 super.onWindowFocusChanged(hasWindowFocus);
228 if (mArrowNavigation) {
229 if (!hasWindowFocus) {
230 // Clear selection when we lose window focus
231 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500232 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800233 mRollo.clearSelectedIcon();
234 mRollo.mState.save();
235 } else if (hasWindowFocus) {
236 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500237 if (mLastSelection == SELECTION_ICONS) {
238 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800239 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500240 Defines.COLUMNS_PER_PAGE;
241 if (selection < 0 || // No selection
242 selection < firstIcon || // off the top of the screen
243 selection >= mRollo.mState.iconCount || // past last icon
244 selection >= firstIcon + // past last icon on screen
245 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
246 selection = firstIcon;
247 }
248
249 // Select the first icon when we gain window focus
250 mRollo.selectIcon(selection, SELECTED_FOCUSED);
251 mRollo.mState.save();
252 } else if (mLastSelection == SELECTION_HOME) {
253 mRollo.setHomeSelected(SELECTED_FOCUSED);
254 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800255 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800256 }
257 }
258 }
259 }
260
261 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700262 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
263 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400264
265 if (!isVisible()) {
266 return;
267 }
268
Mike Cleron7d5d7462009-10-20 14:06:00 -0700269 if (gainFocus) {
Jason Sams510ee042009-12-15 14:23:37 -0800270 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800271 gainFocus();
272 } else {
273 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700274 }
275 } else {
Joe Onorato8eea3912009-12-09 13:01:06 -0800276 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800277 if (mArrowNavigation) {
278 // Clear selection when we lose focus
279 mRollo.clearSelectedIcon();
280 mRollo.setHomeSelected(SELECTED_NONE);
281 mRollo.mState.save();
282 mArrowNavigation = false;
283 }
284 } else {
285 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700286 }
287 }
288 }
289
Mike Cleronb64b67a2009-11-08 14:56:25 -0800290 private void gainFocus() {
291 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
292 // Select the first icon when we gain keyboard focus
293 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800294 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800295 SELECTED_FOCUSED);
296 mRollo.mState.save();
297 }
298 }
299
Mike Cleron7d5d7462009-10-20 14:06:00 -0700300 @Override
301 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700302
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800303 boolean handled = false;
Jason Samse26d9fc2009-11-12 14:00:43 -0800304
Joe Onorato859b3a72009-10-28 15:17:01 -0400305 if (!isVisible()) {
306 return false;
307 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500308 final int iconCount = mRollo.mState.iconCount;
309
Mike Cleron7d5d7462009-10-20 14:06:00 -0700310 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
311 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500312 if (mLastSelection == SELECTION_HOME) {
313 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
314 mLauncher.closeAllApps(true);
315 } else {
316 int whichApp = mRollo.mState.selectedIconIndex;
317 if (whichApp >= 0) {
318 ApplicationInfo app = mAllAppsList.get(whichApp);
319 mLauncher.startActivitySafely(app.intent);
320 handled = true;
321 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700322 }
323 }
324 }
Jason Sams2e19c052009-10-20 18:19:55 -0700325
Joe Onorato478730f2009-11-16 18:54:43 -0800326 if (iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700327 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700328
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800329 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800330 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700331
Mike Cleron7d5d7462009-10-20 14:06:00 -0700332 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500333 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700334
Mike Cleron7d5d7462009-10-20 14:06:00 -0700335 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500336 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700337 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700338
Mike Cleron7d5d7462009-10-20 14:06:00 -0700339 int newSelection = currentSelection;
340
341 switch (keyCode) {
342 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500343 if (mLastSelection == SELECTION_HOME) {
344 mRollo.setHomeSelected(SELECTED_NONE);
345 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
346 if (lastRowCount == 0) {
347 lastRowCount = Defines.COLUMNS_PER_PAGE;
348 }
349 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
350 if (newSelection >= iconCount) {
351 newSelection = iconCount-1;
352 }
353 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
354 - (Defines.ROWS_PER_PAGE - 1);
355 if (target < 0) {
356 target = 0;
357 }
358 if (currentTopRow != target) {
359 mRollo.moveTo(target);
360 }
361 } else {
362 if (currentPageRow > 0) {
363 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
364 } else if (currentTopRow > 0) {
365 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
366 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
Joe Onoratoaf5b4cb2009-12-08 17:51:22 -0800367 } else if (currentPageRow != 0) {
368 newSelection = currentTopRow * Defines.ROWS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500369 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700370 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800371 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700372 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800373
Joe Onoratoa13f5742009-11-02 17:15:19 -0500374 case KeyEvent.KEYCODE_DPAD_DOWN: {
375 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
376 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
377 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500378 if (mLastSelection != SELECTION_HOME) {
379 if (currentRow < rowCount-1) {
380 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato478730f2009-11-16 18:54:43 -0800381 if (currentSelection < 0) {
382 newSelection = 0;
383 } else {
384 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
385 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500386 if (newSelection >= iconCount) {
387 // Go from D to G in this arrangement:
388 // A B C D
389 // E F G
390 newSelection = iconCount - 1;
391 }
392 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
393 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
394 Defines.ROWS_PER_PAGE + 1);
395 }
396 } else {
397 newSelection = -1;
398 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700399 }
400 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800401 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700402 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500403 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700404 case KeyEvent.KEYCODE_DPAD_LEFT:
405 if (currentPageCol > 0) {
406 newSelection = currentSelection - 1;
407 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800408 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700409 break;
410 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700411 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500412 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700413 newSelection = currentSelection + 1;
414 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800415 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700416 break;
417 }
418 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500419 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700420 mRollo.mState.save();
421 }
422 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800423 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700424 }
425
Joe Onorato93839052009-08-06 20:34:32 -0700426 @Override
427 public boolean onTouchEvent(MotionEvent ev)
428 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700429 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700430
Joe Onorato7bb17492009-09-24 17:51:01 -0700431 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700432 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700433 }
434
Joe Onoratofb0ca672009-09-14 17:55:46 -0400435 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700436 return true;
437 }
438
439 super.onTouchEvent(ev);
440
Joe Onoratofb0ca672009-09-14 17:55:46 -0400441 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700442 int y = (int)ev.getY();
443
Joe Onoratobcbeab82009-10-01 21:45:43 -0700444 int action = ev.getAction();
445 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400446 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700447 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
448 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500449 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700450 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700451 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700452 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700453 mTouchTracking = TRACKING_FLING;
454
455 mMotionDownRawX = (int)ev.getRawX();
456 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700457
Mike Cleron7d5d7462009-10-20 14:06:00 -0700458 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700459 mRollo.mState.newTouchDown = 1;
460
461 if (!mRollo.checkClickOK()) {
462 mRollo.clearSelectedIcon();
463 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700464 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800465 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700466 if (mDownIconIndex < 0) {
467 // if nothing was selected, no long press.
468 cancelLongPress();
469 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700470 }
471 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700472 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800473 mVelocityTracker = VelocityTracker.obtain();
474 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700475 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700476 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400477 break;
478 case MotionEvent.ACTION_MOVE:
479 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700480 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500481 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
482 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700483 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700484 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700485 int rawX = (int)ev.getRawX();
486 int rawY = (int)ev.getRawY();
487 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700488 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700489
Joe Onorato82ca5502009-10-15 16:59:23 -0700490 if (!mStartedScrolling && slop < mSlop) {
491 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700492 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800493 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700494 if (mDownIconIndex != mCurrentIconIndex) {
495 // If a different icon is selected, don't allow it to be picked up.
496 // This handles off-axis dragging.
497 cancelLongPress();
498 mCurrentIconIndex = -1;
499 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700500 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700501 if (!mStartedScrolling) {
502 cancelLongPress();
503 mCurrentIconIndex = -1;
504 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700505 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700506 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700507 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700508
Joe Onoratobcbeab82009-10-01 21:45:43 -0700509 mStartedScrolling = true;
510 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800511 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700512 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700513 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400514 }
515 break;
516 case MotionEvent.ACTION_UP:
517 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700518 if (mTouchTracking == TRACKING_HOME) {
519 if (action == MotionEvent.ACTION_UP) {
520 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400521 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700522 mLauncher.closeAllApps(true);
523 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500524 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700525 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700526 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700527 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700528 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700529 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700530 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700531
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800532 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
533 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700534 mRollo.clearSelectedIcon();
535 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700536 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700537
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800538 if (mVelocityTracker != null) {
539 mVelocityTracker.recycle();
540 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700541 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700542 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700543 mTouchTracking = TRACKING_NONE;
544 break;
Joe Onorato93839052009-08-06 20:34:32 -0700545 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700546
547 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700548 }
549
Joe Onorato6665c0f2009-09-02 15:27:24 -0700550 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700551 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400552 return;
553 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700554 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
555 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400556 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700557 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700558 mLauncher.startActivitySafely(app.intent);
559 }
560 }
561
562 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700563 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400564 return true;
565 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700566 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
567 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
568 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700569
570 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700571 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
572 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700573
Joe Onoratobcbeab82009-10-01 21:45:43 -0700574 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
575 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700576 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700577 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700578 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700579
Joe Onorato7bb17492009-09-24 17:51:01 -0700580 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700581 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700582 return true;
583 }
584
Joe Onorato52a653f2009-11-11 14:52:11 -0800585 @Override
586 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
587 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
588 if (!isVisible()) {
589 return false;
590 }
591 String text = null;
592 int index;
593 int count = mAllAppsList.size() + 1; // +1 is home
594 int pos = -1;
595 switch (mLastSelection) {
596 case SELECTION_ICONS:
597 index = mRollo.mState.selectedIconIndex;
598 if (index >= 0) {
599 ApplicationInfo info = mAllAppsList.get(index);
600 if (info.title != null) {
601 text = info.title.toString();
602 pos = index;
603 }
604 }
605 break;
606 case SELECTION_HOME:
607 text = getContext().getString(R.string.all_apps_home_button_label);
608 pos = count;
609 break;
610 }
611 if (text != null) {
Joe Onorato52a653f2009-11-11 14:52:11 -0800612 event.setEnabled(true);
613 event.getText().add(text);
614 //event.setContentDescription(text);
615 event.setItemCount(count);
616 event.setCurrentItemIndex(pos);
617 }
618 }
619 return false;
620 }
621
Joe Onorato5162ea92009-09-03 09:39:42 -0700622 public void setDragController(DragController dragger) {
623 mDragController = dragger;
624 }
625
626 public void onDropCompleted(View target, boolean success) {
627 }
628
Joe Onorato4db52312009-10-06 11:17:43 -0700629 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800630 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700631 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800632 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700633 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800634 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400635 cancelLongPress();
Jason Sams510ee042009-12-15 14:23:37 -0800636 if (mRollo == null) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800637 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800638 mZoom = zoom;
639 mAnimateNextZoom = animate;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800640 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700641 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800642 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700643 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400644 }
645
646 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800647 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800648 }
649
650 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800651 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400652 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700653
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654 public void setApps(ArrayList<ApplicationInfo> list) {
655 mAllAppsList = list;
656 if (mRollo != null) {
Joe Onorato3ecbd812009-12-11 13:38:54 -0800657 mRollo.setApps(list);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400659 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700660 }
661
Joe Onoratoa8138d52009-10-06 19:25:30 -0700662 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500663 if (mAllAppsList == null) {
664 // Not done loading yet. We'll find out about it later.
665 return;
666 }
667
Joe Onoratoa8138d52009-10-06 19:25:30 -0700668 final int N = list.size();
Joe Onorato8eea3912009-12-09 13:01:06 -0800669 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700670 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
671 }
672
673 for (int i=0; i<N; i++) {
674 final ApplicationInfo item = list.get(i);
Joe Onoratob0c27f22009-12-01 16:19:38 -0800675 int index = Collections.binarySearch(mAllAppsList, item,
676 LauncherModel.APP_NAME_COMPARATOR);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700677 if (index < 0) {
678 index = -(index+1);
679 }
680 mAllAppsList.add(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -0800681 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700682 mRollo.addApp(index, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700683 }
684 }
685
Joe Onorato8eea3912009-12-09 13:01:06 -0800686 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700687 mRollo.saveAppsList();
688 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700689 }
690
Joe Onoratoa8138d52009-10-06 19:25:30 -0700691 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500692 if (mAllAppsList == null) {
693 // Not done loading yet. We'll find out about it later.
694 return;
695 }
696
Joe Onoratoa8138d52009-10-06 19:25:30 -0700697 final int N = list.size();
698 for (int i=0; i<N; i++) {
699 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400700 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700701 if (index >= 0) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800702 int ic = mRollo != null ? mRollo.mState.iconCount : 666;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700703 mAllAppsList.remove(index);
Joe Onorato8eea3912009-12-09 13:01:06 -0800704 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700705 mRollo.removeApp(index);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700706 }
707 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800708 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700709 // Try to recover. This should keep us from crashing for now.
710 }
711 }
712
Joe Onorato8eea3912009-12-09 13:01:06 -0800713 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700714 mRollo.saveAppsList();
715 }
716 }
717
718 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
719 // Just remove and add, because they may need to be re-sorted.
720 removeApps(list);
721 addApps(list);
722 }
723
Joe Onoratocb9f7982009-10-31 16:32:02 -0400724 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
725 ComponentName component = item.intent.getComponent();
726 final int N = list.size();
727 for (int i=0; i<N; i++) {
728 ApplicationInfo x = list.get(i);
729 if (x.intent.getComponent().equals(component)) {
730 return i;
731 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700732 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400733 return -1;
734 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700735
Joe Onoratoc567acb2009-08-31 14:34:43 -0700736 private static int countPages(int iconCount) {
737 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
738 int pages = iconCount / iconsPerPage;
739 if (pages*iconsPerPage != iconCount) {
740 pages++;
741 }
742 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743 }
744
Joe Onoratocb75f362009-11-12 13:04:07 -0800745 class AAMessage extends RenderScript.RSMessage {
746 public void run() {
747 mPosX = ((float)mData[0]) / (1 << 16);
748 mVelocity = ((float)mData[1]) / (1 << 16);
749 mZoom = ((float)mData[2]) / (1 << 16);
750 mZoomDirty = false;
751 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800752 }
753
Joe Onorato93839052009-08-06 20:34:32 -0700754 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700755
Joe Onorato1feb3a82009-08-08 22:32:00 -0700756 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700757 private int mWidth;
758 private int mHeight;
759
760 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700761 private Script mScript;
762 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700763 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700764 private Script.Invokable mInvokeFling;
765 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800766 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700767
Jason Samscd689e12009-09-29 15:28:22 -0700768 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700769 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700770 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700771 private ProgramFragment mPFTexMip;
772 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700773 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700774 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700775 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700776 private SimpleMesh mMesh2;
Jason Sams5612e432009-11-16 14:18:07 -0800777 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700778
Joe Onoratod63458b2009-10-15 21:19:09 -0700779 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500780 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700781 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700782
Joe Onoratobf15cb42009-08-07 14:33:40 -0700783 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700784 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700785 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700786
Joe Onoratobf15cb42009-08-07 14:33:40 -0700787 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700788 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700789 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700790 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700791
Joe Onorato6665c0f2009-09-02 15:27:24 -0700792 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700793 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700794
795 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400796 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700797
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700798 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700799 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700800
Jason Sams78aebd82009-09-15 13:06:59 -0700801 class BaseAlloc {
802 Allocation mAlloc;
803 Type mType;
804
805 void save() {
806 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700807 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700808 }
809
Jason Sams476339d2009-09-29 18:14:38 -0700810 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800811 return (Math.abs(mVelocity) < 0.4f) &&
812 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700813 }
814
Jason Sams78aebd82009-09-15 13:06:59 -0700815 class Params extends BaseAlloc {
816 Params() {
817 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
818 mAlloc = Allocation.createTyped(mRS, mType);
819 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700820 }
Jason Sams78aebd82009-09-15 13:06:59 -0700821 public int bubbleWidth;
822 public int bubbleHeight;
823 public int bubbleBitmapWidth;
824 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700825
Joe Onoratobcbeab82009-10-01 21:45:43 -0700826 public int homeButtonWidth;
827 public int homeButtonHeight;
828 public int homeButtonTextureWidth;
829 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700830 }
831
832 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700833 public float newPositionX;
834 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700835 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700836 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700837 public int selectedIconIndex = -1;
838 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700839 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700840 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700841 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700842
843 State() {
844 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
845 mAlloc = Allocation.createTyped(mRS, mType);
846 save();
847 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700848 }
849
850 public RolloRS() {
851 }
852
853 public void init(Resources res, int width, int height) {
854 mRes = res;
855 mWidth = width;
856 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700857 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700858 initProgramVertex();
859 initProgramFragment();
860 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700861 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700862 initGl();
863 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700864 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700865 initRs();
866 }
867
Jason Sams0aa71662009-10-02 18:43:18 -0700868 public void initMesh() {
869 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
870 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
871
Jason Samsd8152b92009-10-13 17:19:10 -0700872 float y = 0;
873 float z = 0;
874 for (int ct=0; ct < 200; ct++) {
875 float angle = 0;
876 float maxAngle = 3.14f * 0.16f;
877 float l = 1.f;
878
879 l = 1 - ((ct-5) * 0.10f);
880 if (ct > 7) {
881 angle = maxAngle * (ct - 7) * 0.2f;
882 angle = Math.min(angle, maxAngle);
883 }
Jason Samsc8514792009-10-29 14:27:29 -0700884 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700885 l = Math.min(1.0f, l);
886
887 y += 0.1f * Math.cos(angle);
888 z += 0.1f * Math.sin(angle);
889
890 float t = 0.1f * ct;
891 float ds = 0.08f;
892 tm.setColor(l, l, l, 0.99f);
893 tm.setTexture(ds, t);
894 tm.addVertex(-0.5f, y, z);
895 tm.setTexture(1 - ds, t);
896 tm.addVertex(0.5f, y, z);
897 }
898 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
899 tm.addTriangle(ct, ct+1, ct+2);
900 tm.addTriangle(ct+1, ct+3, ct+2);
901 }
902 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700903 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700904 }
905
Jason Sams5612e432009-11-16 14:18:07 -0800906 void resize(int w, int h) {
907 mPVA.setupProjectionNormalized(w, h);
908 mWidth = w;
909 mHeight = h;
910 }
911
Jason Samscd689e12009-09-29 15:28:22 -0700912 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800913 mPVA = new ProgramVertex.MatrixAllocation(mRS);
914 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700915
916 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700917 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700918 mPV = pvb.create();
919 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800920 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700921
Jason Sams37e7c2b2009-10-19 12:55:43 -0700922 //pva = new ProgramVertex.MatrixAllocation(mRS);
923 //pva.setupOrthoWindow(mWidth, mHeight);
924 //pvb.setTextureMatrixEnable(true);
925 //mPVOrtho = pvb.create();
926 //mPVOrtho.setName("PVOrtho");
927 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700928
929 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700930 }
Joe Onorato93839052009-08-06 20:34:32 -0700931
Jason Samscd689e12009-09-29 15:28:22 -0700932 private void initProgramFragment() {
933 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700934 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700935 sb.setMag(Sampler.Value.LINEAR);
936 sb.setWrapS(Sampler.Value.CLAMP);
937 sb.setWrapT(Sampler.Value.CLAMP);
938 Sampler linear = sb.create();
939
940 sb.setMin(Sampler.Value.NEAREST);
941 sb.setMag(Sampler.Value.NEAREST);
942 Sampler nearest = sb.create();
943
Jason Sams72f1d312009-12-17 16:58:25 -0800944 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700945 //mPFColor = bf.create();
946 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700947
Jason Sams72f1d312009-12-17 16:58:25 -0800948 bf.setTexture(ProgramFragment.Builder.EnvMode.REPLACE,
949 ProgramFragment.Builder.Format.RGBA, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700950 mPFTexMip = bf.create();
951 mPFTexMip.setName("PFTexMip");
952 mPFTexMip.bindSampler(linear, 0);
953
954 mPFTexNearest = bf.create();
955 mPFTexNearest.setName("PFTexNearest");
956 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700957 }
958
959 private void initProgramStore() {
960 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
961 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700962 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700963 bs.setDitherEnable(true);
964 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
965 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
966 mPSIcons = bs.create();
967 mPSIcons.setName("PSIcons");
968
969 //bs.setDitherEnable(false);
970 //mPSText = bs.create();
971 //mPSText.setName("PSText");
972 }
973
974 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700975 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700976 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700977 }
Jason Sams78aebd82009-09-15 13:06:59 -0700978
Joe Onorato1feb3a82009-08-08 22:32:00 -0700979 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700980 mParams = new Params();
981 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700982
Joe Onoratobf15cb42009-08-07 14:33:40 -0700983 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700984
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700985 mParams.bubbleWidth = bubble.getBubbleWidth();
986 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
987 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
988 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700989
Joe Onoratod63458b2009-10-15 21:19:09 -0700990 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
991 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
992 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500993 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
994 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
995 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -0700996 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
997 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
998 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700999 mParams.homeButtonWidth = 76;
1000 mParams.homeButtonHeight = 68;
1001 mParams.homeButtonTextureWidth = 128;
1002 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001003
Joe Onoratod63458b2009-10-15 21:19:09 -07001004 mState.homeButtonId = mHomeButtonNormal.getID();
1005
Joe Onorato1feb3a82009-08-08 22:32:00 -07001006 mParams.save();
1007 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008
Joe Onorato1291a8c2009-09-15 15:07:25 -04001009 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1010 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1011 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001012
Joe Onorato9c1289c2009-08-17 11:03:03 -04001013 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001014 }
1015
Jason Sams37e7c2b2009-10-19 12:55:43 -07001016 private void initScript(int id) {
1017 }
1018
1019 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001020 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001021 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -07001022 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001023 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001024 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1025 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001026 mInvokeMove = sb.addInvokable("move");
1027 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001028 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001029 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001030 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001031 mScript = sb.create();
1032 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1033 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1034 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1035 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1036 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001037
Jason Sams37e7c2b2009-10-19 12:55:43 -07001038 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001039 }
Joe Onorato93839052009-08-06 20:34:32 -07001040
Jason Sams20df7c72009-11-05 12:30:24 -08001041 void dirtyCheck() {
Jason Sams510ee042009-12-15 14:23:37 -08001042 if (mZoomDirty) {
1043 setZoom(mZoom, mAnimateNextZoom);
Jason Sams20df7c72009-11-05 12:30:24 -08001044 }
1045 }
1046
Joe Onorato9c1289c2009-08-17 11:03:03 -04001047 private void setApps(ArrayList<ApplicationInfo> list) {
1048 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001049 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001050 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001051 allocCount = 1;
1052 }
1053
Joe Onorato9c1289c2009-08-17 11:03:03 -04001054 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001055 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001056 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001057
1058 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001059 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001060 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001061
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001062 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001063
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064 mState.iconCount = count;
Joe Onorato8eea3912009-12-09 13:01:06 -08001065 for (int i=0; i < mState.iconCount; i++) {
1066 createAppIconAllocations(i, list.get(i));
Joe Onorato3ecbd812009-12-11 13:38:54 -08001067 }
Jason Sams510ee042009-12-15 14:23:37 -08001068 for (int i=0; i < mState.iconCount; i++) {
1069 uploadAppIcon(i, list.get(i));
Joe Onorato8eea3912009-12-09 13:01:06 -08001070 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001071 saveAppsList();
1072 }
1073
Joe Onorato3a8820b2009-11-10 15:06:42 -08001074 private void setZoom(float zoom, boolean animate) {
1075 mRollo.clearSelectedIcon();
1076 mRollo.setHomeSelected(SELECTED_NONE);
1077 if (zoom > 0.001f) {
1078 mRollo.mState.zoomTarget = zoom;
1079 } else {
1080 mRollo.mState.zoomTarget = 0;
1081 }
1082 mRollo.mState.save();
1083 if (!animate) {
1084 mRollo.mInvokeSetZoom.execute();
1085 }
1086 }
1087
Jason Samsc8514792009-10-29 14:27:29 -07001088 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1089 int black[] = new int[w > h ? w : h];
1090 Allocation.Adapter2D a = alloc.createAdapter2D();
1091 int mip = 0;
1092 while (w > 1 || h > 1) {
1093 a.subData(0, 0, 1, h, black);
1094 a.subData(w-1, 0, 1, h, black);
1095 a.subData(0, 0, w, 1, black);
1096 a.subData(0, h-1, w, 1, black);
1097 mip++;
1098 w = (w + 1) >> 1;
1099 h = (h + 1) >> 1;
1100 a.setConstraint(Dimension.LOD, mip);
1101 }
1102 a.subData(0, 0, 1, 1, black);
1103 }
1104
Joe Onorato8eea3912009-12-09 13:01:06 -08001105 private void createAppIconAllocations(int index, ApplicationInfo item) {
Joe Onoratoa8138d52009-10-06 19:25:30 -07001106 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001107 Element.RGBA_8888(mRS), true);
Joe Onorato8eea3912009-12-09 13:01:06 -08001108 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(),
1109 item.iconBitmap.getHeight());
Jason Samsc8514792009-10-29 14:27:29 -07001110
Joe Onoratoa8138d52009-10-06 19:25:30 -07001111 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001112 Element.RGBA_8888(mRS), true);
Joe Onorato8eea3912009-12-09 13:01:06 -08001113 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(),
1114 item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001115
1116 mIconIds[index] = mIcons[index].getID();
1117 mLabelIds[index] = mLabels[index].getID();
1118 }
1119
Joe Onorato8eea3912009-12-09 13:01:06 -08001120 private void uploadAppIcon(int index, ApplicationInfo item) {
1121 if (mIconIds[index] != mIcons[index].getID()) {
1122 throw new IllegalStateException("uploadAppIcon index=" + index
1123 + " mIcons[index].getID=" + mIcons[index].getID()
1124 + " mIconsIds[index]=" + mIconIds[index]
1125 + " item=" + item);
1126 }
1127 mIcons[index].uploadToTexture(0);
1128 mLabels[index].uploadToTexture(0);
1129 }
1130
Joe Onoratoa8138d52009-10-06 19:25:30 -07001131 /**
1132 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1133 * fill in the values and call saveAppsList().
1134 */
1135 private void reallocAppsList(int count) {
1136 Allocation[] icons = new Allocation[count];
1137 int[] iconIds = new int[count];
1138 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1139
1140 Allocation[] labels = new Allocation[count];
1141 int[] labelIds = new int[count];
1142 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1143
Joe Onoratobf173f12009-12-08 13:29:38 -08001144 final int oldCount = mRollo.mState.iconCount;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001145
1146 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1147 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1148 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1149 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1150
1151 mIcons = icons;
1152 mIconIds = iconIds;
1153 mLabels = labels;
1154 mLabelIds = labelIds;
1155 }
1156
1157 /**
1158 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1159 */
1160 private void addApp(int index, ApplicationInfo item) {
1161 final int count = mState.iconCount - index;
1162 final int dest = index + 1;
1163
1164 System.arraycopy(mIcons, index, mIcons, dest, count);
1165 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1166 System.arraycopy(mLabels, index, mLabels, dest, count);
1167 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1168
Joe Onorato8eea3912009-12-09 13:01:06 -08001169 createAppIconAllocations(index, item);
Jason Sams510ee042009-12-15 14:23:37 -08001170 uploadAppIcon(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -08001171 mRollo.mState.iconCount++;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001172 }
1173
1174 /**
1175 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1176 */
1177 private void removeApp(int index) {
1178 final int count = mState.iconCount - index - 1;
1179 final int src = index + 1;
1180
1181 System.arraycopy(mIcons, src, mIcons, index, count);
1182 System.arraycopy(mIconIds, src, mIconIds, index, count);
1183 System.arraycopy(mLabels, src, mLabels, index, count);
1184 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1185
Joe Onoratoa276fc52009-12-08 17:02:02 -08001186 mRollo.mState.iconCount--;
Joe Onorato8eea3912009-12-09 13:01:06 -08001187 final int last = mState.iconCount;
Joe Onoratoa276fc52009-12-08 17:02:02 -08001188
Joe Onoratoa8138d52009-10-06 19:25:30 -07001189 mIcons[last] = null;
1190 mIconIds[last] = 0;
1191 mLabels[last] = null;
1192 mLabelIds[last] = 0;
1193 }
1194
1195 /**
1196 * Send the apps list structures to RS.
1197 */
1198 private void saveAppsList() {
1199 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001200
Joe Onoratoa8138d52009-10-06 19:25:30 -07001201 mAllocIconIds.data(mIconIds);
1202 mAllocLabelIds.data(mLabelIds);
1203
Jason Sams37e7c2b2009-10-19 12:55:43 -07001204 if (mScript != null) { // this happens when we init it
1205 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1206 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 }
1208
1209 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001210
1211 // Note: mScript may be null if we haven't initialized it yet.
1212 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001213 if (mInvokeResetWAR != null) {
1214 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001215 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001216 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001218
1219 void initTouchState() {
1220 int width = getWidth();
1221 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001222 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1223 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001224
Jason Sams37e7c2b2009-10-19 12:55:43 -07001225 int centerY = (height / 2);
1226 mTouchYBorders[0] = centerY - (cellHeight * 2);
1227 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001228 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001229 mTouchYBorders[3] = centerY + cellHeight;
1230 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001231
Joe Onorato6665c0f2009-09-02 15:27:24 -07001232 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001233 mTouchXBorders[0] = 0;
1234 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001235 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001236 mTouchXBorders[3] = centerX + (width / 4);
1237 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001238 }
1239
Joe Onorato664457d2009-10-28 16:30:34 -04001240 void fling() {
1241 mInvokeFling.execute();
1242 }
1243
1244 void move() {
1245 mInvokeMove.execute();
1246 }
1247
1248 void moveTo(float row) {
1249 mState.targetPos = row;
1250 mState.save();
1251 mInvokeMoveTo.execute();
1252 }
1253
Jason Sams37e7c2b2009-10-19 12:55:43 -07001254 int chooseTappedIcon(int x, int y, float pos) {
1255 // Adjust for scroll position if not zero.
1256 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001257
Joe Onorato6665c0f2009-09-02 15:27:24 -07001258 int col = -1;
1259 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001260 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1261 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1262 col = i;
1263 break;
1264 }
1265 }
1266 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1267 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1268 row = i;
1269 break;
1270 }
1271 }
1272
1273 if (row < 0 || col < 0) {
1274 return -1;
1275 }
1276
Joe Onorato664457d2009-10-28 16:30:34 -04001277 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001278 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001279
Joe Onorato664457d2009-10-28 16:30:34 -04001280 if (index >= mState.iconCount) {
1281 return -1;
1282 } else {
1283 return index;
1284 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001285 }
1286
Joe Onorato6665c0f2009-09-02 15:27:24 -07001287 /**
1288 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001289 *
1290 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001291 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001292 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001293 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001294 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001295 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001296 }
1297
Joe Onoratoc61cff92009-11-08 11:54:39 -05001298 /**
1299 * Select the icon at the given index.
1300 *
1301 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001302 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001303 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001304 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001305 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001306 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001307 if (mLastSelection == SELECTION_ICONS) {
1308 mLastSelection = SELECTION_NONE;
1309 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001310 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001311 if (pressed == SELECTED_FOCUSED) {
1312 mLastSelection = SELECTION_ICONS;
1313 }
1314
Joe Onorato52a653f2009-11-11 14:52:11 -08001315 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001316 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001317
Joe Onorato52a653f2009-11-11 14:52:11 -08001318 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001319 Bitmap selectionBitmap = mSelectionBitmap;
1320
1321 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001322 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001323 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001324
1325 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001326 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001327 mSelectedIcon.uploadToTexture(0);
1328 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001329
1330 if (prev != index) {
1331 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001332 //setContentDescription(info.title);
1333 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1334 }
1335 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001336 }
1337 }
1338
1339 /**
1340 * You need to call save() on mState on your own after calling this.
1341 */
1342 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001343 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001344 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001345
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001346 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001347 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001348 switch (mode) {
1349 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001350 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001351 break;
1352 case SELECTED_FOCUSED:
1353 mLastSelection = SELECTION_HOME;
1354 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001355 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001356 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1357 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001358 break;
1359 case SELECTED_PRESSED:
1360 mState.homeButtonId = mHomeButtonPressed.getID();
1361 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001362 }
1363 }
Joe Onoratobe386092009-11-17 17:32:16 -08001364
1365 public void dumpState() {
1366 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1367 Log.d(TAG, "mRollo.mHeight=" + mHeight);
1368 Log.d(TAG, "mRollo.mIcons=" + mIcons);
1369 if (mIcons != null) {
1370 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1371 }
1372 if (mIconIds != null) {
1373 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1374 }
1375 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1376 if (mLabelIds != null) {
1377 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1378 }
1379 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1380 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1381 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
Joe Onoratobe386092009-11-17 17:32:16 -08001382 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1383 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1384 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1385 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1386 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1387 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1388 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1389 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1390 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1391 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1392 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1393 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1394 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1395 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1396 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1397 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1398 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1399 }
1400 }
1401
1402 public void dumpState() {
1403 Log.d(TAG, "mRS=" + mRS);
1404 Log.d(TAG, "mRollo=" + mRollo);
1405 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1406 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1407 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1408 Log.d(TAG, "mLastSelection=" + mLastSelection);
1409 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1410 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1411 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1412 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1413 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1414 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1415 Log.d(TAG, "mZoom=" + mZoom);
1416 Log.d(TAG, "mPosX=" + mPosX);
1417 Log.d(TAG, "mVelocity=" + mVelocity);
1418 Log.d(TAG, "mMessageProc=" + mMessageProc);
1419 if (mRollo != null) {
1420 mRollo.dumpState();
1421 }
1422 if (mRS != null) {
1423 mRS.contextDump(0);
1424 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001425 }
Joe Onorato93839052009-08-06 20:34:32 -07001426}
1427
1428