package X6FRs_4.IMuAHx_6.XnFJ5_9;

import X6FRs_4.IMuAHx_6.IFxWkV_5.Xgtqvk_8;
import X6FRs_4.IMuAHx_6.KNBe_11.FSaI4_7;
import X6FRs_4.IMuAHx_6.X6FRs_4.PGPF6hQ_2.IFxWkV_5;
import X6FRs_4.IMuAHx_6.XnFJ5_9.PGPF6hQ_2;
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.graphics.Typeface;
import android.net.Uri;
import android.os.Build;
import android.os.CancellationSignal;
import android.os.Handler;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
/* compiled from: FontsContractCompat.java */
/* loaded from: classes.dex */
public class QxJ9d_1 {

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public static final X6FRs_4.Mqv2r_3.X6FRs_4<String, Typeface> f538KQgRr5e_0 = new X6FRs_4.Mqv2r_3.X6FRs_4<>(16);
    public static final X6FRs_4.IMuAHx_6.XnFJ5_9.PGPF6hQ_2 KQgRr5e_0 = new X6FRs_4.IMuAHx_6.XnFJ5_9.PGPF6hQ_2("fonts", 10, 10000);

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public static final Object f539KQgRr5e_0 = new Object();

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public static final X6FRs_4.Mqv2r_3.IMuAHx_6<String, ArrayList<PGPF6hQ_2.Mqv2r_3<IMuAHx_6>>> f537KQgRr5e_0 = new X6FRs_4.Mqv2r_3.IMuAHx_6<>();

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public static final Comparator<byte[]> f540KQgRr5e_0 = new Mqv2r_3();

    /* compiled from: FontsContractCompat.java */
    /* loaded from: classes.dex */
    public static class IFxWkV_5 {
        public final int KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final Uri f541KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final boolean f542KQgRr5e_0;
        public final int PGPF6hQ_2;
        public final int QxJ9d_1;

        public IFxWkV_5(Uri uri, int i, int i2, boolean z, int i3) {
            FSaI4_7.KQgRr5e_0(uri);
            this.f541KQgRr5e_0 = uri;
            this.KQgRr5e_0 = i;
            this.QxJ9d_1 = i2;
            this.f542KQgRr5e_0 = z;
            this.PGPF6hQ_2 = i3;
        }

        /* renamed from: KQgRr5e_0  reason: collision with other method in class */
        public Uri m346KQgRr5e_0() {
            return this.f541KQgRr5e_0;
        }

        public int PGPF6hQ_2() {
            return this.QxJ9d_1;
        }

        public int QxJ9d_1() {
            return this.KQgRr5e_0;
        }

        /* renamed from: KQgRr5e_0  reason: collision with other method in class */
        public boolean m347KQgRr5e_0() {
            return this.f542KQgRr5e_0;
        }

        public int KQgRr5e_0() {
            return this.PGPF6hQ_2;
        }
    }

    /* compiled from: FontsContractCompat.java */
    /* loaded from: classes.dex */
    public static final class IMuAHx_6 {
        public final int KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final Typeface f543KQgRr5e_0;

        public IMuAHx_6(Typeface typeface, int i) {
            this.f543KQgRr5e_0 = typeface;
            this.KQgRr5e_0 = i;
        }
    }

    /* compiled from: FontsContractCompat.java */
    /* loaded from: classes.dex */
    public static class KQgRr5e_0 implements Callable<IMuAHx_6> {
        public final /* synthetic */ int KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final /* synthetic */ X6FRs_4.IMuAHx_6.XnFJ5_9.KQgRr5e_0 f544KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final /* synthetic */ Context f545KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final /* synthetic */ String f546KQgRr5e_0;

        public KQgRr5e_0(Context context, X6FRs_4.IMuAHx_6.XnFJ5_9.KQgRr5e_0 kQgRr5e_0, int i, String str) {
            this.f545KQgRr5e_0 = context;
            this.f544KQgRr5e_0 = kQgRr5e_0;
            this.KQgRr5e_0 = i;
            this.f546KQgRr5e_0 = str;
        }

