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