blob: 1b6c470dd3e530fe1c07371c0d79a805a1a70c3c [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;
Jason Samsc8514792009-10-29 14:27:29 -070032import android.renderscript.Dimension;
Joe Onorato93839052009-08-06 20:34:32 -070033import android.renderscript.Allocation;
Jason Sams78aebd82009-09-15 13:06:59 -070034import android.renderscript.Type;
Joe Onorato93839052009-08-06 20:34:32 -070035import android.renderscript.Script;
36import android.renderscript.ScriptC;
37import android.renderscript.ProgramFragment;
38import android.renderscript.ProgramStore;
39import android.renderscript.Sampler;
Jason Sams0aa71662009-10-02 18:43:18 -070040import android.renderscript.SimpleMesh;
Joe Onorato93839052009-08-06 20:34:32 -070041
Joe Onoratocb9f7982009-10-31 16:32:02 -040042import android.content.ComponentName;
Joe Onorato93839052009-08-06 20:34:32 -070043import android.content.Context;
44import android.content.res.Resources;
Joe Onorato7c312c12009-08-13 21:36:53 -070045import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070046import android.graphics.Bitmap;
47import android.graphics.BitmapFactory;
48import android.graphics.Canvas;
49import android.graphics.Paint;
Mike Cleron7d5d7462009-10-20 14:06:00 -070050import android.graphics.Rect;
Joe Onorato93839052009-08-06 20:34:32 -070051import android.graphics.drawable.BitmapDrawable;
52import android.graphics.drawable.Drawable;
Joe Onorato93839052009-08-06 20:34:32 -070053import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070054import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070055import android.util.AttributeSet;
56import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070057import android.view.KeyEvent;
58import android.view.MotionEvent;
Joe Onoratob39e51a2009-10-28 15:47:49 -040059import android.view.SoundEffectConstants;
Joe Onorato93839052009-08-06 20:34:32 -070060import android.view.Surface;
61import android.view.SurfaceHolder;
62import android.view.SurfaceView;
Joe Onorato6665c0f2009-09-02 15:27:24 -070063import android.view.View;
Joe Onoratod769a632009-08-11 17:09:02 -070064import android.view.VelocityTracker;
65import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070066import android.graphics.PixelFormat;
67
68
Joe Onorato6665c0f2009-09-02 15:27:24 -070069public class AllAppsView extends RSSurfaceView
Joe Onorato5162ea92009-09-03 09:39:42 -070070 implements View.OnClickListener, View.OnLongClickListener, DragSource {
Joe Onorato9c1289c2009-08-17 11:03:03 -040071 private static final String TAG = "Launcher.AllAppsView";
72
Joe Onoratofb0ca672009-09-14 17:55:46 -040073 /** Bit for mLocks for when there are icons being loaded. */
74 private static final int LOCK_ICONS_PENDING = 1;
75
Joe Onorato68ffd102009-10-15 17:59:43 -070076 private static final int TRACKING_NONE = 0;
77 private static final int TRACKING_FLING = 1;
78 private static final int TRACKING_HOME = 2;
Joe Onoratobcbeab82009-10-01 21:45:43 -070079
Joe Onorato6665c0f2009-09-02 15:27:24 -070080 private Launcher mLauncher;
Joe Onorato5162ea92009-09-03 09:39:42 -070081 private DragController mDragController;
Joe Onoratofb0ca672009-09-14 17:55:46 -040082
83 /** When this is 0, modifications are allowed, when it's not, they're not.
84 * TODO: What about scrolling? */
85 private int mLocks = LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -070086
Joe Onorato82ca5502009-10-15 16:59:23 -070087 private int mSlop;
Joe Onoratof7b0e012009-10-01 14:09:15 -070088 private int mMaxFlingVelocity;
89
Joe Onoratobcbeab82009-10-01 21:45:43 -070090 private Defines mDefines = new Defines();
Joe Onorato1feb3a82009-08-08 22:32:00 -070091 private RenderScript mRS;
92 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040093 private ArrayList<ApplicationInfo> mAllAppsList;
Jason Sams2e19c052009-10-20 18:19:55 -070094
Mike Cleron7d5d7462009-10-20 14:06:00 -070095 /**
96 * True when we are using arrow keys or trackball to drive navigation
97 */
98 private boolean mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -070099
Joe Onoratoc567acb2009-08-31 14:34:43 -0700100 private int mPageCount;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700101 private boolean mStartedScrolling;
Joe Onoratod769a632009-08-11 17:09:02 -0700102 private VelocityTracker mVelocity;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700103 private int mTouchTracking;
Joe Onorato5162ea92009-09-03 09:39:42 -0700104 private int mMotionDownRawX;
105 private int mMotionDownRawY;
Joe Onorato82ca5502009-10-15 16:59:23 -0700106 private int mDownIconIndex = -1;
107 private int mCurrentIconIndex = -1;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700108 private int mHomeButtonTop;
Jason Sams86c87ed2009-09-18 13:55:55 -0700109 private long mTouchTime;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700110
Joe Onorato6665c0f2009-09-02 15:27:24 -0700111 static class Defines {
Joe Onoratoc567acb2009-08-31 14:34:43 -0700112 public static final int ALLOC_PARAMS = 0;
113 public static final int ALLOC_STATE = 1;
Joe Onorato7bb17492009-09-24 17:51:01 -0700114 public static final int ALLOC_ICON_IDS = 3;
115 public static final int ALLOC_LABEL_IDS = 4;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700116
117 public static final int COLUMNS_PER_PAGE = 4;
118 public static final int ROWS_PER_PAGE = 4;
Jason Sams78aebd82009-09-15 13:06:59 -0700119
Joe Onorato6665c0f2009-09-02 15:27:24 -0700120 public static final int ICON_WIDTH_PX = 64;
121 public static final int ICON_TEXTURE_WIDTH_PX = 128;
122
123 public static final int ICON_HEIGHT_PX = 64;
124 public static final int ICON_TEXTURE_HEIGHT_PX = 128;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700125
126 public int SCREEN_WIDTH_PX;
127 public int SCREEN_HEIGHT_PX;
128
Joe Onoratobcbeab82009-10-01 21:45:43 -0700129 public void recompute(int w, int h) {
130 SCREEN_WIDTH_PX = 480;
131 SCREEN_HEIGHT_PX = 800;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700132 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700133 }
Joe Onorato7c312c12009-08-13 21:36:53 -0700134
135 public AllAppsView(Context context, AttributeSet attrs) {
136 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700137 setFocusable(true);
Joe Onoratob39e51a2009-10-28 15:47:49 -0400138 setSoundEffectsEnabled(false);
Joe Onorato93839052009-08-06 20:34:32 -0700139 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratof7b0e012009-10-01 14:09:15 -0700140 final ViewConfiguration config = ViewConfiguration.get(context);
Joe Onorato82ca5502009-10-15 16:59:23 -0700141 mSlop = config.getScaledTouchSlop();
Joe Onoratof7b0e012009-10-01 14:09:15 -0700142 mMaxFlingVelocity = config.getScaledMaximumFlingVelocity();
143
Joe Onorato6665c0f2009-09-02 15:27:24 -0700144 setOnClickListener(this);
145 setOnLongClickListener(this);
Dianne Hackborne52a1b52009-09-30 22:36:20 -0700146 setZOrderOnTop(true);
Jason Samsfd22dac2009-09-20 17:24:16 -0700147 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onorato93839052009-08-06 20:34:32 -0700148 }
149
Joe Onoratob39e51a2009-10-28 15:47:49 -0400150 /**
151 * If you have an attached click listener, View always plays the click sound!?!?
152 * Deal with sound effects by hand.
153 */
154 public void reallyPlaySoundEffect(int sound) {
155 boolean old = isSoundEffectsEnabled();
156 setSoundEffectsEnabled(true);
157 playSoundEffect(sound);
158 setSoundEffectsEnabled(old);
159 }
160
Joe Onorato7c312c12009-08-13 21:36:53 -0700161 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
162 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -0700163 }
164
Joe Onorato6665c0f2009-09-02 15:27:24 -0700165 public void setLauncher(Launcher launcher) {
166 mLauncher = launcher;
Joe Onorato93839052009-08-06 20:34:32 -0700167 }
168
Joe Onorato1feb3a82009-08-08 22:32:00 -0700169 @Override
Joe Onorato7bb17492009-09-24 17:51:01 -0700170 public void surfaceDestroyed(SurfaceHolder holder) {
171 super.surfaceDestroyed(holder);
172
173 destroyRenderScript();
174 mRS = null;
175 mRollo = null;
176 }
177
178 @Override
Joe Onorato93839052009-08-06 20:34:32 -0700179 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Joe Onorato080d9b62009-11-02 12:01:11 -0500180 Log.d(TAG, "starting surfaceChanged");
Joe Onorato6665c0f2009-09-02 15:27:24 -0700181 long startTime = SystemClock.uptimeMillis();
182
Joe Onorato080d9b62009-11-02 12:01:11 -0500183 super.surfaceChanged(holder, format, w, h);
184
Jason Sams81134792009-10-27 15:38:42 -0700185 if (mRS != null) {
186 destroyRenderScript();
187 mRS = null;
188 mRollo = null;
189 }
190
Jason Sams05de32a2009-09-27 14:01:40 -0700191 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700192 mRollo = new RolloRS();
193 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 if (mAllAppsList != null) {
195 mRollo.setApps(mAllAppsList);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700196 Log.d(TAG, "surfaceChanged... calling mRollo.setApps");
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700198
199 Resources res = getContext().getResources();
200 int barHeight = (int)res.getDimension(R.dimen.button_bar_height);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700201 mHomeButtonTop = h - barHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700202
203 long endTime = SystemClock.uptimeMillis();
204 Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms");
Joe Onorato93839052009-08-06 20:34:32 -0700205 }
Jason Sams2e19c052009-10-20 18:19:55 -0700206
Joe Onorato93839052009-08-06 20:34:32 -0700207 @Override
Mike Cleron7d5d7462009-10-20 14:06:00 -0700208 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
209 super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Joe Onorato859b3a72009-10-28 15:17:01 -0400210
211 if (!isVisible()) {
212 return;
213 }
214
Mike Cleron7d5d7462009-10-20 14:06:00 -0700215 if (gainFocus) {
216 if (!mArrowNavigation && mRollo.mState.iconCount > 0) {
217 // Select the first icon when we gain keyboard focus
218 mArrowNavigation = true;
219 mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE);
220 mRollo.mState.save();
221 }
222 } else {
223 if (mArrowNavigation) {
224 // Clear selection when we lose focus
225 mRollo.clearSelectedIcon();
226 mRollo.mState.save();
227 mArrowNavigation = false;
228 }
229 }
230 }
231
232 @Override
233 public boolean onKeyDown(int keyCode, KeyEvent event) {
Jason Sams2e19c052009-10-20 18:19:55 -0700234
Joe Onorato859b3a72009-10-28 15:17:01 -0400235 if (!isVisible()) {
236 return false;
237 }
238
Mike Cleron7d5d7462009-10-20 14:06:00 -0700239 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
240 if (mArrowNavigation) {
241 int whichApp = mRollo.mState.selectedIconIndex;
242 if (whichApp >= 0) {
243 ApplicationInfo app = mAllAppsList.get(whichApp);
244 mLauncher.startActivitySafely(app.intent);
245 }
246 }
247 }
Jason Sams2e19c052009-10-20 18:19:55 -0700248
Mike Cleron7d5d7462009-10-20 14:06:00 -0700249 if (mArrowNavigation && mRollo.mState.iconCount > 0) {
250 mArrowNavigation = true;
Jason Sams2e19c052009-10-20 18:19:55 -0700251
Mike Cleron7d5d7462009-10-20 14:06:00 -0700252 int currentSelection = mRollo.mState.selectedIconIndex;
253 int currentTopRow = (int) mRollo.mMessageProc.mPosX;
Jason Sams2e19c052009-10-20 18:19:55 -0700254
Mike Cleron7d5d7462009-10-20 14:06:00 -0700255 // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1
256 int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700257
Mike Cleron7d5d7462009-10-20 14:06:00 -0700258 // The row of the current selection, in the range 0..ROWS_PER_PAGE-1
259 int currentPageRow = (currentSelection - (currentTopRow * Defines.COLUMNS_PER_PAGE))
260 / Defines.ROWS_PER_PAGE;
Jason Sams2e19c052009-10-20 18:19:55 -0700261
Mike Cleron7d5d7462009-10-20 14:06:00 -0700262 int newSelection = currentSelection;
263
264 switch (keyCode) {
265 case KeyEvent.KEYCODE_DPAD_UP:
266 if (currentPageRow > 0) {
267 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
268 } else if (currentTopRow > 0) {
269 mRollo.moveTo(currentTopRow - 1);
Jason Sams2e19c052009-10-20 18:19:55 -0700270 newSelection = currentSelection - Defines.COLUMNS_PER_PAGE;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700271 }
272 break;
273 case KeyEvent.KEYCODE_DPAD_DOWN:
274 if (currentSelection < mRollo.mState.iconCount - Defines.COLUMNS_PER_PAGE) {
275 if (currentPageRow < Defines.ROWS_PER_PAGE - 1) {
276 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
277 } else {
278 mRollo.moveTo(currentTopRow + 1);
279 newSelection = currentSelection + Defines.COLUMNS_PER_PAGE;
280 }
281 }
282 break;
283 case KeyEvent.KEYCODE_DPAD_LEFT:
284 if (currentPageCol > 0) {
285 newSelection = currentSelection - 1;
286 }
287 break;
288 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jason Sams2e19c052009-10-20 18:19:55 -0700289 if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) &&
Mike Cleron7d5d7462009-10-20 14:06:00 -0700290 (currentSelection < mRollo.mState.iconCount - 1)) {
291 newSelection = currentSelection + 1;
292 }
293 break;
294 }
295 if (newSelection != currentSelection) {
296 mRollo.selectIcon(newSelection);
297 mRollo.mState.save();
298 }
299 }
300 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700301 }
302
Jason Samsd8152b92009-10-13 17:19:10 -0700303 private int mRSMode = 0;
304
Joe Onorato93839052009-08-06 20:34:32 -0700305 @Override
306 public boolean onTouchEvent(MotionEvent ev)
307 {
Mike Cleron7d5d7462009-10-20 14:06:00 -0700308 mArrowNavigation = false;
Jason Sams2e19c052009-10-20 18:19:55 -0700309
Joe Onorato7bb17492009-09-24 17:51:01 -0700310 if (!isVisible()) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700311 return true;
Joe Onoratoe3406a22009-09-03 14:36:25 -0700312 }
313
Joe Onoratofb0ca672009-09-14 17:55:46 -0400314 if (mLocks != 0) {
Joe Onorato85a02a82009-09-08 12:34:22 -0700315 return true;
316 }
317
318 super.onTouchEvent(ev);
319
Joe Onoratofb0ca672009-09-14 17:55:46 -0400320 int x = (int)ev.getX();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700321 int y = (int)ev.getY();
322
Joe Onoratobcbeab82009-10-01 21:45:43 -0700323 int action = ev.getAction();
324 switch (action) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400325 case MotionEvent.ACTION_DOWN:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700326 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
327 mTouchTracking = TRACKING_HOME;
Joe Onoratod63458b2009-10-15 21:19:09 -0700328 mRollo.setHomeSelected(true);
329 mRollo.mState.save();
Mike Cleron7d5d7462009-10-20 14:06:00 -0700330 mCurrentIconIndex = -1;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700331 } else {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700332 mTouchTracking = TRACKING_FLING;
333
334 mMotionDownRawX = (int)ev.getRawX();
335 mMotionDownRawY = (int)ev.getRawY();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700336
Mike Cleron7d5d7462009-10-20 14:06:00 -0700337 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700338 mRollo.mState.newTouchDown = 1;
339
340 if (!mRollo.checkClickOK()) {
341 mRollo.clearSelectedIcon();
342 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700343 mDownIconIndex = mCurrentIconIndex
344 = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX);
345 if (mDownIconIndex < 0) {
346 // if nothing was selected, no long press.
347 cancelLongPress();
348 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700349 }
350 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700351 mRollo.move();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700352 mVelocity = VelocityTracker.obtain();
353 mVelocity.addMovement(ev);
354 mStartedScrolling = false;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700355 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400356 break;
357 case MotionEvent.ACTION_MOVE:
358 case MotionEvent.ACTION_OUTSIDE:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700359 if (mTouchTracking == TRACKING_HOME) {
Joe Onoratod63458b2009-10-15 21:19:09 -0700360 mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]);
361 mRollo.mState.save();
Joe Onorato68ffd102009-10-15 17:59:43 -0700362 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onorato82ca5502009-10-15 16:59:23 -0700363 int rawX = (int)ev.getRawX();
364 int rawY = (int)ev.getRawY();
365 int slop;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700366 slop = Math.abs(rawY - mMotionDownRawY);
Jason Samsd8152b92009-10-13 17:19:10 -0700367
Joe Onorato82ca5502009-10-15 16:59:23 -0700368 if (!mStartedScrolling && slop < mSlop) {
369 // don't update anything so when we do start scrolling
Joe Onoratobcbeab82009-10-01 21:45:43 -0700370 // below, we get the right delta.
Joe Onorato82ca5502009-10-15 16:59:23 -0700371 mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX);
372 if (mDownIconIndex != mCurrentIconIndex) {
373 // If a different icon is selected, don't allow it to be picked up.
374 // This handles off-axis dragging.
375 cancelLongPress();
376 mCurrentIconIndex = -1;
377 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700378 } else {
Joe Onorato82ca5502009-10-15 16:59:23 -0700379 if (!mStartedScrolling) {
380 cancelLongPress();
381 mCurrentIconIndex = -1;
382 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700383 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700384 mRollo.mState.newTouchDown = 1;
Jason Samsd8152b92009-10-13 17:19:10 -0700385 mRollo.move();
Jason Sams86c87ed2009-09-18 13:55:55 -0700386
Joe Onoratobcbeab82009-10-01 21:45:43 -0700387 mStartedScrolling = true;
388 mRollo.clearSelectedIcon();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700389 mVelocity.addMovement(ev);
390 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700391 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400392 }
393 break;
394 case MotionEvent.ACTION_UP:
395 case MotionEvent.ACTION_CANCEL:
Joe Onoratobcbeab82009-10-01 21:45:43 -0700396 if (mTouchTracking == TRACKING_HOME) {
397 if (action == MotionEvent.ACTION_UP) {
398 if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400399 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700400 mLauncher.closeAllApps(true);
401 }
Joe Onoratod63458b2009-10-15 21:19:09 -0700402 mRollo.setHomeSelected(false);
403 mRollo.mState.save();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700404 }
Mike Cleron7d5d7462009-10-20 14:06:00 -0700405 mCurrentIconIndex = -1;
Joe Onorato68ffd102009-10-15 17:59:43 -0700406 } else if (mTouchTracking == TRACKING_FLING) {
Joe Onoratobcbeab82009-10-01 21:45:43 -0700407 mRollo.mState.newTouchDown = 0;
Mike Cleron7d5d7462009-10-20 14:06:00 -0700408 mRollo.mState.newPositionX = ev.getRawY() / getHeight();
Jason Sams476339d2009-09-29 18:14:38 -0700409
Jason Sams12c14a82009-10-06 14:33:15 -0700410 mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700411 mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight();
Jason Sams12c14a82009-10-06 14:33:15 -0700412 mRollo.clearSelectedIcon();
413 mRollo.mState.save();
Jason Samsd8152b92009-10-13 17:19:10 -0700414 mRollo.fling();
Joe Onoratobcbeab82009-10-01 21:45:43 -0700415
Joe Onorato539ed9d2009-10-02 10:22:14 -0700416 if (mVelocity != null) {
417 mVelocity.recycle();
418 mVelocity = null;
419 }
Joe Onoratobcbeab82009-10-01 21:45:43 -0700420 }
Joe Onorato68ffd102009-10-15 17:59:43 -0700421 mTouchTracking = TRACKING_NONE;
422 break;
Joe Onorato93839052009-08-06 20:34:32 -0700423 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700424
425 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700426 }
427
Joe Onorato6665c0f2009-09-02 15:27:24 -0700428 public void onClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700429 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400430 return;
431 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700432 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
433 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
Joe Onoratob39e51a2009-10-28 15:47:49 -0400434 reallyPlaySoundEffect(SoundEffectConstants.CLICK);
Joe Onorato82ca5502009-10-15 16:59:23 -0700435 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700436 mLauncher.startActivitySafely(app.intent);
437 }
438 }
439
440 public boolean onLongClick(View v) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700441 if (mLocks != 0 || !isVisible()) {
Joe Onoratofb0ca672009-09-14 17:55:46 -0400442 return true;
443 }
Joe Onorato82ca5502009-10-15 16:59:23 -0700444 if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex
445 && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) {
446 ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex);
Joe Onorato5162ea92009-09-03 09:39:42 -0700447
448 // We don't really have an accurate location to use. This will do.
Joe Onoratobcbeab82009-10-01 21:45:43 -0700449 int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2);
450 int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX;
Joe Onorato5162ea92009-09-03 09:39:42 -0700451
Joe Onoratobcbeab82009-10-01 21:45:43 -0700452 int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2;
453 int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2;
Joe Onorato5162ea92009-09-03 09:39:42 -0700454 mDragController.startDrag(app.iconBitmap, screenX, screenY,
Joe Onoratobcbeab82009-10-01 21:45:43 -0700455 left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX,
Joe Onorato5162ea92009-09-03 09:39:42 -0700456 this, app, DragController.DRAG_ACTION_COPY);
Joe Onoratoe3406a22009-09-03 14:36:25 -0700457
Joe Onorato7bb17492009-09-24 17:51:01 -0700458 mLauncher.closeAllApps(true);
Joe Onorato5162ea92009-09-03 09:39:42 -0700459 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700460 return true;
461 }
462
Joe Onorato5162ea92009-09-03 09:39:42 -0700463 public void setDragController(DragController dragger) {
464 mDragController = dragger;
465 }
466
467 public void onDropCompleted(View target, boolean success) {
468 }
469
Joe Onorato4db52312009-10-06 11:17:43 -0700470 /**
471 * Zoom to the specifed amount.
472 *
473 * @param amount [0..1] 0 is hidden, 1 is open
474 * @param animate Whether to animate.
475 */
Jason Sams12c14a82009-10-06 14:33:15 -0700476 public void zoom(float amount) {
Joe Onorato7bb17492009-09-24 17:51:01 -0700477 if (mRollo == null) {
478 return;
479 }
480
Joe Onoratofb0ca672009-09-14 17:55:46 -0400481 cancelLongPress();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400482 mRollo.clearSelectedIcon();
Joe Onorato1d708e72009-10-15 21:29:21 -0700483 mRollo.setHomeSelected(false);
Joe Onorato85a02a82009-09-08 12:34:22 -0700484 if (amount > 0.001f) {
Joe Onoratoc5acd422009-10-01 14:21:24 -0700485 // set in readback, so we're correct even before the next frame
Jason Sams12c14a82009-10-06 14:33:15 -0700486 mRollo.mState.zoomTarget = amount;
Joe Onorato85a02a82009-09-08 12:34:22 -0700487 } else {
Joe Onorato7bb17492009-09-24 17:51:01 -0700488 mRollo.mState.zoomTarget = 0;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700489 }
Joe Onoratofb0ca672009-09-14 17:55:46 -0400490 mRollo.mState.save();
Joe Onoratofb0ca672009-09-14 17:55:46 -0400491 }
492
493 public boolean isVisible() {
Joe Onorato7bb17492009-09-24 17:51:01 -0700494 if (mRollo == null) {
495 return false;
496 }
Jason Sams12c14a82009-10-06 14:33:15 -0700497 return mRollo.mMessageProc.mZoom > 0.001f;
Joe Onoratofb0ca672009-09-14 17:55:46 -0400498 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700499
Mike Cleronb6082fa02009-10-19 17:03:36 -0700500 /*
Joe Onorato93839052009-08-06 20:34:32 -0700501 @Override
502 public boolean onTrackballEvent(MotionEvent ev)
503 {
504 float x = ev.getX();
505 float y = ev.getY();
506 //Float tx = new Float(x);
507 //Float ty = new Float(y);
508 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
509
510
511 return true;
512 }
Mike Cleronb6082fa02009-10-19 17:03:36 -0700513 */
Joe Onorato93839052009-08-06 20:34:32 -0700514
Joe Onorato9c1289c2009-08-17 11:03:03 -0400515 public void setApps(ArrayList<ApplicationInfo> list) {
516 mAllAppsList = list;
517 if (mRollo != null) {
518 mRollo.setApps(list);
519 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700520 mPageCount = countPages(list.size());
Joe Onoratofb0ca672009-09-14 17:55:46 -0400521 mLocks &= ~LOCK_ICONS_PENDING;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700522 }
523
Joe Onoratoa8138d52009-10-06 19:25:30 -0700524 public void addApps(ArrayList<ApplicationInfo> list) {
525 final int N = list.size();
526 if (mRollo != null) {
527 mRollo.reallocAppsList(mRollo.mState.iconCount + N);
528 }
529
530 for (int i=0; i<N; i++) {
531 final ApplicationInfo item = list.get(i);
532 int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp);
533 if (index < 0) {
534 index = -(index+1);
535 }
536 mAllAppsList.add(index, item);
537 if (mRollo != null) {
538 mRollo.addApp(index, item);
539 mRollo.mState.iconCount++;
540 }
541 }
542
543 if (mRollo != null) {
544 mRollo.saveAppsList();
545 }
Joe Onoratoc567acb2009-08-31 14:34:43 -0700546 }
547
Joe Onoratoa8138d52009-10-06 19:25:30 -0700548 public void removeApps(ArrayList<ApplicationInfo> list) {
549 final int N = list.size();
550 for (int i=0; i<N; i++) {
551 final ApplicationInfo item = list.get(i);
Joe Onoratocb9f7982009-10-31 16:32:02 -0400552 int index = findAppByComponent(mAllAppsList, item);
Joe Onoratoa8138d52009-10-06 19:25:30 -0700553 if (index >= 0) {
554 mAllAppsList.remove(index);
555 if (mRollo != null) {
556 mRollo.removeApp(index);
557 mRollo.mState.iconCount--;
558 }
559 } else {
560 Log.e(TAG, "couldn't find a match for item \"" + item + "\"");
561 // Try to recover. This should keep us from crashing for now.
562 }
563 }
564
565 if (mRollo != null) {
566 mRollo.saveAppsList();
567 }
568 }
569
570 public void updateApps(String packageName, ArrayList<ApplicationInfo> list) {
571 // Just remove and add, because they may need to be re-sorted.
572 removeApps(list);
573 addApps(list);
574 }
575
576 private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() {
577 public int compare(ApplicationInfo a, ApplicationInfo b) {
578 int result = a.title.toString().compareTo(b.toString());
579 if (result != 0) {
580 return result;
581 }
582 return a.intent.getComponent().compareTo(b.intent.getComponent());
583 }
584 };
585
Joe Onoratocb9f7982009-10-31 16:32:02 -0400586 private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
587 ComponentName component = item.intent.getComponent();
588 final int N = list.size();
589 for (int i=0; i<N; i++) {
590 ApplicationInfo x = list.get(i);
591 if (x.intent.getComponent().equals(component)) {
592 return i;
593 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700594 }
Joe Onoratocb9f7982009-10-31 16:32:02 -0400595 return -1;
596 }
Joe Onoratoa8138d52009-10-06 19:25:30 -0700597
Joe Onoratoc567acb2009-08-31 14:34:43 -0700598 private static int countPages(int iconCount) {
599 int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE;
600 int pages = iconCount / iconsPerPage;
601 if (pages*iconsPerPage != iconCount) {
602 pages++;
603 }
604 return pages;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400605 }
606
Joe Onorato93839052009-08-06 20:34:32 -0700607 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700608
Joe Onorato1feb3a82009-08-08 22:32:00 -0700609 // Allocations ======
Joe Onorato93839052009-08-06 20:34:32 -0700610 private int mWidth;
611 private int mHeight;
612
613 private Resources mRes;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700614 private Script mScript;
615 private Script.Invokable mInvokeMove;
Jason Samsc1c521e2009-10-19 14:45:45 -0700616 private Script.Invokable mInvokeMoveTo;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700617 private Script.Invokable mInvokeFling;
618 private Script.Invokable mInvokeResetWAR;
Jason Sams86c87ed2009-09-18 13:55:55 -0700619
Jason Samsc1c521e2009-10-19 14:45:45 -0700620
Jason Samscd689e12009-09-29 15:28:22 -0700621 private ProgramStore mPSIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700622 private ProgramStore mPSText;
Jason Samscd689e12009-09-29 15:28:22 -0700623 private ProgramFragment mPFColor;
Jason Samsc8514792009-10-29 14:27:29 -0700624 private ProgramFragment mPFTexMip;
625 private ProgramFragment mPFTexNearest;
Joe Onorato93839052009-08-06 20:34:32 -0700626 private ProgramVertex mPV;
Joe Onorato93839052009-08-06 20:34:32 -0700627 private ProgramVertex mPVOrtho;
Jason Sams0aa71662009-10-02 18:43:18 -0700628 private SimpleMesh mMesh;
Jason Samsd8152b92009-10-13 17:19:10 -0700629 private SimpleMesh mMesh2;
Joe Onorato93839052009-08-06 20:34:32 -0700630
Joe Onoratod63458b2009-10-15 21:19:09 -0700631 private Allocation mHomeButtonNormal;
632 private Allocation mHomeButtonPressed;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700633
Joe Onoratobf15cb42009-08-07 14:33:40 -0700634 private Allocation[] mIcons;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700635 private int[] mIconIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700636 private Allocation mAllocIconIds;
Joe Onorato93839052009-08-06 20:34:32 -0700637
Joe Onoratobf15cb42009-08-07 14:33:40 -0700638 private Allocation[] mLabels;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700639 private int[] mLabelIds;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700640 private Allocation mAllocLabelIds;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700641 private Allocation mSelectedIcon;
Joe Onorato93839052009-08-06 20:34:32 -0700642
Joe Onorato6665c0f2009-09-02 15:27:24 -0700643 private int[] mTouchYBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700644 private int[] mTouchXBorders;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700645
646 private Bitmap mSelectionBitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400647 private Canvas mSelectionCanvas;
Joe Onorato93839052009-08-06 20:34:32 -0700648
Jason Samsd8152b92009-10-13 17:19:10 -0700649
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700650 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700651 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700652
Jason Sams78aebd82009-09-15 13:06:59 -0700653 class BaseAlloc {
654 Allocation mAlloc;
655 Type mType;
656
657 void save() {
658 mAlloc.data(this);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700659 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700660 }
661
Jason Sams12c14a82009-10-06 14:33:15 -0700662 class AAMessage extends RenderScript.RSMessage {
663 public void run() {
664 mPosX = ((float)mData[0]) / (1 << 16);
665 mVelocity = ((float)mData[1]) / (1 << 16);
666 mZoom = ((float)mData[2]) / (1 << 16);
667 //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom);
668 }
669 float mZoom;
670 float mPosX;
671 float mVelocity;
672 }
673 AAMessage mMessageProc;
674
Jason Sams476339d2009-09-29 18:14:38 -0700675 private boolean checkClickOK() {
676 //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX));
Jason Sams2e19c052009-10-20 18:19:55 -0700677 return (Math.abs(mMessageProc.mVelocity) < 0.4f) &&
678 (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f);
Jason Sams476339d2009-09-29 18:14:38 -0700679 }
680
Jason Sams78aebd82009-09-15 13:06:59 -0700681 class Params extends BaseAlloc {
682 Params() {
683 mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass");
684 mAlloc = Allocation.createTyped(mRS, mType);
685 save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700686 }
Jason Sams78aebd82009-09-15 13:06:59 -0700687 public int bubbleWidth;
688 public int bubbleHeight;
689 public int bubbleBitmapWidth;
690 public int bubbleBitmapHeight;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700691
Joe Onoratobcbeab82009-10-01 21:45:43 -0700692 public int homeButtonWidth;
693 public int homeButtonHeight;
694 public int homeButtonTextureWidth;
695 public int homeButtonTextureHeight;
Jason Sams78aebd82009-09-15 13:06:59 -0700696 }
697
698 class State extends BaseAlloc {
Jason Sams86c87ed2009-09-18 13:55:55 -0700699 public float newPositionX;
700 public int newTouchDown;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700701 public float flingVelocity;
Jason Sams78aebd82009-09-15 13:06:59 -0700702 public int iconCount;
Jason Sams78aebd82009-09-15 13:06:59 -0700703 public int selectedIconIndex = -1;
704 public int selectedIconTexture;
Joe Onorato7bb17492009-09-24 17:51:01 -0700705 public float zoomTarget;
Joe Onoratod63458b2009-10-15 21:19:09 -0700706 public int homeButtonId;
Jason Samsc1c521e2009-10-19 14:45:45 -0700707 public float targetPos;
Jason Sams78aebd82009-09-15 13:06:59 -0700708
709 State() {
710 mType = Type.createFromClass(mRS, State.class, 1, "StateClass");
711 mAlloc = Allocation.createTyped(mRS, mType);
712 save();
713 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700714 }
715
716 public RolloRS() {
717 }
718
719 public void init(Resources res, int width, int height) {
720 mRes = res;
721 mWidth = width;
722 mHeight = height;
Joe Onoratobcbeab82009-10-01 21:45:43 -0700723 mDefines.recompute(width, height);
Jason Samscd689e12009-09-29 15:28:22 -0700724 initProgramVertex();
725 initProgramFragment();
726 initProgramStore();
Jason Sams0aa71662009-10-02 18:43:18 -0700727 initMesh();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700728 initGl();
729 initData();
Joe Onorato6665c0f2009-09-02 15:27:24 -0700730 initTouchState();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700731 initRs();
732 }
733
Jason Sams0aa71662009-10-02 18:43:18 -0700734 public void initMesh() {
735 SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3,
736 SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR);
737
Jason Samsd8152b92009-10-13 17:19:10 -0700738 float y = 0;
739 float z = 0;
740 for (int ct=0; ct < 200; ct++) {
741 float angle = 0;
742 float maxAngle = 3.14f * 0.16f;
743 float l = 1.f;
744
745 l = 1 - ((ct-5) * 0.10f);
746 if (ct > 7) {
747 angle = maxAngle * (ct - 7) * 0.2f;
748 angle = Math.min(angle, maxAngle);
749 }
Jason Samsc8514792009-10-29 14:27:29 -0700750 l = Math.max(0.4f, l);
Jason Samsd8152b92009-10-13 17:19:10 -0700751 l = Math.min(1.0f, l);
752
753 y += 0.1f * Math.cos(angle);
754 z += 0.1f * Math.sin(angle);
755
756 float t = 0.1f * ct;
757 float ds = 0.08f;
758 tm.setColor(l, l, l, 0.99f);
759 tm.setTexture(ds, t);
760 tm.addVertex(-0.5f, y, z);
761 tm.setTexture(1 - ds, t);
762 tm.addVertex(0.5f, y, z);
763 }
764 for (int ct=0; ct < (200 * 2 - 2); ct+= 2) {
765 tm.addTriangle(ct, ct+1, ct+2);
766 tm.addTriangle(ct+1, ct+3, ct+2);
767 }
768 mMesh2 = tm.create();
Jason Sams37e7c2b2009-10-19 12:55:43 -0700769 mMesh2.setName("SMMesh");
Jason Samsd8152b92009-10-13 17:19:10 -0700770 }
771
Jason Samscd689e12009-09-29 15:28:22 -0700772 private void initProgramVertex() {
773 ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS);
774 pva.setupProjectionNormalized(mWidth, mHeight);
Joe Onorato93839052009-08-06 20:34:32 -0700775
776 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
Jason Sams0aa71662009-10-02 18:43:18 -0700777 pvb.setTextureMatrixEnable(true);
Joe Onorato93839052009-08-06 20:34:32 -0700778 mPV = pvb.create();
779 mPV.setName("PV");
Jason Samscd689e12009-09-29 15:28:22 -0700780 mPV.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700781
Jason Sams37e7c2b2009-10-19 12:55:43 -0700782 //pva = new ProgramVertex.MatrixAllocation(mRS);
783 //pva.setupOrthoWindow(mWidth, mHeight);
784 //pvb.setTextureMatrixEnable(true);
785 //mPVOrtho = pvb.create();
786 //mPVOrtho.setName("PVOrtho");
787 //mPVOrtho.bindAllocation(pva);
Joe Onorato93839052009-08-06 20:34:32 -0700788
789 mRS.contextBindProgramVertex(mPV);
Jason Samscd689e12009-09-29 15:28:22 -0700790 }
Joe Onorato93839052009-08-06 20:34:32 -0700791
Jason Samscd689e12009-09-29 15:28:22 -0700792 private void initProgramFragment() {
793 Sampler.Builder sb = new Sampler.Builder(mRS);
Jason Samsc8514792009-10-29 14:27:29 -0700794 sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR);
Jason Samscd689e12009-09-29 15:28:22 -0700795 sb.setMag(Sampler.Value.LINEAR);
796 sb.setWrapS(Sampler.Value.CLAMP);
797 sb.setWrapT(Sampler.Value.CLAMP);
798 Sampler linear = sb.create();
799
800 sb.setMin(Sampler.Value.NEAREST);
801 sb.setMag(Sampler.Value.NEAREST);
802 Sampler nearest = sb.create();
803
804 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700805 //mPFColor = bf.create();
806 //mPFColor.setName("PFColor");
Jason Samscd689e12009-09-29 15:28:22 -0700807
808 bf.setTexEnable(true, 0);
809 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
Jason Samsc8514792009-10-29 14:27:29 -0700810 mPFTexMip = bf.create();
811 mPFTexMip.setName("PFTexMip");
812 mPFTexMip.bindSampler(linear, 0);
813
814 mPFTexNearest = bf.create();
815 mPFTexNearest.setName("PFTexNearest");
816 mPFTexNearest.bindSampler(nearest, 0);
Jason Samscd689e12009-09-29 15:28:22 -0700817 }
818
819 private void initProgramStore() {
820 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
821 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Jason Sams12c14a82009-10-06 14:33:15 -0700822 bs.setColorMask(true,true,true,false);
Jason Samscd689e12009-09-29 15:28:22 -0700823 bs.setDitherEnable(true);
824 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
825 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
826 mPSIcons = bs.create();
827 mPSIcons.setName("PSIcons");
828
829 //bs.setDitherEnable(false);
830 //mPSText = bs.create();
831 //mPSText.setName("PSText");
832 }
833
834 private void initGl() {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700835 mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1];
Joe Onorato6665c0f2009-09-02 15:27:24 -0700836 mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1];
Joe Onoratobf15cb42009-08-07 14:33:40 -0700837 }
Jason Sams78aebd82009-09-15 13:06:59 -0700838
Joe Onorato1feb3a82009-08-08 22:32:00 -0700839 private void initData() {
Jason Sams78aebd82009-09-15 13:06:59 -0700840 mParams = new Params();
841 mState = new State();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700842
Joe Onoratobf15cb42009-08-07 14:33:40 -0700843 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700844
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700845 mParams.bubbleWidth = bubble.getBubbleWidth();
846 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
847 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
848 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700849
Joe Onoratod63458b2009-10-15 21:19:09 -0700850 mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes,
851 R.drawable.home_button_normal, Element.RGBA_8888(mRS), false);
852 mHomeButtonNormal.uploadToTexture(0);
853 mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes,
854 R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false);
855 mHomeButtonPressed.uploadToTexture(0);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700856 mParams.homeButtonWidth = 76;
857 mParams.homeButtonHeight = 68;
858 mParams.homeButtonTextureWidth = 128;
859 mParams.homeButtonTextureHeight = 128;
Joe Onoratoc567acb2009-08-31 14:34:43 -0700860
Joe Onoratod63458b2009-10-15 21:19:09 -0700861 mState.homeButtonId = mHomeButtonNormal.getID();
862
Joe Onorato1feb3a82009-08-08 22:32:00 -0700863 mParams.save();
864 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400865
Joe Onorato1291a8c2009-09-15 15:07:25 -0400866 mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX,
867 Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888);
868 mSelectionCanvas = new Canvas(mSelectionBitmap);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700869
Joe Onorato9c1289c2009-08-17 11:03:03 -0400870 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700871 }
872
Jason Sams37e7c2b2009-10-19 12:55:43 -0700873 private void initScript(int id) {
874 }
875
876 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700877 ScriptC.Builder sb = new ScriptC.Builder(mRS);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700878 sb.setScript(mRes, R.raw.rollo3);
Joe Onorato93839052009-08-06 20:34:32 -0700879 sb.setRoot(true);
Joe Onoratobcbeab82009-10-01 21:45:43 -0700880 sb.addDefines(mDefines);
Jason Sams78aebd82009-09-15 13:06:59 -0700881 sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS);
882 sb.setType(mState.mType, "state", Defines.ALLOC_STATE);
Jason Sams37e7c2b2009-10-19 12:55:43 -0700883 mInvokeMove = sb.addInvokable("move");
884 mInvokeFling = sb.addInvokable("fling");
Jason Samsc1c521e2009-10-19 14:45:45 -0700885 mInvokeMoveTo = sb.addInvokable("moveTo");
Jason Sams37e7c2b2009-10-19 12:55:43 -0700886 mInvokeResetWAR = sb.addInvokable("resetHWWar");
887 mScript = sb.create();
888 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
889 mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS);
890 mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE);
891 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
892 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700893
Jason Sams12c14a82009-10-06 14:33:15 -0700894 mMessageProc = new AAMessage();
895 mRS.mMessageCallback = mMessageProc;
Jason Sams37e7c2b2009-10-19 12:55:43 -0700896 mRS.contextBindRootScript(mScript);
Joe Onorato93839052009-08-06 20:34:32 -0700897 }
Joe Onorato93839052009-08-06 20:34:32 -0700898
Joe Onorato9c1289c2009-08-17 11:03:03 -0400899 private void setApps(ArrayList<ApplicationInfo> list) {
900 final int count = list != null ? list.size() : 0;
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700901 int allocCount = count;
Joe Onoratoa8138d52009-10-06 19:25:30 -0700902 if (allocCount < 1) {
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700903 allocCount = 1;
904 }
905
Joe Onorato9c1289c2009-08-17 11:03:03 -0400906 mIcons = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700907 mIconIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700908 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400909
910 mLabels = new Allocation[count];
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700911 mLabelIds = new int[allocCount];
Joe Onoratoa8138d52009-10-06 19:25:30 -0700912 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400913
Jason Sams0a8dc2c2009-09-27 17:51:44 -0700914 Element ie8888 = Element.RGBA_8888(mRS);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400915
916 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
917
918 for (int i=0; i<count; i++) {
Joe Onoratoa8138d52009-10-06 19:25:30 -0700919 uploadAppIcon(i, list.get(i));
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920 }
921
Joe Onorato9c1289c2009-08-17 11:03:03 -0400922 mState.iconCount = count;
923
Joe Onoratoa8138d52009-10-06 19:25:30 -0700924 saveAppsList();
925 }
926
Jason Samsc8514792009-10-29 14:27:29 -0700927 private void frameBitmapAllocMips(Allocation alloc, int w, int h) {
928 int black[] = new int[w > h ? w : h];
929 Allocation.Adapter2D a = alloc.createAdapter2D();
930 int mip = 0;
931 while (w > 1 || h > 1) {
932 a.subData(0, 0, 1, h, black);
933 a.subData(w-1, 0, 1, h, black);
934 a.subData(0, 0, w, 1, black);
935 a.subData(0, h-1, w, 1, black);
936 mip++;
937 w = (w + 1) >> 1;
938 h = (h + 1) >> 1;
939 a.setConstraint(Dimension.LOD, mip);
940 }
941 a.subData(0, 0, 1, 1, black);
942 }
943
Joe Onoratoa8138d52009-10-06 19:25:30 -0700944 private void uploadAppIcon(int index, ApplicationInfo item) {
945 mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap,
Jason Samsc8514792009-10-29 14:27:29 -0700946 Element.RGBA_8888(mRS), true);
947 frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight());
948
Joe Onoratoa8138d52009-10-06 19:25:30 -0700949 mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap,
Jason Samsc8514792009-10-29 14:27:29 -0700950 Element.RGBA_8888(mRS), true);
951 frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight());
Joe Onoratoa8138d52009-10-06 19:25:30 -0700952
953 mIcons[index].uploadToTexture(0);
954 mLabels[index].uploadToTexture(0);
955
956 mIconIds[index] = mIcons[index].getID();
957 mLabelIds[index] = mLabels[index].getID();
958 }
959
960 /**
961 * Puts the empty spaces at the end. Updates mState.iconCount. You must
962 * fill in the values and call saveAppsList().
963 */
964 private void reallocAppsList(int count) {
965 Allocation[] icons = new Allocation[count];
966 int[] iconIds = new int[count];
967 mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
968
969 Allocation[] labels = new Allocation[count];
970 int[] labelIds = new int[count];
971 mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count);
972
973 final int oldCount = mIcons.length;
974
975 System.arraycopy(mIcons, 0, icons, 0, oldCount);
976 System.arraycopy(mIconIds, 0, iconIds, 0, oldCount);
977 System.arraycopy(mLabels, 0, labels, 0, oldCount);
978 System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount);
979
980 mIcons = icons;
981 mIconIds = iconIds;
982 mLabels = labels;
983 mLabelIds = labelIds;
984 }
985
986 /**
987 * Handle the allocations for the new app. Make sure you call saveAppsList when done.
988 */
989 private void addApp(int index, ApplicationInfo item) {
990 final int count = mState.iconCount - index;
991 final int dest = index + 1;
992
993 System.arraycopy(mIcons, index, mIcons, dest, count);
994 System.arraycopy(mIconIds, index, mIconIds, dest, count);
995 System.arraycopy(mLabels, index, mLabels, dest, count);
996 System.arraycopy(mLabelIds, index, mLabelIds, dest, count);
997
998 uploadAppIcon(index, item);
999 }
1000
1001 /**
1002 * Handle the allocations for the removed app. Make sure you call saveAppsList when done.
1003 */
1004 private void removeApp(int index) {
1005 final int count = mState.iconCount - index - 1;
1006 final int src = index + 1;
1007
1008 System.arraycopy(mIcons, src, mIcons, index, count);
1009 System.arraycopy(mIconIds, src, mIconIds, index, count);
1010 System.arraycopy(mLabels, src, mLabels, index, count);
1011 System.arraycopy(mLabelIds, src, mLabelIds, index, count);
1012
1013 final int last = mState.iconCount - 1;
1014 mIcons[last] = null;
1015 mIconIds[last] = 0;
1016 mLabels[last] = null;
1017 mLabelIds[last] = 0;
1018 }
1019
1020 /**
1021 * Send the apps list structures to RS.
1022 */
1023 private void saveAppsList() {
1024 mRS.contextBindRootScript(null);
Jason Samsd8152b92009-10-13 17:19:10 -07001025
Joe Onoratoa8138d52009-10-06 19:25:30 -07001026 mAllocIconIds.data(mIconIds);
1027 mAllocLabelIds.data(mLabelIds);
1028
Jason Sams37e7c2b2009-10-19 12:55:43 -07001029 if (mScript != null) { // this happens when we init it
1030 mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS);
1031 mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 }
1033
1034 mState.save();
Joe Onoratoa8138d52009-10-06 19:25:30 -07001035
1036 // Note: mScript may be null if we haven't initialized it yet.
1037 // In that case, this is a no-op.
Jason Sams37e7c2b2009-10-19 12:55:43 -07001038 if (mInvokeResetWAR != null) {
1039 mInvokeResetWAR.execute();
Jason Sams41b61c82009-10-15 15:40:54 -07001040 }
Jason Sams37e7c2b2009-10-19 12:55:43 -07001041 mRS.contextBindRootScript(mScript);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042 }
Joe Onorato6665c0f2009-09-02 15:27:24 -07001043
1044 void initTouchState() {
1045 int width = getWidth();
1046 int height = getHeight();
Jason Sams37e7c2b2009-10-19 12:55:43 -07001047 int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE;
1048 int cellWidth = width / Defines.COLUMNS_PER_PAGE;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001049
Jason Sams37e7c2b2009-10-19 12:55:43 -07001050 int centerY = (height / 2);
1051 mTouchYBorders[0] = centerY - (cellHeight * 2);
1052 mTouchYBorders[1] = centerY - cellHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001053 mTouchYBorders[2] = centerY;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001054 mTouchYBorders[3] = centerY + cellHeight;
1055 mTouchYBorders[4] = centerY + (cellHeight * 2);
Jason Sams78aebd82009-09-15 13:06:59 -07001056
Joe Onorato6665c0f2009-09-02 15:27:24 -07001057 int centerX = (width / 2);
Jason Sams37e7c2b2009-10-19 12:55:43 -07001058 mTouchXBorders[0] = 0;
1059 mTouchXBorders[1] = centerX - (width / 4);
Joe Onorato6665c0f2009-09-02 15:27:24 -07001060 mTouchXBorders[2] = centerX;
Jason Sams37e7c2b2009-10-19 12:55:43 -07001061 mTouchXBorders[3] = centerX + (width / 4);
1062 mTouchXBorders[4] = width;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001063 }
1064
Joe Onorato664457d2009-10-28 16:30:34 -04001065 void fling() {
1066 mInvokeFling.execute();
1067 }
1068
1069 void move() {
1070 mInvokeMove.execute();
1071 }
1072
1073 void moveTo(float row) {
1074 mState.targetPos = row;
1075 mState.save();
1076 mInvokeMoveTo.execute();
1077 }
1078
Jason Sams37e7c2b2009-10-19 12:55:43 -07001079 int chooseTappedIcon(int x, int y, float pos) {
1080 // Adjust for scroll position if not zero.
1081 y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]);
Jason Sams86c87ed2009-09-18 13:55:55 -07001082
Joe Onorato6665c0f2009-09-02 15:27:24 -07001083 int col = -1;
1084 int row = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001085 for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) {
1086 if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) {
1087 col = i;
1088 break;
1089 }
1090 }
1091 for (int i=0; i<Defines.ROWS_PER_PAGE; i++) {
1092 if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) {
1093 row = i;
1094 break;
1095 }
1096 }
1097
1098 if (row < 0 || col < 0) {
1099 return -1;
1100 }
1101
Joe Onorato664457d2009-10-28 16:30:34 -04001102 int index = (((int)pos) * Defines.COLUMNS_PER_PAGE)
Joe Onorato6665c0f2009-09-02 15:27:24 -07001103 + (row * Defines.ROWS_PER_PAGE) + col;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001104
Joe Onorato664457d2009-10-28 16:30:34 -04001105 if (index >= mState.iconCount) {
1106 return -1;
1107 } else {
1108 return index;
1109 }
Jason Samsc1c521e2009-10-19 14:45:45 -07001110 }
1111
Joe Onorato6665c0f2009-09-02 15:27:24 -07001112 /**
1113 * You need to call save() on mState on your own after calling this.
Joe Onorato82ca5502009-10-15 16:59:23 -07001114 *
1115 * @return the index of the icon that was selected.
Joe Onorato6665c0f2009-09-02 15:27:24 -07001116 */
Joe Onorato82ca5502009-10-15 16:59:23 -07001117 int selectIcon(int x, int y, float pos) {
1118 final int index = chooseTappedIcon(x, y, pos);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001119 selectIcon(index);
Joe Onorato82ca5502009-10-15 16:59:23 -07001120 return index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001121 }
1122
1123 void selectIcon(int index) {
Jason Samsc8514792009-10-29 14:27:29 -07001124 if (index < 0 || index >= mAllAppsList.size()) {
Joe Onorato6665c0f2009-09-02 15:27:24 -07001125 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001126 } else {
1127 mState.selectedIconIndex = index;
Joe Onorato1291a8c2009-09-15 15:07:25 -04001128
1129 Bitmap selectionBitmap = mSelectionBitmap;
1130
1131 Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas,
1132 selectionBitmap.getWidth(), selectionBitmap.getHeight(),
1133 mAllAppsList.get(index).iconBitmap);
1134
1135 mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap,
Jason Sams0a8dc2c2009-09-27 17:51:44 -07001136 Element.RGBA_8888(mRS), false);
Joe Onorato1291a8c2009-09-15 15:07:25 -04001137 mSelectedIcon.uploadToTexture(0);
1138 mState.selectedIconTexture = mSelectedIcon.getID();
Joe Onorato6665c0f2009-09-02 15:27:24 -07001139 }
1140 }
1141
1142 /**
1143 * You need to call save() on mState on your own after calling this.
1144 */
1145 void clearSelectedIcon() {
Joe Onorato2ca51dc2009-09-16 11:44:14 -04001146 mState.selectedIconIndex = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -07001147 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07001148
Joe Onoratod63458b2009-10-15 21:19:09 -07001149 void setHomeSelected(boolean pressed) {
1150 if (pressed) {
1151 mState.homeButtonId = mHomeButtonPressed.getID();
1152 } else {
1153 mState.homeButtonId = mHomeButtonNormal.getID();
1154 }
1155 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04001156 }
Joe Onorato93839052009-08-06 20:34:32 -07001157}
1158
1159