blob: 8ee995cfe73c59c2f4eeea50ba805a6d850927e3 [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 Onorato68ba5ca2009-11-12 14:23:43 -0800107 private VelocityTracker mVelocityTracker;
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;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800117 private boolean mAnimateNextZoom;
118 private float mZoom;
119 private float mPosX;
120 private float mVelocity;
121 private AAMessage mMessageProc;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700122
Joe Onorato6665c0f2009-09-02 15:27:24 -0700123 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700124 public static final int ALLOC_PARAMS = 0;
125 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700126 public static final int ALLOC_ICON_IDS = 3;
127 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700128
129 public static final int COLUMNS_PER_PAGE = 4;
130 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700131
Joe Onorato6665c0f2009-09-02 15:27:24 -0700132 public static final int ICON_WIDTH_PX = 64;
133 public static final int ICON_TEXTURE_WIDTH_PX = 128;
134
135 public static final int ICON_HEIGHT_PX = 64;
136 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700137
138 public int SCREEN_WIDTH_PX;
139 public int SCREEN_HEIGHT_PX;
140
Joe Onoratobcbeab82009-10-01 21:45:43 -0700141 public void recompute(int w, int h) {
142 SCREEN_WIDTH_PX = 480;
143 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700144 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700145 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700146
147 public AllAppsView(Context context, AttributeSet attrs) {
148 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700149 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400150 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700151 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700152 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700153 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700154 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
155
Joe Onorato6665c0f2009-09-02 15:27:24 -0700156 setOnClickListener(this);
157 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700158 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700159 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700160 }
161
Joe Onoratob39e51a2009-10-28 15:47:49 -0400162 /**
163 * If you have an attached click listener, View always plays the click sound!?!?
164 * Deal with sound effects by hand.
165 */
166 public void reallyPlaySoundEffect(int sound) {
167 boolean old = isSoundEffectsEnabled();
168 setSoundEffectsEnabled(true);
169 playSoundEffect(sound);
170 setSoundEffectsEnabled(old);
171 }
172
Joe Onorato7c312c12009-08-13 21:36:53 -0700173 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
174 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700175 }
176
Joe Onorato6665c0f2009-09-02 15:27:24 -0700177 public void setLauncher(Launcher launcher) {
178 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700179 }
180
Joe Onorato1feb3a82009-08-08 22:32:00 -0700181 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700182 public void surfaceDestroyed(SurfaceHolder holder) {
183 super.surfaceDestroyed(holder);
Jason Sams20df7c72009-11-05 12:30:24 -0800184 mRollo.mHasSurface = false;
Joe Onoratofab74402009-11-11 16:05:23 -0800185 // Without this, we leak mMessageCallback which leaks the context.
186 mRS.mMessageCallback = null;
Joe Onorato7bb17492009-09-24 17:51:01 -0700187 }
188
189 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700190 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800191 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700192
Joe Onorato080d9b62009-11-02 12:01:11 -0500193 super.surfaceChanged(holder, format, w, h);
194
Jason Sams90396672009-11-03 13:59:34 -0800195 if (mRS == null) {
Jason Sams90396672009-11-03 13:59:34 -0800196 mRS = createRenderScript(true);
197 mRollo = new RolloRS();
Jason Sams20df7c72009-11-05 12:30:24 -0800198 mRollo.mHasSurface = true;
Jason Sams90396672009-11-03 13:59:34 -0800199 mRollo.init(getResources(), w, h);
200 if (mAllAppsList != null) {
201 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800202 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800203 if (mShouldGainFocus) {
204 gainFocus();
205 mShouldGainFocus = false;
206 }
Joe Onorato3a8820b2009-11-10 15:06:42 -0800207 mRollo.dirtyCheck();
Jason Sams20df7c72009-11-05 12:30:24 -0800208 } else {
209 mRollo.mHasSurface = true;
210 mRollo.dirtyCheck();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700212
Joe Onoratocb75f362009-11-12 13:04:07 -0800213 mRS.mMessageCallback = mMessageProc = new AAMessage();
214
Joe Onoratoc567acb2009-08-31 14:34:43 -0700215 Resources res = getContext().getResources();
216 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700217
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800218 //long endTime = SystemClock.uptimeMillis();
219 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700220 }
Jason Sams2e19c052009-10-20 18:19:55 -0700221
Joe Onorato93839052009-08-06 20:34:32 -0700222 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800223 public void onWindowFocusChanged(boolean hasWindowFocus) {
224 super.onWindowFocusChanged(hasWindowFocus);
225 if (mArrowNavigation) {
226 if (!hasWindowFocus) {
227 // Clear selection when we lose window focus
228 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500229 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800230 mRollo.clearSelectedIcon();
231 mRollo.mState.save();
232 } else if (hasWindowFocus) {
233 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500234 if (mLastSelection == SELECTION_ICONS) {
235 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800236 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500237 Defines.COLUMNS_PER_PAGE;
238 if (selection < 0 || // No selection
239 selection < firstIcon || // off the top of the screen
240 selection >= mRollo.mState.iconCount || // past last icon
241 selection >= firstIcon + // past last icon on screen
242 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
243 selection = firstIcon;
244 }
245
246 // Select the first icon when we gain window focus
247 mRollo.selectIcon(selection, SELECTED_FOCUSED);
248 mRollo.mState.save();
249 } else if (mLastSelection == SELECTION_HOME) {
250 mRollo.setHomeSelected(SELECTED_FOCUSED);
251 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800252 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800253 }
254 }
255 }
256 }
257
258 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700259 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
260 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400261
262 if (!isVisible()) {
263 return;
264 }
265
Mike Cleron7d5d7462009-10-20 14:06:00 -0700266 if (gainFocus) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800267 if (mRollo != null) {
268 gainFocus();
269 } else {
270 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700271 }
272 } else {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800273 if (mRollo != null) {
274 if (mArrowNavigation) {
275 // Clear selection when we lose focus
276 mRollo.clearSelectedIcon();
277 mRollo.setHomeSelected(SELECTED_NONE);
278 mRollo.mState.save();
279 mArrowNavigation = false;
280 }
281 } else {
282 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700283 }
284 }
285 }
286
Mike Cleronb64b67a2009-11-08 14:56:25 -0800287 private void gainFocus() {
288 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
289 // Select the first icon when we gain keyboard focus
290 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800291 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800292 SELECTED_FOCUSED);
293 mRollo.mState.save();
294 }
295 }
296
Mike Cleron7d5d7462009-10-20 14:06:00 -0700297 @Override
298 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700299
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800300 boolean handled = false;
301
Joe Onorato859b3a72009-10-28 15:17:01 -0400302 if (!isVisible()) {
303 return false;
304 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500305 final int iconCount = mRollo.mState.iconCount;
306
Mike Cleron7d5d7462009-10-20 14:06:00 -0700307 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
308 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500309 if (mLastSelection == SELECTION_HOME) {
310 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
311 mLauncher.closeAllApps(true);
312 } else {
313 int whichApp = mRollo.mState.selectedIconIndex;
314 if (whichApp >= 0) {
315 ApplicationInfo app = mAllAppsList.get(whichApp);
316 mLauncher.startActivitySafely(app.intent);
317 handled = true;
318 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700319 }
320 }
321 }
Jason Sams2e19c052009-10-20 18:19:55 -0700322
Joe Onoratoa13f5742009-11-02 17:15:19 -0500323 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700324 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700325
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800326 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800327 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700328
Mike Cleron7d5d7462009-10-20 14:06:00 -0700329 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500330 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700331
Mike Cleron7d5d7462009-10-20 14:06:00 -0700332 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500333 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700334 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700335
Mike Cleron7d5d7462009-10-20 14:06:00 -0700336 int newSelection = currentSelection;
337
338 switch (keyCode) {
339 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500340 if (mLastSelection == SELECTION_HOME) {
341 mRollo.setHomeSelected(SELECTED_NONE);
342 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
343 if (lastRowCount == 0) {
344 lastRowCount = Defines.COLUMNS_PER_PAGE;
345 }
346 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
347 if (newSelection >= iconCount) {
348 newSelection = iconCount-1;
349 }
350 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
351 - (Defines.ROWS_PER_PAGE - 1);
352 if (target < 0) {
353 target = 0;
354 }
355 if (currentTopRow != target) {
356 mRollo.moveTo(target);
357 }
358 } else {
359 if (currentPageRow > 0) {
360 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
361 } else if (currentTopRow > 0) {
362 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
363 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
364 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700365 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800366 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700367 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800368
Joe Onoratoa13f5742009-11-02 17:15:19 -0500369 case KeyEvent.KEYCODE_DPAD_DOWN: {
370 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
371 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
372 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500373 if (mLastSelection != SELECTION_HOME) {
374 if (currentRow < rowCount-1) {
375 mRollo.setHomeSelected(SELECTED_NONE);
376 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
377 if (newSelection >= iconCount) {
378 // Go from D to G in this arrangement:
379 // A B C D
380 // E F G
381 newSelection = iconCount - 1;
382 }
383 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
384 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
385 Defines.ROWS_PER_PAGE + 1);
386 }
387 } else {
388 newSelection = -1;
389 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700390 }
391 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800392 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700393 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500394 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700395 case KeyEvent.KEYCODE_DPAD_LEFT:
396 if (currentPageCol > 0) {
397 newSelection = currentSelection - 1;
398 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800399 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700400 break;
401 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700402 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500403 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700404 newSelection = currentSelection + 1;
405 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800406 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700407 break;
408 }
409 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500410 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700411 mRollo.mState.save();
412 }
413 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800414 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700415 }
416
Joe Onorato93839052009-08-06 20:34:32 -0700417 @Override
418 public boolean onTouchEvent(MotionEvent ev)
419 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700420 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700421
Joe Onorato7bb17492009-09-24 17:51:01 -0700422 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700423 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700424 }
425
Joe Onoratofb0ca672009-09-14 17:55:46 -0400426 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700427 return true;
428 }
429
430 super.onTouchEvent(ev);
431
Joe Onoratofb0ca672009-09-14 17:55:46 -0400432 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700433 int y = (int)ev.getY();
434
Joe Onoratobcbeab82009-10-01 21:45:43 -0700435 int action = ev.getAction();
436 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400437 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700438 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
439 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500440 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700441 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700442 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700443 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700444 mTouchTracking = TRACKING_FLING;
445
446 mMotionDownRawX = (int)ev.getRawX();
447 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700448
Mike Cleron7d5d7462009-10-20 14:06:00 -0700449 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700450 mRollo.mState.newTouchDown = 1;
451
452 if (!mRollo.checkClickOK()) {
453 mRollo.clearSelectedIcon();
454 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700455 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800456 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700457 if (mDownIconIndex < 0) {
458 // if nothing was selected, no long press.
459 cancelLongPress();
460 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700461 }
462 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700463 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800464 mVelocityTracker = VelocityTracker.obtain();
465 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700466 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700467 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400468 break;
469 case MotionEvent.ACTION_MOVE:
470 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700471 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500472 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
473 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700474 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700475 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700476 int rawX = (int)ev.getRawX();
477 int rawY = (int)ev.getRawY();
478 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700479 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700480
Joe Onorato82ca5502009-10-15 16:59:23 -0700481 if (!mStartedScrolling && slop < mSlop) {
482 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700483 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800484 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700485 if (mDownIconIndex != mCurrentIconIndex) {
486 // If a different icon is selected, don't allow it to be picked up.
487 // This handles off-axis dragging.
488 cancelLongPress();
489 mCurrentIconIndex = -1;
490 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700491 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700492 if (!mStartedScrolling) {
493 cancelLongPress();
494 mCurrentIconIndex = -1;
495 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700496 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700497 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700498 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700499
Joe Onoratobcbeab82009-10-01 21:45:43 -0700500 mStartedScrolling = true;
501 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800502 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700503 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700504 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400505 }
506 break;
507 case MotionEvent.ACTION_UP:
508 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700509 if (mTouchTracking == TRACKING_HOME) {
510 if (action == MotionEvent.ACTION_UP) {
511 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400512 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700513 mLauncher.closeAllApps(true);
514 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500515 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700516 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700517 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700518 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700519 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700520 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700521 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700522
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800523 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
524 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700525 mRollo.clearSelectedIcon();
526 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700527 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700528
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800529 if (mVelocityTracker != null) {
530 mVelocityTracker.recycle();
531 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700532 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700533 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700534 mTouchTracking = TRACKING_NONE;
535 break;
Joe Onorato93839052009-08-06 20:34:32 -0700536 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700537
538 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700539 }
540
Joe Onorato6665c0f2009-09-02 15:27:24 -0700541 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700542 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400543 return;
544 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700545 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
546 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400547 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700548 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700549 mLauncher.startActivitySafely(app.intent);
550 }
551 }
552
553 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700554 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400555 return true;
556 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700557 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
558 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
559 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700560
561 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700562 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
563 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700564
Joe Onoratobcbeab82009-10-01 21:45:43 -0700565 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
566 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700567 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700568 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700569 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700570
Joe Onorato7bb17492009-09-24 17:51:01 -0700571 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700572 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700573 return true;
574 }
575
Joe Onorato52a653f2009-11-11 14:52:11 -0800576 @Override
577 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
578 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
579 if (!isVisible()) {
580 return false;
581 }
582 String text = null;
583 int index;
584 int count = mAllAppsList.size() + 1; // +1 is home
585 int pos = -1;
586 switch (mLastSelection) {
587 case SELECTION_ICONS:
588 index = mRollo.mState.selectedIconIndex;
589 if (index >= 0) {
590 ApplicationInfo info = mAllAppsList.get(index);
591 if (info.title != null) {
592 text = info.title.toString();
593 pos = index;
594 }
595 }
596 break;
597 case SELECTION_HOME:
598 text = getContext().getString(R.string.all_apps_home_button_label);
599 pos = count;
600 break;
601 }
602 if (text != null) {
603 Log.d(TAG, "dispatchPopulateAccessibilityEvent setting text=" + text);
604 event.setEnabled(true);
605 event.getText().add(text);
606 //event.setContentDescription(text);
607 event.setItemCount(count);
608 event.setCurrentItemIndex(pos);
609 }
610 }
611 return false;
612 }
613
Joe Onorato5162ea92009-09-03 09:39:42 -0700614 public void setDragController(DragController dragger) {
615 mDragController = dragger;
616 }
617
618 public void onDropCompleted(View target, boolean success) {
619 }
620
Joe Onorato4db52312009-10-06 11:17:43 -0700621 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800622 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700623 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800624 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700625 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800626 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400627 cancelLongPress();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800628 if (mRollo == null || !mRollo.mHasSurface) {
629 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800630 mZoom = zoom;
631 mAnimateNextZoom = animate;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800632 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700633 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800634 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700635 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400636 }
637
638 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800639 Log.d(TAG, "isVisible mZoomDirty=" + mZoomDirty
640 + " mMessageProc=" + (mMessageProc == null ? "null" : mZoom));
641 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800642 }
643
644 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800645 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400646 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700647
Joe Onorato9c1289c2009-08-17 11:03:03 -0400648 public void setApps(ArrayList<ApplicationInfo> list) {
649 mAllAppsList = list;
650 if (mRollo != null) {
651 mRollo.setApps(list);
652 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400653 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700654 }
655
Joe Onoratoa8138d52009-10-06 19:25:30 -0700656 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500657 if (mAllAppsList == null) {
658 // Not done loading yet. We'll find out about it later.
659 return;
660 }
661
Joe Onoratoa8138d52009-10-06 19:25:30 -0700662 final int N = list.size();
663 if (mRollo != null) {
664 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
665 }
666
667 for (int i=0; i<N; i++) {
668 final ApplicationInfo item = list.get(i);
669 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
670 if (index < 0) {
671 index = -(index+1);
672 }
673 mAllAppsList.add(index, item);
674 if (mRollo != null) {
675 mRollo.addApp(index, item);
676 mRollo.mState.iconCount++;
677 }
678 }
679
680 if (mRollo != null) {
681 mRollo.saveAppsList();
682 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700683 }
684
Joe Onoratoa8138d52009-10-06 19:25:30 -0700685 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500686 if (mAllAppsList == null) {
687 // Not done loading yet. We'll find out about it later.
688 return;
689 }
690
Joe Onoratoa8138d52009-10-06 19:25:30 -0700691 final int N = list.size();
692 for (int i=0; i<N; i++) {
693 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400694 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700695 if (index >= 0) {
696 mAllAppsList.remove(index);
697 if (mRollo != null) {
698 mRollo.removeApp(index);
699 mRollo.mState.iconCount--;
700 }
701 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800702 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700703 // Try to recover. This should keep us from crashing for now.
704 }
705 }
706
707 if (mRollo != null) {
708 mRollo.saveAppsList();
709 }
710 }
711
712 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
713 // Just remove and add, because they may need to be re-sorted.
714 removeApps(list);
715 addApps(list);
716 }
717
718 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
719 public int compare(ApplicationInfo a, ApplicationInfo b) {
720 int result = a.title.toString().compareTo(b.toString());
721 if (result != 0) {
722 return result;
723 }
724 return a.intent.getComponent().compareTo(b.intent.getComponent());
725 }
726 };
727
Joe Onoratocb9f7982009-10-31 16:32:02 -0400728 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
729 ComponentName component = item.intent.getComponent();
730 final int N = list.size();
731 for (int i=0; i<N; i++) {
732 ApplicationInfo x = list.get(i);
733 if (x.intent.getComponent().equals(component)) {
734 return i;
735 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700736 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400737 return -1;
738 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700739
Joe Onoratoc567acb2009-08-31 14:34:43 -0700740 private static int countPages(int iconCount) {
741 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
742 int pages = iconCount / iconsPerPage;
743 if (pages*iconsPerPage != iconCount) {
744 pages++;
745 }
746 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 }
748
Joe Onoratocb75f362009-11-12 13:04:07 -0800749 class AAMessage extends RenderScript.RSMessage {
750 public void run() {
751 mPosX = ((float)mData[0]) / (1 << 16);
752 mVelocity = ((float)mData[1]) / (1 << 16);
753 mZoom = ((float)mData[2]) / (1 << 16);
754 mZoomDirty = false;
755 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800756 }
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 Sams476339d2009-09-29 18:14:38 -0700816 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800817 return (Math.abs(mVelocity) < 0.4f) &&
818 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700819 }
820
Jason Sams78aebd82009-09-15 13:06:59 -0700821 class Params extends BaseAlloc {
822 Params() {
823 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
824 mAlloc = Allocation.createTyped(mRS, mType);
825 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700826 }
Jason Sams78aebd82009-09-15 13:06:59 -0700827 public int bubbleWidth;
828 public int bubbleHeight;
829 public int bubbleBitmapWidth;
830 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700831
Joe Onoratobcbeab82009-10-01 21:45:43 -0700832 public int homeButtonWidth;
833 public int homeButtonHeight;
834 public int homeButtonTextureWidth;
835 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700836 }
837
838 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700839 public float newPositionX;
840 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700841 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700842 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700843 public int selectedIconIndex = -1;
844 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700845 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700846 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700847 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700848
849 State() {
850 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
851 mAlloc = Allocation.createTyped(mRS, mType);
852 save();
853 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700854 }
855
856 public RolloRS() {
857 }
858
859 public void init(Resources res, int width, int height) {
860 mRes = res;
861 mWidth = width;
862 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700863 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700864 initProgramVertex();
865 initProgramFragment();
866 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700867 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700868 initGl();
869 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700870 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700871 initRs();
872 }
873
Jason Sams0aa71662009-10-02 18:43:18 -0700874 public void initMesh() {
875 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
876 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
877
Jason Samsd8152b92009-10-13 17:19:10 -0700878 float y = 0;
879 float z = 0;
880 for (int ct=0; ct < 200; ct++) {
881 float angle = 0;
882 float maxAngle = 3.14f * 0.16f;
883 float l = 1.f;
884
885 l = 1 - ((ct-5) * 0.10f);
886 if (ct > 7) {
887 angle = maxAngle * (ct - 7) * 0.2f;
888 angle = Math.min(angle, maxAngle);
889 }
Jason Samsc8514792009-10-29 14:27:29 -0700890 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700891 l = Math.min(1.0f, l);
892
893 y += 0.1f * Math.cos(angle);
894 z += 0.1f * Math.sin(angle);
895
896 float t = 0.1f * ct;
897 float ds = 0.08f;
898 tm.setColor(l, l, l, 0.99f);
899 tm.setTexture(ds, t);
900 tm.addVertex(-0.5f, y, z);
901 tm.setTexture(1 - ds, t);
902 tm.addVertex(0.5f, y, z);
903 }
904 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
905 tm.addTriangle(ct, ct+1, ct+2);
906 tm.addTriangle(ct+1, ct+3, ct+2);
907 }
908 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700909 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700910 }
911
Jason Samscd689e12009-09-29 15:28:22 -0700912 private void initProgramVertex() {
913 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
914 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700915
916 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700917 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700918 mPV = pvb.create();
919 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700920 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700921
Jason Sams37e7c2b2009-10-19 12:55:43 -0700922 //pva = new ProgramVertex.MatrixAllocation(mRS);
923 //pva.setupOrthoWindow(mWidth, mHeight);
924 //pvb.setTextureMatrixEnable(true);
925 //mPVOrtho = pvb.create();
926 //mPVOrtho.setName("PVOrtho");
927 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700928
929 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700930 }
Joe Onorato93839052009-08-06 20:34:32 -0700931
Jason Samscd689e12009-09-29 15:28:22 -0700932 private void initProgramFragment() {
933 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700934 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700935 sb.setMag(Sampler.Value.LINEAR);
936 sb.setWrapS(Sampler.Value.CLAMP);
937 sb.setWrapT(Sampler.Value.CLAMP);
938 Sampler linear = sb.create();
939
940 sb.setMin(Sampler.Value.NEAREST);
941 sb.setMag(Sampler.Value.NEAREST);
942 Sampler nearest = sb.create();
943
944 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700945 //mPFColor = bf.create();
946 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700947
948 bf.setTexEnable(true, 0);
949 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700950 mPFTexMip = bf.create();
951 mPFTexMip.setName("PFTexMip");
952 mPFTexMip.bindSampler(linear, 0);
953
954 mPFTexNearest = bf.create();
955 mPFTexNearest.setName("PFTexNearest");
956 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700957 }
958
959 private void initProgramStore() {
960 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
961 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700962 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700963 bs.setDitherEnable(true);
964 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
965 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
966 mPSIcons = bs.create();
967 mPSIcons.setName("PSIcons");
968
969 //bs.setDitherEnable(false);
970 //mPSText = bs.create();
971 //mPSText.setName("PSText");
972 }
973
974 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700975 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700976 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700977 }
Jason Sams78aebd82009-09-15 13:06:59 -0700978
Joe Onorato1feb3a82009-08-08 22:32:00 -0700979 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700980 mParams = new Params();
981 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700982
Joe Onoratobf15cb42009-08-07 14:33:40 -0700983 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700984
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700985 mParams.bubbleWidth = bubble.getBubbleWidth();
986 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
987 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
988 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700989
Joe Onoratod63458b2009-10-15 21:19:09 -0700990 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
991 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
992 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500993 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
994 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
995 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -0700996 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
997 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
998 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700999 mParams.homeButtonWidth = 76;
1000 mParams.homeButtonHeight = 68;
1001 mParams.homeButtonTextureWidth = 128;
1002 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001003
Joe Onoratod63458b2009-10-15 21:19:09 -07001004 mState.homeButtonId = mHomeButtonNormal.getID();
1005
Joe Onorato1feb3a82009-08-08 22:32:00 -07001006 mParams.save();
1007 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001008
Joe Onorato1291a8c2009-09-15 15:07:25 -04001009 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1010 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1011 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001012
Joe Onorato9c1289c2009-08-17 11:03:03 -04001013 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001014 }
1015
Jason Sams37e7c2b2009-10-19 12:55:43 -07001016 private void initScript(int id) {
1017 }
1018
1019 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001020 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001021 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -07001022 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001023 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001024 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1025 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001026 mInvokeMove = sb.addInvokable("move");
1027 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001028 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001029 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001030 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001031 mScript = sb.create();
1032 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1033 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1034 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1035 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1036 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001037
Jason Sams37e7c2b2009-10-19 12:55:43 -07001038 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001039 }
Joe Onorato93839052009-08-06 20:34:32 -07001040
Jason Sams20df7c72009-11-05 12:30:24 -08001041 private void uploadApps(ArrayList<ApplicationInfo> list) {
1042 for (int i=0; i < mState.iconCount; i++) {
1043 uploadAppIcon(i, list.get(i));
1044 }
1045 }
1046
1047 void dirtyCheck() {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001048 if (mHasSurface) {
1049 if (mAppsDirty) {
1050 uploadApps(mAllAppsList);
1051 saveAppsList();
1052 mAppsDirty = false;
1053 }
1054 if (mZoomDirty) {
Joe Onorato68ba5ca2009-11-12 14:23:43 -08001055 setZoom(mZoom, mAnimateNextZoom);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001056 }
Jason Sams20df7c72009-11-05 12:30:24 -08001057 }
1058 }
1059
Joe Onorato9c1289c2009-08-17 11:03:03 -04001060 private void setApps(ArrayList<ApplicationInfo> list) {
1061 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001062 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001063 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001064 allocCount = 1;
1065 }
1066
Joe Onorato9c1289c2009-08-17 11:03:03 -04001067 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001068 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001069 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001070
1071 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001072 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001073 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001075 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001076
1077 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1078
Joe Onorato9c1289c2009-08-17 11:03:03 -04001079 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001080 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001081 saveAppsList();
1082 }
1083
Joe Onorato3a8820b2009-11-10 15:06:42 -08001084 private void setZoom(float zoom, boolean animate) {
1085 mRollo.clearSelectedIcon();
1086 mRollo.setHomeSelected(SELECTED_NONE);
1087 if (zoom > 0.001f) {
1088 mRollo.mState.zoomTarget = zoom;
1089 } else {
1090 mRollo.mState.zoomTarget = 0;
1091 }
1092 mRollo.mState.save();
1093 if (!animate) {
1094 mRollo.mInvokeSetZoom.execute();
1095 }
1096 }
1097
Jason Samsc8514792009-10-29 14:27:29 -07001098 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1099 int black[] = new int[w > h ? w : h];
1100 Allocation.Adapter2D a = alloc.createAdapter2D();
1101 int mip = 0;
1102 while (w > 1 || h > 1) {
1103 a.subData(0, 0, 1, h, black);
1104 a.subData(w-1, 0, 1, h, black);
1105 a.subData(0, 0, w, 1, black);
1106 a.subData(0, h-1, w, 1, black);
1107 mip++;
1108 w = (w + 1) >> 1;
1109 h = (h + 1) >> 1;
1110 a.setConstraint(Dimension.LOD, mip);
1111 }
1112 a.subData(0, 0, 1, 1, black);
1113 }
1114
Joe Onoratoa8138d52009-10-06 19:25:30 -07001115 private void uploadAppIcon(int index, ApplicationInfo item) {
1116 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001117 Element.RGBA_8888(mRS), true);
1118 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1119
Joe Onoratoa8138d52009-10-06 19:25:30 -07001120 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001121 Element.RGBA_8888(mRS), true);
1122 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001123
1124 mIcons[index].uploadToTexture(0);
1125 mLabels[index].uploadToTexture(0);
1126
1127 mIconIds[index] = mIcons[index].getID();
1128 mLabelIds[index] = mLabels[index].getID();
1129 }
1130
1131 /**
1132 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1133 * fill in the values and call saveAppsList().
1134 */
1135 private void reallocAppsList(int count) {
1136 Allocation[] icons = new Allocation[count];
1137 int[] iconIds = new int[count];
1138 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1139
1140 Allocation[] labels = new Allocation[count];
1141 int[] labelIds = new int[count];
1142 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1143
1144 final int oldCount = mIcons.length;
1145
1146 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1147 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1148 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1149 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1150
1151 mIcons = icons;
1152 mIconIds = iconIds;
1153 mLabels = labels;
1154 mLabelIds = labelIds;
1155 }
1156
1157 /**
1158 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1159 */
1160 private void addApp(int index, ApplicationInfo item) {
1161 final int count = mState.iconCount - index;
1162 final int dest = index + 1;
1163
1164 System.arraycopy(mIcons, index, mIcons, dest, count);
1165 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1166 System.arraycopy(mLabels, index, mLabels, dest, count);
1167 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1168
Jason Sams20df7c72009-11-05 12:30:24 -08001169 if (mHasSurface ) {
1170 uploadAppIcon(index, item);
1171 } else {
1172 mAppsDirty = true;
1173 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001174 }
1175
1176 /**
1177 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1178 */
1179 private void removeApp(int index) {
1180 final int count = mState.iconCount - index - 1;
1181 final int src = index + 1;
1182
1183 System.arraycopy(mIcons, src, mIcons, index, count);
1184 System.arraycopy(mIconIds, src, mIconIds, index, count);
1185 System.arraycopy(mLabels, src, mLabels, index, count);
1186 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1187
1188 final int last = mState.iconCount - 1;
1189 mIcons[last] = null;
1190 mIconIds[last] = 0;
1191 mLabels[last] = null;
1192 mLabelIds[last] = 0;
1193 }
1194
1195 /**
1196 * Send the apps list structures to RS.
1197 */
1198 private void saveAppsList() {
1199 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001200
Joe Onoratoa8138d52009-10-06 19:25:30 -07001201 mAllocIconIds.data(mIconIds);
1202 mAllocLabelIds.data(mLabelIds);
1203
Jason Sams37e7c2b2009-10-19 12:55:43 -07001204 if (mScript != null) { // this happens when we init it
1205 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1206 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 }
1208
1209 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001210
1211 // Note: mScript may be null if we haven't initialized it yet.
1212 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001213 if (mInvokeResetWAR != null) {
1214 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001215 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001216 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001218
1219 void initTouchState() {
1220 int width = getWidth();
1221 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001222 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1223 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001224
Jason Sams37e7c2b2009-10-19 12:55:43 -07001225 int centerY = (height / 2);
1226 mTouchYBorders[0] = centerY - (cellHeight * 2);
1227 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001228 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001229 mTouchYBorders[3] = centerY + cellHeight;
1230 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001231
Joe Onorato6665c0f2009-09-02 15:27:24 -07001232 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001233 mTouchXBorders[0] = 0;
1234 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001235 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001236 mTouchXBorders[3] = centerX + (width / 4);
1237 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001238 }
1239
Joe Onorato664457d2009-10-28 16:30:34 -04001240 void fling() {
1241 mInvokeFling.execute();
1242 }
1243
1244 void move() {
1245 mInvokeMove.execute();
1246 }
1247
1248 void moveTo(float row) {
1249 mState.targetPos = row;
1250 mState.save();
1251 mInvokeMoveTo.execute();
1252 }
1253
Jason Sams37e7c2b2009-10-19 12:55:43 -07001254 int chooseTappedIcon(int x, int y, float pos) {
1255 // Adjust for scroll position if not zero.
1256 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001257
Joe Onorato6665c0f2009-09-02 15:27:24 -07001258 int col = -1;
1259 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001260 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1261 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1262 col = i;
1263 break;
1264 }
1265 }
1266 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1267 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1268 row = i;
1269 break;
1270 }
1271 }
1272
1273 if (row < 0 || col < 0) {
1274 return -1;
1275 }
1276
Joe Onorato664457d2009-10-28 16:30:34 -04001277 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001278 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001279
Joe Onorato664457d2009-10-28 16:30:34 -04001280 if (index >= mState.iconCount) {
1281 return -1;
1282 } else {
1283 return index;
1284 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001285 }
1286
Joe Onorato6665c0f2009-09-02 15:27:24 -07001287 /**
1288 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001289 *
1290 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001291 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001292 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001293 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001294 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001295 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001296 }
1297
Joe Onoratoc61cff92009-11-08 11:54:39 -05001298 /**
1299 * Select the icon at the given index.
1300 *
1301 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001302 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001303 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001304 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001305 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001306 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001307 if (mLastSelection == SELECTION_ICONS) {
1308 mLastSelection = SELECTION_NONE;
1309 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001310 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001311 if (pressed == SELECTED_FOCUSED) {
1312 mLastSelection = SELECTION_ICONS;
1313 }
1314
Joe Onorato52a653f2009-11-11 14:52:11 -08001315 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001316 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001317
Joe Onorato52a653f2009-11-11 14:52:11 -08001318 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001319 Bitmap selectionBitmap = mSelectionBitmap;
1320
1321 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001322 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001323 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001324
1325 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001326 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001327 mSelectedIcon.uploadToTexture(0);
1328 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001329
1330 if (prev != index) {
1331 if (info.title != null && info.title.length() > 0) {
1332 Log.d(TAG, "sendAccessibilityEvent SELECTION_ICONS " + info.title);
1333 //setContentDescription(info.title);
1334 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1335 }
1336 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001337 }
1338 }
1339
1340 /**
1341 * You need to call save() on mState on your own after calling this.
1342 */
1343 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001344 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001345 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001346
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001347 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001348 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001349 switch (mode) {
1350 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001351 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001352 break;
1353 case SELECTED_FOCUSED:
1354 mLastSelection = SELECTION_HOME;
1355 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001356 if (prev != SELECTION_HOME) {
1357 Log.d(TAG, "sendAccessibilityEvent SELECTION_HOME");
1358 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1359 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001360 break;
1361 case SELECTED_PRESSED:
1362 mState.homeButtonId = mHomeButtonPressed.getID();
1363 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001364 }
1365 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001366 }
Joe Onorato93839052009-08-06 20:34:32 -07001367}
1368
1369