        @Override // java.util.concurrent.Callable
        /* renamed from: KQgRr5e_0 */
        public IMuAHx_6 call() throws Exception {
            IMuAHx_6 KQgRr5e_0 = QxJ9d_1.KQgRr5e_0(this.f545KQgRr5e_0, this.f544KQgRr5e_0, this.KQgRr5e_0);
            Typeface typeface = KQgRr5e_0.f543KQgRr5e_0;
            if (typeface != null) {
                QxJ9d_1.f538KQgRr5e_0.m513KQgRr5e_0((X6FRs_4.Mqv2r_3.X6FRs_4<String, Typeface>) this.f546KQgRr5e_0, (String) typeface);
            }
            return KQgRr5e_0;
        }
    }

    /* compiled from: FontsContractCompat.java */
    /* loaded from: classes.dex */
    public static class Mqv2r_3 implements Comparator<byte[]> {
        /* JADX WARN: Multi-variable type inference failed */
        @Override // java.util.Comparator
        /* renamed from: KQgRr5e_0 */
        public int compare(byte[] bArr, byte[] bArr2) {
            int i;
            int i2;
            if (bArr.length != bArr2.length) {
                i = bArr.length;
                i2 = bArr2.length;
            } else {
                for (int i3 = 0; i3 < bArr.length; i3++) {
                    if (bArr[i3] != bArr2[i3]) {
                        i = bArr[i3];
                        i2 = bArr2[i3];
                    }
                }
                return 0;
            }
            return i - i2;
        }
    }

    /* compiled from: FontsContractCompat.java */
    /* loaded from: classes.dex */
    public static class PGPF6hQ_2 implements PGPF6hQ_2.Mqv2r_3<IMuAHx_6> {
        public final /* synthetic */ String KQgRr5e_0;

        public PGPF6hQ_2(String str) {
            this.KQgRr5e_0 = str;
        }

        @Override // X6FRs_4.IMuAHx_6.XnFJ5_9.PGPF6hQ_2.Mqv2r_3
        public void KQgRr5e_0(IMuAHx_6 iMuAHx_6) {
            synchronized (QxJ9d_1.f539KQgRr5e_0) {
                ArrayList<PGPF6hQ_2.Mqv2r_3<IMuAHx_6>> arrayList = QxJ9d_1.f537KQgRr5e_0.get(this.KQgRr5e_0);
                if (arrayList == null) {
                    return;
                }
                QxJ9d_1.f537KQgRr5e_0.remove(this.KQgRr5e_0);
                for (int i = 0; i < arrayList.size(); i++) {
                    arrayList.get(i).KQgRr5e_0(iMuAHx_6);
                }
            }
        }
    }

    /* compiled from: FontsContractCompat.java */
    /* renamed from: X6FRs_4.IMuAHx_6.XnFJ5_9.QxJ9d_1$QxJ9d_1  reason: collision with other inner class name */
    /* loaded from: classes.dex */
    public static class C0048QxJ9d_1 implements PGPF6hQ_2.Mqv2r_3<IMuAHx_6> {
        public final /* synthetic */ IFxWkV_5.KQgRr5e_0 KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final /* synthetic */ Handler f547KQgRr5e_0;

        public C0048QxJ9d_1(IFxWkV_5.KQgRr5e_0 kQgRr5e_0, Handler handler) {
            this.KQgRr5e_0 = kQgRr5e_0;
            this.f547KQgRr5e_0 = handler;
        }

        @Override // X6FRs_4.IMuAHx_6.XnFJ5_9.PGPF6hQ_2.Mqv2r_3
        public void KQgRr5e_0(IMuAHx_6 iMuAHx_6) {
            if (iMuAHx_6 == null) {
                this.KQgRr5e_0.callbackFailAsync(1, this.f547KQgRr5e_0);
                return;
            }
            int i = iMuAHx_6.KQgRr5e_0;
            if (i == 0) {
                this.KQgRr5e_0.callbackSuccessAsync(iMuAHx_6.f543KQgRr5e_0, this.f547KQgRr5e_0);
            } else {
                this.KQgRr5e_0.callbackFailAsync(i, this.f547KQgRr5e_0);
            }
        }
    }

