summaryrefslogtreecommitdiffstats
path: root/graphics
diff options
context:
space:
mode:
authord34d <clark@cyngn.com>2015-05-05 13:47:01 -0700
committerClark Scheff <clark@cyngn.com>2015-10-27 18:16:30 -0700
commitb3ae4609f2754fd156e34dfbf39551041e976031 (patch)
tree24d585d9534dd2e1b83e4cf02fd361fd53ded8e6 /graphics
parent455c8e23ce637f5973a9f9b8918e939bf909c6d1 (diff)
downloadframeworks_base-b3ae4609f2754fd156e34dfbf39551041e976031.zip
frameworks_base-b3ae4609f2754fd156e34dfbf39551041e976031.tar.gz
frameworks_base-b3ae4609f2754fd156e34dfbf39551041e976031.tar.bz2
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
Diffstat (limited to 'graphics')
-rw-r--r--graphics/java/android/graphics/FontListParser.java62
-rw-r--r--graphics/java/android/graphics/Typeface.java11
2 files changed, 47 insertions, 26 deletions
diff --git a/graphics/java/android/graphics/FontListParser.java b/graphics/java/android/graphics/FontListParser.java
index 1ca464d..d789690 100644
--- a/graphics/java/android/graphics/FontListParser.java
+++ b/graphics/java/android/graphics/FontListParser.java
@@ -21,6 +21,7 @@ import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
+import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -76,30 +77,53 @@ public class FontListParser {
}
/* Parse fallback list (no names) */
- public static Config parse(File configFilename, File fontDir)
+ public static Config parse(File configFilename, String fontDir)
throws XmlPullParserException, IOException {
- FileInputStream in = new FileInputStream(configFilename);
- if (isLegacyFormat(configFilename)) {
- return parseLegacyFormat(in, fontDir.getAbsolutePath());
- } else {
- return parseNormalFormat(in, fontDir.getAbsolutePath());
- }
+ FileInputStream in = null;
+ in = new FileInputStream(configFilename);
+ return FontListParser.parse(in, fontDir);
}
- private static boolean isLegacyFormat(File configFilename)
+ /* Parse fallback list (no names) */
+ public static Config parse(InputStream in, String fontDir)
throws XmlPullParserException, IOException {
- FileInputStream in = new FileInputStream(configFilename);
- boolean isLegacy = false;
+ BufferedInputStream bis = null;
try {
- XmlPullParser parser = Xml.newPullParser();
- parser.setInput(in, null);
- parser.nextTag();
- parser.require(XmlPullParser.START_TAG, null, "familyset");
- String version = parser.getAttributeValue(null, "version");
- isLegacy = version == null;
+ // wrap input stream in a BufferedInputStream, if it's not already, for mark support
+ if (!(in instanceof BufferedInputStream)) {
+ bis = new BufferedInputStream(in);
+ } else {
+ 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 {
- in.close();
+ if (bis != null) bis.close();
}
+ }
+
+ public static boolean isLegacyFormat(InputStream in)
+ throws XmlPullParserException, IOException {
+ if (!in.markSupported()) {
+ throw new IllegalArgumentException("InputStream does not support mark");
+ }
+ boolean isLegacy = false;
+
+ XmlPullParser parser = Xml.newPullParser();
+ parser.setInput(in, null);
+ parser.nextTag();
+ parser.require(XmlPullParser.START_TAG, null, "familyset");
+ String version = parser.getAttributeValue(null, "version");
+ isLegacy = version == null;
+
+ // reset the stream so we can read it
+ in.reset();
+
return isLegacy;
}
@@ -116,14 +140,10 @@ public class FontListParser {
public static Config parseNormalFormat(InputStream in, String dirName)
throws XmlPullParserException, IOException {
- try {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, null);
parser.nextTag();
return readFamilies(parser, dirName);
- } finally {
- in.close();
- }
}
private static Config readFamilies(XmlPullParser parser, String dirPath)
diff --git a/graphics/java/android/graphics/Typeface.java b/graphics/java/android/graphics/Typeface.java
index 68c9c0b..a64c0af 100644
--- a/graphics/java/android/graphics/Typeface.java
+++ b/graphics/java/android/graphics/Typeface.java
@@ -369,7 +369,8 @@ public class Typeface {
}
try {
- FontListParser.Config fontConfig = FontListParser.parse(configFile, fontDir);
+ FontListParser.Config fontConfig = FontListParser.parse(configFile,
+ fontDir.getAbsolutePath());
FontListParser.Config systemFontConfig = null;
// 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.
if (configFile == themeConfigFile) {
systemFontConfig = FontListParser.parse(systemConfigFile,
- getSystemFontDirLocation());
+ getSystemFontDirLocation().getAbsolutePath());
addMissingFontFamilies(systemFontConfig, fontConfig);
addMissingFontAliases(systemFontConfig, fontConfig);
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);
// TODO: normal in non-Minikin case, remove or make error when Minikin-only
} catch (FileNotFoundException e) {
- Log.e(TAG, "Error opening " + configFile);
+ Log.e(TAG, "Error opening " + configFile, e);
} catch (IOException e) {
- Log.e(TAG, "Error reading " + configFile);
+ Log.e(TAG, "Error reading " + configFile, e);
} catch (XmlPullParserException e) {
- Log.e(TAG, "XML parse exception for " + configFile);
+ Log.e(TAG, "XML parse exception for " + configFile, e);
}
}