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

import android.app.Activity;
import android.content.Context;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.media3.common.PlaybackException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;

/* JADX INFO: loaded from: classes2.dex */
public final class ng implements qg {

    @Nullable
    private static ng zzb;
    public volatile boolean Q;
    public volatile boolean R;

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

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

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final ch1 f14586f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final Executor f14587g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final zzbch f14588h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final ru f14589i;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final th f14591k;

    @Nullable
    private final nh zzn;

    @Nullable
    private final gh zzo;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public volatile long f14592l = 0;

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final CountDownLatch f14590j = new CountDownLatch(1);

    @VisibleForTesting
    public ng(@NonNull Context context, @NonNull ch1 ch1Var, @NonNull ci1 ci1Var, @NonNull gi1 gi1Var, @NonNull ii1 ii1Var, @NonNull hh hhVar, @NonNull Executor executor, @NonNull bh1 bh1Var, zzbch zzbchVar, @Nullable th thVar, @Nullable nh nhVar, @Nullable gh ghVar) {
        this.R = false;
        this.f14581a = context;
        this.f14586f = ch1Var;
        this.f14582b = ci1Var;
        this.f14583c = gi1Var;
        this.f14584d = ii1Var;
        this.f14585e = hhVar;
        this.f14587g = executor;
        this.f14588h = zzbchVar;
        this.f14591k = thVar;
        this.zzn = nhVar;
        this.zzo = ghVar;
        this.R = false;
        this.f14589i = new ru(this, bh1Var);
    }

