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; |
| 23 | |
| 24 | import android.renderscript.RSSurfaceView; |
| 25 | import android.renderscript.RenderScript; |
| 26 | |
| 27 | import android.renderscript.RenderScript; |
| 28 | import android.renderscript.ProgramVertex; |
| 29 | import android.renderscript.Element; |
| 30 | import android.renderscript.Allocation; |
| 31 | import android.renderscript.Script; |
| 32 | import android.renderscript.ScriptC; |
| 33 | import android.renderscript.ProgramFragment; |
| 34 | import android.renderscript.ProgramStore; |
| 35 | import android.renderscript.Sampler; |
| 36 | |
| 37 | import android.content.Context; |
| 38 | import android.content.res.Resources; |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 39 | import android.database.DataSetObserver; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 40 | import android.graphics.Bitmap; |
| 41 | import android.graphics.BitmapFactory; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 42 | import android.graphics.BlurMaskFilter; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 43 | import android.graphics.Canvas; |
| 44 | import android.graphics.Paint; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 45 | import android.graphics.drawable.BitmapDrawable; |
| 46 | import android.graphics.drawable.Drawable; |
| 47 | import android.os.Handler; |
| 48 | import android.os.Message; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 49 | import android.os.SystemClock; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 50 | import android.util.AttributeSet; |
| 51 | import android.util.Log; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 52 | import android.view.KeyEvent; |
| 53 | import android.view.MotionEvent; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 54 | import android.view.Surface; |
| 55 | import android.view.SurfaceHolder; |
| 56 | import android.view.SurfaceView; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 57 | import android.view.View; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 58 | import android.view.VelocityTracker; |
| 59 | import android.view.ViewConfiguration; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 60 | import android.graphics.PixelFormat; |
| 61 | |
| 62 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 63 | public class AllAppsView extends RSSurfaceView |
| 64 | implements View.OnClickListener, View.OnLongClickListener { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 65 | private static final String TAG = "Launcher.AllAppsView"; |
| 66 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 67 | private Launcher mLauncher; |
| 68 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 69 | private RenderScript mRS; |
| 70 | private RolloRS mRollo; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 71 | private ArrayList<ApplicationInfo> mAllAppsList; |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 72 | |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 73 | private ViewConfiguration mConfig; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 74 | private int mPageCount; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 75 | private boolean mStartedScrolling; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 76 | private VelocityTracker mVelocity; |
| 77 | private int mLastScrollX; |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 78 | private int mLastMotionX; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 79 | private TouchHandler mTouchHandler; |
| 80 | private int mScrollHandleTop; |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 81 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 82 | static class Defines { |
| 83 | private static float farSize(float sizeAt0) { |
| 84 | return sizeAt0 * (Defines.RADIUS - Defines.CAMERA_Z) / -Defines.CAMERA_Z; |
| 85 | } |
| 86 | |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 87 | public static final int ALLOC_PARAMS = 0; |
| 88 | public static final int ALLOC_STATE = 1; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 89 | public static final int ALLOC_ICON_IDS = 2; |
| 90 | public static final int ALLOC_LABEL_IDS = 3; |
| 91 | public static final int ALLOC_X_BORDERS = 4; |
| 92 | public static final int ALLOC_Y_BORDERS = 5; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 93 | |
| 94 | public static final int COLUMNS_PER_PAGE = 4; |
| 95 | public static final int ROWS_PER_PAGE = 4; |
| 96 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 97 | public static final float RADIUS = 4.0f; |
| 98 | |
| 99 | public static final int SCREEN_WIDTH_PX = 480; |
| 100 | public static final int SCREEN_HEIGHT_PX = 854; |
| 101 | |
| 102 | public static final int ICON_WIDTH_PX = 64; |
| 103 | public static final int ICON_TEXTURE_WIDTH_PX = 128; |
| 104 | |
| 105 | public static final int ICON_HEIGHT_PX = 64; |
| 106 | public static final int ICON_TEXTURE_HEIGHT_PX = 128; |
| 107 | public static final float ICON_TOP_OFFSET = 0.2f; |
| 108 | |
| 109 | public static final float CAMERA_Z = -2; |
| 110 | public static final float FAR_ICON_SIZE |
| 111 | = farSize(2 * ICON_WIDTH_PX / (float)SCREEN_WIDTH_PX); |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 112 | } |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 113 | |
| 114 | public AllAppsView(Context context, AttributeSet attrs) { |
| 115 | super(context, attrs); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 116 | setFocusable(true); |
| 117 | getHolder().setFormat(PixelFormat.TRANSLUCENT); |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 118 | mConfig = ViewConfiguration.get(context); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 119 | setOnClickListener(this); |
| 120 | setOnLongClickListener(this); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 123 | public AllAppsView(Context context, AttributeSet attrs, int defStyle) { |
| 124 | this(context, attrs); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 127 | public void setLauncher(Launcher launcher) { |
| 128 | mLauncher = launcher; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 131 | @Override |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 132 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
| 133 | super.surfaceChanged(holder, format, w, h); |
| 134 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 135 | long startTime = SystemClock.uptimeMillis(); |
| 136 | |
Jason Sams | b58cbdc | 2009-08-21 16:56:58 -0700 | [diff] [blame] | 137 | mRS = createRenderScript(true); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 138 | mRollo = new RolloRS(); |
| 139 | mRollo.init(getResources(), w, h); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 140 | if (mAllAppsList != null) { |
| 141 | mRollo.setApps(mAllAppsList); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 142 | Log.d(TAG, "surfaceChanged... calling mRollo.setApps"); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 143 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 144 | |
| 145 | Resources res = getContext().getResources(); |
| 146 | int barHeight = (int)res.getDimension(R.dimen.button_bar_height); |
| 147 | mScrollHandleTop = h - barHeight; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 148 | |
| 149 | long endTime = SystemClock.uptimeMillis(); |
| 150 | Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms"); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public boolean onKeyDown(int keyCode, KeyEvent event) |
| 155 | { |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 156 | // this method doesn't work when 'extends View' include 'extends ScrollView'. |
| 157 | return super.onKeyDown(keyCode, event); |
| 158 | } |
| 159 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 160 | @Override |
| 161 | public boolean onTouchEvent(MotionEvent ev) |
| 162 | { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 163 | super.onTouchEvent(ev); |
| 164 | |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 165 | mTouchHandler = mFlingHandler; |
| 166 | /* |
| 167 | int action = ev.getAction(); |
| 168 | if (action == MotionEvent.ACTION_DOWN) { |
| 169 | if (ev.getY() > mScrollHandleTop) { |
| 170 | mTouchHandler = mScrollHandler; |
| 171 | } else { |
| 172 | mTouchHandler = mFlingHandler; |
| 173 | } |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 174 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 175 | */ |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 176 | mTouchHandler.onTouchEvent(ev); |
| 177 | |
| 178 | return true; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 181 | private abstract class TouchHandler { |
| 182 | abstract boolean onTouchEvent(MotionEvent ev); |
| 183 | }; |
| 184 | |
| 185 | private TouchHandler mFlingHandler = new TouchHandler() { |
| 186 | @Override |
| 187 | public boolean onTouchEvent(MotionEvent ev) |
| 188 | { |
| 189 | int x = (int)ev.getX(); |
| 190 | int deltaX; |
| 191 | switch (ev.getAction()) { |
| 192 | case MotionEvent.ACTION_DOWN: |
| 193 | mLastMotionX = x; |
| 194 | mRollo.mState.read(); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 195 | mRollo.mState.startScrollX = mRollo.mState.scrollX = mLastScrollX |
| 196 | = mRollo.mState.currentScrollX; |
| 197 | if (mRollo.mState.flingVelocityX != 0) { |
| 198 | mRollo.clearSelectedIcon(); |
| 199 | } else { |
| 200 | mRollo.selectIcon(x, (int)ev.getY(), mRollo.mState.startScrollX, |
| 201 | (-mRollo.mState.startScrollX / Defines.SCREEN_WIDTH_PX)); |
| 202 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 203 | mRollo.mState.flingVelocityX = 0; |
| 204 | mRollo.mState.adjustedDeceleration = 0; |
| 205 | mRollo.mState.save(); |
| 206 | mVelocity = VelocityTracker.obtain(); |
| 207 | mVelocity.addMovement(ev); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 208 | mStartedScrolling = false; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 209 | break; |
| 210 | case MotionEvent.ACTION_MOVE: |
| 211 | case MotionEvent.ACTION_OUTSIDE: |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 212 | int slop = Math.abs(x - mLastMotionX); |
| 213 | if (!mStartedScrolling && slop < mConfig.getScaledTouchSlop()) { |
| 214 | // don't update mLastMotionX so slop is right and when we do start scrolling |
| 215 | // below, we get the right delta. |
| 216 | } else { |
| 217 | mStartedScrolling = true; |
| 218 | mRollo.clearSelectedIcon(); |
| 219 | deltaX = x - mLastMotionX; |
| 220 | mVelocity.addMovement(ev); |
| 221 | mRollo.mState.currentScrollX = mLastScrollX; |
| 222 | mLastScrollX += deltaX; |
| 223 | mRollo.mState.scrollX = mLastScrollX; |
| 224 | mRollo.mState.save(); |
| 225 | mLastMotionX = x; |
| 226 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 227 | break; |
| 228 | case MotionEvent.ACTION_UP: |
| 229 | case MotionEvent.ACTION_CANCEL: |
| 230 | mVelocity.computeCurrentVelocity(1000 /* px/sec */, |
| 231 | mConfig.getScaledMaximumFlingVelocity()); |
| 232 | mRollo.mState.flingTimeMs = (int)SystemClock.uptimeMillis(); // TODO: use long |
| 233 | mRollo.mState.flingVelocityX = (int)mVelocity.getXVelocity(); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 234 | mRollo.clearSelectedIcon(); |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 235 | mRollo.mState.save(); |
| 236 | mLastMotionX = -10000; |
| 237 | mVelocity.recycle(); |
| 238 | mVelocity = null; |
| 239 | break; |
| 240 | } |
| 241 | return true; |
| 242 | } |
| 243 | }; |
| 244 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 245 | public void onClick(View v) { |
| 246 | int index = mRollo.mState.selectedIconIndex; |
| 247 | if (mRollo.mState.flingVelocityX == 0 && index >= 0 && index < mAllAppsList.size()) { |
| 248 | ApplicationInfo app = mAllAppsList.get(index); |
| 249 | mLauncher.startActivitySafely(app.intent); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | public boolean onLongClick(View v) { |
| 254 | Log.d(TAG, "long click! velocity=" + mRollo.mState.flingVelocityX + " index=" |
| 255 | + mRollo.mState.selectedIconIndex); |
| 256 | return true; |
| 257 | } |
| 258 | |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 259 | /* |
| 260 | private TouchHandler mScrollHandler = new TouchHandler() { |
| 261 | @Override |
| 262 | public boolean onTouchEvent(MotionEvent ev) |
| 263 | { |
| 264 | int x = (int)ev.getX(); |
| 265 | int w = getWidth(); |
| 266 | |
| 267 | float percent = x / (float)w; |
| 268 | |
| 269 | mRollo.mState.read(); |
| 270 | |
| 271 | mRollo.mState.scrollX = mLastScrollX = -(int)(mPageCount * w * percent); |
| 272 | mRollo.mState.flingVelocityX = 0; |
| 273 | mRollo.mState.adjustedDeceleration = 0; |
| 274 | mRollo.mState.save(); |
| 275 | |
| 276 | return true; |
| 277 | } |
| 278 | }; |
| 279 | */ |
| 280 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 281 | @Override |
| 282 | public boolean onTrackballEvent(MotionEvent ev) |
| 283 | { |
| 284 | float x = ev.getX(); |
| 285 | float y = ev.getY(); |
| 286 | //Float tx = new Float(x); |
| 287 | //Float ty = new Float(y); |
| 288 | //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString()); |
| 289 | |
| 290 | |
| 291 | return true; |
| 292 | } |
| 293 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 294 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 295 | mAllAppsList = list; |
| 296 | if (mRollo != null) { |
| 297 | mRollo.setApps(list); |
| 298 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 299 | mPageCount = countPages(list.size()); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 300 | Log.d(TAG, "setApps mRollo=" + mRollo + " list=" + list); |
| 301 | } |
| 302 | |
| 303 | private void invokeIcon(int index) { |
| 304 | Log.d(TAG, "launch it!!!! index=" + index); |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | private static int countPages(int iconCount) { |
| 308 | int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE; |
| 309 | int pages = iconCount / iconsPerPage; |
| 310 | if (pages*iconsPerPage != iconCount) { |
| 311 | pages++; |
| 312 | } |
| 313 | return pages; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 316 | public class RolloRS { |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 317 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 318 | // Allocations ====== |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 319 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 320 | private int mWidth; |
| 321 | private int mHeight; |
| 322 | |
| 323 | private Resources mRes; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 324 | private Script mScript; |
| 325 | private Sampler mSampler; |
| 326 | private Sampler mSamplerText; |
| 327 | private ProgramStore mPSBackground; |
| 328 | private ProgramStore mPSText; |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 329 | private ProgramFragment mPFDebug; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 330 | private ProgramFragment mPFImages; |
| 331 | private ProgramFragment mPFText; |
| 332 | private ProgramVertex mPV; |
| 333 | private ProgramVertex.MatrixAllocation mPVAlloc; |
| 334 | private ProgramVertex mPVOrtho; |
| 335 | private ProgramVertex.MatrixAllocation mPVOrthoAlloc; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 336 | |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 337 | private Allocation mScrollHandle; |
| 338 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 339 | private Allocation[] mIcons; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 340 | private int[] mIconIds; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 341 | private Allocation mAllocIconID; |
| 342 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 343 | private Allocation[] mLabels; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 344 | private int[] mLabelIds; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 345 | private Allocation mAllocLabelID; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 346 | private Allocation mSelectedIcon; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 347 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 348 | private int[] mTouchYBorders; |
| 349 | private Allocation mAllocTouchYBorders; |
| 350 | private int[] mTouchXBorders; |
| 351 | private Allocation mAllocTouchXBorders; |
| 352 | |
| 353 | private Bitmap mSelectionBitmap; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 354 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 355 | Params mParams; |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 356 | State mState; |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 357 | |
| 358 | class Params extends IntAllocation { |
| 359 | Params(RenderScript rs) { |
| 360 | super(rs); |
| 361 | } |
| 362 | @AllocationIndex(0) public int bubbleWidth; |
| 363 | @AllocationIndex(1) public int bubbleHeight; |
| 364 | @AllocationIndex(2) public int bubbleBitmapWidth; |
| 365 | @AllocationIndex(3) public int bubbleBitmapHeight; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 366 | @AllocationIndex(4) public int scrollHandleId; |
| 367 | @AllocationIndex(5) public int scrollHandleTextureWidth; |
| 368 | @AllocationIndex(6) public int scrollHandleTextureHeight; |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 371 | class State extends IntAllocation { |
| 372 | State(RenderScript rs) { |
| 373 | super(rs); |
| 374 | } |
| 375 | @AllocationIndex(0) public int iconCount; |
| 376 | @AllocationIndex(1) public int scrollX; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 377 | @AllocationIndex(2) public int flingTimeMs; |
| 378 | @AllocationIndex(3) public int flingVelocityX; |
| 379 | @AllocationIndex(4) public int adjustedDeceleration; |
| 380 | @AllocationIndex(5) public int currentScrollX; |
Joe Onorato | eb2c02e | 2009-08-12 21:40:52 -0700 | [diff] [blame] | 381 | @AllocationIndex(6) public int flingDuration; |
| 382 | @AllocationIndex(7) public int flingEndPos; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 383 | @AllocationIndex(8) public int startScrollX; |
| 384 | @AllocationIndex(9) public int selectedIconIndex = -1; |
| 385 | @AllocationIndex(10) public int selectedIconTexture; |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | public RolloRS() { |
| 389 | } |
| 390 | |
| 391 | public void init(Resources res, int width, int height) { |
| 392 | mRes = res; |
| 393 | mWidth = width; |
| 394 | mHeight = height; |
| 395 | initGl(); |
| 396 | initData(); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 397 | initTouchState(); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 398 | initRs(); |
| 399 | } |
| 400 | |
| 401 | private void initGl() { |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 402 | Sampler.Builder sb = new Sampler.Builder(mRS); |
| 403 | sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR); |
| 404 | sb.setMag(Sampler.Value.LINEAR); |
| 405 | sb.setWrapS(Sampler.Value.CLAMP); |
| 406 | sb.setWrapT(Sampler.Value.CLAMP); |
| 407 | mSampler = sb.create(); |
| 408 | |
| 409 | sb.setMin(Sampler.Value.NEAREST); |
| 410 | sb.setMag(Sampler.Value.NEAREST); |
| 411 | mSamplerText = sb.create(); |
| 412 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 413 | ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null); |
| 414 | mPFDebug = dbg.create(); |
| 415 | mPFDebug.setName("PFDebug"); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 416 | |
| 417 | ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null); |
| 418 | bf.setTexEnable(true, 0); |
| 419 | bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0); |
| 420 | mPFImages = bf.create(); |
| 421 | mPFImages.setName("PF"); |
| 422 | mPFImages.bindSampler(mSampler, 0); |
| 423 | |
| 424 | bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 425 | //mPFText = bf.create(); |
| 426 | mPFText = (new ProgramFragment.Builder(mRS, null, null)).create(); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 427 | mPFText.setName("PFText"); |
| 428 | mPFText.bindSampler(mSamplerText, 0); |
| 429 | |
| 430 | ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null); |
Joe Onorato | efabe00 | 2009-08-28 09:38:18 -0700 | [diff] [blame] | 431 | bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 432 | bs.setDitherEnable(false); |
| 433 | bs.setDepthMask(true); |
| 434 | bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA, |
| 435 | ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA); |
| 436 | mPSBackground = bs.create(); |
| 437 | mPSBackground.setName("PFS"); |
| 438 | |
| 439 | bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); |
| 440 | bs.setDepthMask(false); |
| 441 | bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA, |
| 442 | ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA); |
| 443 | mPSText = bs.create(); |
| 444 | mPSText.setName("PFSText"); |
| 445 | |
| 446 | mPVAlloc = new ProgramVertex.MatrixAllocation(mRS); |
| 447 | mPVAlloc.setupProjectionNormalized(mWidth, mHeight); |
| 448 | |
| 449 | ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null); |
| 450 | mPV = pvb.create(); |
| 451 | mPV.setName("PV"); |
| 452 | mPV.bindAllocation(mPVAlloc); |
| 453 | |
| 454 | mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS); |
| 455 | mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight); |
| 456 | |
| 457 | pvb.setTextureMatrixEnable(true); |
| 458 | mPVOrtho = pvb.create(); |
| 459 | mPVOrtho.setName("PVOrtho"); |
| 460 | mPVOrtho.bindAllocation(mPVOrthoAlloc); |
| 461 | |
| 462 | mRS.contextBindProgramVertex(mPV); |
| 463 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 464 | mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1]; |
| 465 | mAllocTouchXBorders = Allocation.createSized(mRS, Element.USER_I32, |
| 466 | mTouchXBorders.length); |
| 467 | mAllocTouchXBorders.data(mTouchXBorders); |
| 468 | |
| 469 | mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1]; |
| 470 | mAllocTouchYBorders = Allocation.createSized(mRS, Element.USER_I32, |
| 471 | mTouchYBorders.length); |
| 472 | mAllocTouchYBorders.data(mTouchYBorders); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 473 | |
| 474 | Log.e("rs", "Done loading named"); |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 477 | private void initData() { |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 478 | mParams = new Params(mRS); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 479 | mState = new State(mRS); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 480 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 481 | final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext()); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 482 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 483 | mParams.bubbleWidth = bubble.getBubbleWidth(); |
| 484 | mParams.bubbleHeight = bubble.getMaxBubbleHeight(); |
| 485 | mParams.bubbleBitmapWidth = bubble.getBitmapWidth(); |
| 486 | mParams.bubbleBitmapHeight = bubble.getBitmapHeight(); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 487 | |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 488 | mScrollHandle = Allocation.createFromBitmapResource(mRS, mRes, |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 489 | R.drawable.all_apps_button_pow2, Element.RGBA_8888, false); |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 490 | mScrollHandle.uploadToTexture(0); |
| 491 | mParams.scrollHandleId = mScrollHandle.getID(); |
| 492 | Log.d(TAG, "mParams.scrollHandleId=" + mParams.scrollHandleId); |
| 493 | mParams.scrollHandleTextureWidth = 128; |
| 494 | mParams.scrollHandleTextureHeight = 128; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 495 | |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 496 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 497 | mParams.save(); |
| 498 | mState.save(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 499 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 500 | mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_WIDTH_PX, Defines.ICON_HEIGHT_PX, |
| 501 | Bitmap.Config.ARGB_8888); |
| 502 | Bitmap selectionBitmap = mSelectionBitmap; |
| 503 | Paint paint = new Paint(); |
| 504 | float radius = 12 * getContext().getResources().getDisplayMetrics().density; |
| 505 | //paint.setMaskFilter(new BlurMaskFilter(radius, BlurMaskFilter.Blur.OUTER)); |
| 506 | Canvas canvas = new Canvas(selectionBitmap); |
| 507 | canvas.drawColor(0xffff0000); |
| 508 | |
| 509 | mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap, |
| 510 | Element.RGBA_8888, false); |
| 511 | mSelectedIcon.uploadToTexture(0); |
| 512 | |
| 513 | mState.selectedIconTexture = mSelectedIcon.getID(); |
| 514 | |
| 515 | Log.d(TAG, "initData calling mRollo.setApps"); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 516 | setApps(null); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 519 | private void initRs() { |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 520 | ScriptC.Builder sb = new ScriptC.Builder(mRS); |
| 521 | sb.setScript(mRes, R.raw.rollo); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 522 | sb.setRoot(true); |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 523 | sb.addDefines(Defines.class); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 524 | mScript = sb.create(); |
| 525 | mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 526 | |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 527 | mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS); |
| 528 | mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE); |
| 529 | mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS); |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 530 | mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 531 | mScript.bindAllocation(mAllocTouchXBorders, Defines.ALLOC_X_BORDERS); |
| 532 | mScript.bindAllocation(mAllocTouchYBorders, Defines.ALLOC_Y_BORDERS); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 533 | |
| 534 | mRS.contextBindRootScript(mScript); |
| 535 | } |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 536 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 537 | private void setApps(ArrayList<ApplicationInfo> list) { |
| 538 | final int count = list != null ? list.size() : 0; |
| 539 | mIcons = new Allocation[count]; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 540 | mIconIds = new int[count]; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 541 | mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count); |
| 542 | |
| 543 | mLabels = new Allocation[count]; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 544 | mLabelIds = new int[count]; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 545 | mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count); |
| 546 | |
| 547 | Element ie8888 = Element.RGBA_8888; |
| 548 | |
| 549 | Utilities.BubbleText bubble = new Utilities.BubbleText(getContext()); |
| 550 | |
| 551 | for (int i=0; i<count; i++) { |
| 552 | final ApplicationInfo item = list.get(i); |
| 553 | |
| 554 | mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap, |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 555 | Element.RGBA_8888, false); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 556 | mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap, |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 557 | Element.RGBA_8888, false); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 558 | |
| 559 | mIcons[i].uploadToTexture(0); |
| 560 | mLabels[i].uploadToTexture(0); |
| 561 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 562 | mIconIds[i] = mIcons[i].getID(); |
| 563 | mLabelIds[i] = mLabels[i].getID(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 564 | } |
| 565 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 566 | mAllocIconID.data(mIconIds); |
| 567 | mAllocLabelID.data(mLabelIds); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 568 | |
| 569 | mState.iconCount = count; |
| 570 | |
| 571 | Log.d("AllAppsView", "mScript=" + mScript + " mAllocIconID=" + mAllocIconID); |
| 572 | |
| 573 | if (mScript != null) { // wtf |
| 574 | mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS); |
| 575 | mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS); |
| 576 | } |
| 577 | |
| 578 | mState.save(); |
| 579 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame^] | 580 | |
| 581 | void initTouchState() { |
| 582 | int width = getWidth(); |
| 583 | int height = getHeight(); |
| 584 | |
| 585 | int iconsSize; |
| 586 | if (width < height) { |
| 587 | iconsSize = width; |
| 588 | } else { |
| 589 | iconsSize = height; |
| 590 | } |
| 591 | int cellHeight = iconsSize / Defines.ROWS_PER_PAGE; |
| 592 | int cellWidth = iconsSize / Defines.COLUMNS_PER_PAGE; |
| 593 | |
| 594 | int centerY = (height / 2) - (int)(cellHeight * 0.35f); |
| 595 | mTouchYBorders[0] = centerY - (int)(2.4f * cellHeight); |
| 596 | mTouchYBorders[1] = centerY - (int)(1.15f * cellHeight); |
| 597 | mTouchYBorders[2] = centerY; |
| 598 | mTouchYBorders[3] = centerY + (int)(1.15f * cellHeight);; |
| 599 | mTouchYBorders[4] = centerY + (int)(2.4f * cellHeight); |
| 600 | |
| 601 | mAllocTouchYBorders.data(mTouchYBorders); |
| 602 | |
| 603 | int centerX = (width / 2); |
| 604 | mTouchXBorders[0] = centerX - (2 * cellWidth); |
| 605 | mTouchXBorders[1] = centerX - (int)(0.83f * cellWidth);; |
| 606 | mTouchXBorders[2] = centerX; |
| 607 | mTouchXBorders[3] = centerX + (int)(0.83f * cellWidth);; |
| 608 | mTouchXBorders[4] = centerX + (2 * cellWidth); |
| 609 | |
| 610 | mAllocTouchXBorders.data(mTouchXBorders); |
| 611 | } |
| 612 | |
| 613 | int chooseTappedIcon(int x, int y, int scrollX, int currentPage) { |
| 614 | int col = -1; |
| 615 | int row = -1; |
| 616 | |
| 617 | for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) { |
| 618 | if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) { |
| 619 | col = i; |
| 620 | break; |
| 621 | } |
| 622 | } |
| 623 | for (int i=0; i<Defines.ROWS_PER_PAGE; i++) { |
| 624 | if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) { |
| 625 | row = i; |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if (row < 0 || col < 0) { |
| 631 | return -1; |
| 632 | } |
| 633 | |
| 634 | return (currentPage * Defines.ROWS_PER_PAGE * Defines.COLUMNS_PER_PAGE) |
| 635 | + (row * Defines.ROWS_PER_PAGE) + col; |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * You need to call save() on mState on your own after calling this. |
| 640 | */ |
| 641 | void selectIcon(int x, int y, int scrollX, int currentPage) { |
| 642 | int iconCount = mAllAppsList.size(); |
| 643 | int index = chooseTappedIcon(x, y, scrollX, currentPage); |
| 644 | if (index < 0 || index >= iconCount) { |
| 645 | mState.selectedIconIndex = -1; |
| 646 | return; |
| 647 | } else { |
| 648 | mState.selectedIconIndex = index; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * You need to call save() on mState on your own after calling this. |
| 654 | */ |
| 655 | void clearSelectedIcon() { |
| 656 | mState.selectedIconIndex = -1; |
| 657 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 658 | } |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | |