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