diff options
| author | 2021-01-29 20:33:39 +0000 | |
|---|---|---|
| committer | 2021-01-29 20:33:39 +0000 | |
| commit | 05921e00c8cec837988be0049beff39e488a8127 (patch) | |
| tree | d843f5a3e5d5e8e04db71a3943237db08b40cf2d | |
| parent | f32842575b85b5a81b6347b619375c48042a2931 (diff) | |
| parent | 30900cac3f8d11171b164291ccd4bf7e93a0d98b (diff) | |
Merge "System font check for downloadable font" into sc-dev
| -rw-r--r-- | core/api/current.txt | 1 | ||||
| -rw-r--r-- | core/java/android/content/res/FontResourcesParser.java | 19 | ||||
| -rw-r--r-- | core/res/res/values/attrs.xml | 6 | ||||
| -rw-r--r-- | core/res/res/values/public.xml | 1 | ||||
| -rw-r--r-- | core/tests/coretests/res/font/samplexmldownloadedfont.xml | 3 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/content/res/FontResourcesParserTest.java | 1 | ||||
| -rw-r--r-- | graphics/java/android/graphics/Typeface.java | 21 |
7 files changed, 49 insertions, 3 deletions
diff --git a/core/api/current.txt b/core/api/current.txt index 3dd1f598f94f..b49e7247708c 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -657,6 +657,7 @@ package android { field @Deprecated public static final int fontProviderCerts = 16844125; // 0x101055d field @Deprecated public static final int fontProviderPackage = 16844119; // 0x1010557 field @Deprecated public static final int fontProviderQuery = 16844113; // 0x1010551 + field public static final int fontProviderSystemFontFamily = 16844322; // 0x1010622 field public static final int fontStyle = 16844095; // 0x101053f field public static final int fontVariationSettings = 16844144; // 0x1010570 field public static final int fontWeight = 16844083; // 0x1010533 diff --git a/core/java/android/content/res/FontResourcesParser.java b/core/java/android/content/res/FontResourcesParser.java index 14eb11ab8863..ecd240d9e4dc 100644 --- a/core/java/android/content/res/FontResourcesParser.java +++ b/core/java/android/content/res/FontResourcesParser.java @@ -47,14 +47,17 @@ public class FontResourcesParser { private final @NonNull String mProviderAuthority; private final @NonNull String mProviderPackage; private final @NonNull String mQuery; + private final @Nullable String mSystemFontFamilyName; private final @Nullable List<List<String>> mCerts; public ProviderResourceEntry(@NonNull String authority, @NonNull String pkg, - @NonNull String query, @Nullable List<List<String>> certs) { + @NonNull String query, @Nullable List<List<String>> certs, + @Nullable String systemFontFamilyName) { mProviderAuthority = authority; mProviderPackage = pkg; mQuery = query; mCerts = certs; + mSystemFontFamilyName = systemFontFamilyName; } public @NonNull String getAuthority() { @@ -69,6 +72,10 @@ public class FontResourcesParser { return mQuery; } + public @NonNull String getSystemFontFamilyName() { + return mSystemFontFamilyName; + } + public @Nullable List<List<String>> getCerts() { return mCerts; } @@ -166,6 +173,8 @@ public class FontResourcesParser { String providerPackage = array.getString(R.styleable.FontFamily_fontProviderPackage); String query = array.getString(R.styleable.FontFamily_fontProviderQuery); int certsId = array.getResourceId(R.styleable.FontFamily_fontProviderCerts, 0); + String systemFontFamilyName = array.getString( + R.styleable.FontFamily_fontProviderSystemFontFamily); array.recycle(); if (authority != null && providerPackage != null && query != null) { while (parser.next() != XmlPullParser.END_TAG) { @@ -191,7 +200,13 @@ public class FontResourcesParser { } } } - return new ProviderResourceEntry(authority, providerPackage, query, certs); + return new ProviderResourceEntry( + authority, + providerPackage, + query, + certs, + systemFontFamilyName + ); } List<FontFileResourceEntry> fonts = new ArrayList<>(); while (parser.next() != XmlPullParser.END_TAG) { diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml index 415a0a2ed595..14f1e0e20ef6 100644 --- a/core/res/res/values/attrs.xml +++ b/core/res/res/values/attrs.xml @@ -9276,6 +9276,12 @@ {@deprecated Use app:fontProviderCerts with Jetpack Core library instead.} --> <attr name="fontProviderCerts" format="reference" /> + <!-- Provides the system font family name to check before downloading the font. For example + if the fontProviderQuery asked for "Sans Serif", it is possible to define + fontProviderSystemFontFamily as "sans-serif" to tell the system to use "sans-serif" font + family if it exists on the system. + --> + <attr name="fontProviderSystemFontFamily" format="string" /> </declare-styleable> <!-- Attributes that are read when parsing a tag. --> diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml index ddf3c5f09e9e..9c1c51cdd48e 100644 --- a/core/res/res/values/public.xml +++ b/core/res/res/values/public.xml @@ -3058,6 +3058,7 @@ <public name="sspSuffix" /> <public name="pathAdvancedPattern" /> <public name="sspAdvancedPattern" /> + <public name="fontProviderSystemFontFamily" /> </public-group> <public-group type="drawable" first-id="0x010800b5"> diff --git a/core/tests/coretests/res/font/samplexmldownloadedfont.xml b/core/tests/coretests/res/font/samplexmldownloadedfont.xml index f1bdc473b9d2..9c32ffb0f4c9 100644 --- a/core/tests/coretests/res/font/samplexmldownloadedfont.xml +++ b/core/tests/coretests/res/font/samplexmldownloadedfont.xml @@ -2,5 +2,6 @@ <font-family xmlns:android="http://schemas.android.com/apk/res/android" android:fontProviderAuthority="com.example.test.fontprovider.authority" android:fontProviderPackage="com.example.test.fontprovider.package" - android:fontProviderQuery="MyRequestedFont"> + android:fontProviderQuery="MyRequestedFont" + android:fontProviderSystemFontFamily="my-request-font"> </font-family>
\ No newline at end of file diff --git a/core/tests/coretests/src/android/content/res/FontResourcesParserTest.java b/core/tests/coretests/src/android/content/res/FontResourcesParserTest.java index 7ab9d7f4ffe1..57f01e988440 100644 --- a/core/tests/coretests/src/android/content/res/FontResourcesParserTest.java +++ b/core/tests/coretests/src/android/content/res/FontResourcesParserTest.java @@ -105,6 +105,7 @@ public class FontResourcesParserTest { assertEquals("com.example.test.fontprovider.authority", providerEntry.getAuthority()); assertEquals("com.example.test.fontprovider.package", providerEntry.getPackage()); assertEquals("MyRequestedFont", providerEntry.getQuery()); + assertEquals("my-request-font", providerEntry.getSystemFontFamilyName()); } @Test diff --git a/graphics/java/android/graphics/Typeface.java b/graphics/java/android/graphics/Typeface.java index a0568bf3bf5c..005a72661091 100644 --- a/graphics/java/android/graphics/Typeface.java +++ b/graphics/java/android/graphics/Typeface.java @@ -70,6 +70,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; /** * The Typeface class specifies the typeface and intrinsic style of a font. @@ -249,6 +250,21 @@ public class Typeface { } /** + * Returns true if the system has the font family with the name [familyName]. For example + * querying with "sans-serif" would check if the "sans-serif" family is defined in the system + * and return true if does. + * + * @param familyName The name of the font family, cannot be null. If null, exception will be + * thrown. + */ + private static boolean hasFontFamily(@NonNull String familyName) { + Objects.requireNonNull(familyName, "familyName cannot be null"); + synchronized (SYSTEM_FONT_MAP_LOCK) { + return sSystemFontMap.containsKey(familyName); + } + } + + /** * @hide * Used by Resources to load a font resource of type xml. */ @@ -257,6 +273,11 @@ public class Typeface { FamilyResourceEntry entry, AssetManager mgr, String path) { if (entry instanceof ProviderResourceEntry) { final ProviderResourceEntry providerEntry = (ProviderResourceEntry) entry; + + String systemFontFamilyName = providerEntry.getSystemFontFamilyName(); + if (systemFontFamilyName != null && hasFontFamily(systemFontFamilyName)) { + return Typeface.create(systemFontFamilyName, NORMAL); + } // Downloadable font List<List<String>> givenCerts = providerEntry.getCerts(); List<List<byte[]>> certs = new ArrayList<>(); |