blob: fe7acda17bf77923a9cb94d2bc016fc3a16798a2 [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;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080028import com.android.launcher3.util.FocusLogic;
Adam Cohen091440a2015-03-18 14:16:05 -070029import com.android.launcher3.util.Thunk;
Winson Chungfaa13252011-06-13 18:15:54 -070030
Winson Chung97d85d22011-04-13 11:27:36 -070031/**
Winson Chung4d279d92011-07-21 11:46:32 -070032 * A keyboard listener we set on all the workspace icons.
33 */
Adam Cohenac56cff2011-09-28 20:45:37 -070034class IconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080035 @Override
Winson Chung4d279d92011-07-21 11:46:32 -070036 public boolean onKey(View v, int keyCode, KeyEvent event) {
Adam Cohenac56cff2011-09-28 20:45:37 -070037 return FocusHelper.handleIconKeyEvent(v, keyCode, event);
38 }
39}
40
41/**
Winson Chung3d503fb2011-07-13 17:25:49 -070042 * A keyboard listener we set on all the hotseat buttons.
Winson Chung4e6a9762011-05-09 11:56:34 -070043 */
Adam Cohenac56cff2011-09-28 20:45:37 -070044class HotseatIconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080045 @Override
Winson Chung4e6a9762011-05-09 11:56:34 -070046 public boolean onKey(View v, int keyCode, KeyEvent event) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080047 return FocusHelper.handleHotseatButtonKeyEvent(v, keyCode, event);
Winson Chung4e6a9762011-05-09 11:56:34 -070048 }
49}
50
Tony Wickham0fa5ada2015-11-13 17:32:20 -080051/**
52 * A keyboard listener we set on full screen pages (e.g. custom content).
53 */
54class FullscreenKeyEventListener implements View.OnKeyListener {
55 @Override
56 public boolean onKey(View v, int keyCode, KeyEvent event) {
57 if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT
58 || keyCode == KeyEvent.KEYCODE_PAGE_DOWN || keyCode == KeyEvent.KEYCODE_PAGE_UP) {
59 // Handle the key event just like a workspace icon would in these cases. In this case,
60 // it will basically act as if there is a single icon in the top left (so you could
61 // think of the fullscreen page as a focusable fullscreen widget).
62 return FocusHelper.handleIconKeyEvent(v, keyCode, event);
63 }
64 return false;
65 }
66}
67
Winson Chung97d85d22011-04-13 11:27:36 -070068public class FocusHelper {
Winson Chung97d85d22011-04-13 11:27:36 -070069
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080070 private static final String TAG = "FocusHelper";
71 private static final boolean DEBUG = false;
72
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080073 /**
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070074 * Handles key events in paged folder.
Sunny Goyal290800b2015-03-05 11:33:33 -080075 */
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070076 public static class PagedFolderKeyEventListener implements View.OnKeyListener {
Sunny Goyal290800b2015-03-05 11:33:33 -080077
78 private final Folder mFolder;
79
80 public PagedFolderKeyEventListener(Folder folder) {
81 mFolder = folder;
82 }
83
84 @Override
Hyunyoung Songada50982015-04-10 14:35:23 -070085 public boolean onKey(View v, int keyCode, KeyEvent e) {
86 boolean consume = FocusLogic.shouldConsume(keyCode);
87 if (e.getAction() == KeyEvent.ACTION_UP) {
88 return consume;
Sunny Goyal290800b2015-03-05 11:33:33 -080089 }
Hyunyoung Songada50982015-04-10 14:35:23 -070090 if (DEBUG) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070091 Log.v(TAG, String.format("Handle ALL Folders keyevent=[%s].",
Hyunyoung Songada50982015-04-10 14:35:23 -070092 KeyEvent.keyCodeToString(keyCode)));
93 }
Sunny Goyal290800b2015-03-05 11:33:33 -080094
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070095 if (!(v.getParent() instanceof ShortcutAndWidgetContainer)) {
Sunny Goyal3d706ad2017-03-06 16:56:39 -080096 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Hyunyoung Songada50982015-04-10 14:35:23 -070097 throw new IllegalStateException("Parent of the focused item is not supported.");
98 } else {
99 return false;
100 }
101 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800102
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700103 // Initialize variables.
104 final ShortcutAndWidgetContainer itemContainer = (ShortcutAndWidgetContainer) v.getParent();
105 final CellLayout cellLayout = (CellLayout) itemContainer.getParent();
Hyunyoung Songada50982015-04-10 14:35:23 -0700106
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700107 final int iconIndex = itemContainer.indexOfChild(v);
108 final FolderPagedView pagedView = (FolderPagedView) cellLayout.getParent();
109
110 final int pageIndex = pagedView.indexOfChild(cellLayout);
111 final int pageCount = pagedView.getPageCount();
Sunny Goyalc6205602015-05-21 20:46:33 -0700112 final boolean isLayoutRtl = Utilities.isRtl(v.getResources());
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700113
114 int[][] matrix = FocusLogic.createSparseMatrix(cellLayout);
Hyunyoung Songada50982015-04-10 14:35:23 -0700115 // Process focus.
Tony Wickham329d8bf2015-12-04 09:48:17 -0800116 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex,
117 pageCount, isLayoutRtl);
Hyunyoung Songada50982015-04-10 14:35:23 -0700118 if (newIconIndex == FocusLogic.NOOP) {
119 handleNoopKey(keyCode, v);
120 return consume;
121 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700122 ShortcutAndWidgetContainer newParent = null;
123 View child = null;
124
Hyunyoung Songada50982015-04-10 14:35:23 -0700125 switch (newIconIndex) {
126 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700127 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
128 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700129 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700130 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
131 pagedView.snapToPage(pageIndex - 1);
132 child = newParent.getChildAt(
133 ((newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN)
Tony Wickham329d8bf2015-12-04 09:48:17 -0800134 ^ newParent.invertLayoutHorizontally()) ? 0 : matrix.length - 1,
135 row);
Hyunyoung Songada50982015-04-10 14:35:23 -0700136 }
137 break;
138 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700139 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700140 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700141 pagedView.snapToPage(pageIndex - 1);
142 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700143 }
144 break;
145 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700146 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700147 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700148 pagedView.snapToPage(pageIndex - 1);
Tony Wickham329d8bf2015-12-04 09:48:17 -0800149 child = newParent.getChildAt(matrix.length - 1, matrix[0].length - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700150 }
151 break;
152 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700153 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700154 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700155 pagedView.snapToPage(pageIndex + 1);
156 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700157 }
158 break;
159 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700160 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
161 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700162 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700163 pagedView.snapToPage(pageIndex + 1);
Tony Wickham25189852015-11-04 17:44:32 -0800164 child = FocusLogic.getAdjacentChildInNextFolderPage(
165 newParent, v, newIconIndex);
Hyunyoung Songada50982015-04-10 14:35:23 -0700166 }
167 break;
168 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700169 child = cellLayout.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700170 break;
171 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700172 child = pagedView.getLastItem();
Hyunyoung Songada50982015-04-10 14:35:23 -0700173 break;
174 default: // Go to some item on the current page.
175 child = itemContainer.getChildAt(newIconIndex);
176 break;
177 }
178 if (child != null) {
179 child.requestFocus();
180 playSoundEffect(keyCode, v);
181 } else {
182 handleNoopKey(keyCode, v);
183 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800184 return consume;
185 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800186
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700187 public void handleNoopKey(int keyCode, View v) {
188 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
189 mFolder.mFolderName.requestFocus();
190 playSoundEffect(keyCode, v);
191 }
192 }
Sunny Goyal290800b2015-03-05 11:33:33 -0800193 }
194
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800195 /**
Tony Wickham4fc82872015-11-10 16:52:14 -0800196 * Handles key events in the workspace hotseat (bottom of the screen).
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800197 * <p>Currently we don't special case for the phone UI in different orientations, even though
198 * the hotseat is on the side in landscape mode. This is to ensure that accessibility
199 * consistency is maintained across rotations.
200 */
201 static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) {
202 boolean consume = FocusLogic.shouldConsume(keyCode);
203 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
204 return consume;
205 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800206
Tony2fd02082016-10-07 12:50:01 -0700207 final Launcher launcher = Launcher.getLauncher(v.getContext());
Winsonc0b52fe2015-09-09 16:38:15 -0700208 final DeviceProfile profile = launcher.getDeviceProfile();
Adam Cohen2e6da152015-05-06 11:42:25 -0700209
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800210 if (DEBUG) {
211 Log.v(TAG, String.format(
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700212 "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s",
213 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800214 }
215
216 // Initialize the variables.
Winsonfa56b3f2015-09-14 12:01:13 -0700217 final Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800218 final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent();
219 final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800220
Winsonfa56b3f2015-09-14 12:01:13 -0700221 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700222 int pageIndex = workspace.getNextPage();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800223 int pageCount = workspace.getChildCount();
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700224 int iconIndex = hotseatParent.indexOfChild(v);
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700225 int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets()
226 .getChildAt(iconIndex).getLayoutParams()).cellX;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800227
228 final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700229 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 Songee3e6a72015-02-20 14:25:27 -0800235 final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets();
236
237 ViewGroup parent = null;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800238 int[][] matrix = null;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800239
240 if (keyCode == KeyEvent.KEYCODE_DPAD_UP &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700241 !profile.isVerticalBarLayout()) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700242 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, profile);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800243 iconIndex += iconParent.getChildCount();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800244 parent = iconParent;
245 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700246 profile.isVerticalBarLayout()) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700247 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, profile);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800248 iconIndex += iconParent.getChildCount();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800249 parent = iconParent;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800250 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700251 profile.isVerticalBarLayout()) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800252 keyCode = KeyEvent.KEYCODE_PAGE_DOWN;
Winsonc0b52fe2015-09-09 16:38:15 -0700253 } else {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800254 // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the
255 // matrix extended with hotseat.
256 matrix = FocusLogic.createSparseMatrix(hotseatLayout);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800257 parent = hotseatParent;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800258 }
259
260 // Process the focus.
Tony Wickham329d8bf2015-12-04 09:48:17 -0800261 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex,
262 pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800263
Hyunyoung Song31178b82015-02-24 14:12:51 -0800264 View newIcon = null;
Tony Wickham4fc82872015-11-10 16:52:14 -0800265 switch (newIconIndex) {
266 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
267 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
268 newIcon = parent.getChildAt(0);
269 // TODO(hyunyoungs): handle cases where the child is not an icon but
270 // a folder or a widget.
271 workspace.snapToPage(pageIndex + 1);
272 break;
273 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
274 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
275 newIcon = parent.getChildAt(0);
276 // TODO(hyunyoungs): handle cases where the child is not an icon but
277 // a folder or a widget.
278 workspace.snapToPage(pageIndex - 1);
279 break;
280 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
281 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
282 newIcon = parent.getChildAt(parent.getChildCount() - 1);
283 // TODO(hyunyoungs): handle cases where the child is not an icon but
284 // a folder or a widget.
285 workspace.snapToPage(pageIndex - 1);
286 break;
287 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
288 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
289 // Go to the previous page but keep the focus on the same hotseat icon.
290 workspace.snapToPage(pageIndex - 1);
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800291 // If the page we are going to is fullscreen, have it take the focus from hotseat.
292 CellLayout prevPage = (CellLayout) workspace.getPageAt(pageIndex - 1);
293 boolean isPrevPageFullscreen = ((CellLayout.LayoutParams) prevPage
294 .getShortcutsAndWidgets().getChildAt(0).getLayoutParams()).isFullscreen;
295 if (isPrevPageFullscreen) {
296 workspace.getPageAt(pageIndex - 1).requestFocus();
297 }
Tony Wickham4fc82872015-11-10 16:52:14 -0800298 break;
299 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
300 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
301 // Go to the next page but keep the focus on the same hotseat icon.
302 workspace.snapToPage(pageIndex + 1);
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800303 // If the page we are going to is fullscreen, have it take the focus from hotseat.
304 CellLayout nextPage = (CellLayout) workspace.getPageAt(pageIndex + 1);
305 boolean isNextPageFullscreen = ((CellLayout.LayoutParams) nextPage
306 .getShortcutsAndWidgets().getChildAt(0).getLayoutParams()).isFullscreen;
307 if (isNextPageFullscreen) {
308 workspace.getPageAt(pageIndex + 1).requestFocus();
309 }
Tony Wickham4fc82872015-11-10 16:52:14 -0800310 break;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800311 }
312 if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800313 newIconIndex -= iconParent.getChildCount();
314 }
315 if (parent != null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800316 if (newIcon == null && newIconIndex >= 0) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800317 newIcon = parent.getChildAt(newIconIndex);
318 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800319 if (newIcon != null) {
320 newIcon.requestFocus();
321 playSoundEffect(keyCode, v);
322 }
323 }
324 return consume;
325 }
326
327 /**
328 * Handles key events in a workspace containing icons.
329 */
330 static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) {
331 boolean consume = FocusLogic.shouldConsume(keyCode);
332 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
333 return consume;
334 }
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700335
Tony2fd02082016-10-07 12:50:01 -0700336 Launcher launcher = Launcher.getLauncher(v.getContext());
Adam Cohen2e6da152015-05-06 11:42:25 -0700337 DeviceProfile profile = launcher.getDeviceProfile();
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700338
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800339 if (DEBUG) {
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700340 Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s",
341 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800342 }
343
344 // Initialize the variables.
345 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
Hyunyoung Song38531712015-03-03 19:25:16 -0800346 CellLayout iconLayout = (CellLayout) parent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800347 final Workspace workspace = (Workspace) iconLayout.getParent();
Adam Cohen2e6da152015-05-06 11:42:25 -0700348 final ViewGroup dragLayer = (ViewGroup) workspace.getParent();
Sunny Goyal47328fd2016-05-25 18:56:41 -0700349 final ViewGroup tabs = (ViewGroup) dragLayer.findViewById(R.id.drop_target_bar);
Adam Cohen2e6da152015-05-06 11:42:25 -0700350 final Hotseat hotseat = (Hotseat) dragLayer.findViewById(R.id.hotseat);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700351
Winsonfa56b3f2015-09-14 12:01:13 -0700352 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700353 final int iconIndex = parent.indexOfChild(v);
354 final int pageIndex = workspace.indexOfChild(iconLayout);
355 final int pageCount = workspace.getChildCount();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800356
357 CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0);
358 ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets();
359 int[][] matrix;
360
Hyunyoung Song31178b82015-02-24 14:12:51 -0800361 // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800362 // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended
363 // with the hotseat.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700364 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700365 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, profile);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800366 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700367 profile.isVerticalBarLayout()) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700368 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, profile);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800369 } else {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800370 matrix = FocusLogic.createSparseMatrix(iconLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800371 }
372
373 // Process the focus.
Tony Wickham329d8bf2015-12-04 09:48:17 -0800374 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex,
375 pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800376 boolean isRtl = Utilities.isRtl(v.getResources());
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800377 View newIcon = null;
Tony Wickhamaf78b592015-11-11 09:25:38 -0800378 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800379 switch (newIconIndex) {
380 case FocusLogic.NOOP:
381 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
382 newIcon = tabs;
383 }
384 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800385 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700386 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
387 int newPageIndex = pageIndex - 1;
388 if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) {
389 newPageIndex = pageIndex + 1;
390 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700391 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700392 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800393 if (parent != null) {
394 iconLayout = (CellLayout) parent.getParent();
Tony Wickham329d8bf2015-12-04 09:48:17 -0800395 matrix = FocusLogic.createSparseMatrixWithPivotColumn(iconLayout,
Tony Wickham4fc82872015-11-10 16:52:14 -0800396 iconLayout.getCountX(), row);
Tony Wickham329d8bf2015-12-04 09:48:17 -0800397 newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, FocusLogic.PIVOT,
398 newPageIndex, pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800399 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
400 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex,
401 isRtl);
402 } else if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LAST_ITEM) {
403 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex,
404 isRtl);
405 } else {
406 newIcon = parent.getChildAt(newIconIndex);
407 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800408 }
409 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800410 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800411 workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex - 1);
412 newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
413 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800414 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800415 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
416 workspace.snapToPage(pageIndex - 1);
417 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800418 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800419 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800420 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex, isRtl);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800421 break;
422 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800423 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex, isRtl);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800424 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800425 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700426 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
427 newPageIndex = pageIndex + 1;
428 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
429 newPageIndex = pageIndex - 1;
430 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700431 row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700432 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800433 if (parent != null) {
434 iconLayout = (CellLayout) parent.getParent();
Tony Wickham329d8bf2015-12-04 09:48:17 -0800435 matrix = FocusLogic.createSparseMatrixWithPivotColumn(iconLayout, -1, row);
436 newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, FocusLogic.PIVOT,
437 newPageIndex, pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800438 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
439 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex,
440 isRtl);
441 } else if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LAST_ITEM) {
442 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex,
443 isRtl);
444 } else {
445 newIcon = parent.getChildAt(newIconIndex);
446 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800447 }
448 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800449 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800450 newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
451 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800452 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800453 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
454 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800455 break;
456 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800457 newIcon = getFirstFocusableIconInReverseReadingOrder(workspaceLayout, isRtl);
458 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800459 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800460 newIcon = getFirstFocusableIconInReverseReadingOrder(hotseatLayout, isRtl);
461 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800462 break;
463 default:
464 // current page, some item.
465 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
466 newIcon = parent.getChildAt(newIconIndex);
467 } else if (parent.getChildCount() <= newIconIndex &&
468 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
469 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
470 }
471 break;
472 }
473 if (newIcon != null) {
474 newIcon.requestFocus();
475 playSoundEffect(keyCode, v);
476 }
477 return consume;
478 }
479
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800480 //
481 // Helper methods.
482 //
483
Winson Chung97d85d22011-04-13 11:27:36 -0700484 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700485 * Private helper method to get the CellLayoutChildren given a CellLayout index.
486 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700487 @Thunk static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
Michael Jurkaa52570f2012-03-20 03:18:20 -0700488 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700489 CellLayout parent = (CellLayout) container.getChildAt(i);
490 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700491 }
492
Winson Chung97d85d22011-04-13 11:27:36 -0700493 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800494 * Helper method to be used for playing sound effects.
Winson Chung97d85d22011-04-13 11:27:36 -0700495 */
Adam Cohen091440a2015-03-18 14:16:05 -0700496 @Thunk static void playSoundEffect(int keyCode, View v) {
Winson Chung97d85d22011-04-13 11:27:36 -0700497 switch (keyCode) {
498 case KeyEvent.KEYCODE_DPAD_LEFT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800499 v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
Winson Chung97d85d22011-04-13 11:27:36 -0700500 break;
501 case KeyEvent.KEYCODE_DPAD_RIGHT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800502 v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
Winson Chung97d85d22011-04-13 11:27:36 -0700503 break;
504 case KeyEvent.KEYCODE_DPAD_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700505 case KeyEvent.KEYCODE_PAGE_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700506 case KeyEvent.KEYCODE_MOVE_END:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800507 v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
Adam Cohenac56cff2011-09-28 20:45:37 -0700508 break;
509 case KeyEvent.KEYCODE_DPAD_UP:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800510 case KeyEvent.KEYCODE_PAGE_UP:
Adam Cohenac56cff2011-09-28 20:45:37 -0700511 case KeyEvent.KEYCODE_MOVE_HOME:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800512 v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
Adam Cohenac56cff2011-09-28 20:45:37 -0700513 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800514 default:
Winson Chung97d85d22011-04-13 11:27:36 -0700515 break;
Winson Chung97d85d22011-04-13 11:27:36 -0700516 }
Winson Chung97d85d22011-04-13 11:27:36 -0700517 }
Winsonfa56b3f2015-09-14 12:01:13 -0700518
Tony Wickhamaf78b592015-11-11 09:25:38 -0800519 private static View handlePreviousPageLastItem(Workspace workspace, CellLayout hotseatLayout,
520 int pageIndex, boolean isRtl) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800521 if (pageIndex - 1 < 0) {
522 return null;
523 }
Tony Wickhamaf78b592015-11-11 09:25:38 -0800524 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex - 1);
525 View newIcon = getFirstFocusableIconInReverseReadingOrder(workspaceLayout, isRtl);
526 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800527 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800528 newIcon = getFirstFocusableIconInReverseReadingOrder(hotseatLayout,isRtl);
529 workspace.snapToPage(pageIndex - 1);
530 }
531 return newIcon;
532 }
533
534 private static View handleNextPageFirstItem(Workspace workspace, CellLayout hotseatLayout,
535 int pageIndex, boolean isRtl) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800536 if (pageIndex + 1 >= workspace.getPageCount()) {
537 return null;
538 }
Tony Wickhamaf78b592015-11-11 09:25:38 -0800539 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex + 1);
540 View newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
541 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800542 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800543 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
544 workspace.snapToPage(pageIndex + 1);
545 }
546 return newIcon;
547 }
548
549 private static View getFirstFocusableIconInReadingOrder(CellLayout cellLayout, boolean isRtl) {
550 View icon;
551 int countX = cellLayout.getCountX();
552 for (int y = 0; y < cellLayout.getCountY(); y++) {
553 int increment = isRtl ? -1 : 1;
554 for (int x = isRtl ? countX - 1 : 0; 0 <= x && x < countX; x += increment) {
555 if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
556 return icon;
557 }
558 }
559 }
560 return null;
561 }
562
563 private static View getFirstFocusableIconInReverseReadingOrder(CellLayout cellLayout,
564 boolean isRtl) {
565 View icon;
566 int countX = cellLayout.getCountX();
567 for (int y = cellLayout.getCountY() - 1; y >= 0; y--) {
568 int increment = isRtl ? 1 : -1;
569 for (int x = isRtl ? 0 : countX - 1; 0 <= x && x < countX; x += increment) {
570 if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
571 return icon;
572 }
573 }
574 }
575 return null;
576 }
Winson Chung97d85d22011-04-13 11:27:36 -0700577}