118 lines
4.9 KiB
Java
118 lines
4.9 KiB
Java
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 androidx.annotation.NonNull;
|
|
import androidx.annotation.RequiresPermission;
|
|
import androidx.annotation.VisibleForTesting;
|
|
import androidx.core.content.PermissionChecker;
|
|
import java.util.Calendar;
|
|
public 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();
|
|
|
|
public static class TwilightState {
|
|
public boolean isNight;
|
|
public long nextUpdate;
|
|
public long todaySunrise;
|
|
public long todaySunset;
|
|
public long tomorrowSunrise;
|
|
public long yesterdaySunset;
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public TwilightManager(@NonNull Context context, @NonNull LocationManager locationManager) {
|
|
this.mContext = context;
|
|
this.mLocationManager = locationManager;
|
|
}
|
|
|
|
public static TwilightManager getInstance(@NonNull Context context) {
|
|
if (sInstance == null) {
|
|
Context applicationContext = context.getApplicationContext();
|
|
sInstance = new TwilightManager(applicationContext, (LocationManager) applicationContext.getSystemService("location"));
|
|
}
|
|
return sInstance;
|
|
}
|
|
|
|
@SuppressLint({"MissingPermission"})
|
|
private Location getLastKnownLocation() {
|
|
Location location = null;
|
|
Location lastKnownLocationForProvider = PermissionChecker.checkSelfPermission(this.mContext, "android.permission.ACCESS_COARSE_LOCATION") == 0 ? getLastKnownLocationForProvider("network") : null;
|
|
if (PermissionChecker.checkSelfPermission(this.mContext, "android.permission.ACCESS_FINE_LOCATION") == 0) {
|
|
location = getLastKnownLocationForProvider("gps");
|
|
}
|
|
return (location == null || lastKnownLocationForProvider == null) ? location != null ? location : lastKnownLocationForProvider : location.getTime() > lastKnownLocationForProvider.getTime() ? location : lastKnownLocationForProvider;
|
|
}
|
|
|
|
@RequiresPermission(anyOf = {"android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"})
|
|
private Location getLastKnownLocationForProvider(String str) {
|
|
try {
|
|
if (this.mLocationManager.isProviderEnabled(str)) {
|
|
return this.mLocationManager.getLastKnownLocation(str);
|
|
}
|
|
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();
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public static void setInstance(TwilightManager twilightManager) {
|
|
sInstance = twilightManager;
|
|
}
|
|
|
|
private void updateState(@NonNull Location location) {
|
|
long j;
|
|
TwilightState twilightState = this.mTwilightState;
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
TwilightCalculator instance = TwilightCalculator.getInstance();
|
|
instance.calculateTwilight(currentTimeMillis - 86400000, location.getLatitude(), location.getLongitude());
|
|
long j2 = instance.sunset;
|
|
instance.calculateTwilight(currentTimeMillis, location.getLatitude(), location.getLongitude());
|
|
boolean z2 = instance.state == 1;
|
|
long j3 = instance.sunrise;
|
|
long j4 = instance.sunset;
|
|
instance.calculateTwilight(86400000 + currentTimeMillis, location.getLatitude(), location.getLongitude());
|
|
long j5 = instance.sunrise;
|
|
if (j3 == -1 || j4 == -1) {
|
|
j = 43200000 + currentTimeMillis;
|
|
} else {
|
|
j = (currentTimeMillis > j4 ? 0 + j5 : currentTimeMillis > j3 ? 0 + j4 : 0 + j3) + 60000;
|
|
}
|
|
twilightState.isNight = z2;
|
|
twilightState.yesterdaySunset = j2;
|
|
twilightState.todaySunrise = j3;
|
|
twilightState.todaySunset = j4;
|
|
twilightState.tomorrowSunrise = j5;
|
|
twilightState.nextUpdate = j;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|