package com.google.android.gms.internal.ads;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import java.io.File;
import java.util.HashSet;

/* JADX INFO: loaded from: classes2.dex */
public final class un1 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final File f17340a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final File f17341b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final SharedPreferences f17342c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final gg2 f17343d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final jp1 f17344e;

    public un1(Context context, SharedPreferences sharedPreferences, gg2 gg2Var, jp1 jp1Var) {
        this.f17342c = sharedPreferences;
        File dir = context.getDir("pccache2", 0);
        ih1.E0(dir, false);
        this.f17340a = dir;
        File dir2 = context.getDir("tmppccache2", 0);
        ih1.E0(dir2, true);
        this.f17341b = dir2;
        this.f17343d = gg2Var;
        this.f17344e = jp1Var;
    }

    public final void a(ql1 ql1Var, byte[] bArr, byte[] bArr2) {
        String strB = ql1Var.B().B();
        boolean zIsEmpty = TextUtils.isEmpty(strB);
        jp1 jp1Var = this.f17344e;
        if (!zIsEmpty && bArr2.length != 0) {
            File file = this.f17341b;
            ih1.M0(file);
            file.mkdirs();
            File fileR0 = ih1.r0(strB, file);
            fileR0.getClass();
            fileR0.mkdirs();
            File fileN = ih1.n(strB, "pcam.jar", file);
            fileN.getClass();
            if (bArr == null || bArr.length <= 0 || ih1.h0(fileN, bArr)) {
                File fileN2 = ih1.n(strB, "pcbc", file);
                fileN2.getClass();
                if (ih1.h0(fileN2, bArr2)) {
                    String strB2 = ql1Var.B().B();
                    if (!TextUtils.isEmpty(strB2)) {
                        File fileN3 = ih1.n(strB2, "pcam.jar", file);
                        fileN3.getClass();
                        File fileN4 = ih1.n(strB2, "pcbc", file);
                        fileN4.getClass();
                        File fileN5 = ih1.n(strB2, "pcam.jar", c());
                        fileN5.getClass();
                        File fileN6 = ih1.n(strB2, "pcbc", c());
                        fileN6.getClass();
                        if (fileN3.exists() && !fileN3.renameTo(fileN5)) {
                            jp1Var.b(15318);
                        } else if (fileN4.exists() && fileN4.renameTo(fileN6)) {
                            ql1 ql1VarB = b(1);
                            SharedPreferences.Editor editorEdit = this.f17342c.edit();
                            if (ql1VarB != null && !ql1Var.B().B().equals(ql1VarB.B().B())) {
                                editorEdit.putString(d(), i8.f.a(ql1VarB.b()));
                            }
                            editorEdit.putString(e(), i8.f.a(ql1Var.b()));
                            if (!editorEdit.commit()) {
                                jp1Var.b(15320);
                            }
                        } else {
                            jp1Var.b(15319);
                        }
                    }
                    HashSet hashSet = new HashSet();
                    ql1 ql1VarB2 = b(1);
                    if (ql1VarB2 != null) {
                        hashSet.add(ql1VarB2.B().B());
                    }
                    ql1 ql1VarB3 = b(2);
                    if (ql1VarB3 != null) {
                        hashSet.add(ql1VarB3.B().B());
                    }
                    File[] fileArrListFiles = c().listFiles();
                    if (fileArrListFiles != null) {
                        for (File file2 : fileArrListFiles) {
                            String name = file2.getName();
                            if (!hashSet.contains(name)) {
                                File fileR02 = ih1.r0(name, c());
                                fileR02.getClass();
                                ih1.M0(fileR02);
                            }
                        }
                        return;
                    }
                    return;
                }
            }
        }
        jp1Var.b(15316);
    }

    public final ql1 b(int i10) {
        SharedPreferences sharedPreferences = this.f17342c;
        String string = i10 == 1 ? sharedPreferences.getString(e(), null) : sharedPreferences.getString(d(), null);
        if (TextUtils.isEmpty(string)) {
            return null;
        }
        try {
            byte[] bArrB = i8.f.b(string);
            ql1 ql1VarE = ql1.E(zzian.w(0, bArrB.length, bArrB));
            String strB = ql1VarE.B().B();
            File fileN = ih1.n(strB, "pcam.jar", c());
            fileN.getClass();
            if (!fileN.exists()) {
                fileN = ih1.n(strB, "pcam", c());
                fileN.getClass();
            }
            File fileN2 = ih1.n(strB, "pcbc", c());
            fileN2.getClass();
            if (fileN.exists()) {
                if (fileN2.exists()) {
                    return ql1VarE;
                }
            }
        } catch (zzicg unused) {
            this.f17344e.b(15317);
        }
        return null;
    }

    public final File c() {
        File file = new File(this.f17340a, Integer.toString(((zzbch) this.f17343d.zzb()).zza()));
        if (!file.exists()) {
            file.mkdir();
        }
        return file;
    }

    public final String d() {
        int iZza = ((zzbch) this.f17343d.zzb()).zza();
        return defpackage.e.n(new StringBuilder(String.valueOf(iZza).length() + 6), "FBAMTD", iZza);
    }

    public final String e() {
        int iZza = ((zzbch) this.f17343d.zzb()).zza();
        return defpackage.e.n(new StringBuilder(String.valueOf(iZza).length() + 6), "LATMTD", iZza);
    }
}
