blob: 9c73a01be16aeda987460a60db4ba239e03625d9 [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
19import java.io.Writer;
20import java.util.ArrayList;
21import java.util.concurrent.Semaphore;
22import java.lang.Float;
Joe Onoratoa8138d52009-10-06 19:25:30 -070023import java.util.Collections;
24import java.util.Comparator;
Joe Onorato93839052009-08-06 20:34:32 -070025
26import android.renderscript.RSSurfaceView;
27import android.renderscript.RenderScript;
28
29import android.renderscript.RenderScript;
30import android.renderscript.ProgramVertex;
31import android.renderscript.Element;
32import android.renderscript.Allocation;
Jason Sams78aebd82009-09-15 13:06:59 -070033import android.renderscript.Type;
Joe Onorato93839052009-08-06 20:34:32 -070034import android.renderscript.Script;
35import android.renderscript.ScriptC;
36import android.renderscript.ProgramFragment;
37import android.renderscript.ProgramStore;
38import android.renderscript.Sampler;
Jason Sams0aa71662009-10-02 18:43:18 -070039import android.renderscript.SimpleMesh;
Joe Onorato93839052009-08-06 20:34:32 -070040
41import android.content.Context;
42import android.content.res.Resources;
Joe Onorato7c312c12009-08-13 21:36:53 -070043import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070044import android.graphics.Bitmap;
45import android.graphics.BitmapFactory;
46import android.graphics.Canvas;
47import android.graphics.Paint;
Mike Cleron7d5d7462009-10-20 14:06:00 -070048import android.graphics.Rect;
Joe Onorato93839052009-08-06 20:34:32 -070049import android.graphics.drawable.BitmapDrawable;
50import android.graphics.drawable.Drawable;
Joe Onorato93839052009-08-06 20:34:32 -070051import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070052import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070053import android.util.AttributeSet;
54import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070055import android.view.KeyEvent;
56import android.view.MotionEvent;
Joe Onorato93839052009-08-06 20:34:32 -070057import android.view.Surface;
58import android.view.SurfaceHolder;
59import android.view.SurfaceView;
Joe Onorato6665c0f2009-09-02 15:27:24 -070060import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070061import android.view.VelocityTracker;
62import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070063import android.graphics.PixelFormat;
64
65
Joe Onorato6665c0f2009-09-02 15:27:24 -070066public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070067 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040068 private static final String TAG = "Launcher.AllAppsView";
69
Joe Onoratofb0ca672009-09-14 17:55:46 -040070 /** Bit for mLocks for when there are icons being loaded. */
71 private static final int LOCK_ICONS_PENDING = 1;
72
Joe Onorato68ffd102009-10-15 17:59:43 -070073 private static final int TRACKING_NONE = 0;
74 private static final int TRACKING_FLING = 1;
75 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070076
Joe Onorato6665c0f2009-09-02 15:27:24 -070077 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070078 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040079
80 /** When this is 0, modifications are allowed, when it's not, they're not.
81 * TODO: What about scrolling? */
82 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070083
Joe Onorato82ca5502009-10-15 16:59:23 -070084 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070085 private int mMaxFlingVelocity;
86
Joe Onoratobcbeab82009-10-01 21:45:43 -070087 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070088 private RenderScript mRS;
89 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 private ArrayList<ApplicationInfo> mAllAppsList;
Mike Cleron7d5d7462009-10-20 14:06:00 -070091
92 /**
93 * True when we are using arrow keys or trackball to drive navigation
94 */
95 private boolean mArrowNavigation = false;
96
Joe Onoratoc567acb2009-08-31 14:34:43 -070097 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -070098 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -070099 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700100 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700101 private int mMotionDownRawX;
102 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700103 private int mDownIconIndex = -1;
104 private int mCurrentIconIndex = -1;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700105 private int mHomeButtonTop;
Jason Sams86c87ed2009-09-18 13:55:55 -0700106 private long mTouchTime;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700107
Joe Onorato6665c0f2009-09-02 15:27:24 -0700108 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700109 public static final int ALLOC_PARAMS = 0;
110 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700111 public static final int ALLOC_ICON_IDS = 3;
112 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700113
114 public static final int COLUMNS_PER_PAGE = 4;
115 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700116
Joe Onorato6665c0f2009-09-02 15:27:24 -0700117 public static final int ICON_WIDTH_PX = 64;
118 public static final int ICON_TEXTURE_WIDTH_PX = 128;
119
120 public static final int ICON_HEIGHT_PX = 64;
121 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700122
123 public int SCREEN_WIDTH_PX;
124 public int SCREEN_HEIGHT_PX;
125
Joe Onoratobcbeab82009-10-01 21:45:43 -0700126 public void recompute(int w, int h) {
127 SCREEN_WIDTH_PX = 480;
128 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700129 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700130 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700131
132 public AllAppsView(Context context, AttributeSet attrs) {
133 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700134 setFocusable(true);
135 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700136 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700137 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700138 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
139
Joe Onorato6665c0f2009-09-02 15:27:24 -0700140 setOnClickListener(this);
141 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700142 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700143 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700144 }
145
Joe Onorato7c312c12009-08-13 21:36:53 -0700146 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
147 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700148 }
149
Joe Onorato6665c0f2009-09-02 15:27:24 -0700150 public void setLauncher(Launcher launcher) {
151 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700152 }
153
Joe Onorato1feb3a82009-08-08 22:32:00 -0700154 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700155 public void surfaceDestroyed(SurfaceHolder holder) {
156 super.surfaceDestroyed(holder);
157
158 destroyRenderScript();
159 mRS = null;
160 mRollo = null;
161 }
162
163 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700164 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
165 super.surfaceChanged(holder, format, w, h);
166
Joe Onorato6665c0f2009-09-02 15:27:24 -0700167 long startTime = SystemClock.uptimeMillis();
168
Jason Sams05de32a2009-09-27 14:01:40 -0700169 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700170 mRollo = new RolloRS();
171 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400172 if (mAllAppsList != null) {
173 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700174 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700176
177 Resources res = getContext().getResources();
178 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700179 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700180
181 long endTime = SystemClock.uptimeMillis();
182 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700183 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700184
Joe Onorato93839052009-08-06 20:34:32 -0700185
186 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700187 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
188 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
189 if (gainFocus) {
190 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
191 // Select the first icon when we gain keyboard focus
192 mArrowNavigation = true;
193 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE);
194 mRollo.mState.save();
195 }
196 } else {
197 if (mArrowNavigation) {
198 // Clear selection when we lose focus
199 mRollo.clearSelectedIcon();
200 mRollo.mState.save();
201 mArrowNavigation = false;
202 }
203 }
204 }
205
206 @Override
207 public boolean onKeyDown(int keyCode, KeyEvent event) {
208
209 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
210 if (mArrowNavigation) {
211 int whichApp = mRollo.mState.selectedIconIndex;
212 if (whichApp >= 0) {
213 ApplicationInfo app = mAllAppsList.get(whichApp);
214 mLauncher.startActivitySafely(app.intent);
215 }
216 }
217 }
218
219 if (mArrowNavigation && mRollo.mState.iconCount > 0) {
220 mArrowNavigation = true;
221
222 int currentSelection = mRollo.mState.selectedIconIndex;
223 int currentTopRow = (int) mRollo.mMessageProc.mPosX;
224
225 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
226 int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
227
228 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
229 int currentPageRow = (currentSelection - (currentTopRow * Defines.COLUMNS_PER_PAGE))
230 / Defines.ROWS_PER_PAGE;
231
232 int newSelection = currentSelection;
233
234 switch (keyCode) {
235 case KeyEvent.KEYCODE_DPAD_UP:
236 if (currentPageRow > 0) {
237 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
238 } else if (currentTopRow > 0) {
239 mRollo.moveTo(currentTopRow - 1);
240 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
241 }
242 break;
243 case KeyEvent.KEYCODE_DPAD_DOWN:
244 if (currentSelection < mRollo.mState.iconCount - Defines.COLUMNS_PER_PAGE) {
245 if (currentPageRow < Defines.ROWS_PER_PAGE - 1) {
246 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
247 } else {
248 mRollo.moveTo(currentTopRow + 1);
249 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
250 }
251 }
252 break;
253 case KeyEvent.KEYCODE_DPAD_LEFT:
254 if (currentPageCol > 0) {
255 newSelection = currentSelection - 1;
256 }
257 break;
258 case KeyEvent.KEYCODE_DPAD_RIGHT:
259 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
260 (currentSelection < mRollo.mState.iconCount - 1)) {
261 newSelection = currentSelection + 1;
262 }
263 break;
264 }
265 if (newSelection != currentSelection) {
266 mRollo.selectIcon(newSelection);
267 mRollo.mState.save();
268 }
269 }
270 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700271 }
272
Jason Samsd8152b92009-10-13 17:19:10 -0700273 private int mRSMode = 0;
274
Joe Onorato93839052009-08-06 20:34:32 -0700275 @Override
276 public boolean onTouchEvent(MotionEvent ev)
277 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700278 mArrowNavigation = false;
279
Joe Onorato7bb17492009-09-24 17:51:01 -0700280 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700281 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700282 }
283
Joe Onoratofb0ca672009-09-14 17:55:46 -0400284 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700285 return true;
286 }
287
288 super.onTouchEvent(ev);
289
Joe Onoratofb0ca672009-09-14 17:55:46 -0400290 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700291 int y = (int)ev.getY();
292
Joe Onoratobcbeab82009-10-01 21:45:43 -0700293 int action = ev.getAction();
294 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400295 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700296 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
297 mTouchTracking = TRACKING_HOME;
Joe Onoratod63458b2009-10-15 21:19:09 -0700298 mRollo.setHomeSelected(true);
299 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700300 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700301 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700302 mTouchTracking = TRACKING_FLING;
303
304 mMotionDownRawX = (int)ev.getRawX();
305 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700306
Mike Cleron7d5d7462009-10-20 14:06:00 -0700307 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700308 mRollo.mState.newTouchDown = 1;
309
310 if (!mRollo.checkClickOK()) {
311 mRollo.clearSelectedIcon();
312 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700313 mDownIconIndex = mCurrentIconIndex
314 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
315 if (mDownIconIndex < 0) {
316 // if nothing was selected, no long press.
317 cancelLongPress();
318 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700319 }
320 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700321 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700322 mVelocity = VelocityTracker.obtain();
323 mVelocity.addMovement(ev);
324 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700325 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400326 break;
327 case MotionEvent.ACTION_MOVE:
328 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700329 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratod63458b2009-10-15 21:19:09 -0700330 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]);
331 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700332 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700333 int rawX = (int)ev.getRawX();
334 int rawY = (int)ev.getRawY();
335 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700336 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700337
Joe Onorato82ca5502009-10-15 16:59:23 -0700338 if (!mStartedScrolling && slop < mSlop) {
339 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700340 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700341 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
342 if (mDownIconIndex != mCurrentIconIndex) {
343 // If a different icon is selected, don't allow it to be picked up.
344 // This handles off-axis dragging.
345 cancelLongPress();
346 mCurrentIconIndex = -1;
347 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700348 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700349 if (!mStartedScrolling) {
350 cancelLongPress();
351 mCurrentIconIndex = -1;
352 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700353 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700354 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700355 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700356
Joe Onoratobcbeab82009-10-01 21:45:43 -0700357 mStartedScrolling = true;
358 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700359 mVelocity.addMovement(ev);
360 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700361 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400362 }
363 break;
364 case MotionEvent.ACTION_UP:
365 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700366 if (mTouchTracking == TRACKING_HOME) {
367 if (action == MotionEvent.ACTION_UP) {
368 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
369 mLauncher.closeAllApps(true);
370 }
Joe Onoratod63458b2009-10-15 21:19:09 -0700371 mRollo.setHomeSelected(false);
372 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700373 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700374 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700375 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700376 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700377 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700378
Jason Sams12c14a82009-10-06 14:33:15 -0700379 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700380 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700381 mRollo.clearSelectedIcon();
382 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700383 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700384
Joe Onorato539ed9d2009-10-02 10:22:14 -0700385 if (mVelocity != null) {
386 mVelocity.recycle();
387 mVelocity = null;
388 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700389 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700390 mTouchTracking = TRACKING_NONE;
391 break;
Joe Onorato93839052009-08-06 20:34:32 -0700392 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700393
394 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700395 }
396
Joe Onorato6665c0f2009-09-02 15:27:24 -0700397 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700398 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400399 return;
400 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700401 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
402 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
403 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700404 mLauncher.startActivitySafely(app.intent);
405 }
406 }
407
408 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700409 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400410 return true;
411 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700412 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
413 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
414 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700415
416 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700417 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
418 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700419
Joe Onoratobcbeab82009-10-01 21:45:43 -0700420 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
421 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700422 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700423 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700424 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700425
Joe Onorato7bb17492009-09-24 17:51:01 -0700426 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700427 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700428 return true;
429 }
430
Joe Onorato5162ea92009-09-03 09:39:42 -0700431 public void setDragController(DragController dragger) {
432 mDragController = dragger;
433 }
434
435 public void onDropCompleted(View target, boolean success) {
436 }
437
Joe Onorato4db52312009-10-06 11:17:43 -0700438 /**
439 * Zoom to the specifed amount.
440 *
441 * @param amount [0..1] 0 is hidden, 1 is open
442 * @param animate Whether to animate.
443 */
Jason Sams12c14a82009-10-06 14:33:15 -0700444 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700445 if (mRollo == null) {
446 return;
447 }
448
Joe Onoratofb0ca672009-09-14 17:55:46 -0400449 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400450 mRollo.clearSelectedIcon();
Joe Onorato1d708e72009-10-15 21:29:21 -0700451 mRollo.setHomeSelected(false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700452 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700453 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700454 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700455 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700456 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700457 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400458 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400459 }
460
461 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700462 if (mRollo == null) {
463 return false;
464 }
Jason Sams12c14a82009-10-06 14:33:15 -0700465 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400466 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700467
Mike Cleronb6082fa02009-10-19 17:03:36 -0700468 /*
Joe Onorato93839052009-08-06 20:34:32 -0700469 @Override
470 public boolean onTrackballEvent(MotionEvent ev)
471 {
472 float x = ev.getX();
473 float y = ev.getY();
474 //Float tx = new Float(x);
475 //Float ty = new Float(y);
476 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
477
478
479 return true;
480 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700481 */
Joe Onorato93839052009-08-06 20:34:32 -0700482
Joe Onorato9c1289c2009-08-17 11:03:03 -0400483 public void setApps(ArrayList<ApplicationInfo> list) {
484 mAllAppsList = list;
485 if (mRollo != null) {
486 mRollo.setApps(list);
487 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700488 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400489 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700490 }
491
Joe Onoratoa8138d52009-10-06 19:25:30 -0700492 public void addApps(ArrayList<ApplicationInfo> list) {
493 final int N = list.size();
494 if (mRollo != null) {
495 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
496 }
497
498 for (int i=0; i<N; i++) {
499 final ApplicationInfo item = list.get(i);
500 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
501 if (index < 0) {
502 index = -(index+1);
503 }
504 mAllAppsList.add(index, item);
505 if (mRollo != null) {
506 mRollo.addApp(index, item);
507 mRollo.mState.iconCount++;
508 }
509 }
510
511 if (mRollo != null) {
512 mRollo.saveAppsList();
513 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700514 }
515
Joe Onoratoa8138d52009-10-06 19:25:30 -0700516 public void removeApps(ArrayList<ApplicationInfo> list) {
517 final int N = list.size();
518 for (int i=0; i<N; i++) {
519 final ApplicationInfo item = list.get(i);
520 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
521 if (index >= 0) {
522 mAllAppsList.remove(index);
523 if (mRollo != null) {
524 mRollo.removeApp(index);
525 mRollo.mState.iconCount--;
526 }
527 } else {
528 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
529 // Try to recover. This should keep us from crashing for now.
530 }
531 }
532
533 if (mRollo != null) {
534 mRollo.saveAppsList();
535 }
536 }
537
538 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
539 // Just remove and add, because they may need to be re-sorted.
540 removeApps(list);
541 addApps(list);
542 }
543
544 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
545 public int compare(ApplicationInfo a, ApplicationInfo b) {
546 int result = a.title.toString().compareTo(b.toString());
547 if (result != 0) {
548 return result;
549 }
550 return a.intent.getComponent().compareTo(b.intent.getComponent());
551 }
552 };
553
554 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
555 public int compare(ApplicationInfo a, ApplicationInfo b) {
556 return a.intent.getComponent().compareTo(b.intent.getComponent());
557 }
558 };
559
Joe Onoratoc567acb2009-08-31 14:34:43 -0700560 private static int countPages(int iconCount) {
561 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
562 int pages = iconCount / iconsPerPage;
563 if (pages*iconsPerPage != iconCount) {
564 pages++;
565 }
566 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400567 }
568
Joe Onorato93839052009-08-06 20:34:32 -0700569 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700570
Joe Onorato1feb3a82009-08-08 22:32:00 -0700571 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700572 private int mWidth;
573 private int mHeight;
574
575 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700576 private Script mScript;
577 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700578 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700579 private Script.Invokable mInvokeFling;
580 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700581
Jason Samsc1c521e2009-10-19 14:45:45 -0700582
Jason Samscd689e12009-09-29 15:28:22 -0700583 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700584 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700585 private ProgramFragment mPFColor;
586 private ProgramFragment mPFTexLinear;
Joe Onorato93839052009-08-06 20:34:32 -0700587 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700588 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700589 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700590 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700591
Joe Onoratod63458b2009-10-15 21:19:09 -0700592 private Allocation mHomeButtonNormal;
593 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700594
Joe Onoratobf15cb42009-08-07 14:33:40 -0700595 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700596 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700597 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700598
Joe Onoratobf15cb42009-08-07 14:33:40 -0700599 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700600 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700601 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700602 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700603
Joe Onorato6665c0f2009-09-02 15:27:24 -0700604 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700605 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700606
607 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400608 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700609
Jason Samsd8152b92009-10-13 17:19:10 -0700610
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700611 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700612 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700613
Jason Sams78aebd82009-09-15 13:06:59 -0700614 class BaseAlloc {
615 Allocation mAlloc;
616 Type mType;
617
618 void save() {
619 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700620 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700621 }
622
Jason Sams12c14a82009-10-06 14:33:15 -0700623 class AAMessage extends RenderScript.RSMessage {
624 public void run() {
625 mPosX = ((float)mData[0]) / (1 << 16);
626 mVelocity = ((float)mData[1]) / (1 << 16);
627 mZoom = ((float)mData[2]) / (1 << 16);
628 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
629 }
630 float mZoom;
631 float mPosX;
632 float mVelocity;
633 }
634 AAMessage mMessageProc;
635
Jason Sams476339d2009-09-29 18:14:38 -0700636 private boolean checkClickOK() {
637 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams12c14a82009-10-06 14:33:15 -0700638 return (Math.abs(mMessageProc.mVelocity) < 0.1f) &&
639 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.1f);
Jason Sams476339d2009-09-29 18:14:38 -0700640 }
641
Jason Sams78aebd82009-09-15 13:06:59 -0700642 class Params extends BaseAlloc {
643 Params() {
644 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
645 mAlloc = Allocation.createTyped(mRS, mType);
646 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700647 }
Jason Sams78aebd82009-09-15 13:06:59 -0700648 public int bubbleWidth;
649 public int bubbleHeight;
650 public int bubbleBitmapWidth;
651 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700652
Joe Onoratobcbeab82009-10-01 21:45:43 -0700653 public int homeButtonWidth;
654 public int homeButtonHeight;
655 public int homeButtonTextureWidth;
656 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700657 }
658
659 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700660 public float newPositionX;
661 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700662 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700663 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700664 public int selectedIconIndex = -1;
665 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700666 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700667 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700668 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700669
670 State() {
671 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
672 mAlloc = Allocation.createTyped(mRS, mType);
673 save();
674 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700675 }
676
677 public RolloRS() {
678 }
679
680 public void init(Resources res, int width, int height) {
681 mRes = res;
682 mWidth = width;
683 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700684 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700685 initProgramVertex();
686 initProgramFragment();
687 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700688 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700689 initGl();
690 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700691 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700692 initRs();
693 }
694
Jason Sams0aa71662009-10-02 18:43:18 -0700695 public void initMesh() {
696 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
697 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
698
Jason Samsd8152b92009-10-13 17:19:10 -0700699 float y = 0;
700 float z = 0;
701 for (int ct=0; ct < 200; ct++) {
702 float angle = 0;
703 float maxAngle = 3.14f * 0.16f;
704 float l = 1.f;
705
706 l = 1 - ((ct-5) * 0.10f);
707 if (ct > 7) {
708 angle = maxAngle * (ct - 7) * 0.2f;
709 angle = Math.min(angle, maxAngle);
710 }
711 l = Math.max(0.3f, l);
712 l = Math.min(1.0f, l);
713
714 y += 0.1f * Math.cos(angle);
715 z += 0.1f * Math.sin(angle);
716
717 float t = 0.1f * ct;
718 float ds = 0.08f;
719 tm.setColor(l, l, l, 0.99f);
720 tm.setTexture(ds, t);
721 tm.addVertex(-0.5f, y, z);
722 tm.setTexture(1 - ds, t);
723 tm.addVertex(0.5f, y, z);
724 }
725 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
726 tm.addTriangle(ct, ct+1, ct+2);
727 tm.addTriangle(ct+1, ct+3, ct+2);
728 }
729 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700730 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700731 }
732
Jason Samscd689e12009-09-29 15:28:22 -0700733 private void initProgramVertex() {
734 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
735 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700736
737 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700738 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700739 mPV = pvb.create();
740 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700741 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700742
Jason Sams37e7c2b2009-10-19 12:55:43 -0700743 //pva = new ProgramVertex.MatrixAllocation(mRS);
744 //pva.setupOrthoWindow(mWidth, mHeight);
745 //pvb.setTextureMatrixEnable(true);
746 //mPVOrtho = pvb.create();
747 //mPVOrtho.setName("PVOrtho");
748 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700749
750 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700751 }
Joe Onorato93839052009-08-06 20:34:32 -0700752
Jason Samscd689e12009-09-29 15:28:22 -0700753 private void initProgramFragment() {
754 Sampler.Builder sb = new Sampler.Builder(mRS);
755 sb.setMin(Sampler.Value.LINEAR);
756 sb.setMag(Sampler.Value.LINEAR);
757 sb.setWrapS(Sampler.Value.CLAMP);
758 sb.setWrapT(Sampler.Value.CLAMP);
759 Sampler linear = sb.create();
760
761 sb.setMin(Sampler.Value.NEAREST);
762 sb.setMag(Sampler.Value.NEAREST);
763 Sampler nearest = sb.create();
764
765 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700766 //mPFColor = bf.create();
767 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700768
769 bf.setTexEnable(true, 0);
770 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
771 mPFTexLinear = bf.create();
772 mPFTexLinear.setName("PFTexLinear");
773 mPFTexLinear.bindSampler(linear, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700774 }
775
776 private void initProgramStore() {
777 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
778 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700779 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700780 bs.setDitherEnable(true);
781 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
782 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
783 mPSIcons = bs.create();
784 mPSIcons.setName("PSIcons");
785
786 //bs.setDitherEnable(false);
787 //mPSText = bs.create();
788 //mPSText.setName("PSText");
789 }
790
791 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700792 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700793 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700794 }
Jason Sams78aebd82009-09-15 13:06:59 -0700795
Joe Onorato1feb3a82009-08-08 22:32:00 -0700796 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700797 mParams = new Params();
798 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700799
Joe Onoratobf15cb42009-08-07 14:33:40 -0700800 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700801
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700802 mParams.bubbleWidth = bubble.getBubbleWidth();
803 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
804 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
805 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700806
Joe Onoratod63458b2009-10-15 21:19:09 -0700807 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
808 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
809 mHomeButtonNormal.uploadToTexture(0);
810 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
811 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
812 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700813 mParams.homeButtonWidth = 76;
814 mParams.homeButtonHeight = 68;
815 mParams.homeButtonTextureWidth = 128;
816 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700817
Joe Onoratod63458b2009-10-15 21:19:09 -0700818 mState.homeButtonId = mHomeButtonNormal.getID();
819
Joe Onorato1feb3a82009-08-08 22:32:00 -0700820 mParams.save();
821 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400822
Joe Onorato1291a8c2009-09-15 15:07:25 -0400823 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
824 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
825 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700826
Joe Onorato9c1289c2009-08-17 11:03:03 -0400827 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700828 }
829
Jason Sams37e7c2b2009-10-19 12:55:43 -0700830 private void initScript(int id) {
831 }
832
833 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700834 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700835 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700836 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700837 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700838 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
839 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700840 mInvokeMove = sb.addInvokable("move");
841 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700842 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700843 mInvokeResetWAR = sb.addInvokable("resetHWWar");
844 mScript = sb.create();
845 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
846 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
847 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
848 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
849 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700850
Jason Sams12c14a82009-10-06 14:33:15 -0700851 mMessageProc = new AAMessage();
852 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700853 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700854 }
Joe Onorato93839052009-08-06 20:34:32 -0700855
Joe Onorato9c1289c2009-08-17 11:03:03 -0400856 private void setApps(ArrayList<ApplicationInfo> list) {
857 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700858 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700859 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700860 allocCount = 1;
861 }
862
Joe Onorato9c1289c2009-08-17 11:03:03 -0400863 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700864 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700865 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400866
867 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700868 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700869 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700871 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872
873 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
874
875 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700876 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400877 }
878
Joe Onorato9c1289c2009-08-17 11:03:03 -0400879 mState.iconCount = count;
880
Joe Onoratoa8138d52009-10-06 19:25:30 -0700881 saveAppsList();
882 }
883
884 private void uploadAppIcon(int index, ApplicationInfo item) {
885 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
886 Element.RGBA_8888(mRS), false);
887 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
888 Element.RGBA_8888(mRS), false);
889
890 mIcons[index].uploadToTexture(0);
891 mLabels[index].uploadToTexture(0);
892
893 mIconIds[index] = mIcons[index].getID();
894 mLabelIds[index] = mLabels[index].getID();
895 }
896
897 /**
898 * Puts the empty spaces at the end. Updates mState.iconCount. You must
899 * fill in the values and call saveAppsList().
900 */
901 private void reallocAppsList(int count) {
902 Allocation[] icons = new Allocation[count];
903 int[] iconIds = new int[count];
904 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
905
906 Allocation[] labels = new Allocation[count];
907 int[] labelIds = new int[count];
908 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
909
910 final int oldCount = mIcons.length;
911
912 System.arraycopy(mIcons, 0, icons, 0, oldCount);
913 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
914 System.arraycopy(mLabels, 0, labels, 0, oldCount);
915 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
916
917 mIcons = icons;
918 mIconIds = iconIds;
919 mLabels = labels;
920 mLabelIds = labelIds;
921 }
922
923 /**
924 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
925 */
926 private void addApp(int index, ApplicationInfo item) {
927 final int count = mState.iconCount - index;
928 final int dest = index + 1;
929
930 System.arraycopy(mIcons, index, mIcons, dest, count);
931 System.arraycopy(mIconIds, index, mIconIds, dest, count);
932 System.arraycopy(mLabels, index, mLabels, dest, count);
933 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
934
935 uploadAppIcon(index, item);
936 }
937
938 /**
939 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
940 */
941 private void removeApp(int index) {
942 final int count = mState.iconCount - index - 1;
943 final int src = index + 1;
944
945 System.arraycopy(mIcons, src, mIcons, index, count);
946 System.arraycopy(mIconIds, src, mIconIds, index, count);
947 System.arraycopy(mLabels, src, mLabels, index, count);
948 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
949
950 final int last = mState.iconCount - 1;
951 mIcons[last] = null;
952 mIconIds[last] = 0;
953 mLabels[last] = null;
954 mLabelIds[last] = 0;
955 }
956
957 /**
958 * Send the apps list structures to RS.
959 */
960 private void saveAppsList() {
961 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -0700962
Joe Onoratoa8138d52009-10-06 19:25:30 -0700963 mAllocIconIds.data(mIconIds);
964 mAllocLabelIds.data(mLabelIds);
965
Jason Sams37e7c2b2009-10-19 12:55:43 -0700966 if (mScript != null) { // this happens when we init it
967 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
968 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 }
970
971 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -0700972
973 // Note: mScript may be null if we haven't initialized it yet.
974 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -0700975 if (mInvokeResetWAR != null) {
976 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -0700977 }
Jason Sams37e7c2b2009-10-19 12:55:43 -0700978 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400979 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700980
981 void initTouchState() {
982 int width = getWidth();
983 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700984 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
985 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700986
Jason Sams37e7c2b2009-10-19 12:55:43 -0700987 int centerY = (height / 2);
988 mTouchYBorders[0] = centerY - (cellHeight * 2);
989 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700990 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700991 mTouchYBorders[3] = centerY + cellHeight;
992 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -0700993
Joe Onorato6665c0f2009-09-02 15:27:24 -0700994 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700995 mTouchXBorders[0] = 0;
996 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700997 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700998 mTouchXBorders[3] = centerX + (width / 4);
999 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001000 }
1001
Jason Sams37e7c2b2009-10-19 12:55:43 -07001002 int chooseTappedIcon(int x, int y, float pos) {
1003 // Adjust for scroll position if not zero.
1004 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001005
Joe Onorato6665c0f2009-09-02 15:27:24 -07001006 int col = -1;
1007 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001008 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1009 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1010 col = i;
1011 break;
1012 }
1013 }
1014 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1015 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1016 row = i;
1017 break;
1018 }
1019 }
1020
1021 if (row < 0 || col < 0) {
1022 return -1;
1023 }
1024
Jason Sams37e7c2b2009-10-19 12:55:43 -07001025 return (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001026 + (row * Defines.ROWS_PER_PAGE) + col;
1027 }
1028
Jason Samsd8152b92009-10-13 17:19:10 -07001029 void fling() {
Jason Sams37e7c2b2009-10-19 12:55:43 -07001030 mInvokeFling.execute();
Jason Samsd8152b92009-10-13 17:19:10 -07001031 }
1032
1033 void move() {
Jason Sams37e7c2b2009-10-19 12:55:43 -07001034 mInvokeMove.execute();
Jason Samsd8152b92009-10-13 17:19:10 -07001035 }
1036
Jason Samsc1c521e2009-10-19 14:45:45 -07001037 void moveTo(float row) {
1038 mState.targetPos = row;
1039 mState.save();
1040 mInvokeMoveTo.execute();
1041 }
1042
Joe Onorato6665c0f2009-09-02 15:27:24 -07001043 /**
1044 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001045 *
1046 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001047 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001048 int selectIcon(int x, int y, float pos) {
1049 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001050 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001051 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001052 }
1053
1054 void selectIcon(int index) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001055 if (index < 0) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001056 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001057 } else {
1058 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001059
1060 Bitmap selectionBitmap = mSelectionBitmap;
1061
1062 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1063 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1064 mAllAppsList.get(index).iconBitmap);
1065
1066 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001067 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001068 mSelectedIcon.uploadToTexture(0);
1069 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001070 }
1071 }
1072
1073 /**
1074 * You need to call save() on mState on your own after calling this.
1075 */
1076 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001077 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001078 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001079
Joe Onoratod63458b2009-10-15 21:19:09 -07001080 void setHomeSelected(boolean pressed) {
1081 if (pressed) {
1082 mState.homeButtonId = mHomeButtonPressed.getID();
1083 } else {
1084 mState.homeButtonId = mHomeButtonNormal.getID();
1085 }
1086 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087 }
Joe Onorato93839052009-08-06 20:34:32 -07001088}
1089
1090