Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1 | /* |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 18 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 19 | import android.util.Log; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 20 | import android.view.KeyEvent; |
Sunny Goyal | b3726d9 | 2014-08-20 16:58:17 -0700 | [diff] [blame] | 21 | import android.view.SoundEffectConstants; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 24 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 25 | import com.android.launcher3.util.FocusLogic; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 26 | import com.android.launcher3.util.Thunk; |
Winson Chung | faa1325 | 2011-06-13 18:15:54 -0700 | [diff] [blame] | 27 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 28 | /** |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 29 | * A keyboard listener we set on all the workspace icons. |
| 30 | */ |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 31 | class IconKeyEventListener implements View.OnKeyListener { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 32 | @Override |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 33 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 34 | return FocusHelper.handleIconKeyEvent(v, keyCode, event); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 39 | * A keyboard listener we set on all the hotseat buttons. |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 40 | */ |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 41 | class HotseatIconKeyEventListener implements View.OnKeyListener { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 42 | @Override |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 43 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 44 | return FocusHelper.handleHotseatButtonKeyEvent(v, keyCode, event); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 48 | public class FocusHelper { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 49 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 50 | private static final String TAG = "FocusHelper"; |
| 51 | private static final boolean DEBUG = false; |
| 52 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 53 | /** |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 54 | * Handles key events in paged folder. |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 55 | */ |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 56 | public static class PagedFolderKeyEventListener implements View.OnKeyListener { |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 57 | |
| 58 | private final Folder mFolder; |
| 59 | |
| 60 | public PagedFolderKeyEventListener(Folder folder) { |
| 61 | mFolder = folder; |
| 62 | } |
| 63 | |
| 64 | @Override |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 65 | public boolean onKey(View v, int keyCode, KeyEvent e) { |
| 66 | boolean consume = FocusLogic.shouldConsume(keyCode); |
| 67 | if (e.getAction() == KeyEvent.ACTION_UP) { |
| 68 | return consume; |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 69 | } |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 70 | if (DEBUG) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 71 | Log.v(TAG, String.format("Handle ALL Folders keyevent=[%s].", |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 72 | KeyEvent.keyCodeToString(keyCode))); |
| 73 | } |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 74 | |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 75 | |
| 76 | if (!(v.getParent() instanceof ShortcutAndWidgetContainer)) { |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 77 | if (LauncherAppState.isDogfoodBuild()) { |
| 78 | throw new IllegalStateException("Parent of the focused item is not supported."); |
| 79 | } else { |
| 80 | return false; |
| 81 | } |
| 82 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 83 | |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 84 | // Initialize variables. |
| 85 | final ShortcutAndWidgetContainer itemContainer = (ShortcutAndWidgetContainer) v.getParent(); |
| 86 | final CellLayout cellLayout = (CellLayout) itemContainer.getParent(); |
| 87 | final int countX = cellLayout.getCountX(); |
| 88 | final int countY = cellLayout.getCountY(); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 89 | |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 90 | final int iconIndex = itemContainer.indexOfChild(v); |
| 91 | final FolderPagedView pagedView = (FolderPagedView) cellLayout.getParent(); |
| 92 | |
| 93 | final int pageIndex = pagedView.indexOfChild(cellLayout); |
| 94 | final int pageCount = pagedView.getPageCount(); |
| 95 | |
| 96 | int[][] matrix = FocusLogic.createSparseMatrix(cellLayout); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 97 | // Process focus. |
| 98 | int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix, |
| 99 | iconIndex, pageIndex, pageCount); |
| 100 | if (newIconIndex == FocusLogic.NOOP) { |
| 101 | handleNoopKey(keyCode, v); |
| 102 | return consume; |
| 103 | } |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 104 | ShortcutAndWidgetContainer newParent = null; |
| 105 | View child = null; |
| 106 | |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 107 | switch (newIconIndex) { |
| 108 | case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 109 | case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN: |
| 110 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 111 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 112 | int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY; |
| 113 | pagedView.snapToPage(pageIndex - 1); |
| 114 | child = newParent.getChildAt( |
| 115 | ((newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) |
| 116 | ^ newParent.invertLayoutHorizontally()) ? 0 : countX - 1, row); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 117 | } |
| 118 | break; |
| 119 | case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 120 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 121 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 122 | pagedView.snapToPage(pageIndex - 1); |
| 123 | child = newParent.getChildAt(0, 0); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 124 | } |
| 125 | break; |
| 126 | case FocusLogic.PREVIOUS_PAGE_LAST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 127 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 128 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 129 | pagedView.snapToPage(pageIndex - 1); |
| 130 | child = newParent.getChildAt(countX - 1, countY - 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 131 | } |
| 132 | break; |
| 133 | case FocusLogic.NEXT_PAGE_FIRST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 134 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 135 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 136 | pagedView.snapToPage(pageIndex + 1); |
| 137 | child = newParent.getChildAt(0, 0); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 138 | } |
| 139 | break; |
| 140 | case FocusLogic.NEXT_PAGE_LEFT_COLUMN: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 141 | case FocusLogic.NEXT_PAGE_RIGHT_COLUMN: |
| 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 = FocusLogic.getAdjacentChildInNextPage(newParent, v, newIconIndex); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 146 | } |
| 147 | break; |
| 148 | case FocusLogic.CURRENT_PAGE_FIRST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 149 | child = cellLayout.getChildAt(0, 0); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 150 | break; |
| 151 | case FocusLogic.CURRENT_PAGE_LAST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 152 | child = pagedView.getLastItem(); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 153 | break; |
| 154 | default: // Go to some item on the current page. |
| 155 | child = itemContainer.getChildAt(newIconIndex); |
| 156 | break; |
| 157 | } |
| 158 | if (child != null) { |
| 159 | child.requestFocus(); |
| 160 | playSoundEffect(keyCode, v); |
| 161 | } else { |
| 162 | handleNoopKey(keyCode, v); |
| 163 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 164 | return consume; |
| 165 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 166 | |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 167 | public void handleNoopKey(int keyCode, View v) { |
| 168 | if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { |
| 169 | mFolder.mFolderName.requestFocus(); |
| 170 | playSoundEffect(keyCode, v); |
| 171 | } |
| 172 | } |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 175 | /** |
| 176 | * Handles key events in the workspace hot seat (bottom of the screen). |
| 177 | * <p>Currently we don't special case for the phone UI in different orientations, even though |
| 178 | * the hotseat is on the side in landscape mode. This is to ensure that accessibility |
| 179 | * consistency is maintained across rotations. |
| 180 | */ |
| 181 | static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) { |
| 182 | boolean consume = FocusLogic.shouldConsume(keyCode); |
| 183 | if (e.getAction() == KeyEvent.ACTION_UP || !consume) { |
| 184 | return consume; |
| 185 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 186 | |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 187 | DeviceProfile profile = LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 188 | if (DEBUG) { |
| 189 | Log.v(TAG, String.format( |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 190 | "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s", |
| 191 | KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout())); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // Initialize the variables. |
| 195 | final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent(); |
| 196 | final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 197 | Hotseat hotseat = (Hotseat) hotseatLayout.getParent(); |
| 198 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 199 | Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace); |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 200 | int pageIndex = workspace.getNextPage(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 201 | int pageCount = workspace.getChildCount(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 202 | int countX = -1; |
| 203 | int countY = -1; |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 204 | int iconIndex = hotseatParent.indexOfChild(v); |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 205 | int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets() |
| 206 | .getChildAt(iconIndex).getLayoutParams()).cellX; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 207 | |
| 208 | final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex); |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 209 | if (iconLayout == null) { |
| 210 | // This check is to guard against cases where key strokes rushes in when workspace |
| 211 | // child creation/deletion is still in flux. (e.g., during drop or fling |
| 212 | // animation.) |
| 213 | return consume; |
| 214 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 215 | final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets(); |
| 216 | |
| 217 | ViewGroup parent = null; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 218 | int[][] matrix = null; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 219 | |
| 220 | if (keyCode == KeyEvent.KEYCODE_DPAD_UP && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 221 | !profile.isVerticalBarLayout()) { |
| 222 | matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, |
| 223 | true /* hotseat horizontal */, hotseat.getAllAppsButtonRank(), |
| 224 | iconRank == hotseat.getAllAppsButtonRank() /* include all apps icon */); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 225 | iconIndex += iconParent.getChildCount(); |
| 226 | countX = iconLayout.getCountX(); |
| 227 | countY = iconLayout.getCountY() + hotseatLayout.getCountY(); |
| 228 | parent = iconParent; |
| 229 | } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 230 | profile.isVerticalBarLayout()) { |
| 231 | matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, |
| 232 | false /* hotseat horizontal */, hotseat.getAllAppsButtonRank(), |
| 233 | iconRank == hotseat.getAllAppsButtonRank() /* include all apps icon */); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 234 | iconIndex += iconParent.getChildCount(); |
| 235 | countX = iconLayout.getCountX() + hotseatLayout.getCountX(); |
| 236 | countY = iconLayout.getCountY(); |
| 237 | parent = iconParent; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 238 | } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 239 | profile.isVerticalBarLayout()) { |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 240 | keyCode = KeyEvent.KEYCODE_PAGE_DOWN; |
| 241 | }else { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 242 | // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the |
| 243 | // matrix extended with hotseat. |
| 244 | matrix = FocusLogic.createSparseMatrix(hotseatLayout); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 245 | countX = hotseatLayout.getCountX(); |
| 246 | countY = hotseatLayout.getCountY(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 247 | parent = hotseatParent; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // Process the focus. |
| 251 | int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix, |
| 252 | iconIndex, pageIndex, pageCount); |
| 253 | |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 254 | View newIcon = null; |
| 255 | if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) { |
| 256 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1); |
| 257 | newIcon = parent.getChildAt(0); |
| 258 | // TODO(hyunyoungs): handle cases where the child is not an icon but |
| 259 | // a folder or a widget. |
| 260 | workspace.snapToPage(pageIndex + 1); |
| 261 | } |
| 262 | if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 263 | newIconIndex -= iconParent.getChildCount(); |
| 264 | } |
| 265 | if (parent != null) { |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 266 | if (newIcon == null && newIconIndex >=0) { |
| 267 | newIcon = parent.getChildAt(newIconIndex); |
| 268 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 269 | if (newIcon != null) { |
| 270 | newIcon.requestFocus(); |
| 271 | playSoundEffect(keyCode, v); |
| 272 | } |
| 273 | } |
| 274 | return consume; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Handles key events in a workspace containing icons. |
| 279 | */ |
| 280 | static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) { |
| 281 | boolean consume = FocusLogic.shouldConsume(keyCode); |
| 282 | if (e.getAction() == KeyEvent.ACTION_UP || !consume) { |
| 283 | return consume; |
| 284 | } |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 285 | |
| 286 | LauncherAppState app = LauncherAppState.getInstance(); |
| 287 | DeviceProfile profile = app.getDynamicGrid().getDeviceProfile(); |
| 288 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 289 | if (DEBUG) { |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 290 | Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s", |
| 291 | KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout())); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Initialize the variables. |
| 295 | ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent(); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 296 | CellLayout iconLayout = (CellLayout) parent.getParent(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 297 | final Workspace workspace = (Workspace) iconLayout.getParent(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 298 | final ViewGroup launcher = (ViewGroup) workspace.getParent(); |
| 299 | final ViewGroup tabs = (ViewGroup) launcher.findViewById(R.id.search_drop_target_bar); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 300 | final Hotseat hotseat = (Hotseat) launcher.findViewById(R.id.hotseat); |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 301 | |
| 302 | final int iconIndex = parent.indexOfChild(v); |
| 303 | final int pageIndex = workspace.indexOfChild(iconLayout); |
| 304 | final int pageCount = workspace.getChildCount(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 305 | int countX = iconLayout.getCountX(); |
| 306 | int countY = iconLayout.getCountY(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 307 | |
| 308 | CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0); |
| 309 | ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets(); |
| 310 | int[][] matrix; |
| 311 | |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 312 | // 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] | 313 | // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended |
| 314 | // with the hotseat. |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 315 | if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) { |
| 316 | matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, true /* horizontal */, |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 317 | hotseat.getAllAppsButtonRank(), false /* all apps icon is ignored */); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 318 | countY = countY + 1; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 319 | } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 320 | profile.isVerticalBarLayout()) { |
| 321 | matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, false /* horizontal */, |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 322 | hotseat.getAllAppsButtonRank(), false /* all apps icon is ignored */); |
| 323 | countX = countX + 1; |
| 324 | } else if (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) { |
| 325 | workspace.removeWorkspaceItem(v); |
| 326 | return consume; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 327 | } else { |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 328 | matrix = FocusLogic.createSparseMatrix(iconLayout); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // Process the focus. |
| 332 | int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix, |
| 333 | iconIndex, pageIndex, pageCount); |
| 334 | View newIcon = null; |
| 335 | switch (newIconIndex) { |
| 336 | case FocusLogic.NOOP: |
| 337 | if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { |
| 338 | newIcon = tabs; |
| 339 | } |
| 340 | break; |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 341 | case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN: |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 342 | case FocusLogic.NEXT_PAGE_RIGHT_COLUMN: |
| 343 | int newPageIndex = pageIndex - 1; |
| 344 | if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) { |
| 345 | newPageIndex = pageIndex + 1; |
| 346 | } |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 347 | int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY; |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 348 | parent = getCellLayoutChildrenForIndex(workspace, newPageIndex); |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 349 | workspace.snapToPage(newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 350 | if (parent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 351 | workspace.snapToPage(newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 352 | iconLayout = (CellLayout) parent.getParent(); |
Hyunyoung Song | ac721f8 | 2015-03-04 16:33:56 -0800 | [diff] [blame] | 353 | matrix = FocusLogic.createSparseMatrix(iconLayout, |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 354 | iconLayout.getCountX(), row); |
| 355 | newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY, matrix, |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 356 | FocusLogic.PIVOT, newPageIndex, pageCount); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 357 | newIcon = parent.getChildAt(newIconIndex); |
| 358 | } |
| 359 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 360 | case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM: |
| 361 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1); |
| 362 | newIcon = parent.getChildAt(0); |
| 363 | workspace.snapToPage(pageIndex - 1); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 364 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 365 | case FocusLogic.PREVIOUS_PAGE_LAST_ITEM: |
| 366 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1); |
| 367 | newIcon = parent.getChildAt(parent.getChildCount() - 1); |
| 368 | workspace.snapToPage(pageIndex - 1); |
| 369 | break; |
| 370 | case FocusLogic.NEXT_PAGE_FIRST_ITEM: |
| 371 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1); |
| 372 | newIcon = parent.getChildAt(0); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 373 | workspace.snapToPage(pageIndex + 1); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 374 | break; |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 375 | case FocusLogic.NEXT_PAGE_LEFT_COLUMN: |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 376 | case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN: |
| 377 | newPageIndex = pageIndex + 1; |
| 378 | if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) { |
| 379 | newPageIndex = pageIndex - 1; |
| 380 | } |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 381 | workspace.snapToPage(newPageIndex); |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 382 | row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY; |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 383 | parent = getCellLayoutChildrenForIndex(workspace, newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 384 | if (parent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame^] | 385 | workspace.snapToPage(newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 386 | iconLayout = (CellLayout) parent.getParent(); |
Hyunyoung Song | ac721f8 | 2015-03-04 16:33:56 -0800 | [diff] [blame] | 387 | matrix = FocusLogic.createSparseMatrix(iconLayout, -1, row); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 388 | newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY, matrix, |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 389 | FocusLogic.PIVOT, newPageIndex, pageCount); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 390 | newIcon = parent.getChildAt(newIconIndex); |
| 391 | } |
| 392 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 393 | case FocusLogic.CURRENT_PAGE_FIRST_ITEM: |
| 394 | newIcon = parent.getChildAt(0); |
| 395 | break; |
| 396 | case FocusLogic.CURRENT_PAGE_LAST_ITEM: |
| 397 | newIcon = parent.getChildAt(parent.getChildCount() - 1); |
| 398 | break; |
| 399 | default: |
| 400 | // current page, some item. |
| 401 | if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) { |
| 402 | newIcon = parent.getChildAt(newIconIndex); |
| 403 | } else if (parent.getChildCount() <= newIconIndex && |
| 404 | newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) { |
| 405 | newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount()); |
| 406 | } |
| 407 | break; |
| 408 | } |
| 409 | if (newIcon != null) { |
| 410 | newIcon.requestFocus(); |
| 411 | playSoundEffect(keyCode, v); |
| 412 | } |
| 413 | return consume; |
| 414 | } |
| 415 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 416 | // |
| 417 | // Helper methods. |
| 418 | // |
| 419 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 420 | /** |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 421 | * Private helper method to get the CellLayoutChildren given a CellLayout index. |
| 422 | */ |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 423 | private static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex( |
| 424 | ViewGroup container, int i) { |
Sunny Goyal | b3726d9 | 2014-08-20 16:58:17 -0700 | [diff] [blame] | 425 | CellLayout parent = (CellLayout) container.getChildAt(i); |
| 426 | return parent.getShortcutsAndWidgets(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 429 | /** |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 430 | * Helper method to be used for playing sound effects. |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 431 | */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 432 | @Thunk static void playSoundEffect(int keyCode, View v) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 433 | switch (keyCode) { |
| 434 | case KeyEvent.KEYCODE_DPAD_LEFT: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 435 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 436 | break; |
| 437 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 438 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 439 | break; |
| 440 | case KeyEvent.KEYCODE_DPAD_DOWN: |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 441 | case KeyEvent.KEYCODE_PAGE_DOWN: |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 442 | case KeyEvent.KEYCODE_MOVE_END: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 443 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 444 | break; |
| 445 | case KeyEvent.KEYCODE_DPAD_UP: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 446 | case KeyEvent.KEYCODE_PAGE_UP: |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 447 | case KeyEvent.KEYCODE_MOVE_HOME: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 448 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 449 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 450 | default: |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 451 | break; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 452 | } |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 453 | } |
| 454 | } |