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