content: package androidx.core.graphics;
  
  import android.content.Context;
  import android.content.res.Resources;
  import android.graphics.Typeface;
  import android.os.CancellationSignal;
  import android.os.ParcelFileDescriptor;
  import android.system.ErrnoException;
  import android.system.Os;
  import android.system.OsConstants;
  import android.util.Log;
  import androidx.core.content.res.FontResourcesParserCompat;
  import androidx.core.provider.FontsContractCompat;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.lang.reflect.Array;
  import java.lang.reflect.Constructor;
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  import mt.Log1F380D;
  
  /* compiled from: 0042 */
  class TypefaceCompatApi21Impl extends TypefaceCompatBaseImpl {
      private static final String ADD_FONT_WEIGHT_STYLE_METHOD = "addFontWeightStyle";
      private static final String CREATE_FROM_FAMILIES_WITH_DEFAULT_METHOD = "createFromFamiliesWithDefault";
      private static final String FONT_FAMILY_CLASS = "android.graphics.FontFamily";
      private static final String TAG = "TypefaceCompatApi21Impl";
      private static Method sAddFontWeightStyle;
      private static Method sCreateFromFamiliesWithDefault;
      private static Class<?> sFontFamily;
      private static Constructor<?> sFontFamilyCtor;
      private static boolean sHasInitBeenCalled = false;
  
      TypefaceCompatApi21Impl() {
      }
  
      private static boolean addFontWeightStyle(Object family, String name, int weight, boolean style) {
          init();
          try {
              return ((Boolean) sAddFontWeightStyle.invoke(family, new Object[]{name, Integer.valueOf(weight), Boolean.valueOf(style)})).booleanValue();
          } catch (IllegalAccessException | InvocationTargetException e) {
              throw new RuntimeException(e);
          }
      }
  
      private static Typeface createFromFamiliesWithDefault(Object family) {
          init();
          try {
              Object newInstance = Array.newInstance(sFontFamily, 1);
              Array.set(newInstance, 0, family);
              return (Typeface) sCreateFromFamiliesWithDefault.invoke((Object) null, new Object[]{newInstance});
          } catch (IllegalAccessException | InvocationTargetException e) {
              throw new RuntimeException(e);
          }
      }
  
      private File getFile(ParcelFileDescriptor fd) {
          try {
              String readlink = Os.readlink("/proc/self/fd/" + fd.getFd());
              Log1F380D.a((Object) readlink);
              if (OsConstants.S_ISREG(Os.stat(readlink).st_mode)) {
                  return new File(readlink);
              }
              return null;
          } catch (ErrnoException e) {
              return null;
          }
      }
  
      private static void init() {
          Method method;
          Constructor<?> constructor;
          Class<?> cls;
          Method method2;
          if (!sHasInitBeenCalled) {
              sHasInitBeenCalled = true;
              try {
                  cls = Class.forName(FONT_FAMILY_CLASS);
                  constructor = cls.getConstructor(new Class[0]);
                  method = cls.getMethod(ADD_FONT_WEIGHT_STYLE_METHOD, new Class[]{String.class, Integer.TYPE, Boolean.TYPE});
                  method2 = Typeface.class.getMethod(CREATE_FROM_FAMILIES_WITH_DEFAULT_METHOD, new Class[]{Array.newInstance(cls, 1).getClass()});
              } catch (ClassNotFoundException | NoSuchMethodException e) {
                  Log.e(TAG, e.getClass().getName(), e);
                  cls = null;
                  constructor = null;
                  method = null;
                  method2 = null;
              }
              sFontFamilyCtor = constructor;
              sFontFamily = cls;
              sAddFontWeightStyle = method;
              sCreateFromFamiliesWithDefault = method2;
          }
      }
  
      private static Object newFamily() {
          init();
          try {
              return sFontFamilyCtor.newInstance(new Object[0]);
          } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
              throw new RuntimeException(e);
          }
      }
  
      public Typeface createFromFontFamilyFilesResourceEntry(Context context, FontResourcesParserCompat.FontFamilyFilesResourceEntry entry, Resources resources, int style) {
          Object newFamily = newFamily();
          FontResourcesParserCompat.FontFileResourceEntry[] entries = entry.getEntries();
          int length = entries.length;
          int i = 0;
          while (i < length) {
              FontResourcesParserCompat.FontFileResourceEntry fontFileResourceEntry = entries[i];
              File tempFile = TypefaceCompatUtil.getTempFile(context);
              if (tempFile == null) {
                  return null;
              }
              try {
                  if (!TypefaceCompatUtil.copyToFile(tempFile, resources, fontFileResourceEntry.getResourceId())) {
                      tempFile.delete();
                      return null;
                  } else if (!addFontWeightStyle(newFamily, tempFile.getPath(), fontFileResourceEntry.getWeight(), fontFileResourceEntry.isItalic())) {
                      return null;
                  } else {
                      tempFile.delete();
                      i++;
                  }
              } catch (RuntimeException e) {
                  return null;
              } finally {
                  tempFile.delete();
              }
          }
          return createFromFamiliesWithDefault(newFamily);
      }
  
      public Typeface createFromFontInfo(Context context, CancellationSignal cancellationSignal, FontsContractCompat.FontInfo[] fonts, int style) {
          FileInputStream fileInputStream;
          if (fonts.length < 1) {
              return null;
          }
          FontsContractCompat.FontInfo findBestInfo = findBestInfo(fonts, style);
          try {
              ParcelFileDescriptor openFileDescriptor = context.getContentResolver().openFileDescriptor(findBestInfo.getUri(), "r", cancellationSignal);
              if (openFileDescriptor == null) {
                  if (openFileDescriptor != null) {
                      openFileDescriptor.close();
                  }
                  return null;
              }
              try {
                  File file = getFile(openFileDescriptor);
                  if (file != null) {
                      if (file.canRead()) {
                          Typeface createFromFile = Typeface.createFromFile(file);
                          if (openFileDescriptor != null) {
                              openFileDescriptor.close();
                          }
                          return createFromFile;
                      }
                  }
                  fileInputStream = new FileInputStream(openFileDescriptor.getFileDescriptor());
                  Typeface createFromInputStream = super.createFromInputStream(context, fileInputStream);
                  fileInputStream.close();
                  if (openFileDescriptor != null) {
                      openFileDescriptor.close();
                  }
                  return createFromInputStream;
              } catch (Throwable th) {
                  if (openFileDescriptor != null) {
                      openFileDescriptor.close();
                  }
                  throw th;
              }
          } catch (IOException e) {
              return null;
          } catch (Throwable th2) {
              th.addSuppressed(th2);
          }
          throw th;
      }
  }
  