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

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import androidx.annotation.Nullable;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public qj f16459b = null;

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

    public final void a(Context context) {
        synchronized (this.f16458a) {
            try {
                if (!this.f16460c) {
                    Context applicationContext = context.getApplicationContext();
                    if (applicationContext == null) {
                        applicationContext = context;
                    }
                    Application application = applicationContext instanceof Application ? (Application) applicationContext : null;
                    if (application == null) {
                        q7.j.d("Can not cast Context to Application");
                        return;
                    }
                    if (this.f16459b == null) {
                        this.f16459b = new qj();
                    }
                    qj qjVar = this.f16459b;
                    if (!qjVar.f15757h) {
                        application.registerActivityLifecycleCallbacks(qjVar);
                        if (context instanceof Activity) {
                            qjVar.a((Activity) context);
                        }
                        qjVar.f15750a = application;
                        qjVar.f15758i = ((Long) n7.y.f31093e.f31096c.b(ao.f9046r1)).longValue();
                        qjVar.f15757h = true;
                    }
                    this.f16460c = true;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void b(rj rjVar) {
        synchronized (this.f16458a) {
            try {
                if (this.f16459b == null) {
                    this.f16459b = new qj();
                }
                qj qjVar = this.f16459b;
                synchronized (qjVar.f15751b) {
                    qjVar.f15754e.add(rjVar);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void c(rj rjVar) {
        synchronized (this.f16458a) {
            try {
                qj qjVar = this.f16459b;
                if (qjVar == null) {
                    return;
                }
                synchronized (qjVar.f15751b) {
                    qjVar.f15754e.remove(rjVar);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean d() {
        synchronized (this.f16458a) {
            try {
                qj qjVar = this.f16459b;
                if (qjVar == null) {
                    return false;
                }
                return qjVar.f15752c.get();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Nullable
    public final Activity zzd() {
        synchronized (this.f16458a) {
            try {
                qj qjVar = this.f16459b;
                if (qjVar == null) {
                    return null;
                }
                return qjVar.zzd();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Nullable
    public final Context zze() {
        synchronized (this.f16458a) {
            try {
                qj qjVar = this.f16459b;
                if (qjVar == null) {
                    return null;
                }
                return qjVar.zze();
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
