From 7d3d2aa286f59faad3fec15e9dbd3aee42d1da95 Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Wed, 20 Mar 2019 10:07:08 -0700 Subject: 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 --- graphics/java/android/graphics/Typeface.java | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'graphics/java') diff --git a/graphics/java/android/graphics/Typeface.java b/graphics/java/android/graphics/Typeface.java index 9995f1e73018..c8b361bbff2f 100644 --- a/graphics/java/android/graphics/Typeface.java +++ b/graphics/java/android/graphics/Typeface.java @@ -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; } -- cgit v1.2.3-59-g8ed1b