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