compat/src/main/java/androidx/core/provider/FontsContractCompat.java
public static void requestFont(final @NonNull Context context,
            final @NonNull FontRequest request, final @NonNull FontRequestCallback callback,
            final @NonNull Handler handler) {
        final Handler callerThreadHandler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                // TODO: Cache the result.
                FontFamilyResult result;
                try {
                    result = fetchFonts(context, null /* cancellation signal */, request);
                } catch (PackageManager.NameNotFoundException e) {
                    callerThreadHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onTypefaceRequestFailed(
                                    FontRequestCallback.FAIL_REASON_PROVIDER_NOT_FOUND);
                        }
                    });
                    return;
                }

                if (result.getStatusCode() != FontFamilyResult.STATUS_OK) {
                    switch (result.getStatusCode()) {
                        case FontFamilyResult.STATUS_WRONG_CERTIFICATES:
                            callerThreadHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onTypefaceRequestFailed(
                                            FontRequestCallback.FAIL_REASON_WRONG_CERTIFICATES);
                                }
                            });
                            return;
                        case FontFamilyResult.STATUS_UNEXPECTED_DATA_PROVIDED:
                            callerThreadHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onTypefaceRequestFailed(
                                            FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR);
                                }
                            });
                            return;
                        default:
                            // fetchFont returns unexpected status type. Fallback to load error.
                            callerThreadHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onTypefaceRequestFailed(
                                            FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR);
                                }
                            });
                            return;
                    }
                }

                final FontInfo[] fonts = result.getFonts();
                if (fonts == null || fonts.length == 0) {
                    callerThreadHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onTypefaceRequestFailed(
                                    FontRequestCallback.FAIL_REASON_FONT_NOT_FOUND);
                        }
                    });
                    return;
                }
                for (final FontInfo font : fonts) {
                    if (font.getResultCode() != Columns.RESULT_CODE_OK) {
                        // We proceed if all font entry is ready to use. Otherwise report the first
                        // error.
                        final int resultCode = font.getResultCode();
                        if (resultCode < 0) {
                            // Negative values are reserved for internal errors. Fallback to load
                            // error.
                            callerThreadHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onTypefaceRequestFailed(
                                            FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR);
                                }
                            });
                        } else {
                            callerThreadHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onTypefaceRequestFailed(resultCode);
                                }
                            });
                        }
                        return;
                    }
                }

                final Typeface typeface = buildTypeface(context, null /* cancellation signal */,
                        fonts);
                if (typeface == null) {
                    // Something went wrong during reading font files. This happens if the given
                    // font file is an unsupported font type.
                    callerThreadHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onTypefaceRequestFailed(
                                    FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR);
                        }
                    });
                    return;
                }

                callerThreadHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onTypefaceRetrieved(typeface);
                    }
                });
            }
        });
    }
/**
     * Create a typeface object given a font request. The font will be asynchronously fetched,
     * therefore the result is delivered to the given callback. See {@link FontRequest}.
     * Only one of the methods in callback will be invoked, depending on whether the request
     * succeeds or fails. These calls will happen on the caller thread.
     * @param context A context to be used for fetching from font provider.
     * @param request A {@link FontRequest} object that identifies the provider and query for the
     *                request. May not be null.
     * @param callback A callback that will be triggered when results are obtained. May not be null.
     * @param handler A handler to be processed the font fetching.
     */
Create a typeface object given a font request. The font will be asynchronously fetched,
therefore the result is delivered to the given callback.
Create a typeface object given a font request . The font will be asynchronously fetched therefore the result is delivered to the given callback .
public static void requestFont(final @NonNull Context context,
            final @NonNull FontRequest request, final @NonNull FontRequestCallback callback,
            final @NonNull Handler handler) {
        final Handler callerThreadHandler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                FontFamilyResult result;
                try {
                    result = fetchFonts(context, null /* cancellation signal */, request);
                } catch (PackageManager.NameNotFoundException e) {
                    callerThreadHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onTypefaceRequestFailed(
                                    FontRequestCallback.FAIL_REASON_PROVIDER_NOT_FOUND);
                        }
                    });
                    return;
                }
                if (result.getStatusCode() != FontFamilyResult.STATUS_OK) {
                    switch (result.getStatusCode()) {
                        case FontFamilyResult.STATUS_WRONG_CERTIFICATES:
                            callerThreadHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onTypefaceRequestFailed(
                                            FontRequestCallback.FAIL_REASON_WRONG_CERTIFICATES);
                                }
                            });
                            return;
                        case FontFamilyResult.STATUS_UNEXPECTED_DATA_PROVIDED:
                            callerThreadHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onTypefaceRequestFailed(
                                            FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR);
                                }
                            });
                            return;
                        default:
                            callerThreadHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onTypefaceRequestFailed(
                                            FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR);
                                }
                            });
                            return;
                    }
                }
                final FontInfo[] fonts = result.getFonts();
                if (fonts == null || fonts.length == 0) {
                    callerThreadHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onTypefaceRequestFailed(
                                    FontRequestCallback.FAIL_REASON_FONT_NOT_FOUND);
                        }
                    });
                    return;
                }
                for (final FontInfo font : fonts) {
                    if (font.getResultCode() != Columns.RESULT_CODE_OK) {
                        final int resultCode = font.getResultCode();
                        if (resultCode < 0) {
                            callerThreadHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onTypefaceRequestFailed(
                                            FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR);
                                }
                            });
                        } else {
                            callerThreadHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onTypefaceRequestFailed(resultCode);
                                }
                            });
                        }
                        return;
                    }
                }
                final Typeface typeface = buildTypeface(context, null /* cancellation signal */,
                        fonts);
                if (typeface == null) {
                    callerThreadHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onTypefaceRequestFailed(
                                    FontRequestCallback.FAIL_REASON_FONT_LOAD_ERROR);
                        }
                    });
                    return;
                }
                callerThreadHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onTypefaceRetrieved(typeface);
                    }
                });
            }
        });
    }
