package androidx.appcompat.app;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;
import java.util.Calendar;

/* JADX INFO: compiled from: TwilightManager.java */
/* JADX INFO: loaded from: classes.dex */
class m {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static m f277d;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final LocationManager f279b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final a f280c = new a();

    /* JADX INFO: compiled from: TwilightManager.java */
    private static class a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        boolean f281a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        long f282b;

        a() {
        }
    }

    m(Context context, LocationManager locationManager) {
        this.f278a = context;
        this.f279b = locationManager;
    }

    static m a(Context context) {
        if (f277d == null) {
            Context applicationContext = context.getApplicationContext();
            f277d = new m(applicationContext, (LocationManager) applicationContext.getSystemService("location"));
        }
        return f277d;
    }

    @SuppressLint({"MissingPermission"})
    private Location b() {
        Location locationC = androidx.core.content.b.b(this.f278a, "android.permission.ACCESS_COARSE_LOCATION") == 0 ? c("network") : null;
        Location locationC2 = androidx.core.content.b.b(this.f278a, "android.permission.ACCESS_FINE_LOCATION") == 0 ? c("gps") : null;
        return (locationC2 == null || locationC == null) ? locationC2 != null ? locationC2 : locationC : locationC2.getTime() > locationC.getTime() ? locationC2 : locationC;
    }

    private Location c(String str) {
        try {
            if (this.f279b.isProviderEnabled(str)) {
                return this.f279b.getLastKnownLocation(str);
            }
            return null;
        } catch (Exception e3) {
            Log.d("TwilightManager", "Failed to get last known location", e3);
            return null;
        }
    }

    private boolean e() {
        return this.f280c.f282b > System.currentTimeMillis();
    }

    private void f(Location location) {
        long j3;
        a aVar = this.f280c;
        long jCurrentTimeMillis = System.currentTimeMillis();
        l lVarB = l.b();
        lVarB.a(jCurrentTimeMillis - 86400000, location.getLatitude(), location.getLongitude());
        lVarB.a(jCurrentTimeMillis, location.getLatitude(), location.getLongitude());
        boolean z2 = lVarB.f276c == 1;
        long j4 = lVarB.f275b;
        long j5 = lVarB.f274a;
        lVarB.a(jCurrentTimeMillis + 86400000, location.getLatitude(), location.getLongitude());
        long j6 = lVarB.f275b;
        if (j4 == -1 || j5 == -1) {
            j3 = 43200000 + jCurrentTimeMillis;
        } else {
            j3 = (jCurrentTimeMillis > j5 ? j6 + 0 : jCurrentTimeMillis > j4 ? j5 + 0 : j4 + 0) + 60000;
        }
        aVar.f281a = z2;
        aVar.f282b = j3;
    }

    boolean d() {
        a aVar = this.f280c;
        if (e()) {
            return aVar.f281a;
        }
        Location locationB = b();
        if (locationB != null) {
            f(locationB);
            return aVar.f281a;
        }
        Log.i("TwilightManager", "Could not get last known location. This is probably because the app does not have any location permissions. Falling back to hardcoded sunrise/sunset values.");
        int i3 = Calendar.getInstance().get(11);
        return i3 < 6 || i3 >= 22;
    }
}
