package c.f.i;

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 c.f.d.c.f;
import c.f.e.k;
import c.f.i.c;
import com.seewo.vcommons.executor.ButtonActionExecutor;
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;

/* JADX INFO: compiled from: FontsContractCompat.java */
/* JADX INFO: loaded from: classes.dex */
public class b {
    static final c.d.e<String, Typeface> a = new c.d.e<>(16);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static final c.f.i.c f1402b = new c.f.i.c("fonts", 10, 10000);

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    static final Object f1403c = new Object();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    static final c.d.g<String, ArrayList<c.d<g>>> f1404d = new c.d.g<>();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private static final Comparator<byte[]> f1405e = new d();

    /* JADX INFO: compiled from: FontsContractCompat.java */
    class a implements Callable<g> {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ Context f1406b;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        final /* synthetic */ c.f.i.a f1407f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        final /* synthetic */ int f1408g;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        final /* synthetic */ String f1409h;

        a(Context context, c.f.i.a aVar, int i2, String str) {
            this.f1406b = context;
            this.f1407f = aVar;
            this.f1408g = i2;
            this.f1409h = str;
        }

        @Override // java.util.concurrent.Callable
        /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
        public g call() throws Exception {
            g gVarF = b.f(this.f1406b, this.f1407f, this.f1408g);
            Typeface typeface = gVarF.a;
            if (typeface != null) {
                b.a.d(this.f1409h, typeface);
            }
            return gVarF;
        }
    }

    /* JADX INFO: renamed from: c.f.i.b$b, reason: collision with other inner class name */
    /* JADX INFO: compiled from: FontsContractCompat.java */
    class C0028b implements c.d<g> {
        final /* synthetic */ f.a a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ Handler f1410b;

        C0028b(f.a aVar, Handler handler) {
            this.a = aVar;
            this.f1410b = handler;
        }

        @Override // c.f.i.c.d
        /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
        public void a(g gVar) {
            if (gVar == null) {
                this.a.a(1, this.f1410b);
                return;
            }
            int i2 = gVar.f1416b;
            if (i2 == 0) {
                this.a.b(gVar.a, this.f1410b);
            } else {
                this.a.a(i2, this.f1410b);
            }
        }
    }

    /* JADX INFO: compiled from: FontsContractCompat.java */
    class c implements c.d<g> {
        final /* synthetic */ String a;

        c(String str) {
            this.a = str;
        }

        @Override // c.f.i.c.d
        /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
        public void a(g gVar) {
            synchronized (b.f1403c) {
                ArrayList<c.d<g>> arrayList = b.f1404d.get(this.a);
                if (arrayList == null) {
                    return;
                }
                b.f1404d.remove(this.a);
                for (int i2 = 0; i2 < arrayList.size(); i2++) {
                    arrayList.get(i2).a(gVar);
                }
            }
        }
    }

    /* JADX INFO: compiled from: FontsContractCompat.java */
    class d implements Comparator<byte[]> {
        d() {
        }

        @Override // java.util.Comparator
        /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
        public int compare(byte[] bArr, byte[] bArr2) {
            int length;
            int length2;
            if (bArr.length == bArr2.length) {
                for (int i2 = 0; i2 < bArr.length; i2++) {
                    if (bArr[i2] != bArr2[i2]) {
                        length = bArr[i2];
                        length2 = bArr2[i2];
                    }
                }
                return 0;
            }
            length = bArr.length;
            length2 = bArr2.length;
            return length - length2;
        }
    }

    /* JADX INFO: compiled from: FontsContractCompat.java */
    public static class e {
        private final int a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final f[] f1411b;

        public e(int i2, f[] fVarArr) {
            this.a = i2;
            this.f1411b = fVarArr;
        }

        public f[] a() {
            return this.f1411b;
        }

        public int b() {
            return this.a;
        }
    }

    /* JADX INFO: compiled from: FontsContractCompat.java */
    public static class f {
        private final Uri a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final int f1412b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private final int f1413c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private final boolean f1414d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private final int f1415e;

        public f(Uri uri, int i2, int i3, boolean z, int i4) {
            c.f.k.f.b(uri);
            this.a = uri;
            this.f1412b = i2;
            this.f1413c = i3;
            this.f1414d = z;
            this.f1415e = i4;
        }

        public int a() {
            return this.f1415e;
        }

        public int b() {
            return this.f1412b;
        }

        public Uri c() {
            return this.a;
        }

        public int d() {
            return this.f1413c;
        }

        public boolean e() {
            return this.f1414d;
        }
    }

    /* JADX INFO: compiled from: FontsContractCompat.java */
    private static final class g {
        final Typeface a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final int f1416b;

        g(Typeface typeface, int i2) {
            this.a = typeface;
            this.f1416b = i2;
        }
    }

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

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

    public static e c(Context context, CancellationSignal cancellationSignal, c.f.i.a aVar) throws PackageManager.NameNotFoundException {
        ProviderInfo providerInfoH = h(context.getPackageManager(), aVar, context.getResources());
        return providerInfoH == null ? new e(1, null) : new e(0, e(context, aVar, providerInfoH.authority, cancellationSignal));
    }

    private static List<List<byte[]>> d(c.f.i.a aVar, Resources resources) {
        return aVar.a() != null ? aVar.a() : c.f.d.c.c.c(resources, aVar.b());
    }

