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