Merge "System font check for downloadable font" into sc-dev

This commit is contained in:
TreeHugger Robot
2021-01-29 20:33:39 +00:00
committed by Android (Google) Code Review
7 changed files with 49 additions and 3 deletions

View File

@@ -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

View File

@@ -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) {

View File

@@ -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. -->

View File

@@ -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">

View File

@@ -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>

View File

@@ -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

View File

@@ -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.
@@ -248,6 +249,21 @@ public class Typeface {
return (mStyle & ITALIC) != 0;
}
/**
* 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<>();