package p7;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import com.google.android.gms.internal.ads.ao;
import com.google.android.gms.internal.ads.ps0;
import com.google.android.gms.internal.ads.tn;
import com.google.android.gms.internal.ads.zzebe;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.json.JSONException;
import org.json.JSONObject;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Object f31715a = new Object();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public String f31716b = "";

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public String f31717c = "";

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public boolean f31718d = false;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f31719e = false;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public String f31720f = "";

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public ps0 f31721g;

    @Nullable
    @VisibleForTesting
    public static final String zzo(Context context, String str, String str2) {
        HashMap map = new HashMap();
        map.put(RtspHeaders.USER_AGENT, m7.n.C.f30464c.A(context, str2));
        com.google.common.util.concurrent.w wVarZzb = new v(context).zzb(0, str, map, null);
        try {
            return (String) wVarZzb.get(((Integer) n7.y.f31093e.f31096c.b(ao.M5)).intValue(), TimeUnit.MILLISECONDS);
        } catch (InterruptedException unused) {
            "Interrupted while retrieving a response from: ".concat(String.valueOf(str));
            wVarZzb.cancel(true);
            return null;
        } catch (TimeoutException unused2) {
            "Timeout while retrieving a response from: ".concat(String.valueOf(str));
            wVarZzb.cancel(true);
            return null;
        } catch (Exception unused3) {
            "Error retrieving a response from: ".concat(String.valueOf(str));
            return null;
        }
    }

    public final void a(Context context) {
        ps0 ps0Var;
        if (!((Boolean) n7.y.f31093e.f31096c.b(ao.f9104ua)).booleanValue() || (ps0Var = this.f31721g) == null) {
            return;
        }
        ps0Var.e(new j(this, context), zzebe.f19669d);
    }

    public final boolean b(Context context, String str, String str2) {
        tn tnVar = ao.K5;
        n7.y yVar = n7.y.f31093e;
        String strZzo = zzo(context, j(context, (String) yVar.f31096c.b(tnVar), str, str2).toString(), str2);
        if (TextUtils.isEmpty(strZzo)) {
            q7.j.a("Not linked for debug signals.");
            return false;
        }
        try {
            boolean zEquals = "1".equals(new JSONObject(strZzo.trim()).optString("debug_mode"));
            f(zEquals);
            if (((Boolean) yVar.f31096c.b(ao.f9104ua)).booleanValue()) {
                o0 o0VarG = m7.n.C.f30469h.g();
                if (true != zEquals) {
                    str = "";
                }
                o0VarG.g(str);
            }
            return zEquals;
        } catch (JSONException unused) {
            return false;
        }
    }

    public final void c(Context context, String str, String str2) {
        r0 r0Var = m7.n.C.f30464c;
        r0.r(context, j(context, (String) n7.y.f31093e.f31096c.b(ao.I5), str, str2));
    }

    public final boolean d(Context context, String str, String str2, String str3) {
        if (TextUtils.isEmpty(str2) || !h()) {
            return false;
        }
        q7.j.a("Sending troubleshooting signals to the server.");
        e(context, str, str2, str3);
        return true;
    }

    public final void e(Context context, String str, String str2, String str3) {
        Uri.Builder builderBuildUpon = j(context, (String) n7.y.f31093e.f31096c.b(ao.L5), str3, str).buildUpon();
        builderBuildUpon.appendQueryParameter("debugData", str2);
        r0 r0Var = m7.n.C.f30464c;
        new a0(context, str, builderBuildUpon.build().toString(), null, null).b();
    }

    /* JADX WARN: Removed duplicated region for block: B:15:0x0036  */
    /* JADX WARN: Removed duplicated region for block: B:16:0x0037 A[Catch: all -> 0x0045, TryCatch #0 {all -> 0x0045, blocks: (B:4:0x0003, B:6:0x0017, B:8:0x0026, B:11:0x002c, B:13:0x0032, B:16:0x0037, B:17:0x003b, B:19:0x0041, B:22:0x0047), top: B:26:0x0003 }] */
    /* JADX WARN: Removed duplicated region for block: B:19:0x0041 A[Catch: all -> 0x0045, TryCatch #0 {all -> 0x0045, blocks: (B:4:0x0003, B:6:0x0017, B:8:0x0026, B:11:0x002c, B:13:0x0032, B:16:0x0037, B:17:0x003b, B:19:0x0041, B:22:0x0047), top: B:26:0x0003 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void f(boolean z10) {
        synchronized (this.f31715a) {
            try {
                this.f31719e = z10;
                if (((Boolean) n7.y.f31093e.f31096c.b(ao.f9104ua)).booleanValue()) {
                    m7.n.C.f30469h.g().f(z10);
                    ps0 ps0Var = this.f31721g;
                    if (ps0Var != null) {
                        if (!ps0Var.f15522u) {
                            if (z10) {
                                ps0Var.j();
                                if (ps0Var.f15520s) {
                                    ps0Var.k();
                                }
                            }
                            if (!ps0Var.f()) {
                                ps0Var.l();
                            }
                        } else if (z10) {
                            if (ps0Var.f15520s) {
                            }
                        } else if (!ps0Var.f()) {
                        }
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean g() {
        boolean z10;
        synchronized (this.f31715a) {
            z10 = this.f31719e;
        }
        return z10;
    }

    public final boolean h() {
        boolean z10;
        synchronized (this.f31715a) {
            z10 = this.f31718d;
        }
        return z10;
    }

    public final void i(String str, Context context, boolean z10, boolean z11) {
        if (context instanceof Activity) {
            r0.f31763l.post(new k(this, context, str, z10, z11));
        } else {
            q7.j.c("Can not create dialog without Activity Context");
        }
    }

    public final Uri j(Context context, String str, String str2, String str3) {
        String str4;
        String str5;
        Uri.Builder builderBuildUpon = Uri.parse(str).buildUpon();
        synchronized (this.f31715a) {
            if (TextUtils.isEmpty(this.f31716b)) {
                r0 r0Var = m7.n.C.f30464c;
                try {
                    FileInputStream fileInputStreamOpenFileInput = context.openFileInput("debug_signals_id.txt");
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    hb.a.e(fileInputStreamOpenFileInput, byteArrayOutputStream, true);
                    str5 = new String(byteArrayOutputStream.toByteArray(), "UTF-8");
                } catch (IOException unused) {
                    q7.j.a("Error reading from internal storage.");
                    str5 = "";
                }
                this.f31716b = str5;
                if (TextUtils.isEmpty(str5)) {
                    r0 r0Var2 = m7.n.C.f30464c;
                    String string = UUID.randomUUID().toString();
                    this.f31716b = string;
                    try {
                        FileOutputStream fileOutputStreamOpenFileOutput = context.openFileOutput("debug_signals_id.txt", 0);
                        fileOutputStreamOpenFileOutput.write(string.getBytes(StandardCharsets.UTF_8));
                        fileOutputStreamOpenFileOutput.close();
                    } catch (Exception unused2) {
                    }
                }
                str4 = this.f31716b;
            } else {
                str4 = this.f31716b;
            }
        }
        builderBuildUpon.appendQueryParameter("linkedDeviceId", str4);
        builderBuildUpon.appendQueryParameter("adSlotPath", str2);
        builderBuildUpon.appendQueryParameter("afmaVersion", str3);
        return builderBuildUpon.build();
    }
}
