blob: 678ed0f82b3b7d96fdbab907ebf53975cf0a2b2e [file] [log] [blame]
Winson Chung97d85d22011-04-13 11:27:36 -07001/*
Hyunyoung Songee3e6a72015-02-20 14:25:27 -08002 * Copyright (C) 2015 The Android Open Source Project
Winson Chung97d85d22011-04-13 11:27:36 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung97d85d22011-04-13 11:27:36 -070018
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080019import android.util.Log;
Winson Chung97d85d22011-04-13 11:27:36 -070020import android.view.KeyEvent;
Sunny Goyalb3726d92014-08-20 16:58:17 -070021import android.view.SoundEffectConstants;
Winson Chung97d85d22011-04-13 11:27:36 -070022import android.view.View;
23import android.view.ViewGroup;
Winson Chung97d85d22011-04-13 11:27:36 -070024
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080025import com.android.launcher3.util.FocusLogic;
Adam Cohen091440a2015-03-18 14:16:05 -070026import com.android.launcher3.util.Thunk;
Winson Chungfaa13252011-06-13 18:15:54 -070027
Winson Chung97d85d22011-04-13 11:27:36 -070028/**
Winson Chung4d279d92011-07-21 11:46:32 -070029 * A keyboard listener we set on all the workspace icons.
30 */
Adam Cohenac56cff2011-09-28 20:45:37 -070031class IconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080032 @Override
Winson Chung4d279d92011-07-21 11:46:32 -070033 public boolean onKey(View v, int keyCode, KeyEvent event) {
Adam Cohenac56cff2011-09-28 20:45:37 -070034 return FocusHelper.handleIconKeyEvent(v, keyCode, event);
35 }
36}
37
38/**
Winson Chung3d503fb2011-07-13 17:25:49 -070039 * A keyboard listener we set on all the hotseat buttons.
Winson Chung4e6a9762011-05-09 11:56:34 -070040 */
Adam Cohenac56cff2011-09-28 20:45:37 -070041class HotseatIconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080042 @Override
Winson Chung4e6a9762011-05-09 11:56:34 -070043 public boolean onKey(View v, int keyCode, KeyEvent event) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080044 return FocusHelper.handleHotseatButtonKeyEvent(v, keyCode, event);
Winson Chung4e6a9762011-05-09 11:56:34 -070045 }
46}
47
Winson Chung97d85d22011-04-13 11:27:36 -070048public class FocusHelper {
Winson Chung97d85d22011-04-13 11:27:36 -070049
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080050 private static final String TAG = "FocusHelper";
51 private static final boolean DEBUG = false;
52
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080053 /**
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070054 * Handles key events in paged folder.
Sunny Goyal290800b2015-03-05 11:33:33 -080055 */
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070056 public static class PagedFolderKeyEventListener implements View.OnKeyListener {
Sunny Goyal290800b2015-03-05 11:33:33 -080057
58 private final Folder mFolder;
59
60 public PagedFolderKeyEventListener(Folder folder) {
61 mFolder = folder;
62 }
63
64 @Override
Hyunyoung Songada50982015-04-10 14:35:23 -070065 public boolean onKey(View v, int keyCode, KeyEvent e) {
66 boolean consume = FocusLogic.shouldConsume(keyCode);
67 if (e.getAction() == KeyEvent.ACTION_UP) {
68 return consume;
Sunny Goyal290800b2015-03-05 11:33:33 -080069 }
Hyunyoung Songada50982015-04-10 14:35:23 -070070 if (DEBUG) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070071 Log.v(TAG, String.format("Handle ALL Folders keyevent=[%s].",
Hyunyoung Songada50982015-04-10 14:35:23 -070072 KeyEvent.keyCodeToString(keyCode)));
73 }
Sunny Goyal290800b2015-03-05 11:33:33 -080074
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070075
76 if (!(v.getParent() instanceof ShortcutAndWidgetContainer)) {
Hyunyoung Songada50982015-04-10 14:35:23 -070077 if (LauncherAppState.isDogfoodBuild()) {
78 throw new IllegalStateException("Parent of the focused item is not supported.");
79 } else {
80 return false;
81 }
82 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080083
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070084 // Initialize variables.
85 final ShortcutAndWidgetContainer itemContainer = (ShortcutAndWidgetContainer) v.getParent();
86 final CellLayout cellLayout = (CellLayout) itemContainer.getParent();
87 final int countX = cellLayout.getCountX();
88 final int countY = cellLayout.getCountY();
Hyunyoung Songada50982015-04-10 14:35:23 -070089
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070090 final int iconIndex = itemContainer.indexOfChild(v);
91 final FolderPagedView pagedView = (FolderPagedView) cellLayout.getParent();
92
93 final int pageIndex = pagedView.indexOfChild(cellLayout);
94 final int pageCount = pagedView.getPageCount();
95
96 int[][] matrix = FocusLogic.createSparseMatrix(cellLayout);
Hyunyoung Songada50982015-04-10 14:35:23 -070097 // Process focus.
98 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix,
99 iconIndex, pageIndex, pageCount);
100 if (newIconIndex == FocusLogic.NOOP) {
101 handleNoopKey(keyCode, v);
102 return consume;
103 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700104 ShortcutAndWidgetContainer newParent = null;
105 View child = null;
106
Hyunyoung Songada50982015-04-10 14:35:23 -0700107 switch (newIconIndex) {
108 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700109 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
110 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700111 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700112 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
113 pagedView.snapToPage(pageIndex - 1);
114 child = newParent.getChildAt(
115 ((newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN)
116 ^ newParent.invertLayoutHorizontally()) ? 0 : countX - 1, row);
Hyunyoung Songada50982015-04-10 14:35:23 -0700117 }
118 break;
119 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700120 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700121 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700122 pagedView.snapToPage(pageIndex - 1);
123 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700124 }
125 break;
126 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700127 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700128 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700129 pagedView.snapToPage(pageIndex - 1);
130 child = newParent.getChildAt(countX - 1, countY - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700131 }
132 break;
133 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700134 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700135 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700136 pagedView.snapToPage(pageIndex + 1);
137 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700138 }
139 break;
140 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700141 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
142 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 = FocusLogic.getAdjacentChildInNextPage(newParent, v, newIconIndex);
Hyunyoung Songada50982015-04-10 14:35:23 -0700146 }
147 break;
148 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700149 child = cellLayout.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700150 break;
151 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700152 child = pagedView.getLastItem();
Hyunyoung Songada50982015-04-10 14:35:23 -0700153 break;
154 default: // Go to some item on the current page.
155 child = itemContainer.getChildAt(newIconIndex);
156 break;
157 }
158 if (child != null) {
159 child.requestFocus();
160 playSoundEffect(keyCode, v);
161 } else {
162 handleNoopKey(keyCode, v);
163 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800164 return consume;
165 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800166
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700167 public void handleNoopKey(int keyCode, View v) {
168 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
169 mFolder.mFolderName.requestFocus();
170 playSoundEffect(keyCode, v);
171 }
172 }
Sunny Goyal290800b2015-03-05 11:33:33 -0800173 }
174
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800175 /**
176 * Handles key events in the workspace hot seat (bottom of the screen).
177 * <p>Currently we don't special case for the phone UI in different orientations, even though
178 * the hotseat is on the side in landscape mode. This is to ensure that accessibility
179 * consistency is maintained across rotations.
180 */
181 static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) {
182 boolean consume = FocusLogic.shouldConsume(keyCode);
183 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
184 return consume;
185 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800186
Hyunyoung Songada50982015-04-10 14:35:23 -0700187 DeviceProfile profile = LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800188 if (DEBUG) {
189 Log.v(TAG, String.format(
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700190 "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s",
191 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800192 }
193
194 // Initialize the variables.
195 final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent();
196 final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800197 Hotseat hotseat = (Hotseat) hotseatLayout.getParent();
198
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800199 Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700200 int pageIndex = workspace.getNextPage();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800201 int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800202 int countX = -1;
203 int countY = -1;
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700204 int iconIndex = hotseatParent.indexOfChild(v);
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700205 int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets()
206 .getChildAt(iconIndex).getLayoutParams()).cellX;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800207
208 final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700209 if (iconLayout == null) {
210 // This check is to guard against cases where key strokes rushes in when workspace
211 // child creation/deletion is still in flux. (e.g., during drop or fling
212 // animation.)
213 return consume;
214 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800215 final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets();
216
217 ViewGroup parent = null;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800218 int[][] matrix = null;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800219
220 if (keyCode == KeyEvent.KEYCODE_DPAD_UP &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700221 !profile.isVerticalBarLayout()) {
222 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout,
223 true /* hotseat horizontal */, hotseat.getAllAppsButtonRank(),
224 iconRank == hotseat.getAllAppsButtonRank() /* include all apps icon */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800225 iconIndex += iconParent.getChildCount();
226 countX = iconLayout.getCountX();
227 countY = iconLayout.getCountY() + hotseatLayout.getCountY();
228 parent = iconParent;
229 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700230 profile.isVerticalBarLayout()) {
231 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout,
232 false /* hotseat horizontal */, hotseat.getAllAppsButtonRank(),
233 iconRank == hotseat.getAllAppsButtonRank() /* include all apps icon */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800234 iconIndex += iconParent.getChildCount();
235 countX = iconLayout.getCountX() + hotseatLayout.getCountX();
236 countY = iconLayout.getCountY();
237 parent = iconParent;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800238 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700239 profile.isVerticalBarLayout()) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800240 keyCode = KeyEvent.KEYCODE_PAGE_DOWN;
241 }else {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800242 // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the
243 // matrix extended with hotseat.
244 matrix = FocusLogic.createSparseMatrix(hotseatLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800245 countX = hotseatLayout.getCountX();
246 countY = hotseatLayout.getCountY();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800247 parent = hotseatParent;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800248 }
249
250 // Process the focus.
251 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix,
252 iconIndex, pageIndex, pageCount);
253
Hyunyoung Song31178b82015-02-24 14:12:51 -0800254 View newIcon = null;
255 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
256 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
257 newIcon = parent.getChildAt(0);
258 // TODO(hyunyoungs): handle cases where the child is not an icon but
259 // a folder or a widget.
260 workspace.snapToPage(pageIndex + 1);
261 }
262 if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800263 newIconIndex -= iconParent.getChildCount();
264 }
265 if (parent != null) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800266 if (newIcon == null && newIconIndex >=0) {
267 newIcon = parent.getChildAt(newIconIndex);
268 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800269 if (newIcon != null) {
270 newIcon.requestFocus();
271 playSoundEffect(keyCode, v);
272 }
273 }
274 return consume;
275 }
276
277 /**
278 * Handles key events in a workspace containing icons.
279 */
280 static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) {
281 boolean consume = FocusLogic.shouldConsume(keyCode);
282 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
283 return consume;
284 }
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700285
286 LauncherAppState app = LauncherAppState.getInstance();
287 DeviceProfile profile = app.getDynamicGrid().getDeviceProfile();
288
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800289 if (DEBUG) {
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700290 Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s",
291 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800292 }
293
294 // Initialize the variables.
295 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
Hyunyoung Song38531712015-03-03 19:25:16 -0800296 CellLayout iconLayout = (CellLayout) parent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800297 final Workspace workspace = (Workspace) iconLayout.getParent();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800298 final ViewGroup launcher = (ViewGroup) workspace.getParent();
299 final ViewGroup tabs = (ViewGroup) launcher.findViewById(R.id.search_drop_target_bar);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800300 final Hotseat hotseat = (Hotseat) launcher.findViewById(R.id.hotseat);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700301
302 final int iconIndex = parent.indexOfChild(v);
303 final int pageIndex = workspace.indexOfChild(iconLayout);
304 final int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800305 int countX = iconLayout.getCountX();
306 int countY = iconLayout.getCountY();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800307
308 CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0);
309 ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets();
310 int[][] matrix;
311
Hyunyoung Song31178b82015-02-24 14:12:51 -0800312 // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800313 // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended
314 // with the hotseat.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700315 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) {
316 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, true /* horizontal */,
Winson Chungc393b072015-05-20 15:03:13 -0700317 hotseat.getAllAppsButtonRank(),
318 !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800319 countY = countY + 1;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800320 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700321 profile.isVerticalBarLayout()) {
322 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, false /* horizontal */,
Winson Chungc393b072015-05-20 15:03:13 -0700323 hotseat.getAllAppsButtonRank(),
324 !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800325 countX = countX + 1;
326 } else if (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) {
327 workspace.removeWorkspaceItem(v);
328 return consume;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800329 } else {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800330 matrix = FocusLogic.createSparseMatrix(iconLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800331 }
332
333 // Process the focus.
334 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix,
335 iconIndex, pageIndex, pageCount);
336 View newIcon = null;
337 switch (newIconIndex) {
338 case FocusLogic.NOOP:
339 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
340 newIcon = tabs;
341 }
342 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800343 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700344 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
345 int newPageIndex = pageIndex - 1;
346 if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) {
347 newPageIndex = pageIndex + 1;
348 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700349 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700350 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700351 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800352 if (parent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700353 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800354 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800355 matrix = FocusLogic.createSparseMatrix(iconLayout,
Hyunyoung Song38531712015-03-03 19:25:16 -0800356 iconLayout.getCountX(), row);
357 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY, matrix,
Hyunyoung Songada50982015-04-10 14:35:23 -0700358 FocusLogic.PIVOT, newPageIndex, pageCount);
Hyunyoung Song38531712015-03-03 19:25:16 -0800359 newIcon = parent.getChildAt(newIconIndex);
360 }
361 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800362 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
363 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
364 newIcon = parent.getChildAt(0);
365 workspace.snapToPage(pageIndex - 1);
Hyunyoung Song38531712015-03-03 19:25:16 -0800366 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800367 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
368 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
369 newIcon = parent.getChildAt(parent.getChildCount() - 1);
370 workspace.snapToPage(pageIndex - 1);
371 break;
372 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
373 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
374 newIcon = parent.getChildAt(0);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800375 workspace.snapToPage(pageIndex + 1);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800376 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800377 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700378 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
379 newPageIndex = pageIndex + 1;
380 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
381 newPageIndex = pageIndex - 1;
382 }
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700383 workspace.snapToPage(newPageIndex);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700384 row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700385 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800386 if (parent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700387 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800388 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800389 matrix = FocusLogic.createSparseMatrix(iconLayout, -1, row);
Hyunyoung Song38531712015-03-03 19:25:16 -0800390 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY, matrix,
Hyunyoung Songada50982015-04-10 14:35:23 -0700391 FocusLogic.PIVOT, newPageIndex, pageCount);
Hyunyoung Song38531712015-03-03 19:25:16 -0800392 newIcon = parent.getChildAt(newIconIndex);
393 }
394 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800395 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
396 newIcon = parent.getChildAt(0);
397 break;
398 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
399 newIcon = parent.getChildAt(parent.getChildCount() - 1);
400 break;
401 default:
402 // current page, some item.
403 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
404 newIcon = parent.getChildAt(newIconIndex);
405 } else if (parent.getChildCount() <= newIconIndex &&
406 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
407 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
408 }
409 break;
410 }
411 if (newIcon != null) {
412 newIcon.requestFocus();
413 playSoundEffect(keyCode, v);
414 }
415 return consume;
416 }
417
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800418 //
419 // Helper methods.
420 //
421
Winson Chung97d85d22011-04-13 11:27:36 -0700422 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700423 * Private helper method to get the CellLayoutChildren given a CellLayout index.
424 */
Michael Jurkaa52570f2012-03-20 03:18:20 -0700425 private static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
426 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700427 CellLayout parent = (CellLayout) container.getChildAt(i);
428 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700429 }
430
Winson Chung97d85d22011-04-13 11:27:36 -0700431 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800432 * Helper method to be used for playing sound effects.
Winson Chung97d85d22011-04-13 11:27:36 -0700433 */
Adam Cohen091440a2015-03-18 14:16:05 -0700434 @Thunk static void playSoundEffect(int keyCode, View v) {
Winson Chung97d85d22011-04-13 11:27:36 -0700435 switch (keyCode) {
436 case KeyEvent.KEYCODE_DPAD_LEFT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800437 v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
Winson Chung97d85d22011-04-13 11:27:36 -0700438 break;
439 case KeyEvent.KEYCODE_DPAD_RIGHT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800440 v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
Winson Chung97d85d22011-04-13 11:27:36 -0700441 break;
442 case KeyEvent.KEYCODE_DPAD_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700443 case KeyEvent.KEYCODE_PAGE_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700444 case KeyEvent.KEYCODE_MOVE_END:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800445 v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
Adam Cohenac56cff2011-09-28 20:45:37 -0700446 break;
447 case KeyEvent.KEYCODE_DPAD_UP:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800448 case KeyEvent.KEYCODE_PAGE_UP:
Adam Cohenac56cff2011-09-28 20:45:37 -0700449 case KeyEvent.KEYCODE_MOVE_HOME:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800450 v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
Adam Cohenac56cff2011-09-28 20:45:37 -0700451 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800452 default:
Winson Chung97d85d22011-04-13 11:27:36 -0700453 break;
Winson Chung97d85d22011-04-13 11:27:36 -0700454 }
Winson Chung97d85d22011-04-13 11:27:36 -0700455 }
456}