Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import java.io.Writer; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.concurrent.Semaphore; |
| 22 | import java.lang.Float; |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 23 | import java.util.Collections; |
| 24 | import java.util.Comparator; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 25 | |
| 26 | import android.renderscript.RSSurfaceView; |
| 27 | import android.renderscript.RenderScript; |
| 28 | |
| 29 | import android.renderscript.RenderScript; |
| 30 | import android.renderscript.ProgramVertex; |
| 31 | import android.renderscript.Element; |
| 32 | import android.renderscript.Allocation; |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 33 | import android.renderscript.Type; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 34 | import android.renderscript.Script; |
| 35 | import android.renderscript.ScriptC; |
| 36 | import android.renderscript.ProgramFragment; |
| 37 | import android.renderscript.ProgramStore; |
| 38 | import android.renderscript.Sampler; |
Jason Sams | 0aa7166 | 2009-10-02 18:43:18 -0700 | [diff] [blame] | 39 | import android.renderscript.SimpleMesh; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 40 | |
| 41 | import android.content.Context; |
| 42 | import android.content.res.Resources; |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 43 | import android.database.DataSetObserver; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 44 | import android.graphics.Bitmap; |
| 45 | import android.graphics.BitmapFactory; |
| 46 | import android.graphics.Canvas; |
| 47 | import android.graphics.Paint; |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 48 | import android.graphics.Rect; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 49 | import android.graphics.drawable.BitmapDrawable; |
| 50 | import android.graphics.drawable.Drawable; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 51 | import android.os.Message; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 52 | import android.os.SystemClock; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 53 | import android.util.AttributeSet; |
| 54 | import android.util.Log; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 55 | import android.view.KeyEvent; |
| 56 | import android.view.MotionEvent; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 57 | import android.view.Surface; |
| 58 | import android.view.SurfaceHolder; |
| 59 | import android.view.SurfaceView; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 60 | import android.view.View; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 61 | import android.view.VelocityTracker; |
| 62 | import android.view.ViewConfiguration; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 63 | import android.graphics.PixelFormat; |
| 64 | |
| 65 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 66 | public class AllAppsView extends RSSurfaceView |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 67 | implements View.OnClickListener, View.OnLongClickListener, DragSource { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 68 | private static final String TAG = "Launcher.AllAppsView"; |
| 69 | |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 70 | /** Bit for mLocks for when there are icons being loaded. */ |
| 71 | private static final int LOCK_ICONS_PENDING = 1; |
| 72 | |
Joe Onorato | 68ffd10 | 2009-10-15 17:59:43 -0700 | [diff] [blame] | 73 | private static final int TRACKING_NONE = 0; |
| 74 | private static final int TRACKING_FLING = 1; |
| 75 | private static final int TRACKING_HOME = 2; |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 76 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 77 | private Launcher mLauncher; |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 78 | private DragController mDragController; |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 79 | |
| 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 Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 83 | |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 84 | private int mSlop; |
Joe Onorato | f7b0e01 | 2009-10-01 14:09:15 -0700 | [diff] [blame] | 85 | private int mMaxFlingVelocity; |
| 86 | |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 87 | private Defines mDefines = new Defines(); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 88 | private RenderScript mRS; |
| 89 | private RolloRS mRollo; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 90 | private ArrayList<ApplicationInfo> mAllAppsList; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 91 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 92 | /** |
| 93 | * True when we are using arrow keys or trackball to drive navigation |
| 94 | */ |
| 95 | private boolean mArrowNavigation = false; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 96 | |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 97 | private int mPageCount; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 98 | private boolean mStartedScrolling; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 99 | private VelocityTracker mVelocity; |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 100 | private int mTouchTracking; |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 101 | private int mMotionDownRawX; |
| 102 | private int mMotionDownRawY; |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 103 | private int mDownIconIndex = -1; |
| 104 | private int mCurrentIconIndex = -1; |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 105 | private int mHomeButtonTop; |
Jason Sams | 86c87ed | 2009-09-18 13:55:55 -0700 | [diff] [blame] | 106 | private long mTouchTime; |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 107 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 108 | static class Defines { |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 109 | public static final int ALLOC_PARAMS = 0; |
| 110 | public static final int ALLOC_STATE = 1; |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 111 | public static final int ALLOC_ICON_IDS = 3; |
| 112 | public static final int ALLOC_LABEL_IDS = 4; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 113 | |
| 114 | public static final int COLUMNS_PER_PAGE = 4; |
| 115 | public static final int ROWS_PER_PAGE = 4; |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 116 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 117 | 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 Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 122 | |
| 123 | public int SCREEN_WIDTH_PX; |
| 124 | public int SCREEN_HEIGHT_PX; |
| 125 | |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 126 | public void recompute(int w, int h) { |
| 127 | SCREEN_WIDTH_PX = 480; |
| 128 | SCREEN_HEIGHT_PX = 800; |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 129 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 130 | } |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 131 | |
| 132 | public AllAppsView(Context context, AttributeSet attrs) { |
| 133 | super(context, attrs); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 134 | setFocusable(true); |
| 135 | getHolder().setFormat(PixelFormat.TRANSLUCENT); |
Joe Onorato | f7b0e01 | 2009-10-01 14:09:15 -0700 | [diff] [blame] | 136 | final ViewConfiguration config = ViewConfiguration.get(context); |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 137 | mSlop = config.getScaledTouchSlop(); |
Joe Onorato | f7b0e01 | 2009-10-01 14:09:15 -0700 | [diff] [blame] | 138 | mMaxFlingVelocity = config.getScaledMaximumFlingVelocity(); |
| 139 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 140 | setOnClickListener(this); |
| 141 | setOnLongClickListener(this); |
Dianne Hackborn | e52a1b5 | 2009-09-30 22:36:20 -0700 | [diff] [blame] | 142 | setZOrderOnTop(true); |
Jason Sams | fd22dac | 2009-09-20 17:24:16 -0700 | [diff] [blame] | 143 | getHolder().setFormat(PixelFormat.TRANSLUCENT); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 146 | public AllAppsView(Context context, AttributeSet attrs, int defStyle) { |
| 147 | this(context, attrs); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 150 | public void setLauncher(Launcher launcher) { |
| 151 | mLauncher = launcher; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 154 | @Override |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 155 | public void surfaceDestroyed(SurfaceHolder holder) { |
| 156 | super.surfaceDestroyed(holder); |
| 157 | |
| 158 | destroyRenderScript(); |
| 159 | mRS = null; |
| 160 | mRollo = null; |
| 161 | } |
| 162 | |
| 163 | @Override |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 164 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
| 165 | super.surfaceChanged(holder, format, w, h); |
| 166 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 167 | long startTime = SystemClock.uptimeMillis(); |
| 168 | |
Jason Sams | 8113479 | 2009-10-27 15:38:42 -0700 | [diff] [blame^] | 169 | if (mRS != null) { |
| 170 | destroyRenderScript(); |
| 171 | mRS = null; |
| 172 | mRollo = null; |
| 173 | } |
| 174 | |
Jason Sams | 05de32a | 2009-09-27 14:01:40 -0700 | [diff] [blame] | 175 | mRS = createRenderScript(true); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 176 | mRollo = new RolloRS(); |
| 177 | mRollo.init(getResources(), w, h); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 178 | if (mAllAppsList != null) { |
| 179 | mRollo.setApps(mAllAppsList); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 180 | Log.d(TAG, "surfaceChanged... calling mRollo.setApps"); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 181 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 182 | |
| 183 | Resources res = getContext().getResources(); |
| 184 | int barHeight = (int)res.getDimension(R.dimen.button_bar_height); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 185 | mHomeButtonTop = h - barHeight; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 186 | |
| 187 | long endTime = SystemClock.uptimeMillis(); |
| 188 | Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms"); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 189 | } |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 190 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 191 | |
| 192 | @Override |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 193 | protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { |
| 194 | super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); |
| 195 | if (gainFocus) { |
| 196 | if (!mArrowNavigation && mRollo.mState.iconCount > 0) { |
| 197 | // Select the first icon when we gain keyboard focus |
| 198 | mArrowNavigation = true; |
| 199 | mRollo.selectIcon(Math.round(mRollo.mMessageProc.mPosX) * Defines.COLUMNS_PER_PAGE); |
| 200 | mRollo.mState.save(); |
| 201 | } |
| 202 | } else { |
| 203 | if (mArrowNavigation) { |
| 204 | // Clear selection when we lose focus |
| 205 | mRollo.clearSelectedIcon(); |
| 206 | mRollo.mState.save(); |
| 207 | mArrowNavigation = false; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 214 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 215 | if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) { |
| 216 | if (mArrowNavigation) { |
| 217 | int whichApp = mRollo.mState.selectedIconIndex; |
| 218 | if (whichApp >= 0) { |
| 219 | ApplicationInfo app = mAllAppsList.get(whichApp); |
| 220 | mLauncher.startActivitySafely(app.intent); |
| 221 | } |
| 222 | } |
| 223 | } |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 224 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 225 | if (mArrowNavigation && mRollo.mState.iconCount > 0) { |
| 226 | mArrowNavigation = true; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 227 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 228 | int currentSelection = mRollo.mState.selectedIconIndex; |
| 229 | int currentTopRow = (int) mRollo.mMessageProc.mPosX; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 230 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 231 | // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1 |
| 232 | int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 233 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 234 | // The row of the current selection, in the range 0..ROWS_PER_PAGE-1 |
| 235 | int currentPageRow = (currentSelection - (currentTopRow * Defines.COLUMNS_PER_PAGE)) |
| 236 | / Defines.ROWS_PER_PAGE; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 237 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 238 | int newSelection = currentSelection; |
| 239 | |
| 240 | switch (keyCode) { |
| 241 | case KeyEvent.KEYCODE_DPAD_UP: |
| 242 | if (currentPageRow > 0) { |
| 243 | newSelection = currentSelection - Defines.COLUMNS_PER_PAGE; |
| 244 | } else if (currentTopRow > 0) { |
| 245 | mRollo.moveTo(currentTopRow - 1); |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 246 | newSelection = currentSelection - Defines.COLUMNS_PER_PAGE; |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 247 | } |
| 248 | break; |
| 249 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 250 | if (currentSelection < mRollo.mState.iconCount - Defines.COLUMNS_PER_PAGE) { |
| 251 | if (currentPageRow < Defines.ROWS_PER_PAGE - 1) { |
| 252 | newSelection = currentSelection + Defines.COLUMNS_PER_PAGE; |
| 253 | } else { |
| 254 | mRollo.moveTo(currentTopRow + 1); |
| 255 | newSelection = currentSelection + Defines.COLUMNS_PER_PAGE; |
| 256 | } |
| 257 | } |
| 258 | break; |
| 259 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 260 | if (currentPageCol > 0) { |
| 261 | newSelection = currentSelection - 1; |
| 262 | } |
| 263 | break; |
| 264 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 265 | if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) && |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 266 | (currentSelection < mRollo.mState.iconCount - 1)) { |
| 267 | newSelection = currentSelection + 1; |
| 268 | } |
| 269 | break; |
| 270 | } |
| 271 | if (newSelection != currentSelection) { |
| 272 | mRollo.selectIcon(newSelection); |
| 273 | mRollo.mState.save(); |
| 274 | } |
| 275 | } |
| 276 | return true; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 279 | private int mRSMode = 0; |
| 280 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 281 | @Override |
| 282 | public boolean onTouchEvent(MotionEvent ev) |
| 283 | { |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 284 | mArrowNavigation = false; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 285 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 286 | if (!isVisible()) { |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 287 | return true; |
Joe Onorato | e3406a2 | 2009-09-03 14:36:25 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 290 | if (mLocks != 0) { |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 291 | return true; |
| 292 | } |
| 293 | |
| 294 | super.onTouchEvent(ev); |
| 295 | |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 296 | int x = (int)ev.getX(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 297 | int y = (int)ev.getY(); |
| 298 | |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 299 | int action = ev.getAction(); |
| 300 | switch (action) { |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 301 | case MotionEvent.ACTION_DOWN: |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 302 | if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) { |
| 303 | mTouchTracking = TRACKING_HOME; |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 304 | mRollo.setHomeSelected(true); |
| 305 | mRollo.mState.save(); |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 306 | mCurrentIconIndex = -1; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 307 | } else { |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 308 | mTouchTracking = TRACKING_FLING; |
| 309 | |
| 310 | mMotionDownRawX = (int)ev.getRawX(); |
| 311 | mMotionDownRawY = (int)ev.getRawY(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 312 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 313 | mRollo.mState.newPositionX = ev.getRawY() / getHeight(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 314 | mRollo.mState.newTouchDown = 1; |
| 315 | |
| 316 | if (!mRollo.checkClickOK()) { |
| 317 | mRollo.clearSelectedIcon(); |
| 318 | } else { |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 319 | mDownIconIndex = mCurrentIconIndex |
| 320 | = mRollo.selectIcon(x, y, mRollo.mMessageProc.mPosX); |
| 321 | if (mDownIconIndex < 0) { |
| 322 | // if nothing was selected, no long press. |
| 323 | cancelLongPress(); |
| 324 | } |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 325 | } |
| 326 | mRollo.mState.save(); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 327 | mRollo.move(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 328 | mVelocity = VelocityTracker.obtain(); |
| 329 | mVelocity.addMovement(ev); |
| 330 | mStartedScrolling = false; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 331 | } |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 332 | break; |
| 333 | case MotionEvent.ACTION_MOVE: |
| 334 | case MotionEvent.ACTION_OUTSIDE: |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 335 | if (mTouchTracking == TRACKING_HOME) { |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 336 | mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]); |
| 337 | mRollo.mState.save(); |
Joe Onorato | 68ffd10 | 2009-10-15 17:59:43 -0700 | [diff] [blame] | 338 | } else if (mTouchTracking == TRACKING_FLING) { |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 339 | int rawX = (int)ev.getRawX(); |
| 340 | int rawY = (int)ev.getRawY(); |
| 341 | int slop; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 342 | slop = Math.abs(rawY - mMotionDownRawY); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 343 | |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 344 | if (!mStartedScrolling && slop < mSlop) { |
| 345 | // don't update anything so when we do start scrolling |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 346 | // below, we get the right delta. |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 347 | mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mRollo.mMessageProc.mPosX); |
| 348 | if (mDownIconIndex != mCurrentIconIndex) { |
| 349 | // If a different icon is selected, don't allow it to be picked up. |
| 350 | // This handles off-axis dragging. |
| 351 | cancelLongPress(); |
| 352 | mCurrentIconIndex = -1; |
| 353 | } |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 354 | } else { |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 355 | if (!mStartedScrolling) { |
| 356 | cancelLongPress(); |
| 357 | mCurrentIconIndex = -1; |
| 358 | } |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 359 | mRollo.mState.newPositionX = ev.getRawY() / getHeight(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 360 | mRollo.mState.newTouchDown = 1; |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 361 | mRollo.move(); |
Jason Sams | 86c87ed | 2009-09-18 13:55:55 -0700 | [diff] [blame] | 362 | |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 363 | mStartedScrolling = true; |
| 364 | mRollo.clearSelectedIcon(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 365 | mVelocity.addMovement(ev); |
| 366 | mRollo.mState.save(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 367 | } |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 368 | } |
| 369 | break; |
| 370 | case MotionEvent.ACTION_UP: |
| 371 | case MotionEvent.ACTION_CANCEL: |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 372 | if (mTouchTracking == TRACKING_HOME) { |
| 373 | if (action == MotionEvent.ACTION_UP) { |
| 374 | if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) { |
| 375 | mLauncher.closeAllApps(true); |
| 376 | } |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 377 | mRollo.setHomeSelected(false); |
| 378 | mRollo.mState.save(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 379 | } |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 380 | mCurrentIconIndex = -1; |
Joe Onorato | 68ffd10 | 2009-10-15 17:59:43 -0700 | [diff] [blame] | 381 | } else if (mTouchTracking == TRACKING_FLING) { |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 382 | mRollo.mState.newTouchDown = 0; |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 383 | mRollo.mState.newPositionX = ev.getRawY() / getHeight(); |
Jason Sams | 476339d | 2009-09-29 18:14:38 -0700 | [diff] [blame] | 384 | |
Jason Sams | 12c14a8 | 2009-10-06 14:33:15 -0700 | [diff] [blame] | 385 | mVelocity.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 386 | mRollo.mState.flingVelocity = mVelocity.getYVelocity() / getHeight(); |
Jason Sams | 12c14a8 | 2009-10-06 14:33:15 -0700 | [diff] [blame] | 387 | mRollo.clearSelectedIcon(); |
| 388 | mRollo.mState.save(); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 389 | mRollo.fling(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 390 | |
Joe Onorato | 539ed9d | 2009-10-02 10:22:14 -0700 | [diff] [blame] | 391 | if (mVelocity != null) { |
| 392 | mVelocity.recycle(); |
| 393 | mVelocity = null; |
| 394 | } |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 395 | } |
Joe Onorato | 68ffd10 | 2009-10-15 17:59:43 -0700 | [diff] [blame] | 396 | mTouchTracking = TRACKING_NONE; |
| 397 | break; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 398 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 399 | |
| 400 | return true; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 403 | public void onClick(View v) { |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 404 | if (mLocks != 0 || !isVisible()) { |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 405 | return; |
| 406 | } |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 407 | if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex |
| 408 | && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) { |
| 409 | ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 410 | mLauncher.startActivitySafely(app.intent); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | public boolean onLongClick(View v) { |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 415 | if (mLocks != 0 || !isVisible()) { |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 416 | return true; |
| 417 | } |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 418 | if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex |
| 419 | && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) { |
| 420 | ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex); |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 421 | |
| 422 | // We don't really have an accurate location to use. This will do. |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 423 | int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2); |
| 424 | int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX; |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 425 | |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 426 | int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2; |
| 427 | int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2; |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 428 | mDragController.startDrag(app.iconBitmap, screenX, screenY, |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 429 | left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX, |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 430 | this, app, DragController.DRAG_ACTION_COPY); |
Joe Onorato | e3406a2 | 2009-09-03 14:36:25 -0700 | [diff] [blame] | 431 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 432 | mLauncher.closeAllApps(true); |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 433 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 434 | return true; |
| 435 | } |
| 436 | |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 437 | public void setDragController(DragController dragger) { |
| 438 | mDragController = dragger; |
| 439 | } |
| 440 | |
| 441 | public void onDropCompleted(View target, boolean success) { |
| 442 | } |
| 443 | |
Joe Onorato | 4db5231 | 2009-10-06 11:17:43 -0700 | [diff] [blame] | 444 | /** |
| 445 | * Zoom to the specifed amount. |
| 446 | * |
| 447 | * @param amount [0..1] 0 is hidden, 1 is open |
| 448 | * @param animate Whether to animate. |
| 449 | */ |
Jason Sams | 12c14a8 | 2009-10-06 14:33:15 -0700 | [diff] [blame] | 450 | public void zoom(float amount) { |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 451 | if (mRollo == null) { |
| 452 | return; |
| 453 | } |
| 454 | |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 455 | cancelLongPress(); |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 456 | mRollo.clearSelectedIcon(); |
Joe Onorato | 1d708e7 | 2009-10-15 21:29:21 -0700 | [diff] [blame] | 457 | mRollo.setHomeSelected(false); |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 458 | if (amount > 0.001f) { |
Joe Onorato | c5acd42 | 2009-10-01 14:21:24 -0700 | [diff] [blame] | 459 | // set in readback, so we're correct even before the next frame |
Jason Sams | 12c14a8 | 2009-10-06 14:33:15 -0700 | [diff] [blame] | 460 | mRollo.mState.zoomTarget = amount; |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 461 | } else { |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 462 | mRollo.mState.zoomTarget = 0; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 463 | } |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 464 | mRollo.mState.save(); |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | public boolean isVisible() { |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 468 | if (mRollo == null) { |
| 469 | return false; |
| 470 | } |
Jason Sams | 12c14a8 | 2009-10-06 14:33:15 -0700 | [diff] [blame] | 471 | return mRollo.mMessageProc.mZoom > 0.001f; |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 472 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 473 | |
Mike Cleron | b6082fa0 | 2009-10-19 17:03:36 -0700 | [diff] [blame] | 474 | /* |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 475 | @Override |
| 476 | public boolean onTrackballEvent(MotionEvent ev) |
| 477 | { |
| 478 | float x = ev.getX(); |
| 479 | float y = ev.getY(); |
| 480 | //Float tx = new Float(x); |
| 481 | //Float ty = new Float(y); |
| 482 | //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString()); |
| 483 | |
| 484 | |
| 485 | return true; |
| 486 | } |
Mike Cleron | b6082fa0 | 2009-10-19 17:03:36 -0700 | [diff] [blame] | 487 | */ |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 488 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 489 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 490 | mAllAppsList = list; |
| 491 | if (mRollo != null) { |
| 492 | mRollo.setApps(list); |
| 493 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 494 | mPageCount = countPages(list.size()); |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 495 | mLocks &= ~LOCK_ICONS_PENDING; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 498 | public void addApps(ArrayList<ApplicationInfo> list) { |
| 499 | final int N = list.size(); |
| 500 | if (mRollo != null) { |
| 501 | mRollo.reallocAppsList(mRollo.mState.iconCount + N); |
| 502 | } |
| 503 | |
| 504 | for (int i=0; i<N; i++) { |
| 505 | final ApplicationInfo item = list.get(i); |
| 506 | int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp); |
| 507 | if (index < 0) { |
| 508 | index = -(index+1); |
| 509 | } |
| 510 | mAllAppsList.add(index, item); |
| 511 | if (mRollo != null) { |
| 512 | mRollo.addApp(index, item); |
| 513 | mRollo.mState.iconCount++; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if (mRollo != null) { |
| 518 | mRollo.saveAppsList(); |
| 519 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 522 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 523 | final int N = list.size(); |
| 524 | for (int i=0; i<N; i++) { |
| 525 | final ApplicationInfo item = list.get(i); |
| 526 | int index = Collections.binarySearch(mAllAppsList, item, mAppIntentComp); |
| 527 | if (index >= 0) { |
| 528 | mAllAppsList.remove(index); |
| 529 | if (mRollo != null) { |
| 530 | mRollo.removeApp(index); |
| 531 | mRollo.mState.iconCount--; |
| 532 | } |
| 533 | } else { |
| 534 | Log.e(TAG, "couldn't find a match for item \"" + item + "\""); |
| 535 | // Try to recover. This should keep us from crashing for now. |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | if (mRollo != null) { |
| 540 | mRollo.saveAppsList(); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | public void updateApps(String packageName, ArrayList<ApplicationInfo> list) { |
| 545 | // Just remove and add, because they may need to be re-sorted. |
| 546 | removeApps(list); |
| 547 | addApps(list); |
| 548 | } |
| 549 | |
| 550 | private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() { |
| 551 | public int compare(ApplicationInfo a, ApplicationInfo b) { |
| 552 | int result = a.title.toString().compareTo(b.toString()); |
| 553 | if (result != 0) { |
| 554 | return result; |
| 555 | } |
| 556 | return a.intent.getComponent().compareTo(b.intent.getComponent()); |
| 557 | } |
| 558 | }; |
| 559 | |
| 560 | private Comparator<ApplicationInfo> mAppIntentComp = new Comparator<ApplicationInfo>() { |
| 561 | public int compare(ApplicationInfo a, ApplicationInfo b) { |
| 562 | return a.intent.getComponent().compareTo(b.intent.getComponent()); |
| 563 | } |
| 564 | }; |
| 565 | |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 566 | private static int countPages(int iconCount) { |
| 567 | int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE; |
| 568 | int pages = iconCount / iconsPerPage; |
| 569 | if (pages*iconsPerPage != iconCount) { |
| 570 | pages++; |
| 571 | } |
| 572 | return pages; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 573 | } |
| 574 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 575 | public class RolloRS { |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 576 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 577 | // Allocations ====== |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 578 | private int mWidth; |
| 579 | private int mHeight; |
| 580 | |
| 581 | private Resources mRes; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 582 | private Script mScript; |
| 583 | private Script.Invokable mInvokeMove; |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 584 | private Script.Invokable mInvokeMoveTo; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 585 | private Script.Invokable mInvokeFling; |
| 586 | private Script.Invokable mInvokeResetWAR; |
Jason Sams | 86c87ed | 2009-09-18 13:55:55 -0700 | [diff] [blame] | 587 | |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 588 | |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 589 | private ProgramStore mPSIcons; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 590 | private ProgramStore mPSText; |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 591 | private ProgramFragment mPFColor; |
| 592 | private ProgramFragment mPFTexLinear; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 593 | private ProgramVertex mPV; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 594 | private ProgramVertex mPVOrtho; |
Jason Sams | 0aa7166 | 2009-10-02 18:43:18 -0700 | [diff] [blame] | 595 | private SimpleMesh mMesh; |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 596 | private SimpleMesh mMesh2; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 597 | |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 598 | private Allocation mHomeButtonNormal; |
| 599 | private Allocation mHomeButtonPressed; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 600 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 601 | private Allocation[] mIcons; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 602 | private int[] mIconIds; |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 603 | private Allocation mAllocIconIds; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 604 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 605 | private Allocation[] mLabels; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 606 | private int[] mLabelIds; |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 607 | private Allocation mAllocLabelIds; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 608 | private Allocation mSelectedIcon; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 609 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 610 | private int[] mTouchYBorders; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 611 | private int[] mTouchXBorders; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 612 | |
| 613 | private Bitmap mSelectionBitmap; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 614 | private Canvas mSelectionCanvas; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 615 | |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 616 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 617 | Params mParams; |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 618 | State mState; |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 619 | |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 620 | class BaseAlloc { |
| 621 | Allocation mAlloc; |
| 622 | Type mType; |
| 623 | |
| 624 | void save() { |
| 625 | mAlloc.data(this); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 626 | } |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Jason Sams | 12c14a8 | 2009-10-06 14:33:15 -0700 | [diff] [blame] | 629 | class AAMessage extends RenderScript.RSMessage { |
| 630 | public void run() { |
| 631 | mPosX = ((float)mData[0]) / (1 << 16); |
| 632 | mVelocity = ((float)mData[1]) / (1 << 16); |
| 633 | mZoom = ((float)mData[2]) / (1 << 16); |
| 634 | //Log.d("rs", "new msg " + mPosX + " " + mVelocity + " " + mZoom); |
| 635 | } |
| 636 | float mZoom; |
| 637 | float mPosX; |
| 638 | float mVelocity; |
| 639 | } |
| 640 | AAMessage mMessageProc; |
| 641 | |
Jason Sams | 476339d | 2009-09-29 18:14:38 -0700 | [diff] [blame] | 642 | private boolean checkClickOK() { |
| 643 | //android.util.Log.e("rs", "check click " + Float.toString(mReadback.velocity) + ", " + Float.toString(mReadback.posX)); |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 644 | return (Math.abs(mMessageProc.mVelocity) < 0.4f) && |
| 645 | (Math.abs(mMessageProc.mPosX - Math.round(mMessageProc.mPosX)) < 0.4f); |
Jason Sams | 476339d | 2009-09-29 18:14:38 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 648 | class Params extends BaseAlloc { |
| 649 | Params() { |
| 650 | mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass"); |
| 651 | mAlloc = Allocation.createTyped(mRS, mType); |
| 652 | save(); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 653 | } |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 654 | public int bubbleWidth; |
| 655 | public int bubbleHeight; |
| 656 | public int bubbleBitmapWidth; |
| 657 | public int bubbleBitmapHeight; |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 658 | |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 659 | public int homeButtonWidth; |
| 660 | public int homeButtonHeight; |
| 661 | public int homeButtonTextureWidth; |
| 662 | public int homeButtonTextureHeight; |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | class State extends BaseAlloc { |
Jason Sams | 86c87ed | 2009-09-18 13:55:55 -0700 | [diff] [blame] | 666 | public float newPositionX; |
| 667 | public int newTouchDown; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 668 | public float flingVelocity; |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 669 | public int iconCount; |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 670 | public int selectedIconIndex = -1; |
| 671 | public int selectedIconTexture; |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 672 | public float zoomTarget; |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 673 | public int homeButtonId; |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 674 | public float targetPos; |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 675 | |
| 676 | State() { |
| 677 | mType = Type.createFromClass(mRS, State.class, 1, "StateClass"); |
| 678 | mAlloc = Allocation.createTyped(mRS, mType); |
| 679 | save(); |
| 680 | } |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | public RolloRS() { |
| 684 | } |
| 685 | |
| 686 | public void init(Resources res, int width, int height) { |
| 687 | mRes = res; |
| 688 | mWidth = width; |
| 689 | mHeight = height; |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 690 | mDefines.recompute(width, height); |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 691 | initProgramVertex(); |
| 692 | initProgramFragment(); |
| 693 | initProgramStore(); |
Jason Sams | 0aa7166 | 2009-10-02 18:43:18 -0700 | [diff] [blame] | 694 | initMesh(); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 695 | initGl(); |
| 696 | initData(); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 697 | initTouchState(); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 698 | initRs(); |
| 699 | } |
| 700 | |
Jason Sams | 0aa7166 | 2009-10-02 18:43:18 -0700 | [diff] [blame] | 701 | public void initMesh() { |
| 702 | SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3, |
| 703 | SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR); |
| 704 | |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 705 | float y = 0; |
| 706 | float z = 0; |
| 707 | for (int ct=0; ct < 200; ct++) { |
| 708 | float angle = 0; |
| 709 | float maxAngle = 3.14f * 0.16f; |
| 710 | float l = 1.f; |
| 711 | |
| 712 | l = 1 - ((ct-5) * 0.10f); |
| 713 | if (ct > 7) { |
| 714 | angle = maxAngle * (ct - 7) * 0.2f; |
| 715 | angle = Math.min(angle, maxAngle); |
| 716 | } |
| 717 | l = Math.max(0.3f, l); |
| 718 | l = Math.min(1.0f, l); |
| 719 | |
| 720 | y += 0.1f * Math.cos(angle); |
| 721 | z += 0.1f * Math.sin(angle); |
| 722 | |
| 723 | float t = 0.1f * ct; |
| 724 | float ds = 0.08f; |
| 725 | tm.setColor(l, l, l, 0.99f); |
| 726 | tm.setTexture(ds, t); |
| 727 | tm.addVertex(-0.5f, y, z); |
| 728 | tm.setTexture(1 - ds, t); |
| 729 | tm.addVertex(0.5f, y, z); |
| 730 | } |
| 731 | for (int ct=0; ct < (200 * 2 - 2); ct+= 2) { |
| 732 | tm.addTriangle(ct, ct+1, ct+2); |
| 733 | tm.addTriangle(ct+1, ct+3, ct+2); |
| 734 | } |
| 735 | mMesh2 = tm.create(); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 736 | mMesh2.setName("SMMesh"); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 739 | private void initProgramVertex() { |
| 740 | ProgramVertex.MatrixAllocation pva = new ProgramVertex.MatrixAllocation(mRS); |
| 741 | pva.setupProjectionNormalized(mWidth, mHeight); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 742 | |
| 743 | ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null); |
Jason Sams | 0aa7166 | 2009-10-02 18:43:18 -0700 | [diff] [blame] | 744 | pvb.setTextureMatrixEnable(true); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 745 | mPV = pvb.create(); |
| 746 | mPV.setName("PV"); |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 747 | mPV.bindAllocation(pva); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 748 | |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 749 | //pva = new ProgramVertex.MatrixAllocation(mRS); |
| 750 | //pva.setupOrthoWindow(mWidth, mHeight); |
| 751 | //pvb.setTextureMatrixEnable(true); |
| 752 | //mPVOrtho = pvb.create(); |
| 753 | //mPVOrtho.setName("PVOrtho"); |
| 754 | //mPVOrtho.bindAllocation(pva); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 755 | |
| 756 | mRS.contextBindProgramVertex(mPV); |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 757 | } |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 758 | |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 759 | private void initProgramFragment() { |
| 760 | Sampler.Builder sb = new Sampler.Builder(mRS); |
| 761 | sb.setMin(Sampler.Value.LINEAR); |
| 762 | sb.setMag(Sampler.Value.LINEAR); |
| 763 | sb.setWrapS(Sampler.Value.CLAMP); |
| 764 | sb.setWrapT(Sampler.Value.CLAMP); |
| 765 | Sampler linear = sb.create(); |
| 766 | |
| 767 | sb.setMin(Sampler.Value.NEAREST); |
| 768 | sb.setMag(Sampler.Value.NEAREST); |
| 769 | Sampler nearest = sb.create(); |
| 770 | |
| 771 | ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 772 | //mPFColor = bf.create(); |
| 773 | //mPFColor.setName("PFColor"); |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 774 | |
| 775 | bf.setTexEnable(true, 0); |
| 776 | bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0); |
| 777 | mPFTexLinear = bf.create(); |
| 778 | mPFTexLinear.setName("PFTexLinear"); |
| 779 | mPFTexLinear.bindSampler(linear, 0); |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | private void initProgramStore() { |
| 783 | ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null); |
| 784 | bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); |
Jason Sams | 12c14a8 | 2009-10-06 14:33:15 -0700 | [diff] [blame] | 785 | bs.setColorMask(true,true,true,false); |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 786 | bs.setDitherEnable(true); |
| 787 | bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA, |
| 788 | ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA); |
| 789 | mPSIcons = bs.create(); |
| 790 | mPSIcons.setName("PSIcons"); |
| 791 | |
| 792 | //bs.setDitherEnable(false); |
| 793 | //mPSText = bs.create(); |
| 794 | //mPSText.setName("PSText"); |
| 795 | } |
| 796 | |
| 797 | private void initGl() { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 798 | mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1]; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 799 | mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1]; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 800 | } |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 801 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 802 | private void initData() { |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 803 | mParams = new Params(); |
| 804 | mState = new State(); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 805 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 806 | final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext()); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 807 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 808 | mParams.bubbleWidth = bubble.getBubbleWidth(); |
| 809 | mParams.bubbleHeight = bubble.getMaxBubbleHeight(); |
| 810 | mParams.bubbleBitmapWidth = bubble.getBitmapWidth(); |
| 811 | mParams.bubbleBitmapHeight = bubble.getBitmapHeight(); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 812 | |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 813 | mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes, |
| 814 | R.drawable.home_button_normal, Element.RGBA_8888(mRS), false); |
| 815 | mHomeButtonNormal.uploadToTexture(0); |
| 816 | mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes, |
| 817 | R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false); |
| 818 | mHomeButtonPressed.uploadToTexture(0); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 819 | mParams.homeButtonWidth = 76; |
| 820 | mParams.homeButtonHeight = 68; |
| 821 | mParams.homeButtonTextureWidth = 128; |
| 822 | mParams.homeButtonTextureHeight = 128; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 823 | |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 824 | mState.homeButtonId = mHomeButtonNormal.getID(); |
| 825 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 826 | mParams.save(); |
| 827 | mState.save(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 828 | |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 829 | mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX, |
| 830 | Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888); |
| 831 | mSelectionCanvas = new Canvas(mSelectionBitmap); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 832 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 833 | setApps(null); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 836 | private void initScript(int id) { |
| 837 | } |
| 838 | |
| 839 | private void initRs() { |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 840 | ScriptC.Builder sb = new ScriptC.Builder(mRS); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 841 | sb.setScript(mRes, R.raw.rollo3); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 842 | sb.setRoot(true); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 843 | sb.addDefines(mDefines); |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 844 | sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS); |
| 845 | sb.setType(mState.mType, "state", Defines.ALLOC_STATE); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 846 | mInvokeMove = sb.addInvokable("move"); |
| 847 | mInvokeFling = sb.addInvokable("fling"); |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 848 | mInvokeMoveTo = sb.addInvokable("moveTo"); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 849 | mInvokeResetWAR = sb.addInvokable("resetHWWar"); |
| 850 | mScript = sb.create(); |
| 851 | mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 852 | mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS); |
| 853 | mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE); |
| 854 | mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS); |
| 855 | mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 856 | |
Jason Sams | 12c14a8 | 2009-10-06 14:33:15 -0700 | [diff] [blame] | 857 | mMessageProc = new AAMessage(); |
| 858 | mRS.mMessageCallback = mMessageProc; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 859 | mRS.contextBindRootScript(mScript); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 860 | } |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 861 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 862 | private void setApps(ArrayList<ApplicationInfo> list) { |
| 863 | final int count = list != null ? list.size() : 0; |
Jason Sams | 0a8dc2c | 2009-09-27 17:51:44 -0700 | [diff] [blame] | 864 | int allocCount = count; |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 865 | if (allocCount < 1) { |
Jason Sams | 0a8dc2c | 2009-09-27 17:51:44 -0700 | [diff] [blame] | 866 | allocCount = 1; |
| 867 | } |
| 868 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 869 | mIcons = new Allocation[count]; |
Jason Sams | 0a8dc2c | 2009-09-27 17:51:44 -0700 | [diff] [blame] | 870 | mIconIds = new int[allocCount]; |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 871 | mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 872 | |
| 873 | mLabels = new Allocation[count]; |
Jason Sams | 0a8dc2c | 2009-09-27 17:51:44 -0700 | [diff] [blame] | 874 | mLabelIds = new int[allocCount]; |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 875 | mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 876 | |
Jason Sams | 0a8dc2c | 2009-09-27 17:51:44 -0700 | [diff] [blame] | 877 | Element ie8888 = Element.RGBA_8888(mRS); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 878 | |
| 879 | Utilities.BubbleText bubble = new Utilities.BubbleText(getContext()); |
| 880 | |
| 881 | for (int i=0; i<count; i++) { |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 882 | uploadAppIcon(i, list.get(i)); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 883 | } |
| 884 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 885 | mState.iconCount = count; |
| 886 | |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 887 | saveAppsList(); |
| 888 | } |
| 889 | |
| 890 | private void uploadAppIcon(int index, ApplicationInfo item) { |
| 891 | mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap, |
| 892 | Element.RGBA_8888(mRS), false); |
| 893 | mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap, |
| 894 | Element.RGBA_8888(mRS), false); |
| 895 | |
| 896 | mIcons[index].uploadToTexture(0); |
| 897 | mLabels[index].uploadToTexture(0); |
| 898 | |
| 899 | mIconIds[index] = mIcons[index].getID(); |
| 900 | mLabelIds[index] = mLabels[index].getID(); |
| 901 | } |
| 902 | |
| 903 | /** |
| 904 | * Puts the empty spaces at the end. Updates mState.iconCount. You must |
| 905 | * fill in the values and call saveAppsList(). |
| 906 | */ |
| 907 | private void reallocAppsList(int count) { |
| 908 | Allocation[] icons = new Allocation[count]; |
| 909 | int[] iconIds = new int[count]; |
| 910 | mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count); |
| 911 | |
| 912 | Allocation[] labels = new Allocation[count]; |
| 913 | int[] labelIds = new int[count]; |
| 914 | mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count); |
| 915 | |
| 916 | final int oldCount = mIcons.length; |
| 917 | |
| 918 | System.arraycopy(mIcons, 0, icons, 0, oldCount); |
| 919 | System.arraycopy(mIconIds, 0, iconIds, 0, oldCount); |
| 920 | System.arraycopy(mLabels, 0, labels, 0, oldCount); |
| 921 | System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount); |
| 922 | |
| 923 | mIcons = icons; |
| 924 | mIconIds = iconIds; |
| 925 | mLabels = labels; |
| 926 | mLabelIds = labelIds; |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * Handle the allocations for the new app. Make sure you call saveAppsList when done. |
| 931 | */ |
| 932 | private void addApp(int index, ApplicationInfo item) { |
| 933 | final int count = mState.iconCount - index; |
| 934 | final int dest = index + 1; |
| 935 | |
| 936 | System.arraycopy(mIcons, index, mIcons, dest, count); |
| 937 | System.arraycopy(mIconIds, index, mIconIds, dest, count); |
| 938 | System.arraycopy(mLabels, index, mLabels, dest, count); |
| 939 | System.arraycopy(mLabelIds, index, mLabelIds, dest, count); |
| 940 | |
| 941 | uploadAppIcon(index, item); |
| 942 | } |
| 943 | |
| 944 | /** |
| 945 | * Handle the allocations for the removed app. Make sure you call saveAppsList when done. |
| 946 | */ |
| 947 | private void removeApp(int index) { |
| 948 | final int count = mState.iconCount - index - 1; |
| 949 | final int src = index + 1; |
| 950 | |
| 951 | System.arraycopy(mIcons, src, mIcons, index, count); |
| 952 | System.arraycopy(mIconIds, src, mIconIds, index, count); |
| 953 | System.arraycopy(mLabels, src, mLabels, index, count); |
| 954 | System.arraycopy(mLabelIds, src, mLabelIds, index, count); |
| 955 | |
| 956 | final int last = mState.iconCount - 1; |
| 957 | mIcons[last] = null; |
| 958 | mIconIds[last] = 0; |
| 959 | mLabels[last] = null; |
| 960 | mLabelIds[last] = 0; |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * Send the apps list structures to RS. |
| 965 | */ |
| 966 | private void saveAppsList() { |
| 967 | mRS.contextBindRootScript(null); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 968 | |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 969 | mAllocIconIds.data(mIconIds); |
| 970 | mAllocLabelIds.data(mLabelIds); |
| 971 | |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 972 | if (mScript != null) { // this happens when we init it |
| 973 | mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS); |
| 974 | mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | mState.save(); |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 978 | |
| 979 | // Note: mScript may be null if we haven't initialized it yet. |
| 980 | // In that case, this is a no-op. |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 981 | if (mInvokeResetWAR != null) { |
| 982 | mInvokeResetWAR.execute(); |
Jason Sams | 41b61c8 | 2009-10-15 15:40:54 -0700 | [diff] [blame] | 983 | } |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 984 | mRS.contextBindRootScript(mScript); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 985 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 986 | |
| 987 | void initTouchState() { |
| 988 | int width = getWidth(); |
| 989 | int height = getHeight(); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 990 | int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE; |
| 991 | int cellWidth = width / Defines.COLUMNS_PER_PAGE; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 992 | |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 993 | int centerY = (height / 2); |
| 994 | mTouchYBorders[0] = centerY - (cellHeight * 2); |
| 995 | mTouchYBorders[1] = centerY - cellHeight; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 996 | mTouchYBorders[2] = centerY; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 997 | mTouchYBorders[3] = centerY + cellHeight; |
| 998 | mTouchYBorders[4] = centerY + (cellHeight * 2); |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 999 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1000 | int centerX = (width / 2); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1001 | mTouchXBorders[0] = 0; |
| 1002 | mTouchXBorders[1] = centerX - (width / 4); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1003 | mTouchXBorders[2] = centerX; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1004 | mTouchXBorders[3] = centerX + (width / 4); |
| 1005 | mTouchXBorders[4] = width; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1008 | int chooseTappedIcon(int x, int y, float pos) { |
| 1009 | // Adjust for scroll position if not zero. |
| 1010 | y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]); |
Jason Sams | 86c87ed | 2009-09-18 13:55:55 -0700 | [diff] [blame] | 1011 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1012 | int col = -1; |
| 1013 | int row = -1; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1014 | for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) { |
| 1015 | if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) { |
| 1016 | col = i; |
| 1017 | break; |
| 1018 | } |
| 1019 | } |
| 1020 | for (int i=0; i<Defines.ROWS_PER_PAGE; i++) { |
| 1021 | if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) { |
| 1022 | row = i; |
| 1023 | break; |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | if (row < 0 || col < 0) { |
| 1028 | return -1; |
| 1029 | } |
| 1030 | |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1031 | return (((int)pos) * Defines.COLUMNS_PER_PAGE) |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1032 | + (row * Defines.ROWS_PER_PAGE) + col; |
| 1033 | } |
| 1034 | |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 1035 | void fling() { |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1036 | mInvokeFling.execute(); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | void move() { |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1040 | mInvokeMove.execute(); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 1043 | void moveTo(float row) { |
| 1044 | mState.targetPos = row; |
| 1045 | mState.save(); |
| 1046 | mInvokeMoveTo.execute(); |
| 1047 | } |
| 1048 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1049 | /** |
| 1050 | * You need to call save() on mState on your own after calling this. |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 1051 | * |
| 1052 | * @return the index of the icon that was selected. |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1053 | */ |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 1054 | int selectIcon(int x, int y, float pos) { |
| 1055 | final int index = chooseTappedIcon(x, y, pos); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 1056 | selectIcon(index); |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 1057 | return index; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | void selectIcon(int index) { |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 1061 | if (index < 0) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1062 | mState.selectedIconIndex = -1; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1063 | } else { |
| 1064 | mState.selectedIconIndex = index; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 1065 | |
| 1066 | Bitmap selectionBitmap = mSelectionBitmap; |
| 1067 | |
| 1068 | Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas, |
| 1069 | selectionBitmap.getWidth(), selectionBitmap.getHeight(), |
| 1070 | mAllAppsList.get(index).iconBitmap); |
| 1071 | |
| 1072 | mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap, |
Jason Sams | 0a8dc2c | 2009-09-27 17:51:44 -0700 | [diff] [blame] | 1073 | Element.RGBA_8888(mRS), false); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 1074 | mSelectedIcon.uploadToTexture(0); |
| 1075 | mState.selectedIconTexture = mSelectedIcon.getID(); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | /** |
| 1080 | * You need to call save() on mState on your own after calling this. |
| 1081 | */ |
| 1082 | void clearSelectedIcon() { |
Joe Onorato | 2ca51dc | 2009-09-16 11:44:14 -0400 | [diff] [blame] | 1083 | mState.selectedIconIndex = -1; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1084 | } |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 1085 | |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 1086 | void setHomeSelected(boolean pressed) { |
| 1087 | if (pressed) { |
| 1088 | mState.homeButtonId = mHomeButtonPressed.getID(); |
| 1089 | } else { |
| 1090 | mState.homeButtonId = mHomeButtonNormal.getID(); |
| 1091 | } |
| 1092 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1093 | } |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | |