blob: 2dd3b4de0958dbf1f170a0a5e85dd6a2b8ae62d2 [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;
Jason Sams2e19c052009-10-20 18:19:55 -070091
Mike Cleron7d5d7462009-10-20 14:06:00 -070092 /**
93 * True when we are using arrow keys or trackball to drive navigation
94 */
95 private boolean mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -070096
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 Sams81134792009-10-27 15:38:42 -0700169 if (mRS != null) {
170 destroyRenderScript();
171 mRS = null;
172 mRollo = null;
173 }
174
Jason Sams05de32a2009-09-27 14:01:40 -0700175 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700176 mRollo = new RolloRS();
177 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400178 if (mAllAppsList != null) {
179 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700180 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700182
183 Resources res = getContext().getResources();
184 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700185 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700186
187 long endTime = SystemClock.uptimeMillis();
188 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700189 }
Jason Sams2e19c052009-10-20 18:19:55 -0700190
Joe Onorato93839052009-08-06 20:34:32 -0700191
192 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700193 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
194 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
195 if (gainFocus) {
196 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
197 // Select the first icon when we gain keyboard focus
198 mArrowNavigation = true;
199 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE);
200 mRollo.mState.save();
201 }
202 } else {
203 if (mArrowNavigation) {
204 // Clear selection when we lose focus
205 mRollo.clearSelectedIcon();
206 mRollo.mState.save();
207 mArrowNavigation = false;
208 }
209 }
210 }
211
212 @Override
213 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700214
Mike Cleron7d5d7462009-10-20 14:06:00 -0700215 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
216 if (mArrowNavigation) {
217 int whichApp = mRollo.mState.selectedIconIndex;
218 if (whichApp >= 0) {
219 ApplicationInfo app = mAllAppsList.get(whichApp);
220 mLauncher.startActivitySafely(app.intent);
221 }
222 }
223 }
Jason Sams2e19c052009-10-20 18:19:55 -0700224
Mike Cleron7d5d7462009-10-20 14:06:00 -0700225 if (mArrowNavigation && mRollo.mState.iconCount > 0) {
226 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700227
Mike Cleron7d5d7462009-10-20 14:06:00 -0700228 int currentSelection = mRollo.mState.selectedIconIndex;
229 int currentTopRow = (int) mRollo.mMessageProc.mPosX;
Jason Sams2e19c052009-10-20 18:19:55 -0700230
Mike Cleron7d5d7462009-10-20 14:06:00 -0700231 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
232 int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700233
Mike Cleron7d5d7462009-10-20 14:06:00 -0700234 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
235 int currentPageRow = (currentSelection - (currentTopRow * Defines.COLUMNS_PER_PAGE))
236 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700237
Mike Cleron7d5d7462009-10-20 14:06:00 -0700238 int newSelection = currentSelection;
239
240 switch (keyCode) {
241 case KeyEvent.KEYCODE_DPAD_UP:
242 if (currentPageRow > 0) {
243 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
244 } else if (currentTopRow > 0) {
245 mRollo.moveTo(currentTopRow - 1);
Jason Sams2e19c052009-10-20 18:19:55 -0700246 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700247 }
248 break;
249 case KeyEvent.KEYCODE_DPAD_DOWN:
250 if (currentSelection < mRollo.mState.iconCount - Defines.COLUMNS_PER_PAGE) {
251 if (currentPageRow < Defines.ROWS_PER_PAGE - 1) {
252 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
253 } else {
254 mRollo.moveTo(currentTopRow + 1);
255 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
256 }
257 }
258 break;
259 case KeyEvent.KEYCODE_DPAD_LEFT:
260 if (currentPageCol > 0) {
261 newSelection = currentSelection - 1;
262 }
263 break;
264 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700265 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Mike Cleron7d5d7462009-10-20 14:06:00 -0700266 (currentSelection < mRollo.mState.iconCount - 1)) {
267 newSelection = currentSelection + 1;
268 }
269 break;
270 }
271 if (newSelection != currentSelection) {
272 mRollo.selectIcon(newSelection);
273 mRollo.mState.save();
274 }
275 }
276 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700277 }
278
Jason Samsd8152b92009-10-13 17:19:10 -0700279 private int mRSMode = 0;
280
Joe Onorato93839052009-08-06 20:34:32 -0700281 @Override
282 public boolean onTouchEvent(MotionEvent ev)
283 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700284 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700285
Joe Onorato7bb17492009-09-24 17:51:01 -0700286 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700287 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700288 }
289
Joe Onoratofb0ca672009-09-14 17:55:46 -0400290 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700291 return true;
292 }
293
294 super.onTouchEvent(ev);
295
Joe Onoratofb0ca672009-09-14 17:55:46 -0400296 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700297 int y = (int)ev.getY();
298
Joe Onoratobcbeab82009-10-01 21:45:43 -0700299 int action = ev.getAction();
300 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400301 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700302 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
303 mTouchTracking = TRACKING_HOME;
Joe Onoratod63458b2009-10-15 21:19:09 -0700304 mRollo.setHomeSelected(true);
305 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700306 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700307 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700308 mTouchTracking = TRACKING_FLING;
309
310 mMotionDownRawX = (int)ev.getRawX();
311 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700312
Mike Cleron7d5d7462009-10-20 14:06:00 -0700313 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700314 mRollo.mState.newTouchDown = 1;
315
316 if (!mRollo.checkClickOK()) {
317 mRollo.clearSelectedIcon();
318 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700319 mDownIconIndex = mCurrentIconIndex
320 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
321 if (mDownIconIndex < 0) {
322 // if nothing was selected, no long press.
323 cancelLongPress();
324 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700325 }
326 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700327 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700328 mVelocity = VelocityTracker.obtain();
329 mVelocity.addMovement(ev);
330 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700331 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400332 break;
333 case MotionEvent.ACTION_MOVE:
334 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700335 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratod63458b2009-10-15 21:19:09 -0700336 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]);
337 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700338 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700339 int rawX = (int)ev.getRawX();
340 int rawY = (int)ev.getRawY();
341 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700342 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700343
Joe Onorato82ca5502009-10-15 16:59:23 -0700344 if (!mStartedScrolling && slop < mSlop) {
345 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700346 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700347 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
348 if (mDownIconIndex != mCurrentIconIndex) {
349 // If a different icon is selected, don't allow it to be picked up.
350 // This handles off-axis dragging.
351 cancelLongPress();
352 mCurrentIconIndex = -1;
353 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700354 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700355 if (!mStartedScrolling) {
356 cancelLongPress();
357 mCurrentIconIndex = -1;
358 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700359 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700360 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700361 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700362
Joe Onoratobcbeab82009-10-01 21:45:43 -0700363 mStartedScrolling = true;
364 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700365 mVelocity.addMovement(ev);
366 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700367 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400368 }
369 break;
370 case MotionEvent.ACTION_UP:
371 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700372 if (mTouchTracking == TRACKING_HOME) {
373 if (action == MotionEvent.ACTION_UP) {
374 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
375 mLauncher.closeAllApps(true);
376 }
Joe Onoratod63458b2009-10-15 21:19:09 -0700377 mRollo.setHomeSelected(false);
378 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700379 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700380 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700381 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700382 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700383 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700384
Jason Sams12c14a82009-10-06 14:33:15 -0700385 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700386 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700387 mRollo.clearSelectedIcon();
388 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700389 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700390
Joe Onorato539ed9d2009-10-02 10:22:14 -0700391 if (mVelocity != null) {
392 mVelocity.recycle();
393 mVelocity = null;
394 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700395 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700396 mTouchTracking = TRACKING_NONE;
397 break;
Joe Onorato93839052009-08-06 20:34:32 -0700398 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700399
400 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700401 }
402
Joe Onorato6665c0f2009-09-02 15:27:24 -0700403 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700404 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400405 return;
406 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700407 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
408 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
409 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700410 mLauncher.startActivitySafely(app.intent);
411 }
412 }
413
414 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700415 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400416 return true;
417 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700418 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
419 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
420 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700421
422 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700423 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
424 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700425
Joe Onoratobcbeab82009-10-01 21:45:43 -0700426 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
427 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700428 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700429 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700430 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700431
Joe Onorato7bb17492009-09-24 17:51:01 -0700432 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700433 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700434 return true;
435 }
436
Joe Onorato5162ea92009-09-03 09:39:42 -0700437 public void setDragController(DragController dragger) {
438 mDragController = dragger;
439 }
440
441 public void onDropCompleted(View target, boolean success) {
442 }
443
Joe Onorato4db52312009-10-06 11:17:43 -0700444 /**
445 * Zoom to the specifed amount.
446 *
447 * @param amount [0..1] 0 is hidden, 1 is open
448 * @param animate Whether to animate.
449 */
Jason Sams12c14a82009-10-06 14:33:15 -0700450 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700451 if (mRollo == null) {
452 return;
453 }
454
Joe Onoratofb0ca672009-09-14 17:55:46 -0400455 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400456 mRollo.clearSelectedIcon();
Joe Onorato1d708e72009-10-15 21:29:21 -0700457 mRollo.setHomeSelected(false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700458 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700459 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700460 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700461 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700462 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700463 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400464 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400465 }
466
467 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700468 if (mRollo == null) {
469 return false;
470 }
Jason Sams12c14a82009-10-06 14:33:15 -0700471 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400472 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700473
Mike Cleronb6082fa02009-10-19 17:03:36 -0700474 /*
Joe Onorato93839052009-08-06 20:34:32 -0700475 @Override
476 public boolean onTrackballEvent(MotionEvent ev)
477 {
478 float x = ev.getX();
479 float y = ev.getY();
480 //Float tx = new Float(x);
481 //Float ty = new Float(y);
482 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
483
484
485 return true;
486 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700487 */
Joe Onorato93839052009-08-06 20:34:32 -0700488
Joe Onorato9c1289c2009-08-17 11:03:03 -0400489 public void setApps(ArrayList<ApplicationInfo> list) {
490 mAllAppsList = list;
491 if (mRollo != null) {
492 mRollo.setApps(list);
493 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700494 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400495 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700496 }
497
Joe Onoratoa8138d52009-10-06 19:25:30 -0700498 public void addApps(ArrayList<ApplicationInfo> list) {
499 final int N = list.size();
500 if (mRollo != null) {
501 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
502 }
503
504 for (int i=0; i<N; i++) {
505 final ApplicationInfo item = list.get(i);
506 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
507 if (index < 0) {
508 index = -(index+1);
509 }
510 mAllAppsList.add(index, item);
511 if (mRollo != null) {
512 mRollo.addApp(index, item);
513 mRollo.mState.iconCount++;
514 }
515 }
516
517 if (mRollo != null) {
518 mRollo.saveAppsList();
519 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700520 }
521
Joe Onoratoa8138d52009-10-06 19:25:30 -0700522 public void removeApps(ArrayList<ApplicationInfo> list) {
523 final int N = list.size();
524 for (int i=0; i<N; i++) {
525 final ApplicationInfo item = list.get(i);
526 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
527 if (index >= 0) {
528 mAllAppsList.remove(index);
529 if (mRollo != null) {
530 mRollo.removeApp(index);
531 mRollo.mState.iconCount--;
532 }
533 } else {
534 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
535 // Try to recover. This should keep us from crashing for now.
536 }
537 }
538
539 if (mRollo != null) {
540 mRollo.saveAppsList();
541 }
542 }
543
544 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
545 // Just remove and add, because they may need to be re-sorted.
546 removeApps(list);
547 addApps(list);
548 }
549
550 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
551 public int compare(ApplicationInfo a, ApplicationInfo b) {
552 int result = a.title.toString().compareTo(b.toString());
553 if (result != 0) {
554 return result;
555 }
556 return a.intent.getComponent().compareTo(b.intent.getComponent());
557 }
558 };
559
560 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
561 public int compare(ApplicationInfo a, ApplicationInfo b) {
562 return a.intent.getComponent().compareTo(b.intent.getComponent());
563 }
564 };
565
Joe Onoratoc567acb2009-08-31 14:34:43 -0700566 private static int countPages(int iconCount) {
567 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
568 int pages = iconCount / iconsPerPage;
569 if (pages*iconsPerPage != iconCount) {
570 pages++;
571 }
572 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573 }
574
Joe Onorato93839052009-08-06 20:34:32 -0700575 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700576
Joe Onorato1feb3a82009-08-08 22:32:00 -0700577 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700578 private int mWidth;
579 private int mHeight;
580
581 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700582 private Script mScript;
583 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700584 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700585 private Script.Invokable mInvokeFling;
586 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700587
Jason Samsc1c521e2009-10-19 14:45:45 -0700588
Jason Samscd689e12009-09-29 15:28:22 -0700589 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700590 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700591 private ProgramFragment mPFColor;
592 private ProgramFragment mPFTexLinear;
Joe Onorato93839052009-08-06 20:34:32 -0700593 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700594 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700595 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700596 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700597
Joe Onoratod63458b2009-10-15 21:19:09 -0700598 private Allocation mHomeButtonNormal;
599 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700600
Joe Onoratobf15cb42009-08-07 14:33:40 -0700601 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700602 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700603 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700604
Joe Onoratobf15cb42009-08-07 14:33:40 -0700605 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700606 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700607 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700608 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700609
Joe Onorato6665c0f2009-09-02 15:27:24 -0700610 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700611 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700612
613 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400614 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700615
Jason Samsd8152b92009-10-13 17:19:10 -0700616
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700617 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700618 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700619
Jason Sams78aebd82009-09-15 13:06:59 -0700620 class BaseAlloc {
621 Allocation mAlloc;
622 Type mType;
623
624 void save() {
625 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700626 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700627 }
628
Jason Sams12c14a82009-10-06 14:33:15 -0700629 class AAMessage extends RenderScript.RSMessage {
630 public void run() {
631 mPosX = ((float)mData[0]) / (1 << 16);
632 mVelocity = ((float)mData[1]) / (1 << 16);
633 mZoom = ((float)mData[2]) / (1 << 16);
634 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
635 }
636 float mZoom;
637 float mPosX;
638 float mVelocity;
639 }
640 AAMessage mMessageProc;
641
Jason Sams476339d2009-09-29 18:14:38 -0700642 private boolean checkClickOK() {
643 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams2e19c052009-10-20 18:19:55 -0700644 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
645 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700646 }
647
Jason Sams78aebd82009-09-15 13:06:59 -0700648 class Params extends BaseAlloc {
649 Params() {
650 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
651 mAlloc = Allocation.createTyped(mRS, mType);
652 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700653 }
Jason Sams78aebd82009-09-15 13:06:59 -0700654 public int bubbleWidth;
655 public int bubbleHeight;
656 public int bubbleBitmapWidth;
657 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700658
Joe Onoratobcbeab82009-10-01 21:45:43 -0700659 public int homeButtonWidth;
660 public int homeButtonHeight;
661 public int homeButtonTextureWidth;
662 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700663 }
664
665 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700666 public float newPositionX;
667 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700668 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700669 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700670 public int selectedIconIndex = -1;
671 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700672 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700673 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700674 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700675
676 State() {
677 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
678 mAlloc = Allocation.createTyped(mRS, mType);
679 save();
680 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700681 }
682
683 public RolloRS() {
684 }
685
686 public void init(Resources res, int width, int height) {
687 mRes = res;
688 mWidth = width;
689 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700690 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700691 initProgramVertex();
692 initProgramFragment();
693 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700694 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700695 initGl();
696 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700697 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700698 initRs();
699 }
700
Jason Sams0aa71662009-10-02 18:43:18 -0700701 public void initMesh() {
702 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
703 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
704
Jason Samsd8152b92009-10-13 17:19:10 -0700705 float y = 0;
706 float z = 0;
707 for (int ct=0; ct < 200; ct++) {
708 float angle = 0;
709 float maxAngle = 3.14f * 0.16f;
710 float l = 1.f;
711
712 l = 1 - ((ct-5) * 0.10f);
713 if (ct > 7) {
714 angle = maxAngle * (ct - 7) * 0.2f;
715 angle = Math.min(angle, maxAngle);
716 }
717 l = Math.max(0.3f, l);
718 l = Math.min(1.0f, l);
719
720 y += 0.1f * Math.cos(angle);
721 z += 0.1f * Math.sin(angle);
722
723 float t = 0.1f * ct;
724 float ds = 0.08f;
725 tm.setColor(l, l, l, 0.99f);
726 tm.setTexture(ds, t);
727 tm.addVertex(-0.5f, y, z);
728 tm.setTexture(1 - ds, t);
729 tm.addVertex(0.5f, y, z);
730 }
731 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
732 tm.addTriangle(ct, ct+1, ct+2);
733 tm.addTriangle(ct+1, ct+3, ct+2);
734 }
735 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700736 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700737 }
738
Jason Samscd689e12009-09-29 15:28:22 -0700739 private void initProgramVertex() {
740 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
741 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700742
743 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700744 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700745 mPV = pvb.create();
746 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700747 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700748
Jason Sams37e7c2b2009-10-19 12:55:43 -0700749 //pva = new ProgramVertex.MatrixAllocation(mRS);
750 //pva.setupOrthoWindow(mWidth, mHeight);
751 //pvb.setTextureMatrixEnable(true);
752 //mPVOrtho = pvb.create();
753 //mPVOrtho.setName("PVOrtho");
754 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700755
756 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700757 }
Joe Onorato93839052009-08-06 20:34:32 -0700758
Jason Samscd689e12009-09-29 15:28:22 -0700759 private void initProgramFragment() {
760 Sampler.Builder sb = new Sampler.Builder(mRS);
761 sb.setMin(Sampler.Value.LINEAR);
762 sb.setMag(Sampler.Value.LINEAR);
763 sb.setWrapS(Sampler.Value.CLAMP);
764 sb.setWrapT(Sampler.Value.CLAMP);
765 Sampler linear = sb.create();
766
767 sb.setMin(Sampler.Value.NEAREST);
768 sb.setMag(Sampler.Value.NEAREST);
769 Sampler nearest = sb.create();
770
771 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700772 //mPFColor = bf.create();
773 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700774
775 bf.setTexEnable(true, 0);
776 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
777 mPFTexLinear = bf.create();
778 mPFTexLinear.setName("PFTexLinear");
779 mPFTexLinear.bindSampler(linear, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700780 }
781
782 private void initProgramStore() {
783 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
784 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700785 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700786 bs.setDitherEnable(true);
787 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
788 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
789 mPSIcons = bs.create();
790 mPSIcons.setName("PSIcons");
791
792 //bs.setDitherEnable(false);
793 //mPSText = bs.create();
794 //mPSText.setName("PSText");
795 }
796
797 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700798 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700799 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700800 }
Jason Sams78aebd82009-09-15 13:06:59 -0700801
Joe Onorato1feb3a82009-08-08 22:32:00 -0700802 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700803 mParams = new Params();
804 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700805
Joe Onoratobf15cb42009-08-07 14:33:40 -0700806 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700807
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700808 mParams.bubbleWidth = bubble.getBubbleWidth();
809 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
810 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
811 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700812
Joe Onoratod63458b2009-10-15 21:19:09 -0700813 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
814 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
815 mHomeButtonNormal.uploadToTexture(0);
816 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
817 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
818 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700819 mParams.homeButtonWidth = 76;
820 mParams.homeButtonHeight = 68;
821 mParams.homeButtonTextureWidth = 128;
822 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700823
Joe Onoratod63458b2009-10-15 21:19:09 -0700824 mState.homeButtonId = mHomeButtonNormal.getID();
825
Joe Onorato1feb3a82009-08-08 22:32:00 -0700826 mParams.save();
827 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400828
Joe Onorato1291a8c2009-09-15 15:07:25 -0400829 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
830 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
831 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700832
Joe Onorato9c1289c2009-08-17 11:03:03 -0400833 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700834 }
835
Jason Sams37e7c2b2009-10-19 12:55:43 -0700836 private void initScript(int id) {
837 }
838
839 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700840 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700841 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700842 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700843 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700844 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
845 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700846 mInvokeMove = sb.addInvokable("move");
847 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700848 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700849 mInvokeResetWAR = sb.addInvokable("resetHWWar");
850 mScript = sb.create();
851 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
852 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
853 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
854 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
855 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700856
Jason Sams12c14a82009-10-06 14:33:15 -0700857 mMessageProc = new AAMessage();
858 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700859 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700860 }
Joe Onorato93839052009-08-06 20:34:32 -0700861
Joe Onorato9c1289c2009-08-17 11:03:03 -0400862 private void setApps(ArrayList<ApplicationInfo> list) {
863 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700864 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700865 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700866 allocCount = 1;
867 }
868
Joe Onorato9c1289c2009-08-17 11:03:03 -0400869 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700870 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700871 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400872
873 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700874 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700875 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400876
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700877 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878
879 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
880
881 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700882 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400883 }
884
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885 mState.iconCount = count;
886
Joe Onoratoa8138d52009-10-06 19:25:30 -0700887 saveAppsList();
888 }
889
890 private void uploadAppIcon(int index, ApplicationInfo item) {
891 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
892 Element.RGBA_8888(mRS), false);
893 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
894 Element.RGBA_8888(mRS), false);
895
896 mIcons[index].uploadToTexture(0);
897 mLabels[index].uploadToTexture(0);
898
899 mIconIds[index] = mIcons[index].getID();
900 mLabelIds[index] = mLabels[index].getID();
901 }
902
903 /**
904 * Puts the empty spaces at the end. Updates mState.iconCount. You must
905 * fill in the values and call saveAppsList().
906 */
907 private void reallocAppsList(int count) {
908 Allocation[] icons = new Allocation[count];
909 int[] iconIds = new int[count];
910 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
911
912 Allocation[] labels = new Allocation[count];
913 int[] labelIds = new int[count];
914 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
915
916 final int oldCount = mIcons.length;
917
918 System.arraycopy(mIcons, 0, icons, 0, oldCount);
919 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
920 System.arraycopy(mLabels, 0, labels, 0, oldCount);
921 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
922
923 mIcons = icons;
924 mIconIds = iconIds;
925 mLabels = labels;
926 mLabelIds = labelIds;
927 }
928
929 /**
930 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
931 */
932 private void addApp(int index, ApplicationInfo item) {
933 final int count = mState.iconCount - index;
934 final int dest = index + 1;
935
936 System.arraycopy(mIcons, index, mIcons, dest, count);
937 System.arraycopy(mIconIds, index, mIconIds, dest, count);
938 System.arraycopy(mLabels, index, mLabels, dest, count);
939 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
940
941 uploadAppIcon(index, item);
942 }
943
944 /**
945 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
946 */
947 private void removeApp(int index) {
948 final int count = mState.iconCount - index - 1;
949 final int src = index + 1;
950
951 System.arraycopy(mIcons, src, mIcons, index, count);
952 System.arraycopy(mIconIds, src, mIconIds, index, count);
953 System.arraycopy(mLabels, src, mLabels, index, count);
954 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
955
956 final int last = mState.iconCount - 1;
957 mIcons[last] = null;
958 mIconIds[last] = 0;
959 mLabels[last] = null;
960 mLabelIds[last] = 0;
961 }
962
963 /**
964 * Send the apps list structures to RS.
965 */
966 private void saveAppsList() {
967 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -0700968
Joe Onoratoa8138d52009-10-06 19:25:30 -0700969 mAllocIconIds.data(mIconIds);
970 mAllocLabelIds.data(mLabelIds);
971
Jason Sams37e7c2b2009-10-19 12:55:43 -0700972 if (mScript != null) { // this happens when we init it
973 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
974 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 }
976
977 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -0700978
979 // Note: mScript may be null if we haven't initialized it yet.
980 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -0700981 if (mInvokeResetWAR != null) {
982 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -0700983 }
Jason Sams37e7c2b2009-10-19 12:55:43 -0700984 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700986
987 void initTouchState() {
988 int width = getWidth();
989 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700990 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
991 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700992
Jason Sams37e7c2b2009-10-19 12:55:43 -0700993 int centerY = (height / 2);
994 mTouchYBorders[0] = centerY - (cellHeight * 2);
995 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700996 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700997 mTouchYBorders[3] = centerY + cellHeight;
998 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -0700999
Joe Onorato6665c0f2009-09-02 15:27:24 -07001000 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001001 mTouchXBorders[0] = 0;
1002 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001003 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001004 mTouchXBorders[3] = centerX + (width / 4);
1005 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001006 }
1007
Jason Sams37e7c2b2009-10-19 12:55:43 -07001008 int chooseTappedIcon(int x, int y, float pos) {
1009 // Adjust for scroll position if not zero.
1010 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001011
Joe Onorato6665c0f2009-09-02 15:27:24 -07001012 int col = -1;
1013 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001014 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1015 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1016 col = i;
1017 break;
1018 }
1019 }
1020 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1021 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1022 row = i;
1023 break;
1024 }
1025 }
1026
1027 if (row < 0 || col < 0) {
1028 return -1;
1029 }
1030
Jason Sams37e7c2b2009-10-19 12:55:43 -07001031 return (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001032 + (row * Defines.ROWS_PER_PAGE) + col;
1033 }
1034
Jason Samsd8152b92009-10-13 17:19:10 -07001035 void fling() {
Jason Sams37e7c2b2009-10-19 12:55:43 -07001036 mInvokeFling.execute();
Jason Samsd8152b92009-10-13 17:19:10 -07001037 }
1038
1039 void move() {
Jason Sams37e7c2b2009-10-19 12:55:43 -07001040 mInvokeMove.execute();
Jason Samsd8152b92009-10-13 17:19:10 -07001041 }
1042
Jason Samsc1c521e2009-10-19 14:45:45 -07001043 void moveTo(float row) {
1044 mState.targetPos = row;
1045 mState.save();
1046 mInvokeMoveTo.execute();
1047 }
1048
Joe Onorato6665c0f2009-09-02 15:27:24 -07001049 /**
1050 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001051 *
1052 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001053 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001054 int selectIcon(int x, int y, float pos) {
1055 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001056 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001057 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001058 }
1059
1060 void selectIcon(int index) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001061 if (index < 0) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001062 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001063 } else {
1064 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001065
1066 Bitmap selectionBitmap = mSelectionBitmap;
1067
1068 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1069 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1070 mAllAppsList.get(index).iconBitmap);
1071
1072 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001073 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001074 mSelectedIcon.uploadToTexture(0);
1075 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001076 }
1077 }
1078
1079 /**
1080 * You need to call save() on mState on your own after calling this.
1081 */
1082 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001083 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001084 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001085
Joe Onoratod63458b2009-10-15 21:19:09 -07001086 void setHomeSelected(boolean pressed) {
1087 if (pressed) {
1088 mState.homeButtonId = mHomeButtonPressed.getID();
1089 } else {
1090 mState.homeButtonId = mHomeButtonNormal.getID();
1091 }
1092 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001093 }
Joe Onorato93839052009-08-06 20:34:32 -07001094}
1095
1096