package J;

import A1.C0005f;
import D1.F;
import N1.B0;
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.Build;
import android.os.RemoteException;
import android.os.Trace;
import android.util.Log;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import n4.u0;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final B0 f3415a = new B0(2);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final F f3416b = new F(2);

    public static C0005f a(Context context, List list) {
        s7.d.e("FontProvider.getFontFamilyResult");
        try {
            ArrayList arrayList = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                e eVar = (e) list.get(i);
                ProviderInfo providerInfoB = b(context.getPackageManager(), eVar, context.getResources());
                if (providerInfoB == null) {
                    C0005f c0005f = new C0005f();
                    c0005f.f155a = 1;
                    c0005f.f156b = Collections.singletonList(null);
                    return c0005f;
                }
                arrayList.add(c(context, eVar, providerInfoB.authority));
            }
            C0005f c0005f2 = new C0005f();
            c0005f2.f155a = 0;
            c0005f2.f156b = arrayList;
            return c0005f2;
        } finally {
            Trace.endSection();
        }
    }

    public static ProviderInfo b(PackageManager packageManager, e eVar, Resources resources) {
        s7.d.e("FontProvider.getProvider");
        try {
            List listK = eVar.f3420d;
            String str = eVar.f3417a;
            String str2 = eVar.f3418b;
            if (listK == null) {
                listK = E.b.k(resources, 0);
            }
            c cVar = new c();
            cVar.f3412a = str;
            cVar.f3413b = str2;
            cVar.f3414c = listK;
            B0 b02 = f3415a;
            ProviderInfo providerInfo = (ProviderInfo) b02.h(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());
            }
            F f3 = f3416b;
            Collections.sort(arrayList, f3);
            for (int i = 0; i < listK.size(); i++) {
                ArrayList arrayList2 = new ArrayList((Collection) listK.get(i));
                Collections.sort(arrayList2, f3);
                if (arrayList.size() == arrayList2.size()) {
                    for (int i5 = 0; i5 < arrayList.size(); i5++) {
                        if (!Arrays.equals((byte[]) arrayList.get(i5), (byte[]) arrayList2.get(i5))) {
                            break;
                        }
                    }
                    b02.m(cVar, providerInfoResolveContentProvider);
                    return providerInfoResolveContentProvider;
                }
            }
            Trace.endSection();
            return null;
        } finally {
            Trace.endSection();
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:37:0x00db  */
    /* JADX WARN: Removed duplicated region for block: B:62:0x012b A[Catch: all -> 0x014c, TRY_ENTER, TryCatch #3 {all -> 0x014c, blocks: (B:3:0x000b, B:5:0x003e, B:62:0x012b, B:63:0x012e, B:70:0x0145, B:71:0x0148, B:72:0x014b, B:6:0x0045, B:8:0x004c, B:30:0x00a1, B:32:0x00a6, B:34:0x00ac, B:35:0x00d5, B:39:0x00de, B:44:0x00eb, B:47:0x00f4, B:51:0x010a, B:54:0x0113, B:58:0x011f, B:49:0x00ff, B:67:0x013f, B:68:0x0142, B:10:0x0065, B:11:0x006d, B:12:0x0070, B:16:0x007a, B:19:0x0080, B:21:0x0089, B:25:0x0093, B:28:0x0099), top: B:81:0x000b, inners: #0 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static j[] c(Context context, e eVar, String str) {
        Cursor cursorQuery;
        Cursor cursor;
        s7.d.e("FontProvider.query");
        try {
            ArrayList arrayList = new ArrayList();
            Uri uriBuild = new Uri.Builder().scheme("content").authority(str).build();
            Uri uriBuild2 = new Uri.Builder().scheme("content").authority(str).appendPath("file").build();
            b bVar = Build.VERSION.SDK_INT < 24 ? new b(context, uriBuild, 0) : new b(context, uriBuild, 1);
            Cursor cursor2 = null;
            try {
                String[] strArr = {"_id", "file_id", "font_ttc_index", "font_variation_settings", "font_weight", "font_italic", "result_code"};
                s7.d.e("ContentQueryWrapper.query");
                try {
                    String[] strArr2 = {eVar.f3419c};
                    switch (bVar.f3410a) {
                        case u0.f15495q /* 0 */:
                            cursorQuery = null;
                            ContentProviderClient contentProviderClient = bVar.f3411b;
                            if (contentProviderClient != null) {
                                try {
                                    cursorQuery = contentProviderClient.query(uriBuild, strArr, "query = ?", strArr2, null, null);
                                } catch (RemoteException e8) {
                                    Log.w("FontsProvider", "Unable to query the content provider", e8);
                                }
                                break;
                            }
                            cursor = cursorQuery;
                            Trace.endSection();
                            if (cursor != null && cursor.getCount() > 0) {
                                int columnIndex = cursor.getColumnIndex("result_code");
                                ArrayList arrayList2 = 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()) {
                                    int i = columnIndex != -1 ? cursor.getInt(columnIndex) : 0;
                                    arrayList2.add(new j(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) : 400, columnIndex6 != -1 && cursor.getInt(columnIndex6) == 1, i));
                                }
                                arrayList = arrayList2;
                            }
                            if (cursor != null) {
                                cursor.close();
                            }
                            bVar.a();
                            return (j[]) arrayList.toArray(new j[0]);
                        default:
                            cursorQuery = null;
                            ContentProviderClient contentProviderClient2 = bVar.f3411b;
                            if (contentProviderClient2 != null) {
                                try {
                                    cursorQuery = contentProviderClient2.query(uriBuild, strArr, "query = ?", strArr2, null, null);
                                } catch (RemoteException e9) {
                                    Log.w("FontsProvider", "Unable to query the content provider", e9);
                                }
                                break;
                            }
                            cursor = cursorQuery;
                            Trace.endSection();
                            if (cursor != null) {
                                int columnIndex7 = cursor.getColumnIndex("result_code");
                                ArrayList arrayList22 = new ArrayList();
                                int columnIndex22 = cursor.getColumnIndex("_id");
                                int columnIndex32 = cursor.getColumnIndex("file_id");
                                int columnIndex42 = cursor.getColumnIndex("font_ttc_index");
                                int columnIndex52 = cursor.getColumnIndex("font_weight");
                                int columnIndex62 = cursor.getColumnIndex("font_italic");
                                while (cursor.moveToNext()) {
                                }
                                arrayList = arrayList22;
                            }
                            if (cursor != null) {
                            }
                            bVar.a();
                            return (j[]) arrayList.toArray(new j[0]);
                    }
                } finally {
                }
            } catch (Throwable th) {
                if (0 != 0) {
                    cursor2.close();
                }
                bVar.a();
                throw th;
            }
        } finally {
        }
    }
}
