content: package androidx.appcompat.app;
  
  import android.content.Context;
  import android.location.Location;
  import android.location.LocationManager;
  import android.util.Log;
  import androidx.core.content.PermissionChecker;
  import java.util.Calendar;
  
  class TwilightManager {
      private static final int SUNRISE = 6;
      private static final int SUNSET = 22;
      private static final String TAG = "TwilightManager";
      private static TwilightManager sInstance;
      private final Context mContext;
      private final LocationManager mLocationManager;
      private final TwilightState mTwilightState = new TwilightState();
  
      private static class TwilightState {
          boolean isNight;
          long nextUpdate;
          long todaySunrise;
          long todaySunset;
          long tomorrowSunrise;
          long yesterdaySunset;
  
          TwilightState() {
          }
      }
  
      TwilightManager(Context context, LocationManager locationManager) {
          this.mContext = context;
          this.mLocationManager = locationManager;
      }
  
      static TwilightManager getInstance(Context context) {
          if (sInstance == null) {
              Context context2 = context.getApplicationContext();
              sInstance = new TwilightManager(context2, (LocationManager) context2.getSystemService("location"));
          }
          return sInstance;
      }
  
      private Location getLastKnownLocation() {
          Location location = null;
          Location location2 = null;
          if (PermissionChecker.checkSelfPermission(this.mContext, "android.permission.ACCESS_COARSE_LOCATION") == 0) {
              location = getLastKnownLocationForProvider("network");
          }
          if (PermissionChecker.checkSelfPermission(this.mContext, "android.permission.ACCESS_FINE_LOCATION") == 0) {
              location2 = getLastKnownLocationForProvider("gps");
          }
          return (location2 == null || location == null) ? location2 != null ? location2 : location : location2.getTime() > location.getTime() ? location2 : location;
      }
  
      private Location getLastKnownLocationForProvider(String provider) {
          try {
              if (this.mLocationManager.isProviderEnabled(provider)) {
                  return this.mLocationManager.getLastKnownLocation(provider);
              }
              return null;
          } catch (Exception e) {
              Log.d(TAG, "Failed to get last known location", e);
              return null;
          }
      }
  
      private boolean isStateValid() {
          return this.mTwilightState.nextUpdate > System.currentTimeMillis();
      }
  
      static void setInstance(TwilightManager twilightManager) {
          sInstance = twilightManager;
      }
  
      private void updateState(Location location) {
          long j;
          TwilightState twilightState = this.mTwilightState;
          long currentTimeMillis = System.currentTimeMillis();
          TwilightCalculator instance = TwilightCalculator.getInstance();
          TwilightCalculator twilightCalculator = instance;
          twilightCalculator.calculateTwilight(currentTimeMillis - 86400000, location.getLatitude(), location.getLongitude());
          long j2 = instance.sunset;
          twilightCalculator.calculateTwilight(currentTimeMillis, location.getLatitude(), location.getLongitude());
          boolean z = true;
          if (instance.state != 1) {
              z = false;
          }
          long j3 = instance.sunrise;
          long j4 = j2;
          long j5 = instance.sunset;
          long j6 = j3;
          boolean z2 = z;
          instance.calculateTwilight(86400000 + currentTimeMillis, location.getLatitude(), location.getLongitude());
          long j7 = instance.sunrise;
          if (j6 == -1 || j5 == -1) {
              j = currentTimeMillis + 43200000;
          } else {
              j = (currentTimeMillis > j5 ? 0 + j7 : currentTimeMillis > j6 ? 0 + j5 : 0 + j6) + 60000;
          }
          twilightState.isNight = z2;
          twilightState.yesterdaySunset = j4;
          twilightState.todaySunrise = j6;
          twilightState.todaySunset = j5;
          twilightState.tomorrowSunrise = j7;
          twilightState.nextUpdate = j;
      }
  
      /* access modifiers changed from: package-private */
      public boolean isNight() {
          TwilightState twilightState = this.mTwilightState;
          if (isStateValid()) {
              return twilightState.isNight;
          }
          Location lastKnownLocation = getLastKnownLocation();
          if (lastKnownLocation != null) {
              updateState(lastKnownLocation);
              return twilightState.isNight;
          }
          Log.i(TAG, "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;
      }
  }
  