blob: 1d21da8926114d7cec914dd17290c4c575d48f70 [file] [log] [blame]
Joe Onorato93839052009-08-06 20:34:32 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Joe Onoratocb9f7982009-10-31 16:32:02 -040019import android.content.ComponentName;
Joe Onorato93839052009-08-06 20:34:32 -070020import android.content.Context;
21import android.content.res.Resources;
22import android.graphics.Bitmap;
Joe Onorato93839052009-08-06 20:34:32 -070023import android.graphics.Canvas;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080024import android.graphics.PixelFormat;
Mike Cleron7d5d7462009-10-20 14:06:00 -070025import android.graphics.Rect;
Joe Onoratod769a632009-08-11 17:09:02 -070026import android.os.SystemClock;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080027import android.renderscript.Allocation;
28import android.renderscript.Dimension;
29import android.renderscript.Element;
30import android.renderscript.ProgramFragment;
31import android.renderscript.ProgramStore;
32import android.renderscript.ProgramVertex;
33import android.renderscript.RSSurfaceView;
34import android.renderscript.RenderScript;
35import android.renderscript.Sampler;
36import android.renderscript.Script;
37import android.renderscript.ScriptC;
38import android.renderscript.SimpleMesh;
39import android.renderscript.Type;
Joe Onorato93839052009-08-06 20:34:32 -070040import android.util.AttributeSet;
41import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070042import android.view.KeyEvent;
43import android.view.MotionEvent;
Joe Onoratob39e51a2009-10-28 15:47:49 -040044import android.view.SoundEffectConstants;
Joe Onorato93839052009-08-06 20:34:32 -070045import android.view.SurfaceHolder;
Joe Onoratod769a632009-08-11 17:09:02 -070046import android.view.VelocityTracker;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080047import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070048import android.view.ViewConfiguration;
Joe Onorato52a653f2009-11-11 14:52:11 -080049import android.view.accessibility.AccessibilityEvent;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080050
51import java.util.ArrayList;
Joe Onoratobe386092009-11-17 17:32:16 -080052import java.util.Arrays;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080053import java.util.Collections;
54import java.util.Comparator;
Joe Onorato93839052009-08-06 20:34:32 -070055
56
Joe Onorato6665c0f2009-09-02 15:27:24 -070057public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070058 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040059 private static final String TAG = "Launcher.AllAppsView";
60
Joe Onoratofb0ca672009-09-14 17:55:46 -040061 /** Bit for mLocks for when there are icons being loaded. */
62 private static final int LOCK_ICONS_PENDING = 1;
63
Joe Onorato68ffd102009-10-15 17:59:43 -070064 private static final int TRACKING_NONE = 0;
65 private static final int TRACKING_FLING = 1;
66 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070067
Joe Onoratoeb8325a2009-11-08 13:20:30 -050068 private static final int SELECTED_NONE = 0;
69 private static final int SELECTED_FOCUSED = 1;
70 private static final int SELECTED_PRESSED = 2;
71
72 private static final int SELECTION_NONE = 0;
73 private static final int SELECTION_ICONS = 1;
74 private static final int SELECTION_HOME = 2;
75
Joe Onorato6665c0f2009-09-02 15:27:24 -070076 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070077 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040078
79 /** When this is 0, modifications are allowed, when it's not, they're not.
80 * TODO: What about scrolling? */
81 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070082
Joe Onorato82ca5502009-10-15 16:59:23 -070083 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070084 private int mMaxFlingVelocity;
85
Joe Onoratobcbeab82009-10-01 21:45:43 -070086 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070087 private RenderScript mRS;
88 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070090
Mike Cleron7d5d7462009-10-20 14:06:00 -070091 /**
92 * True when we are using arrow keys or trackball to drive navigation
93 */
94 private boolean mArrowNavigation = false;
Joe Onoratoeb8325a2009-11-08 13:20:30 -050095 private boolean mStartedScrolling;
96
97 /**
98 * Used to keep track of the selection when AllAppsView loses window focus.
99 * One of the SELECTION_ constants.
100 */
101 private int mLastSelection;
Jason Sams2e19c052009-10-20 18:19:55 -0700102
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800103 /**
104 * Used to keep track of the selection when AllAppsView loses window focus
105 */
106 private int mLastSelectedIcon;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500107
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800108 private VelocityTracker mVelocityTracker;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700109 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700110 private int mMotionDownRawX;
111 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700112 private int mDownIconIndex = -1;
113 private int mCurrentIconIndex = -1;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800114
Mike Cleronb64b67a2009-11-08 14:56:25 -0800115 private boolean mShouldGainFocus;
116
Joe Onorato6374c512010-01-06 20:42:25 -0800117 private boolean mHaveSurface = false;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800118 private boolean mZoomDirty = false;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800119 private boolean mAnimateNextZoom;
Joe Onorato6374c512010-01-06 20:42:25 -0800120 private float mNextZoom;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800121 private float mZoom;
122 private float mPosX;
123 private float mVelocity;
124 private AAMessage mMessageProc;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700125
Joe Onorato6665c0f2009-09-02 15:27:24 -0700126 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700127 public static final int ALLOC_PARAMS = 0;
128 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700129 public static final int ALLOC_ICON_IDS = 3;
130 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700131
132 public static final int COLUMNS_PER_PAGE = 4;
133 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700134
Joe Onorato6665c0f2009-09-02 15:27:24 -0700135 public static final int ICON_WIDTH_PX = 64;
136 public static final int ICON_TEXTURE_WIDTH_PX = 128;
137
138 public static final int ICON_HEIGHT_PX = 64;
139 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700140
141 public int SCREEN_WIDTH_PX;
142 public int SCREEN_HEIGHT_PX;
143
Joe Onoratobcbeab82009-10-01 21:45:43 -0700144 public void recompute(int w, int h) {
145 SCREEN_WIDTH_PX = 480;
146 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700147 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700148 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700149
150 public AllAppsView(Context context, AttributeSet attrs) {
151 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700152 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400153 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700154 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700155 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700156 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700157 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
158
Joe Onorato6665c0f2009-09-02 15:27:24 -0700159 setOnClickListener(this);
160 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700161 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700162 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samse26d9fc2009-11-12 14:00:43 -0800163
164 mRS = createRenderScript(true);
165 }
166
167 @Override
168 protected void onDetachedFromWindow() {
169 destroyRenderScript();
Joe Onorato93839052009-08-06 20:34:32 -0700170 }
171
Joe Onoratob39e51a2009-10-28 15:47:49 -0400172 /**
173 * If you have an attached click listener, View always plays the click sound!?!?
174 * Deal with sound effects by hand.
175 */
176 public void reallyPlaySoundEffect(int sound) {
177 boolean old = isSoundEffectsEnabled();
178 setSoundEffectsEnabled(true);
179 playSoundEffect(sound);
180 setSoundEffectsEnabled(old);
181 }
182
Joe Onorato7c312c12009-08-13 21:36:53 -0700183 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
184 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700185 }
186
Joe Onorato6665c0f2009-09-02 15:27:24 -0700187 public void setLauncher(Launcher launcher) {
188 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700189 }
190
Joe Onorato1feb3a82009-08-08 22:32:00 -0700191 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700192 public void surfaceDestroyed(SurfaceHolder holder) {
193 super.surfaceDestroyed(holder);
Joe Onoratofab74402009-11-11 16:05:23 -0800194 // Without this, we leak mMessageCallback which leaks the context.
195 mRS.mMessageCallback = null;
Joe Onorato6374c512010-01-06 20:42:25 -0800196 // We may lose any callbacks that are pending, so make sure that we re-sync that
197 // on the next surfaceChanged.
198 mZoomDirty = true;
199 mHaveSurface = false;
Joe Onorato7bb17492009-09-24 17:51:01 -0700200 }
201
202 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700203 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800204 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700205
Joe Onorato080d9b62009-11-02 12:01:11 -0500206 super.surfaceChanged(holder, format, w, h);
207
Joe Onorato6374c512010-01-06 20:42:25 -0800208 mHaveSurface = true;
209
Jason Samse26d9fc2009-11-12 14:00:43 -0800210 if (mRollo == null) {
Jason Sams90396672009-11-03 13:59:34 -0800211 mRollo = new RolloRS();
212 mRollo.init(getResources(), w, h);
213 if (mAllAppsList != null) {
214 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800215 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800216 if (mShouldGainFocus) {
217 gainFocus();
218 mShouldGainFocus = false;
219 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 }
Jason Samse26d9fc2009-11-12 14:00:43 -0800221 mRollo.dirtyCheck();
Jason Sams5612e432009-11-16 14:18:07 -0800222 mRollo.resize(w, h);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700223
Joe Onoratocb75f362009-11-12 13:04:07 -0800224 mRS.mMessageCallback = mMessageProc = new AAMessage();
225
Joe Onoratoc567acb2009-08-31 14:34:43 -0700226 Resources res = getContext().getResources();
227 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700228
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800229 //long endTime = SystemClock.uptimeMillis();
230 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700231 }
Jason Sams2e19c052009-10-20 18:19:55 -0700232
Joe Onorato93839052009-08-06 20:34:32 -0700233 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800234 public void onWindowFocusChanged(boolean hasWindowFocus) {
235 super.onWindowFocusChanged(hasWindowFocus);
236 if (mArrowNavigation) {
237 if (!hasWindowFocus) {
238 // Clear selection when we lose window focus
239 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500240 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800241 mRollo.clearSelectedIcon();
242 mRollo.mState.save();
243 } else if (hasWindowFocus) {
244 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500245 if (mLastSelection == SELECTION_ICONS) {
246 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800247 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500248 Defines.COLUMNS_PER_PAGE;
249 if (selection < 0 || // No selection
250 selection < firstIcon || // off the top of the screen
251 selection >= mRollo.mState.iconCount || // past last icon
252 selection >= firstIcon + // past last icon on screen
253 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
254 selection = firstIcon;
255 }
256
257 // Select the first icon when we gain window focus
258 mRollo.selectIcon(selection, SELECTED_FOCUSED);
259 mRollo.mState.save();
260 } else if (mLastSelection == SELECTION_HOME) {
261 mRollo.setHomeSelected(SELECTED_FOCUSED);
262 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800263 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800264 }
265 }
266 }
267 }
268
269 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700270 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
271 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400272
273 if (!isVisible()) {
274 return;
275 }
276
Mike Cleron7d5d7462009-10-20 14:06:00 -0700277 if (gainFocus) {
Jason Sams510ee042009-12-15 14:23:37 -0800278 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800279 gainFocus();
280 } else {
281 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700282 }
283 } else {
Joe Onorato8eea3912009-12-09 13:01:06 -0800284 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800285 if (mArrowNavigation) {
286 // Clear selection when we lose focus
287 mRollo.clearSelectedIcon();
288 mRollo.setHomeSelected(SELECTED_NONE);
289 mRollo.mState.save();
290 mArrowNavigation = false;
291 }
292 } else {
293 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700294 }
295 }
296 }
297
Mike Cleronb64b67a2009-11-08 14:56:25 -0800298 private void gainFocus() {
299 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
300 // Select the first icon when we gain keyboard focus
301 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800302 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800303 SELECTED_FOCUSED);
304 mRollo.mState.save();
305 }
306 }
307
Mike Cleron7d5d7462009-10-20 14:06:00 -0700308 @Override
309 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700310
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800311 boolean handled = false;
Jason Samse26d9fc2009-11-12 14:00:43 -0800312
Joe Onorato859b3a72009-10-28 15:17:01 -0400313 if (!isVisible()) {
314 return false;
315 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500316 final int iconCount = mRollo.mState.iconCount;
317
Mike Cleron7d5d7462009-10-20 14:06:00 -0700318 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
319 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500320 if (mLastSelection == SELECTION_HOME) {
321 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
322 mLauncher.closeAllApps(true);
323 } else {
324 int whichApp = mRollo.mState.selectedIconIndex;
325 if (whichApp >= 0) {
326 ApplicationInfo app = mAllAppsList.get(whichApp);
327 mLauncher.startActivitySafely(app.intent);
328 handled = true;
329 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700330 }
331 }
332 }
Jason Sams2e19c052009-10-20 18:19:55 -0700333
Joe Onorato478730f2009-11-16 18:54:43 -0800334 if (iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700335 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700336
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800337 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800338 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700339
Mike Cleron7d5d7462009-10-20 14:06:00 -0700340 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500341 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700342
Mike Cleron7d5d7462009-10-20 14:06:00 -0700343 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500344 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700345 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700346
Mike Cleron7d5d7462009-10-20 14:06:00 -0700347 int newSelection = currentSelection;
348
349 switch (keyCode) {
350 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500351 if (mLastSelection == SELECTION_HOME) {
352 mRollo.setHomeSelected(SELECTED_NONE);
353 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
354 if (lastRowCount == 0) {
355 lastRowCount = Defines.COLUMNS_PER_PAGE;
356 }
357 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
358 if (newSelection >= iconCount) {
359 newSelection = iconCount-1;
360 }
361 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
362 - (Defines.ROWS_PER_PAGE - 1);
363 if (target < 0) {
364 target = 0;
365 }
366 if (currentTopRow != target) {
367 mRollo.moveTo(target);
368 }
369 } else {
370 if (currentPageRow > 0) {
371 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
372 } else if (currentTopRow > 0) {
373 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
374 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
Joe Onoratoaf5b4cb2009-12-08 17:51:22 -0800375 } else if (currentPageRow != 0) {
376 newSelection = currentTopRow * Defines.ROWS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500377 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700378 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800379 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700380 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800381
Joe Onoratoa13f5742009-11-02 17:15:19 -0500382 case KeyEvent.KEYCODE_DPAD_DOWN: {
383 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
384 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
385 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500386 if (mLastSelection != SELECTION_HOME) {
387 if (currentRow < rowCount-1) {
388 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato478730f2009-11-16 18:54:43 -0800389 if (currentSelection < 0) {
390 newSelection = 0;
391 } else {
392 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
393 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500394 if (newSelection >= iconCount) {
395 // Go from D to G in this arrangement:
396 // A B C D
397 // E F G
398 newSelection = iconCount - 1;
399 }
400 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
401 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
402 Defines.ROWS_PER_PAGE + 1);
403 }
404 } else {
405 newSelection = -1;
406 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700407 }
408 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800409 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700410 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500411 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700412 case KeyEvent.KEYCODE_DPAD_LEFT:
413 if (currentPageCol > 0) {
414 newSelection = currentSelection - 1;
415 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800416 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700417 break;
418 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700419 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500420 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700421 newSelection = currentSelection + 1;
422 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800423 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700424 break;
425 }
426 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500427 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700428 mRollo.mState.save();
429 }
430 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800431 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700432 }
433
Joe Onorato93839052009-08-06 20:34:32 -0700434 @Override
435 public boolean onTouchEvent(MotionEvent ev)
436 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700437 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700438
Joe Onorato7bb17492009-09-24 17:51:01 -0700439 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700440 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700441 }
442
Joe Onoratofb0ca672009-09-14 17:55:46 -0400443 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700444 return true;
445 }
446
447 super.onTouchEvent(ev);
448
Joe Onoratofb0ca672009-09-14 17:55:46 -0400449 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700450 int y = (int)ev.getY();
451
Joe Onoratobcbeab82009-10-01 21:45:43 -0700452 int action = ev.getAction();
453 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400454 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700455 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
456 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500457 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700458 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700459 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700460 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700461 mTouchTracking = TRACKING_FLING;
462
463 mMotionDownRawX = (int)ev.getRawX();
464 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700465
Mike Cleron7d5d7462009-10-20 14:06:00 -0700466 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700467 mRollo.mState.newTouchDown = 1;
468
469 if (!mRollo.checkClickOK()) {
470 mRollo.clearSelectedIcon();
471 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700472 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800473 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700474 if (mDownIconIndex < 0) {
475 // if nothing was selected, no long press.
476 cancelLongPress();
477 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700478 }
479 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700480 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800481 mVelocityTracker = VelocityTracker.obtain();
482 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700483 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700484 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400485 break;
486 case MotionEvent.ACTION_MOVE:
487 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700488 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500489 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
490 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700491 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700492 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700493 int rawX = (int)ev.getRawX();
494 int rawY = (int)ev.getRawY();
495 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700496 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700497
Joe Onorato82ca5502009-10-15 16:59:23 -0700498 if (!mStartedScrolling && slop < mSlop) {
499 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700500 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800501 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700502 if (mDownIconIndex != mCurrentIconIndex) {
503 // If a different icon is selected, don't allow it to be picked up.
504 // This handles off-axis dragging.
505 cancelLongPress();
506 mCurrentIconIndex = -1;
507 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700508 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700509 if (!mStartedScrolling) {
510 cancelLongPress();
511 mCurrentIconIndex = -1;
512 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700513 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700514 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700515 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700516
Joe Onoratobcbeab82009-10-01 21:45:43 -0700517 mStartedScrolling = true;
518 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800519 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700520 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700521 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400522 }
523 break;
524 case MotionEvent.ACTION_UP:
525 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700526 if (mTouchTracking == TRACKING_HOME) {
527 if (action == MotionEvent.ACTION_UP) {
528 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400529 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700530 mLauncher.closeAllApps(true);
531 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500532 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700533 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700534 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700535 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700536 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700537 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700538 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700539
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800540 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
541 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700542 mRollo.clearSelectedIcon();
543 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700544 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700545
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800546 if (mVelocityTracker != null) {
547 mVelocityTracker.recycle();
548 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700549 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700550 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700551 mTouchTracking = TRACKING_NONE;
552 break;
Joe Onorato93839052009-08-06 20:34:32 -0700553 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700554
555 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700556 }
557
Joe Onorato6665c0f2009-09-02 15:27:24 -0700558 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700559 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400560 return;
561 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700562 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
563 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400564 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700565 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700566 mLauncher.startActivitySafely(app.intent);
567 }
568 }
569
570 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700571 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400572 return true;
573 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700574 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
575 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
576 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700577
578 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700579 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
580 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700581
Joe Onoratobcbeab82009-10-01 21:45:43 -0700582 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
583 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700584 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700585 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700586 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700587
Joe Onorato7bb17492009-09-24 17:51:01 -0700588 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700589 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700590 return true;
591 }
592
Joe Onorato52a653f2009-11-11 14:52:11 -0800593 @Override
594 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
595 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
596 if (!isVisible()) {
597 return false;
598 }
599 String text = null;
600 int index;
601 int count = mAllAppsList.size() + 1; // +1 is home
602 int pos = -1;
603 switch (mLastSelection) {
604 case SELECTION_ICONS:
605 index = mRollo.mState.selectedIconIndex;
606 if (index >= 0) {
607 ApplicationInfo info = mAllAppsList.get(index);
608 if (info.title != null) {
609 text = info.title.toString();
610 pos = index;
611 }
612 }
613 break;
614 case SELECTION_HOME:
615 text = getContext().getString(R.string.all_apps_home_button_label);
616 pos = count;
617 break;
618 }
619 if (text != null) {
Joe Onorato52a653f2009-11-11 14:52:11 -0800620 event.setEnabled(true);
621 event.getText().add(text);
622 //event.setContentDescription(text);
623 event.setItemCount(count);
624 event.setCurrentItemIndex(pos);
625 }
626 }
627 return false;
628 }
629
Joe Onorato5162ea92009-09-03 09:39:42 -0700630 public void setDragController(DragController dragger) {
631 mDragController = dragger;
632 }
633
634 public void onDropCompleted(View target, boolean success) {
635 }
636
Joe Onorato4db52312009-10-06 11:17:43 -0700637 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800638 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700639 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800640 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700641 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800642 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400643 cancelLongPress();
Joe Onorato6374c512010-01-06 20:42:25 -0800644 mNextZoom = zoom;
645 mAnimateNextZoom = animate;
646 // if we do setZoom while we don't have a surface, we won't
647 // get the callbacks that actually set mZoom.
648 if (mRollo == null || !mHaveSurface) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800649 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800650 mZoom = zoom;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800651 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700652 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800653 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700654 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400655 }
656
657 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800658 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800659 }
660
661 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800662 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400663 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700664
Joe Onorato9c1289c2009-08-17 11:03:03 -0400665 public void setApps(ArrayList<ApplicationInfo> list) {
666 mAllAppsList = list;
667 if (mRollo != null) {
Joe Onorato3ecbd812009-12-11 13:38:54 -0800668 mRollo.setApps(list);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400670 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700671 }
672
Joe Onoratoa8138d52009-10-06 19:25:30 -0700673 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500674 if (mAllAppsList == null) {
675 // Not done loading yet. We'll find out about it later.
676 return;
677 }
678
Joe Onoratoa8138d52009-10-06 19:25:30 -0700679 final int N = list.size();
Joe Onorato8eea3912009-12-09 13:01:06 -0800680 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700681 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
682 }
683
684 for (int i=0; i<N; i++) {
685 final ApplicationInfo item = list.get(i);
Joe Onoratob0c27f22009-12-01 16:19:38 -0800686 int index = Collections.binarySearch(mAllAppsList, item,
687 LauncherModel.APP_NAME_COMPARATOR);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700688 if (index < 0) {
689 index = -(index+1);
690 }
691 mAllAppsList.add(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -0800692 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700693 mRollo.addApp(index, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700694 }
695 }
696
Joe Onorato8eea3912009-12-09 13:01:06 -0800697 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700698 mRollo.saveAppsList();
699 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700700 }
701
Joe Onoratoa8138d52009-10-06 19:25:30 -0700702 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500703 if (mAllAppsList == null) {
704 // Not done loading yet. We'll find out about it later.
705 return;
706 }
707
Joe Onoratoa8138d52009-10-06 19:25:30 -0700708 final int N = list.size();
709 for (int i=0; i<N; i++) {
710 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400711 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700712 if (index >= 0) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800713 int ic = mRollo != null ? mRollo.mState.iconCount : 666;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700714 mAllAppsList.remove(index);
Joe Onorato8eea3912009-12-09 13:01:06 -0800715 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700716 mRollo.removeApp(index);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700717 }
718 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800719 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700720 // Try to recover. This should keep us from crashing for now.
721 }
722 }
723
Joe Onorato8eea3912009-12-09 13:01:06 -0800724 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700725 mRollo.saveAppsList();
726 }
727 }
728
729 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
730 // Just remove and add, because they may need to be re-sorted.
731 removeApps(list);
732 addApps(list);
733 }
734
Joe Onoratocb9f7982009-10-31 16:32:02 -0400735 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
736 ComponentName component = item.intent.getComponent();
737 final int N = list.size();
738 for (int i=0; i<N; i++) {
739 ApplicationInfo x = list.get(i);
740 if (x.intent.getComponent().equals(component)) {
741 return i;
742 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700743 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400744 return -1;
745 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700746
Joe Onoratoc567acb2009-08-31 14:34:43 -0700747 private static int countPages(int iconCount) {
748 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
749 int pages = iconCount / iconsPerPage;
750 if (pages*iconsPerPage != iconCount) {
751 pages++;
752 }
753 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400754 }
755
Joe Onoratocb75f362009-11-12 13:04:07 -0800756 class AAMessage extends RenderScript.RSMessage {
757 public void run() {
758 mPosX = ((float)mData[0]) / (1 << 16);
759 mVelocity = ((float)mData[1]) / (1 << 16);
760 mZoom = ((float)mData[2]) / (1 << 16);
761 mZoomDirty = false;
762 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800763 }
764
Joe Onorato93839052009-08-06 20:34:32 -0700765 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700766
Joe Onorato1feb3a82009-08-08 22:32:00 -0700767 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700768 private int mWidth;
769 private int mHeight;
770
771 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700772 private Script mScript;
773 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700774 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700775 private Script.Invokable mInvokeFling;
776 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800777 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700778
Jason Samscd689e12009-09-29 15:28:22 -0700779 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700780 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700781 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700782 private ProgramFragment mPFTexMip;
783 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700784 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700785 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700786 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700787 private SimpleMesh mMesh2;
Jason Sams5612e432009-11-16 14:18:07 -0800788 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700789
Joe Onoratod63458b2009-10-15 21:19:09 -0700790 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500791 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700792 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700793
Joe Onoratobf15cb42009-08-07 14:33:40 -0700794 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700795 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700796 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700797
Joe Onoratobf15cb42009-08-07 14:33:40 -0700798 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700799 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700800 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700801 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700802
Joe Onorato6665c0f2009-09-02 15:27:24 -0700803 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700804 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700805
806 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400807 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700808
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700809 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700810 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700811
Jason Sams78aebd82009-09-15 13:06:59 -0700812 class BaseAlloc {
813 Allocation mAlloc;
814 Type mType;
815
816 void save() {
817 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700818 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700819 }
820
Jason Sams476339d2009-09-29 18:14:38 -0700821 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800822 return (Math.abs(mVelocity) < 0.4f) &&
823 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700824 }
825
Jason Sams78aebd82009-09-15 13:06:59 -0700826 class Params extends BaseAlloc {
827 Params() {
828 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
829 mAlloc = Allocation.createTyped(mRS, mType);
830 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700831 }
Jason Sams78aebd82009-09-15 13:06:59 -0700832 public int bubbleWidth;
833 public int bubbleHeight;
834 public int bubbleBitmapWidth;
835 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700836
Joe Onoratobcbeab82009-10-01 21:45:43 -0700837 public int homeButtonWidth;
838 public int homeButtonHeight;
839 public int homeButtonTextureWidth;
840 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700841 }
842
843 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700844 public float newPositionX;
845 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700846 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700847 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700848 public int selectedIconIndex = -1;
849 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700850 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700851 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700852 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700853
854 State() {
855 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
856 mAlloc = Allocation.createTyped(mRS, mType);
857 save();
858 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700859 }
860
861 public RolloRS() {
862 }
863
864 public void init(Resources res, int width, int height) {
865 mRes = res;
866 mWidth = width;
867 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700868 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700869 initProgramVertex();
870 initProgramFragment();
871 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700872 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700873 initGl();
874 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700875 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700876 initRs();
877 }
878
Jason Sams0aa71662009-10-02 18:43:18 -0700879 public void initMesh() {
880 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
881 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
882
Jason Samsd8152b92009-10-13 17:19:10 -0700883 float y = 0;
884 float z = 0;
885 for (int ct=0; ct < 200; ct++) {
886 float angle = 0;
887 float maxAngle = 3.14f * 0.16f;
888 float l = 1.f;
889
890 l = 1 - ((ct-5) * 0.10f);
891 if (ct > 7) {
892 angle = maxAngle * (ct - 7) * 0.2f;
893 angle = Math.min(angle, maxAngle);
894 }
Jason Samsc8514792009-10-29 14:27:29 -0700895 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700896 l = Math.min(1.0f, l);
897
898 y += 0.1f * Math.cos(angle);
899 z += 0.1f * Math.sin(angle);
900
901 float t = 0.1f * ct;
902 float ds = 0.08f;
903 tm.setColor(l, l, l, 0.99f);
904 tm.setTexture(ds, t);
905 tm.addVertex(-0.5f, y, z);
906 tm.setTexture(1 - ds, t);
907 tm.addVertex(0.5f, y, z);
908 }
909 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
910 tm.addTriangle(ct, ct+1, ct+2);
911 tm.addTriangle(ct+1, ct+3, ct+2);
912 }
913 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700914 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700915 }
916
Jason Sams5612e432009-11-16 14:18:07 -0800917 void resize(int w, int h) {
918 mPVA.setupProjectionNormalized(w, h);
919 mWidth = w;
920 mHeight = h;
921 }
922
Jason Samscd689e12009-09-29 15:28:22 -0700923 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800924 mPVA = new ProgramVertex.MatrixAllocation(mRS);
925 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700926
927 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700928 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700929 mPV = pvb.create();
930 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800931 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700932
Jason Sams37e7c2b2009-10-19 12:55:43 -0700933 //pva = new ProgramVertex.MatrixAllocation(mRS);
934 //pva.setupOrthoWindow(mWidth, mHeight);
935 //pvb.setTextureMatrixEnable(true);
936 //mPVOrtho = pvb.create();
937 //mPVOrtho.setName("PVOrtho");
938 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700939
940 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700941 }
Joe Onorato93839052009-08-06 20:34:32 -0700942
Jason Samscd689e12009-09-29 15:28:22 -0700943 private void initProgramFragment() {
944 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700945 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700946 sb.setMag(Sampler.Value.LINEAR);
947 sb.setWrapS(Sampler.Value.CLAMP);
948 sb.setWrapT(Sampler.Value.CLAMP);
949 Sampler linear = sb.create();
950
951 sb.setMin(Sampler.Value.NEAREST);
952 sb.setMag(Sampler.Value.NEAREST);
953 Sampler nearest = sb.create();
954
Jason Sams72f1d312009-12-17 16:58:25 -0800955 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700956 //mPFColor = bf.create();
957 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700958
Jason Sams72f1d312009-12-17 16:58:25 -0800959 bf.setTexture(ProgramFragment.Builder.EnvMode.REPLACE,
960 ProgramFragment.Builder.Format.RGBA, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700961 mPFTexMip = bf.create();
962 mPFTexMip.setName("PFTexMip");
963 mPFTexMip.bindSampler(linear, 0);
964
965 mPFTexNearest = bf.create();
966 mPFTexNearest.setName("PFTexNearest");
967 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700968 }
969
970 private void initProgramStore() {
971 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
972 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700973 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700974 bs.setDitherEnable(true);
975 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
976 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
977 mPSIcons = bs.create();
978 mPSIcons.setName("PSIcons");
979
980 //bs.setDitherEnable(false);
981 //mPSText = bs.create();
982 //mPSText.setName("PSText");
983 }
984
985 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700986 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700987 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700988 }
Jason Sams78aebd82009-09-15 13:06:59 -0700989
Joe Onorato1feb3a82009-08-08 22:32:00 -0700990 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700991 mParams = new Params();
992 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700993
Joe Onoratobf15cb42009-08-07 14:33:40 -0700994 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700995
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700996 mParams.bubbleWidth = bubble.getBubbleWidth();
997 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
998 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
999 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001000
Joe Onoratod63458b2009-10-15 21:19:09 -07001001 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1002 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1003 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001004 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1005 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1006 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001007 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1008 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1009 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001010 mParams.homeButtonWidth = 76;
1011 mParams.homeButtonHeight = 68;
1012 mParams.homeButtonTextureWidth = 128;
1013 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001014
Joe Onoratod63458b2009-10-15 21:19:09 -07001015 mState.homeButtonId = mHomeButtonNormal.getID();
1016
Joe Onorato1feb3a82009-08-08 22:32:00 -07001017 mParams.save();
1018 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001019
Joe Onorato1291a8c2009-09-15 15:07:25 -04001020 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1021 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1022 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001023
Joe Onorato9c1289c2009-08-17 11:03:03 -04001024 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001025 }
1026
Jason Sams37e7c2b2009-10-19 12:55:43 -07001027 private void initScript(int id) {
1028 }
1029
1030 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001031 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Samsd15f3ef2010-01-06 14:58:06 -08001032 sb.setScript(mRes, R.raw.allapps);
Joe Onorato93839052009-08-06 20:34:32 -07001033 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001034 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001035 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1036 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001037 mInvokeMove = sb.addInvokable("move");
1038 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001039 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001040 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001041 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001042 mScript = sb.create();
1043 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1044 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1045 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1046 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1047 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001048
Jason Sams37e7c2b2009-10-19 12:55:43 -07001049 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001050 }
Joe Onorato93839052009-08-06 20:34:32 -07001051
Jason Sams20df7c72009-11-05 12:30:24 -08001052 void dirtyCheck() {
Jason Sams510ee042009-12-15 14:23:37 -08001053 if (mZoomDirty) {
Joe Onorato6374c512010-01-06 20:42:25 -08001054 setZoom(mNextZoom, mAnimateNextZoom);
Jason Sams20df7c72009-11-05 12:30:24 -08001055 }
1056 }
1057
Joe Onorato9c1289c2009-08-17 11:03:03 -04001058 private void setApps(ArrayList<ApplicationInfo> list) {
1059 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001060 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001061 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001062 allocCount = 1;
1063 }
1064
Joe Onorato9c1289c2009-08-17 11:03:03 -04001065 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001066 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001067 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001068
1069 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001070 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001071 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001072
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001073 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074
Joe Onorato9c1289c2009-08-17 11:03:03 -04001075 mState.iconCount = count;
Joe Onorato8eea3912009-12-09 13:01:06 -08001076 for (int i=0; i < mState.iconCount; i++) {
1077 createAppIconAllocations(i, list.get(i));
Joe Onorato3ecbd812009-12-11 13:38:54 -08001078 }
Jason Sams510ee042009-12-15 14:23:37 -08001079 for (int i=0; i < mState.iconCount; i++) {
1080 uploadAppIcon(i, list.get(i));
Joe Onorato8eea3912009-12-09 13:01:06 -08001081 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001082 saveAppsList();
1083 }
1084
Joe Onorato3a8820b2009-11-10 15:06:42 -08001085 private void setZoom(float zoom, boolean animate) {
1086 mRollo.clearSelectedIcon();
1087 mRollo.setHomeSelected(SELECTED_NONE);
1088 if (zoom > 0.001f) {
1089 mRollo.mState.zoomTarget = zoom;
1090 } else {
1091 mRollo.mState.zoomTarget = 0;
1092 }
1093 mRollo.mState.save();
1094 if (!animate) {
1095 mRollo.mInvokeSetZoom.execute();
1096 }
1097 }
1098
Jason Samsc8514792009-10-29 14:27:29 -07001099 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1100 int black[] = new int[w > h ? w : h];
1101 Allocation.Adapter2D a = alloc.createAdapter2D();
1102 int mip = 0;
1103 while (w > 1 || h > 1) {
1104 a.subData(0, 0, 1, h, black);
1105 a.subData(w-1, 0, 1, h, black);
1106 a.subData(0, 0, w, 1, black);
1107 a.subData(0, h-1, w, 1, black);
1108 mip++;
1109 w = (w + 1) >> 1;
1110 h = (h + 1) >> 1;
1111 a.setConstraint(Dimension.LOD, mip);
1112 }
1113 a.subData(0, 0, 1, 1, black);
1114 }
1115
Joe Onorato8eea3912009-12-09 13:01:06 -08001116 private void createAppIconAllocations(int index, ApplicationInfo item) {
Joe Onoratoa8138d52009-10-06 19:25:30 -07001117 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001118 Element.RGBA_8888(mRS), true);
Joe Onorato8eea3912009-12-09 13:01:06 -08001119 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(),
1120 item.iconBitmap.getHeight());
Jason Samsc8514792009-10-29 14:27:29 -07001121
Joe Onoratoa8138d52009-10-06 19:25:30 -07001122 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001123 Element.RGBA_8888(mRS), true);
Joe Onorato8eea3912009-12-09 13:01:06 -08001124 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(),
1125 item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001126
1127 mIconIds[index] = mIcons[index].getID();
1128 mLabelIds[index] = mLabels[index].getID();
1129 }
1130
Joe Onorato8eea3912009-12-09 13:01:06 -08001131 private void uploadAppIcon(int index, ApplicationInfo item) {
1132 if (mIconIds[index] != mIcons[index].getID()) {
1133 throw new IllegalStateException("uploadAppIcon index=" + index
1134 + " mIcons[index].getID=" + mIcons[index].getID()
1135 + " mIconsIds[index]=" + mIconIds[index]
1136 + " item=" + item);
1137 }
1138 mIcons[index].uploadToTexture(0);
1139 mLabels[index].uploadToTexture(0);
1140 }
1141
Joe Onoratoa8138d52009-10-06 19:25:30 -07001142 /**
1143 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1144 * fill in the values and call saveAppsList().
1145 */
1146 private void reallocAppsList(int count) {
1147 Allocation[] icons = new Allocation[count];
1148 int[] iconIds = new int[count];
1149 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1150
1151 Allocation[] labels = new Allocation[count];
1152 int[] labelIds = new int[count];
1153 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1154
Joe Onoratobf173f12009-12-08 13:29:38 -08001155 final int oldCount = mRollo.mState.iconCount;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001156
1157 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1158 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1159 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1160 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1161
1162 mIcons = icons;
1163 mIconIds = iconIds;
1164 mLabels = labels;
1165 mLabelIds = labelIds;
1166 }
1167
1168 /**
1169 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1170 */
1171 private void addApp(int index, ApplicationInfo item) {
1172 final int count = mState.iconCount - index;
1173 final int dest = index + 1;
1174
1175 System.arraycopy(mIcons, index, mIcons, dest, count);
1176 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1177 System.arraycopy(mLabels, index, mLabels, dest, count);
1178 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1179
Joe Onorato8eea3912009-12-09 13:01:06 -08001180 createAppIconAllocations(index, item);
Jason Sams510ee042009-12-15 14:23:37 -08001181 uploadAppIcon(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -08001182 mRollo.mState.iconCount++;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001183 }
1184
1185 /**
1186 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1187 */
1188 private void removeApp(int index) {
1189 final int count = mState.iconCount - index - 1;
1190 final int src = index + 1;
1191
1192 System.arraycopy(mIcons, src, mIcons, index, count);
1193 System.arraycopy(mIconIds, src, mIconIds, index, count);
1194 System.arraycopy(mLabels, src, mLabels, index, count);
1195 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1196
Joe Onoratoa276fc52009-12-08 17:02:02 -08001197 mRollo.mState.iconCount--;
Joe Onorato8eea3912009-12-09 13:01:06 -08001198 final int last = mState.iconCount;
Joe Onoratoa276fc52009-12-08 17:02:02 -08001199
Joe Onoratoa8138d52009-10-06 19:25:30 -07001200 mIcons[last] = null;
1201 mIconIds[last] = 0;
1202 mLabels[last] = null;
1203 mLabelIds[last] = 0;
1204 }
1205
1206 /**
1207 * Send the apps list structures to RS.
1208 */
1209 private void saveAppsList() {
1210 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001211
Joe Onoratoa8138d52009-10-06 19:25:30 -07001212 mAllocIconIds.data(mIconIds);
1213 mAllocLabelIds.data(mLabelIds);
1214
Jason Sams37e7c2b2009-10-19 12:55:43 -07001215 if (mScript != null) { // this happens when we init it
1216 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1217 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001218 }
1219
1220 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001221
1222 // Note: mScript may be null if we haven't initialized it yet.
1223 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001224 if (mInvokeResetWAR != null) {
1225 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001226 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001227 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001228 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001229
1230 void initTouchState() {
1231 int width = getWidth();
1232 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001233 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1234 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001235
Jason Sams37e7c2b2009-10-19 12:55:43 -07001236 int centerY = (height / 2);
1237 mTouchYBorders[0] = centerY - (cellHeight * 2);
1238 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001239 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001240 mTouchYBorders[3] = centerY + cellHeight;
1241 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001242
Joe Onorato6665c0f2009-09-02 15:27:24 -07001243 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001244 mTouchXBorders[0] = 0;
1245 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001246 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001247 mTouchXBorders[3] = centerX + (width / 4);
1248 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001249 }
1250
Joe Onorato664457d2009-10-28 16:30:34 -04001251 void fling() {
1252 mInvokeFling.execute();
1253 }
1254
1255 void move() {
1256 mInvokeMove.execute();
1257 }
1258
1259 void moveTo(float row) {
1260 mState.targetPos = row;
1261 mState.save();
1262 mInvokeMoveTo.execute();
1263 }
1264
Jason Sams37e7c2b2009-10-19 12:55:43 -07001265 int chooseTappedIcon(int x, int y, float pos) {
1266 // Adjust for scroll position if not zero.
1267 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001268
Joe Onorato6665c0f2009-09-02 15:27:24 -07001269 int col = -1;
1270 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001271 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1272 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1273 col = i;
1274 break;
1275 }
1276 }
1277 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1278 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1279 row = i;
1280 break;
1281 }
1282 }
1283
1284 if (row < 0 || col < 0) {
1285 return -1;
1286 }
1287
Joe Onorato664457d2009-10-28 16:30:34 -04001288 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001289 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001290
Joe Onorato664457d2009-10-28 16:30:34 -04001291 if (index >= mState.iconCount) {
1292 return -1;
1293 } else {
1294 return index;
1295 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001296 }
1297
Joe Onorato6665c0f2009-09-02 15:27:24 -07001298 /**
1299 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001300 *
1301 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001302 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001303 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001304 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001305 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001306 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001307 }
1308
Joe Onoratoc61cff92009-11-08 11:54:39 -05001309 /**
1310 * Select the icon at the given index.
1311 *
1312 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001313 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001314 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001315 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001316 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001317 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001318 if (mLastSelection == SELECTION_ICONS) {
1319 mLastSelection = SELECTION_NONE;
1320 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001321 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001322 if (pressed == SELECTED_FOCUSED) {
1323 mLastSelection = SELECTION_ICONS;
1324 }
1325
Joe Onorato52a653f2009-11-11 14:52:11 -08001326 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001327 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001328
Joe Onorato52a653f2009-11-11 14:52:11 -08001329 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001330 Bitmap selectionBitmap = mSelectionBitmap;
1331
1332 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001333 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001334 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001335
1336 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001337 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001338 mSelectedIcon.uploadToTexture(0);
1339 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001340
1341 if (prev != index) {
1342 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001343 //setContentDescription(info.title);
1344 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1345 }
1346 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001347 }
1348 }
1349
1350 /**
1351 * You need to call save() on mState on your own after calling this.
1352 */
1353 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001354 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001355 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001356
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001357 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001358 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001359 switch (mode) {
1360 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001361 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001362 break;
1363 case SELECTED_FOCUSED:
1364 mLastSelection = SELECTION_HOME;
1365 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001366 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001367 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1368 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001369 break;
1370 case SELECTED_PRESSED:
1371 mState.homeButtonId = mHomeButtonPressed.getID();
1372 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001373 }
1374 }
Joe Onoratobe386092009-11-17 17:32:16 -08001375
1376 public void dumpState() {
1377 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1378 Log.d(TAG, "mRollo.mHeight=" + mHeight);
1379 Log.d(TAG, "mRollo.mIcons=" + mIcons);
1380 if (mIcons != null) {
1381 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1382 }
1383 if (mIconIds != null) {
1384 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1385 }
1386 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
1387 if (mLabelIds != null) {
1388 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1389 }
1390 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
1391 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1392 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
Joe Onoratobe386092009-11-17 17:32:16 -08001393 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1394 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1395 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1396 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1397 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1398 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1399 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1400 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1401 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1402 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1403 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1404 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1405 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1406 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1407 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1408 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1409 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1410 }
1411 }
1412
1413 public void dumpState() {
1414 Log.d(TAG, "mRS=" + mRS);
1415 Log.d(TAG, "mRollo=" + mRollo);
1416 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1417 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1418 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1419 Log.d(TAG, "mLastSelection=" + mLastSelection);
1420 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1421 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1422 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1423 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1424 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1425 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1426 Log.d(TAG, "mZoom=" + mZoom);
1427 Log.d(TAG, "mPosX=" + mPosX);
1428 Log.d(TAG, "mVelocity=" + mVelocity);
1429 Log.d(TAG, "mMessageProc=" + mMessageProc);
1430 if (mRollo != null) {
1431 mRollo.dumpState();
1432 }
1433 if (mRS != null) {
1434 mRS.contextDump(0);
1435 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001436 }
Joe Onorato93839052009-08-06 20:34:32 -07001437}
1438
1439