blob: 3751b881ba7ef9ab40bd336269a1c50526fe74a5 [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 Goyal6c56c682015-07-16 14:09:05 -070025import com.android.launcher3.config.ProviderConfig;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080026import com.android.launcher3.util.FocusLogic;
Adam Cohen091440a2015-03-18 14:16:05 -070027import com.android.launcher3.util.Thunk;
Winson Chungfaa13252011-06-13 18:15:54 -070028
Winson Chung97d85d22011-04-13 11:27:36 -070029/**
Winson Chung4d279d92011-07-21 11:46:32 -070030 * A keyboard listener we set on all the workspace icons.
31 */
Adam Cohenac56cff2011-09-28 20:45:37 -070032class IconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080033 @Override
Winson Chung4d279d92011-07-21 11:46:32 -070034 public boolean onKey(View v, int keyCode, KeyEvent event) {
Adam Cohenac56cff2011-09-28 20:45:37 -070035 return FocusHelper.handleIconKeyEvent(v, keyCode, event);
36 }
37}
38
39/**
Winson Chung3d503fb2011-07-13 17:25:49 -070040 * A keyboard listener we set on all the hotseat buttons.
Winson Chung4e6a9762011-05-09 11:56:34 -070041 */
Adam Cohenac56cff2011-09-28 20:45:37 -070042class HotseatIconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080043 @Override
Winson Chung4e6a9762011-05-09 11:56:34 -070044 public boolean onKey(View v, int keyCode, KeyEvent event) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080045 return FocusHelper.handleHotseatButtonKeyEvent(v, keyCode, event);
Winson Chung4e6a9762011-05-09 11:56:34 -070046 }
47}
48
Winson Chung97d85d22011-04-13 11:27:36 -070049public class FocusHelper {
Winson Chung97d85d22011-04-13 11:27:36 -070050
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080051 private static final String TAG = "FocusHelper";
52 private static final boolean DEBUG = false;
53
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080054 /**
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070055 * Handles key events in paged folder.
Sunny Goyal290800b2015-03-05 11:33:33 -080056 */
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070057 public static class PagedFolderKeyEventListener implements View.OnKeyListener {
Sunny Goyal290800b2015-03-05 11:33:33 -080058
59 private final Folder mFolder;
60
61 public PagedFolderKeyEventListener(Folder folder) {
62 mFolder = folder;
63 }
64
65 @Override
Hyunyoung Songada50982015-04-10 14:35:23 -070066 public boolean onKey(View v, int keyCode, KeyEvent e) {
67 boolean consume = FocusLogic.shouldConsume(keyCode);
68 if (e.getAction() == KeyEvent.ACTION_UP) {
69 return consume;
Sunny Goyal290800b2015-03-05 11:33:33 -080070 }
Hyunyoung Songada50982015-04-10 14:35:23 -070071 if (DEBUG) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070072 Log.v(TAG, String.format("Handle ALL Folders keyevent=[%s].",
Hyunyoung Songada50982015-04-10 14:35:23 -070073 KeyEvent.keyCodeToString(keyCode)));
74 }
Sunny Goyal290800b2015-03-05 11:33:33 -080075
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070076
77 if (!(v.getParent() instanceof ShortcutAndWidgetContainer)) {
Sunny Goyal6c56c682015-07-16 14:09:05 -070078 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Hyunyoung Songada50982015-04-10 14:35:23 -070079 throw new IllegalStateException("Parent of the focused item is not supported.");
80 } else {
81 return false;
82 }
83 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080084
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070085 // Initialize variables.
86 final ShortcutAndWidgetContainer itemContainer = (ShortcutAndWidgetContainer) v.getParent();
87 final CellLayout cellLayout = (CellLayout) itemContainer.getParent();
88 final int countX = cellLayout.getCountX();
89 final int countY = cellLayout.getCountY();
Hyunyoung Songada50982015-04-10 14:35:23 -070090
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070091 final int iconIndex = itemContainer.indexOfChild(v);
92 final FolderPagedView pagedView = (FolderPagedView) cellLayout.getParent();
93
94 final int pageIndex = pagedView.indexOfChild(cellLayout);
95 final int pageCount = pagedView.getPageCount();
Sunny Goyalc6205602015-05-21 20:46:33 -070096 final boolean isLayoutRtl = Utilities.isRtl(v.getResources());
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070097
98 int[][] matrix = FocusLogic.createSparseMatrix(cellLayout);
Hyunyoung Songada50982015-04-10 14:35:23 -070099 // Process focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700100 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700101 countY, matrix, iconIndex, pageIndex, pageCount, isLayoutRtl);
Hyunyoung Songada50982015-04-10 14:35:23 -0700102 if (newIconIndex == FocusLogic.NOOP) {
103 handleNoopKey(keyCode, v);
104 return consume;
105 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700106 ShortcutAndWidgetContainer newParent = null;
107 View child = null;
108
Hyunyoung Songada50982015-04-10 14:35:23 -0700109 switch (newIconIndex) {
110 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700111 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
112 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700113 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700114 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
115 pagedView.snapToPage(pageIndex - 1);
116 child = newParent.getChildAt(
117 ((newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN)
118 ^ newParent.invertLayoutHorizontally()) ? 0 : countX - 1, row);
Hyunyoung Songada50982015-04-10 14:35:23 -0700119 }
120 break;
121 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700122 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700123 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700124 pagedView.snapToPage(pageIndex - 1);
125 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700126 }
127 break;
128 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700129 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700130 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700131 pagedView.snapToPage(pageIndex - 1);
132 child = newParent.getChildAt(countX - 1, countY - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700133 }
134 break;
135 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700136 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700137 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700138 pagedView.snapToPage(pageIndex + 1);
139 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700140 }
141 break;
142 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700143 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
144 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700145 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700146 pagedView.snapToPage(pageIndex + 1);
147 child = FocusLogic.getAdjacentChildInNextPage(newParent, v, newIconIndex);
Hyunyoung Songada50982015-04-10 14:35:23 -0700148 }
149 break;
150 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700151 child = cellLayout.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700152 break;
153 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700154 child = pagedView.getLastItem();
Hyunyoung Songada50982015-04-10 14:35:23 -0700155 break;
156 default: // Go to some item on the current page.
157 child = itemContainer.getChildAt(newIconIndex);
158 break;
159 }
160 if (child != null) {
161 child.requestFocus();
162 playSoundEffect(keyCode, v);
163 } else {
164 handleNoopKey(keyCode, v);
165 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800166 return consume;
167 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800168
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700169 public void handleNoopKey(int keyCode, View v) {
170 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
171 mFolder.mFolderName.requestFocus();
172 playSoundEffect(keyCode, v);
173 }
174 }
Sunny Goyal290800b2015-03-05 11:33:33 -0800175 }
176
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800177 /**
178 * Handles key events in the workspace hot seat (bottom of the screen).
179 * <p>Currently we don't special case for the phone UI in different orientations, even though
180 * the hotseat is on the side in landscape mode. This is to ensure that accessibility
181 * consistency is maintained across rotations.
182 */
183 static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) {
184 boolean consume = FocusLogic.shouldConsume(keyCode);
185 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
186 return consume;
187 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800188
Adam Cohen2e6da152015-05-06 11:42:25 -0700189 DeviceProfile profile = ((Launcher) v.getContext()).getDeviceProfile();
190
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800191 if (DEBUG) {
192 Log.v(TAG, String.format(
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700193 "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s",
194 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800195 }
196
197 // Initialize the variables.
198 final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent();
199 final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800200 Hotseat hotseat = (Hotseat) hotseatLayout.getParent();
201
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800202 Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700203 int pageIndex = workspace.getNextPage();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800204 int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800205 int countX = -1;
206 int countY = -1;
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700207 int iconIndex = hotseatParent.indexOfChild(v);
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700208 int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets()
209 .getChildAt(iconIndex).getLayoutParams()).cellX;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800210
211 final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700212 if (iconLayout == null) {
213 // This check is to guard against cases where key strokes rushes in when workspace
214 // child creation/deletion is still in flux. (e.g., during drop or fling
215 // animation.)
216 return consume;
217 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800218 final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets();
219
220 ViewGroup parent = null;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800221 int[][] matrix = null;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800222
223 if (keyCode == KeyEvent.KEYCODE_DPAD_UP &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700224 !profile.isVerticalBarLayout()) {
225 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700226 true /* hotseat horizontal */, profile.inv.hotseatAllAppsRank,
227 iconRank == profile.inv.hotseatAllAppsRank /* include all apps icon */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800228 iconIndex += iconParent.getChildCount();
229 countX = iconLayout.getCountX();
230 countY = iconLayout.getCountY() + hotseatLayout.getCountY();
231 parent = iconParent;
232 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700233 profile.isVerticalBarLayout()) {
234 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700235 false /* hotseat horizontal */, profile.inv.hotseatAllAppsRank,
236 iconRank == profile.inv.hotseatAllAppsRank /* include all apps icon */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800237 iconIndex += iconParent.getChildCount();
238 countX = iconLayout.getCountX() + hotseatLayout.getCountX();
239 countY = iconLayout.getCountY();
240 parent = iconParent;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800241 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700242 profile.isVerticalBarLayout()) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800243 keyCode = KeyEvent.KEYCODE_PAGE_DOWN;
244 }else {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800245 // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the
246 // matrix extended with hotseat.
247 matrix = FocusLogic.createSparseMatrix(hotseatLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800248 countX = hotseatLayout.getCountX();
249 countY = hotseatLayout.getCountY();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800250 parent = hotseatParent;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800251 }
252
253 // Process the focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700254 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700255 countY, matrix, iconIndex, pageIndex, pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800256
Hyunyoung Song31178b82015-02-24 14:12:51 -0800257 View newIcon = null;
258 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
259 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
260 newIcon = parent.getChildAt(0);
261 // TODO(hyunyoungs): handle cases where the child is not an icon but
262 // a folder or a widget.
263 workspace.snapToPage(pageIndex + 1);
264 }
265 if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800266 newIconIndex -= iconParent.getChildCount();
267 }
268 if (parent != null) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800269 if (newIcon == null && newIconIndex >=0) {
270 newIcon = parent.getChildAt(newIconIndex);
271 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800272 if (newIcon != null) {
273 newIcon.requestFocus();
274 playSoundEffect(keyCode, v);
275 }
276 }
277 return consume;
278 }
279
280 /**
281 * Handles key events in a workspace containing icons.
282 */
283 static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) {
284 boolean consume = FocusLogic.shouldConsume(keyCode);
285 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
286 return consume;
287 }
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700288
Adam Cohen2e6da152015-05-06 11:42:25 -0700289 Launcher launcher = (Launcher) v.getContext();
290 DeviceProfile profile = launcher.getDeviceProfile();
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700291
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800292 if (DEBUG) {
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700293 Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s",
294 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800295 }
296
297 // Initialize the variables.
298 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
Hyunyoung Song38531712015-03-03 19:25:16 -0800299 CellLayout iconLayout = (CellLayout) parent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800300 final Workspace workspace = (Workspace) iconLayout.getParent();
Adam Cohen2e6da152015-05-06 11:42:25 -0700301 final ViewGroup dragLayer = (ViewGroup) workspace.getParent();
302 final ViewGroup tabs = (ViewGroup) dragLayer.findViewById(R.id.search_drop_target_bar);
303 final Hotseat hotseat = (Hotseat) dragLayer.findViewById(R.id.hotseat);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700304
305 final int iconIndex = parent.indexOfChild(v);
306 final int pageIndex = workspace.indexOfChild(iconLayout);
307 final int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800308 int countX = iconLayout.getCountX();
309 int countY = iconLayout.getCountY();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800310
311 CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0);
312 ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets();
313 int[][] matrix;
314
Hyunyoung Song31178b82015-02-24 14:12:51 -0800315 // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800316 // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended
317 // with the hotseat.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700318 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) {
319 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, true /* horizontal */,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700320 profile.inv.hotseatAllAppsRank,
Winson Chungc393b072015-05-20 15:03:13 -0700321 !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800322 countY = countY + 1;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800323 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700324 profile.isVerticalBarLayout()) {
325 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, false /* horizontal */,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700326 profile.inv.hotseatAllAppsRank,
Winson Chungc393b072015-05-20 15:03:13 -0700327 !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800328 countX = countX + 1;
329 } else if (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) {
330 workspace.removeWorkspaceItem(v);
331 return consume;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800332 } else {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800333 matrix = FocusLogic.createSparseMatrix(iconLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800334 }
335
336 // Process the focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700337 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700338 countY, matrix, iconIndex, pageIndex, pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800339 View newIcon = null;
340 switch (newIconIndex) {
341 case FocusLogic.NOOP:
342 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
343 newIcon = tabs;
344 }
345 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800346 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700347 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
348 int newPageIndex = pageIndex - 1;
349 if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) {
350 newPageIndex = pageIndex + 1;
351 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700352 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700353 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700354 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800355 if (parent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700356 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800357 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800358 matrix = FocusLogic.createSparseMatrix(iconLayout,
Hyunyoung Song38531712015-03-03 19:25:16 -0800359 iconLayout.getCountX(), row);
Adam Cohen2e6da152015-05-06 11:42:25 -0700360 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY,
Sunny Goyalc6205602015-05-21 20:46:33 -0700361 matrix, FocusLogic.PIVOT, newPageIndex, pageCount,
362 Utilities.isRtl(v.getResources()));
Hyunyoung Song38531712015-03-03 19:25:16 -0800363 newIcon = parent.getChildAt(newIconIndex);
364 }
365 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800366 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
367 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
368 newIcon = parent.getChildAt(0);
369 workspace.snapToPage(pageIndex - 1);
Hyunyoung Song38531712015-03-03 19:25:16 -0800370 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800371 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
372 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
373 newIcon = parent.getChildAt(parent.getChildCount() - 1);
374 workspace.snapToPage(pageIndex - 1);
375 break;
376 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
377 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
378 newIcon = parent.getChildAt(0);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800379 workspace.snapToPage(pageIndex + 1);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800380 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800381 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700382 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
383 newPageIndex = pageIndex + 1;
384 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
385 newPageIndex = pageIndex - 1;
386 }
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700387 workspace.snapToPage(newPageIndex);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700388 row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700389 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800390 if (parent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700391 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800392 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800393 matrix = FocusLogic.createSparseMatrix(iconLayout, -1, row);
Adam Cohen2e6da152015-05-06 11:42:25 -0700394 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY,
Sunny Goyalc6205602015-05-21 20:46:33 -0700395 matrix, FocusLogic.PIVOT, newPageIndex, pageCount,
396 Utilities.isRtl(v.getResources()));
Hyunyoung Song38531712015-03-03 19:25:16 -0800397 newIcon = parent.getChildAt(newIconIndex);
398 }
399 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800400 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
401 newIcon = parent.getChildAt(0);
402 break;
403 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
404 newIcon = parent.getChildAt(parent.getChildCount() - 1);
405 break;
406 default:
407 // current page, some item.
408 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
409 newIcon = parent.getChildAt(newIconIndex);
410 } else if (parent.getChildCount() <= newIconIndex &&
411 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
412 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
413 }
414 break;
415 }
416 if (newIcon != null) {
417 newIcon.requestFocus();
418 playSoundEffect(keyCode, v);
419 }
420 return consume;
421 }
422
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800423 //
424 // Helper methods.
425 //
426
Winson Chung97d85d22011-04-13 11:27:36 -0700427 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700428 * Private helper method to get the CellLayoutChildren given a CellLayout index.
429 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700430 @Thunk static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
Michael Jurkaa52570f2012-03-20 03:18:20 -0700431 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700432 CellLayout parent = (CellLayout) container.getChildAt(i);
433 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700434 }
435
Winson Chung97d85d22011-04-13 11:27:36 -0700436 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800437 * Helper method to be used for playing sound effects.
Winson Chung97d85d22011-04-13 11:27:36 -0700438 */
Adam Cohen091440a2015-03-18 14:16:05 -0700439 @Thunk static void playSoundEffect(int keyCode, View v) {
Winson Chung97d85d22011-04-13 11:27:36 -0700440 switch (keyCode) {
441 case KeyEvent.KEYCODE_DPAD_LEFT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800442 v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
Winson Chung97d85d22011-04-13 11:27:36 -0700443 break;
444 case KeyEvent.KEYCODE_DPAD_RIGHT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800445 v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
Winson Chung97d85d22011-04-13 11:27:36 -0700446 break;
447 case KeyEvent.KEYCODE_DPAD_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700448 case KeyEvent.KEYCODE_PAGE_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700449 case KeyEvent.KEYCODE_MOVE_END:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800450 v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
Adam Cohenac56cff2011-09-28 20:45:37 -0700451 break;
452 case KeyEvent.KEYCODE_DPAD_UP:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800453 case KeyEvent.KEYCODE_PAGE_UP:
Adam Cohenac56cff2011-09-28 20:45:37 -0700454 case KeyEvent.KEYCODE_MOVE_HOME:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800455 v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
Adam Cohenac56cff2011-09-28 20:45:37 -0700456 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800457 default:
Winson Chung97d85d22011-04-13 11:27:36 -0700458 break;
Winson Chung97d85d22011-04-13 11:27:36 -0700459 }
Winson Chung97d85d22011-04-13 11:27:36 -0700460 }
461}