blob: 20572766efc102c5ed0c050b33ec3affb332f05e [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;
137 public static final int ICON_TEXTURE_WIDTH_PX = 128;
138
139 public static final int ICON_HEIGHT_PX = 64;
140 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700141
142 public int SCREEN_WIDTH_PX;
143 public int SCREEN_HEIGHT_PX;
144
Joe Onoratobcbeab82009-10-01 21:45:43 -0700145 public void recompute(int w, int h) {
146 SCREEN_WIDTH_PX = 480;
147 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700148 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700149 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700150
151 public AllAppsView(Context context, AttributeSet attrs) {
152 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700153 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400154 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700155 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700156 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700157 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700158 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
159
Joe Onorato6665c0f2009-09-02 15:27:24 -0700160 setOnClickListener(this);
161 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700162 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700163 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samse26d9fc2009-11-12 14:00:43 -0800164
165 mRS = createRenderScript(true);
166 }
167
168 @Override
169 protected void onDetachedFromWindow() {
170 destroyRenderScript();
Joe Onorato93839052009-08-06 20:34:32 -0700171 }
172
Joe Onoratob39e51a2009-10-28 15:47:49 -0400173 /**
174 * If you have an attached click listener, View always plays the click sound!?!?
175 * Deal with sound effects by hand.
176 */
177 public void reallyPlaySoundEffect(int sound) {
178 boolean old = isSoundEffectsEnabled();
179 setSoundEffectsEnabled(true);
180 playSoundEffect(sound);
181 setSoundEffectsEnabled(old);
182 }
183
Joe Onorato7c312c12009-08-13 21:36:53 -0700184 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
185 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700186 }
187
Joe Onorato6665c0f2009-09-02 15:27:24 -0700188 public void setLauncher(Launcher launcher) {
189 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700190 }
191
Joe Onorato1feb3a82009-08-08 22:32:00 -0700192 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700193 public void surfaceDestroyed(SurfaceHolder holder) {
194 super.surfaceDestroyed(holder);
Joe Onoratofab74402009-11-11 16:05:23 -0800195 // Without this, we leak mMessageCallback which leaks the context.
196 mRS.mMessageCallback = null;
Joe Onorato6374c512010-01-06 20:42:25 -0800197 // We may lose any callbacks that are pending, so make sure that we re-sync that
198 // on the next surfaceChanged.
199 mZoomDirty = true;
200 mHaveSurface = false;
Joe Onorato7bb17492009-09-24 17:51:01 -0700201 }
202
203 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700204 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800205 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700206
Joe Onorato080d9b62009-11-02 12:01:11 -0500207 super.surfaceChanged(holder, format, w, h);
208
Joe Onorato6374c512010-01-06 20:42:25 -0800209 mHaveSurface = true;
210
Jason Samse26d9fc2009-11-12 14:00:43 -0800211 if (mRollo == null) {
Jason Sams90396672009-11-03 13:59:34 -0800212 mRollo = new RolloRS();
213 mRollo.init(getResources(), w, h);
214 if (mAllAppsList != null) {
215 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800216 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800217 if (mShouldGainFocus) {
218 gainFocus();
219 mShouldGainFocus = false;
220 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 }
Jason Samse26d9fc2009-11-12 14:00:43 -0800222 mRollo.dirtyCheck();
Jason Sams5612e432009-11-16 14:18:07 -0800223 mRollo.resize(w, h);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700224
Joe Onoratocb75f362009-11-12 13:04:07 -0800225 mRS.mMessageCallback = mMessageProc = new AAMessage();
226
Joe Onoratoc567acb2009-08-31 14:34:43 -0700227 Resources res = getContext().getResources();
228 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700229
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800230 //long endTime = SystemClock.uptimeMillis();
231 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700232 }
Jason Sams2e19c052009-10-20 18:19:55 -0700233
Joe Onorato93839052009-08-06 20:34:32 -0700234 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800235 public void onWindowFocusChanged(boolean hasWindowFocus) {
236 super.onWindowFocusChanged(hasWindowFocus);
237 if (mArrowNavigation) {
238 if (!hasWindowFocus) {
239 // Clear selection when we lose window focus
240 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500241 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800242 mRollo.clearSelectedIcon();
243 mRollo.mState.save();
244 } else if (hasWindowFocus) {
245 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500246 if (mLastSelection == SELECTION_ICONS) {
247 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800248 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500249 Defines.COLUMNS_PER_PAGE;
250 if (selection < 0 || // No selection
251 selection < firstIcon || // off the top of the screen
252 selection >= mRollo.mState.iconCount || // past last icon
253 selection >= firstIcon + // past last icon on screen
254 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
255 selection = firstIcon;
256 }
257
258 // Select the first icon when we gain window focus
259 mRollo.selectIcon(selection, SELECTED_FOCUSED);
260 mRollo.mState.save();
261 } else if (mLastSelection == SELECTION_HOME) {
262 mRollo.setHomeSelected(SELECTED_FOCUSED);
263 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800264 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800265 }
266 }
267 }
268 }
269
270 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700271 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
272 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400273
274 if (!isVisible()) {
275 return;
276 }
277
Mike Cleron7d5d7462009-10-20 14:06:00 -0700278 if (gainFocus) {
Jason Sams510ee042009-12-15 14:23:37 -0800279 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800280 gainFocus();
281 } else {
282 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700283 }
284 } else {
Joe Onorato8eea3912009-12-09 13:01:06 -0800285 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800286 if (mArrowNavigation) {
287 // Clear selection when we lose focus
288 mRollo.clearSelectedIcon();
289 mRollo.setHomeSelected(SELECTED_NONE);
290 mRollo.mState.save();
291 mArrowNavigation = false;
292 }
293 } else {
294 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700295 }
296 }
297 }
298
Mike Cleronb64b67a2009-11-08 14:56:25 -0800299 private void gainFocus() {
300 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
301 // Select the first icon when we gain keyboard focus
302 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800303 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800304 SELECTED_FOCUSED);
305 mRollo.mState.save();
306 }
307 }
308
Mike Cleron7d5d7462009-10-20 14:06:00 -0700309 @Override
310 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700311
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800312 boolean handled = false;
Jason Samse26d9fc2009-11-12 14:00:43 -0800313
Joe Onorato859b3a72009-10-28 15:17:01 -0400314 if (!isVisible()) {
315 return false;
316 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500317 final int iconCount = mRollo.mState.iconCount;
318
Mike Cleron7d5d7462009-10-20 14:06:00 -0700319 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
320 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500321 if (mLastSelection == SELECTION_HOME) {
322 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
323 mLauncher.closeAllApps(true);
324 } else {
325 int whichApp = mRollo.mState.selectedIconIndex;
326 if (whichApp >= 0) {
327 ApplicationInfo app = mAllAppsList.get(whichApp);
328 mLauncher.startActivitySafely(app.intent);
329 handled = true;
330 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700331 }
332 }
333 }
Jason Sams2e19c052009-10-20 18:19:55 -0700334
Joe Onorato478730f2009-11-16 18:54:43 -0800335 if (iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700336 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700337
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800338 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800339 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700340
Mike Cleron7d5d7462009-10-20 14:06:00 -0700341 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500342 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700343
Mike Cleron7d5d7462009-10-20 14:06:00 -0700344 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500345 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700346 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700347
Mike Cleron7d5d7462009-10-20 14:06:00 -0700348 int newSelection = currentSelection;
349
350 switch (keyCode) {
351 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500352 if (mLastSelection == SELECTION_HOME) {
353 mRollo.setHomeSelected(SELECTED_NONE);
354 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
355 if (lastRowCount == 0) {
356 lastRowCount = Defines.COLUMNS_PER_PAGE;
357 }
358 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
359 if (newSelection >= iconCount) {
360 newSelection = iconCount-1;
361 }
362 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
363 - (Defines.ROWS_PER_PAGE - 1);
364 if (target < 0) {
365 target = 0;
366 }
367 if (currentTopRow != target) {
368 mRollo.moveTo(target);
369 }
370 } else {
371 if (currentPageRow > 0) {
372 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
373 } else if (currentTopRow > 0) {
374 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
375 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
Joe Onoratoaf5b4cb2009-12-08 17:51:22 -0800376 } else if (currentPageRow != 0) {
377 newSelection = currentTopRow * Defines.ROWS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500378 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700379 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800380 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700381 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800382
Joe Onoratoa13f5742009-11-02 17:15:19 -0500383 case KeyEvent.KEYCODE_DPAD_DOWN: {
384 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
385 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
386 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500387 if (mLastSelection != SELECTION_HOME) {
388 if (currentRow < rowCount-1) {
389 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato478730f2009-11-16 18:54:43 -0800390 if (currentSelection < 0) {
391 newSelection = 0;
392 } else {
393 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
394 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500395 if (newSelection >= iconCount) {
396 // Go from D to G in this arrangement:
397 // A B C D
398 // E F G
399 newSelection = iconCount - 1;
400 }
401 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
402 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
403 Defines.ROWS_PER_PAGE + 1);
404 }
405 } else {
406 newSelection = -1;
407 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700408 }
409 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800410 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700411 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500412 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700413 case KeyEvent.KEYCODE_DPAD_LEFT:
414 if (currentPageCol > 0) {
415 newSelection = currentSelection - 1;
416 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800417 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700418 break;
419 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700420 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500421 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700422 newSelection = currentSelection + 1;
423 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800424 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700425 break;
426 }
427 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500428 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700429 mRollo.mState.save();
430 }
431 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800432 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700433 }
434
Joe Onorato93839052009-08-06 20:34:32 -0700435 @Override
436 public boolean onTouchEvent(MotionEvent ev)
437 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700438 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700439
Joe Onorato7bb17492009-09-24 17:51:01 -0700440 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700441 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700442 }
443
Joe Onoratofb0ca672009-09-14 17:55:46 -0400444 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700445 return true;
446 }
447
448 super.onTouchEvent(ev);
449
Joe Onoratofb0ca672009-09-14 17:55:46 -0400450 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700451 int y = (int)ev.getY();
452
Joe Onoratobcbeab82009-10-01 21:45:43 -0700453 int action = ev.getAction();
454 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400455 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700456 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
457 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500458 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700459 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700460 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700461 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700462 mTouchTracking = TRACKING_FLING;
463
464 mMotionDownRawX = (int)ev.getRawX();
465 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700466
Mike Cleron7d5d7462009-10-20 14:06:00 -0700467 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700468 mRollo.mState.newTouchDown = 1;
469
470 if (!mRollo.checkClickOK()) {
471 mRollo.clearSelectedIcon();
472 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700473 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800474 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700475 if (mDownIconIndex < 0) {
476 // if nothing was selected, no long press.
477 cancelLongPress();
478 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700479 }
480 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700481 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800482 mVelocityTracker = VelocityTracker.obtain();
483 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700484 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700485 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400486 break;
487 case MotionEvent.ACTION_MOVE:
488 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700489 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500490 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
491 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700492 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700493 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700494 int rawX = (int)ev.getRawX();
495 int rawY = (int)ev.getRawY();
496 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700497 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700498
Joe Onorato82ca5502009-10-15 16:59:23 -0700499 if (!mStartedScrolling && slop < mSlop) {
500 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700501 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800502 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700503 if (mDownIconIndex != mCurrentIconIndex) {
504 // If a different icon is selected, don't allow it to be picked up.
505 // This handles off-axis dragging.
506 cancelLongPress();
507 mCurrentIconIndex = -1;
508 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700509 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700510 if (!mStartedScrolling) {
511 cancelLongPress();
512 mCurrentIconIndex = -1;
513 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700514 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700515 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700516 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700517
Joe Onoratobcbeab82009-10-01 21:45:43 -0700518 mStartedScrolling = true;
519 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800520 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700521 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700522 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400523 }
524 break;
525 case MotionEvent.ACTION_UP:
526 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700527 if (mTouchTracking == TRACKING_HOME) {
528 if (action == MotionEvent.ACTION_UP) {
529 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400530 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700531 mLauncher.closeAllApps(true);
532 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500533 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700534 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700535 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700536 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700537 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700538 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700539 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700540
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800541 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
542 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700543 mRollo.clearSelectedIcon();
544 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700545 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700546
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800547 if (mVelocityTracker != null) {
548 mVelocityTracker.recycle();
549 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700550 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700551 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700552 mTouchTracking = TRACKING_NONE;
553 break;
Joe Onorato93839052009-08-06 20:34:32 -0700554 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700555
556 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700557 }
558
Joe Onorato6665c0f2009-09-02 15:27:24 -0700559 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700560 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400561 return;
562 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700563 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
564 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400565 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700566 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700567 mLauncher.startActivitySafely(app.intent);
568 }
569 }
570
571 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700572 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400573 return true;
574 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700575 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
576 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
577 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700578
Jason Samsb4ecab22010-01-19 16:43:26 -0800579 Bitmap bmp = app.iconBitmap;
580 final int w = bmp.getWidth();
581 final int h = bmp.getHeight();
582
Joe Onorato5162ea92009-09-03 09:39:42 -0700583 // We don't really have an accurate location to use. This will do.
Jason Samsb4ecab22010-01-19 16:43:26 -0800584 int screenX = mMotionDownRawX - (w / 2);
585 int screenY = mMotionDownRawY - h;
Joe Onorato5162ea92009-09-03 09:39:42 -0700586
Joe Onoratobcbeab82009-10-01 21:45:43 -0700587 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
588 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Jason Samsb4ecab22010-01-19 16:43:26 -0800589 mDragController.startDrag(bmp, screenX, screenY,
590 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700591
Joe Onorato7bb17492009-09-24 17:51:01 -0700592 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700593 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700594 return true;
595 }
596
Joe Onorato52a653f2009-11-11 14:52:11 -0800597 @Override
598 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
599 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
600 if (!isVisible()) {
601 return false;
602 }
603 String text = null;
604 int index;
605 int count = mAllAppsList.size() + 1; // +1 is home
606 int pos = -1;
607 switch (mLastSelection) {
608 case SELECTION_ICONS:
609 index = mRollo.mState.selectedIconIndex;
610 if (index >= 0) {
611 ApplicationInfo info = mAllAppsList.get(index);
612 if (info.title != null) {
613 text = info.title.toString();
614 pos = index;
615 }
616 }
617 break;
618 case SELECTION_HOME:
619 text = getContext().getString(R.string.all_apps_home_button_label);
620 pos = count;
621 break;
622 }
623 if (text != null) {
Joe Onorato52a653f2009-11-11 14:52:11 -0800624 event.setEnabled(true);
625 event.getText().add(text);
626 //event.setContentDescription(text);
627 event.setItemCount(count);
628 event.setCurrentItemIndex(pos);
629 }
630 }
631 return false;
632 }
633
Joe Onorato5162ea92009-09-03 09:39:42 -0700634 public void setDragController(DragController dragger) {
635 mDragController = dragger;
636 }
637
638 public void onDropCompleted(View target, boolean success) {
639 }
640
Joe Onorato4db52312009-10-06 11:17:43 -0700641 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800642 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700643 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800644 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700645 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800646 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400647 cancelLongPress();
Joe Onorato6374c512010-01-06 20:42:25 -0800648 mNextZoom = zoom;
649 mAnimateNextZoom = animate;
650 // if we do setZoom while we don't have a surface, we won't
651 // get the callbacks that actually set mZoom.
652 if (mRollo == null || !mHaveSurface) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800653 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800654 mZoom = zoom;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800655 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700656 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800657 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700658 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400659 }
660
661 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800662 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800663 }
664
665 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800666 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400667 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700668
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669 public void setApps(ArrayList<ApplicationInfo> list) {
670 mAllAppsList = list;
671 if (mRollo != null) {
Joe Onorato3ecbd812009-12-11 13:38:54 -0800672 mRollo.setApps(list);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400673 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400674 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700675 }
676
Joe Onoratoa8138d52009-10-06 19:25:30 -0700677 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500678 if (mAllAppsList == null) {
679 // Not done loading yet. We'll find out about it later.
680 return;
681 }
682
Joe Onoratoa8138d52009-10-06 19:25:30 -0700683 final int N = list.size();
Joe Onorato8eea3912009-12-09 13:01:06 -0800684 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700685 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
686 }
687
688 for (int i=0; i<N; i++) {
689 final ApplicationInfo item = list.get(i);
Joe Onoratob0c27f22009-12-01 16:19:38 -0800690 int index = Collections.binarySearch(mAllAppsList, item,
691 LauncherModel.APP_NAME_COMPARATOR);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700692 if (index < 0) {
693 index = -(index+1);
694 }
695 mAllAppsList.add(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -0800696 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700697 mRollo.addApp(index, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700698 }
699 }
700
Joe Onorato8eea3912009-12-09 13:01:06 -0800701 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700702 mRollo.saveAppsList();
703 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700704 }
705
Joe Onoratoa8138d52009-10-06 19:25:30 -0700706 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500707 if (mAllAppsList == null) {
708 // Not done loading yet. We'll find out about it later.
709 return;
710 }
711
Joe Onoratoa8138d52009-10-06 19:25:30 -0700712 final int N = list.size();
713 for (int i=0; i<N; i++) {
714 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400715 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700716 if (index >= 0) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800717 int ic = mRollo != null ? mRollo.mState.iconCount : 666;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700718 mAllAppsList.remove(index);
Joe Onorato8eea3912009-12-09 13:01:06 -0800719 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700720 mRollo.removeApp(index);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700721 }
722 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800723 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700724 // Try to recover. This should keep us from crashing for now.
725 }
726 }
727
Joe Onorato8eea3912009-12-09 13:01:06 -0800728 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700729 mRollo.saveAppsList();
730 }
731 }
732
733 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
734 // Just remove and add, because they may need to be re-sorted.
735 removeApps(list);
736 addApps(list);
737 }
738
Joe Onoratocb9f7982009-10-31 16:32:02 -0400739 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
740 ComponentName component = item.intent.getComponent();
741 final int N = list.size();
742 for (int i=0; i<N; i++) {
743 ApplicationInfo x = list.get(i);
744 if (x.intent.getComponent().equals(component)) {
745 return i;
746 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700747 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400748 return -1;
749 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700750
Joe Onoratoc567acb2009-08-31 14:34:43 -0700751 private static int countPages(int iconCount) {
752 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
753 int pages = iconCount / iconsPerPage;
754 if (pages*iconsPerPage != iconCount) {
755 pages++;
756 }
757 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 }
759
Joe Onoratocb75f362009-11-12 13:04:07 -0800760 class AAMessage extends RenderScript.RSMessage {
761 public void run() {
762 mPosX = ((float)mData[0]) / (1 << 16);
763 mVelocity = ((float)mData[1]) / (1 << 16);
764 mZoom = ((float)mData[2]) / (1 << 16);
765 mZoomDirty = false;
766 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800767 }
768
Joe Onorato93839052009-08-06 20:34:32 -0700769 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700770
Joe Onorato1feb3a82009-08-08 22:32:00 -0700771 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700772 private int mWidth;
773 private int mHeight;
774
775 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700776 private Script mScript;
777 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700778 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700779 private Script.Invokable mInvokeFling;
780 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800781 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700782
Jason Samscd689e12009-09-29 15:28:22 -0700783 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700784 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700785 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700786 private ProgramFragment mPFTexMip;
787 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700788 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700789 private ProgramVertex mPVOrtho;
Jason Samsb4ecab22010-01-19 16:43:26 -0800790 private ProgramVertex mPVCurve;
Jason Sams0aa71662009-10-02 18:43:18 -0700791 private SimpleMesh mMesh;
Jason Sams5612e432009-11-16 14:18:07 -0800792 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700793
Jason Samsb4ecab22010-01-19 16:43:26 -0800794 private Allocation mUniformAlloc;
795
Joe Onoratod63458b2009-10-15 21:19:09 -0700796 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500797 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700798 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700799
Joe Onoratobf15cb42009-08-07 14:33:40 -0700800 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700801 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700802 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700803
Joe Onoratobf15cb42009-08-07 14:33:40 -0700804 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700805 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700806 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700807 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700808
Joe Onorato6665c0f2009-09-02 15:27:24 -0700809 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700810 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700811
812 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400813 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700814
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700815 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700816 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700817
Jason Sams78aebd82009-09-15 13:06:59 -0700818 class BaseAlloc {
819 Allocation mAlloc;
820 Type mType;
821
822 void save() {
823 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700824 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700825 }
826
Jason Sams476339d2009-09-29 18:14:38 -0700827 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800828 return (Math.abs(mVelocity) < 0.4f) &&
829 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700830 }
831
Jason Sams78aebd82009-09-15 13:06:59 -0700832 class Params extends BaseAlloc {
833 Params() {
834 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
835 mAlloc = Allocation.createTyped(mRS, mType);
836 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700837 }
Jason Sams78aebd82009-09-15 13:06:59 -0700838 public int bubbleWidth;
839 public int bubbleHeight;
840 public int bubbleBitmapWidth;
841 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700842
Joe Onoratobcbeab82009-10-01 21:45:43 -0700843 public int homeButtonWidth;
844 public int homeButtonHeight;
845 public int homeButtonTextureWidth;
846 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700847 }
848
849 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700850 public float newPositionX;
851 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700852 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700853 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700854 public int selectedIconIndex = -1;
855 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700856 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700857 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700858 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700859
860 State() {
861 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
862 mAlloc = Allocation.createTyped(mRS, mType);
863 save();
864 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700865 }
866
867 public RolloRS() {
868 }
869
870 public void init(Resources res, int width, int height) {
871 mRes = res;
872 mWidth = width;
873 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700874 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700875 initProgramVertex();
876 initProgramFragment();
877 initProgramStore();
Jason Samsb4ecab22010-01-19 16:43:26 -0800878 //initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700879 initGl();
880 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700881 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700882 initRs();
883 }
884
Jason Samsb4ecab22010-01-19 16:43:26 -0800885 public void initMesh2() {
886 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Jason Sams0aa71662009-10-02 18:43:18 -0700887
Jason Samsb4ecab22010-01-19 16:43:26 -0800888 for (int ct=0; ct < 16; ct++) {
889 float pos = (1.f / 16.f) * ct;
890 tm.addVertex(0.0f, pos);
891 tm.addVertex(1.0f, pos);
Jason Samsd8152b92009-10-13 17:19:10 -0700892 }
Jason Samsb4ecab22010-01-19 16:43:26 -0800893 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
Jason Samsd8152b92009-10-13 17:19:10 -0700894 tm.addTriangle(ct, ct+1, ct+2);
895 tm.addTriangle(ct+1, ct+3, ct+2);
896 }
Jason Samsb4ecab22010-01-19 16:43:26 -0800897 mMesh = tm.create();
898 mMesh.setName("SMCell");
Jason Samsd8152b92009-10-13 17:19:10 -0700899 }
900
Jason Sams5612e432009-11-16 14:18:07 -0800901 void resize(int w, int h) {
902 mPVA.setupProjectionNormalized(w, h);
903 mWidth = w;
904 mHeight = h;
905 }
906
Jason Samscd689e12009-09-29 15:28:22 -0700907 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800908 mPVA = new ProgramVertex.MatrixAllocation(mRS);
909 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700910
911 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700912 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700913 mPV = pvb.create();
914 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800915 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700916
Jason Samsb4ecab22010-01-19 16:43:26 -0800917 Element.Builder eb = new Element.Builder(mRS);
918 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
919 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
920 Element e = eb.create();
921
922 mUniformAlloc = Allocation.createSized(mRS, e, 1);
923
924 initMesh2();
925 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
926 String t = new String("void main() {\n" +
927 // Animation
928 " float ani = UNI_Position.z;\n" +
929
930 " float scale = (2.0 / 480.0);\n" +
931 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
932 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
933 " float y = 0.0;\n" +
934 " float z = 0.0;\n" +
935 " float lum = 1.0;\n" +
936
937 " float cv = min(ys, 50.0) - 50.0;\n" +
938 " y += cv * 0.681;\n" + // Roughy 47 degrees
939 " z += -cv * 0.731;\n" +
940 " cv = clamp(ys, 50.0, 120.0) - 120.0;\n" + // curve range
941 " y += cv * cos(cv * 0.4 / (180.0 / 3.14));\n" +
942 " z += cv * sin(cv * 0.4 / (180.0 / 3.14));\n" +
943
944 " cv = max(ys, 750.0) - 750.0;\n" +
945 " y += cv * 0.681;\n" +
946 " z += cv * 0.731;\n" +
947 " cv = clamp(ys, 680.0, 750.0) - 680.0;\n" +
948 " y += cv * cos(cv * 0.4 / (180.0 / 3.14));\n" +
949 " z += cv * sin(cv * 0.4 / (180.0 / 3.14));\n" +
950
951 " y += clamp(ys, 120.0, 680.0);\n" +
952 " lum += (clamp(ys, 60.0, 115.0) - 115.0) / 100.0;\n" +
953 " lum -= (clamp(ys, 685.0, 740.0) - 685.0) / 100.0;\n" +
954
955 " vec4 pos;\n" +
956 " pos.x = x * scale - 1.0;\n" +
957 " pos.y = y * scale - 1.66;\n" +
958 " pos.z = z * scale;\n" +
959 " pos.w = 1.0;\n" +
960
961 " pos.x *= 1.0 + ani * 4.0;\n" +
962 " pos.y *= 1.0 + ani * 4.0;\n" +
963 " pos.z -= ani * 1.5;\n" +
964 " lum *= 1.0 - ani;\n" +
965
966 " gl_Position = UNI_MVP * pos;\n" +
967 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
968 " varTex0.xy = ATTRIB_position;\n" +
969 " varTex0.y = 1.0 - varTex0.y;\n" +
970 " varTex0.zw = vec2(0.0, 0.0);\n" +
971 "}\n");
972 sb.setShader(t);
973 sb.addConstant(mUniformAlloc.getType());
974 sb.addInput(mMesh.getVertexType(0).getElement());
975 mPVCurve = sb.create();
976 mPVCurve.setName("PVCurve");
977 mPVCurve.bindAllocation(mPVA);
978 mPVCurve.bindConstants(mUniformAlloc, 1);
979
980 float tf[] = new float[] {72.f, 72.f, 0.f, 0.f, 120.f, 120.f, 0.f, 0.f};
981 mUniformAlloc.data(tf);
982
Jason Sams37e7c2b2009-10-19 12:55:43 -0700983 //pva = new ProgramVertex.MatrixAllocation(mRS);
984 //pva.setupOrthoWindow(mWidth, mHeight);
985 //pvb.setTextureMatrixEnable(true);
986 //mPVOrtho = pvb.create();
987 //mPVOrtho.setName("PVOrtho");
988 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700989
990 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700991 }
Joe Onorato93839052009-08-06 20:34:32 -0700992
Jason Samscd689e12009-09-29 15:28:22 -0700993 private void initProgramFragment() {
994 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700995 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -0800996 sb.setMag(Sampler.Value.NEAREST);
Jason Samscd689e12009-09-29 15:28:22 -0700997 sb.setWrapS(Sampler.Value.CLAMP);
998 sb.setWrapT(Sampler.Value.CLAMP);
999 Sampler linear = sb.create();
1000
1001 sb.setMin(Sampler.Value.NEAREST);
1002 sb.setMag(Sampler.Value.NEAREST);
1003 Sampler nearest = sb.create();
1004
Jason Sams72f1d312009-12-17 16:58:25 -08001005 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001006 //mPFColor = bf.create();
1007 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -07001008
Jason Samsb4ecab22010-01-19 16:43:26 -08001009 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
Jason Sams72f1d312009-12-17 16:58:25 -08001010 ProgramFragment.Builder.Format.RGBA, 0);
Jason Samsc8514792009-10-29 14:27:29 -07001011 mPFTexMip = bf.create();
1012 mPFTexMip.setName("PFTexMip");
1013 mPFTexMip.bindSampler(linear, 0);
1014
1015 mPFTexNearest = bf.create();
1016 mPFTexNearest.setName("PFTexNearest");
1017 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -07001018 }
1019
1020 private void initProgramStore() {
1021 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1022 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -07001023 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -07001024 bs.setDitherEnable(true);
1025 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1026 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1027 mPSIcons = bs.create();
1028 mPSIcons.setName("PSIcons");
1029
1030 //bs.setDitherEnable(false);
1031 //mPSText = bs.create();
1032 //mPSText.setName("PSText");
1033 }
1034
1035 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001036 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -07001037 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -07001038 }
Jason Sams78aebd82009-09-15 13:06:59 -07001039
Joe Onorato1feb3a82009-08-08 22:32:00 -07001040 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -07001041 mParams = new Params();
1042 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001043
Joe Onoratobf15cb42009-08-07 14:33:40 -07001044 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -07001045
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001046 mParams.bubbleWidth = bubble.getBubbleWidth();
1047 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1048 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1049 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001050
Joe Onoratod63458b2009-10-15 21:19:09 -07001051 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1052 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1053 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001054 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1055 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1056 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001057 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1058 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1059 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001060 mParams.homeButtonWidth = 76;
1061 mParams.homeButtonHeight = 68;
1062 mParams.homeButtonTextureWidth = 128;
1063 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001064
Joe Onoratod63458b2009-10-15 21:19:09 -07001065 mState.homeButtonId = mHomeButtonNormal.getID();
1066
Joe Onorato1feb3a82009-08-08 22:32:00 -07001067 mParams.save();
1068 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001069
Joe Onorato1291a8c2009-09-15 15:07:25 -04001070 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1071 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1072 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001073
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001075 }
1076
Jason Sams37e7c2b2009-10-19 12:55:43 -07001077 private void initScript(int id) {
1078 }
1079
1080 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001081 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Samsd15f3ef2010-01-06 14:58:06 -08001082 sb.setScript(mRes, R.raw.allapps);
Joe Onorato93839052009-08-06 20:34:32 -07001083 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001084 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001085 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1086 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Samsb4ecab22010-01-19 16:43:26 -08001087 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001088 mInvokeMove = sb.addInvokable("move");
1089 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001090 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001091 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001092 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001093 mScript = sb.create();
1094 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1095 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1096 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1097 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1098 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Jason Samsb4ecab22010-01-19 16:43:26 -08001099 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Joe Onorato93839052009-08-06 20:34:32 -07001100
Jason Sams37e7c2b2009-10-19 12:55:43 -07001101 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001102 }
Joe Onorato93839052009-08-06 20:34:32 -07001103
Jason Sams20df7c72009-11-05 12:30:24 -08001104 void dirtyCheck() {
Jason Sams510ee042009-12-15 14:23:37 -08001105 if (mZoomDirty) {
Joe Onorato6374c512010-01-06 20:42:25 -08001106 setZoom(mNextZoom, mAnimateNextZoom);
Jason Sams20df7c72009-11-05 12:30:24 -08001107 }
1108 }
1109
Joe Onorato9c1289c2009-08-17 11:03:03 -04001110 private void setApps(ArrayList<ApplicationInfo> list) {
1111 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001112 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001113 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001114 allocCount = 1;
1115 }
1116
Joe Onorato9c1289c2009-08-17 11:03:03 -04001117 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001118 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001119 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001120
1121 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001122 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001123 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001124
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001125 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 mState.iconCount = count;
Joe Onorato8eea3912009-12-09 13:01:06 -08001128 for (int i=0; i < mState.iconCount; i++) {
1129 createAppIconAllocations(i, list.get(i));
Joe Onorato3ecbd812009-12-11 13:38:54 -08001130 }
Jason Sams510ee042009-12-15 14:23:37 -08001131 for (int i=0; i < mState.iconCount; i++) {
1132 uploadAppIcon(i, list.get(i));
Joe Onorato8eea3912009-12-09 13:01:06 -08001133 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001134 saveAppsList();
1135 }
1136
Joe Onorato3a8820b2009-11-10 15:06:42 -08001137 private void setZoom(float zoom, boolean animate) {
1138 mRollo.clearSelectedIcon();
1139 mRollo.setHomeSelected(SELECTED_NONE);
1140 if (zoom > 0.001f) {
1141 mRollo.mState.zoomTarget = zoom;
1142 } else {
1143 mRollo.mState.zoomTarget = 0;
1144 }
1145 mRollo.mState.save();
1146 if (!animate) {
1147 mRollo.mInvokeSetZoom.execute();
1148 }
1149 }
1150
Joe Onorato8eea3912009-12-09 13:01:06 -08001151 private void createAppIconAllocations(int index, ApplicationInfo item) {
Joe Onoratoa8138d52009-10-06 19:25:30 -07001152 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001153 Element.RGBA_8888(mRS), true);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001154 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001155 Element.RGBA_8888(mRS), true);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001156 mIconIds[index] = mIcons[index].getID();
1157 mLabelIds[index] = mLabels[index].getID();
1158 }
1159
Joe Onorato8eea3912009-12-09 13:01:06 -08001160 private void uploadAppIcon(int index, ApplicationInfo item) {
1161 if (mIconIds[index] != mIcons[index].getID()) {
1162 throw new IllegalStateException("uploadAppIcon index=" + index
1163 + " mIcons[index].getID=" + mIcons[index].getID()
1164 + " mIconsIds[index]=" + mIconIds[index]
1165 + " item=" + item);
1166 }
1167 mIcons[index].uploadToTexture(0);
1168 mLabels[index].uploadToTexture(0);
1169 }
1170
Joe Onoratoa8138d52009-10-06 19:25:30 -07001171 /**
1172 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1173 * fill in the values and call saveAppsList().
1174 */
1175 private void reallocAppsList(int count) {
1176 Allocation[] icons = new Allocation[count];
1177 int[] iconIds = new int[count];
1178 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1179
1180 Allocation[] labels = new Allocation[count];
1181 int[] labelIds = new int[count];
1182 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1183
Joe Onoratobf173f12009-12-08 13:29:38 -08001184 final int oldCount = mRollo.mState.iconCount;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001185
1186 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1187 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1188 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1189 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1190
1191 mIcons = icons;
1192 mIconIds = iconIds;
1193 mLabels = labels;
1194 mLabelIds = labelIds;
1195 }
1196
1197 /**
1198 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1199 */
1200 private void addApp(int index, ApplicationInfo item) {
1201 final int count = mState.iconCount - index;
1202 final int dest = index + 1;
1203
1204 System.arraycopy(mIcons, index, mIcons, dest, count);
1205 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1206 System.arraycopy(mLabels, index, mLabels, dest, count);
1207 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1208
Joe Onorato8eea3912009-12-09 13:01:06 -08001209 createAppIconAllocations(index, item);
Jason Sams510ee042009-12-15 14:23:37 -08001210 uploadAppIcon(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -08001211 mRollo.mState.iconCount++;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001212 }
1213
1214 /**
1215 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1216 */
1217 private void removeApp(int index) {
1218 final int count = mState.iconCount - index - 1;
1219 final int src = index + 1;
1220
1221 System.arraycopy(mIcons, src, mIcons, index, count);
1222 System.arraycopy(mIconIds, src, mIconIds, index, count);
1223 System.arraycopy(mLabels, src, mLabels, index, count);
1224 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1225
Joe Onoratoa276fc52009-12-08 17:02:02 -08001226 mRollo.mState.iconCount--;
Joe Onorato8eea3912009-12-09 13:01:06 -08001227 final int last = mState.iconCount;
Joe Onoratoa276fc52009-12-08 17:02:02 -08001228
Joe Onoratoa8138d52009-10-06 19:25:30 -07001229 mIcons[last] = null;
1230 mIconIds[last] = 0;
1231 mLabels[last] = null;
1232 mLabelIds[last] = 0;
1233 }
1234
1235 /**
1236 * Send the apps list structures to RS.
1237 */
1238 private void saveAppsList() {
1239 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001240
Joe Onoratoa8138d52009-10-06 19:25:30 -07001241 mAllocIconIds.data(mIconIds);
1242 mAllocLabelIds.data(mLabelIds);
1243
Jason Sams37e7c2b2009-10-19 12:55:43 -07001244 if (mScript != null) { // this happens when we init it
1245 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1246 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001247 }
1248
1249 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001250
1251 // Note: mScript may be null if we haven't initialized it yet.
1252 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001253 if (mInvokeResetWAR != null) {
1254 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001255 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001256 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001257 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001258
1259 void initTouchState() {
1260 int width = getWidth();
1261 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001262 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1263 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001264
Jason Sams37e7c2b2009-10-19 12:55:43 -07001265 int centerY = (height / 2);
1266 mTouchYBorders[0] = centerY - (cellHeight * 2);
1267 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001268 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001269 mTouchYBorders[3] = centerY + cellHeight;
1270 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001271
Joe Onorato6665c0f2009-09-02 15:27:24 -07001272 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001273 mTouchXBorders[0] = 0;
1274 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001275 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001276 mTouchXBorders[3] = centerX + (width / 4);
1277 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001278 }
1279
Joe Onorato664457d2009-10-28 16:30:34 -04001280 void fling() {
1281 mInvokeFling.execute();
1282 }
1283
1284 void move() {
1285 mInvokeMove.execute();
1286 }
1287
1288 void moveTo(float row) {
1289 mState.targetPos = row;
1290 mState.save();
1291 mInvokeMoveTo.execute();
1292 }
1293
Jason Sams37e7c2b2009-10-19 12:55:43 -07001294 int chooseTappedIcon(int x, int y, float pos) {
1295 // Adjust for scroll position if not zero.
1296 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001297
Joe Onorato6665c0f2009-09-02 15:27:24 -07001298 int col = -1;
1299 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001300 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1301 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1302 col = i;
1303 break;
1304 }
1305 }
1306 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1307 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1308 row = i;
1309 break;
1310 }
1311 }
1312
1313 if (row < 0 || col < 0) {
1314 return -1;
1315 }
1316
Joe Onorato664457d2009-10-28 16:30:34 -04001317 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001318 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001319
Joe Onorato664457d2009-10-28 16:30:34 -04001320 if (index >= mState.iconCount) {
1321 return -1;
1322 } else {
1323 return index;
1324 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001325 }
1326
Joe Onorato6665c0f2009-09-02 15:27:24 -07001327 /**
1328 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001329 *
1330 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001331 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001332 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001333 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001334 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001335 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001336 }
1337
Joe Onoratoc61cff92009-11-08 11:54:39 -05001338 /**
1339 * Select the icon at the given index.
1340 *
1341 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001342 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001343 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001344 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001345 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001346 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001347 if (mLastSelection == SELECTION_ICONS) {
1348 mLastSelection = SELECTION_NONE;
1349 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001350 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001351 if (pressed == SELECTED_FOCUSED) {
1352 mLastSelection = SELECTION_ICONS;
1353 }
1354
Joe Onorato52a653f2009-11-11 14:52:11 -08001355 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001356 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001357
Joe Onorato52a653f2009-11-11 14:52:11 -08001358 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001359 Bitmap selectionBitmap = mSelectionBitmap;
1360
1361 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001362 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001363 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001364
1365 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001366 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001367 mSelectedIcon.uploadToTexture(0);
1368 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001369
1370 if (prev != index) {
1371 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001372 //setContentDescription(info.title);
1373 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1374 }
1375 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001376 }
1377 }
1378
1379 /**
1380 * You need to call save() on mState on your own after calling this.
1381 */
1382 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001383 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001384 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001385
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001386 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001387 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001388 switch (mode) {
1389 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001390 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001391 break;
1392 case SELECTED_FOCUSED:
1393 mLastSelection = SELECTION_HOME;
1394 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001395 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001396 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1397 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001398 break;
1399 case SELECTED_PRESSED:
1400 mState.homeButtonId = mHomeButtonPressed.getID();
1401 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001402 }
1403 }
Joe Onoratobe386092009-11-17 17:32:16 -08001404
1405 public void dumpState() {
1406 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1407 Log.d(TAG, "mRollo.mHeight=" + mHeight);
1408 Log.d(TAG, "mRollo.mIcons=" + mIcons);
1409 if (mIcons != null) {
1410 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1411 }
1412 if (mIconIds != null) {
1413 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1414 }
1415 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1416 if (mLabelIds != null) {
1417 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1418 }
1419 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1420 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1421 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
Joe Onoratobe386092009-11-17 17:32:16 -08001422 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1423 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1424 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1425 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1426 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1427 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1428 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1429 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1430 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1431 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1432 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1433 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1434 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1435 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1436 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1437 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1438 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1439 }
1440 }
1441
1442 public void dumpState() {
1443 Log.d(TAG, "mRS=" + mRS);
1444 Log.d(TAG, "mRollo=" + mRollo);
1445 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1446 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1447 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1448 Log.d(TAG, "mLastSelection=" + mLastSelection);
1449 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1450 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1451 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1452 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1453 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1454 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1455 Log.d(TAG, "mZoom=" + mZoom);
1456 Log.d(TAG, "mPosX=" + mPosX);
1457 Log.d(TAG, "mVelocity=" + mVelocity);
1458 Log.d(TAG, "mMessageProc=" + mMessageProc);
1459 if (mRollo != null) {
1460 mRollo.dumpState();
1461 }
1462 if (mRS != null) {
1463 mRS.contextDump(0);
1464 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001465 }
Joe Onorato93839052009-08-06 20:34:32 -07001466}
1467
1468