blob: 8791c896a3c13f73aa9d6db79800fe4696754f8d [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
Winson Chung4e6a9762011-05-09 11:56:34 -070019import android.content.res.Configuration;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080020import android.util.Log;
Winson Chung97d85d22011-04-13 11:27:36 -070021import android.view.KeyEvent;
Sunny Goyalb3726d92014-08-20 16:58:17 -070022import android.view.SoundEffectConstants;
Winson Chung97d85d22011-04-13 11:27:36 -070023import android.view.View;
24import android.view.ViewGroup;
Winson Chung97d85d22011-04-13 11:27:36 -070025
Sunny Goyal290800b2015-03-05 11:33:33 -080026import com.android.launcher3.FocusHelper.PagedViewKeyListener;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080027import com.android.launcher3.util.FocusLogic;
Adam Cohen091440a2015-03-18 14:16:05 -070028import com.android.launcher3.util.Thunk;
Winson Chungfaa13252011-06-13 18:15:54 -070029
Winson Chung97d85d22011-04-13 11:27:36 -070030/**
Winson Chung4d279d92011-07-21 11:46:32 -070031 * A keyboard listener we set on all the workspace icons.
32 */
Adam Cohenac56cff2011-09-28 20:45:37 -070033class IconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080034 @Override
Winson Chung4d279d92011-07-21 11:46:32 -070035 public boolean onKey(View v, int keyCode, KeyEvent event) {
Adam Cohenac56cff2011-09-28 20:45:37 -070036 return FocusHelper.handleIconKeyEvent(v, keyCode, event);
37 }
38}
39
40/**
Winson Chung3d503fb2011-07-13 17:25:49 -070041 * A keyboard listener we set on all the hotseat buttons.
Winson Chung4e6a9762011-05-09 11:56:34 -070042 */
Adam Cohenac56cff2011-09-28 20:45:37 -070043class HotseatIconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080044 @Override
Winson Chung4e6a9762011-05-09 11:56:34 -070045 public boolean onKey(View v, int keyCode, KeyEvent event) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080046 return FocusHelper.handleHotseatButtonKeyEvent(v, keyCode, event);
Winson Chung4e6a9762011-05-09 11:56:34 -070047 }
48}
49
Winson Chung97d85d22011-04-13 11:27:36 -070050public class FocusHelper {
Winson Chung97d85d22011-04-13 11:27:36 -070051
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080052 private static final String TAG = "FocusHelper";
53 private static final boolean DEBUG = false;
54
55 //
56 // Key code handling methods.
57 //
58
59 /**
Sunny Goyal290800b2015-03-05 11:33:33 -080060 * A keyboard listener for scrollable folders
61 */
62 public static class PagedFolderKeyEventListener extends PagedViewKeyListener {
63
64 private final Folder mFolder;
65
66 public PagedFolderKeyEventListener(Folder folder) {
67 mFolder = folder;
68 }
69
70 @Override
71 public void handleNoopKey(int keyCode, View v) {
72 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
73 mFolder.mFolderName.requestFocus();
74 playSoundEffect(keyCode, v);
75 }
76 }
77 }
78
79 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080080 * Handles key events in the all apps screen.
81 */
Sunny Goyal290800b2015-03-05 11:33:33 -080082 public static class PagedViewKeyListener implements View.OnKeyListener {
83
Hyunyoung Songada50982015-04-10 14:35:23 -070084 @Override
85 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) {
91 Log.v(TAG, String.format("Handle ALL APPS and Folders keyevent=[%s].",
92 KeyEvent.keyCodeToString(keyCode)));
93 }
Sunny Goyal290800b2015-03-05 11:33:33 -080094
Hyunyoung Songada50982015-04-10 14:35:23 -070095 // Initialize variables.
96 ViewGroup parentLayout;
97 ViewGroup itemContainer;
98 int countX;
99 int countY;
100 if (v.getParent() instanceof ShortcutAndWidgetContainer) {
101 itemContainer = (ViewGroup) v.getParent();
102 parentLayout = (ViewGroup) itemContainer.getParent();
103 countX = ((CellLayout) parentLayout).getCountX();
104 countY = ((CellLayout) parentLayout).getCountY();
105 } else {
106 if (LauncherAppState.isDogfoodBuild()) {
107 throw new IllegalStateException("Parent of the focused item is not supported.");
108 } else {
109 return false;
110 }
111 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800112
Hyunyoung Songada50982015-04-10 14:35:23 -0700113 final int iconIndex = itemContainer.indexOfChild(v);
114 final PagedView container = (PagedView) parentLayout.getParent();
115 final int pageIndex = container.indexToPage(container.indexOfChild(parentLayout));
116 final int pageCount = container.getChildCount();
117 ViewGroup newParent = null;
118 View child = null;
119 // TODO(hyunyoungs): this matrix is not applicable on the last page.
120 int[][] matrix = FocusLogic.createFullMatrix(countX, countY, true);
121
122 // Process focus.
123 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix,
124 iconIndex, pageIndex, pageCount);
125 if (newIconIndex == FocusLogic.NOOP) {
126 handleNoopKey(keyCode, v);
127 return consume;
128 }
129 switch (newIconIndex) {
130 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
131 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
132 int newPageIndex = pageIndex - 1;
133 if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) {
134 newPageIndex = pageIndex + 1;
135 }
136 newParent = getAppsCustomizePage(container, newPageIndex);
137 if (newParent != null) {
138 int row = FocusLogic.findRow(matrix, iconIndex);
139 container.snapToPage(newPageIndex);
140 // no need to create a new matrix.
141 child = newParent.getChildAt(matrix[countX-1][row]);
142 }
143 break;
144 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
145 newParent = getAppsCustomizePage(container, pageIndex - 1);
146 if (newParent != null) {
147 container.snapToPage(pageIndex - 1);
148 child = newParent.getChildAt(0);
149 }
150 break;
151 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
152 newParent = getAppsCustomizePage(container, pageIndex - 1);
153 if (newParent != null) {
154 container.snapToPage(pageIndex - 1);
155 child = newParent.getChildAt(newParent.getChildCount() - 1);
156 }
157 break;
158 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
159 newParent = getAppsCustomizePage(container, pageIndex + 1);
160 if (newParent != null) {
161 container.snapToPage(pageIndex + 1);
162 child = newParent.getChildAt(0);
163 }
164 break;
165 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
166 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
167 newPageIndex = pageIndex + 1;
168 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
169 newPageIndex = pageIndex -1;
170 }
171 newParent = getAppsCustomizePage(container, newPageIndex);
172 if (newParent != null) {
173 container.snapToPage(newPageIndex);
174 int row = FocusLogic.findRow(matrix, iconIndex);
175 child = newParent.getChildAt(matrix[0][row]);
176 }
177 break;
178 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
179 child = container.getChildAt(0);
180 break;
181 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
182 child = itemContainer.getChildAt(itemContainer.getChildCount() - 1);
183 break;
184 default: // Go to some item on the current page.
185 child = itemContainer.getChildAt(newIconIndex);
186 break;
187 }
188 if (child != null) {
189 child.requestFocus();
190 playSoundEffect(keyCode, v);
191 } else {
192 handleNoopKey(keyCode, v);
193 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800194 return consume;
195 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800196
Hyunyoung Songada50982015-04-10 14:35:23 -0700197 public void handleNoopKey(int keyCode, View v) { }
Sunny Goyal290800b2015-03-05 11:33:33 -0800198 }
199
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800200 /**
201 * Handles key events in the workspace hot seat (bottom of the screen).
202 * <p>Currently we don't special case for the phone UI in different orientations, even though
203 * the hotseat is on the side in landscape mode. This is to ensure that accessibility
204 * consistency is maintained across rotations.
205 */
206 static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) {
207 boolean consume = FocusLogic.shouldConsume(keyCode);
208 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
209 return consume;
210 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800211
Hyunyoung Songada50982015-04-10 14:35:23 -0700212 DeviceProfile profile = LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile();
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.
220 final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent();
221 final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800222 Hotseat hotseat = (Hotseat) hotseatLayout.getParent();
223
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800224 Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
225 int pageIndex = workspace.getCurrentPage();
226 int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800227 int countX = -1;
228 int countY = -1;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800229 int iconIndex = findIndexOfView(hotseatParent, v);
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700230 int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets()
231 .getChildAt(iconIndex).getLayoutParams()).cellX;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800232
233 final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex);
234 final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets();
235
236 ViewGroup parent = null;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800237 int[][] matrix = null;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800238
239 if (keyCode == KeyEvent.KEYCODE_DPAD_UP &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700240 !profile.isVerticalBarLayout()) {
241 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout,
242 true /* hotseat horizontal */, hotseat.getAllAppsButtonRank(),
243 iconRank == hotseat.getAllAppsButtonRank() /* include all apps icon */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800244 iconIndex += iconParent.getChildCount();
245 countX = iconLayout.getCountX();
246 countY = iconLayout.getCountY() + hotseatLayout.getCountY();
247 parent = iconParent;
248 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700249 profile.isVerticalBarLayout()) {
250 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout,
251 false /* hotseat horizontal */, hotseat.getAllAppsButtonRank(),
252 iconRank == hotseat.getAllAppsButtonRank() /* include all apps icon */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800253 iconIndex += iconParent.getChildCount();
254 countX = iconLayout.getCountX() + hotseatLayout.getCountX();
255 countY = iconLayout.getCountY();
256 parent = iconParent;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800257 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700258 profile.isVerticalBarLayout()) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800259 keyCode = KeyEvent.KEYCODE_PAGE_DOWN;
260 }else {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800261 // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the
262 // matrix extended with hotseat.
263 matrix = FocusLogic.createSparseMatrix(hotseatLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800264 countX = hotseatLayout.getCountX();
265 countY = hotseatLayout.getCountY();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800266 parent = hotseatParent;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800267 }
268
269 // Process the focus.
270 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix,
271 iconIndex, pageIndex, pageCount);
272
Hyunyoung Song31178b82015-02-24 14:12:51 -0800273 View newIcon = null;
274 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
275 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
276 newIcon = parent.getChildAt(0);
277 // TODO(hyunyoungs): handle cases where the child is not an icon but
278 // a folder or a widget.
279 workspace.snapToPage(pageIndex + 1);
280 }
281 if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800282 newIconIndex -= iconParent.getChildCount();
283 }
284 if (parent != null) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800285 if (newIcon == null && newIconIndex >=0) {
286 newIcon = parent.getChildAt(newIconIndex);
287 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800288 if (newIcon != null) {
289 newIcon.requestFocus();
290 playSoundEffect(keyCode, v);
291 }
292 }
293 return consume;
294 }
295
296 /**
297 * Handles key events in a workspace containing icons.
298 */
299 static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) {
300 boolean consume = FocusLogic.shouldConsume(keyCode);
301 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
302 return consume;
303 }
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700304
305 LauncherAppState app = LauncherAppState.getInstance();
306 DeviceProfile profile = app.getDynamicGrid().getDeviceProfile();
307
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800308 if (DEBUG) {
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700309 Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s",
310 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800311 }
312
313 // Initialize the variables.
314 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
Hyunyoung Song38531712015-03-03 19:25:16 -0800315 CellLayout iconLayout = (CellLayout) parent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800316 final Workspace workspace = (Workspace) iconLayout.getParent();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800317 final ViewGroup launcher = (ViewGroup) workspace.getParent();
318 final ViewGroup tabs = (ViewGroup) launcher.findViewById(R.id.search_drop_target_bar);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800319 final Hotseat hotseat = (Hotseat) launcher.findViewById(R.id.hotseat);
320 int pageIndex = workspace.indexOfChild(iconLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800321 int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800322 int countX = iconLayout.getCountX();
323 int countY = iconLayout.getCountY();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800324 final int iconIndex = findIndexOfView(parent, v);
325
326 CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0);
327 ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets();
328 int[][] matrix;
329
Hyunyoung Song31178b82015-02-24 14:12:51 -0800330 // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800331 // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended
332 // with the hotseat.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700333 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) {
334 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, true /* horizontal */,
Hyunyoung Song31178b82015-02-24 14:12:51 -0800335 hotseat.getAllAppsButtonRank(), false /* all apps icon is ignored */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800336 countY = countY + 1;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800337 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700338 profile.isVerticalBarLayout()) {
339 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, false /* horizontal */,
Hyunyoung Song31178b82015-02-24 14:12:51 -0800340 hotseat.getAllAppsButtonRank(), false /* all apps icon is ignored */);
341 countX = countX + 1;
342 } else if (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) {
343 workspace.removeWorkspaceItem(v);
344 return consume;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800345 } else {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800346 matrix = FocusLogic.createSparseMatrix(iconLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800347 }
348
349 // Process the focus.
350 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix,
351 iconIndex, pageIndex, pageCount);
352 View newIcon = null;
353 switch (newIconIndex) {
354 case FocusLogic.NOOP:
355 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
356 newIcon = tabs;
357 }
358 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800359 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700360 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
361 int newPageIndex = pageIndex - 1;
362 if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) {
363 newPageIndex = pageIndex + 1;
364 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800365 int row = FocusLogic.findRow(matrix, iconIndex);
Hyunyoung Songada50982015-04-10 14:35:23 -0700366 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800367 if (parent != null) {
368 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800369 matrix = FocusLogic.createSparseMatrix(iconLayout,
Hyunyoung Song38531712015-03-03 19:25:16 -0800370 iconLayout.getCountX(), row);
371 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY, matrix,
Hyunyoung Songada50982015-04-10 14:35:23 -0700372 FocusLogic.PIVOT, newPageIndex, pageCount);
Hyunyoung Song38531712015-03-03 19:25:16 -0800373 newIcon = parent.getChildAt(newIconIndex);
374 }
375 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800376 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
377 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
378 newIcon = parent.getChildAt(0);
379 workspace.snapToPage(pageIndex - 1);
Hyunyoung Song38531712015-03-03 19:25:16 -0800380 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800381 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
382 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
383 newIcon = parent.getChildAt(parent.getChildCount() - 1);
384 workspace.snapToPage(pageIndex - 1);
385 break;
386 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
387 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
388 newIcon = parent.getChildAt(0);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800389 workspace.snapToPage(pageIndex + 1);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800390 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800391 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700392 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
393 newPageIndex = pageIndex + 1;
394 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
395 newPageIndex = pageIndex - 1;
396 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800397 row = FocusLogic.findRow(matrix, iconIndex);
Hyunyoung Songada50982015-04-10 14:35:23 -0700398 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800399 if (parent != null) {
400 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800401 matrix = FocusLogic.createSparseMatrix(iconLayout, -1, row);
Hyunyoung Song38531712015-03-03 19:25:16 -0800402 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY, matrix,
Hyunyoung Songada50982015-04-10 14:35:23 -0700403 FocusLogic.PIVOT, newPageIndex, pageCount);
Hyunyoung Song38531712015-03-03 19:25:16 -0800404 newIcon = parent.getChildAt(newIconIndex);
405 }
406 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800407 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
408 newIcon = parent.getChildAt(0);
409 break;
410 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
411 newIcon = parent.getChildAt(parent.getChildCount() - 1);
412 break;
413 default:
414 // current page, some item.
415 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
416 newIcon = parent.getChildAt(newIconIndex);
417 } else if (parent.getChildCount() <= newIconIndex &&
418 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
419 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
420 }
421 break;
422 }
423 if (newIcon != null) {
424 newIcon.requestFocus();
425 playSoundEffect(keyCode, v);
426 }
427 return consume;
428 }
429
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800430 //
431 // Helper methods.
432 //
433
Winson Chung97d85d22011-04-13 11:27:36 -0700434 /**
Adam Cohenae4f1552011-10-20 00:15:42 -0700435 * Returns the Viewgroup containing page contents for the page at the index specified.
436 */
Adam Cohen091440a2015-03-18 14:16:05 -0700437 @Thunk static ViewGroup getAppsCustomizePage(ViewGroup container, int index) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700438 ViewGroup page = (ViewGroup) ((PagedView) container).getPageAt(index);
Winson Chungc58497e2013-09-03 17:48:37 -0700439 if (page instanceof CellLayout) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700440 // There are two layers, a PagedViewCellLayout and PagedViewCellLayoutChildren
Winson Chungc58497e2013-09-03 17:48:37 -0700441 page = ((CellLayout) page).getShortcutsAndWidgets();
Adam Cohenae4f1552011-10-20 00:15:42 -0700442 }
443 return page;
444 }
445
446 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700447 * Private helper method to get the CellLayoutChildren given a CellLayout index.
448 */
Michael Jurkaa52570f2012-03-20 03:18:20 -0700449 private static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
450 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700451 CellLayout parent = (CellLayout) container.getChildAt(i);
452 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700453 }
454
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800455 private static int findIndexOfView(ViewGroup parent, View v) {
456 for (int i = 0; i < parent.getChildCount(); i++) {
457 if (v != null && v.equals(parent.getChildAt(i))) {
458 return i;
Winson Chung97d85d22011-04-13 11:27:36 -0700459 }
460 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800461 return -1;
Winson Chung97d85d22011-04-13 11:27:36 -0700462 }
463
464 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800465 * Helper method to be used for playing sound effects.
Winson Chung97d85d22011-04-13 11:27:36 -0700466 */
Adam Cohen091440a2015-03-18 14:16:05 -0700467 @Thunk static void playSoundEffect(int keyCode, View v) {
Winson Chung97d85d22011-04-13 11:27:36 -0700468 switch (keyCode) {
469 case KeyEvent.KEYCODE_DPAD_LEFT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800470 v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
Winson Chung97d85d22011-04-13 11:27:36 -0700471 break;
472 case KeyEvent.KEYCODE_DPAD_RIGHT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800473 v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
Winson Chung97d85d22011-04-13 11:27:36 -0700474 break;
475 case KeyEvent.KEYCODE_DPAD_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700476 case KeyEvent.KEYCODE_PAGE_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700477 case KeyEvent.KEYCODE_MOVE_END:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800478 v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
Adam Cohenac56cff2011-09-28 20:45:37 -0700479 break;
480 case KeyEvent.KEYCODE_DPAD_UP:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800481 case KeyEvent.KEYCODE_PAGE_UP:
Adam Cohenac56cff2011-09-28 20:45:37 -0700482 case KeyEvent.KEYCODE_MOVE_HOME:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800483 v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
Adam Cohenac56cff2011-09-28 20:45:37 -0700484 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800485 default:
Winson Chung97d85d22011-04-13 11:27:36 -0700486 break;
Winson Chung97d85d22011-04-13 11:27:36 -0700487 }
Winson Chung97d85d22011-04-13 11:27:36 -0700488 }
489}