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