    /* compiled from: FontsContractCompat.java */
    /* loaded from: classes.dex */
    public static class X6FRs_4 {
        public final int KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final IFxWkV_5[] f548KQgRr5e_0;

        public X6FRs_4(int i, IFxWkV_5[] iFxWkV_5Arr) {
            this.KQgRr5e_0 = i;
            this.f548KQgRr5e_0 = iFxWkV_5Arr;
        }

        public int KQgRr5e_0() {
            return this.KQgRr5e_0;
        }

        /* renamed from: KQgRr5e_0  reason: collision with other method in class */
        public IFxWkV_5[] m348KQgRr5e_0() {
            return this.f548KQgRr5e_0;
        }
    }

    public static IMuAHx_6 KQgRr5e_0(Context context, X6FRs_4.IMuAHx_6.XnFJ5_9.KQgRr5e_0 kQgRr5e_0, int i) {
        try {
            X6FRs_4 KQgRr5e_02 = KQgRr5e_0(context, (CancellationSignal) null, kQgRr5e_0);
            if (KQgRr5e_02.KQgRr5e_0() == 0) {
                Typeface KQgRr5e_03 = X6FRs_4.IMuAHx_6.IFxWkV_5.PGPF6hQ_2.KQgRr5e_0(context, null, KQgRr5e_02.m348KQgRr5e_0(), i);
                return new IMuAHx_6(KQgRr5e_03, KQgRr5e_03 != null ? 0 : -3);
            }
            return new IMuAHx_6(null, KQgRr5e_02.KQgRr5e_0() == 1 ? -2 : -3);
        } catch (PackageManager.NameNotFoundException unused) {
            return new IMuAHx_6(null, -1);
        }
    }

    public static Typeface KQgRr5e_0(Context context, X6FRs_4.IMuAHx_6.XnFJ5_9.KQgRr5e_0 kQgRr5e_0, IFxWkV_5.KQgRr5e_0 kQgRr5e_02, Handler handler, boolean z, int i, int i2) {
        String str = kQgRr5e_0.m344KQgRr5e_0() + "-" + i2;
        Typeface QxJ9d_1 = f538KQgRr5e_0.QxJ9d_1(str);
        if (QxJ9d_1 != null) {
            if (kQgRr5e_02 != null) {
                kQgRr5e_02.onFontRetrieved(QxJ9d_1);
            }
            return QxJ9d_1;
        } else if (z && i == -1) {
            IMuAHx_6 KQgRr5e_02 = KQgRr5e_0(context, kQgRr5e_0, i2);
            if (kQgRr5e_02 != null) {
                int i3 = KQgRr5e_02.KQgRr5e_0;
                if (i3 == 0) {
                    kQgRr5e_02.callbackSuccessAsync(KQgRr5e_02.f543KQgRr5e_0, handler);
                } else {
                    kQgRr5e_02.callbackFailAsync(i3, handler);
                }
            }
            return KQgRr5e_02.f543KQgRr5e_0;
        } else {
            KQgRr5e_0 kQgRr5e_03 = new KQgRr5e_0(context, kQgRr5e_0, i2, str);
            if (z) {
                try {
                    return ((IMuAHx_6) KQgRr5e_0.KQgRr5e_0(kQgRr5e_03, i)).f543KQgRr5e_0;
                } catch (InterruptedException unused) {
                    return null;
                }
            }
            C0048QxJ9d_1 c0048QxJ9d_1 = kQgRr5e_02 == null ? null : new C0048QxJ9d_1(kQgRr5e_02, handler);
            synchronized (f539KQgRr5e_0) {
                ArrayList<PGPF6hQ_2.Mqv2r_3<IMuAHx_6>> arrayList = f537KQgRr5e_0.get(str);
                if (arrayList != null) {
                    if (c0048QxJ9d_1 != null) {
                        arrayList.add(c0048QxJ9d_1);
                    }
                    return null;
                }
                if (c0048QxJ9d_1 != null) {
                    ArrayList<PGPF6hQ_2.Mqv2r_3<IMuAHx_6>> arrayList2 = new ArrayList<>();
                    arrayList2.add(c0048QxJ9d_1);
                    f537KQgRr5e_0.put(str, arrayList2);
                }
                KQgRr5e_0.KQgRr5e_0(kQgRr5e_03, new PGPF6hQ_2(str));
                return null;
            }
        }
    }

