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