package android.support.v7.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 f810d;

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

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

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

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

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

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

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

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

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

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

        a() {
        }
    }

    j(Context context, LocationManager locationManager) {
        this.f811a = context;
        this.f812b = locationManager;
    }

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

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

    private boolean e() {
        return this.f813c.f819f > System.currentTimeMillis();
    }

    private void f(Location location) {
        long j;
        a aVar = this.f813c;
        long jCurrentTimeMillis = System.currentTimeMillis();
        i iVarB = i.b();
        iVarB.a(jCurrentTimeMillis - 86400000, location.getLatitude(), location.getLongitude());
        long j2 = iVarB.f807a;
        iVarB.a(jCurrentTimeMillis, location.getLatitude(), location.getLongitude());
        boolean z = iVarB.f809c == 1;
        long j3 = iVarB.f808b;
        long j4 = iVarB.f807a;
        iVarB.a(86400000 + jCurrentTimeMillis, location.getLatitude(), location.getLongitude());
        long j5 = iVarB.f808b;
        if (j3 == -1 || j4 == -1) {
            j = 43200000 + jCurrentTimeMillis;
        } else {
            j = (jCurrentTimeMillis > j4 ? 0 + j5 : jCurrentTimeMillis > j3 ? 0 + j4 : 0 + j3) + 60000;
        }
        aVar.f814a = z;
        aVar.f815b = j2;
        aVar.f816c = j3;
        aVar.f817d = j4;
        aVar.f818e = j5;
        aVar.f819f = j;
    }

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