compat/src/main/java/androidx/core/content/res/ResourcesCompat.java
private static Typeface loadFont(
            @NonNull Context context, Resources wrapper, TypedValue value, int id, int style,
            @Nullable FontCallback fontCallback, @Nullable Handler handler,
            boolean isRequestFromLayoutInflator) {
        if (value.string == null) {
            throw new NotFoundException("Resource \"" + wrapper.getResourceName(id) + "\" ("
                    + Integer.toHexString(id) + ") is not a Font: " + value);
        }

        final String file = value.string.toString();
        if (!file.startsWith("res/")) {
            // Early exit if the specified string is unlikely to be a resource path.
            if (fontCallback != null) {
                fontCallback.callbackFailAsync(
                        FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR, handler);
            }
            return null;
        }
        Typeface typeface = TypefaceCompat.findFromCache(wrapper, id, style);

        if (typeface != null) {
            if (fontCallback != null) {
                fontCallback.callbackSuccessAsync(typeface, handler);
            }
            return typeface;
        }

        try {
            if (file.toLowerCase().endsWith(".xml")) {
                final XmlResourceParser rp = wrapper.getXml(id);
                final FamilyResourceEntry familyEntry =
                        FontResourcesParserCompat.parse(rp, wrapper);
                if (familyEntry == null) {
                    Log.e(TAG, "Failed to find font-family tag");
                    if (fontCallback != null) {
                        fontCallback.callbackFailAsync(
                                FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR, handler);
                    }
                    return null;
                }
                return TypefaceCompat.createFromResourcesFamilyXml(context, familyEntry, wrapper,
                        id, style, fontCallback, handler, isRequestFromLayoutInflator);
            }
            typeface = TypefaceCompat.createFromResourcesFontFile(
                    context, wrapper, id, file, style);
            if (fontCallback != null) {
                if (typeface != null) {
                    fontCallback.callbackSuccessAsync(typeface, handler);
                } else {
                    fontCallback.callbackFailAsync(
                            FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR, handler);
                }
            }
            return typeface;
        } catch (XmlPullParserException e) {
            Log.e(TAG, "Failed to parse xml resource " + file, e);
        } catch (IOException e) {
            Log.e(TAG, "Failed to read xml resource " + file, e);
        }
        if (fontCallback != null) {
            fontCallback.callbackFailAsync(
                    FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR, handler);
        }
        return null;
    }
/**
     * Load the given font. This method will always return null for asynchronous requests, which
     * provide a fontCallback, as there is no immediate result. When the callback is not provided,
     * the request is treated as synchronous and fails if async loading is required.
     */
