blob: d59c64431ec5f30942119de8a2a6b0cb2c07f6bb [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
Tony Wickham0fa5ada2015-11-13 17:32:20 -080048/**
49 * A keyboard listener we set on full screen pages (e.g. custom content).
50 */
51class FullscreenKeyEventListener implements View.OnKeyListener {
52 @Override
53 public boolean onKey(View v, int keyCode, KeyEvent event) {
54 if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT
55 || keyCode == KeyEvent.KEYCODE_PAGE_DOWN || keyCode == KeyEvent.KEYCODE_PAGE_UP) {
56 // Handle the key event just like a workspace icon would in these cases. In this case,
57 // it will basically act as if there is a single icon in the top left (so you could
58 // think of the fullscreen page as a focusable fullscreen widget).
59 return FocusHelper.handleIconKeyEvent(v, keyCode, event);
60 }
61 return false;
62 }
63}
64
Winson Chung97d85d22011-04-13 11:27:36 -070065public class FocusHelper {
Winson Chung97d85d22011-04-13 11:27:36 -070066
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080067 private static final String TAG = "FocusHelper";
68 private static final boolean DEBUG = false;
69
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080070 /**
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070071 * Handles key events in paged folder.
Sunny Goyal290800b2015-03-05 11:33:33 -080072 */
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070073 public static class PagedFolderKeyEventListener implements View.OnKeyListener {
Sunny Goyal290800b2015-03-05 11:33:33 -080074
75 private final Folder mFolder;
76
77 public PagedFolderKeyEventListener(Folder folder) {
78 mFolder = folder;
79 }
80
81 @Override
Hyunyoung Songada50982015-04-10 14:35:23 -070082 public boolean onKey(View v, int keyCode, KeyEvent e) {
83 boolean consume = FocusLogic.shouldConsume(keyCode);
84 if (e.getAction() == KeyEvent.ACTION_UP) {
85 return consume;
Sunny Goyal290800b2015-03-05 11:33:33 -080086 }
Hyunyoung Songada50982015-04-10 14:35:23 -070087 if (DEBUG) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070088 Log.v(TAG, String.format("Handle ALL Folders keyevent=[%s].",
Hyunyoung Songada50982015-04-10 14:35:23 -070089 KeyEvent.keyCodeToString(keyCode)));
90 }
Sunny Goyal290800b2015-03-05 11:33:33 -080091
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070092 if (!(v.getParent() instanceof ShortcutAndWidgetContainer)) {
Hyunyoung Songada50982015-04-10 14:35:23 -070093 if (LauncherAppState.isDogfoodBuild()) {
94 throw new IllegalStateException("Parent of the focused item is not supported.");
95 } else {
96 return false;
97 }
98 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080099
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700100 // Initialize variables.
101 final ShortcutAndWidgetContainer itemContainer = (ShortcutAndWidgetContainer) v.getParent();
102 final CellLayout cellLayout = (CellLayout) itemContainer.getParent();
Hyunyoung Songada50982015-04-10 14:35:23 -0700103
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700104 final int iconIndex = itemContainer.indexOfChild(v);
105 final FolderPagedView pagedView = (FolderPagedView) cellLayout.getParent();
106
107 final int pageIndex = pagedView.indexOfChild(cellLayout);
108 final int pageCount = pagedView.getPageCount();
Sunny Goyalc6205602015-05-21 20:46:33 -0700109 final boolean isLayoutRtl = Utilities.isRtl(v.getResources());
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700110
111 int[][] matrix = FocusLogic.createSparseMatrix(cellLayout);
Hyunyoung Songada50982015-04-10 14:35:23 -0700112 // Process focus.
Tony Wickham329d8bf2015-12-04 09:48:17 -0800113 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex,
114 pageCount, isLayoutRtl);
Hyunyoung Songada50982015-04-10 14:35:23 -0700115 if (newIconIndex == FocusLogic.NOOP) {
116 handleNoopKey(keyCode, v);
117 return consume;
118 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700119 ShortcutAndWidgetContainer newParent = null;
120 View child = null;
121
Hyunyoung Songada50982015-04-10 14:35:23 -0700122 switch (newIconIndex) {
123 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700124 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
125 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700126 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700127 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
128 pagedView.snapToPage(pageIndex - 1);
129 child = newParent.getChildAt(
130 ((newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN)
Tony Wickham329d8bf2015-12-04 09:48:17 -0800131 ^ newParent.invertLayoutHorizontally()) ? 0 : matrix.length - 1,
132 row);
Hyunyoung Songada50982015-04-10 14:35:23 -0700133 }
134 break;
135 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700136 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700137 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700138 pagedView.snapToPage(pageIndex - 1);
139 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700140 }
141 break;
142 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700143 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);
Tony Wickham329d8bf2015-12-04 09:48:17 -0800146 child = newParent.getChildAt(matrix.length - 1, matrix[0].length - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700147 }
148 break;
149 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700150 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700151 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700152 pagedView.snapToPage(pageIndex + 1);
153 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700154 }
155 break;
156 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700157 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
158 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700159 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700160 pagedView.snapToPage(pageIndex + 1);
Tony Wickham25189852015-11-04 17:44:32 -0800161 child = FocusLogic.getAdjacentChildInNextFolderPage(
162 newParent, v, newIconIndex);
Hyunyoung Songada50982015-04-10 14:35:23 -0700163 }
164 break;
165 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700166 child = cellLayout.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700167 break;
168 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700169 child = pagedView.getLastItem();
Hyunyoung Songada50982015-04-10 14:35:23 -0700170 break;
171 default: // Go to some item on the current page.
172 child = itemContainer.getChildAt(newIconIndex);
173 break;
174 }
175 if (child != null) {
176 child.requestFocus();
177 playSoundEffect(keyCode, v);
178 } else {
179 handleNoopKey(keyCode, v);
180 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800181 return consume;
182 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800183
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700184 public void handleNoopKey(int keyCode, View v) {
185 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
186 mFolder.mFolderName.requestFocus();
187 playSoundEffect(keyCode, v);
188 }
189 }
Sunny Goyal290800b2015-03-05 11:33:33 -0800190 }
191
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800192 /**
Tony Wickham4fc82872015-11-10 16:52:14 -0800193 * Handles key events in the workspace hotseat (bottom of the screen).
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800194 * <p>Currently we don't special case for the phone UI in different orientations, even though
195 * the hotseat is on the side in landscape mode. This is to ensure that accessibility
196 * consistency is maintained across rotations.
197 */
198 static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) {
199 boolean consume = FocusLogic.shouldConsume(keyCode);
200 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
201 return consume;
202 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800203
Winsonc0b52fe2015-09-09 16:38:15 -0700204 final Launcher launcher = (Launcher) v.getContext();
205 final DeviceProfile profile = launcher.getDeviceProfile();
Adam Cohen2e6da152015-05-06 11:42:25 -0700206
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800207 if (DEBUG) {
208 Log.v(TAG, String.format(
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700209 "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s",
210 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800211 }
212
213 // Initialize the variables.
Winsonfa56b3f2015-09-14 12:01:13 -0700214 final Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800215 final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent();
216 final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800217
Winsonfa56b3f2015-09-14 12:01:13 -0700218 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700219 int pageIndex = workspace.getNextPage();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800220 int pageCount = workspace.getChildCount();
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700221 int iconIndex = hotseatParent.indexOfChild(v);
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700222 int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets()
223 .getChildAt(iconIndex).getLayoutParams()).cellX;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800224
225 final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700226 if (iconLayout == null) {
227 // This check is to guard against cases where key strokes rushes in when workspace
228 // child creation/deletion is still in flux. (e.g., during drop or fling
229 // animation.)
230 return consume;
231 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800232 final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets();
233
234 ViewGroup parent = null;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800235 int[][] matrix = null;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800236
237 if (keyCode == KeyEvent.KEYCODE_DPAD_UP &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700238 !profile.isVerticalBarLayout()) {
Tony Wickham329d8bf2015-12-04 09:48:17 -0800239 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout,
Tony Wickham6cbd2222015-11-09 17:51:08 -0800240 true /* hotseat horizontal */, profile.inv.hotseatAllAppsRank);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800241 iconIndex += iconParent.getChildCount();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800242 parent = iconParent;
243 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700244 profile.isVerticalBarLayout()) {
Tony Wickham329d8bf2015-12-04 09:48:17 -0800245 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout,
Tony Wickham6cbd2222015-11-09 17:51:08 -0800246 false /* hotseat horizontal */, profile.inv.hotseatAllAppsRank);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800247 iconIndex += iconParent.getChildCount();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800248 parent = iconParent;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800249 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700250 profile.isVerticalBarLayout()) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800251 keyCode = KeyEvent.KEYCODE_PAGE_DOWN;
Winsonfa56b3f2015-09-14 12:01:13 -0700252 } else if (isUninstallKeyChord(e)) {
253 matrix = FocusLogic.createSparseMatrix(iconLayout);
254 if (UninstallDropTarget.supportsDrop(launcher, itemInfo)) {
255 UninstallDropTarget.startUninstallActivity(launcher, itemInfo);
256 }
257 } else if (isDeleteKeyChord(e)) {
258 matrix = FocusLogic.createSparseMatrix(iconLayout);
Winson2949fb52015-09-24 09:56:11 -0700259 launcher.removeItem(v, itemInfo, true /* deleteFromDb */);
Winsonc0b52fe2015-09-09 16:38:15 -0700260 } 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 Song31178b82015-02-24 14:12:51 -0800264 parent = hotseatParent;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800265 }
266
267 // Process the focus.
Tony Wickham329d8bf2015-12-04 09:48:17 -0800268 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex,
269 pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800270
Hyunyoung Song31178b82015-02-24 14:12:51 -0800271 View newIcon = null;
Tony Wickham4fc82872015-11-10 16:52:14 -0800272 switch (newIconIndex) {
273 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
274 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
275 newIcon = parent.getChildAt(0);
276 // TODO(hyunyoungs): handle cases where the child is not an icon but
277 // a folder or a widget.
278 workspace.snapToPage(pageIndex + 1);
279 break;
280 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
281 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
282 newIcon = parent.getChildAt(0);
283 // TODO(hyunyoungs): handle cases where the child is not an icon but
284 // a folder or a widget.
285 workspace.snapToPage(pageIndex - 1);
286 break;
287 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
288 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
289 newIcon = parent.getChildAt(parent.getChildCount() - 1);
290 // TODO(hyunyoungs): handle cases where the child is not an icon but
291 // a folder or a widget.
292 workspace.snapToPage(pageIndex - 1);
293 break;
294 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
295 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
296 // Go to the previous page but keep the focus on the same hotseat icon.
297 workspace.snapToPage(pageIndex - 1);
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800298 // If the page we are going to is fullscreen, have it take the focus from hotseat.
299 CellLayout prevPage = (CellLayout) workspace.getPageAt(pageIndex - 1);
300 boolean isPrevPageFullscreen = ((CellLayout.LayoutParams) prevPage
301 .getShortcutsAndWidgets().getChildAt(0).getLayoutParams()).isFullscreen;
302 if (isPrevPageFullscreen) {
303 workspace.getPageAt(pageIndex - 1).requestFocus();
304 }
Tony Wickham4fc82872015-11-10 16:52:14 -0800305 break;
306 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
307 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
308 // Go to the next page but keep the focus on the same hotseat icon.
309 workspace.snapToPage(pageIndex + 1);
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800310 // If the page we are going to is fullscreen, have it take the focus from hotseat.
311 CellLayout nextPage = (CellLayout) workspace.getPageAt(pageIndex + 1);
312 boolean isNextPageFullscreen = ((CellLayout.LayoutParams) nextPage
313 .getShortcutsAndWidgets().getChildAt(0).getLayoutParams()).isFullscreen;
314 if (isNextPageFullscreen) {
315 workspace.getPageAt(pageIndex + 1).requestFocus();
316 }
Tony Wickham4fc82872015-11-10 16:52:14 -0800317 break;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800318 }
319 if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800320 newIconIndex -= iconParent.getChildCount();
321 }
322 if (parent != null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800323 if (newIcon == null && newIconIndex >= 0) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800324 newIcon = parent.getChildAt(newIconIndex);
325 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800326 if (newIcon != null) {
327 newIcon.requestFocus();
328 playSoundEffect(keyCode, v);
329 }
330 }
331 return consume;
332 }
333
334 /**
335 * Handles key events in a workspace containing icons.
336 */
337 static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) {
338 boolean consume = FocusLogic.shouldConsume(keyCode);
339 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
340 return consume;
341 }
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700342
Adam Cohen2e6da152015-05-06 11:42:25 -0700343 Launcher launcher = (Launcher) v.getContext();
344 DeviceProfile profile = launcher.getDeviceProfile();
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700345
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800346 if (DEBUG) {
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700347 Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s",
348 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800349 }
350
351 // Initialize the variables.
352 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
Hyunyoung Song38531712015-03-03 19:25:16 -0800353 CellLayout iconLayout = (CellLayout) parent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800354 final Workspace workspace = (Workspace) iconLayout.getParent();
Adam Cohen2e6da152015-05-06 11:42:25 -0700355 final ViewGroup dragLayer = (ViewGroup) workspace.getParent();
356 final ViewGroup tabs = (ViewGroup) dragLayer.findViewById(R.id.search_drop_target_bar);
357 final Hotseat hotseat = (Hotseat) dragLayer.findViewById(R.id.hotseat);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700358
Winsonfa56b3f2015-09-14 12:01:13 -0700359 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700360 final int iconIndex = parent.indexOfChild(v);
361 final int pageIndex = workspace.indexOfChild(iconLayout);
362 final int pageCount = workspace.getChildCount();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800363
364 CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0);
365 ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets();
366 int[][] matrix;
367
Hyunyoung Song31178b82015-02-24 14:12:51 -0800368 // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800369 // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended
370 // with the hotseat.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700371 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) {
Tony Wickham329d8bf2015-12-04 09:48:17 -0800372 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout,
373 true /* horizontal */, profile.inv.hotseatAllAppsRank);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800374 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700375 profile.isVerticalBarLayout()) {
Tony Wickham329d8bf2015-12-04 09:48:17 -0800376 matrix = FocusLogic.createSparseMatrixWithHotseat(iconLayout, hotseatLayout,
377 false /* horizontal */, profile.inv.hotseatAllAppsRank);
Winsonfa56b3f2015-09-14 12:01:13 -0700378 } else if (isUninstallKeyChord(e)) {
379 matrix = FocusLogic.createSparseMatrix(iconLayout);
380 if (UninstallDropTarget.supportsDrop(launcher, itemInfo)) {
381 UninstallDropTarget.startUninstallActivity(launcher, itemInfo);
382 }
383 } else if (isDeleteKeyChord(e)) {
384 matrix = FocusLogic.createSparseMatrix(iconLayout);
Winson2949fb52015-09-24 09:56:11 -0700385 launcher.removeItem(v, itemInfo, true /* deleteFromDb */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800386 } else {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800387 matrix = FocusLogic.createSparseMatrix(iconLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800388 }
389
390 // Process the focus.
Tony Wickham329d8bf2015-12-04 09:48:17 -0800391 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, iconIndex, pageIndex,
392 pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800393 boolean isRtl = Utilities.isRtl(v.getResources());
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800394 View newIcon = null;
Tony Wickhamaf78b592015-11-11 09:25:38 -0800395 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800396 switch (newIconIndex) {
397 case FocusLogic.NOOP:
398 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
399 newIcon = tabs;
400 }
401 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800402 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700403 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
404 int newPageIndex = pageIndex - 1;
405 if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) {
406 newPageIndex = pageIndex + 1;
407 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700408 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700409 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800410 if (parent != null) {
411 iconLayout = (CellLayout) parent.getParent();
Tony Wickham329d8bf2015-12-04 09:48:17 -0800412 matrix = FocusLogic.createSparseMatrixWithPivotColumn(iconLayout,
Tony Wickham4fc82872015-11-10 16:52:14 -0800413 iconLayout.getCountX(), row);
Tony Wickham329d8bf2015-12-04 09:48:17 -0800414 newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, FocusLogic.PIVOT,
415 newPageIndex, pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800416 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
417 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex,
418 isRtl);
419 } else if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LAST_ITEM) {
420 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex,
421 isRtl);
422 } else {
423 newIcon = parent.getChildAt(newIconIndex);
424 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800425 }
426 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800427 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800428 workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex - 1);
429 newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
430 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800431 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800432 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
433 workspace.snapToPage(pageIndex - 1);
434 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800435 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800436 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800437 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex, isRtl);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800438 break;
439 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800440 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex, isRtl);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800441 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800442 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700443 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
444 newPageIndex = pageIndex + 1;
445 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
446 newPageIndex = pageIndex - 1;
447 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700448 row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700449 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800450 if (parent != null) {
451 iconLayout = (CellLayout) parent.getParent();
Tony Wickham329d8bf2015-12-04 09:48:17 -0800452 matrix = FocusLogic.createSparseMatrixWithPivotColumn(iconLayout, -1, row);
453 newIconIndex = FocusLogic.handleKeyEvent(keyCode, matrix, FocusLogic.PIVOT,
454 newPageIndex, pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800455 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
456 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex,
457 isRtl);
458 } else if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LAST_ITEM) {
459 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex,
460 isRtl);
461 } else {
462 newIcon = parent.getChildAt(newIconIndex);
463 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800464 }
465 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800466 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800467 newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
468 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800469 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800470 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
471 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800472 break;
473 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800474 newIcon = getFirstFocusableIconInReverseReadingOrder(workspaceLayout, isRtl);
475 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800476 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800477 newIcon = getFirstFocusableIconInReverseReadingOrder(hotseatLayout, isRtl);
478 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800479 break;
480 default:
481 // current page, some item.
482 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
483 newIcon = parent.getChildAt(newIconIndex);
484 } else if (parent.getChildCount() <= newIconIndex &&
485 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
486 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
487 }
488 break;
489 }
490 if (newIcon != null) {
491 newIcon.requestFocus();
492 playSoundEffect(keyCode, v);
493 }
494 return consume;
495 }
496
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800497 //
498 // Helper methods.
499 //
500
Winson Chung97d85d22011-04-13 11:27:36 -0700501 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700502 * Private helper method to get the CellLayoutChildren given a CellLayout index.
503 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700504 @Thunk static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
Michael Jurkaa52570f2012-03-20 03:18:20 -0700505 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700506 CellLayout parent = (CellLayout) container.getChildAt(i);
507 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700508 }
509
Winson Chung97d85d22011-04-13 11:27:36 -0700510 /**
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 */
Adam Cohen091440a2015-03-18 14:16:05 -0700513 @Thunk 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 }
Winsonfa56b3f2015-09-14 12:01:13 -0700535
536 /**
537 * Returns whether the key event represents a valid uninstall key chord.
538 */
539 private static boolean isUninstallKeyChord(KeyEvent event) {
540 int keyCode = event.getKeyCode();
541 return (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) &&
542 event.hasModifiers(KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON);
543 }
544
545 /**
546 * Returns whether the key event represents a valid delete key chord.
547 */
548 private static boolean isDeleteKeyChord(KeyEvent event) {
549 int keyCode = event.getKeyCode();
550 return (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) &&
551 event.hasModifiers(KeyEvent.META_CTRL_ON);
552 }
Tony Wickhamaf78b592015-11-11 09:25:38 -0800553
554 private static View handlePreviousPageLastItem(Workspace workspace, CellLayout hotseatLayout,
555 int pageIndex, boolean isRtl) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800556 if (pageIndex - 1 < 0) {
557 return null;
558 }
Tony Wickhamaf78b592015-11-11 09:25:38 -0800559 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex - 1);
560 View newIcon = getFirstFocusableIconInReverseReadingOrder(workspaceLayout, isRtl);
561 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800562 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800563 newIcon = getFirstFocusableIconInReverseReadingOrder(hotseatLayout,isRtl);
564 workspace.snapToPage(pageIndex - 1);
565 }
566 return newIcon;
567 }
568
569 private static View handleNextPageFirstItem(Workspace workspace, CellLayout hotseatLayout,
570 int pageIndex, boolean isRtl) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800571 if (pageIndex + 1 >= workspace.getPageCount()) {
572 return null;
573 }
Tony Wickhamaf78b592015-11-11 09:25:38 -0800574 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex + 1);
575 View newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
576 if (newIcon == null) {
Tony Wickham0fa5ada2015-11-13 17:32:20 -0800577 // Check the hotseat if no focusable item was found on the workspace.
Tony Wickhamaf78b592015-11-11 09:25:38 -0800578 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
579 workspace.snapToPage(pageIndex + 1);
580 }
581 return newIcon;
582 }
583
584 private static View getFirstFocusableIconInReadingOrder(CellLayout cellLayout, boolean isRtl) {
585 View icon;
586 int countX = cellLayout.getCountX();
587 for (int y = 0; y < cellLayout.getCountY(); y++) {
588 int increment = isRtl ? -1 : 1;
589 for (int x = isRtl ? countX - 1 : 0; 0 <= x && x < countX; x += increment) {
590 if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
591 return icon;
592 }
593 }
594 }
595 return null;
596 }
597
598 private static View getFirstFocusableIconInReverseReadingOrder(CellLayout cellLayout,
599 boolean isRtl) {
600 View icon;
601 int countX = cellLayout.getCountX();
602 for (int y = cellLayout.getCountY() - 1; y >= 0; y--) {
603 int increment = isRtl ? 1 : -1;
604 for (int x = isRtl ? 0 : countX - 1; 0 <= x && x < countX; x += increment) {
605 if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
606 return icon;
607 }
608 }
609 }
610 return null;
611 }
Winson Chung97d85d22011-04-13 11:27:36 -0700612}