blob: 6f345875a105447bc01c6c275d13773520fe109a [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
Sunny Goyal6c56c682015-07-16 14:09:05 -070025import com.android.launcher3.config.ProviderConfig;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080026import com.android.launcher3.util.FocusLogic;
Adam Cohen091440a2015-03-18 14:16:05 -070027import com.android.launcher3.util.Thunk;
Winson Chungfaa13252011-06-13 18:15:54 -070028
Winson Chung97d85d22011-04-13 11:27:36 -070029/**
Winson Chung4d279d92011-07-21 11:46:32 -070030 * A keyboard listener we set on all the workspace icons.
31 */
Adam Cohenac56cff2011-09-28 20:45:37 -070032class IconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080033 @Override
Winson Chung4d279d92011-07-21 11:46:32 -070034 public boolean onKey(View v, int keyCode, KeyEvent event) {
Adam Cohenac56cff2011-09-28 20:45:37 -070035 return FocusHelper.handleIconKeyEvent(v, keyCode, event);
36 }
37}
38
39/**
Winson Chung3d503fb2011-07-13 17:25:49 -070040 * A keyboard listener we set on all the hotseat buttons.
Winson Chung4e6a9762011-05-09 11:56:34 -070041 */
Adam Cohenac56cff2011-09-28 20:45:37 -070042class HotseatIconKeyEventListener implements View.OnKeyListener {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080043 @Override
Winson Chung4e6a9762011-05-09 11:56:34 -070044 public boolean onKey(View v, int keyCode, KeyEvent event) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080045 return FocusHelper.handleHotseatButtonKeyEvent(v, keyCode, event);
Winson Chung4e6a9762011-05-09 11:56:34 -070046 }
47}
48
Winson Chung97d85d22011-04-13 11:27:36 -070049public class FocusHelper {
Winson Chung97d85d22011-04-13 11:27:36 -070050
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080051 private static final String TAG = "FocusHelper";
52 private static final boolean DEBUG = false;
53
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080054 /**
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070055 * Handles key events in paged folder.
Sunny Goyal290800b2015-03-05 11:33:33 -080056 */
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070057 public static class PagedFolderKeyEventListener implements View.OnKeyListener {
Sunny Goyal290800b2015-03-05 11:33:33 -080058
59 private final Folder mFolder;
60
61 public PagedFolderKeyEventListener(Folder folder) {
62 mFolder = folder;
63 }
64
65 @Override
Hyunyoung Songada50982015-04-10 14:35:23 -070066 public boolean onKey(View v, int keyCode, KeyEvent e) {
67 boolean consume = FocusLogic.shouldConsume(keyCode);
68 if (e.getAction() == KeyEvent.ACTION_UP) {
69 return consume;
Sunny Goyal290800b2015-03-05 11:33:33 -080070 }
Hyunyoung Songada50982015-04-10 14:35:23 -070071 if (DEBUG) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070072 Log.v(TAG, String.format("Handle ALL Folders keyevent=[%s].",
Hyunyoung Songada50982015-04-10 14:35:23 -070073 KeyEvent.keyCodeToString(keyCode)));
74 }
Sunny Goyal290800b2015-03-05 11:33:33 -080075
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070076
77 if (!(v.getParent() instanceof ShortcutAndWidgetContainer)) {
Sunny Goyal6c56c682015-07-16 14:09:05 -070078 if (ProviderConfig.IS_DOGFOOD_BUILD) {
Hyunyoung Songada50982015-04-10 14:35:23 -070079 throw new IllegalStateException("Parent of the focused item is not supported.");
80 } else {
81 return false;
82 }
83 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -080084
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070085 // Initialize variables.
86 final ShortcutAndWidgetContainer itemContainer = (ShortcutAndWidgetContainer) v.getParent();
87 final CellLayout cellLayout = (CellLayout) itemContainer.getParent();
88 final int countX = cellLayout.getCountX();
89 final int countY = cellLayout.getCountY();
Hyunyoung Songada50982015-04-10 14:35:23 -070090
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070091 final int iconIndex = itemContainer.indexOfChild(v);
92 final FolderPagedView pagedView = (FolderPagedView) cellLayout.getParent();
93
94 final int pageIndex = pagedView.indexOfChild(cellLayout);
95 final int pageCount = pagedView.getPageCount();
Sunny Goyalc6205602015-05-21 20:46:33 -070096 final boolean isLayoutRtl = Utilities.isRtl(v.getResources());
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -070097
98 int[][] matrix = FocusLogic.createSparseMatrix(cellLayout);
Hyunyoung Songada50982015-04-10 14:35:23 -070099 // Process focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700100 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700101 countY, matrix, iconIndex, pageIndex, pageCount, isLayoutRtl);
Hyunyoung Songada50982015-04-10 14:35:23 -0700102 if (newIconIndex == FocusLogic.NOOP) {
103 handleNoopKey(keyCode, v);
104 return consume;
105 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700106 ShortcutAndWidgetContainer newParent = null;
107 View child = null;
108
Hyunyoung Songada50982015-04-10 14:35:23 -0700109 switch (newIconIndex) {
110 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700111 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
112 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700113 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700114 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
115 pagedView.snapToPage(pageIndex - 1);
116 child = newParent.getChildAt(
117 ((newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN)
118 ^ newParent.invertLayoutHorizontally()) ? 0 : countX - 1, row);
Hyunyoung Songada50982015-04-10 14:35:23 -0700119 }
120 break;
121 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700122 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700123 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700124 pagedView.snapToPage(pageIndex - 1);
125 child = newParent.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700126 }
127 break;
128 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700129 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700130 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700131 pagedView.snapToPage(pageIndex - 1);
132 child = newParent.getChildAt(countX - 1, countY - 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700133 }
134 break;
135 case FocusLogic.NEXT_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.NEXT_PAGE_LEFT_COLUMN:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700143 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
144 newParent = getCellLayoutChildrenForIndex(pagedView, pageIndex + 1);
Hyunyoung Songada50982015-04-10 14:35:23 -0700145 if (newParent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700146 pagedView.snapToPage(pageIndex + 1);
147 child = FocusLogic.getAdjacentChildInNextPage(newParent, v, newIconIndex);
Hyunyoung Songada50982015-04-10 14:35:23 -0700148 }
149 break;
150 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700151 child = cellLayout.getChildAt(0, 0);
Hyunyoung Songada50982015-04-10 14:35:23 -0700152 break;
153 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700154 child = pagedView.getLastItem();
Hyunyoung Songada50982015-04-10 14:35:23 -0700155 break;
156 default: // Go to some item on the current page.
157 child = itemContainer.getChildAt(newIconIndex);
158 break;
159 }
160 if (child != null) {
161 child.requestFocus();
162 playSoundEffect(keyCode, v);
163 } else {
164 handleNoopKey(keyCode, v);
165 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800166 return consume;
167 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800168
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700169 public void handleNoopKey(int keyCode, View v) {
170 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
171 mFolder.mFolderName.requestFocus();
172 playSoundEffect(keyCode, v);
173 }
174 }
Sunny Goyal290800b2015-03-05 11:33:33 -0800175 }
176
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800177 /**
178 * Handles key events in the workspace hot seat (bottom of the screen).
179 * <p>Currently we don't special case for the phone UI in different orientations, even though
180 * the hotseat is on the side in landscape mode. This is to ensure that accessibility
181 * consistency is maintained across rotations.
182 */
183 static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e) {
184 boolean consume = FocusLogic.shouldConsume(keyCode);
185 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
186 return consume;
187 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800188
Winsonc0b52fe2015-09-09 16:38:15 -0700189 final Launcher launcher = (Launcher) v.getContext();
190 final DeviceProfile profile = launcher.getDeviceProfile();
Adam Cohen2e6da152015-05-06 11:42:25 -0700191
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800192 if (DEBUG) {
193 Log.v(TAG, String.format(
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700194 "Handle HOTSEAT BUTTONS keyevent=[%s] on hotseat buttons, isVertical=%s",
195 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800196 }
197
198 // Initialize the variables.
199 final ShortcutAndWidgetContainer hotseatParent = (ShortcutAndWidgetContainer) v.getParent();
200 final CellLayout hotseatLayout = (CellLayout) hotseatParent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800201
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800202 Workspace workspace = (Workspace) v.getRootView().findViewById(R.id.workspace);
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,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700226 true /* hotseat horizontal */, profile.inv.hotseatAllAppsRank,
227 iconRank == profile.inv.hotseatAllAppsRank /* include all apps icon */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800228 iconIndex += iconParent.getChildCount();
229 countX = iconLayout.getCountX();
230 countY = iconLayout.getCountY() + hotseatLayout.getCountY();
231 parent = iconParent;
232 } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700233 profile.isVerticalBarLayout()) {
234 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700235 false /* hotseat horizontal */, profile.inv.hotseatAllAppsRank,
236 iconRank == profile.inv.hotseatAllAppsRank /* include all apps icon */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800237 iconIndex += iconParent.getChildCount();
238 countX = iconLayout.getCountX() + hotseatLayout.getCountX();
239 countY = iconLayout.getCountY();
240 parent = iconParent;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800241 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700242 profile.isVerticalBarLayout()) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800243 keyCode = KeyEvent.KEYCODE_PAGE_DOWN;
Winsonc0b52fe2015-09-09 16:38:15 -0700244 } else if (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) {
245 ItemInfo info = (ItemInfo) v.getTag();
246 launcher.removeItem(v, info, true /* deleteFromDb */);
247 } else {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800248 // For other KEYCODE_DPAD_LEFT and KEYCODE_DPAD_RIGHT navigation, do not use the
249 // matrix extended with hotseat.
250 matrix = FocusLogic.createSparseMatrix(hotseatLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800251 countX = hotseatLayout.getCountX();
252 countY = hotseatLayout.getCountY();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800253 parent = hotseatParent;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800254 }
255
256 // Process the focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700257 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700258 countY, matrix, iconIndex, pageIndex, pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800259
Hyunyoung Song31178b82015-02-24 14:12:51 -0800260 View newIcon = null;
261 if (newIconIndex == FocusLogic.NEXT_PAGE_FIRST_ITEM) {
262 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
263 newIcon = parent.getChildAt(0);
264 // TODO(hyunyoungs): handle cases where the child is not an icon but
265 // a folder or a widget.
266 workspace.snapToPage(pageIndex + 1);
267 }
268 if (parent == iconParent && newIconIndex >= iconParent.getChildCount()) {
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800269 newIconIndex -= iconParent.getChildCount();
270 }
271 if (parent != null) {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800272 if (newIcon == null && newIconIndex >=0) {
273 newIcon = parent.getChildAt(newIconIndex);
274 }
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800275 if (newIcon != null) {
276 newIcon.requestFocus();
277 playSoundEffect(keyCode, v);
278 }
279 }
280 return consume;
281 }
282
283 /**
284 * Handles key events in a workspace containing icons.
285 */
286 static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) {
287 boolean consume = FocusLogic.shouldConsume(keyCode);
288 if (e.getAction() == KeyEvent.ACTION_UP || !consume) {
289 return consume;
290 }
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700291
Adam Cohen2e6da152015-05-06 11:42:25 -0700292 Launcher launcher = (Launcher) v.getContext();
293 DeviceProfile profile = launcher.getDeviceProfile();
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700294
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800295 if (DEBUG) {
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700296 Log.v(TAG, String.format("Handle WORKSPACE ICONS keyevent=[%s] isVerticalBar=%s",
297 KeyEvent.keyCodeToString(keyCode), profile.isVerticalBarLayout()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800298 }
299
300 // Initialize the variables.
301 ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent();
Hyunyoung Song38531712015-03-03 19:25:16 -0800302 CellLayout iconLayout = (CellLayout) parent.getParent();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800303 final Workspace workspace = (Workspace) iconLayout.getParent();
Adam Cohen2e6da152015-05-06 11:42:25 -0700304 final ViewGroup dragLayer = (ViewGroup) workspace.getParent();
305 final ViewGroup tabs = (ViewGroup) dragLayer.findViewById(R.id.search_drop_target_bar);
306 final Hotseat hotseat = (Hotseat) dragLayer.findViewById(R.id.hotseat);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700307
308 final int iconIndex = parent.indexOfChild(v);
309 final int pageIndex = workspace.indexOfChild(iconLayout);
310 final int pageCount = workspace.getChildCount();
Hyunyoung Song31178b82015-02-24 14:12:51 -0800311 int countX = iconLayout.getCountX();
312 int countY = iconLayout.getCountY();
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800313
314 CellLayout hotseatLayout = (CellLayout) hotseat.getChildAt(0);
315 ShortcutAndWidgetContainer hotseatParent = hotseatLayout.getShortcutsAndWidgets();
316 int[][] matrix;
317
Hyunyoung Song31178b82015-02-24 14:12:51 -0800318 // KEYCODE_DPAD_DOWN in portrait (KEYCODE_DPAD_RIGHT in landscape) is the only key allowed
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800319 // to take a user to the hotseat. For other dpad navigation, do not use the matrix extended
320 // with the hotseat.
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700321 if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && !profile.isVerticalBarLayout()) {
322 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, true /* horizontal */,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700323 profile.inv.hotseatAllAppsRank,
Winson Chungc393b072015-05-20 15:03:13 -0700324 !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800325 countY = countY + 1;
Hyunyoung Song31178b82015-02-24 14:12:51 -0800326 } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT &&
Hyunyoung Song18bfaaf2015-03-17 11:32:21 -0700327 profile.isVerticalBarLayout()) {
328 matrix = FocusLogic.createSparseMatrix(iconLayout, hotseatLayout, false /* horizontal */,
Sunny Goyal4f3e9382015-06-05 00:13:25 -0700329 profile.inv.hotseatAllAppsRank,
Winson Chungc393b072015-05-20 15:03:13 -0700330 !hotseat.hasIcons() /* ignore all apps icon, unless there are no other icons */);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800331 countX = countX + 1;
332 } else if (keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_FORWARD_DEL) {
Winsonc0b52fe2015-09-09 16:38:15 -0700333 ItemInfo info = (ItemInfo) v.getTag();
334 launcher.removeItem(v, info, true /* deleteFromDb */);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800335 return consume;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800336 } else {
Hyunyoung Song31178b82015-02-24 14:12:51 -0800337 matrix = FocusLogic.createSparseMatrix(iconLayout);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800338 }
339
340 // Process the focus.
Adam Cohen2e6da152015-05-06 11:42:25 -0700341 int newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX,
Sunny Goyalc6205602015-05-21 20:46:33 -0700342 countY, matrix, iconIndex, pageIndex, pageCount, Utilities.isRtl(v.getResources()));
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800343 View newIcon = null;
344 switch (newIconIndex) {
345 case FocusLogic.NOOP:
346 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
347 newIcon = tabs;
348 }
349 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800350 case FocusLogic.PREVIOUS_PAGE_RIGHT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700351 case FocusLogic.NEXT_PAGE_RIGHT_COLUMN:
352 int newPageIndex = pageIndex - 1;
353 if (newIconIndex == FocusLogic.NEXT_PAGE_RIGHT_COLUMN) {
354 newPageIndex = pageIndex + 1;
355 }
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700356 int row = ((CellLayout.LayoutParams) v.getLayoutParams()).cellY;
Hyunyoung Songada50982015-04-10 14:35:23 -0700357 parent = getCellLayoutChildrenForIndex(workspace, newPageIndex);
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700358 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800359 if (parent != null) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700360 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800361 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800362 matrix = FocusLogic.createSparseMatrix(iconLayout,
Hyunyoung Song38531712015-03-03 19:25:16 -0800363 iconLayout.getCountX(), row);
Adam Cohen2e6da152015-05-06 11:42:25 -0700364 newIconIndex = FocusLogic.handleKeyEvent(keyCode, countX + 1, countY,
Sunny Goyalc6205602015-05-21 20:46:33 -0700365 matrix, FocusLogic.PIVOT, newPageIndex, pageCount,
366 Utilities.isRtl(v.getResources()));
Hyunyoung Song38531712015-03-03 19:25:16 -0800367 newIcon = parent.getChildAt(newIconIndex);
368 }
369 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800370 case FocusLogic.PREVIOUS_PAGE_FIRST_ITEM:
371 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
372 newIcon = parent.getChildAt(0);
373 workspace.snapToPage(pageIndex - 1);
Hyunyoung Song38531712015-03-03 19:25:16 -0800374 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800375 case FocusLogic.PREVIOUS_PAGE_LAST_ITEM:
376 parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1);
377 newIcon = parent.getChildAt(parent.getChildCount() - 1);
378 workspace.snapToPage(pageIndex - 1);
379 break;
380 case FocusLogic.NEXT_PAGE_FIRST_ITEM:
381 parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1);
382 newIcon = parent.getChildAt(0);
Hyunyoung Song31178b82015-02-24 14:12:51 -0800383 workspace.snapToPage(pageIndex + 1);
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800384 break;
Hyunyoung Song38531712015-03-03 19:25:16 -0800385 case FocusLogic.NEXT_PAGE_LEFT_COLUMN:
Hyunyoung Songada50982015-04-10 14:35:23 -0700386 case FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN:
387 newPageIndex = pageIndex + 1;
388 if (newIconIndex == FocusLogic.PREVIOUS_PAGE_LEFT_COLUMN) {
389 newPageIndex = pageIndex - 1;
390 }
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700391 workspace.snapToPage(newPageIndex);
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700392 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) {
Sunny Goyalfc3c1ed2015-04-09 18:48:21 -0700395 workspace.snapToPage(newPageIndex);
Hyunyoung Song38531712015-03-03 19:25:16 -0800396 iconLayout = (CellLayout) parent.getParent();
Hyunyoung Songac721f82015-03-04 16:33:56 -0800397 matrix = FocusLogic.createSparseMatrix(iconLayout, -1, 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()));
Hyunyoung Song38531712015-03-03 19:25:16 -0800401 newIcon = parent.getChildAt(newIconIndex);
402 }
403 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800404 case FocusLogic.CURRENT_PAGE_FIRST_ITEM:
405 newIcon = parent.getChildAt(0);
406 break;
407 case FocusLogic.CURRENT_PAGE_LAST_ITEM:
408 newIcon = parent.getChildAt(parent.getChildCount() - 1);
409 break;
410 default:
411 // current page, some item.
412 if (0 <= newIconIndex && newIconIndex < parent.getChildCount()) {
413 newIcon = parent.getChildAt(newIconIndex);
414 } else if (parent.getChildCount() <= newIconIndex &&
415 newIconIndex < parent.getChildCount() + hotseatParent.getChildCount()) {
416 newIcon = hotseatParent.getChildAt(newIconIndex - parent.getChildCount());
417 }
418 break;
419 }
420 if (newIcon != null) {
421 newIcon.requestFocus();
422 playSoundEffect(keyCode, v);
423 }
424 return consume;
425 }
426
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800427 //
428 // Helper methods.
429 //
430
Winson Chung97d85d22011-04-13 11:27:36 -0700431 /**
Winson Chung97d85d22011-04-13 11:27:36 -0700432 * Private helper method to get the CellLayoutChildren given a CellLayout index.
433 */
Sunny Goyal316490e2015-06-02 09:38:28 -0700434 @Thunk static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
Michael Jurkaa52570f2012-03-20 03:18:20 -0700435 ViewGroup container, int i) {
Sunny Goyalb3726d92014-08-20 16:58:17 -0700436 CellLayout parent = (CellLayout) container.getChildAt(i);
437 return parent.getShortcutsAndWidgets();
Winson Chung97d85d22011-04-13 11:27:36 -0700438 }
439
Winson Chung97d85d22011-04-13 11:27:36 -0700440 /**
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800441 * Helper method to be used for playing sound effects.
Winson Chung97d85d22011-04-13 11:27:36 -0700442 */
Adam Cohen091440a2015-03-18 14:16:05 -0700443 @Thunk static void playSoundEffect(int keyCode, View v) {
Winson Chung97d85d22011-04-13 11:27:36 -0700444 switch (keyCode) {
445 case KeyEvent.KEYCODE_DPAD_LEFT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800446 v.playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
Winson Chung97d85d22011-04-13 11:27:36 -0700447 break;
448 case KeyEvent.KEYCODE_DPAD_RIGHT:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800449 v.playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
Winson Chung97d85d22011-04-13 11:27:36 -0700450 break;
451 case KeyEvent.KEYCODE_DPAD_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700452 case KeyEvent.KEYCODE_PAGE_DOWN:
Winson Chung97d85d22011-04-13 11:27:36 -0700453 case KeyEvent.KEYCODE_MOVE_END:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800454 v.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN);
Adam Cohenac56cff2011-09-28 20:45:37 -0700455 break;
456 case KeyEvent.KEYCODE_DPAD_UP:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800457 case KeyEvent.KEYCODE_PAGE_UP:
Adam Cohenac56cff2011-09-28 20:45:37 -0700458 case KeyEvent.KEYCODE_MOVE_HOME:
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800459 v.playSoundEffect(SoundEffectConstants.NAVIGATION_UP);
Adam Cohenac56cff2011-09-28 20:45:37 -0700460 break;
Hyunyoung Songee3e6a72015-02-20 14:25:27 -0800461 default:
Winson Chung97d85d22011-04-13 11:27:36 -0700462 break;
Winson Chung97d85d22011-04-13 11:27:36 -0700463 }
Winson Chung97d85d22011-04-13 11:27:36 -0700464 }
465}