    static f[] e(Context context, c.f.i.a aVar, String str, CancellationSignal cancellationSignal) {
        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();
        Cursor cursorQuery = null;
        try {
            cursorQuery = Build.VERSION.SDK_INT > 16 ? context.getContentResolver().query(uriBuild, new String[]{"_id", "file_id", "font_ttc_index", "font_variation_settings", "font_weight", "font_italic", "result_code"}, "query = ?", new String[]{aVar.f()}, null, cancellationSignal) : context.getContentResolver().query(uriBuild, new String[]{"_id", "file_id", "font_ttc_index", "font_variation_settings", "font_weight", "font_italic", "result_code"}, "query = ?", new String[]{aVar.f()}, null);
            if (cursorQuery != null && cursorQuery.getCount() > 0) {
                int columnIndex = cursorQuery.getColumnIndex("result_code");
                ArrayList arrayList2 = new ArrayList();
                int columnIndex2 = cursorQuery.getColumnIndex("_id");
                int columnIndex3 = cursorQuery.getColumnIndex("file_id");
                int columnIndex4 = cursorQuery.getColumnIndex("font_ttc_index");
                int columnIndex5 = cursorQuery.getColumnIndex("font_weight");
                int columnIndex6 = cursorQuery.getColumnIndex("font_italic");
                while (cursorQuery.moveToNext()) {
                    int i2 = columnIndex != -1 ? cursorQuery.getInt(columnIndex) : 0;
                    arrayList2.add(new f(columnIndex3 == -1 ? ContentUris.withAppendedId(uriBuild, cursorQuery.getLong(columnIndex2)) : ContentUris.withAppendedId(uriBuild2, cursorQuery.getLong(columnIndex3)), columnIndex4 != -1 ? cursorQuery.getInt(columnIndex4) : 0, columnIndex5 != -1 ? cursorQuery.getInt(columnIndex5) : ButtonActionExecutor.TIME_PAGE_CHANGED_DLEAYED, columnIndex6 != -1 && cursorQuery.getInt(columnIndex6) == 1, i2));
                }
                arrayList = arrayList2;
            }
            return (f[]) arrayList.toArray(new f[0]);
        } finally {
            if (cursorQuery != null) {
                cursorQuery.close();
            }
        }
    }

    static g f(Context context, c.f.i.a aVar, int i2) {
        try {
            e eVarC = c(context, null, aVar);
            if (eVarC.b() != 0) {
                return new g(null, eVarC.b() == 1 ? -2 : -3);
            }
            Typeface typefaceA = c.f.e.d.a(context, null, eVarC.a(), i2);
            return new g(typefaceA, typefaceA != null ? 0 : -3);
        } catch (PackageManager.NameNotFoundException unused) {
            return new g(null, -1);
        }
    }

    public static Typeface g(Context context, c.f.i.a aVar, f.a aVar2, Handler handler, boolean z, int i2, int i3) {
        String str = aVar.c() + "-" + i3;
        Typeface typefaceC = a.c(str);
        if (typefaceC != null) {
            if (aVar2 != null) {
                aVar2.d(typefaceC);
            }
            return typefaceC;
        }
        if (z && i2 == -1) {
            g gVarF = f(context, aVar, i3);
            if (aVar2 != null) {
                int i4 = gVarF.f1416b;
                if (i4 == 0) {
                    aVar2.b(gVarF.a, handler);
                } else {
                    aVar2.a(i4, handler);
                }
            }
            return gVarF.a;
        }
        a aVar3 = new a(context, aVar, i3, str);
        if (z) {
            try {
                return ((g) f1402b.e(aVar3, i2)).a;
            } catch (InterruptedException unused) {
                return null;
            }
        }
        C0028b c0028b = aVar2 == null ? null : new C0028b(aVar2, handler);
        synchronized (f1403c) {
            ArrayList<c.d<g>> arrayList = f1404d.get(str);
            if (arrayList != null) {
                if (c0028b != null) {
                    arrayList.add(c0028b);
                }
                return null;
            }
            if (c0028b != null) {
                ArrayList<c.d<g>> arrayList2 = new ArrayList<>();
                arrayList2.add(c0028b);
                f1404d.put(str, arrayList2);
            }
            f1402b.d(aVar3, new c(str));
            return null;
        }
    }

    public static ProviderInfo h(PackageManager packageManager, c.f.i.a aVar, Resources resources) throws PackageManager.NameNotFoundException {
        String strD = aVar.d();
        ProviderInfo providerInfoResolveContentProvider = packageManager.resolveContentProvider(strD, 0);
        if (providerInfoResolveContentProvider == null) {
            throw new PackageManager.NameNotFoundException("No package found for authority: " + strD);
        }
        if (!providerInfoResolveContentProvider.packageName.equals(aVar.e())) {
            throw new PackageManager.NameNotFoundException("Found content provider " + strD + ", but package was not " + aVar.e());
        }
        List<byte[]> listA = a(packageManager.getPackageInfo(providerInfoResolveContentProvider.packageName, 64).signatures);
        Collections.sort(listA, f1405e);
        List<List<byte[]>> listD = d(aVar, resources);
        for (int i2 = 0; i2 < listD.size(); i2++) {
            ArrayList arrayList = new ArrayList(listD.get(i2));
            Collections.sort(arrayList, f1405e);
            if (b(listA, arrayList)) {
                return providerInfoResolveContentProvider;
            }
        }
        return null;
    }

    public static Map<Uri, ByteBuffer> i(Context context, f[] fVarArr, CancellationSignal cancellationSignal) {
        HashMap map = new HashMap();
        for (f fVar : fVarArr) {
            if (fVar.a() == 0) {
                Uri uriC = fVar.c();
                if (!map.containsKey(uriC)) {
                    map.put(uriC, k.f(context, cancellationSignal, uriC));
                }
            }
        }
        return Collections.unmodifiableMap(map);
    }
}
