content: package androidx.core.graphics;
  
  import android.content.Context;
  import android.content.res.Resources;
  import android.graphics.Typeface;
  import android.os.CancellationSignal;
  import android.util.Log;
  import androidx.constraintlayout.core.motion.utils.TypedValues;
  import androidx.core.content.res.FontResourcesParserCompat;
  import androidx.core.provider.FontsContractCompat;
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  import java.lang.reflect.Field;
  import java.util.concurrent.ConcurrentHashMap;
  
  class TypefaceCompatBaseImpl {
      private static final int INVALID_KEY = 0;
      private static final String TAG = "TypefaceCompatBaseImpl";
      private ConcurrentHashMap<Long, FontResourcesParserCompat.FontFamilyFilesResourceEntry> mFontFamilies = new ConcurrentHashMap<>();
  
      private interface StyleExtractor<T> {
          int getWeight(T t);
  
          boolean isItalic(T t);
      }
  
      TypefaceCompatBaseImpl() {
      }
  
      private void addFontFamily(Typeface typeface, FontResourcesParserCompat.FontFamilyFilesResourceEntry entry) {
          long uniqueKey = getUniqueKey(typeface);
          if (uniqueKey != 0) {
              this.mFontFamilies.put(Long.valueOf(uniqueKey), entry);
          }
      }
  
      private FontResourcesParserCompat.FontFileResourceEntry findBestEntry(FontResourcesParserCompat.FontFamilyFilesResourceEntry entry, int style) {
          return (FontResourcesParserCompat.FontFileResourceEntry) findBestFont(entry.getEntries(), style, new StyleExtractor<FontResourcesParserCompat.FontFileResourceEntry>() {
              public int getWeight(FontResourcesParserCompat.FontFileResourceEntry entry) {
                  return entry.getWeight();
              }
  
              public boolean isItalic(FontResourcesParserCompat.FontFileResourceEntry entry) {
                  return entry.isItalic();
              }
          });
      }
  
      private static <T> T findBestFont(T[] tArr, int style, StyleExtractor<T> styleExtractor) {
          int i = (style & 1) == 0 ? 400 : TypedValues.TransitionType.TYPE_DURATION;
          boolean z = (style & 2) != 0;
          T t = null;
          int i2 = Integer.MAX_VALUE;
          for (T t2 : tArr) {
              int abs = (Math.abs(styleExtractor.getWeight(t2) - i) * 2) + (styleExtractor.isItalic(t2) == z ? 0 : 1);
              if (t == null || i2 > abs) {
                  t = t2;
                  i2 = abs;
              }
          }
          return t;
      }
  
      private static long getUniqueKey(Typeface typeface) {
          if (typeface == null) {
              return 0;
          }
          try {
              Field declaredField = Typeface.class.getDeclaredField("native_instance");
              declaredField.setAccessible(true);
              return ((Number) declaredField.get(typeface)).longValue();
          } catch (NoSuchFieldException e) {
              Log.e(TAG, "Could not retrieve font from family.", e);
              return 0;
          } catch (IllegalAccessException e2) {
              Log.e(TAG, "Could not retrieve font from family.", e2);
              return 0;
          }
      }
  
      public Typeface createFromFontFamilyFilesResourceEntry(Context context, FontResourcesParserCompat.FontFamilyFilesResourceEntry entry, Resources resources, int style) {
          FontResourcesParserCompat.FontFileResourceEntry findBestEntry = findBestEntry(entry, style);
          if (findBestEntry == null) {
              return null;
          }
          Typeface createFromResourcesFontFile = TypefaceCompat.createFromResourcesFontFile(context, resources, findBestEntry.getResourceId(), findBestEntry.getFileName(), 0, style);
          addFontFamily(createFromResourcesFontFile, entry);
          return createFromResourcesFontFile;
      }
  
      public Typeface createFromFontInfo(Context context, CancellationSignal cancellationSignal, FontsContractCompat.FontInfo[] fonts, int style) {
          if (fonts.length < 1) {
              return null;
          }
          InputStream inputStream = null;
          try {
              inputStream = context.getContentResolver().openInputStream(findBestInfo(fonts, style).getUri());
              return createFromInputStream(context, inputStream);
          } catch (IOException e) {
              return null;
          } finally {
              TypefaceCompatUtil.closeQuietly(inputStream);
          }
      }
  
      /* access modifiers changed from: protected */
      public Typeface createFromInputStream(Context context, InputStream is) {
          File tempFile = TypefaceCompatUtil.getTempFile(context);
          if (tempFile == null) {
              return null;
          }
          try {
              if (!TypefaceCompatUtil.copyToFile(tempFile, is)) {
                  return null;
              }
              Typeface createFromFile = Typeface.createFromFile(tempFile.getPath());
              tempFile.delete();
              return createFromFile;
          } catch (RuntimeException e) {
              return null;
          } finally {
              tempFile.delete();
          }
      }
  
      public Typeface createFromResourcesFontFile(Context context, Resources resources, int id, String path, int style) {
          File tempFile = TypefaceCompatUtil.getTempFile(context);
          if (tempFile == null) {
              return null;
          }
          try {
              if (!TypefaceCompatUtil.copyToFile(tempFile, resources, id)) {
                  return null;
              }
              Typeface createFromFile = Typeface.createFromFile(tempFile.getPath());
              tempFile.delete();
              return createFromFile;
          } catch (RuntimeException e) {
              return null;
          } finally {
              tempFile.delete();
          }
      }
  
      /* access modifiers changed from: protected */
      public FontsContractCompat.FontInfo findBestInfo(FontsContractCompat.FontInfo[] fonts, int style) {
          return (FontsContractCompat.FontInfo) findBestFont(fonts, style, new StyleExtractor<FontsContractCompat.FontInfo>() {
              public int getWeight(FontsContractCompat.FontInfo info) {
                  return info.getWeight();
              }
  
              public boolean isItalic(FontsContractCompat.FontInfo info) {
                  return info.isItalic();
              }
          });
      }
  
      /* access modifiers changed from: package-private */
      public FontResourcesParserCompat.FontFamilyFilesResourceEntry getFontFamily(Typeface typeface) {
          long uniqueKey = getUniqueKey(typeface);
          if (uniqueKey == 0) {
              return null;
          }
          return this.mFontFamilies.get(Long.valueOf(uniqueKey));
      }
  }
  