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