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(); |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 95 | final boolean isLayoutRtl = Utilities.isRtl(v.getResources()); |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 96 | |
| 97 | int[][] matrix = FocusLogic.createSparseMatrix(cellLayout); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 98 | // Process focus. |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 99 | int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 100 | countY, matrix, iconIndex, pageIndex, pageCount, isLayoutRtl); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 101 | if (newIconIndex == FocusLogic.NOOP) { |
| 102 | handleNoopKey(keyCode, v); |
| 103 | return consume; |
| 104 | } |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 105 | ShortcutAndWidgetContainer newParent = null; |
| 106 | View child = null; |
| 107 | |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 108 | switch (newIconIndex) { |
| 109 | case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 110 | case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN: |
| 111 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 112 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 113 | int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY; |
| 114 | pagedView.snapToPage(pageIndex - 1); |
| 115 | child = newParent.getChildAt( |
| 116 | ((newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) |
| 117 | ^ newParent.invertLayoutHorizontally()) ? 0 : countX - 1, row); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 118 | } |
| 119 | break; |
| 120 | case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 121 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 122 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 123 | pagedView.snapToPage(pageIndex - 1); |
| 124 | child = newParent.getChildAt(0, 0); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 125 | } |
| 126 | break; |
| 127 | case FocusLogic.PREVIOUS_PAGE_LAST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 128 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 129 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 130 | pagedView.snapToPage(pageIndex - 1); |
| 131 | child = newParent.getChildAt(countX - 1, countY - 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 132 | } |
| 133 | break; |
| 134 | case FocusLogic.NEXT_PAGE_FIRST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 135 | newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 136 | if (newParent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 137 | pagedView.snapToPage(pageIndex + 1); |
| 138 | child = newParent.getChildAt(0, 0); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 139 | } |
| 140 | break; |
| 141 | case FocusLogic.NEXT_PAGE_LEFT_COLUMN: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 142 | case FocusLogic.NEXT_PAGE_RIGHT_COLUMN: |
| 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 = FocusLogic.getAdjacentChildInNextPage(newParent, v, newIconIndex); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 147 | } |
| 148 | break; |
| 149 | case FocusLogic.CURRENT_PAGE_FIRST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 150 | child = cellLayout.getChildAt(0, 0); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 151 | break; |
| 152 | case FocusLogic.CURRENT_PAGE_LAST_ITEM: |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 153 | child = pagedView.getLastItem(); |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 154 | break; |
| 155 | default: // Go to some item on the current page. |
| 156 | child = itemContainer.getChildAt(newIconIndex); |
| 157 | break; |
| 158 | } |
| 159 | if (child != null) { |
| 160 | child.requestFocus(); |
| 161 | playSoundEffect(keyCode, v); |
| 162 | } else { |
| 163 | handleNoopKey(keyCode, v); |
| 164 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 165 | return consume; |
| 166 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 167 | |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 168 | public void handleNoopKey(int keyCode, View v) { |
| 169 | if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { |
| 170 | mFolder.mFolderName.requestFocus(); |
| 171 | playSoundEffect(keyCode, v); |
| 172 | } |
| 173 | } |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 176 | /** |
| 177 | * Handles key events in the workspace hot seat (bottom of the screen). |
| 178 | * <p>Currently we don't special case for the phone UI in different orientations, even though |
| 179 | * the hotseat is on the side in landscape mode. This is to ensure that accessibility |
| 180 | * consistency is maintained across rotations. |
| 181 | */ |
| 182 | static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) { |
| 183 | boolean consume = FocusLogic.shouldConsume(keyCode); |
| 184 | if (e.getAction() == KeyEvent.ACTION_UP || !consume) { |
| 185 | return consume; |
| 186 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 187 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 188 | DeviceProfile profile = ((Launcher) v.getContext()).getDeviceProfile(); |
| 189 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 190 | if (DEBUG) { |
| 191 | Log.v(TAG, String.format( |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 192 | "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s", |
| 193 | KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout())); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Initialize the variables. |
| 197 | final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent(); |
| 198 | final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 199 | Hotseat hotseat = (Hotseat) hotseatLayout.getParent(); |
| 200 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 201 | Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace); |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 202 | int pageIndex = workspace.getNextPage(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 203 | int pageCount = workspace.getChildCount(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 204 | int countX = -1; |
| 205 | int countY = -1; |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 206 | int iconIndex = hotseatParent.indexOfChild(v); |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 207 | int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets() |
| 208 | .getChildAt(iconIndex).getLayoutParams()).cellX; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 209 | |
| 210 | final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex); |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 211 | if (iconLayout == null) { |
| 212 | // This check is to guard against cases where key strokes rushes in when workspace |
| 213 | // child creation/deletion is still in flux. (e.g., during drop or fling |
| 214 | // animation.) |
| 215 | return consume; |
| 216 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 217 | final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets(); |
| 218 | |
| 219 | ViewGroup parent = null; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 220 | int[][] matrix = null; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 221 | |
| 222 | if (keyCode == KeyEvent.KEYCODE_DPAD_UP && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 223 | !profile.isVerticalBarLayout()) { |
| 224 | matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, |
| 225 | true /* hotseat horizontal */, hotseat.getAllAppsButtonRank(), |
| 226 | iconRank == hotseat.getAllAppsButtonRank() /* include all apps icon */); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 227 | iconIndex += iconParent.getChildCount(); |
| 228 | countX = iconLayout.getCountX(); |
| 229 | countY = iconLayout.getCountY() + hotseatLayout.getCountY(); |
| 230 | parent = iconParent; |
| 231 | } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 232 | profile.isVerticalBarLayout()) { |
| 233 | matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, |
| 234 | false /* hotseat horizontal */, hotseat.getAllAppsButtonRank(), |
| 235 | iconRank == hotseat.getAllAppsButtonRank() /* include all apps icon */); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 236 | iconIndex += iconParent.getChildCount(); |
| 237 | countX = iconLayout.getCountX() + hotseatLayout.getCountX(); |
| 238 | countY = iconLayout.getCountY(); |
| 239 | parent = iconParent; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 240 | } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 241 | profile.isVerticalBarLayout()) { |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 242 | keyCode = KeyEvent.KEYCODE_PAGE_DOWN; |
| 243 | }else { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 244 | // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the |
| 245 | // matrix extended with hotseat. |
| 246 | matrix = FocusLogic.createSparseMatrix(hotseatLayout); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 247 | countX = hotseatLayout.getCountX(); |
| 248 | countY = hotseatLayout.getCountY(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 249 | parent = hotseatParent; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Process the focus. |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 253 | int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 254 | countY, matrix, iconIndex, pageIndex, pageCount, Utilities.isRtl(v.getResources())); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 255 | |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 256 | View newIcon = null; |
| 257 | if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) { |
| 258 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1); |
| 259 | newIcon = parent.getChildAt(0); |
| 260 | // TODO(hyunyoungs): handle cases where the child is not an icon but |
| 261 | // a folder or a widget. |
| 262 | workspace.snapToPage(pageIndex + 1); |
| 263 | } |
| 264 | if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) { |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 265 | newIconIndex -= iconParent.getChildCount(); |
| 266 | } |
| 267 | if (parent != null) { |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 268 | if (newIcon == null && newIconIndex >=0) { |
| 269 | newIcon = parent.getChildAt(newIconIndex); |
| 270 | } |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 271 | if (newIcon != null) { |
| 272 | newIcon.requestFocus(); |
| 273 | playSoundEffect(keyCode, v); |
| 274 | } |
| 275 | } |
| 276 | return consume; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Handles key events in a workspace containing icons. |
| 281 | */ |
| 282 | static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) { |
| 283 | boolean consume = FocusLogic.shouldConsume(keyCode); |
| 284 | if (e.getAction() == KeyEvent.ACTION_UP || !consume) { |
| 285 | return consume; |
| 286 | } |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 287 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 288 | Launcher launcher = (Launcher) v.getContext(); |
| 289 | DeviceProfile profile = launcher.getDeviceProfile(); |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 290 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 291 | if (DEBUG) { |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 292 | Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s", |
| 293 | KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout())); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // Initialize the variables. |
| 297 | ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent(); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 298 | CellLayout iconLayout = (CellLayout) parent.getParent(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 299 | final Workspace workspace = (Workspace) iconLayout.getParent(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 300 | final ViewGroup dragLayer = (ViewGroup) workspace.getParent(); |
| 301 | final ViewGroup tabs = (ViewGroup) dragLayer.findViewById(R.id.search_drop_target_bar); |
| 302 | final Hotseat hotseat = (Hotseat) dragLayer.findViewById(R.id.hotseat); |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 303 | |
| 304 | final int iconIndex = parent.indexOfChild(v); |
| 305 | final int pageIndex = workspace.indexOfChild(iconLayout); |
| 306 | final int pageCount = workspace.getChildCount(); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 307 | int countX = iconLayout.getCountX(); |
| 308 | int countY = iconLayout.getCountY(); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 309 | |
| 310 | CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0); |
| 311 | ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets(); |
| 312 | int[][] matrix; |
| 313 | |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 314 | // 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] | 315 | // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended |
| 316 | // with the hotseat. |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 317 | if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) { |
| 318 | matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, true /* horizontal */, |
Winson Chung | c393b07 | 2015-05-20 15:03:13 -0700 | [diff] [blame] | 319 | hotseat.getAllAppsButtonRank(), |
| 320 | !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 321 | countY = countY + 1; |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 322 | } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && |
Hyunyoung Song | 18bfaaf | 2015-03-17 11:32:21 -0700 | [diff] [blame] | 323 | profile.isVerticalBarLayout()) { |
| 324 | matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, false /* horizontal */, |
Winson Chung | c393b07 | 2015-05-20 15:03:13 -0700 | [diff] [blame] | 325 | hotseat.getAllAppsButtonRank(), |
| 326 | !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 327 | countX = countX + 1; |
| 328 | } else if (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) { |
| 329 | workspace.removeWorkspaceItem(v); |
| 330 | return consume; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 331 | } else { |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 332 | matrix = FocusLogic.createSparseMatrix(iconLayout); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // Process the focus. |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 336 | int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 337 | countY, matrix, iconIndex, pageIndex, pageCount, Utilities.isRtl(v.getResources())); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 338 | View newIcon = null; |
| 339 | switch (newIconIndex) { |
| 340 | case FocusLogic.NOOP: |
| 341 | if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { |
| 342 | newIcon = tabs; |
| 343 | } |
| 344 | break; |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 345 | case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN: |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 346 | case FocusLogic.NEXT_PAGE_RIGHT_COLUMN: |
| 347 | int newPageIndex = pageIndex - 1; |
| 348 | if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) { |
| 349 | newPageIndex = pageIndex + 1; |
| 350 | } |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 351 | int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY; |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 352 | parent = getCellLayoutChildrenForIndex(workspace, newPageIndex); |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 353 | workspace.snapToPage(newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 354 | if (parent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 355 | workspace.snapToPage(newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 356 | iconLayout = (CellLayout) parent.getParent(); |
Hyunyoung Song | ac721f8 | 2015-03-04 16:33:56 -0800 | [diff] [blame] | 357 | matrix = FocusLogic.createSparseMatrix(iconLayout, |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 358 | iconLayout.getCountX(), row); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 359 | newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY, |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 360 | matrix, FocusLogic.PIVOT, newPageIndex, pageCount, |
| 361 | Utilities.isRtl(v.getResources())); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 362 | newIcon = parent.getChildAt(newIconIndex); |
| 363 | } |
| 364 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 365 | case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM: |
| 366 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1); |
| 367 | newIcon = parent.getChildAt(0); |
| 368 | workspace.snapToPage(pageIndex - 1); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 369 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 370 | case FocusLogic.PREVIOUS_PAGE_LAST_ITEM: |
| 371 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1); |
| 372 | newIcon = parent.getChildAt(parent.getChildCount() - 1); |
| 373 | workspace.snapToPage(pageIndex - 1); |
| 374 | break; |
| 375 | case FocusLogic.NEXT_PAGE_FIRST_ITEM: |
| 376 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1); |
| 377 | newIcon = parent.getChildAt(0); |
Hyunyoung Song | 31178b8 | 2015-02-24 14:12:51 -0800 | [diff] [blame] | 378 | workspace.snapToPage(pageIndex + 1); |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 379 | break; |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 380 | case FocusLogic.NEXT_PAGE_LEFT_COLUMN: |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 381 | case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN: |
| 382 | newPageIndex = pageIndex + 1; |
| 383 | if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) { |
| 384 | newPageIndex = pageIndex - 1; |
| 385 | } |
Hyunyoung Song | b76cd62 | 2015-04-16 14:34:09 -0700 | [diff] [blame] | 386 | workspace.snapToPage(newPageIndex); |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 387 | row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY; |
Hyunyoung Song | ada5098 | 2015-04-10 14:35:23 -0700 | [diff] [blame] | 388 | parent = getCellLayoutChildrenForIndex(workspace, newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 389 | if (parent != null) { |
Sunny Goyal | fc3c1ed | 2015-04-09 18:48:21 -0700 | [diff] [blame] | 390 | workspace.snapToPage(newPageIndex); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 391 | iconLayout = (CellLayout) parent.getParent(); |
Hyunyoung Song | ac721f8 | 2015-03-04 16:33:56 -0800 | [diff] [blame] | 392 | matrix = FocusLogic.createSparseMatrix(iconLayout, -1, row); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 393 | newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY, |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 394 | matrix, FocusLogic.PIVOT, newPageIndex, pageCount, |
| 395 | Utilities.isRtl(v.getResources())); |
Hyunyoung Song | 3853171 | 2015-03-03 19:25:16 -0800 | [diff] [blame] | 396 | newIcon = parent.getChildAt(newIconIndex); |
| 397 | } |
| 398 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 399 | case FocusLogic.CURRENT_PAGE_FIRST_ITEM: |
| 400 | newIcon = parent.getChildAt(0); |
| 401 | break; |
| 402 | case FocusLogic.CURRENT_PAGE_LAST_ITEM: |
| 403 | newIcon = parent.getChildAt(parent.getChildCount() - 1); |
| 404 | break; |
| 405 | default: |
| 406 | // current page, some item. |
| 407 | if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) { |
| 408 | newIcon = parent.getChildAt(newIconIndex); |
| 409 | } else if (parent.getChildCount() <= newIconIndex && |
| 410 | newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) { |
| 411 | newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount()); |
| 412 | } |
| 413 | break; |
| 414 | } |
| 415 | if (newIcon != null) { |
| 416 | newIcon.requestFocus(); |
| 417 | playSoundEffect(keyCode, v); |
| 418 | } |
| 419 | return consume; |
| 420 | } |
| 421 | |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 422 | // |
| 423 | // Helper methods. |
| 424 | // |
| 425 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 426 | /** |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 427 | * Private helper method to get the CellLayoutChildren given a CellLayout index. |
| 428 | */ |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 429 | @Thunk static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex( |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 430 | ViewGroup container, int i) { |
Sunny Goyal | b3726d9 | 2014-08-20 16:58:17 -0700 | [diff] [blame] | 431 | CellLayout parent = (CellLayout) container.getChildAt(i); |
| 432 | return parent.getShortcutsAndWidgets(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 435 | /** |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 436 | * Helper method to be used for playing sound effects. |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 437 | */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 438 | @Thunk static void playSoundEffect(int keyCode, View v) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 439 | switch (keyCode) { |
| 440 | case KeyEvent.KEYCODE_DPAD_LEFT: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 441 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 442 | break; |
| 443 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 444 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 445 | break; |
| 446 | case KeyEvent.KEYCODE_DPAD_DOWN: |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 447 | case KeyEvent.KEYCODE_PAGE_DOWN: |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 448 | case KeyEvent.KEYCODE_MOVE_END: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 449 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 450 | break; |
| 451 | case KeyEvent.KEYCODE_DPAD_UP: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 452 | case KeyEvent.KEYCODE_PAGE_UP: |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 453 | case KeyEvent.KEYCODE_MOVE_HOME: |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 454 | v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 455 | break; |
Hyunyoung Song | ee3e6a7 | 2015-02-20 14:25:27 -0800 | [diff] [blame] | 456 | default: |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 457 | break; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 458 | } |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 459 | } |
| 460 | } |