blob: f5fdd36199aa459960ba936b02489fc2311ca8f9 [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);
Joe Onorato859b3a72009-10-28 15:17:01 -0400195
196 if (!isVisible()) {
197 return;
198 }
199
Mike Cleron7d5d7462009-10-20 14:06:00 -0700200 if (gainFocus) {
201 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
202 // Select the first icon when we gain keyboard focus
203 mArrowNavigation = true;
204 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE);
205 mRollo.mState.save();
206 }
207 } else {
208 if (mArrowNavigation) {
209 // Clear selection when we lose focus
210 mRollo.clearSelectedIcon();
211 mRollo.mState.save();
212 mArrowNavigation = false;
213 }
214 }
215 }
216
217 @Override
218 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700219
Joe Onorato859b3a72009-10-28 15:17:01 -0400220 if (!isVisible()) {
221 return false;
222 }
223
Mike Cleron7d5d7462009-10-20 14:06:00 -0700224 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
225 if (mArrowNavigation) {
226 int whichApp = mRollo.mState.selectedIconIndex;
227 if (whichApp >= 0) {
228 ApplicationInfo app = mAllAppsList.get(whichApp);
229 mLauncher.startActivitySafely(app.intent);
230 }
231 }
232 }
Jason Sams2e19c052009-10-20 18:19:55 -0700233
Mike Cleron7d5d7462009-10-20 14:06:00 -0700234 if (mArrowNavigation && mRollo.mState.iconCount > 0) {
235 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700236
Mike Cleron7d5d7462009-10-20 14:06:00 -0700237 int currentSelection = mRollo.mState.selectedIconIndex;
238 int currentTopRow = (int) mRollo.mMessageProc.mPosX;
Jason Sams2e19c052009-10-20 18:19:55 -0700239
Mike Cleron7d5d7462009-10-20 14:06:00 -0700240 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
241 int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700242
Mike Cleron7d5d7462009-10-20 14:06:00 -0700243 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
244 int currentPageRow = (currentSelection - (currentTopRow * Defines.COLUMNS_PER_PAGE))
245 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700246
Mike Cleron7d5d7462009-10-20 14:06:00 -0700247 int newSelection = currentSelection;
248
249 switch (keyCode) {
250 case KeyEvent.KEYCODE_DPAD_UP:
251 if (currentPageRow > 0) {
252 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
253 } else if (currentTopRow > 0) {
254 mRollo.moveTo(currentTopRow - 1);
Jason Sams2e19c052009-10-20 18:19:55 -0700255 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700256 }
257 break;
258 case KeyEvent.KEYCODE_DPAD_DOWN:
259 if (currentSelection < mRollo.mState.iconCount - Defines.COLUMNS_PER_PAGE) {
260 if (currentPageRow < Defines.ROWS_PER_PAGE - 1) {
261 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
262 } else {
263 mRollo.moveTo(currentTopRow + 1);
264 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
265 }
266 }
267 break;
268 case KeyEvent.KEYCODE_DPAD_LEFT:
269 if (currentPageCol > 0) {
270 newSelection = currentSelection - 1;
271 }
272 break;
273 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700274 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Mike Cleron7d5d7462009-10-20 14:06:00 -0700275 (currentSelection < mRollo.mState.iconCount - 1)) {
276 newSelection = currentSelection + 1;
277 }
278 break;
279 }
280 if (newSelection != currentSelection) {
281 mRollo.selectIcon(newSelection);
282 mRollo.mState.save();
283 }
284 }
285 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700286 }
287
Jason Samsd8152b92009-10-13 17:19:10 -0700288 private int mRSMode = 0;
289
Joe Onorato93839052009-08-06 20:34:32 -0700290 @Override
291 public boolean onTouchEvent(MotionEvent ev)
292 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700293 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700294
Joe Onorato7bb17492009-09-24 17:51:01 -0700295 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700296 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700297 }
298
Joe Onoratofb0ca672009-09-14 17:55:46 -0400299 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700300 return true;
301 }
302
303 super.onTouchEvent(ev);
304
Joe Onoratofb0ca672009-09-14 17:55:46 -0400305 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700306 int y = (int)ev.getY();
307
Joe Onoratobcbeab82009-10-01 21:45:43 -0700308 int action = ev.getAction();
309 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400310 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700311 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
312 mTouchTracking = TRACKING_HOME;
Joe Onoratod63458b2009-10-15 21:19:09 -0700313 mRollo.setHomeSelected(true);
314 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700315 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700316 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700317 mTouchTracking = TRACKING_FLING;
318
319 mMotionDownRawX = (int)ev.getRawX();
320 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700321
Mike Cleron7d5d7462009-10-20 14:06:00 -0700322 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700323 mRollo.mState.newTouchDown = 1;
324
325 if (!mRollo.checkClickOK()) {
326 mRollo.clearSelectedIcon();
327 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700328 mDownIconIndex = mCurrentIconIndex
329 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
330 if (mDownIconIndex < 0) {
331 // if nothing was selected, no long press.
332 cancelLongPress();
333 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700334 }
335 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700336 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700337 mVelocity = VelocityTracker.obtain();
338 mVelocity.addMovement(ev);
339 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700340 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400341 break;
342 case MotionEvent.ACTION_MOVE:
343 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700344 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratod63458b2009-10-15 21:19:09 -0700345 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]);
346 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700347 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700348 int rawX = (int)ev.getRawX();
349 int rawY = (int)ev.getRawY();
350 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700351 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700352
Joe Onorato82ca5502009-10-15 16:59:23 -0700353 if (!mStartedScrolling && slop < mSlop) {
354 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700355 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700356 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
357 if (mDownIconIndex != mCurrentIconIndex) {
358 // If a different icon is selected, don't allow it to be picked up.
359 // This handles off-axis dragging.
360 cancelLongPress();
361 mCurrentIconIndex = -1;
362 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700363 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700364 if (!mStartedScrolling) {
365 cancelLongPress();
366 mCurrentIconIndex = -1;
367 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700368 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700369 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700370 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700371
Joe Onoratobcbeab82009-10-01 21:45:43 -0700372 mStartedScrolling = true;
373 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700374 mVelocity.addMovement(ev);
375 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700376 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400377 }
378 break;
379 case MotionEvent.ACTION_UP:
380 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700381 if (mTouchTracking == TRACKING_HOME) {
382 if (action == MotionEvent.ACTION_UP) {
383 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
384 mLauncher.closeAllApps(true);
385 }
Joe Onoratod63458b2009-10-15 21:19:09 -0700386 mRollo.setHomeSelected(false);
387 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700388 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700389 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700390 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700391 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700392 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700393
Jason Sams12c14a82009-10-06 14:33:15 -0700394 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700395 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700396 mRollo.clearSelectedIcon();
397 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700398 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700399
Joe Onorato539ed9d2009-10-02 10:22:14 -0700400 if (mVelocity != null) {
401 mVelocity.recycle();
402 mVelocity = null;
403 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700404 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700405 mTouchTracking = TRACKING_NONE;
406 break;
Joe Onorato93839052009-08-06 20:34:32 -0700407 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700408
409 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700410 }
411
Joe Onorato6665c0f2009-09-02 15:27:24 -0700412 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700413 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400414 return;
415 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700416 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
417 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
418 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700419 mLauncher.startActivitySafely(app.intent);
420 }
421 }
422
423 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700424 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400425 return true;
426 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700427 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
428 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
429 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700430
431 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700432 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
433 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700434
Joe Onoratobcbeab82009-10-01 21:45:43 -0700435 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
436 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700437 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700438 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700439 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700440
Joe Onorato7bb17492009-09-24 17:51:01 -0700441 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700442 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700443 return true;
444 }
445
Joe Onorato5162ea92009-09-03 09:39:42 -0700446 public void setDragController(DragController dragger) {
447 mDragController = dragger;
448 }
449
450 public void onDropCompleted(View target, boolean success) {
451 }
452
Joe Onorato4db52312009-10-06 11:17:43 -0700453 /**
454 * Zoom to the specifed amount.
455 *
456 * @param amount [0..1] 0 is hidden, 1 is open
457 * @param animate Whether to animate.
458 */
Jason Sams12c14a82009-10-06 14:33:15 -0700459 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700460 if (mRollo == null) {
461 return;
462 }
463
Joe Onoratofb0ca672009-09-14 17:55:46 -0400464 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400465 mRollo.clearSelectedIcon();
Joe Onorato1d708e72009-10-15 21:29:21 -0700466 mRollo.setHomeSelected(false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700467 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700468 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700469 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700470 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700471 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700472 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400473 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400474 }
475
476 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700477 if (mRollo == null) {
478 return false;
479 }
Jason Sams12c14a82009-10-06 14:33:15 -0700480 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400481 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700482
Mike Cleronb6082fa02009-10-19 17:03:36 -0700483 /*
Joe Onorato93839052009-08-06 20:34:32 -0700484 @Override
485 public boolean onTrackballEvent(MotionEvent ev)
486 {
487 float x = ev.getX();
488 float y = ev.getY();
489 //Float tx = new Float(x);
490 //Float ty = new Float(y);
491 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
492
493
494 return true;
495 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700496 */
Joe Onorato93839052009-08-06 20:34:32 -0700497
Joe Onorato9c1289c2009-08-17 11:03:03 -0400498 public void setApps(ArrayList<ApplicationInfo> list) {
499 mAllAppsList = list;
500 if (mRollo != null) {
501 mRollo.setApps(list);
502 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700503 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400504 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700505 }
506
Joe Onoratoa8138d52009-10-06 19:25:30 -0700507 public void addApps(ArrayList<ApplicationInfo> list) {
508 final int N = list.size();
509 if (mRollo != null) {
510 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
511 }
512
513 for (int i=0; i<N; i++) {
514 final ApplicationInfo item = list.get(i);
515 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
516 if (index < 0) {
517 index = -(index+1);
518 }
519 mAllAppsList.add(index, item);
520 if (mRollo != null) {
521 mRollo.addApp(index, item);
522 mRollo.mState.iconCount++;
523 }
524 }
525
526 if (mRollo != null) {
527 mRollo.saveAppsList();
528 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700529 }
530
Joe Onoratoa8138d52009-10-06 19:25:30 -0700531 public void removeApps(ArrayList<ApplicationInfo> list) {
532 final int N = list.size();
533 for (int i=0; i<N; i++) {
534 final ApplicationInfo item = list.get(i);
535 int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp);
536 if (index >= 0) {
537 mAllAppsList.remove(index);
538 if (mRollo != null) {
539 mRollo.removeApp(index);
540 mRollo.mState.iconCount--;
541 }
542 } else {
543 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
544 // Try to recover. This should keep us from crashing for now.
545 }
546 }
547
548 if (mRollo != null) {
549 mRollo.saveAppsList();
550 }
551 }
552
553 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
554 // Just remove and add, because they may need to be re-sorted.
555 removeApps(list);
556 addApps(list);
557 }
558
559 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
560 public int compare(ApplicationInfo a, ApplicationInfo b) {
561 int result = a.title.toString().compareTo(b.toString());
562 if (result != 0) {
563 return result;
564 }
565 return a.intent.getComponent().compareTo(b.intent.getComponent());
566 }
567 };
568
569 private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() {
570 public int compare(ApplicationInfo a, ApplicationInfo b) {
571 return a.intent.getComponent().compareTo(b.intent.getComponent());
572 }
573 };
574
Joe Onoratoc567acb2009-08-31 14:34:43 -0700575 private static int countPages(int iconCount) {
576 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
577 int pages = iconCount / iconsPerPage;
578 if (pages*iconsPerPage != iconCount) {
579 pages++;
580 }
581 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400582 }
583
Joe Onorato93839052009-08-06 20:34:32 -0700584 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700585
Joe Onorato1feb3a82009-08-08 22:32:00 -0700586 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700587 private int mWidth;
588 private int mHeight;
589
590 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700591 private Script mScript;
592 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700593 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700594 private Script.Invokable mInvokeFling;
595 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700596
Jason Samsc1c521e2009-10-19 14:45:45 -0700597
Jason Samscd689e12009-09-29 15:28:22 -0700598 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700599 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700600 private ProgramFragment mPFColor;
601 private ProgramFragment mPFTexLinear;
Joe Onorato93839052009-08-06 20:34:32 -0700602 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700603 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700604 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700605 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700606
Joe Onoratod63458b2009-10-15 21:19:09 -0700607 private Allocation mHomeButtonNormal;
608 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700609
Joe Onoratobf15cb42009-08-07 14:33:40 -0700610 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700611 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700612 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700613
Joe Onoratobf15cb42009-08-07 14:33:40 -0700614 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700615 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700616 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700617 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700618
Joe Onorato6665c0f2009-09-02 15:27:24 -0700619 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700620 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700621
622 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400623 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700624
Jason Samsd8152b92009-10-13 17:19:10 -0700625
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700626 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700627 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700628
Jason Sams78aebd82009-09-15 13:06:59 -0700629 class BaseAlloc {
630 Allocation mAlloc;
631 Type mType;
632
633 void save() {
634 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700635 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700636 }
637
Jason Sams12c14a82009-10-06 14:33:15 -0700638 class AAMessage extends RenderScript.RSMessage {
639 public void run() {
640 mPosX = ((float)mData[0]) / (1 << 16);
641 mVelocity = ((float)mData[1]) / (1 << 16);
642 mZoom = ((float)mData[2]) / (1 << 16);
643 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
644 }
645 float mZoom;
646 float mPosX;
647 float mVelocity;
648 }
649 AAMessage mMessageProc;
650
Jason Sams476339d2009-09-29 18:14:38 -0700651 private boolean checkClickOK() {
652 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams2e19c052009-10-20 18:19:55 -0700653 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
654 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700655 }
656
Jason Sams78aebd82009-09-15 13:06:59 -0700657 class Params extends BaseAlloc {
658 Params() {
659 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
660 mAlloc = Allocation.createTyped(mRS, mType);
661 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700662 }
Jason Sams78aebd82009-09-15 13:06:59 -0700663 public int bubbleWidth;
664 public int bubbleHeight;
665 public int bubbleBitmapWidth;
666 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700667
Joe Onoratobcbeab82009-10-01 21:45:43 -0700668 public int homeButtonWidth;
669 public int homeButtonHeight;
670 public int homeButtonTextureWidth;
671 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700672 }
673
674 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700675 public float newPositionX;
676 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700677 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700678 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700679 public int selectedIconIndex = -1;
680 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700681 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700682 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700683 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700684
685 State() {
686 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
687 mAlloc = Allocation.createTyped(mRS, mType);
688 save();
689 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700690 }
691
692 public RolloRS() {
693 }
694
695 public void init(Resources res, int width, int height) {
696 mRes = res;
697 mWidth = width;
698 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700699 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700700 initProgramVertex();
701 initProgramFragment();
702 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700703 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700704 initGl();
705 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700706 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700707 initRs();
708 }
709
Jason Sams0aa71662009-10-02 18:43:18 -0700710 public void initMesh() {
711 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
712 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
713
Jason Samsd8152b92009-10-13 17:19:10 -0700714 float y = 0;
715 float z = 0;
716 for (int ct=0; ct < 200; ct++) {
717 float angle = 0;
718 float maxAngle = 3.14f * 0.16f;
719 float l = 1.f;
720
721 l = 1 - ((ct-5) * 0.10f);
722 if (ct > 7) {
723 angle = maxAngle * (ct - 7) * 0.2f;
724 angle = Math.min(angle, maxAngle);
725 }
726 l = Math.max(0.3f, l);
727 l = Math.min(1.0f, l);
728
729 y += 0.1f * Math.cos(angle);
730 z += 0.1f * Math.sin(angle);
731
732 float t = 0.1f * ct;
733 float ds = 0.08f;
734 tm.setColor(l, l, l, 0.99f);
735 tm.setTexture(ds, t);
736 tm.addVertex(-0.5f, y, z);
737 tm.setTexture(1 - ds, t);
738 tm.addVertex(0.5f, y, z);
739 }
740 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
741 tm.addTriangle(ct, ct+1, ct+2);
742 tm.addTriangle(ct+1, ct+3, ct+2);
743 }
744 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700745 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700746 }
747
Jason Samscd689e12009-09-29 15:28:22 -0700748 private void initProgramVertex() {
749 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
750 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700751
752 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700753 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700754 mPV = pvb.create();
755 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700756 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700757
Jason Sams37e7c2b2009-10-19 12:55:43 -0700758 //pva = new ProgramVertex.MatrixAllocation(mRS);
759 //pva.setupOrthoWindow(mWidth, mHeight);
760 //pvb.setTextureMatrixEnable(true);
761 //mPVOrtho = pvb.create();
762 //mPVOrtho.setName("PVOrtho");
763 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700764
765 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700766 }
Joe Onorato93839052009-08-06 20:34:32 -0700767
Jason Samscd689e12009-09-29 15:28:22 -0700768 private void initProgramFragment() {
769 Sampler.Builder sb = new Sampler.Builder(mRS);
770 sb.setMin(Sampler.Value.LINEAR);
771 sb.setMag(Sampler.Value.LINEAR);
772 sb.setWrapS(Sampler.Value.CLAMP);
773 sb.setWrapT(Sampler.Value.CLAMP);
774 Sampler linear = sb.create();
775
776 sb.setMin(Sampler.Value.NEAREST);
777 sb.setMag(Sampler.Value.NEAREST);
778 Sampler nearest = sb.create();
779
780 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700781 //mPFColor = bf.create();
782 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700783
784 bf.setTexEnable(true, 0);
785 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
786 mPFTexLinear = bf.create();
787 mPFTexLinear.setName("PFTexLinear");
788 mPFTexLinear.bindSampler(linear, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700789 }
790
791 private void initProgramStore() {
792 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
793 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700794 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700795 bs.setDitherEnable(true);
796 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
797 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
798 mPSIcons = bs.create();
799 mPSIcons.setName("PSIcons");
800
801 //bs.setDitherEnable(false);
802 //mPSText = bs.create();
803 //mPSText.setName("PSText");
804 }
805
806 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700807 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700808 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700809 }
Jason Sams78aebd82009-09-15 13:06:59 -0700810
Joe Onorato1feb3a82009-08-08 22:32:00 -0700811 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700812 mParams = new Params();
813 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700814
Joe Onoratobf15cb42009-08-07 14:33:40 -0700815 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700816
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700817 mParams.bubbleWidth = bubble.getBubbleWidth();
818 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
819 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
820 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700821
Joe Onoratod63458b2009-10-15 21:19:09 -0700822 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
823 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
824 mHomeButtonNormal.uploadToTexture(0);
825 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
826 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
827 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700828 mParams.homeButtonWidth = 76;
829 mParams.homeButtonHeight = 68;
830 mParams.homeButtonTextureWidth = 128;
831 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700832
Joe Onoratod63458b2009-10-15 21:19:09 -0700833 mState.homeButtonId = mHomeButtonNormal.getID();
834
Joe Onorato1feb3a82009-08-08 22:32:00 -0700835 mParams.save();
836 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400837
Joe Onorato1291a8c2009-09-15 15:07:25 -0400838 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
839 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
840 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700841
Joe Onorato9c1289c2009-08-17 11:03:03 -0400842 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700843 }
844
Jason Sams37e7c2b2009-10-19 12:55:43 -0700845 private void initScript(int id) {
846 }
847
848 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700849 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700850 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700851 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700852 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700853 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
854 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700855 mInvokeMove = sb.addInvokable("move");
856 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700857 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700858 mInvokeResetWAR = sb.addInvokable("resetHWWar");
859 mScript = sb.create();
860 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
861 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
862 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
863 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
864 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700865
Jason Sams12c14a82009-10-06 14:33:15 -0700866 mMessageProc = new AAMessage();
867 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700868 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700869 }
Joe Onorato93839052009-08-06 20:34:32 -0700870
Joe Onorato9c1289c2009-08-17 11:03:03 -0400871 private void setApps(ArrayList<ApplicationInfo> list) {
872 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700873 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700874 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700875 allocCount = 1;
876 }
877
Joe Onorato9c1289c2009-08-17 11:03:03 -0400878 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700879 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700880 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400881
882 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700883 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700884 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400885
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700886 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400887
888 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
889
890 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700891 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400892 }
893
Joe Onorato9c1289c2009-08-17 11:03:03 -0400894 mState.iconCount = count;
895
Joe Onoratoa8138d52009-10-06 19:25:30 -0700896 saveAppsList();
897 }
898
899 private void uploadAppIcon(int index, ApplicationInfo item) {
900 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
901 Element.RGBA_8888(mRS), false);
902 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
903 Element.RGBA_8888(mRS), false);
904
905 mIcons[index].uploadToTexture(0);
906 mLabels[index].uploadToTexture(0);
907
908 mIconIds[index] = mIcons[index].getID();
909 mLabelIds[index] = mLabels[index].getID();
910 }
911
912 /**
913 * Puts the empty spaces at the end. Updates mState.iconCount. You must
914 * fill in the values and call saveAppsList().
915 */
916 private void reallocAppsList(int count) {
917 Allocation[] icons = new Allocation[count];
918 int[] iconIds = new int[count];
919 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
920
921 Allocation[] labels = new Allocation[count];
922 int[] labelIds = new int[count];
923 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
924
925 final int oldCount = mIcons.length;
926
927 System.arraycopy(mIcons, 0, icons, 0, oldCount);
928 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
929 System.arraycopy(mLabels, 0, labels, 0, oldCount);
930 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
931
932 mIcons = icons;
933 mIconIds = iconIds;
934 mLabels = labels;
935 mLabelIds = labelIds;
936 }
937
938 /**
939 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
940 */
941 private void addApp(int index, ApplicationInfo item) {
942 final int count = mState.iconCount - index;
943 final int dest = index + 1;
944
945 System.arraycopy(mIcons, index, mIcons, dest, count);
946 System.arraycopy(mIconIds, index, mIconIds, dest, count);
947 System.arraycopy(mLabels, index, mLabels, dest, count);
948 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
949
950 uploadAppIcon(index, item);
951 }
952
953 /**
954 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
955 */
956 private void removeApp(int index) {
957 final int count = mState.iconCount - index - 1;
958 final int src = index + 1;
959
960 System.arraycopy(mIcons, src, mIcons, index, count);
961 System.arraycopy(mIconIds, src, mIconIds, index, count);
962 System.arraycopy(mLabels, src, mLabels, index, count);
963 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
964
965 final int last = mState.iconCount - 1;
966 mIcons[last] = null;
967 mIconIds[last] = 0;
968 mLabels[last] = null;
969 mLabelIds[last] = 0;
970 }
971
972 /**
973 * Send the apps list structures to RS.
974 */
975 private void saveAppsList() {
976 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -0700977
Joe Onoratoa8138d52009-10-06 19:25:30 -0700978 mAllocIconIds.data(mIconIds);
979 mAllocLabelIds.data(mLabelIds);
980
Jason Sams37e7c2b2009-10-19 12:55:43 -0700981 if (mScript != null) { // this happens when we init it
982 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
983 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400984 }
985
986 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -0700987
988 // Note: mScript may be null if we haven't initialized it yet.
989 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -0700990 if (mInvokeResetWAR != null) {
991 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -0700992 }
Jason Sams37e7c2b2009-10-19 12:55:43 -0700993 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700995
996 void initTouchState() {
997 int width = getWidth();
998 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700999 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1000 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001001
Jason Sams37e7c2b2009-10-19 12:55:43 -07001002 int centerY = (height / 2);
1003 mTouchYBorders[0] = centerY - (cellHeight * 2);
1004 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001005 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001006 mTouchYBorders[3] = centerY + cellHeight;
1007 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001008
Joe Onorato6665c0f2009-09-02 15:27:24 -07001009 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001010 mTouchXBorders[0] = 0;
1011 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001012 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001013 mTouchXBorders[3] = centerX + (width / 4);
1014 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001015 }
1016
Jason Sams37e7c2b2009-10-19 12:55:43 -07001017 int chooseTappedIcon(int x, int y, float pos) {
1018 // Adjust for scroll position if not zero.
1019 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001020
Joe Onorato6665c0f2009-09-02 15:27:24 -07001021 int col = -1;
1022 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001023 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1024 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1025 col = i;
1026 break;
1027 }
1028 }
1029 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1030 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1031 row = i;
1032 break;
1033 }
1034 }
1035
1036 if (row < 0 || col < 0) {
1037 return -1;
1038 }
1039
Jason Sams37e7c2b2009-10-19 12:55:43 -07001040 return (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001041 + (row * Defines.ROWS_PER_PAGE) + col;
1042 }
1043
Jason Samsd8152b92009-10-13 17:19:10 -07001044 void fling() {
Jason Sams37e7c2b2009-10-19 12:55:43 -07001045 mInvokeFling.execute();
Jason Samsd8152b92009-10-13 17:19:10 -07001046 }
1047
1048 void move() {
Jason Sams37e7c2b2009-10-19 12:55:43 -07001049 mInvokeMove.execute();
Jason Samsd8152b92009-10-13 17:19:10 -07001050 }
1051
Jason Samsc1c521e2009-10-19 14:45:45 -07001052 void moveTo(float row) {
1053 mState.targetPos = row;
1054 mState.save();
1055 mInvokeMoveTo.execute();
1056 }
1057
Joe Onorato6665c0f2009-09-02 15:27:24 -07001058 /**
1059 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001060 *
1061 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001062 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001063 int selectIcon(int x, int y, float pos) {
1064 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001065 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001066 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001067 }
1068
1069 void selectIcon(int index) {
Joe Onorato82ca5502009-10-15 16:59:23 -07001070 if (index < 0) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001071 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001072 } else {
1073 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001074
1075 Bitmap selectionBitmap = mSelectionBitmap;
1076
1077 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1078 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1079 mAllAppsList.get(index).iconBitmap);
1080
1081 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001082 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001083 mSelectedIcon.uploadToTexture(0);
1084 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001085 }
1086 }
1087
1088 /**
1089 * You need to call save() on mState on your own after calling this.
1090 */
1091 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001092 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001093 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001094
Joe Onoratod63458b2009-10-15 21:19:09 -07001095 void setHomeSelected(boolean pressed) {
1096 if (pressed) {
1097 mState.homeButtonId = mHomeButtonPressed.getID();
1098 } else {
1099 mState.homeButtonId = mHomeButtonNormal.getID();
1100 }
1101 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001102 }
Joe Onorato93839052009-08-06 20:34:32 -07001103}
1104
1105