package com.google.android.gms.ads.nonagon.signalgeneration;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.JsonReader;
import androidx.annotation.Nullable;
import com.google.android.gms.internal.ads.ao;
import com.google.android.gms.internal.ads.l20;
import com.google.android.gms.internal.ads.nj1;
import com.google.android.gms.internal.ads.rj1;
import com.google.android.gms.internal.ads.sj1;
import com.google.android.gms.internal.ads.tn;
import com.google.android.gms.internal.ads.zzdxh;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import n7.a4;
import org.json.JSONException;
import org.json.JSONObject;

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

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

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

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

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

    @Nullable
    private final PackageInfo zze;

    public v(Context context, long j10, @Nullable PackageInfo packageInfo, h0 h0Var, ScheduledExecutorService scheduledExecutorService) {
        this.f8146a = context;
        this.f8148c = j10;
        this.zze = packageInfo;
        this.f8147b = h0Var;
        this.f8149d = scheduledExecutorService;
    }

    public static final void b(Bundle bundle, zzdxh zzdxhVar) {
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.f9118v8)).booleanValue()) {
            com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b.n(m7.n.C.f30472k, bundle, zzdxhVar.a());
        }
    }

    public static final void c(int i10, Bundle bundle) {
        bundle.putBoolean("sod_h", false);
        bundle.putInt("cmr", i10 - 1);
    }

    public static String zzc(@Nullable String str) {
        return str == null ? "" : p7.r0.E(str, "f8L7o2HxjA4p9Z1nQw3E5r6T8yU2iCv0B9kM4sD1f7G3hJ5lK2z0X9cW8vQ6b5N3m1Rg8F2o0Lp7A1e9I4u3Y2t0H8x6W5v4Z1n9Q2w7E3r5T8y6U1i0C9vB8k7M4s3D1f2G0h9J5l8K4z7X3cW2v1Q0b9N8m6A5r4F3o2Lp1E0u9I8y7Y6t5H4x3W2v1Z0n9Q8w7E6r5T4y3U2i1C0v9B8k7M6s5D4f3G2h1J0l9K8z7X6cW5v4Q3b2N1m0Rg9F8o7Lp6A5e4I3u2Y1t0H8x7W6v5Z4n3Q2w1E0r9T8y7U6i5C4v3B2k1M0s9D8f7G6h5J4l3K2z1X0cW9v8Q7b6N5m4A3r2F1o0Lp9E8u7I6y5T4h3W2v1Z0n0Q9w8E7r6T5y4U3i2C1v0B9k8M7s6D5f4G3h2J1l0K9z8X7cW6v5Q4b3N2m1R0g9F8o7L6p5A4e3I2u1Y0t9H8x7W6v5Z4n3Q2w1E0r9T8y7U6i5C4v3B2k1M0s9D8f7G6h5J4l3K2z1X0cW9v8Q7b6N5m4A3r2F1o0Lp9E8u7I6y5T4h3W2");
    }

    public final boolean a() {
        return this.f8147b.b().size() >= ((Integer) n7.y.f31093e.f31096c.b(ao.f9102u8)).intValue();
    }

    /* JADX WARN: Removed duplicated region for block: B:19:0x0079  */
    /* JADX WARN: Removed duplicated region for block: B:32:? A[RETURN, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void zza(@Nullable String str, @Nullable c0 c0Var) {
        String string;
        String str2;
        if (TextUtils.isEmpty(str) || a()) {
            return;
        }
        JSONObject jSONObject = new JSONObject();
        try {
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("params", c0Var.f8004a);
            jSONObject2.put("signal_dictionary", n7.x.f31085g.f31086a.d(c0Var.f8006c));
            jSONObject.put("sr", jSONObject2);
            str2 = c0Var.zzc;
        } catch (JSONException e10) {
            m7.n.C.f30469h.d("DiskCachingManager.createStringToWrite", e10);
        }
        if (TextUtils.isEmpty(str2)) {
            string = "";
            if (TextUtils.isEmpty(string)) {
                h0 h0Var = this.f8147b;
                h0Var.c();
                synchronized (h0Var.f8040d) {
                    h0Var.f8038b.putString(str, string).commit();
                }
                return;
            }
            return;
        }
        jSONObject.put("rs", Base64.encodeToString(zzc(str2).getBytes(StandardCharsets.UTF_8), 10));
        m7.n.C.f30472k.getClass();
        jSONObject.put("ts_ms", System.currentTimeMillis());
        string = jSONObject.toString();
        if (TextUtils.isEmpty(string)) {
        }
    }

    @Nullable
    public final c0 zzb(l20 l20Var, p pVar, Bundle bundle) {
        int i10;
        int i11;
        long j10;
        m7.n nVar;
        long jCurrentTimeMillis;
        tn tnVar;
        n7.y yVar;
        nj1 nj1VarZza;
        b(bundle, zzdxh.SIGNAL_ON_DISK_VALIDATION_START);
        if (m7.n.C.f30469h.g().C()) {
            this.f8147b.a();
            c(7, bundle);
        } else {
            PackageInfo packageInfo = this.zze;
            if (packageInfo != null) {
                h0 h0Var = this.f8147b;
                Context context = this.f8146a;
                String strZze = h0Var.zze();
                h0Var.c();
                synchronized (h0Var.f8040d) {
                    i10 = h0Var.f8037a.getInt("vc", -1);
                }
                String strZzg = h0Var.zzg();
                h0Var.c();
                synchronized (h0Var.f8040d) {
                    i11 = h0Var.f8037a.getInt("aav", -1);
                }
                if (TextUtils.equals(context.getApplicationInfo().packageName, strZze) && i10 == packageInfo.versionCode && TextUtils.equals(Build.MODEL, strZzg) && i11 == Build.VERSION.SDK_INT) {
                    for (Map.Entry entry : h0Var.b().entrySet()) {
                        try {
                            j10 = new JSONObject((String) entry.getValue()).getLong("ts_ms");
                            nVar = m7.n.C;
                            nVar.f30472k.getClass();
                            jCurrentTimeMillis = System.currentTimeMillis() - j10;
                            tnVar = ao.f9085t8;
                            yVar = n7.y.f31093e;
                        } catch (IOException | JSONException unused) {
                        }
                        if (jCurrentTimeMillis <= ((Long) yVar.f31096c.b(tnVar)).longValue()) {
                            rj1 rj1VarE = rj1.e(context);
                            long jLongValue = ((Long) yVar.f31096c.b(ao.T3)).longValue();
                            boolean zC = nVar.f30469h.g().C();
                            rj1VarE.getClass();
                            synchronized (rj1.class) {
                                nj1VarZza = rj1VarE.zza(null, null, jLongValue, zC);
                            }
                            nj1 nj1VarF = sj1.e(context).f(((Long) yVar.f31096c.b(ao.U3)).longValue(), nVar.f30469h.g().C());
                            long j11 = nj1VarZza.f14650b;
                            if (j11 == -1 || j11 <= j10) {
                                long j12 = nj1VarF.f14650b;
                                if (j12 == -1 || j12 <= j10) {
                                }
                            }
                        }
                        h0Var.zzb((String) entry.getKey());
                    }
                } else {
                    h0Var.a();
                    String str = context.getApplicationInfo().packageName;
                    int i12 = packageInfo.versionCode;
                    String str2 = Build.MODEL;
                    int i13 = Build.VERSION.SDK_INT;
                    h0Var.c();
                    synchronized (h0Var.f8040d) {
                        h0Var.f8038b.putString("pn", str).putInt("vc", i12).putString("dm", str2).putInt("aav", i13).commit();
                    }
                }
                b(bundle, zzdxh.SIGNAL_ON_DISK_VALIDATION_END);
                m7.n.C.f30472k.getClass();
                long jCurrentTimeMillis2 = System.currentTimeMillis() - this.f8148c;
                tn tnVar2 = ao.f9036q8;
                n7.y yVar2 = n7.y.f31093e;
                if (jCurrentTimeMillis2 > ((Long) yVar2.f31096c.b(tnVar2)).longValue()) {
                    c(2, bundle);
                    return null;
                }
                b(bundle, zzdxh.SIGNAL_ON_DISK_CACHE_KEY_START);
                String str3 = l20Var.f13475a;
                String str4 = l20Var.f13476b;
                a4 a4Var = l20Var.f13478d;
                String string = a4Var.Q.toString();
                String string2 = a4Var.f30895c.toString();
                String str5 = a4Var.f30901i;
                String str6 = a4Var.S;
                String strValueOf = String.valueOf(a4Var.R);
                int length = String.valueOf(str3).length();
                int length2 = String.valueOf(str4).length();
                int length3 = String.valueOf(string).length();
                int length4 = String.valueOf(string2).length();
                int length5 = String.valueOf(str5).length();
                StringBuilder sb = new StringBuilder(length + length2 + length3 + length4 + length5 + String.valueOf(str6).length() + strValueOf.length());
                u.a.h(sb, str3, str4, string, string2);
                sb.append(str5);
                sb.append(str6);
                sb.append(strValueOf);
                String strZzg2 = q7.e.zzg(sb.toString());
                if (TextUtils.isEmpty(strZzg2)) {
                    c(3, bundle);
                    return null;
                }
                b(bundle, zzdxh.SIGNAL_ON_DISK_CACHE_KEY_END);
                b(bundle, zzdxh.SIGNAL_ON_DISK_READ_AND_REMOVE_START);
                String strZzb = this.f8147b.zzb(strZzg2);
                b(bundle, zzdxh.SIGNAL_ON_DISK_READ_AND_REMOVE_END);
                if (!a()) {
                    l20 l20Var2 = new l20(str3, str4, l20Var.f13477c, a4Var, 2, strZzg2);
                    this.f8149d.schedule(new k.h(this, strZzg2, pVar, l20Var2, 5, 0), ((Long) yVar2.f31096c.b(ao.f9069s8)).longValue(), TimeUnit.MILLISECONDS);
                }
                if (TextUtils.isEmpty(strZzb)) {
                    c(4, bundle);
                    return null;
                }
                b(bundle, zzdxh.SIGNAL_ON_DISK_DECODE_START);
                try {
                    JSONObject jSONObject = new JSONObject(strZzb);
                    String string3 = jSONObject.getString("sr");
                    if (TextUtils.isEmpty(string3)) {
                        c(8, bundle);
                        return null;
                    }
                    String string4 = jSONObject.getString("rs");
                    if (TextUtils.isEmpty(string4)) {
                        c(9, bundle);
                        return null;
                    }
                    String strZzc = zzc(new String(Base64.decode(string4, 10), StandardCharsets.UTF_8));
                    b(bundle, zzdxh.SIGNAL_ON_DISK_DECODE_END);
                    try {
                        c0 c0Var = new c0(new JsonReader(new StringReader(string3)), null);
                        c0Var.zzc = strZzc;
                        c0Var.zze = bundle;
                        bundle.putBoolean("sod_h", true);
                        return c0Var;
                    } catch (IOException e10) {
                        c(6, bundle);
                        m7.n.C.f30469h.d("DiskCachingManager.getSignalResponse", e10);
                        return null;
                    }
                } catch (JSONException e11) {
                    c(5, bundle);
                    m7.n.C.f30469h.d("DiskCachingManager.getSignalResponse", e11);
                    return null;
                }
            }
            this.f8147b.a();
            c(10, bundle);
        }
        return null;
    }
}
