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

Commit 0d9fa435 authored by d34d's avatar d34d Committed by Clark Scheff
Browse files

Themes: Make parse() method in FontListParser public

This allows other apps, such as the Theme Chooser, to use the parser
without needing to duplicate code.  Methods were generalized to use
input streams that support marking so that the stream can be reset
after checking if the xml is in the legacy format.

Change-Id: Id77adabba836267e3dfcdd536b2859d3faa7d78d
REF: CHOOSER-83
parent 8536e710
Loading
Loading
Loading
Loading
+41 −21
Original line number Original line Diff line number Diff line
@@ -21,6 +21,7 @@ import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserException;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.IOException;
@@ -76,30 +77,53 @@ public class FontListParser {
    }
    }


    /* Parse fallback list (no names) */
    /* Parse fallback list (no names) */
    public static Config parse(File configFilename, File fontDir)
    public static Config parse(File configFilename, String fontDir)
            throws XmlPullParserException, IOException {
            throws XmlPullParserException, IOException {
        FileInputStream in = new FileInputStream(configFilename);
        FileInputStream in = null;
        if (isLegacyFormat(configFilename)) {
        in = new FileInputStream(configFilename);
            return parseLegacyFormat(in, fontDir.getAbsolutePath());
        return FontListParser.parse(in, fontDir);
    }

    /* Parse fallback list (no names) */
    public static Config parse(InputStream in, String fontDir)
            throws XmlPullParserException, IOException {
        BufferedInputStream bis = null;
        try {
            // wrap input stream in a BufferedInputStream, if it's not already, for mark support
            if (!(in instanceof BufferedInputStream)) {
                bis = new BufferedInputStream(in);
            } else {
            } else {
            return parseNormalFormat(in, fontDir.getAbsolutePath());
                bis = (BufferedInputStream) in;
            }
            // mark the beginning so we can reset to this position after checking format
            bis.mark(in.available());
            if (isLegacyFormat(bis)) {
                return parseLegacyFormat(bis, fontDir);
            } else {
                return parseNormalFormat(bis, fontDir);
            }
        } finally {
            if (bis != null) bis.close();
        }
        }
    }
    }


    private static boolean isLegacyFormat(File configFilename)
    public static boolean isLegacyFormat(InputStream in)
            throws XmlPullParserException, IOException {
            throws XmlPullParserException, IOException {
        FileInputStream in = new FileInputStream(configFilename);
        if (!in.markSupported()) {
            throw new IllegalArgumentException("InputStream does not support mark");
        }
        boolean isLegacy = false;
        boolean isLegacy = false;
        try {

        XmlPullParser parser = Xml.newPullParser();
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, null);
        parser.setInput(in, null);
        parser.nextTag();
        parser.nextTag();
        parser.require(XmlPullParser.START_TAG, null, "familyset");
        parser.require(XmlPullParser.START_TAG, null, "familyset");
        String version = parser.getAttributeValue(null, "version");
        String version = parser.getAttributeValue(null, "version");
        isLegacy = version == null;
        isLegacy = version == null;
        } finally {

            in.close();
        // reset the stream so we can read it
        }
        in.reset();

        return isLegacy;
        return isLegacy;
    }
    }


@@ -116,14 +140,10 @@ public class FontListParser {


    public static Config parseNormalFormat(InputStream in, String dirName)
    public static Config parseNormalFormat(InputStream in, String dirName)
            throws XmlPullParserException, IOException {
            throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(in, null);
            parser.setInput(in, null);
            parser.nextTag();
            parser.nextTag();
            return readFamilies(parser, dirName);
            return readFamilies(parser, dirName);
        } finally {
            in.close();
        }
    }
    }


    private static Config readFamilies(XmlPullParser parser, String dirPath)
    private static Config readFamilies(XmlPullParser parser, String dirPath)
+6 −5
Original line number Original line Diff line number Diff line
@@ -369,7 +369,8 @@ public class Typeface {
        }
        }


        try {
        try {
            FontListParser.Config fontConfig = FontListParser.parse(configFile, fontDir);
            FontListParser.Config fontConfig = FontListParser.parse(configFile,
                    fontDir.getAbsolutePath());
            FontListParser.Config systemFontConfig = null;
            FontListParser.Config systemFontConfig = null;


            // If the fonts are coming from a theme, we will need to make sure that we include
            // If the fonts are coming from a theme, we will need to make sure that we include
@@ -377,7 +378,7 @@ public class Typeface {
            // NOTE: All the system font families without names ALWAYS get added.
            // NOTE: All the system font families without names ALWAYS get added.
            if (configFile == themeConfigFile) {
            if (configFile == themeConfigFile) {
                systemFontConfig = FontListParser.parse(systemConfigFile,
                systemFontConfig = FontListParser.parse(systemConfigFile,
                        getSystemFontDirLocation());
                        getSystemFontDirLocation().getAbsolutePath());
                addMissingFontFamilies(systemFontConfig, fontConfig);
                addMissingFontFamilies(systemFontConfig, fontConfig);
                addMissingFontAliases(systemFontConfig, fontConfig);
                addMissingFontAliases(systemFontConfig, fontConfig);
                addFallbackFontsForFamilyName(systemFontConfig, fontConfig, SANS_SERIF_FAMILY_NAME);
                addFallbackFontsForFamilyName(systemFontConfig, fontConfig, SANS_SERIF_FAMILY_NAME);
@@ -428,11 +429,11 @@ public class Typeface {
            Log.w(TAG, "Didn't create default family (most likely, non-Minikin build)", e);
            Log.w(TAG, "Didn't create default family (most likely, non-Minikin build)", e);
            // TODO: normal in non-Minikin case, remove or make error when Minikin-only
            // TODO: normal in non-Minikin case, remove or make error when Minikin-only
        } catch (FileNotFoundException e) {
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Error opening " + configFile);
            Log.e(TAG, "Error opening " + configFile, e);
        } catch (IOException e) {
        } catch (IOException e) {
            Log.e(TAG, "Error reading " + configFile);
            Log.e(TAG, "Error reading " + configFile, e);
        } catch (XmlPullParserException e) {
        } catch (XmlPullParserException e) {
            Log.e(TAG, "XML parse exception for " + configFile);
            Log.e(TAG, "XML parse exception for " + configFile, e);
        }
        }
    }
    }