1 /* 2 * Copyright (C) 2015 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.launcher3.util; 18 19 import android.util.Log; 20 import android.view.KeyEvent; 21 import android.view.View; 22 import android.view.ViewGroup; 23 24 import com.android.launcher3.CellLayout; 25 import com.android.launcher3.DeviceProfile; 26 import com.android.launcher3.ShortcutAndWidgetContainer; 27 import com.android.launcher3.config.FeatureFlags; 28 29 import java.util.Arrays; 30 31 /** 32 * Calculates the next item that a {@link KeyEvent} should change the focus to. 33 *<p> 34 * Note, this utility class calculates everything regards to icon index and its (x,y) coordinates. 35 * Currently supports: 36 * <ul> 37 * <li> full matrix of cells that are 1x1 38 * <li> sparse matrix of cells that are 1x1 39 * [ 1][ ][ 2][ ] 40 * [ ][ ][ 3][ ] 41 * [ ][ 4][ ][ ] 42 * [ ][ 5][ 6][ 7] 43 * </ul> 44 * *<p> 45 * For testing, one can use a BT keyboard, or use following adb command. 46 * ex. $ adb shell input keyevent 20 // KEYCODE_DPAD_LEFT 47 */ 48 public class FocusLogic { 49 50 private static final String TAG = "FocusLogic"; 51 private static final boolean DEBUG = false; 52 53 /** Item and page index related constant used by {@link #handleKeyEvent}. */ 54 public static final int NOOP = -1; 55 56 public static final int PREVIOUS_PAGE_RIGHT_COLUMN = -2; 57 public static final int PREVIOUS_PAGE_FIRST_ITEM = -3; 58 public static final int PREVIOUS_PAGE_LAST_ITEM = -4; 59 public static final int PREVIOUS_PAGE_LEFT_COLUMN = -5; 60 61 public static final int CURRENT_PAGE_FIRST_ITEM = -6; 62 public static final int CURRENT_PAGE_LAST_ITEM = -7; 63 64 public static final int NEXT_PAGE_FIRST_ITEM = -8; 65 public static final int NEXT_PAGE_LEFT_COLUMN = -9; 66 public static final int NEXT_PAGE_RIGHT_COLUMN = -10; 67 68 public static final int ALL_APPS_COLUMN = -11; 69 70 // Matrix related constant. 71 public static final int EMPTY = -1; 72 public static final int PIVOT = 100; 73 74 /** 75 * Returns true only if this utility class handles the key code. 76 */ 77 public static boolean shouldConsume(int keyCode) { 78 return (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT || 79 keyCode == KeyEvent.KEYCODE_DPAD_UP || keyCode == KeyEvent.KEYCODE_DPAD_DOWN || 80 keyCode == KeyEvent.KEYCODE_MOVE_HOME || keyCode == KeyEvent.KEYCODE_MOVE_END || 81 keyCode == KeyEvent.KEYCODE_PAGE_UP || keyCode == KeyEvent.KEYCODE_PAGE_DOWN); 82 } 83 84 public static int handleKeyEvent(int keyCode, int [][] map, int iconIdx, int pageIndex, 85 int pageCount, boolean isRtl) { 86 87 int cntX = map == null ? -1 : map.length; 88 int cntY = map == null ? -1 : map[0].length; 89 90 if (DEBUG) { 91 Log.v(TAG, String.format( 92 "handleKeyEvent START: cntX=%d, cntY=%d, iconIdx=%d, pageIdx=%d, pageCnt=%d", 93 cntX, cntY, iconIdx, pageIndex, pageCount)); 94 } 95 96 int newIndex = NOOP; 97 switch (keyCode) { 98 case KeyEvent.KEYCODE_DPAD_LEFT: 99 newIndex = handleDpadHorizontal(iconIdx, cntX, cntY, map, -1 /*increment*/, isRtl); 100 if (!isRtl && newIndex == NOOP && pageIndex > 0) { 101 newIndex = PREVIOUS_PAGE_RIGHT_COLUMN; 102 } else if (isRtl && newIndex == NOOP && pageIndex < pageCount - 1) { 103 newIndex = NEXT_PAGE_RIGHT_COLUMN; 104 } 105 break; 106 case KeyEvent.KEYCODE_DPAD_RIGHT: 107 newIndex = handleDpadHorizontal(iconIdx, cntX, cntY, map, 1 /*increment*/, isRtl); 108 if (!isRtl && newIndex == NOOP && pageIndex < pageCount - 1) { 109 newIndex = NEXT_PAGE_LEFT_COLUMN; 110 } else if (isRtl && newIndex == NOOP && pageIndex > 0) { 111 newIndex = PREVIOUS_PAGE_LEFT_COLUMN; 112 } 113 break; 114 case KeyEvent.KEYCODE_DPAD_DOWN: 115 newIndex = handleDpadVertical(iconIdx, cntX, cntY, map, 1 /*increment*/); 116 break; 117 case KeyEvent.KEYCODE_DPAD_UP: 118 newIndex = handleDpadVertical(iconIdx, cntX, cntY, map, -1 /*increment*/); 119 break; 120 case KeyEvent.KEYCODE_MOVE_HOME: 121 newIndex = handleMoveHome(); 122 break; 123 case KeyEvent.KEYCODE_MOVE_END: 124 newIndex = handleMoveEnd(); 125 break; 126 case KeyEvent.KEYCODE_PAGE_DOWN: 127 newIndex = handlePageDown(pageIndex, pageCount); 128 break; 129 case KeyEvent.KEYCODE_PAGE_UP: 130 newIndex = handlePageUp(pageIndex); 131 break; 132 default: 133 break; 134 } 135 136 if (DEBUG) { 137 Log.v(TAG, String.format("handleKeyEvent FINISH: index [%d -> %s]", 138 iconIdx, getStringIndex(newIndex))); 139 } 140 return newIndex; 141 } 142 143 /** 144 * Returns a matrix of size (m x n) that has been initialized with {@link #EMPTY}. 145 * 146 * @param m number of columns in the matrix 147 * @param n number of rows in the matrix 148 */ 149 // TODO: get rid of dynamic matrix creation. 150 private static int[][] createFullMatrix(int m, int n) { 151 int[][] matrix = new int [m][n]; 152 153 for (int i=0; i < m;i++) { 154 Arrays.fill(matrix[i], EMPTY); 155 } 156 return matrix; 157 } 158 159 /** 160 * Returns a matrix of size same as the {@link CellLayout} dimension that is initialized with the 161 * index of the child view. 162 */ 163 // TODO: get rid of the dynamic matrix creation 164 public static int[][] createSparseMatrix(CellLayout layout) { 165 ShortcutAndWidgetContainer parent = layout.getShortcutsAndWidgets(); 166 final int m = layout.getCountX(); 167 final int n = layout.getCountY(); 168 final boolean invert = parent.invertLayoutHorizontally(); 169 170 int[][] matrix = createFullMatrix(m, n); 171 172 // Iterate thru the children. 173 for (int i = 0; i < parent.getChildCount(); i++ ) { 174 View cell = parent.getChildAt(i); 175 if (!cell.isFocusable()) { 176 continue; 177 } 178 int cx = ((CellLayout.LayoutParams) cell.getLayoutParams()).cellX; 179 int cy = ((CellLayout.LayoutParams) cell.getLayoutParams()).cellY; 180 matrix[invert ? (m - cx - 1) : cx][cy] = i; 181 } 182 if (DEBUG) { 183 printMatrix(matrix); 184 } 185 return matrix; 186 } 187 188 /** 189 * Creates a sparse matrix that merges the icon and hotseat view group using the cell layout. 190 * The size of the returning matrix is [icon column count x (icon + hotseat row count)] 191 * in portrait orientation. In landscape, [(icon + hotseat) column count x (icon row count)] 192 */ 193 // TODO: get rid of the dynamic matrix creation 194 public static int[][] createSparseMatrixWithHotseat( 195 CellLayout iconLayout, CellLayout hotseatLayout, DeviceProfile dp) { 196 197 ViewGroup iconParent = iconLayout.getShortcutsAndWidgets(); 198 ViewGroup hotseatParent = hotseatLayout.getShortcutsAndWidgets(); 199 200 boolean isHotseatHorizontal = !dp.isVerticalBarLayout(); 201 boolean moreIconsInHotseatThanWorkspace = !FeatureFlags.NO_ALL_APPS_ICON && 202 (isHotseatHorizontal 203 ? hotseatLayout.getCountX() > iconLayout.getCountX() 204 : hotseatLayout.getCountY() > iconLayout.getCountY()); 205 206 int m, n; 207 if (isHotseatHorizontal) { 208 m = hotseatLayout.getCountX(); 209 n = iconLayout.getCountY() + hotseatLayout.getCountY(); 210 } else { 211 m = iconLayout.getCountX() + hotseatLayout.getCountX(); 212 n = hotseatLayout.getCountY(); 213 } 214 int[][] matrix = createFullMatrix(m, n); 215 if (moreIconsInHotseatThanWorkspace) { 216 int allappsiconRank = dp.inv.getAllAppsButtonRank(); 217 if (isHotseatHorizontal) { 218 for (int j = 0; j < n; j++) { 219 matrix[allappsiconRank][j] = ALL_APPS_COLUMN; 220 } 221 } else { 222 for (int j = 0; j < m; j++) { 223 matrix[j][allappsiconRank] = ALL_APPS_COLUMN; 224 } 225 } 226 } 227 // Iterate thru the children of the workspace. 228 for (int i = 0; i < iconParent.getChildCount(); i++) { 229 View cell = iconParent.getChildAt(i); 230 if (!cell.isFocusable()) { 231 continue; 232 } 233 int cx = ((CellLayout.LayoutParams) cell.getLayoutParams()).cellX; 234 int cy = ((CellLayout.LayoutParams) cell.getLayoutParams()).cellY; 235 if (moreIconsInHotseatThanWorkspace) { 236 int allappsiconRank = dp.inv.getAllAppsButtonRank(); 237 if (isHotseatHorizontal && cx >= allappsiconRank) { 238 // Add 1 to account for the All Apps button. 239 cx++; 240 } 241 if (!isHotseatHorizontal && cy >= allappsiconRank) { 242 // Add 1 to account for the All Apps button. 243 cy++; 244 } 245 } 246 matrix[cx][cy] = i; 247 } 248 249 // Iterate thru the children of the hotseat. 250 for (int i = hotseatParent.getChildCount() - 1; i >= 0; i--) { 251 if (isHotseatHorizontal) { 252 int cx = ((CellLayout.LayoutParams) 253 hotseatParent.getChildAt(i).getLayoutParams()).cellX; 254 matrix[cx][iconLayout.getCountY()] = iconParent.getChildCount() + i; 255 } else { 256 int cy = ((CellLayout.LayoutParams) 257 hotseatParent.getChildAt(i).getLayoutParams()).cellY; 258 matrix[iconLayout.getCountX()][cy] = iconParent.getChildCount() + i; 259 } 260 } 261 if (DEBUG) { 262 printMatrix(matrix); 263 } 264 return matrix; 265 } 266 267 /** 268 * Creates a sparse matrix that merges the icon of previous/next page and last column of 269 * current page. When left key is triggered on the leftmost column, sparse matrix is created 270 * that combines previous page matrix and an extra column on the right. Likewise, when right 271 * key is triggered on the rightmost column, sparse matrix is created that combines this column 272 * on the 0th column and the next page matrix. 273 * 274 * @param pivotX x coordinate of the focused item in the current page 275 * @param pivotY y coordinate of the focused item in the current page 276 */ 277 // TODO: get rid of the dynamic matrix creation 278 public static int[][] createSparseMatrixWithPivotColumn(CellLayout iconLayout, 279 int pivotX, int pivotY) { 280 281 ViewGroup iconParent = iconLayout.getShortcutsAndWidgets(); 282 283 int[][] matrix = createFullMatrix(iconLayout.getCountX() + 1, iconLayout.getCountY()); 284 285 // Iterate thru the children of the top parent. 286 for (int i = 0; i < iconParent.getChildCount(); i++) { 287 View cell = iconParent.getChildAt(i); 288 if (!cell.isFocusable()) { 289 continue; 290 } 291 int cx = ((CellLayout.LayoutParams) cell.getLayoutParams()).cellX; 292 int cy = ((CellLayout.LayoutParams) cell.getLayoutParams()).cellY; 293 if (pivotX < 0) { 294 matrix[cx - pivotX][cy] = i; 295 } else { 296 matrix[cx][cy] = i; 297 } 298 } 299 300 if (pivotX < 0) { 301 matrix[0][pivotY] = PIVOT; 302 } else { 303 matrix[pivotX][pivotY] = PIVOT; 304 } 305 if (DEBUG) { 306 printMatrix(matrix); 307 } 308 return matrix; 309 } 310 311 // 312 // key event handling methods. 313 // 314 315 /** 316 * Calculates icon that has is closest to the horizontal axis in reference to the cur icon. 317 * 318 * Example of the check order for KEYCODE_DPAD_RIGHT: 319 * [ ][ ][13][14][15] 320 * [ ][ 6][ 8][10][12] 321 * [ X][ 1][ 2][ 3][ 4] 322 * [ ][ 5][ 7][ 9][11] 323 */ 324 // TODO: add unit tests to verify all permutation. 325 private static int handleDpadHorizontal(int iconIdx, int cntX, int cntY, 326 int[][] matrix, int increment, boolean isRtl) { 327 if(matrix == null) { 328 throw new IllegalStateException("Dpad navigation requires a matrix."); 329 } 330 int newIconIndex = NOOP; 331 332 int xPos = -1; 333 int yPos = -1; 334 // Figure out the location of the icon. 335 for (int i = 0; i < cntX; i++) { 336 for (int j = 0; j < cntY; j++) { 337 if (matrix[i][j] == iconIdx) { 338 xPos = i; 339 yPos = j; 340 } 341 } 342 } 343 if (DEBUG) { 344 Log.v(TAG, String.format("\thandleDpadHorizontal: \t[x, y]=[%d, %d] iconIndex=%d", 345 xPos, yPos, iconIdx)); 346 } 347 348 // Rule1: check first in the horizontal direction 349 for (int x = xPos + increment; 0 <= x && x < cntX; x += increment) { 350 if ((newIconIndex = inspectMatrix(x, yPos, cntX, cntY, matrix)) != NOOP 351 && newIconIndex != ALL_APPS_COLUMN) { 352 return newIconIndex; 353 } 354 } 355 356 // Rule2: check (x1-n, yPos + increment), (x1-n, yPos - increment) 357 // (x2-n, yPos + 2*increment), (x2-n, yPos - 2*increment) 358 int nextYPos1; 359 int nextYPos2; 360 boolean haveCrossedAllAppsColumn1 = false; 361 boolean haveCrossedAllAppsColumn2 = false; 362 int x = -1; 363 for (int coeff = 1; coeff < cntY; coeff++) { 364 nextYPos1 = yPos + coeff * increment; 365 nextYPos2 = yPos - coeff * increment; 366 x = xPos + increment * coeff; 367 if (inspectMatrix(x, nextYPos1, cntX, cntY, matrix) == ALL_APPS_COLUMN) { 368 haveCrossedAllAppsColumn1 = true; 369 } 370 if (inspectMatrix(x, nextYPos2, cntX, cntY, matrix) == ALL_APPS_COLUMN) { 371 haveCrossedAllAppsColumn2 = true; 372 } 373 for (; 0 <= x && x < cntX; x += increment) { 374 int offset1 = haveCrossedAllAppsColumn1 && x < cntX - 1 ? increment : 0; 375 newIconIndex = inspectMatrix(x, nextYPos1 + offset1, cntX, cntY, matrix); 376 if (newIconIndex != NOOP) { 377 return newIconIndex; 378 } 379 int offset2 = haveCrossedAllAppsColumn2 && x < cntX - 1 ? -increment : 0; 380 newIconIndex = inspectMatrix(x, nextYPos2 + offset2, cntX, cntY, matrix); 381 if (newIconIndex != NOOP) { 382 return newIconIndex; 383 } 384 } 385 } 386 387 // Rule3: if switching between pages, do a brute-force search to find an item that was 388 // missed by rules 1 and 2 (such as when going from a bottom right icon to top left) 389 if (iconIdx == PIVOT) { 390 if (isRtl) { 391 return increment < 0 ? NEXT_PAGE_FIRST_ITEM : PREVIOUS_PAGE_LAST_ITEM; 392 } 393 return increment < 0 ? PREVIOUS_PAGE_LAST_ITEM : NEXT_PAGE_FIRST_ITEM; 394 } 395 return newIconIndex; 396 } 397 398 /** 399 * Calculates icon that is closest to the vertical axis in reference to the current icon. 400 * 401 * Example of the check order for KEYCODE_DPAD_DOWN: 402 * [ ][ ][ ][ X][ ][ ][ ] 403 * [ ][ ][ 5][ 1][ 4][ ][ ] 404 * [ ][10][ 7][ 2][ 6][ 9][ ] 405 * [14][12][ 9][ 3][ 8][11][13] 406 */ 407 // TODO: add unit tests to verify all permutation. 408 private static int handleDpadVertical(int iconIndex, int cntX, int cntY, 409 int [][] matrix, int increment) { 410 int newIconIndex = NOOP; 411 if(matrix == null) { 412 throw new IllegalStateException("Dpad navigation requires a matrix."); 413 } 414 415 int xPos = -1; 416 int yPos = -1; 417 // Figure out the location of the icon. 418 for (int i = 0; i< cntX; i++) { 419 for (int j = 0; j < cntY; j++) { 420 if (matrix[i][j] == iconIndex) { 421 xPos = i; 422 yPos = j; 423 } 424 } 425 } 426 427 if (DEBUG) { 428 Log.v(TAG, String.format("\thandleDpadVertical: \t[x, y]=[%d, %d] iconIndex=%d", 429 xPos, yPos, iconIndex)); 430 } 431 432 // Rule1: check first in the dpad direction 433 for (int y = yPos + increment; 0 <= y && y <cntY && 0 <= y; y += increment) { 434 if ((newIconIndex = inspectMatrix(xPos, y, cntX, cntY, matrix)) != NOOP 435 && newIconIndex != ALL_APPS_COLUMN) { 436 return newIconIndex; 437 } 438 } 439 440 // Rule2: check (xPos + increment, y_(1-n)), (xPos - increment, y_(1-n)) 441 // (xPos + 2*increment, y_(2-n))), (xPos - 2*increment, y_(2-n)) 442 int nextXPos1; 443 int nextXPos2; 444 boolean haveCrossedAllAppsColumn1 = false; 445 boolean haveCrossedAllAppsColumn2 = false; 446 int y = -1; 447 for (int coeff = 1; coeff < cntX; coeff++) { 448 nextXPos1 = xPos + coeff * increment; 449 nextXPos2 = xPos - coeff * increment; 450 y = yPos + increment * coeff; 451 if (inspectMatrix(nextXPos1, y, cntX, cntY, matrix) == ALL_APPS_COLUMN) { 452 haveCrossedAllAppsColumn1 = true; 453 } 454 if (inspectMatrix(nextXPos2, y, cntX, cntY, matrix) == ALL_APPS_COLUMN) { 455 haveCrossedAllAppsColumn2 = true; 456 } 457 for (; 0 <= y && y < cntY; y = y + increment) { 458 int offset1 = haveCrossedAllAppsColumn1 && y < cntY - 1 ? increment : 0; 459 newIconIndex = inspectMatrix(nextXPos1 + offset1, y, cntX, cntY, matrix); 460 if (newIconIndex != NOOP) { 461 return newIconIndex; 462 } 463 int offset2 = haveCrossedAllAppsColumn2 && y < cntY - 1 ? -increment : 0; 464 newIconIndex = inspectMatrix(nextXPos2 + offset2, y, cntX, cntY, matrix); 465 if (newIconIndex != NOOP) { 466 return newIconIndex; 467 } 468 } 469 } 470 return newIconIndex; 471 } 472 473 private static int handleMoveHome() { 474 return CURRENT_PAGE_FIRST_ITEM; 475 } 476 477 private static int handleMoveEnd() { 478 return CURRENT_PAGE_LAST_ITEM; 479 } 480 481 private static int handlePageDown(int pageIndex, int pageCount) { 482 if (pageIndex < pageCount -1) { 483 return NEXT_PAGE_FIRST_ITEM; 484 } 485 return CURRENT_PAGE_LAST_ITEM; 486 } 487 488 private static int handlePageUp(int pageIndex) { 489 if (pageIndex > 0) { 490 return PREVIOUS_PAGE_FIRST_ITEM; 491 } else { 492 return CURRENT_PAGE_FIRST_ITEM; 493 } 494 } 495 496 // 497 // Helper methods. 498 // 499 500 private static boolean isValid(int xPos, int yPos, int countX, int countY) { 501 return (0 <= xPos && xPos < countX && 0 <= yPos && yPos < countY); 502 } 503 504 private static int inspectMatrix(int x, int y, int cntX, int cntY, int[][] matrix) { 505 int newIconIndex = NOOP; 506 if (isValid(x, y, cntX, cntY)) { 507 if (matrix[x][y] != -1) { 508 newIconIndex = matrix[x][y]; 509 if (DEBUG) { 510 Log.v(TAG, String.format("\t\tinspect: \t[x, y]=[%d, %d] %d", 511 x, y, matrix[x][y])); 512 } 513 return newIconIndex; 514 } 515 } 516 return newIconIndex; 517 } 518 519 /** 520 * Only used for debugging. 521 */ 522 private static String getStringIndex(int index) { 523 switch(index) { 524 case NOOP: return "NOOP"; 525 case PREVIOUS_PAGE_FIRST_ITEM: return "PREVIOUS_PAGE_FIRST"; 526 case PREVIOUS_PAGE_LAST_ITEM: return "PREVIOUS_PAGE_LAST"; 527 case PREVIOUS_PAGE_RIGHT_COLUMN:return "PREVIOUS_PAGE_RIGHT_COLUMN"; 528 case CURRENT_PAGE_FIRST_ITEM: return "CURRENT_PAGE_FIRST"; 529 case CURRENT_PAGE_LAST_ITEM: return "CURRENT_PAGE_LAST"; 530 case NEXT_PAGE_FIRST_ITEM: return "NEXT_PAGE_FIRST"; 531 case NEXT_PAGE_LEFT_COLUMN: return "NEXT_PAGE_LEFT_COLUMN"; 532 case ALL_APPS_COLUMN: return "ALL_APPS_COLUMN"; 533 default: 534 return Integer.toString(index); 535 } 536 } 537 538 /** 539 * Only used for debugging. 540 */ 541 private static void printMatrix(int[][] matrix) { 542 Log.v(TAG, "\tprintMap:"); 543 int m = matrix.length; 544 int n = matrix[0].length; 545 546 for (int j=0; j < n; j++) { 547 String colY = "\t\t"; 548 for (int i=0; i < m; i++) { 549 colY += String.format("%3d",matrix[i][j]); 550 } 551 Log.v(TAG, colY); 552 } 553 } 554 555 /** 556 * @param edgeColumn the column of the new icon. either {@link #NEXT_PAGE_LEFT_COLUMN} or 557 * {@link #NEXT_PAGE_RIGHT_COLUMN} 558 * @return the view adjacent to {@param oldView} in the {@param nextPage} of the folder. 559 */ 560 public static View getAdjacentChildInNextFolderPage( 561 ShortcutAndWidgetContainer nextPage, View oldView, int edgeColumn) { 562 final int newRow = ((CellLayout.LayoutParams) oldView.getLayoutParams()).cellY; 563 564 int column = (edgeColumn == NEXT_PAGE_LEFT_COLUMN) ^ nextPage.invertLayoutHorizontally() 565 ? 0 : (((CellLayout) nextPage.getParent()).getCountX() - 1); 566 567 for (; column >= 0; column--) { 568 for (int row = newRow; row >= 0; row--) { 569 View newView = nextPage.getChildAt(column, row); 570 if (newView != null) { 571 return newView; 572 } 573 } 574 } 575 return null; 576 } 577 } 578