blob: fe50e3a4ecb232b3f9863a36205ad01536f23034 [file] [log] [blame]
Winson Chung97d85d22011-04-13 11:27:36 -07001/*
Hyunyoung Songee3e6a72015-02-20 14:25:27 -08002 * Copyright (C) 2015 The Android Open Source Project
Winson Chung97d85d22011-04-13 11:27:36 -07003 *
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung97d85d22011-04-13 11:27:36 -070018
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080019import android.util.Log;
Winson Chung97d85d22011-04-13 11:27:36 -070020import android.view.KeyEvent;
Sunny Goyalb3726d92014-08-20 16:58:17 -070021import android.view.SoundEffectConstants;
Winson Chung97d85d22011-04-13 11:27:36 -070022import android.view.View;
23import android.view.ViewGroup;
Winson Chung97d85d22011-04-13 11:27:36 -070024
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080025import com.android.launcher3.util.FocusLogic;
Adam Cohen091440a2015-03-18 14:16:05 -070026import com.android.launcher3.util.Thunk;
Winson Chungfaa13252011-06-13 18:15:54 -070027
Winson Chung97d85d22011-04-13 11:27:36 -070028/**
Winson Chung4d279d92011-07-21 11:46:32 -070029 * A keyboard listener we set on all the workspace icons.
30 */
Adam Cohenac56cff2011-09-28 20:45:37 -070031class IconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080032 @Override
Winson Chung4d279d92011-07-21 11:46:32 -070033 public boolean onKey(View v, int keyCode, KeyEvent event) {
Adam Cohenac56cff2011-09-28 20:45:37 -070034 return FocusHelper.handleIconKeyEvent(v, keyCode, event);
35 }
36}
37
38/**
Winson Chung3d503fb2011-07-13 17:25:49 -070039 * A keyboard listener we set on all the hotseat buttons.
Winson Chung4e6a9762011-05-09 11:56:34 -070040 */
Adam Cohenac56cff2011-09-28 20:45:37 -070041class HotseatIconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080042 @Override
Winson Chung4e6a9762011-05-09 11:56:34 -070043 public boolean onKey(View v, int keyCode, KeyEvent event) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080044 return FocusHelper.handleHotseatButtonKeyEvent(v, keyCode, event);
Winson Chung4e6a9762011-05-09 11:56:34 -070045 }
46}
47
Winson Chung97d85d22011-04-13 11:27:36 -070048public class FocusHelper {
Winson Chung97d85d22011-04-13 11:27:36 -070049
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080050 private static final String TAG = "FocusHelper";
51 private static final boolean DEBUG = false;
52
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080053 /**
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070054 * Handles key events in paged folder.
Sunny Goyal290800b2015-03-05 11:33:33 -080055 */
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070056 public static class PagedFolderKeyEventListener implements View.OnKeyListener {
Sunny Goyal290800b2015-03-05 11:33:33 -080057
58 private final Folder mFolder;
59
60 public PagedFolderKeyEventListener(Folder folder) {
61 mFolder = folder;
62 }
63
64 @Override
Hyunyoung Songada50982015-04-10 14:35:23 -070065 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 Goyal290800b2015-03-05 11:33:33 -080069 }
Hyunyoung Songada50982015-04-10 14:35:23 -070070 if (DEBUG) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070071 Log.v(TAG, String.format("Handle ALL Folders keyevent=[%s].",
Hyunyoung Songada50982015-04-10 14:35:23 -070072 KeyEvent.keyCodeToString(keyCode)));
73 }
Sunny Goyal290800b2015-03-05 11:33:33 -080074
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070075
76 if (!(v.getParent() instanceof ShortcutAndWidgetContainer)) {
Hyunyoung Songada50982015-04-10 14:35:23 -070077 if (LauncherAppState.isDogfoodBuild()) {
78 throw new IllegalStateException("Parent of the focused item is not supported.");
79 } else {
80 return false;
81 }
82 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080083
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070084 // 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 Songada50982015-04-10 14:35:23 -070089
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070090 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();
Adam Cohen2e6da152015-05-06 11:42:25 -070095 Launcher launcher = (Launcher) v.getContext();
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070096
97 int[][] matrix = FocusLogic.createSparseMatrix(cellLayout);
Hyunyoung Songada50982015-04-10 14:35:23 -070098 // Process focus.
Adam Cohen2e6da152015-05-06 11:42:25 -070099 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
100 countY, matrix, iconIndex, pageIndex, pageCount,
101 launcher.getDeviceProfile().isLayoutRtl);
Hyunyoung Songada50982015-04-10 14:35:23 -0700102 if (newIconIndex == FocusLogic.NOOP) {
103 handleNoopKey(keyCode, v);
104 return consume;
105 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700106 ShortcutAndWidgetContainer newParent = null;
107 View child = null;
108
Hyunyoung Songada50982015-04-10 14:35:23 -0700109 switch (newIconIndex) {
110 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700111 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
112 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700113 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700114 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 Songada50982015-04-10 14:35:23 -0700119 }
120 break;
121 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700122 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700123 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700124 pagedView.snapToPage(pageIndex - 1);
125 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700126 }
127 break;
128 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700129 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700130 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700131 pagedView.snapToPage(pageIndex - 1);
132 child = newParent.getChildAt(countX - 1, countY - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700133 }
134 break;
135 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700136 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700137 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700138 pagedView.snapToPage(pageIndex + 1);
139 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700140 }
141 break;
142 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700143 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
144 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700145 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700146 pagedView.snapToPage(pageIndex + 1);
147 child = FocusLogic.getAdjacentChildInNextPage(newParent, v, newIconIndex);
Hyunyoung Songada50982015-04-10 14:35:23 -0700148 }
149 break;
150 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700151 child = cellLayout.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700152 break;
153 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700154 child = pagedView.getLastItem();
Hyunyoung Songada50982015-04-10 14:35:23 -0700155 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 Songee3e6a72015-02-20 14:25:27 -0800166 return consume;
167 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800168
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700169 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 Goyal290800b2015-03-05 11:33:33 -0800175 }
176
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800177 /**
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 Songee3e6a72015-02-20 14:25:27 -0800188
Adam Cohen2e6da152015-05-06 11:42:25 -0700189 DeviceProfile profile = ((Launcher) v.getContext()).getDeviceProfile();
190
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800191 if (DEBUG) {
192 Log.v(TAG, String.format(
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700193 "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s",
194 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800195 }
196
197 // Initialize the variables.
198 final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent();
199 final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800200 Hotseat hotseat = (Hotseat) hotseatLayout.getParent();
201
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800202 Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700203 int pageIndex = workspace.getNextPage();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800204 int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800205 int countX = -1;
206 int countY = -1;
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700207 int iconIndex = hotseatParent.indexOfChild(v);
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700208 int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets()
209 .getChildAt(iconIndex).getLayoutParams()).cellX;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800210
211 final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700212 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 Songee3e6a72015-02-20 14:25:27 -0800218 final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets();
219
220 ViewGroup parent = null;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800221 int[][] matrix = null;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800222
223 if (keyCode == KeyEvent.KEYCODE_DPAD_UP &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700224 !profile.isVerticalBarLayout()) {
225 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout,
226 true /* hotseat horizontal */, hotseat.getAllAppsButtonRank(),
227 iconRank == hotseat.getAllAppsButtonRank() /* include all apps icon */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800228 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 Song18bfaaf2015-03-17 11:32:21 -0700233 profile.isVerticalBarLayout()) {
234 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout,
235 false /* hotseat horizontal */, hotseat.getAllAppsButtonRank(),
236 iconRank == hotseat.getAllAppsButtonRank() /* include all apps icon */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800237 iconIndex += iconParent.getChildCount();
238 countX = iconLayout.getCountX() + hotseatLayout.getCountX();
239 countY = iconLayout.getCountY();
240 parent = iconParent;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800241 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700242 profile.isVerticalBarLayout()) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800243 keyCode = KeyEvent.KEYCODE_PAGE_DOWN;
244 }else {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800245 // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the
246 // matrix extended with hotseat.
247 matrix = FocusLogic.createSparseMatrix(hotseatLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800248 countX = hotseatLayout.getCountX();
249 countY = hotseatLayout.getCountY();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800250 parent = hotseatParent;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800251 }
252
253 // Process the focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700254 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
255 countY, matrix, iconIndex, pageIndex, pageCount, profile.isLayoutRtl);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800256
Hyunyoung Song31178b82015-02-24 14:12:51 -0800257 View newIcon = null;
258 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
259 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
260 newIcon = parent.getChildAt(0);
261 // TODO(hyunyoungs): handle cases where the child is not an icon but
262 // a folder or a widget.
263 workspace.snapToPage(pageIndex + 1);
264 }
265 if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800266 newIconIndex -= iconParent.getChildCount();
267 }
268 if (parent != null) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800269 if (newIcon == null && newIconIndex >=0) {
270 newIcon = parent.getChildAt(newIconIndex);
271 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800272 if (newIcon != null) {
273 newIcon.requestFocus();
274 playSoundEffect(keyCode, v);
275 }
276 }
277 return consume;
278 }
279
280 /**
281 * Handles key events in a workspace containing icons.
282 */
283 static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) {
284 boolean consume = FocusLogic.shouldConsume(keyCode);
285 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
286 return consume;
287 }
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700288
Adam Cohen2e6da152015-05-06 11:42:25 -0700289 Launcher launcher = (Launcher) v.getContext();
290 DeviceProfile profile = launcher.getDeviceProfile();
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700291
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800292 if (DEBUG) {
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700293 Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s",
294 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800295 }
296
297 // Initialize the variables.
298 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
Hyunyoung Song38531712015-03-03 19:25:16 -0800299 CellLayout iconLayout = (CellLayout) parent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800300 final Workspace workspace = (Workspace) iconLayout.getParent();
Adam Cohen2e6da152015-05-06 11:42:25 -0700301 final ViewGroup dragLayer = (ViewGroup) workspace.getParent();
302 final ViewGroup tabs = (ViewGroup) dragLayer.findViewById(R.id.search_drop_target_bar);
303 final Hotseat hotseat = (Hotseat) dragLayer.findViewById(R.id.hotseat);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700304
305 final int iconIndex = parent.indexOfChild(v);
306 final int pageIndex = workspace.indexOfChild(iconLayout);
307 final int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800308 int countX = iconLayout.getCountX();
309 int countY = iconLayout.getCountY();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800310
311 CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0);
312 ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets();
313 int[][] matrix;
314
Hyunyoung Song31178b82015-02-24 14:12:51 -0800315 // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800316 // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended
317 // with the hotseat.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700318 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) {
319 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, true /* horizontal */,
Winson Chungc393b072015-05-20 15:03:13 -0700320 hotseat.getAllAppsButtonRank(),
321 !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800322 countY = countY + 1;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800323 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700324 profile.isVerticalBarLayout()) {
325 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, false /* horizontal */,
Winson Chungc393b072015-05-20 15:03:13 -0700326 hotseat.getAllAppsButtonRank(),
327 !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800328 countX = countX + 1;
329 } else if (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) {
330 workspace.removeWorkspaceItem(v);
331 return consume;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800332 } else {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800333 matrix = FocusLogic.createSparseMatrix(iconLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800334 }
335
336 // Process the focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700337 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
338 countY, matrix, iconIndex, pageIndex, pageCount, profile.isLayoutRtl);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800339 View newIcon = null;
340 switch (newIconIndex) {
341 case FocusLogic.NOOP:
342 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
343 newIcon = tabs;
344 }
345 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800346 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700347 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
348 int newPageIndex = pageIndex - 1;
349 if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) {
350 newPageIndex = pageIndex + 1;
351 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700352 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700353 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700354 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800355 if (parent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700356 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800357 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800358 matrix = FocusLogic.createSparseMatrix(iconLayout,
Hyunyoung Song38531712015-03-03 19:25:16 -0800359 iconLayout.getCountX(), row);
Adam Cohen2e6da152015-05-06 11:42:25 -0700360 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY,
361 matrix, FocusLogic.PIVOT, newPageIndex, pageCount, profile.isLayoutRtl);
Hyunyoung Song38531712015-03-03 19:25:16 -0800362 newIcon = parent.getChildAt(newIconIndex);
363 }
364 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800365 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
366 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
367 newIcon = parent.getChildAt(0);
368 workspace.snapToPage(pageIndex - 1);
Hyunyoung Song38531712015-03-03 19:25:16 -0800369 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800370 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 Song31178b82015-02-24 14:12:51 -0800378 workspace.snapToPage(pageIndex + 1);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800379 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800380 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700381 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
382 newPageIndex = pageIndex + 1;
383 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
384 newPageIndex = pageIndex - 1;
385 }
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700386 workspace.snapToPage(newPageIndex);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700387 row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700388 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800389 if (parent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700390 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800391 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800392 matrix = FocusLogic.createSparseMatrix(iconLayout, -1, row);
Adam Cohen2e6da152015-05-06 11:42:25 -0700393 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY,
394 matrix, FocusLogic.PIVOT, newPageIndex, pageCount, profile.isLayoutRtl);
Hyunyoung Song38531712015-03-03 19:25:16 -0800395 newIcon = parent.getChildAt(newIconIndex);
396 }
397 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800398 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
399 newIcon = parent.getChildAt(0);
400 break;
401 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
402 newIcon = parent.getChildAt(parent.getChildCount() - 1);
403 break;
404 default:
405 // current page, some item.
406 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
407 newIcon = parent.getChildAt(newIconIndex);
408 } else if (parent.getChildCount() <= newIconIndex &&
409 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
410 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
411 }
412 break;
413 }
414 if (newIcon != null) {
415 newIcon.requestFocus();
416 playSoundEffect(keyCode, v);
417 }
418 return consume;
419 }
420
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800421 //
422 // Helper methods.
423 //
424
Winson Chung97d85d22011-04-13 11:27:36 -0700425 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700426 * Private helper method to get the CellLayoutChildren given a CellLayout index.
427 */
Michael Jurkaa52570f2012-03-20 03:18:20 -0700428 private static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
429 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700430 CellLayout parent = (CellLayout) container.getChildAt(i);
431 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700432 }
433
Winson Chung97d85d22011-04-13 11:27:36 -0700434 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800435 * Helper method to be used for playing sound effects.
Winson Chung97d85d22011-04-13 11:27:36 -0700436 */
Adam Cohen091440a2015-03-18 14:16:05 -0700437 @Thunk static void playSoundEffect(int keyCode, View v) {
Winson Chung97d85d22011-04-13 11:27:36 -0700438 switch (keyCode) {
439 case KeyEvent.KEYCODE_DPAD_LEFT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800440 v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
Winson Chung97d85d22011-04-13 11:27:36 -0700441 break;
442 case KeyEvent.KEYCODE_DPAD_RIGHT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800443 v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
Winson Chung97d85d22011-04-13 11:27:36 -0700444 break;
445 case KeyEvent.KEYCODE_DPAD_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700446 case KeyEvent.KEYCODE_PAGE_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700447 case KeyEvent.KEYCODE_MOVE_END:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800448 v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
Adam Cohenac56cff2011-09-28 20:45:37 -0700449 break;
450 case KeyEvent.KEYCODE_DPAD_UP:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800451 case KeyEvent.KEYCODE_PAGE_UP:
Adam Cohenac56cff2011-09-28 20:45:37 -0700452 case KeyEvent.KEYCODE_MOVE_HOME:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800453 v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
Adam Cohenac56cff2011-09-28 20:45:37 -0700454 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800455 default:
Winson Chung97d85d22011-04-13 11:27:36 -0700456 break;
Winson Chung97d85d22011-04-13 11:27:36 -0700457 }
Winson Chung97d85d22011-04-13 11:27:36 -0700458 }
459}