package a.c.f;

import a.c.c.b.f;
import a.c.d.i;
import a.c.f.c;
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 com.seewo.libmcuservice.constant.IRemoteConstants;
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 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    static final a.b.d<String, Typeface> f203a = new a.b.d<>(16);

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

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

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

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

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

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

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

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

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

        a(Context context, a.c.f.a aVar, int i, String str) {
            this.f208c = context;
            this.f209d = aVar;
            this.f210e = i;
            this.f211f = 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.f208c, this.f209d, this.f210e);
            Typeface typeface = gVarF.f222a;
            if (typeface != null) {
                b.f203a.d(this.f211f, typeface);
            }
            return gVarF;
        }
    }

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

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

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

        C0008b(f.a aVar, Handler handler) {
            this.f212a = aVar;
            this.f213b = handler;
        }

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

    /* JADX INFO: compiled from: FontsContractCompat.java */
    static class c implements c.d<g> {

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

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

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

    /* JADX INFO: compiled from: FontsContractCompat.java */
    static 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 i = 0; i < bArr.length; i++) {
                    if (bArr[i] != bArr2[i]) {
                        length = bArr[i];
                        length2 = bArr2[i];
                    }
                }
                return 0;
            }
            length = bArr.length;
            length2 = bArr2.length;
            return length - length2;
        }
    }

    /* JADX INFO: compiled from: FontsContractCompat.java */
    public static class e {

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

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

        public e(int i, f[] fVarArr) {
            this.f215a = i;
            this.f216b = fVarArr;
        }

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

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

    /* JADX INFO: compiled from: FontsContractCompat.java */
    public static class f {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final Uri f217a;

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

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

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

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

        public f(Uri uri, int i, int i2, boolean z, int i3) {
            this.f217a = (Uri) a.c.h.b.b(uri);
            this.f218b = i;
            this.f219c = i2;
            this.f220d = z;
            this.f221e = i3;
        }

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

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

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

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

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

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

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final Typeface f222a;

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

        g(Typeface typeface, int i) {
            this.f222a = typeface;
            this.f223b = i;
        }
    }

    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 i = 0; i < list.size(); i++) {
            if (!Arrays.equals(list.get(i), list2.get(i))) {
                return false;
            }
        }
        return true;
    }

    public static e c(Context context, CancellationSignal cancellationSignal, a.c.f.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(a.c.f.a aVar, Resources resources) {
        return aVar.a() != null ? aVar.a() : a.c.c.b.c.c(resources, aVar.b());
    }

    static f[] e(Context context, a.c.f.a aVar, String str, CancellationSignal cancellationSignal) {
        ArrayList arrayList = new ArrayList();
        Uri uriBuild = new Uri.Builder().scheme(IRemoteConstants.KEY_INF_PUBLISH_CONTENT).authority(str).build();
        Uri uriBuild2 = new Uri.Builder().scheme(IRemoteConstants.KEY_INF_PUBLISH_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 i = 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, i));
                }
                arrayList = arrayList2;
            }
            return (f[]) arrayList.toArray(new f[0]);
        } finally {
            if (cursorQuery != null) {
                cursorQuery.close();
            }
        }
    }

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

    public static Typeface g(Context context, a.c.f.a aVar, f.a aVar2, Handler handler, boolean z, int i, int i2) {
        String str = aVar.c() + "-" + i2;
        Typeface typefaceC = f203a.c(str);
        if (typefaceC != null) {
            if (aVar2 != null) {
                aVar2.d(typefaceC);
            }
            return typefaceC;
        }
        if (z && i == -1) {
            g gVarF = f(context, aVar, i2);
            if (aVar2 != null) {
                int i3 = gVarF.f223b;
                if (i3 == 0) {
                    aVar2.b(gVarF.f222a, handler);
                } else {
                    aVar2.a(i3, handler);
                }
            }
            return gVarF.f222a;
        }
        a aVar3 = new a(context, aVar, i2, str);
        if (z) {
            try {
                return ((g) f204b.e(aVar3, i)).f222a;
            } catch (InterruptedException unused) {
                return null;
            }
        }
        C0008b c0008b = aVar2 == null ? null : new C0008b(aVar2, handler);
        synchronized (f205c) {
            a.b.f<String, ArrayList<c.d<g>>> fVar = f206d;
            if (fVar.containsKey(str)) {
                if (c0008b != null) {
                    fVar.get(str).add(c0008b);
                }
                return null;
            }
            if (c0008b != null) {
                ArrayList<c.d<g>> arrayList = new ArrayList<>();
                arrayList.add(c0008b);
                fVar.put(str, arrayList);
            }
            f204b.d(aVar3, new c(str));
            return null;
        }
    }

    public static ProviderInfo h(PackageManager packageManager, a.c.f.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, f207e);
        List<List<byte[]>> listD = d(aVar, resources);
        for (int i = 0; i < listD.size(); i++) {
            ArrayList arrayList = new ArrayList(listD.get(i));
            Collections.sort(arrayList, f207e);
            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, i.f(context, cancellationSignal, uriC));
                }
            }
        }
        return Collections.unmodifiableMap(map);
    }
}
