content: package androidx.core.graphics;
  
  import android.content.Context;
  import android.content.res.AssetManager;
  import android.content.res.Resources;
  import android.graphics.Typeface;
  import android.graphics.fonts.FontVariationAxis;
  import android.net.Uri;
  import android.os.CancellationSignal;
  import android.os.ParcelFileDescriptor;
  import android.util.Log;
  import androidx.core.content.res.FontResourcesParserCompat;
  import androidx.core.provider.FontsContractCompat;
  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 java.nio.ByteBuffer;
  import java.util.Map;
  
  public class TypefaceCompatApi26Impl extends TypefaceCompatApi21Impl {
      private static final String ABORT_CREATION_METHOD = "abortCreation";
      private static final String ADD_FONT_FROM_ASSET_MANAGER_METHOD = "addFontFromAssetManager";
      private static final String ADD_FONT_FROM_BUFFER_METHOD = "addFontFromBuffer";
      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 FREEZE_METHOD = "freeze";
      private static final int RESOLVE_BY_FONT_TABLE = -1;
      private static final String TAG = "TypefaceCompatApi26Impl";
      protected final Method mAbortCreation;
      protected final Method mAddFontFromAssetManager;
      protected final Method mAddFontFromBuffer;
      protected final Method mCreateFromFamiliesWithDefault;
      protected final Class<?> mFontFamily;
      protected final Constructor<?> mFontFamilyCtor;
      protected final Method mFreeze;
  
      public TypefaceCompatApi26Impl() {
          Method method;
          Method method2;
          Method method3;
          Method method4;
          Method method5;
          Constructor<?> constructor;
          Class<?> cls;
          try {
              cls = obtainFontFamily();
              constructor = obtainFontFamilyCtor(cls);
              method5 = obtainAddFontFromAssetManagerMethod(cls);
              method4 = obtainAddFontFromBufferMethod(cls);
              method3 = obtainFreezeMethod(cls);
              method2 = obtainAbortCreationMethod(cls);
              method = obtainCreateFromFamiliesWithDefaultMethod(cls);
          } catch (ClassNotFoundException | NoSuchMethodException e) {
              Log.e(TAG, "Unable to collect necessary methods for class " + e.getClass().getName(), e);
              cls = null;
              constructor = null;
              method5 = null;
              method4 = null;
              method3 = null;
              method2 = null;
              method = null;
          }
          this.mFontFamily = cls;
          this.mFontFamilyCtor = constructor;
          this.mAddFontFromAssetManager = method5;
          this.mAddFontFromBuffer = method4;
          this.mFreeze = method3;
          this.mAbortCreation = method2;
          this.mCreateFromFamiliesWithDefault = method;
      }
  
      private void abortCreation(Object family) {
          try {
              this.mAbortCreation.invoke(family, new Object[0]);
          } catch (IllegalAccessException | InvocationTargetException e) {
          }
      }
  
      private boolean addFontFromAssetManager(Context context, Object family, String fileName, int ttcIndex, int weight, int style, FontVariationAxis[] axes) {
          try {
              return ((Boolean) this.mAddFontFromAssetManager.invoke(family, new Object[]{context.getAssets(), fileName, 0, false, Integer.valueOf(ttcIndex), Integer.valueOf(weight), Integer.valueOf(style), axes})).booleanValue();
          } catch (IllegalAccessException | InvocationTargetException e) {
              return false;
          }
      }
  
      private boolean addFontFromBuffer(Object family, ByteBuffer buffer, int ttcIndex, int weight, int style) {
          try {
              return ((Boolean) this.mAddFontFromBuffer.invoke(family, new Object[]{buffer, Integer.valueOf(ttcIndex), null, Integer.valueOf(weight), Integer.valueOf(style)})).booleanValue();
          } catch (IllegalAccessException | InvocationTargetException e) {
              return false;
          }
      }
  
      private boolean freeze(Object family) {
          try {
              return ((Boolean) this.mFreeze.invoke(family, new Object[0])).booleanValue();
          } catch (IllegalAccessException | InvocationTargetException e) {
              return false;
          }
      }
  
      private boolean isFontFamilyPrivateAPIAvailable() {
          if (this.mAddFontFromAssetManager == null) {
              Log.w(TAG, "Unable to collect necessary private methods. Fallback to legacy implementation.");
          }
          return this.mAddFontFromAssetManager != null;
      }
  
      private Object newFamily() {
          try {
              return this.mFontFamilyCtor.newInstance(new Object[0]);
          } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
              return null;
          }
      }
  
      /* access modifiers changed from: protected */
      public Typeface createFromFamiliesWithDefault(Object family) {
          try {
              Object newInstance = Array.newInstance(this.mFontFamily, 1);
              Array.set(newInstance, 0, family);
              return (Typeface) this.mCreateFromFamiliesWithDefault.invoke((Object) null, new Object[]{newInstance, -1, -1});
          } catch (IllegalAccessException | InvocationTargetException e) {
              return null;
          }
      }
  
      public Typeface createFromFontFamilyFilesResourceEntry(Context context, FontResourcesParserCompat.FontFamilyFilesResourceEntry entry, Resources resources, int style) {
          if (!isFontFamilyPrivateAPIAvailable()) {
              return super.createFromFontFamilyFilesResourceEntry(context, entry, resources, style);
          }
          Object newFamily = newFamily();
          if (newFamily == null) {
              return null;
          }
          for (FontResourcesParserCompat.FontFileResourceEntry fontFileResourceEntry : entry.getEntries()) {
              if (!addFontFromAssetManager(context, newFamily, fontFileResourceEntry.getFileName(), fontFileResourceEntry.getTtcIndex(), fontFileResourceEntry.getWeight(), fontFileResourceEntry.isItalic() ? 1 : 0, FontVariationAxis.fromFontVariationSettings(fontFileResourceEntry.getVariationSettings()))) {
                  abortCreation(newFamily);
                  return null;
              }
          }
          if (!freeze(newFamily)) {
              return null;
          }
          return createFromFamiliesWithDefault(newFamily);
      }
  
      public Typeface createFromFontInfo(Context context, CancellationSignal cancellationSignal, FontsContractCompat.FontInfo[] fonts, int style) {
          Typeface createFromFamiliesWithDefault;
          int i;
          ParcelFileDescriptor openFileDescriptor;
          Throwable th;
          CancellationSignal cancellationSignal2 = cancellationSignal;
          FontsContractCompat.FontInfo[] fontInfoArr = fonts;
          int i2 = style;
          if (fontInfoArr.length < 1) {
              return null;
          }
          if (!isFontFamilyPrivateAPIAvailable()) {
              FontsContractCompat.FontInfo findBestInfo = findBestInfo(fontInfoArr, i2);
              try {
                  openFileDescriptor = context.getContentResolver().openFileDescriptor(findBestInfo.getUri(), "r", cancellationSignal2);
                  if (openFileDescriptor == null) {
                      if (openFileDescriptor != null) {
                          openFileDescriptor.close();
                      }
                      return null;
                  }
                  Typeface build = new Typeface.Builder(openFileDescriptor.getFileDescriptor()).setWeight(findBestInfo.getWeight()).setItalic(findBestInfo.isItalic()).build();
                  if (openFileDescriptor != null) {
                      openFileDescriptor.close();
                  }
                  return build;
              } catch (IOException e) {
                  return null;
              } catch (Throwable th2) {
                  th.addSuppressed(th2);
              }
          } else {
              Map<Uri, ByteBuffer> readFontInfoIntoByteBuffer = TypefaceCompatUtil.readFontInfoIntoByteBuffer(context, fontInfoArr, cancellationSignal2);
              Object newFamily = newFamily();
              if (newFamily == null) {
                  return null;
              }
              int length = fontInfoArr.length;
              boolean z = false;
              int i3 = 0;
              while (i3 < length) {
                  FontsContractCompat.FontInfo fontInfo = fontInfoArr[i3];
                  ByteBuffer byteBuffer = readFontInfoIntoByteBuffer.get(fontInfo.getUri());
                  if (byteBuffer == null) {
                      i = i3;
                  } else {
                      i = i3;
                      if (!addFontFromBuffer(newFamily, byteBuffer, fontInfo.getTtcIndex(), fontInfo.getWeight(), fontInfo.isItalic() ? 1 : 0)) {
                          abortCreation(newFamily);
                          return null;
                      }
                      z = true;
                  }
                  i3 = i + 1;
              }
              if (!z) {
                  abortCreation(newFamily);
                  return null;
              } else if (freeze(newFamily) && (createFromFamiliesWithDefault = createFromFamiliesWithDefault(newFamily)) != null) {
                  return Typeface.create(createFromFamiliesWithDefault, i2);
              } else {
                  return null;
              }
          }
          throw th;
      }
  
      public Typeface createFromResourcesFontFile(Context context, Resources resources, int id, String path, int style) {
          if (!isFontFamilyPrivateAPIAvailable()) {
              return super.createFromResourcesFontFile(context, resources, id, path, style);
          }
          Object newFamily = newFamily();
          if (newFamily == null) {
              return null;
          }
          if (!addFontFromAssetManager(context, newFamily, path, 0, -1, -1, (FontVariationAxis[]) null)) {
              abortCreation(newFamily);
              return null;
          } else if (!freeze(newFamily)) {
              return null;
          } else {
              return createFromFamiliesWithDefault(newFamily);
          }
      }
  
      /* access modifiers changed from: protected */
      public Method obtainAbortCreationMethod(Class<?> cls) throws NoSuchMethodException {
          return cls.getMethod(ABORT_CREATION_METHOD, new Class[0]);
      }
  
      /* access modifiers changed from: protected */
      public Method obtainAddFontFromAssetManagerMethod(Class<?> cls) throws NoSuchMethodException {
          return cls.getMethod(ADD_FONT_FROM_ASSET_MANAGER_METHOD, new Class[]{AssetManager.class, String.class, Integer.TYPE, Boolean.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, FontVariationAxis[].class});
      }
  
      /* access modifiers changed from: protected */
      public Method obtainAddFontFromBufferMethod(Class<?> cls) throws NoSuchMethodException {
          return cls.getMethod(ADD_FONT_FROM_BUFFER_METHOD, new Class[]{ByteBuffer.class, Integer.TYPE, FontVariationAxis[].class, Integer.TYPE, Integer.TYPE});
      }
  
      /* access modifiers changed from: protected */
      public Method obtainCreateFromFamiliesWithDefaultMethod(Class<?> cls) throws NoSuchMethodException {
          Method declaredMethod = Typeface.class.getDeclaredMethod(CREATE_FROM_FAMILIES_WITH_DEFAULT_METHOD, new Class[]{Array.newInstance(cls, 1).getClass(), Integer.TYPE, Integer.TYPE});
          declaredMethod.setAccessible(true);
          return declaredMethod;
      }
  
      /* access modifiers changed from: protected */
      public Class<?> obtainFontFamily() throws ClassNotFoundException {
          return Class.forName(FONT_FAMILY_CLASS);
      }
  
      /* access modifiers changed from: protected */
      public Constructor<?> obtainFontFamilyCtor(Class<?> cls) throws NoSuchMethodException {
          return cls.getConstructor(new Class[0]);
      }
  
      /* access modifiers changed from: protected */
      public Method obtainFreezeMethod(Class<?> cls) throws NoSuchMethodException {
          return cls.getMethod(FREEZE_METHOD, new Class[0]);
      }
  }
  