blob: 0c45075c77abc59f3970cc2c0a6e96d4eba21998 [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 Onorato3a8820b2009-11-10 15:06:42 -0800117 private boolean mZoomDirty = false;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800118 private boolean mAnimateNextZoom;
119 private float mZoom;
120 private float mPosX;
121 private float mVelocity;
122 private AAMessage mMessageProc;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700123
Joe Onorato6665c0f2009-09-02 15:27:24 -0700124 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700125 public static final int ALLOC_PARAMS = 0;
126 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700127 public static final int ALLOC_ICON_IDS = 3;
128 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700129
130 public static final int COLUMNS_PER_PAGE = 4;
131 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700132
Joe Onorato6665c0f2009-09-02 15:27:24 -0700133 public static final int ICON_WIDTH_PX = 64;
134 public static final int ICON_TEXTURE_WIDTH_PX = 128;
135
136 public static final int ICON_HEIGHT_PX = 64;
137 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700138
139 public int SCREEN_WIDTH_PX;
140 public int SCREEN_HEIGHT_PX;
141
Joe Onoratobcbeab82009-10-01 21:45:43 -0700142 public void recompute(int w, int h) {
143 SCREEN_WIDTH_PX = 480;
144 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700145 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700146 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700147
148 public AllAppsView(Context context, AttributeSet attrs) {
149 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700150 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400151 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700152 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700153 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700154 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700155 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
156
Joe Onorato6665c0f2009-09-02 15:27:24 -0700157 setOnClickListener(this);
158 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700159 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700160 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samse26d9fc2009-11-12 14:00:43 -0800161
162 mRS = createRenderScript(true);
163 }
164
165 @Override
166 protected void onDetachedFromWindow() {
167 destroyRenderScript();
Joe Onorato93839052009-08-06 20:34:32 -0700168 }
169
Joe Onoratob39e51a2009-10-28 15:47:49 -0400170 /**
171 * If you have an attached click listener, View always plays the click sound!?!?
172 * Deal with sound effects by hand.
173 */
174 public void reallyPlaySoundEffect(int sound) {
175 boolean old = isSoundEffectsEnabled();
176 setSoundEffectsEnabled(true);
177 playSoundEffect(sound);
178 setSoundEffectsEnabled(old);
179 }
180
Joe Onorato7c312c12009-08-13 21:36:53 -0700181 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
182 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700183 }
184
Joe Onorato6665c0f2009-09-02 15:27:24 -0700185 public void setLauncher(Launcher launcher) {
186 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700187 }
188
Joe Onorato1feb3a82009-08-08 22:32:00 -0700189 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700190 public void surfaceDestroyed(SurfaceHolder holder) {
191 super.surfaceDestroyed(holder);
Jason Sams20df7c72009-11-05 12:30:24 -0800192 mRollo.mHasSurface = false;
Joe Onoratofab74402009-11-11 16:05:23 -0800193 // Without this, we leak mMessageCallback which leaks the context.
194 mRS.mMessageCallback = null;
Joe Onorato7bb17492009-09-24 17:51:01 -0700195 }
196
197 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700198 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800199 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700200
Joe Onorato080d9b62009-11-02 12:01:11 -0500201 super.surfaceChanged(holder, format, w, h);
202
Jason Samse26d9fc2009-11-12 14:00:43 -0800203 if (mRollo == null) {
Jason Sams90396672009-11-03 13:59:34 -0800204 mRollo = new RolloRS();
Jason Sams20df7c72009-11-05 12:30:24 -0800205 mRollo.mHasSurface = true;
Jason Sams90396672009-11-03 13:59:34 -0800206 mRollo.init(getResources(), w, h);
207 if (mAllAppsList != null) {
208 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800209 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800210 if (mShouldGainFocus) {
211 gainFocus();
212 mShouldGainFocus = false;
213 }
Jason Sams20df7c72009-11-05 12:30:24 -0800214 } else {
215 mRollo.mHasSurface = true;
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
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800225 //long endTime = SystemClock.uptimeMillis();
226 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700227 }
Jason Sams2e19c052009-10-20 18:19:55 -0700228
Joe Onorato93839052009-08-06 20:34:32 -0700229 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800230 public void onWindowFocusChanged(boolean hasWindowFocus) {
231 super.onWindowFocusChanged(hasWindowFocus);
232 if (mArrowNavigation) {
233 if (!hasWindowFocus) {
234 // Clear selection when we lose window focus
235 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500236 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800237 mRollo.clearSelectedIcon();
238 mRollo.mState.save();
239 } else if (hasWindowFocus) {
240 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500241 if (mLastSelection == SELECTION_ICONS) {
242 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800243 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500244 Defines.COLUMNS_PER_PAGE;
245 if (selection < 0 || // No selection
246 selection < firstIcon || // off the top of the screen
247 selection >= mRollo.mState.iconCount || // past last icon
248 selection >= firstIcon + // past last icon on screen
249 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
250 selection = firstIcon;
251 }
252
253 // Select the first icon when we gain window focus
254 mRollo.selectIcon(selection, SELECTED_FOCUSED);
255 mRollo.mState.save();
256 } else if (mLastSelection == SELECTION_HOME) {
257 mRollo.setHomeSelected(SELECTED_FOCUSED);
258 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800259 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800260 }
261 }
262 }
263 }
264
265 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700266 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
267 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400268
269 if (!isVisible()) {
270 return;
271 }
272
Mike Cleron7d5d7462009-10-20 14:06:00 -0700273 if (gainFocus) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800274 if (mRollo != null && mRollo.mHasSurface) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800275 gainFocus();
276 } else {
277 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700278 }
279 } else {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800280 if (mRollo != null && mRollo.mHasSurface) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800281 if (mArrowNavigation) {
282 // Clear selection when we lose focus
283 mRollo.clearSelectedIcon();
284 mRollo.setHomeSelected(SELECTED_NONE);
285 mRollo.mState.save();
286 mArrowNavigation = false;
287 }
288 } else {
289 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700290 }
291 }
292 }
293
Mike Cleronb64b67a2009-11-08 14:56:25 -0800294 private void gainFocus() {
295 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
296 // Select the first icon when we gain keyboard focus
297 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800298 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800299 SELECTED_FOCUSED);
300 mRollo.mState.save();
301 }
302 }
303
Mike Cleron7d5d7462009-10-20 14:06:00 -0700304 @Override
305 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700306
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800307 boolean handled = false;
Jason Samse26d9fc2009-11-12 14:00:43 -0800308
Joe Onorato859b3a72009-10-28 15:17:01 -0400309 if (!isVisible()) {
310 return false;
311 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500312 final int iconCount = mRollo.mState.iconCount;
313
Mike Cleron7d5d7462009-10-20 14:06:00 -0700314 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
315 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500316 if (mLastSelection == SELECTION_HOME) {
317 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
318 mLauncher.closeAllApps(true);
319 } else {
320 int whichApp = mRollo.mState.selectedIconIndex;
321 if (whichApp >= 0) {
322 ApplicationInfo app = mAllAppsList.get(whichApp);
323 mLauncher.startActivitySafely(app.intent);
324 handled = true;
325 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700326 }
327 }
328 }
Jason Sams2e19c052009-10-20 18:19:55 -0700329
Joe Onorato478730f2009-11-16 18:54:43 -0800330 if (iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700331 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700332
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800333 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800334 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700335
Mike Cleron7d5d7462009-10-20 14:06:00 -0700336 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500337 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700338
Mike Cleron7d5d7462009-10-20 14:06:00 -0700339 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500340 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700341 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700342
Mike Cleron7d5d7462009-10-20 14:06:00 -0700343 int newSelection = currentSelection;
344
345 switch (keyCode) {
346 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500347 if (mLastSelection == SELECTION_HOME) {
348 mRollo.setHomeSelected(SELECTED_NONE);
349 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
350 if (lastRowCount == 0) {
351 lastRowCount = Defines.COLUMNS_PER_PAGE;
352 }
353 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
354 if (newSelection >= iconCount) {
355 newSelection = iconCount-1;
356 }
357 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
358 - (Defines.ROWS_PER_PAGE - 1);
359 if (target < 0) {
360 target = 0;
361 }
362 if (currentTopRow != target) {
363 mRollo.moveTo(target);
364 }
365 } else {
366 if (currentPageRow > 0) {
367 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
368 } else if (currentTopRow > 0) {
369 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
370 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
Joe Onoratoaf5b4cb2009-12-08 17:51:22 -0800371 } else if (currentPageRow != 0) {
372 newSelection = currentTopRow * Defines.ROWS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500373 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700374 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800375 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700376 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800377
Joe Onoratoa13f5742009-11-02 17:15:19 -0500378 case KeyEvent.KEYCODE_DPAD_DOWN: {
379 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
380 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
381 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500382 if (mLastSelection != SELECTION_HOME) {
383 if (currentRow < rowCount-1) {
384 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato478730f2009-11-16 18:54:43 -0800385 if (currentSelection < 0) {
386 newSelection = 0;
387 } else {
388 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
389 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500390 if (newSelection >= iconCount) {
391 // Go from D to G in this arrangement:
392 // A B C D
393 // E F G
394 newSelection = iconCount - 1;
395 }
396 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
397 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
398 Defines.ROWS_PER_PAGE + 1);
399 }
400 } else {
401 newSelection = -1;
402 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700403 }
404 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800405 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700406 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500407 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700408 case KeyEvent.KEYCODE_DPAD_LEFT:
409 if (currentPageCol > 0) {
410 newSelection = currentSelection - 1;
411 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800412 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700413 break;
414 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700415 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500416 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700417 newSelection = currentSelection + 1;
418 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800419 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700420 break;
421 }
422 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500423 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700424 mRollo.mState.save();
425 }
426 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800427 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700428 }
429
Joe Onorato93839052009-08-06 20:34:32 -0700430 @Override
431 public boolean onTouchEvent(MotionEvent ev)
432 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700433 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700434
Joe Onorato7bb17492009-09-24 17:51:01 -0700435 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700436 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700437 }
438
Joe Onoratofb0ca672009-09-14 17:55:46 -0400439 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700440 return true;
441 }
442
443 super.onTouchEvent(ev);
444
Joe Onoratofb0ca672009-09-14 17:55:46 -0400445 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700446 int y = (int)ev.getY();
447
Joe Onoratobcbeab82009-10-01 21:45:43 -0700448 int action = ev.getAction();
449 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400450 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700451 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
452 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500453 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700454 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700455 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700456 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700457 mTouchTracking = TRACKING_FLING;
458
459 mMotionDownRawX = (int)ev.getRawX();
460 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700461
Mike Cleron7d5d7462009-10-20 14:06:00 -0700462 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700463 mRollo.mState.newTouchDown = 1;
464
465 if (!mRollo.checkClickOK()) {
466 mRollo.clearSelectedIcon();
467 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700468 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800469 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700470 if (mDownIconIndex < 0) {
471 // if nothing was selected, no long press.
472 cancelLongPress();
473 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700474 }
475 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700476 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800477 mVelocityTracker = VelocityTracker.obtain();
478 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700479 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700480 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400481 break;
482 case MotionEvent.ACTION_MOVE:
483 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700484 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500485 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
486 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700487 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700488 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700489 int rawX = (int)ev.getRawX();
490 int rawY = (int)ev.getRawY();
491 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700492 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700493
Joe Onorato82ca5502009-10-15 16:59:23 -0700494 if (!mStartedScrolling && slop < mSlop) {
495 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700496 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800497 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700498 if (mDownIconIndex != mCurrentIconIndex) {
499 // If a different icon is selected, don't allow it to be picked up.
500 // This handles off-axis dragging.
501 cancelLongPress();
502 mCurrentIconIndex = -1;
503 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700504 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700505 if (!mStartedScrolling) {
506 cancelLongPress();
507 mCurrentIconIndex = -1;
508 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700509 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700510 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700511 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700512
Joe Onoratobcbeab82009-10-01 21:45:43 -0700513 mStartedScrolling = true;
514 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800515 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700516 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700517 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400518 }
519 break;
520 case MotionEvent.ACTION_UP:
521 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700522 if (mTouchTracking == TRACKING_HOME) {
523 if (action == MotionEvent.ACTION_UP) {
524 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400525 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700526 mLauncher.closeAllApps(true);
527 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500528 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700529 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700530 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700531 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700532 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700533 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700534 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700535
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800536 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
537 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700538 mRollo.clearSelectedIcon();
539 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700540 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700541
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800542 if (mVelocityTracker != null) {
543 mVelocityTracker.recycle();
544 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700545 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700546 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700547 mTouchTracking = TRACKING_NONE;
548 break;
Joe Onorato93839052009-08-06 20:34:32 -0700549 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700550
551 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700552 }
553
Joe Onorato6665c0f2009-09-02 15:27:24 -0700554 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700555 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400556 return;
557 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700558 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
559 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400560 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700561 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700562 mLauncher.startActivitySafely(app.intent);
563 }
564 }
565
566 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700567 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400568 return true;
569 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700570 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
571 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
572 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700573
574 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700575 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
576 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700577
Joe Onoratobcbeab82009-10-01 21:45:43 -0700578 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
579 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700580 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700581 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700582 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700583
Joe Onorato7bb17492009-09-24 17:51:01 -0700584 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700585 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700586 return true;
587 }
588
Joe Onorato52a653f2009-11-11 14:52:11 -0800589 @Override
590 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
591 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
592 if (!isVisible()) {
593 return false;
594 }
595 String text = null;
596 int index;
597 int count = mAllAppsList.size() + 1; // +1 is home
598 int pos = -1;
599 switch (mLastSelection) {
600 case SELECTION_ICONS:
601 index = mRollo.mState.selectedIconIndex;
602 if (index >= 0) {
603 ApplicationInfo info = mAllAppsList.get(index);
604 if (info.title != null) {
605 text = info.title.toString();
606 pos = index;
607 }
608 }
609 break;
610 case SELECTION_HOME:
611 text = getContext().getString(R.string.all_apps_home_button_label);
612 pos = count;
613 break;
614 }
615 if (text != null) {
Joe Onorato52a653f2009-11-11 14:52:11 -0800616 event.setEnabled(true);
617 event.getText().add(text);
618 //event.setContentDescription(text);
619 event.setItemCount(count);
620 event.setCurrentItemIndex(pos);
621 }
622 }
623 return false;
624 }
625
Joe Onorato5162ea92009-09-03 09:39:42 -0700626 public void setDragController(DragController dragger) {
627 mDragController = dragger;
628 }
629
630 public void onDropCompleted(View target, boolean success) {
631 }
632
Joe Onorato4db52312009-10-06 11:17:43 -0700633 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800634 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700635 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800636 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700637 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800638 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400639 cancelLongPress();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800640 if (mRollo == null || !mRollo.mHasSurface) {
641 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800642 mZoom = zoom;
643 mAnimateNextZoom = animate;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800644 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700645 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800646 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700647 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400648 }
649
650 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800651 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800652 }
653
654 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800655 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400656 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700657
Joe Onorato9c1289c2009-08-17 11:03:03 -0400658 public void setApps(ArrayList<ApplicationInfo> list) {
659 mAllAppsList = list;
660 if (mRollo != null) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800661 if (mRollo.mHasSurface) {
662 mRollo.setApps(list);
663 } else {
664 mRollo.mAppsDirty = true;
665 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400666 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400667 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700668 }
669
Joe Onoratoa8138d52009-10-06 19:25:30 -0700670 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500671 if (mAllAppsList == null) {
672 // Not done loading yet. We'll find out about it later.
673 return;
674 }
675
Joe Onoratoa8138d52009-10-06 19:25:30 -0700676 final int N = list.size();
Joe Onoratoa276fc52009-12-08 17:02:02 -0800677 if (mRollo != null && mRollo.mHasSurface) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700678 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
679 }
680
681 for (int i=0; i<N; i++) {
682 final ApplicationInfo item = list.get(i);
Joe Onoratob0c27f22009-12-01 16:19:38 -0800683 int index = Collections.binarySearch(mAllAppsList, item,
684 LauncherModel.APP_NAME_COMPARATOR);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700685 if (index < 0) {
686 index = -(index+1);
687 }
688 mAllAppsList.add(index, item);
Joe Onoratoa276fc52009-12-08 17:02:02 -0800689 if (mRollo != null && mRollo.mHasSurface) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700690 mRollo.addApp(index, item);
691 mRollo.mState.iconCount++;
692 }
693 }
694
Joe Onoratoa276fc52009-12-08 17:02:02 -0800695 if (mRollo != null && mRollo.mHasSurface) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700696 mRollo.saveAppsList();
697 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700698 }
699
Joe Onoratoa8138d52009-10-06 19:25:30 -0700700 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500701 if (mAllAppsList == null) {
702 // Not done loading yet. We'll find out about it later.
703 return;
704 }
705
Joe Onoratoa8138d52009-10-06 19:25:30 -0700706 final int N = list.size();
707 for (int i=0; i<N; i++) {
708 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400709 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700710 if (index >= 0) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800711 int ic = mRollo != null ? mRollo.mState.iconCount : 666;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700712 mAllAppsList.remove(index);
Joe Onoratoa276fc52009-12-08 17:02:02 -0800713 if (mRollo != null && mRollo.mHasSurface) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700714 mRollo.removeApp(index);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700715 }
716 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800717 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700718 // Try to recover. This should keep us from crashing for now.
719 }
720 }
721
Joe Onoratoa276fc52009-12-08 17:02:02 -0800722 if (mRollo != null && mRollo.mHasSurface) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700723 mRollo.saveAppsList();
724 }
725 }
726
727 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
728 // Just remove and add, because they may need to be re-sorted.
729 removeApps(list);
730 addApps(list);
731 }
732
Joe Onoratocb9f7982009-10-31 16:32:02 -0400733 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
734 ComponentName component = item.intent.getComponent();
735 final int N = list.size();
736 for (int i=0; i<N; i++) {
737 ApplicationInfo x = list.get(i);
738 if (x.intent.getComponent().equals(component)) {
739 return i;
740 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700741 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400742 return -1;
743 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700744
Joe Onoratoc567acb2009-08-31 14:34:43 -0700745 private static int countPages(int iconCount) {
746 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
747 int pages = iconCount / iconsPerPage;
748 if (pages*iconsPerPage != iconCount) {
749 pages++;
750 }
751 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400752 }
753
Joe Onoratocb75f362009-11-12 13:04:07 -0800754 class AAMessage extends RenderScript.RSMessage {
755 public void run() {
756 mPosX = ((float)mData[0]) / (1 << 16);
757 mVelocity = ((float)mData[1]) / (1 << 16);
758 mZoom = ((float)mData[2]) / (1 << 16);
759 mZoomDirty = false;
760 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800761 }
762
Joe Onorato93839052009-08-06 20:34:32 -0700763 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700764
Joe Onorato1feb3a82009-08-08 22:32:00 -0700765 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700766 private int mWidth;
767 private int mHeight;
768
769 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700770 private Script mScript;
771 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700772 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700773 private Script.Invokable mInvokeFling;
774 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800775 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700776
Jason Samscd689e12009-09-29 15:28:22 -0700777 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700778 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700779 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700780 private ProgramFragment mPFTexMip;
781 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700782 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700783 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700784 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700785 private SimpleMesh mMesh2;
Jason Sams5612e432009-11-16 14:18:07 -0800786 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700787
Joe Onoratod63458b2009-10-15 21:19:09 -0700788 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500789 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700790 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700791
Joe Onoratobf15cb42009-08-07 14:33:40 -0700792 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700793 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700794 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700795
Joe Onoratobf15cb42009-08-07 14:33:40 -0700796 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700797 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700798 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700799 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700800
Joe Onorato6665c0f2009-09-02 15:27:24 -0700801 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700802 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700803
804 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400805 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700806
Jason Sams20df7c72009-11-05 12:30:24 -0800807 boolean mHasSurface = false;
808 private boolean mAppsDirty = false;
Jason Samsd8152b92009-10-13 17:19:10 -0700809
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700810 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700811 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700812
Jason Sams78aebd82009-09-15 13:06:59 -0700813 class BaseAlloc {
814 Allocation mAlloc;
815 Type mType;
816
817 void save() {
818 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700819 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700820 }
821
Jason Sams476339d2009-09-29 18:14:38 -0700822 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800823 return (Math.abs(mVelocity) < 0.4f) &&
824 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700825 }
826
Jason Sams78aebd82009-09-15 13:06:59 -0700827 class Params extends BaseAlloc {
828 Params() {
829 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
830 mAlloc = Allocation.createTyped(mRS, mType);
831 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700832 }
Jason Sams78aebd82009-09-15 13:06:59 -0700833 public int bubbleWidth;
834 public int bubbleHeight;
835 public int bubbleBitmapWidth;
836 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700837
Joe Onoratobcbeab82009-10-01 21:45:43 -0700838 public int homeButtonWidth;
839 public int homeButtonHeight;
840 public int homeButtonTextureWidth;
841 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700842 }
843
844 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700845 public float newPositionX;
846 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700847 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700848 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700849 public int selectedIconIndex = -1;
850 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700851 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700852 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700853 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700854
855 State() {
856 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
857 mAlloc = Allocation.createTyped(mRS, mType);
858 save();
859 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700860 }
861
862 public RolloRS() {
863 }
864
865 public void init(Resources res, int width, int height) {
866 mRes = res;
867 mWidth = width;
868 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700869 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700870 initProgramVertex();
871 initProgramFragment();
872 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700873 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700874 initGl();
875 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700876 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700877 initRs();
878 }
879
Jason Sams0aa71662009-10-02 18:43:18 -0700880 public void initMesh() {
881 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
882 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
883
Jason Samsd8152b92009-10-13 17:19:10 -0700884 float y = 0;
885 float z = 0;
886 for (int ct=0; ct < 200; ct++) {
887 float angle = 0;
888 float maxAngle = 3.14f * 0.16f;
889 float l = 1.f;
890
891 l = 1 - ((ct-5) * 0.10f);
892 if (ct > 7) {
893 angle = maxAngle * (ct - 7) * 0.2f;
894 angle = Math.min(angle, maxAngle);
895 }
Jason Samsc8514792009-10-29 14:27:29 -0700896 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700897 l = Math.min(1.0f, l);
898
899 y += 0.1f * Math.cos(angle);
900 z += 0.1f * Math.sin(angle);
901
902 float t = 0.1f * ct;
903 float ds = 0.08f;
904 tm.setColor(l, l, l, 0.99f);
905 tm.setTexture(ds, t);
906 tm.addVertex(-0.5f, y, z);
907 tm.setTexture(1 - ds, t);
908 tm.addVertex(0.5f, y, z);
909 }
910 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
911 tm.addTriangle(ct, ct+1, ct+2);
912 tm.addTriangle(ct+1, ct+3, ct+2);
913 }
914 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700915 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700916 }
917
Jason Sams5612e432009-11-16 14:18:07 -0800918 void resize(int w, int h) {
919 mPVA.setupProjectionNormalized(w, h);
920 mWidth = w;
921 mHeight = h;
922 }
923
Jason Samscd689e12009-09-29 15:28:22 -0700924 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800925 mPVA = new ProgramVertex.MatrixAllocation(mRS);
926 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700927
928 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700929 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700930 mPV = pvb.create();
931 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800932 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700933
Jason Sams37e7c2b2009-10-19 12:55:43 -0700934 //pva = new ProgramVertex.MatrixAllocation(mRS);
935 //pva.setupOrthoWindow(mWidth, mHeight);
936 //pvb.setTextureMatrixEnable(true);
937 //mPVOrtho = pvb.create();
938 //mPVOrtho.setName("PVOrtho");
939 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700940
941 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700942 }
Joe Onorato93839052009-08-06 20:34:32 -0700943
Jason Samscd689e12009-09-29 15:28:22 -0700944 private void initProgramFragment() {
945 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700946 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700947 sb.setMag(Sampler.Value.LINEAR);
948 sb.setWrapS(Sampler.Value.CLAMP);
949 sb.setWrapT(Sampler.Value.CLAMP);
950 Sampler linear = sb.create();
951
952 sb.setMin(Sampler.Value.NEAREST);
953 sb.setMag(Sampler.Value.NEAREST);
954 Sampler nearest = sb.create();
955
956 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700957 //mPFColor = bf.create();
958 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700959
960 bf.setTexEnable(true, 0);
961 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700962 mPFTexMip = bf.create();
963 mPFTexMip.setName("PFTexMip");
964 mPFTexMip.bindSampler(linear, 0);
965
966 mPFTexNearest = bf.create();
967 mPFTexNearest.setName("PFTexNearest");
968 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700969 }
970
971 private void initProgramStore() {
972 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
973 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700974 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700975 bs.setDitherEnable(true);
976 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
977 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
978 mPSIcons = bs.create();
979 mPSIcons.setName("PSIcons");
980
981 //bs.setDitherEnable(false);
982 //mPSText = bs.create();
983 //mPSText.setName("PSText");
984 }
985
986 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700987 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700988 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700989 }
Jason Sams78aebd82009-09-15 13:06:59 -0700990
Joe Onorato1feb3a82009-08-08 22:32:00 -0700991 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700992 mParams = new Params();
993 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700994
Joe Onoratobf15cb42009-08-07 14:33:40 -0700995 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700996
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700997 mParams.bubbleWidth = bubble.getBubbleWidth();
998 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
999 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1000 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001001
Joe Onoratod63458b2009-10-15 21:19:09 -07001002 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1003 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1004 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001005 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1006 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1007 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001008 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1009 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1010 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001011 mParams.homeButtonWidth = 76;
1012 mParams.homeButtonHeight = 68;
1013 mParams.homeButtonTextureWidth = 128;
1014 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001015
Joe Onoratod63458b2009-10-15 21:19:09 -07001016 mState.homeButtonId = mHomeButtonNormal.getID();
1017
Joe Onorato1feb3a82009-08-08 22:32:00 -07001018 mParams.save();
1019 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020
Joe Onorato1291a8c2009-09-15 15:07:25 -04001021 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1022 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1023 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001024
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001026 }
1027
Jason Sams37e7c2b2009-10-19 12:55:43 -07001028 private void initScript(int id) {
1029 }
1030
1031 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001032 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001033 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -07001034 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001035 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001036 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1037 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001038 mInvokeMove = sb.addInvokable("move");
1039 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001040 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001041 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001042 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001043 mScript = sb.create();
1044 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1045 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1046 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1047 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1048 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001049
Jason Sams37e7c2b2009-10-19 12:55:43 -07001050 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001051 }
Joe Onorato93839052009-08-06 20:34:32 -07001052
Jason Sams20df7c72009-11-05 12:30:24 -08001053 private void uploadApps(ArrayList<ApplicationInfo> list) {
1054 for (int i=0; i < mState.iconCount; i++) {
1055 uploadAppIcon(i, list.get(i));
1056 }
1057 }
1058
1059 void dirtyCheck() {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001060 if (mHasSurface) {
1061 if (mAppsDirty) {
1062 uploadApps(mAllAppsList);
1063 saveAppsList();
1064 mAppsDirty = false;
1065 }
1066 if (mZoomDirty) {
Joe Onorato68ba5ca2009-11-12 14:23:43 -08001067 setZoom(mZoom, mAnimateNextZoom);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001068 }
Jason Sams20df7c72009-11-05 12:30:24 -08001069 }
1070 }
1071
Joe Onorato9c1289c2009-08-17 11:03:03 -04001072 private void setApps(ArrayList<ApplicationInfo> list) {
1073 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001074 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001075 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001076 allocCount = 1;
1077 }
1078
Joe Onorato9c1289c2009-08-17 11:03:03 -04001079 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001080 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001081 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082
1083 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001084 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001085 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001086
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001087 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088
1089 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1090
Joe Onorato9c1289c2009-08-17 11:03:03 -04001091 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001092 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001093 saveAppsList();
1094 }
1095
Joe Onorato3a8820b2009-11-10 15:06:42 -08001096 private void setZoom(float zoom, boolean animate) {
1097 mRollo.clearSelectedIcon();
1098 mRollo.setHomeSelected(SELECTED_NONE);
1099 if (zoom > 0.001f) {
1100 mRollo.mState.zoomTarget = zoom;
1101 } else {
1102 mRollo.mState.zoomTarget = 0;
1103 }
1104 mRollo.mState.save();
1105 if (!animate) {
1106 mRollo.mInvokeSetZoom.execute();
1107 }
1108 }
1109
Jason Samsc8514792009-10-29 14:27:29 -07001110 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1111 int black[] = new int[w > h ? w : h];
1112 Allocation.Adapter2D a = alloc.createAdapter2D();
1113 int mip = 0;
1114 while (w > 1 || h > 1) {
1115 a.subData(0, 0, 1, h, black);
1116 a.subData(w-1, 0, 1, h, black);
1117 a.subData(0, 0, w, 1, black);
1118 a.subData(0, h-1, w, 1, black);
1119 mip++;
1120 w = (w + 1) >> 1;
1121 h = (h + 1) >> 1;
1122 a.setConstraint(Dimension.LOD, mip);
1123 }
1124 a.subData(0, 0, 1, 1, black);
1125 }
1126
Joe Onoratoa8138d52009-10-06 19:25:30 -07001127 private void uploadAppIcon(int index, ApplicationInfo item) {
1128 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001129 Element.RGBA_8888(mRS), true);
1130 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1131
Joe Onoratoa8138d52009-10-06 19:25:30 -07001132 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001133 Element.RGBA_8888(mRS), true);
1134 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001135
1136 mIcons[index].uploadToTexture(0);
1137 mLabels[index].uploadToTexture(0);
1138
1139 mIconIds[index] = mIcons[index].getID();
1140 mLabelIds[index] = mLabels[index].getID();
1141 }
1142
1143 /**
1144 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1145 * fill in the values and call saveAppsList().
1146 */
1147 private void reallocAppsList(int count) {
1148 Allocation[] icons = new Allocation[count];
1149 int[] iconIds = new int[count];
1150 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1151
1152 Allocation[] labels = new Allocation[count];
1153 int[] labelIds = new int[count];
1154 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1155
Joe Onoratobf173f12009-12-08 13:29:38 -08001156 final int oldCount = mRollo.mState.iconCount;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001157
1158 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1159 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1160 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1161 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1162
1163 mIcons = icons;
1164 mIconIds = iconIds;
1165 mLabels = labels;
1166 mLabelIds = labelIds;
1167 }
1168
1169 /**
1170 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1171 */
1172 private void addApp(int index, ApplicationInfo item) {
1173 final int count = mState.iconCount - index;
1174 final int dest = index + 1;
1175
1176 System.arraycopy(mIcons, index, mIcons, dest, count);
1177 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1178 System.arraycopy(mLabels, index, mLabels, dest, count);
1179 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1180
Joe Onoratoa276fc52009-12-08 17:02:02 -08001181 if (mHasSurface) {
Jason Sams20df7c72009-11-05 12:30:24 -08001182 uploadAppIcon(index, item);
1183 } else {
1184 mAppsDirty = true;
1185 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001186 }
1187
1188 /**
1189 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1190 */
1191 private void removeApp(int index) {
1192 final int count = mState.iconCount - index - 1;
1193 final int src = index + 1;
1194
1195 System.arraycopy(mIcons, src, mIcons, index, count);
1196 System.arraycopy(mIconIds, src, mIconIds, index, count);
1197 System.arraycopy(mLabels, src, mLabels, index, count);
1198 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1199
Joe Onoratoa276fc52009-12-08 17:02:02 -08001200 mRollo.mState.iconCount--;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001201 final int last = mState.iconCount - 1;
Joe Onoratoa276fc52009-12-08 17:02:02 -08001202
Joe Onoratoa8138d52009-10-06 19:25:30 -07001203 mIcons[last] = null;
1204 mIconIds[last] = 0;
1205 mLabels[last] = null;
1206 mLabelIds[last] = 0;
1207 }
1208
1209 /**
1210 * Send the apps list structures to RS.
1211 */
1212 private void saveAppsList() {
1213 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001214
Joe Onoratoa8138d52009-10-06 19:25:30 -07001215 mAllocIconIds.data(mIconIds);
1216 mAllocLabelIds.data(mLabelIds);
1217
Jason Sams37e7c2b2009-10-19 12:55:43 -07001218 if (mScript != null) { // this happens when we init it
1219 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1220 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001221 }
1222
1223 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001224
1225 // Note: mScript may be null if we haven't initialized it yet.
1226 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001227 if (mInvokeResetWAR != null) {
1228 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001229 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001230 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001231 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001232
1233 void initTouchState() {
1234 int width = getWidth();
1235 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001236 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1237 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001238
Jason Sams37e7c2b2009-10-19 12:55:43 -07001239 int centerY = (height / 2);
1240 mTouchYBorders[0] = centerY - (cellHeight * 2);
1241 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001242 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001243 mTouchYBorders[3] = centerY + cellHeight;
1244 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001245
Joe Onorato6665c0f2009-09-02 15:27:24 -07001246 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001247 mTouchXBorders[0] = 0;
1248 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001249 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001250 mTouchXBorders[3] = centerX + (width / 4);
1251 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001252 }
1253
Joe Onorato664457d2009-10-28 16:30:34 -04001254 void fling() {
1255 mInvokeFling.execute();
1256 }
1257
1258 void move() {
1259 mInvokeMove.execute();
1260 }
1261
1262 void moveTo(float row) {
1263 mState.targetPos = row;
1264 mState.save();
1265 mInvokeMoveTo.execute();
1266 }
1267
Jason Sams37e7c2b2009-10-19 12:55:43 -07001268 int chooseTappedIcon(int x, int y, float pos) {
1269 // Adjust for scroll position if not zero.
1270 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001271
Joe Onorato6665c0f2009-09-02 15:27:24 -07001272 int col = -1;
1273 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001274 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1275 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1276 col = i;
1277 break;
1278 }
1279 }
1280 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1281 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1282 row = i;
1283 break;
1284 }
1285 }
1286
1287 if (row < 0 || col < 0) {
1288 return -1;
1289 }
1290
Joe Onorato664457d2009-10-28 16:30:34 -04001291 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001292 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001293
Joe Onorato664457d2009-10-28 16:30:34 -04001294 if (index >= mState.iconCount) {
1295 return -1;
1296 } else {
1297 return index;
1298 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001299 }
1300
Joe Onorato6665c0f2009-09-02 15:27:24 -07001301 /**
1302 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001303 *
1304 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001305 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001306 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001307 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001308 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001309 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001310 }
1311
Joe Onoratoc61cff92009-11-08 11:54:39 -05001312 /**
1313 * Select the icon at the given index.
1314 *
1315 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001316 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001317 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001318 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001319 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001320 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001321 if (mLastSelection == SELECTION_ICONS) {
1322 mLastSelection = SELECTION_NONE;
1323 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001324 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001325 if (pressed == SELECTED_FOCUSED) {
1326 mLastSelection = SELECTION_ICONS;
1327 }
1328
Joe Onorato52a653f2009-11-11 14:52:11 -08001329 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001330 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001331
Joe Onorato52a653f2009-11-11 14:52:11 -08001332 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001333 Bitmap selectionBitmap = mSelectionBitmap;
1334
1335 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001336 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001337 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001338
1339 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001340 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001341 mSelectedIcon.uploadToTexture(0);
1342 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001343
1344 if (prev != index) {
1345 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001346 //setContentDescription(info.title);
1347 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1348 }
1349 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001350 }
1351 }
1352
1353 /**
1354 * You need to call save() on mState on your own after calling this.
1355 */
1356 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001357 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001358 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001359
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001360 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001361 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001362 switch (mode) {
1363 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001364 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001365 break;
1366 case SELECTED_FOCUSED:
1367 mLastSelection = SELECTION_HOME;
1368 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001369 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001370 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1371 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001372 break;
1373 case SELECTED_PRESSED:
1374 mState.homeButtonId = mHomeButtonPressed.getID();
1375 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001376 }
1377 }
Joe Onoratobe386092009-11-17 17:32:16 -08001378
1379 public void dumpState() {
1380 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1381 Log.d(TAG, "mRollo.mHeight=" + mHeight);
1382 Log.d(TAG, "mRollo.mIcons=" + mIcons);
1383 if (mIcons != null) {
1384 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1385 }
1386 if (mIconIds != null) {
1387 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1388 }
1389 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1390 if (mLabelIds != null) {
1391 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1392 }
1393 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1394 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1395 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
1396 Log.d(TAG, "mRollo.mHasSurface=" + mHasSurface);
1397 Log.d(TAG, "mRollo.mAppsDirty=" + mAppsDirty);
1398 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1399 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1400 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1401 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1402 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1403 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1404 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1405 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1406 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1407 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1408 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1409 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1410 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1411 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1412 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1413 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1414 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1415 }
1416 }
1417
1418 public void dumpState() {
1419 Log.d(TAG, "mRS=" + mRS);
1420 Log.d(TAG, "mRollo=" + mRollo);
1421 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1422 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1423 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1424 Log.d(TAG, "mLastSelection=" + mLastSelection);
1425 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1426 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1427 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1428 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1429 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1430 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1431 Log.d(TAG, "mZoom=" + mZoom);
1432 Log.d(TAG, "mPosX=" + mPosX);
1433 Log.d(TAG, "mVelocity=" + mVelocity);
1434 Log.d(TAG, "mMessageProc=" + mMessageProc);
1435 if (mRollo != null) {
1436 mRollo.dumpState();
1437 }
1438 if (mRS != null) {
1439 mRS.contextDump(0);
1440 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441 }
Joe Onorato93839052009-08-06 20:34:32 -07001442}
1443
1444