Skip FontFamily creation if the font files don't exists.

Bug: 186580197
Test: atest FontFamilyUpdateRequestTest FontListParserTest
      FontManagerTest NativeSystemFontTest
      PersistentSystemFontConfigTest SystemFontsTest
      SystemFontsUniqueNameTest UpdatableFontDirTest
      UpdatableSystemFontTest
Change-Id: Ibbcfb9ed43d05dd9db0132f3a7cb0321243be054
This commit is contained in:
Seigo Nonaka
2021-04-28 17:02:54 -07:00
parent a3a8f663dd
commit 2a77167ce4
4 changed files with 34 additions and 13 deletions

View File

@@ -325,6 +325,6 @@ public final class FontListParserTest {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(buffer, "UTF-8");
parser.nextTag();
return FontListParser.readFamily(parser, "", null);
return FontListParser.readFamily(parser, "", null, true);
}
}

View File

@@ -74,7 +74,7 @@ public class FontListParser {
parser.setInput(in, null);
parser.nextTag();
return readFamilies(parser, "/system/fonts/", new FontCustomizationParser.Result(), null,
0, 0);
0, 0, true);
}
/**
@@ -116,7 +116,7 @@ public class FontListParser {
parser.setInput(is, null);
parser.nextTag();
return readFamilies(parser, systemFontDir, oemCustomization, updatableFontMap,
lastModifiedDate, configVersion);
lastModifiedDate, configVersion, false /* filter out the non-exising files */);
}
}
@@ -126,7 +126,8 @@ public class FontListParser {
@NonNull FontCustomizationParser.Result customization,
@Nullable Map<String, File> updatableFontMap,
long lastModifiedDate,
int configVersion)
int configVersion,
boolean allowNonExistingFile)
throws XmlPullParserException, IOException {
List<FontConfig.FontFamily> families = new ArrayList<>();
List<FontConfig.Alias> aliases = new ArrayList<>(customization.getAdditionalAliases());
@@ -139,7 +140,11 @@ public class FontListParser {
if (parser.getEventType() != XmlPullParser.START_TAG) continue;
String tag = parser.getName();
if (tag.equals("family")) {
FontConfig.FontFamily family = readFamily(parser, fontDir, updatableFontMap);
FontConfig.FontFamily family = readFamily(parser, fontDir, updatableFontMap,
allowNonExistingFile);
if (family == null) {
continue;
}
String name = family.getName();
if (name == null || !oemNamedFamilies.containsKey(name)) {
// The OEM customization overrides system named family. Skip if OEM
@@ -165,9 +170,15 @@ public class FontListParser {
/**
* Read family tag in fonts.xml or oem_customization.xml
*
* @param parser An XML parser.
* @param fontDir a font directory name.
* @param updatableFontMap a updated font file map.
* @param allowNonExistingFile true to allow font file that doesn't exists
* @return a FontFamily instance. null if no font files are available in this FontFamily.
*/
public static FontConfig.FontFamily readFamily(XmlPullParser parser, String fontDir,
@Nullable Map<String, File> updatableFontMap)
public static @Nullable FontConfig.FontFamily readFamily(XmlPullParser parser, String fontDir,
@Nullable Map<String, File> updatableFontMap, boolean allowNonExistingFile)
throws XmlPullParserException, IOException {
final String name = parser.getAttributeValue(null, "name");
final String lang = parser.getAttributeValue("", "lang");
@@ -177,7 +188,11 @@ public class FontListParser {
if (parser.getEventType() != XmlPullParser.START_TAG) continue;
final String tag = parser.getName();
if (tag.equals(TAG_FONT)) {
fonts.add(readFont(parser, fontDir, updatableFontMap));
FontConfig.Font font = readFont(parser, fontDir, updatableFontMap,
allowNonExistingFile);
if (font != null) {
fonts.add(font);
}
} else {
skip(parser);
}
@@ -190,6 +205,9 @@ public class FontListParser {
intVariant = FontConfig.FontFamily.VARIANT_ELEGANT;
}
}
if (fonts.isEmpty()) {
return null;
}
return new FontConfig.FontFamily(fonts, name, LocaleList.forLanguageTags(lang), intVariant);
}
@@ -197,10 +215,11 @@ public class FontListParser {
private static final Pattern FILENAME_WHITESPACE_PATTERN =
Pattern.compile("^[ \\n\\r\\t]+|[ \\n\\r\\t]+$");
private static FontConfig.Font readFont(
private static @Nullable FontConfig.Font readFont(
@NonNull XmlPullParser parser,
@NonNull String fontDir,
@Nullable Map<String, File> updatableFontMap)
@Nullable Map<String, File> updatableFontMap,
boolean allowNonExistingFile)
throws XmlPullParserException, IOException {
String indexStr = parser.getAttributeValue(null, ATTR_INDEX);
@@ -253,7 +272,9 @@ public class FontListParser {
File file = new File(filePath);
if (!(allowNonExistingFile || file.isFile())) {
return null;
}
return new FontConfig.Font(file,
originalPath == null ? null : new File(originalPath),

View File

@@ -134,7 +134,7 @@ public class FontCustomizationParser {
throw new IllegalArgumentException("customizationType must be specified");
}
if (customizationType.equals("new-named-family")) {
out.add(FontListParser.readFamily(parser, fontDir, updatableFontMap));
out.add(FontListParser.readFamily(parser, fontDir, updatableFontMap, false));
} else {
throw new IllegalArgumentException("Unknown customizationType=" + customizationType);
}

View File

@@ -963,7 +963,7 @@ public final class UpdatableFontDirTest {
parser.setInput(is, "UTF-8");
parser.nextTag();
FontConfig.FontFamily fontFamily = FontListParser.readFamily(parser, "", null);
FontConfig.FontFamily fontFamily = FontListParser.readFamily(parser, "", null, true);
List<FontUpdateRequest.Font> fonts = new ArrayList<>();
for (FontConfig.Font font : fontFamily.getFontList()) {
String name = font.getFile().getName();