package q3;

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.util.Log;
import com.google.firebase.analytics.FirebaseAnalytics;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import q3.g;

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

    /* renamed from: a, reason: collision with root package name */
    private static final Comparator f33395a = new Comparator() { // from class: q3.c
        @Override // java.util.Comparator
        public final int compare(Object obj, Object obj2) {
            int g10;
            g10 = d.g((byte[]) obj, (byte[]) obj2);
            return g10;
        }
    };

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public interface a {
        static a a(Context context, Uri uri) {
            return new b(context, uri);
        }

        Cursor b(Uri uri, String[] strArr, String str, String[] strArr2, String str2, CancellationSignal cancellationSignal);

        void close();
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class b implements a {

        /* renamed from: a, reason: collision with root package name */
        private final ContentProviderClient f33396a;

        b(Context context, Uri uri) {
            this.f33396a = context.getContentResolver().acquireUnstableContentProviderClient(uri);
        }

        @Override // q3.d.a
        public Cursor b(Uri uri, String[] strArr, String str, String[] strArr2, String str2, CancellationSignal cancellationSignal) {
            ContentProviderClient contentProviderClient = this.f33396a;
            if (contentProviderClient == null) {
                return null;
            }
            try {
                return contentProviderClient.query(uri, strArr, str, strArr2, str2, cancellationSignal);
            } catch (RemoteException e10) {
                Log.w("FontsProvider", "Unable to query the content provider", e10);
                return null;
            }
        }

        @Override // q3.d.a
        public void close() {
            ContentProviderClient contentProviderClient = this.f33396a;
            if (contentProviderClient != null) {
                contentProviderClient.close();
            }
        }
    }

    private static List b(Signature[] signatureArr) {
        ArrayList arrayList = new ArrayList();
        for (Signature signature : signatureArr) {
            arrayList.add(signature.toByteArray());
        }
        return arrayList;
    }

    private static boolean c(List list, List list2) {
        if (list.size() != list2.size()) {
            return false;
        }
        for (int i10 = 0; i10 < list.size(); i10++) {
            if (!Arrays.equals((byte[]) list.get(i10), (byte[]) list2.get(i10))) {
                return false;
            }
        }
        return true;
    }

    private static List d(e eVar, Resources resources) {
        return eVar.b() != null ? eVar.b() : androidx.core.content.res.e.c(resources, eVar.c());
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static g.a e(Context context, e eVar, CancellationSignal cancellationSignal) {
        ProviderInfo f10 = f(context.getPackageManager(), eVar, context.getResources());
        return f10 == null ? g.a.a(1, null) : g.a.a(0, h(context, eVar, f10.authority, cancellationSignal));
    }

    static ProviderInfo f(PackageManager packageManager, e eVar, Resources resources) {
        String e10 = eVar.e();
        ProviderInfo resolveContentProvider = packageManager.resolveContentProvider(e10, 0);
        if (resolveContentProvider == null) {
            throw new PackageManager.NameNotFoundException("No package found for authority: " + e10);
        }
        if (!resolveContentProvider.packageName.equals(eVar.f())) {
            throw new PackageManager.NameNotFoundException("Found content provider " + e10 + ", but package was not " + eVar.f());
        }
        List b10 = b(packageManager.getPackageInfo(resolveContentProvider.packageName, 64).signatures);
        Collections.sort(b10, f33395a);
        List d10 = d(eVar, resources);
        for (int i10 = 0; i10 < d10.size(); i10++) {
            ArrayList arrayList = new ArrayList((Collection) d10.get(i10));
            Collections.sort(arrayList, f33395a);
            if (c(b10, arrayList)) {
                return resolveContentProvider;
            }
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ int g(byte[] bArr, byte[] bArr2) {
        if (bArr.length != bArr2.length) {
            return bArr.length - bArr2.length;
        }
        for (int i10 = 0; i10 < bArr.length; i10++) {
            byte b10 = bArr[i10];
            byte b11 = bArr2[i10];
            if (b10 != b11) {
                return b10 - b11;
            }
        }
        return 0;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:7:0x00fb  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    static g.b[] h(Context context, e eVar, String str, CancellationSignal cancellationSignal) {
        ArrayList arrayList;
        int i10;
        Uri withAppendedId;
        int i11;
        boolean z10;
        ArrayList arrayList2 = new ArrayList();
        Uri build = new Uri.Builder().scheme(FirebaseAnalytics.Param.CONTENT).authority(str).build();
        Uri build2 = new Uri.Builder().scheme(FirebaseAnalytics.Param.CONTENT).authority(str).appendPath("file").build();
        a a10 = a.a(context, build);
        Cursor cursor = null;
        try {
            int i12 = 1;
            Cursor b10 = a10.b(build, new String[]{"_id", "file_id", "font_ttc_index", "font_variation_settings", "font_weight", "font_italic", "result_code"}, "query = ?", new String[]{eVar.g()}, null, cancellationSignal);
            if (b10 != null) {
                try {
                    if (b10.getCount() > 0) {
                        int columnIndex = b10.getColumnIndex("result_code");
                        ArrayList arrayList3 = new ArrayList();
                        int columnIndex2 = b10.getColumnIndex("_id");
                        int columnIndex3 = b10.getColumnIndex("file_id");
                        int columnIndex4 = b10.getColumnIndex("font_ttc_index");
                        int columnIndex5 = b10.getColumnIndex("font_weight");
                        int columnIndex6 = b10.getColumnIndex("font_italic");
                        while (b10.moveToNext()) {
                            int i13 = columnIndex != -1 ? b10.getInt(columnIndex) : 0;
                            int i14 = columnIndex4 != -1 ? b10.getInt(columnIndex4) : 0;
                            if (columnIndex3 == -1) {
                                i10 = i14;
                                withAppendedId = ContentUris.withAppendedId(build, b10.getLong(columnIndex2));
                            } else {
                                i10 = i14;
                                withAppendedId = ContentUris.withAppendedId(build2, b10.getLong(columnIndex3));
                            }
                            int i15 = columnIndex5 != -1 ? b10.getInt(columnIndex5) : 400;
                            if (columnIndex6 == -1 || b10.getInt(columnIndex6) != i12) {
                                i11 = i15;
                                z10 = 0;
                            } else {
                                z10 = i12;
                                i11 = i15;
                            }
                            arrayList3.add(g.b.a(withAppendedId, i10, i11, z10, i13));
                            i12 = 1;
                        }
                        arrayList = arrayList3;
                        if (b10 != null) {
                            b10.close();
                        }
                        a10.close();
                        return (g.b[]) arrayList.toArray(new g.b[0]);
                    }
                } catch (Throwable th2) {
                    th = th2;
                    cursor = b10;
                    if (cursor != null) {
                        cursor.close();
                    }
                    a10.close();
                    throw th;
                }
            }
            arrayList = arrayList2;
            if (b10 != null) {
            }
            a10.close();
            return (g.b[]) arrayList.toArray(new g.b[0]);
        } catch (Throwable th3) {
            th = th3;
        }
    }
}
