package n0;

import android.content.ContentProviderClient;
import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.Signature;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.CancellationSignal;
import android.os.RemoteException;
import android.os.Trace;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import tv.danmaku.ijk.media.player.IjkEventListener;
import xyz.stream.utils.j0;

/* JADX INFO: loaded from: classes.dex */
public abstract class d {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final androidx.collection.n f30726a = new androidx.collection.n(2);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final androidx.browser.trusted.b f30727b = new androidx.browser.trusted.b(1);

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:46:0x0125  */
    /* JADX WARN: Removed duplicated region for block: B:48:0x0129 A[Catch: all -> 0x012d, TRY_ENTER, TryCatch #4 {all -> 0x012d, blocks: (B:3:0x0019, B:48:0x0129, B:51:0x012f, B:53:0x0136, B:54:0x013b, B:62:0x0150, B:63:0x0153, B:65:0x015a, B:66:0x015f), top: B:78:0x0019 }] */
    /* JADX WARN: Removed duplicated region for block: B:53:0x0136 A[Catch: all -> 0x012d, TryCatch #4 {all -> 0x012d, blocks: (B:3:0x0019, B:48:0x0129, B:51:0x012f, B:53:0x0136, B:54:0x013b, B:62:0x0150, B:63:0x0153, B:65:0x015a, B:66:0x015f), top: B:78:0x0019 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static k[] a(Context context, e eVar, String str, CancellationSignal cancellationSignal) {
        ArrayList arrayList;
        int i10;
        Cursor cursor;
        ArrayList arrayList2;
        Object obj;
        Trace.beginSection(androidx.datastore.preferences.core.c.t("FontProvider.query"));
        try {
            ArrayList arrayList3 = new ArrayList();
            Uri uriBuild = new Uri.Builder().scheme("content").authority(str).build();
            Uri uriBuild2 = new Uri.Builder().scheme("content").authority(str).appendPath("file").build();
            j0 j0Var = new j0(context, uriBuild);
            Cursor cursorQuery = null;
            try {
                String[] strArr = {"_id", "file_id", "font_ttc_index", "font_variation_settings", "font_weight", "font_italic", "result_code"};
                Trace.beginSection(androidx.datastore.preferences.core.c.t("ContentQueryWrapper.query"));
                try {
                    String[] strArr2 = {eVar.f30730c};
                    Object obj2 = j0Var.f36154b;
                    if (((ContentProviderClient) obj2) != null) {
                        try {
                            arrayList = arrayList3;
                            i10 = 1;
                            try {
                                cursorQuery = ((ContentProviderClient) obj2).query(uriBuild, strArr, "query = ?", strArr2, null, cancellationSignal);
                            } catch (RemoteException unused) {
                            }
                        } catch (RemoteException unused2) {
                            arrayList = arrayList3;
                            i10 = 1;
                        }
                        cursor = cursorQuery;
                        if (cursor != null) {
                            arrayList2 = arrayList;
                        }
                        if (cursor != null) {
                        }
                        obj = j0Var.f36154b;
                        if (((ContentProviderClient) obj) != null) {
                        }
                        return (k[]) arrayList2.toArray(new k[0]);
                    }
                    arrayList = arrayList3;
                    i10 = 1;
                    cursor = cursorQuery;
                    try {
                        if (cursor != null || cursor.getCount() <= 0) {
                            arrayList2 = arrayList;
                        } else {
                            int columnIndex = cursor.getColumnIndex("result_code");
                            ArrayList arrayList4 = new ArrayList();
                            int columnIndex2 = cursor.getColumnIndex("_id");
                            int columnIndex3 = cursor.getColumnIndex("file_id");
                            int columnIndex4 = cursor.getColumnIndex("font_ttc_index");
                            int columnIndex5 = cursor.getColumnIndex("font_weight");
                            int columnIndex6 = cursor.getColumnIndex("font_italic");
                            while (cursor.moveToNext()) {
                                arrayList4.add(new k(columnIndex3 == -1 ? ContentUris.withAppendedId(uriBuild, cursor.getLong(columnIndex2)) : ContentUris.withAppendedId(uriBuild2, cursor.getLong(columnIndex3)), columnIndex4 != -1 ? cursor.getInt(columnIndex4) : 0, columnIndex5 != -1 ? cursor.getInt(columnIndex5) : IjkEventListener.VIDEO_SIZE_CHANGED, (columnIndex6 == -1 || cursor.getInt(columnIndex6) != i10) ? 0 : i10, columnIndex != -1 ? cursor.getInt(columnIndex) : 0));
                            }
                            arrayList2 = arrayList4;
                        }
                        if (cursor != null) {
                            cursor.close();
                        }
                        obj = j0Var.f36154b;
                        if (((ContentProviderClient) obj) != null) {
                            ((ContentProviderClient) obj).close();
                        }
                        return (k[]) arrayList2.toArray(new k[0]);
                    } catch (Throwable th) {
                        th = th;
                        cursorQuery = cursor;
                    }
                } finally {
                }
            } catch (Throwable th2) {
                th = th2;
            }
            if (cursorQuery != null) {
                cursorQuery.close();
            }
            Object obj3 = j0Var.f36154b;
            if (((ContentProviderClient) obj3) != null) {
                ((ContentProviderClient) obj3).close();
            }
            throw th;
        } catch (Throwable th3) {
            throw th3;
        }
    }

    @NonNull
    public static j getFontFamilyResult(@NonNull Context context, @NonNull List<e> list, @Nullable CancellationSignal cancellationSignal) throws PackageManager.NameNotFoundException {
        Trace.beginSection(androidx.datastore.preferences.core.c.t("FontProvider.getFontFamilyResult"));
        try {
            ArrayList arrayList = new ArrayList();
            for (int i10 = 0; i10 < list.size(); i10++) {
                e eVar = list.get(i10);
                ProviderInfo provider = getProvider(context.getPackageManager(), eVar, context.getResources());
                if (provider == null) {
                    return j.create(1, (k[]) null);
                }
                arrayList.add(a(context, eVar, provider.authority, cancellationSignal));
            }
            return j.create(0, arrayList);
        } finally {
            Trace.endSection();
        }
    }

    @Nullable
    @VisibleForTesting
    public static ProviderInfo getProvider(@NonNull PackageManager packageManager, @NonNull e eVar, @Nullable Resources resources) throws PackageManager.NameNotFoundException {
        Trace.beginSection(androidx.datastore.preferences.core.c.t("FontProvider.getProvider"));
        try {
            List<List<byte[]>> certificates = eVar.getCertificates();
            String str = eVar.f30728a;
            String str2 = eVar.f30729b;
            List<List<byte[]>> certificates2 = certificates != null ? eVar.getCertificates() : i0.i.a(resources, 0);
            c cVar = new c();
            cVar.f30723a = str;
            cVar.f30724b = str2;
            cVar.f30725c = certificates2;
            androidx.collection.n nVar = f30726a;
            ProviderInfo providerInfo = (ProviderInfo) nVar.b(cVar);
            if (providerInfo != null) {
                return providerInfo;
            }
            ProviderInfo providerInfoResolveContentProvider = packageManager.resolveContentProvider(str, 0);
            if (providerInfoResolveContentProvider == null) {
                throw new PackageManager.NameNotFoundException("No package found for authority: " + str);
            }
            if (!providerInfoResolveContentProvider.packageName.equals(str2)) {
                throw new PackageManager.NameNotFoundException("Found content provider " + str + ", but package was not " + str2);
            }
            Signature[] signatureArr = packageManager.getPackageInfo(providerInfoResolveContentProvider.packageName, 64).signatures;
            ArrayList arrayList = new ArrayList();
            for (Signature signature : signatureArr) {
                arrayList.add(signature.toByteArray());
            }
            androidx.browser.trusted.b bVar = f30727b;
            Collections.sort(arrayList, bVar);
            for (int i10 = 0; i10 < certificates2.size(); i10++) {
                ArrayList arrayList2 = new ArrayList(certificates2.get(i10));
                Collections.sort(arrayList2, bVar);
                if (arrayList.size() == arrayList2.size()) {
                    for (int i11 = 0; i11 < arrayList.size(); i11++) {
                        if (!Arrays.equals((byte[]) arrayList.get(i11), (byte[]) arrayList2.get(i11))) {
                            break;
                        }
                    }
                    nVar.c(cVar, providerInfoResolveContentProvider);
                    return providerInfoResolveContentProvider;
                }
            }
            Trace.endSection();
            return null;
        } finally {
            Trace.endSection();
        }
    }
}
