From 07abc181a878754032253a0befcd1e7a289114b6 Mon Sep 17 00:00:00 2001 From: Bob Badour Date: Mon, 27 Jan 2020 14:10:31 -0800 Subject: [PATCH] Show all licenses when multiple. Unreadable partitions like recovery have their notices copied to /system/etc/NOTICE.xml.gz or to /vendor/etc/NOTICE.xml.gz or to both. If the notice requirements for a library change between building a vendor blob and building a sytem image using that blob, the UI hides one of the licenses. This makes the build system harder to understand for developers and could cause other hassles. Test: Manually using 2 devices. Test: atest Change-Id: I018d2a1949d11b201e4eeb23f186be8d244e161d --- .../license/LicenseHtmlGeneratorFromXml.java | 46 ++++++++++++------- .../LicenseHtmlGeneratorFromXmlTest.java | 35 ++++++++++---- 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXml.java b/packages/SettingsLib/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXml.java index 9db4a35c1d785..b4c95e6ee2dfa 100644 --- a/packages/SettingsLib/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXml.java +++ b/packages/SettingsLib/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXml.java @@ -35,8 +35,10 @@ import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.zip.GZIPInputStream; /** @@ -84,7 +86,7 @@ class LicenseHtmlGeneratorFromXml { * "9645f39e9db895a4aa6e02cb57294595". Here "9645f39e9db895a4aa6e02cb57294595" is a MD5 sum * of the content of packages/services/Telephony/MODULE_LICENSE_APACHE2. */ - private final Map mFileNameToContentIdMap = new HashMap(); + private final Map> mFileNameToContentIdMap = new HashMap(); /* * A map from a content id (MD5 sum of file content) to a license file content. @@ -186,10 +188,10 @@ class LicenseHtmlGeneratorFromXml { * */ @VisibleForTesting - static void parse(InputStreamReader in, Map outFileNameToContentIdMap, + static void parse(InputStreamReader in, Map> outFileNameToContentIdMap, Map outContentIdToFileContentMap) throws XmlPullParserException, IOException { - Map fileNameToContentIdMap = new HashMap(); + Map> fileNameToContentIdMap = new HashMap>(); Map contentIdToFileContentMap = new HashMap(); XmlPullParser parser = Xml.newPullParser(); @@ -206,7 +208,10 @@ class LicenseHtmlGeneratorFromXml { if (!TextUtils.isEmpty(contentId)) { String fileName = readText(parser).trim(); if (!TextUtils.isEmpty(fileName)) { - fileNameToContentIdMap.put(fileName, contentId); + Set contentIds = + fileNameToContentIdMap.computeIfAbsent( + fileName, k -> new HashSet<>()); + contentIds.add(contentId); } } } else if (TAG_FILE_CONTENT.equals(parser.getName())) { @@ -224,7 +229,13 @@ class LicenseHtmlGeneratorFromXml { state = parser.next(); } - outFileNameToContentIdMap.putAll(fileNameToContentIdMap); + for (Map.Entry> entry : fileNameToContentIdMap.entrySet()) { + outFileNameToContentIdMap.merge( + entry.getKey(), entry.getValue(), (s1, s2) -> { + s1.addAll(s2); + return s1; + }); + } outContentIdToFileContentMap.putAll(contentIdToFileContentMap); } @@ -240,7 +251,7 @@ class LicenseHtmlGeneratorFromXml { } @VisibleForTesting - static void generateHtml(Map fileNameToContentIdMap, + static void generateHtml(Map> fileNameToContentIdMap, Map contentIdToFileContentMap, PrintWriter writer, String noticeHeader) { List fileNameList = new ArrayList(); @@ -259,19 +270,20 @@ class LicenseHtmlGeneratorFromXml { // Prints all the file list with a link to its license file content. for (String fileName : fileNameList) { - String contentId = fileNameToContentIdMap.get(fileName); - // Assigns an id to a newly referred license file content. - if (!contentIdToOrderMap.containsKey(contentId)) { - contentIdToOrderMap.put(contentId, count); + for (String contentId : fileNameToContentIdMap.get(fileName)) { + // Assigns an id to a newly referred license file content. + if (!contentIdToOrderMap.containsKey(contentId)) { + contentIdToOrderMap.put(contentId, count); - // An index in contentIdAndFileNamesList is the order of each element. - contentIdAndFileNamesList.add(new ContentIdAndFileNames(contentId)); - count++; + // An index in contentIdAndFileNamesList is the order of each element. + contentIdAndFileNamesList.add(new ContentIdAndFileNames(contentId)); + count++; + } + + int id = contentIdToOrderMap.get(contentId); + contentIdAndFileNamesList.get(id).mFileNameList.add(fileName); + writer.format("
  • %s
  • \n", id, fileName); } - - int id = contentIdToOrderMap.get(contentId); - contentIdAndFileNamesList.get(id).mFileNameList.add(fileName); - writer.format("
  • %s
  • \n", id, fileName); } writer.println(HTML_MIDDLE_STRING); diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXmlTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXmlTest.java index 4b5e9097b3fe4..e87461f85762f 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXmlTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXmlTest.java @@ -28,8 +28,11 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; +import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; @RunWith(RobolectricTestRunner.class) public class LicenseHtmlGeneratorFromXmlTest { @@ -68,6 +71,7 @@ public class LicenseHtmlGeneratorFromXmlTest { private static final String HTML_BODY_STRING = "
  • /file0
  • \n" + + "
  • /file0
  • \n" + "
  • /file1
  • \n" + "\n" + "\n" @@ -82,6 +86,15 @@ public class LicenseHtmlGeneratorFromXmlTest { + "license content #0\n" + "\n" + "\n" + + "\n" + + "
    Notices for file(s):
    \n" + + "
    \n" + + "/file0
    \n" + + "
    \n" + + "
    \n"
    +            + "license content #1\n"
    +            + "
    \n" + + "\n" + "\n"; private static final String EXPECTED_HTML_STRING = HTML_HEAD_STRING + HTML_BODY_STRING; @@ -91,22 +104,22 @@ public class LicenseHtmlGeneratorFromXmlTest { @Test public void testParseValidXmlStream() throws XmlPullParserException, IOException { - Map fileNameToContentIdMap = new HashMap<>(); + Map> fileNameToContentIdMap = new HashMap<>(); Map contentIdToFileContentMap = new HashMap<>(); LicenseHtmlGeneratorFromXml.parse( new InputStreamReader(new ByteArrayInputStream(VALILD_XML_STRING.getBytes())), fileNameToContentIdMap, contentIdToFileContentMap); assertThat(fileNameToContentIdMap.size()).isEqualTo(2); - assertThat(fileNameToContentIdMap.get("/file0")).isEqualTo("0"); - assertThat(fileNameToContentIdMap.get("/file1")).isEqualTo("0"); + assertThat(fileNameToContentIdMap.get("/file0")).containsExactly("0"); + assertThat(fileNameToContentIdMap.get("/file1")).containsExactly("0"); assertThat(contentIdToFileContentMap.size()).isEqualTo(1); assertThat(contentIdToFileContentMap.get("0")).isEqualTo("license content #0"); } @Test(expected = XmlPullParserException.class) public void testParseInvalidXmlStream() throws XmlPullParserException, IOException { - Map fileNameToContentIdMap = new HashMap<>(); + Map> fileNameToContentIdMap = new HashMap<>(); Map contentIdToFileContentMap = new HashMap<>(); LicenseHtmlGeneratorFromXml.parse( @@ -116,12 +129,13 @@ public class LicenseHtmlGeneratorFromXmlTest { @Test public void testGenerateHtml() { - Map fileNameToContentIdMap = new HashMap<>(); + Map> fileNameToContentIdMap = new HashMap<>(); Map contentIdToFileContentMap = new HashMap<>(); - fileNameToContentIdMap.put("/file0", "0"); - fileNameToContentIdMap.put("/file1", "0"); + fileNameToContentIdMap.put("/file0", new HashSet(Arrays.asList("0", "1"))); + fileNameToContentIdMap.put("/file1", new HashSet(Arrays.asList("0"))); contentIdToFileContentMap.put("0", "license content #0"); + contentIdToFileContentMap.put("1", "license content #1"); StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( @@ -131,12 +145,13 @@ public class LicenseHtmlGeneratorFromXmlTest { @Test public void testGenerateHtmlWithCustomHeading() { - Map fileNameToContentIdMap = new HashMap<>(); + Map> fileNameToContentIdMap = new HashMap<>(); Map contentIdToFileContentMap = new HashMap<>(); - fileNameToContentIdMap.put("/file0", "0"); - fileNameToContentIdMap.put("/file1", "0"); + fileNameToContentIdMap.put("/file0", new HashSet(Arrays.asList("0", "1"))); + fileNameToContentIdMap.put("/file1", new HashSet(Arrays.asList("0"))); contentIdToFileContentMap.put("0", "license content #0"); + contentIdToFileContentMap.put("1", "license content #1"); StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml(