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