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:
@@ -34,7 +34,6 @@ import android.graphics.fonts.FontFamily;
|
||||
import android.graphics.fonts.FontStyle;
|
||||
import android.graphics.fonts.FontVariationAxis;
|
||||
import android.net.Uri;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.os.CancellationSignal;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
@@ -652,17 +651,12 @@ public class FontsContract {
|
||||
if (familyBuilder == null) {
|
||||
familyBuilder = new FontFamily.Builder(font);
|
||||
} else {
|
||||
try {
|
||||
familyBuilder.addFont(font);
|
||||
} catch (IllegalArgumentException e) {
|
||||
if (context.getApplicationInfo().targetSdkVersion <= VERSION_CODES.P) {
|
||||
// Surpress the IllegalArgumentException for keeping the backward
|
||||
// compatibility.
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
familyBuilder.addFont(font);
|
||||
}
|
||||
} 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) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user