blob: eca5eec2a1c80d981eee1e38c93010262dde0125 [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();
Sunny Goyalc6205602015-05-21 20:46:33 -070095 final boolean isLayoutRtl = Utilities.isRtl(v.getResources());
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070096
97 int[][] matrix = FocusLogic.createSparseMatrix(cellLayout);
Hyunyoung Songada50982015-04-10 14:35:23 -070098 // Process focus.
Adam Cohen2e6da152015-05-06 11:42:25 -070099 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700100 countY, matrix, iconIndex, pageIndex, pageCount, isLayoutRtl);
Hyunyoung Songada50982015-04-10 14:35:23 -0700101 if (newIconIndex == FocusLogic.NOOP) {
102 handleNoopKey(keyCode, v);
103 return consume;
104 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700105 ShortcutAndWidgetContainer newParent = null;
106 View child = null;
107
Hyunyoung Songada50982015-04-10 14:35:23 -0700108 switch (newIconIndex) {
109 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700110 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
111 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700112 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700113 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
114 pagedView.snapToPage(pageIndex - 1);
115 child = newParent.getChildAt(
116 ((newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN)
117 ^ newParent.invertLayoutHorizontally()) ? 0 : countX - 1, row);
Hyunyoung Songada50982015-04-10 14:35:23 -0700118 }
119 break;
120 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700121 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700122 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700123 pagedView.snapToPage(pageIndex - 1);
124 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700125 }
126 break;
127 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700128 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700129 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700130 pagedView.snapToPage(pageIndex - 1);
131 child = newParent.getChildAt(countX - 1, countY - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700132 }
133 break;
134 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700135 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700136 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700137 pagedView.snapToPage(pageIndex + 1);
138 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700139 }
140 break;
141 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700142 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
143 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700144 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700145 pagedView.snapToPage(pageIndex + 1);
146 child = FocusLogic.getAdjacentChildInNextPage(newParent, v, newIconIndex);
Hyunyoung Songada50982015-04-10 14:35:23 -0700147 }
148 break;
149 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700150 child = cellLayout.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700151 break;
152 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700153 child = pagedView.getLastItem();
Hyunyoung Songada50982015-04-10 14:35:23 -0700154 break;
155 default: // Go to some item on the current page.
156 child = itemContainer.getChildAt(newIconIndex);
157 break;
158 }
159 if (child != null) {
160 child.requestFocus();
161 playSoundEffect(keyCode, v);
162 } else {
163 handleNoopKey(keyCode, v);
164 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800165 return consume;
166 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800167
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700168 public void handleNoopKey(int keyCode, View v) {
169 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
170 mFolder.mFolderName.requestFocus();
171 playSoundEffect(keyCode, v);
172 }
173 }
Sunny Goyal290800b2015-03-05 11:33:33 -0800174 }
175
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800176 /**
177 * Handles key events in the workspace hot seat (bottom of the screen).
178 * <p>Currently we don't special case for the phone UI in different orientations, even though
179 * the hotseat is on the side in landscape mode. This is to ensure that accessibility
180 * consistency is maintained across rotations.
181 */
182 static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) {
183 boolean consume = FocusLogic.shouldConsume(keyCode);
184 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
185 return consume;
186 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800187
Winsonc0b52fe2015-09-09 16:38:15 -0700188 final Launcher launcher = (Launcher) v.getContext();
189 final DeviceProfile profile = launcher.getDeviceProfile();
Adam Cohen2e6da152015-05-06 11:42:25 -0700190
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
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800201 Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700202 int pageIndex = workspace.getNextPage();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800203 int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800204 int countX = -1;
205 int countY = -1;
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700206 int iconIndex = hotseatParent.indexOfChild(v);
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700207 int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets()
208 .getChildAt(iconIndex).getLayoutParams()).cellX;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800209
210 final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700211 if (iconLayout == null) {
212 // This check is to guard against cases where key strokes rushes in when workspace
213 // child creation/deletion is still in flux. (e.g., during drop or fling
214 // animation.)
215 return consume;
216 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800217 final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets();
218
219 ViewGroup parent = null;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800220 int[][] matrix = null;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800221
222 if (keyCode == KeyEvent.KEYCODE_DPAD_UP &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700223 !profile.isVerticalBarLayout()) {
224 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700225 true /* hotseat horizontal */, profile.inv.hotseatAllAppsRank,
226 iconRank == profile.inv.hotseatAllAppsRank /* include all apps icon */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800227 iconIndex += iconParent.getChildCount();
228 countX = iconLayout.getCountX();
229 countY = iconLayout.getCountY() + hotseatLayout.getCountY();
230 parent = iconParent;
231 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700232 profile.isVerticalBarLayout()) {
233 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700234 false /* hotseat horizontal */, profile.inv.hotseatAllAppsRank,
235 iconRank == profile.inv.hotseatAllAppsRank /* include all apps icon */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800236 iconIndex += iconParent.getChildCount();
237 countX = iconLayout.getCountX() + hotseatLayout.getCountX();
238 countY = iconLayout.getCountY();
239 parent = iconParent;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800240 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700241 profile.isVerticalBarLayout()) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800242 keyCode = KeyEvent.KEYCODE_PAGE_DOWN;
Winsonc0b52fe2015-09-09 16:38:15 -0700243 } else if (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) {
244 ItemInfo info = (ItemInfo) v.getTag();
245 launcher.removeItem(v, info, true /* deleteFromDb */);
246 } else {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800247 // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the
248 // matrix extended with hotseat.
249 matrix = FocusLogic.createSparseMatrix(hotseatLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800250 countX = hotseatLayout.getCountX();
251 countY = hotseatLayout.getCountY();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800252 parent = hotseatParent;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800253 }
254
255 // Process the focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700256 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700257 countY, matrix, iconIndex, pageIndex, pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800258
Hyunyoung Song31178b82015-02-24 14:12:51 -0800259 View newIcon = null;
260 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
261 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
262 newIcon = parent.getChildAt(0);
263 // TODO(hyunyoungs): handle cases where the child is not an icon but
264 // a folder or a widget.
265 workspace.snapToPage(pageIndex + 1);
266 }
267 if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800268 newIconIndex -= iconParent.getChildCount();
269 }
270 if (parent != null) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800271 if (newIcon == null && newIconIndex >=0) {
272 newIcon = parent.getChildAt(newIconIndex);
273 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800274 if (newIcon != null) {
275 newIcon.requestFocus();
276 playSoundEffect(keyCode, v);
277 }
278 }
279 return consume;
280 }
281
282 /**
283 * Handles key events in a workspace containing icons.
284 */
285 static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) {
286 boolean consume = FocusLogic.shouldConsume(keyCode);
287 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
288 return consume;
289 }
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700290
Adam Cohen2e6da152015-05-06 11:42:25 -0700291 Launcher launcher = (Launcher) v.getContext();
292 DeviceProfile profile = launcher.getDeviceProfile();
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700293
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800294 if (DEBUG) {
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700295 Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s",
296 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800297 }
298
299 // Initialize the variables.
300 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
Hyunyoung Song38531712015-03-03 19:25:16 -0800301 CellLayout iconLayout = (CellLayout) parent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800302 final Workspace workspace = (Workspace) iconLayout.getParent();
Adam Cohen2e6da152015-05-06 11:42:25 -0700303 final ViewGroup dragLayer = (ViewGroup) workspace.getParent();
304 final ViewGroup tabs = (ViewGroup) dragLayer.findViewById(R.id.search_drop_target_bar);
305 final Hotseat hotseat = (Hotseat) dragLayer.findViewById(R.id.hotseat);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700306
307 final int iconIndex = parent.indexOfChild(v);
308 final int pageIndex = workspace.indexOfChild(iconLayout);
309 final int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800310 int countX = iconLayout.getCountX();
311 int countY = iconLayout.getCountY();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800312
313 CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0);
314 ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets();
315 int[][] matrix;
316
Hyunyoung Song31178b82015-02-24 14:12:51 -0800317 // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800318 // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended
319 // with the hotseat.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700320 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) {
321 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, true /* horizontal */,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700322 profile.inv.hotseatAllAppsRank,
Winson Chungc393b072015-05-20 15:03:13 -0700323 !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800324 countY = countY + 1;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800325 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700326 profile.isVerticalBarLayout()) {
327 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, false /* horizontal */,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700328 profile.inv.hotseatAllAppsRank,
Winson Chungc393b072015-05-20 15:03:13 -0700329 !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800330 countX = countX + 1;
331 } else if (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) {
Winsonc0b52fe2015-09-09 16:38:15 -0700332 ItemInfo info = (ItemInfo) v.getTag();
333 launcher.removeItem(v, info, true /* deleteFromDb */);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800334 return consume;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800335 } else {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800336 matrix = FocusLogic.createSparseMatrix(iconLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800337 }
338
339 // Process the focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700340 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700341 countY, matrix, iconIndex, pageIndex, pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800342 View newIcon = null;
343 switch (newIconIndex) {
344 case FocusLogic.NOOP:
345 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
346 newIcon = tabs;
347 }
348 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800349 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700350 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
351 int newPageIndex = pageIndex - 1;
352 if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) {
353 newPageIndex = pageIndex + 1;
354 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700355 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700356 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700357 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800358 if (parent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700359 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800360 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800361 matrix = FocusLogic.createSparseMatrix(iconLayout,
Hyunyoung Song38531712015-03-03 19:25:16 -0800362 iconLayout.getCountX(), row);
Adam Cohen2e6da152015-05-06 11:42:25 -0700363 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY,
Sunny Goyalc6205602015-05-21 20:46:33 -0700364 matrix, FocusLogic.PIVOT, newPageIndex, pageCount,
365 Utilities.isRtl(v.getResources()));
Hyunyoung Song38531712015-03-03 19:25:16 -0800366 newIcon = parent.getChildAt(newIconIndex);
367 }
368 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800369 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
370 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
371 newIcon = parent.getChildAt(0);
372 workspace.snapToPage(pageIndex - 1);
Hyunyoung Song38531712015-03-03 19:25:16 -0800373 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800374 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
375 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
376 newIcon = parent.getChildAt(parent.getChildCount() - 1);
377 workspace.snapToPage(pageIndex - 1);
378 break;
379 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
380 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
381 newIcon = parent.getChildAt(0);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800382 workspace.snapToPage(pageIndex + 1);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800383 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800384 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700385 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
386 newPageIndex = pageIndex + 1;
387 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
388 newPageIndex = pageIndex - 1;
389 }
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700390 workspace.snapToPage(newPageIndex);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700391 row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700392 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800393 if (parent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700394 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800395 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800396 matrix = FocusLogic.createSparseMatrix(iconLayout, -1, row);
Adam Cohen2e6da152015-05-06 11:42:25 -0700397 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY,
Sunny Goyalc6205602015-05-21 20:46:33 -0700398 matrix, FocusLogic.PIVOT, newPageIndex, pageCount,
399 Utilities.isRtl(v.getResources()));
Hyunyoung Song38531712015-03-03 19:25:16 -0800400 newIcon = parent.getChildAt(newIconIndex);
401 }
402 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800403 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
404 newIcon = parent.getChildAt(0);
405 break;
406 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
407 newIcon = parent.getChildAt(parent.getChildCount() - 1);
408 break;
409 default:
410 // current page, some item.
411 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
412 newIcon = parent.getChildAt(newIconIndex);
413 } else if (parent.getChildCount() <= newIconIndex &&
414 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
415 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
416 }
417 break;
418 }
419 if (newIcon != null) {
420 newIcon.requestFocus();
421 playSoundEffect(keyCode, v);
422 }
423 return consume;
424 }
425
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800426 //
427 // Helper methods.
428 //
429
Winson Chung97d85d22011-04-13 11:27:36 -0700430 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700431 * Private helper method to get the CellLayoutChildren given a CellLayout index.
432 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700433 @Thunk static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
Michael Jurkaa52570f2012-03-20 03:18:20 -0700434 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700435 CellLayout parent = (CellLayout) container.getChildAt(i);
436 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700437 }
438
Winson Chung97d85d22011-04-13 11:27:36 -0700439 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800440 * Helper method to be used for playing sound effects.
Winson Chung97d85d22011-04-13 11:27:36 -0700441 */
Adam Cohen091440a2015-03-18 14:16:05 -0700442 @Thunk static void playSoundEffect(int keyCode, View v) {
Winson Chung97d85d22011-04-13 11:27:36 -0700443 switch (keyCode) {
444 case KeyEvent.KEYCODE_DPAD_LEFT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800445 v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
Winson Chung97d85d22011-04-13 11:27:36 -0700446 break;
447 case KeyEvent.KEYCODE_DPAD_RIGHT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800448 v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
Winson Chung97d85d22011-04-13 11:27:36 -0700449 break;
450 case KeyEvent.KEYCODE_DPAD_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700451 case KeyEvent.KEYCODE_PAGE_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700452 case KeyEvent.KEYCODE_MOVE_END:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800453 v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
Adam Cohenac56cff2011-09-28 20:45:37 -0700454 break;
455 case KeyEvent.KEYCODE_DPAD_UP:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800456 case KeyEvent.KEYCODE_PAGE_UP:
Adam Cohenac56cff2011-09-28 20:45:37 -0700457 case KeyEvent.KEYCODE_MOVE_HOME:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800458 v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
Adam Cohenac56cff2011-09-28 20:45:37 -0700459 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800460 default:
Winson Chung97d85d22011-04-13 11:27:36 -0700461 break;
Winson Chung97d85d22011-04-13 11:27:36 -0700462 }
Winson Chung97d85d22011-04-13 11:27:36 -0700463 }
464}