blob: 76fee5b91f41ff3660a66141e400adfd84e1eeff [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;
Mike Cleron4a5c1e12009-11-03 10:17:05 -080049
50import java.util.ArrayList;
51import java.util.Collections;
52import java.util.Comparator;
Joe Onorato93839052009-08-06 20:34:32 -070053
54
Joe Onorato6665c0f2009-09-02 15:27:24 -070055public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070056 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040057 private static final String TAG = "Launcher.AllAppsView";
58
Joe Onoratofb0ca672009-09-14 17:55:46 -040059 /** Bit for mLocks for when there are icons being loaded. */
60 private static final int LOCK_ICONS_PENDING = 1;
61
Joe Onorato68ffd102009-10-15 17:59:43 -070062 private static final int TRACKING_NONE = 0;
63 private static final int TRACKING_FLING = 1;
64 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070065
Joe Onoratoeb8325a2009-11-08 13:20:30 -050066 private static final int SELECTED_NONE = 0;
67 private static final int SELECTED_FOCUSED = 1;
68 private static final int SELECTED_PRESSED = 2;
69
70 private static final int SELECTION_NONE = 0;
71 private static final int SELECTION_ICONS = 1;
72 private static final int SELECTION_HOME = 2;
73
Joe Onorato6665c0f2009-09-02 15:27:24 -070074 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070075 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040076
77 /** When this is 0, modifications are allowed, when it's not, they're not.
78 * TODO: What about scrolling? */
79 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070080
Joe Onorato82ca5502009-10-15 16:59:23 -070081 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070082 private int mMaxFlingVelocity;
83
Joe Onoratobcbeab82009-10-01 21:45:43 -070084 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070085 private RenderScript mRS;
86 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040087 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070088
Mike Cleron7d5d7462009-10-20 14:06:00 -070089 /**
90 * True when we are using arrow keys or trackball to drive navigation
91 */
92 private boolean mArrowNavigation = false;
Joe Onoratoeb8325a2009-11-08 13:20:30 -050093 private boolean mStartedScrolling;
94
95 /**
96 * Used to keep track of the selection when AllAppsView loses window focus.
97 * One of the SELECTION_ constants.
98 */
99 private int mLastSelection;
Jason Sams2e19c052009-10-20 18:19:55 -0700100
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800101 /**
102 * Used to keep track of the selection when AllAppsView loses window focus
103 */
104 private int mLastSelectedIcon;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500105
Joe Onoratod769a632009-08-11 17:09:02 -0700106 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700107 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700108 private int mMotionDownRawX;
109 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700110 private int mDownIconIndex = -1;
111 private int mCurrentIconIndex = -1;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800112
Mike Cleronb64b67a2009-11-08 14:56:25 -0800113 private boolean mShouldGainFocus;
114
Joe Onorato3a8820b2009-11-10 15:06:42 -0800115 private boolean mZoomDirty = false;
116 private float mNextZoom;
117 private boolean mNextAnimate;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700118
Joe Onorato6665c0f2009-09-02 15:27:24 -0700119 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700120 public static final int ALLOC_PARAMS = 0;
121 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700122 public static final int ALLOC_ICON_IDS = 3;
123 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700124
125 public static final int COLUMNS_PER_PAGE = 4;
126 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700127
Joe Onorato6665c0f2009-09-02 15:27:24 -0700128 public static final int ICON_WIDTH_PX = 64;
129 public static final int ICON_TEXTURE_WIDTH_PX = 128;
130
131 public static final int ICON_HEIGHT_PX = 64;
132 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700133
134 public int SCREEN_WIDTH_PX;
135 public int SCREEN_HEIGHT_PX;
136
Joe Onoratobcbeab82009-10-01 21:45:43 -0700137 public void recompute(int w, int h) {
138 SCREEN_WIDTH_PX = 480;
139 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700140 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700141 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700142
143 public AllAppsView(Context context, AttributeSet attrs) {
144 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700145 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400146 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700147 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700148 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700149 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700150 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
151
Joe Onorato6665c0f2009-09-02 15:27:24 -0700152 setOnClickListener(this);
153 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700154 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700155 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700156 }
157
Joe Onoratob39e51a2009-10-28 15:47:49 -0400158 /**
159 * If you have an attached click listener, View always plays the click sound!?!?
160 * Deal with sound effects by hand.
161 */
162 public void reallyPlaySoundEffect(int sound) {
163 boolean old = isSoundEffectsEnabled();
164 setSoundEffectsEnabled(true);
165 playSoundEffect(sound);
166 setSoundEffectsEnabled(old);
167 }
168
Joe Onorato7c312c12009-08-13 21:36:53 -0700169 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
170 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700171 }
172
Joe Onorato6665c0f2009-09-02 15:27:24 -0700173 public void setLauncher(Launcher launcher) {
174 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700175 }
176
Joe Onorato1feb3a82009-08-08 22:32:00 -0700177 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700178 public void surfaceDestroyed(SurfaceHolder holder) {
179 super.surfaceDestroyed(holder);
Jason Sams20df7c72009-11-05 12:30:24 -0800180 mRollo.mHasSurface = false;
Joe Onorato7bb17492009-09-24 17:51:01 -0700181 }
182
183 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700184 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800185 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700186
Joe Onorato080d9b62009-11-02 12:01:11 -0500187 super.surfaceChanged(holder, format, w, h);
188
Jason Sams90396672009-11-03 13:59:34 -0800189 if (mRS == null) {
Jason Sams90396672009-11-03 13:59:34 -0800190 mRS = createRenderScript(true);
191 mRollo = new RolloRS();
Jason Sams20df7c72009-11-05 12:30:24 -0800192 mRollo.mHasSurface = true;
Jason Sams90396672009-11-03 13:59:34 -0800193 mRollo.init(getResources(), w, h);
194 if (mAllAppsList != null) {
195 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800196 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800197 if (mShouldGainFocus) {
198 gainFocus();
199 mShouldGainFocus = false;
200 }
Joe Onorato3a8820b2009-11-10 15:06:42 -0800201 mRollo.dirtyCheck();
Jason Sams20df7c72009-11-05 12:30:24 -0800202 } else {
203 mRollo.mHasSurface = true;
204 mRollo.dirtyCheck();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700206
207 Resources res = getContext().getResources();
208 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700209
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800210 //long endTime = SystemClock.uptimeMillis();
211 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700212 }
Jason Sams2e19c052009-10-20 18:19:55 -0700213
Joe Onorato93839052009-08-06 20:34:32 -0700214 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800215 public void onWindowFocusChanged(boolean hasWindowFocus) {
216 super.onWindowFocusChanged(hasWindowFocus);
217 if (mArrowNavigation) {
218 if (!hasWindowFocus) {
219 // Clear selection when we lose window focus
220 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500221 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800222 mRollo.clearSelectedIcon();
223 mRollo.mState.save();
224 } else if (hasWindowFocus) {
225 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500226 if (mLastSelection == SELECTION_ICONS) {
227 int selection = mLastSelectedIcon;
228 final int firstIcon = Math.round(mRollo.mMessageProc.mPosX) *
229 Defines.COLUMNS_PER_PAGE;
230 if (selection < 0 || // No selection
231 selection < firstIcon || // off the top of the screen
232 selection >= mRollo.mState.iconCount || // past last icon
233 selection >= firstIcon + // past last icon on screen
234 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
235 selection = firstIcon;
236 }
237
238 // Select the first icon when we gain window focus
239 mRollo.selectIcon(selection, SELECTED_FOCUSED);
240 mRollo.mState.save();
241 } else if (mLastSelection == SELECTION_HOME) {
242 mRollo.setHomeSelected(SELECTED_FOCUSED);
243 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800244 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800245 }
246 }
247 }
248 }
249
250 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700251 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
252 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400253
254 if (!isVisible()) {
255 return;
256 }
257
Mike Cleron7d5d7462009-10-20 14:06:00 -0700258 if (gainFocus) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800259 if (mRollo != null) {
260 gainFocus();
261 } else {
262 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700263 }
264 } else {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800265 if (mRollo != null) {
266 if (mArrowNavigation) {
267 // Clear selection when we lose focus
268 mRollo.clearSelectedIcon();
269 mRollo.setHomeSelected(SELECTED_NONE);
270 mRollo.mState.save();
271 mArrowNavigation = false;
272 }
273 } else {
274 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700275 }
276 }
277 }
278
Mike Cleronb64b67a2009-11-08 14:56:25 -0800279 private void gainFocus() {
280 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
281 // Select the first icon when we gain keyboard focus
282 mArrowNavigation = true;
283 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE,
284 SELECTED_FOCUSED);
285 mRollo.mState.save();
286 }
287 }
288
Mike Cleron7d5d7462009-10-20 14:06:00 -0700289 @Override
290 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700291
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800292 boolean handled = false;
293
Joe Onorato859b3a72009-10-28 15:17:01 -0400294 if (!isVisible()) {
295 return false;
296 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500297 final int iconCount = mRollo.mState.iconCount;
298
Mike Cleron7d5d7462009-10-20 14:06:00 -0700299 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
300 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500301 if (mLastSelection == SELECTION_HOME) {
302 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
303 mLauncher.closeAllApps(true);
304 } else {
305 int whichApp = mRollo.mState.selectedIconIndex;
306 if (whichApp >= 0) {
307 ApplicationInfo app = mAllAppsList.get(whichApp);
308 mLauncher.startActivitySafely(app.intent);
309 handled = true;
310 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700311 }
312 }
313 }
Jason Sams2e19c052009-10-20 18:19:55 -0700314
Joe Onoratoa13f5742009-11-02 17:15:19 -0500315 if (mArrowNavigation && iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700316 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700317
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800318 int currentSelection = mRollo.mState.selectedIconIndex;
319 int currentTopRow = Math.round(mRollo.mMessageProc.mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700320
Mike Cleron7d5d7462009-10-20 14:06:00 -0700321 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500322 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700323
Mike Cleron7d5d7462009-10-20 14:06:00 -0700324 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500325 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700326 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700327
Mike Cleron7d5d7462009-10-20 14:06:00 -0700328 int newSelection = currentSelection;
329
330 switch (keyCode) {
331 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500332 if (mLastSelection == SELECTION_HOME) {
333 mRollo.setHomeSelected(SELECTED_NONE);
334 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
335 if (lastRowCount == 0) {
336 lastRowCount = Defines.COLUMNS_PER_PAGE;
337 }
338 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
339 if (newSelection >= iconCount) {
340 newSelection = iconCount-1;
341 }
342 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
343 - (Defines.ROWS_PER_PAGE - 1);
344 if (target < 0) {
345 target = 0;
346 }
347 if (currentTopRow != target) {
348 mRollo.moveTo(target);
349 }
350 } else {
351 if (currentPageRow > 0) {
352 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
353 } else if (currentTopRow > 0) {
354 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
355 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
356 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700357 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800358 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700359 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800360
Joe Onoratoa13f5742009-11-02 17:15:19 -0500361 case KeyEvent.KEYCODE_DPAD_DOWN: {
362 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
363 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
364 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500365 if (mLastSelection != SELECTION_HOME) {
366 if (currentRow < rowCount-1) {
367 mRollo.setHomeSelected(SELECTED_NONE);
368 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
369 if (newSelection >= iconCount) {
370 // Go from D to G in this arrangement:
371 // A B C D
372 // E F G
373 newSelection = iconCount - 1;
374 }
375 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
376 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
377 Defines.ROWS_PER_PAGE + 1);
378 }
379 } else {
380 newSelection = -1;
381 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700382 }
383 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800384 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700385 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500386 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700387 case KeyEvent.KEYCODE_DPAD_LEFT:
388 if (currentPageCol > 0) {
389 newSelection = currentSelection - 1;
390 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800391 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700392 break;
393 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700394 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Joe Onoratoa13f5742009-11-02 17:15:19 -0500395 (currentSelection < iconCount - 1)) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700396 newSelection = currentSelection + 1;
397 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800398 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700399 break;
400 }
401 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500402 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700403 mRollo.mState.save();
404 }
405 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800406 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700407 }
408
Joe Onorato93839052009-08-06 20:34:32 -0700409 @Override
410 public boolean onTouchEvent(MotionEvent ev)
411 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700412 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700413
Joe Onorato7bb17492009-09-24 17:51:01 -0700414 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700415 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700416 }
417
Joe Onoratofb0ca672009-09-14 17:55:46 -0400418 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700419 return true;
420 }
421
422 super.onTouchEvent(ev);
423
Joe Onoratofb0ca672009-09-14 17:55:46 -0400424 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700425 int y = (int)ev.getY();
426
Joe Onoratobcbeab82009-10-01 21:45:43 -0700427 int action = ev.getAction();
428 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400429 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700430 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
431 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500432 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700433 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700434 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700435 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700436 mTouchTracking = TRACKING_FLING;
437
438 mMotionDownRawX = (int)ev.getRawX();
439 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700440
Mike Cleron7d5d7462009-10-20 14:06:00 -0700441 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700442 mRollo.mState.newTouchDown = 1;
443
444 if (!mRollo.checkClickOK()) {
445 mRollo.clearSelectedIcon();
446 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700447 mDownIconIndex = mCurrentIconIndex
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500448 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700449 if (mDownIconIndex < 0) {
450 // if nothing was selected, no long press.
451 cancelLongPress();
452 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700453 }
454 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700455 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700456 mVelocity = VelocityTracker.obtain();
457 mVelocity.addMovement(ev);
458 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700459 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400460 break;
461 case MotionEvent.ACTION_MOVE:
462 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700463 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500464 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
465 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700466 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700467 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700468 int rawX = (int)ev.getRawX();
469 int rawY = (int)ev.getRawY();
470 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700471 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700472
Joe Onorato82ca5502009-10-15 16:59:23 -0700473 if (!mStartedScrolling && slop < mSlop) {
474 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700475 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700476 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
477 if (mDownIconIndex != mCurrentIconIndex) {
478 // If a different icon is selected, don't allow it to be picked up.
479 // This handles off-axis dragging.
480 cancelLongPress();
481 mCurrentIconIndex = -1;
482 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700483 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700484 if (!mStartedScrolling) {
485 cancelLongPress();
486 mCurrentIconIndex = -1;
487 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700488 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700489 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700490 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700491
Joe Onoratobcbeab82009-10-01 21:45:43 -0700492 mStartedScrolling = true;
493 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700494 mVelocity.addMovement(ev);
495 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700496 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400497 }
498 break;
499 case MotionEvent.ACTION_UP:
500 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700501 if (mTouchTracking == TRACKING_HOME) {
502 if (action == MotionEvent.ACTION_UP) {
503 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400504 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700505 mLauncher.closeAllApps(true);
506 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500507 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700508 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700509 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700510 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700511 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700512 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700513 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700514
Jason Sams12c14a82009-10-06 14:33:15 -0700515 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700516 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700517 mRollo.clearSelectedIcon();
518 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700519 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700520
Joe Onorato539ed9d2009-10-02 10:22:14 -0700521 if (mVelocity != null) {
522 mVelocity.recycle();
523 mVelocity = null;
524 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700525 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700526 mTouchTracking = TRACKING_NONE;
527 break;
Joe Onorato93839052009-08-06 20:34:32 -0700528 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700529
530 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700531 }
532
Joe Onorato6665c0f2009-09-02 15:27:24 -0700533 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700534 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400535 return;
536 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700537 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
538 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400539 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700540 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700541 mLauncher.startActivitySafely(app.intent);
542 }
543 }
544
545 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700546 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400547 return true;
548 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700549 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
550 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
551 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700552
553 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700554 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
555 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700556
Joe Onoratobcbeab82009-10-01 21:45:43 -0700557 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
558 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700559 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700560 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700561 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700562
Joe Onorato7bb17492009-09-24 17:51:01 -0700563 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700564 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700565 return true;
566 }
567
Joe Onorato5162ea92009-09-03 09:39:42 -0700568 public void setDragController(DragController dragger) {
569 mDragController = dragger;
570 }
571
572 public void onDropCompleted(View target, boolean success) {
573 }
574
Joe Onorato4db52312009-10-06 11:17:43 -0700575 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800576 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700577 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800578 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700579 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800580 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400581 cancelLongPress();
Joe Onorato3a8820b2009-11-10 15:06:42 -0800582 if (mRollo == null || !mRollo.mHasSurface) {
583 mZoomDirty = true;
584 mNextZoom = zoom;
585 mNextAnimate = animate;
586 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700587 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800588 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700589 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400590 }
591
592 public boolean isVisible() {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800593 if (mZoomDirty) {
594 return mNextZoom > 0.001f;
595 } else {
596 if (mRollo == null) {
597 return false;
598 } else {
599 return mRollo.mMessageProc.mZoom > 0.001f;
600 }
Joe Onorato7bb17492009-09-24 17:51:01 -0700601 }
Joe Onorato3a8820b2009-11-10 15:06:42 -0800602 }
603
604 public boolean isOpaque() {
605 if (mZoomDirty) {
606 return mNextZoom > 0.999f;
607 } else {
608 if (mRollo == null) {
609 return false;
610 } else {
611 return mRollo.mMessageProc.mZoom > 0.999f;
612 }
613 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400614 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700615
Joe Onorato9c1289c2009-08-17 11:03:03 -0400616 public void setApps(ArrayList<ApplicationInfo> list) {
617 mAllAppsList = list;
618 if (mRollo != null) {
619 mRollo.setApps(list);
620 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400621 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700622 }
623
Joe Onoratoa8138d52009-10-06 19:25:30 -0700624 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500625 if (mAllAppsList == null) {
626 // Not done loading yet. We'll find out about it later.
627 return;
628 }
629
Joe Onoratoa8138d52009-10-06 19:25:30 -0700630 final int N = list.size();
631 if (mRollo != null) {
632 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
633 }
634
635 for (int i=0; i<N; i++) {
636 final ApplicationInfo item = list.get(i);
637 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
638 if (index < 0) {
639 index = -(index+1);
640 }
641 mAllAppsList.add(index, item);
642 if (mRollo != null) {
643 mRollo.addApp(index, item);
644 mRollo.mState.iconCount++;
645 }
646 }
647
648 if (mRollo != null) {
649 mRollo.saveAppsList();
650 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700651 }
652
Joe Onoratoa8138d52009-10-06 19:25:30 -0700653 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500654 if (mAllAppsList == null) {
655 // Not done loading yet. We'll find out about it later.
656 return;
657 }
658
Joe Onoratoa8138d52009-10-06 19:25:30 -0700659 final int N = list.size();
660 for (int i=0; i<N; i++) {
661 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400662 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700663 if (index >= 0) {
664 mAllAppsList.remove(index);
665 if (mRollo != null) {
666 mRollo.removeApp(index);
667 mRollo.mState.iconCount--;
668 }
669 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800670 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700671 // Try to recover. This should keep us from crashing for now.
672 }
673 }
674
675 if (mRollo != null) {
676 mRollo.saveAppsList();
677 }
678 }
679
680 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
681 // Just remove and add, because they may need to be re-sorted.
682 removeApps(list);
683 addApps(list);
684 }
685
686 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
687 public int compare(ApplicationInfo a, ApplicationInfo b) {
688 int result = a.title.toString().compareTo(b.toString());
689 if (result != 0) {
690 return result;
691 }
692 return a.intent.getComponent().compareTo(b.intent.getComponent());
693 }
694 };
695
Joe Onoratocb9f7982009-10-31 16:32:02 -0400696 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
697 ComponentName component = item.intent.getComponent();
698 final int N = list.size();
699 for (int i=0; i<N; i++) {
700 ApplicationInfo x = list.get(i);
701 if (x.intent.getComponent().equals(component)) {
702 return i;
703 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700704 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400705 return -1;
706 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700707
Joe Onoratoc567acb2009-08-31 14:34:43 -0700708 private static int countPages(int iconCount) {
709 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
710 int pages = iconCount / iconsPerPage;
711 if (pages*iconsPerPage != iconCount) {
712 pages++;
713 }
714 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400715 }
716
Joe Onorato93839052009-08-06 20:34:32 -0700717 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700718
Joe Onorato1feb3a82009-08-08 22:32:00 -0700719 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700720 private int mWidth;
721 private int mHeight;
722
723 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700724 private Script mScript;
725 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700726 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700727 private Script.Invokable mInvokeFling;
728 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800729 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700730
Jason Samscd689e12009-09-29 15:28:22 -0700731 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700732 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700733 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700734 private ProgramFragment mPFTexMip;
735 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700736 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700737 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700738 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700739 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700740
Joe Onoratod63458b2009-10-15 21:19:09 -0700741 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500742 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700743 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700744
Joe Onoratobf15cb42009-08-07 14:33:40 -0700745 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700746 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700747 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700748
Joe Onoratobf15cb42009-08-07 14:33:40 -0700749 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700750 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700751 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700752 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700753
Joe Onorato6665c0f2009-09-02 15:27:24 -0700754 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700755 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700756
757 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400758 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700759
Jason Sams20df7c72009-11-05 12:30:24 -0800760 boolean mHasSurface = false;
761 private boolean mAppsDirty = false;
Jason Samsd8152b92009-10-13 17:19:10 -0700762
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700763 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700764 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700765
Jason Sams78aebd82009-09-15 13:06:59 -0700766 class BaseAlloc {
767 Allocation mAlloc;
768 Type mType;
769
770 void save() {
771 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700772 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700773 }
774
Jason Sams12c14a82009-10-06 14:33:15 -0700775 class AAMessage extends RenderScript.RSMessage {
776 public void run() {
777 mPosX = ((float)mData[0]) / (1 << 16);
778 mVelocity = ((float)mData[1]) / (1 << 16);
779 mZoom = ((float)mData[2]) / (1 << 16);
Jason Sams12c14a82009-10-06 14:33:15 -0700780 }
781 float mZoom;
782 float mPosX;
783 float mVelocity;
784 }
785 AAMessage mMessageProc;
786
Jason Sams476339d2009-09-29 18:14:38 -0700787 private boolean checkClickOK() {
Jason Sams2e19c052009-10-20 18:19:55 -0700788 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
789 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700790 }
791
Jason Sams78aebd82009-09-15 13:06:59 -0700792 class Params extends BaseAlloc {
793 Params() {
794 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
795 mAlloc = Allocation.createTyped(mRS, mType);
796 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700797 }
Jason Sams78aebd82009-09-15 13:06:59 -0700798 public int bubbleWidth;
799 public int bubbleHeight;
800 public int bubbleBitmapWidth;
801 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700802
Joe Onoratobcbeab82009-10-01 21:45:43 -0700803 public int homeButtonWidth;
804 public int homeButtonHeight;
805 public int homeButtonTextureWidth;
806 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700807 }
808
809 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700810 public float newPositionX;
811 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700812 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700813 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700814 public int selectedIconIndex = -1;
815 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700816 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700817 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700818 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700819
820 State() {
821 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
822 mAlloc = Allocation.createTyped(mRS, mType);
823 save();
824 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700825 }
826
827 public RolloRS() {
828 }
829
830 public void init(Resources res, int width, int height) {
831 mRes = res;
832 mWidth = width;
833 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700834 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700835 initProgramVertex();
836 initProgramFragment();
837 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700838 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700839 initGl();
840 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700841 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700842 initRs();
843 }
844
Jason Sams0aa71662009-10-02 18:43:18 -0700845 public void initMesh() {
846 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
847 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
848
Jason Samsd8152b92009-10-13 17:19:10 -0700849 float y = 0;
850 float z = 0;
851 for (int ct=0; ct < 200; ct++) {
852 float angle = 0;
853 float maxAngle = 3.14f * 0.16f;
854 float l = 1.f;
855
856 l = 1 - ((ct-5) * 0.10f);
857 if (ct > 7) {
858 angle = maxAngle * (ct - 7) * 0.2f;
859 angle = Math.min(angle, maxAngle);
860 }
Jason Samsc8514792009-10-29 14:27:29 -0700861 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700862 l = Math.min(1.0f, l);
863
864 y += 0.1f * Math.cos(angle);
865 z += 0.1f * Math.sin(angle);
866
867 float t = 0.1f * ct;
868 float ds = 0.08f;
869 tm.setColor(l, l, l, 0.99f);
870 tm.setTexture(ds, t);
871 tm.addVertex(-0.5f, y, z);
872 tm.setTexture(1 - ds, t);
873 tm.addVertex(0.5f, y, z);
874 }
875 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
876 tm.addTriangle(ct, ct+1, ct+2);
877 tm.addTriangle(ct+1, ct+3, ct+2);
878 }
879 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700880 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700881 }
882
Jason Samscd689e12009-09-29 15:28:22 -0700883 private void initProgramVertex() {
884 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
885 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700886
887 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700888 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700889 mPV = pvb.create();
890 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700891 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700892
Jason Sams37e7c2b2009-10-19 12:55:43 -0700893 //pva = new ProgramVertex.MatrixAllocation(mRS);
894 //pva.setupOrthoWindow(mWidth, mHeight);
895 //pvb.setTextureMatrixEnable(true);
896 //mPVOrtho = pvb.create();
897 //mPVOrtho.setName("PVOrtho");
898 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700899
900 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700901 }
Joe Onorato93839052009-08-06 20:34:32 -0700902
Jason Samscd689e12009-09-29 15:28:22 -0700903 private void initProgramFragment() {
904 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700905 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700906 sb.setMag(Sampler.Value.LINEAR);
907 sb.setWrapS(Sampler.Value.CLAMP);
908 sb.setWrapT(Sampler.Value.CLAMP);
909 Sampler linear = sb.create();
910
911 sb.setMin(Sampler.Value.NEAREST);
912 sb.setMag(Sampler.Value.NEAREST);
913 Sampler nearest = sb.create();
914
915 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700916 //mPFColor = bf.create();
917 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700918
919 bf.setTexEnable(true, 0);
920 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700921 mPFTexMip = bf.create();
922 mPFTexMip.setName("PFTexMip");
923 mPFTexMip.bindSampler(linear, 0);
924
925 mPFTexNearest = bf.create();
926 mPFTexNearest.setName("PFTexNearest");
927 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700928 }
929
930 private void initProgramStore() {
931 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
932 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700933 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700934 bs.setDitherEnable(true);
935 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
936 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
937 mPSIcons = bs.create();
938 mPSIcons.setName("PSIcons");
939
940 //bs.setDitherEnable(false);
941 //mPSText = bs.create();
942 //mPSText.setName("PSText");
943 }
944
945 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700946 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700947 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700948 }
Jason Sams78aebd82009-09-15 13:06:59 -0700949
Joe Onorato1feb3a82009-08-08 22:32:00 -0700950 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700951 mParams = new Params();
952 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700953
Joe Onoratobf15cb42009-08-07 14:33:40 -0700954 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700955
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700956 mParams.bubbleWidth = bubble.getBubbleWidth();
957 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
958 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
959 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700960
Joe Onoratod63458b2009-10-15 21:19:09 -0700961 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
962 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
963 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500964 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
965 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
966 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -0700967 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
968 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
969 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700970 mParams.homeButtonWidth = 76;
971 mParams.homeButtonHeight = 68;
972 mParams.homeButtonTextureWidth = 128;
973 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700974
Joe Onoratod63458b2009-10-15 21:19:09 -0700975 mState.homeButtonId = mHomeButtonNormal.getID();
976
Joe Onorato1feb3a82009-08-08 22:32:00 -0700977 mParams.save();
978 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979
Joe Onorato1291a8c2009-09-15 15:07:25 -0400980 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
981 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
982 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700983
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700985 }
986
Jason Sams37e7c2b2009-10-19 12:55:43 -0700987 private void initScript(int id) {
988 }
989
990 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700991 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700992 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700993 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700994 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700995 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
996 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700997 mInvokeMove = sb.addInvokable("move");
998 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700999 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001000 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001001 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001002 mScript = sb.create();
1003 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1004 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1005 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1006 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1007 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -07001008
Jason Sams12c14a82009-10-06 14:33:15 -07001009 mMessageProc = new AAMessage();
1010 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001011 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001012 }
Joe Onorato93839052009-08-06 20:34:32 -07001013
Jason Sams20df7c72009-11-05 12:30:24 -08001014 private void uploadApps(ArrayList<ApplicationInfo> list) {
1015 for (int i=0; i < mState.iconCount; i++) {
1016 uploadAppIcon(i, list.get(i));
1017 }
1018 }
1019
1020 void dirtyCheck() {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001021 if (mHasSurface) {
1022 if (mAppsDirty) {
1023 uploadApps(mAllAppsList);
1024 saveAppsList();
1025 mAppsDirty = false;
1026 }
1027 if (mZoomDirty) {
1028 setZoom(mNextZoom, mNextAnimate);
1029 mZoomDirty = false;
1030 }
Jason Sams20df7c72009-11-05 12:30:24 -08001031 }
1032 }
1033
Joe Onorato9c1289c2009-08-17 11:03:03 -04001034 private void setApps(ArrayList<ApplicationInfo> list) {
1035 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001036 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001037 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001038 allocCount = 1;
1039 }
1040
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001042 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001043 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001044
1045 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001046 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001047 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001049 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050
1051 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
1052
Joe Onorato9c1289c2009-08-17 11:03:03 -04001053 mState.iconCount = count;
Jason Sams20df7c72009-11-05 12:30:24 -08001054 uploadApps(list);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001055 saveAppsList();
1056 }
1057
Joe Onorato3a8820b2009-11-10 15:06:42 -08001058 private void setZoom(float zoom, boolean animate) {
1059 mRollo.clearSelectedIcon();
1060 mRollo.setHomeSelected(SELECTED_NONE);
1061 if (zoom > 0.001f) {
1062 mRollo.mState.zoomTarget = zoom;
1063 } else {
1064 mRollo.mState.zoomTarget = 0;
1065 }
1066 mRollo.mState.save();
1067 if (!animate) {
1068 mRollo.mInvokeSetZoom.execute();
1069 }
1070 }
1071
Jason Samsc8514792009-10-29 14:27:29 -07001072 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
1073 int black[] = new int[w > h ? w : h];
1074 Allocation.Adapter2D a = alloc.createAdapter2D();
1075 int mip = 0;
1076 while (w > 1 || h > 1) {
1077 a.subData(0, 0, 1, h, black);
1078 a.subData(w-1, 0, 1, h, black);
1079 a.subData(0, 0, w, 1, black);
1080 a.subData(0, h-1, w, 1, black);
1081 mip++;
1082 w = (w + 1) >> 1;
1083 h = (h + 1) >> 1;
1084 a.setConstraint(Dimension.LOD, mip);
1085 }
1086 a.subData(0, 0, 1, 1, black);
1087 }
1088
Joe Onoratoa8138d52009-10-06 19:25:30 -07001089 private void uploadAppIcon(int index, ApplicationInfo item) {
1090 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001091 Element.RGBA_8888(mRS), true);
1092 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
1093
Joe Onoratoa8138d52009-10-06 19:25:30 -07001094 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -07001095 Element.RGBA_8888(mRS), true);
1096 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -07001097
1098 mIcons[index].uploadToTexture(0);
1099 mLabels[index].uploadToTexture(0);
1100
1101 mIconIds[index] = mIcons[index].getID();
1102 mLabelIds[index] = mLabels[index].getID();
1103 }
1104
1105 /**
1106 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1107 * fill in the values and call saveAppsList().
1108 */
1109 private void reallocAppsList(int count) {
1110 Allocation[] icons = new Allocation[count];
1111 int[] iconIds = new int[count];
1112 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1113
1114 Allocation[] labels = new Allocation[count];
1115 int[] labelIds = new int[count];
1116 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1117
1118 final int oldCount = mIcons.length;
1119
1120 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1121 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
1122 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1123 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
1124
1125 mIcons = icons;
1126 mIconIds = iconIds;
1127 mLabels = labels;
1128 mLabelIds = labelIds;
1129 }
1130
1131 /**
1132 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1133 */
1134 private void addApp(int index, ApplicationInfo item) {
1135 final int count = mState.iconCount - index;
1136 final int dest = index + 1;
1137
1138 System.arraycopy(mIcons, index, mIcons, dest, count);
1139 System.arraycopy(mIconIds, index, mIconIds, dest, count);
1140 System.arraycopy(mLabels, index, mLabels, dest, count);
1141 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
1142
Jason Sams20df7c72009-11-05 12:30:24 -08001143 if (mHasSurface ) {
1144 uploadAppIcon(index, item);
1145 } else {
1146 mAppsDirty = true;
1147 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001148 }
1149
1150 /**
1151 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1152 */
1153 private void removeApp(int index) {
1154 final int count = mState.iconCount - index - 1;
1155 final int src = index + 1;
1156
1157 System.arraycopy(mIcons, src, mIcons, index, count);
1158 System.arraycopy(mIconIds, src, mIconIds, index, count);
1159 System.arraycopy(mLabels, src, mLabels, index, count);
1160 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1161
1162 final int last = mState.iconCount - 1;
1163 mIcons[last] = null;
1164 mIconIds[last] = 0;
1165 mLabels[last] = null;
1166 mLabelIds[last] = 0;
1167 }
1168
1169 /**
1170 * Send the apps list structures to RS.
1171 */
1172 private void saveAppsList() {
1173 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001174
Joe Onoratoa8138d52009-10-06 19:25:30 -07001175 mAllocIconIds.data(mIconIds);
1176 mAllocLabelIds.data(mLabelIds);
1177
Jason Sams37e7c2b2009-10-19 12:55:43 -07001178 if (mScript != null) { // this happens when we init it
1179 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1180 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001181 }
1182
1183 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001184
1185 // Note: mScript may be null if we haven't initialized it yet.
1186 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001187 if (mInvokeResetWAR != null) {
1188 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001189 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001190 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001192
1193 void initTouchState() {
1194 int width = getWidth();
1195 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001196 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1197 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001198
Jason Sams37e7c2b2009-10-19 12:55:43 -07001199 int centerY = (height / 2);
1200 mTouchYBorders[0] = centerY - (cellHeight * 2);
1201 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001202 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001203 mTouchYBorders[3] = centerY + cellHeight;
1204 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001205
Joe Onorato6665c0f2009-09-02 15:27:24 -07001206 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001207 mTouchXBorders[0] = 0;
1208 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001209 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001210 mTouchXBorders[3] = centerX + (width / 4);
1211 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001212 }
1213
Joe Onorato664457d2009-10-28 16:30:34 -04001214 void fling() {
1215 mInvokeFling.execute();
1216 }
1217
1218 void move() {
1219 mInvokeMove.execute();
1220 }
1221
1222 void moveTo(float row) {
1223 mState.targetPos = row;
1224 mState.save();
1225 mInvokeMoveTo.execute();
1226 }
1227
Jason Sams37e7c2b2009-10-19 12:55:43 -07001228 int chooseTappedIcon(int x, int y, float pos) {
1229 // Adjust for scroll position if not zero.
1230 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001231
Joe Onorato6665c0f2009-09-02 15:27:24 -07001232 int col = -1;
1233 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001234 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1235 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1236 col = i;
1237 break;
1238 }
1239 }
1240 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1241 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1242 row = i;
1243 break;
1244 }
1245 }
1246
1247 if (row < 0 || col < 0) {
1248 return -1;
1249 }
1250
Joe Onorato664457d2009-10-28 16:30:34 -04001251 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001252 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001253
Joe Onorato664457d2009-10-28 16:30:34 -04001254 if (index >= mState.iconCount) {
1255 return -1;
1256 } else {
1257 return index;
1258 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001259 }
1260
Joe Onorato6665c0f2009-09-02 15:27:24 -07001261 /**
1262 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001263 *
1264 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001265 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001266 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001267 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001268 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001269 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001270 }
1271
Joe Onoratoc61cff92009-11-08 11:54:39 -05001272 /**
1273 * Select the icon at the given index.
1274 *
1275 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001276 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001277 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001278 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001279 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001280 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001281 if (mLastSelection == SELECTION_ICONS) {
1282 mLastSelection = SELECTION_NONE;
1283 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001284 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001285 if (pressed == SELECTED_FOCUSED) {
1286 mLastSelection = SELECTION_ICONS;
1287 }
1288
Joe Onorato6665c0f2009-09-02 15:27:24 -07001289 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001290
1291 Bitmap selectionBitmap = mSelectionBitmap;
1292
1293 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001294 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1295 pressed == SELECTED_PRESSED,
Joe Onorato1291a8c2009-09-15 15:07:25 -04001296 mAllAppsList.get(index).iconBitmap);
1297
1298 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001299 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001300 mSelectedIcon.uploadToTexture(0);
1301 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001302 }
1303 }
1304
1305 /**
1306 * You need to call save() on mState on your own after calling this.
1307 */
1308 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001309 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001310 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001311
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001312 void setHomeSelected(int mode) {
1313 switch (mode) {
1314 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001315 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001316 break;
1317 case SELECTED_FOCUSED:
1318 mLastSelection = SELECTION_HOME;
1319 mState.homeButtonId = mHomeButtonFocused.getID();
1320 break;
1321 case SELECTED_PRESSED:
1322 mState.homeButtonId = mHomeButtonPressed.getID();
1323 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001324 }
1325 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001326 }
Joe Onorato93839052009-08-06 20:34:32 -07001327}
1328
1329