1/* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package androidx.core.graphics; 18 19import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; 20 21import android.graphics.Typeface; 22 23import androidx.annotation.RequiresApi; 24import androidx.annotation.RestrictTo; 25 26import java.lang.reflect.Array; 27import java.lang.reflect.InvocationTargetException; 28import java.lang.reflect.Method; 29 30/** 31 * Implementation of the Typeface compat methods for API 28 and above. 32 * @hide 33 */ 34@RestrictTo(LIBRARY_GROUP) 35@RequiresApi(28) 36public class TypefaceCompatApi28Impl extends TypefaceCompatApi26Impl { 37 private static final String TAG = "TypefaceCompatApi28Impl"; 38 39 private static final String CREATE_FROM_FAMILIES_WITH_DEFAULT_METHOD = 40 "createFromFamiliesWithDefault"; 41 private static final int RESOLVE_BY_FONT_TABLE = -1; 42 private static final String DEFAULT_FAMILY = "sans-serif"; 43 44 /** 45 * Call method Typeface#createFromFamiliesWithDefault( 46 * FontFamily[] families, String fallbackName, int weight, int italic) 47 */ 48 @Override 49 protected Typeface createFromFamiliesWithDefault(Object family) { 50 try { 51 Object familyArray = Array.newInstance(mFontFamily, 1); 52 Array.set(familyArray, 0, family); 53 return (Typeface) mCreateFromFamiliesWithDefault.invoke(null /* static method */, 54 familyArray, DEFAULT_FAMILY, RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE); 55 } catch (IllegalAccessException | InvocationTargetException e) { 56 throw new RuntimeException(e); 57 } 58 } 59 60 @Override 61 protected Method obtainCreateFromFamiliesWithDefaultMethod(Class fontFamily) 62 throws NoSuchMethodException { 63 Object familyArray = Array.newInstance(fontFamily, 1); 64 Method m = Typeface.class.getDeclaredMethod(CREATE_FROM_FAMILIES_WITH_DEFAULT_METHOD, 65 familyArray.getClass(), String.class, Integer.TYPE, Integer.TYPE); 66 m.setAccessible(true); 67 return m; 68 } 69} 70