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