blob: d5eac9b2ea5ad45035365b07b5e099553ab737b9 [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 if (!(v.getParent() instanceof ShortcutAndWidgetContainer)) {
Hyunyoung Songada50982015-04-10 14:35:23 -070076 if (LauncherAppState.isDogfoodBuild()) {
77 throw new IllegalStateException("Parent of the focused item is not supported.");
78 } else {
79 return false;
80 }
81 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080082
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070083 // Initialize variables.
84 final ShortcutAndWidgetContainer itemContainer = (ShortcutAndWidgetContainer) v.getParent();
85 final CellLayout cellLayout = (CellLayout) itemContainer.getParent();
86 final int countX = cellLayout.getCountX();
87 final int countY = cellLayout.getCountY();
Hyunyoung Songada50982015-04-10 14:35:23 -070088
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070089 final int iconIndex = itemContainer.indexOfChild(v);
90 final FolderPagedView pagedView = (FolderPagedView) cellLayout.getParent();
91
92 final int pageIndex = pagedView.indexOfChild(cellLayout);
93 final int pageCount = pagedView.getPageCount();
Sunny Goyalc6205602015-05-21 20:46:33 -070094 final boolean isLayoutRtl = Utilities.isRtl(v.getResources());
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070095
96 int[][] matrix = FocusLogic.createSparseMatrix(cellLayout);
Hyunyoung Songada50982015-04-10 14:35:23 -070097 // Process focus.
Adam Cohen2e6da152015-05-06 11:42:25 -070098 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -070099 countY, matrix, iconIndex, pageIndex, pageCount, isLayoutRtl);
Hyunyoung Songada50982015-04-10 14:35:23 -0700100 if (newIconIndex == FocusLogic.NOOP) {
101 handleNoopKey(keyCode, v);
102 return consume;
103 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700104 ShortcutAndWidgetContainer newParent = null;
105 View child = null;
106
Hyunyoung Songada50982015-04-10 14:35:23 -0700107 switch (newIconIndex) {
108 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700109 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
110 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700111 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700112 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
113 pagedView.snapToPage(pageIndex - 1);
114 child = newParent.getChildAt(
115 ((newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN)
116 ^ newParent.invertLayoutHorizontally()) ? 0 : countX - 1, row);
Hyunyoung Songada50982015-04-10 14:35:23 -0700117 }
118 break;
119 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700120 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700121 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700122 pagedView.snapToPage(pageIndex - 1);
123 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700124 }
125 break;
126 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700127 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700128 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700129 pagedView.snapToPage(pageIndex - 1);
130 child = newParent.getChildAt(countX - 1, countY - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700131 }
132 break;
133 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700134 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700135 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700136 pagedView.snapToPage(pageIndex + 1);
137 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700138 }
139 break;
140 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700141 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
142 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700143 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700144 pagedView.snapToPage(pageIndex + 1);
145 child = FocusLogic.getAdjacentChildInNextPage(newParent, v, newIconIndex);
Hyunyoung Songada50982015-04-10 14:35:23 -0700146 }
147 break;
148 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700149 child = cellLayout.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700150 break;
151 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700152 child = pagedView.getLastItem();
Hyunyoung Songada50982015-04-10 14:35:23 -0700153 break;
154 default: // Go to some item on the current page.
155 child = itemContainer.getChildAt(newIconIndex);
156 break;
157 }
158 if (child != null) {
159 child.requestFocus();
160 playSoundEffect(keyCode, v);
161 } else {
162 handleNoopKey(keyCode, v);
163 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800164 return consume;
165 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800166
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700167 public void handleNoopKey(int keyCode, View v) {
168 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
169 mFolder.mFolderName.requestFocus();
170 playSoundEffect(keyCode, v);
171 }
172 }
Sunny Goyal290800b2015-03-05 11:33:33 -0800173 }
174
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800175 /**
176 * Handles key events in the workspace hot seat (bottom of the screen).
177 * <p>Currently we don't special case for the phone UI in different orientations, even though
178 * the hotseat is on the side in landscape mode. This is to ensure that accessibility
179 * consistency is maintained across rotations.
180 */
181 static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) {
182 boolean consume = FocusLogic.shouldConsume(keyCode);
183 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
184 return consume;
185 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800186
Winsonc0b52fe2015-09-09 16:38:15 -0700187 final Launcher launcher = (Launcher) v.getContext();
188 final DeviceProfile profile = launcher.getDeviceProfile();
Adam Cohen2e6da152015-05-06 11:42:25 -0700189
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800190 if (DEBUG) {
191 Log.v(TAG, String.format(
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700192 "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s",
193 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800194 }
195
196 // Initialize the variables.
Winsonfa56b3f2015-09-14 12:01:13 -0700197 final Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800198 final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent();
199 final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800200
Winsonfa56b3f2015-09-14 12:01:13 -0700201 final ItemInfo itemInfo = (ItemInfo) v.getTag();
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;
Winsonfa56b3f2015-09-14 12:01:13 -0700243 } else if (isUninstallKeyChord(e)) {
244 matrix = FocusLogic.createSparseMatrix(iconLayout);
245 if (UninstallDropTarget.supportsDrop(launcher, itemInfo)) {
246 UninstallDropTarget.startUninstallActivity(launcher, itemInfo);
247 }
248 } else if (isDeleteKeyChord(e)) {
249 matrix = FocusLogic.createSparseMatrix(iconLayout);
Winson2949fb52015-09-24 09:56:11 -0700250 launcher.removeItem(v, itemInfo, true /* deleteFromDb */);
Winsonc0b52fe2015-09-09 16:38:15 -0700251 } else {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800252 // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the
253 // matrix extended with hotseat.
254 matrix = FocusLogic.createSparseMatrix(hotseatLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800255 countX = hotseatLayout.getCountX();
256 countY = hotseatLayout.getCountY();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800257 parent = hotseatParent;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800258 }
259
260 // Process the focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700261 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700262 countY, matrix, iconIndex, pageIndex, pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800263
Hyunyoung Song31178b82015-02-24 14:12:51 -0800264 View newIcon = null;
265 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
266 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
267 newIcon = parent.getChildAt(0);
268 // TODO(hyunyoungs): handle cases where the child is not an icon but
269 // a folder or a widget.
270 workspace.snapToPage(pageIndex + 1);
271 }
272 if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800273 newIconIndex -= iconParent.getChildCount();
274 }
275 if (parent != null) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800276 if (newIcon == null && newIconIndex >=0) {
277 newIcon = parent.getChildAt(newIconIndex);
278 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800279 if (newIcon != null) {
280 newIcon.requestFocus();
281 playSoundEffect(keyCode, v);
282 }
283 }
284 return consume;
285 }
286
287 /**
288 * Handles key events in a workspace containing icons.
289 */
290 static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) {
291 boolean consume = FocusLogic.shouldConsume(keyCode);
292 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
293 return consume;
294 }
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700295
Adam Cohen2e6da152015-05-06 11:42:25 -0700296 Launcher launcher = (Launcher) v.getContext();
297 DeviceProfile profile = launcher.getDeviceProfile();
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700298
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800299 if (DEBUG) {
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700300 Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s",
301 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800302 }
303
304 // Initialize the variables.
305 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
Hyunyoung Song38531712015-03-03 19:25:16 -0800306 CellLayout iconLayout = (CellLayout) parent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800307 final Workspace workspace = (Workspace) iconLayout.getParent();
Adam Cohen2e6da152015-05-06 11:42:25 -0700308 final ViewGroup dragLayer = (ViewGroup) workspace.getParent();
309 final ViewGroup tabs = (ViewGroup) dragLayer.findViewById(R.id.search_drop_target_bar);
310 final Hotseat hotseat = (Hotseat) dragLayer.findViewById(R.id.hotseat);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700311
Winsonfa56b3f2015-09-14 12:01:13 -0700312 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700313 final int iconIndex = parent.indexOfChild(v);
314 final int pageIndex = workspace.indexOfChild(iconLayout);
315 final int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800316 int countX = iconLayout.getCountX();
317 int countY = iconLayout.getCountY();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800318
319 CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0);
320 ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets();
321 int[][] matrix;
322
Hyunyoung Song31178b82015-02-24 14:12:51 -0800323 // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800324 // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended
325 // with the hotseat.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700326 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) {
327 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, true /* 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 Songee3e6a72015-02-20 14:25:27 -0800330 countY = countY + 1;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800331 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700332 profile.isVerticalBarLayout()) {
333 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, false /* horizontal */,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700334 profile.inv.hotseatAllAppsRank,
Winson Chungc393b072015-05-20 15:03:13 -0700335 !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800336 countX = countX + 1;
Winsonfa56b3f2015-09-14 12:01:13 -0700337 } else if (isUninstallKeyChord(e)) {
338 matrix = FocusLogic.createSparseMatrix(iconLayout);
339 if (UninstallDropTarget.supportsDrop(launcher, itemInfo)) {
340 UninstallDropTarget.startUninstallActivity(launcher, itemInfo);
341 }
342 } else if (isDeleteKeyChord(e)) {
343 matrix = FocusLogic.createSparseMatrix(iconLayout);
Winson2949fb52015-09-24 09:56:11 -0700344 launcher.removeItem(v, itemInfo, true /* deleteFromDb */);
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.
Adam Cohen2e6da152015-05-06 11:42:25 -0700350 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700351 countY, matrix, iconIndex, pageIndex, pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800352 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 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700365 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700366 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700367 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800368 if (parent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700369 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800370 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800371 matrix = FocusLogic.createSparseMatrix(iconLayout,
Hyunyoung Song38531712015-03-03 19:25:16 -0800372 iconLayout.getCountX(), row);
Adam Cohen2e6da152015-05-06 11:42:25 -0700373 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY,
Sunny Goyalc6205602015-05-21 20:46:33 -0700374 matrix, FocusLogic.PIVOT, newPageIndex, pageCount,
375 Utilities.isRtl(v.getResources()));
Hyunyoung Song38531712015-03-03 19:25:16 -0800376 newIcon = parent.getChildAt(newIconIndex);
377 }
378 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800379 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
380 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
381 newIcon = parent.getChildAt(0);
382 workspace.snapToPage(pageIndex - 1);
Hyunyoung Song38531712015-03-03 19:25:16 -0800383 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800384 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
385 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
386 newIcon = parent.getChildAt(parent.getChildCount() - 1);
387 workspace.snapToPage(pageIndex - 1);
388 break;
389 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
390 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
391 newIcon = parent.getChildAt(0);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800392 workspace.snapToPage(pageIndex + 1);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800393 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800394 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700395 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
396 newPageIndex = pageIndex + 1;
397 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
398 newPageIndex = pageIndex - 1;
399 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700400 row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700401 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800402 if (parent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700403 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800404 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800405 matrix = FocusLogic.createSparseMatrix(iconLayout, -1, row);
Adam Cohen2e6da152015-05-06 11:42:25 -0700406 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY,
Sunny Goyalc6205602015-05-21 20:46:33 -0700407 matrix, FocusLogic.PIVOT, newPageIndex, pageCount,
408 Utilities.isRtl(v.getResources()));
Hyunyoung Song38531712015-03-03 19:25:16 -0800409 newIcon = parent.getChildAt(newIconIndex);
410 }
411 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800412 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
413 newIcon = parent.getChildAt(0);
414 break;
415 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
416 newIcon = parent.getChildAt(parent.getChildCount() - 1);
417 break;
418 default:
419 // current page, some item.
420 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
421 newIcon = parent.getChildAt(newIconIndex);
422 } else if (parent.getChildCount() <= newIconIndex &&
423 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
424 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
425 }
426 break;
427 }
428 if (newIcon != null) {
429 newIcon.requestFocus();
430 playSoundEffect(keyCode, v);
431 }
432 return consume;
433 }
434
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800435 //
436 // Helper methods.
437 //
438
Winson Chung97d85d22011-04-13 11:27:36 -0700439 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700440 * Private helper method to get the CellLayoutChildren given a CellLayout index.
441 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700442 @Thunk static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
Michael Jurkaa52570f2012-03-20 03:18:20 -0700443 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700444 CellLayout parent = (CellLayout) container.getChildAt(i);
445 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700446 }
447
Winson Chung97d85d22011-04-13 11:27:36 -0700448 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800449 * Helper method to be used for playing sound effects.
Winson Chung97d85d22011-04-13 11:27:36 -0700450 */
Adam Cohen091440a2015-03-18 14:16:05 -0700451 @Thunk static void playSoundEffect(int keyCode, View v) {
Winson Chung97d85d22011-04-13 11:27:36 -0700452 switch (keyCode) {
453 case KeyEvent.KEYCODE_DPAD_LEFT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800454 v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
Winson Chung97d85d22011-04-13 11:27:36 -0700455 break;
456 case KeyEvent.KEYCODE_DPAD_RIGHT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800457 v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
Winson Chung97d85d22011-04-13 11:27:36 -0700458 break;
459 case KeyEvent.KEYCODE_DPAD_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700460 case KeyEvent.KEYCODE_PAGE_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700461 case KeyEvent.KEYCODE_MOVE_END:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800462 v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
Adam Cohenac56cff2011-09-28 20:45:37 -0700463 break;
464 case KeyEvent.KEYCODE_DPAD_UP:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800465 case KeyEvent.KEYCODE_PAGE_UP:
Adam Cohenac56cff2011-09-28 20:45:37 -0700466 case KeyEvent.KEYCODE_MOVE_HOME:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800467 v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
Adam Cohenac56cff2011-09-28 20:45:37 -0700468 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800469 default:
Winson Chung97d85d22011-04-13 11:27:36 -0700470 break;
Winson Chung97d85d22011-04-13 11:27:36 -0700471 }
Winson Chung97d85d22011-04-13 11:27:36 -0700472 }
Winsonfa56b3f2015-09-14 12:01:13 -0700473
474 /**
475 * Returns whether the key event represents a valid uninstall key chord.
476 */
477 private static boolean isUninstallKeyChord(KeyEvent event) {
478 int keyCode = event.getKeyCode();
479 return (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) &&
480 event.hasModifiers(KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON);
481 }
482
483 /**
484 * Returns whether the key event represents a valid delete key chord.
485 */
486 private static boolean isDeleteKeyChord(KeyEvent event) {
487 int keyCode = event.getKeyCode();
488 return (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) &&
489 event.hasModifiers(KeyEvent.META_CTRL_ON);
490 }
Winson Chung97d85d22011-04-13 11:27:36 -0700491}