    public static Map<Uri, ByteBuffer> KQgRr5e_0(Context context, IFxWkV_5[] iFxWkV_5Arr, CancellationSignal cancellationSignal) {
        HashMap hashMap = new HashMap();
        for (IFxWkV_5 iFxWkV_5 : iFxWkV_5Arr) {
            if (iFxWkV_5.KQgRr5e_0() == 0) {
                Uri m346KQgRr5e_0 = iFxWkV_5.m346KQgRr5e_0();
                if (!hashMap.containsKey(m346KQgRr5e_0)) {
                    hashMap.put(m346KQgRr5e_0, Xgtqvk_8.KQgRr5e_0(context, cancellationSignal, m346KQgRr5e_0));
                }
            }
        }
        return Collections.unmodifiableMap(hashMap);
    }

    public static X6FRs_4 KQgRr5e_0(Context context, CancellationSignal cancellationSignal, X6FRs_4.IMuAHx_6.XnFJ5_9.KQgRr5e_0 kQgRr5e_0) throws PackageManager.NameNotFoundException {
        ProviderInfo KQgRr5e_02 = KQgRr5e_0(context.getPackageManager(), kQgRr5e_0, context.getResources());
        if (KQgRr5e_02 == null) {
            return new X6FRs_4(1, null);
        }
        return new X6FRs_4(0, KQgRr5e_0(context, kQgRr5e_0, KQgRr5e_02.authority, cancellationSignal));
    }

    public static ProviderInfo KQgRr5e_0(PackageManager packageManager, X6FRs_4.IMuAHx_6.XnFJ5_9.KQgRr5e_0 kQgRr5e_0, Resources resources) throws PackageManager.NameNotFoundException {
        String QxJ9d_1 = kQgRr5e_0.QxJ9d_1();
        ProviderInfo resolveContentProvider = packageManager.resolveContentProvider(QxJ9d_1, 0);
        if (resolveContentProvider != null) {
            if (resolveContentProvider.packageName.equals(kQgRr5e_0.PGPF6hQ_2())) {
                List<byte[]> KQgRr5e_02 = KQgRr5e_0(packageManager.getPackageInfo(resolveContentProvider.packageName, 64).signatures);
                Collections.sort(KQgRr5e_02, f540KQgRr5e_0);
                List<List<byte[]>> KQgRr5e_03 = KQgRr5e_0(kQgRr5e_0, resources);
                for (int i = 0; i < KQgRr5e_03.size(); i++) {
                    ArrayList arrayList = new ArrayList(KQgRr5e_03.get(i));
                    Collections.sort(arrayList, f540KQgRr5e_0);
                    if (KQgRr5e_0(KQgRr5e_02, arrayList)) {
                        return resolveContentProvider;
                    }
                }
                return null;
            }
            throw new PackageManager.NameNotFoundException("Found content provider " + QxJ9d_1 + ", but package was not " + kQgRr5e_0.PGPF6hQ_2());
        }
        throw new PackageManager.NameNotFoundException("No package found for authority: " + QxJ9d_1);
    }

