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