Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 18 | |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 19 | import android.content.res.Configuration; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 20 | import android.view.KeyEvent; |
| 21 | import android.view.View; |
| 22 | import android.view.ViewGroup; |
| 23 | import android.view.ViewParent; |
Winson Chung | a1f133d | 2013-07-25 11:14:30 -0700 | [diff] [blame] | 24 | import android.widget.ScrollView; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 25 | |
Winson Chung | faa1325 | 2011-06-13 18:15:54 -0700 | [diff] [blame] | 26 | import java.util.ArrayList; |
| 27 | import java.util.Collections; |
| 28 | import java.util.Comparator; |
| 29 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 30 | /** |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 31 | * A keyboard listener we set on all the workspace icons. |
| 32 | */ |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 33 | class IconKeyEventListener implements View.OnKeyListener { |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 34 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 35 | return FocusHelper.handleIconKeyEvent(v, keyCode, event); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * A keyboard listener we set on all the workspace icons. |
| 41 | */ |
| 42 | class FolderKeyEventListener implements View.OnKeyListener { |
| 43 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 44 | return FocusHelper.handleFolderKeyEvent(v, keyCode, event); |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 49 | * A keyboard listener we set on all the hotseat buttons. |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 50 | */ |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 51 | class HotseatIconKeyEventListener implements View.OnKeyListener { |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 52 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 53 | final Configuration configuration = v.getResources().getConfiguration(); |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 54 | return FocusHelper.handleHotseatButtonKeyEvent(v, keyCode, event, configuration.orientation); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 58 | public class FocusHelper { |
| 59 | /** |
| 60 | * Private helper to get the parent TabHost in the view hiearchy. |
| 61 | */ |
Adam Cohen | c956cba | 2014-07-24 09:17:37 -0700 | [diff] [blame] | 62 | private static AppsCustomizeTabHost findTabHostParent(View v) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 63 | ViewParent p = v.getParent(); |
Adam Cohen | c956cba | 2014-07-24 09:17:37 -0700 | [diff] [blame] | 64 | while (p != null && !(p instanceof AppsCustomizeTabHost)) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 65 | p = p.getParent(); |
| 66 | } |
Adam Cohen | c956cba | 2014-07-24 09:17:37 -0700 | [diff] [blame] | 67 | return (AppsCustomizeTabHost) p; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 71 | * Returns the Viewgroup containing page contents for the page at the index specified. |
| 72 | */ |
| 73 | private static ViewGroup getAppsCustomizePage(ViewGroup container, int index) { |
| 74 | ViewGroup page = (ViewGroup) ((PagedView) container).getPageAt(index); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 75 | if (page instanceof CellLayout) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 76 | // There are two layers, a PagedViewCellLayout and PagedViewCellLayoutChildren |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 77 | page = ((CellLayout) page).getShortcutsAndWidgets(); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 78 | } |
| 79 | return page; |
| 80 | } |
| 81 | |
| 82 | /** |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 83 | * Handles key events in a PageViewExtendedLayout containing PagedViewWidgets. |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 84 | */ |
| 85 | static boolean handlePagedViewGridLayoutWidgetKeyEvent(PagedViewWidget w, int keyCode, |
| 86 | KeyEvent e) { |
| 87 | |
| 88 | final PagedViewGridLayout parent = (PagedViewGridLayout) w.getParent(); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 89 | final PagedView container = (PagedView) parent.getParent(); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 90 | final int widgetIndex = parent.indexOfChild(w); |
| 91 | final int widgetCount = parent.getChildCount(); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 92 | final int pageIndex = ((PagedView) container).indexToPage(container.indexOfChild(parent)); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 93 | final int pageCount = container.getChildCount(); |
| 94 | final int cellCountX = parent.getCellCountX(); |
| 95 | final int cellCountY = parent.getCellCountY(); |
| 96 | final int x = widgetIndex % cellCountX; |
| 97 | final int y = widgetIndex / cellCountX; |
| 98 | |
| 99 | final int action = e.getAction(); |
| 100 | final boolean handleKeyEvent = (action != KeyEvent.ACTION_UP); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 101 | ViewGroup newParent = null; |
Winson Chung | 3b0b46a | 2011-07-28 10:45:09 -0700 | [diff] [blame] | 102 | // Now that we load items in the bg asynchronously, we can't just focus |
| 103 | // child siblings willy-nilly |
| 104 | View child = null; |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 105 | boolean wasHandled = false; |
| 106 | switch (keyCode) { |
| 107 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 108 | if (handleKeyEvent) { |
| 109 | // Select the previous widget or the last widget on the previous page |
| 110 | if (widgetIndex > 0) { |
| 111 | parent.getChildAt(widgetIndex - 1).requestFocus(); |
| 112 | } else { |
| 113 | if (pageIndex > 0) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 114 | newParent = getAppsCustomizePage(container, pageIndex - 1); |
| 115 | if (newParent != null) { |
| 116 | child = newParent.getChildAt(newParent.getChildCount() - 1); |
| 117 | if (child != null) child.requestFocus(); |
| 118 | } |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | wasHandled = true; |
| 123 | break; |
| 124 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 125 | if (handleKeyEvent) { |
| 126 | // Select the next widget or the first widget on the next page |
| 127 | if (widgetIndex < (widgetCount - 1)) { |
| 128 | parent.getChildAt(widgetIndex + 1).requestFocus(); |
| 129 | } else { |
| 130 | if (pageIndex < (pageCount - 1)) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 131 | newParent = getAppsCustomizePage(container, pageIndex + 1); |
| 132 | if (newParent != null) { |
| 133 | child = newParent.getChildAt(0); |
| 134 | if (child != null) child.requestFocus(); |
| 135 | } |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | wasHandled = true; |
| 140 | break; |
| 141 | case KeyEvent.KEYCODE_DPAD_UP: |
| 142 | if (handleKeyEvent) { |
| 143 | // Select the closest icon in the previous row, otherwise select the tab bar |
| 144 | if (y > 0) { |
| 145 | int newWidgetIndex = ((y - 1) * cellCountX) + x; |
Winson Chung | 3b0b46a | 2011-07-28 10:45:09 -0700 | [diff] [blame] | 146 | child = parent.getChildAt(newWidgetIndex); |
| 147 | if (child != null) child.requestFocus(); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | wasHandled = true; |
| 151 | break; |
| 152 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 153 | if (handleKeyEvent) { |
| 154 | // Select the closest icon in the previous row, otherwise do nothing |
| 155 | if (y < (cellCountY - 1)) { |
| 156 | int newWidgetIndex = Math.min(widgetCount - 1, ((y + 1) * cellCountX) + x); |
Winson Chung | 3b0b46a | 2011-07-28 10:45:09 -0700 | [diff] [blame] | 157 | child = parent.getChildAt(newWidgetIndex); |
| 158 | if (child != null) child.requestFocus(); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | wasHandled = true; |
| 162 | break; |
| 163 | case KeyEvent.KEYCODE_ENTER: |
| 164 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 165 | if (handleKeyEvent) { |
| 166 | // Simulate a click on the widget |
| 167 | View.OnClickListener clickListener = (View.OnClickListener) container; |
| 168 | clickListener.onClick(w); |
| 169 | } |
| 170 | wasHandled = true; |
| 171 | break; |
| 172 | case KeyEvent.KEYCODE_PAGE_UP: |
| 173 | if (handleKeyEvent) { |
| 174 | // Select the first item on the previous page, or the first item on this page |
| 175 | // if there is no previous page |
| 176 | if (pageIndex > 0) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 177 | newParent = getAppsCustomizePage(container, pageIndex - 1); |
| 178 | if (newParent != null) { |
| 179 | child = newParent.getChildAt(0); |
| 180 | } |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 181 | } else { |
Winson Chung | 3b0b46a | 2011-07-28 10:45:09 -0700 | [diff] [blame] | 182 | child = parent.getChildAt(0); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 183 | } |
Winson Chung | 3b0b46a | 2011-07-28 10:45:09 -0700 | [diff] [blame] | 184 | if (child != null) child.requestFocus(); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 185 | } |
| 186 | wasHandled = true; |
| 187 | break; |
| 188 | case KeyEvent.KEYCODE_PAGE_DOWN: |
| 189 | if (handleKeyEvent) { |
| 190 | // Select the first item on the next page, or the last item on this page |
| 191 | // if there is no next page |
| 192 | if (pageIndex < (pageCount - 1)) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 193 | newParent = getAppsCustomizePage(container, pageIndex + 1); |
| 194 | if (newParent != null) { |
| 195 | child = newParent.getChildAt(0); |
| 196 | } |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 197 | } else { |
Winson Chung | 3b0b46a | 2011-07-28 10:45:09 -0700 | [diff] [blame] | 198 | child = parent.getChildAt(widgetCount - 1); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 199 | } |
Winson Chung | 3b0b46a | 2011-07-28 10:45:09 -0700 | [diff] [blame] | 200 | if (child != null) child.requestFocus(); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 201 | } |
| 202 | wasHandled = true; |
| 203 | break; |
| 204 | case KeyEvent.KEYCODE_MOVE_HOME: |
| 205 | if (handleKeyEvent) { |
| 206 | // Select the first item on this page |
Winson Chung | 3b0b46a | 2011-07-28 10:45:09 -0700 | [diff] [blame] | 207 | child = parent.getChildAt(0); |
| 208 | if (child != null) child.requestFocus(); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 209 | } |
| 210 | wasHandled = true; |
| 211 | break; |
| 212 | case KeyEvent.KEYCODE_MOVE_END: |
| 213 | if (handleKeyEvent) { |
| 214 | // Select the last item on this page |
| 215 | parent.getChildAt(widgetCount - 1).requestFocus(); |
| 216 | } |
| 217 | wasHandled = true; |
| 218 | break; |
| 219 | default: break; |
| 220 | } |
| 221 | return wasHandled; |
| 222 | } |
| 223 | |
| 224 | /** |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 225 | * Handles key events in a PageViewCellLayout containing PagedViewIcons. |
| 226 | */ |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 227 | static boolean handleAppsCustomizeKeyEvent(View v, int keyCode, KeyEvent e) { |
Sunny Goyal | 508da15 | 2014-08-14 10:53:27 -0700 | [diff] [blame^] | 228 | final int action = e.getAction(); |
| 229 | if (((action == KeyEvent.ACTION_DOWN) && v.onKeyDown(keyCode, e)) |
| 230 | || ((action == KeyEvent.ACTION_UP) && v.onKeyUp(keyCode, e))) { |
| 231 | // Let the view handle the confirmation key. |
| 232 | return true; |
| 233 | } |
| 234 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 235 | ViewGroup parentLayout; |
| 236 | ViewGroup itemContainer; |
| 237 | int countX; |
| 238 | int countY; |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 239 | if (v.getParent() instanceof ShortcutAndWidgetContainer) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 240 | itemContainer = (ViewGroup) v.getParent(); |
| 241 | parentLayout = (ViewGroup) itemContainer.getParent(); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 242 | countX = ((CellLayout) parentLayout).getCountX(); |
| 243 | countY = ((CellLayout) parentLayout).getCountY(); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 244 | } else { |
| 245 | itemContainer = parentLayout = (ViewGroup) v.getParent(); |
| 246 | countX = ((PagedViewGridLayout) parentLayout).getCellCountX(); |
| 247 | countY = ((PagedViewGridLayout) parentLayout).getCellCountY(); |
| 248 | } |
| 249 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 250 | // Note we have an extra parent because of the |
| 251 | // PagedViewCellLayout/PagedViewCellLayoutChildren relationship |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 252 | final PagedView container = (PagedView) parentLayout.getParent(); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 253 | final int iconIndex = itemContainer.indexOfChild(v); |
| 254 | final int itemCount = itemContainer.getChildCount(); |
| 255 | final int pageIndex = ((PagedView) container).indexToPage(container.indexOfChild(parentLayout)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 256 | final int pageCount = container.getChildCount(); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 257 | |
| 258 | final int x = iconIndex % countX; |
| 259 | final int y = iconIndex / countX; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 260 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 261 | final boolean handleKeyEvent = (action != KeyEvent.ACTION_UP); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 262 | ViewGroup newParent = null; |
Winson Chung | 90576b5 | 2011-09-27 17:45:27 -0700 | [diff] [blame] | 263 | // Side pages do not always load synchronously, so check before focusing child siblings |
| 264 | // willy-nilly |
| 265 | View child = null; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 266 | boolean wasHandled = false; |
| 267 | switch (keyCode) { |
| 268 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 269 | if (handleKeyEvent) { |
| 270 | // Select the previous icon or the last icon on the previous page |
Winson Chung | 90576b5 | 2011-09-27 17:45:27 -0700 | [diff] [blame] | 271 | if (iconIndex > 0) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 272 | itemContainer.getChildAt(iconIndex - 1).requestFocus(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 273 | } else { |
| 274 | if (pageIndex > 0) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 275 | newParent = getAppsCustomizePage(container, pageIndex - 1); |
Winson Chung | 90576b5 | 2011-09-27 17:45:27 -0700 | [diff] [blame] | 276 | if (newParent != null) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 277 | container.snapToPage(pageIndex - 1); |
Winson Chung | 90576b5 | 2011-09-27 17:45:27 -0700 | [diff] [blame] | 278 | child = newParent.getChildAt(newParent.getChildCount() - 1); |
| 279 | if (child != null) child.requestFocus(); |
| 280 | } |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |
| 284 | wasHandled = true; |
| 285 | break; |
| 286 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 287 | if (handleKeyEvent) { |
| 288 | // Select the next icon or the first icon on the next page |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 289 | if (iconIndex < (itemCount - 1)) { |
| 290 | itemContainer.getChildAt(iconIndex + 1).requestFocus(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 291 | } else { |
| 292 | if (pageIndex < (pageCount - 1)) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 293 | newParent = getAppsCustomizePage(container, pageIndex + 1); |
Winson Chung | 90576b5 | 2011-09-27 17:45:27 -0700 | [diff] [blame] | 294 | if (newParent != null) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 295 | container.snapToPage(pageIndex + 1); |
Winson Chung | 90576b5 | 2011-09-27 17:45:27 -0700 | [diff] [blame] | 296 | child = newParent.getChildAt(0); |
| 297 | if (child != null) child.requestFocus(); |
| 298 | } |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | } |
| 302 | wasHandled = true; |
| 303 | break; |
| 304 | case KeyEvent.KEYCODE_DPAD_UP: |
| 305 | if (handleKeyEvent) { |
| 306 | // Select the closest icon in the previous row, otherwise select the tab bar |
| 307 | if (y > 0) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 308 | int newiconIndex = ((y - 1) * countX) + x; |
| 309 | itemContainer.getChildAt(newiconIndex).requestFocus(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | wasHandled = true; |
| 313 | break; |
| 314 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 315 | if (handleKeyEvent) { |
| 316 | // Select the closest icon in the previous row, otherwise do nothing |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 317 | if (y < (countY - 1)) { |
| 318 | int newiconIndex = Math.min(itemCount - 1, ((y + 1) * countX) + x); |
| 319 | itemContainer.getChildAt(newiconIndex).requestFocus(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | wasHandled = true; |
| 323 | break; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 324 | case KeyEvent.KEYCODE_PAGE_UP: |
| 325 | if (handleKeyEvent) { |
| 326 | // Select the first icon on the previous page, or the first icon on this page |
| 327 | // if there is no previous page |
| 328 | if (pageIndex > 0) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 329 | newParent = getAppsCustomizePage(container, pageIndex - 1); |
Winson Chung | 90576b5 | 2011-09-27 17:45:27 -0700 | [diff] [blame] | 330 | if (newParent != null) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 331 | container.snapToPage(pageIndex - 1); |
Winson Chung | 90576b5 | 2011-09-27 17:45:27 -0700 | [diff] [blame] | 332 | child = newParent.getChildAt(0); |
| 333 | if (child != null) child.requestFocus(); |
| 334 | } |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 335 | } else { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 336 | itemContainer.getChildAt(0).requestFocus(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | wasHandled = true; |
| 340 | break; |
| 341 | case KeyEvent.KEYCODE_PAGE_DOWN: |
| 342 | if (handleKeyEvent) { |
| 343 | // Select the first icon on the next page, or the last icon on this page |
| 344 | // if there is no next page |
| 345 | if (pageIndex < (pageCount - 1)) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 346 | newParent = getAppsCustomizePage(container, pageIndex + 1); |
Winson Chung | 90576b5 | 2011-09-27 17:45:27 -0700 | [diff] [blame] | 347 | if (newParent != null) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 348 | container.snapToPage(pageIndex + 1); |
Winson Chung | 90576b5 | 2011-09-27 17:45:27 -0700 | [diff] [blame] | 349 | child = newParent.getChildAt(0); |
| 350 | if (child != null) child.requestFocus(); |
| 351 | } |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 352 | } else { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 353 | itemContainer.getChildAt(itemCount - 1).requestFocus(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | wasHandled = true; |
| 357 | break; |
| 358 | case KeyEvent.KEYCODE_MOVE_HOME: |
| 359 | if (handleKeyEvent) { |
| 360 | // Select the first icon on this page |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 361 | itemContainer.getChildAt(0).requestFocus(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 362 | } |
| 363 | wasHandled = true; |
| 364 | break; |
| 365 | case KeyEvent.KEYCODE_MOVE_END: |
| 366 | if (handleKeyEvent) { |
| 367 | // Select the last icon on this page |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 368 | itemContainer.getChildAt(itemCount - 1).requestFocus(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 369 | } |
| 370 | wasHandled = true; |
| 371 | break; |
| 372 | default: break; |
| 373 | } |
| 374 | return wasHandled; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Handles key events in the tab widget. |
| 379 | */ |
| 380 | static boolean handleTabKeyEvent(AccessibleTabView v, int keyCode, KeyEvent e) { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 381 | if (!LauncherAppState.getInstance().isScreenLarge()) return false; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 382 | |
| 383 | final FocusOnlyTabWidget parent = (FocusOnlyTabWidget) v.getParent(); |
Adam Cohen | c956cba | 2014-07-24 09:17:37 -0700 | [diff] [blame] | 384 | final AppsCustomizeTabHost tabHost = findTabHostParent(parent); |
| 385 | final ViewGroup contents = tabHost.getContent(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 386 | final int tabCount = parent.getTabCount(); |
| 387 | final int tabIndex = parent.getChildTabIndex(v); |
| 388 | |
| 389 | final int action = e.getAction(); |
| 390 | final boolean handleKeyEvent = (action != KeyEvent.ACTION_UP); |
| 391 | boolean wasHandled = false; |
| 392 | switch (keyCode) { |
| 393 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 394 | if (handleKeyEvent) { |
| 395 | // Select the previous tab |
| 396 | if (tabIndex > 0) { |
| 397 | parent.getChildTabViewAt(tabIndex - 1).requestFocus(); |
| 398 | } |
| 399 | } |
| 400 | wasHandled = true; |
| 401 | break; |
| 402 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 403 | if (handleKeyEvent) { |
| 404 | // Select the next tab, or if the last tab has a focus right id, select that |
| 405 | if (tabIndex < (tabCount - 1)) { |
| 406 | parent.getChildTabViewAt(tabIndex + 1).requestFocus(); |
| 407 | } else { |
| 408 | if (v.getNextFocusRightId() != View.NO_ID) { |
| 409 | tabHost.findViewById(v.getNextFocusRightId()).requestFocus(); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | wasHandled = true; |
| 414 | break; |
| 415 | case KeyEvent.KEYCODE_DPAD_UP: |
| 416 | // Do nothing |
| 417 | wasHandled = true; |
| 418 | break; |
| 419 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 420 | if (handleKeyEvent) { |
| 421 | // Select the content view |
| 422 | contents.requestFocus(); |
| 423 | } |
| 424 | wasHandled = true; |
| 425 | break; |
| 426 | default: break; |
| 427 | } |
| 428 | return wasHandled; |
| 429 | } |
| 430 | |
| 431 | /** |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 432 | * Handles key events in the workspace hotseat (bottom of the screen). |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 433 | */ |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 434 | static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e, int orientation) { |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 435 | final ViewGroup parent = (ViewGroup) v.getParent(); |
| 436 | final ViewGroup launcher = (ViewGroup) parent.getParent(); |
| 437 | final Workspace workspace = (Workspace) launcher.findViewById(R.id.workspace); |
| 438 | final int buttonIndex = parent.indexOfChild(v); |
| 439 | final int buttonCount = parent.getChildCount(); |
| 440 | final int pageIndex = workspace.getCurrentPage(); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 441 | |
| 442 | // NOTE: currently we don't special case for the phone UI in different |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 443 | // orientations, even though the hotseat is on the side in landscape mode. This |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 444 | // is to ensure that accessibility consistency is maintained across rotations. |
| 445 | |
| 446 | final int action = e.getAction(); |
| 447 | final boolean handleKeyEvent = (action != KeyEvent.ACTION_UP); |
| 448 | boolean wasHandled = false; |
| 449 | switch (keyCode) { |
| 450 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 451 | if (handleKeyEvent) { |
Winson Chung | 74608b5 | 2011-06-23 13:23:20 -0700 | [diff] [blame] | 452 | // Select the previous button, otherwise snap to the previous page |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 453 | if (buttonIndex > 0) { |
| 454 | parent.getChildAt(buttonIndex - 1).requestFocus(); |
| 455 | } else { |
Winson Chung | 74608b5 | 2011-06-23 13:23:20 -0700 | [diff] [blame] | 456 | workspace.snapToPage(pageIndex - 1); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | wasHandled = true; |
| 460 | break; |
| 461 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 462 | if (handleKeyEvent) { |
Winson Chung | 74608b5 | 2011-06-23 13:23:20 -0700 | [diff] [blame] | 463 | // Select the next button, otherwise snap to the next page |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 464 | if (buttonIndex < (buttonCount - 1)) { |
| 465 | parent.getChildAt(buttonIndex + 1).requestFocus(); |
| 466 | } else { |
Winson Chung | 74608b5 | 2011-06-23 13:23:20 -0700 | [diff] [blame] | 467 | workspace.snapToPage(pageIndex + 1); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | wasHandled = true; |
| 471 | break; |
| 472 | case KeyEvent.KEYCODE_DPAD_UP: |
| 473 | if (handleKeyEvent) { |
| 474 | // Select the first bubble text view in the current page of the workspace |
| 475 | final CellLayout layout = (CellLayout) workspace.getChildAt(pageIndex); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 476 | final ShortcutAndWidgetContainer children = layout.getShortcutsAndWidgets(); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 477 | final View newIcon = getIconInDirection(layout, children, -1, 1); |
Winson Chung | 4e6a976 | 2011-05-09 11:56:34 -0700 | [diff] [blame] | 478 | if (newIcon != null) { |
| 479 | newIcon.requestFocus(); |
| 480 | } else { |
| 481 | workspace.requestFocus(); |
| 482 | } |
| 483 | } |
| 484 | wasHandled = true; |
| 485 | break; |
| 486 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 487 | // Do nothing |
| 488 | wasHandled = true; |
| 489 | break; |
| 490 | default: break; |
| 491 | } |
| 492 | return wasHandled; |
| 493 | } |
| 494 | |
| 495 | /** |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 496 | * Private helper method to get the CellLayoutChildren given a CellLayout index. |
| 497 | */ |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 498 | private static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex( |
| 499 | ViewGroup container, int i) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 500 | ViewGroup parent = (ViewGroup) container.getChildAt(i); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 501 | return (ShortcutAndWidgetContainer) parent.getChildAt(0); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Private helper method to sort all the CellLayout children in order of their (x,y) spatially |
| 506 | * from top left to bottom right. |
| 507 | */ |
| 508 | private static ArrayList<View> getCellLayoutChildrenSortedSpatially(CellLayout layout, |
| 509 | ViewGroup parent) { |
| 510 | // First we order each the CellLayout children by their x,y coordinates |
| 511 | final int cellCountX = layout.getCountX(); |
| 512 | final int count = parent.getChildCount(); |
| 513 | ArrayList<View> views = new ArrayList<View>(); |
| 514 | for (int j = 0; j < count; ++j) { |
| 515 | views.add(parent.getChildAt(j)); |
| 516 | } |
| 517 | Collections.sort(views, new Comparator<View>() { |
| 518 | @Override |
| 519 | public int compare(View lhs, View rhs) { |
| 520 | CellLayout.LayoutParams llp = (CellLayout.LayoutParams) lhs.getLayoutParams(); |
| 521 | CellLayout.LayoutParams rlp = (CellLayout.LayoutParams) rhs.getLayoutParams(); |
| 522 | int lvIndex = (llp.cellY * cellCountX) + llp.cellX; |
| 523 | int rvIndex = (rlp.cellY * cellCountX) + rlp.cellX; |
| 524 | return lvIndex - rvIndex; |
| 525 | } |
| 526 | }); |
| 527 | return views; |
| 528 | } |
| 529 | /** |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 530 | * Private helper method to find the index of the next BubbleTextView or FolderIcon in the |
| 531 | * direction delta. |
| 532 | * |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 533 | * @param delta either -1 or 1 depending on the direction we want to search |
| 534 | */ |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 535 | private static View findIndexOfIcon(ArrayList<View> views, int i, int delta) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 536 | // Then we find the next BubbleTextView offset by delta from i |
| 537 | final int count = views.size(); |
| 538 | int newI = i + delta; |
| 539 | while (0 <= newI && newI < count) { |
| 540 | View newV = views.get(newI); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 541 | if (newV instanceof BubbleTextView || newV instanceof FolderIcon) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 542 | return newV; |
| 543 | } |
| 544 | newI += delta; |
| 545 | } |
| 546 | return null; |
| 547 | } |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 548 | private static View getIconInDirection(CellLayout layout, ViewGroup parent, int i, |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 549 | int delta) { |
| 550 | final ArrayList<View> views = getCellLayoutChildrenSortedSpatially(layout, parent); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 551 | return findIndexOfIcon(views, i, delta); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 552 | } |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 553 | private static View getIconInDirection(CellLayout layout, ViewGroup parent, View v, |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 554 | int delta) { |
| 555 | final ArrayList<View> views = getCellLayoutChildrenSortedSpatially(layout, parent); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 556 | return findIndexOfIcon(views, views.indexOf(v), delta); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 557 | } |
| 558 | /** |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 559 | * Private helper method to find the next closest BubbleTextView or FolderIcon in the direction |
| 560 | * delta on the next line. |
| 561 | * |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 562 | * @param delta either -1 or 1 depending on the line and direction we want to search |
| 563 | */ |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 564 | private static View getClosestIconOnLine(CellLayout layout, ViewGroup parent, View v, |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 565 | int lineDelta) { |
| 566 | final ArrayList<View> views = getCellLayoutChildrenSortedSpatially(layout, parent); |
| 567 | final CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 568 | final int cellCountY = layout.getCountY(); |
| 569 | final int row = lp.cellY; |
| 570 | final int newRow = row + lineDelta; |
| 571 | if (0 <= newRow && newRow < cellCountY) { |
| 572 | float closestDistance = Float.MAX_VALUE; |
| 573 | int closestIndex = -1; |
| 574 | int index = views.indexOf(v); |
| 575 | int endIndex = (lineDelta < 0) ? -1 : views.size(); |
| 576 | while (index != endIndex) { |
| 577 | View newV = views.get(index); |
| 578 | CellLayout.LayoutParams tmpLp = (CellLayout.LayoutParams) newV.getLayoutParams(); |
| 579 | boolean satisfiesRow = (lineDelta < 0) ? (tmpLp.cellY < row) : (tmpLp.cellY > row); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 580 | if (satisfiesRow && |
| 581 | (newV instanceof BubbleTextView || newV instanceof FolderIcon)) { |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 582 | float tmpDistance = (float) Math.sqrt(Math.pow(tmpLp.cellX - lp.cellX, 2) + |
| 583 | Math.pow(tmpLp.cellY - lp.cellY, 2)); |
| 584 | if (tmpDistance < closestDistance) { |
| 585 | closestIndex = index; |
| 586 | closestDistance = tmpDistance; |
| 587 | } |
| 588 | } |
| 589 | if (index <= endIndex) { |
| 590 | ++index; |
| 591 | } else { |
| 592 | --index; |
| 593 | } |
| 594 | } |
| 595 | if (closestIndex > -1) { |
| 596 | return views.get(closestIndex); |
| 597 | } |
| 598 | } |
| 599 | return null; |
| 600 | } |
| 601 | |
| 602 | /** |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 603 | * Handles key events in a Workspace containing. |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 604 | */ |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 605 | static boolean handleIconKeyEvent(View v, int keyCode, KeyEvent e) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 606 | ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 607 | final CellLayout layout = (CellLayout) parent.getParent(); |
| 608 | final Workspace workspace = (Workspace) layout.getParent(); |
| 609 | final ViewGroup launcher = (ViewGroup) workspace.getParent(); |
Adam Cohen | 24ce0b3 | 2014-01-14 16:18:14 -0800 | [diff] [blame] | 610 | final ViewGroup tabs = (ViewGroup) launcher.findViewById(R.id.search_drop_target_bar); |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 611 | final ViewGroup hotseat = (ViewGroup) launcher.findViewById(R.id.hotseat); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 612 | int pageIndex = workspace.indexOfChild(layout); |
| 613 | int pageCount = workspace.getChildCount(); |
| 614 | |
| 615 | final int action = e.getAction(); |
| 616 | final boolean handleKeyEvent = (action != KeyEvent.ACTION_UP); |
| 617 | boolean wasHandled = false; |
| 618 | switch (keyCode) { |
| 619 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 620 | if (handleKeyEvent) { |
| 621 | // Select the previous icon or the last icon on the previous page if possible |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 622 | View newIcon = getIconInDirection(layout, parent, v, -1); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 623 | if (newIcon != null) { |
| 624 | newIcon.requestFocus(); |
| 625 | } else { |
| 626 | if (pageIndex > 0) { |
| 627 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 628 | newIcon = getIconInDirection(layout, parent, |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 629 | parent.getChildCount(), -1); |
| 630 | if (newIcon != null) { |
| 631 | newIcon.requestFocus(); |
| 632 | } else { |
| 633 | // Snap to the previous page |
| 634 | workspace.snapToPage(pageIndex - 1); |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | wasHandled = true; |
| 640 | break; |
| 641 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 642 | if (handleKeyEvent) { |
| 643 | // Select the next icon or the first icon on the next page if possible |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 644 | View newIcon = getIconInDirection(layout, parent, v, 1); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 645 | if (newIcon != null) { |
| 646 | newIcon.requestFocus(); |
| 647 | } else { |
| 648 | if (pageIndex < (pageCount - 1)) { |
| 649 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 650 | newIcon = getIconInDirection(layout, parent, -1, 1); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 651 | if (newIcon != null) { |
| 652 | newIcon.requestFocus(); |
| 653 | } else { |
| 654 | // Snap to the next page |
| 655 | workspace.snapToPage(pageIndex + 1); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | wasHandled = true; |
| 661 | break; |
| 662 | case KeyEvent.KEYCODE_DPAD_UP: |
| 663 | if (handleKeyEvent) { |
| 664 | // Select the closest icon in the previous line, otherwise select the tab bar |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 665 | View newIcon = getClosestIconOnLine(layout, parent, v, -1); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 666 | if (newIcon != null) { |
| 667 | newIcon.requestFocus(); |
| 668 | wasHandled = true; |
| 669 | } else { |
| 670 | tabs.requestFocus(); |
| 671 | } |
| 672 | } |
| 673 | break; |
| 674 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 675 | if (handleKeyEvent) { |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 676 | // Select the closest icon in the next line, otherwise select the button bar |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 677 | View newIcon = getClosestIconOnLine(layout, parent, v, 1); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 678 | if (newIcon != null) { |
| 679 | newIcon.requestFocus(); |
| 680 | wasHandled = true; |
Winson Chung | 4d279d9 | 2011-07-21 11:46:32 -0700 | [diff] [blame] | 681 | } else if (hotseat != null) { |
| 682 | hotseat.requestFocus(); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | break; |
| 686 | case KeyEvent.KEYCODE_PAGE_UP: |
| 687 | if (handleKeyEvent) { |
| 688 | // Select the first icon on the previous page or the first icon on this page |
| 689 | // if there is no previous page |
| 690 | if (pageIndex > 0) { |
| 691 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex - 1); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 692 | View newIcon = getIconInDirection(layout, parent, -1, 1); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 693 | if (newIcon != null) { |
| 694 | newIcon.requestFocus(); |
| 695 | } else { |
| 696 | // Snap to the previous page |
| 697 | workspace.snapToPage(pageIndex - 1); |
| 698 | } |
| 699 | } else { |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 700 | View newIcon = getIconInDirection(layout, parent, -1, 1); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 701 | if (newIcon != null) { |
| 702 | newIcon.requestFocus(); |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | wasHandled = true; |
| 707 | break; |
| 708 | case KeyEvent.KEYCODE_PAGE_DOWN: |
| 709 | if (handleKeyEvent) { |
| 710 | // Select the first icon on the next page or the last icon on this page |
| 711 | // if there is no previous page |
| 712 | if (pageIndex < (pageCount - 1)) { |
| 713 | parent = getCellLayoutChildrenForIndex(workspace, pageIndex + 1); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 714 | View newIcon = getIconInDirection(layout, parent, -1, 1); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 715 | if (newIcon != null) { |
| 716 | newIcon.requestFocus(); |
| 717 | } else { |
| 718 | // Snap to the next page |
| 719 | workspace.snapToPage(pageIndex + 1); |
| 720 | } |
| 721 | } else { |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 722 | View newIcon = getIconInDirection(layout, parent, |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 723 | parent.getChildCount(), -1); |
| 724 | if (newIcon != null) { |
| 725 | newIcon.requestFocus(); |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | wasHandled = true; |
| 730 | break; |
| 731 | case KeyEvent.KEYCODE_MOVE_HOME: |
| 732 | if (handleKeyEvent) { |
| 733 | // Select the first icon on this page |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 734 | View newIcon = getIconInDirection(layout, parent, -1, 1); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 735 | if (newIcon != null) { |
| 736 | newIcon.requestFocus(); |
| 737 | } |
| 738 | } |
| 739 | wasHandled = true; |
| 740 | break; |
| 741 | case KeyEvent.KEYCODE_MOVE_END: |
| 742 | if (handleKeyEvent) { |
| 743 | // Select the last icon on this page |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 744 | View newIcon = getIconInDirection(layout, parent, |
| 745 | parent.getChildCount(), -1); |
| 746 | if (newIcon != null) { |
| 747 | newIcon.requestFocus(); |
| 748 | } |
| 749 | } |
| 750 | wasHandled = true; |
| 751 | break; |
| 752 | default: break; |
| 753 | } |
| 754 | return wasHandled; |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * Handles key events for items in a Folder. |
| 759 | */ |
| 760 | static boolean handleFolderKeyEvent(View v, int keyCode, KeyEvent e) { |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 761 | ShortcutAndWidgetContainer parent = (ShortcutAndWidgetContainer) v.getParent(); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 762 | final CellLayout layout = (CellLayout) parent.getParent(); |
Winson Chung | a1f133d | 2013-07-25 11:14:30 -0700 | [diff] [blame] | 763 | final ScrollView scrollView = (ScrollView) layout.getParent(); |
| 764 | final Folder folder = (Folder) scrollView.getParent(); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 765 | View title = folder.mFolderName; |
| 766 | |
| 767 | final int action = e.getAction(); |
| 768 | final boolean handleKeyEvent = (action != KeyEvent.ACTION_UP); |
| 769 | boolean wasHandled = false; |
| 770 | switch (keyCode) { |
| 771 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 772 | if (handleKeyEvent) { |
| 773 | // Select the previous icon |
| 774 | View newIcon = getIconInDirection(layout, parent, v, -1); |
| 775 | if (newIcon != null) { |
| 776 | newIcon.requestFocus(); |
| 777 | } |
| 778 | } |
| 779 | wasHandled = true; |
| 780 | break; |
| 781 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 782 | if (handleKeyEvent) { |
| 783 | // Select the next icon |
| 784 | View newIcon = getIconInDirection(layout, parent, v, 1); |
| 785 | if (newIcon != null) { |
| 786 | newIcon.requestFocus(); |
| 787 | } else { |
| 788 | title.requestFocus(); |
| 789 | } |
| 790 | } |
| 791 | wasHandled = true; |
| 792 | break; |
| 793 | case KeyEvent.KEYCODE_DPAD_UP: |
| 794 | if (handleKeyEvent) { |
| 795 | // Select the closest icon in the previous line |
| 796 | View newIcon = getClosestIconOnLine(layout, parent, v, -1); |
| 797 | if (newIcon != null) { |
| 798 | newIcon.requestFocus(); |
| 799 | } |
| 800 | } |
| 801 | wasHandled = true; |
| 802 | break; |
| 803 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 804 | if (handleKeyEvent) { |
| 805 | // Select the closest icon in the next line |
| 806 | View newIcon = getClosestIconOnLine(layout, parent, v, 1); |
| 807 | if (newIcon != null) { |
| 808 | newIcon.requestFocus(); |
| 809 | } else { |
| 810 | title.requestFocus(); |
| 811 | } |
| 812 | } |
| 813 | wasHandled = true; |
| 814 | break; |
| 815 | case KeyEvent.KEYCODE_MOVE_HOME: |
| 816 | if (handleKeyEvent) { |
| 817 | // Select the first icon on this page |
| 818 | View newIcon = getIconInDirection(layout, parent, -1, 1); |
| 819 | if (newIcon != null) { |
| 820 | newIcon.requestFocus(); |
| 821 | } |
| 822 | } |
| 823 | wasHandled = true; |
| 824 | break; |
| 825 | case KeyEvent.KEYCODE_MOVE_END: |
| 826 | if (handleKeyEvent) { |
| 827 | // Select the last icon on this page |
| 828 | View newIcon = getIconInDirection(layout, parent, |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 829 | parent.getChildCount(), -1); |
| 830 | if (newIcon != null) { |
| 831 | newIcon.requestFocus(); |
| 832 | } |
| 833 | } |
| 834 | wasHandled = true; |
| 835 | break; |
| 836 | default: break; |
| 837 | } |
| 838 | return wasHandled; |
| 839 | } |
| 840 | } |