Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5be14aba authored by Jin Dong's avatar Jin Dong Committed by Fan Zhang
Browse files

Add vendor notice header at the top of Third-party licenses

Vendor can add notice header at the top of "Third-party licenses"
by overlay the string notice_header.

Bug: 116298367
Test: robotest
Change-Id: If80d69180664970441d0addccd81d65d7ab55c3b
parent 58131839
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -1116,4 +1116,6 @@
    <!-- time label for event have that happened very recently [CHAR LIMIT=60] -->
    <string name="time_unit_just_now">Just now</string>

    <!-- The notice header of Third-party licenses. not translatable -->
    <string name="notice_header" translatable="false"></string>
</resources>
+13 −6
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ import java.util.zip.GZIPInputStream;
 * TODO: Remove duplicate codes once backward support ends.
 */
class LicenseHtmlGeneratorFromXml {
    private static final String TAG = "LicenseHtmlGeneratorFromXml";
    private static final String TAG = "LicenseGeneratorFromXml";

    private static final String TAG_ROOT = "licenses";
    private static final String TAG_FILE_NAME = "file-name";
@@ -107,12 +107,13 @@ class LicenseHtmlGeneratorFromXml {
        mXmlFiles = xmlFiles;
    }

    public static boolean generateHtml(List<File> xmlFiles, File outputFile) {
    public static boolean generateHtml(List<File> xmlFiles, File outputFile,
            String noticeHeader) {
        LicenseHtmlGeneratorFromXml genertor = new LicenseHtmlGeneratorFromXml(xmlFiles);
        return genertor.generateHtml(outputFile);
        return genertor.generateHtml(outputFile, noticeHeader);
    }

    private boolean generateHtml(File outputFile) {
    private boolean generateHtml(File outputFile, String noticeHeader) {
        for (File xmlFile : mXmlFiles) {
            parse(xmlFile);
        }
@@ -125,7 +126,8 @@ class LicenseHtmlGeneratorFromXml {
        try {
            writer = new PrintWriter(outputFile);

            generateHtml(mFileNameToContentIdMap, mContentIdToFileContentMap, writer);
            generateHtml(mFileNameToContentIdMap, mContentIdToFileContentMap, writer,
                noticeHeader);

            writer.flush();
            writer.close();
@@ -239,13 +241,18 @@ class LicenseHtmlGeneratorFromXml {

    @VisibleForTesting
    static void generateHtml(Map<String, String> fileNameToContentIdMap,
            Map<String, String> contentIdToFileContentMap, PrintWriter writer) {
            Map<String, String> contentIdToFileContentMap, PrintWriter writer,
            String noticeHeader) {
        List<String> fileNameList = new ArrayList();
        fileNameList.addAll(fileNameToContentIdMap.keySet());
        Collections.sort(fileNameList);

        writer.println(HTML_HEAD_STRING);

        if (!TextUtils.isEmpty(noticeHeader)) {
            writer.println(noticeHeader);
        }

        int count = 0;
        Map<String, Integer> contentIdToOrderMap = new HashMap();
        List<ContentIdAndFileNames> contentIdAndFileNamesList = new ArrayList();
+1 −1
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ public class LicenseHtmlLoader extends AsyncLoader<File> {

        File cachedHtmlFile = getCachedHtmlFile(mContext);
        if (!isCachedHtmlFileOutdated(xmlFiles, cachedHtmlFile)
                || generateHtmlFile(xmlFiles, cachedHtmlFile)) {
                || generateHtmlFile(mContext, xmlFiles, cachedHtmlFile)) {
            return cachedHtmlFile;
        }

+5 −3
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.settingslib.license;
import android.content.Context;
import android.util.Log;

import com.android.settingslib.R;
import com.android.settingslib.utils.AsyncLoaderCompat;

import java.io.File;
@@ -65,7 +66,7 @@ public class LicenseHtmlLoaderCompat extends AsyncLoaderCompat<File> {

        File cachedHtmlFile = getCachedHtmlFile(mContext);
        if (!isCachedHtmlFileOutdated(xmlFiles, cachedHtmlFile)
                || generateHtmlFile(xmlFiles, cachedHtmlFile)) {
                || generateHtmlFile(mContext, xmlFiles, cachedHtmlFile)) {
            return cachedHtmlFile;
        }

@@ -101,7 +102,8 @@ public class LicenseHtmlLoaderCompat extends AsyncLoaderCompat<File> {
        return outdated;
    }

    static boolean generateHtmlFile(List<File> xmlFiles, File htmlFile) {
        return LicenseHtmlGeneratorFromXml.generateHtml(xmlFiles, htmlFile);
    static boolean generateHtmlFile(Context context, List<File> xmlFiles, File htmlFile) {
        return LicenseHtmlGeneratorFromXml.generateHtml(xmlFiles, htmlFile,
                context.getString(R.string.notice_header));
    }
}
+29 −4
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ public class LicenseHtmlGeneratorFromXmlTest {
            + "<file-content contentId=\"0\"><![CDATA[license content #0]]></file-content>\n"
            + "</licenses2>";

    private static final String EXPECTED_HTML_STRING =
    private static final String HTML_HEAD_STRING =
            "<html><head>\n"
            + "<style type=\"text/css\">\n"
            + "body { padding: 0; font-family: sans-serif; }\n"
@@ -63,8 +63,12 @@ public class LicenseHtmlGeneratorFromXmlTest {
            + "</head>"
            + "<body topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\">\n"
            + "<div class=\"toc\">\n"
            + "<ul>\n"
            + "<li><a href=\"#id0\">/file0</a></li>\n"
            + "<ul>\n";

    private static final String HTML_CUSTOM_HEADING = "Custom heading";

    private static final String HTML_BODY_STRING =
            "<li><a href=\"#id0\">/file0</a></li>\n"
            + "<li><a href=\"#id0\">/file1</a></li>\n"
            + "</ul>\n"
            + "</div><!-- table of contents -->\n"
@@ -81,6 +85,11 @@ public class LicenseHtmlGeneratorFromXmlTest {
            + "</td></tr><!-- same-license -->\n"
            + "</table></body></html>\n";

    private static final String EXPECTED_HTML_STRING = HTML_HEAD_STRING + HTML_BODY_STRING;

    private static final String EXPECTED_HTML_STRING_WITH_CUSTOM_HEADING =
            HTML_HEAD_STRING + HTML_CUSTOM_HEADING + "\n" + HTML_BODY_STRING;

    @Test
    public void testParseValidXmlStream() throws XmlPullParserException, IOException {
        Map<String, String> fileNameToContentIdMap = new HashMap<String, String>();
@@ -117,7 +126,23 @@ public class LicenseHtmlGeneratorFromXmlTest {

        StringWriter output = new StringWriter();
        LicenseHtmlGeneratorFromXml.generateHtml(
                fileNameToContentIdMap, contentIdToFileContentMap, new PrintWriter(output));
                fileNameToContentIdMap, contentIdToFileContentMap, new PrintWriter(output), "");
        assertThat(output.toString()).isEqualTo(EXPECTED_HTML_STRING);
    }

    @Test
    public void testGenerateHtmlWithCustomHeading() {
        Map<String, String> fileNameToContentIdMap = new HashMap<String, String>();
        Map<String, String> contentIdToFileContentMap = new HashMap<String, String>();

        fileNameToContentIdMap.put("/file0", "0");
        fileNameToContentIdMap.put("/file1", "0");
        contentIdToFileContentMap.put("0", "license content #0");

        StringWriter output = new StringWriter();
        LicenseHtmlGeneratorFromXml.generateHtml(
                fileNameToContentIdMap, contentIdToFileContentMap, new PrintWriter(output),
                HTML_CUSTOM_HEADING);
        assertThat(output.toString()).isEqualTo(EXPECTED_HTML_STRING_WITH_CUSTOM_HEADING);
    }
}
Loading