blob: f8008816f31b29943b8d933406f127bd36b0f34c [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 Onorato75fd1ec2010-02-18 15:31:52 -0800117 private boolean mHaveSurface = false;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800118 private boolean mZoomDirty = false;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800119 private boolean mAnimateNextZoom;
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800120 private float mNextZoom;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800121 private float mZoom;
122 private float mPosX;
123 private float mVelocity;
124 private AAMessage mMessageProc;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700125
Joe Onorato6665c0f2009-09-02 15:27:24 -0700126 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700127 public static final int ALLOC_PARAMS = 0;
128 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700129 public static final int ALLOC_ICON_IDS = 3;
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800130 public static final int ALLOC_LABEL_IDS = 4;
131 public static final int ALLOC_VP_CONSTANTS = 5;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700132
133 public static final int COLUMNS_PER_PAGE = 4;
134 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700135
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800136 public static final int ICON_WIDTH_PX = 64;
137 public static final int ICON_TEXTURE_WIDTH_PX = 74;
138 public static final int SELECTION_TEXTURE_WIDTH_PX = 74 + 20;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700139
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800140 public static final int ICON_HEIGHT_PX = 64;
141 public static final int ICON_TEXTURE_HEIGHT_PX = 74;
142 public static final int SELECTION_TEXTURE_HEIGHT_PX = 74 + 20;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700143 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700144
145 public AllAppsView(Context context, AttributeSet attrs) {
146 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700147 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400148 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700149 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700150 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700151 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700152 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
153
Joe Onorato6665c0f2009-09-02 15:27:24 -0700154 setOnClickListener(this);
155 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700156 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700157 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Jason Samse26d9fc2009-11-12 14:00:43 -0800158
159 mRS = createRenderScript(true);
160 }
161
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800162 /**
163 * Note that this implementation prohibits this view from ever being reattached.
164 */
Jason Samse26d9fc2009-11-12 14:00:43 -0800165 @Override
166 protected void onDetachedFromWindow() {
167 destroyRenderScript();
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800168 mRS.mMessageCallback = null;
169 mRS = null;
Joe Onorato93839052009-08-06 20:34:32 -0700170 }
171
Joe Onoratob39e51a2009-10-28 15:47:49 -0400172 /**
173 * If you have an attached click listener, View always plays the click sound!?!?
174 * Deal with sound effects by hand.
175 */
176 public void reallyPlaySoundEffect(int sound) {
177 boolean old = isSoundEffectsEnabled();
178 setSoundEffectsEnabled(true);
179 playSoundEffect(sound);
180 setSoundEffectsEnabled(old);
181 }
182
Joe Onorato7c312c12009-08-13 21:36:53 -0700183 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
184 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700185 }
186
Joe Onorato6665c0f2009-09-02 15:27:24 -0700187 public void setLauncher(Launcher launcher) {
188 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700189 }
190
Joe Onorato1feb3a82009-08-08 22:32:00 -0700191 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700192 public void surfaceDestroyed(SurfaceHolder holder) {
193 super.surfaceDestroyed(holder);
Joe Onoratofab74402009-11-11 16:05:23 -0800194 // Without this, we leak mMessageCallback which leaks the context.
195 mRS.mMessageCallback = null;
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800196 // We may lose any callbacks that are pending, so make sure that we re-sync that
197 // on the next surfaceChanged.
198 mZoomDirty = true;
199 mHaveSurface = false;
Joe Onorato7bb17492009-09-24 17:51:01 -0700200 }
201
202 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700203 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800204 //long startTime = SystemClock.uptimeMillis();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700205
Joe Onorato080d9b62009-11-02 12:01:11 -0500206 super.surfaceChanged(holder, format, w, h);
207
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800208 mHaveSurface = true;
209
Jason Samse26d9fc2009-11-12 14:00:43 -0800210 if (mRollo == null) {
Jason Sams90396672009-11-03 13:59:34 -0800211 mRollo = new RolloRS();
212 mRollo.init(getResources(), w, h);
213 if (mAllAppsList != null) {
214 mRollo.setApps(mAllAppsList);
Jason Sams90396672009-11-03 13:59:34 -0800215 }
Mike Cleronb64b67a2009-11-08 14:56:25 -0800216 if (mShouldGainFocus) {
217 gainFocus();
218 mShouldGainFocus = false;
219 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 }
Jason Samse26d9fc2009-11-12 14:00:43 -0800221 mRollo.dirtyCheck();
Jason Sams5612e432009-11-16 14:18:07 -0800222 mRollo.resize(w, h);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700223
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800224 if (mRS != null) {
225 mRS.mMessageCallback = mMessageProc = new AAMessage();
226 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800227
Joe Onoratoc567acb2009-08-31 14:34:43 -0700228 Resources res = getContext().getResources();
229 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700230
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800231
232 if (mRollo.mUniformAlloc != null) {
233 float tf[] = new float[] {72.f, 72.f,
234 120.f, 120.f, 0.f, 0.f,
235 120.f, 680.f,
236 (2.f / 480.f), 0, -((float)w / 2) - 0.25f, -380.25f};
237 if (w > h) {
238 tf[6] = 40.f;
239 tf[7] = h - 40.f;
240 tf[9] = 1.f;
241 tf[10] = -((float)w / 2) - 0.25f;
242 tf[11] = -((float)h / 2) - 0.25f;
243 }
244
245 mRollo.mUniformAlloc.data(tf);
246 }
247
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800248 //long endTime = SystemClock.uptimeMillis();
249 //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700250 }
Jason Sams2e19c052009-10-20 18:19:55 -0700251
Joe Onorato93839052009-08-06 20:34:32 -0700252 @Override
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800253 public void onWindowFocusChanged(boolean hasWindowFocus) {
254 super.onWindowFocusChanged(hasWindowFocus);
255 if (mArrowNavigation) {
256 if (!hasWindowFocus) {
257 // Clear selection when we lose window focus
258 mLastSelectedIcon = mRollo.mState.selectedIconIndex;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500259 mRollo.setHomeSelected(SELECTED_NONE);
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800260 mRollo.clearSelectedIcon();
261 mRollo.mState.save();
262 } else if (hasWindowFocus) {
263 if (mRollo.mState.iconCount > 0) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500264 if (mLastSelection == SELECTION_ICONS) {
265 int selection = mLastSelectedIcon;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800266 final int firstIcon = Math.round(mPosX) *
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500267 Defines.COLUMNS_PER_PAGE;
268 if (selection < 0 || // No selection
269 selection < firstIcon || // off the top of the screen
270 selection >= mRollo.mState.iconCount || // past last icon
271 selection >= firstIcon + // past last icon on screen
272 (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) {
273 selection = firstIcon;
274 }
275
276 // Select the first icon when we gain window focus
277 mRollo.selectIcon(selection, SELECTED_FOCUSED);
278 mRollo.mState.save();
279 } else if (mLastSelection == SELECTION_HOME) {
280 mRollo.setHomeSelected(SELECTED_FOCUSED);
281 mRollo.mState.save();
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800282 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800283 }
284 }
285 }
286 }
287
288 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700289 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
290 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400291
292 if (!isVisible()) {
293 return;
294 }
295
Mike Cleron7d5d7462009-10-20 14:06:00 -0700296 if (gainFocus) {
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800297 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800298 gainFocus();
299 } else {
300 mShouldGainFocus = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700301 }
302 } else {
Joe Onorato8eea3912009-12-09 13:01:06 -0800303 if (mRollo != null) {
Mike Cleronb64b67a2009-11-08 14:56:25 -0800304 if (mArrowNavigation) {
305 // Clear selection when we lose focus
306 mRollo.clearSelectedIcon();
307 mRollo.setHomeSelected(SELECTED_NONE);
308 mRollo.mState.save();
309 mArrowNavigation = false;
310 }
311 } else {
312 mShouldGainFocus = false;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700313 }
314 }
315 }
316
Mike Cleronb64b67a2009-11-08 14:56:25 -0800317 private void gainFocus() {
318 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
319 // Select the first icon when we gain keyboard focus
320 mArrowNavigation = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800321 mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE,
Mike Cleronb64b67a2009-11-08 14:56:25 -0800322 SELECTED_FOCUSED);
323 mRollo.mState.save();
324 }
325 }
326
Mike Cleron7d5d7462009-10-20 14:06:00 -0700327 @Override
328 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700329
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800330 boolean handled = false;
Jason Samse26d9fc2009-11-12 14:00:43 -0800331
Joe Onorato859b3a72009-10-28 15:17:01 -0400332 if (!isVisible()) {
333 return false;
334 }
Joe Onoratoa13f5742009-11-02 17:15:19 -0500335 final int iconCount = mRollo.mState.iconCount;
336
Mike Cleron7d5d7462009-10-20 14:06:00 -0700337 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
338 if (mArrowNavigation) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500339 if (mLastSelection == SELECTION_HOME) {
340 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
341 mLauncher.closeAllApps(true);
342 } else {
343 int whichApp = mRollo.mState.selectedIconIndex;
344 if (whichApp >= 0) {
345 ApplicationInfo app = mAllAppsList.get(whichApp);
346 mLauncher.startActivitySafely(app.intent);
347 handled = true;
348 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700349 }
350 }
351 }
Jason Sams2e19c052009-10-20 18:19:55 -0700352
Joe Onorato478730f2009-11-16 18:54:43 -0800353 if (iconCount > 0) {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700354 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700355
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800356 int currentSelection = mRollo.mState.selectedIconIndex;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800357 int currentTopRow = Math.round(mPosX);
Jason Sams2e19c052009-10-20 18:19:55 -0700358
Mike Cleron7d5d7462009-10-20 14:06:00 -0700359 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500360 final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700361
Mike Cleron7d5d7462009-10-20 14:06:00 -0700362 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
Joe Onoratoa13f5742009-11-02 17:15:19 -0500363 final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE))
Mike Cleron7d5d7462009-10-20 14:06:00 -0700364 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700365
Mike Cleron7d5d7462009-10-20 14:06:00 -0700366 int newSelection = currentSelection;
367
368 switch (keyCode) {
369 case KeyEvent.KEYCODE_DPAD_UP:
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500370 if (mLastSelection == SELECTION_HOME) {
371 mRollo.setHomeSelected(SELECTED_NONE);
372 int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE;
373 if (lastRowCount == 0) {
374 lastRowCount = Defines.COLUMNS_PER_PAGE;
375 }
376 newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2);
377 if (newSelection >= iconCount) {
378 newSelection = iconCount-1;
379 }
380 int target = (newSelection / Defines.COLUMNS_PER_PAGE)
381 - (Defines.ROWS_PER_PAGE - 1);
382 if (target < 0) {
383 target = 0;
384 }
385 if (currentTopRow != target) {
386 mRollo.moveTo(target);
387 }
388 } else {
389 if (currentPageRow > 0) {
390 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
391 } else if (currentTopRow > 0) {
392 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
393 mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE);
Joe Onoratoaf5b4cb2009-12-08 17:51:22 -0800394 } else if (currentPageRow != 0) {
395 newSelection = currentTopRow * Defines.ROWS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500396 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700397 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800398 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700399 break;
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800400
Joe Onoratoa13f5742009-11-02 17:15:19 -0500401 case KeyEvent.KEYCODE_DPAD_DOWN: {
402 final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE
403 + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1);
404 final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500405 if (mLastSelection != SELECTION_HOME) {
406 if (currentRow < rowCount-1) {
407 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onorato478730f2009-11-16 18:54:43 -0800408 if (currentSelection < 0) {
409 newSelection = 0;
410 } else {
411 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
412 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500413 if (newSelection >= iconCount) {
414 // Go from D to G in this arrangement:
415 // A B C D
416 // E F G
417 newSelection = iconCount - 1;
418 }
419 if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) {
420 mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) -
421 Defines.ROWS_PER_PAGE + 1);
422 }
423 } else {
424 newSelection = -1;
425 mRollo.setHomeSelected(SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700426 }
427 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800428 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700429 break;
Joe Onoratoa13f5742009-11-02 17:15:19 -0500430 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700431 case KeyEvent.KEYCODE_DPAD_LEFT:
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800432 if (mLastSelection != SELECTION_HOME) {
433 if (currentPageCol > 0) {
434 newSelection = currentSelection - 1;
435 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700436 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800437 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700438 break;
439 case KeyEvent.KEYCODE_DPAD_RIGHT:
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800440 if (mLastSelection != SELECTION_HOME) {
441 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
442 (currentSelection < iconCount - 1)) {
443 newSelection = currentSelection + 1;
444 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700445 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800446 handled = true;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700447 break;
448 }
449 if (newSelection != currentSelection) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500450 mRollo.selectIcon(newSelection, SELECTED_FOCUSED);
Mike Cleron7d5d7462009-10-20 14:06:00 -0700451 mRollo.mState.save();
452 }
453 }
Mike Cleron4a5c1e12009-11-03 10:17:05 -0800454 return handled;
Joe Onorato93839052009-08-06 20:34:32 -0700455 }
456
Joe Onorato93839052009-08-06 20:34:32 -0700457 @Override
458 public boolean onTouchEvent(MotionEvent ev)
459 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700460 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700461
Joe Onorato7bb17492009-09-24 17:51:01 -0700462 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700463 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700464 }
465
Joe Onoratofb0ca672009-09-14 17:55:46 -0400466 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700467 return true;
468 }
469
470 super.onTouchEvent(ev);
471
Joe Onoratofb0ca672009-09-14 17:55:46 -0400472 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700473 int y = (int)ev.getY();
474
Joe Onoratobcbeab82009-10-01 21:45:43 -0700475 int action = ev.getAction();
476 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400477 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700478 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
479 mTouchTracking = TRACKING_HOME;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500480 mRollo.setHomeSelected(SELECTED_PRESSED);
Joe Onoratod63458b2009-10-15 21:19:09 -0700481 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700482 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700483 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700484 mTouchTracking = TRACKING_FLING;
485
486 mMotionDownRawX = (int)ev.getRawX();
487 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700488
Mike Cleron7d5d7462009-10-20 14:06:00 -0700489 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700490 mRollo.mState.newTouchDown = 1;
491
492 if (!mRollo.checkClickOK()) {
493 mRollo.clearSelectedIcon();
494 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700495 mDownIconIndex = mCurrentIconIndex
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800496 = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED);
Joe Onorato82ca5502009-10-15 16:59:23 -0700497 if (mDownIconIndex < 0) {
498 // if nothing was selected, no long press.
499 cancelLongPress();
500 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700501 }
502 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700503 mRollo.move();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800504 mVelocityTracker = VelocityTracker.obtain();
505 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700506 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700507 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400508 break;
509 case MotionEvent.ACTION_MOVE:
510 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700511 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500512 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]
513 ? SELECTED_PRESSED : SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700514 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700515 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700516 int rawX = (int)ev.getRawX();
517 int rawY = (int)ev.getRawY();
518 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700519 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700520
Joe Onorato82ca5502009-10-15 16:59:23 -0700521 if (!mStartedScrolling && slop < mSlop) {
522 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700523 // below, we get the right delta.
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800524 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX);
Joe Onorato82ca5502009-10-15 16:59:23 -0700525 if (mDownIconIndex != mCurrentIconIndex) {
526 // If a different icon is selected, don't allow it to be picked up.
527 // This handles off-axis dragging.
528 cancelLongPress();
529 mCurrentIconIndex = -1;
530 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700531 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700532 if (!mStartedScrolling) {
533 cancelLongPress();
534 mCurrentIconIndex = -1;
535 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700536 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700537 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700538 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700539
Joe Onoratobcbeab82009-10-01 21:45:43 -0700540 mStartedScrolling = true;
541 mRollo.clearSelectedIcon();
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800542 mVelocityTracker.addMovement(ev);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700543 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700544 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400545 }
546 break;
547 case MotionEvent.ACTION_UP:
548 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700549 if (mTouchTracking == TRACKING_HOME) {
550 if (action == MotionEvent.ACTION_UP) {
551 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400552 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700553 mLauncher.closeAllApps(true);
554 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500555 mRollo.setHomeSelected(SELECTED_NONE);
Joe Onoratod63458b2009-10-15 21:19:09 -0700556 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700557 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700558 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700559 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700560 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700561 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700562
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800563 mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
564 mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700565 mRollo.clearSelectedIcon();
566 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700567 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700568
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800569 if (mVelocityTracker != null) {
570 mVelocityTracker.recycle();
571 mVelocityTracker = null;
Joe Onorato539ed9d2009-10-02 10:22:14 -0700572 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700573 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700574 mTouchTracking = TRACKING_NONE;
575 break;
Joe Onorato93839052009-08-06 20:34:32 -0700576 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700577
578 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700579 }
580
Joe Onorato6665c0f2009-09-02 15:27:24 -0700581 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700582 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400583 return;
584 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700585 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
586 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400587 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700588 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700589 mLauncher.startActivitySafely(app.intent);
590 }
591 }
592
593 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700594 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400595 return true;
596 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700597 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
598 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
599 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700600
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800601 Bitmap bmp = app.iconBitmap;
Joe Onoratof5e4dd02010-01-14 11:16:55 -0500602 final int w = bmp.getWidth();
603 final int h = bmp.getHeight();
604
605 // We don't really have an accurate location to use. This will do.
606 int screenX = mMotionDownRawX - (w / 2);
607 int screenY = mMotionDownRawY - h;
608
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800609 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
610 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onoratof5e4dd02010-01-14 11:16:55 -0500611 mDragController.startDrag(bmp, screenX, screenY,
612 0, 0, w, h, this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700613
Joe Onorato7bb17492009-09-24 17:51:01 -0700614 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700615 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700616 return true;
617 }
618
Joe Onorato52a653f2009-11-11 14:52:11 -0800619 @Override
620 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
621 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) {
622 if (!isVisible()) {
623 return false;
624 }
625 String text = null;
626 int index;
627 int count = mAllAppsList.size() + 1; // +1 is home
628 int pos = -1;
629 switch (mLastSelection) {
630 case SELECTION_ICONS:
631 index = mRollo.mState.selectedIconIndex;
632 if (index >= 0) {
633 ApplicationInfo info = mAllAppsList.get(index);
634 if (info.title != null) {
635 text = info.title.toString();
636 pos = index;
637 }
638 }
639 break;
640 case SELECTION_HOME:
641 text = getContext().getString(R.string.all_apps_home_button_label);
642 pos = count;
643 break;
644 }
645 if (text != null) {
Joe Onorato52a653f2009-11-11 14:52:11 -0800646 event.setEnabled(true);
647 event.getText().add(text);
648 //event.setContentDescription(text);
649 event.setItemCount(count);
650 event.setCurrentItemIndex(pos);
651 }
652 }
653 return false;
654 }
655
Joe Onorato5162ea92009-09-03 09:39:42 -0700656 public void setDragController(DragController dragger) {
657 mDragController = dragger;
658 }
659
660 public void onDropCompleted(View target, boolean success) {
661 }
662
Joe Onorato4db52312009-10-06 11:17:43 -0700663 /**
Joe Onorato3a8820b2009-11-10 15:06:42 -0800664 * Zoom to the specifed level.
Joe Onorato4db52312009-10-06 11:17:43 -0700665 *
Joe Onorato3a8820b2009-11-10 15:06:42 -0800666 * @param zoom [0..1] 0 is hidden, 1 is open
Joe Onorato4db52312009-10-06 11:17:43 -0700667 */
Joe Onorato3a8820b2009-11-10 15:06:42 -0800668 public void zoom(float zoom, boolean animate) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400669 cancelLongPress();
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800670 mNextZoom = zoom;
671 mAnimateNextZoom = animate;
672 // if we do setZoom while we don't have a surface, we won't
673 // get the callbacks that actually set mZoom.
674 if (mRollo == null || !mHaveSurface) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800675 mZoomDirty = true;
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800676 mZoom = zoom;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800677 return;
Joe Onorato85a02a82009-09-08 12:34:22 -0700678 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800679 mRollo.setZoom(zoom, animate);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700680 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400681 }
682
683 public boolean isVisible() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800684 return mZoom > 0.001f;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800685 }
686
687 public boolean isOpaque() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800688 return mZoom > 0.999f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400689 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700690
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 public void setApps(ArrayList<ApplicationInfo> list) {
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800692 if (mRS == null) {
693 // We've been removed from the window. Don't bother with all this.
694 return;
695 }
696
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697 mAllAppsList = list;
698 if (mRollo != null) {
Joe Onorato3ecbd812009-12-11 13:38:54 -0800699 mRollo.setApps(list);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400701 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700702 }
703
Joe Onoratoa8138d52009-10-06 19:25:30 -0700704 public void addApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500705 if (mAllAppsList == null) {
706 // Not done loading yet. We'll find out about it later.
707 return;
708 }
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800709 if (mRS == null) {
710 // We've been removed from the window. Don't bother with all this.
711 return;
712 }
Joe Onorato2d804762009-11-05 16:02:32 -0500713
Joe Onoratoa8138d52009-10-06 19:25:30 -0700714 final int N = list.size();
Joe Onorato8eea3912009-12-09 13:01:06 -0800715 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700716 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
717 }
718
719 for (int i=0; i<N; i++) {
720 final ApplicationInfo item = list.get(i);
Joe Onoratob0c27f22009-12-01 16:19:38 -0800721 int index = Collections.binarySearch(mAllAppsList, item,
722 LauncherModel.APP_NAME_COMPARATOR);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700723 if (index < 0) {
724 index = -(index+1);
725 }
726 mAllAppsList.add(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -0800727 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700728 mRollo.addApp(index, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700729 }
730 }
731
Joe Onorato8eea3912009-12-09 13:01:06 -0800732 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700733 mRollo.saveAppsList();
734 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700735 }
736
Joe Onoratoa8138d52009-10-06 19:25:30 -0700737 public void removeApps(ArrayList<ApplicationInfo> list) {
Joe Onorato2d804762009-11-05 16:02:32 -0500738 if (mAllAppsList == null) {
739 // Not done loading yet. We'll find out about it later.
740 return;
741 }
742
Joe Onoratoa8138d52009-10-06 19:25:30 -0700743 final int N = list.size();
744 for (int i=0; i<N; i++) {
745 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400746 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700747 if (index >= 0) {
Joe Onoratoa276fc52009-12-08 17:02:02 -0800748 int ic = mRollo != null ? mRollo.mState.iconCount : 666;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700749 mAllAppsList.remove(index);
Joe Onorato8eea3912009-12-09 13:01:06 -0800750 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700751 mRollo.removeApp(index);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700752 }
753 } else {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800754 Log.w(TAG, "couldn't find a match for item \"" + item + "\"");
Joe Onoratoa8138d52009-10-06 19:25:30 -0700755 // Try to recover. This should keep us from crashing for now.
756 }
757 }
758
Joe Onorato8eea3912009-12-09 13:01:06 -0800759 if (mRollo != null) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700760 mRollo.saveAppsList();
761 }
762 }
763
764 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
765 // Just remove and add, because they may need to be re-sorted.
766 removeApps(list);
767 addApps(list);
768 }
769
Joe Onoratocb9f7982009-10-31 16:32:02 -0400770 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
771 ComponentName component = item.intent.getComponent();
772 final int N = list.size();
773 for (int i=0; i<N; i++) {
774 ApplicationInfo x = list.get(i);
775 if (x.intent.getComponent().equals(component)) {
776 return i;
777 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700778 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400779 return -1;
780 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700781
Joe Onoratoc567acb2009-08-31 14:34:43 -0700782 private static int countPages(int iconCount) {
783 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
784 int pages = iconCount / iconsPerPage;
785 if (pages*iconsPerPage != iconCount) {
786 pages++;
787 }
788 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400789 }
790
Joe Onoratocb75f362009-11-12 13:04:07 -0800791 class AAMessage extends RenderScript.RSMessage {
792 public void run() {
793 mPosX = ((float)mData[0]) / (1 << 16);
794 mVelocity = ((float)mData[1]) / (1 << 16);
795 mZoom = ((float)mData[2]) / (1 << 16);
796 mZoomDirty = false;
797 }
Joe Onoratocb75f362009-11-12 13:04:07 -0800798 }
799
Joe Onorato93839052009-08-06 20:34:32 -0700800 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700801
Joe Onorato1feb3a82009-08-08 22:32:00 -0700802 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700803 private int mWidth;
804 private int mHeight;
805
806 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700807 private Script mScript;
808 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700809 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700810 private Script.Invokable mInvokeFling;
811 private Script.Invokable mInvokeResetWAR;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800812 private Script.Invokable mInvokeSetZoom;
Jason Samsc1c521e2009-10-19 14:45:45 -0700813
Jason Samscd689e12009-09-29 15:28:22 -0700814 private ProgramStore mPSIcons;
Jason Samsc8514792009-10-29 14:27:29 -0700815 private ProgramFragment mPFTexMip;
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800816 private ProgramFragment mPFTexMipAlpha;
Jason Samsc8514792009-10-29 14:27:29 -0700817 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700818 private ProgramVertex mPV;
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800819 private ProgramVertex mPVCurve;
Jason Sams0aa71662009-10-02 18:43:18 -0700820 private SimpleMesh mMesh;
Jason Sams5612e432009-11-16 14:18:07 -0800821 private ProgramVertex.MatrixAllocation mPVA;
Joe Onorato93839052009-08-06 20:34:32 -0700822
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800823 private Allocation mUniformAlloc;
824
Joe Onoratod63458b2009-10-15 21:19:09 -0700825 private Allocation mHomeButtonNormal;
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500826 private Allocation mHomeButtonFocused;
Joe Onoratod63458b2009-10-15 21:19:09 -0700827 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700828
Joe Onoratobf15cb42009-08-07 14:33:40 -0700829 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700830 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700831 private Allocation mAllocIconIds;
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800832
833 private Allocation[] mLabels;
834 private int[] mLabelIds;
835 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700836 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700837
Joe Onorato6665c0f2009-09-02 15:27:24 -0700838 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700839 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700840
841 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400842 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700843
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700844 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700845 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700846
Jason Sams78aebd82009-09-15 13:06:59 -0700847 class BaseAlloc {
848 Allocation mAlloc;
849 Type mType;
850
851 void save() {
852 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700853 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700854 }
855
Jason Sams476339d2009-09-29 18:14:38 -0700856 private boolean checkClickOK() {
Joe Onorato68ba5ca2009-11-12 14:23:43 -0800857 return (Math.abs(mVelocity) < 0.4f) &&
858 (Math.abs(mPosX - Math.round(mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700859 }
860
Jason Sams78aebd82009-09-15 13:06:59 -0700861 class Params extends BaseAlloc {
862 Params() {
863 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
864 mAlloc = Allocation.createTyped(mRS, mType);
865 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700866 }
Jason Sams78aebd82009-09-15 13:06:59 -0700867 public int bubbleWidth;
868 public int bubbleHeight;
869 public int bubbleBitmapWidth;
870 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700871
Joe Onoratobcbeab82009-10-01 21:45:43 -0700872 public int homeButtonWidth;
873 public int homeButtonHeight;
874 public int homeButtonTextureWidth;
875 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700876 }
877
878 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700879 public float newPositionX;
880 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700881 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700882 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700883 public int selectedIconIndex = -1;
884 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700885 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700886 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700887 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700888
889 State() {
890 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
891 mAlloc = Allocation.createTyped(mRS, mType);
892 save();
893 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700894 }
895
896 public RolloRS() {
897 }
898
899 public void init(Resources res, int width, int height) {
900 mRes = res;
901 mWidth = width;
902 mHeight = height;
Jason Samscd689e12009-09-29 15:28:22 -0700903 initProgramVertex();
904 initProgramFragment();
905 initProgramStore();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700906 initGl();
907 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700908 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700909 initRs();
910 }
911
Jason Sams0aa71662009-10-02 18:43:18 -0700912 public void initMesh() {
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800913 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 2, 0);
Jason Sams0aa71662009-10-02 18:43:18 -0700914
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800915 for (int ct=0; ct < 16; ct++) {
916 float pos = (1.f / (16.f - 1)) * ct;
917 tm.addVertex(0.0f, pos);
918 tm.addVertex(1.0f, pos);
Jason Samsd8152b92009-10-13 17:19:10 -0700919 }
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800920 for (int ct=0; ct < (16 * 2 - 2); ct+= 2) {
Jason Samsd8152b92009-10-13 17:19:10 -0700921 tm.addTriangle(ct, ct+1, ct+2);
922 tm.addTriangle(ct+1, ct+3, ct+2);
923 }
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800924 mMesh = tm.create();
925 mMesh.setName("SMCell");
Jason Samsd8152b92009-10-13 17:19:10 -0700926 }
927
Jason Sams5612e432009-11-16 14:18:07 -0800928 void resize(int w, int h) {
929 mPVA.setupProjectionNormalized(w, h);
930 mWidth = w;
931 mHeight = h;
932 }
933
Jason Samscd689e12009-09-29 15:28:22 -0700934 private void initProgramVertex() {
Jason Sams5612e432009-11-16 14:18:07 -0800935 mPVA = new ProgramVertex.MatrixAllocation(mRS);
936 resize(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700937
938 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700939 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700940 mPV = pvb.create();
941 mPV.setName("PV");
Jason Sams5612e432009-11-16 14:18:07 -0800942 mPV.bindAllocation(mPVA);
Joe Onorato93839052009-08-06 20:34:32 -0700943
Joe Onorato75fd1ec2010-02-18 15:31:52 -0800944 Element.Builder eb = new Element.Builder(mRS);
945 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "ImgSize");
946 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "Position");
947 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 2), "BendPos");
948 eb.add(Element.createVector(mRS, Element.DataType.FLOAT_32, 4), "ScaleOffset");
949 Element e = eb.create();
950
951 mUniformAlloc = Allocation.createSized(mRS, e, 1);
952
953 initMesh();
954 ProgramVertex.ShaderBuilder sb = new ProgramVertex.ShaderBuilder(mRS);
955 String t = new String("void main() {\n" +
956 // Animation
957 " float ani = UNI_Position.z;\n" +
958
959 " float bendY1 = UNI_BendPos.x;\n" +
960 " float bendY2 = UNI_BendPos.y;\n" +
961 " float bendAngle = 47.0 * (3.14 / 180.0);\n" +
962 " float bendDistance = bendY1 * 0.4;\n" +
963 " float distanceDimLevel = 0.6;\n" +
964
965 " float bendStep = (bendAngle / bendDistance) * (bendAngle * 0.5);\n" +
966 " float aDy = cos(bendAngle);\n" +
967 " float aDz = sin(bendAngle);\n" +
968
969 " float scale = (2.0 / 480.0);\n" +
970 " float x = UNI_Position.x + UNI_ImgSize.x * (1.0 - ani) * (ATTRIB_position.x - 0.5);\n" +
971 " float ys= UNI_Position.y + UNI_ImgSize.y * (1.0 - ani) * ATTRIB_position.y;\n" +
972 " float y = 0.0;\n" +
973 " float z = 0.0;\n" +
974 " float lum = 1.0;\n" +
975
976 " float cv = min(ys, bendY1 - bendDistance) - (bendY1 - bendDistance);\n" +
977 " y += cv * aDy;\n" +
978 " z += -cv * aDz;\n" +
979 " cv = clamp(ys, bendY1 - bendDistance, bendY1) - bendY1;\n" + // curve range
980 " lum += cv / bendDistance * distanceDimLevel;\n" +
981 " y += cv * cos(cv * bendStep);\n" +
982 " z += cv * sin(cv * bendStep);\n" +
983
984 " cv = max(ys, bendY2 + bendDistance) - (bendY2 + bendDistance);\n" +
985 " y += cv * aDy;\n" +
986 " z += cv * aDz;\n" +
987 " cv = clamp(ys, bendY2, bendY2 + bendDistance) - bendY2;\n" +
988 " lum -= cv / bendDistance * distanceDimLevel;\n" +
989 " y += cv * cos(cv * bendStep);\n" +
990 " z += cv * sin(cv * bendStep);\n" +
991
992 " y += clamp(ys, bendY1, bendY2);\n" +
993
994 " vec4 pos;\n" +
995 " pos.x = (x + UNI_ScaleOffset.z) * UNI_ScaleOffset.x;\n" +
996 " pos.y = (y + UNI_ScaleOffset.w) * UNI_ScaleOffset.x;\n" +
997 " pos.z = z * UNI_ScaleOffset.x;\n" +
998 " pos.w = 1.0;\n" +
999
1000 " pos.x *= 1.0 + ani * 4.0;\n" +
1001 " pos.y *= 1.0 + ani * 4.0;\n" +
1002 " pos.z -= ani * 1.5;\n" +
1003 " lum *= 1.0 - ani;\n" +
1004
1005 " gl_Position = UNI_MVP * pos;\n" +
1006 " varColor.rgba = vec4(lum, lum, lum, 1.0);\n" +
1007 " varTex0.xy = ATTRIB_position;\n" +
1008 " varTex0.y = 1.0 - varTex0.y;\n" +
1009 " varTex0.zw = vec2(0.0, 0.0);\n" +
1010 "}\n");
1011 sb.setShader(t);
1012 sb.addConstant(mUniformAlloc.getType());
1013 sb.addInput(mMesh.getVertexType(0).getElement());
1014 mPVCurve = sb.create();
1015 mPVCurve.setName("PVCurve");
1016 mPVCurve.bindAllocation(mPVA);
1017 mPVCurve.bindConstants(mUniformAlloc, 1);
Joe Onorato93839052009-08-06 20:34:32 -07001018
1019 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -07001020 }
Joe Onorato93839052009-08-06 20:34:32 -07001021
Jason Samscd689e12009-09-29 15:28:22 -07001022 private void initProgramFragment() {
1023 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -07001024 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001025 sb.setMag(Sampler.Value.NEAREST);
Jason Samscd689e12009-09-29 15:28:22 -07001026 sb.setWrapS(Sampler.Value.CLAMP);
1027 sb.setWrapT(Sampler.Value.CLAMP);
1028 Sampler linear = sb.create();
1029
1030 sb.setMin(Sampler.Value.NEAREST);
1031 sb.setMag(Sampler.Value.NEAREST);
1032 Sampler nearest = sb.create();
1033
Jason Sams52ba5b02010-02-11 18:15:48 -08001034 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS);
Jason Sams52ba5b02010-02-11 18:15:48 -08001035 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1036 ProgramFragment.Builder.Format.RGBA, 0);
Jason Samsc8514792009-10-29 14:27:29 -07001037 mPFTexMip = bf.create();
1038 mPFTexMip.setName("PFTexMip");
1039 mPFTexMip.bindSampler(linear, 0);
1040
1041 mPFTexNearest = bf.create();
1042 mPFTexNearest.setName("PFTexNearest");
1043 mPFTexNearest.bindSampler(nearest, 0);
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001044
1045 bf.setTexture(ProgramFragment.Builder.EnvMode.MODULATE,
1046 ProgramFragment.Builder.Format.ALPHA, 0);
1047 mPFTexMipAlpha = bf.create();
1048 mPFTexMipAlpha.setName("PFTexMipAlpha");
1049 mPFTexMipAlpha.bindSampler(linear, 0);
1050
Jason Samscd689e12009-09-29 15:28:22 -07001051 }
1052
1053 private void initProgramStore() {
1054 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
1055 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -07001056 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -07001057 bs.setDitherEnable(true);
1058 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
1059 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
1060 mPSIcons = bs.create();
1061 mPSIcons.setName("PSIcons");
Jason Samscd689e12009-09-29 15:28:22 -07001062 }
1063
1064 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001065 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -07001066 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -07001067 }
Jason Sams78aebd82009-09-15 13:06:59 -07001068
Joe Onorato1feb3a82009-08-08 22:32:00 -07001069 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -07001070 mParams = new Params();
1071 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001072
Joe Onoratobf15cb42009-08-07 14:33:40 -07001073 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -07001074
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001075 mParams.bubbleWidth = bubble.getBubbleWidth();
1076 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
1077 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
1078 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -07001079
Joe Onoratod63458b2009-10-15 21:19:09 -07001080 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
1081 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
1082 mHomeButtonNormal.uploadToTexture(0);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001083 mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes,
1084 R.drawable.home_button_focused, Element.RGBA_8888(mRS), false);
1085 mHomeButtonFocused.uploadToTexture(0);
Joe Onoratod63458b2009-10-15 21:19:09 -07001086 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
1087 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
1088 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001089 mParams.homeButtonWidth = 76;
1090 mParams.homeButtonHeight = 68;
1091 mParams.homeButtonTextureWidth = 128;
1092 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -07001093
Joe Onoratod63458b2009-10-15 21:19:09 -07001094 mState.homeButtonId = mHomeButtonNormal.getID();
1095
Joe Onorato1feb3a82009-08-08 22:32:00 -07001096 mParams.save();
1097 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001098
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001099 mSelectionBitmap = Bitmap.createBitmap(Defines.SELECTION_TEXTURE_WIDTH_PX,
1100 Defines.SELECTION_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001101 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001102
Joe Onorato9c1289c2009-08-17 11:03:03 -04001103 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -07001104 }
1105
Jason Sams37e7c2b2009-10-19 12:55:43 -07001106 private void initScript(int id) {
1107 }
1108
1109 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -07001110 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001111 sb.setScript(mRes, R.raw.allapps);
Joe Onorato93839052009-08-06 20:34:32 -07001112 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -07001113 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -07001114 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
1115 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001116 sb.setType(mUniformAlloc.getType(), "vpConstants", Defines.ALLOC_VP_CONSTANTS);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001117 mInvokeMove = sb.addInvokable("move");
1118 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -07001119 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001120 mInvokeResetWAR = sb.addInvokable("resetHWWar");
Joe Onorato3a8820b2009-11-10 15:06:42 -08001121 mInvokeSetZoom = sb.addInvokable("setZoom");
Jason Sams37e7c2b2009-10-19 12:55:43 -07001122 mScript = sb.create();
1123 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1124 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
1125 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
1126 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001127 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
1128 mScript.bindAllocation(mUniformAlloc, Defines.ALLOC_VP_CONSTANTS);
Joe Onorato93839052009-08-06 20:34:32 -07001129
Jason Sams37e7c2b2009-10-19 12:55:43 -07001130 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -07001131 }
Joe Onorato93839052009-08-06 20:34:32 -07001132
Jason Sams20df7c72009-11-05 12:30:24 -08001133 void dirtyCheck() {
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001134 if (mZoomDirty) {
1135 setZoom(mNextZoom, mAnimateNextZoom);
Jason Sams20df7c72009-11-05 12:30:24 -08001136 }
1137 }
1138
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 private void setApps(ArrayList<ApplicationInfo> list) {
1140 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001141 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001142 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001143 allocCount = 1;
1144 }
1145
Joe Onorato9c1289c2009-08-17 11:03:03 -04001146 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001147 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -07001148 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001149
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001150 mLabels = new Allocation[count];
1151 mLabelIds = new int[allocCount];
1152 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
1153
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001154 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001155
Joe Onorato9c1289c2009-08-17 11:03:03 -04001156 mState.iconCount = count;
Joe Onorato8eea3912009-12-09 13:01:06 -08001157 for (int i=0; i < mState.iconCount; i++) {
1158 createAppIconAllocations(i, list.get(i));
Joe Onorato3ecbd812009-12-11 13:38:54 -08001159 }
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001160 for (int i=0; i < mState.iconCount; i++) {
1161 uploadAppIcon(i, list.get(i));
Joe Onorato8eea3912009-12-09 13:01:06 -08001162 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001163 saveAppsList();
1164 }
1165
Joe Onorato3a8820b2009-11-10 15:06:42 -08001166 private void setZoom(float zoom, boolean animate) {
1167 mRollo.clearSelectedIcon();
1168 mRollo.setHomeSelected(SELECTED_NONE);
1169 if (zoom > 0.001f) {
1170 mRollo.mState.zoomTarget = zoom;
1171 } else {
1172 mRollo.mState.zoomTarget = 0;
1173 }
1174 mRollo.mState.save();
1175 if (!animate) {
1176 mRollo.mInvokeSetZoom.execute();
1177 }
1178 }
1179
Joe Onorato8eea3912009-12-09 13:01:06 -08001180 private void createAppIconAllocations(int index, ApplicationInfo item) {
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001181 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
1182 Element.RGBA_8888(mRS), true);
1183 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
1184 Element.A_8(mRS), true);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001185 mIconIds[index] = mIcons[index].getID();
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001186 mLabelIds[index] = mLabels[index].getID();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001187 }
1188
Joe Onorato8eea3912009-12-09 13:01:06 -08001189 private void uploadAppIcon(int index, ApplicationInfo item) {
1190 if (mIconIds[index] != mIcons[index].getID()) {
1191 throw new IllegalStateException("uploadAppIcon index=" + index
1192 + " mIcons[index].getID=" + mIcons[index].getID()
1193 + " mIconsIds[index]=" + mIconIds[index]
1194 + " item=" + item);
1195 }
1196 mIcons[index].uploadToTexture(0);
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001197 mLabels[index].uploadToTexture(0);
Joe Onorato8eea3912009-12-09 13:01:06 -08001198 }
1199
Joe Onoratoa8138d52009-10-06 19:25:30 -07001200 /**
1201 * Puts the empty spaces at the end. Updates mState.iconCount. You must
1202 * fill in the values and call saveAppsList().
1203 */
1204 private void reallocAppsList(int count) {
1205 Allocation[] icons = new Allocation[count];
1206 int[] iconIds = new int[count];
1207 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1208
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001209 Allocation[] labels = new Allocation[count];
1210 int[] labelIds = new int[count];
1211 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
1212
Joe Onoratobf173f12009-12-08 13:29:38 -08001213 final int oldCount = mRollo.mState.iconCount;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001214
1215 System.arraycopy(mIcons, 0, icons, 0, oldCount);
1216 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001217 System.arraycopy(mLabels, 0, labels, 0, oldCount);
1218 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001219
1220 mIcons = icons;
1221 mIconIds = iconIds;
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001222 mLabels = labels;
1223 mLabelIds = labelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001224 }
1225
1226 /**
1227 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
1228 */
1229 private void addApp(int index, ApplicationInfo item) {
1230 final int count = mState.iconCount - index;
1231 final int dest = index + 1;
1232
1233 System.arraycopy(mIcons, index, mIcons, dest, count);
1234 System.arraycopy(mIconIds, index, mIconIds, dest, count);
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001235 System.arraycopy(mLabels, index, mLabels, dest, count);
1236 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001237
Joe Onorato8eea3912009-12-09 13:01:06 -08001238 createAppIconAllocations(index, item);
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001239 uploadAppIcon(index, item);
Joe Onorato8eea3912009-12-09 13:01:06 -08001240 mRollo.mState.iconCount++;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001241 }
1242
1243 /**
1244 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1245 */
1246 private void removeApp(int index) {
1247 final int count = mState.iconCount - index - 1;
1248 final int src = index + 1;
1249
1250 System.arraycopy(mIcons, src, mIcons, index, count);
1251 System.arraycopy(mIconIds, src, mIconIds, index, count);
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001252 System.arraycopy(mLabels, src, mLabels, index, count);
1253 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
Joe Onoratoa8138d52009-10-06 19:25:30 -07001254
Joe Onoratoa276fc52009-12-08 17:02:02 -08001255 mRollo.mState.iconCount--;
Joe Onorato8eea3912009-12-09 13:01:06 -08001256 final int last = mState.iconCount;
Joe Onoratoa276fc52009-12-08 17:02:02 -08001257
Joe Onoratoa8138d52009-10-06 19:25:30 -07001258 mIcons[last] = null;
1259 mIconIds[last] = 0;
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001260 mLabels[last] = null;
1261 mLabelIds[last] = 0;
Joe Onoratoa8138d52009-10-06 19:25:30 -07001262 }
1263
1264 /**
1265 * Send the apps list structures to RS.
1266 */
1267 private void saveAppsList() {
Jason Sams37e7c2b2009-10-19 12:55:43 -07001268 if (mScript != null) { // this happens when we init it
Joe Onoratobf5350d2010-02-22 14:46:10 -08001269 mRS.contextBindRootScript(null);
1270
1271 mAllocIconIds.data(mIconIds);
1272 mAllocLabelIds.data(mLabelIds);
1273
Jason Sams37e7c2b2009-10-19 12:55:43 -07001274 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001275 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001276
Joe Onoratobf5350d2010-02-22 14:46:10 -08001277 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001278
Joe Onoratobf5350d2010-02-22 14:46:10 -08001279 // Note: mScript may be null if we haven't initialized it yet.
1280 // In that case, this is a no-op.
1281 if (mInvokeResetWAR != null) {
1282 mInvokeResetWAR.execute();
1283 }
1284
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001285 mRS.contextBindRootScript(mScript);
1286 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001287 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001288
1289 void initTouchState() {
1290 int width = getWidth();
1291 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001292 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1293 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001294
Jason Sams37e7c2b2009-10-19 12:55:43 -07001295 int centerY = (height / 2);
1296 mTouchYBorders[0] = centerY - (cellHeight * 2);
1297 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001298 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001299 mTouchYBorders[3] = centerY + cellHeight;
1300 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001301
Joe Onorato6665c0f2009-09-02 15:27:24 -07001302 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001303 mTouchXBorders[0] = 0;
1304 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001305 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001306 mTouchXBorders[3] = centerX + (width / 4);
1307 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001308 }
1309
Joe Onorato664457d2009-10-28 16:30:34 -04001310 void fling() {
1311 mInvokeFling.execute();
1312 }
1313
1314 void move() {
1315 mInvokeMove.execute();
1316 }
1317
1318 void moveTo(float row) {
1319 mState.targetPos = row;
1320 mState.save();
1321 mInvokeMoveTo.execute();
1322 }
1323
Jason Sams37e7c2b2009-10-19 12:55:43 -07001324 int chooseTappedIcon(int x, int y, float pos) {
1325 // Adjust for scroll position if not zero.
1326 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001327
Joe Onorato6665c0f2009-09-02 15:27:24 -07001328 int col = -1;
1329 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001330 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1331 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1332 col = i;
1333 break;
1334 }
1335 }
1336 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1337 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1338 row = i;
1339 break;
1340 }
1341 }
1342
1343 if (row < 0 || col < 0) {
1344 return -1;
1345 }
1346
Joe Onorato664457d2009-10-28 16:30:34 -04001347 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001348 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001349
Joe Onorato664457d2009-10-28 16:30:34 -04001350 if (index >= mState.iconCount) {
1351 return -1;
1352 } else {
1353 return index;
1354 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001355 }
1356
Joe Onorato6665c0f2009-09-02 15:27:24 -07001357 /**
1358 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001359 *
1360 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001361 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001362 int selectIcon(int x, int y, float pos, int pressed) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001363 final int index = chooseTappedIcon(x, y, pos);
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001364 selectIcon(index, pressed);
Joe Onorato82ca5502009-10-15 16:59:23 -07001365 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001366 }
1367
Joe Onoratoc61cff92009-11-08 11:54:39 -05001368 /**
1369 * Select the icon at the given index.
1370 *
1371 * @param index The index.
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001372 * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED
Joe Onoratoc61cff92009-11-08 11:54:39 -05001373 */
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001374 void selectIcon(int index, int pressed) {
Joe Onorato2d804762009-11-05 16:02:32 -05001375 if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001376 mState.selectedIconIndex = -1;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001377 if (mLastSelection == SELECTION_ICONS) {
1378 mLastSelection = SELECTION_NONE;
1379 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001380 } else {
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001381 if (pressed == SELECTED_FOCUSED) {
1382 mLastSelection = SELECTION_ICONS;
1383 }
1384
Joe Onorato52a653f2009-11-11 14:52:11 -08001385 int prev = mState.selectedIconIndex;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001386 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001387
Joe Onorato52a653f2009-11-11 14:52:11 -08001388 ApplicationInfo info = mAllAppsList.get(index);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001389 Bitmap selectionBitmap = mSelectionBitmap;
1390
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001391 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001392 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
Joe Onorato52a653f2009-11-11 14:52:11 -08001393 pressed == SELECTED_PRESSED, info.iconBitmap);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001394
1395 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001396 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001397 mSelectedIcon.uploadToTexture(0);
1398 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001399
1400 if (prev != index) {
1401 if (info.title != null && info.title.length() > 0) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001402 //setContentDescription(info.title);
1403 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1404 }
1405 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001406 }
1407 }
1408
1409 /**
1410 * You need to call save() on mState on your own after calling this.
1411 */
1412 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001413 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001414 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001415
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001416 void setHomeSelected(int mode) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001417 final int prev = mLastSelection;
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001418 switch (mode) {
1419 case SELECTED_NONE:
Joe Onoratod63458b2009-10-15 21:19:09 -07001420 mState.homeButtonId = mHomeButtonNormal.getID();
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001421 break;
1422 case SELECTED_FOCUSED:
1423 mLastSelection = SELECTION_HOME;
1424 mState.homeButtonId = mHomeButtonFocused.getID();
Joe Onorato52a653f2009-11-11 14:52:11 -08001425 if (prev != SELECTION_HOME) {
Joe Onorato52a653f2009-11-11 14:52:11 -08001426 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
1427 }
Joe Onoratoeb8325a2009-11-08 13:20:30 -05001428 break;
1429 case SELECTED_PRESSED:
1430 mState.homeButtonId = mHomeButtonPressed.getID();
1431 break;
Joe Onoratod63458b2009-10-15 21:19:09 -07001432 }
1433 }
Joe Onoratobe386092009-11-17 17:32:16 -08001434
1435 public void dumpState() {
1436 Log.d(TAG, "mRollo.mWidth=" + mWidth);
1437 Log.d(TAG, "mRollo.mHeight=" + mHeight);
1438 Log.d(TAG, "mRollo.mIcons=" + mIcons);
1439 if (mIcons != null) {
1440 Log.d(TAG, "mRollo.mIcons.length=" + mIcons.length);
1441 }
1442 if (mIconIds != null) {
1443 Log.d(TAG, "mRollo.mIconIds.length=" + mIconIds.length);
1444 }
1445 Log.d(TAG, "mRollo.mIconIds=" + Arrays.toString(mIconIds));
Joe Onorato75fd1ec2010-02-18 15:31:52 -08001446 if (mLabelIds != null) {
1447 Log.d(TAG, "mRollo.mLabelIds.length=" + mLabelIds.length);
1448 }
1449 Log.d(TAG, "mRollo.mLabelIds=" + Arrays.toString(mLabelIds));
Joe Onoratobe386092009-11-17 17:32:16 -08001450 Log.d(TAG, "mRollo.mTouchXBorders=" + Arrays.toString(mTouchXBorders));
1451 Log.d(TAG, "mRollo.mTouchYBorders=" + Arrays.toString(mTouchYBorders));
Joe Onoratobe386092009-11-17 17:32:16 -08001452 Log.d(TAG, "mRollo.mState.newPositionX=" + mState.newPositionX);
1453 Log.d(TAG, "mRollo.mState.newTouchDown=" + mState.newTouchDown);
1454 Log.d(TAG, "mRollo.mState.flingVelocity=" + mState.flingVelocity);
1455 Log.d(TAG, "mRollo.mState.iconCount=" + mState.iconCount);
1456 Log.d(TAG, "mRollo.mState.selectedIconIndex=" + mState.selectedIconIndex);
1457 Log.d(TAG, "mRollo.mState.selectedIconTexture=" + mState.selectedIconTexture);
1458 Log.d(TAG, "mRollo.mState.zoomTarget=" + mState.zoomTarget);
1459 Log.d(TAG, "mRollo.mState.homeButtonId=" + mState.homeButtonId);
1460 Log.d(TAG, "mRollo.mState.targetPos=" + mState.targetPos);
1461 Log.d(TAG, "mRollo.mParams.bubbleWidth=" + mParams.bubbleWidth);
1462 Log.d(TAG, "mRollo.mParams.bubbleHeight=" + mParams.bubbleHeight);
1463 Log.d(TAG, "mRollo.mParams.bubbleBitmapWidth=" + mParams.bubbleBitmapWidth);
1464 Log.d(TAG, "mRollo.mParams.bubbleBitmapHeight=" + mParams.bubbleBitmapHeight);
1465 Log.d(TAG, "mRollo.mParams.homeButtonWidth=" + mParams.homeButtonWidth);
1466 Log.d(TAG, "mRollo.mParams.homeButtonHeight=" + mParams.homeButtonHeight);
1467 Log.d(TAG, "mRollo.mParams.homeButtonTextureWidth=" + mParams.homeButtonTextureWidth);
1468 Log.d(TAG, "mRollo.mParams.homeButtonTextureHeight=" + mParams.homeButtonTextureHeight);
1469 }
1470 }
1471
1472 public void dumpState() {
1473 Log.d(TAG, "mRS=" + mRS);
1474 Log.d(TAG, "mRollo=" + mRollo);
1475 ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList);
1476 Log.d(TAG, "mArrowNavigation=" + mArrowNavigation);
1477 Log.d(TAG, "mStartedScrolling=" + mStartedScrolling);
1478 Log.d(TAG, "mLastSelection=" + mLastSelection);
1479 Log.d(TAG, "mLastSelectedIcon=" + mLastSelectedIcon);
1480 Log.d(TAG, "mVelocityTracker=" + mVelocityTracker);
1481 Log.d(TAG, "mTouchTracking=" + mTouchTracking);
1482 Log.d(TAG, "mShouldGainFocus=" + mShouldGainFocus);
1483 Log.d(TAG, "mZoomDirty=" + mZoomDirty);
1484 Log.d(TAG, "mAnimateNextZoom=" + mAnimateNextZoom);
1485 Log.d(TAG, "mZoom=" + mZoom);
1486 Log.d(TAG, "mPosX=" + mPosX);
1487 Log.d(TAG, "mVelocity=" + mVelocity);
1488 Log.d(TAG, "mMessageProc=" + mMessageProc);
1489 if (mRollo != null) {
1490 mRollo.dumpState();
1491 }
1492 if (mRS != null) {
1493 mRS.contextDump(0);
1494 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001495 }
Joe Onorato93839052009-08-06 20:34:32 -07001496}
1497
1498