    public static synchronized ng h(Context context, ExecutorService executorService, gh1 gh1Var, boolean z10) {
        try {
            if (zzb == null) {
                ch1 ch1VarA = ch1.a(context, executorService, z10);
                tn tnVar = ao.f8766a4;
                n7.y yVar = n7.y.f31093e;
                xg xgVarA = ((Boolean) yVar.f31096c.b(tnVar)).booleanValue() ? xg.a(context) : null;
                th thVarA = ((Boolean) yVar.f31096c.b(ao.f8783b4)).booleanValue() ? th.a(context, executorService) : null;
                nh nhVar = ((Boolean) yVar.f31096c.b(ao.f8914j3)).booleanValue() ? new nh() : null;
                gh ghVar = ((Boolean) yVar.f31096c.b(ao.f9064s3)).booleanValue() ? new gh() : null;
                nh1 nh1Var = new nh1(context, executorService, ch1VarA, new mh1());
                w8.t tVarC = w8.k.c(new y61(nh1Var, 3), executorService);
                tVarC.c(executorService, new jc1(nh1Var, 5));
                nh1Var.f14614e = tVarC;
                zzbad zzbadVar = new zzbad(context);
                hh hhVar = new hh(gh1Var, nh1Var, new rh(context, zzbadVar), zzbadVar, xgVarA, thVarA, nhVar, ghVar);
                zzbch zzbchVarT = ih1.T(context, ch1VarA);
                bh1 bh1Var = new bh1();
                ng ngVar = new ng(context, ch1VarA, new ci1(context, zzbchVarT), new gi1(context, zzbchVarT, new ru(ch1VarA, 15), ((Boolean) yVar.f31096c.b(ao.T2)).booleanValue()), new ii1(context, hhVar, ch1VarA, bh1Var, false), hhVar, executorService, bh1Var, zzbchVarT, thVarA, nhVar, ghVar);
                zzb = ngVar;
                ngVar.e();
                zzb.f();
            }
        } catch (Throwable th) {
            throw th;
        }
        return zzb;
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final void a(int i10, int i11, int i12) {
        DisplayMetrics displayMetrics;
        if (!((Boolean) n7.y.f31093e.f31096c.b(ao.f8908id)).booleanValue() || (displayMetrics = this.f14581a.getResources().getDisplayMetrics()) == null) {
            return;
        }
        float f10 = i10;
        float f11 = displayMetrics.density;
        float f12 = i11;
        MotionEvent motionEventObtain = MotionEvent.obtain(0L, 0L, 0, f10 * f11, f12 * f11, 0.0f, 0.0f, 0, 0.0f, 0.0f, 0, 0);
        zzd(motionEventObtain);
        motionEventObtain.recycle();
        float f13 = displayMetrics.density;
        MotionEvent motionEventObtain2 = MotionEvent.obtain(0L, 0L, 2, f10 * f13, f12 * f13, 0.0f, 0.0f, 0, 0.0f, 0.0f, 0, 0);
        zzd(motionEventObtain2);
        motionEventObtain2.recycle();
        float f14 = displayMetrics.density;
        MotionEvent motionEventObtain3 = MotionEvent.obtain(0L, i12, 1, f10 * f14, f12 * f14, 0.0f, 0.0f, 0, 0.0f, 0.0f, 0, 0);
        zzd(motionEventObtain3);
        motionEventObtain3.recycle();
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final String b(Context context) {
        return "19";
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final void c(StackTraceElement[] stackTraceElementArr) {
        gh ghVar = this.zzo;
        if (ghVar != null) {
            ghVar.f11743a = new ArrayList(Arrays.asList(stackTraceElementArr));
        }
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final String d(Context context) {
        th thVar = this.f14591k;
        if (thVar != null && thVar.f16898d) {
            thVar.f16896b = System.currentTimeMillis();
        }
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.f8914j3)).booleanValue()) {
            nh nhVar = this.zzn;
            nhVar.f14600b = nhVar.f14599a;
            nhVar.f14599a = SystemClock.uptimeMillis();
        }
        f();
        eh1 eh1VarZzb = this.f14584d.zzb();
        if (eh1VarZzb == null) {
            return "";
        }
        long jCurrentTimeMillis = System.currentTimeMillis();
        String strZza = ((ai1) eh1VarZzb).zza(context, null);
        this.f14586f.e(PlaybackException.ERROR_CODE_AUDIO_TRACK_INIT_FAILED, System.currentTimeMillis() - jCurrentTimeMillis, null, strZza, null);
        return strZza;
    }

    public final synchronized void e() {
        long jCurrentTimeMillis = System.currentTimeMillis();
        bi1 bi1VarI = i();
        if (bi1VarI == null) {
            this.f14586f.b(4013, System.currentTimeMillis() - jCurrentTimeMillis);
        } else if (this.f14584d.a(bi1VarI)) {
            this.R = true;
            this.f14590j.countDown();
        }
    }

    public final void f() {
        if (this.Q) {
            return;
        }
        synchronized (this.f14593m) {
            try {
                if (!this.Q) {
                    if ((System.currentTimeMillis() / 1000) - this.f14592l < 3600) {
                        return;
                    }
                    bi1 bi1VarZzc = this.f14584d.zzc();
                    if ((bi1VarZzc == null || bi1VarZzc.f9546a.D() - (System.currentTimeMillis() / 1000) < 3600) && ih1.A(this.f14588h)) {
                        this.f14587g.execute(new mv1(this));
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void g() {
        String strB;
        String strC;
        int length;
        bi1 bi1VarI;
        wi wiVar;
        boolean zZza;
        long jCurrentTimeMillis = System.currentTimeMillis();
        bi1 bi1VarI2 = i();
        if (bi1VarI2 != null) {
            strB = bi1VarI2.f9546a.B();
            strC = bi1VarI2.f9546a.C();
        } else {
            strB = null;
            strC = null;
        }
        try {
            try {
                Context context = this.f14581a;
                zzbch zzbchVar = this.f14588h;
                ch1 ch1Var = this.f14586f;
                ei1 ei1VarH = ih1.h(context, zzbchVar, strB, strC, ch1Var);
                byte[] bArr = ei1VarH.f10801b;
                if (bArr == null || (length = bArr.length) == 0) {
                    ch1Var.b(5009, System.currentTimeMillis() - jCurrentTimeMillis);
                } else {
                    try {
                        zzian zzianVarW = zzian.w(0, length, bArr);
                        ua2 ua2Var = ua2.f17195b;
                        int i10 = fa2.f11138a;
                        si siVarE = si.E(zzianVarW, ua2.f17196c);
                        if (siVarE.B().B().isEmpty() || siVarE.B().C().isEmpty() || siVarE.D().z().length == 0 || ((bi1VarI = i()) != null && (wiVar = bi1VarI.f9546a) != null && siVarE.B().B().equals(wiVar.B()) && siVarE.B().C().equals(wiVar.C()))) {
                            this.f14586f.b(5010, System.currentTimeMillis() - jCurrentTimeMillis);
                        } else {
                            ru ruVar = this.f14589i;
                            int i11 = ei1VarH.f10802c;
                            if (!((Boolean) n7.y.f31093e.f31096c.b(ao.R2)).booleanValue()) {
                                zZza = this.f14582b.zza(siVarE, ruVar);
                            } else if (i11 == 3) {
                                zZza = this.f14583c.a(siVarE);
                            } else {
                                if (i11 == 4) {
                                    zZza = this.f14583c.zza(siVarE, ruVar);
                                }
                                this.f14586f.b(4009, System.currentTimeMillis() - jCurrentTimeMillis);
                            }
                            if (zZza) {
                                bi1 bi1VarI3 = i();
                                if (bi1VarI3 != null) {
                                    if (this.f14584d.a(bi1VarI3)) {
                                        this.R = true;
                                    }
                                    this.f14592l = System.currentTimeMillis() / 1000;
                                }
                            } else {
                                this.f14586f.b(4009, System.currentTimeMillis() - jCurrentTimeMillis);
                            }
                        }
                    } catch (NullPointerException unused) {
                        this.f14586f.b(2030, System.currentTimeMillis() - jCurrentTimeMillis);
                    }
                }
            } catch (Throwable th) {
                this.f14590j.countDown();
                throw th;
            }
        } catch (zzicg e10) {
            this.f14586f.c(PlaybackException.ERROR_CODE_DECODER_QUERY_FAILED, System.currentTimeMillis() - jCurrentTimeMillis, e10);
        }
        this.f14590j.countDown();
    }

    public final bi1 i() {
        if (ih1.A(this.f14588h)) {
            return ((Boolean) n7.y.f31093e.f31096c.b(ao.R2)).booleanValue() ? this.f14583c.zzc(1) : this.f14582b.zzb(1);
        }
        return null;
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final void zzd(@Nullable MotionEvent motionEvent) {
        eh1 eh1VarZzb = this.f14584d.zzb();
        if (eh1VarZzb != null) {
            try {
                ((ai1) eh1VarZzb).a(motionEvent);
            } catch (zzfwz e10) {
                this.f14586f.c(e10.a(), -1L, e10);
            }
        }
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final String zzf(Context context, String str, @Nullable View view, @Nullable Activity activity) {
        th thVar = this.f14591k;
        if (thVar != null && thVar.f16898d) {
            thVar.f16896b = System.currentTimeMillis();
        }
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.f8914j3)).booleanValue()) {
            nh nhVar = this.zzn;
            nhVar.f14606h = nhVar.f14605g;
            nhVar.f14605g = SystemClock.uptimeMillis();
        }
        f();
        eh1 eh1VarZzb = this.f14584d.zzb();
        if (eh1VarZzb == null) {
            return "";
        }
        long jCurrentTimeMillis = System.currentTimeMillis();
        String strZzc = ((ai1) eh1VarZzb).zzc(context, null, str, view, activity);
        this.f14586f.e(5000, System.currentTimeMillis() - jCurrentTimeMillis, null, strZzc, null);
        return strZzc;
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final String zzg(Context context, @Nullable String str, @Nullable View view) {
        return zzf(context, str, view, null);
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final void zzh(@Nullable View view) {
        this.f14585e.f12158c.a(view);
    }

    @Override // com.google.android.gms.internal.ads.qg
    public final String zzj(Context context, @Nullable View view, @Nullable Activity activity) {
        th thVar = this.f14591k;
        if (thVar != null && thVar.f16898d) {
            thVar.f16896b = System.currentTimeMillis();
        }
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.f8914j3)).booleanValue()) {
            this.zzn.a(context, view);
        }
        f();
        eh1 eh1VarZzb = this.f14584d.zzb();
        if (eh1VarZzb == null) {
            return "";
        }
        long jCurrentTimeMillis = System.currentTimeMillis();
        String strZzb = ((ai1) eh1VarZzb).zzb(context, null, view, activity);
        this.f14586f.e(PlaybackException.ERROR_CODE_AUDIO_TRACK_WRITE_FAILED, System.currentTimeMillis() - jCurrentTimeMillis, null, strZzb, null);
        return strZzb;
    }
}
