ImageGenerator: add an option to specify the supported locales

A smaller list of locales helps to reduce the size of the generated
images; and mitigate the additional space requirement on the tight boot
image for some devices. The caller of the program will be responsible
for providing the locale list.

Also add a verbose option and switch to logger.

Bug: 74397117
Test: generate and check the image
Change-Id: I7b7cee681ccdc6e13fdd0575cf02f8add2ad9a2d
diff --git a/tools/image_generator/ImageGenerator.java b/tools/image_generator/ImageGenerator.java
index 0a1c85b..9d88267 100644
--- a/tools/image_generator/ImageGenerator.java
+++ b/tools/image_generator/ImageGenerator.java
@@ -46,6 +46,8 @@
 import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.TreeMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.imageio.ImageIO;
 import javax.xml.parsers.DocumentBuilder;
@@ -59,6 +61,8 @@
 
     private static final float DEFAULT_FONT_SIZE = 40;
 
+    private static final Logger LOGGER = Logger.getLogger(ImageGenerator.class.getName());
+
     // This is the canvas we used to draw texts.
     private BufferedImage mBufferedImage;
 
@@ -322,12 +326,13 @@
      * directory and collect the translated text.
      *
      * @param resourcePath the path to the resource directory
+     * @param localesSet a list of supported locales; resources of other locales will be omitted.
      * @return a map with the locale as key, and translated text as value
      * @throws LocalizedStringNotFoundException if we cannot find the translated text for the given
      *     locale
      */
-    public Map<Locale, String> readLocalizedStringFromXmls(String resourcePath)
-            throws IOException, LocalizedStringNotFoundException {
+    public Map<Locale, String> readLocalizedStringFromXmls(String resourcePath,
+            Set<String> localesSet) throws IOException, LocalizedStringNotFoundException {
         File resourceDir = new File(resourcePath);
         if (!resourceDir.isDirectory()) {
             throw new LocalizedStringNotFoundException(resourcePath + " is not a directory.");
@@ -354,6 +359,12 @@
         String[] nameList =
                 resourceDir.list((File file, String name) -> name.startsWith("values-"));
         for (String name : nameList) {
+            String localeString = name.substring(7);
+            if (localesSet != null && !localesSet.contains(localeString)) {
+                LOGGER.info("Skip parsing text for locale " + localeString);
+                continue;
+            }
+
             File textFile = new File(resourcePath, name + "/strings.xml");
             String localizedText;
             try {
@@ -536,7 +547,7 @@
      */
     private void drawText(String text, Locale locale, String languageTag)
             throws IOException, FontFormatException {
-        System.out.println("Encoding \"" + locale + "\" as \"" + languageTag + "\": " + text);
+        LOGGER.info("Encoding \"" + locale + "\" as \"" + languageTag + "\": " + text);
 
         Graphics2D graphics = createGraphics(locale);
         FontMetrics fontMetrics = graphics.getFontMetrics();
@@ -697,6 +708,19 @@
                         .hasArg(false)
                         .create());
 
+        options.addOption(
+                OptionBuilder.withLongOpt("verbose")
+                        .withDescription("Output the logging above info level.")
+                        .hasArg(false)
+                        .create());
+
+        options.addOption(
+                OptionBuilder.withLongOpt("locales")
+                        .withDescription("A list of android locales separated by ',' e.g."
+                                + " 'af,en,zh-rTW'")
+                        .hasArg(true)
+                        .create());
+
         return options;
     }
 
@@ -716,6 +740,12 @@
 
         int imageWidth = Integer.parseUnsignedInt(cmd.getOptionValue("image_width"));
 
+        if (cmd.hasOption("verbose")) {
+            LOGGER.setLevel(Level.INFO);
+        } else {
+            LOGGER.setLevel(Level.WARNING);
+        }
+
         ImageGenerator imageGenerator =
                 new ImageGenerator(
                         imageWidth,
@@ -724,8 +754,16 @@
                         cmd.getOptionValue("font_dir"),
                         cmd.hasOption("center_alignment"));
 
+        Set<String> localesSet = null;
+        if (cmd.hasOption("locales")) {
+            String[] localesList = cmd.getOptionValue("locales").split(",");
+            localesSet = new HashSet<>(Arrays.asList(localesList));
+            // Ensures that we have the default locale, all english translations are identical.
+            localesSet.add("en-rAU");
+        }
         Map<Locale, String> localizedStringMap =
-                imageGenerator.readLocalizedStringFromXmls(cmd.getOptionValue("resource_dir"));
+                imageGenerator.readLocalizedStringFromXmls(cmd.getOptionValue("resource_dir"),
+                        localesSet);
         imageGenerator.generateImage(localizedStringMap, cmd.getOptionValue("output_file"));
     }
 }