blob: 7f6e8ff735ccbf1754bc0099f6d01cef69b33fdf [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 Onoratofab74402009-11-11 16:05:23 -0800182 // Without this, we leak mMessageCallback which leaks the context.
183 mRS.mMessageCallback = null;
Joe Onorato7bb17492009-09-24 17:51:01 -0700184 }
185
186 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700187 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800188 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700189
Joe Onorato080d9b62009-11-02 12:01:11 -0500190 super.surfaceChanged(holder, format, w, h);
191
Jason Sams90396672009-11-03 13:59:34 -0800192 if (mRS == null) {
Jason Sams90396672009-11-03 13:59:34 -0800193 mRS = createRenderScript(true);
194 mRollo = new RolloRS();
Jason Sams20df7c72009-11-05 12:30:24 -0800195 mRollo.mHasSurface = true;
Jason Sams90396672009-11-03 13:59:34 -0800196 mRollo.init(getResources(), w, h);
197 if (mAllAppsList != null) {
198 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800199 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800200 if (mShouldGainFocus) {
201 gainFocus();
202 mShouldGainFocus = false;
203 }
Joe Onorato3a8820b2009-11-10 15:06:42 -0800204 mRollo.dirtyCheck();
Jason Sams20df7c72009-11-05 12:30:24 -0800205 } else {
206 mRollo.mHasSurface = true;
207 mRollo.dirtyCheck();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700209
210 Resources res = getContext().getResources();
211 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700212
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800213 //long endTime = SystemClock.uptimeMillis();
214 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700215 }
Jason Sams2e19c052009-10-20 18:19:55 -0700216
Joe Onorato93839052009-08-06 20:34:32 -0700217 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800218 public void onWindowFocusChanged(boolean hasWindowFocus) {
219 super.onWindowFocusChanged(hasWindowFocus);
220 if (mArrowNavigation) {
221 if (!hasWindowFocus) {
222 // Clear selection when we lose window focus
223 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500224 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800225 mRollo.clearSelectedIcon();
226 mRollo.mState.save();
227 } else if (hasWindowFocus) {
228 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500229 if (mLastSelection == SELECTION_ICONS) {
230 int selection = mLastSelectedIcon;
231 final int firstIcon = Math.round(mRollo.mMessageProc.mPosX) *
232 Defines.COLUMNS_PER_PAGE;
233 if (selection < 0 || // No selection
234 selection < firstIcon || // off the top of the screen
235 selection >= mRollo.mState.iconCount || // past last icon
236 selection >= firstIcon + // past last icon on screen
237 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
238 selection = firstIcon;
239 }
240
241 // Select the first icon when we gain window focus
242 mRollo.selectIcon(selection, SELECTED_FOCUSED);
243 mRollo.mState.save();
244 } else if (mLastSelection == SELECTION_HOME) {
245 mRollo.setHomeSelected(SELECTED_FOCUSED);
246 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800247 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800248 }
249 }
250 }
251 }
252
253 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700254 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
255 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400256
257 if (!isVisible()) {
258 return;
259 }
260
Mike Cleron7d5d7462009-10-20 14:06:00 -0700261 if (gainFocus) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800262 if (mRollo != null) {
263 gainFocus();
264 } else {
265 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700266 }
267 } else {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800268 if (mRollo != null) {
269 if (mArrowNavigation) {
270 // Clear selection when we lose focus
271 mRollo.clearSelectedIcon();
272 mRollo.setHomeSelected(SELECTED_NONE);
273 mRollo.mState.save();
274 mArrowNavigation = false;
275 }
276 } else {
277 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700278 }
279 }
280 }
281
Mike Cleronb64b67a2009-11-08 14:56:25 -0800282 private void gainFocus() {
283 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
284 // Select the first icon when we gain keyboard focus
285 mArrowNavigation = true;
286 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE,
287 SELECTED_FOCUSED);
288 mRollo.mState.save();
289 }
290 }
291
Mike Cleron7d5d7462009-10-20 14:06:00 -0700292 @Override
293 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700294
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800295 boolean handled = false;
296
Joe Onorato859b3a72009-10-28 15:17:01 -0400297 if (!isVisible()) {
298 return false;
299 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500300 final int iconCount = mRollo.mState.iconCount;
301
Mike Cleron7d5d7462009-10-20 14:06:00 -0700302 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
303 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500304 if (mLastSelection == SELECTION_HOME) {
305 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
306 mLauncher.closeAllApps(true);
307 } else {
308 int whichApp = mRollo.mState.selectedIconIndex;
309 if (whichApp >= 0) {
310 ApplicationInfo app = mAllAppsList.get(whichApp);
311 mLauncher.startActivitySafely(app.intent);
312 handled = true;
313 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700314 }
315 }
316 }
Jason Sams2e19c052009-10-20 18:19:55 -0700317
Joe Onoratoa13f5742009-11-02 17:15:19 -0500318 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700319 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700320
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800321 int currentSelection = mRollo.mState.selectedIconIndex;
322 int currentTopRow = Math.round(mRollo.mMessageProc.mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700323
Mike Cleron7d5d7462009-10-20 14:06:00 -0700324 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500325 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700326
Mike Cleron7d5d7462009-10-20 14:06:00 -0700327 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500328 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700329 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700330
Mike Cleron7d5d7462009-10-20 14:06:00 -0700331 int newSelection = currentSelection;
332
333 switch (keyCode) {
334 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500335 if (mLastSelection == SELECTION_HOME) {
336 mRollo.setHomeSelected(SELECTED_NONE);
337 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
338 if (lastRowCount == 0) {
339 lastRowCount = Defines.COLUMNS_PER_PAGE;
340 }
341 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
342 if (newSelection >= iconCount) {
343 newSelection = iconCount-1;
344 }
345 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
346 - (Defines.ROWS_PER_PAGE - 1);
347 if (target < 0) {
348 target = 0;
349 }
350 if (currentTopRow != target) {
351 mRollo.moveTo(target);
352 }
353 } else {
354 if (currentPageRow > 0) {
355 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
356 } else if (currentTopRow > 0) {
357 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
358 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
359 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700360 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800361 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700362 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800363
Joe Onoratoa13f5742009-11-02 17:15:19 -0500364 case KeyEvent.KEYCODE_DPAD_DOWN: {
365 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
366 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
367 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500368 if (mLastSelection != SELECTION_HOME) {
369 if (currentRow < rowCount-1) {
370 mRollo.setHomeSelected(SELECTED_NONE);
371 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
372 if (newSelection >= iconCount) {
373 // Go from D to G in this arrangement:
374 // A B C D
375 // E F G
376 newSelection = iconCount - 1;
377 }
378 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
379 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
380 Defines.ROWS_PER_PAGE + 1);
381 }
382 } else {
383 newSelection = -1;
384 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700385 }
386 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800387 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700388 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500389 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700390 case KeyEvent.KEYCODE_DPAD_LEFT:
391 if (currentPageCol > 0) {
392 newSelection = currentSelection - 1;
393 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800394 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700395 break;
396 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700397 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500398 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700399 newSelection = currentSelection + 1;
400 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800401 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700402 break;
403 }
404 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500405 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700406 mRollo.mState.save();
407 }
408 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800409 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700410 }
411
Joe Onorato93839052009-08-06 20:34:32 -0700412 @Override
413 public boolean onTouchEvent(MotionEvent ev)
414 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700415 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700416
Joe Onorato7bb17492009-09-24 17:51:01 -0700417 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700418 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700419 }
420
Joe Onoratofb0ca672009-09-14 17:55:46 -0400421 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700422 return true;
423 }
424
425 super.onTouchEvent(ev);
426
Joe Onoratofb0ca672009-09-14 17:55:46 -0400427 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700428 int y = (int)ev.getY();
429
Joe Onoratobcbeab82009-10-01 21:45:43 -0700430 int action = ev.getAction();
431 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400432 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700433 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
434 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500435 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700436 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700437 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700438 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700439 mTouchTracking = TRACKING_FLING;
440
441 mMotionDownRawX = (int)ev.getRawX();
442 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700443
Mike Cleron7d5d7462009-10-20 14:06:00 -0700444 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700445 mRollo.mState.newTouchDown = 1;
446
447 if (!mRollo.checkClickOK()) {
448 mRollo.clearSelectedIcon();
449 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700450 mDownIconIndex = mCurrentIconIndex
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500451 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700452 if (mDownIconIndex < 0) {
453 // if nothing was selected, no long press.
454 cancelLongPress();
455 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700456 }
457 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700458 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700459 mVelocity = VelocityTracker.obtain();
460 mVelocity.addMovement(ev);
461 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700462 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400463 break;
464 case MotionEvent.ACTION_MOVE:
465 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700466 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500467 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
468 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700469 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700470 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700471 int rawX = (int)ev.getRawX();
472 int rawY = (int)ev.getRawY();
473 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700474 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700475
Joe Onorato82ca5502009-10-15 16:59:23 -0700476 if (!mStartedScrolling && slop < mSlop) {
477 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700478 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700479 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
480 if (mDownIconIndex != mCurrentIconIndex) {
481 // If a different icon is selected, don't allow it to be picked up.
482 // This handles off-axis dragging.
483 cancelLongPress();
484 mCurrentIconIndex = -1;
485 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700486 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700487 if (!mStartedScrolling) {
488 cancelLongPress();
489 mCurrentIconIndex = -1;
490 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700491 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700492 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700493 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700494
Joe Onoratobcbeab82009-10-01 21:45:43 -0700495 mStartedScrolling = true;
496 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700497 mVelocity.addMovement(ev);
498 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700499 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400500 }
501 break;
502 case MotionEvent.ACTION_UP:
503 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700504 if (mTouchTracking == TRACKING_HOME) {
505 if (action == MotionEvent.ACTION_UP) {
506 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400507 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700508 mLauncher.closeAllApps(true);
509 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500510 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700511 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700512 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700513 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700514 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700515 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700516 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700517
Jason Sams12c14a82009-10-06 14:33:15 -0700518 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700519 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700520 mRollo.clearSelectedIcon();
521 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700522 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700523
Joe Onorato539ed9d2009-10-02 10:22:14 -0700524 if (mVelocity != null) {
525 mVelocity.recycle();
526 mVelocity = null;
527 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700528 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700529 mTouchTracking = TRACKING_NONE;
530 break;
Joe Onorato93839052009-08-06 20:34:32 -0700531 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700532
533 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700534 }
535
Joe Onorato6665c0f2009-09-02 15:27:24 -0700536 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700537 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400538 return;
539 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700540 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
541 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400542 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700543 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700544 mLauncher.startActivitySafely(app.intent);
545 }
546 }
547
548 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700549 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400550 return true;
551 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700552 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
553 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
554 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700555
556 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700557 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
558 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700559
Joe Onoratobcbeab82009-10-01 21:45:43 -0700560 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
561 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700562 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700563 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700564 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700565
Joe Onorato7bb17492009-09-24 17:51:01 -0700566 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700567 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700568 return true;
569 }
570
Joe Onorato52a653f2009-11-11 14:52:11 -0800571 @Override
572 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
573 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
574 if (!isVisible()) {
575 return false;
576 }
577 String text = null;
578 int index;
579 int count = mAllAppsList.size() + 1; // +1 is home
580 int pos = -1;
581 switch (mLastSelection) {
582 case SELECTION_ICONS:
583 index = mRollo.mState.selectedIconIndex;
584 if (index >= 0) {
585 ApplicationInfo info = mAllAppsList.get(index);
586 if (info.title != null) {
587 text = info.title.toString();
588 pos = index;
589 }
590 }
591 break;
592 case SELECTION_HOME:
593 text = getContext().getString(R.string.all_apps_home_button_label);
594 pos = count;
595 break;
596 }
597 if (text != null) {
598 Log.d(TAG, "dispatchPopulateAccessibilityEvent setting text=" + text);
599 event.setEnabled(true);
600 event.getText().add(text);
601 //event.setContentDescription(text);
602 event.setItemCount(count);
603 event.setCurrentItemIndex(pos);
604 }
605 }
606 return false;
607 }
608
Joe Onorato5162ea92009-09-03 09:39:42 -0700609 public void setDragController(DragController dragger) {
610 mDragController = dragger;
611 }
612
613 public void onDropCompleted(View target, boolean success) {
614 }
615
Joe Onorato4db52312009-10-06 11:17:43 -0700616 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800617 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700618 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800619 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700620 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800621 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400622 cancelLongPress();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800623 if (mRollo == null || !mRollo.mHasSurface) {
624 mZoomDirty = true;
625 mNextZoom = zoom;
626 mNextAnimate = animate;
627 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700628 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800629 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700630 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400631 }
632
633 public boolean isVisible() {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800634 if (mZoomDirty) {
635 return mNextZoom > 0.001f;
636 } else {
637 if (mRollo == null) {
638 return false;
639 } else {
640 return mRollo.mMessageProc.mZoom > 0.001f;
641 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700642 }
Joe Onorato3a8820b2009-11-10 15:06:42 -0800643 }
644
645 public boolean isOpaque() {
646 if (mZoomDirty) {
647 return mNextZoom > 0.999f;
648 } else {
649 if (mRollo == null) {
650 return false;
651 } else {
652 return mRollo.mMessageProc.mZoom > 0.999f;
653 }
654 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400655 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700656
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 public void setApps(ArrayList<ApplicationInfo> list) {
658 mAllAppsList = list;
659 if (mRollo != null) {
660 mRollo.setApps(list);
661 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400662 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700663 }
664
Joe Onoratoa8138d52009-10-06 19:25:30 -0700665 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500666 if (mAllAppsList == null) {
667 // Not done loading yet. We'll find out about it later.
668 return;
669 }
670
Joe Onoratoa8138d52009-10-06 19:25:30 -0700671 final int N = list.size();
672 if (mRollo != null) {
673 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
674 }
675
676 for (int i=0; i<N; i++) {
677 final ApplicationInfo item = list.get(i);
678 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
679 if (index < 0) {
680 index = -(index+1);
681 }
682 mAllAppsList.add(index, item);
683 if (mRollo != null) {
684 mRollo.addApp(index, item);
685 mRollo.mState.iconCount++;
686 }
687 }
688
689 if (mRollo != null) {
690 mRollo.saveAppsList();
691 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700692 }
693
Joe Onoratoa8138d52009-10-06 19:25:30 -0700694 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500695 if (mAllAppsList == null) {
696 // Not done loading yet. We'll find out about it later.
697 return;
698 }
699
Joe Onoratoa8138d52009-10-06 19:25:30 -0700700 final int N = list.size();
701 for (int i=0; i<N; i++) {
702 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400703 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700704 if (index >= 0) {
705 mAllAppsList.remove(index);
706 if (mRollo != null) {
707 mRollo.removeApp(index);
708 mRollo.mState.iconCount--;
709 }
710 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800711 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700712 // Try to recover. This should keep us from crashing for now.
713 }
714 }
715
716 if (mRollo != null) {
717 mRollo.saveAppsList();
718 }
719 }
720
721 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
722 // Just remove and add, because they may need to be re-sorted.
723 removeApps(list);
724 addApps(list);
725 }
726
727 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
728 public int compare(ApplicationInfo a, ApplicationInfo b) {
729 int result = a.title.toString().compareTo(b.toString());
730 if (result != 0) {
731 return result;
732 }
733 return a.intent.getComponent().compareTo(b.intent.getComponent());
734 }
735 };
736
Joe Onoratocb9f7982009-10-31 16:32:02 -0400737 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
738 ComponentName component = item.intent.getComponent();
739 final int N = list.size();
740 for (int i=0; i<N; i++) {
741 ApplicationInfo x = list.get(i);
742 if (x.intent.getComponent().equals(component)) {
743 return i;
744 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700745 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400746 return -1;
747 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700748
Joe Onoratoc567acb2009-08-31 14:34:43 -0700749 private static int countPages(int iconCount) {
750 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
751 int pages = iconCount / iconsPerPage;
752 if (pages*iconsPerPage != iconCount) {
753 pages++;
754 }
755 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400756 }
757
Joe Onorato93839052009-08-06 20:34:32 -0700758 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700759
Joe Onorato1feb3a82009-08-08 22:32:00 -0700760 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700761 private int mWidth;
762 private int mHeight;
763
764 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700765 private Script mScript;
766 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700767 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700768 private Script.Invokable mInvokeFling;
769 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800770 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700771
Jason Samscd689e12009-09-29 15:28:22 -0700772 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700773 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700774 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700775 private ProgramFragment mPFTexMip;
776 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700777 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700778 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700779 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700780 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700781
Joe Onoratod63458b2009-10-15 21:19:09 -0700782 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500783 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700784 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700785
Joe Onoratobf15cb42009-08-07 14:33:40 -0700786 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700787 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700788 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700789
Joe Onoratobf15cb42009-08-07 14:33:40 -0700790 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700791 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700792 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700793 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700794
Joe Onorato6665c0f2009-09-02 15:27:24 -0700795 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700796 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700797
798 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400799 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700800
Jason Sams20df7c72009-11-05 12:30:24 -0800801 boolean mHasSurface = false;
802 private boolean mAppsDirty = false;
Jason Samsd8152b92009-10-13 17:19:10 -0700803
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700804 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700805 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700806
Jason Sams78aebd82009-09-15 13:06:59 -0700807 class BaseAlloc {
808 Allocation mAlloc;
809 Type mType;
810
811 void save() {
812 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700813 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700814 }
815
Jason Sams12c14a82009-10-06 14:33:15 -0700816 class AAMessage extends RenderScript.RSMessage {
817 public void run() {
818 mPosX = ((float)mData[0]) / (1 << 16);
819 mVelocity = ((float)mData[1]) / (1 << 16);
820 mZoom = ((float)mData[2]) / (1 << 16);
Jason Sams12c14a82009-10-06 14:33:15 -0700821 }
822 float mZoom;
823 float mPosX;
824 float mVelocity;
825 }
826 AAMessage mMessageProc;
827
Jason Sams476339d2009-09-29 18:14:38 -0700828 private boolean checkClickOK() {
Jason Sams2e19c052009-10-20 18:19:55 -0700829 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
830 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700831 }
832
Jason Sams78aebd82009-09-15 13:06:59 -0700833 class Params extends BaseAlloc {
834 Params() {
835 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
836 mAlloc = Allocation.createTyped(mRS, mType);
837 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700838 }
Jason Sams78aebd82009-09-15 13:06:59 -0700839 public int bubbleWidth;
840 public int bubbleHeight;
841 public int bubbleBitmapWidth;
842 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700843
Joe Onoratobcbeab82009-10-01 21:45:43 -0700844 public int homeButtonWidth;
845 public int homeButtonHeight;
846 public int homeButtonTextureWidth;
847 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700848 }
849
850 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700851 public float newPositionX;
852 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700853 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700854 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700855 public int selectedIconIndex = -1;
856 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700857 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700858 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700859 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700860
861 State() {
862 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
863 mAlloc = Allocation.createTyped(mRS, mType);
864 save();
865 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700866 }
867
868 public RolloRS() {
869 }
870
871 public void init(Resources res, int width, int height) {
872 mRes = res;
873 mWidth = width;
874 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700875 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700876 initProgramVertex();
877 initProgramFragment();
878 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700879 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700880 initGl();
881 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700882 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700883 initRs();
884 }
885
Jason Sams0aa71662009-10-02 18:43:18 -0700886 public void initMesh() {
887 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
888 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
889
Jason Samsd8152b92009-10-13 17:19:10 -0700890 float y = 0;
891 float z = 0;
892 for (int ct=0; ct < 200; ct++) {
893 float angle = 0;
894 float maxAngle = 3.14f * 0.16f;
895 float l = 1.f;
896
897 l = 1 - ((ct-5) * 0.10f);
898 if (ct > 7) {
899 angle = maxAngle * (ct - 7) * 0.2f;
900 angle = Math.min(angle, maxAngle);
901 }
Jason Samsc8514792009-10-29 14:27:29 -0700902 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700903 l = Math.min(1.0f, l);
904
905 y += 0.1f * Math.cos(angle);
906 z += 0.1f * Math.sin(angle);
907
908 float t = 0.1f * ct;
909 float ds = 0.08f;
910 tm.setColor(l, l, l, 0.99f);
911 tm.setTexture(ds, t);
912 tm.addVertex(-0.5f, y, z);
913 tm.setTexture(1 - ds, t);
914 tm.addVertex(0.5f, y, z);
915 }
916 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
917 tm.addTriangle(ct, ct+1, ct+2);
918 tm.addTriangle(ct+1, ct+3, ct+2);
919 }
920 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700921 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700922 }
923
Jason Samscd689e12009-09-29 15:28:22 -0700924 private void initProgramVertex() {
925 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
926 pva.setupProjectionNormalized(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 Samscd689e12009-09-29 15:28:22 -0700932 mPV.bindAllocation(pva);
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 Sams12c14a82009-10-06 14:33:15 -07001050 mMessageProc = new AAMessage();
1051 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001052 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001053 }
Joe Onorato93839052009-08-06 20:34:32 -07001054
Jason Sams20df7c72009-11-05 12:30:24 -08001055 private void uploadApps(ArrayList<ApplicationInfo> list) {
1056 for (int i=0; i < mState.iconCount; i++) {
1057 uploadAppIcon(i, list.get(i));
1058 }
1059 }
1060
1061 void dirtyCheck() {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001062 if (mHasSurface) {
1063 if (mAppsDirty) {
1064 uploadApps(mAllAppsList);
1065 saveAppsList();
1066 mAppsDirty = false;
1067 }
1068 if (mZoomDirty) {
1069 setZoom(mNextZoom, mNextAnimate);
1070 mZoomDirty = false;
1071 }
Jason Sams20df7c72009-11-05 12:30:24 -08001072 }
1073 }
1074
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075 private void setApps(ArrayList<ApplicationInfo> list) {
1076 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001077 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001078 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001079 allocCount = 1;
1080 }
1081
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001083 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001084 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001085
1086 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001087 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001088 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001089
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001090 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001091
1092 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1093
Joe Onorato9c1289c2009-08-17 11:03:03 -04001094 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001095 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001096 saveAppsList();
1097 }
1098
Joe Onorato3a8820b2009-11-10 15:06:42 -08001099 private void setZoom(float zoom, boolean animate) {
1100 mRollo.clearSelectedIcon();
1101 mRollo.setHomeSelected(SELECTED_NONE);
1102 if (zoom > 0.001f) {
1103 mRollo.mState.zoomTarget = zoom;
1104 } else {
1105 mRollo.mState.zoomTarget = 0;
1106 }
1107 mRollo.mState.save();
1108 if (!animate) {
1109 mRollo.mInvokeSetZoom.execute();
1110 }
1111 }
1112
Jason Samsc8514792009-10-29 14:27:29 -07001113 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1114 int black[] = new int[w > h ? w : h];
1115 Allocation.Adapter2D a = alloc.createAdapter2D();
1116 int mip = 0;
1117 while (w > 1 || h > 1) {
1118 a.subData(0, 0, 1, h, black);
1119 a.subData(w-1, 0, 1, h, black);
1120 a.subData(0, 0, w, 1, black);
1121 a.subData(0, h-1, w, 1, black);
1122 mip++;
1123 w = (w + 1) >> 1;
1124 h = (h + 1) >> 1;
1125 a.setConstraint(Dimension.LOD, mip);
1126 }
1127 a.subData(0, 0, 1, 1, black);
1128 }
1129
Joe Onoratoa8138d52009-10-06 19:25:30 -07001130 private void uploadAppIcon(int index, ApplicationInfo item) {
1131 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001132 Element.RGBA_8888(mRS), true);
1133 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1134
Joe Onoratoa8138d52009-10-06 19:25:30 -07001135 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001136 Element.RGBA_8888(mRS), true);
1137 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001138
1139 mIcons[index].uploadToTexture(0);
1140 mLabels[index].uploadToTexture(0);
1141
1142 mIconIds[index] = mIcons[index].getID();
1143 mLabelIds[index] = mLabels[index].getID();
1144 }
1145
1146 /**
1147 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1148 * fill in the values and call saveAppsList().
1149 */
1150 private void reallocAppsList(int count) {
1151 Allocation[] icons = new Allocation[count];
1152 int[] iconIds = new int[count];
1153 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1154
1155 Allocation[] labels = new Allocation[count];
1156 int[] labelIds = new int[count];
1157 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1158
1159 final int oldCount = mIcons.length;
1160
1161 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1162 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1163 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1164 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1165
1166 mIcons = icons;
1167 mIconIds = iconIds;
1168 mLabels = labels;
1169 mLabelIds = labelIds;
1170 }
1171
1172 /**
1173 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1174 */
1175 private void addApp(int index, ApplicationInfo item) {
1176 final int count = mState.iconCount - index;
1177 final int dest = index + 1;
1178
1179 System.arraycopy(mIcons, index, mIcons, dest, count);
1180 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1181 System.arraycopy(mLabels, index, mLabels, dest, count);
1182 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1183
Jason Sams20df7c72009-11-05 12:30:24 -08001184 if (mHasSurface ) {
1185 uploadAppIcon(index, item);
1186 } else {
1187 mAppsDirty = true;
1188 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001189 }
1190
1191 /**
1192 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1193 */
1194 private void removeApp(int index) {
1195 final int count = mState.iconCount - index - 1;
1196 final int src = index + 1;
1197
1198 System.arraycopy(mIcons, src, mIcons, index, count);
1199 System.arraycopy(mIconIds, src, mIconIds, index, count);
1200 System.arraycopy(mLabels, src, mLabels, index, count);
1201 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1202
1203 final int last = mState.iconCount - 1;
1204 mIcons[last] = null;
1205 mIconIds[last] = 0;
1206 mLabels[last] = null;
1207 mLabelIds[last] = 0;
1208 }
1209
1210 /**
1211 * Send the apps list structures to RS.
1212 */
1213 private void saveAppsList() {
1214 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001215
Joe Onoratoa8138d52009-10-06 19:25:30 -07001216 mAllocIconIds.data(mIconIds);
1217 mAllocLabelIds.data(mLabelIds);
1218
Jason Sams37e7c2b2009-10-19 12:55:43 -07001219 if (mScript != null) { // this happens when we init it
1220 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1221 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001222 }
1223
1224 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001225
1226 // Note: mScript may be null if we haven't initialized it yet.
1227 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001228 if (mInvokeResetWAR != null) {
1229 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001230 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001231 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001233
1234 void initTouchState() {
1235 int width = getWidth();
1236 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001237 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1238 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001239
Jason Sams37e7c2b2009-10-19 12:55:43 -07001240 int centerY = (height / 2);
1241 mTouchYBorders[0] = centerY - (cellHeight * 2);
1242 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001243 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001244 mTouchYBorders[3] = centerY + cellHeight;
1245 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001246
Joe Onorato6665c0f2009-09-02 15:27:24 -07001247 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001248 mTouchXBorders[0] = 0;
1249 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001250 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001251 mTouchXBorders[3] = centerX + (width / 4);
1252 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001253 }
1254
Joe Onorato664457d2009-10-28 16:30:34 -04001255 void fling() {
1256 mInvokeFling.execute();
1257 }
1258
1259 void move() {
1260 mInvokeMove.execute();
1261 }
1262
1263 void moveTo(float row) {
1264 mState.targetPos = row;
1265 mState.save();
1266 mInvokeMoveTo.execute();
1267 }
1268
Jason Sams37e7c2b2009-10-19 12:55:43 -07001269 int chooseTappedIcon(int x, int y, float pos) {
1270 // Adjust for scroll position if not zero.
1271 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001272
Joe Onorato6665c0f2009-09-02 15:27:24 -07001273 int col = -1;
1274 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001275 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1276 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1277 col = i;
1278 break;
1279 }
1280 }
1281 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1282 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1283 row = i;
1284 break;
1285 }
1286 }
1287
1288 if (row < 0 || col < 0) {
1289 return -1;
1290 }
1291
Joe Onorato664457d2009-10-28 16:30:34 -04001292 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001293 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001294
Joe Onorato664457d2009-10-28 16:30:34 -04001295 if (index >= mState.iconCount) {
1296 return -1;
1297 } else {
1298 return index;
1299 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001300 }
1301
Joe Onorato6665c0f2009-09-02 15:27:24 -07001302 /**
1303 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001304 *
1305 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001306 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001307 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001308 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001309 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001310 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001311 }
1312
Joe Onoratoc61cff92009-11-08 11:54:39 -05001313 /**
1314 * Select the icon at the given index.
1315 *
1316 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001317 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001318 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001319 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001320 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001321 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001322 if (mLastSelection == SELECTION_ICONS) {
1323 mLastSelection = SELECTION_NONE;
1324 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001325 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001326 if (pressed == SELECTED_FOCUSED) {
1327 mLastSelection = SELECTION_ICONS;
1328 }
1329
Joe Onorato52a653f2009-11-11 14:52:11 -08001330 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001331 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001332
Joe Onorato52a653f2009-11-11 14:52:11 -08001333 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001334 Bitmap selectionBitmap = mSelectionBitmap;
1335
1336 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001337 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001338 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001339
1340 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001341 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001342 mSelectedIcon.uploadToTexture(0);
1343 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001344
1345 if (prev != index) {
1346 if (info.title != null && info.title.length() > 0) {
1347 Log.d(TAG, "sendAccessibilityEvent SELECTION_ICONS " + info.title);
1348 //setContentDescription(info.title);
1349 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1350 }
1351 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001352 }
1353 }
1354
1355 /**
1356 * You need to call save() on mState on your own after calling this.
1357 */
1358 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001359 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001360 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001361
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001362 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001363 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001364 switch (mode) {
1365 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001366 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001367 break;
1368 case SELECTED_FOCUSED:
1369 mLastSelection = SELECTION_HOME;
1370 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001371 if (prev != SELECTION_HOME) {
1372 Log.d(TAG, "sendAccessibilityEvent SELECTION_HOME");
1373 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1374 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001375 break;
1376 case SELECTED_PRESSED:
1377 mState.homeButtonId = mHomeButtonPressed.getID();
1378 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001379 }
1380 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001381 }
Joe Onorato93839052009-08-06 20:34:32 -07001382}
1383
1384