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 | |
Joe Onorato | cb9f798 | 2009-10-31 16:32:02 -0400 | [diff] [blame] | 19 | import android.content.ComponentName; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.Resources; |
| 22 | import android.graphics.Bitmap; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 23 | import android.graphics.Canvas; |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 24 | import android.graphics.PixelFormat; |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 25 | import android.graphics.Rect; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 26 | import android.os.SystemClock; |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 27 | import android.renderscript.Allocation; |
| 28 | import android.renderscript.Dimension; |
| 29 | import android.renderscript.Element; |
| 30 | import android.renderscript.ProgramFragment; |
| 31 | import android.renderscript.ProgramStore; |
| 32 | import android.renderscript.ProgramVertex; |
| 33 | import android.renderscript.RSSurfaceView; |
| 34 | import android.renderscript.RenderScript; |
| 35 | import android.renderscript.Sampler; |
| 36 | import android.renderscript.Script; |
| 37 | import android.renderscript.ScriptC; |
| 38 | import android.renderscript.SimpleMesh; |
| 39 | import android.renderscript.Type; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 40 | import android.util.AttributeSet; |
| 41 | import android.util.Log; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 42 | import android.view.KeyEvent; |
| 43 | import android.view.MotionEvent; |
Joe Onorato | b39e51a | 2009-10-28 15:47:49 -0400 | [diff] [blame] | 44 | import android.view.SoundEffectConstants; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 45 | import android.view.SurfaceHolder; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 46 | import android.view.VelocityTracker; |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 47 | import android.view.View; |
Joe Onorato | d769a63 | 2009-08-11 17:09:02 -0700 | [diff] [blame] | 48 | import android.view.ViewConfiguration; |
Joe Onorato | 52a653f | 2009-11-11 14:52:11 -0800 | [diff] [blame] | 49 | import android.view.accessibility.AccessibilityEvent; |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 50 | |
| 51 | import java.util.ArrayList; |
| 52 | import java.util.Collections; |
| 53 | import java.util.Comparator; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 54 | |
| 55 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 56 | public class AllAppsView extends RSSurfaceView |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 57 | implements View.OnClickListener, View.OnLongClickListener, DragSource { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 58 | private static final String TAG = "Launcher.AllAppsView"; |
| 59 | |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 60 | /** Bit for mLocks for when there are icons being loaded. */ |
| 61 | private static final int LOCK_ICONS_PENDING = 1; |
| 62 | |
Joe Onorato | 68ffd10 | 2009-10-15 17:59:43 -0700 | [diff] [blame] | 63 | private static final int TRACKING_NONE = 0; |
| 64 | private static final int TRACKING_FLING = 1; |
| 65 | private static final int TRACKING_HOME = 2; |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 66 | |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 67 | private static final int SELECTED_NONE = 0; |
| 68 | private static final int SELECTED_FOCUSED = 1; |
| 69 | private static final int SELECTED_PRESSED = 2; |
| 70 | |
| 71 | private static final int SELECTION_NONE = 0; |
| 72 | private static final int SELECTION_ICONS = 1; |
| 73 | private static final int SELECTION_HOME = 2; |
| 74 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 75 | private Launcher mLauncher; |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 76 | private DragController mDragController; |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 77 | |
| 78 | /** When this is 0, modifications are allowed, when it's not, they're not. |
| 79 | * TODO: What about scrolling? */ |
| 80 | private int mLocks = LOCK_ICONS_PENDING; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 81 | |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 82 | private int mSlop; |
Joe Onorato | f7b0e01 | 2009-10-01 14:09:15 -0700 | [diff] [blame] | 83 | private int mMaxFlingVelocity; |
| 84 | |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 85 | private Defines mDefines = new Defines(); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 86 | private RenderScript mRS; |
| 87 | private RolloRS mRollo; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 88 | private ArrayList<ApplicationInfo> mAllAppsList; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 89 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 90 | /** |
| 91 | * True when we are using arrow keys or trackball to drive navigation |
| 92 | */ |
| 93 | private boolean mArrowNavigation = false; |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 94 | private boolean mStartedScrolling; |
| 95 | |
| 96 | /** |
| 97 | * Used to keep track of the selection when AllAppsView loses window focus. |
| 98 | * One of the SELECTION_ constants. |
| 99 | */ |
| 100 | private int mLastSelection; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 101 | |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 102 | /** |
| 103 | * Used to keep track of the selection when AllAppsView loses window focus |
| 104 | */ |
| 105 | private int mLastSelectedIcon; |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 106 | |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 107 | private VelocityTracker mVelocityTracker; |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 108 | private int mTouchTracking; |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 109 | private int mMotionDownRawX; |
| 110 | private int mMotionDownRawY; |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 111 | private int mDownIconIndex = -1; |
| 112 | private int mCurrentIconIndex = -1; |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 113 | |
Mike Cleron | b64b67a | 2009-11-08 14:56:25 -0800 | [diff] [blame] | 114 | private boolean mShouldGainFocus; |
| 115 | |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 116 | private boolean mZoomDirty = false; |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 117 | private boolean mAnimateNextZoom; |
| 118 | private float mZoom; |
| 119 | private float mPosX; |
| 120 | private float mVelocity; |
| 121 | private AAMessage mMessageProc; |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 122 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 123 | static class Defines { |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 124 | public static final int ALLOC_PARAMS = 0; |
| 125 | public static final int ALLOC_STATE = 1; |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 126 | public static final int ALLOC_ICON_IDS = 3; |
| 127 | public static final int ALLOC_LABEL_IDS = 4; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 128 | |
| 129 | public static final int COLUMNS_PER_PAGE = 4; |
| 130 | public static final int ROWS_PER_PAGE = 4; |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 131 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 132 | public static final int ICON_WIDTH_PX = 64; |
| 133 | public static final int ICON_TEXTURE_WIDTH_PX = 128; |
| 134 | |
| 135 | public static final int ICON_HEIGHT_PX = 64; |
| 136 | public static final int ICON_TEXTURE_HEIGHT_PX = 128; |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 137 | |
| 138 | public int SCREEN_WIDTH_PX; |
| 139 | public int SCREEN_HEIGHT_PX; |
| 140 | |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 141 | public void recompute(int w, int h) { |
| 142 | SCREEN_WIDTH_PX = 480; |
| 143 | SCREEN_HEIGHT_PX = 800; |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 144 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 145 | } |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 146 | |
| 147 | public AllAppsView(Context context, AttributeSet attrs) { |
| 148 | super(context, attrs); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 149 | setFocusable(true); |
Joe Onorato | b39e51a | 2009-10-28 15:47:49 -0400 | [diff] [blame] | 150 | setSoundEffectsEnabled(false); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 151 | getHolder().setFormat(PixelFormat.TRANSLUCENT); |
Joe Onorato | f7b0e01 | 2009-10-01 14:09:15 -0700 | [diff] [blame] | 152 | final ViewConfiguration config = ViewConfiguration.get(context); |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 153 | mSlop = config.getScaledTouchSlop(); |
Joe Onorato | f7b0e01 | 2009-10-01 14:09:15 -0700 | [diff] [blame] | 154 | mMaxFlingVelocity = config.getScaledMaximumFlingVelocity(); |
| 155 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 156 | setOnClickListener(this); |
| 157 | setOnLongClickListener(this); |
Dianne Hackborn | e52a1b5 | 2009-09-30 22:36:20 -0700 | [diff] [blame] | 158 | setZOrderOnTop(true); |
Jason Sams | fd22dac | 2009-09-20 17:24:16 -0700 | [diff] [blame] | 159 | getHolder().setFormat(PixelFormat.TRANSLUCENT); |
Jason Sams | e26d9fc | 2009-11-12 14:00:43 -0800 | [diff] [blame] | 160 | |
| 161 | mRS = createRenderScript(true); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | protected void onDetachedFromWindow() { |
| 166 | destroyRenderScript(); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Joe Onorato | b39e51a | 2009-10-28 15:47:49 -0400 | [diff] [blame] | 169 | /** |
| 170 | * If you have an attached click listener, View always plays the click sound!?!? |
| 171 | * Deal with sound effects by hand. |
| 172 | */ |
| 173 | public void reallyPlaySoundEffect(int sound) { |
| 174 | boolean old = isSoundEffectsEnabled(); |
| 175 | setSoundEffectsEnabled(true); |
| 176 | playSoundEffect(sound); |
| 177 | setSoundEffectsEnabled(old); |
| 178 | } |
| 179 | |
Joe Onorato | 7c312c1 | 2009-08-13 21:36:53 -0700 | [diff] [blame] | 180 | public AllAppsView(Context context, AttributeSet attrs, int defStyle) { |
| 181 | this(context, attrs); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 184 | public void setLauncher(Launcher launcher) { |
| 185 | mLauncher = launcher; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 188 | @Override |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 189 | public void surfaceDestroyed(SurfaceHolder holder) { |
| 190 | super.surfaceDestroyed(holder); |
Jason Sams | 20df7c7 | 2009-11-05 12:30:24 -0800 | [diff] [blame] | 191 | mRollo.mHasSurface = false; |
Joe Onorato | fab7440 | 2009-11-11 16:05:23 -0800 | [diff] [blame] | 192 | // Without this, we leak mMessageCallback which leaks the context. |
| 193 | mRS.mMessageCallback = null; |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 197 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 198 | //long startTime = SystemClock.uptimeMillis(); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 199 | |
Joe Onorato | 080d9b6 | 2009-11-02 12:01:11 -0500 | [diff] [blame] | 200 | super.surfaceChanged(holder, format, w, h); |
| 201 | |
Jason Sams | e26d9fc | 2009-11-12 14:00:43 -0800 | [diff] [blame] | 202 | if (mRollo == null) { |
Jason Sams | 9039667 | 2009-11-03 13:59:34 -0800 | [diff] [blame] | 203 | mRollo = new RolloRS(); |
Jason Sams | 20df7c7 | 2009-11-05 12:30:24 -0800 | [diff] [blame] | 204 | mRollo.mHasSurface = true; |
Jason Sams | 9039667 | 2009-11-03 13:59:34 -0800 | [diff] [blame] | 205 | mRollo.init(getResources(), w, h); |
| 206 | if (mAllAppsList != null) { |
| 207 | mRollo.setApps(mAllAppsList); |
Jason Sams | 9039667 | 2009-11-03 13:59:34 -0800 | [diff] [blame] | 208 | } |
Mike Cleron | b64b67a | 2009-11-08 14:56:25 -0800 | [diff] [blame] | 209 | if (mShouldGainFocus) { |
| 210 | gainFocus(); |
| 211 | mShouldGainFocus = false; |
| 212 | } |
Jason Sams | 20df7c7 | 2009-11-05 12:30:24 -0800 | [diff] [blame] | 213 | } else { |
| 214 | mRollo.mHasSurface = true; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 215 | } |
Jason Sams | e26d9fc | 2009-11-12 14:00:43 -0800 | [diff] [blame] | 216 | mRollo.dirtyCheck(); |
Jason Sams | 5612e43 | 2009-11-16 14:18:07 -0800 | [diff] [blame] | 217 | mRollo.resize(w, h); |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 218 | |
Joe Onorato | cb75f36 | 2009-11-12 13:04:07 -0800 | [diff] [blame] | 219 | mRS.mMessageCallback = mMessageProc = new AAMessage(); |
| 220 | |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 221 | Resources res = getContext().getResources(); |
| 222 | int barHeight = (int)res.getDimension(R.dimen.button_bar_height); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 223 | |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 224 | //long endTime = SystemClock.uptimeMillis(); |
| 225 | //Log.d(TAG, "surfaceChanged took " + (endTime-startTime) + "ms"); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 226 | } |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 227 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 228 | @Override |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 229 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 230 | super.onWindowFocusChanged(hasWindowFocus); |
| 231 | if (mArrowNavigation) { |
| 232 | if (!hasWindowFocus) { |
| 233 | // Clear selection when we lose window focus |
| 234 | mLastSelectedIcon = mRollo.mState.selectedIconIndex; |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 235 | mRollo.setHomeSelected(SELECTED_NONE); |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 236 | mRollo.clearSelectedIcon(); |
| 237 | mRollo.mState.save(); |
| 238 | } else if (hasWindowFocus) { |
| 239 | if (mRollo.mState.iconCount > 0) { |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 240 | if (mLastSelection == SELECTION_ICONS) { |
| 241 | int selection = mLastSelectedIcon; |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 242 | final int firstIcon = Math.round(mPosX) * |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 243 | Defines.COLUMNS_PER_PAGE; |
| 244 | if (selection < 0 || // No selection |
| 245 | selection < firstIcon || // off the top of the screen |
| 246 | selection >= mRollo.mState.iconCount || // past last icon |
| 247 | selection >= firstIcon + // past last icon on screen |
| 248 | (Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE)) { |
| 249 | selection = firstIcon; |
| 250 | } |
| 251 | |
| 252 | // Select the first icon when we gain window focus |
| 253 | mRollo.selectIcon(selection, SELECTED_FOCUSED); |
| 254 | mRollo.mState.save(); |
| 255 | } else if (mLastSelection == SELECTION_HOME) { |
| 256 | mRollo.setHomeSelected(SELECTED_FOCUSED); |
| 257 | mRollo.mState.save(); |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 258 | } |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | @Override |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 265 | protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { |
| 266 | super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); |
Joe Onorato | 859b3a7 | 2009-10-28 15:17:01 -0400 | [diff] [blame] | 267 | |
| 268 | if (!isVisible()) { |
| 269 | return; |
| 270 | } |
| 271 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 272 | if (gainFocus) { |
Mike Cleron | b64b67a | 2009-11-08 14:56:25 -0800 | [diff] [blame] | 273 | if (mRollo != null) { |
| 274 | gainFocus(); |
| 275 | } else { |
| 276 | mShouldGainFocus = true; |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 277 | } |
| 278 | } else { |
Mike Cleron | b64b67a | 2009-11-08 14:56:25 -0800 | [diff] [blame] | 279 | if (mRollo != null) { |
| 280 | if (mArrowNavigation) { |
| 281 | // Clear selection when we lose focus |
| 282 | mRollo.clearSelectedIcon(); |
| 283 | mRollo.setHomeSelected(SELECTED_NONE); |
| 284 | mRollo.mState.save(); |
| 285 | mArrowNavigation = false; |
| 286 | } |
| 287 | } else { |
| 288 | mShouldGainFocus = false; |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Mike Cleron | b64b67a | 2009-11-08 14:56:25 -0800 | [diff] [blame] | 293 | private void gainFocus() { |
| 294 | if (!mArrowNavigation && mRollo.mState.iconCount > 0) { |
| 295 | // Select the first icon when we gain keyboard focus |
| 296 | mArrowNavigation = true; |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 297 | mRollo.selectIcon(Math.round(mPosX) * Defines.COLUMNS_PER_PAGE, |
Mike Cleron | b64b67a | 2009-11-08 14:56:25 -0800 | [diff] [blame] | 298 | SELECTED_FOCUSED); |
| 299 | mRollo.mState.save(); |
| 300 | } |
| 301 | } |
| 302 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 303 | @Override |
| 304 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 305 | |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 306 | boolean handled = false; |
Jason Sams | e26d9fc | 2009-11-12 14:00:43 -0800 | [diff] [blame] | 307 | |
Joe Onorato | 859b3a7 | 2009-10-28 15:17:01 -0400 | [diff] [blame] | 308 | if (!isVisible()) { |
| 309 | return false; |
| 310 | } |
Joe Onorato | a13f574 | 2009-11-02 17:15:19 -0500 | [diff] [blame] | 311 | final int iconCount = mRollo.mState.iconCount; |
| 312 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 313 | if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) { |
| 314 | if (mArrowNavigation) { |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 315 | if (mLastSelection == SELECTION_HOME) { |
| 316 | reallyPlaySoundEffect(SoundEffectConstants.CLICK); |
| 317 | mLauncher.closeAllApps(true); |
| 318 | } else { |
| 319 | int whichApp = mRollo.mState.selectedIconIndex; |
| 320 | if (whichApp >= 0) { |
| 321 | ApplicationInfo app = mAllAppsList.get(whichApp); |
| 322 | mLauncher.startActivitySafely(app.intent); |
| 323 | handled = true; |
| 324 | } |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | } |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 328 | |
Joe Onorato | 478730f | 2009-11-16 18:54:43 -0800 | [diff] [blame] | 329 | if (iconCount > 0) { |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 330 | mArrowNavigation = true; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 331 | |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 332 | int currentSelection = mRollo.mState.selectedIconIndex; |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 333 | int currentTopRow = Math.round(mPosX); |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 334 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 335 | // The column of the current selection, in the range 0..COLUMNS_PER_PAGE-1 |
Joe Onorato | a13f574 | 2009-11-02 17:15:19 -0500 | [diff] [blame] | 336 | final int currentPageCol = currentSelection % Defines.COLUMNS_PER_PAGE; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 337 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 338 | // The row of the current selection, in the range 0..ROWS_PER_PAGE-1 |
Joe Onorato | a13f574 | 2009-11-02 17:15:19 -0500 | [diff] [blame] | 339 | final int currentPageRow = (currentSelection - (currentTopRow*Defines.COLUMNS_PER_PAGE)) |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 340 | / Defines.ROWS_PER_PAGE; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 341 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 342 | int newSelection = currentSelection; |
| 343 | |
| 344 | switch (keyCode) { |
| 345 | case KeyEvent.KEYCODE_DPAD_UP: |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 346 | if (mLastSelection == SELECTION_HOME) { |
| 347 | mRollo.setHomeSelected(SELECTED_NONE); |
| 348 | int lastRowCount = iconCount % Defines.COLUMNS_PER_PAGE; |
| 349 | if (lastRowCount == 0) { |
| 350 | lastRowCount = Defines.COLUMNS_PER_PAGE; |
| 351 | } |
| 352 | newSelection = iconCount - lastRowCount + (Defines.COLUMNS_PER_PAGE / 2); |
| 353 | if (newSelection >= iconCount) { |
| 354 | newSelection = iconCount-1; |
| 355 | } |
| 356 | int target = (newSelection / Defines.COLUMNS_PER_PAGE) |
| 357 | - (Defines.ROWS_PER_PAGE - 1); |
| 358 | if (target < 0) { |
| 359 | target = 0; |
| 360 | } |
| 361 | if (currentTopRow != target) { |
| 362 | mRollo.moveTo(target); |
| 363 | } |
| 364 | } else { |
| 365 | if (currentPageRow > 0) { |
| 366 | newSelection = currentSelection - Defines.COLUMNS_PER_PAGE; |
| 367 | } else if (currentTopRow > 0) { |
| 368 | newSelection = currentSelection - Defines.COLUMNS_PER_PAGE; |
| 369 | mRollo.moveTo(newSelection / Defines.COLUMNS_PER_PAGE); |
Joe Onorato | 478730f | 2009-11-16 18:54:43 -0800 | [diff] [blame] | 370 | } else { |
| 371 | newSelection = Defines.COLUMNS_PER_PAGE * (Defines.ROWS_PER_PAGE-1); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 372 | } |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 373 | } |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 374 | handled = true; |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 375 | break; |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 376 | |
Joe Onorato | a13f574 | 2009-11-02 17:15:19 -0500 | [diff] [blame] | 377 | case KeyEvent.KEYCODE_DPAD_DOWN: { |
| 378 | final int rowCount = iconCount / Defines.COLUMNS_PER_PAGE |
| 379 | + (iconCount % Defines.COLUMNS_PER_PAGE == 0 ? 0 : 1); |
| 380 | final int currentRow = currentSelection / Defines.COLUMNS_PER_PAGE; |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 381 | if (mLastSelection != SELECTION_HOME) { |
| 382 | if (currentRow < rowCount-1) { |
| 383 | mRollo.setHomeSelected(SELECTED_NONE); |
Joe Onorato | 478730f | 2009-11-16 18:54:43 -0800 | [diff] [blame] | 384 | if (currentSelection < 0) { |
| 385 | newSelection = 0; |
| 386 | } else { |
| 387 | newSelection = currentSelection + Defines.COLUMNS_PER_PAGE; |
| 388 | } |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 389 | if (newSelection >= iconCount) { |
| 390 | // Go from D to G in this arrangement: |
| 391 | // A B C D |
| 392 | // E F G |
| 393 | newSelection = iconCount - 1; |
| 394 | } |
| 395 | if (currentPageRow >= Defines.ROWS_PER_PAGE - 1) { |
| 396 | mRollo.moveTo((newSelection / Defines.COLUMNS_PER_PAGE) - |
| 397 | Defines.ROWS_PER_PAGE + 1); |
| 398 | } |
| 399 | } else { |
| 400 | newSelection = -1; |
| 401 | mRollo.setHomeSelected(SELECTED_FOCUSED); |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 404 | handled = true; |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 405 | break; |
Joe Onorato | a13f574 | 2009-11-02 17:15:19 -0500 | [diff] [blame] | 406 | } |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 407 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 408 | if (currentPageCol > 0) { |
| 409 | newSelection = currentSelection - 1; |
| 410 | } |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 411 | handled = true; |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 412 | break; |
| 413 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 414 | if ((currentPageCol < Defines.COLUMNS_PER_PAGE - 1) && |
Joe Onorato | a13f574 | 2009-11-02 17:15:19 -0500 | [diff] [blame] | 415 | (currentSelection < iconCount - 1)) { |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 416 | newSelection = currentSelection + 1; |
| 417 | } |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 418 | handled = true; |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 419 | break; |
| 420 | } |
| 421 | if (newSelection != currentSelection) { |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 422 | mRollo.selectIcon(newSelection, SELECTED_FOCUSED); |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 423 | mRollo.mState.save(); |
| 424 | } |
| 425 | } |
Mike Cleron | 4a5c1e1 | 2009-11-03 10:17:05 -0800 | [diff] [blame] | 426 | return handled; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 429 | @Override |
| 430 | public boolean onTouchEvent(MotionEvent ev) |
| 431 | { |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 432 | mArrowNavigation = false; |
Jason Sams | 2e19c05 | 2009-10-20 18:19:55 -0700 | [diff] [blame] | 433 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 434 | if (!isVisible()) { |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 435 | return true; |
Joe Onorato | e3406a2 | 2009-09-03 14:36:25 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 438 | if (mLocks != 0) { |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 439 | return true; |
| 440 | } |
| 441 | |
| 442 | super.onTouchEvent(ev); |
| 443 | |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 444 | int x = (int)ev.getX(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 445 | int y = (int)ev.getY(); |
| 446 | |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 447 | int action = ev.getAction(); |
| 448 | switch (action) { |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 449 | case MotionEvent.ACTION_DOWN: |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 450 | if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) { |
| 451 | mTouchTracking = TRACKING_HOME; |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 452 | mRollo.setHomeSelected(SELECTED_PRESSED); |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 453 | mRollo.mState.save(); |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 454 | mCurrentIconIndex = -1; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 455 | } else { |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 456 | mTouchTracking = TRACKING_FLING; |
| 457 | |
| 458 | mMotionDownRawX = (int)ev.getRawX(); |
| 459 | mMotionDownRawY = (int)ev.getRawY(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 460 | |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 461 | mRollo.mState.newPositionX = ev.getRawY() / getHeight(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 462 | mRollo.mState.newTouchDown = 1; |
| 463 | |
| 464 | if (!mRollo.checkClickOK()) { |
| 465 | mRollo.clearSelectedIcon(); |
| 466 | } else { |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 467 | mDownIconIndex = mCurrentIconIndex |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 468 | = mRollo.selectIcon(x, y, mPosX, SELECTED_PRESSED); |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 469 | if (mDownIconIndex < 0) { |
| 470 | // if nothing was selected, no long press. |
| 471 | cancelLongPress(); |
| 472 | } |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 473 | } |
| 474 | mRollo.mState.save(); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 475 | mRollo.move(); |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 476 | mVelocityTracker = VelocityTracker.obtain(); |
| 477 | mVelocityTracker.addMovement(ev); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 478 | mStartedScrolling = false; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 479 | } |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 480 | break; |
| 481 | case MotionEvent.ACTION_MOVE: |
| 482 | case MotionEvent.ACTION_OUTSIDE: |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 483 | if (mTouchTracking == TRACKING_HOME) { |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 484 | mRollo.setHomeSelected(y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1] |
| 485 | ? SELECTED_PRESSED : SELECTED_NONE); |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 486 | mRollo.mState.save(); |
Joe Onorato | 68ffd10 | 2009-10-15 17:59:43 -0700 | [diff] [blame] | 487 | } else if (mTouchTracking == TRACKING_FLING) { |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 488 | int rawX = (int)ev.getRawX(); |
| 489 | int rawY = (int)ev.getRawY(); |
| 490 | int slop; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 491 | slop = Math.abs(rawY - mMotionDownRawY); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 492 | |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 493 | if (!mStartedScrolling && slop < mSlop) { |
| 494 | // don't update anything so when we do start scrolling |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 495 | // below, we get the right delta. |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 496 | mCurrentIconIndex = mRollo.chooseTappedIcon(x, y, mPosX); |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 497 | if (mDownIconIndex != mCurrentIconIndex) { |
| 498 | // If a different icon is selected, don't allow it to be picked up. |
| 499 | // This handles off-axis dragging. |
| 500 | cancelLongPress(); |
| 501 | mCurrentIconIndex = -1; |
| 502 | } |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 503 | } else { |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 504 | if (!mStartedScrolling) { |
| 505 | cancelLongPress(); |
| 506 | mCurrentIconIndex = -1; |
| 507 | } |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 508 | mRollo.mState.newPositionX = ev.getRawY() / getHeight(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 509 | mRollo.mState.newTouchDown = 1; |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 510 | mRollo.move(); |
Jason Sams | 86c87ed | 2009-09-18 13:55:55 -0700 | [diff] [blame] | 511 | |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 512 | mStartedScrolling = true; |
| 513 | mRollo.clearSelectedIcon(); |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 514 | mVelocityTracker.addMovement(ev); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 515 | mRollo.mState.save(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 516 | } |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 517 | } |
| 518 | break; |
| 519 | case MotionEvent.ACTION_UP: |
| 520 | case MotionEvent.ACTION_CANCEL: |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 521 | if (mTouchTracking == TRACKING_HOME) { |
| 522 | if (action == MotionEvent.ACTION_UP) { |
| 523 | if (y > mRollo.mTouchYBorders[mRollo.mTouchYBorders.length-1]) { |
Joe Onorato | b39e51a | 2009-10-28 15:47:49 -0400 | [diff] [blame] | 524 | reallyPlaySoundEffect(SoundEffectConstants.CLICK); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 525 | mLauncher.closeAllApps(true); |
| 526 | } |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 527 | mRollo.setHomeSelected(SELECTED_NONE); |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 528 | mRollo.mState.save(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 529 | } |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 530 | mCurrentIconIndex = -1; |
Joe Onorato | 68ffd10 | 2009-10-15 17:59:43 -0700 | [diff] [blame] | 531 | } else if (mTouchTracking == TRACKING_FLING) { |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 532 | mRollo.mState.newTouchDown = 0; |
Mike Cleron | 7d5d746 | 2009-10-20 14:06:00 -0700 | [diff] [blame] | 533 | mRollo.mState.newPositionX = ev.getRawY() / getHeight(); |
Jason Sams | 476339d | 2009-09-29 18:14:38 -0700 | [diff] [blame] | 534 | |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 535 | mVelocityTracker.computeCurrentVelocity(1000 /* px/sec */, mMaxFlingVelocity); |
| 536 | mRollo.mState.flingVelocity = mVelocityTracker.getYVelocity() / getHeight(); |
Jason Sams | 12c14a8 | 2009-10-06 14:33:15 -0700 | [diff] [blame] | 537 | mRollo.clearSelectedIcon(); |
| 538 | mRollo.mState.save(); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 539 | mRollo.fling(); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 540 | |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 541 | if (mVelocityTracker != null) { |
| 542 | mVelocityTracker.recycle(); |
| 543 | mVelocityTracker = null; |
Joe Onorato | 539ed9d | 2009-10-02 10:22:14 -0700 | [diff] [blame] | 544 | } |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 545 | } |
Joe Onorato | 68ffd10 | 2009-10-15 17:59:43 -0700 | [diff] [blame] | 546 | mTouchTracking = TRACKING_NONE; |
| 547 | break; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 548 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 549 | |
| 550 | return true; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 553 | public void onClick(View v) { |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 554 | if (mLocks != 0 || !isVisible()) { |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 555 | return; |
| 556 | } |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 557 | if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex |
| 558 | && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) { |
Joe Onorato | b39e51a | 2009-10-28 15:47:49 -0400 | [diff] [blame] | 559 | reallyPlaySoundEffect(SoundEffectConstants.CLICK); |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 560 | ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 561 | mLauncher.startActivitySafely(app.intent); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | public boolean onLongClick(View v) { |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 566 | if (mLocks != 0 || !isVisible()) { |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 567 | return true; |
| 568 | } |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 569 | if (mRollo.checkClickOK() && mCurrentIconIndex == mDownIconIndex |
| 570 | && mCurrentIconIndex >= 0 && mCurrentIconIndex < mAllAppsList.size()) { |
| 571 | ApplicationInfo app = mAllAppsList.get(mCurrentIconIndex); |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 572 | |
| 573 | // 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] | 574 | int screenX = mMotionDownRawX - (mDefines.ICON_WIDTH_PX / 2); |
| 575 | int screenY = mMotionDownRawY - mDefines.ICON_HEIGHT_PX; |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 576 | |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 577 | int left = (mDefines.ICON_TEXTURE_WIDTH_PX - mDefines.ICON_WIDTH_PX) / 2; |
| 578 | int top = (mDefines.ICON_TEXTURE_HEIGHT_PX - mDefines.ICON_HEIGHT_PX) / 2; |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 579 | mDragController.startDrag(app.iconBitmap, screenX, screenY, |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 580 | left, top, mDefines.ICON_WIDTH_PX, mDefines.ICON_HEIGHT_PX, |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 581 | this, app, DragController.DRAG_ACTION_COPY); |
Joe Onorato | e3406a2 | 2009-09-03 14:36:25 -0700 | [diff] [blame] | 582 | |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 583 | mLauncher.closeAllApps(true); |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 584 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 585 | return true; |
| 586 | } |
| 587 | |
Joe Onorato | 52a653f | 2009-11-11 14:52:11 -0800 | [diff] [blame] | 588 | @Override |
| 589 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 590 | if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SELECTED) { |
| 591 | if (!isVisible()) { |
| 592 | return false; |
| 593 | } |
| 594 | String text = null; |
| 595 | int index; |
| 596 | int count = mAllAppsList.size() + 1; // +1 is home |
| 597 | int pos = -1; |
| 598 | switch (mLastSelection) { |
| 599 | case SELECTION_ICONS: |
| 600 | index = mRollo.mState.selectedIconIndex; |
| 601 | if (index >= 0) { |
| 602 | ApplicationInfo info = mAllAppsList.get(index); |
| 603 | if (info.title != null) { |
| 604 | text = info.title.toString(); |
| 605 | pos = index; |
| 606 | } |
| 607 | } |
| 608 | break; |
| 609 | case SELECTION_HOME: |
| 610 | text = getContext().getString(R.string.all_apps_home_button_label); |
| 611 | pos = count; |
| 612 | break; |
| 613 | } |
| 614 | if (text != null) { |
Joe Onorato | 52a653f | 2009-11-11 14:52:11 -0800 | [diff] [blame] | 615 | event.setEnabled(true); |
| 616 | event.getText().add(text); |
| 617 | //event.setContentDescription(text); |
| 618 | event.setItemCount(count); |
| 619 | event.setCurrentItemIndex(pos); |
| 620 | } |
| 621 | } |
| 622 | return false; |
| 623 | } |
| 624 | |
Joe Onorato | 5162ea9 | 2009-09-03 09:39:42 -0700 | [diff] [blame] | 625 | public void setDragController(DragController dragger) { |
| 626 | mDragController = dragger; |
| 627 | } |
| 628 | |
| 629 | public void onDropCompleted(View target, boolean success) { |
| 630 | } |
| 631 | |
Joe Onorato | 4db5231 | 2009-10-06 11:17:43 -0700 | [diff] [blame] | 632 | /** |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 633 | * Zoom to the specifed level. |
Joe Onorato | 4db5231 | 2009-10-06 11:17:43 -0700 | [diff] [blame] | 634 | * |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 635 | * @param zoom [0..1] 0 is hidden, 1 is open |
Joe Onorato | 4db5231 | 2009-10-06 11:17:43 -0700 | [diff] [blame] | 636 | */ |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 637 | public void zoom(float zoom, boolean animate) { |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 638 | cancelLongPress(); |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 639 | if (mRollo == null || !mRollo.mHasSurface) { |
| 640 | mZoomDirty = true; |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 641 | mZoom = zoom; |
| 642 | mAnimateNextZoom = animate; |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 643 | return; |
Joe Onorato | 85a02a8 | 2009-09-08 12:34:22 -0700 | [diff] [blame] | 644 | } else { |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 645 | mRollo.setZoom(zoom, animate); |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 646 | } |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | public boolean isVisible() { |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 650 | return mZoom > 0.001f; |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | public boolean isOpaque() { |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 654 | return mZoom > 0.999f; |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 655 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 656 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 657 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 658 | mAllAppsList = list; |
| 659 | if (mRollo != null) { |
| 660 | mRollo.setApps(list); |
| 661 | } |
Joe Onorato | fb0ca67 | 2009-09-14 17:55:46 -0400 | [diff] [blame] | 662 | mLocks &= ~LOCK_ICONS_PENDING; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 665 | public void addApps(ArrayList<ApplicationInfo> list) { |
Joe Onorato | 2d80476 | 2009-11-05 16:02:32 -0500 | [diff] [blame] | 666 | if (mAllAppsList == null) { |
| 667 | // Not done loading yet. We'll find out about it later. |
| 668 | return; |
| 669 | } |
| 670 | |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 671 | final int N = list.size(); |
| 672 | if (mRollo != null) { |
| 673 | mRollo.reallocAppsList(mRollo.mState.iconCount + N); |
| 674 | } |
| 675 | |
| 676 | for (int i=0; i<N; i++) { |
| 677 | final ApplicationInfo item = list.get(i); |
| 678 | int index = Collections.binarySearch(mAllAppsList, item, mAppNameComp); |
| 679 | if (index < 0) { |
| 680 | index = -(index+1); |
| 681 | } |
| 682 | mAllAppsList.add(index, item); |
| 683 | if (mRollo != null) { |
| 684 | mRollo.addApp(index, item); |
| 685 | mRollo.mState.iconCount++; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | if (mRollo != null) { |
| 690 | mRollo.saveAppsList(); |
| 691 | } |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 694 | public void removeApps(ArrayList<ApplicationInfo> list) { |
Joe Onorato | 2d80476 | 2009-11-05 16:02:32 -0500 | [diff] [blame] | 695 | if (mAllAppsList == null) { |
| 696 | // Not done loading yet. We'll find out about it later. |
| 697 | return; |
| 698 | } |
| 699 | |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 700 | final int N = list.size(); |
| 701 | for (int i=0; i<N; i++) { |
| 702 | final ApplicationInfo item = list.get(i); |
Joe Onorato | cb9f798 | 2009-10-31 16:32:02 -0400 | [diff] [blame] | 703 | int index = findAppByComponent(mAllAppsList, item); |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 704 | if (index >= 0) { |
| 705 | mAllAppsList.remove(index); |
| 706 | if (mRollo != null) { |
| 707 | mRollo.removeApp(index); |
| 708 | mRollo.mState.iconCount--; |
| 709 | } |
| 710 | } else { |
Joe Onorato | a30ce8e | 2009-11-11 08:16:49 -0800 | [diff] [blame] | 711 | Log.w(TAG, "couldn't find a match for item \"" + item + "\""); |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 712 | // Try to recover. This should keep us from crashing for now. |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | if (mRollo != null) { |
| 717 | mRollo.saveAppsList(); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | public void updateApps(String packageName, ArrayList<ApplicationInfo> list) { |
| 722 | // Just remove and add, because they may need to be re-sorted. |
| 723 | removeApps(list); |
| 724 | addApps(list); |
| 725 | } |
| 726 | |
| 727 | private Comparator<ApplicationInfo> mAppNameComp = new Comparator<ApplicationInfo>() { |
| 728 | public int compare(ApplicationInfo a, ApplicationInfo b) { |
| 729 | int result = a.title.toString().compareTo(b.toString()); |
| 730 | if (result != 0) { |
| 731 | return result; |
| 732 | } |
| 733 | return a.intent.getComponent().compareTo(b.intent.getComponent()); |
| 734 | } |
| 735 | }; |
| 736 | |
Joe Onorato | cb9f798 | 2009-10-31 16:32:02 -0400 | [diff] [blame] | 737 | private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) { |
| 738 | ComponentName component = item.intent.getComponent(); |
| 739 | final int N = list.size(); |
| 740 | for (int i=0; i<N; i++) { |
| 741 | ApplicationInfo x = list.get(i); |
| 742 | if (x.intent.getComponent().equals(component)) { |
| 743 | return i; |
| 744 | } |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 745 | } |
Joe Onorato | cb9f798 | 2009-10-31 16:32:02 -0400 | [diff] [blame] | 746 | return -1; |
| 747 | } |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 748 | |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 749 | private static int countPages(int iconCount) { |
| 750 | int iconsPerPage = Defines.COLUMNS_PER_PAGE * Defines.ROWS_PER_PAGE; |
| 751 | int pages = iconCount / iconsPerPage; |
| 752 | if (pages*iconsPerPage != iconCount) { |
| 753 | pages++; |
| 754 | } |
| 755 | return pages; |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 756 | } |
| 757 | |
Joe Onorato | cb75f36 | 2009-11-12 13:04:07 -0800 | [diff] [blame] | 758 | class AAMessage extends RenderScript.RSMessage { |
| 759 | public void run() { |
| 760 | mPosX = ((float)mData[0]) / (1 << 16); |
| 761 | mVelocity = ((float)mData[1]) / (1 << 16); |
| 762 | mZoom = ((float)mData[2]) / (1 << 16); |
| 763 | mZoomDirty = false; |
| 764 | } |
Joe Onorato | cb75f36 | 2009-11-12 13:04:07 -0800 | [diff] [blame] | 765 | } |
| 766 | |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 767 | public class RolloRS { |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 768 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 769 | // Allocations ====== |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 770 | private int mWidth; |
| 771 | private int mHeight; |
| 772 | |
| 773 | private Resources mRes; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 774 | private Script mScript; |
| 775 | private Script.Invokable mInvokeMove; |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 776 | private Script.Invokable mInvokeMoveTo; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 777 | private Script.Invokable mInvokeFling; |
| 778 | private Script.Invokable mInvokeResetWAR; |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 779 | private Script.Invokable mInvokeSetZoom; |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 780 | |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 781 | private ProgramStore mPSIcons; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 782 | private ProgramStore mPSText; |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 783 | private ProgramFragment mPFColor; |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 784 | private ProgramFragment mPFTexMip; |
| 785 | private ProgramFragment mPFTexNearest; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 786 | private ProgramVertex mPV; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 787 | private ProgramVertex mPVOrtho; |
Jason Sams | 0aa7166 | 2009-10-02 18:43:18 -0700 | [diff] [blame] | 788 | private SimpleMesh mMesh; |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 789 | private SimpleMesh mMesh2; |
Jason Sams | 5612e43 | 2009-11-16 14:18:07 -0800 | [diff] [blame] | 790 | private ProgramVertex.MatrixAllocation mPVA; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 791 | |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 792 | private Allocation mHomeButtonNormal; |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 793 | private Allocation mHomeButtonFocused; |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 794 | private Allocation mHomeButtonPressed; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 795 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 796 | private Allocation[] mIcons; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 797 | private int[] mIconIds; |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 798 | private Allocation mAllocIconIds; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 799 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 800 | private Allocation[] mLabels; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 801 | private int[] mLabelIds; |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 802 | private Allocation mAllocLabelIds; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 803 | private Allocation mSelectedIcon; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 804 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 805 | private int[] mTouchYBorders; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 806 | private int[] mTouchXBorders; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 807 | |
| 808 | private Bitmap mSelectionBitmap; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 809 | private Canvas mSelectionCanvas; |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 810 | |
Jason Sams | 20df7c7 | 2009-11-05 12:30:24 -0800 | [diff] [blame] | 811 | boolean mHasSurface = false; |
| 812 | private boolean mAppsDirty = false; |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 813 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 814 | Params mParams; |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 815 | State mState; |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 816 | |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 817 | class BaseAlloc { |
| 818 | Allocation mAlloc; |
| 819 | Type mType; |
| 820 | |
| 821 | void save() { |
| 822 | mAlloc.data(this); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 823 | } |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Jason Sams | 476339d | 2009-09-29 18:14:38 -0700 | [diff] [blame] | 826 | private boolean checkClickOK() { |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 827 | return (Math.abs(mVelocity) < 0.4f) && |
| 828 | (Math.abs(mPosX - Math.round(mPosX)) < 0.4f); |
Jason Sams | 476339d | 2009-09-29 18:14:38 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 831 | class Params extends BaseAlloc { |
| 832 | Params() { |
| 833 | mType = Type.createFromClass(mRS, Params.class, 1, "ParamsClass"); |
| 834 | mAlloc = Allocation.createTyped(mRS, mType); |
| 835 | save(); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 836 | } |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 837 | public int bubbleWidth; |
| 838 | public int bubbleHeight; |
| 839 | public int bubbleBitmapWidth; |
| 840 | public int bubbleBitmapHeight; |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 841 | |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 842 | public int homeButtonWidth; |
| 843 | public int homeButtonHeight; |
| 844 | public int homeButtonTextureWidth; |
| 845 | public int homeButtonTextureHeight; |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | class State extends BaseAlloc { |
Jason Sams | 86c87ed | 2009-09-18 13:55:55 -0700 | [diff] [blame] | 849 | public float newPositionX; |
| 850 | public int newTouchDown; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 851 | public float flingVelocity; |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 852 | public int iconCount; |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 853 | public int selectedIconIndex = -1; |
| 854 | public int selectedIconTexture; |
Joe Onorato | 7bb1749 | 2009-09-24 17:51:01 -0700 | [diff] [blame] | 855 | public float zoomTarget; |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 856 | public int homeButtonId; |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 857 | public float targetPos; |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 858 | |
| 859 | State() { |
| 860 | mType = Type.createFromClass(mRS, State.class, 1, "StateClass"); |
| 861 | mAlloc = Allocation.createTyped(mRS, mType); |
| 862 | save(); |
| 863 | } |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | public RolloRS() { |
| 867 | } |
| 868 | |
| 869 | public void init(Resources res, int width, int height) { |
| 870 | mRes = res; |
| 871 | mWidth = width; |
| 872 | mHeight = height; |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 873 | mDefines.recompute(width, height); |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 874 | initProgramVertex(); |
| 875 | initProgramFragment(); |
| 876 | initProgramStore(); |
Jason Sams | 0aa7166 | 2009-10-02 18:43:18 -0700 | [diff] [blame] | 877 | initMesh(); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 878 | initGl(); |
| 879 | initData(); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 880 | initTouchState(); |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 881 | initRs(); |
| 882 | } |
| 883 | |
Jason Sams | 0aa7166 | 2009-10-02 18:43:18 -0700 | [diff] [blame] | 884 | public void initMesh() { |
| 885 | SimpleMesh.TriangleMeshBuilder tm = new SimpleMesh.TriangleMeshBuilder(mRS, 3, |
| 886 | SimpleMesh.TriangleMeshBuilder.TEXTURE_0 | SimpleMesh.TriangleMeshBuilder.COLOR); |
| 887 | |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 888 | float y = 0; |
| 889 | float z = 0; |
| 890 | for (int ct=0; ct < 200; ct++) { |
| 891 | float angle = 0; |
| 892 | float maxAngle = 3.14f * 0.16f; |
| 893 | float l = 1.f; |
| 894 | |
| 895 | l = 1 - ((ct-5) * 0.10f); |
| 896 | if (ct > 7) { |
| 897 | angle = maxAngle * (ct - 7) * 0.2f; |
| 898 | angle = Math.min(angle, maxAngle); |
| 899 | } |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 900 | l = Math.max(0.4f, l); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 901 | l = Math.min(1.0f, l); |
| 902 | |
| 903 | y += 0.1f * Math.cos(angle); |
| 904 | z += 0.1f * Math.sin(angle); |
| 905 | |
| 906 | float t = 0.1f * ct; |
| 907 | float ds = 0.08f; |
| 908 | tm.setColor(l, l, l, 0.99f); |
| 909 | tm.setTexture(ds, t); |
| 910 | tm.addVertex(-0.5f, y, z); |
| 911 | tm.setTexture(1 - ds, t); |
| 912 | tm.addVertex(0.5f, y, z); |
| 913 | } |
| 914 | for (int ct=0; ct < (200 * 2 - 2); ct+= 2) { |
| 915 | tm.addTriangle(ct, ct+1, ct+2); |
| 916 | tm.addTriangle(ct+1, ct+3, ct+2); |
| 917 | } |
| 918 | mMesh2 = tm.create(); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 919 | mMesh2.setName("SMMesh"); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Jason Sams | 5612e43 | 2009-11-16 14:18:07 -0800 | [diff] [blame] | 922 | void resize(int w, int h) { |
| 923 | mPVA.setupProjectionNormalized(w, h); |
| 924 | mWidth = w; |
| 925 | mHeight = h; |
| 926 | } |
| 927 | |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 928 | private void initProgramVertex() { |
Jason Sams | 5612e43 | 2009-11-16 14:18:07 -0800 | [diff] [blame] | 929 | mPVA = new ProgramVertex.MatrixAllocation(mRS); |
| 930 | resize(mWidth, mHeight); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 931 | |
| 932 | ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null); |
Jason Sams | 0aa7166 | 2009-10-02 18:43:18 -0700 | [diff] [blame] | 933 | pvb.setTextureMatrixEnable(true); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 934 | mPV = pvb.create(); |
| 935 | mPV.setName("PV"); |
Jason Sams | 5612e43 | 2009-11-16 14:18:07 -0800 | [diff] [blame] | 936 | mPV.bindAllocation(mPVA); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 937 | |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 938 | //pva = new ProgramVertex.MatrixAllocation(mRS); |
| 939 | //pva.setupOrthoWindow(mWidth, mHeight); |
| 940 | //pvb.setTextureMatrixEnable(true); |
| 941 | //mPVOrtho = pvb.create(); |
| 942 | //mPVOrtho.setName("PVOrtho"); |
| 943 | //mPVOrtho.bindAllocation(pva); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 944 | |
| 945 | mRS.contextBindProgramVertex(mPV); |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 946 | } |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 947 | |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 948 | private void initProgramFragment() { |
| 949 | Sampler.Builder sb = new Sampler.Builder(mRS); |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 950 | sb.setMin(Sampler.Value.LINEAR_MIP_LINEAR); |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 951 | sb.setMag(Sampler.Value.LINEAR); |
| 952 | sb.setWrapS(Sampler.Value.CLAMP); |
| 953 | sb.setWrapT(Sampler.Value.CLAMP); |
| 954 | Sampler linear = sb.create(); |
| 955 | |
| 956 | sb.setMin(Sampler.Value.NEAREST); |
| 957 | sb.setMag(Sampler.Value.NEAREST); |
| 958 | Sampler nearest = sb.create(); |
| 959 | |
| 960 | ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 961 | //mPFColor = bf.create(); |
| 962 | //mPFColor.setName("PFColor"); |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 963 | |
| 964 | bf.setTexEnable(true, 0); |
| 965 | bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0); |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 966 | mPFTexMip = bf.create(); |
| 967 | mPFTexMip.setName("PFTexMip"); |
| 968 | mPFTexMip.bindSampler(linear, 0); |
| 969 | |
| 970 | mPFTexNearest = bf.create(); |
| 971 | mPFTexNearest.setName("PFTexNearest"); |
| 972 | mPFTexNearest.bindSampler(nearest, 0); |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | private void initProgramStore() { |
| 976 | ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null); |
| 977 | bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS); |
Jason Sams | 12c14a8 | 2009-10-06 14:33:15 -0700 | [diff] [blame] | 978 | bs.setColorMask(true,true,true,false); |
Jason Sams | cd689e1 | 2009-09-29 15:28:22 -0700 | [diff] [blame] | 979 | bs.setDitherEnable(true); |
| 980 | bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA, |
| 981 | ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA); |
| 982 | mPSIcons = bs.create(); |
| 983 | mPSIcons.setName("PSIcons"); |
| 984 | |
| 985 | //bs.setDitherEnable(false); |
| 986 | //mPSText = bs.create(); |
| 987 | //mPSText.setName("PSText"); |
| 988 | } |
| 989 | |
| 990 | private void initGl() { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 991 | mTouchXBorders = new int[Defines.COLUMNS_PER_PAGE+1]; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 992 | mTouchYBorders = new int[Defines.ROWS_PER_PAGE+1]; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 993 | } |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 994 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 995 | private void initData() { |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 996 | mParams = new Params(); |
| 997 | mState = new State(); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 998 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 999 | final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext()); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 1000 | |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 1001 | mParams.bubbleWidth = bubble.getBubbleWidth(); |
| 1002 | mParams.bubbleHeight = bubble.getMaxBubbleHeight(); |
| 1003 | mParams.bubbleBitmapWidth = bubble.getBitmapWidth(); |
| 1004 | mParams.bubbleBitmapHeight = bubble.getBitmapHeight(); |
Joe Onorato | 43e7bcf | 2009-08-08 18:53:53 -0700 | [diff] [blame] | 1005 | |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 1006 | mHomeButtonNormal = Allocation.createFromBitmapResource(mRS, mRes, |
| 1007 | R.drawable.home_button_normal, Element.RGBA_8888(mRS), false); |
| 1008 | mHomeButtonNormal.uploadToTexture(0); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 1009 | mHomeButtonFocused = Allocation.createFromBitmapResource(mRS, mRes, |
| 1010 | R.drawable.home_button_focused, Element.RGBA_8888(mRS), false); |
| 1011 | mHomeButtonFocused.uploadToTexture(0); |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 1012 | mHomeButtonPressed = Allocation.createFromBitmapResource(mRS, mRes, |
| 1013 | R.drawable.home_button_pressed, Element.RGBA_8888(mRS), false); |
| 1014 | mHomeButtonPressed.uploadToTexture(0); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 1015 | mParams.homeButtonWidth = 76; |
| 1016 | mParams.homeButtonHeight = 68; |
| 1017 | mParams.homeButtonTextureWidth = 128; |
| 1018 | mParams.homeButtonTextureHeight = 128; |
Joe Onorato | c567acb | 2009-08-31 14:34:43 -0700 | [diff] [blame] | 1019 | |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 1020 | mState.homeButtonId = mHomeButtonNormal.getID(); |
| 1021 | |
Joe Onorato | 1feb3a8 | 2009-08-08 22:32:00 -0700 | [diff] [blame] | 1022 | mParams.save(); |
| 1023 | mState.save(); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1024 | |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 1025 | mSelectionBitmap = Bitmap.createBitmap(Defines.ICON_TEXTURE_WIDTH_PX, |
| 1026 | Defines.ICON_TEXTURE_HEIGHT_PX, Bitmap.Config.ARGB_8888); |
| 1027 | mSelectionCanvas = new Canvas(mSelectionBitmap); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1028 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1029 | setApps(null); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1032 | private void initScript(int id) { |
| 1033 | } |
| 1034 | |
| 1035 | private void initRs() { |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 1036 | ScriptC.Builder sb = new ScriptC.Builder(mRS); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1037 | sb.setScript(mRes, R.raw.rollo3); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 1038 | sb.setRoot(true); |
Joe Onorato | bcbeab8 | 2009-10-01 21:45:43 -0700 | [diff] [blame] | 1039 | sb.addDefines(mDefines); |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 1040 | sb.setType(mParams.mType, "params", Defines.ALLOC_PARAMS); |
| 1041 | sb.setType(mState.mType, "state", Defines.ALLOC_STATE); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1042 | mInvokeMove = sb.addInvokable("move"); |
| 1043 | mInvokeFling = sb.addInvokable("fling"); |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 1044 | mInvokeMoveTo = sb.addInvokable("moveTo"); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1045 | mInvokeResetWAR = sb.addInvokable("resetHWWar"); |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 1046 | mInvokeSetZoom = sb.addInvokable("setZoom"); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1047 | mScript = sb.create(); |
| 1048 | mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 1049 | mScript.bindAllocation(mParams.mAlloc, Defines.ALLOC_PARAMS); |
| 1050 | mScript.bindAllocation(mState.mAlloc, Defines.ALLOC_STATE); |
| 1051 | mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS); |
| 1052 | mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 1053 | |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1054 | mRS.contextBindRootScript(mScript); |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 1055 | } |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 1056 | |
Jason Sams | 20df7c7 | 2009-11-05 12:30:24 -0800 | [diff] [blame] | 1057 | private void uploadApps(ArrayList<ApplicationInfo> list) { |
| 1058 | for (int i=0; i < mState.iconCount; i++) { |
| 1059 | uploadAppIcon(i, list.get(i)); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | void dirtyCheck() { |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 1064 | if (mHasSurface) { |
| 1065 | if (mAppsDirty) { |
| 1066 | uploadApps(mAllAppsList); |
| 1067 | saveAppsList(); |
| 1068 | mAppsDirty = false; |
| 1069 | } |
| 1070 | if (mZoomDirty) { |
Joe Onorato | 68ba5ca | 2009-11-12 14:23:43 -0800 | [diff] [blame] | 1071 | setZoom(mZoom, mAnimateNextZoom); |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 1072 | } |
Jason Sams | 20df7c7 | 2009-11-05 12:30:24 -0800 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1076 | private void setApps(ArrayList<ApplicationInfo> list) { |
| 1077 | final int count = list != null ? list.size() : 0; |
Jason Sams | 0a8dc2c | 2009-09-27 17:51:44 -0700 | [diff] [blame] | 1078 | int allocCount = count; |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 1079 | if (allocCount < 1) { |
Jason Sams | 0a8dc2c | 2009-09-27 17:51:44 -0700 | [diff] [blame] | 1080 | allocCount = 1; |
| 1081 | } |
| 1082 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1083 | mIcons = new Allocation[count]; |
Jason Sams | 0a8dc2c | 2009-09-27 17:51:44 -0700 | [diff] [blame] | 1084 | mIconIds = new int[allocCount]; |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 1085 | mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1086 | |
| 1087 | mLabels = new Allocation[count]; |
Jason Sams | 0a8dc2c | 2009-09-27 17:51:44 -0700 | [diff] [blame] | 1088 | mLabelIds = new int[allocCount]; |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 1089 | mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), allocCount); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1090 | |
Jason Sams | 0a8dc2c | 2009-09-27 17:51:44 -0700 | [diff] [blame] | 1091 | Element ie8888 = Element.RGBA_8888(mRS); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1092 | |
| 1093 | Utilities.BubbleText bubble = new Utilities.BubbleText(getContext()); |
| 1094 | |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1095 | mState.iconCount = count; |
Jason Sams | 20df7c7 | 2009-11-05 12:30:24 -0800 | [diff] [blame] | 1096 | uploadApps(list); |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 1097 | saveAppsList(); |
| 1098 | } |
| 1099 | |
Joe Onorato | 3a8820b | 2009-11-10 15:06:42 -0800 | [diff] [blame] | 1100 | private void setZoom(float zoom, boolean animate) { |
| 1101 | mRollo.clearSelectedIcon(); |
| 1102 | mRollo.setHomeSelected(SELECTED_NONE); |
| 1103 | if (zoom > 0.001f) { |
| 1104 | mRollo.mState.zoomTarget = zoom; |
| 1105 | } else { |
| 1106 | mRollo.mState.zoomTarget = 0; |
| 1107 | } |
| 1108 | mRollo.mState.save(); |
| 1109 | if (!animate) { |
| 1110 | mRollo.mInvokeSetZoom.execute(); |
| 1111 | } |
| 1112 | } |
| 1113 | |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 1114 | private void frameBitmapAllocMips(Allocation alloc, int w, int h) { |
| 1115 | int black[] = new int[w > h ? w : h]; |
| 1116 | Allocation.Adapter2D a = alloc.createAdapter2D(); |
| 1117 | int mip = 0; |
| 1118 | while (w > 1 || h > 1) { |
| 1119 | a.subData(0, 0, 1, h, black); |
| 1120 | a.subData(w-1, 0, 1, h, black); |
| 1121 | a.subData(0, 0, w, 1, black); |
| 1122 | a.subData(0, h-1, w, 1, black); |
| 1123 | mip++; |
| 1124 | w = (w + 1) >> 1; |
| 1125 | h = (h + 1) >> 1; |
| 1126 | a.setConstraint(Dimension.LOD, mip); |
| 1127 | } |
| 1128 | a.subData(0, 0, 1, 1, black); |
| 1129 | } |
| 1130 | |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 1131 | private void uploadAppIcon(int index, ApplicationInfo item) { |
| 1132 | mIcons[index] = Allocation.createFromBitmap(mRS, item.iconBitmap, |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 1133 | Element.RGBA_8888(mRS), true); |
| 1134 | frameBitmapAllocMips(mIcons[index], item.iconBitmap.getWidth(), item.iconBitmap.getHeight()); |
| 1135 | |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 1136 | mLabels[index] = Allocation.createFromBitmap(mRS, item.titleBitmap, |
Jason Sams | c851479 | 2009-10-29 14:27:29 -0700 | [diff] [blame] | 1137 | Element.RGBA_8888(mRS), true); |
| 1138 | frameBitmapAllocMips(mLabels[index], item.titleBitmap.getWidth(), item.titleBitmap.getHeight()); |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 1139 | |
| 1140 | mIcons[index].uploadToTexture(0); |
| 1141 | mLabels[index].uploadToTexture(0); |
| 1142 | |
| 1143 | mIconIds[index] = mIcons[index].getID(); |
| 1144 | mLabelIds[index] = mLabels[index].getID(); |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * Puts the empty spaces at the end. Updates mState.iconCount. You must |
| 1149 | * fill in the values and call saveAppsList(). |
| 1150 | */ |
| 1151 | private void reallocAppsList(int count) { |
| 1152 | Allocation[] icons = new Allocation[count]; |
| 1153 | int[] iconIds = new int[count]; |
| 1154 | mAllocIconIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count); |
| 1155 | |
| 1156 | Allocation[] labels = new Allocation[count]; |
| 1157 | int[] labelIds = new int[count]; |
| 1158 | mAllocLabelIds = Allocation.createSized(mRS, Element.USER_I32(mRS), count); |
| 1159 | |
| 1160 | final int oldCount = mIcons.length; |
| 1161 | |
| 1162 | System.arraycopy(mIcons, 0, icons, 0, oldCount); |
| 1163 | System.arraycopy(mIconIds, 0, iconIds, 0, oldCount); |
| 1164 | System.arraycopy(mLabels, 0, labels, 0, oldCount); |
| 1165 | System.arraycopy(mLabelIds, 0, labelIds, 0, oldCount); |
| 1166 | |
| 1167 | mIcons = icons; |
| 1168 | mIconIds = iconIds; |
| 1169 | mLabels = labels; |
| 1170 | mLabelIds = labelIds; |
| 1171 | } |
| 1172 | |
| 1173 | /** |
| 1174 | * Handle the allocations for the new app. Make sure you call saveAppsList when done. |
| 1175 | */ |
| 1176 | private void addApp(int index, ApplicationInfo item) { |
| 1177 | final int count = mState.iconCount - index; |
| 1178 | final int dest = index + 1; |
| 1179 | |
| 1180 | System.arraycopy(mIcons, index, mIcons, dest, count); |
| 1181 | System.arraycopy(mIconIds, index, mIconIds, dest, count); |
| 1182 | System.arraycopy(mLabels, index, mLabels, dest, count); |
| 1183 | System.arraycopy(mLabelIds, index, mLabelIds, dest, count); |
| 1184 | |
Jason Sams | 20df7c7 | 2009-11-05 12:30:24 -0800 | [diff] [blame] | 1185 | if (mHasSurface ) { |
| 1186 | uploadAppIcon(index, item); |
| 1187 | } else { |
| 1188 | mAppsDirty = true; |
| 1189 | } |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | /** |
| 1193 | * Handle the allocations for the removed app. Make sure you call saveAppsList when done. |
| 1194 | */ |
| 1195 | private void removeApp(int index) { |
| 1196 | final int count = mState.iconCount - index - 1; |
| 1197 | final int src = index + 1; |
| 1198 | |
| 1199 | System.arraycopy(mIcons, src, mIcons, index, count); |
| 1200 | System.arraycopy(mIconIds, src, mIconIds, index, count); |
| 1201 | System.arraycopy(mLabels, src, mLabels, index, count); |
| 1202 | System.arraycopy(mLabelIds, src, mLabelIds, index, count); |
| 1203 | |
| 1204 | final int last = mState.iconCount - 1; |
| 1205 | mIcons[last] = null; |
| 1206 | mIconIds[last] = 0; |
| 1207 | mLabels[last] = null; |
| 1208 | mLabelIds[last] = 0; |
| 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * Send the apps list structures to RS. |
| 1213 | */ |
| 1214 | private void saveAppsList() { |
| 1215 | mRS.contextBindRootScript(null); |
Jason Sams | d8152b9 | 2009-10-13 17:19:10 -0700 | [diff] [blame] | 1216 | |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 1217 | mAllocIconIds.data(mIconIds); |
| 1218 | mAllocLabelIds.data(mLabelIds); |
| 1219 | |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1220 | if (mScript != null) { // this happens when we init it |
| 1221 | mScript.bindAllocation(mAllocIconIds, Defines.ALLOC_ICON_IDS); |
| 1222 | mScript.bindAllocation(mAllocLabelIds, Defines.ALLOC_LABEL_IDS); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | mState.save(); |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 1226 | |
| 1227 | // Note: mScript may be null if we haven't initialized it yet. |
| 1228 | // In that case, this is a no-op. |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1229 | if (mInvokeResetWAR != null) { |
| 1230 | mInvokeResetWAR.execute(); |
Jason Sams | 41b61c8 | 2009-10-15 15:40:54 -0700 | [diff] [blame] | 1231 | } |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1232 | mRS.contextBindRootScript(mScript); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1233 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1234 | |
| 1235 | void initTouchState() { |
| 1236 | int width = getWidth(); |
| 1237 | int height = getHeight(); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1238 | int cellHeight = 145;//iconsSize / Defines.ROWS_PER_PAGE; |
| 1239 | int cellWidth = width / Defines.COLUMNS_PER_PAGE; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1240 | |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1241 | int centerY = (height / 2); |
| 1242 | mTouchYBorders[0] = centerY - (cellHeight * 2); |
| 1243 | mTouchYBorders[1] = centerY - cellHeight; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1244 | mTouchYBorders[2] = centerY; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1245 | mTouchYBorders[3] = centerY + cellHeight; |
| 1246 | mTouchYBorders[4] = centerY + (cellHeight * 2); |
Jason Sams | 78aebd8 | 2009-09-15 13:06:59 -0700 | [diff] [blame] | 1247 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1248 | int centerX = (width / 2); |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1249 | mTouchXBorders[0] = 0; |
| 1250 | mTouchXBorders[1] = centerX - (width / 4); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1251 | mTouchXBorders[2] = centerX; |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1252 | mTouchXBorders[3] = centerX + (width / 4); |
| 1253 | mTouchXBorders[4] = width; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
Joe Onorato | 664457d | 2009-10-28 16:30:34 -0400 | [diff] [blame] | 1256 | void fling() { |
| 1257 | mInvokeFling.execute(); |
| 1258 | } |
| 1259 | |
| 1260 | void move() { |
| 1261 | mInvokeMove.execute(); |
| 1262 | } |
| 1263 | |
| 1264 | void moveTo(float row) { |
| 1265 | mState.targetPos = row; |
| 1266 | mState.save(); |
| 1267 | mInvokeMoveTo.execute(); |
| 1268 | } |
| 1269 | |
Jason Sams | 37e7c2b | 2009-10-19 12:55:43 -0700 | [diff] [blame] | 1270 | int chooseTappedIcon(int x, int y, float pos) { |
| 1271 | // Adjust for scroll position if not zero. |
| 1272 | y += (pos - ((int)pos)) * (mTouchYBorders[1] - mTouchYBorders[0]); |
Jason Sams | 86c87ed | 2009-09-18 13:55:55 -0700 | [diff] [blame] | 1273 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1274 | int col = -1; |
| 1275 | int row = -1; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1276 | for (int i=0; i<Defines.COLUMNS_PER_PAGE; i++) { |
| 1277 | if (x >= mTouchXBorders[i] && x < mTouchXBorders[i+1]) { |
| 1278 | col = i; |
| 1279 | break; |
| 1280 | } |
| 1281 | } |
| 1282 | for (int i=0; i<Defines.ROWS_PER_PAGE; i++) { |
| 1283 | if (y >= mTouchYBorders[i] && y < mTouchYBorders[i+1]) { |
| 1284 | row = i; |
| 1285 | break; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | if (row < 0 || col < 0) { |
| 1290 | return -1; |
| 1291 | } |
| 1292 | |
Joe Onorato | 664457d | 2009-10-28 16:30:34 -0400 | [diff] [blame] | 1293 | int index = (((int)pos) * Defines.COLUMNS_PER_PAGE) |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1294 | + (row * Defines.ROWS_PER_PAGE) + col; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1295 | |
Joe Onorato | 664457d | 2009-10-28 16:30:34 -0400 | [diff] [blame] | 1296 | if (index >= mState.iconCount) { |
| 1297 | return -1; |
| 1298 | } else { |
| 1299 | return index; |
| 1300 | } |
Jason Sams | c1c521e | 2009-10-19 14:45:45 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1303 | /** |
| 1304 | * 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] | 1305 | * |
| 1306 | * @return the index of the icon that was selected. |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1307 | */ |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 1308 | int selectIcon(int x, int y, float pos, int pressed) { |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 1309 | final int index = chooseTappedIcon(x, y, pos); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 1310 | selectIcon(index, pressed); |
Joe Onorato | 82ca550 | 2009-10-15 16:59:23 -0700 | [diff] [blame] | 1311 | return index; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 1312 | } |
| 1313 | |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 1314 | /** |
| 1315 | * Select the icon at the given index. |
| 1316 | * |
| 1317 | * @param index The index. |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 1318 | * @param pressed one of SELECTED_PRESSED or SELECTED_FOCUSED |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 1319 | */ |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 1320 | void selectIcon(int index, int pressed) { |
Joe Onorato | 2d80476 | 2009-11-05 16:02:32 -0500 | [diff] [blame] | 1321 | if (mAllAppsList == null || index < 0 || index >= mAllAppsList.size()) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1322 | mState.selectedIconIndex = -1; |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 1323 | if (mLastSelection == SELECTION_ICONS) { |
| 1324 | mLastSelection = SELECTION_NONE; |
| 1325 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1326 | } else { |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 1327 | if (pressed == SELECTED_FOCUSED) { |
| 1328 | mLastSelection = SELECTION_ICONS; |
| 1329 | } |
| 1330 | |
Joe Onorato | 52a653f | 2009-11-11 14:52:11 -0800 | [diff] [blame] | 1331 | int prev = mState.selectedIconIndex; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1332 | mState.selectedIconIndex = index; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 1333 | |
Joe Onorato | 52a653f | 2009-11-11 14:52:11 -0800 | [diff] [blame] | 1334 | ApplicationInfo info = mAllAppsList.get(index); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 1335 | Bitmap selectionBitmap = mSelectionBitmap; |
| 1336 | |
| 1337 | Utilities.drawSelectedAllAppsBitmap(mSelectionCanvas, |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 1338 | selectionBitmap.getWidth(), selectionBitmap.getHeight(), |
Joe Onorato | 52a653f | 2009-11-11 14:52:11 -0800 | [diff] [blame] | 1339 | pressed == SELECTED_PRESSED, info.iconBitmap); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 1340 | |
| 1341 | mSelectedIcon = Allocation.createFromBitmap(mRS, selectionBitmap, |
Jason Sams | 0a8dc2c | 2009-09-27 17:51:44 -0700 | [diff] [blame] | 1342 | Element.RGBA_8888(mRS), false); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 1343 | mSelectedIcon.uploadToTexture(0); |
| 1344 | mState.selectedIconTexture = mSelectedIcon.getID(); |
Joe Onorato | 52a653f | 2009-11-11 14:52:11 -0800 | [diff] [blame] | 1345 | |
| 1346 | if (prev != index) { |
| 1347 | if (info.title != null && info.title.length() > 0) { |
Joe Onorato | 52a653f | 2009-11-11 14:52:11 -0800 | [diff] [blame] | 1348 | //setContentDescription(info.title); |
| 1349 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
| 1350 | } |
| 1351 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | /** |
| 1356 | * You need to call save() on mState on your own after calling this. |
| 1357 | */ |
| 1358 | void clearSelectedIcon() { |
Joe Onorato | 2ca51dc | 2009-09-16 11:44:14 -0400 | [diff] [blame] | 1359 | mState.selectedIconIndex = -1; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 1360 | } |
Joe Onorato | a8138d5 | 2009-10-06 19:25:30 -0700 | [diff] [blame] | 1361 | |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 1362 | void setHomeSelected(int mode) { |
Joe Onorato | 52a653f | 2009-11-11 14:52:11 -0800 | [diff] [blame] | 1363 | final int prev = mLastSelection; |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 1364 | switch (mode) { |
| 1365 | case SELECTED_NONE: |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 1366 | mState.homeButtonId = mHomeButtonNormal.getID(); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 1367 | break; |
| 1368 | case SELECTED_FOCUSED: |
| 1369 | mLastSelection = SELECTION_HOME; |
| 1370 | mState.homeButtonId = mHomeButtonFocused.getID(); |
Joe Onorato | 52a653f | 2009-11-11 14:52:11 -0800 | [diff] [blame] | 1371 | if (prev != SELECTION_HOME) { |
Joe Onorato | 52a653f | 2009-11-11 14:52:11 -0800 | [diff] [blame] | 1372 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
| 1373 | } |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 1374 | break; |
| 1375 | case SELECTED_PRESSED: |
| 1376 | mState.homeButtonId = mHomeButtonPressed.getID(); |
| 1377 | break; |
Joe Onorato | d63458b | 2009-10-15 21:19:09 -0700 | [diff] [blame] | 1378 | } |
| 1379 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 1380 | } |
Joe Onorato | 9383905 | 2009-08-06 20:34:32 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | |