blob: 37ec15c9cbebb460e52131702ebdf83db8113dbb [file] [log] [blame]
Joe Onorato93839052009-08-06 20:34:32 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
Joe Onoratocb9f7982009-10-31 16:32:02 -040019import android.content.ComponentName;
Joe Onorato93839052009-08-06 20:34:32 -070020import android.content.Context;
21import android.content.res.Resources;
22import android.graphics.Bitmap;
Joe Onorato93839052009-08-06 20:34:32 -070023import android.graphics.Canvas;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080024import android.graphics.PixelFormat;
Mike Cleron7d5d7462009-10-20 14:06:00 -070025import android.graphics.Rect;
Joe Onoratod769a632009-08-11 17:09:02 -070026import android.os.SystemClock;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080027import android.renderscript.Allocation;
28import android.renderscript.Dimension;
29import android.renderscript.Element;
30import android.renderscript.ProgramFragment;
31import android.renderscript.ProgramStore;
32import android.renderscript.ProgramVertex;
33import android.renderscript.RSSurfaceView;
34import android.renderscript.RenderScript;
35import android.renderscript.Sampler;
36import android.renderscript.Script;
37import android.renderscript.ScriptC;
38import android.renderscript.SimpleMesh;
39import android.renderscript.Type;
Joe Onorato93839052009-08-06 20:34:32 -070040import android.util.AttributeSet;
41import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070042import android.view.KeyEvent;
43import android.view.MotionEvent;
Joe Onoratob39e51a2009-10-28 15:47:49 -040044import android.view.SoundEffectConstants;
Joe Onorato93839052009-08-06 20:34:32 -070045import android.view.SurfaceHolder;
Joe Onoratod769a632009-08-11 17:09:02 -070046import android.view.VelocityTracker;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080047import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070048import android.view.ViewConfiguration;
Joe Onorato52a653f2009-11-11 14:52:11 -080049import android.view.accessibility.AccessibilityEvent;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080050
51import java.util.ArrayList;
Joe Onoratobe386092009-11-17 17:32:16 -080052import java.util.Arrays;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080053import java.util.Collections;
54import java.util.Comparator;
Joe Onorato93839052009-08-06 20:34:32 -070055
56
Joe Onorato6665c0f2009-09-02 15:27:24 -070057public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070058 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040059 private static final String TAG = "Launcher.AllAppsView";
60
Joe Onoratofb0ca672009-09-14 17:55:46 -040061 /** Bit for mLocks for when there are icons being loaded. */
62 private static final int LOCK_ICONS_PENDING = 1;
63
Joe Onorato68ffd102009-10-15 17:59:43 -070064 private static final int TRACKING_NONE = 0;
65 private static final int TRACKING_FLING = 1;
66 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070067
Joe Onoratoeb8325a2009-11-08 13:20:30 -050068 private static final int SELECTED_NONE = 0;
69 private static final int SELECTED_FOCUSED = 1;
70 private static final int SELECTED_PRESSED = 2;
71
72 private static final int SELECTION_NONE = 0;
73 private static final int SELECTION_ICONS = 1;
74 private static final int SELECTION_HOME = 2;
75
Joe Onorato6665c0f2009-09-02 15:27:24 -070076 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070077 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040078
79 /** When this is 0, modifications are allowed, when it's not, they're not.
80 * TODO: What about scrolling? */
81 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070082
Joe Onorato82ca5502009-10-15 16:59:23 -070083 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070084 private int mMaxFlingVelocity;
85
Joe Onoratobcbeab82009-10-01 21:45:43 -070086 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070087 private RenderScript mRS;
88 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040089 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070090
Mike Cleron7d5d7462009-10-20 14:06:00 -070091 /**
92 * True when we are using arrow keys or trackball to drive navigation
93 */
94 private boolean mArrowNavigation = false;
Joe Onoratoeb8325a2009-11-08 13:20:30 -050095 private boolean mStartedScrolling;
96
97 /**
98 * Used to keep track of the selection when AllAppsView loses window focus.
99 * One of the SELECTION_ constants.
100 */
101 private int mLastSelection;
Jason Sams2e19c052009-10-20 18:19:55 -0700102
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800103 /**
104 * Used to keep track of the selection when AllAppsView loses window focus
105 */
106 private int mLastSelectedIcon;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500107
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800108 private VelocityTracker mVelocityTracker;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700109 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700110 private int mMotionDownRawX;
111 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700112 private int mDownIconIndex = -1;
113 private int mCurrentIconIndex = -1;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800114
Mike Cleronb64b67a2009-11-08 14:56:25 -0800115 private boolean mShouldGainFocus;
116
Joe Onorato3a8820b2009-11-10 15:06:42 -0800117 private boolean mZoomDirty = false;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800118 private boolean mAnimateNextZoom;
119 private float mZoom;
120 private float mPosX;
121 private float mVelocity;
122 private AAMessage mMessageProc;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700123
Joe Onorato6665c0f2009-09-02 15:27:24 -0700124 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700125 public static final int ALLOC_PARAMS = 0;
126 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700127 public static final int ALLOC_ICON_IDS = 3;
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_TEXTURE_WIDTH_PX = 128;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700133 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700134
135 public int SCREEN_WIDTH_PX;
136 public int SCREEN_HEIGHT_PX;
137
Joe Onoratobcbeab82009-10-01 21:45:43 -0700138 public void recompute(int w, int h) {
139 SCREEN_WIDTH_PX = 480;
140 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700141 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700142 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700143
144 public AllAppsView(Context context, AttributeSet attrs) {
145 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700146 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400147 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700148 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700149 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700150 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700151 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
152
Joe Onorato6665c0f2009-09-02 15:27:24 -0700153 setOnClickListener(this);
154 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700155 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700156 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samse26d9fc2009-11-12 14:00:43 -0800157
158 mRS = createRenderScript(true);
159 }
160
161 @Override
162 protected void onDetachedFromWindow() {
163 destroyRenderScript();
Joe Onorato93839052009-08-06 20:34:32 -0700164 }
165
Joe Onoratob39e51a2009-10-28 15:47:49 -0400166 /**
167 * If you have an attached click listener, View always plays the click sound!?!?
168 * Deal with sound effects by hand.
169 */
170 public void reallyPlaySoundEffect(int sound) {
171 boolean old = isSoundEffectsEnabled();
172 setSoundEffectsEnabled(true);
173 playSoundEffect(sound);
174 setSoundEffectsEnabled(old);
175 }
176
Joe Onorato7c312c12009-08-13 21:36:53 -0700177 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
178 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700179 }
180
Joe Onorato6665c0f2009-09-02 15:27:24 -0700181 public void setLauncher(Launcher launcher) {
182 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700183 }
184
Joe Onorato1feb3a82009-08-08 22:32:00 -0700185 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700186 public void surfaceDestroyed(SurfaceHolder holder) {
187 super.surfaceDestroyed(holder);
Jason Sams20df7c72009-11-05 12:30:24 -0800188 mRollo.mHasSurface = false;
Joe Onoratofab74402009-11-11 16:05:23 -0800189 // Without this, we leak mMessageCallback which leaks the context.
190 mRS.mMessageCallback = null;
Joe Onorato7bb17492009-09-24 17:51:01 -0700191 }
192
193 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700194 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800195 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700196
Joe Onorato080d9b62009-11-02 12:01:11 -0500197 super.surfaceChanged(holder, format, w, h);
198
Jason Samse26d9fc2009-11-12 14:00:43 -0800199 if (mRollo == null) {
Jason Sams90396672009-11-03 13:59:34 -0800200 mRollo = new RolloRS();
Jason Sams20df7c72009-11-05 12:30:24 -0800201 mRollo.mHasSurface = true;
Jason Sams90396672009-11-03 13:59:34 -0800202 mRollo.init(getResources(), w, h);
203 if (mAllAppsList != null) {
204 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800205 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800206 if (mShouldGainFocus) {
207 gainFocus();
208 mShouldGainFocus = false;
209 }
Jason Sams20df7c72009-11-05 12:30:24 -0800210 } else {
211 mRollo.mHasSurface = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 }
Jason Samse26d9fc2009-11-12 14:00:43 -0800213 mRollo.dirtyCheck();
Jason Sams5612e432009-11-16 14:18:07 -0800214 mRollo.resize(w, h);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700215
Joe Onoratocb75f362009-11-12 13:04:07 -0800216 mRS.mMessageCallback = mMessageProc = new AAMessage();
217
Joe Onoratoc567acb2009-08-31 14:34:43 -0700218 Resources res = getContext().getResources();
219 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700220
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800221 //long endTime = SystemClock.uptimeMillis();
222 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700223 }
Jason Sams2e19c052009-10-20 18:19:55 -0700224
Joe Onorato93839052009-08-06 20:34:32 -0700225 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800226 public void onWindowFocusChanged(boolean hasWindowFocus) {
227 super.onWindowFocusChanged(hasWindowFocus);
228 if (mArrowNavigation) {
229 if (!hasWindowFocus) {
230 // Clear selection when we lose window focus
231 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500232 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800233 mRollo.clearSelectedIcon();
234 mRollo.mState.save();
235 } else if (hasWindowFocus) {
236 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500237 if (mLastSelection == SELECTION_ICONS) {
238 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800239 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500240 Defines.COLUMNS_PER_PAGE;
241 if (selection < 0 || // No selection
242 selection < firstIcon || // off the top of the screen
243 selection >= mRollo.mState.iconCount || // past last icon
244 selection >= firstIcon + // past last icon on screen
245 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
246 selection = firstIcon;
247 }
248
249 // Select the first icon when we gain window focus
250 mRollo.selectIcon(selection, SELECTED_FOCUSED);
251 mRollo.mState.save();
252 } else if (mLastSelection == SELECTION_HOME) {
253 mRollo.setHomeSelected(SELECTED_FOCUSED);
254 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800255 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800256 }
257 }
258 }
259 }
260
261 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700262 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
263 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400264
265 if (!isVisible()) {
266 return;
267 }
268
Mike Cleron7d5d7462009-10-20 14:06:00 -0700269 if (gainFocus) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800270 if (mRollo != null && mRollo.mHasSurface) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800271 gainFocus();
272 } else {
273 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700274 }
275 } else {
Joe Onorato8eea3912009-12-09 13:01:06 -0800276 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800277 if (mArrowNavigation) {
278 // Clear selection when we lose focus
279 mRollo.clearSelectedIcon();
280 mRollo.setHomeSelected(SELECTED_NONE);
281 mRollo.mState.save();
282 mArrowNavigation = false;
283 }
284 } else {
285 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700286 }
287 }
288 }
289
Mike Cleronb64b67a2009-11-08 14:56:25 -0800290 private void gainFocus() {
291 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
292 // Select the first icon when we gain keyboard focus
293 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800294 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800295 SELECTED_FOCUSED);
296 mRollo.mState.save();
297 }
298 }
299
Mike Cleron7d5d7462009-10-20 14:06:00 -0700300 @Override
301 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700302
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800303 boolean handled = false;
Jason Samse26d9fc2009-11-12 14:00:43 -0800304
Joe Onorato859b3a72009-10-28 15:17:01 -0400305 if (!isVisible()) {
306 return false;
307 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500308 final int iconCount = mRollo.mState.iconCount;
309
Mike Cleron7d5d7462009-10-20 14:06:00 -0700310 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
311 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500312 if (mLastSelection == SELECTION_HOME) {
313 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
314 mLauncher.closeAllApps(true);
315 } else {
316 int whichApp = mRollo.mState.selectedIconIndex;
317 if (whichApp >= 0) {
318 ApplicationInfo app = mAllAppsList.get(whichApp);
319 mLauncher.startActivitySafely(app.intent);
320 handled = true;
321 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700322 }
323 }
324 }
Jason Sams2e19c052009-10-20 18:19:55 -0700325
Joe Onorato478730f2009-11-16 18:54:43 -0800326 if (iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700327 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700328
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800329 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800330 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700331
Mike Cleron7d5d7462009-10-20 14:06:00 -0700332 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500333 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700334
Mike Cleron7d5d7462009-10-20 14:06:00 -0700335 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500336 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700337 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700338
Mike Cleron7d5d7462009-10-20 14:06:00 -0700339 int newSelection = currentSelection;
340
341 switch (keyCode) {
342 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500343 if (mLastSelection == SELECTION_HOME) {
344 mRollo.setHomeSelected(SELECTED_NONE);
345 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
346 if (lastRowCount == 0) {
347 lastRowCount = Defines.COLUMNS_PER_PAGE;
348 }
349 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
350 if (newSelection >= iconCount) {
351 newSelection = iconCount-1;
352 }
353 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
354 - (Defines.ROWS_PER_PAGE - 1);
355 if (target < 0) {
356 target = 0;
357 }
358 if (currentTopRow != target) {
359 mRollo.moveTo(target);
360 }
361 } else {
362 if (currentPageRow > 0) {
363 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
364 } else if (currentTopRow > 0) {
365 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
366 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
Joe Onoratoaf5b4cb2009-12-08 17:51:22 -0800367 } else if (currentPageRow != 0) {
368 newSelection = currentTopRow * Defines.ROWS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500369 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700370 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800371 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700372 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800373
Joe Onoratoa13f5742009-11-02 17:15:19 -0500374 case KeyEvent.KEYCODE_DPAD_DOWN: {
375 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
376 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
377 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500378 if (mLastSelection != SELECTION_HOME) {
379 if (currentRow < rowCount-1) {
380 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato478730f2009-11-16 18:54:43 -0800381 if (currentSelection < 0) {
382 newSelection = 0;
383 } else {
384 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
385 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500386 if (newSelection >= iconCount) {
387 // Go from D to G in this arrangement:
388 // A B C D
389 // E F G
390 newSelection = iconCount - 1;
391 }
392 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
393 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
394 Defines.ROWS_PER_PAGE + 1);
395 }
396 } else {
397 newSelection = -1;
398 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700399 }
400 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800401 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700402 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500403 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700404 case KeyEvent.KEYCODE_DPAD_LEFT:
405 if (currentPageCol > 0) {
406 newSelection = currentSelection - 1;
407 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800408 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700409 break;
410 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700411 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500412 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700413 newSelection = currentSelection + 1;
414 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800415 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700416 break;
417 }
418 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500419 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700420 mRollo.mState.save();
421 }
422 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800423 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700424 }
425
Joe Onorato93839052009-08-06 20:34:32 -0700426 @Override
427 public boolean onTouchEvent(MotionEvent ev)
428 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700429 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700430
Joe Onorato7bb17492009-09-24 17:51:01 -0700431 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700432 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700433 }
434
Joe Onoratofb0ca672009-09-14 17:55:46 -0400435 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700436 return true;
437 }
438
439 super.onTouchEvent(ev);
440
Joe Onoratofb0ca672009-09-14 17:55:46 -0400441 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700442 int y = (int)ev.getY();
443
Joe Onoratobcbeab82009-10-01 21:45:43 -0700444 int action = ev.getAction();
445 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400446 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700447 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
448 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500449 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700450 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700451 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700452 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700453 mTouchTracking = TRACKING_FLING;
454
455 mMotionDownRawX = (int)ev.getRawX();
456 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700457
Mike Cleron7d5d7462009-10-20 14:06:00 -0700458 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700459 mRollo.mState.newTouchDown = 1;
460
461 if (!mRollo.checkClickOK()) {
462 mRollo.clearSelectedIcon();
463 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700464 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800465 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700466 if (mDownIconIndex < 0) {
467 // if nothing was selected, no long press.
468 cancelLongPress();
469 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700470 }
471 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700472 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800473 mVelocityTracker = VelocityTracker.obtain();
474 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700475 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700476 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400477 break;
478 case MotionEvent.ACTION_MOVE:
479 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700480 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500481 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
482 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700483 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700484 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700485 int rawX = (int)ev.getRawX();
486 int rawY = (int)ev.getRawY();
487 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700488 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700489
Joe Onorato82ca5502009-10-15 16:59:23 -0700490 if (!mStartedScrolling && slop < mSlop) {
491 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700492 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800493 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700494 if (mDownIconIndex != mCurrentIconIndex) {
495 // If a different icon is selected, don't allow it to be picked up.
496 // This handles off-axis dragging.
497 cancelLongPress();
498 mCurrentIconIndex = -1;
499 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700500 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700501 if (!mStartedScrolling) {
502 cancelLongPress();
503 mCurrentIconIndex = -1;
504 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700505 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700506 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700507 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700508
Joe Onoratobcbeab82009-10-01 21:45:43 -0700509 mStartedScrolling = true;
510 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800511 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700512 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700513 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400514 }
515 break;
516 case MotionEvent.ACTION_UP:
517 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700518 if (mTouchTracking == TRACKING_HOME) {
519 if (action == MotionEvent.ACTION_UP) {
520 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400521 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700522 mLauncher.closeAllApps(true);
523 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500524 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700525 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700526 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700527 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700528 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700529 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700530 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700531
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800532 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
533 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700534 mRollo.clearSelectedIcon();
535 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700536 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700537
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800538 if (mVelocityTracker != null) {
539 mVelocityTracker.recycle();
540 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700541 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700542 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700543 mTouchTracking = TRACKING_NONE;
544 break;
Joe Onorato93839052009-08-06 20:34:32 -0700545 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700546
547 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700548 }
549
Joe Onorato6665c0f2009-09-02 15:27:24 -0700550 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700551 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400552 return;
553 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700554 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
555 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400556 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700557 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700558 mLauncher.startActivitySafely(app.intent);
559 }
560 }
561
562 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700563 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400564 return true;
565 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700566 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
567 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
568 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700569
Joe Onoratof5e4dd02010-01-14 11:16:55 -0500570 Bitmap bmp = Utilities.extractIconFromTexture(app.iconBitmap, getContext());
Joe Onorato5162ea92009-09-03 09:39:42 -0700571
Joe Onoratof5e4dd02010-01-14 11:16:55 -0500572 final int w = bmp.getWidth();
573 final int h = bmp.getHeight();
574
575 // We don't really have an accurate location to use. This will do.
576 int screenX = mMotionDownRawX - (w / 2);
577 int screenY = mMotionDownRawY - h;
578
579 mDragController.startDrag(bmp, screenX, screenY,
580 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
581 bmp.recycle();
Joe Onoratoe3406a22009-09-03 14:36:25 -0700582
Joe Onorato7bb17492009-09-24 17:51:01 -0700583 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700584 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700585 return true;
586 }
587
Joe Onorato52a653f2009-11-11 14:52:11 -0800588 @Override
589 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
590 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
591 if (!isVisible()) {
592 return false;
593 }
594 String text = null;
595 int index;
596 int count = mAllAppsList.size() + 1; // +1 is home
597 int pos = -1;
598 switch (mLastSelection) {
599 case SELECTION_ICONS:
600 index = mRollo.mState.selectedIconIndex;
601 if (index >= 0) {
602 ApplicationInfo info = mAllAppsList.get(index);
603 if (info.title != null) {
604 text = info.title.toString();
605 pos = index;
606 }
607 }
608 break;
609 case SELECTION_HOME:
610 text = getContext().getString(R.string.all_apps_home_button_label);
611 pos = count;
612 break;
613 }
614 if (text != null) {
Joe Onorato52a653f2009-11-11 14:52:11 -0800615 event.setEnabled(true);
616 event.getText().add(text);
617 //event.setContentDescription(text);
618 event.setItemCount(count);
619 event.setCurrentItemIndex(pos);
620 }
621 }
622 return false;
623 }
624
Joe Onorato5162ea92009-09-03 09:39:42 -0700625 public void setDragController(DragController dragger) {
626 mDragController = dragger;
627 }
628
629 public void onDropCompleted(View target, boolean success) {
630 }
631
Joe Onorato4db52312009-10-06 11:17:43 -0700632 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800633 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700634 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800635 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700636 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800637 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400638 cancelLongPress();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800639 if (mRollo == null || !mRollo.mHasSurface) {
640 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800641 mZoom = zoom;
642 mAnimateNextZoom = animate;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800643 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700644 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800645 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700646 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400647 }
648
649 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800650 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800651 }
652
653 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800654 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400655 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700656
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 public void setApps(ArrayList<ApplicationInfo> list) {
658 mAllAppsList = list;
659 if (mRollo != null) {
Joe Onorato3ecbd812009-12-11 13:38:54 -0800660 mRollo.setApps(list);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400662 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700663 }
664
Joe Onoratoa8138d52009-10-06 19:25:30 -0700665 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500666 if (mAllAppsList == null) {
667 // Not done loading yet. We'll find out about it later.
668 return;
669 }
670
Joe Onoratoa8138d52009-10-06 19:25:30 -0700671 final int N = list.size();
Joe Onorato8eea3912009-12-09 13:01:06 -0800672 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700673 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
674 }
675
676 for (int i=0; i<N; i++) {
677 final ApplicationInfo item = list.get(i);
Joe Onoratob0c27f22009-12-01 16:19:38 -0800678 int index = Collections.binarySearch(mAllAppsList, item,
679 LauncherModel.APP_NAME_COMPARATOR);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700680 if (index < 0) {
681 index = -(index+1);
682 }
683 mAllAppsList.add(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -0800684 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700685 mRollo.addApp(index, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700686 }
687 }
688
Joe Onorato8eea3912009-12-09 13:01:06 -0800689 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700690 mRollo.saveAppsList();
691 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700692 }
693
Joe Onoratoa8138d52009-10-06 19:25:30 -0700694 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500695 if (mAllAppsList == null) {
696 // Not done loading yet. We'll find out about it later.
697 return;
698 }
699
Joe Onoratoa8138d52009-10-06 19:25:30 -0700700 final int N = list.size();
701 for (int i=0; i<N; i++) {
702 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400703 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700704 if (index >= 0) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800705 int ic = mRollo != null ? mRollo.mState.iconCount : 666;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700706 mAllAppsList.remove(index);
Joe Onorato8eea3912009-12-09 13:01:06 -0800707 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700708 mRollo.removeApp(index);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700709 }
710 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800711 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700712 // Try to recover. This should keep us from crashing for now.
713 }
714 }
715
Joe Onorato8eea3912009-12-09 13:01:06 -0800716 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700717 mRollo.saveAppsList();
718 }
719 }
720
721 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
722 // Just remove and add, because they may need to be re-sorted.
723 removeApps(list);
724 addApps(list);
725 }
726
Joe Onoratocb9f7982009-10-31 16:32:02 -0400727 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
728 ComponentName component = item.intent.getComponent();
729 final int N = list.size();
730 for (int i=0; i<N; i++) {
731 ApplicationInfo x = list.get(i);
732 if (x.intent.getComponent().equals(component)) {
733 return i;
734 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700735 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400736 return -1;
737 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700738
Joe Onoratoc567acb2009-08-31 14:34:43 -0700739 private static int countPages(int iconCount) {
740 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
741 int pages = iconCount / iconsPerPage;
742 if (pages*iconsPerPage != iconCount) {
743 pages++;
744 }
745 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400746 }
747
Joe Onoratocb75f362009-11-12 13:04:07 -0800748 class AAMessage extends RenderScript.RSMessage {
749 public void run() {
750 mPosX = ((float)mData[0]) / (1 << 16);
751 mVelocity = ((float)mData[1]) / (1 << 16);
752 mZoom = ((float)mData[2]) / (1 << 16);
753 mZoomDirty = false;
754 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800755 }
756
Joe Onorato93839052009-08-06 20:34:32 -0700757 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700758
Joe Onorato1feb3a82009-08-08 22:32:00 -0700759 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700760 private int mWidth;
761 private int mHeight;
762
763 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700764 private Script mScript;
765 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700766 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700767 private Script.Invokable mInvokeFling;
768 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800769 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700770
Jason Samscd689e12009-09-29 15:28:22 -0700771 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700772 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700773 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700774 private ProgramFragment mPFTexMip;
775 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700776 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700777 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700778 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700779 private SimpleMesh mMesh2;
Jason Sams5612e432009-11-16 14:18:07 -0800780 private ProgramVertex.MatrixAllocation mPVA;
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 Onorato6665c0f2009-09-02 15:27:24 -0700789 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700790
Joe Onorato6665c0f2009-09-02 15:27:24 -0700791 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700792 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700793
794 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400795 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700796
Jason Sams20df7c72009-11-05 12:30:24 -0800797 boolean mHasSurface = false;
Joe Onorato8cf34292009-12-11 10:59:51 -0800798 private boolean mAppsDirty = true;
Jason Samsd8152b92009-10-13 17:19:10 -0700799
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700800 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700801 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700802
Jason Sams78aebd82009-09-15 13:06:59 -0700803 class BaseAlloc {
804 Allocation mAlloc;
805 Type mType;
806
807 void save() {
808 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700809 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700810 }
811
Jason Sams476339d2009-09-29 18:14:38 -0700812 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800813 return (Math.abs(mVelocity) < 0.4f) &&
814 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700815 }
816
Jason Sams78aebd82009-09-15 13:06:59 -0700817 class Params extends BaseAlloc {
818 Params() {
819 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
820 mAlloc = Allocation.createTyped(mRS, mType);
821 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700822 }
Jason Sams78aebd82009-09-15 13:06:59 -0700823 public int bubbleWidth;
824 public int bubbleHeight;
825 public int bubbleBitmapWidth;
826 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700827
Joe Onoratobcbeab82009-10-01 21:45:43 -0700828 public int homeButtonWidth;
829 public int homeButtonHeight;
830 public int homeButtonTextureWidth;
831 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700832 }
833
834 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700835 public float newPositionX;
836 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700837 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700838 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700839 public int selectedIconIndex = -1;
840 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700841 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700842 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700843 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700844
845 State() {
846 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
847 mAlloc = Allocation.createTyped(mRS, mType);
848 save();
849 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700850 }
851
852 public RolloRS() {
853 }
854
855 public void init(Resources res, int width, int height) {
856 mRes = res;
857 mWidth = width;
858 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700859 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700860 initProgramVertex();
861 initProgramFragment();
862 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700863 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700864 initGl();
865 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700866 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700867 initRs();
868 }
869
Jason Sams0aa71662009-10-02 18:43:18 -0700870 public void initMesh() {
871 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
872 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
873
Jason Samsd8152b92009-10-13 17:19:10 -0700874 float y = 0;
875 float z = 0;
876 for (int ct=0; ct < 200; ct++) {
877 float angle = 0;
878 float maxAngle = 3.14f * 0.16f;
879 float l = 1.f;
880
Joe Onoratof5e4dd02010-01-14 11:16:55 -0500881 l = 1 - ((ct-7) * 0.10f);
Jason Samsd8152b92009-10-13 17:19:10 -0700882 if (ct > 7) {
883 angle = maxAngle * (ct - 7) * 0.2f;
884 angle = Math.min(angle, maxAngle);
885 }
Jason Samsc8514792009-10-29 14:27:29 -0700886 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700887 l = Math.min(1.0f, l);
888
889 y += 0.1f * Math.cos(angle);
890 z += 0.1f * Math.sin(angle);
891
892 float t = 0.1f * ct;
893 float ds = 0.08f;
894 tm.setColor(l, l, l, 0.99f);
895 tm.setTexture(ds, t);
896 tm.addVertex(-0.5f, y, z);
897 tm.setTexture(1 - ds, t);
898 tm.addVertex(0.5f, y, z);
899 }
900 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
901 tm.addTriangle(ct, ct+1, ct+2);
902 tm.addTriangle(ct+1, ct+3, ct+2);
903 }
904 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700905 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700906 }
907
Jason Sams5612e432009-11-16 14:18:07 -0800908 void resize(int w, int h) {
909 mPVA.setupProjectionNormalized(w, h);
910 mWidth = w;
911 mHeight = h;
912 }
913
Jason Samscd689e12009-09-29 15:28:22 -0700914 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800915 mPVA = new ProgramVertex.MatrixAllocation(mRS);
916 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700917
918 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700919 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700920 mPV = pvb.create();
921 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800922 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700923
Jason Sams37e7c2b2009-10-19 12:55:43 -0700924 //pva = new ProgramVertex.MatrixAllocation(mRS);
925 //pva.setupOrthoWindow(mWidth, mHeight);
926 //pvb.setTextureMatrixEnable(true);
927 //mPVOrtho = pvb.create();
928 //mPVOrtho.setName("PVOrtho");
929 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700930
931 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700932 }
Joe Onorato93839052009-08-06 20:34:32 -0700933
Jason Samscd689e12009-09-29 15:28:22 -0700934 private void initProgramFragment() {
935 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700936 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700937 sb.setMag(Sampler.Value.LINEAR);
938 sb.setWrapS(Sampler.Value.CLAMP);
939 sb.setWrapT(Sampler.Value.CLAMP);
940 Sampler linear = sb.create();
941
942 sb.setMin(Sampler.Value.NEAREST);
943 sb.setMag(Sampler.Value.NEAREST);
944 Sampler nearest = sb.create();
945
946 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Joe Onoratof5e4dd02010-01-14 11:16:55 -0500947 mPFColor = bf.create();
948 mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700949
950 bf.setTexEnable(true, 0);
951 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700952 mPFTexMip = bf.create();
953 mPFTexMip.setName("PFTexMip");
954 mPFTexMip.bindSampler(linear, 0);
955
956 mPFTexNearest = bf.create();
957 mPFTexNearest.setName("PFTexNearest");
958 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700959 }
960
961 private void initProgramStore() {
962 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
963 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700964 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700965 bs.setDitherEnable(true);
966 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
967 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
968 mPSIcons = bs.create();
969 mPSIcons.setName("PSIcons");
970
971 //bs.setDitherEnable(false);
972 //mPSText = bs.create();
973 //mPSText.setName("PSText");
974 }
975
976 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700977 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700978 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700979 }
Jason Sams78aebd82009-09-15 13:06:59 -0700980
Joe Onorato1feb3a82009-08-08 22:32:00 -0700981 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700982 mParams = new Params();
983 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700984
Joe Onoratobf15cb42009-08-07 14:33:40 -0700985 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700986
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700987 mParams.bubbleWidth = bubble.getBubbleWidth();
988 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
989 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
990 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700991
Joe Onoratod63458b2009-10-15 21:19:09 -0700992 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
993 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
994 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500995 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
996 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
997 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -0700998 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
999 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1000 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001001 mParams.homeButtonWidth = 76;
1002 mParams.homeButtonHeight = 68;
1003 mParams.homeButtonTextureWidth = 128;
1004 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001005
Joe Onoratod63458b2009-10-15 21:19:09 -07001006 mState.homeButtonId = mHomeButtonNormal.getID();
1007
Joe Onorato1feb3a82009-08-08 22:32:00 -07001008 mParams.save();
1009 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001010
Joe Onorato1291a8c2009-09-15 15:07:25 -04001011 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
1012 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
1013 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001014
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001016 }
1017
Jason Sams37e7c2b2009-10-19 12:55:43 -07001018 private void initScript(int id) {
1019 }
1020
1021 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001022 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001023 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -07001024 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001025 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001026 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1027 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001028 mInvokeMove = sb.addInvokable("move");
1029 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001030 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001031 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001032 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001033 mScript = sb.create();
1034 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1035 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1036 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1037 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001038
Jason Sams37e7c2b2009-10-19 12:55:43 -07001039 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001040 }
Joe Onorato93839052009-08-06 20:34:32 -07001041
Jason Sams20df7c72009-11-05 12:30:24 -08001042 void dirtyCheck() {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001043 if (mHasSurface) {
Joe Onorato8cf34292009-12-11 10:59:51 -08001044 if (mAppsDirty && mAllAppsList != null) {
Joe Onorato8eea3912009-12-09 13:01:06 -08001045 for (int i=0; i < mState.iconCount; i++) {
1046 uploadAppIcon(i, mAllAppsList.get(i));
1047 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08001048 saveAppsList();
1049 mAppsDirty = false;
1050 }
1051 if (mZoomDirty) {
Joe Onorato68ba5ca2009-11-12 14:23:43 -08001052 setZoom(mZoom, mAnimateNextZoom);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001053 }
Jason Sams20df7c72009-11-05 12:30:24 -08001054 }
1055 }
1056
Joe Onorato9c1289c2009-08-17 11:03:03 -04001057 private void setApps(ArrayList<ApplicationInfo> list) {
1058 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001059 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001060 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001061 allocCount = 1;
1062 }
1063
Joe Onorato9c1289c2009-08-17 11:03:03 -04001064 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001065 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001066 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001067
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001068 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001069
Joe Onorato9c1289c2009-08-17 11:03:03 -04001070 mState.iconCount = count;
Joe Onoratof5e4dd02010-01-14 11:16:55 -05001071 long before = SystemClock.uptimeMillis();
Joe Onorato8eea3912009-12-09 13:01:06 -08001072 for (int i=0; i < mState.iconCount; i++) {
1073 createAppIconAllocations(i, list.get(i));
Joe Onorato3ecbd812009-12-11 13:38:54 -08001074 }
Joe Onoratof5e4dd02010-01-14 11:16:55 -05001075 long after = SystemClock.uptimeMillis();
1076 //Log.d(TAG, "createAppIconAllocations took " + (after-before) + "ms");
Joe Onorato3ecbd812009-12-11 13:38:54 -08001077 if (mHasSurface) {
1078 for (int i=0; i < mState.iconCount; i++) {
1079 uploadAppIcon(i, list.get(i));
1080 }
1081 } else {
1082 mRollo.mAppsDirty = true;
Joe Onorato8eea3912009-12-09 13:01:06 -08001083 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001084 saveAppsList();
1085 }
1086
Joe Onorato3a8820b2009-11-10 15:06:42 -08001087 private void setZoom(float zoom, boolean animate) {
1088 mRollo.clearSelectedIcon();
1089 mRollo.setHomeSelected(SELECTED_NONE);
1090 if (zoom > 0.001f) {
1091 mRollo.mState.zoomTarget = zoom;
1092 } else {
1093 mRollo.mState.zoomTarget = 0;
1094 }
1095 mRollo.mState.save();
1096 if (!animate) {
1097 mRollo.mInvokeSetZoom.execute();
1098 }
1099 }
1100
Jason Samsc8514792009-10-29 14:27:29 -07001101 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1102 int black[] = new int[w > h ? w : h];
1103 Allocation.Adapter2D a = alloc.createAdapter2D();
1104 int mip = 0;
1105 while (w > 1 || h > 1) {
1106 a.subData(0, 0, 1, h, black);
1107 a.subData(w-1, 0, 1, h, black);
1108 a.subData(0, 0, w, 1, black);
1109 a.subData(0, h-1, w, 1, black);
1110 mip++;
1111 w = (w + 1) >> 1;
1112 h = (h + 1) >> 1;
1113 a.setConstraint(Dimension.LOD, mip);
1114 }
1115 a.subData(0, 0, 1, 1, black);
1116 }
1117
Joe Onorato8eea3912009-12-09 13:01:06 -08001118 private void createAppIconAllocations(int index, ApplicationInfo item) {
Joe Onoratof5e4dd02010-01-14 11:16:55 -05001119 Bitmap bitmap = item.iconBitmap;
1120 mIcons[index] = Allocation.createFromBitmap(mRS, bitmap, Element.RGBA_8888(mRS), true);
1121 frameBitmapAllocMips(mIcons[index], bitmap.getWidth(), bitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001122
1123 mIconIds[index] = mIcons[index].getID();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001124 }
1125
Joe Onorato8eea3912009-12-09 13:01:06 -08001126 private void uploadAppIcon(int index, ApplicationInfo item) {
1127 if (mIconIds[index] != mIcons[index].getID()) {
1128 throw new IllegalStateException("uploadAppIcon index=" + index
1129 + " mIcons[index].getID=" + mIcons[index].getID()
1130 + " mIconsIds[index]=" + mIconIds[index]
1131 + " item=" + item);
1132 }
1133 mIcons[index].uploadToTexture(0);
Joe Onorato8eea3912009-12-09 13:01:06 -08001134 }
1135
Joe Onoratoa8138d52009-10-06 19:25:30 -07001136 /**
1137 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1138 * fill in the values and call saveAppsList().
1139 */
1140 private void reallocAppsList(int count) {
1141 Allocation[] icons = new Allocation[count];
1142 int[] iconIds = new int[count];
1143 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1144
Joe Onoratobf173f12009-12-08 13:29:38 -08001145 final int oldCount = mRollo.mState.iconCount;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001146
1147 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1148 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001149
1150 mIcons = icons;
1151 mIconIds = iconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001152 }
1153
1154 /**
1155 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1156 */
1157 private void addApp(int index, ApplicationInfo item) {
1158 final int count = mState.iconCount - index;
1159 final int dest = index + 1;
1160
1161 System.arraycopy(mIcons, index, mIcons, dest, count);
1162 System.arraycopy(mIconIds, index, mIconIds, dest, count);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001163
Joe Onorato8eea3912009-12-09 13:01:06 -08001164 createAppIconAllocations(index, item);
1165
Joe Onoratoa276fc52009-12-08 17:02:02 -08001166 if (mHasSurface) {
Jason Sams20df7c72009-11-05 12:30:24 -08001167 uploadAppIcon(index, item);
1168 } else {
1169 mAppsDirty = true;
1170 }
Joe Onorato8eea3912009-12-09 13:01:06 -08001171
1172 mRollo.mState.iconCount++;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001173 }
1174
1175 /**
1176 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1177 */
1178 private void removeApp(int index) {
1179 final int count = mState.iconCount - index - 1;
1180 final int src = index + 1;
1181
1182 System.arraycopy(mIcons, src, mIcons, index, count);
1183 System.arraycopy(mIconIds, src, mIconIds, index, count);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001184
Joe Onoratoa276fc52009-12-08 17:02:02 -08001185 mRollo.mState.iconCount--;
Joe Onorato8eea3912009-12-09 13:01:06 -08001186 final int last = mState.iconCount;
Joe Onoratoa276fc52009-12-08 17:02:02 -08001187
Joe Onoratoa8138d52009-10-06 19:25:30 -07001188 mIcons[last] = null;
1189 mIconIds[last] = 0;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001190 }
1191
1192 /**
1193 * Send the apps list structures to RS.
1194 */
1195 private void saveAppsList() {
1196 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001197
Joe Onoratoa8138d52009-10-06 19:25:30 -07001198 mAllocIconIds.data(mIconIds);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001199
Jason Sams37e7c2b2009-10-19 12:55:43 -07001200 if (mScript != null) { // this happens when we init it
1201 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001202 }
1203
1204 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001205
1206 // Note: mScript may be null if we haven't initialized it yet.
1207 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001208 if (mInvokeResetWAR != null) {
1209 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001210 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001211 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001213
1214 void initTouchState() {
1215 int width = getWidth();
1216 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001217 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1218 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001219
Jason Sams37e7c2b2009-10-19 12:55:43 -07001220 int centerY = (height / 2);
1221 mTouchYBorders[0] = centerY - (cellHeight * 2);
1222 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001223 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001224 mTouchYBorders[3] = centerY + cellHeight;
1225 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001226
Joe Onorato6665c0f2009-09-02 15:27:24 -07001227 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001228 mTouchXBorders[0] = 0;
1229 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001230 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001231 mTouchXBorders[3] = centerX + (width / 4);
1232 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001233 }
1234
Joe Onorato664457d2009-10-28 16:30:34 -04001235 void fling() {
1236 mInvokeFling.execute();
1237 }
1238
1239 void move() {
1240 mInvokeMove.execute();
1241 }
1242
1243 void moveTo(float row) {
1244 mState.targetPos = row;
1245 mState.save();
1246 mInvokeMoveTo.execute();
1247 }
1248
Jason Sams37e7c2b2009-10-19 12:55:43 -07001249 int chooseTappedIcon(int x, int y, float pos) {
1250 // Adjust for scroll position if not zero.
1251 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001252
Joe Onorato6665c0f2009-09-02 15:27:24 -07001253 int col = -1;
1254 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001255 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1256 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1257 col = i;
1258 break;
1259 }
1260 }
1261 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1262 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1263 row = i;
1264 break;
1265 }
1266 }
1267
1268 if (row < 0 || col < 0) {
1269 return -1;
1270 }
1271
Joe Onorato664457d2009-10-28 16:30:34 -04001272 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001273 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001274
Joe Onorato664457d2009-10-28 16:30:34 -04001275 if (index >= mState.iconCount) {
1276 return -1;
1277 } else {
1278 return index;
1279 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001280 }
1281
Joe Onorato6665c0f2009-09-02 15:27:24 -07001282 /**
1283 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001284 *
1285 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001286 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001287 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001288 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001289 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001290 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001291 }
1292
Joe Onoratoc61cff92009-11-08 11:54:39 -05001293 /**
1294 * Select the icon at the given index.
1295 *
1296 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001297 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001298 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001299 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001300 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001301 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001302 if (mLastSelection == SELECTION_ICONS) {
1303 mLastSelection = SELECTION_NONE;
1304 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001305 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001306 if (pressed == SELECTED_FOCUSED) {
1307 mLastSelection = SELECTION_ICONS;
1308 }
1309
Joe Onorato52a653f2009-11-11 14:52:11 -08001310 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001311 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001312
Joe Onorato52a653f2009-11-11 14:52:11 -08001313 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001314 Bitmap selectionBitmap = mSelectionBitmap;
1315
Joe Onoratof5e4dd02010-01-14 11:16:55 -05001316 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas, selectionBitmap,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001317 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001318 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001319
1320 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001321 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001322 mSelectedIcon.uploadToTexture(0);
1323 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001324
1325 if (prev != index) {
1326 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001327 //setContentDescription(info.title);
1328 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1329 }
1330 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001331 }
1332 }
1333
1334 /**
1335 * You need to call save() on mState on your own after calling this.
1336 */
1337 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001338 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001339 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001340
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001341 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001342 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001343 switch (mode) {
1344 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001345 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001346 break;
1347 case SELECTED_FOCUSED:
1348 mLastSelection = SELECTION_HOME;
1349 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001350 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001351 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1352 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001353 break;
1354 case SELECTED_PRESSED:
1355 mState.homeButtonId = mHomeButtonPressed.getID();
1356 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001357 }
1358 }
Joe Onoratobe386092009-11-17 17:32:16 -08001359
1360 public void dumpState() {
1361 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1362 Log.d(TAG, "mRollo.mHeight=" + mHeight);
1363 Log.d(TAG, "mRollo.mIcons=" + mIcons);
1364 if (mIcons != null) {
1365 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1366 }
1367 if (mIconIds != null) {
1368 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1369 }
1370 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
Joe Onoratobe386092009-11-17 17:32:16 -08001371 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1372 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
1373 Log.d(TAG, "mRollo.mHasSurface=" + mHasSurface);
1374 Log.d(TAG, "mRollo.mAppsDirty=" + mAppsDirty);
1375 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1376 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1377 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1378 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1379 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1380 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1381 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1382 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1383 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1384 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1385 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1386 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1387 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1388 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1389 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1390 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1391 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1392 }
1393 }
1394
1395 public void dumpState() {
1396 Log.d(TAG, "mRS=" + mRS);
1397 Log.d(TAG, "mRollo=" + mRollo);
1398 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1399 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1400 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1401 Log.d(TAG, "mLastSelection=" + mLastSelection);
1402 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1403 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1404 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1405 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1406 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1407 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1408 Log.d(TAG, "mZoom=" + mZoom);
1409 Log.d(TAG, "mPosX=" + mPosX);
1410 Log.d(TAG, "mVelocity=" + mVelocity);
1411 Log.d(TAG, "mMessageProc=" + mMessageProc);
1412 if (mRollo != null) {
1413 mRollo.dumpState();
1414 }
1415 if (mRS != null) {
1416 mRS.contextDump(0);
1417 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001418 }
Joe Onorato93839052009-08-06 20:34:32 -07001419}
1420
1421