blob: a4891dafb18ea7886c3acc40ceb0d7793cba836f [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;
52import java.util.Collections;
53import java.util.Comparator;
Joe Onorato93839052009-08-06 20:34:32 -070054
55
Joe Onorato6665c0f2009-09-02 15:27:24 -070056public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070057 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040058 private static final String TAG = "Launcher.AllAppsView";
59
Joe Onoratofb0ca672009-09-14 17:55:46 -040060 /** Bit for mLocks for when there are icons being loaded. */
61 private static final int LOCK_ICONS_PENDING = 1;
62
Joe Onorato68ffd102009-10-15 17:59:43 -070063 private static final int TRACKING_NONE = 0;
64 private static final int TRACKING_FLING = 1;
65 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070066
Joe Onoratoeb8325a2009-11-08 13:20:30 -050067 private static final int SELECTED_NONE = 0;
68 private static final int SELECTED_FOCUSED = 1;
69 private static final int SELECTED_PRESSED = 2;
70
71 private static final int SELECTION_NONE = 0;
72 private static final int SELECTION_ICONS = 1;
73 private static final int SELECTION_HOME = 2;
74
Joe Onorato6665c0f2009-09-02 15:27:24 -070075 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070076 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040077
78 /** When this is 0, modifications are allowed, when it's not, they're not.
79 * TODO: What about scrolling? */
80 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070081
Joe Onorato82ca5502009-10-15 16:59:23 -070082 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070083 private int mMaxFlingVelocity;
84
Joe Onoratobcbeab82009-10-01 21:45:43 -070085 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070086 private RenderScript mRS;
87 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040088 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070089
Mike Cleron7d5d7462009-10-20 14:06:00 -070090 /**
91 * True when we are using arrow keys or trackball to drive navigation
92 */
93 private boolean mArrowNavigation = false;
Joe Onoratoeb8325a2009-11-08 13:20:30 -050094 private boolean mStartedScrolling;
95
96 /**
97 * Used to keep track of the selection when AllAppsView loses window focus.
98 * One of the SELECTION_ constants.
99 */
100 private int mLastSelection;
Jason Sams2e19c052009-10-20 18:19:55 -0700101
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800102 /**
103 * Used to keep track of the selection when AllAppsView loses window focus
104 */
105 private int mLastSelectedIcon;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500106
Joe Onoratod769a632009-08-11 17:09:02 -0700107 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700108 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700109 private int mMotionDownRawX;
110 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700111 private int mDownIconIndex = -1;
112 private int mCurrentIconIndex = -1;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800113
Mike Cleronb64b67a2009-11-08 14:56:25 -0800114 private boolean mShouldGainFocus;
115
Joe Onorato3a8820b2009-11-10 15:06:42 -0800116 private boolean mZoomDirty = false;
117 private float mNextZoom;
118 private boolean mNextAnimate;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700119
Joe Onorato6665c0f2009-09-02 15:27:24 -0700120 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700121 public static final int ALLOC_PARAMS = 0;
122 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700123 public static final int ALLOC_ICON_IDS = 3;
124 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700125
126 public static final int COLUMNS_PER_PAGE = 4;
127 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700128
Joe Onorato6665c0f2009-09-02 15:27:24 -0700129 public static final int ICON_WIDTH_PX = 64;
130 public static final int ICON_TEXTURE_WIDTH_PX = 128;
131
132 public static final int ICON_HEIGHT_PX = 64;
133 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700134
135 public int SCREEN_WIDTH_PX;
136 public int SCREEN_HEIGHT_PX;
137
Joe Onoratobcbeab82009-10-01 21:45:43 -0700138 public void recompute(int w, int h) {
139 SCREEN_WIDTH_PX = 480;
140 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700141 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700142 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700143
144 public AllAppsView(Context context, AttributeSet attrs) {
145 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700146 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400147 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700148 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700149 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700150 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700151 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
152
Joe Onorato6665c0f2009-09-02 15:27:24 -0700153 setOnClickListener(this);
154 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700155 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700156 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700157 }
158
Joe Onoratob39e51a2009-10-28 15:47:49 -0400159 /**
160 * If you have an attached click listener, View always plays the click sound!?!?
161 * Deal with sound effects by hand.
162 */
163 public void reallyPlaySoundEffect(int sound) {
164 boolean old = isSoundEffectsEnabled();
165 setSoundEffectsEnabled(true);
166 playSoundEffect(sound);
167 setSoundEffectsEnabled(old);
168 }
169
Joe Onorato7c312c12009-08-13 21:36:53 -0700170 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
171 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700172 }
173
Joe Onorato6665c0f2009-09-02 15:27:24 -0700174 public void setLauncher(Launcher launcher) {
175 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700176 }
177
Joe Onorato1feb3a82009-08-08 22:32:00 -0700178 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700179 public void surfaceDestroyed(SurfaceHolder holder) {
180 super.surfaceDestroyed(holder);
Jason Sams20df7c72009-11-05 12:30:24 -0800181 mRollo.mHasSurface = false;
Joe Onorato7bb17492009-09-24 17:51:01 -0700182 }
183
184 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700185 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800186 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700187
Joe Onorato080d9b62009-11-02 12:01:11 -0500188 super.surfaceChanged(holder, format, w, h);
189
Jason Sams90396672009-11-03 13:59:34 -0800190 if (mRS == null) {
Jason Sams90396672009-11-03 13:59:34 -0800191 mRS = createRenderScript(true);
192 mRollo = new RolloRS();
Jason Sams20df7c72009-11-05 12:30:24 -0800193 mRollo.mHasSurface = true;
Jason Sams90396672009-11-03 13:59:34 -0800194 mRollo.init(getResources(), w, h);
195 if (mAllAppsList != null) {
196 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800197 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800198 if (mShouldGainFocus) {
199 gainFocus();
200 mShouldGainFocus = false;
201 }
Joe Onorato3a8820b2009-11-10 15:06:42 -0800202 mRollo.dirtyCheck();
Jason Sams20df7c72009-11-05 12:30:24 -0800203 } else {
204 mRollo.mHasSurface = true;
205 mRollo.dirtyCheck();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700207
208 Resources res = getContext().getResources();
209 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700210
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800211 //long endTime = SystemClock.uptimeMillis();
212 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700213 }
Jason Sams2e19c052009-10-20 18:19:55 -0700214
Joe Onorato93839052009-08-06 20:34:32 -0700215 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800216 public void onWindowFocusChanged(boolean hasWindowFocus) {
217 super.onWindowFocusChanged(hasWindowFocus);
218 if (mArrowNavigation) {
219 if (!hasWindowFocus) {
220 // Clear selection when we lose window focus
221 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500222 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800223 mRollo.clearSelectedIcon();
224 mRollo.mState.save();
225 } else if (hasWindowFocus) {
226 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500227 if (mLastSelection == SELECTION_ICONS) {
228 int selection = mLastSelectedIcon;
229 final int firstIcon = Math.round(mRollo.mMessageProc.mPosX) *
230 Defines.COLUMNS_PER_PAGE;
231 if (selection < 0 || // No selection
232 selection < firstIcon || // off the top of the screen
233 selection >= mRollo.mState.iconCount || // past last icon
234 selection >= firstIcon + // past last icon on screen
235 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
236 selection = firstIcon;
237 }
238
239 // Select the first icon when we gain window focus
240 mRollo.selectIcon(selection, SELECTED_FOCUSED);
241 mRollo.mState.save();
242 } else if (mLastSelection == SELECTION_HOME) {
243 mRollo.setHomeSelected(SELECTED_FOCUSED);
244 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800245 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800246 }
247 }
248 }
249 }
250
251 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700252 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
253 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400254
255 if (!isVisible()) {
256 return;
257 }
258
Mike Cleron7d5d7462009-10-20 14:06:00 -0700259 if (gainFocus) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800260 if (mRollo != null) {
261 gainFocus();
262 } else {
263 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700264 }
265 } else {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800266 if (mRollo != null) {
267 if (mArrowNavigation) {
268 // Clear selection when we lose focus
269 mRollo.clearSelectedIcon();
270 mRollo.setHomeSelected(SELECTED_NONE);
271 mRollo.mState.save();
272 mArrowNavigation = false;
273 }
274 } else {
275 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700276 }
277 }
278 }
279
Mike Cleronb64b67a2009-11-08 14:56:25 -0800280 private void gainFocus() {
281 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
282 // Select the first icon when we gain keyboard focus
283 mArrowNavigation = true;
284 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE,
285 SELECTED_FOCUSED);
286 mRollo.mState.save();
287 }
288 }
289
Mike Cleron7d5d7462009-10-20 14:06:00 -0700290 @Override
291 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700292
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800293 boolean handled = false;
294
Joe Onorato859b3a72009-10-28 15:17:01 -0400295 if (!isVisible()) {
296 return false;
297 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500298 final int iconCount = mRollo.mState.iconCount;
299
Mike Cleron7d5d7462009-10-20 14:06:00 -0700300 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
301 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500302 if (mLastSelection == SELECTION_HOME) {
303 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
304 mLauncher.closeAllApps(true);
305 } else {
306 int whichApp = mRollo.mState.selectedIconIndex;
307 if (whichApp >= 0) {
308 ApplicationInfo app = mAllAppsList.get(whichApp);
309 mLauncher.startActivitySafely(app.intent);
310 handled = true;
311 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700312 }
313 }
314 }
Jason Sams2e19c052009-10-20 18:19:55 -0700315
Joe Onoratoa13f5742009-11-02 17:15:19 -0500316 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700317 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700318
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800319 int currentSelection = mRollo.mState.selectedIconIndex;
320 int currentTopRow = Math.round(mRollo.mMessageProc.mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700321
Mike Cleron7d5d7462009-10-20 14:06:00 -0700322 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500323 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700324
Mike Cleron7d5d7462009-10-20 14:06:00 -0700325 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500326 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700327 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700328
Mike Cleron7d5d7462009-10-20 14:06:00 -0700329 int newSelection = currentSelection;
330
331 switch (keyCode) {
332 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500333 if (mLastSelection == SELECTION_HOME) {
334 mRollo.setHomeSelected(SELECTED_NONE);
335 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
336 if (lastRowCount == 0) {
337 lastRowCount = Defines.COLUMNS_PER_PAGE;
338 }
339 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
340 if (newSelection >= iconCount) {
341 newSelection = iconCount-1;
342 }
343 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
344 - (Defines.ROWS_PER_PAGE - 1);
345 if (target < 0) {
346 target = 0;
347 }
348 if (currentTopRow != target) {
349 mRollo.moveTo(target);
350 }
351 } else {
352 if (currentPageRow > 0) {
353 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
354 } else if (currentTopRow > 0) {
355 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
356 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
357 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700358 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800359 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700360 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800361
Joe Onoratoa13f5742009-11-02 17:15:19 -0500362 case KeyEvent.KEYCODE_DPAD_DOWN: {
363 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
364 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
365 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500366 if (mLastSelection != SELECTION_HOME) {
367 if (currentRow < rowCount-1) {
368 mRollo.setHomeSelected(SELECTED_NONE);
369 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
370 if (newSelection >= iconCount) {
371 // Go from D to G in this arrangement:
372 // A B C D
373 // E F G
374 newSelection = iconCount - 1;
375 }
376 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
377 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
378 Defines.ROWS_PER_PAGE + 1);
379 }
380 } else {
381 newSelection = -1;
382 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700383 }
384 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800385 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700386 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500387 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700388 case KeyEvent.KEYCODE_DPAD_LEFT:
389 if (currentPageCol > 0) {
390 newSelection = currentSelection - 1;
391 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800392 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700393 break;
394 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700395 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500396 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700397 newSelection = currentSelection + 1;
398 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800399 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700400 break;
401 }
402 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500403 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700404 mRollo.mState.save();
405 }
406 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800407 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700408 }
409
Joe Onorato93839052009-08-06 20:34:32 -0700410 @Override
411 public boolean onTouchEvent(MotionEvent ev)
412 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700413 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700414
Joe Onorato7bb17492009-09-24 17:51:01 -0700415 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700416 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700417 }
418
Joe Onoratofb0ca672009-09-14 17:55:46 -0400419 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700420 return true;
421 }
422
423 super.onTouchEvent(ev);
424
Joe Onoratofb0ca672009-09-14 17:55:46 -0400425 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700426 int y = (int)ev.getY();
427
Joe Onoratobcbeab82009-10-01 21:45:43 -0700428 int action = ev.getAction();
429 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400430 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700431 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
432 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500433 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700434 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700435 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700436 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700437 mTouchTracking = TRACKING_FLING;
438
439 mMotionDownRawX = (int)ev.getRawX();
440 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700441
Mike Cleron7d5d7462009-10-20 14:06:00 -0700442 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700443 mRollo.mState.newTouchDown = 1;
444
445 if (!mRollo.checkClickOK()) {
446 mRollo.clearSelectedIcon();
447 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700448 mDownIconIndex = mCurrentIconIndex
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500449 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700450 if (mDownIconIndex < 0) {
451 // if nothing was selected, no long press.
452 cancelLongPress();
453 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700454 }
455 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700456 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700457 mVelocity = VelocityTracker.obtain();
458 mVelocity.addMovement(ev);
459 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700460 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400461 break;
462 case MotionEvent.ACTION_MOVE:
463 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700464 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500465 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
466 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700467 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700468 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700469 int rawX = (int)ev.getRawX();
470 int rawY = (int)ev.getRawY();
471 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700472 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700473
Joe Onorato82ca5502009-10-15 16:59:23 -0700474 if (!mStartedScrolling && slop < mSlop) {
475 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700476 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700477 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
478 if (mDownIconIndex != mCurrentIconIndex) {
479 // If a different icon is selected, don't allow it to be picked up.
480 // This handles off-axis dragging.
481 cancelLongPress();
482 mCurrentIconIndex = -1;
483 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700484 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700485 if (!mStartedScrolling) {
486 cancelLongPress();
487 mCurrentIconIndex = -1;
488 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700489 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700490 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700491 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700492
Joe Onoratobcbeab82009-10-01 21:45:43 -0700493 mStartedScrolling = true;
494 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700495 mVelocity.addMovement(ev);
496 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700497 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400498 }
499 break;
500 case MotionEvent.ACTION_UP:
501 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700502 if (mTouchTracking == TRACKING_HOME) {
503 if (action == MotionEvent.ACTION_UP) {
504 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400505 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700506 mLauncher.closeAllApps(true);
507 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500508 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700509 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700510 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700511 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700512 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700513 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700514 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700515
Jason Sams12c14a82009-10-06 14:33:15 -0700516 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700517 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700518 mRollo.clearSelectedIcon();
519 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700520 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700521
Joe Onorato539ed9d2009-10-02 10:22:14 -0700522 if (mVelocity != null) {
523 mVelocity.recycle();
524 mVelocity = null;
525 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700526 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700527 mTouchTracking = TRACKING_NONE;
528 break;
Joe Onorato93839052009-08-06 20:34:32 -0700529 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700530
531 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700532 }
533
Joe Onorato6665c0f2009-09-02 15:27:24 -0700534 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700535 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400536 return;
537 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700538 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
539 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400540 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700541 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700542 mLauncher.startActivitySafely(app.intent);
543 }
544 }
545
546 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700547 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400548 return true;
549 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700550 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
551 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
552 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700553
554 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700555 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
556 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700557
Joe Onoratobcbeab82009-10-01 21:45:43 -0700558 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
559 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700560 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700561 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700562 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700563
Joe Onorato7bb17492009-09-24 17:51:01 -0700564 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700565 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700566 return true;
567 }
568
Joe Onorato52a653f2009-11-11 14:52:11 -0800569 @Override
570 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
571 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
572 if (!isVisible()) {
573 return false;
574 }
575 String text = null;
576 int index;
577 int count = mAllAppsList.size() + 1; // +1 is home
578 int pos = -1;
579 switch (mLastSelection) {
580 case SELECTION_ICONS:
581 index = mRollo.mState.selectedIconIndex;
582 if (index >= 0) {
583 ApplicationInfo info = mAllAppsList.get(index);
584 if (info.title != null) {
585 text = info.title.toString();
586 pos = index;
587 }
588 }
589 break;
590 case SELECTION_HOME:
591 text = getContext().getString(R.string.all_apps_home_button_label);
592 pos = count;
593 break;
594 }
595 if (text != null) {
596 Log.d(TAG, "dispatchPopulateAccessibilityEvent setting text=" + text);
597 event.setEnabled(true);
598 event.getText().add(text);
599 //event.setContentDescription(text);
600 event.setItemCount(count);
601 event.setCurrentItemIndex(pos);
602 }
603 }
604 return false;
605 }
606
Joe Onorato5162ea92009-09-03 09:39:42 -0700607 public void setDragController(DragController dragger) {
608 mDragController = dragger;
609 }
610
611 public void onDropCompleted(View target, boolean success) {
612 }
613
Joe Onorato4db52312009-10-06 11:17:43 -0700614 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800615 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700616 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800617 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700618 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800619 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400620 cancelLongPress();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800621 if (mRollo == null || !mRollo.mHasSurface) {
622 mZoomDirty = true;
623 mNextZoom = zoom;
624 mNextAnimate = animate;
625 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700626 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800627 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700628 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400629 }
630
631 public boolean isVisible() {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800632 if (mZoomDirty) {
633 return mNextZoom > 0.001f;
634 } else {
635 if (mRollo == null) {
636 return false;
637 } else {
638 return mRollo.mMessageProc.mZoom > 0.001f;
639 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700640 }
Joe Onorato3a8820b2009-11-10 15:06:42 -0800641 }
642
643 public boolean isOpaque() {
644 if (mZoomDirty) {
645 return mNextZoom > 0.999f;
646 } else {
647 if (mRollo == null) {
648 return false;
649 } else {
650 return mRollo.mMessageProc.mZoom > 0.999f;
651 }
652 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400653 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700654
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 public void setApps(ArrayList<ApplicationInfo> list) {
656 mAllAppsList = list;
657 if (mRollo != null) {
658 mRollo.setApps(list);
659 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400660 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700661 }
662
Joe Onoratoa8138d52009-10-06 19:25:30 -0700663 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500664 if (mAllAppsList == null) {
665 // Not done loading yet. We'll find out about it later.
666 return;
667 }
668
Joe Onoratoa8138d52009-10-06 19:25:30 -0700669 final int N = list.size();
670 if (mRollo != null) {
671 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
672 }
673
674 for (int i=0; i<N; i++) {
675 final ApplicationInfo item = list.get(i);
676 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
677 if (index < 0) {
678 index = -(index+1);
679 }
680 mAllAppsList.add(index, item);
681 if (mRollo != null) {
682 mRollo.addApp(index, item);
683 mRollo.mState.iconCount++;
684 }
685 }
686
687 if (mRollo != null) {
688 mRollo.saveAppsList();
689 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700690 }
691
Joe Onoratoa8138d52009-10-06 19:25:30 -0700692 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500693 if (mAllAppsList == null) {
694 // Not done loading yet. We'll find out about it later.
695 return;
696 }
697
Joe Onoratoa8138d52009-10-06 19:25:30 -0700698 final int N = list.size();
699 for (int i=0; i<N; i++) {
700 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400701 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700702 if (index >= 0) {
703 mAllAppsList.remove(index);
704 if (mRollo != null) {
705 mRollo.removeApp(index);
706 mRollo.mState.iconCount--;
707 }
708 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800709 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700710 // Try to recover. This should keep us from crashing for now.
711 }
712 }
713
714 if (mRollo != null) {
715 mRollo.saveAppsList();
716 }
717 }
718
719 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
720 // Just remove and add, because they may need to be re-sorted.
721 removeApps(list);
722 addApps(list);
723 }
724
725 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
726 public int compare(ApplicationInfo a, ApplicationInfo b) {
727 int result = a.title.toString().compareTo(b.toString());
728 if (result != 0) {
729 return result;
730 }
731 return a.intent.getComponent().compareTo(b.intent.getComponent());
732 }
733 };
734
Joe Onoratocb9f7982009-10-31 16:32:02 -0400735 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
736 ComponentName component = item.intent.getComponent();
737 final int N = list.size();
738 for (int i=0; i<N; i++) {
739 ApplicationInfo x = list.get(i);
740 if (x.intent.getComponent().equals(component)) {
741 return i;
742 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700743 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400744 return -1;
745 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700746
Joe Onoratoc567acb2009-08-31 14:34:43 -0700747 private static int countPages(int iconCount) {
748 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
749 int pages = iconCount / iconsPerPage;
750 if (pages*iconsPerPage != iconCount) {
751 pages++;
752 }
753 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754 }
755
Joe Onorato93839052009-08-06 20:34:32 -0700756 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700757
Joe Onorato1feb3a82009-08-08 22:32:00 -0700758 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700759 private int mWidth;
760 private int mHeight;
761
762 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700763 private Script mScript;
764 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700765 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700766 private Script.Invokable mInvokeFling;
767 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800768 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700769
Jason Samscd689e12009-09-29 15:28:22 -0700770 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700771 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700772 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700773 private ProgramFragment mPFTexMip;
774 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700775 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700776 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700777 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700778 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700779
Joe Onoratod63458b2009-10-15 21:19:09 -0700780 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500781 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700782 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700783
Joe Onoratobf15cb42009-08-07 14:33:40 -0700784 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700785 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700786 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700787
Joe Onoratobf15cb42009-08-07 14:33:40 -0700788 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700789 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700790 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700791 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700792
Joe Onorato6665c0f2009-09-02 15:27:24 -0700793 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700794 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700795
796 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400797 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700798
Jason Sams20df7c72009-11-05 12:30:24 -0800799 boolean mHasSurface = false;
800 private boolean mAppsDirty = false;
Jason Samsd8152b92009-10-13 17:19:10 -0700801
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700802 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700803 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700804
Jason Sams78aebd82009-09-15 13:06:59 -0700805 class BaseAlloc {
806 Allocation mAlloc;
807 Type mType;
808
809 void save() {
810 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700811 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700812 }
813
Jason Sams12c14a82009-10-06 14:33:15 -0700814 class AAMessage extends RenderScript.RSMessage {
815 public void run() {
816 mPosX = ((float)mData[0]) / (1 << 16);
817 mVelocity = ((float)mData[1]) / (1 << 16);
818 mZoom = ((float)mData[2]) / (1 << 16);
Jason Sams12c14a82009-10-06 14:33:15 -0700819 }
820 float mZoom;
821 float mPosX;
822 float mVelocity;
823 }
824 AAMessage mMessageProc;
825
Jason Sams476339d2009-09-29 18:14:38 -0700826 private boolean checkClickOK() {
Jason Sams2e19c052009-10-20 18:19:55 -0700827 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
828 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700829 }
830
Jason Sams78aebd82009-09-15 13:06:59 -0700831 class Params extends BaseAlloc {
832 Params() {
833 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
834 mAlloc = Allocation.createTyped(mRS, mType);
835 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700836 }
Jason Sams78aebd82009-09-15 13:06:59 -0700837 public int bubbleWidth;
838 public int bubbleHeight;
839 public int bubbleBitmapWidth;
840 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700841
Joe Onoratobcbeab82009-10-01 21:45:43 -0700842 public int homeButtonWidth;
843 public int homeButtonHeight;
844 public int homeButtonTextureWidth;
845 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700846 }
847
848 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700849 public float newPositionX;
850 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700851 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700852 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700853 public int selectedIconIndex = -1;
854 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700855 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700856 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700857 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700858
859 State() {
860 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
861 mAlloc = Allocation.createTyped(mRS, mType);
862 save();
863 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700864 }
865
866 public RolloRS() {
867 }
868
869 public void init(Resources res, int width, int height) {
870 mRes = res;
871 mWidth = width;
872 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700873 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700874 initProgramVertex();
875 initProgramFragment();
876 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700877 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700878 initGl();
879 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700880 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700881 initRs();
882 }
883
Jason Sams0aa71662009-10-02 18:43:18 -0700884 public void initMesh() {
885 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
886 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
887
Jason Samsd8152b92009-10-13 17:19:10 -0700888 float y = 0;
889 float z = 0;
890 for (int ct=0; ct < 200; ct++) {
891 float angle = 0;
892 float maxAngle = 3.14f * 0.16f;
893 float l = 1.f;
894
895 l = 1 - ((ct-5) * 0.10f);
896 if (ct > 7) {
897 angle = maxAngle * (ct - 7) * 0.2f;
898 angle = Math.min(angle, maxAngle);
899 }
Jason Samsc8514792009-10-29 14:27:29 -0700900 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700901 l = Math.min(1.0f, l);
902
903 y += 0.1f * Math.cos(angle);
904 z += 0.1f * Math.sin(angle);
905
906 float t = 0.1f * ct;
907 float ds = 0.08f;
908 tm.setColor(l, l, l, 0.99f);
909 tm.setTexture(ds, t);
910 tm.addVertex(-0.5f, y, z);
911 tm.setTexture(1 - ds, t);
912 tm.addVertex(0.5f, y, z);
913 }
914 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
915 tm.addTriangle(ct, ct+1, ct+2);
916 tm.addTriangle(ct+1, ct+3, ct+2);
917 }
918 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700919 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700920 }
921
Jason Samscd689e12009-09-29 15:28:22 -0700922 private void initProgramVertex() {
923 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
924 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700925
926 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700927 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700928 mPV = pvb.create();
929 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700930 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700931
Jason Sams37e7c2b2009-10-19 12:55:43 -0700932 //pva = new ProgramVertex.MatrixAllocation(mRS);
933 //pva.setupOrthoWindow(mWidth, mHeight);
934 //pvb.setTextureMatrixEnable(true);
935 //mPVOrtho = pvb.create();
936 //mPVOrtho.setName("PVOrtho");
937 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700938
939 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700940 }
Joe Onorato93839052009-08-06 20:34:32 -0700941
Jason Samscd689e12009-09-29 15:28:22 -0700942 private void initProgramFragment() {
943 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700944 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700945 sb.setMag(Sampler.Value.LINEAR);
946 sb.setWrapS(Sampler.Value.CLAMP);
947 sb.setWrapT(Sampler.Value.CLAMP);
948 Sampler linear = sb.create();
949
950 sb.setMin(Sampler.Value.NEAREST);
951 sb.setMag(Sampler.Value.NEAREST);
952 Sampler nearest = sb.create();
953
954 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700955 //mPFColor = bf.create();
956 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700957
958 bf.setTexEnable(true, 0);
959 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700960 mPFTexMip = bf.create();
961 mPFTexMip.setName("PFTexMip");
962 mPFTexMip.bindSampler(linear, 0);
963
964 mPFTexNearest = bf.create();
965 mPFTexNearest.setName("PFTexNearest");
966 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700967 }
968
969 private void initProgramStore() {
970 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
971 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700972 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700973 bs.setDitherEnable(true);
974 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
975 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
976 mPSIcons = bs.create();
977 mPSIcons.setName("PSIcons");
978
979 //bs.setDitherEnable(false);
980 //mPSText = bs.create();
981 //mPSText.setName("PSText");
982 }
983
984 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700985 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700986 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700987 }
Jason Sams78aebd82009-09-15 13:06:59 -0700988
Joe Onorato1feb3a82009-08-08 22:32:00 -0700989 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700990 mParams = new Params();
991 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700992
Joe Onoratobf15cb42009-08-07 14:33:40 -0700993 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700994
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700995 mParams.bubbleWidth = bubble.getBubbleWidth();
996 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
997 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
998 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700999
Joe Onoratod63458b2009-10-15 21:19:09 -07001000 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1001 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1002 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001003 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1004 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1005 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001006 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1007 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1008 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001009 mParams.homeButtonWidth = 76;
1010 mParams.homeButtonHeight = 68;
1011 mParams.homeButtonTextureWidth = 128;
1012 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001013
Joe Onoratod63458b2009-10-15 21:19:09 -07001014 mState.homeButtonId = mHomeButtonNormal.getID();
1015
Joe Onorato1feb3a82009-08-08 22:32:00 -07001016 mParams.save();
1017 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001018
Joe Onorato1291a8c2009-09-15 15:07:25 -04001019 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1020 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1021 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001022
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001024 }
1025
Jason Sams37e7c2b2009-10-19 12:55:43 -07001026 private void initScript(int id) {
1027 }
1028
1029 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001030 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001031 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -07001032 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001033 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001034 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1035 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001036 mInvokeMove = sb.addInvokable("move");
1037 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001038 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001039 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001040 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001041 mScript = sb.create();
1042 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1043 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1044 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1045 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1046 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001047
Jason Sams12c14a82009-10-06 14:33:15 -07001048 mMessageProc = new AAMessage();
1049 mRS.mMessageCallback = mMessageProc;
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) {
1067 setZoom(mNextZoom, mNextAnimate);
1068 mZoomDirty = false;
1069 }
Jason Sams20df7c72009-11-05 12:30:24 -08001070 }
1071 }
1072
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073 private void setApps(ArrayList<ApplicationInfo> list) {
1074 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001075 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001076 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001077 allocCount = 1;
1078 }
1079
Joe Onorato9c1289c2009-08-17 11:03:03 -04001080 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001081 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001082 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001083
1084 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001085 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001086 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001088 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001089
1090 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1091
Joe Onorato9c1289c2009-08-17 11:03:03 -04001092 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001093 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001094 saveAppsList();
1095 }
1096
Joe Onorato3a8820b2009-11-10 15:06:42 -08001097 private void setZoom(float zoom, boolean animate) {
1098 mRollo.clearSelectedIcon();
1099 mRollo.setHomeSelected(SELECTED_NONE);
1100 if (zoom > 0.001f) {
1101 mRollo.mState.zoomTarget = zoom;
1102 } else {
1103 mRollo.mState.zoomTarget = 0;
1104 }
1105 mRollo.mState.save();
1106 if (!animate) {
1107 mRollo.mInvokeSetZoom.execute();
1108 }
1109 }
1110
Jason Samsc8514792009-10-29 14:27:29 -07001111 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1112 int black[] = new int[w > h ? w : h];
1113 Allocation.Adapter2D a = alloc.createAdapter2D();
1114 int mip = 0;
1115 while (w > 1 || h > 1) {
1116 a.subData(0, 0, 1, h, black);
1117 a.subData(w-1, 0, 1, h, black);
1118 a.subData(0, 0, w, 1, black);
1119 a.subData(0, h-1, w, 1, black);
1120 mip++;
1121 w = (w + 1) >> 1;
1122 h = (h + 1) >> 1;
1123 a.setConstraint(Dimension.LOD, mip);
1124 }
1125 a.subData(0, 0, 1, 1, black);
1126 }
1127
Joe Onoratoa8138d52009-10-06 19:25:30 -07001128 private void uploadAppIcon(int index, ApplicationInfo item) {
1129 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001130 Element.RGBA_8888(mRS), true);
1131 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1132
Joe Onoratoa8138d52009-10-06 19:25:30 -07001133 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001134 Element.RGBA_8888(mRS), true);
1135 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001136
1137 mIcons[index].uploadToTexture(0);
1138 mLabels[index].uploadToTexture(0);
1139
1140 mIconIds[index] = mIcons[index].getID();
1141 mLabelIds[index] = mLabels[index].getID();
1142 }
1143
1144 /**
1145 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1146 * fill in the values and call saveAppsList().
1147 */
1148 private void reallocAppsList(int count) {
1149 Allocation[] icons = new Allocation[count];
1150 int[] iconIds = new int[count];
1151 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1152
1153 Allocation[] labels = new Allocation[count];
1154 int[] labelIds = new int[count];
1155 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1156
1157 final int oldCount = mIcons.length;
1158
1159 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1160 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1161 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1162 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1163
1164 mIcons = icons;
1165 mIconIds = iconIds;
1166 mLabels = labels;
1167 mLabelIds = labelIds;
1168 }
1169
1170 /**
1171 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1172 */
1173 private void addApp(int index, ApplicationInfo item) {
1174 final int count = mState.iconCount - index;
1175 final int dest = index + 1;
1176
1177 System.arraycopy(mIcons, index, mIcons, dest, count);
1178 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1179 System.arraycopy(mLabels, index, mLabels, dest, count);
1180 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1181
Jason Sams20df7c72009-11-05 12:30:24 -08001182 if (mHasSurface ) {
1183 uploadAppIcon(index, item);
1184 } else {
1185 mAppsDirty = true;
1186 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001187 }
1188
1189 /**
1190 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1191 */
1192 private void removeApp(int index) {
1193 final int count = mState.iconCount - index - 1;
1194 final int src = index + 1;
1195
1196 System.arraycopy(mIcons, src, mIcons, index, count);
1197 System.arraycopy(mIconIds, src, mIconIds, index, count);
1198 System.arraycopy(mLabels, src, mLabels, index, count);
1199 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1200
1201 final int last = mState.iconCount - 1;
1202 mIcons[last] = null;
1203 mIconIds[last] = 0;
1204 mLabels[last] = null;
1205 mLabelIds[last] = 0;
1206 }
1207
1208 /**
1209 * Send the apps list structures to RS.
1210 */
1211 private void saveAppsList() {
1212 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001213
Joe Onoratoa8138d52009-10-06 19:25:30 -07001214 mAllocIconIds.data(mIconIds);
1215 mAllocLabelIds.data(mLabelIds);
1216
Jason Sams37e7c2b2009-10-19 12:55:43 -07001217 if (mScript != null) { // this happens when we init it
1218 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1219 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001220 }
1221
1222 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001223
1224 // Note: mScript may be null if we haven't initialized it yet.
1225 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001226 if (mInvokeResetWAR != null) {
1227 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001228 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001229 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001230 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001231
1232 void initTouchState() {
1233 int width = getWidth();
1234 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001235 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1236 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001237
Jason Sams37e7c2b2009-10-19 12:55:43 -07001238 int centerY = (height / 2);
1239 mTouchYBorders[0] = centerY - (cellHeight * 2);
1240 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001241 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001242 mTouchYBorders[3] = centerY + cellHeight;
1243 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001244
Joe Onorato6665c0f2009-09-02 15:27:24 -07001245 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001246 mTouchXBorders[0] = 0;
1247 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001248 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001249 mTouchXBorders[3] = centerX + (width / 4);
1250 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001251 }
1252
Joe Onorato664457d2009-10-28 16:30:34 -04001253 void fling() {
1254 mInvokeFling.execute();
1255 }
1256
1257 void move() {
1258 mInvokeMove.execute();
1259 }
1260
1261 void moveTo(float row) {
1262 mState.targetPos = row;
1263 mState.save();
1264 mInvokeMoveTo.execute();
1265 }
1266
Jason Sams37e7c2b2009-10-19 12:55:43 -07001267 int chooseTappedIcon(int x, int y, float pos) {
1268 // Adjust for scroll position if not zero.
1269 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001270
Joe Onorato6665c0f2009-09-02 15:27:24 -07001271 int col = -1;
1272 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001273 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1274 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1275 col = i;
1276 break;
1277 }
1278 }
1279 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1280 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1281 row = i;
1282 break;
1283 }
1284 }
1285
1286 if (row < 0 || col < 0) {
1287 return -1;
1288 }
1289
Joe Onorato664457d2009-10-28 16:30:34 -04001290 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001291 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001292
Joe Onorato664457d2009-10-28 16:30:34 -04001293 if (index >= mState.iconCount) {
1294 return -1;
1295 } else {
1296 return index;
1297 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001298 }
1299
Joe Onorato6665c0f2009-09-02 15:27:24 -07001300 /**
1301 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001302 *
1303 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001304 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001305 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001306 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001307 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001308 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001309 }
1310
Joe Onoratoc61cff92009-11-08 11:54:39 -05001311 /**
1312 * Select the icon at the given index.
1313 *
1314 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001315 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001316 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001317 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001318 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001319 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001320 if (mLastSelection == SELECTION_ICONS) {
1321 mLastSelection = SELECTION_NONE;
1322 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001323 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001324 if (pressed == SELECTED_FOCUSED) {
1325 mLastSelection = SELECTION_ICONS;
1326 }
1327
Joe Onorato52a653f2009-11-11 14:52:11 -08001328 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001329 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001330
Joe Onorato52a653f2009-11-11 14:52:11 -08001331 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001332 Bitmap selectionBitmap = mSelectionBitmap;
1333
1334 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001335 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001336 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001337
1338 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001339 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001340 mSelectedIcon.uploadToTexture(0);
1341 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001342
1343 if (prev != index) {
1344 if (info.title != null && info.title.length() > 0) {
1345 Log.d(TAG, "sendAccessibilityEvent SELECTION_ICONS " + info.title);
1346 //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) {
1370 Log.d(TAG, "sendAccessibilityEvent SELECTION_HOME");
1371 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1372 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001373 break;
1374 case SELECTED_PRESSED:
1375 mState.homeButtonId = mHomeButtonPressed.getID();
1376 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001377 }
1378 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001379 }
Joe Onorato93839052009-08-06 20:34:32 -07001380}
1381
1382