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 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 19 | import android.util.Log; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 20 | import android.view.KeyEvent; |
Sunny Goyal | b3726d9 | 2014-08-20 16:58:17 -0700 | [diff] [blame] | 21 | import android.view.SoundEffectConstants; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 24 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 25 | import com.android.launcher3.util.FocusLogic; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 26 | import com.android.launcher3.util.Thunk; |
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 | /** |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 39 | * A keyboard listener we set on all the hotseat buttons. |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 40 | */ |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 41 | class HotseatIconKeyEventListener implements View.OnKeyListener { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 42 | @Override |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 43 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 44 | return FocusHelper.handleHotseatButtonKeyEvent(v, keyCode, event); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Tony Wickham | 0fa5ada | 2015-11-13 17:32:20 -0800 | [diff] [blame] | 48 | /** |
| 49 | * A keyboard listener we set on full screen pages (e.g. custom content). |
| 50 | */ |
| 51 | class FullscreenKeyEventListener implements View.OnKeyListener { |
| 52 | @Override |
| 53 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 54 | if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT |
| 55 | || keyCode == KeyEvent.KEYCODE_PAGE_DOWN || keyCode == KeyEvent.KEYCODE_PAGE_UP) { |
| 56 | // Handle the key event just like a workspace icon would in these cases. In this case, |
| 57 | // it will basically act as if there is a single icon in the top left (so you could |
| 58 | // think of the fullscreen page as a focusable fullscreen widget). |
| 59 | return FocusHelper.handleIconKeyEvent(v, keyCode, event); |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 65 | public class FocusHelper { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 66 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 67 | private static final String TAG = "FocusHelper"; |
| 68 | private static final boolean DEBUG = false; |
| 69 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 70 | /** |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 71 | * Handles key events in paged folder. |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 72 | */ |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 73 | public static class PagedFolderKeyEventListener implements View.OnKeyListener { |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 74 | |
| 75 | private final Folder mFolder; |
| 76 | |
| 77 | public PagedFolderKeyEventListener(Folder folder) { |
| 78 | mFolder = folder; |
| 79 | } |
| 80 | |
| 81 | @Override |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 82 | public boolean onKey(View v, int keyCode, KeyEvent e) { |
| 83 | boolean consume = FocusLogic.shouldConsume(keyCode); |
| 84 | if (e.getAction() == KeyEvent.ACTION_UP) { |
| 85 | return consume; |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 86 | } |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 87 | if (DEBUG) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 88 | Log.v(TAG, String.format("Handle ALL Folders keyevent=[%s].", |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 89 | KeyEvent.keyCodeToString(keyCode))); |
| 90 | } |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 91 | |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 92 | if (!(v.getParent() instanceof ShortcutAndWidgetContainer)) { |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 93 | if (LauncherAppState.isDogfoodBuild()) { |
| 94 | throw new IllegalStateException("Parent of the focused item is not supported."); |
| 95 | } else { |
| 96 | return false; |
| 97 | } |
| 98 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 99 | |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 100 | // Initialize variables. |
| 101 | final ShortcutAndWidgetContainer itemContainer = (ShortcutAndWidgetContainer) v.getParent(); |
| 102 | final CellLayout cellLayout = (CellLayout) itemContainer.getParent(); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 103 | |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 104 | final int iconIndex = itemContainer.indexOfChild(v); |
| 105 | final FolderPagedView pagedView = (FolderPagedView) cellLayout.getParent(); |
| 106 | |
| 107 | final int pageIndex = pagedView.indexOfChild(cellLayout); |
| 108 | final int pageCount = pagedView.getPageCount(); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 109 | final boolean isLayoutRtl = Utilities.isRtl(v.getResources()); |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 110 | |
| 111 | int[][] matrix = FocusLogic.createSparseMatrix(cellLayout); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 112 | // Process focus. |
Tony Wickham | 329d8bf | 2015-12-04 09:48:17 -0800 | [diff] [blame^] | 113 | int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex, |
| 114 | pageCount, isLayoutRtl); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 115 | if (newIconIndex == FocusLogic.NOOP) { |
| 116 | handleNoopKey(keyCode, v); |
| 117 | return consume; |
| 118 | } |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 119 | ShortcutAndWidgetContainer newParent = null; |
| 120 | View child = null; |
| 121 | |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 122 | switch (newIconIndex) { |
| 123 | case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 124 | case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN: |
| 125 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 126 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 127 | int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY; |
| 128 | pagedView.snapToPage(pageIndex - 1); |
| 129 | child = newParent.getChildAt( |
| 130 | ((newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) |
Tony Wickham | 329d8bf | 2015-12-04 09:48:17 -0800 | [diff] [blame^] | 131 | ^ newParent.invertLayoutHorizontally()) ? 0 : matrix.length - 1, |
| 132 | row); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 133 | } |
| 134 | break; |
| 135 | case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 136 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 137 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 138 | pagedView.snapToPage(pageIndex - 1); |
| 139 | child = newParent.getChildAt(0, 0); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 140 | } |
| 141 | break; |
| 142 | case FocusLogic.PREVIOUS_PAGE_LAST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 143 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 144 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 145 | pagedView.snapToPage(pageIndex - 1); |
Tony Wickham | 329d8bf | 2015-12-04 09:48:17 -0800 | [diff] [blame^] | 146 | child = newParent.getChildAt(matrix.length - 1, matrix[0].length - 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 147 | } |
| 148 | break; |
| 149 | case FocusLogic.NEXT_PAGE_FIRST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 150 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 151 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 152 | pagedView.snapToPage(pageIndex + 1); |
| 153 | child = newParent.getChildAt(0, 0); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 154 | } |
| 155 | break; |
| 156 | case FocusLogic.NEXT_PAGE_LEFT_COLUMN: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 157 | case FocusLogic.NEXT_PAGE_RIGHT_COLUMN: |
| 158 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 159 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 160 | pagedView.snapToPage(pageIndex + 1); |
Tony Wickham | 2518985 | 2015-11-04 17:44:32 -0800 | [diff] [blame] | 161 | child = FocusLogic.getAdjacentChildInNextFolderPage( |
| 162 | newParent, v, newIconIndex); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 163 | } |
| 164 | break; |
| 165 | case FocusLogic.CURRENT_PAGE_FIRST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 166 | child = cellLayout.getChildAt(0, 0); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 167 | break; |
| 168 | case FocusLogic.CURRENT_PAGE_LAST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 169 | child = pagedView.getLastItem(); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 170 | break; |
| 171 | default: // Go to some item on the current page. |
| 172 | child = itemContainer.getChildAt(newIconIndex); |
| 173 | break; |
| 174 | } |
| 175 | if (child != null) { |
| 176 | child.requestFocus(); |
| 177 | playSoundEffect(keyCode, v); |
| 178 | } else { |
| 179 | handleNoopKey(keyCode, v); |
| 180 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 181 | return consume; |
| 182 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 183 | |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 184 | public void handleNoopKey(int keyCode, View v) { |
| 185 | if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { |
| 186 | mFolder.mFolderName.requestFocus(); |
| 187 | playSoundEffect(keyCode, v); |
| 188 | } |
| 189 | } |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 192 | /** |
Tony Wickham | 4fc8287 | 2015-11-10 16:52:14 -0800 | [diff] [blame] | 193 | * Handles key events in the workspace hotseat (bottom of the screen). |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 194 | * <p>Currently we don't special case for the phone UI in different orientations, even though |
| 195 | * the hotseat is on the side in landscape mode. This is to ensure that accessibility |
| 196 | * consistency is maintained across rotations. |
| 197 | */ |
| 198 | static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) { |
| 199 | boolean consume = FocusLogic.shouldConsume(keyCode); |
| 200 | if (e.getAction() == KeyEvent.ACTION_UP || !consume) { |
| 201 | return consume; |
| 202 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 203 | |
Winson | c0b52fe | 2015-09-09 16:38:15 -0700 | [diff] [blame] | 204 | final Launcher launcher = (Launcher) v.getContext(); |
| 205 | final DeviceProfile profile = launcher.getDeviceProfile(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 206 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 207 | if (DEBUG) { |
| 208 | Log.v(TAG, String.format( |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 209 | "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s", |
| 210 | KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout())); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Initialize the variables. |
Winson | fa56b3f | 2015-09-14 12:01:13 -0700 | [diff] [blame] | 214 | final Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 215 | final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent(); |
| 216 | final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 217 | |
Winson | fa56b3f | 2015-09-14 12:01:13 -0700 | [diff] [blame] | 218 | final ItemInfo itemInfo = (ItemInfo) v.getTag(); |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 219 | int pageIndex = workspace.getNextPage(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 220 | int pageCount = workspace.getChildCount(); |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 221 | int iconIndex = hotseatParent.indexOfChild(v); |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 222 | int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets() |
| 223 | .getChildAt(iconIndex).getLayoutParams()).cellX; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 224 | |
| 225 | final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex); |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 226 | if (iconLayout == null) { |
| 227 | // This check is to guard against cases where key strokes rushes in when workspace |
| 228 | // child creation/deletion is still in flux. (e.g., during drop or fling |
| 229 | // animation.) |
| 230 | return consume; |
| 231 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 232 | final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets(); |
| 233 | |
| 234 | ViewGroup parent = null; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 235 | int[][] matrix = null; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 236 | |
| 237 | if (keyCode == KeyEvent.KEYCODE_DPAD_UP && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 238 | !profile.isVerticalBarLayout()) { |
Tony Wickham | 329d8bf | 2015-12-04 09:48:17 -0800 | [diff] [blame^] | 239 | matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, |
Tony Wickham | 6cbd222 | 2015-11-09 17:51:08 -0800 | [diff] [blame] | 240 | true /* hotseat horizontal */, profile.inv.hotseatAllAppsRank); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 241 | iconIndex += iconParent.getChildCount(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 242 | parent = iconParent; |
| 243 | } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 244 | profile.isVerticalBarLayout()) { |
Tony Wickham | 329d8bf | 2015-12-04 09:48:17 -0800 | [diff] [blame^] | 245 | matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, |
Tony Wickham | 6cbd222 | 2015-11-09 17:51:08 -0800 | [diff] [blame] | 246 | false /* hotseat horizontal */, profile.inv.hotseatAllAppsRank); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 247 | iconIndex += iconParent.getChildCount(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 248 | parent = iconParent; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 249 | } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 250 | profile.isVerticalBarLayout()) { |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 251 | keyCode = KeyEvent.KEYCODE_PAGE_DOWN; |
Winson | fa56b3f | 2015-09-14 12:01:13 -0700 | [diff] [blame] | 252 | } else if (isUninstallKeyChord(e)) { |
| 253 | matrix = FocusLogic.createSparseMatrix(iconLayout); |
| 254 | if (UninstallDropTarget.supportsDrop(launcher, itemInfo)) { |
| 255 | UninstallDropTarget.startUninstallActivity(launcher, itemInfo); |
| 256 | } |
| 257 | } else if (isDeleteKeyChord(e)) { |
| 258 | matrix = FocusLogic.createSparseMatrix(iconLayout); |
Winson | 2949fb5 | 2015-09-24 09:56:11 -0700 | [diff] [blame] | 259 | launcher.removeItem(v, itemInfo, true /* deleteFromDb */); |
Winson | c0b52fe | 2015-09-09 16:38:15 -0700 | [diff] [blame] | 260 | } else { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 261 | // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the |
| 262 | // matrix extended with hotseat. |
| 263 | matrix = FocusLogic.createSparseMatrix(hotseatLayout); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 264 | parent = hotseatParent; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // Process the focus. |
Tony Wickham | 329d8bf | 2015-12-04 09:48:17 -0800 | [diff] [blame^] | 268 | int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex, |
| 269 | pageCount, Utilities.isRtl(v.getResources())); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 270 | |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 271 | View newIcon = null; |
Tony Wickham | 4fc8287 | 2015-11-10 16:52:14 -0800 | [diff] [blame] | 272 | switch (newIconIndex) { |
| 273 | case FocusLogic.NEXT_PAGE_FIRST_ITEM: |
| 274 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1); |
| 275 | newIcon = parent.getChildAt(0); |
| 276 | // TODO(hyunyoungs): handle cases where the child is not an icon but |
| 277 | // a folder or a widget. |
| 278 | workspace.snapToPage(pageIndex + 1); |
| 279 | break; |
| 280 | case FocusLogic.PREVIOUS_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 | break; |
| 287 | case FocusLogic.PREVIOUS_PAGE_LAST_ITEM: |
| 288 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1); |
| 289 | newIcon = parent.getChildAt(parent.getChildCount() - 1); |
| 290 | // TODO(hyunyoungs): handle cases where the child is not an icon but |
| 291 | // a folder or a widget. |
| 292 | workspace.snapToPage(pageIndex - 1); |
| 293 | break; |
| 294 | case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN: |
| 295 | case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN: |
| 296 | // Go to the previous page but keep the focus on the same hotseat icon. |
| 297 | workspace.snapToPage(pageIndex - 1); |
Tony Wickham | 0fa5ada | 2015-11-13 17:32:20 -0800 | [diff] [blame] | 298 | // If the page we are going to is fullscreen, have it take the focus from hotseat. |
| 299 | CellLayout prevPage = (CellLayout) workspace.getPageAt(pageIndex - 1); |
| 300 | boolean isPrevPageFullscreen = ((CellLayout.LayoutParams) prevPage |
| 301 | .getShortcutsAndWidgets().getChildAt(0).getLayoutParams()).isFullscreen; |
| 302 | if (isPrevPageFullscreen) { |
| 303 | workspace.getPageAt(pageIndex - 1).requestFocus(); |
| 304 | } |
Tony Wickham | 4fc8287 | 2015-11-10 16:52:14 -0800 | [diff] [blame] | 305 | break; |
| 306 | case FocusLogic.NEXT_PAGE_LEFT_COLUMN: |
| 307 | case FocusLogic.NEXT_PAGE_RIGHT_COLUMN: |
| 308 | // Go to the next page but keep the focus on the same hotseat icon. |
| 309 | workspace.snapToPage(pageIndex + 1); |
Tony Wickham | 0fa5ada | 2015-11-13 17:32:20 -0800 | [diff] [blame] | 310 | // If the page we are going to is fullscreen, have it take the focus from hotseat. |
| 311 | CellLayout nextPage = (CellLayout) workspace.getPageAt(pageIndex + 1); |
| 312 | boolean isNextPageFullscreen = ((CellLayout.LayoutParams) nextPage |
| 313 | .getShortcutsAndWidgets().getChildAt(0).getLayoutParams()).isFullscreen; |
| 314 | if (isNextPageFullscreen) { |
| 315 | workspace.getPageAt(pageIndex + 1).requestFocus(); |
| 316 | } |
Tony Wickham | 4fc8287 | 2015-11-10 16:52:14 -0800 | [diff] [blame] | 317 | break; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 318 | } |
| 319 | if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 320 | newIconIndex -= iconParent.getChildCount(); |
| 321 | } |
| 322 | if (parent != null) { |
Tony Wickham | 0fa5ada | 2015-11-13 17:32:20 -0800 | [diff] [blame] | 323 | if (newIcon == null && newIconIndex >= 0) { |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 324 | newIcon = parent.getChildAt(newIconIndex); |
| 325 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 326 | if (newIcon != null) { |
| 327 | newIcon.requestFocus(); |
| 328 | playSoundEffect(keyCode, v); |
| 329 | } |
| 330 | } |
| 331 | return consume; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Handles key events in a workspace containing icons. |
| 336 | */ |
| 337 | static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) { |
| 338 | boolean consume = FocusLogic.shouldConsume(keyCode); |
| 339 | if (e.getAction() == KeyEvent.ACTION_UP || !consume) { |
| 340 | return consume; |
| 341 | } |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 342 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 343 | Launcher launcher = (Launcher) v.getContext(); |
| 344 | DeviceProfile profile = launcher.getDeviceProfile(); |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 345 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 346 | if (DEBUG) { |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 347 | Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s", |
| 348 | KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout())); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // Initialize the variables. |
| 352 | ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent(); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 353 | CellLayout iconLayout = (CellLayout) parent.getParent(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 354 | final Workspace workspace = (Workspace) iconLayout.getParent(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 355 | final ViewGroup dragLayer = (ViewGroup) workspace.getParent(); |
| 356 | final ViewGroup tabs = (ViewGroup) dragLayer.findViewById(R.id.search_drop_target_bar); |
| 357 | final Hotseat hotseat = (Hotseat) dragLayer.findViewById(R.id.hotseat); |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 358 | |
Winson | fa56b3f | 2015-09-14 12:01:13 -0700 | [diff] [blame] | 359 | final ItemInfo itemInfo = (ItemInfo) v.getTag(); |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 360 | final int iconIndex = parent.indexOfChild(v); |
| 361 | final int pageIndex = workspace.indexOfChild(iconLayout); |
| 362 | final int pageCount = workspace.getChildCount(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 363 | |
| 364 | CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0); |
| 365 | ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets(); |
| 366 | int[][] matrix; |
| 367 | |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 368 | // 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] | 369 | // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended |
| 370 | // with the hotseat. |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 371 | if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) { |
Tony Wickham | 329d8bf | 2015-12-04 09:48:17 -0800 | [diff] [blame^] | 372 | matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, |
| 373 | true /* horizontal */, profile.inv.hotseatAllAppsRank); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 374 | } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 375 | profile.isVerticalBarLayout()) { |
Tony Wickham | 329d8bf | 2015-12-04 09:48:17 -0800 | [diff] [blame^] | 376 | matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, |
| 377 | false /* horizontal */, profile.inv.hotseatAllAppsRank); |
Winson | fa56b3f | 2015-09-14 12:01:13 -0700 | [diff] [blame] | 378 | } else if (isUninstallKeyChord(e)) { |
| 379 | matrix = FocusLogic.createSparseMatrix(iconLayout); |
| 380 | if (UninstallDropTarget.supportsDrop(launcher, itemInfo)) { |
| 381 | UninstallDropTarget.startUninstallActivity(launcher, itemInfo); |
| 382 | } |
| 383 | } else if (isDeleteKeyChord(e)) { |
| 384 | matrix = FocusLogic.createSparseMatrix(iconLayout); |
Winson | 2949fb5 | 2015-09-24 09:56:11 -0700 | [diff] [blame] | 385 | launcher.removeItem(v, itemInfo, true /* deleteFromDb */); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 386 | } else { |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 387 | matrix = FocusLogic.createSparseMatrix(iconLayout); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // Process the focus. |
Tony Wickham | 329d8bf | 2015-12-04 09:48:17 -0800 | [diff] [blame^] | 391 | int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex, |
| 392 | pageCount, Utilities.isRtl(v.getResources())); |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 393 | boolean isRtl = Utilities.isRtl(v.getResources()); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 394 | View newIcon = null; |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 395 | CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 396 | switch (newIconIndex) { |
| 397 | case FocusLogic.NOOP: |
| 398 | if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { |
| 399 | newIcon = tabs; |
| 400 | } |
| 401 | break; |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 402 | case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN: |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 403 | case FocusLogic.NEXT_PAGE_RIGHT_COLUMN: |
| 404 | int newPageIndex = pageIndex - 1; |
| 405 | if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) { |
| 406 | newPageIndex = pageIndex + 1; |
| 407 | } |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 408 | int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY; |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 409 | parent = getCellLayoutChildrenForIndex(workspace, newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 410 | if (parent != null) { |
| 411 | iconLayout = (CellLayout) parent.getParent(); |
Tony Wickham | 329d8bf | 2015-12-04 09:48:17 -0800 | [diff] [blame^] | 412 | matrix = FocusLogic.createSparseMatrixWithPivotColumn(iconLayout, |
Tony Wickham | 4fc8287 | 2015-11-10 16:52:14 -0800 | [diff] [blame] | 413 | iconLayout.getCountX(), row); |
Tony Wickham | 329d8bf | 2015-12-04 09:48:17 -0800 | [diff] [blame^] | 414 | newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, FocusLogic.PIVOT, |
| 415 | newPageIndex, pageCount, Utilities.isRtl(v.getResources())); |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 416 | if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) { |
| 417 | newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex, |
| 418 | isRtl); |
| 419 | } else if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LAST_ITEM) { |
| 420 | newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex, |
| 421 | isRtl); |
| 422 | } else { |
| 423 | newIcon = parent.getChildAt(newIconIndex); |
| 424 | } |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 425 | } |
| 426 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 427 | case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM: |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 428 | workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex - 1); |
| 429 | newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl); |
| 430 | if (newIcon == null) { |
Tony Wickham | 0fa5ada | 2015-11-13 17:32:20 -0800 | [diff] [blame] | 431 | // Check the hotseat if no focusable item was found on the workspace. |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 432 | newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl); |
| 433 | workspace.snapToPage(pageIndex - 1); |
| 434 | } |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 435 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 436 | case FocusLogic.PREVIOUS_PAGE_LAST_ITEM: |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 437 | newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex, isRtl); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 438 | break; |
| 439 | case FocusLogic.NEXT_PAGE_FIRST_ITEM: |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 440 | newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex, isRtl); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 441 | break; |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 442 | case FocusLogic.NEXT_PAGE_LEFT_COLUMN: |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 443 | case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN: |
| 444 | newPageIndex = pageIndex + 1; |
| 445 | if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) { |
| 446 | newPageIndex = pageIndex - 1; |
| 447 | } |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 448 | row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY; |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 449 | parent = getCellLayoutChildrenForIndex(workspace, newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 450 | if (parent != null) { |
| 451 | iconLayout = (CellLayout) parent.getParent(); |
Tony Wickham | 329d8bf | 2015-12-04 09:48:17 -0800 | [diff] [blame^] | 452 | matrix = FocusLogic.createSparseMatrixWithPivotColumn(iconLayout, -1, row); |
| 453 | newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, FocusLogic.PIVOT, |
| 454 | newPageIndex, pageCount, Utilities.isRtl(v.getResources())); |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 455 | if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) { |
| 456 | newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex, |
| 457 | isRtl); |
| 458 | } else if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LAST_ITEM) { |
| 459 | newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex, |
| 460 | isRtl); |
| 461 | } else { |
| 462 | newIcon = parent.getChildAt(newIconIndex); |
| 463 | } |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 464 | } |
| 465 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 466 | case FocusLogic.CURRENT_PAGE_FIRST_ITEM: |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 467 | newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl); |
| 468 | if (newIcon == null) { |
Tony Wickham | 0fa5ada | 2015-11-13 17:32:20 -0800 | [diff] [blame] | 469 | // Check the hotseat if no focusable item was found on the workspace. |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 470 | newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl); |
| 471 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 472 | break; |
| 473 | case FocusLogic.CURRENT_PAGE_LAST_ITEM: |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 474 | newIcon = getFirstFocusableIconInReverseReadingOrder(workspaceLayout, isRtl); |
| 475 | if (newIcon == null) { |
Tony Wickham | 0fa5ada | 2015-11-13 17:32:20 -0800 | [diff] [blame] | 476 | // Check the hotseat if no focusable item was found on the workspace. |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 477 | newIcon = getFirstFocusableIconInReverseReadingOrder(hotseatLayout, isRtl); |
| 478 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 479 | break; |
| 480 | default: |
| 481 | // current page, some item. |
| 482 | if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) { |
| 483 | newIcon = parent.getChildAt(newIconIndex); |
| 484 | } else if (parent.getChildCount() <= newIconIndex && |
| 485 | newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) { |
| 486 | newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount()); |
| 487 | } |
| 488 | break; |
| 489 | } |
| 490 | if (newIcon != null) { |
| 491 | newIcon.requestFocus(); |
| 492 | playSoundEffect(keyCode, v); |
| 493 | } |
| 494 | return consume; |
| 495 | } |
| 496 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 497 | // |
| 498 | // Helper methods. |
| 499 | // |
| 500 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 501 | /** |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 502 | * Private helper method to get the CellLayoutChildren given a CellLayout index. |
| 503 | */ |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 504 | @Thunk static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex( |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 505 | ViewGroup container, int i) { |
Sunny Goyal | b3726d9 | 2014-08-20 16:58:17 -0700 | [diff] [blame] | 506 | CellLayout parent = (CellLayout) container.getChildAt(i); |
| 507 | return parent.getShortcutsAndWidgets(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 510 | /** |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 511 | * Helper method to be used for playing sound effects. |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 512 | */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 513 | @Thunk static void playSoundEffect(int keyCode, View v) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 514 | switch (keyCode) { |
| 515 | case KeyEvent.KEYCODE_DPAD_LEFT: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 516 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 517 | break; |
| 518 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 519 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 520 | break; |
| 521 | case KeyEvent.KEYCODE_DPAD_DOWN: |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 522 | case KeyEvent.KEYCODE_PAGE_DOWN: |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 523 | case KeyEvent.KEYCODE_MOVE_END: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 524 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 525 | break; |
| 526 | case KeyEvent.KEYCODE_DPAD_UP: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 527 | case KeyEvent.KEYCODE_PAGE_UP: |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 528 | case KeyEvent.KEYCODE_MOVE_HOME: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 529 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 530 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 531 | default: |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 532 | break; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 533 | } |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 534 | } |
Winson | fa56b3f | 2015-09-14 12:01:13 -0700 | [diff] [blame] | 535 | |
| 536 | /** |
| 537 | * Returns whether the key event represents a valid uninstall key chord. |
| 538 | */ |
| 539 | private static boolean isUninstallKeyChord(KeyEvent event) { |
| 540 | int keyCode = event.getKeyCode(); |
| 541 | return (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) && |
| 542 | event.hasModifiers(KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Returns whether the key event represents a valid delete key chord. |
| 547 | */ |
| 548 | private static boolean isDeleteKeyChord(KeyEvent event) { |
| 549 | int keyCode = event.getKeyCode(); |
| 550 | return (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) && |
| 551 | event.hasModifiers(KeyEvent.META_CTRL_ON); |
| 552 | } |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 553 | |
| 554 | private static View handlePreviousPageLastItem(Workspace workspace, CellLayout hotseatLayout, |
| 555 | int pageIndex, boolean isRtl) { |
Tony Wickham | 0fa5ada | 2015-11-13 17:32:20 -0800 | [diff] [blame] | 556 | if (pageIndex - 1 < 0) { |
| 557 | return null; |
| 558 | } |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 559 | CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex - 1); |
| 560 | View newIcon = getFirstFocusableIconInReverseReadingOrder(workspaceLayout, isRtl); |
| 561 | if (newIcon == null) { |
Tony Wickham | 0fa5ada | 2015-11-13 17:32:20 -0800 | [diff] [blame] | 562 | // Check the hotseat if no focusable item was found on the workspace. |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 563 | newIcon = getFirstFocusableIconInReverseReadingOrder(hotseatLayout,isRtl); |
| 564 | workspace.snapToPage(pageIndex - 1); |
| 565 | } |
| 566 | return newIcon; |
| 567 | } |
| 568 | |
| 569 | private static View handleNextPageFirstItem(Workspace workspace, CellLayout hotseatLayout, |
| 570 | int pageIndex, boolean isRtl) { |
Tony Wickham | 0fa5ada | 2015-11-13 17:32:20 -0800 | [diff] [blame] | 571 | if (pageIndex + 1 >= workspace.getPageCount()) { |
| 572 | return null; |
| 573 | } |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 574 | CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex + 1); |
| 575 | View newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl); |
| 576 | if (newIcon == null) { |
Tony Wickham | 0fa5ada | 2015-11-13 17:32:20 -0800 | [diff] [blame] | 577 | // Check the hotseat if no focusable item was found on the workspace. |
Tony Wickham | af78b59 | 2015-11-11 09:25:38 -0800 | [diff] [blame] | 578 | newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl); |
| 579 | workspace.snapToPage(pageIndex + 1); |
| 580 | } |
| 581 | return newIcon; |
| 582 | } |
| 583 | |
| 584 | private static View getFirstFocusableIconInReadingOrder(CellLayout cellLayout, boolean isRtl) { |
| 585 | View icon; |
| 586 | int countX = cellLayout.getCountX(); |
| 587 | for (int y = 0; y < cellLayout.getCountY(); y++) { |
| 588 | int increment = isRtl ? -1 : 1; |
| 589 | for (int x = isRtl ? countX - 1 : 0; 0 <= x && x < countX; x += increment) { |
| 590 | if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) { |
| 591 | return icon; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | return null; |
| 596 | } |
| 597 | |
| 598 | private static View getFirstFocusableIconInReverseReadingOrder(CellLayout cellLayout, |
| 599 | boolean isRtl) { |
| 600 | View icon; |
| 601 | int countX = cellLayout.getCountX(); |
| 602 | for (int y = cellLayout.getCountY() - 1; y >= 0; y--) { |
| 603 | int increment = isRtl ? 1 : -1; |
| 604 | for (int x = isRtl ? 0 : countX - 1; 0 <= x && x < countX; x += increment) { |
| 605 | if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) { |
| 606 | return icon; |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | return null; |
| 611 | } |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 612 | } |