blob: fc689520191eaa4451b4c89152a9be103c05d848 [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;
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/**
40 * A keyboard listener we set on all the workspace icons.
41 */
42class FolderKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080043 @Override
Adam Cohenac56cff2011-09-28 20:45:37 -070044 public boolean onKey(View v, int keyCode, KeyEvent event) {
45 return FocusHelper.handleFolderKeyEvent(v, keyCode, event);
Winson Chung4d279d92011-07-21 11:46:32 -070046 }
47}
48
49/**
Winson Chung3d503fb2011-07-13 17:25:49 -070050 * A keyboard listener we set on all the hotseat buttons.
Winson Chung4e6a9762011-05-09 11:56:34 -070051 */
Adam Cohenac56cff2011-09-28 20:45:37 -070052class HotseatIconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080053 @Override
Winson Chung4e6a9762011-05-09 11:56:34 -070054 public boolean onKey(View v, int keyCode, KeyEvent event) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080055 return FocusHelper.handleHotseatButtonKeyEvent(v, keyCode, event);
Winson Chung4e6a9762011-05-09 11:56:34 -070056 }
57}
58
Winson Chung97d85d22011-04-13 11:27:36 -070059public class FocusHelper {
Winson Chung97d85d22011-04-13 11:27:36 -070060
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080061 private static final String TAG = "FocusHelper";
62 private static final boolean DEBUG = false;
63
64 //
65 // Key code handling methods.
66 //
67
68 /**
Sunny Goyal290800b2015-03-05 11:33:33 -080069 * A keyboard listener for scrollable folders
70 */
71 public static class PagedFolderKeyEventListener extends PagedViewKeyListener {
72
73 private final Folder mFolder;
74
75 public PagedFolderKeyEventListener(Folder folder) {
76 mFolder = folder;
77 }
78
79 @Override
80 public void handleNoopKey(int keyCode, View v) {
81 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
82 mFolder.mFolderName.requestFocus();
83 playSoundEffect(keyCode, v);
84 }
85 }
86 }
87
88 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080089 * Handles key events in the all apps screen.
90 */
Sunny Goyal290800b2015-03-05 11:33:33 -080091 public static class PagedViewKeyListener implements View.OnKeyListener {
92
93 @Override
94 public boolean onKey(View v, int keyCode, KeyEvent e) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080095 boolean consume = FocusLogic.shouldConsume(keyCode);
96 if (e.getAction() == KeyEvent.ACTION_UP) {
97 return consume;
98 }
99 if (DEBUG) {
100 Log.v(TAG, String.format("Handle ALL APPS keyevent=[%s].",
101 KeyEvent.keyCodeToString(keyCode)));
102 }
103
104 // Initialize variables.
105 ViewGroup parentLayout;
106 ViewGroup itemContainer;
107 int countX;
108 int countY;
109 if (v.getParent() instanceof ShortcutAndWidgetContainer) {
110 itemContainer = (ViewGroup) v.getParent();
111 parentLayout = (ViewGroup) itemContainer.getParent();
112 countX = ((CellLayout) parentLayout).getCountX();
113 countY = ((CellLayout) parentLayout).getCountY();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800114 } else {
Sunny Goyal290800b2015-03-05 11:33:33 -0800115 if (LauncherAppState.isDogfoodBuild()) {
116 throw new IllegalStateException("Parent of the focused item is not supported.");
117 } else {
118 return false;
119 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800120 }
Sunny Goyal290800b2015-03-05 11:33:33 -0800121
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800122 final int iconIndex = itemContainer.indexOfChild(v);
123 final PagedView container = (PagedView) parentLayout.getParent();
124 final int pageIndex = container.indexToPage(container.indexOfChild(parentLayout));
125 final int pageCount = container.getChildCount();
126 ViewGroup newParent = null;
127 View child = null;
Hyunyoung Song38531712015-03-03 19:25:16 -0800128 // TODO(hyunyoungs): this matrix is not applicable on the last page.
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800129 int[][] matrix = FocusLogic.createFullMatrix(countX, countY, true);
130
131 // Process focus.
132 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, matrix,
133 iconIndex, pageIndex, pageCount);
134 if (newIconIndex == FocusLogic.NOOP) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800135 handleNoopKey(keyCode, v);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800136 return consume;
137 }
138 switch (newIconIndex) {
Hyunyoung Song38531712015-03-03 19:25:16 -0800139 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
140 newParent = getAppsCustomizePage(container, pageIndex -1);
141 if (newParent != null) {
142 int row = FocusLogic.findRow(matrix, iconIndex);
143 container.snapToPage(pageIndex - 1);
144 // no need to create a new matrix.
145 child = newParent.getChildAt(matrix[countX-1][row]);
146 }
147 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800148 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
149 newParent = getAppsCustomizePage(container, pageIndex - 1);
150 if (newParent != null) {
151 container.snapToPage(pageIndex - 1);
152 child = newParent.getChildAt(0);
153 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800154 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800155 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
156 newParent = getAppsCustomizePage(container, pageIndex - 1);
157 if (newParent != null) {
158 container.snapToPage(pageIndex - 1);
159 child = newParent.getChildAt(newParent.getChildCount() - 1);
160 }
161 break;
162 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
163 newParent = getAppsCustomizePage(container, pageIndex + 1);
164 if (newParent != null) {
165 container.snapToPage(pageIndex + 1);
166 child = newParent.getChildAt(0);
167 }
168 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800169 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
170 newParent = getAppsCustomizePage(container, pageIndex + 1);
171 if (newParent != null) {
172 container.snapToPage(pageIndex + 1);
173 int row = FocusLogic.findRow(matrix, iconIndex);
174 child = newParent.getChildAt(matrix[0][row]);
175 }
176 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800177 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
178 child = container.getChildAt(0);
179 break;
180 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
181 child = itemContainer.getChildAt(itemContainer.getChildCount() - 1);
182 break;
183 default: // Go to some item on the current page.
184 child = itemContainer.getChildAt(newIconIndex);
185 break;
186 }
187 if (child != null) {
188 child.requestFocus();
189 playSoundEffect(keyCode, v);
Sunny Goyal290800b2015-03-05 11:33:33 -0800190 } else {
191 handleNoopKey(keyCode, v);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800192 }
193 return consume;
194 }
195
Sunny Goyal290800b2015-03-05 11:33:33 -0800196 public void handleNoopKey(int keyCode, View v) { }
197 }
198
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800199 /**
200 * Handles key events in the workspace hot seat (bottom of the screen).
201 * <p>Currently we don't special case for the phone UI in different orientations, even though
202 * the hotseat is on the side in landscape mode. This is to ensure that accessibility
203 * consistency is maintained across rotations.
204 */
205 static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) {
206 boolean consume = FocusLogic.shouldConsume(keyCode);
207 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
208 return consume;
209 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800210
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700211 LauncherAppState app = LauncherAppState.getInstance();
212 DeviceProfile profile = app.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:
360 int row = FocusLogic.findRow(matrix, iconIndex);
361 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
362 if (parent != null) {
363 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800364 matrix = FocusLogic.createSparseMatrix(iconLayout,
Hyunyoung Song38531712015-03-03 19:25:16 -0800365 iconLayout.getCountX(), row);
366 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY, matrix,
367 FocusLogic.PIVOT, pageIndex - 1, pageCount);
368 newIcon = parent.getChildAt(newIconIndex);
369 }
370 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800371 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
372 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
373 newIcon = parent.getChildAt(0);
374 workspace.snapToPage(pageIndex - 1);
Hyunyoung Song38531712015-03-03 19:25:16 -0800375 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800376 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
377 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
378 newIcon = parent.getChildAt(parent.getChildCount() - 1);
379 workspace.snapToPage(pageIndex - 1);
380 break;
381 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
382 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
383 newIcon = parent.getChildAt(0);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800384 workspace.snapToPage(pageIndex + 1);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800385 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800386 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
387 row = FocusLogic.findRow(matrix, iconIndex);
388 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
389 if (parent != null) {
390 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800391 matrix = FocusLogic.createSparseMatrix(iconLayout, -1, row);
Hyunyoung Song38531712015-03-03 19:25:16 -0800392 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY, matrix,
393 FocusLogic.PIVOT, pageIndex, pageCount);
394 newIcon = parent.getChildAt(newIconIndex);
395 }
396 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800397 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
398 newIcon = parent.getChildAt(0);
399 break;
400 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
401 newIcon = parent.getChildAt(parent.getChildCount() - 1);
402 break;
403 default:
404 // current page, some item.
405 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
406 newIcon = parent.getChildAt(newIconIndex);
407 } else if (parent.getChildCount() <= newIconIndex &&
408 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
409 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
410 }
411 break;
412 }
413 if (newIcon != null) {
414 newIcon.requestFocus();
415 playSoundEffect(keyCode, v);
416 }
417 return consume;
418 }
419
420 /**
421 * Handles key events for items in a Folder.
422 */
423 static boolean handleFolderKeyEvent(View v, int keyCode, KeyEvent e) {
424 boolean consume = FocusLogic.shouldConsume(keyCode);
425 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
426 return consume;
427 }
428 if (DEBUG) {
429 Log.v(TAG, String.format("Handle FOLDER keyevent=[%s].",
430 KeyEvent.keyCodeToString(keyCode)));
431 }
432
433 // Initialize the variables.
434 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
435 final CellLayout layout = (CellLayout) parent.getParent();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800436 final Folder folder = (Folder) layout.getParent().getParent();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800437 View title = folder.mFolderName;
438 Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
439 final int countX = layout.getCountX();
440 final int countY = layout.getCountY();
441 final int iconIndex = findIndexOfView(parent, v);
442 int pageIndex = workspace.indexOfChild(layout);
443 int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800444 int[][] map = FocusLogic.createFullMatrix(countX, countY, true /* incremental order */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800445
446 // Process the focus.
447 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX, countY, map, iconIndex,
448 pageIndex, pageCount);
449 View newIcon = null;
450 switch (newIconIndex) {
451 case FocusLogic.NOOP:
452 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
453 newIcon = title;
454 }
455 break;
456 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
457 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
458 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
459 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
460 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
461 if (DEBUG) {
462 Log.v(TAG, "Page advance handling not supported on folder icons.");
463 }
464 break;
465 default: // current page some item.
466 newIcon = parent.getChildAt(newIconIndex);
467 break;
468 }
469 if (newIcon != null) {
470 newIcon.requestFocus();
471 playSoundEffect(keyCode, v);
472 }
473 return consume;
474 }
475
476 //
477 // Helper methods.
478 //
479
Winson Chung97d85d22011-04-13 11:27:36 -0700480 /**
Adam Cohenae4f1552011-10-20 00:15:42 -0700481 * Returns the Viewgroup containing page contents for the page at the index specified.
482 */
483 private static ViewGroup getAppsCustomizePage(ViewGroup container, int index) {
484 ViewGroup page = (ViewGroup) ((PagedView) container).getPageAt(index);
Winson Chungc58497e2013-09-03 17:48:37 -0700485 if (page instanceof CellLayout) {
Adam Cohenae4f1552011-10-20 00:15:42 -0700486 // There are two layers, a PagedViewCellLayout and PagedViewCellLayoutChildren
Winson Chungc58497e2013-09-03 17:48:37 -0700487 page = ((CellLayout) page).getShortcutsAndWidgets();
Adam Cohenae4f1552011-10-20 00:15:42 -0700488 }
489 return page;
490 }
491
492 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700493 * Private helper method to get the CellLayoutChildren given a CellLayout index.
494 */
Michael Jurkaa52570f2012-03-20 03:18:20 -0700495 private static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
496 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700497 CellLayout parent = (CellLayout) container.getChildAt(i);
498 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700499 }
500
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800501 private static int findIndexOfView(ViewGroup parent, View v) {
502 for (int i = 0; i < parent.getChildCount(); i++) {
503 if (v != null && v.equals(parent.getChildAt(i))) {
504 return i;
Winson Chung97d85d22011-04-13 11:27:36 -0700505 }
506 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800507 return -1;
Winson Chung97d85d22011-04-13 11:27:36 -0700508 }
509
510 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800511 * Helper method to be used for playing sound effects.
Winson Chung97d85d22011-04-13 11:27:36 -0700512 */
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800513 private static void playSoundEffect(int keyCode, View v) {
Winson Chung97d85d22011-04-13 11:27:36 -0700514 switch (keyCode) {
515 case KeyEvent.KEYCODE_DPAD_LEFT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800516 v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
Winson Chung97d85d22011-04-13 11:27:36 -0700517 break;
518 case KeyEvent.KEYCODE_DPAD_RIGHT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800519 v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
Winson Chung97d85d22011-04-13 11:27:36 -0700520 break;
521 case KeyEvent.KEYCODE_DPAD_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700522 case KeyEvent.KEYCODE_PAGE_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700523 case KeyEvent.KEYCODE_MOVE_END:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800524 v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
Adam Cohenac56cff2011-09-28 20:45:37 -0700525 break;
526 case KeyEvent.KEYCODE_DPAD_UP:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800527 case KeyEvent.KEYCODE_PAGE_UP:
Adam Cohenac56cff2011-09-28 20:45:37 -0700528 case KeyEvent.KEYCODE_MOVE_HOME:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800529 v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
Adam Cohenac56cff2011-09-28 20:45:37 -0700530 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800531 default:
Winson Chung97d85d22011-04-13 11:27:36 -0700532 break;
Winson Chung97d85d22011-04-13 11:27:36 -0700533 }
Winson Chung97d85d22011-04-13 11:27:36 -0700534 }
535}