blob: 999844d1ebd39f5ed8c324cc02d406cf99547e34 [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 Onorato6374c512010-01-06 20:42:25 -0800117 private boolean mHaveSurface = false;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800118 private boolean mZoomDirty = false;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800119 private boolean mAnimateNextZoom;
Joe Onorato6374c512010-01-06 20:42:25 -0800120 private float mNextZoom;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800121 private float mZoom;
122 private float mPosX;
123 private float mVelocity;
124 private AAMessage mMessageProc;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700125
Joe Onorato6665c0f2009-09-02 15:27:24 -0700126 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700127 public static final int ALLOC_PARAMS = 0;
128 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700129 public static final int ALLOC_ICON_IDS = 3;
130 public static final int ALLOC_LABEL_IDS = 4;
Jason Samsb4ecab22010-01-19 16:43:26 -0800131 public static final int ALLOC_VP_CONSTANTS = 5;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700132
133 public static final int COLUMNS_PER_PAGE = 4;
134 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700135
Joe Onorato6665c0f2009-09-02 15:27:24 -0700136 public static final int ICON_WIDTH_PX = 64;
Jason Sams1a94ee32010-01-20 13:34:30 -0800137 public static final int ICON_TEXTURE_WIDTH_PX = 74;
138 public static final int SELECTION_TEXTURE_WIDTH_PX = 74 + 20;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700139
140 public static final int ICON_HEIGHT_PX = 64;
Jason Sams1a94ee32010-01-20 13:34:30 -0800141 public static final int ICON_TEXTURE_HEIGHT_PX = 74;
142 public static final int SELECTION_TEXTURE_HEIGHT_PX = 74 + 20;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700143
144 public int SCREEN_WIDTH_PX;
145 public int SCREEN_HEIGHT_PX;
146
Joe Onoratobcbeab82009-10-01 21:45:43 -0700147 public void recompute(int w, int h) {
148 SCREEN_WIDTH_PX = 480;
149 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700150 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700151 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700152
153 public AllAppsView(Context context, AttributeSet attrs) {
154 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700155 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400156 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700157 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700158 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700159 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700160 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
161
Joe Onorato6665c0f2009-09-02 15:27:24 -0700162 setOnClickListener(this);
163 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700164 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700165 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samse26d9fc2009-11-12 14:00:43 -0800166
167 mRS = createRenderScript(true);
168 }
169
170 @Override
171 protected void onDetachedFromWindow() {
172 destroyRenderScript();
Joe Onorato93839052009-08-06 20:34:32 -0700173 }
174
Joe Onoratob39e51a2009-10-28 15:47:49 -0400175 /**
176 * If you have an attached click listener, View always plays the click sound!?!?
177 * Deal with sound effects by hand.
178 */
179 public void reallyPlaySoundEffect(int sound) {
180 boolean old = isSoundEffectsEnabled();
181 setSoundEffectsEnabled(true);
182 playSoundEffect(sound);
183 setSoundEffectsEnabled(old);
184 }
185
Joe Onorato7c312c12009-08-13 21:36:53 -0700186 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
187 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700188 }
189
Joe Onorato6665c0f2009-09-02 15:27:24 -0700190 public void setLauncher(Launcher launcher) {
191 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700192 }
193
Joe Onorato1feb3a82009-08-08 22:32:00 -0700194 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700195 public void surfaceDestroyed(SurfaceHolder holder) {
196 super.surfaceDestroyed(holder);
Joe Onoratofab74402009-11-11 16:05:23 -0800197 // Without this, we leak mMessageCallback which leaks the context.
198 mRS.mMessageCallback = null;
Joe Onorato6374c512010-01-06 20:42:25 -0800199 // We may lose any callbacks that are pending, so make sure that we re-sync that
200 // on the next surfaceChanged.
201 mZoomDirty = true;
202 mHaveSurface = false;
Joe Onorato7bb17492009-09-24 17:51:01 -0700203 }
204
205 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700206 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800207 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700208
Joe Onorato080d9b62009-11-02 12:01:11 -0500209 super.surfaceChanged(holder, format, w, h);
210
Joe Onorato6374c512010-01-06 20:42:25 -0800211 mHaveSurface = true;
212
Jason Samse26d9fc2009-11-12 14:00:43 -0800213 if (mRollo == null) {
Jason Sams90396672009-11-03 13:59:34 -0800214 mRollo = new RolloRS();
215 mRollo.init(getResources(), w, h);
216 if (mAllAppsList != null) {
217 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800218 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800219 if (mShouldGainFocus) {
220 gainFocus();
221 mShouldGainFocus = false;
222 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400223 }
Jason Samse26d9fc2009-11-12 14:00:43 -0800224 mRollo.dirtyCheck();
Jason Sams5612e432009-11-16 14:18:07 -0800225 mRollo.resize(w, h);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700226
Joe Onoratocb75f362009-11-12 13:04:07 -0800227 mRS.mMessageCallback = mMessageProc = new AAMessage();
228
Joe Onoratoc567acb2009-08-31 14:34:43 -0700229 Resources res = getContext().getResources();
230 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700231
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800232 //long endTime = SystemClock.uptimeMillis();
233 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700234 }
Jason Sams2e19c052009-10-20 18:19:55 -0700235
Joe Onorato93839052009-08-06 20:34:32 -0700236 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800237 public void onWindowFocusChanged(boolean hasWindowFocus) {
238 super.onWindowFocusChanged(hasWindowFocus);
239 if (mArrowNavigation) {
240 if (!hasWindowFocus) {
241 // Clear selection when we lose window focus
242 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500243 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800244 mRollo.clearSelectedIcon();
245 mRollo.mState.save();
246 } else if (hasWindowFocus) {
247 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500248 if (mLastSelection == SELECTION_ICONS) {
249 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800250 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500251 Defines.COLUMNS_PER_PAGE;
252 if (selection < 0 || // No selection
253 selection < firstIcon || // off the top of the screen
254 selection >= mRollo.mState.iconCount || // past last icon
255 selection >= firstIcon + // past last icon on screen
256 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
257 selection = firstIcon;
258 }
259
260 // Select the first icon when we gain window focus
261 mRollo.selectIcon(selection, SELECTED_FOCUSED);
262 mRollo.mState.save();
263 } else if (mLastSelection == SELECTION_HOME) {
264 mRollo.setHomeSelected(SELECTED_FOCUSED);
265 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800266 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800267 }
268 }
269 }
270 }
271
272 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700273 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
274 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400275
276 if (!isVisible()) {
277 return;
278 }
279
Mike Cleron7d5d7462009-10-20 14:06:00 -0700280 if (gainFocus) {
Jason Sams510ee042009-12-15 14:23:37 -0800281 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800282 gainFocus();
283 } else {
284 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700285 }
286 } else {
Joe Onorato8eea3912009-12-09 13:01:06 -0800287 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800288 if (mArrowNavigation) {
289 // Clear selection when we lose focus
290 mRollo.clearSelectedIcon();
291 mRollo.setHomeSelected(SELECTED_NONE);
292 mRollo.mState.save();
293 mArrowNavigation = false;
294 }
295 } else {
296 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700297 }
298 }
299 }
300
Mike Cleronb64b67a2009-11-08 14:56:25 -0800301 private void gainFocus() {
302 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
303 // Select the first icon when we gain keyboard focus
304 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800305 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800306 SELECTED_FOCUSED);
307 mRollo.mState.save();
308 }
309 }
310
Mike Cleron7d5d7462009-10-20 14:06:00 -0700311 @Override
312 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700313
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800314 boolean handled = false;
Jason Samse26d9fc2009-11-12 14:00:43 -0800315
Joe Onorato859b3a72009-10-28 15:17:01 -0400316 if (!isVisible()) {
317 return false;
318 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500319 final int iconCount = mRollo.mState.iconCount;
320
Mike Cleron7d5d7462009-10-20 14:06:00 -0700321 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
322 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500323 if (mLastSelection == SELECTION_HOME) {
324 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
325 mLauncher.closeAllApps(true);
326 } else {
327 int whichApp = mRollo.mState.selectedIconIndex;
328 if (whichApp >= 0) {
329 ApplicationInfo app = mAllAppsList.get(whichApp);
330 mLauncher.startActivitySafely(app.intent);
331 handled = true;
332 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700333 }
334 }
335 }
Jason Sams2e19c052009-10-20 18:19:55 -0700336
Joe Onorato478730f2009-11-16 18:54:43 -0800337 if (iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700338 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700339
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800340 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800341 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700342
Mike Cleron7d5d7462009-10-20 14:06:00 -0700343 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500344 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700345
Mike Cleron7d5d7462009-10-20 14:06:00 -0700346 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500347 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700348 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700349
Mike Cleron7d5d7462009-10-20 14:06:00 -0700350 int newSelection = currentSelection;
351
352 switch (keyCode) {
353 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500354 if (mLastSelection == SELECTION_HOME) {
355 mRollo.setHomeSelected(SELECTED_NONE);
356 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
357 if (lastRowCount == 0) {
358 lastRowCount = Defines.COLUMNS_PER_PAGE;
359 }
360 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
361 if (newSelection >= iconCount) {
362 newSelection = iconCount-1;
363 }
364 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
365 - (Defines.ROWS_PER_PAGE - 1);
366 if (target < 0) {
367 target = 0;
368 }
369 if (currentTopRow != target) {
370 mRollo.moveTo(target);
371 }
372 } else {
373 if (currentPageRow > 0) {
374 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
375 } else if (currentTopRow > 0) {
376 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
377 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
Joe Onoratoaf5b4cb2009-12-08 17:51:22 -0800378 } else if (currentPageRow != 0) {
379 newSelection = currentTopRow * Defines.ROWS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500380 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700381 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800382 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700383 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800384
Joe Onoratoa13f5742009-11-02 17:15:19 -0500385 case KeyEvent.KEYCODE_DPAD_DOWN: {
386 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
387 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
388 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500389 if (mLastSelection != SELECTION_HOME) {
390 if (currentRow < rowCount-1) {
391 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato478730f2009-11-16 18:54:43 -0800392 if (currentSelection < 0) {
393 newSelection = 0;
394 } else {
395 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
396 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500397 if (newSelection >= iconCount) {
398 // Go from D to G in this arrangement:
399 // A B C D
400 // E F G
401 newSelection = iconCount - 1;
402 }
403 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
404 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
405 Defines.ROWS_PER_PAGE + 1);
406 }
407 } else {
408 newSelection = -1;
409 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700410 }
411 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800412 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700413 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500414 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700415 case KeyEvent.KEYCODE_DPAD_LEFT:
416 if (currentPageCol > 0) {
417 newSelection = currentSelection - 1;
418 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800419 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700420 break;
421 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700422 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500423 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700424 newSelection = currentSelection + 1;
425 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800426 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700427 break;
428 }
429 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500430 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700431 mRollo.mState.save();
432 }
433 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800434 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700435 }
436
Joe Onorato93839052009-08-06 20:34:32 -0700437 @Override
438 public boolean onTouchEvent(MotionEvent ev)
439 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700440 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700441
Joe Onorato7bb17492009-09-24 17:51:01 -0700442 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700443 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700444 }
445
Joe Onoratofb0ca672009-09-14 17:55:46 -0400446 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700447 return true;
448 }
449
450 super.onTouchEvent(ev);
451
Joe Onoratofb0ca672009-09-14 17:55:46 -0400452 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700453 int y = (int)ev.getY();
454
Joe Onoratobcbeab82009-10-01 21:45:43 -0700455 int action = ev.getAction();
456 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400457 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700458 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
459 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500460 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700461 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700462 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700463 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700464 mTouchTracking = TRACKING_FLING;
465
466 mMotionDownRawX = (int)ev.getRawX();
467 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700468
Mike Cleron7d5d7462009-10-20 14:06:00 -0700469 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700470 mRollo.mState.newTouchDown = 1;
471
472 if (!mRollo.checkClickOK()) {
473 mRollo.clearSelectedIcon();
474 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700475 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800476 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700477 if (mDownIconIndex < 0) {
478 // if nothing was selected, no long press.
479 cancelLongPress();
480 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700481 }
482 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700483 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800484 mVelocityTracker = VelocityTracker.obtain();
485 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700486 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700487 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400488 break;
489 case MotionEvent.ACTION_MOVE:
490 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700491 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500492 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
493 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700494 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700495 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700496 int rawX = (int)ev.getRawX();
497 int rawY = (int)ev.getRawY();
498 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700499 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700500
Joe Onorato82ca5502009-10-15 16:59:23 -0700501 if (!mStartedScrolling && slop < mSlop) {
502 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700503 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800504 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700505 if (mDownIconIndex != mCurrentIconIndex) {
506 // If a different icon is selected, don't allow it to be picked up.
507 // This handles off-axis dragging.
508 cancelLongPress();
509 mCurrentIconIndex = -1;
510 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700511 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700512 if (!mStartedScrolling) {
513 cancelLongPress();
514 mCurrentIconIndex = -1;
515 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700516 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700517 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700518 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700519
Joe Onoratobcbeab82009-10-01 21:45:43 -0700520 mStartedScrolling = true;
521 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800522 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700523 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700524 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400525 }
526 break;
527 case MotionEvent.ACTION_UP:
528 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700529 if (mTouchTracking == TRACKING_HOME) {
530 if (action == MotionEvent.ACTION_UP) {
531 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400532 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700533 mLauncher.closeAllApps(true);
534 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500535 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700536 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700537 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700538 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700539 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700540 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700541 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700542
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800543 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
544 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700545 mRollo.clearSelectedIcon();
546 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700547 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700548
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800549 if (mVelocityTracker != null) {
550 mVelocityTracker.recycle();
551 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700552 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700553 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700554 mTouchTracking = TRACKING_NONE;
555 break;
Joe Onorato93839052009-08-06 20:34:32 -0700556 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700557
558 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700559 }
560
Joe Onorato6665c0f2009-09-02 15:27:24 -0700561 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700562 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400563 return;
564 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700565 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
566 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400567 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700568 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700569 mLauncher.startActivitySafely(app.intent);
570 }
571 }
572
573 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700574 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400575 return true;
576 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700577 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
578 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
579 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700580
Jason Samsb4ecab22010-01-19 16:43:26 -0800581 Bitmap bmp = app.iconBitmap;
582 final int w = bmp.getWidth();
583 final int h = bmp.getHeight();
584
Joe Onorato5162ea92009-09-03 09:39:42 -0700585 // We don't really have an accurate location to use. This will do.
Jason Samsb4ecab22010-01-19 16:43:26 -0800586 int screenX = mMotionDownRawX - (w / 2);
587 int screenY = mMotionDownRawY - h;
Joe Onorato5162ea92009-09-03 09:39:42 -0700588
Joe Onoratobcbeab82009-10-01 21:45:43 -0700589 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
590 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Jason Samsb4ecab22010-01-19 16:43:26 -0800591 mDragController.startDrag(bmp, screenX, screenY,
592 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700593
Joe Onorato7bb17492009-09-24 17:51:01 -0700594 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700595 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700596 return true;
597 }
598
Joe Onorato52a653f2009-11-11 14:52:11 -0800599 @Override
600 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
601 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
602 if (!isVisible()) {
603 return false;
604 }
605 String text = null;
606 int index;
607 int count = mAllAppsList.size() + 1; // +1 is home
608 int pos = -1;
609 switch (mLastSelection) {
610 case SELECTION_ICONS:
611 index = mRollo.mState.selectedIconIndex;
612 if (index >= 0) {
613 ApplicationInfo info = mAllAppsList.get(index);
614 if (info.title != null) {
615 text = info.title.toString();
616 pos = index;
617 }
618 }
619 break;
620 case SELECTION_HOME:
621 text = getContext().getString(R.string.all_apps_home_button_label);
622 pos = count;
623 break;
624 }
625 if (text != null) {
Joe Onorato52a653f2009-11-11 14:52:11 -0800626 event.setEnabled(true);
627 event.getText().add(text);
628 //event.setContentDescription(text);
629 event.setItemCount(count);
630 event.setCurrentItemIndex(pos);
631 }
632 }
633 return false;
634 }
635
Joe Onorato5162ea92009-09-03 09:39:42 -0700636 public void setDragController(DragController dragger) {
637 mDragController = dragger;
638 }
639
640 public void onDropCompleted(View target, boolean success) {
641 }
642
Joe Onorato4db52312009-10-06 11:17:43 -0700643 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800644 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700645 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800646 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700647 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800648 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400649 cancelLongPress();
Joe Onorato6374c512010-01-06 20:42:25 -0800650 mNextZoom = zoom;
651 mAnimateNextZoom = animate;
652 // if we do setZoom while we don't have a surface, we won't
653 // get the callbacks that actually set mZoom.
654 if (mRollo == null || !mHaveSurface) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800655 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800656 mZoom = zoom;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800657 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700658 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800659 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700660 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400661 }
662
663 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800664 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800665 }
666
667 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800668 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400669 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700670
Joe Onorato9c1289c2009-08-17 11:03:03 -0400671 public void setApps(ArrayList<ApplicationInfo> list) {
672 mAllAppsList = list;
673 if (mRollo != null) {
Joe Onorato3ecbd812009-12-11 13:38:54 -0800674 mRollo.setApps(list);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400675 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400676 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700677 }
678
Joe Onoratoa8138d52009-10-06 19:25:30 -0700679 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500680 if (mAllAppsList == null) {
681 // Not done loading yet. We'll find out about it later.
682 return;
683 }
684
Joe Onoratoa8138d52009-10-06 19:25:30 -0700685 final int N = list.size();
Joe Onorato8eea3912009-12-09 13:01:06 -0800686 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700687 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
688 }
689
690 for (int i=0; i<N; i++) {
691 final ApplicationInfo item = list.get(i);
Joe Onoratob0c27f22009-12-01 16:19:38 -0800692 int index = Collections.binarySearch(mAllAppsList, item,
693 LauncherModel.APP_NAME_COMPARATOR);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700694 if (index < 0) {
695 index = -(index+1);
696 }
697 mAllAppsList.add(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -0800698 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700699 mRollo.addApp(index, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700700 }
701 }
702
Joe Onorato8eea3912009-12-09 13:01:06 -0800703 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700704 mRollo.saveAppsList();
705 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700706 }
707
Joe Onoratoa8138d52009-10-06 19:25:30 -0700708 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500709 if (mAllAppsList == null) {
710 // Not done loading yet. We'll find out about it later.
711 return;
712 }
713
Joe Onoratoa8138d52009-10-06 19:25:30 -0700714 final int N = list.size();
715 for (int i=0; i<N; i++) {
716 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400717 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700718 if (index >= 0) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800719 int ic = mRollo != null ? mRollo.mState.iconCount : 666;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700720 mAllAppsList.remove(index);
Joe Onorato8eea3912009-12-09 13:01:06 -0800721 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700722 mRollo.removeApp(index);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700723 }
724 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800725 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700726 // Try to recover. This should keep us from crashing for now.
727 }
728 }
729
Joe Onorato8eea3912009-12-09 13:01:06 -0800730 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700731 mRollo.saveAppsList();
732 }
733 }
734
735 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
736 // Just remove and add, because they may need to be re-sorted.
737 removeApps(list);
738 addApps(list);
739 }
740
Joe Onoratocb9f7982009-10-31 16:32:02 -0400741 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
742 ComponentName component = item.intent.getComponent();
743 final int N = list.size();
744 for (int i=0; i<N; i++) {
745 ApplicationInfo x = list.get(i);
746 if (x.intent.getComponent().equals(component)) {
747 return i;
748 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700749 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400750 return -1;
751 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700752
Joe Onoratoc567acb2009-08-31 14:34:43 -0700753 private static int countPages(int iconCount) {
754 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
755 int pages = iconCount / iconsPerPage;
756 if (pages*iconsPerPage != iconCount) {
757 pages++;
758 }
759 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760 }
761
Joe Onoratocb75f362009-11-12 13:04:07 -0800762 class AAMessage extends RenderScript.RSMessage {
763 public void run() {
764 mPosX = ((float)mData[0]) / (1 << 16);
765 mVelocity = ((float)mData[1]) / (1 << 16);
766 mZoom = ((float)mData[2]) / (1 << 16);
767 mZoomDirty = false;
768 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800769 }
770
Joe Onorato93839052009-08-06 20:34:32 -0700771 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700772
Joe Onorato1feb3a82009-08-08 22:32:00 -0700773 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700774 private int mWidth;
775 private int mHeight;
776
777 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700778 private Script mScript;
779 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700780 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700781 private Script.Invokable mInvokeFling;
782 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800783 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700784
Jason Samscd689e12009-09-29 15:28:22 -0700785 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700786 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700787 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700788 private ProgramFragment mPFTexMip;
Jason Sams6ec11bc2010-01-19 17:56:52 -0800789 private ProgramFragment mPFTexMipAlpha;
Jason Samsc8514792009-10-29 14:27:29 -0700790 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700791 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700792 private ProgramVertex mPVOrtho;
Jason Samsb4ecab22010-01-19 16:43:26 -0800793 private ProgramVertex mPVCurve;
Jason Sams0aa71662009-10-02 18:43:18 -0700794 private SimpleMesh mMesh;
Jason Sams5612e432009-11-16 14:18:07 -0800795 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700796
Jason Samsb4ecab22010-01-19 16:43:26 -0800797 private Allocation mUniformAlloc;
798
Joe Onoratod63458b2009-10-15 21:19:09 -0700799 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500800 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700801 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700802
Joe Onoratobf15cb42009-08-07 14:33:40 -0700803 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700804 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700805 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700806
Joe Onoratobf15cb42009-08-07 14:33:40 -0700807 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700808 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700809 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700810 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700811
Joe Onorato6665c0f2009-09-02 15:27:24 -0700812 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700813 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700814
815 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400816 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700817
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700818 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700819 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700820
Jason Sams78aebd82009-09-15 13:06:59 -0700821 class BaseAlloc {
822 Allocation mAlloc;
823 Type mType;
824
825 void save() {
826 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700827 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700828 }
829
Jason Sams476339d2009-09-29 18:14:38 -0700830 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800831 return (Math.abs(mVelocity) < 0.4f) &&
832 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700833 }
834
Jason Sams78aebd82009-09-15 13:06:59 -0700835 class Params extends BaseAlloc {
836 Params() {
837 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
838 mAlloc = Allocation.createTyped(mRS, mType);
839 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700840 }
Jason Sams78aebd82009-09-15 13:06:59 -0700841 public int bubbleWidth;
842 public int bubbleHeight;
843 public int bubbleBitmapWidth;
844 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700845
Joe Onoratobcbeab82009-10-01 21:45:43 -0700846 public int homeButtonWidth;
847 public int homeButtonHeight;
848 public int homeButtonTextureWidth;
849 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700850 }
851
852 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700853 public float newPositionX;
854 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700855 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700856 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700857 public int selectedIconIndex = -1;
858 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700859 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700860 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700861 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700862
863 State() {
864 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
865 mAlloc = Allocation.createTyped(mRS, mType);
866 save();
867 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700868 }
869
870 public RolloRS() {
871 }
872
873 public void init(Resources res, int width, int height) {
874 mRes = res;
875 mWidth = width;
876 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700877 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700878 initProgramVertex();
879 initProgramFragment();
880 initProgramStore();
Jason Samsb4ecab22010-01-19 16:43:26 -0800881 //initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700882 initGl();
883 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700884 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700885 initRs();
886 }
887
Jason Samsb4ecab22010-01-19 16:43:26 -0800888 public void initMesh2() {
889 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Jason Sams0aa71662009-10-02 18:43:18 -0700890
Jason Samsb4ecab22010-01-19 16:43:26 -0800891 for (int ct=0; ct < 16; ct++) {
892 float pos = (1.f / 16.f) * ct;
893 tm.addVertex(0.0f, pos);
894 tm.addVertex(1.0f, pos);
Jason Samsd8152b92009-10-13 17:19:10 -0700895 }
Jason Samsb4ecab22010-01-19 16:43:26 -0800896 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
Jason Samsd8152b92009-10-13 17:19:10 -0700897 tm.addTriangle(ct, ct+1, ct+2);
898 tm.addTriangle(ct+1, ct+3, ct+2);
899 }
Jason Samsb4ecab22010-01-19 16:43:26 -0800900 mMesh = tm.create();
901 mMesh.setName("SMCell");
Jason Samsd8152b92009-10-13 17:19:10 -0700902 }
903
Jason Sams5612e432009-11-16 14:18:07 -0800904 void resize(int w, int h) {
905 mPVA.setupProjectionNormalized(w, h);
906 mWidth = w;
907 mHeight = h;
908 }
909
Jason Samscd689e12009-09-29 15:28:22 -0700910 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800911 mPVA = new ProgramVertex.MatrixAllocation(mRS);
912 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700913
914 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700915 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700916 mPV = pvb.create();
917 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800918 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700919
Jason Samsb4ecab22010-01-19 16:43:26 -0800920 Element.Builder eb = new Element.Builder(mRS);
921 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
922 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
923 Element e = eb.create();
924
925 mUniformAlloc = Allocation.createSized(mRS, e, 1);
926
927 initMesh2();
928 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
929 String t = new String("void main() {\n" +
930 // Animation
931 " float ani = UNI_Position.z;\n" +
932
933 " float scale = (2.0 / 480.0);\n" +
934 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
935 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
936 " float y = 0.0;\n" +
937 " float z = 0.0;\n" +
938 " float lum = 1.0;\n" +
939
940 " float cv = min(ys, 50.0) - 50.0;\n" +
941 " y += cv * 0.681;\n" + // Roughy 47 degrees
942 " z += -cv * 0.731;\n" +
943 " cv = clamp(ys, 50.0, 120.0) - 120.0;\n" + // curve range
944 " y += cv * cos(cv * 0.4 / (180.0 / 3.14));\n" +
945 " z += cv * sin(cv * 0.4 / (180.0 / 3.14));\n" +
946
947 " cv = max(ys, 750.0) - 750.0;\n" +
948 " y += cv * 0.681;\n" +
949 " z += cv * 0.731;\n" +
950 " cv = clamp(ys, 680.0, 750.0) - 680.0;\n" +
951 " y += cv * cos(cv * 0.4 / (180.0 / 3.14));\n" +
952 " z += cv * sin(cv * 0.4 / (180.0 / 3.14));\n" +
953
954 " y += clamp(ys, 120.0, 680.0);\n" +
955 " lum += (clamp(ys, 60.0, 115.0) - 115.0) / 100.0;\n" +
956 " lum -= (clamp(ys, 685.0, 740.0) - 685.0) / 100.0;\n" +
957
958 " vec4 pos;\n" +
959 " pos.x = x * scale - 1.0;\n" +
960 " pos.y = y * scale - 1.66;\n" +
961 " pos.z = z * scale;\n" +
962 " pos.w = 1.0;\n" +
963
964 " pos.x *= 1.0 + ani * 4.0;\n" +
965 " pos.y *= 1.0 + ani * 4.0;\n" +
966 " pos.z -= ani * 1.5;\n" +
967 " lum *= 1.0 - ani;\n" +
968
969 " gl_Position = UNI_MVP * pos;\n" +
970 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
971 " varTex0.xy = ATTRIB_position;\n" +
972 " varTex0.y = 1.0 - varTex0.y;\n" +
973 " varTex0.zw = vec2(0.0, 0.0);\n" +
974 "}\n");
975 sb.setShader(t);
976 sb.addConstant(mUniformAlloc.getType());
977 sb.addInput(mMesh.getVertexType(0).getElement());
978 mPVCurve = sb.create();
979 mPVCurve.setName("PVCurve");
980 mPVCurve.bindAllocation(mPVA);
981 mPVCurve.bindConstants(mUniformAlloc, 1);
982
983 float tf[] = new float[] {72.f, 72.f, 0.f, 0.f, 120.f, 120.f, 0.f, 0.f};
984 mUniformAlloc.data(tf);
985
Jason Sams37e7c2b2009-10-19 12:55:43 -0700986 //pva = new ProgramVertex.MatrixAllocation(mRS);
987 //pva.setupOrthoWindow(mWidth, mHeight);
988 //pvb.setTextureMatrixEnable(true);
989 //mPVOrtho = pvb.create();
990 //mPVOrtho.setName("PVOrtho");
991 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700992
993 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700994 }
Joe Onorato93839052009-08-06 20:34:32 -0700995
Jason Samscd689e12009-09-29 15:28:22 -0700996 private void initProgramFragment() {
997 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700998 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -0800999 sb.setMag(Sampler.Value.NEAREST);
Jason Samscd689e12009-09-29 15:28:22 -07001000 sb.setWrapS(Sampler.Value.CLAMP);
1001 sb.setWrapT(Sampler.Value.CLAMP);
1002 Sampler linear = sb.create();
1003
1004 sb.setMin(Sampler.Value.NEAREST);
1005 sb.setMag(Sampler.Value.NEAREST);
1006 Sampler nearest = sb.create();
1007
Jason Sams72f1d312009-12-17 16:58:25 -08001008 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001009 //mPFColor = bf.create();
1010 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -07001011
Jason Samsb4ecab22010-01-19 16:43:26 -08001012 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
Jason Sams72f1d312009-12-17 16:58:25 -08001013 ProgramFragment.Builder.Format.RGBA, 0);
Jason Samsc8514792009-10-29 14:27:29 -07001014 mPFTexMip = bf.create();
1015 mPFTexMip.setName("PFTexMip");
1016 mPFTexMip.bindSampler(linear, 0);
1017
1018 mPFTexNearest = bf.create();
1019 mPFTexNearest.setName("PFTexNearest");
1020 mPFTexNearest.bindSampler(nearest, 0);
Jason Sams6ec11bc2010-01-19 17:56:52 -08001021
1022 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1023 ProgramFragment.Builder.Format.ALPHA, 0);
1024 mPFTexMipAlpha = bf.create();
1025 mPFTexMipAlpha.setName("PFTexMipAlpha");
1026 mPFTexMipAlpha.bindSampler(linear, 0);
1027
Jason Samscd689e12009-09-29 15:28:22 -07001028 }
1029
1030 private void initProgramStore() {
1031 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1032 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -07001033 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -07001034 bs.setDitherEnable(true);
1035 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1036 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1037 mPSIcons = bs.create();
1038 mPSIcons.setName("PSIcons");
1039
1040 //bs.setDitherEnable(false);
1041 //mPSText = bs.create();
1042 //mPSText.setName("PSText");
1043 }
1044
1045 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001046 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -07001047 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -07001048 }
Jason Sams78aebd82009-09-15 13:06:59 -07001049
Joe Onorato1feb3a82009-08-08 22:32:00 -07001050 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -07001051 mParams = new Params();
1052 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001053
Joe Onoratobf15cb42009-08-07 14:33:40 -07001054 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -07001055
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001056 mParams.bubbleWidth = bubble.getBubbleWidth();
1057 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1058 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1059 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001060
Joe Onoratod63458b2009-10-15 21:19:09 -07001061 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1062 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1063 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001064 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1065 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1066 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001067 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1068 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1069 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001070 mParams.homeButtonWidth = 76;
1071 mParams.homeButtonHeight = 68;
1072 mParams.homeButtonTextureWidth = 128;
1073 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001074
Joe Onoratod63458b2009-10-15 21:19:09 -07001075 mState.homeButtonId = mHomeButtonNormal.getID();
1076
Joe Onorato1feb3a82009-08-08 22:32:00 -07001077 mParams.save();
1078 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001079
Jason Sams1a94ee32010-01-20 13:34:30 -08001080 mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1081 Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001082 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001083
Joe Onorato9c1289c2009-08-17 11:03:03 -04001084 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001085 }
1086
Jason Sams37e7c2b2009-10-19 12:55:43 -07001087 private void initScript(int id) {
1088 }
1089
1090 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001091 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Samsd15f3ef2010-01-06 14:58:06 -08001092 sb.setScript(mRes, R.raw.allapps);
Joe Onorato93839052009-08-06 20:34:32 -07001093 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001094 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001095 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1096 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Samsb4ecab22010-01-19 16:43:26 -08001097 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001098 mInvokeMove = sb.addInvokable("move");
1099 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001100 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001101 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001102 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001103 mScript = sb.create();
1104 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1105 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1106 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1107 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1108 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Jason Samsb4ecab22010-01-19 16:43:26 -08001109 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Joe Onorato93839052009-08-06 20:34:32 -07001110
Jason Sams37e7c2b2009-10-19 12:55:43 -07001111 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001112 }
Joe Onorato93839052009-08-06 20:34:32 -07001113
Jason Sams20df7c72009-11-05 12:30:24 -08001114 void dirtyCheck() {
Jason Sams510ee042009-12-15 14:23:37 -08001115 if (mZoomDirty) {
Joe Onorato6374c512010-01-06 20:42:25 -08001116 setZoom(mNextZoom, mAnimateNextZoom);
Jason Sams20df7c72009-11-05 12:30:24 -08001117 }
1118 }
1119
Joe Onorato9c1289c2009-08-17 11:03:03 -04001120 private void setApps(ArrayList<ApplicationInfo> list) {
1121 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001122 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001123 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001124 allocCount = 1;
1125 }
1126
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001128 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001129 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130
1131 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001132 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001133 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001135 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136
Joe Onorato9c1289c2009-08-17 11:03:03 -04001137 mState.iconCount = count;
Joe Onorato8eea3912009-12-09 13:01:06 -08001138 for (int i=0; i < mState.iconCount; i++) {
1139 createAppIconAllocations(i, list.get(i));
Joe Onorato3ecbd812009-12-11 13:38:54 -08001140 }
Jason Sams510ee042009-12-15 14:23:37 -08001141 for (int i=0; i < mState.iconCount; i++) {
1142 uploadAppIcon(i, list.get(i));
Joe Onorato8eea3912009-12-09 13:01:06 -08001143 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001144 saveAppsList();
1145 }
1146
Joe Onorato3a8820b2009-11-10 15:06:42 -08001147 private void setZoom(float zoom, boolean animate) {
1148 mRollo.clearSelectedIcon();
1149 mRollo.setHomeSelected(SELECTED_NONE);
1150 if (zoom > 0.001f) {
1151 mRollo.mState.zoomTarget = zoom;
1152 } else {
1153 mRollo.mState.zoomTarget = 0;
1154 }
1155 mRollo.mState.save();
1156 if (!animate) {
1157 mRollo.mInvokeSetZoom.execute();
1158 }
1159 }
1160
Joe Onorato8eea3912009-12-09 13:01:06 -08001161 private void createAppIconAllocations(int index, ApplicationInfo item) {
Joe Onoratoa8138d52009-10-06 19:25:30 -07001162 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001163 Element.RGBA_8888(mRS), true);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001164 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Sams6ec11bc2010-01-19 17:56:52 -08001165 Element.A_8(mRS), true);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001166 mIconIds[index] = mIcons[index].getID();
1167 mLabelIds[index] = mLabels[index].getID();
1168 }
1169
Joe Onorato8eea3912009-12-09 13:01:06 -08001170 private void uploadAppIcon(int index, ApplicationInfo item) {
1171 if (mIconIds[index] != mIcons[index].getID()) {
1172 throw new IllegalStateException("uploadAppIcon index=" + index
1173 + " mIcons[index].getID=" + mIcons[index].getID()
1174 + " mIconsIds[index]=" + mIconIds[index]
1175 + " item=" + item);
1176 }
1177 mIcons[index].uploadToTexture(0);
1178 mLabels[index].uploadToTexture(0);
1179 }
1180
Joe Onoratoa8138d52009-10-06 19:25:30 -07001181 /**
1182 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1183 * fill in the values and call saveAppsList().
1184 */
1185 private void reallocAppsList(int count) {
1186 Allocation[] icons = new Allocation[count];
1187 int[] iconIds = new int[count];
1188 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1189
1190 Allocation[] labels = new Allocation[count];
1191 int[] labelIds = new int[count];
1192 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1193
Joe Onoratobf173f12009-12-08 13:29:38 -08001194 final int oldCount = mRollo.mState.iconCount;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001195
1196 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1197 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1198 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1199 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1200
1201 mIcons = icons;
1202 mIconIds = iconIds;
1203 mLabels = labels;
1204 mLabelIds = labelIds;
1205 }
1206
1207 /**
1208 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1209 */
1210 private void addApp(int index, ApplicationInfo item) {
1211 final int count = mState.iconCount - index;
1212 final int dest = index + 1;
1213
1214 System.arraycopy(mIcons, index, mIcons, dest, count);
1215 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1216 System.arraycopy(mLabels, index, mLabels, dest, count);
1217 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1218
Joe Onorato8eea3912009-12-09 13:01:06 -08001219 createAppIconAllocations(index, item);
Jason Sams510ee042009-12-15 14:23:37 -08001220 uploadAppIcon(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -08001221 mRollo.mState.iconCount++;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001222 }
1223
1224 /**
1225 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1226 */
1227 private void removeApp(int index) {
1228 final int count = mState.iconCount - index - 1;
1229 final int src = index + 1;
1230
1231 System.arraycopy(mIcons, src, mIcons, index, count);
1232 System.arraycopy(mIconIds, src, mIconIds, index, count);
1233 System.arraycopy(mLabels, src, mLabels, index, count);
1234 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1235
Joe Onoratoa276fc52009-12-08 17:02:02 -08001236 mRollo.mState.iconCount--;
Joe Onorato8eea3912009-12-09 13:01:06 -08001237 final int last = mState.iconCount;
Joe Onoratoa276fc52009-12-08 17:02:02 -08001238
Joe Onoratoa8138d52009-10-06 19:25:30 -07001239 mIcons[last] = null;
1240 mIconIds[last] = 0;
1241 mLabels[last] = null;
1242 mLabelIds[last] = 0;
1243 }
1244
1245 /**
1246 * Send the apps list structures to RS.
1247 */
1248 private void saveAppsList() {
1249 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001250
Joe Onoratoa8138d52009-10-06 19:25:30 -07001251 mAllocIconIds.data(mIconIds);
1252 mAllocLabelIds.data(mLabelIds);
1253
Jason Sams37e7c2b2009-10-19 12:55:43 -07001254 if (mScript != null) { // this happens when we init it
1255 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1256 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001257 }
1258
1259 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001260
1261 // Note: mScript may be null if we haven't initialized it yet.
1262 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001263 if (mInvokeResetWAR != null) {
1264 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001265 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001266 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001267 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001268
1269 void initTouchState() {
1270 int width = getWidth();
1271 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001272 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1273 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001274
Jason Sams37e7c2b2009-10-19 12:55:43 -07001275 int centerY = (height / 2);
1276 mTouchYBorders[0] = centerY - (cellHeight * 2);
1277 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001278 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001279 mTouchYBorders[3] = centerY + cellHeight;
1280 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001281
Joe Onorato6665c0f2009-09-02 15:27:24 -07001282 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001283 mTouchXBorders[0] = 0;
1284 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001285 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001286 mTouchXBorders[3] = centerX + (width / 4);
1287 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001288 }
1289
Joe Onorato664457d2009-10-28 16:30:34 -04001290 void fling() {
1291 mInvokeFling.execute();
1292 }
1293
1294 void move() {
1295 mInvokeMove.execute();
1296 }
1297
1298 void moveTo(float row) {
1299 mState.targetPos = row;
1300 mState.save();
1301 mInvokeMoveTo.execute();
1302 }
1303
Jason Sams37e7c2b2009-10-19 12:55:43 -07001304 int chooseTappedIcon(int x, int y, float pos) {
1305 // Adjust for scroll position if not zero.
1306 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001307
Joe Onorato6665c0f2009-09-02 15:27:24 -07001308 int col = -1;
1309 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001310 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1311 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1312 col = i;
1313 break;
1314 }
1315 }
1316 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1317 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1318 row = i;
1319 break;
1320 }
1321 }
1322
1323 if (row < 0 || col < 0) {
1324 return -1;
1325 }
1326
Joe Onorato664457d2009-10-28 16:30:34 -04001327 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001328 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001329
Joe Onorato664457d2009-10-28 16:30:34 -04001330 if (index >= mState.iconCount) {
1331 return -1;
1332 } else {
1333 return index;
1334 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001335 }
1336
Joe Onorato6665c0f2009-09-02 15:27:24 -07001337 /**
1338 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001339 *
1340 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001341 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001342 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001343 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001344 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001345 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001346 }
1347
Joe Onoratoc61cff92009-11-08 11:54:39 -05001348 /**
1349 * Select the icon at the given index.
1350 *
1351 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001352 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001353 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001354 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001355 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001356 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001357 if (mLastSelection == SELECTION_ICONS) {
1358 mLastSelection = SELECTION_NONE;
1359 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001360 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001361 if (pressed == SELECTED_FOCUSED) {
1362 mLastSelection = SELECTION_ICONS;
1363 }
1364
Joe Onorato52a653f2009-11-11 14:52:11 -08001365 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001366 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001367
Joe Onorato52a653f2009-11-11 14:52:11 -08001368 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001369 Bitmap selectionBitmap = mSelectionBitmap;
1370
1371 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001372 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001373 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001374
1375 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001376 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001377 mSelectedIcon.uploadToTexture(0);
1378 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001379
1380 if (prev != index) {
1381 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001382 //setContentDescription(info.title);
1383 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1384 }
1385 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001386 }
1387 }
1388
1389 /**
1390 * You need to call save() on mState on your own after calling this.
1391 */
1392 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001393 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001394 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001395
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001396 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001397 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001398 switch (mode) {
1399 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001400 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001401 break;
1402 case SELECTED_FOCUSED:
1403 mLastSelection = SELECTION_HOME;
1404 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001405 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001406 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1407 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001408 break;
1409 case SELECTED_PRESSED:
1410 mState.homeButtonId = mHomeButtonPressed.getID();
1411 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001412 }
1413 }
Joe Onoratobe386092009-11-17 17:32:16 -08001414
1415 public void dumpState() {
1416 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1417 Log.d(TAG, "mRollo.mHeight=" + mHeight);
1418 Log.d(TAG, "mRollo.mIcons=" + mIcons);
1419 if (mIcons != null) {
1420 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1421 }
1422 if (mIconIds != null) {
1423 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1424 }
1425 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1426 if (mLabelIds != null) {
1427 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1428 }
1429 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1430 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1431 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
Joe Onoratobe386092009-11-17 17:32:16 -08001432 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1433 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1434 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1435 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1436 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1437 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1438 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1439 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1440 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1441 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1442 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1443 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1444 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1445 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1446 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1447 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1448 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1449 }
1450 }
1451
1452 public void dumpState() {
1453 Log.d(TAG, "mRS=" + mRS);
1454 Log.d(TAG, "mRollo=" + mRollo);
1455 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1456 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1457 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1458 Log.d(TAG, "mLastSelection=" + mLastSelection);
1459 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1460 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1461 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1462 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1463 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1464 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1465 Log.d(TAG, "mZoom=" + mZoom);
1466 Log.d(TAG, "mPosX=" + mPosX);
1467 Log.d(TAG, "mVelocity=" + mVelocity);
1468 Log.d(TAG, "mMessageProc=" + mMessageProc);
1469 if (mRollo != null) {
1470 mRollo.dumpState();
1471 }
1472 if (mRS != null) {
1473 mRS.contextDump(0);
1474 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001475 }
Joe Onorato93839052009-08-06 20:34:32 -07001476}
1477
1478