    public static List<List<byte[]>> KQgRr5e_0(X6FRs_4.IMuAHx_6.XnFJ5_9.KQgRr5e_0 kQgRr5e_0, Resources resources) {
        if (kQgRr5e_0.m345KQgRr5e_0() != null) {
            return kQgRr5e_0.m345KQgRr5e_0();
        }
        return X6FRs_4.IMuAHx_6.X6FRs_4.PGPF6hQ_2.PGPF6hQ_2.KQgRr5e_0(resources, kQgRr5e_0.KQgRr5e_0());
    }

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

    public static List<byte[]> KQgRr5e_0(Signature[] signatureArr) {
        ArrayList arrayList = new ArrayList();
        for (Signature signature : signatureArr) {
            arrayList.add(signature.toByteArray());
        }
        return arrayList;
    }

    /* JADX WARN: Removed duplicated region for block: B:40:0x013a  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static IFxWkV_5[] KQgRr5e_0(Context context, X6FRs_4.IMuAHx_6.XnFJ5_9.KQgRr5e_0 kQgRr5e_0, String str, CancellationSignal cancellationSignal) {
        Cursor cursor;
        ArrayList arrayList;
        String str2;
        String str3;
        int i;
        Cursor query;
        ArrayList arrayList2;
        Uri withAppendedId;
        ArrayList arrayList3 = new ArrayList();
        Uri build = new Uri.Builder().scheme("content").authority(str).build();
        Uri build2 = new Uri.Builder().scheme("content").authority(str).appendPath("file").build();
        try {
            if (Build.VERSION.SDK_INT > 16) {
                arrayList = arrayList3;
                str2 = "file_id";
                query = context.getContentResolver().query(build, new String[]{"_id", "file_id", "font_ttc_index", "font_variation_settings", "font_weight", "font_italic", "result_code"}, "query = ?", new String[]{kQgRr5e_0.Mqv2r_3()}, null, cancellationSignal);
                str3 = "_id";
                i = 1;
            } else {
                arrayList = arrayList3;
                str2 = "file_id";
                str3 = "_id";
                i = 1;
                query = context.getContentResolver().query(build, new String[]{str3, str2, "font_ttc_index", "font_variation_settings", "font_weight", "font_italic", "result_code"}, "query = ?", new String[]{kQgRr5e_0.Mqv2r_3()}, null);
            }
            if (query != null) {
                try {
                    if (query.getCount() > 0) {
                        int columnIndex = query.getColumnIndex("result_code");
                        ArrayList arrayList4 = new ArrayList();
                        int columnIndex2 = query.getColumnIndex(str3);
                        int columnIndex3 = query.getColumnIndex(str2);
                        int columnIndex4 = query.getColumnIndex("font_ttc_index");
                        int columnIndex5 = query.getColumnIndex("font_weight");
                        int columnIndex6 = query.getColumnIndex("font_italic");
                        while (query.moveToNext()) {
                            int i2 = columnIndex != -1 ? query.getInt(columnIndex) : 0;
                            int i3 = columnIndex4 != -1 ? query.getInt(columnIndex4) : 0;
                            if (columnIndex3 == -1) {
                                withAppendedId = ContentUris.withAppendedId(build, query.getLong(columnIndex2));
                            } else {
                                withAppendedId = ContentUris.withAppendedId(build2, query.getLong(columnIndex3));
                            }
                            arrayList4.add(new IFxWkV_5(withAppendedId, i3, columnIndex5 != -1 ? query.getInt(columnIndex5) : 400, columnIndex6 != -1 && query.getInt(columnIndex6) == i, i2));
                        }
                        arrayList2 = arrayList4;
                        if (query != null) {
                            query.close();
                        }
                        return (IFxWkV_5[]) arrayList2.toArray(new IFxWkV_5[0]);
                    }
                } catch (Throwable th) {
                    th = th;
                    cursor = query;
                    if (cursor != null) {
                        cursor.close();
                    }
                    throw th;
                }
            }
            arrayList2 = arrayList;
            if (query != null) {
            }
            return (IFxWkV_5[]) arrayList2.toArray(new IFxWkV_5[0]);
        } catch (Throwable th2) {
            th = th2;
            cursor = null;
        }
    }
}
