Revive the old behavior of unsupported filed in public APIs

The behavior of the Typeface creating function when unsuppoted font
files are passed has changed unexpectedly. This CL revives the old
behaviors. Here is the list of public APIs and expected behaviors.

Resources#getFont for unsupported font
  Exception: Resources#NotFoundException
Resources#getFont for unsupported font in XML
  Exception: Resources#NotFoundException
Resources#getFont for unsupported font from provider
  No Exception: Typeface.DEFAULT is returned

Typeface#Builder for unsupported font
  No Exception: null is returned
Typeface#Builder for unavailable font
  No Exception: null is returned

Typeface#createFromAsset for unsupported font
  Not Exception: Typeface.DEFAULT is returned
Typeface#createFromAsset for unavailable font
  Exception: RuntimeExcetpion

FontsContract#buildTypeface for unspported font
  No Exception: null is returned
FontsContract#buildTypeface for unspported font in XML
  No Exception: null is returned
FontsContract#buildTypeface for unspported font from provider
  No Exception: null is returned

TextView inflation for unsupported font
  No Exception: Typeface.DEFAULT is set
TextView inflation for unsupported font in XML
  No Exception: Typeface.DEFAULT is set
TextView inflation for unsupported font from provider
  No Exception: Typeface.DEFAULT is set

Bug: 127714175
Test: Manually done
Change-Id: Iaab037f4168546409ead67ed8eee7340418418ed
This commit is contained in:
Seigo Nonaka
2019-03-20 10:07:08 -07:00
parent 3d0a536ec8
commit 7d3d2aa286
2 changed files with 10 additions and 23 deletions

View File

@@ -33,7 +33,6 @@ import android.graphics.fonts.FontStyle;
import android.graphics.fonts.FontVariationAxis;
import android.graphics.fonts.SystemFonts;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.ParcelFileDescriptor;
import android.provider.FontRequest;
import android.provider.FontsContract;
@@ -48,7 +47,6 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.Preconditions;
import dalvik.annotation.optimization.CriticalNative;
import dalvik.system.VMRuntime;
import libcore.util.NativeAllocationRegistry;
@@ -266,16 +264,7 @@ public class Typeface {
if (familyBuilder == null) {
familyBuilder = new FontFamily.Builder(fontBuilder.build());
} else {
try {
familyBuilder.addFont(fontBuilder.build());
} catch (IllegalArgumentException e) {
if (VMRuntime.getRuntime().getTargetSdkVersion() <= VERSION_CODES.P) {
// Surpress the IllegalArgumentException for keeping the backward
// compatibility.
continue;
}
throw e;
}
familyBuilder.addFont(fontBuilder.build());
}
}
if (familyBuilder == null) {
@@ -297,6 +286,10 @@ public class Typeface {
typeface = new Typeface.CustomFallbackBuilder(family)
.setStyle(bestFont.getStyle())
.build();
} catch (IllegalArgumentException e) {
// To be a compatible behavior with API28 or before, catch IllegalArgumentExcetpion
// thrown by native code and returns null.
return null;
} catch (IOException e) {
typeface = Typeface.DEFAULT;
}