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 j {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static j f148d;
    private final Context a;

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

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

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

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

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

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

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

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

        a() {
        }
    }

    j(Context context, LocationManager locationManager) {
        this.a = context;
        this.f149b = locationManager;
    }

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

    @SuppressLint({"MissingPermission"})
    private Location b() {
        Location locationC = c.f.d.b.b(this.a, "android.permission.ACCESS_COARSE_LOCATION") == 0 ? c("network") : null;
        Location locationC2 = c.f.d.b.b(this.a, "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.f149b.isProviderEnabled(str)) {
                return this.f149b.getLastKnownLocation(str);
            }
            return null;
        } catch (Exception e2) {
            Log.d("TwilightManager", "Failed to get last known location", e2);
            return null;
        }
    }

    private boolean e() {
        return this.f150c.f155f > System.currentTimeMillis();
    }

    private void f(Location location) {
        long j2;
        a aVar = this.f150c;
        long jCurrentTimeMillis = System.currentTimeMillis();
        i iVarB = i.b();
        iVarB.a(jCurrentTimeMillis - 86400000, location.getLatitude(), location.getLongitude());
        long j3 = iVarB.a;
        iVarB.a(jCurrentTimeMillis, location.getLatitude(), location.getLongitude());
        boolean z = iVarB.f147c == 1;
        long j4 = iVarB.f146b;
        long j5 = iVarB.a;
        iVarB.a(86400000 + jCurrentTimeMillis, location.getLatitude(), location.getLongitude());
        long j6 = iVarB.f146b;
        if (j4 == -1 || j5 == -1) {
            j2 = 43200000 + jCurrentTimeMillis;
        } else {
            j2 = (jCurrentTimeMillis > j5 ? 0 + j6 : jCurrentTimeMillis > j4 ? 0 + j5 : 0 + j4) + 60000;
        }
        aVar.a = z;
        aVar.f151b = j3;
        aVar.f152c = j4;
        aVar.f153d = j5;
        aVar.f154e = j6;
        aVar.f155f = j2;
    }

    boolean d() {
        a aVar = this.f150c;
        if (e()) {
            return aVar.a;
        }
        Location locationB = b();
        if (locationB != null) {
            f(locationB);
            return aVar.a;
        }
        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 i2 = Calendar.getInstance().get(11);
        return i2 < 6 || i2 >= 22;
    }
}