public static void requestFont ( final @ NonNull Context context , final @ NonNull FontRequest request , final @ NonNull FontRequestCallback callback , final @ NonNull Handler handler ) { final Handler callerThreadHandler = new Handler ( ) ; handler . post ( new Runnable ( ) { @ Override public void run ( ) { FontFamilyResult result ; try { result = fetchFonts ( context , null /* cancellation signal */ , request ) ; } catch ( PackageManager . NameNotFoundException e ) { callerThreadHandler . post ( new Runnable ( ) { @ Override public void run ( ) { callback . onTypefaceRequestFailed ( FontRequestCallback . FAIL_REASON_PROVIDER_NOT_FOUND ) ; } } ) ; return ; } if ( result . getStatusCode ( ) != FontFamilyResult . STATUS_OK ) { switch ( result . getStatusCode ( ) ) { case FontFamilyResult . STATUS_WRONG_CERTIFICATES : callerThreadHandler . post ( new Runnable ( ) { @ Override public void run ( ) { callback . onTypefaceRequestFailed ( FontRequestCallback . FAIL_REASON_WRONG_CERTIFICATES ) ; } } ) ; return ; case FontFamilyResult . STATUS_UNEXPECTED_DATA_PROVIDED : callerThreadHandler . post ( new Runnable ( ) { @ Override public void run ( ) { callback . onTypefaceRequestFailed ( FontRequestCallback . FAIL_REASON_FONT_LOAD_ERROR ) ; } } ) ; return ; default : callerThreadHandler . post ( new Runnable ( ) { @ Override public void run ( ) { callback . onTypefaceRequestFailed ( FontRequestCallback . FAIL_REASON_FONT_LOAD_ERROR ) ; } } ) ; return ; } } final FontInfo [ ] fonts = result . getFonts ( ) ; if ( fonts == null || fonts . length == 0 ) { callerThreadHandler . post ( new Runnable ( ) { @ Override public void run ( ) { callback . onTypefaceRequestFailed ( FontRequestCallback . FAIL_REASON_FONT_NOT_FOUND ) ; } } ) ; return ; } for ( final FontInfo font : fonts ) { if ( font . getResultCode ( ) != Columns . RESULT_CODE_OK ) { final int resultCode = font . getResultCode ( ) ; if ( resultCode < 0 ) { callerThreadHandler . post ( new Runnable ( ) { @ Override public void run ( ) { callback . onTypefaceRequestFailed ( FontRequestCallback . FAIL_REASON_FONT_LOAD_ERROR ) ; } } ) ; } else { callerThreadHandler . post ( new Runnable ( ) { @ Override public void run ( ) { callback . onTypefaceRequestFailed ( resultCode ) ; } } ) ; } return ; } } final Typeface typeface = buildTypeface ( context , null /* cancellation signal */ , fonts ) ; if ( typeface == null ) { callerThreadHandler . post ( new Runnable ( ) { @ Override public void run ( ) { callback . onTypefaceRequestFailed ( FontRequestCallback . FAIL_REASON_FONT_LOAD_ERROR ) ; } } ) ; return ; } callerThreadHandler . post ( new Runnable ( ) { @ Override public void run ( ) { callback . onTypefaceRetrieved ( typeface ) ; } } ) ; } } ) ; }
Create a typeface object given a font request.
Create a typeface object given a font request .
// TODO: Cache the result. // fetchFont returns unexpected status type. Fallback to load error. // We proceed if all font entry is ready to use. Otherwise report the first // error. // Negative values are reserved for internal errors. Fallback to load // error. // Something went wrong during reading font files. This happens if the given // font file is an unsupported font type.