blob: e5aecf7be18b999b8f6daa8eacda81f6d55c1b99 [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
Sunny Goyal3d706ad2017-03-06 16:56:39 -080025import com.android.launcher3.config.FeatureFlags;
Sunny Goyal26119432016-02-18 22:09:23 +000026import com.android.launcher3.folder.Folder;
Adam Cohenf9c184a2016-01-15 16:47:43 -080027import com.android.launcher3.folder.FolderPagedView;
Sunny Goyale396abf2020-04-06 15:11:17 -070028import com.android.launcher3.model.data.ItemInfo;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080029import com.android.launcher3.util.FocusLogic;
Adam Cohen091440a2015-03-18 14:16:05 -070030import com.android.launcher3.util.Thunk;
Winson Chungfaa13252011-06-13 18:15:54 -070031
Winson Chung97d85d22011-04-13 11:27:36 -070032/**
Winson Chung4d279d92011-07-21 11:46:32 -070033 * A keyboard listener we set on all the workspace icons.
34 */
Adam Cohenac56cff2011-09-28 20:45:37 -070035class IconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080036 @Override
Winson Chung4d279d92011-07-21 11:46:32 -070037 public boolean onKey(View v, int keyCode, KeyEvent event) {
Adam Cohenac56cff2011-09-28 20:45:37 -070038 return FocusHelper.handleIconKeyEvent(v, keyCode, event);
39 }
40}
41
42/**
Winson Chung3d503fb2011-07-13 17:25:49 -070043 * A keyboard listener we set on all the hotseat buttons.
Winson Chung4e6a9762011-05-09 11:56:34 -070044 */
Adam Cohenac56cff2011-09-28 20:45:37 -070045class HotseatIconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080046 @Override
Winson Chung4e6a9762011-05-09 11:56:34 -070047 public boolean onKey(View v, int keyCode, KeyEvent event) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080048 return FocusHelper.handleHotseatButtonKeyEvent(v, keyCode, event);
Winson Chung4e6a9762011-05-09 11:56:34 -070049 }
50}
51
Tony Wickham0fa5ada2015-11-13 17:32:20 -080052/**
53 * A keyboard listener we set on full screen pages (e.g. custom content).
54 */
55class FullscreenKeyEventListener implements View.OnKeyListener {
56 @Override
57 public boolean onKey(View v, int keyCode, KeyEvent event) {
58 if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT
59 || keyCode == KeyEvent.KEYCODE_PAGE_DOWN || keyCode == KeyEvent.KEYCODE_PAGE_UP) {
60 // Handle the key event just like a workspace icon would in these cases. In this case,
61 // it will basically act as if there is a single icon in the top left (so you could
62 // think of the fullscreen page as a focusable fullscreen widget).
63 return FocusHelper.handleIconKeyEvent(v, keyCode, event);
64 }
65 return false;
66 }
67}
68
Sunny Goyale5986992018-05-23 10:51:37 -070069/**
70 * TODO: Reevaluate if this is still required
71 */
Winson Chung97d85d22011-04-13 11:27:36 -070072public class FocusHelper {
Winson Chung97d85d22011-04-13 11:27:36 -070073
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080074 private static final String TAG = "FocusHelper";
75 private static final boolean DEBUG = false;
76
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080077 /**
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070078 * Handles key events in paged folder.
Sunny Goyal290800b2015-03-05 11:33:33 -080079 */
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070080 public static class PagedFolderKeyEventListener implements View.OnKeyListener {
Sunny Goyal290800b2015-03-05 11:33:33 -080081
82 private final Folder mFolder;
83
84 public PagedFolderKeyEventListener(Folder folder) {
85 mFolder = folder;
86 }
87
88 @Override
Hyunyoung Songada50982015-04-10 14:35:23 -070089 public boolean onKey(View v, int keyCode, KeyEvent e) {
90 boolean consume = FocusLogic.shouldConsume(keyCode);
91 if (e.getAction() == KeyEvent.ACTION_UP) {
92 return consume;
Sunny Goyal290800b2015-03-05 11:33:33 -080093 }
Hyunyoung Songada50982015-04-10 14:35:23 -070094 if (DEBUG) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070095 Log.v(TAG, String.format("Handle ALL Folders keyevent=[%s].",
Hyunyoung Songada50982015-04-10 14:35:23 -070096 KeyEvent.keyCodeToString(keyCode)));
97 }
Sunny Goyal290800b2015-03-05 11:33:33 -080098
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070099 if (!(v.getParent() instanceof ShortcutAndWidgetContainer)) {
Zak Cohen3eeb41d2020-02-14 14:15:13 -0800100 if (FeatureFlags.IS_STUDIO_BUILD) {
Hyunyoung Songada50982015-04-10 14:35:23 -0700101 throw new IllegalStateException("Parent of the focused item is not supported.");
102 } else {
103 return false;
104 }
105 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800106
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700107 // Initialize variables.
108 final ShortcutAndWidgetContainer itemContainer = (ShortcutAndWidgetContainer) v.getParent();
109 final CellLayout cellLayout = (CellLayout) itemContainer.getParent();
Hyunyoung Songada50982015-04-10 14:35:23 -0700110
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700111 final int iconIndex = itemContainer.indexOfChild(v);
112 final FolderPagedView pagedView = (FolderPagedView) cellLayout.getParent();
113
114 final int pageIndex = pagedView.indexOfChild(cellLayout);
115 final int pageCount = pagedView.getPageCount();
Sunny Goyalc6205602015-05-21 20:46:33 -0700116 final boolean isLayoutRtl = Utilities.isRtl(v.getResources());
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700117
118 int[][] matrix = FocusLogic.createSparseMatrix(cellLayout);
Hyunyoung Songada50982015-04-10 14:35:23 -0700119 // Process focus.
Tony Wickham329d8bf2015-12-04 09:48:17 -0800120 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex,
121 pageCount, isLayoutRtl);
Hyunyoung Songada50982015-04-10 14:35:23 -0700122 if (newIconIndex == FocusLogic.NOOP) {
123 handleNoopKey(keyCode, v);
124 return consume;
125 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700126 ShortcutAndWidgetContainer newParent = null;
127 View child = null;
128
Hyunyoung Songada50982015-04-10 14:35:23 -0700129 switch (newIconIndex) {
130 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700131 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
132 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700133 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700134 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
135 pagedView.snapToPage(pageIndex - 1);
136 child = newParent.getChildAt(
137 ((newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN)
Tony Wickham329d8bf2015-12-04 09:48:17 -0800138 ^ newParent.invertLayoutHorizontally()) ? 0 : matrix.length - 1,
139 row);
Hyunyoung Songada50982015-04-10 14:35:23 -0700140 }
141 break;
142 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700143 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700144 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700145 pagedView.snapToPage(pageIndex - 1);
146 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700147 }
148 break;
149 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700150 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700151 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700152 pagedView.snapToPage(pageIndex - 1);
Tony Wickham329d8bf2015-12-04 09:48:17 -0800153 child = newParent.getChildAt(matrix.length - 1, matrix[0].length - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700154 }
155 break;
156 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700157 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700158 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700159 pagedView.snapToPage(pageIndex + 1);
160 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700161 }
162 break;
163 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700164 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
165 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700166 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700167 pagedView.snapToPage(pageIndex + 1);
Tony Wickham25189852015-11-04 17:44:32 -0800168 child = FocusLogic.getAdjacentChildInNextFolderPage(
169 newParent, v, newIconIndex);
Hyunyoung Songada50982015-04-10 14:35:23 -0700170 }
171 break;
172 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700173 child = cellLayout.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700174 break;
175 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700176 child = pagedView.getLastItem();
Hyunyoung Songada50982015-04-10 14:35:23 -0700177 break;
178 default: // Go to some item on the current page.
179 child = itemContainer.getChildAt(newIconIndex);
180 break;
181 }
182 if (child != null) {
183 child.requestFocus();
184 playSoundEffect(keyCode, v);
185 } else {
186 handleNoopKey(keyCode, v);
187 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800188 return consume;
189 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800190
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700191 public void handleNoopKey(int keyCode, View v) {
192 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
193 mFolder.mFolderName.requestFocus();
194 playSoundEffect(keyCode, v);
195 }
196 }
Sunny Goyal290800b2015-03-05 11:33:33 -0800197 }
198
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800199 /**
Tony Wickham4fc82872015-11-10 16:52:14 -0800200 * Handles key events in the workspace hotseat (bottom of the screen).
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800201 * <p>Currently we don't special case for the phone UI in different orientations, even though
202 * the hotseat is on the side in landscape mode. This is to ensure that accessibility
203 * consistency is maintained across rotations.
204 */
205 static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) {
206 boolean consume = FocusLogic.shouldConsume(keyCode);
207 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
208 return consume;
209 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800210
Tony2fd02082016-10-07 12:50:01 -0700211 final Launcher launcher = Launcher.getLauncher(v.getContext());
Winsonc0b52fe2015-09-09 16:38:15 -0700212 final DeviceProfile profile = launcher.getDeviceProfile();
Adam Cohen2e6da152015-05-06 11:42:25 -0700213
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800214 if (DEBUG) {
215 Log.v(TAG, String.format(
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700216 "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s",
217 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800218 }
219
220 // Initialize the variables.
Winsonfa56b3f2015-09-14 12:01:13 -0700221 final Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800222 final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent();
223 final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800224
Winsonfa56b3f2015-09-14 12:01:13 -0700225 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700226 int pageIndex = workspace.getNextPage();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800227 int pageCount = workspace.getChildCount();
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700228 int iconIndex = hotseatParent.indexOfChild(v);
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700229 int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets()
230 .getChildAt(iconIndex).getLayoutParams()).cellX;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800231
232 final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700233 if (iconLayout == null) {
234 // This check is to guard against cases where key strokes rushes in when workspace
235 // child creation/deletion is still in flux. (e.g., during drop or fling
236 // animation.)
237 return consume;
238 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800239 final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets();
240
241 ViewGroup parent = null;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800242 int[][] matrix = null;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800243
244 if (keyCode == KeyEvent.KEYCODE_DPAD_UP &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700245 !profile.isVerticalBarLayout()) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700246 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, profile);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800247 iconIndex += iconParent.getChildCount();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800248 parent = iconParent;
249 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700250 profile.isVerticalBarLayout()) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700251 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, profile);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800252 iconIndex += iconParent.getChildCount();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800253 parent = iconParent;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800254 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700255 profile.isVerticalBarLayout()) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800256 keyCode = KeyEvent.KEYCODE_PAGE_DOWN;
Winsonc0b52fe2015-09-09 16:38:15 -0700257 } else {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800258 // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the
259 // matrix extended with hotseat.
260 matrix = FocusLogic.createSparseMatrix(hotseatLayout);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800261 parent = hotseatParent;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800262 }
263
264 // Process the focus.
Tony Wickham329d8bf2015-12-04 09:48:17 -0800265 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex,
266 pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800267
Hyunyoung Song31178b82015-02-24 14:12:51 -0800268 View newIcon = null;
Tony Wickham4fc82872015-11-10 16:52:14 -0800269 switch (newIconIndex) {
270 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
271 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
272 newIcon = parent.getChildAt(0);
273 // TODO(hyunyoungs): handle cases where the child is not an icon but
274 // a folder or a widget.
275 workspace.snapToPage(pageIndex + 1);
276 break;
277 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
278 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
279 newIcon = parent.getChildAt(0);
280 // TODO(hyunyoungs): handle cases where the child is not an icon but
281 // a folder or a widget.
282 workspace.snapToPage(pageIndex - 1);
283 break;
284 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
285 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
286 newIcon = parent.getChildAt(parent.getChildCount() - 1);
287 // TODO(hyunyoungs): handle cases where the child is not an icon but
288 // a folder or a widget.
289 workspace.snapToPage(pageIndex - 1);
290 break;
291 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
292 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
293 // Go to the previous page but keep the focus on the same hotseat icon.
294 workspace.snapToPage(pageIndex - 1);
295 break;
296 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
297 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
298 // Go to the next page but keep the focus on the same hotseat icon.
299 workspace.snapToPage(pageIndex + 1);
300 break;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800301 }
302 if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800303 newIconIndex -= iconParent.getChildCount();
304 }
305 if (parent != null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800306 if (newIcon == null && newIconIndex >= 0) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800307 newIcon = parent.getChildAt(newIconIndex);
308 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800309 if (newIcon != null) {
310 newIcon.requestFocus();
311 playSoundEffect(keyCode, v);
312 }
313 }
314 return consume;
315 }
316
317 /**
318 * Handles key events in a workspace containing icons.
319 */
320 static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) {
321 boolean consume = FocusLogic.shouldConsume(keyCode);
322 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
323 return consume;
324 }
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700325
Tony2fd02082016-10-07 12:50:01 -0700326 Launcher launcher = Launcher.getLauncher(v.getContext());
Adam Cohen2e6da152015-05-06 11:42:25 -0700327 DeviceProfile profile = launcher.getDeviceProfile();
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700328
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800329 if (DEBUG) {
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700330 Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s",
331 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800332 }
333
334 // Initialize the variables.
335 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
Hyunyoung Song38531712015-03-03 19:25:16 -0800336 CellLayout iconLayout = (CellLayout) parent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800337 final Workspace workspace = (Workspace) iconLayout.getParent();
Adam Cohen2e6da152015-05-06 11:42:25 -0700338 final ViewGroup dragLayer = (ViewGroup) workspace.getParent();
Sunny Goyal47328fd2016-05-25 18:56:41 -0700339 final ViewGroup tabs = (ViewGroup) dragLayer.findViewById(R.id.drop_target_bar);
Adam Cohen2e6da152015-05-06 11:42:25 -0700340 final Hotseat hotseat = (Hotseat) dragLayer.findViewById(R.id.hotseat);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700341
Winsonfa56b3f2015-09-14 12:01:13 -0700342 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700343 final int iconIndex = parent.indexOfChild(v);
344 final int pageIndex = workspace.indexOfChild(iconLayout);
345 final int pageCount = workspace.getChildCount();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800346
347 CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0);
348 ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets();
349 int[][] matrix;
350
Hyunyoung Song31178b82015-02-24 14:12:51 -0800351 // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800352 // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended
353 // with the hotseat.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700354 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700355 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, profile);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800356 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700357 profile.isVerticalBarLayout()) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700358 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, profile);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800359 } else {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800360 matrix = FocusLogic.createSparseMatrix(iconLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800361 }
362
363 // Process the focus.
Tony Wickham329d8bf2015-12-04 09:48:17 -0800364 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex,
365 pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800366 boolean isRtl = Utilities.isRtl(v.getResources());
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800367 View newIcon = null;
Tony Wickhamaf78b592015-11-11 09:25:38 -0800368 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800369 switch (newIconIndex) {
370 case FocusLogic.NOOP:
371 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
372 newIcon = tabs;
373 }
374 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800375 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700376 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
377 int newPageIndex = pageIndex - 1;
378 if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) {
379 newPageIndex = pageIndex + 1;
380 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700381 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700382 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800383 if (parent != null) {
384 iconLayout = (CellLayout) parent.getParent();
Tony Wickham329d8bf2015-12-04 09:48:17 -0800385 matrix = FocusLogic.createSparseMatrixWithPivotColumn(iconLayout,
Tony Wickham4fc82872015-11-10 16:52:14 -0800386 iconLayout.getCountX(), row);
Tony Wickham329d8bf2015-12-04 09:48:17 -0800387 newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, FocusLogic.PIVOT,
388 newPageIndex, pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800389 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
390 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex,
391 isRtl);
392 } else if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LAST_ITEM) {
393 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex,
394 isRtl);
395 } else {
396 newIcon = parent.getChildAt(newIconIndex);
397 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800398 }
399 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800400 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800401 workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex - 1);
402 newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
403 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800404 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800405 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
406 workspace.snapToPage(pageIndex - 1);
407 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800408 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800409 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800410 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex, isRtl);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800411 break;
412 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800413 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex, isRtl);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800414 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800415 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700416 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
417 newPageIndex = pageIndex + 1;
418 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
419 newPageIndex = pageIndex - 1;
420 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700421 row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700422 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800423 if (parent != null) {
424 iconLayout = (CellLayout) parent.getParent();
Tony Wickham329d8bf2015-12-04 09:48:17 -0800425 matrix = FocusLogic.createSparseMatrixWithPivotColumn(iconLayout, -1, row);
426 newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, FocusLogic.PIVOT,
427 newPageIndex, pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800428 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
429 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex,
430 isRtl);
431 } else if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LAST_ITEM) {
432 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex,
433 isRtl);
434 } else {
435 newIcon = parent.getChildAt(newIconIndex);
436 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800437 }
438 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800439 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800440 newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
441 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800442 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800443 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
444 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800445 break;
446 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800447 newIcon = getFirstFocusableIconInReverseReadingOrder(workspaceLayout, isRtl);
448 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800449 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800450 newIcon = getFirstFocusableIconInReverseReadingOrder(hotseatLayout, isRtl);
451 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800452 break;
453 default:
454 // current page, some item.
455 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
456 newIcon = parent.getChildAt(newIconIndex);
457 } else if (parent.getChildCount() <= newIconIndex &&
458 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
459 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
460 }
461 break;
462 }
463 if (newIcon != null) {
464 newIcon.requestFocus();
465 playSoundEffect(keyCode, v);
466 }
467 return consume;
468 }
469
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800470 //
471 // Helper methods.
472 //
473
Winson Chung97d85d22011-04-13 11:27:36 -0700474 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700475 * Private helper method to get the CellLayoutChildren given a CellLayout index.
476 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700477 @Thunk static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
Michael Jurkaa52570f2012-03-20 03:18:20 -0700478 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700479 CellLayout parent = (CellLayout) container.getChildAt(i);
480 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700481 }
482
Winson Chung97d85d22011-04-13 11:27:36 -0700483 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800484 * Helper method to be used for playing sound effects.
Winson Chung97d85d22011-04-13 11:27:36 -0700485 */
Adam Cohen091440a2015-03-18 14:16:05 -0700486 @Thunk static void playSoundEffect(int keyCode, View v) {
Winson Chung97d85d22011-04-13 11:27:36 -0700487 switch (keyCode) {
488 case KeyEvent.KEYCODE_DPAD_LEFT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800489 v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
Winson Chung97d85d22011-04-13 11:27:36 -0700490 break;
491 case KeyEvent.KEYCODE_DPAD_RIGHT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800492 v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
Winson Chung97d85d22011-04-13 11:27:36 -0700493 break;
494 case KeyEvent.KEYCODE_DPAD_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700495 case KeyEvent.KEYCODE_PAGE_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700496 case KeyEvent.KEYCODE_MOVE_END:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800497 v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
Adam Cohenac56cff2011-09-28 20:45:37 -0700498 break;
499 case KeyEvent.KEYCODE_DPAD_UP:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800500 case KeyEvent.KEYCODE_PAGE_UP:
Adam Cohenac56cff2011-09-28 20:45:37 -0700501 case KeyEvent.KEYCODE_MOVE_HOME:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800502 v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
Adam Cohenac56cff2011-09-28 20:45:37 -0700503 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800504 default:
Winson Chung97d85d22011-04-13 11:27:36 -0700505 break;
Winson Chung97d85d22011-04-13 11:27:36 -0700506 }
Winson Chung97d85d22011-04-13 11:27:36 -0700507 }
Winsonfa56b3f2015-09-14 12:01:13 -0700508
Tony Wickhamaf78b592015-11-11 09:25:38 -0800509 private static View handlePreviousPageLastItem(Workspace workspace, CellLayout hotseatLayout,
510 int pageIndex, boolean isRtl) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800511 if (pageIndex - 1 < 0) {
512 return null;
513 }
Tony Wickhamaf78b592015-11-11 09:25:38 -0800514 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex - 1);
515 View newIcon = getFirstFocusableIconInReverseReadingOrder(workspaceLayout, isRtl);
516 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800517 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800518 newIcon = getFirstFocusableIconInReverseReadingOrder(hotseatLayout,isRtl);
519 workspace.snapToPage(pageIndex - 1);
520 }
521 return newIcon;
522 }
523
524 private static View handleNextPageFirstItem(Workspace workspace, CellLayout hotseatLayout,
525 int pageIndex, boolean isRtl) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800526 if (pageIndex + 1 >= workspace.getPageCount()) {
527 return null;
528 }
Tony Wickhamaf78b592015-11-11 09:25:38 -0800529 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex + 1);
530 View newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
531 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800532 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800533 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
534 workspace.snapToPage(pageIndex + 1);
535 }
536 return newIcon;
537 }
538
539 private static View getFirstFocusableIconInReadingOrder(CellLayout cellLayout, boolean isRtl) {
540 View icon;
541 int countX = cellLayout.getCountX();
542 for (int y = 0; y < cellLayout.getCountY(); y++) {
543 int increment = isRtl ? -1 : 1;
544 for (int x = isRtl ? countX - 1 : 0; 0 <= x && x < countX; x += increment) {
545 if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
546 return icon;
547 }
548 }
549 }
550 return null;
551 }
552
553 private static View getFirstFocusableIconInReverseReadingOrder(CellLayout cellLayout,
554 boolean isRtl) {
555 View icon;
556 int countX = cellLayout.getCountX();
557 for (int y = cellLayout.getCountY() - 1; y >= 0; y--) {
558 int increment = isRtl ? 1 : -1;
559 for (int x = isRtl ? 0 : countX - 1; 0 <= x && x < countX; x += increment) {
560 if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
561 return icon;
562 }
563 }
564 }
565 return null;
566 }
Winson Chung97d85d22011-04-13 11:27:36 -0700567}