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