blob: 321874b81abbe46bb35c9fcde8c44074b26a9302 [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;
Jason Samse48eae02010-02-09 15:55:48 -080034import android.renderscript.RenderScriptGL;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080035import android.renderscript.RenderScript;
36import android.renderscript.Sampler;
37import android.renderscript.Script;
38import android.renderscript.ScriptC;
39import android.renderscript.SimpleMesh;
40import android.renderscript.Type;
Joe Onorato93839052009-08-06 20:34:32 -070041import android.util.AttributeSet;
42import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070043import android.view.KeyEvent;
44import android.view.MotionEvent;
Joe Onoratob39e51a2009-10-28 15:47:49 -040045import android.view.SoundEffectConstants;
Joe Onorato93839052009-08-06 20:34:32 -070046import android.view.SurfaceHolder;
Joe Onoratod769a632009-08-11 17:09:02 -070047import android.view.VelocityTracker;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080048import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070049import android.view.ViewConfiguration;
Joe Onorato52a653f2009-11-11 14:52:11 -080050import android.view.accessibility.AccessibilityEvent;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080051
52import java.util.ArrayList;
Joe Onoratobe386092009-11-17 17:32:16 -080053import java.util.Arrays;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080054import java.util.Collections;
55import java.util.Comparator;
Joe Onorato93839052009-08-06 20:34:32 -070056
57
Joe Onorato6665c0f2009-09-02 15:27:24 -070058public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070059 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040060 private static final String TAG = "Launcher.AllAppsView";
61
Joe Onoratofb0ca672009-09-14 17:55:46 -040062 /** Bit for mLocks for when there are icons being loaded. */
63 private static final int LOCK_ICONS_PENDING = 1;
64
Joe Onorato68ffd102009-10-15 17:59:43 -070065 private static final int TRACKING_NONE = 0;
66 private static final int TRACKING_FLING = 1;
67 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070068
Joe Onoratoeb8325a2009-11-08 13:20:30 -050069 private static final int SELECTED_NONE = 0;
70 private static final int SELECTED_FOCUSED = 1;
71 private static final int SELECTED_PRESSED = 2;
72
73 private static final int SELECTION_NONE = 0;
74 private static final int SELECTION_ICONS = 1;
75 private static final int SELECTION_HOME = 2;
76
Joe Onorato6665c0f2009-09-02 15:27:24 -070077 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070078 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040079
80 /** When this is 0, modifications are allowed, when it's not, they're not.
81 * TODO: What about scrolling? */
82 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070083
Joe Onorato82ca5502009-10-15 16:59:23 -070084 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070085 private int mMaxFlingVelocity;
86
Joe Onoratobcbeab82009-10-01 21:45:43 -070087 private Defines mDefines = new Defines();
Jason Samse48eae02010-02-09 15:55:48 -080088 private RenderScriptGL mRS;
Joe Onorato1feb3a82009-08-08 22:32:00 -070089 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070091
Mike Cleron7d5d7462009-10-20 14:06:00 -070092 /**
93 * True when we are using arrow keys or trackball to drive navigation
94 */
95 private boolean mArrowNavigation = false;
Joe Onoratoeb8325a2009-11-08 13:20:30 -050096 private boolean mStartedScrolling;
97
98 /**
99 * Used to keep track of the selection when AllAppsView loses window focus.
100 * One of the SELECTION_ constants.
101 */
102 private int mLastSelection;
Jason Sams2e19c052009-10-20 18:19:55 -0700103
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800104 /**
105 * Used to keep track of the selection when AllAppsView loses window focus
106 */
107 private int mLastSelectedIcon;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500108
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800109 private VelocityTracker mVelocityTracker;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700110 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700111 private int mMotionDownRawX;
112 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700113 private int mDownIconIndex = -1;
114 private int mCurrentIconIndex = -1;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800115
Mike Cleronb64b67a2009-11-08 14:56:25 -0800116 private boolean mShouldGainFocus;
117
Joe Onorato6374c512010-01-06 20:42:25 -0800118 private boolean mHaveSurface = false;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800119 private boolean mZoomDirty = false;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800120 private boolean mAnimateNextZoom;
Joe Onorato6374c512010-01-06 20:42:25 -0800121 private float mNextZoom;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800122 private float mZoom;
123 private float mPosX;
124 private float mVelocity;
125 private AAMessage mMessageProc;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700126
Joe Onorato6665c0f2009-09-02 15:27:24 -0700127 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700128 public static final int ALLOC_PARAMS = 0;
129 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700130 public static final int ALLOC_ICON_IDS = 3;
131 public static final int ALLOC_LABEL_IDS = 4;
Jason Samsb4ecab22010-01-19 16:43:26 -0800132 public static final int ALLOC_VP_CONSTANTS = 5;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700133
134 public static final int COLUMNS_PER_PAGE = 4;
135 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700136
Joe Onorato6665c0f2009-09-02 15:27:24 -0700137 public static final int ICON_WIDTH_PX = 64;
Jason Sams1a94ee32010-01-20 13:34:30 -0800138 public static final int ICON_TEXTURE_WIDTH_PX = 74;
139 public static final int SELECTION_TEXTURE_WIDTH_PX = 74 + 20;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700140
141 public static final int ICON_HEIGHT_PX = 64;
Jason Sams1a94ee32010-01-20 13:34:30 -0800142 public static final int ICON_TEXTURE_HEIGHT_PX = 74;
143 public static final int SELECTION_TEXTURE_HEIGHT_PX = 74 + 20;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700144 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700145
146 public AllAppsView(Context context, AttributeSet attrs) {
147 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700148 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400149 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700150 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700151 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700152 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700153 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
154
Joe Onorato6665c0f2009-09-02 15:27:24 -0700155 setOnClickListener(this);
156 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700157 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700158 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samse26d9fc2009-11-12 14:00:43 -0800159
160 mRS = createRenderScript(true);
161 }
162
163 @Override
164 protected void onDetachedFromWindow() {
165 destroyRenderScript();
Joe Onorato93839052009-08-06 20:34:32 -0700166 }
167
Joe Onoratob39e51a2009-10-28 15:47:49 -0400168 /**
169 * If you have an attached click listener, View always plays the click sound!?!?
170 * Deal with sound effects by hand.
171 */
172 public void reallyPlaySoundEffect(int sound) {
173 boolean old = isSoundEffectsEnabled();
174 setSoundEffectsEnabled(true);
175 playSoundEffect(sound);
176 setSoundEffectsEnabled(old);
177 }
178
Joe Onorato7c312c12009-08-13 21:36:53 -0700179 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
180 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700181 }
182
Joe Onorato6665c0f2009-09-02 15:27:24 -0700183 public void setLauncher(Launcher launcher) {
184 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700185 }
186
Joe Onorato1feb3a82009-08-08 22:32:00 -0700187 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700188 public void surfaceDestroyed(SurfaceHolder holder) {
189 super.surfaceDestroyed(holder);
Joe Onoratofab74402009-11-11 16:05:23 -0800190 // Without this, we leak mMessageCallback which leaks the context.
191 mRS.mMessageCallback = null;
Joe Onorato6374c512010-01-06 20:42:25 -0800192 // We may lose any callbacks that are pending, so make sure that we re-sync that
193 // on the next surfaceChanged.
194 mZoomDirty = true;
195 mHaveSurface = false;
Joe Onorato7bb17492009-09-24 17:51:01 -0700196 }
197
198 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700199 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800200 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700201
Joe Onorato080d9b62009-11-02 12:01:11 -0500202 super.surfaceChanged(holder, format, w, h);
203
Joe Onorato6374c512010-01-06 20:42:25 -0800204 mHaveSurface = true;
205
Jason Samse26d9fc2009-11-12 14:00:43 -0800206 if (mRollo == null) {
Jason Sams90396672009-11-03 13:59:34 -0800207 mRollo = new RolloRS();
208 mRollo.init(getResources(), w, h);
209 if (mAllAppsList != null) {
210 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800211 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800212 if (mShouldGainFocus) {
213 gainFocus();
214 mShouldGainFocus = false;
215 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 }
Jason Samse26d9fc2009-11-12 14:00:43 -0800217 mRollo.dirtyCheck();
Jason Sams5612e432009-11-16 14:18:07 -0800218 mRollo.resize(w, h);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700219
Joe Onoratocb75f362009-11-12 13:04:07 -0800220 mRS.mMessageCallback = mMessageProc = new AAMessage();
221
Joe Onoratoc567acb2009-08-31 14:34:43 -0700222 Resources res = getContext().getResources();
223 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700224
Jason Sams76512482010-02-04 16:31:35 -0800225
226 if (mRollo.mUniformAlloc != null) {
227 float tf[] = new float[] {72.f, 72.f,
228 120.f, 120.f, 0.f, 0.f,
229 120.f, 680.f,
230 (2.f / 480.f), 0, -((float)w / 2) - 0.25f, -380.25f};
231 if (w > h) {
232 tf[6] = 40.f;
233 tf[7] = h - 40.f;
234 tf[9] = 1.f;
235 tf[10] = -((float)w / 2) - 0.25f;
236 tf[11] = -((float)h / 2) - 0.25f;
237 }
238
239 mRollo.mUniformAlloc.data(tf);
240 }
241
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800242 //long endTime = SystemClock.uptimeMillis();
243 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700244 }
Jason Sams2e19c052009-10-20 18:19:55 -0700245
Joe Onorato93839052009-08-06 20:34:32 -0700246 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800247 public void onWindowFocusChanged(boolean hasWindowFocus) {
248 super.onWindowFocusChanged(hasWindowFocus);
249 if (mArrowNavigation) {
250 if (!hasWindowFocus) {
251 // Clear selection when we lose window focus
252 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500253 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800254 mRollo.clearSelectedIcon();
255 mRollo.mState.save();
256 } else if (hasWindowFocus) {
257 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500258 if (mLastSelection == SELECTION_ICONS) {
259 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800260 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500261 Defines.COLUMNS_PER_PAGE;
262 if (selection < 0 || // No selection
263 selection < firstIcon || // off the top of the screen
264 selection >= mRollo.mState.iconCount || // past last icon
265 selection >= firstIcon + // past last icon on screen
266 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
267 selection = firstIcon;
268 }
269
270 // Select the first icon when we gain window focus
271 mRollo.selectIcon(selection, SELECTED_FOCUSED);
272 mRollo.mState.save();
273 } else if (mLastSelection == SELECTION_HOME) {
274 mRollo.setHomeSelected(SELECTED_FOCUSED);
275 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800276 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800277 }
278 }
279 }
280 }
281
282 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700283 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
284 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400285
286 if (!isVisible()) {
287 return;
288 }
289
Mike Cleron7d5d7462009-10-20 14:06:00 -0700290 if (gainFocus) {
Jason Sams510ee042009-12-15 14:23:37 -0800291 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800292 gainFocus();
293 } else {
294 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700295 }
296 } else {
Joe Onorato8eea3912009-12-09 13:01:06 -0800297 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800298 if (mArrowNavigation) {
299 // Clear selection when we lose focus
300 mRollo.clearSelectedIcon();
301 mRollo.setHomeSelected(SELECTED_NONE);
302 mRollo.mState.save();
303 mArrowNavigation = false;
304 }
305 } else {
306 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700307 }
308 }
309 }
310
Mike Cleronb64b67a2009-11-08 14:56:25 -0800311 private void gainFocus() {
312 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
313 // Select the first icon when we gain keyboard focus
314 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800315 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800316 SELECTED_FOCUSED);
317 mRollo.mState.save();
318 }
319 }
320
Mike Cleron7d5d7462009-10-20 14:06:00 -0700321 @Override
322 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700323
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800324 boolean handled = false;
Jason Samse26d9fc2009-11-12 14:00:43 -0800325
Joe Onorato859b3a72009-10-28 15:17:01 -0400326 if (!isVisible()) {
327 return false;
328 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500329 final int iconCount = mRollo.mState.iconCount;
330
Mike Cleron7d5d7462009-10-20 14:06:00 -0700331 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
332 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500333 if (mLastSelection == SELECTION_HOME) {
334 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
335 mLauncher.closeAllApps(true);
336 } else {
337 int whichApp = mRollo.mState.selectedIconIndex;
338 if (whichApp >= 0) {
339 ApplicationInfo app = mAllAppsList.get(whichApp);
340 mLauncher.startActivitySafely(app.intent);
341 handled = true;
342 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700343 }
344 }
345 }
Jason Sams2e19c052009-10-20 18:19:55 -0700346
Joe Onorato478730f2009-11-16 18:54:43 -0800347 if (iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700348 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700349
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800350 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800351 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700352
Mike Cleron7d5d7462009-10-20 14:06:00 -0700353 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500354 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700355
Mike Cleron7d5d7462009-10-20 14:06:00 -0700356 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500357 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700358 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700359
Mike Cleron7d5d7462009-10-20 14:06:00 -0700360 int newSelection = currentSelection;
361
362 switch (keyCode) {
363 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500364 if (mLastSelection == SELECTION_HOME) {
365 mRollo.setHomeSelected(SELECTED_NONE);
366 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
367 if (lastRowCount == 0) {
368 lastRowCount = Defines.COLUMNS_PER_PAGE;
369 }
370 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
371 if (newSelection >= iconCount) {
372 newSelection = iconCount-1;
373 }
374 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
375 - (Defines.ROWS_PER_PAGE - 1);
376 if (target < 0) {
377 target = 0;
378 }
379 if (currentTopRow != target) {
380 mRollo.moveTo(target);
381 }
382 } else {
383 if (currentPageRow > 0) {
384 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
385 } else if (currentTopRow > 0) {
386 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
387 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
Joe Onoratoaf5b4cb2009-12-08 17:51:22 -0800388 } else if (currentPageRow != 0) {
389 newSelection = currentTopRow * Defines.ROWS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500390 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700391 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800392 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700393 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800394
Joe Onoratoa13f5742009-11-02 17:15:19 -0500395 case KeyEvent.KEYCODE_DPAD_DOWN: {
396 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
397 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
398 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500399 if (mLastSelection != SELECTION_HOME) {
400 if (currentRow < rowCount-1) {
401 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato478730f2009-11-16 18:54:43 -0800402 if (currentSelection < 0) {
403 newSelection = 0;
404 } else {
405 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
406 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500407 if (newSelection >= iconCount) {
408 // Go from D to G in this arrangement:
409 // A B C D
410 // E F G
411 newSelection = iconCount - 1;
412 }
413 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
414 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
415 Defines.ROWS_PER_PAGE + 1);
416 }
417 } else {
418 newSelection = -1;
419 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700420 }
421 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800422 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700423 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500424 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700425 case KeyEvent.KEYCODE_DPAD_LEFT:
Joe Onoratoe9a3f3d2010-02-01 18:36:43 -0500426 if (mLastSelection != SELECTION_HOME) {
427 if (currentPageCol > 0) {
428 newSelection = currentSelection - 1;
429 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700430 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800431 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700432 break;
433 case KeyEvent.KEYCODE_DPAD_RIGHT:
Joe Onoratoe9a3f3d2010-02-01 18:36:43 -0500434 if (mLastSelection != SELECTION_HOME) {
435 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
436 (currentSelection < iconCount - 1)) {
437 newSelection = currentSelection + 1;
438 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700439 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800440 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700441 break;
442 }
443 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500444 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700445 mRollo.mState.save();
446 }
447 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800448 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700449 }
450
Joe Onorato93839052009-08-06 20:34:32 -0700451 @Override
452 public boolean onTouchEvent(MotionEvent ev)
453 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700454 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700455
Joe Onorato7bb17492009-09-24 17:51:01 -0700456 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700457 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700458 }
459
Joe Onoratofb0ca672009-09-14 17:55:46 -0400460 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700461 return true;
462 }
463
464 super.onTouchEvent(ev);
465
Joe Onoratofb0ca672009-09-14 17:55:46 -0400466 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700467 int y = (int)ev.getY();
468
Joe Onoratobcbeab82009-10-01 21:45:43 -0700469 int action = ev.getAction();
470 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400471 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700472 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
473 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500474 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700475 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700476 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700477 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700478 mTouchTracking = TRACKING_FLING;
479
480 mMotionDownRawX = (int)ev.getRawX();
481 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700482
Mike Cleron7d5d7462009-10-20 14:06:00 -0700483 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700484 mRollo.mState.newTouchDown = 1;
485
486 if (!mRollo.checkClickOK()) {
487 mRollo.clearSelectedIcon();
488 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700489 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800490 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700491 if (mDownIconIndex < 0) {
492 // if nothing was selected, no long press.
493 cancelLongPress();
494 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700495 }
496 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700497 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800498 mVelocityTracker = VelocityTracker.obtain();
499 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700500 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700501 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400502 break;
503 case MotionEvent.ACTION_MOVE:
504 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700505 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500506 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
507 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700508 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700509 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700510 int rawX = (int)ev.getRawX();
511 int rawY = (int)ev.getRawY();
512 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700513 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700514
Joe Onorato82ca5502009-10-15 16:59:23 -0700515 if (!mStartedScrolling && slop < mSlop) {
516 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700517 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800518 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700519 if (mDownIconIndex != mCurrentIconIndex) {
520 // If a different icon is selected, don't allow it to be picked up.
521 // This handles off-axis dragging.
522 cancelLongPress();
523 mCurrentIconIndex = -1;
524 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700525 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700526 if (!mStartedScrolling) {
527 cancelLongPress();
528 mCurrentIconIndex = -1;
529 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700530 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700531 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700532 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700533
Joe Onoratobcbeab82009-10-01 21:45:43 -0700534 mStartedScrolling = true;
535 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800536 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700537 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700538 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400539 }
540 break;
541 case MotionEvent.ACTION_UP:
542 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700543 if (mTouchTracking == TRACKING_HOME) {
544 if (action == MotionEvent.ACTION_UP) {
545 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400546 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700547 mLauncher.closeAllApps(true);
548 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500549 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700550 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700551 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700552 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700553 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700554 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700555 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700556
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800557 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
558 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700559 mRollo.clearSelectedIcon();
560 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700561 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700562
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800563 if (mVelocityTracker != null) {
564 mVelocityTracker.recycle();
565 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700566 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700567 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700568 mTouchTracking = TRACKING_NONE;
569 break;
Joe Onorato93839052009-08-06 20:34:32 -0700570 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700571
572 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700573 }
574
Joe Onorato6665c0f2009-09-02 15:27:24 -0700575 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700576 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400577 return;
578 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700579 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
580 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400581 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700582 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700583 mLauncher.startActivitySafely(app.intent);
584 }
585 }
586
587 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700588 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400589 return true;
590 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700591 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
592 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
593 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700594
Jason Samsb4ecab22010-01-19 16:43:26 -0800595 Bitmap bmp = app.iconBitmap;
596 final int w = bmp.getWidth();
597 final int h = bmp.getHeight();
598
Joe Onorato5162ea92009-09-03 09:39:42 -0700599 // We don't really have an accurate location to use. This will do.
Jason Samsb4ecab22010-01-19 16:43:26 -0800600 int screenX = mMotionDownRawX - (w / 2);
601 int screenY = mMotionDownRawY - h;
Joe Onorato5162ea92009-09-03 09:39:42 -0700602
Joe Onoratobcbeab82009-10-01 21:45:43 -0700603 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
604 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Jason Samsb4ecab22010-01-19 16:43:26 -0800605 mDragController.startDrag(bmp, screenX, screenY,
606 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700607
Joe Onorato7bb17492009-09-24 17:51:01 -0700608 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700609 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700610 return true;
611 }
612
Joe Onorato52a653f2009-11-11 14:52:11 -0800613 @Override
614 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
615 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
616 if (!isVisible()) {
617 return false;
618 }
619 String text = null;
620 int index;
621 int count = mAllAppsList.size() + 1; // +1 is home
622 int pos = -1;
623 switch (mLastSelection) {
624 case SELECTION_ICONS:
625 index = mRollo.mState.selectedIconIndex;
626 if (index >= 0) {
627 ApplicationInfo info = mAllAppsList.get(index);
628 if (info.title != null) {
629 text = info.title.toString();
630 pos = index;
631 }
632 }
633 break;
634 case SELECTION_HOME:
635 text = getContext().getString(R.string.all_apps_home_button_label);
636 pos = count;
637 break;
638 }
639 if (text != null) {
Joe Onorato52a653f2009-11-11 14:52:11 -0800640 event.setEnabled(true);
641 event.getText().add(text);
642 //event.setContentDescription(text);
643 event.setItemCount(count);
644 event.setCurrentItemIndex(pos);
645 }
646 }
647 return false;
648 }
649
Joe Onorato5162ea92009-09-03 09:39:42 -0700650 public void setDragController(DragController dragger) {
651 mDragController = dragger;
652 }
653
654 public void onDropCompleted(View target, boolean success) {
655 }
656
Joe Onorato4db52312009-10-06 11:17:43 -0700657 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800658 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700659 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800660 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700661 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800662 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400663 cancelLongPress();
Joe Onorato6374c512010-01-06 20:42:25 -0800664 mNextZoom = zoom;
665 mAnimateNextZoom = animate;
666 // if we do setZoom while we don't have a surface, we won't
667 // get the callbacks that actually set mZoom.
668 if (mRollo == null || !mHaveSurface) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800669 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800670 mZoom = zoom;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800671 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700672 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800673 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700674 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400675 }
676
677 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800678 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800679 }
680
681 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800682 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400683 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700684
Joe Onorato9c1289c2009-08-17 11:03:03 -0400685 public void setApps(ArrayList<ApplicationInfo> list) {
686 mAllAppsList = list;
687 if (mRollo != null) {
Joe Onorato3ecbd812009-12-11 13:38:54 -0800688 mRollo.setApps(list);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400689 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400690 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700691 }
692
Joe Onoratoa8138d52009-10-06 19:25:30 -0700693 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500694 if (mAllAppsList == null) {
695 // Not done loading yet. We'll find out about it later.
696 return;
697 }
698
Joe Onoratoa8138d52009-10-06 19:25:30 -0700699 final int N = list.size();
Joe Onorato8eea3912009-12-09 13:01:06 -0800700 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700701 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
702 }
703
704 for (int i=0; i<N; i++) {
705 final ApplicationInfo item = list.get(i);
Joe Onoratob0c27f22009-12-01 16:19:38 -0800706 int index = Collections.binarySearch(mAllAppsList, item,
707 LauncherModel.APP_NAME_COMPARATOR);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700708 if (index < 0) {
709 index = -(index+1);
710 }
711 mAllAppsList.add(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -0800712 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700713 mRollo.addApp(index, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700714 }
715 }
716
Joe Onorato8eea3912009-12-09 13:01:06 -0800717 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700718 mRollo.saveAppsList();
719 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700720 }
721
Joe Onoratoa8138d52009-10-06 19:25:30 -0700722 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500723 if (mAllAppsList == null) {
724 // Not done loading yet. We'll find out about it later.
725 return;
726 }
727
Joe Onoratoa8138d52009-10-06 19:25:30 -0700728 final int N = list.size();
729 for (int i=0; i<N; i++) {
730 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400731 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700732 if (index >= 0) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800733 int ic = mRollo != null ? mRollo.mState.iconCount : 666;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700734 mAllAppsList.remove(index);
Joe Onorato8eea3912009-12-09 13:01:06 -0800735 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700736 mRollo.removeApp(index);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700737 }
738 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800739 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700740 // Try to recover. This should keep us from crashing for now.
741 }
742 }
743
Joe Onorato8eea3912009-12-09 13:01:06 -0800744 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700745 mRollo.saveAppsList();
746 }
747 }
748
749 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
750 // Just remove and add, because they may need to be re-sorted.
751 removeApps(list);
752 addApps(list);
753 }
754
Joe Onoratocb9f7982009-10-31 16:32:02 -0400755 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
756 ComponentName component = item.intent.getComponent();
757 final int N = list.size();
758 for (int i=0; i<N; i++) {
759 ApplicationInfo x = list.get(i);
760 if (x.intent.getComponent().equals(component)) {
761 return i;
762 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700763 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400764 return -1;
765 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700766
Joe Onoratoc567acb2009-08-31 14:34:43 -0700767 private static int countPages(int iconCount) {
768 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
769 int pages = iconCount / iconsPerPage;
770 if (pages*iconsPerPage != iconCount) {
771 pages++;
772 }
773 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400774 }
775
Joe Onoratocb75f362009-11-12 13:04:07 -0800776 class AAMessage extends RenderScript.RSMessage {
777 public void run() {
778 mPosX = ((float)mData[0]) / (1 << 16);
779 mVelocity = ((float)mData[1]) / (1 << 16);
780 mZoom = ((float)mData[2]) / (1 << 16);
781 mZoomDirty = false;
782 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800783 }
784
Joe Onorato93839052009-08-06 20:34:32 -0700785 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700786
Joe Onorato1feb3a82009-08-08 22:32:00 -0700787 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700788 private int mWidth;
789 private int mHeight;
790
791 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700792 private Script mScript;
793 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700794 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700795 private Script.Invokable mInvokeFling;
796 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800797 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700798
Jason Samscd689e12009-09-29 15:28:22 -0700799 private ProgramStore mPSIcons;
Jason Samsc8514792009-10-29 14:27:29 -0700800 private ProgramFragment mPFTexMip;
Jason Sams6ec11bc2010-01-19 17:56:52 -0800801 private ProgramFragment mPFTexMipAlpha;
Jason Samsc8514792009-10-29 14:27:29 -0700802 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700803 private ProgramVertex mPV;
Jason Samsb4ecab22010-01-19 16:43:26 -0800804 private ProgramVertex mPVCurve;
Jason Sams0aa71662009-10-02 18:43:18 -0700805 private SimpleMesh mMesh;
Jason Sams5612e432009-11-16 14:18:07 -0800806 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700807
Jason Samsb4ecab22010-01-19 16:43:26 -0800808 private Allocation mUniformAlloc;
809
Joe Onoratod63458b2009-10-15 21:19:09 -0700810 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500811 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700812 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700813
Joe Onoratobf15cb42009-08-07 14:33:40 -0700814 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700815 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700816 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700817
Joe Onoratobf15cb42009-08-07 14:33:40 -0700818 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700819 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700820 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700821 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700822
Joe Onorato6665c0f2009-09-02 15:27:24 -0700823 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700824 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700825
826 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400827 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700828
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700829 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700830 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700831
Jason Sams78aebd82009-09-15 13:06:59 -0700832 class BaseAlloc {
833 Allocation mAlloc;
834 Type mType;
835
836 void save() {
837 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700838 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700839 }
840
Jason Sams476339d2009-09-29 18:14:38 -0700841 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800842 return (Math.abs(mVelocity) < 0.4f) &&
843 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700844 }
845
Jason Sams78aebd82009-09-15 13:06:59 -0700846 class Params extends BaseAlloc {
847 Params() {
848 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
849 mAlloc = Allocation.createTyped(mRS, mType);
850 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700851 }
Jason Sams78aebd82009-09-15 13:06:59 -0700852 public int bubbleWidth;
853 public int bubbleHeight;
854 public int bubbleBitmapWidth;
855 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700856
Joe Onoratobcbeab82009-10-01 21:45:43 -0700857 public int homeButtonWidth;
858 public int homeButtonHeight;
859 public int homeButtonTextureWidth;
860 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700861 }
862
863 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700864 public float newPositionX;
865 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700866 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700867 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700868 public int selectedIconIndex = -1;
869 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700870 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700871 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700872 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700873
874 State() {
875 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
876 mAlloc = Allocation.createTyped(mRS, mType);
877 save();
878 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700879 }
880
881 public RolloRS() {
882 }
883
884 public void init(Resources res, int width, int height) {
885 mRes = res;
886 mWidth = width;
887 mHeight = height;
Jason Samscd689e12009-09-29 15:28:22 -0700888 initProgramVertex();
889 initProgramFragment();
890 initProgramStore();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700891 initGl();
892 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700893 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700894 initRs();
895 }
896
Jason Sams82e1a272010-02-04 17:32:57 -0800897 public void initMesh() {
Jason Samsb4ecab22010-01-19 16:43:26 -0800898 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Jason Sams0aa71662009-10-02 18:43:18 -0700899
Jason Samsb4ecab22010-01-19 16:43:26 -0800900 for (int ct=0; ct < 16; ct++) {
Jason Sams76512482010-02-04 16:31:35 -0800901 float pos = (1.f / (16.f - 1)) * ct;
Jason Samsb4ecab22010-01-19 16:43:26 -0800902 tm.addVertex(0.0f, pos);
903 tm.addVertex(1.0f, pos);
Jason Samsd8152b92009-10-13 17:19:10 -0700904 }
Jason Samsb4ecab22010-01-19 16:43:26 -0800905 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
Jason Samsd8152b92009-10-13 17:19:10 -0700906 tm.addTriangle(ct, ct+1, ct+2);
907 tm.addTriangle(ct+1, ct+3, ct+2);
908 }
Jason Samsb4ecab22010-01-19 16:43:26 -0800909 mMesh = tm.create();
910 mMesh.setName("SMCell");
Jason Samsd8152b92009-10-13 17:19:10 -0700911 }
912
Jason Sams5612e432009-11-16 14:18:07 -0800913 void resize(int w, int h) {
914 mPVA.setupProjectionNormalized(w, h);
915 mWidth = w;
916 mHeight = h;
917 }
918
Jason Samscd689e12009-09-29 15:28:22 -0700919 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800920 mPVA = new ProgramVertex.MatrixAllocation(mRS);
921 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700922
923 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700924 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700925 mPV = pvb.create();
926 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800927 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700928
Jason Samsb4ecab22010-01-19 16:43:26 -0800929 Element.Builder eb = new Element.Builder(mRS);
930 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
931 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
Jason Sams66ed54e2010-02-04 12:39:27 -0800932 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "BendPos");
933 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
Jason Samsb4ecab22010-01-19 16:43:26 -0800934 Element e = eb.create();
935
936 mUniformAlloc = Allocation.createSized(mRS, e, 1);
937
Jason Sams82e1a272010-02-04 17:32:57 -0800938 initMesh();
Jason Samsb4ecab22010-01-19 16:43:26 -0800939 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
940 String t = new String("void main() {\n" +
941 // Animation
942 " float ani = UNI_Position.z;\n" +
943
Jason Sams66ed54e2010-02-04 12:39:27 -0800944 " float bendY1 = UNI_BendPos.x;\n" +
945 " float bendY2 = UNI_BendPos.y;\n" +
Jason Sams76512482010-02-04 16:31:35 -0800946 " float bendAngle = 47.0 * (3.14 / 180.0);\n" +
947 " float bendDistance = bendY1 * 0.4;\n" +
Jason Sams66ed54e2010-02-04 12:39:27 -0800948 " float distanceDimLevel = 0.6;\n" +
949
950 " float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
951 " float aDy = cos(bendAngle);\n" +
952 " float aDz = sin(bendAngle);\n" +
953
Jason Samsb4ecab22010-01-19 16:43:26 -0800954 " float scale = (2.0 / 480.0);\n" +
955 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
956 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
957 " float y = 0.0;\n" +
958 " float z = 0.0;\n" +
959 " float lum = 1.0;\n" +
960
Jason Sams66ed54e2010-02-04 12:39:27 -0800961 " float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
962 " y += cv * aDy;\n" +
963 " z += -cv * aDz;\n" +
964 " cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" + // curve range
965 " lum += cv / bendDistance * distanceDimLevel;\n" +
966 " y += cv * cos(cv * bendStep);\n" +
967 " z += cv * sin(cv * bendStep);\n" +
Jason Samsb4ecab22010-01-19 16:43:26 -0800968
Jason Sams66ed54e2010-02-04 12:39:27 -0800969 " cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
970 " y += cv * aDy;\n" +
971 " z += cv * aDz;\n" +
972 " cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
973 " lum -= cv / bendDistance * distanceDimLevel;\n" +
974 " y += cv * cos(cv * bendStep);\n" +
975 " z += cv * sin(cv * bendStep);\n" +
Jason Samsb4ecab22010-01-19 16:43:26 -0800976
Jason Sams66ed54e2010-02-04 12:39:27 -0800977 " y += clamp(ys, bendY1, bendY2);\n" +
Jason Samsb4ecab22010-01-19 16:43:26 -0800978
979 " vec4 pos;\n" +
Jason Sams66ed54e2010-02-04 12:39:27 -0800980 " pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
981 " pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
982 " pos.z = z * UNI_ScaleOffset.x;\n" +
Jason Samsb4ecab22010-01-19 16:43:26 -0800983 " pos.w = 1.0;\n" +
984
985 " pos.x *= 1.0 + ani * 4.0;\n" +
986 " pos.y *= 1.0 + ani * 4.0;\n" +
987 " pos.z -= ani * 1.5;\n" +
988 " lum *= 1.0 - ani;\n" +
989
990 " gl_Position = UNI_MVP * pos;\n" +
991 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
992 " varTex0.xy = ATTRIB_position;\n" +
993 " varTex0.y = 1.0 - varTex0.y;\n" +
994 " varTex0.zw = vec2(0.0, 0.0);\n" +
995 "}\n");
996 sb.setShader(t);
997 sb.addConstant(mUniformAlloc.getType());
998 sb.addInput(mMesh.getVertexType(0).getElement());
999 mPVCurve = sb.create();
1000 mPVCurve.setName("PVCurve");
1001 mPVCurve.bindAllocation(mPVA);
1002 mPVCurve.bindConstants(mUniformAlloc, 1);
1003
Joe Onorato93839052009-08-06 20:34:32 -07001004 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -07001005 }
Joe Onorato93839052009-08-06 20:34:32 -07001006
Jason Samscd689e12009-09-29 15:28:22 -07001007 private void initProgramFragment() {
1008 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -07001009 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -08001010 sb.setMag(Sampler.Value.NEAREST);
Jason Samscd689e12009-09-29 15:28:22 -07001011 sb.setWrapS(Sampler.Value.CLAMP);
1012 sb.setWrapT(Sampler.Value.CLAMP);
1013 Sampler linear = sb.create();
1014
1015 sb.setMin(Sampler.Value.NEAREST);
1016 sb.setMag(Sampler.Value.NEAREST);
1017 Sampler nearest = sb.create();
1018
Jason Sams72f1d312009-12-17 16:58:25 -08001019 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
Jason Samsb4ecab22010-01-19 16:43:26 -08001020 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
Jason Sams72f1d312009-12-17 16:58:25 -08001021 ProgramFragment.Builder.Format.RGBA, 0);
Jason Samsc8514792009-10-29 14:27:29 -07001022 mPFTexMip = bf.create();
1023 mPFTexMip.setName("PFTexMip");
1024 mPFTexMip.bindSampler(linear, 0);
1025
1026 mPFTexNearest = bf.create();
1027 mPFTexNearest.setName("PFTexNearest");
1028 mPFTexNearest.bindSampler(nearest, 0);
Jason Sams6ec11bc2010-01-19 17:56:52 -08001029
1030 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1031 ProgramFragment.Builder.Format.ALPHA, 0);
1032 mPFTexMipAlpha = bf.create();
1033 mPFTexMipAlpha.setName("PFTexMipAlpha");
1034 mPFTexMipAlpha.bindSampler(linear, 0);
1035
Jason Samscd689e12009-09-29 15:28:22 -07001036 }
1037
1038 private void initProgramStore() {
1039 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1040 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -07001041 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -07001042 bs.setDitherEnable(true);
1043 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1044 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1045 mPSIcons = bs.create();
1046 mPSIcons.setName("PSIcons");
Jason Samscd689e12009-09-29 15:28:22 -07001047 }
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 }
Joe Onoratob8315542010-02-03 20:39:25 -08001270 if (mScript != null) {
1271 mRS.contextBindRootScript(mScript);
1272 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001273 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001274
1275 void initTouchState() {
1276 int width = getWidth();
1277 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001278 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1279 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001280
Jason Sams37e7c2b2009-10-19 12:55:43 -07001281 int centerY = (height / 2);
1282 mTouchYBorders[0] = centerY - (cellHeight * 2);
1283 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001284 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001285 mTouchYBorders[3] = centerY + cellHeight;
1286 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001287
Joe Onorato6665c0f2009-09-02 15:27:24 -07001288 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001289 mTouchXBorders[0] = 0;
1290 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001291 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001292 mTouchXBorders[3] = centerX + (width / 4);
1293 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001294 }
1295
Joe Onorato664457d2009-10-28 16:30:34 -04001296 void fling() {
1297 mInvokeFling.execute();
1298 }
1299
1300 void move() {
1301 mInvokeMove.execute();
1302 }
1303
1304 void moveTo(float row) {
1305 mState.targetPos = row;
1306 mState.save();
1307 mInvokeMoveTo.execute();
1308 }
1309
Jason Sams37e7c2b2009-10-19 12:55:43 -07001310 int chooseTappedIcon(int x, int y, float pos) {
1311 // Adjust for scroll position if not zero.
1312 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001313
Joe Onorato6665c0f2009-09-02 15:27:24 -07001314 int col = -1;
1315 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001316 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1317 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1318 col = i;
1319 break;
1320 }
1321 }
1322 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1323 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1324 row = i;
1325 break;
1326 }
1327 }
1328
1329 if (row < 0 || col < 0) {
1330 return -1;
1331 }
1332
Joe Onorato664457d2009-10-28 16:30:34 -04001333 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001334 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001335
Joe Onorato664457d2009-10-28 16:30:34 -04001336 if (index >= mState.iconCount) {
1337 return -1;
1338 } else {
1339 return index;
1340 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001341 }
1342
Joe Onorato6665c0f2009-09-02 15:27:24 -07001343 /**
1344 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001345 *
1346 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001347 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001348 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001349 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001350 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001351 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001352 }
1353
Joe Onoratoc61cff92009-11-08 11:54:39 -05001354 /**
1355 * Select the icon at the given index.
1356 *
1357 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001358 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001359 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001360 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001361 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001362 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001363 if (mLastSelection == SELECTION_ICONS) {
1364 mLastSelection = SELECTION_NONE;
1365 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001366 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001367 if (pressed == SELECTED_FOCUSED) {
1368 mLastSelection = SELECTION_ICONS;
1369 }
1370
Joe Onorato52a653f2009-11-11 14:52:11 -08001371 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001372 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001373
Joe Onorato52a653f2009-11-11 14:52:11 -08001374 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001375 Bitmap selectionBitmap = mSelectionBitmap;
1376
1377 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001378 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001379 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001380
1381 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001382 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001383 mSelectedIcon.uploadToTexture(0);
1384 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001385
1386 if (prev != index) {
1387 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001388 //setContentDescription(info.title);
1389 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1390 }
1391 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001392 }
1393 }
1394
1395 /**
1396 * You need to call save() on mState on your own after calling this.
1397 */
1398 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001399 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001400 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001401
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001402 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001403 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001404 switch (mode) {
1405 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001406 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001407 break;
1408 case SELECTED_FOCUSED:
1409 mLastSelection = SELECTION_HOME;
1410 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001411 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001412 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1413 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001414 break;
1415 case SELECTED_PRESSED:
1416 mState.homeButtonId = mHomeButtonPressed.getID();
1417 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001418 }
1419 }
Joe Onoratobe386092009-11-17 17:32:16 -08001420
1421 public void dumpState() {
1422 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1423 Log.d(TAG, "mRollo.mHeight=" + mHeight);
1424 Log.d(TAG, "mRollo.mIcons=" + mIcons);
1425 if (mIcons != null) {
1426 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1427 }
1428 if (mIconIds != null) {
1429 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1430 }
1431 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1432 if (mLabelIds != null) {
1433 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1434 }
1435 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1436 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1437 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
Joe Onoratobe386092009-11-17 17:32:16 -08001438 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1439 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1440 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1441 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1442 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1443 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1444 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1445 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1446 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1447 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1448 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1449 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1450 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1451 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1452 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1453 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1454 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1455 }
1456 }
1457
1458 public void dumpState() {
1459 Log.d(TAG, "mRS=" + mRS);
1460 Log.d(TAG, "mRollo=" + mRollo);
1461 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1462 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1463 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1464 Log.d(TAG, "mLastSelection=" + mLastSelection);
1465 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1466 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1467 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1468 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1469 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1470 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1471 Log.d(TAG, "mZoom=" + mZoom);
1472 Log.d(TAG, "mPosX=" + mPosX);
1473 Log.d(TAG, "mVelocity=" + mVelocity);
1474 Log.d(TAG, "mMessageProc=" + mMessageProc);
1475 if (mRollo != null) {
1476 mRollo.dumpState();
1477 }
1478 if (mRS != null) {
1479 mRS.contextDump(0);
1480 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001481 }
Joe Onorato93839052009-08-06 20:34:32 -07001482}
1483
1484