blob: f07040db03cd1ee9a4173ed619cda8936f22bcac [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
Sunny Goyale5986992018-05-23 10:51:37 -070068/**
69 * TODO: Reevaluate if this is still required
70 */
Winson Chung97d85d22011-04-13 11:27:36 -070071public class FocusHelper {
Winson Chung97d85d22011-04-13 11:27:36 -070072
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080073 private static final String TAG = "FocusHelper";
74 private static final boolean DEBUG = false;
75
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080076 /**
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070077 * Handles key events in paged folder.
Sunny Goyal290800b2015-03-05 11:33:33 -080078 */
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070079 public static class PagedFolderKeyEventListener implements View.OnKeyListener {
Sunny Goyal290800b2015-03-05 11:33:33 -080080
81 private final Folder mFolder;
82
83 public PagedFolderKeyEventListener(Folder folder) {
84 mFolder = folder;
85 }
86
87 @Override
Hyunyoung Songada50982015-04-10 14:35:23 -070088 public boolean onKey(View v, int keyCode, KeyEvent e) {
89 boolean consume = FocusLogic.shouldConsume(keyCode);
90 if (e.getAction() == KeyEvent.ACTION_UP) {
91 return consume;
Sunny Goyal290800b2015-03-05 11:33:33 -080092 }
Hyunyoung Songada50982015-04-10 14:35:23 -070093 if (DEBUG) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070094 Log.v(TAG, String.format("Handle ALL Folders keyevent=[%s].",
Hyunyoung Songada50982015-04-10 14:35:23 -070095 KeyEvent.keyCodeToString(keyCode)));
96 }
Sunny Goyal290800b2015-03-05 11:33:33 -080097
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070098 if (!(v.getParent() instanceof ShortcutAndWidgetContainer)) {
Zak Cohen3eeb41d2020-02-14 14:15:13 -080099 if (FeatureFlags.IS_STUDIO_BUILD) {
Hyunyoung Songada50982015-04-10 14:35:23 -0700100 throw new IllegalStateException("Parent of the focused item is not supported.");
101 } else {
102 return false;
103 }
104 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800105
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700106 // Initialize variables.
107 final ShortcutAndWidgetContainer itemContainer = (ShortcutAndWidgetContainer) v.getParent();
108 final CellLayout cellLayout = (CellLayout) itemContainer.getParent();
Hyunyoung Songada50982015-04-10 14:35:23 -0700109
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700110 final int iconIndex = itemContainer.indexOfChild(v);
111 final FolderPagedView pagedView = (FolderPagedView) cellLayout.getParent();
112
113 final int pageIndex = pagedView.indexOfChild(cellLayout);
114 final int pageCount = pagedView.getPageCount();
Sunny Goyalc6205602015-05-21 20:46:33 -0700115 final boolean isLayoutRtl = Utilities.isRtl(v.getResources());
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700116
117 int[][] matrix = FocusLogic.createSparseMatrix(cellLayout);
Hyunyoung Songada50982015-04-10 14:35:23 -0700118 // Process focus.
Tony Wickham329d8bf2015-12-04 09:48:17 -0800119 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex,
120 pageCount, isLayoutRtl);
Hyunyoung Songada50982015-04-10 14:35:23 -0700121 if (newIconIndex == FocusLogic.NOOP) {
122 handleNoopKey(keyCode, v);
123 return consume;
124 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700125 ShortcutAndWidgetContainer newParent = null;
126 View child = null;
127
Hyunyoung Songada50982015-04-10 14:35:23 -0700128 switch (newIconIndex) {
129 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700130 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
131 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700132 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700133 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
134 pagedView.snapToPage(pageIndex - 1);
135 child = newParent.getChildAt(
136 ((newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN)
Tony Wickham329d8bf2015-12-04 09:48:17 -0800137 ^ newParent.invertLayoutHorizontally()) ? 0 : matrix.length - 1,
138 row);
Hyunyoung Songada50982015-04-10 14:35:23 -0700139 }
140 break;
141 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700142 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700143 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700144 pagedView.snapToPage(pageIndex - 1);
145 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700146 }
147 break;
148 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700149 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700150 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700151 pagedView.snapToPage(pageIndex - 1);
Tony Wickham329d8bf2015-12-04 09:48:17 -0800152 child = newParent.getChildAt(matrix.length - 1, matrix[0].length - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700153 }
154 break;
155 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700156 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700157 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700158 pagedView.snapToPage(pageIndex + 1);
159 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700160 }
161 break;
162 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700163 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
164 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700165 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700166 pagedView.snapToPage(pageIndex + 1);
Tony Wickham25189852015-11-04 17:44:32 -0800167 child = FocusLogic.getAdjacentChildInNextFolderPage(
168 newParent, v, newIconIndex);
Hyunyoung Songada50982015-04-10 14:35:23 -0700169 }
170 break;
171 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700172 child = cellLayout.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700173 break;
174 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700175 child = pagedView.getLastItem();
Hyunyoung Songada50982015-04-10 14:35:23 -0700176 break;
177 default: // Go to some item on the current page.
178 child = itemContainer.getChildAt(newIconIndex);
179 break;
180 }
181 if (child != null) {
182 child.requestFocus();
183 playSoundEffect(keyCode, v);
184 } else {
185 handleNoopKey(keyCode, v);
186 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800187 return consume;
188 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800189
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700190 public void handleNoopKey(int keyCode, View v) {
191 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
192 mFolder.mFolderName.requestFocus();
193 playSoundEffect(keyCode, v);
194 }
195 }
Sunny Goyal290800b2015-03-05 11:33:33 -0800196 }
197
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800198 /**
Tony Wickham4fc82872015-11-10 16:52:14 -0800199 * Handles key events in the workspace hotseat (bottom of the screen).
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800200 * <p>Currently we don't special case for the phone UI in different orientations, even though
201 * the hotseat is on the side in landscape mode. This is to ensure that accessibility
202 * consistency is maintained across rotations.
203 */
204 static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) {
205 boolean consume = FocusLogic.shouldConsume(keyCode);
206 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
207 return consume;
208 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800209
Tony2fd02082016-10-07 12:50:01 -0700210 final Launcher launcher = Launcher.getLauncher(v.getContext());
Winsonc0b52fe2015-09-09 16:38:15 -0700211 final DeviceProfile profile = launcher.getDeviceProfile();
Adam Cohen2e6da152015-05-06 11:42:25 -0700212
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800213 if (DEBUG) {
214 Log.v(TAG, String.format(
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700215 "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s",
216 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800217 }
218
219 // Initialize the variables.
Winsonfa56b3f2015-09-14 12:01:13 -0700220 final Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800221 final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent();
222 final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800223
Winsonfa56b3f2015-09-14 12:01:13 -0700224 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700225 int pageIndex = workspace.getNextPage();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800226 int pageCount = workspace.getChildCount();
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700227 int iconIndex = hotseatParent.indexOfChild(v);
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700228 int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets()
229 .getChildAt(iconIndex).getLayoutParams()).cellX;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800230
231 final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700232 if (iconLayout == null) {
233 // This check is to guard against cases where key strokes rushes in when workspace
234 // child creation/deletion is still in flux. (e.g., during drop or fling
235 // animation.)
236 return consume;
237 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800238 final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets();
239
240 ViewGroup parent = null;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800241 int[][] matrix = null;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800242
243 if (keyCode == KeyEvent.KEYCODE_DPAD_UP &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700244 !profile.isVerticalBarLayout()) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700245 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, profile);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800246 iconIndex += iconParent.getChildCount();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800247 parent = iconParent;
248 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700249 profile.isVerticalBarLayout()) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700250 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, profile);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800251 iconIndex += iconParent.getChildCount();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800252 parent = iconParent;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800253 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700254 profile.isVerticalBarLayout()) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800255 keyCode = KeyEvent.KEYCODE_PAGE_DOWN;
Winsonc0b52fe2015-09-09 16:38:15 -0700256 } else {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800257 // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the
258 // matrix extended with hotseat.
259 matrix = FocusLogic.createSparseMatrix(hotseatLayout);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800260 parent = hotseatParent;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800261 }
262
263 // Process the focus.
Tony Wickham329d8bf2015-12-04 09:48:17 -0800264 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex,
265 pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800266
Hyunyoung Song31178b82015-02-24 14:12:51 -0800267 View newIcon = null;
Tony Wickham4fc82872015-11-10 16:52:14 -0800268 switch (newIconIndex) {
269 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
270 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
271 newIcon = parent.getChildAt(0);
272 // TODO(hyunyoungs): handle cases where the child is not an icon but
273 // a folder or a widget.
274 workspace.snapToPage(pageIndex + 1);
275 break;
276 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
277 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
278 newIcon = parent.getChildAt(0);
279 // TODO(hyunyoungs): handle cases where the child is not an icon but
280 // a folder or a widget.
281 workspace.snapToPage(pageIndex - 1);
282 break;
283 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
284 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
285 newIcon = parent.getChildAt(parent.getChildCount() - 1);
286 // TODO(hyunyoungs): handle cases where the child is not an icon but
287 // a folder or a widget.
288 workspace.snapToPage(pageIndex - 1);
289 break;
290 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
291 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
292 // Go to the previous page but keep the focus on the same hotseat icon.
293 workspace.snapToPage(pageIndex - 1);
294 break;
295 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
296 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
297 // Go to the next page but keep the focus on the same hotseat icon.
298 workspace.snapToPage(pageIndex + 1);
299 break;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800300 }
301 if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800302 newIconIndex -= iconParent.getChildCount();
303 }
304 if (parent != null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800305 if (newIcon == null && newIconIndex >= 0) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800306 newIcon = parent.getChildAt(newIconIndex);
307 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800308 if (newIcon != null) {
309 newIcon.requestFocus();
310 playSoundEffect(keyCode, v);
311 }
312 }
313 return consume;
314 }
315
316 /**
317 * Handles key events in a workspace containing icons.
318 */
319 static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) {
320 boolean consume = FocusLogic.shouldConsume(keyCode);
321 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
322 return consume;
323 }
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700324
Tony2fd02082016-10-07 12:50:01 -0700325 Launcher launcher = Launcher.getLauncher(v.getContext());
Adam Cohen2e6da152015-05-06 11:42:25 -0700326 DeviceProfile profile = launcher.getDeviceProfile();
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700327
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800328 if (DEBUG) {
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700329 Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s",
330 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800331 }
332
333 // Initialize the variables.
334 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
Hyunyoung Song38531712015-03-03 19:25:16 -0800335 CellLayout iconLayout = (CellLayout) parent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800336 final Workspace workspace = (Workspace) iconLayout.getParent();
Adam Cohen2e6da152015-05-06 11:42:25 -0700337 final ViewGroup dragLayer = (ViewGroup) workspace.getParent();
Sunny Goyal47328fd2016-05-25 18:56:41 -0700338 final ViewGroup tabs = (ViewGroup) dragLayer.findViewById(R.id.drop_target_bar);
Adam Cohen2e6da152015-05-06 11:42:25 -0700339 final Hotseat hotseat = (Hotseat) dragLayer.findViewById(R.id.hotseat);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700340
Winsonfa56b3f2015-09-14 12:01:13 -0700341 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700342 final int iconIndex = parent.indexOfChild(v);
343 final int pageIndex = workspace.indexOfChild(iconLayout);
344 final int pageCount = workspace.getChildCount();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800345
346 CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0);
347 ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets();
348 int[][] matrix;
349
Hyunyoung Song31178b82015-02-24 14:12:51 -0800350 // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800351 // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended
352 // with the hotseat.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700353 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700354 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, profile);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800355 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700356 profile.isVerticalBarLayout()) {
Sunny Goyalbb011da2016-06-15 15:42:29 -0700357 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout, profile);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800358 } else {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800359 matrix = FocusLogic.createSparseMatrix(iconLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800360 }
361
362 // Process the focus.
Tony Wickham329d8bf2015-12-04 09:48:17 -0800363 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex,
364 pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800365 boolean isRtl = Utilities.isRtl(v.getResources());
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800366 View newIcon = null;
Tony Wickhamaf78b592015-11-11 09:25:38 -0800367 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800368 switch (newIconIndex) {
369 case FocusLogic.NOOP:
370 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
371 newIcon = tabs;
372 }
373 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800374 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700375 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
376 int newPageIndex = pageIndex - 1;
377 if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) {
378 newPageIndex = pageIndex + 1;
379 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700380 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700381 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800382 if (parent != null) {
383 iconLayout = (CellLayout) parent.getParent();
Tony Wickham329d8bf2015-12-04 09:48:17 -0800384 matrix = FocusLogic.createSparseMatrixWithPivotColumn(iconLayout,
Tony Wickham4fc82872015-11-10 16:52:14 -0800385 iconLayout.getCountX(), row);
Tony Wickham329d8bf2015-12-04 09:48:17 -0800386 newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, FocusLogic.PIVOT,
387 newPageIndex, pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800388 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
389 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex,
390 isRtl);
391 } else if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LAST_ITEM) {
392 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex,
393 isRtl);
394 } else {
395 newIcon = parent.getChildAt(newIconIndex);
396 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800397 }
398 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800399 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800400 workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex - 1);
401 newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
402 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800403 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800404 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
405 workspace.snapToPage(pageIndex - 1);
406 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800407 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800408 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800409 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex, isRtl);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800410 break;
411 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800412 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex, isRtl);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800413 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800414 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700415 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
416 newPageIndex = pageIndex + 1;
417 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
418 newPageIndex = pageIndex - 1;
419 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700420 row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700421 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800422 if (parent != null) {
423 iconLayout = (CellLayout) parent.getParent();
Tony Wickham329d8bf2015-12-04 09:48:17 -0800424 matrix = FocusLogic.createSparseMatrixWithPivotColumn(iconLayout, -1, row);
425 newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, FocusLogic.PIVOT,
426 newPageIndex, pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800427 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
428 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex,
429 isRtl);
430 } else if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LAST_ITEM) {
431 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex,
432 isRtl);
433 } else {
434 newIcon = parent.getChildAt(newIconIndex);
435 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800436 }
437 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800438 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800439 newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
440 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800441 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800442 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
443 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800444 break;
445 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800446 newIcon = getFirstFocusableIconInReverseReadingOrder(workspaceLayout, isRtl);
447 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800448 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800449 newIcon = getFirstFocusableIconInReverseReadingOrder(hotseatLayout, isRtl);
450 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800451 break;
452 default:
453 // current page, some item.
454 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
455 newIcon = parent.getChildAt(newIconIndex);
456 } else if (parent.getChildCount() <= newIconIndex &&
457 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
458 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
459 }
460 break;
461 }
462 if (newIcon != null) {
463 newIcon.requestFocus();
464 playSoundEffect(keyCode, v);
465 }
466 return consume;
467 }
468
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800469 //
470 // Helper methods.
471 //
472
Winson Chung97d85d22011-04-13 11:27:36 -0700473 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700474 * Private helper method to get the CellLayoutChildren given a CellLayout index.
475 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700476 @Thunk static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
Michael Jurkaa52570f2012-03-20 03:18:20 -0700477 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700478 CellLayout parent = (CellLayout) container.getChildAt(i);
479 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700480 }
481
Winson Chung97d85d22011-04-13 11:27:36 -0700482 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800483 * Helper method to be used for playing sound effects.
Winson Chung97d85d22011-04-13 11:27:36 -0700484 */
Adam Cohen091440a2015-03-18 14:16:05 -0700485 @Thunk static void playSoundEffect(int keyCode, View v) {
Winson Chung97d85d22011-04-13 11:27:36 -0700486 switch (keyCode) {
487 case KeyEvent.KEYCODE_DPAD_LEFT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800488 v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
Winson Chung97d85d22011-04-13 11:27:36 -0700489 break;
490 case KeyEvent.KEYCODE_DPAD_RIGHT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800491 v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
Winson Chung97d85d22011-04-13 11:27:36 -0700492 break;
493 case KeyEvent.KEYCODE_DPAD_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700494 case KeyEvent.KEYCODE_PAGE_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700495 case KeyEvent.KEYCODE_MOVE_END:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800496 v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
Adam Cohenac56cff2011-09-28 20:45:37 -0700497 break;
498 case KeyEvent.KEYCODE_DPAD_UP:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800499 case KeyEvent.KEYCODE_PAGE_UP:
Adam Cohenac56cff2011-09-28 20:45:37 -0700500 case KeyEvent.KEYCODE_MOVE_HOME:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800501 v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
Adam Cohenac56cff2011-09-28 20:45:37 -0700502 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800503 default:
Winson Chung97d85d22011-04-13 11:27:36 -0700504 break;
Winson Chung97d85d22011-04-13 11:27:36 -0700505 }
Winson Chung97d85d22011-04-13 11:27:36 -0700506 }
Winsonfa56b3f2015-09-14 12:01:13 -0700507
Tony Wickhamaf78b592015-11-11 09:25:38 -0800508 private static View handlePreviousPageLastItem(Workspace workspace, CellLayout hotseatLayout,
509 int pageIndex, boolean isRtl) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800510 if (pageIndex - 1 < 0) {
511 return null;
512 }
Tony Wickhamaf78b592015-11-11 09:25:38 -0800513 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex - 1);
514 View newIcon = getFirstFocusableIconInReverseReadingOrder(workspaceLayout, isRtl);
515 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800516 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800517 newIcon = getFirstFocusableIconInReverseReadingOrder(hotseatLayout,isRtl);
518 workspace.snapToPage(pageIndex - 1);
519 }
520 return newIcon;
521 }
522
523 private static View handleNextPageFirstItem(Workspace workspace, CellLayout hotseatLayout,
524 int pageIndex, boolean isRtl) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800525 if (pageIndex + 1 >= workspace.getPageCount()) {
526 return null;
527 }
Tony Wickhamaf78b592015-11-11 09:25:38 -0800528 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex + 1);
529 View newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
530 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800531 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800532 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
533 workspace.snapToPage(pageIndex + 1);
534 }
535 return newIcon;
536 }
537
538 private static View getFirstFocusableIconInReadingOrder(CellLayout cellLayout, boolean isRtl) {
539 View icon;
540 int countX = cellLayout.getCountX();
541 for (int y = 0; y < cellLayout.getCountY(); y++) {
542 int increment = isRtl ? -1 : 1;
543 for (int x = isRtl ? countX - 1 : 0; 0 <= x && x < countX; x += increment) {
544 if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
545 return icon;
546 }
547 }
548 }
549 return null;
550 }
551
552 private static View getFirstFocusableIconInReverseReadingOrder(CellLayout cellLayout,
553 boolean isRtl) {
554 View icon;
555 int countX = cellLayout.getCountX();
556 for (int y = cellLayout.getCountY() - 1; y >= 0; y--) {
557 int increment = isRtl ? 1 : -1;
558 for (int x = isRtl ? 0 : countX - 1; 0 <= x && x < countX; x += increment) {
559 if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
560 return icon;
561 }
562 }
563 }
564 return null;
565 }
Winson Chung97d85d22011-04-13 11:27:36 -0700566}