blob: 44b18ffe49846eb8a77da90961bdb3a40e4ed994 [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:
Joe Onoratoe9a3f3d2010-02-01 18:36:43 -0500416 if (mLastSelection != SELECTION_HOME) {
417 if (currentPageCol > 0) {
418 newSelection = currentSelection - 1;
419 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700420 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800421 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700422 break;
423 case KeyEvent.KEYCODE_DPAD_RIGHT:
Joe Onoratoe9a3f3d2010-02-01 18:36:43 -0500424 if (mLastSelection != SELECTION_HOME) {
425 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
426 (currentSelection < iconCount - 1)) {
427 newSelection = currentSelection + 1;
428 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700429 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800430 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700431 break;
432 }
433 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500434 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700435 mRollo.mState.save();
436 }
437 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800438 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700439 }
440
Joe Onorato93839052009-08-06 20:34:32 -0700441 @Override
442 public boolean onTouchEvent(MotionEvent ev)
443 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700444 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700445
Joe Onorato7bb17492009-09-24 17:51:01 -0700446 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700447 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700448 }
449
Joe Onoratofb0ca672009-09-14 17:55:46 -0400450 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700451 return true;
452 }
453
454 super.onTouchEvent(ev);
455
Joe Onoratofb0ca672009-09-14 17:55:46 -0400456 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700457 int y = (int)ev.getY();
458
Joe Onoratobcbeab82009-10-01 21:45:43 -0700459 int action = ev.getAction();
460 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400461 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700462 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
463 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500464 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700465 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700466 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700467 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700468 mTouchTracking = TRACKING_FLING;
469
470 mMotionDownRawX = (int)ev.getRawX();
471 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700472
Mike Cleron7d5d7462009-10-20 14:06:00 -0700473 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700474 mRollo.mState.newTouchDown = 1;
475
476 if (!mRollo.checkClickOK()) {
477 mRollo.clearSelectedIcon();
478 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700479 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800480 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700481 if (mDownIconIndex < 0) {
482 // if nothing was selected, no long press.
483 cancelLongPress();
484 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700485 }
486 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700487 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800488 mVelocityTracker = VelocityTracker.obtain();
489 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700490 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700491 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400492 break;
493 case MotionEvent.ACTION_MOVE:
494 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700495 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500496 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
497 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700498 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700499 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700500 int rawX = (int)ev.getRawX();
501 int rawY = (int)ev.getRawY();
502 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700503 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700504
Joe Onorato82ca5502009-10-15 16:59:23 -0700505 if (!mStartedScrolling && slop < mSlop) {
506 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700507 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800508 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700509 if (mDownIconIndex != mCurrentIconIndex) {
510 // If a different icon is selected, don't allow it to be picked up.
511 // This handles off-axis dragging.
512 cancelLongPress();
513 mCurrentIconIndex = -1;
514 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700515 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700516 if (!mStartedScrolling) {
517 cancelLongPress();
518 mCurrentIconIndex = -1;
519 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700520 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700521 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700522 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700523
Joe Onoratobcbeab82009-10-01 21:45:43 -0700524 mStartedScrolling = true;
525 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800526 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700527 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700528 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400529 }
530 break;
531 case MotionEvent.ACTION_UP:
532 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700533 if (mTouchTracking == TRACKING_HOME) {
534 if (action == MotionEvent.ACTION_UP) {
535 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400536 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700537 mLauncher.closeAllApps(true);
538 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500539 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700540 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700541 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700542 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700543 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700544 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700545 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700546
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800547 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
548 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700549 mRollo.clearSelectedIcon();
550 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700551 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700552
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800553 if (mVelocityTracker != null) {
554 mVelocityTracker.recycle();
555 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700556 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700557 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700558 mTouchTracking = TRACKING_NONE;
559 break;
Joe Onorato93839052009-08-06 20:34:32 -0700560 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700561
562 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700563 }
564
Joe Onorato6665c0f2009-09-02 15:27:24 -0700565 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700566 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400567 return;
568 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700569 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
570 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400571 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700572 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700573 mLauncher.startActivitySafely(app.intent);
574 }
575 }
576
577 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700578 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400579 return true;
580 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700581 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
582 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
583 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700584
Jason Samsb4ecab22010-01-19 16:43:26 -0800585 Bitmap bmp = app.iconBitmap;
586 final int w = bmp.getWidth();
587 final int h = bmp.getHeight();
588
Joe Onorato5162ea92009-09-03 09:39:42 -0700589 // We don't really have an accurate location to use. This will do.
Jason Samsb4ecab22010-01-19 16:43:26 -0800590 int screenX = mMotionDownRawX - (w / 2);
591 int screenY = mMotionDownRawY - h;
Joe Onorato5162ea92009-09-03 09:39:42 -0700592
Joe Onoratobcbeab82009-10-01 21:45:43 -0700593 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
594 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Jason Samsb4ecab22010-01-19 16:43:26 -0800595 mDragController.startDrag(bmp, screenX, screenY,
596 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700597
Joe Onorato7bb17492009-09-24 17:51:01 -0700598 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700599 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700600 return true;
601 }
602
Joe Onorato52a653f2009-11-11 14:52:11 -0800603 @Override
604 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
605 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
606 if (!isVisible()) {
607 return false;
608 }
609 String text = null;
610 int index;
611 int count = mAllAppsList.size() + 1; // +1 is home
612 int pos = -1;
613 switch (mLastSelection) {
614 case SELECTION_ICONS:
615 index = mRollo.mState.selectedIconIndex;
616 if (index >= 0) {
617 ApplicationInfo info = mAllAppsList.get(index);
618 if (info.title != null) {
619 text = info.title.toString();
620 pos = index;
621 }
622 }
623 break;
624 case SELECTION_HOME:
625 text = getContext().getString(R.string.all_apps_home_button_label);
626 pos = count;
627 break;
628 }
629 if (text != null) {
Joe Onorato52a653f2009-11-11 14:52:11 -0800630 event.setEnabled(true);
631 event.getText().add(text);
632 //event.setContentDescription(text);
633 event.setItemCount(count);
634 event.setCurrentItemIndex(pos);
635 }
636 }
637 return false;
638 }
639
Joe Onorato5162ea92009-09-03 09:39:42 -0700640 public void setDragController(DragController dragger) {
641 mDragController = dragger;
642 }
643
644 public void onDropCompleted(View target, boolean success) {
645 }
646
Joe Onorato4db52312009-10-06 11:17:43 -0700647 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800648 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700649 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800650 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700651 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800652 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400653 cancelLongPress();
Joe Onorato6374c512010-01-06 20:42:25 -0800654 mNextZoom = zoom;
655 mAnimateNextZoom = animate;
656 // if we do setZoom while we don't have a surface, we won't
657 // get the callbacks that actually set mZoom.
658 if (mRollo == null || !mHaveSurface) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800659 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800660 mZoom = zoom;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800661 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700662 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800663 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700664 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400665 }
666
667 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800668 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800669 }
670
671 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800672 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400673 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700674
Joe Onorato9c1289c2009-08-17 11:03:03 -0400675 public void setApps(ArrayList<ApplicationInfo> list) {
676 mAllAppsList = list;
677 if (mRollo != null) {
Joe Onorato3ecbd812009-12-11 13:38:54 -0800678 mRollo.setApps(list);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400679 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400680 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700681 }
682
Joe Onoratoa8138d52009-10-06 19:25:30 -0700683 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500684 if (mAllAppsList == null) {
685 // Not done loading yet. We'll find out about it later.
686 return;
687 }
688
Joe Onoratoa8138d52009-10-06 19:25:30 -0700689 final int N = list.size();
Joe Onorato8eea3912009-12-09 13:01:06 -0800690 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700691 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
692 }
693
694 for (int i=0; i<N; i++) {
695 final ApplicationInfo item = list.get(i);
Joe Onoratob0c27f22009-12-01 16:19:38 -0800696 int index = Collections.binarySearch(mAllAppsList, item,
697 LauncherModel.APP_NAME_COMPARATOR);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700698 if (index < 0) {
699 index = -(index+1);
700 }
701 mAllAppsList.add(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -0800702 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700703 mRollo.addApp(index, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700704 }
705 }
706
Joe Onorato8eea3912009-12-09 13:01:06 -0800707 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700708 mRollo.saveAppsList();
709 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700710 }
711
Joe Onoratoa8138d52009-10-06 19:25:30 -0700712 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500713 if (mAllAppsList == null) {
714 // Not done loading yet. We'll find out about it later.
715 return;
716 }
717
Joe Onoratoa8138d52009-10-06 19:25:30 -0700718 final int N = list.size();
719 for (int i=0; i<N; i++) {
720 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400721 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700722 if (index >= 0) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800723 int ic = mRollo != null ? mRollo.mState.iconCount : 666;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700724 mAllAppsList.remove(index);
Joe Onorato8eea3912009-12-09 13:01:06 -0800725 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700726 mRollo.removeApp(index);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700727 }
728 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800729 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700730 // Try to recover. This should keep us from crashing for now.
731 }
732 }
733
Joe Onorato8eea3912009-12-09 13:01:06 -0800734 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700735 mRollo.saveAppsList();
736 }
737 }
738
739 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
740 // Just remove and add, because they may need to be re-sorted.
741 removeApps(list);
742 addApps(list);
743 }
744
Joe Onoratocb9f7982009-10-31 16:32:02 -0400745 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
746 ComponentName component = item.intent.getComponent();
747 final int N = list.size();
748 for (int i=0; i<N; i++) {
749 ApplicationInfo x = list.get(i);
750 if (x.intent.getComponent().equals(component)) {
751 return i;
752 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700753 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400754 return -1;
755 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700756
Joe Onoratoc567acb2009-08-31 14:34:43 -0700757 private static int countPages(int iconCount) {
758 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
759 int pages = iconCount / iconsPerPage;
760 if (pages*iconsPerPage != iconCount) {
761 pages++;
762 }
763 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400764 }
765
Joe Onoratocb75f362009-11-12 13:04:07 -0800766 class AAMessage extends RenderScript.RSMessage {
767 public void run() {
768 mPosX = ((float)mData[0]) / (1 << 16);
769 mVelocity = ((float)mData[1]) / (1 << 16);
770 mZoom = ((float)mData[2]) / (1 << 16);
771 mZoomDirty = false;
772 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800773 }
774
Joe Onorato93839052009-08-06 20:34:32 -0700775 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700776
Joe Onorato1feb3a82009-08-08 22:32:00 -0700777 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700778 private int mWidth;
779 private int mHeight;
780
781 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700782 private Script mScript;
783 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700784 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700785 private Script.Invokable mInvokeFling;
786 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800787 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700788
Jason Samscd689e12009-09-29 15:28:22 -0700789 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700790 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700791 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700792 private ProgramFragment mPFTexMip;
Jason Sams6ec11bc2010-01-19 17:56:52 -0800793 private ProgramFragment mPFTexMipAlpha;
Jason Samsc8514792009-10-29 14:27:29 -0700794 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700795 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700796 private ProgramVertex mPVOrtho;
Jason Samsb4ecab22010-01-19 16:43:26 -0800797 private ProgramVertex mPVCurve;
Jason Sams0aa71662009-10-02 18:43:18 -0700798 private SimpleMesh mMesh;
Jason Sams5612e432009-11-16 14:18:07 -0800799 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700800
Jason Samsb4ecab22010-01-19 16:43:26 -0800801 private Allocation mUniformAlloc;
802
Joe Onoratod63458b2009-10-15 21:19:09 -0700803 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500804 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700805 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700806
Joe Onoratobf15cb42009-08-07 14:33:40 -0700807 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700808 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700809 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700810
Joe Onoratobf15cb42009-08-07 14:33:40 -0700811 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700812 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700813 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700814 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700815
Joe Onorato6665c0f2009-09-02 15:27:24 -0700816 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700817 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700818
819 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400820 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700821
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700822 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700823 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700824
Jason Sams78aebd82009-09-15 13:06:59 -0700825 class BaseAlloc {
826 Allocation mAlloc;
827 Type mType;
828
829 void save() {
830 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700831 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700832 }
833
Jason Sams476339d2009-09-29 18:14:38 -0700834 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800835 return (Math.abs(mVelocity) < 0.4f) &&
836 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700837 }
838
Jason Sams78aebd82009-09-15 13:06:59 -0700839 class Params extends BaseAlloc {
840 Params() {
841 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
842 mAlloc = Allocation.createTyped(mRS, mType);
843 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700844 }
Jason Sams78aebd82009-09-15 13:06:59 -0700845 public int bubbleWidth;
846 public int bubbleHeight;
847 public int bubbleBitmapWidth;
848 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700849
Joe Onoratobcbeab82009-10-01 21:45:43 -0700850 public int homeButtonWidth;
851 public int homeButtonHeight;
852 public int homeButtonTextureWidth;
853 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700854 }
855
856 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700857 public float newPositionX;
858 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700859 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700860 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700861 public int selectedIconIndex = -1;
862 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700863 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700864 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700865 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700866
867 State() {
868 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
869 mAlloc = Allocation.createTyped(mRS, mType);
870 save();
871 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700872 }
873
874 public RolloRS() {
875 }
876
877 public void init(Resources res, int width, int height) {
878 mRes = res;
879 mWidth = width;
880 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700881 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700882 initProgramVertex();
883 initProgramFragment();
884 initProgramStore();
Jason Samsb4ecab22010-01-19 16:43:26 -0800885 //initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700886 initGl();
887 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700888 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700889 initRs();
890 }
891
Jason Samsb4ecab22010-01-19 16:43:26 -0800892 public void initMesh2() {
893 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Jason Sams0aa71662009-10-02 18:43:18 -0700894
Jason Samsb4ecab22010-01-19 16:43:26 -0800895 for (int ct=0; ct < 16; ct++) {
896 float pos = (1.f / 16.f) * ct;
897 tm.addVertex(0.0f, pos);
898 tm.addVertex(1.0f, pos);
Jason Samsd8152b92009-10-13 17:19:10 -0700899 }
Jason Samsb4ecab22010-01-19 16:43:26 -0800900 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
Jason Samsd8152b92009-10-13 17:19:10 -0700901 tm.addTriangle(ct, ct+1, ct+2);
902 tm.addTriangle(ct+1, ct+3, ct+2);
903 }
Jason Samsb4ecab22010-01-19 16:43:26 -0800904 mMesh = tm.create();
905 mMesh.setName("SMCell");
Jason Samsd8152b92009-10-13 17:19:10 -0700906 }
907
Jason Sams5612e432009-11-16 14:18:07 -0800908 void resize(int w, int h) {
909 mPVA.setupProjectionNormalized(w, h);
910 mWidth = w;
911 mHeight = h;
912 }
913
Jason Samscd689e12009-09-29 15:28:22 -0700914 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800915 mPVA = new ProgramVertex.MatrixAllocation(mRS);
916 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700917
918 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700919 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700920 mPV = pvb.create();
921 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800922 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700923
Jason Samsb4ecab22010-01-19 16:43:26 -0800924 Element.Builder eb = new Element.Builder(mRS);
925 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
926 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
927 Element e = eb.create();
928
929 mUniformAlloc = Allocation.createSized(mRS, e, 1);
930
931 initMesh2();
932 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
933 String t = new String("void main() {\n" +
934 // Animation
935 " float ani = UNI_Position.z;\n" +
936
937 " float scale = (2.0 / 480.0);\n" +
938 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
939 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
940 " float y = 0.0;\n" +
941 " float z = 0.0;\n" +
942 " float lum = 1.0;\n" +
943
944 " float cv = min(ys, 50.0) - 50.0;\n" +
945 " y += cv * 0.681;\n" + // Roughy 47 degrees
946 " z += -cv * 0.731;\n" +
947 " cv = clamp(ys, 50.0, 120.0) - 120.0;\n" + // curve range
948 " y += cv * cos(cv * 0.4 / (180.0 / 3.14));\n" +
949 " z += cv * sin(cv * 0.4 / (180.0 / 3.14));\n" +
950
951 " cv = max(ys, 750.0) - 750.0;\n" +
952 " y += cv * 0.681;\n" +
953 " z += cv * 0.731;\n" +
954 " cv = clamp(ys, 680.0, 750.0) - 680.0;\n" +
955 " y += cv * cos(cv * 0.4 / (180.0 / 3.14));\n" +
956 " z += cv * sin(cv * 0.4 / (180.0 / 3.14));\n" +
957
958 " y += clamp(ys, 120.0, 680.0);\n" +
959 " lum += (clamp(ys, 60.0, 115.0) - 115.0) / 100.0;\n" +
960 " lum -= (clamp(ys, 685.0, 740.0) - 685.0) / 100.0;\n" +
961
962 " vec4 pos;\n" +
963 " pos.x = x * scale - 1.0;\n" +
964 " pos.y = y * scale - 1.66;\n" +
965 " pos.z = z * scale;\n" +
966 " pos.w = 1.0;\n" +
967
968 " pos.x *= 1.0 + ani * 4.0;\n" +
969 " pos.y *= 1.0 + ani * 4.0;\n" +
970 " pos.z -= ani * 1.5;\n" +
971 " lum *= 1.0 - ani;\n" +
972
973 " gl_Position = UNI_MVP * pos;\n" +
974 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
975 " varTex0.xy = ATTRIB_position;\n" +
976 " varTex0.y = 1.0 - varTex0.y;\n" +
977 " varTex0.zw = vec2(0.0, 0.0);\n" +
978 "}\n");
979 sb.setShader(t);
980 sb.addConstant(mUniformAlloc.getType());
981 sb.addInput(mMesh.getVertexType(0).getElement());
982 mPVCurve = sb.create();
983 mPVCurve.setName("PVCurve");
984 mPVCurve.bindAllocation(mPVA);
985 mPVCurve.bindConstants(mUniformAlloc, 1);
986
987 float tf[] = new float[] {72.f, 72.f, 0.f, 0.f, 120.f, 120.f, 0.f, 0.f};
988 mUniformAlloc.data(tf);
989
Jason Sams37e7c2b2009-10-19 12:55:43 -0700990 //pva = new ProgramVertex.MatrixAllocation(mRS);
991 //pva.setupOrthoWindow(mWidth, mHeight);
992 //pvb.setTextureMatrixEnable(true);
993 //mPVOrtho = pvb.create();
994 //mPVOrtho.setName("PVOrtho");
995 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700996
997 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700998 }
Joe Onorato93839052009-08-06 20:34:32 -0700999
Jason Samscd689e12009-09-29 15:28:22 -07001000 private void initProgramFragment() {
1001 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -07001002 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -08001003 sb.setMag(Sampler.Value.NEAREST);
Jason Samscd689e12009-09-29 15:28:22 -07001004 sb.setWrapS(Sampler.Value.CLAMP);
1005 sb.setWrapT(Sampler.Value.CLAMP);
1006 Sampler linear = sb.create();
1007
1008 sb.setMin(Sampler.Value.NEAREST);
1009 sb.setMag(Sampler.Value.NEAREST);
1010 Sampler nearest = sb.create();
1011
Jason Sams72f1d312009-12-17 16:58:25 -08001012 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001013 //mPFColor = bf.create();
1014 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -07001015
Jason Samsb4ecab22010-01-19 16:43:26 -08001016 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
Jason Sams72f1d312009-12-17 16:58:25 -08001017 ProgramFragment.Builder.Format.RGBA, 0);
Jason Samsc8514792009-10-29 14:27:29 -07001018 mPFTexMip = bf.create();
1019 mPFTexMip.setName("PFTexMip");
1020 mPFTexMip.bindSampler(linear, 0);
1021
1022 mPFTexNearest = bf.create();
1023 mPFTexNearest.setName("PFTexNearest");
1024 mPFTexNearest.bindSampler(nearest, 0);
Jason Sams6ec11bc2010-01-19 17:56:52 -08001025
1026 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1027 ProgramFragment.Builder.Format.ALPHA, 0);
1028 mPFTexMipAlpha = bf.create();
1029 mPFTexMipAlpha.setName("PFTexMipAlpha");
1030 mPFTexMipAlpha.bindSampler(linear, 0);
1031
Jason Samscd689e12009-09-29 15:28:22 -07001032 }
1033
1034 private void initProgramStore() {
1035 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1036 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -07001037 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -07001038 bs.setDitherEnable(true);
1039 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1040 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1041 mPSIcons = bs.create();
1042 mPSIcons.setName("PSIcons");
1043
1044 //bs.setDitherEnable(false);
1045 //mPSText = bs.create();
1046 //mPSText.setName("PSText");
1047 }
1048
1049 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001050 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -07001051 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -07001052 }
Jason Sams78aebd82009-09-15 13:06:59 -07001053
Joe Onorato1feb3a82009-08-08 22:32:00 -07001054 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -07001055 mParams = new Params();
1056 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001057
Joe Onoratobf15cb42009-08-07 14:33:40 -07001058 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -07001059
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001060 mParams.bubbleWidth = bubble.getBubbleWidth();
1061 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1062 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1063 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001064
Joe Onoratod63458b2009-10-15 21:19:09 -07001065 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1066 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1067 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001068 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1069 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1070 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001071 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1072 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1073 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001074 mParams.homeButtonWidth = 76;
1075 mParams.homeButtonHeight = 68;
1076 mParams.homeButtonTextureWidth = 128;
1077 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001078
Joe Onoratod63458b2009-10-15 21:19:09 -07001079 mState.homeButtonId = mHomeButtonNormal.getID();
1080
Joe Onorato1feb3a82009-08-08 22:32:00 -07001081 mParams.save();
1082 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001083
Jason Sams1a94ee32010-01-20 13:34:30 -08001084 mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1085 Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001086 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001087
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001089 }
1090
Jason Sams37e7c2b2009-10-19 12:55:43 -07001091 private void initScript(int id) {
1092 }
1093
1094 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001095 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Samsd15f3ef2010-01-06 14:58:06 -08001096 sb.setScript(mRes, R.raw.allapps);
Joe Onorato93839052009-08-06 20:34:32 -07001097 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001098 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001099 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1100 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Samsb4ecab22010-01-19 16:43:26 -08001101 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001102 mInvokeMove = sb.addInvokable("move");
1103 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001104 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001105 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001106 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001107 mScript = sb.create();
1108 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1109 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1110 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1111 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1112 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Jason Samsb4ecab22010-01-19 16:43:26 -08001113 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Joe Onorato93839052009-08-06 20:34:32 -07001114
Jason Sams37e7c2b2009-10-19 12:55:43 -07001115 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001116 }
Joe Onorato93839052009-08-06 20:34:32 -07001117
Jason Sams20df7c72009-11-05 12:30:24 -08001118 void dirtyCheck() {
Jason Sams510ee042009-12-15 14:23:37 -08001119 if (mZoomDirty) {
Joe Onorato6374c512010-01-06 20:42:25 -08001120 setZoom(mNextZoom, mAnimateNextZoom);
Jason Sams20df7c72009-11-05 12:30:24 -08001121 }
1122 }
1123
Joe Onorato9c1289c2009-08-17 11:03:03 -04001124 private void setApps(ArrayList<ApplicationInfo> list) {
1125 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001126 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001127 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001128 allocCount = 1;
1129 }
1130
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001132 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001133 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134
1135 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001136 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001137 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001138
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001139 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 mState.iconCount = count;
Joe Onorato8eea3912009-12-09 13:01:06 -08001142 for (int i=0; i < mState.iconCount; i++) {
1143 createAppIconAllocations(i, list.get(i));
Joe Onorato3ecbd812009-12-11 13:38:54 -08001144 }
Jason Sams510ee042009-12-15 14:23:37 -08001145 for (int i=0; i < mState.iconCount; i++) {
1146 uploadAppIcon(i, list.get(i));
Joe Onorato8eea3912009-12-09 13:01:06 -08001147 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001148 saveAppsList();
1149 }
1150
Joe Onorato3a8820b2009-11-10 15:06:42 -08001151 private void setZoom(float zoom, boolean animate) {
1152 mRollo.clearSelectedIcon();
1153 mRollo.setHomeSelected(SELECTED_NONE);
1154 if (zoom > 0.001f) {
1155 mRollo.mState.zoomTarget = zoom;
1156 } else {
1157 mRollo.mState.zoomTarget = 0;
1158 }
1159 mRollo.mState.save();
1160 if (!animate) {
1161 mRollo.mInvokeSetZoom.execute();
1162 }
1163 }
1164
Joe Onorato8eea3912009-12-09 13:01:06 -08001165 private void createAppIconAllocations(int index, ApplicationInfo item) {
Joe Onoratoa8138d52009-10-06 19:25:30 -07001166 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001167 Element.RGBA_8888(mRS), true);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001168 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Sams6ec11bc2010-01-19 17:56:52 -08001169 Element.A_8(mRS), true);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001170 mIconIds[index] = mIcons[index].getID();
1171 mLabelIds[index] = mLabels[index].getID();
1172 }
1173
Joe Onorato8eea3912009-12-09 13:01:06 -08001174 private void uploadAppIcon(int index, ApplicationInfo item) {
1175 if (mIconIds[index] != mIcons[index].getID()) {
1176 throw new IllegalStateException("uploadAppIcon index=" + index
1177 + " mIcons[index].getID=" + mIcons[index].getID()
1178 + " mIconsIds[index]=" + mIconIds[index]
1179 + " item=" + item);
1180 }
1181 mIcons[index].uploadToTexture(0);
1182 mLabels[index].uploadToTexture(0);
1183 }
1184
Joe Onoratoa8138d52009-10-06 19:25:30 -07001185 /**
1186 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1187 * fill in the values and call saveAppsList().
1188 */
1189 private void reallocAppsList(int count) {
1190 Allocation[] icons = new Allocation[count];
1191 int[] iconIds = new int[count];
1192 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1193
1194 Allocation[] labels = new Allocation[count];
1195 int[] labelIds = new int[count];
1196 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1197
Joe Onoratobf173f12009-12-08 13:29:38 -08001198 final int oldCount = mRollo.mState.iconCount;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001199
1200 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1201 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1202 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1203 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1204
1205 mIcons = icons;
1206 mIconIds = iconIds;
1207 mLabels = labels;
1208 mLabelIds = labelIds;
1209 }
1210
1211 /**
1212 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1213 */
1214 private void addApp(int index, ApplicationInfo item) {
1215 final int count = mState.iconCount - index;
1216 final int dest = index + 1;
1217
1218 System.arraycopy(mIcons, index, mIcons, dest, count);
1219 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1220 System.arraycopy(mLabels, index, mLabels, dest, count);
1221 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1222
Joe Onorato8eea3912009-12-09 13:01:06 -08001223 createAppIconAllocations(index, item);
Jason Sams510ee042009-12-15 14:23:37 -08001224 uploadAppIcon(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -08001225 mRollo.mState.iconCount++;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001226 }
1227
1228 /**
1229 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1230 */
1231 private void removeApp(int index) {
1232 final int count = mState.iconCount - index - 1;
1233 final int src = index + 1;
1234
1235 System.arraycopy(mIcons, src, mIcons, index, count);
1236 System.arraycopy(mIconIds, src, mIconIds, index, count);
1237 System.arraycopy(mLabels, src, mLabels, index, count);
1238 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1239
Joe Onoratoa276fc52009-12-08 17:02:02 -08001240 mRollo.mState.iconCount--;
Joe Onorato8eea3912009-12-09 13:01:06 -08001241 final int last = mState.iconCount;
Joe Onoratoa276fc52009-12-08 17:02:02 -08001242
Joe Onoratoa8138d52009-10-06 19:25:30 -07001243 mIcons[last] = null;
1244 mIconIds[last] = 0;
1245 mLabels[last] = null;
1246 mLabelIds[last] = 0;
1247 }
1248
1249 /**
1250 * Send the apps list structures to RS.
1251 */
1252 private void saveAppsList() {
1253 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001254
Joe Onoratoa8138d52009-10-06 19:25:30 -07001255 mAllocIconIds.data(mIconIds);
1256 mAllocLabelIds.data(mLabelIds);
1257
Jason Sams37e7c2b2009-10-19 12:55:43 -07001258 if (mScript != null) { // this happens when we init it
1259 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1260 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001261 }
1262
1263 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001264
1265 // Note: mScript may be null if we haven't initialized it yet.
1266 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001267 if (mInvokeResetWAR != null) {
1268 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001269 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001270 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001272
1273 void initTouchState() {
1274 int width = getWidth();
1275 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001276 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1277 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001278
Jason Sams37e7c2b2009-10-19 12:55:43 -07001279 int centerY = (height / 2);
1280 mTouchYBorders[0] = centerY - (cellHeight * 2);
1281 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001282 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001283 mTouchYBorders[3] = centerY + cellHeight;
1284 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001285
Joe Onorato6665c0f2009-09-02 15:27:24 -07001286 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001287 mTouchXBorders[0] = 0;
1288 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001289 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001290 mTouchXBorders[3] = centerX + (width / 4);
1291 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001292 }
1293
Joe Onorato664457d2009-10-28 16:30:34 -04001294 void fling() {
1295 mInvokeFling.execute();
1296 }
1297
1298 void move() {
1299 mInvokeMove.execute();
1300 }
1301
1302 void moveTo(float row) {
1303 mState.targetPos = row;
1304 mState.save();
1305 mInvokeMoveTo.execute();
1306 }
1307
Jason Sams37e7c2b2009-10-19 12:55:43 -07001308 int chooseTappedIcon(int x, int y, float pos) {
1309 // Adjust for scroll position if not zero.
1310 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001311
Joe Onorato6665c0f2009-09-02 15:27:24 -07001312 int col = -1;
1313 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001314 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1315 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1316 col = i;
1317 break;
1318 }
1319 }
1320 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1321 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1322 row = i;
1323 break;
1324 }
1325 }
1326
1327 if (row < 0 || col < 0) {
1328 return -1;
1329 }
1330
Joe Onorato664457d2009-10-28 16:30:34 -04001331 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001332 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001333
Joe Onorato664457d2009-10-28 16:30:34 -04001334 if (index >= mState.iconCount) {
1335 return -1;
1336 } else {
1337 return index;
1338 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001339 }
1340
Joe Onorato6665c0f2009-09-02 15:27:24 -07001341 /**
1342 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001343 *
1344 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001345 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001346 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001347 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001348 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001349 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001350 }
1351
Joe Onoratoc61cff92009-11-08 11:54:39 -05001352 /**
1353 * Select the icon at the given index.
1354 *
1355 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001356 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001357 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001358 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001359 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001360 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001361 if (mLastSelection == SELECTION_ICONS) {
1362 mLastSelection = SELECTION_NONE;
1363 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001364 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001365 if (pressed == SELECTED_FOCUSED) {
1366 mLastSelection = SELECTION_ICONS;
1367 }
1368
Joe Onorato52a653f2009-11-11 14:52:11 -08001369 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001370 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001371
Joe Onorato52a653f2009-11-11 14:52:11 -08001372 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001373 Bitmap selectionBitmap = mSelectionBitmap;
1374
1375 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001376 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001377 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001378
1379 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001380 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001381 mSelectedIcon.uploadToTexture(0);
1382 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001383
1384 if (prev != index) {
1385 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001386 //setContentDescription(info.title);
1387 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1388 }
1389 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001390 }
1391 }
1392
1393 /**
1394 * You need to call save() on mState on your own after calling this.
1395 */
1396 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001397 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001398 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001399
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001400 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001401 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001402 switch (mode) {
1403 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001404 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001405 break;
1406 case SELECTED_FOCUSED:
1407 mLastSelection = SELECTION_HOME;
1408 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001409 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001410 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1411 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001412 break;
1413 case SELECTED_PRESSED:
1414 mState.homeButtonId = mHomeButtonPressed.getID();
1415 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001416 }
1417 }
Joe Onoratobe386092009-11-17 17:32:16 -08001418
1419 public void dumpState() {
1420 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1421 Log.d(TAG, "mRollo.mHeight=" + mHeight);
1422 Log.d(TAG, "mRollo.mIcons=" + mIcons);
1423 if (mIcons != null) {
1424 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1425 }
1426 if (mIconIds != null) {
1427 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1428 }
1429 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1430 if (mLabelIds != null) {
1431 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1432 }
1433 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1434 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1435 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
Joe Onoratobe386092009-11-17 17:32:16 -08001436 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1437 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1438 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1439 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1440 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1441 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1442 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1443 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1444 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1445 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1446 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1447 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1448 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1449 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1450 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1451 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1452 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1453 }
1454 }
1455
1456 public void dumpState() {
1457 Log.d(TAG, "mRS=" + mRS);
1458 Log.d(TAG, "mRollo=" + mRollo);
1459 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1460 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1461 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1462 Log.d(TAG, "mLastSelection=" + mLastSelection);
1463 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1464 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1465 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1466 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1467 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1468 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1469 Log.d(TAG, "mZoom=" + mZoom);
1470 Log.d(TAG, "mPosX=" + mPosX);
1471 Log.d(TAG, "mVelocity=" + mVelocity);
1472 Log.d(TAG, "mMessageProc=" + mMessageProc);
1473 if (mRollo != null) {
1474 mRollo.dumpState();
1475 }
1476 if (mRS != null) {
1477 mRS.contextDump(0);
1478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001479 }
Joe Onorato93839052009-08-06 20:34:32 -07001480}
1481
1482