package p073z;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.os.StrictMode;
import android.util.Log;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import p014e0.C0655g;

/* renamed from: z.p0 */
/* loaded from: classes.dex */
public class C1904p0 {

    /* renamed from: z.p0$a */
    /* loaded from: classes.dex */
    public static class C1905a {
        /* renamed from: a */
        public static ParcelFileDescriptor m72a(ContentResolver contentResolver, Uri uri, String str, CancellationSignal cancellationSignal) {
            ParcelFileDescriptor openFileDescriptor;
            openFileDescriptor = contentResolver.openFileDescriptor(uri, str, cancellationSignal);
            return openFileDescriptor;
        }
    }

    /* renamed from: a */
    public static void m80a(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException unused) {
            }
        }
    }

    /* renamed from: b */
    public static ByteBuffer m79b(Context context, Resources resources, int i) {
        File m76e = m76e(context);
        if (m76e == null) {
            return null;
        }
        try {
            if (m78c(m76e, resources, i)) {
                return m74g(m76e);
            }
            return null;
        } finally {
            m76e.delete();
        }
    }

    /* renamed from: c */
    public static boolean m78c(File file, Resources resources, int i) {
        InputStream inputStream;
        try {
            inputStream = resources.openRawResource(i);
        } catch (Throwable th) {
            th = th;
            inputStream = null;
        }
        try {
            boolean m77d = m77d(file, inputStream);
            m80a(inputStream);
            return m77d;
        } catch (Throwable th2) {
            th = th2;
            m80a(inputStream);
            throw th;
        }
    }

    /* renamed from: d */
    public static boolean m77d(File file, InputStream inputStream) {
        StrictMode.ThreadPolicy allowThreadDiskWrites = StrictMode.allowThreadDiskWrites();
        FileOutputStream fileOutputStream = null;
        try {
            try {
                FileOutputStream fileOutputStream2 = new FileOutputStream(file, false);
                try {
                    byte[] bArr = new byte[1024];
                    while (true) {
                        int read = inputStream.read(bArr);
                        if (read == -1) {
                            m80a(fileOutputStream2);
                            StrictMode.setThreadPolicy(allowThreadDiskWrites);
                            return true;
                        }
                        fileOutputStream2.write(bArr, 0, read);
                    }
                } catch (IOException e) {
                    e = e;
                    fileOutputStream = fileOutputStream2;
                    Log.e("TypefaceCompatUtil", "Error copying resource contents to temp file: " + e.getMessage());
                    m80a(fileOutputStream);
                    StrictMode.setThreadPolicy(allowThreadDiskWrites);
                    return false;
                } catch (Throwable th) {
                    th = th;
                    fileOutputStream = fileOutputStream2;
                    m80a(fileOutputStream);
                    StrictMode.setThreadPolicy(allowThreadDiskWrites);
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (IOException e2) {
            e = e2;
        }
    }

    /* renamed from: e */
    public static File m76e(Context context) {
        File cacheDir = context.getCacheDir();
        if (cacheDir == null) {
            return null;
        }
        String str = ".font" + Process.myPid() + "-" + Process.myTid() + "-";
        for (int i = 0; i < 100; i++) {
            File file = new File(cacheDir, str + i);
            if (file.createNewFile()) {
                return file;
            }
        }
        return null;
    }

    /* renamed from: f */
    public static ByteBuffer m75f(Context context, CancellationSignal cancellationSignal, Uri uri) {
        try {
            ParcelFileDescriptor m72a = C1905a.m72a(context.getContentResolver(), uri, "r", cancellationSignal);
            if (m72a == null) {
                if (m72a != null) {
                    m72a.close();
                }
                return null;
            }
            FileInputStream fileInputStream = new FileInputStream(m72a.getFileDescriptor());
            try {
                FileChannel channel = fileInputStream.getChannel();
                MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0L, channel.size());
                fileInputStream.close();
                m72a.close();
                return map;
            } catch (Throwable th) {
                try {
                    fileInputStream.close();
                } catch (Throwable th2) {
                    Throwable.class.getDeclaredMethod("addSuppressed", Throwable.class).invoke(th, th2);
                }
                throw th;
            }
        } catch (IOException unused) {
            return null;
        }
    }

    /* renamed from: g */
    public static ByteBuffer m74g(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            FileChannel channel = fileInputStream.getChannel();
            MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0L, channel.size());
            fileInputStream.close();
            return map;
        } catch (IOException unused) {
            return null;
        }
    }

    /* renamed from: h */
    public static Map<Uri, ByteBuffer> m73h(Context context, C0655g.C0657b[] c0657bArr, CancellationSignal cancellationSignal) {
        HashMap hashMap = new HashMap();
        for (C0655g.C0657b c0657b : c0657bArr) {
            if (c0657b.m3191b() == 0) {
                Uri m3189d = c0657b.m3189d();
                if (!hashMap.containsKey(m3189d)) {
                    hashMap.put(m3189d, m75f(context, cancellationSignal, m3189d));
                }
            }
        }
        return Collections.unmodifiableMap(hashMap);
    }
}