blob: 8f80afd860e6be4f56f22a950f1a9ad1bf9a82c1 [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 Onoratoc567acb2009-08-31 14:34:43 -0700143 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700144
145 public AllAppsView(Context context, AttributeSet attrs) {
146 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700147 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400148 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700149 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700150 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700151 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700152 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
153
Joe Onorato6665c0f2009-09-02 15:27:24 -0700154 setOnClickListener(this);
155 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700156 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700157 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samse26d9fc2009-11-12 14:00:43 -0800158
159 mRS = createRenderScript(true);
160 }
161
162 @Override
163 protected void onDetachedFromWindow() {
164 destroyRenderScript();
Joe Onorato93839052009-08-06 20:34:32 -0700165 }
166
Joe Onoratob39e51a2009-10-28 15:47:49 -0400167 /**
168 * If you have an attached click listener, View always plays the click sound!?!?
169 * Deal with sound effects by hand.
170 */
171 public void reallyPlaySoundEffect(int sound) {
172 boolean old = isSoundEffectsEnabled();
173 setSoundEffectsEnabled(true);
174 playSoundEffect(sound);
175 setSoundEffectsEnabled(old);
176 }
177
Joe Onorato7c312c12009-08-13 21:36:53 -0700178 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
179 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700180 }
181
Joe Onorato6665c0f2009-09-02 15:27:24 -0700182 public void setLauncher(Launcher launcher) {
183 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700184 }
185
Joe Onorato1feb3a82009-08-08 22:32:00 -0700186 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700187 public void surfaceDestroyed(SurfaceHolder holder) {
188 super.surfaceDestroyed(holder);
Joe Onoratofab74402009-11-11 16:05:23 -0800189 // Without this, we leak mMessageCallback which leaks the context.
190 mRS.mMessageCallback = null;
Joe Onorato6374c512010-01-06 20:42:25 -0800191 // We may lose any callbacks that are pending, so make sure that we re-sync that
192 // on the next surfaceChanged.
193 mZoomDirty = true;
194 mHaveSurface = false;
Joe Onorato7bb17492009-09-24 17:51:01 -0700195 }
196
197 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700198 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800199 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700200
Joe Onorato080d9b62009-11-02 12:01:11 -0500201 super.surfaceChanged(holder, format, w, h);
202
Joe Onorato6374c512010-01-06 20:42:25 -0800203 mHaveSurface = true;
204
Jason Samse26d9fc2009-11-12 14:00:43 -0800205 if (mRollo == null) {
Jason Sams90396672009-11-03 13:59:34 -0800206 mRollo = new RolloRS();
207 mRollo.init(getResources(), w, h);
208 if (mAllAppsList != null) {
209 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800210 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800211 if (mShouldGainFocus) {
212 gainFocus();
213 mShouldGainFocus = false;
214 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 }
Jason Samse26d9fc2009-11-12 14:00:43 -0800216 mRollo.dirtyCheck();
Jason Sams5612e432009-11-16 14:18:07 -0800217 mRollo.resize(w, h);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700218
Joe Onoratocb75f362009-11-12 13:04:07 -0800219 mRS.mMessageCallback = mMessageProc = new AAMessage();
220
Joe Onoratoc567acb2009-08-31 14:34:43 -0700221 Resources res = getContext().getResources();
222 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700223
Jason Sams76512482010-02-04 16:31:35 -0800224
225 if (mRollo.mUniformAlloc != null) {
226 float tf[] = new float[] {72.f, 72.f,
227 120.f, 120.f, 0.f, 0.f,
228 120.f, 680.f,
229 (2.f / 480.f), 0, -((float)w / 2) - 0.25f, -380.25f};
230 if (w > h) {
231 tf[6] = 40.f;
232 tf[7] = h - 40.f;
233 tf[9] = 1.f;
234 tf[10] = -((float)w / 2) - 0.25f;
235 tf[11] = -((float)h / 2) - 0.25f;
236 }
237
238 mRollo.mUniformAlloc.data(tf);
239 }
240
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800241 //long endTime = SystemClock.uptimeMillis();
242 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700243 }
Jason Sams2e19c052009-10-20 18:19:55 -0700244
Joe Onorato93839052009-08-06 20:34:32 -0700245 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800246 public void onWindowFocusChanged(boolean hasWindowFocus) {
247 super.onWindowFocusChanged(hasWindowFocus);
248 if (mArrowNavigation) {
249 if (!hasWindowFocus) {
250 // Clear selection when we lose window focus
251 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500252 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800253 mRollo.clearSelectedIcon();
254 mRollo.mState.save();
255 } else if (hasWindowFocus) {
256 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500257 if (mLastSelection == SELECTION_ICONS) {
258 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800259 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500260 Defines.COLUMNS_PER_PAGE;
261 if (selection < 0 || // No selection
262 selection < firstIcon || // off the top of the screen
263 selection >= mRollo.mState.iconCount || // past last icon
264 selection >= firstIcon + // past last icon on screen
265 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
266 selection = firstIcon;
267 }
268
269 // Select the first icon when we gain window focus
270 mRollo.selectIcon(selection, SELECTED_FOCUSED);
271 mRollo.mState.save();
272 } else if (mLastSelection == SELECTION_HOME) {
273 mRollo.setHomeSelected(SELECTED_FOCUSED);
274 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800275 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800276 }
277 }
278 }
279 }
280
281 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700282 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
283 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400284
285 if (!isVisible()) {
286 return;
287 }
288
Mike Cleron7d5d7462009-10-20 14:06:00 -0700289 if (gainFocus) {
Jason Sams510ee042009-12-15 14:23:37 -0800290 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800291 gainFocus();
292 } else {
293 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700294 }
295 } else {
Joe Onorato8eea3912009-12-09 13:01:06 -0800296 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800297 if (mArrowNavigation) {
298 // Clear selection when we lose focus
299 mRollo.clearSelectedIcon();
300 mRollo.setHomeSelected(SELECTED_NONE);
301 mRollo.mState.save();
302 mArrowNavigation = false;
303 }
304 } else {
305 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700306 }
307 }
308 }
309
Mike Cleronb64b67a2009-11-08 14:56:25 -0800310 private void gainFocus() {
311 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
312 // Select the first icon when we gain keyboard focus
313 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800314 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800315 SELECTED_FOCUSED);
316 mRollo.mState.save();
317 }
318 }
319
Mike Cleron7d5d7462009-10-20 14:06:00 -0700320 @Override
321 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700322
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800323 boolean handled = false;
Jason Samse26d9fc2009-11-12 14:00:43 -0800324
Joe Onorato859b3a72009-10-28 15:17:01 -0400325 if (!isVisible()) {
326 return false;
327 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500328 final int iconCount = mRollo.mState.iconCount;
329
Mike Cleron7d5d7462009-10-20 14:06:00 -0700330 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
331 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500332 if (mLastSelection == SELECTION_HOME) {
333 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
334 mLauncher.closeAllApps(true);
335 } else {
336 int whichApp = mRollo.mState.selectedIconIndex;
337 if (whichApp >= 0) {
338 ApplicationInfo app = mAllAppsList.get(whichApp);
339 mLauncher.startActivitySafely(app.intent);
340 handled = true;
341 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700342 }
343 }
344 }
Jason Sams2e19c052009-10-20 18:19:55 -0700345
Joe Onorato478730f2009-11-16 18:54:43 -0800346 if (iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700347 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700348
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800349 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800350 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700351
Mike Cleron7d5d7462009-10-20 14:06:00 -0700352 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500353 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700354
Mike Cleron7d5d7462009-10-20 14:06:00 -0700355 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500356 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700357 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700358
Mike Cleron7d5d7462009-10-20 14:06:00 -0700359 int newSelection = currentSelection;
360
361 switch (keyCode) {
362 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500363 if (mLastSelection == SELECTION_HOME) {
364 mRollo.setHomeSelected(SELECTED_NONE);
365 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
366 if (lastRowCount == 0) {
367 lastRowCount = Defines.COLUMNS_PER_PAGE;
368 }
369 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
370 if (newSelection >= iconCount) {
371 newSelection = iconCount-1;
372 }
373 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
374 - (Defines.ROWS_PER_PAGE - 1);
375 if (target < 0) {
376 target = 0;
377 }
378 if (currentTopRow != target) {
379 mRollo.moveTo(target);
380 }
381 } else {
382 if (currentPageRow > 0) {
383 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
384 } else if (currentTopRow > 0) {
385 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
386 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
Joe Onoratoaf5b4cb2009-12-08 17:51:22 -0800387 } else if (currentPageRow != 0) {
388 newSelection = currentTopRow * Defines.ROWS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500389 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700390 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800391 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700392 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800393
Joe Onoratoa13f5742009-11-02 17:15:19 -0500394 case KeyEvent.KEYCODE_DPAD_DOWN: {
395 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
396 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
397 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500398 if (mLastSelection != SELECTION_HOME) {
399 if (currentRow < rowCount-1) {
400 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato478730f2009-11-16 18:54:43 -0800401 if (currentSelection < 0) {
402 newSelection = 0;
403 } else {
404 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
405 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500406 if (newSelection >= iconCount) {
407 // Go from D to G in this arrangement:
408 // A B C D
409 // E F G
410 newSelection = iconCount - 1;
411 }
412 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
413 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
414 Defines.ROWS_PER_PAGE + 1);
415 }
416 } else {
417 newSelection = -1;
418 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700419 }
420 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800421 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700422 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500423 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700424 case KeyEvent.KEYCODE_DPAD_LEFT:
Joe Onoratoe9a3f3d2010-02-01 18:36:43 -0500425 if (mLastSelection != SELECTION_HOME) {
426 if (currentPageCol > 0) {
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 case KeyEvent.KEYCODE_DPAD_RIGHT:
Joe Onoratoe9a3f3d2010-02-01 18:36:43 -0500433 if (mLastSelection != SELECTION_HOME) {
434 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
435 (currentSelection < iconCount - 1)) {
436 newSelection = currentSelection + 1;
437 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700438 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800439 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700440 break;
441 }
442 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500443 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700444 mRollo.mState.save();
445 }
446 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800447 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700448 }
449
Joe Onorato93839052009-08-06 20:34:32 -0700450 @Override
451 public boolean onTouchEvent(MotionEvent ev)
452 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700453 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700454
Joe Onorato7bb17492009-09-24 17:51:01 -0700455 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700456 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700457 }
458
Joe Onoratofb0ca672009-09-14 17:55:46 -0400459 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700460 return true;
461 }
462
463 super.onTouchEvent(ev);
464
Joe Onoratofb0ca672009-09-14 17:55:46 -0400465 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700466 int y = (int)ev.getY();
467
Joe Onoratobcbeab82009-10-01 21:45:43 -0700468 int action = ev.getAction();
469 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400470 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700471 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
472 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500473 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700474 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700475 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700476 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700477 mTouchTracking = TRACKING_FLING;
478
479 mMotionDownRawX = (int)ev.getRawX();
480 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700481
Mike Cleron7d5d7462009-10-20 14:06:00 -0700482 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700483 mRollo.mState.newTouchDown = 1;
484
485 if (!mRollo.checkClickOK()) {
486 mRollo.clearSelectedIcon();
487 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700488 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800489 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700490 if (mDownIconIndex < 0) {
491 // if nothing was selected, no long press.
492 cancelLongPress();
493 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700494 }
495 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700496 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800497 mVelocityTracker = VelocityTracker.obtain();
498 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700499 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700500 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400501 break;
502 case MotionEvent.ACTION_MOVE:
503 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700504 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500505 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
506 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700507 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700508 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700509 int rawX = (int)ev.getRawX();
510 int rawY = (int)ev.getRawY();
511 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700512 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700513
Joe Onorato82ca5502009-10-15 16:59:23 -0700514 if (!mStartedScrolling && slop < mSlop) {
515 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700516 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800517 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700518 if (mDownIconIndex != mCurrentIconIndex) {
519 // If a different icon is selected, don't allow it to be picked up.
520 // This handles off-axis dragging.
521 cancelLongPress();
522 mCurrentIconIndex = -1;
523 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700524 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700525 if (!mStartedScrolling) {
526 cancelLongPress();
527 mCurrentIconIndex = -1;
528 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700529 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700530 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700531 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700532
Joe Onoratobcbeab82009-10-01 21:45:43 -0700533 mStartedScrolling = true;
534 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800535 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700536 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700537 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400538 }
539 break;
540 case MotionEvent.ACTION_UP:
541 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700542 if (mTouchTracking == TRACKING_HOME) {
543 if (action == MotionEvent.ACTION_UP) {
544 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400545 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700546 mLauncher.closeAllApps(true);
547 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500548 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700549 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700550 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700551 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700552 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700553 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700554 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700555
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800556 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
557 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700558 mRollo.clearSelectedIcon();
559 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700560 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700561
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800562 if (mVelocityTracker != null) {
563 mVelocityTracker.recycle();
564 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700565 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700566 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700567 mTouchTracking = TRACKING_NONE;
568 break;
Joe Onorato93839052009-08-06 20:34:32 -0700569 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700570
571 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700572 }
573
Joe Onorato6665c0f2009-09-02 15:27:24 -0700574 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700575 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400576 return;
577 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700578 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
579 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400580 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700581 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700582 mLauncher.startActivitySafely(app.intent);
583 }
584 }
585
586 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700587 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400588 return true;
589 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700590 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
591 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
592 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700593
Jason Samsb4ecab22010-01-19 16:43:26 -0800594 Bitmap bmp = app.iconBitmap;
595 final int w = bmp.getWidth();
596 final int h = bmp.getHeight();
597
Joe Onorato5162ea92009-09-03 09:39:42 -0700598 // We don't really have an accurate location to use. This will do.
Jason Samsb4ecab22010-01-19 16:43:26 -0800599 int screenX = mMotionDownRawX - (w / 2);
600 int screenY = mMotionDownRawY - h;
Joe Onorato5162ea92009-09-03 09:39:42 -0700601
Joe Onoratobcbeab82009-10-01 21:45:43 -0700602 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
603 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Jason Samsb4ecab22010-01-19 16:43:26 -0800604 mDragController.startDrag(bmp, screenX, screenY,
605 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700606
Joe Onorato7bb17492009-09-24 17:51:01 -0700607 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700608 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700609 return true;
610 }
611
Joe Onorato52a653f2009-11-11 14:52:11 -0800612 @Override
613 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
614 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
615 if (!isVisible()) {
616 return false;
617 }
618 String text = null;
619 int index;
620 int count = mAllAppsList.size() + 1; // +1 is home
621 int pos = -1;
622 switch (mLastSelection) {
623 case SELECTION_ICONS:
624 index = mRollo.mState.selectedIconIndex;
625 if (index >= 0) {
626 ApplicationInfo info = mAllAppsList.get(index);
627 if (info.title != null) {
628 text = info.title.toString();
629 pos = index;
630 }
631 }
632 break;
633 case SELECTION_HOME:
634 text = getContext().getString(R.string.all_apps_home_button_label);
635 pos = count;
636 break;
637 }
638 if (text != null) {
Joe Onorato52a653f2009-11-11 14:52:11 -0800639 event.setEnabled(true);
640 event.getText().add(text);
641 //event.setContentDescription(text);
642 event.setItemCount(count);
643 event.setCurrentItemIndex(pos);
644 }
645 }
646 return false;
647 }
648
Joe Onorato5162ea92009-09-03 09:39:42 -0700649 public void setDragController(DragController dragger) {
650 mDragController = dragger;
651 }
652
653 public void onDropCompleted(View target, boolean success) {
654 }
655
Joe Onorato4db52312009-10-06 11:17:43 -0700656 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800657 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700658 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800659 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700660 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800661 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400662 cancelLongPress();
Joe Onorato6374c512010-01-06 20:42:25 -0800663 mNextZoom = zoom;
664 mAnimateNextZoom = animate;
665 // if we do setZoom while we don't have a surface, we won't
666 // get the callbacks that actually set mZoom.
667 if (mRollo == null || !mHaveSurface) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800668 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800669 mZoom = zoom;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800670 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700671 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800672 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700673 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400674 }
675
676 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800677 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800678 }
679
680 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800681 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400682 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700683
Joe Onorato9c1289c2009-08-17 11:03:03 -0400684 public void setApps(ArrayList<ApplicationInfo> list) {
685 mAllAppsList = list;
686 if (mRollo != null) {
Joe Onorato3ecbd812009-12-11 13:38:54 -0800687 mRollo.setApps(list);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400688 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400689 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700690 }
691
Joe Onoratoa8138d52009-10-06 19:25:30 -0700692 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500693 if (mAllAppsList == null) {
694 // Not done loading yet. We'll find out about it later.
695 return;
696 }
697
Joe Onoratoa8138d52009-10-06 19:25:30 -0700698 final int N = list.size();
Joe Onorato8eea3912009-12-09 13:01:06 -0800699 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700700 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
701 }
702
703 for (int i=0; i<N; i++) {
704 final ApplicationInfo item = list.get(i);
Joe Onoratob0c27f22009-12-01 16:19:38 -0800705 int index = Collections.binarySearch(mAllAppsList, item,
706 LauncherModel.APP_NAME_COMPARATOR);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700707 if (index < 0) {
708 index = -(index+1);
709 }
710 mAllAppsList.add(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -0800711 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700712 mRollo.addApp(index, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700713 }
714 }
715
Joe Onorato8eea3912009-12-09 13:01:06 -0800716 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700717 mRollo.saveAppsList();
718 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700719 }
720
Joe Onoratoa8138d52009-10-06 19:25:30 -0700721 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500722 if (mAllAppsList == null) {
723 // Not done loading yet. We'll find out about it later.
724 return;
725 }
726
Joe Onoratoa8138d52009-10-06 19:25:30 -0700727 final int N = list.size();
728 for (int i=0; i<N; i++) {
729 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400730 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700731 if (index >= 0) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800732 int ic = mRollo != null ? mRollo.mState.iconCount : 666;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700733 mAllAppsList.remove(index);
Joe Onorato8eea3912009-12-09 13:01:06 -0800734 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700735 mRollo.removeApp(index);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700736 }
737 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800738 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700739 // Try to recover. This should keep us from crashing for now.
740 }
741 }
742
Joe Onorato8eea3912009-12-09 13:01:06 -0800743 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700744 mRollo.saveAppsList();
745 }
746 }
747
748 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
749 // Just remove and add, because they may need to be re-sorted.
750 removeApps(list);
751 addApps(list);
752 }
753
Joe Onoratocb9f7982009-10-31 16:32:02 -0400754 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
755 ComponentName component = item.intent.getComponent();
756 final int N = list.size();
757 for (int i=0; i<N; i++) {
758 ApplicationInfo x = list.get(i);
759 if (x.intent.getComponent().equals(component)) {
760 return i;
761 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700762 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400763 return -1;
764 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700765
Joe Onoratoc567acb2009-08-31 14:34:43 -0700766 private static int countPages(int iconCount) {
767 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
768 int pages = iconCount / iconsPerPage;
769 if (pages*iconsPerPage != iconCount) {
770 pages++;
771 }
772 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400773 }
774
Joe Onoratocb75f362009-11-12 13:04:07 -0800775 class AAMessage extends RenderScript.RSMessage {
776 public void run() {
777 mPosX = ((float)mData[0]) / (1 << 16);
778 mVelocity = ((float)mData[1]) / (1 << 16);
779 mZoom = ((float)mData[2]) / (1 << 16);
780 mZoomDirty = false;
781 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800782 }
783
Joe Onorato93839052009-08-06 20:34:32 -0700784 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700785
Joe Onorato1feb3a82009-08-08 22:32:00 -0700786 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700787 private int mWidth;
788 private int mHeight;
789
790 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700791 private Script mScript;
792 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700793 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700794 private Script.Invokable mInvokeFling;
795 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800796 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700797
Jason Samscd689e12009-09-29 15:28:22 -0700798 private ProgramStore mPSIcons;
Jason Samsc8514792009-10-29 14:27:29 -0700799 private ProgramFragment mPFTexMip;
Jason Sams6ec11bc2010-01-19 17:56:52 -0800800 private ProgramFragment mPFTexMipAlpha;
Jason Samsc8514792009-10-29 14:27:29 -0700801 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700802 private ProgramVertex mPV;
Jason Samsb4ecab22010-01-19 16:43:26 -0800803 private ProgramVertex mPVCurve;
Jason Sams0aa71662009-10-02 18:43:18 -0700804 private SimpleMesh mMesh;
Jason Sams5612e432009-11-16 14:18:07 -0800805 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700806
Jason Samsb4ecab22010-01-19 16:43:26 -0800807 private Allocation mUniformAlloc;
808
Joe Onoratod63458b2009-10-15 21:19:09 -0700809 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500810 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700811 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700812
Joe Onoratobf15cb42009-08-07 14:33:40 -0700813 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700814 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700815 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700816
Joe Onoratobf15cb42009-08-07 14:33:40 -0700817 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700818 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700819 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700820 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700821
Joe Onorato6665c0f2009-09-02 15:27:24 -0700822 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700823 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700824
825 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400826 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700827
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700828 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700829 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700830
Jason Sams78aebd82009-09-15 13:06:59 -0700831 class BaseAlloc {
832 Allocation mAlloc;
833 Type mType;
834
835 void save() {
836 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700837 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700838 }
839
Jason Sams476339d2009-09-29 18:14:38 -0700840 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800841 return (Math.abs(mVelocity) < 0.4f) &&
842 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700843 }
844
Jason Sams78aebd82009-09-15 13:06:59 -0700845 class Params extends BaseAlloc {
846 Params() {
847 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
848 mAlloc = Allocation.createTyped(mRS, mType);
849 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700850 }
Jason Sams78aebd82009-09-15 13:06:59 -0700851 public int bubbleWidth;
852 public int bubbleHeight;
853 public int bubbleBitmapWidth;
854 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700855
Joe Onoratobcbeab82009-10-01 21:45:43 -0700856 public int homeButtonWidth;
857 public int homeButtonHeight;
858 public int homeButtonTextureWidth;
859 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700860 }
861
862 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700863 public float newPositionX;
864 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700865 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700866 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700867 public int selectedIconIndex = -1;
868 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700869 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700870 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700871 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700872
873 State() {
874 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
875 mAlloc = Allocation.createTyped(mRS, mType);
876 save();
877 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700878 }
879
880 public RolloRS() {
881 }
882
883 public void init(Resources res, int width, int height) {
884 mRes = res;
885 mWidth = width;
886 mHeight = height;
Jason Samscd689e12009-09-29 15:28:22 -0700887 initProgramVertex();
888 initProgramFragment();
889 initProgramStore();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700890 initGl();
891 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700892 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700893 initRs();
894 }
895
Jason Sams82e1a272010-02-04 17:32:57 -0800896 public void initMesh() {
Jason Samsb4ecab22010-01-19 16:43:26 -0800897 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Jason Sams0aa71662009-10-02 18:43:18 -0700898
Jason Samsb4ecab22010-01-19 16:43:26 -0800899 for (int ct=0; ct < 16; ct++) {
Jason Sams76512482010-02-04 16:31:35 -0800900 float pos = (1.f / (16.f - 1)) * ct;
Jason Samsb4ecab22010-01-19 16:43:26 -0800901 tm.addVertex(0.0f, pos);
902 tm.addVertex(1.0f, pos);
Jason Samsd8152b92009-10-13 17:19:10 -0700903 }
Jason Samsb4ecab22010-01-19 16:43:26 -0800904 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
Jason Samsd8152b92009-10-13 17:19:10 -0700905 tm.addTriangle(ct, ct+1, ct+2);
906 tm.addTriangle(ct+1, ct+3, ct+2);
907 }
Jason Samsb4ecab22010-01-19 16:43:26 -0800908 mMesh = tm.create();
909 mMesh.setName("SMCell");
Jason Samsd8152b92009-10-13 17:19:10 -0700910 }
911
Jason Sams5612e432009-11-16 14:18:07 -0800912 void resize(int w, int h) {
913 mPVA.setupProjectionNormalized(w, h);
914 mWidth = w;
915 mHeight = h;
916 }
917
Jason Samscd689e12009-09-29 15:28:22 -0700918 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800919 mPVA = new ProgramVertex.MatrixAllocation(mRS);
920 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700921
922 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700923 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700924 mPV = pvb.create();
925 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800926 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700927
Jason Samsb4ecab22010-01-19 16:43:26 -0800928 Element.Builder eb = new Element.Builder(mRS);
929 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
930 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
Jason Sams66ed54e2010-02-04 12:39:27 -0800931 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "BendPos");
932 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
Jason Samsb4ecab22010-01-19 16:43:26 -0800933 Element e = eb.create();
934
935 mUniformAlloc = Allocation.createSized(mRS, e, 1);
936
Jason Sams82e1a272010-02-04 17:32:57 -0800937 initMesh();
Jason Samsb4ecab22010-01-19 16:43:26 -0800938 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
939 String t = new String("void main() {\n" +
940 // Animation
941 " float ani = UNI_Position.z;\n" +
942
Jason Sams66ed54e2010-02-04 12:39:27 -0800943 " float bendY1 = UNI_BendPos.x;\n" +
944 " float bendY2 = UNI_BendPos.y;\n" +
Jason Sams76512482010-02-04 16:31:35 -0800945 " float bendAngle = 47.0 * (3.14 / 180.0);\n" +
946 " float bendDistance = bendY1 * 0.4;\n" +
Jason Sams66ed54e2010-02-04 12:39:27 -0800947 " float distanceDimLevel = 0.6;\n" +
948
949 " float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
950 " float aDy = cos(bendAngle);\n" +
951 " float aDz = sin(bendAngle);\n" +
952
Jason Samsb4ecab22010-01-19 16:43:26 -0800953 " float scale = (2.0 / 480.0);\n" +
954 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
955 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
956 " float y = 0.0;\n" +
957 " float z = 0.0;\n" +
958 " float lum = 1.0;\n" +
959
Jason Sams66ed54e2010-02-04 12:39:27 -0800960 " float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
961 " y += cv * aDy;\n" +
962 " z += -cv * aDz;\n" +
963 " cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" + // curve range
964 " lum += cv / bendDistance * distanceDimLevel;\n" +
965 " y += cv * cos(cv * bendStep);\n" +
966 " z += cv * sin(cv * bendStep);\n" +
Jason Samsb4ecab22010-01-19 16:43:26 -0800967
Jason Sams66ed54e2010-02-04 12:39:27 -0800968 " cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
969 " y += cv * aDy;\n" +
970 " z += cv * aDz;\n" +
971 " cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
972 " lum -= cv / bendDistance * distanceDimLevel;\n" +
973 " y += cv * cos(cv * bendStep);\n" +
974 " z += cv * sin(cv * bendStep);\n" +
Jason Samsb4ecab22010-01-19 16:43:26 -0800975
Jason Sams66ed54e2010-02-04 12:39:27 -0800976 " y += clamp(ys, bendY1, bendY2);\n" +
Jason Samsb4ecab22010-01-19 16:43:26 -0800977
978 " vec4 pos;\n" +
Jason Sams66ed54e2010-02-04 12:39:27 -0800979 " pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
980 " pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
981 " pos.z = z * UNI_ScaleOffset.x;\n" +
Jason Samsb4ecab22010-01-19 16:43:26 -0800982 " pos.w = 1.0;\n" +
983
984 " pos.x *= 1.0 + ani * 4.0;\n" +
985 " pos.y *= 1.0 + ani * 4.0;\n" +
986 " pos.z -= ani * 1.5;\n" +
987 " lum *= 1.0 - ani;\n" +
988
989 " gl_Position = UNI_MVP * pos;\n" +
990 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
991 " varTex0.xy = ATTRIB_position;\n" +
992 " varTex0.y = 1.0 - varTex0.y;\n" +
993 " varTex0.zw = vec2(0.0, 0.0);\n" +
994 "}\n");
995 sb.setShader(t);
996 sb.addConstant(mUniformAlloc.getType());
997 sb.addInput(mMesh.getVertexType(0).getElement());
998 mPVCurve = sb.create();
999 mPVCurve.setName("PVCurve");
1000 mPVCurve.bindAllocation(mPVA);
1001 mPVCurve.bindConstants(mUniformAlloc, 1);
1002
Joe Onorato93839052009-08-06 20:34:32 -07001003 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -07001004 }
Joe Onorato93839052009-08-06 20:34:32 -07001005
Jason Samscd689e12009-09-29 15:28:22 -07001006 private void initProgramFragment() {
1007 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -07001008 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -08001009 sb.setMag(Sampler.Value.NEAREST);
Jason Samscd689e12009-09-29 15:28:22 -07001010 sb.setWrapS(Sampler.Value.CLAMP);
1011 sb.setWrapT(Sampler.Value.CLAMP);
1012 Sampler linear = sb.create();
1013
1014 sb.setMin(Sampler.Value.NEAREST);
1015 sb.setMag(Sampler.Value.NEAREST);
1016 Sampler nearest = sb.create();
1017
Jason Sams72f1d312009-12-17 16:58:25 -08001018 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
Jason Samsb4ecab22010-01-19 16:43:26 -08001019 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
Jason Sams72f1d312009-12-17 16:58:25 -08001020 ProgramFragment.Builder.Format.RGBA, 0);
Jason Samsc8514792009-10-29 14:27:29 -07001021 mPFTexMip = bf.create();
1022 mPFTexMip.setName("PFTexMip");
1023 mPFTexMip.bindSampler(linear, 0);
1024
1025 mPFTexNearest = bf.create();
1026 mPFTexNearest.setName("PFTexNearest");
1027 mPFTexNearest.bindSampler(nearest, 0);
Jason Sams6ec11bc2010-01-19 17:56:52 -08001028
1029 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1030 ProgramFragment.Builder.Format.ALPHA, 0);
1031 mPFTexMipAlpha = bf.create();
1032 mPFTexMipAlpha.setName("PFTexMipAlpha");
1033 mPFTexMipAlpha.bindSampler(linear, 0);
1034
Jason Samscd689e12009-09-29 15:28:22 -07001035 }
1036
1037 private void initProgramStore() {
1038 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1039 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -07001040 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -07001041 bs.setDitherEnable(true);
1042 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1043 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1044 mPSIcons = bs.create();
1045 mPSIcons.setName("PSIcons");
Jason Samscd689e12009-09-29 15:28:22 -07001046 }
1047
1048 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001049 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -07001050 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -07001051 }
Jason Sams78aebd82009-09-15 13:06:59 -07001052
Joe Onorato1feb3a82009-08-08 22:32:00 -07001053 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -07001054 mParams = new Params();
1055 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001056
Joe Onoratobf15cb42009-08-07 14:33:40 -07001057 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -07001058
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001059 mParams.bubbleWidth = bubble.getBubbleWidth();
1060 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1061 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1062 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001063
Joe Onoratod63458b2009-10-15 21:19:09 -07001064 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1065 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1066 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001067 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1068 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1069 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001070 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1071 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1072 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001073 mParams.homeButtonWidth = 76;
1074 mParams.homeButtonHeight = 68;
1075 mParams.homeButtonTextureWidth = 128;
1076 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001077
Joe Onoratod63458b2009-10-15 21:19:09 -07001078 mState.homeButtonId = mHomeButtonNormal.getID();
1079
Joe Onorato1feb3a82009-08-08 22:32:00 -07001080 mParams.save();
1081 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082
Jason Sams1a94ee32010-01-20 13:34:30 -08001083 mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1084 Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001085 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001086
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001088 }
1089
Jason Sams37e7c2b2009-10-19 12:55:43 -07001090 private void initScript(int id) {
1091 }
1092
1093 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001094 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Samsd15f3ef2010-01-06 14:58:06 -08001095 sb.setScript(mRes, R.raw.allapps);
Joe Onorato93839052009-08-06 20:34:32 -07001096 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001097 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001098 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1099 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Samsb4ecab22010-01-19 16:43:26 -08001100 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001101 mInvokeMove = sb.addInvokable("move");
1102 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001103 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001104 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001105 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001106 mScript = sb.create();
1107 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1108 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1109 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1110 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1111 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Jason Samsb4ecab22010-01-19 16:43:26 -08001112 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Joe Onorato93839052009-08-06 20:34:32 -07001113
Jason Sams37e7c2b2009-10-19 12:55:43 -07001114 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001115 }
Joe Onorato93839052009-08-06 20:34:32 -07001116
Jason Sams20df7c72009-11-05 12:30:24 -08001117 void dirtyCheck() {
Jason Sams510ee042009-12-15 14:23:37 -08001118 if (mZoomDirty) {
Joe Onorato6374c512010-01-06 20:42:25 -08001119 setZoom(mNextZoom, mAnimateNextZoom);
Jason Sams20df7c72009-11-05 12:30:24 -08001120 }
1121 }
1122
Joe Onorato9c1289c2009-08-17 11:03:03 -04001123 private void setApps(ArrayList<ApplicationInfo> list) {
1124 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001125 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001126 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001127 allocCount = 1;
1128 }
1129
Joe Onorato9c1289c2009-08-17 11:03:03 -04001130 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001131 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001132 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133
1134 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001135 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001136 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001137
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001138 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140 mState.iconCount = count;
Joe Onorato8eea3912009-12-09 13:01:06 -08001141 for (int i=0; i < mState.iconCount; i++) {
1142 createAppIconAllocations(i, list.get(i));
Joe Onorato3ecbd812009-12-11 13:38:54 -08001143 }
Jason Sams510ee042009-12-15 14:23:37 -08001144 for (int i=0; i < mState.iconCount; i++) {
1145 uploadAppIcon(i, list.get(i));
Joe Onorato8eea3912009-12-09 13:01:06 -08001146 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001147 saveAppsList();
1148 }
1149
Joe Onorato3a8820b2009-11-10 15:06:42 -08001150 private void setZoom(float zoom, boolean animate) {
1151 mRollo.clearSelectedIcon();
1152 mRollo.setHomeSelected(SELECTED_NONE);
1153 if (zoom > 0.001f) {
1154 mRollo.mState.zoomTarget = zoom;
1155 } else {
1156 mRollo.mState.zoomTarget = 0;
1157 }
1158 mRollo.mState.save();
1159 if (!animate) {
1160 mRollo.mInvokeSetZoom.execute();
1161 }
1162 }
1163
Joe Onorato8eea3912009-12-09 13:01:06 -08001164 private void createAppIconAllocations(int index, ApplicationInfo item) {
Joe Onoratoa8138d52009-10-06 19:25:30 -07001165 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001166 Element.RGBA_8888(mRS), true);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001167 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Sams6ec11bc2010-01-19 17:56:52 -08001168 Element.A_8(mRS), true);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001169 mIconIds[index] = mIcons[index].getID();
1170 mLabelIds[index] = mLabels[index].getID();
1171 }
1172
Joe Onorato8eea3912009-12-09 13:01:06 -08001173 private void uploadAppIcon(int index, ApplicationInfo item) {
1174 if (mIconIds[index] != mIcons[index].getID()) {
1175 throw new IllegalStateException("uploadAppIcon index=" + index
1176 + " mIcons[index].getID=" + mIcons[index].getID()
1177 + " mIconsIds[index]=" + mIconIds[index]
1178 + " item=" + item);
1179 }
1180 mIcons[index].uploadToTexture(0);
1181 mLabels[index].uploadToTexture(0);
1182 }
1183
Joe Onoratoa8138d52009-10-06 19:25:30 -07001184 /**
1185 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1186 * fill in the values and call saveAppsList().
1187 */
1188 private void reallocAppsList(int count) {
1189 Allocation[] icons = new Allocation[count];
1190 int[] iconIds = new int[count];
1191 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1192
1193 Allocation[] labels = new Allocation[count];
1194 int[] labelIds = new int[count];
1195 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1196
Joe Onoratobf173f12009-12-08 13:29:38 -08001197 final int oldCount = mRollo.mState.iconCount;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001198
1199 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1200 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1201 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1202 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1203
1204 mIcons = icons;
1205 mIconIds = iconIds;
1206 mLabels = labels;
1207 mLabelIds = labelIds;
1208 }
1209
1210 /**
1211 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1212 */
1213 private void addApp(int index, ApplicationInfo item) {
1214 final int count = mState.iconCount - index;
1215 final int dest = index + 1;
1216
1217 System.arraycopy(mIcons, index, mIcons, dest, count);
1218 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1219 System.arraycopy(mLabels, index, mLabels, dest, count);
1220 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1221
Joe Onorato8eea3912009-12-09 13:01:06 -08001222 createAppIconAllocations(index, item);
Jason Sams510ee042009-12-15 14:23:37 -08001223 uploadAppIcon(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -08001224 mRollo.mState.iconCount++;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001225 }
1226
1227 /**
1228 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1229 */
1230 private void removeApp(int index) {
1231 final int count = mState.iconCount - index - 1;
1232 final int src = index + 1;
1233
1234 System.arraycopy(mIcons, src, mIcons, index, count);
1235 System.arraycopy(mIconIds, src, mIconIds, index, count);
1236 System.arraycopy(mLabels, src, mLabels, index, count);
1237 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1238
Joe Onoratoa276fc52009-12-08 17:02:02 -08001239 mRollo.mState.iconCount--;
Joe Onorato8eea3912009-12-09 13:01:06 -08001240 final int last = mState.iconCount;
Joe Onoratoa276fc52009-12-08 17:02:02 -08001241
Joe Onoratoa8138d52009-10-06 19:25:30 -07001242 mIcons[last] = null;
1243 mIconIds[last] = 0;
1244 mLabels[last] = null;
1245 mLabelIds[last] = 0;
1246 }
1247
1248 /**
1249 * Send the apps list structures to RS.
1250 */
1251 private void saveAppsList() {
1252 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001253
Joe Onoratoa8138d52009-10-06 19:25:30 -07001254 mAllocIconIds.data(mIconIds);
1255 mAllocLabelIds.data(mLabelIds);
1256
Jason Sams37e7c2b2009-10-19 12:55:43 -07001257 if (mScript != null) { // this happens when we init it
1258 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1259 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001260 }
1261
1262 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001263
1264 // Note: mScript may be null if we haven't initialized it yet.
1265 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001266 if (mInvokeResetWAR != null) {
1267 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001268 }
Joe Onoratob8315542010-02-03 20:39:25 -08001269 if (mScript != null) {
1270 mRS.contextBindRootScript(mScript);
1271 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001272 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001273
1274 void initTouchState() {
1275 int width = getWidth();
1276 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001277 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1278 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001279
Jason Sams37e7c2b2009-10-19 12:55:43 -07001280 int centerY = (height / 2);
1281 mTouchYBorders[0] = centerY - (cellHeight * 2);
1282 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001283 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001284 mTouchYBorders[3] = centerY + cellHeight;
1285 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001286
Joe Onorato6665c0f2009-09-02 15:27:24 -07001287 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001288 mTouchXBorders[0] = 0;
1289 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001290 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001291 mTouchXBorders[3] = centerX + (width / 4);
1292 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001293 }
1294
Joe Onorato664457d2009-10-28 16:30:34 -04001295 void fling() {
1296 mInvokeFling.execute();
1297 }
1298
1299 void move() {
1300 mInvokeMove.execute();
1301 }
1302
1303 void moveTo(float row) {
1304 mState.targetPos = row;
1305 mState.save();
1306 mInvokeMoveTo.execute();
1307 }
1308
Jason Sams37e7c2b2009-10-19 12:55:43 -07001309 int chooseTappedIcon(int x, int y, float pos) {
1310 // Adjust for scroll position if not zero.
1311 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001312
Joe Onorato6665c0f2009-09-02 15:27:24 -07001313 int col = -1;
1314 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001315 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1316 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1317 col = i;
1318 break;
1319 }
1320 }
1321 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1322 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1323 row = i;
1324 break;
1325 }
1326 }
1327
1328 if (row < 0 || col < 0) {
1329 return -1;
1330 }
1331
Joe Onorato664457d2009-10-28 16:30:34 -04001332 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001333 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001334
Joe Onorato664457d2009-10-28 16:30:34 -04001335 if (index >= mState.iconCount) {
1336 return -1;
1337 } else {
1338 return index;
1339 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001340 }
1341
Joe Onorato6665c0f2009-09-02 15:27:24 -07001342 /**
1343 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001344 *
1345 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001346 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001347 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001348 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001349 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001350 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001351 }
1352
Joe Onoratoc61cff92009-11-08 11:54:39 -05001353 /**
1354 * Select the icon at the given index.
1355 *
1356 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001357 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001358 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001359 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001360 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001361 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001362 if (mLastSelection == SELECTION_ICONS) {
1363 mLastSelection = SELECTION_NONE;
1364 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001365 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001366 if (pressed == SELECTED_FOCUSED) {
1367 mLastSelection = SELECTION_ICONS;
1368 }
1369
Joe Onorato52a653f2009-11-11 14:52:11 -08001370 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001371 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001372
Joe Onorato52a653f2009-11-11 14:52:11 -08001373 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001374 Bitmap selectionBitmap = mSelectionBitmap;
1375
1376 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001377 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001378 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001379
1380 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001381 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001382 mSelectedIcon.uploadToTexture(0);
1383 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001384
1385 if (prev != index) {
1386 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001387 //setContentDescription(info.title);
1388 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1389 }
1390 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001391 }
1392 }
1393
1394 /**
1395 * You need to call save() on mState on your own after calling this.
1396 */
1397 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001398 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001399 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001400
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001401 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001402 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001403 switch (mode) {
1404 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001405 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001406 break;
1407 case SELECTED_FOCUSED:
1408 mLastSelection = SELECTION_HOME;
1409 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001410 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001411 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1412 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001413 break;
1414 case SELECTED_PRESSED:
1415 mState.homeButtonId = mHomeButtonPressed.getID();
1416 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001417 }
1418 }
Joe Onoratobe386092009-11-17 17:32:16 -08001419
1420 public void dumpState() {
1421 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1422 Log.d(TAG, "mRollo.mHeight=" + mHeight);
1423 Log.d(TAG, "mRollo.mIcons=" + mIcons);
1424 if (mIcons != null) {
1425 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1426 }
1427 if (mIconIds != null) {
1428 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1429 }
1430 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1431 if (mLabelIds != null) {
1432 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1433 }
1434 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1435 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1436 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
Joe Onoratobe386092009-11-17 17:32:16 -08001437 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1438 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1439 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1440 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1441 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1442 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1443 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1444 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1445 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1446 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1447 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1448 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1449 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1450 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1451 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1452 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1453 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1454 }
1455 }
1456
1457 public void dumpState() {
1458 Log.d(TAG, "mRS=" + mRS);
1459 Log.d(TAG, "mRollo=" + mRollo);
1460 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1461 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1462 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1463 Log.d(TAG, "mLastSelection=" + mLastSelection);
1464 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1465 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1466 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1467 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1468 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1469 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1470 Log.d(TAG, "mZoom=" + mZoom);
1471 Log.d(TAG, "mPosX=" + mPosX);
1472 Log.d(TAG, "mVelocity=" + mVelocity);
1473 Log.d(TAG, "mMessageProc=" + mMessageProc);
1474 if (mRollo != null) {
1475 mRollo.dumpState();
1476 }
1477 if (mRS != null) {
1478 mRS.contextDump(0);
1479 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001480 }
Joe Onorato93839052009-08-06 20:34:32 -07001481}
1482
1483