blob: b243da3d57f8dfa3c0ec93ea3f34dc1536016d19 [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);
Tony Wickham25189852015-11-04 17:44:32 -0800145 child = FocusLogic.getAdjacentChildInNextFolderPage(
146 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 /**
Tony Wickham4fc82872015-11-10 16:52:14 -0800177 * Handles key events in the workspace hotseat (bottom of the screen).
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800178 * <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.
Winsonfa56b3f2015-09-14 12:01:13 -0700198 final Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800199 final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent();
200 final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800201
Winsonfa56b3f2015-09-14 12:01:13 -0700202 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700203 int pageIndex = workspace.getNextPage();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800204 int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800205 int countX = -1;
206 int countY = -1;
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700207 int iconIndex = hotseatParent.indexOfChild(v);
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700208 int iconRank = ((CellLayout.LayoutParams) hotseatLayout.getShortcutsAndWidgets()
209 .getChildAt(iconIndex).getLayoutParams()).cellX;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800210
211 final CellLayout iconLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700212 if (iconLayout == null) {
213 // This check is to guard against cases where key strokes rushes in when workspace
214 // child creation/deletion is still in flux. (e.g., during drop or fling
215 // animation.)
216 return consume;
217 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800218 final ViewGroup iconParent = iconLayout.getShortcutsAndWidgets();
219
220 ViewGroup parent = null;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800221 int[][] matrix = null;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800222
223 if (keyCode == KeyEvent.KEYCODE_DPAD_UP &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700224 !profile.isVerticalBarLayout()) {
225 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout,
Tony Wickham6cbd2222015-11-09 17:51:08 -0800226 true /* hotseat horizontal */, profile.inv.hotseatAllAppsRank);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800227 iconIndex += iconParent.getChildCount();
Tony Wickham6cbd2222015-11-09 17:51:08 -0800228 countX = hotseatLayout.getCountX();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800229 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,
Tony Wickham6cbd2222015-11-09 17:51:08 -0800234 false /* hotseat horizontal */, profile.inv.hotseatAllAppsRank);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800235 iconIndex += iconParent.getChildCount();
236 countX = iconLayout.getCountX() + hotseatLayout.getCountX();
Tony Wickham6cbd2222015-11-09 17:51:08 -0800237 countY = hotseatLayout.getCountY();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800238 parent = iconParent;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800239 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700240 profile.isVerticalBarLayout()) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800241 keyCode = KeyEvent.KEYCODE_PAGE_DOWN;
Winsonfa56b3f2015-09-14 12:01:13 -0700242 } else if (isUninstallKeyChord(e)) {
243 matrix = FocusLogic.createSparseMatrix(iconLayout);
244 if (UninstallDropTarget.supportsDrop(launcher, itemInfo)) {
245 UninstallDropTarget.startUninstallActivity(launcher, itemInfo);
246 }
247 } else if (isDeleteKeyChord(e)) {
248 matrix = FocusLogic.createSparseMatrix(iconLayout);
Winson2949fb52015-09-24 09:56:11 -0700249 launcher.removeItem(v, itemInfo, true /* deleteFromDb */);
Winsonc0b52fe2015-09-09 16:38:15 -0700250 } else {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800251 // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the
252 // matrix extended with hotseat.
253 matrix = FocusLogic.createSparseMatrix(hotseatLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800254 countX = hotseatLayout.getCountX();
255 countY = hotseatLayout.getCountY();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800256 parent = hotseatParent;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800257 }
258
259 // Process the focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700260 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700261 countY, matrix, iconIndex, pageIndex, pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800262
Hyunyoung Song31178b82015-02-24 14:12:51 -0800263 View newIcon = null;
Tony Wickham4fc82872015-11-10 16:52:14 -0800264 switch (newIconIndex) {
265 case 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 break;
272 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
273 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
274 newIcon = parent.getChildAt(0);
275 // TODO(hyunyoungs): handle cases where the child is not an icon but
276 // a folder or a widget.
277 workspace.snapToPage(pageIndex - 1);
278 break;
279 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
280 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
281 newIcon = parent.getChildAt(parent.getChildCount() - 1);
282 // TODO(hyunyoungs): handle cases where the child is not an icon but
283 // a folder or a widget.
284 workspace.snapToPage(pageIndex - 1);
285 break;
286 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
287 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
288 // Go to the previous page but keep the focus on the same hotseat icon.
289 workspace.snapToPage(pageIndex - 1);
290 break;
291 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
292 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
293 // Go to the next page but keep the focus on the same hotseat icon.
294 workspace.snapToPage(pageIndex + 1);
295 break;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800296 }
297 if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800298 newIconIndex -= iconParent.getChildCount();
299 }
300 if (parent != null) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800301 if (newIcon == null && newIconIndex >=0) {
302 newIcon = parent.getChildAt(newIconIndex);
303 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800304 if (newIcon != null) {
305 newIcon.requestFocus();
306 playSoundEffect(keyCode, v);
307 }
308 }
309 return consume;
310 }
311
312 /**
313 * Handles key events in a workspace containing icons.
314 */
315 static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) {
316 boolean consume = FocusLogic.shouldConsume(keyCode);
317 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
318 return consume;
319 }
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700320
Adam Cohen2e6da152015-05-06 11:42:25 -0700321 Launcher launcher = (Launcher) v.getContext();
322 DeviceProfile profile = launcher.getDeviceProfile();
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700323
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800324 if (DEBUG) {
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700325 Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s",
326 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800327 }
328
329 // Initialize the variables.
330 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
Hyunyoung Song38531712015-03-03 19:25:16 -0800331 CellLayout iconLayout = (CellLayout) parent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800332 final Workspace workspace = (Workspace) iconLayout.getParent();
Adam Cohen2e6da152015-05-06 11:42:25 -0700333 final ViewGroup dragLayer = (ViewGroup) workspace.getParent();
334 final ViewGroup tabs = (ViewGroup) dragLayer.findViewById(R.id.search_drop_target_bar);
335 final Hotseat hotseat = (Hotseat) dragLayer.findViewById(R.id.hotseat);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700336
Winsonfa56b3f2015-09-14 12:01:13 -0700337 final ItemInfo itemInfo = (ItemInfo) v.getTag();
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700338 final int iconIndex = parent.indexOfChild(v);
339 final int pageIndex = workspace.indexOfChild(iconLayout);
340 final int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800341 int countX = iconLayout.getCountX();
342 int countY = iconLayout.getCountY();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800343
344 CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0);
345 ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets();
346 int[][] matrix;
347
Hyunyoung Song31178b82015-02-24 14:12:51 -0800348 // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800349 // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended
350 // with the hotseat.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700351 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) {
352 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, true /* horizontal */,
Tony Wickham6cbd2222015-11-09 17:51:08 -0800353 profile.inv.hotseatAllAppsRank);
354 countX = hotseatLayout.getCountX();
355 countY = countY + hotseatLayout.getCountY();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800356 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700357 profile.isVerticalBarLayout()) {
358 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, false /* horizontal */,
Tony Wickham6cbd2222015-11-09 17:51:08 -0800359 profile.inv.hotseatAllAppsRank);
360 countX = countX + hotseatLayout.getCountX();
361 countY = hotseatLayout.getCountY();
Winsonfa56b3f2015-09-14 12:01:13 -0700362 } else if (isUninstallKeyChord(e)) {
363 matrix = FocusLogic.createSparseMatrix(iconLayout);
364 if (UninstallDropTarget.supportsDrop(launcher, itemInfo)) {
365 UninstallDropTarget.startUninstallActivity(launcher, itemInfo);
366 }
367 } else if (isDeleteKeyChord(e)) {
368 matrix = FocusLogic.createSparseMatrix(iconLayout);
Winson2949fb52015-09-24 09:56:11 -0700369 launcher.removeItem(v, itemInfo, true /* deleteFromDb */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800370 } else {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800371 matrix = FocusLogic.createSparseMatrix(iconLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800372 }
373
374 // Process the focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700375 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700376 countY, matrix, iconIndex, pageIndex, pageCount, Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800377 boolean isRtl = Utilities.isRtl(v.getResources());
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800378 View newIcon = null;
Tony Wickhamaf78b592015-11-11 09:25:38 -0800379 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800380 switch (newIconIndex) {
381 case FocusLogic.NOOP:
382 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
383 newIcon = tabs;
384 }
385 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800386 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700387 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
388 int newPageIndex = pageIndex - 1;
389 if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) {
390 newPageIndex = pageIndex + 1;
391 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700392 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700393 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800394 if (parent != null) {
395 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800396 matrix = FocusLogic.createSparseMatrix(iconLayout,
Tony Wickham4fc82872015-11-10 16:52:14 -0800397 iconLayout.getCountX(), row);
Adam Cohen2e6da152015-05-06 11:42:25 -0700398 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY,
Sunny Goyalc6205602015-05-21 20:46:33 -0700399 matrix, FocusLogic.PIVOT, newPageIndex, pageCount,
400 Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800401 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
402 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex,
403 isRtl);
404 } else if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LAST_ITEM) {
405 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex,
406 isRtl);
407 } else {
408 newIcon = parent.getChildAt(newIconIndex);
409 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800410 }
411 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800412 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800413 workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex - 1);
414 newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
415 if (newIcon == null) {
416 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
417 workspace.snapToPage(pageIndex - 1);
418 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800419 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800420 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800421 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex, isRtl);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800422 break;
423 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800424 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex, isRtl);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800425 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800426 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700427 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
428 newPageIndex = pageIndex + 1;
429 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
430 newPageIndex = pageIndex - 1;
431 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700432 row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700433 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800434 if (parent != null) {
435 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800436 matrix = FocusLogic.createSparseMatrix(iconLayout, -1, row);
Adam Cohen2e6da152015-05-06 11:42:25 -0700437 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY,
Sunny Goyalc6205602015-05-21 20:46:33 -0700438 matrix, FocusLogic.PIVOT, newPageIndex, pageCount,
439 Utilities.isRtl(v.getResources()));
Tony Wickhamaf78b592015-11-11 09:25:38 -0800440 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
441 newIcon = handleNextPageFirstItem(workspace, hotseatLayout, pageIndex,
442 isRtl);
443 } else if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LAST_ITEM) {
444 newIcon = handlePreviousPageLastItem(workspace, hotseatLayout, pageIndex,
445 isRtl);
446 } else {
447 newIcon = parent.getChildAt(newIconIndex);
448 }
Hyunyoung Song38531712015-03-03 19:25:16 -0800449 }
450 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800451 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800452 newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
453 if (newIcon == null) {
454 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
455 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800456 break;
457 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Tony Wickhamaf78b592015-11-11 09:25:38 -0800458 newIcon = getFirstFocusableIconInReverseReadingOrder(workspaceLayout, isRtl);
459 if (newIcon == null) {
460 newIcon = getFirstFocusableIconInReverseReadingOrder(hotseatLayout, isRtl);
461 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800462 break;
463 default:
464 // current page, some item.
465 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
466 newIcon = parent.getChildAt(newIconIndex);
467 } else if (parent.getChildCount() <= newIconIndex &&
468 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
469 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
470 }
471 break;
472 }
473 if (newIcon != null) {
474 newIcon.requestFocus();
475 playSoundEffect(keyCode, v);
476 }
477 return consume;
478 }
479
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800480 //
481 // Helper methods.
482 //
483
Winson Chung97d85d22011-04-13 11:27:36 -0700484 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700485 * Private helper method to get the CellLayoutChildren given a CellLayout index.
486 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700487 @Thunk static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
Michael Jurkaa52570f2012-03-20 03:18:20 -0700488 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700489 CellLayout parent = (CellLayout) container.getChildAt(i);
490 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700491 }
492
Winson Chung97d85d22011-04-13 11:27:36 -0700493 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800494 * Helper method to be used for playing sound effects.
Winson Chung97d85d22011-04-13 11:27:36 -0700495 */
Adam Cohen091440a2015-03-18 14:16:05 -0700496 @Thunk static void playSoundEffect(int keyCode, View v) {
Winson Chung97d85d22011-04-13 11:27:36 -0700497 switch (keyCode) {
498 case KeyEvent.KEYCODE_DPAD_LEFT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800499 v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
Winson Chung97d85d22011-04-13 11:27:36 -0700500 break;
501 case KeyEvent.KEYCODE_DPAD_RIGHT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800502 v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
Winson Chung97d85d22011-04-13 11:27:36 -0700503 break;
504 case KeyEvent.KEYCODE_DPAD_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700505 case KeyEvent.KEYCODE_PAGE_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700506 case KeyEvent.KEYCODE_MOVE_END:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800507 v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
Adam Cohenac56cff2011-09-28 20:45:37 -0700508 break;
509 case KeyEvent.KEYCODE_DPAD_UP:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800510 case KeyEvent.KEYCODE_PAGE_UP:
Adam Cohenac56cff2011-09-28 20:45:37 -0700511 case KeyEvent.KEYCODE_MOVE_HOME:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800512 v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
Adam Cohenac56cff2011-09-28 20:45:37 -0700513 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800514 default:
Winson Chung97d85d22011-04-13 11:27:36 -0700515 break;
Winson Chung97d85d22011-04-13 11:27:36 -0700516 }
Winson Chung97d85d22011-04-13 11:27:36 -0700517 }
Winsonfa56b3f2015-09-14 12:01:13 -0700518
519 /**
520 * Returns whether the key event represents a valid uninstall key chord.
521 */
522 private static boolean isUninstallKeyChord(KeyEvent event) {
523 int keyCode = event.getKeyCode();
524 return (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) &&
525 event.hasModifiers(KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON);
526 }
527
528 /**
529 * Returns whether the key event represents a valid delete key chord.
530 */
531 private static boolean isDeleteKeyChord(KeyEvent event) {
532 int keyCode = event.getKeyCode();
533 return (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) &&
534 event.hasModifiers(KeyEvent.META_CTRL_ON);
535 }
Tony Wickhamaf78b592015-11-11 09:25:38 -0800536
537 private static View handlePreviousPageLastItem(Workspace workspace, CellLayout hotseatLayout,
538 int pageIndex, boolean isRtl) {
539 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex - 1);
540 View newIcon = getFirstFocusableIconInReverseReadingOrder(workspaceLayout, isRtl);
541 if (newIcon == null) {
542 newIcon = getFirstFocusableIconInReverseReadingOrder(hotseatLayout,isRtl);
543 workspace.snapToPage(pageIndex - 1);
544 }
545 return newIcon;
546 }
547
548 private static View handleNextPageFirstItem(Workspace workspace, CellLayout hotseatLayout,
549 int pageIndex, boolean isRtl) {
550 CellLayout workspaceLayout = (CellLayout) workspace.getChildAt(pageIndex + 1);
551 View newIcon = getFirstFocusableIconInReadingOrder(workspaceLayout, isRtl);
552 if (newIcon == null) {
553 newIcon = getFirstFocusableIconInReadingOrder(hotseatLayout, isRtl);
554 workspace.snapToPage(pageIndex + 1);
555 }
556 return newIcon;
557 }
558
559 private static View getFirstFocusableIconInReadingOrder(CellLayout cellLayout, boolean isRtl) {
560 View icon;
561 int countX = cellLayout.getCountX();
562 for (int y = 0; y < cellLayout.getCountY(); y++) {
563 int increment = isRtl ? -1 : 1;
564 for (int x = isRtl ? countX - 1 : 0; 0 <= x && x < countX; x += increment) {
565 if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
566 return icon;
567 }
568 }
569 }
570 return null;
571 }
572
573 private static View getFirstFocusableIconInReverseReadingOrder(CellLayout cellLayout,
574 boolean isRtl) {
575 View icon;
576 int countX = cellLayout.getCountX();
577 for (int y = cellLayout.getCountY() - 1; y >= 0; y--) {
578 int increment = isRtl ? 1 : -1;
579 for (int x = isRtl ? 0 : countX - 1; 0 <= x && x < countX; x += increment) {
580 if ((icon = cellLayout.getChildAt(x, y)) != null && icon.isFocusable()) {
581 return icon;
582 }
583 }
584 }
585 return null;
586 }
Winson Chung97d85d22011-04-13 11:27:36 -0700587}