diff options
author | Bilal Alsharifi <bilal.alsharifi@gmail.com> | 2021-08-10 11:09:31 -0400 |
---|---|---|
committer | Bilal Alsharifi <bilal.alsharifi@gmail.com> | 2021-08-10 11:09:31 -0400 |
commit | e7814e641f459794afff636ee592aee1f6051005 (patch) | |
tree | 817c8f7999ec0d47086766d2c9a4342054f3d108 | |
parent | 6ff10a158a7218587f144f5944d1fbc1f089bbd3 (diff) | |
download | sdl_android-e7814e641f459794afff636ee592aee1f6051005.tar.gz |
Update Uniqueness helper methods
-rw-r--r-- | base/src/main/java/com/smartdevicelink/managers/screen/menu/MenuReplaceOperation.java | 110 |
1 files changed, 50 insertions, 60 deletions
diff --git a/base/src/main/java/com/smartdevicelink/managers/screen/menu/MenuReplaceOperation.java b/base/src/main/java/com/smartdevicelink/managers/screen/menu/MenuReplaceOperation.java index b7a7b4b1b..d3084d494 100644 --- a/base/src/main/java/com/smartdevicelink/managers/screen/menu/MenuReplaceOperation.java +++ b/base/src/main/java/com/smartdevicelink/managers/screen/menu/MenuReplaceOperation.java @@ -3,6 +3,7 @@ package com.smartdevicelink.managers.screen.menu; import static com.smartdevicelink.managers.ManagerUtility.WindowCapabilityUtility.hasImageFieldOfName; import static com.smartdevicelink.managers.ManagerUtility.WindowCapabilityUtility.hasTextFieldOfName; import static com.smartdevicelink.managers.screen.menu.MenuReplaceUtilities.addMenuRequestWithCommandId; +import static com.smartdevicelink.managers.screen.menu.MenuReplaceUtilities.cloneMenuCellsList; import static com.smartdevicelink.managers.screen.menu.MenuReplaceUtilities.commandIdForRPCRequest; import static com.smartdevicelink.managers.screen.menu.MenuReplaceUtilities.deleteCommandsForCells; import static com.smartdevicelink.managers.screen.menu.MenuReplaceUtilities.findAllArtworksToBeUploadedFromCells; @@ -47,6 +48,8 @@ class MenuReplaceOperation extends Task { private WindowCapability windowCapability; private List<MenuCell> currentMenu; private final List<MenuCell> updatedMenu; + private List<MenuCell> currentStrippedMenu; + private List<MenuCell> updatedStrippedMenu; private final boolean isDynamicMenuUpdateActive; private final MenuManagerCompletionListener operationCompletionListener; private MenuConfiguration menuConfiguration; @@ -372,78 +375,23 @@ class MenuReplaceOperation extends Task { return stringBuilder.toString(); } - private List<MenuCell> cloneMenuCellsList(List<MenuCell> originalList) { - if (originalList == null) { - return new ArrayList<>(); - } - - List<MenuCell> clone = new ArrayList<>(); - for (MenuCell menuCell : originalList) { - clone.add(menuCell.clone()); - } - return clone; - } - - private void addUniqueNamesToCellsWithDuplicatePrimaryText(List<MenuCell> cells) { - HashMap<String, Integer> countsMap = new HashMap<>(); - - for (MenuCell cell : cells) { - String cellTitle = cell.getTitle(); - Integer counter = countsMap.get(cellTitle); - - if (counter != null) { - countsMap.put(cellTitle, ++counter); - cell.setUniqueTitle(cellTitle + " (" + counter + ")"); - } else { - countsMap.put(cellTitle, 1); - cell.setUniqueTitle(cellTitle); - } - - if (isSubMenuCell(cell) && !cell.getSubCells().isEmpty()) { - addUniqueNamesToCellsWithDuplicatePrimaryText(cell.getSubCells()); - } - } - } - - void addUniqueNamesBasedOnStrippedCells(List<MenuCell> strippedCells, List<MenuCell> originalCells) { - if (strippedCells == null || originalCells == null || strippedCells.size() != originalCells.size()) { - return; - } - // Tracks how many of each cell primary text there are so that we can append numbers to make each unique as necessary - HashMap<MenuCell, Integer> countsMap = new HashMap<>(); - for (int i = 0; i < strippedCells.size(); i++) { - MenuCell cell = strippedCells.get(i); - Integer counter = countsMap.get(cell); - if (counter != null) { - countsMap.put(cell, ++counter); - originalCells.get(i).setUniqueTitle(originalCells.get(i).getTitle() + " (" + counter + ")"); - } else { - countsMap.put(cell, 1); - originalCells.get(i).setUniqueTitle(originalCells.get(i).getTitle()); - } - - if (isSubMenuCell(cell) && !cell.getSubCells().isEmpty()) { - addUniqueNamesBasedOnStrippedCells(cell.getSubCells(), originalCells.get(i).getSubCells()); - } - } - } - - List<MenuCell> removeUnusedProperties(List<MenuCell> cells) { + List<MenuCell> cellsWithRemovedPropertiesFromCells(List<MenuCell> cells, WindowCapability windowCapability) { if (cells == null) { return null; } + List<MenuCell> removePropertiesClone = cloneMenuCellsList(cells); + for (MenuCell cell : removePropertiesClone) { // Strip away fields that cannot be used to determine uniqueness visually including fields not supported by the HMI cell.setVoiceCommands(null); - cell.setUniqueTitle(null); - cell.setMenuSelectionListener(null); // Don't check ImageFieldName.subMenuIcon because it was added in 7.0 when the feature was added in 5.0. // Just assume that if cmdIcon is not available, the submenu icon is not either. if (!hasImageFieldOfName(windowCapability, ImageFieldName.cmdIcon)) { cell.setIcon(null); } + // Check for subMenu fields supported if (isSubMenuCell(cell)) { if (!hasTextFieldOfName(windowCapability, TextFieldName.menuSubMenuSecondaryText)) { @@ -455,7 +403,7 @@ class MenuReplaceOperation extends Task { if (!hasImageFieldOfName(windowCapability, ImageFieldName.menuSubMenuSecondaryImage)) { cell.setSecondaryArtwork(null); } - cell.setSubCells(removeUnusedProperties(cell.getSubCells())); + cell.setSubCells(cellsWithRemovedPropertiesFromCells(cell.getSubCells(), windowCapability)); } else { if (!hasTextFieldOfName(windowCapability, TextFieldName.menuCommandSecondaryText)) { cell.setSecondaryText(null); @@ -471,6 +419,48 @@ class MenuReplaceOperation extends Task { return removePropertiesClone; } + private void addUniqueNamesToCells(List<MenuCell> menuCells, boolean supportsMenuUniqueness) { + if (menuCells == null) { + return; + } + + // Tracks how many of each cell primary text there are so that we can append numbers to make each unique as necessary + HashMap<String, Integer> dictCounter = new HashMap<>(); + + for (MenuCell cell : menuCells) { + String key = supportsMenuUniqueness ? String.valueOf(cell.hashCode()) : cell.getTitle(); + Integer counter = dictCounter.get(key); + + if (counter != null) { + dictCounter.put(key, ++counter); + } else { + dictCounter.put(key, 1); + } + + counter = dictCounter.get(key); + if (counter != null && counter > 1) { + cell.setUniqueTitle(cell.getTitle() + " (" + counter + ")"); + } + + if (isSubMenuCell(cell) && !cell.getSubCells().isEmpty()) { + addUniqueNamesToCells(cell.getSubCells(), supportsMenuUniqueness); + } + } + } + + private void applyUniqueNamesOnCells(List<MenuCell> fromMenuCells, List<MenuCell> toMenuCells) { + if (fromMenuCells.size() != toMenuCells.size()) { + return; + } + + for (int i = 0; i < fromMenuCells.size(); i++) { + toMenuCells.get(i).setUniqueTitle(fromMenuCells.get(i).getUniqueTitle()); + if (isSubMenuCell(fromMenuCells.get(i)) && !fromMenuCells.get(i).getSubCells().isEmpty()) { + applyUniqueNamesOnCells(fromMenuCells.get(i).getSubCells(), toMenuCells.get(i).getSubCells()); + } + } + } + void setMenuConfiguration(MenuConfiguration menuConfiguration) { this.menuConfiguration = menuConfiguration; } |