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