Load the given font. This method will always return null for asynchronous requests, which
provide a fontCallback, as there is no immediate result. When the callback is not provided,
the request is treated as synchronous and fails if async loading is required.
Load the given font . This method will always return null for asynchronous requests which provide a fontCallback as there is no immediate result . When the callback is not provided the request is treated as synchronous and fails if async loading is required .
private static Typeface loadFont(
            @NonNull Context context, Resources wrapper, TypedValue value, int id, int style,
            @Nullable FontCallback fontCallback, @Nullable Handler handler,
            boolean isRequestFromLayoutInflator) {
        if (value.string == null) {
            throw new NotFoundException("Resource \"" + wrapper.getResourceName(id) + "\" ("
                    + Integer.toHexString(id) + ") is not a Font: " + value);
        }
        final String file = value.string.toString();
        if (!file.startsWith("res/")) {
            if (fontCallback != null) {
                fontCallback.callbackFailAsync(
                        FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR, handler);
            }
            return null;
        }
        Typeface typeface = TypefaceCompat.findFromCache(wrapper, id, style);
        if (typeface != null) {
            if (fontCallback != null) {
                fontCallback.callbackSuccessAsync(typeface, handler);
            }
            return typeface;
        }
        try {
            if (file.toLowerCase().endsWith(".xml")) {
                final XmlResourceParser rp = wrapper.getXml(id);
                final FamilyResourceEntry familyEntry =
                        FontResourcesParserCompat.parse(rp, wrapper);
                if (familyEntry == null) {
                    Log.e(TAG, "Failed to find font-family tag");
                    if (fontCallback != null) {
                        fontCallback.callbackFailAsync(
                                FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR, handler);
                    }
                    return null;
                }
                return TypefaceCompat.createFromResourcesFamilyXml(context, familyEntry, wrapper,
                        id, style, fontCallback, handler, isRequestFromLayoutInflator);
            }
            typeface = TypefaceCompat.createFromResourcesFontFile(
                    context, wrapper, id, file, style);
            if (fontCallback != null) {
                if (typeface != null) {
                    fontCallback.callbackSuccessAsync(typeface, handler);
                } else {
                    fontCallback.callbackFailAsync(
                            FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR, handler);
                }
            }
            return typeface;
        } catch (XmlPullParserException e) {
            Log.e(TAG, "Failed to parse xml resource " + file, e);
        } catch (IOException e) {
            Log.e(TAG, "Failed to read xml resource " + file, e);
        }
        if (fontCallback != null) {
            fontCallback.callbackFailAsync(
                    FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR, handler);
        }
        return null;
    }
private static Typeface loadFont ( @ NonNull Context context , Resources wrapper , TypedValue value , int id , int style , @ Nullable FontCallback fontCallback , @ Nullable Handler handler , boolean isRequestFromLayoutInflator ) { if ( value . string == null ) { throw new NotFoundException ( "Resource \"" + wrapper . getResourceName ( id ) + "\" (" + Integer . toHexString ( id ) + ") is not a Font: " + value ) ; } final String file = value . string . toString ( ) ; if ( ! file . startsWith ( "res/" ) ) { if ( fontCallback != null ) { fontCallback . callbackFailAsync ( FontRequestCallback . FAIL_REASON_FONT_LOAD_ERROR , handler ) ; } return null ; } Typeface typeface = TypefaceCompat . findFromCache ( wrapper , id , style ) ; if ( typeface != null ) { if ( fontCallback != null ) { fontCallback . callbackSuccessAsync ( typeface , handler ) ; } return typeface ; } try { if ( file . toLowerCase ( ) . endsWith ( ".xml" ) ) { final XmlResourceParser rp = wrapper . getXml ( id ) ; final FamilyResourceEntry familyEntry = FontResourcesParserCompat . parse ( rp , wrapper ) ; if ( familyEntry == null ) { Log . e ( TAG , "Failed to find font-family tag" ) ; if ( fontCallback != null ) { fontCallback . callbackFailAsync ( FontRequestCallback . FAIL_REASON_FONT_LOAD_ERROR , handler ) ; } return null ; } return TypefaceCompat . createFromResourcesFamilyXml ( context , familyEntry , wrapper , id , style , fontCallback , handler , isRequestFromLayoutInflator ) ; } typeface = TypefaceCompat . createFromResourcesFontFile ( context , wrapper , id , file , style ) ; if ( fontCallback != null ) { if ( typeface != null ) { fontCallback . callbackSuccessAsync ( typeface , handler ) ; } else { fontCallback . callbackFailAsync ( FontRequestCallback . FAIL_REASON_FONT_LOAD_ERROR , handler ) ; } } return typeface ; } catch ( XmlPullParserException e ) { Log . e ( TAG , "Failed to parse xml resource " + file , e ) ; } catch ( IOException e ) { Log . e ( TAG , "Failed to read xml resource " + file , e ) ; } if ( fontCallback != null ) { fontCallback . callbackFailAsync ( FontRequestCallback . FAIL_REASON_FONT_LOAD_ERROR , handler ) ; } return null ; }
Load the given font.
Load the given font .
// Early exit if the specified string is unlikely to be a resource path.