package com.google.firebase.messaging;

import android.content.Context;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.PowerManager;
import java.io.IOException;

/* JADX INFO: loaded from: classes2.dex */
public final class l0 implements Runnable {

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static Boolean f24210g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static Boolean f24211h;

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final PowerManager.WakeLock f24214c;

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

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

    public l0(j0 j0Var, Context context, s sVar, long j10) {
        this.f24215d = j0Var;
        this.f24212a = context;
        this.f24216e = j10;
        this.f24213b = sVar;
        this.f24214c = ((PowerManager) context.getSystemService("power")).newWakeLock(1, "wake:com.google.firebase.messaging");
    }

    public static boolean a(Context context) {
        boolean zBooleanValue;
        synchronized (f24209f) {
            try {
                Boolean bool = f24211h;
                boolean zBooleanValue2 = (bool == null && bool == null) ? context.checkCallingOrSelfPermission("android.permission.ACCESS_NETWORK_STATE") == 0 : bool.booleanValue();
                Boolean boolValueOf = Boolean.valueOf(zBooleanValue2);
                f24211h = boolValueOf;
                zBooleanValue = boolValueOf.booleanValue();
            } catch (Throwable th) {
                throw th;
            }
        }
        return zBooleanValue;
    }

    public static boolean b(Context context) {
        boolean zBooleanValue;
        synchronized (f24209f) {
            try {
                Boolean bool = f24210g;
                boolean zBooleanValue2 = (bool == null && bool == null) ? context.checkCallingOrSelfPermission("android.permission.WAKE_LOCK") == 0 : bool.booleanValue();
                Boolean boolValueOf = Boolean.valueOf(zBooleanValue2);
                f24210g = boolValueOf;
                zBooleanValue = boolValueOf.booleanValue();
            } catch (Throwable th) {
                throw th;
            }
        }
        return zBooleanValue;
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x001f  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final synchronized boolean c() {
        boolean z10;
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) this.f24212a.getSystemService("connectivity");
            NetworkInfo activeNetworkInfo = connectivityManager != null ? connectivityManager.getActiveNetworkInfo() : null;
            if (activeNetworkInfo != null) {
                z10 = activeNetworkInfo.isConnected();
            }
        } catch (Throwable th) {
            throw th;
        }
        return z10;
    }

    @Override // java.lang.Runnable
    public final void run() {
        j0 j0Var = this.f24215d;
        Context context = this.f24212a;
        boolean zB = b(context);
        PowerManager.WakeLock wakeLock = this.f24214c;
        if (zB) {
            wakeLock.acquire(g.f24172a);
        }
        try {
            try {
                synchronized (j0Var) {
                    j0Var.f24203g = true;
                }
            } catch (IOException e10) {
                e10.getMessage();
                synchronized (j0Var) {
                    j0Var.f24203g = false;
                    if (!b(context)) {
                        return;
                    }
                }
            }
            if (!this.f24213b.c()) {
                synchronized (j0Var) {
                    j0Var.f24203g = false;
                }
                if (b(context)) {
                    try {
                        wakeLock.release();
                        return;
                    } catch (RuntimeException unused) {
                        return;
                    }
                }
                return;
            }
            if (a(context) && !c()) {
                context.registerReceiver(new k0(this, this), new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
                if (b(context)) {
                    try {
                        wakeLock.release();
                        return;
                    } catch (RuntimeException unused2) {
                        return;
                    }
                }
                return;
            }
            if (j0Var.d()) {
                synchronized (j0Var) {
                    j0Var.f24203g = false;
                }
            } else {
                j0Var.e(this.f24216e);
            }
            if (!b(context)) {
                return;
            }
            try {
                wakeLock.release();
            } catch (RuntimeException unused3) {
            }
        } catch (Throwable th) {
            if (b(context)) {
                try {
                    wakeLock.release();
                } catch (RuntimeException unused4) {
                }
            }
            throw th;
        }
    }
}
