Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1 | /* |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 18 | |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 19 | import android.content.res.Configuration; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 20 | import android.util.Log; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 21 | import android.view.KeyEvent; |
Sunny Goyal | b3726d9 | 2014-08-20 16:58:17 -0700 | [diff] [blame] | 22 | import android.view.SoundEffectConstants; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 23 | import android.view.View; |
| 24 | import android.view.ViewGroup; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 25 | |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 26 | import com.android.launcher3.FocusHelper.PagedViewKeyListener; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 27 | import com.android.launcher3.util.FocusLogic; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 28 | import com.android.launcher3.util.Thunk; |
Winson Chung | faa1325 | 2011-06-13 18:15:54 -0700 | [diff] [blame] | 29 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 30 | /** |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 31 | * A keyboard listener we set on all the workspace icons. |
| 32 | */ |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 33 | class IconKeyEventListener implements View.OnKeyListener { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 34 | @Override |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 35 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 36 | return FocusHelper.handleIconKeyEvent(v, keyCode, event); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 41 | * A keyboard listener we set on all the hotseat buttons. |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 42 | */ |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 43 | class HotseatIconKeyEventListener implements View.OnKeyListener { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 44 | @Override |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 45 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 46 | return FocusHelper.handleHotseatButtonKeyEvent(v, keyCode, event); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 50 | public class FocusHelper { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 51 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 52 | private static final String TAG = "FocusHelper"; |
| 53 | private static final boolean DEBUG = false; |
| 54 | |
| 55 | // |
| 56 | // Key code handling methods. |
| 57 | // |
| 58 | |
| 59 | /** |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 60 | * A keyboard listener for scrollable folders |
| 61 | */ |
| 62 | public static class PagedFolderKeyEventListener extends PagedViewKeyListener { |
| 63 | |
| 64 | private final Folder mFolder; |
| 65 | |
| 66 | public PagedFolderKeyEventListener(Folder folder) { |
| 67 | mFolder = folder; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void handleNoopKey(int keyCode, View v) { |
| 72 | if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { |
| 73 | mFolder.mFolderName.requestFocus(); |
| 74 | playSoundEffect(keyCode, v); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 80 | * Handles key events in the all apps screen. |
| 81 | */ |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 82 | public static class PagedViewKeyListener implements View.OnKeyListener { |
| 83 | |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 84 | @Override |
| 85 | public boolean onKey(View v, int keyCode, KeyEvent e) { |
| 86 | boolean consume = FocusLogic.shouldConsume(keyCode); |
| 87 | if (e.getAction() == KeyEvent.ACTION_UP) { |
| 88 | return consume; |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 89 | } |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 90 | if (DEBUG) { |
| 91 | Log.v(TAG, String.format("Handle ALL APPS and Folders keyevent=[%s].", |
| 92 | KeyEvent.keyCodeToString(keyCode))); |
| 93 | } |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 94 | |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 95 | // Initialize variables. |
| 96 | ViewGroup parentLayout; |
| 97 | ViewGroup itemContainer; |
| 98 | int countX; |
| 99 | int countY; |
| 100 | if (v.getParent() instanceof ShortcutAndWidgetContainer) { |
| 101 | itemContainer = (ViewGroup) v.getParent(); |
| 102 | parentLayout = (ViewGroup) itemContainer.getParent(); |
| 103 | countX = ((CellLayout) parentLayout).getCountX(); |
| 104 | countY = ((CellLayout) parentLayout).getCountY(); |
| 105 | } else { |
| 106 | if (LauncherAppState.isDogfoodBuild()) { |
| 107 | throw new IllegalStateException("Parent of the focused item is not supported."); |
| 108 | } else { |
| 109 | return false; |
| 110 | } |
| 111 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 112 | |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 113 | final int iconIndex = itemContainer.indexOfChild(v); |
| 114 | final PagedView container = (PagedView) parentLayout.getParent(); |
| 115 | final int pageIndex = container.indexToPage(container.indexOfChild(parentLayout)); |
| 116 | final int pageCount = container.getChildCount(); |
| 117 | ViewGroup newParent = null; |
| 118 | View child = null; |
| 119 | // TODO(hyunyoungs): this matrix is not applicable on the last page. |
| 120 | int[][] matrix = FocusLogic.createFullMatrix(countX, countY, true); |
| 121 | |
| 122 | // Process focus. |
| 123 | int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix, |
| 124 | iconIndex, pageIndex, pageCount); |
| 125 | if (newIconIndex == FocusLogic.NOOP) { |
| 126 | handleNoopKey(keyCode, v); |
| 127 | return consume; |
| 128 | } |
| 129 | switch (newIconIndex) { |
| 130 | case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN: |
| 131 | case FocusLogic.NEXT_PAGE_RIGHT_COLUMN: |
| 132 | int newPageIndex = pageIndex - 1; |
| 133 | if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) { |
| 134 | newPageIndex = pageIndex + 1; |
| 135 | } |
| 136 | newParent = getAppsCustomizePage(container, newPageIndex); |
| 137 | if (newParent != null) { |
| 138 | int row = FocusLogic.findRow(matrix, iconIndex); |
| 139 | container.snapToPage(newPageIndex); |
| 140 | // no need to create a new matrix. |
| 141 | child = newParent.getChildAt(matrix[countX-1][row]); |
| 142 | } |
| 143 | break; |
| 144 | case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM: |
| 145 | newParent = getAppsCustomizePage(container, pageIndex - 1); |
| 146 | if (newParent != null) { |
| 147 | container.snapToPage(pageIndex - 1); |
| 148 | child = newParent.getChildAt(0); |
| 149 | } |
| 150 | break; |
| 151 | case FocusLogic.PREVIOUS_PAGE_LAST_ITEM: |
| 152 | newParent = getAppsCustomizePage(container, pageIndex - 1); |
| 153 | if (newParent != null) { |
| 154 | container.snapToPage(pageIndex - 1); |
| 155 | child = newParent.getChildAt(newParent.getChildCount() - 1); |
| 156 | } |
| 157 | break; |
| 158 | case FocusLogic.NEXT_PAGE_FIRST_ITEM: |
| 159 | newParent = getAppsCustomizePage(container, pageIndex + 1); |
| 160 | if (newParent != null) { |
| 161 | container.snapToPage(pageIndex + 1); |
| 162 | child = newParent.getChildAt(0); |
| 163 | } |
| 164 | break; |
| 165 | case FocusLogic.NEXT_PAGE_LEFT_COLUMN: |
| 166 | case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN: |
| 167 | newPageIndex = pageIndex + 1; |
| 168 | if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) { |
| 169 | newPageIndex = pageIndex -1; |
| 170 | } |
| 171 | newParent = getAppsCustomizePage(container, newPageIndex); |
| 172 | if (newParent != null) { |
| 173 | container.snapToPage(newPageIndex); |
| 174 | int row = FocusLogic.findRow(matrix, iconIndex); |
| 175 | child = newParent.getChildAt(matrix[0][row]); |
| 176 | } |
| 177 | break; |
| 178 | case FocusLogic.CURRENT_PAGE_FIRST_ITEM: |
| 179 | child = container.getChildAt(0); |
| 180 | break; |
| 181 | case FocusLogic.CURRENT_PAGE_LAST_ITEM: |
| 182 | child = itemContainer.getChildAt(itemContainer.getChildCount() - 1); |
| 183 | break; |
| 184 | default: // Go to some item on the current page. |
| 185 | child = itemContainer.getChildAt(newIconIndex); |
| 186 | break; |
| 187 | } |
| 188 | if (child != null) { |
| 189 | child.requestFocus(); |
| 190 | playSoundEffect(keyCode, v); |
| 191 | } else { |
| 192 | handleNoopKey(keyCode, v); |
| 193 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 194 | return consume; |
| 195 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 196 | |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 197 | public void handleNoopKey(int keyCode, View v) { } |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 200 | /** |
| 201 | * Handles key events in the workspace hot seat (bottom of the screen). |
| 202 | * <p>Currently we don't special case for the phone UI in different orientations, even though |
| 203 | * the hotseat is on the side in landscape mode. This is to ensure that accessibility |
| 204 | * consistency is maintained across rotations. |
| 205 | */ |
| 206 | static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) { |
| 207 | boolean consume = FocusLogic.shouldConsume(keyCode); |
| 208 | if (e.getAction() == KeyEvent.ACTION_UP || !consume) { |
| 209 | return consume; |
| 210 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 211 | |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 212 | DeviceProfile profile = LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 213 | if (DEBUG) { |
| 214 | Log.v(TAG, String.format( |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 215 | "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s", |
| 216 | KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout())); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Initialize the variables. |
| 220 | final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent(); |
| 221 | final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 222 | Hotseat hotseat = (Hotseat) hotseatLayout.getParent(); |
| 223 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 224 | Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace); |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame^] | 225 | int pageIndex = workspace.getNextPage(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 226 | int pageCount = workspace.getChildCount(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 227 | int countX = -1; |
| 228 | int countY = -1; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 229 | int iconIndex = findIndexOfView(hotseatParent, v); |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 230 | int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets() |
| 231 | .getChildAt(iconIndex).getLayoutParams()).cellX; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 232 | |
| 233 | final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex); |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame^] | 234 | if (iconLayout == null) { |
| 235 | // This check is to guard against cases where key strokes rushes in when workspace |
| 236 | // child creation/deletion is still in flux. (e.g., during drop or fling |
| 237 | // animation.) |
| 238 | return consume; |
| 239 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 240 | final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets(); |
| 241 | |
| 242 | ViewGroup parent = null; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 243 | int[][] matrix = null; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 244 | |
| 245 | if (keyCode == KeyEvent.KEYCODE_DPAD_UP && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 246 | !profile.isVerticalBarLayout()) { |
| 247 | matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, |
| 248 | true /* hotseat horizontal */, hotseat.getAllAppsButtonRank(), |
| 249 | iconRank == hotseat.getAllAppsButtonRank() /* include all apps icon */); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 250 | iconIndex += iconParent.getChildCount(); |
| 251 | countX = iconLayout.getCountX(); |
| 252 | countY = iconLayout.getCountY() + hotseatLayout.getCountY(); |
| 253 | parent = iconParent; |
| 254 | } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 255 | profile.isVerticalBarLayout()) { |
| 256 | matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, |
| 257 | false /* hotseat horizontal */, hotseat.getAllAppsButtonRank(), |
| 258 | iconRank == hotseat.getAllAppsButtonRank() /* include all apps icon */); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 259 | iconIndex += iconParent.getChildCount(); |
| 260 | countX = iconLayout.getCountX() + hotseatLayout.getCountX(); |
| 261 | countY = iconLayout.getCountY(); |
| 262 | parent = iconParent; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 263 | } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 264 | profile.isVerticalBarLayout()) { |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 265 | keyCode = KeyEvent.KEYCODE_PAGE_DOWN; |
| 266 | }else { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 267 | // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the |
| 268 | // matrix extended with hotseat. |
| 269 | matrix = FocusLogic.createSparseMatrix(hotseatLayout); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 270 | countX = hotseatLayout.getCountX(); |
| 271 | countY = hotseatLayout.getCountY(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 272 | parent = hotseatParent; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // Process the focus. |
| 276 | int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix, |
| 277 | iconIndex, pageIndex, pageCount); |
| 278 | |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 279 | View newIcon = null; |
| 280 | if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) { |
| 281 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1); |
| 282 | newIcon = parent.getChildAt(0); |
| 283 | // TODO(hyunyoungs): handle cases where the child is not an icon but |
| 284 | // a folder or a widget. |
| 285 | workspace.snapToPage(pageIndex + 1); |
| 286 | } |
| 287 | if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 288 | newIconIndex -= iconParent.getChildCount(); |
| 289 | } |
| 290 | if (parent != null) { |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 291 | if (newIcon == null && newIconIndex >=0) { |
| 292 | newIcon = parent.getChildAt(newIconIndex); |
| 293 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 294 | if (newIcon != null) { |
| 295 | newIcon.requestFocus(); |
| 296 | playSoundEffect(keyCode, v); |
| 297 | } |
| 298 | } |
| 299 | return consume; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Handles key events in a workspace containing icons. |
| 304 | */ |
| 305 | static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) { |
| 306 | boolean consume = FocusLogic.shouldConsume(keyCode); |
| 307 | if (e.getAction() == KeyEvent.ACTION_UP || !consume) { |
| 308 | return consume; |
| 309 | } |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 310 | |
| 311 | LauncherAppState app = LauncherAppState.getInstance(); |
| 312 | DeviceProfile profile = app.getDynamicGrid().getDeviceProfile(); |
| 313 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 314 | if (DEBUG) { |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 315 | Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s", |
| 316 | KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout())); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // Initialize the variables. |
| 320 | ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent(); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 321 | CellLayout iconLayout = (CellLayout) parent.getParent(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 322 | final Workspace workspace = (Workspace) iconLayout.getParent(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 323 | final ViewGroup launcher = (ViewGroup) workspace.getParent(); |
| 324 | final ViewGroup tabs = (ViewGroup) launcher.findViewById(R.id.search_drop_target_bar); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 325 | final Hotseat hotseat = (Hotseat) launcher.findViewById(R.id.hotseat); |
| 326 | int pageIndex = workspace.indexOfChild(iconLayout); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 327 | int pageCount = workspace.getChildCount(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 328 | int countX = iconLayout.getCountX(); |
| 329 | int countY = iconLayout.getCountY(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 330 | final int iconIndex = findIndexOfView(parent, v); |
| 331 | |
| 332 | CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0); |
| 333 | ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets(); |
| 334 | int[][] matrix; |
| 335 | |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 336 | // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 337 | // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended |
| 338 | // with the hotseat. |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 339 | if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) { |
| 340 | matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, true /* horizontal */, |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 341 | hotseat.getAllAppsButtonRank(), false /* all apps icon is ignored */); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 342 | countY = countY + 1; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 343 | } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 344 | profile.isVerticalBarLayout()) { |
| 345 | matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, false /* horizontal */, |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 346 | hotseat.getAllAppsButtonRank(), false /* all apps icon is ignored */); |
| 347 | countX = countX + 1; |
| 348 | } else if (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) { |
| 349 | workspace.removeWorkspaceItem(v); |
| 350 | return consume; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 351 | } else { |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 352 | matrix = FocusLogic.createSparseMatrix(iconLayout); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Process the focus. |
| 356 | int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix, |
| 357 | iconIndex, pageIndex, pageCount); |
| 358 | View newIcon = null; |
| 359 | switch (newIconIndex) { |
| 360 | case FocusLogic.NOOP: |
| 361 | if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { |
| 362 | newIcon = tabs; |
| 363 | } |
| 364 | break; |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 365 | case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN: |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 366 | case FocusLogic.NEXT_PAGE_RIGHT_COLUMN: |
| 367 | int newPageIndex = pageIndex - 1; |
| 368 | if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) { |
| 369 | newPageIndex = pageIndex + 1; |
| 370 | } |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 371 | int row = FocusLogic.findRow(matrix, iconIndex); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 372 | parent = getCellLayoutChildrenForIndex(workspace, newPageIndex); |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame^] | 373 | workspace.snapToPage(newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 374 | if (parent != null) { |
| 375 | iconLayout = (CellLayout) parent.getParent(); |
Hyunyoung Song | ac721f8 | 2015-03-04 16:33:56 -0800 | [diff] [blame] | 376 | matrix = FocusLogic.createSparseMatrix(iconLayout, |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 377 | iconLayout.getCountX(), row); |
| 378 | newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY, matrix, |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 379 | FocusLogic.PIVOT, newPageIndex, pageCount); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 380 | newIcon = parent.getChildAt(newIconIndex); |
| 381 | } |
| 382 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 383 | case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM: |
| 384 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1); |
| 385 | newIcon = parent.getChildAt(0); |
| 386 | workspace.snapToPage(pageIndex - 1); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 387 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 388 | case FocusLogic.PREVIOUS_PAGE_LAST_ITEM: |
| 389 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1); |
| 390 | newIcon = parent.getChildAt(parent.getChildCount() - 1); |
| 391 | workspace.snapToPage(pageIndex - 1); |
| 392 | break; |
| 393 | case FocusLogic.NEXT_PAGE_FIRST_ITEM: |
| 394 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1); |
| 395 | newIcon = parent.getChildAt(0); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 396 | workspace.snapToPage(pageIndex + 1); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 397 | break; |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 398 | case FocusLogic.NEXT_PAGE_LEFT_COLUMN: |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 399 | case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN: |
| 400 | newPageIndex = pageIndex + 1; |
| 401 | if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) { |
| 402 | newPageIndex = pageIndex - 1; |
| 403 | } |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame^] | 404 | workspace.snapToPage(newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 405 | row = FocusLogic.findRow(matrix, iconIndex); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 406 | parent = getCellLayoutChildrenForIndex(workspace, newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 407 | if (parent != null) { |
| 408 | iconLayout = (CellLayout) parent.getParent(); |
Hyunyoung Song | ac721f8 | 2015-03-04 16:33:56 -0800 | [diff] [blame] | 409 | matrix = FocusLogic.createSparseMatrix(iconLayout, -1, row); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 410 | newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY, matrix, |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 411 | FocusLogic.PIVOT, newPageIndex, pageCount); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 412 | newIcon = parent.getChildAt(newIconIndex); |
| 413 | } |
| 414 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 415 | case FocusLogic.CURRENT_PAGE_FIRST_ITEM: |
| 416 | newIcon = parent.getChildAt(0); |
| 417 | break; |
| 418 | case FocusLogic.CURRENT_PAGE_LAST_ITEM: |
| 419 | newIcon = parent.getChildAt(parent.getChildCount() - 1); |
| 420 | break; |
| 421 | default: |
| 422 | // current page, some item. |
| 423 | if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) { |
| 424 | newIcon = parent.getChildAt(newIconIndex); |
| 425 | } else if (parent.getChildCount() <= newIconIndex && |
| 426 | newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) { |
| 427 | newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount()); |
| 428 | } |
| 429 | break; |
| 430 | } |
| 431 | if (newIcon != null) { |
| 432 | newIcon.requestFocus(); |
| 433 | playSoundEffect(keyCode, v); |
| 434 | } |
| 435 | return consume; |
| 436 | } |
| 437 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 438 | // |
| 439 | // Helper methods. |
| 440 | // |
| 441 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 442 | /** |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 443 | * Returns the Viewgroup containing page contents for the page at the index specified. |
| 444 | */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 445 | @Thunk static ViewGroup getAppsCustomizePage(ViewGroup container, int index) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 446 | ViewGroup page = (ViewGroup) ((PagedView) container).getPageAt(index); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 447 | if (page instanceof CellLayout) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 448 | // There are two layers, a PagedViewCellLayout and PagedViewCellLayoutChildren |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 449 | page = ((CellLayout) page).getShortcutsAndWidgets(); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 450 | } |
| 451 | return page; |
| 452 | } |
| 453 | |
| 454 | /** |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 455 | * Private helper method to get the CellLayoutChildren given a CellLayout index. |
| 456 | */ |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 457 | private static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex( |
| 458 | ViewGroup container, int i) { |
Sunny Goyal | b3726d9 | 2014-08-20 16:58:17 -0700 | [diff] [blame] | 459 | CellLayout parent = (CellLayout) container.getChildAt(i); |
| 460 | return parent.getShortcutsAndWidgets(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 463 | private static int findIndexOfView(ViewGroup parent, View v) { |
| 464 | for (int i = 0; i < parent.getChildCount(); i++) { |
| 465 | if (v != null && v.equals(parent.getChildAt(i))) { |
| 466 | return i; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 469 | return -1; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /** |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 473 | * Helper method to be used for playing sound effects. |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 474 | */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 475 | @Thunk static void playSoundEffect(int keyCode, View v) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 476 | switch (keyCode) { |
| 477 | case KeyEvent.KEYCODE_DPAD_LEFT: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 478 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 479 | break; |
| 480 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 481 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 482 | break; |
| 483 | case KeyEvent.KEYCODE_DPAD_DOWN: |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 484 | case KeyEvent.KEYCODE_PAGE_DOWN: |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 485 | case KeyEvent.KEYCODE_MOVE_END: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 486 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 487 | break; |
| 488 | case KeyEvent.KEYCODE_DPAD_UP: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 489 | case KeyEvent.KEYCODE_PAGE_UP: |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 490 | case KeyEvent.KEYCODE_MOVE_HOME: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 491 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 492 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 493 | default: |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 494 | break; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 495 | } |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 496 | } |
| 497 | } |