jimu-decompiled/sources/androidx/appcompat/app/TwilightManager.java
2025-05-13 19:24:51 +02:00

108 lines
3.4 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.core.content.PermissionChecker;
import java.util.Calendar;
/* loaded from: classes.dex */
class TwilightManager {
private static TwilightManager d;
private final Context a;
private final LocationManager b;
private final TwilightState c = new TwilightState();
private static class TwilightState {
boolean a;
long b;
long c;
long d;
long e;
long f;
TwilightState() {
}
}
TwilightManager(Context context, LocationManager locationManager) {
this.a = context;
this.b = locationManager;
}
static TwilightManager a(Context context) {
if (d == null) {
Context applicationContext = context.getApplicationContext();
d = new TwilightManager(applicationContext, (LocationManager) applicationContext.getSystemService("location"));
}
return d;
}
@SuppressLint({"MissingPermission"})
private Location b() {
Location a = PermissionChecker.a(this.a, "android.permission.ACCESS_COARSE_LOCATION") == 0 ? a("network") : null;
Location a2 = PermissionChecker.a(this.a, "android.permission.ACCESS_FINE_LOCATION") == 0 ? a("gps") : null;
return (a2 == null || a == null) ? a2 != null ? a2 : a : a2.getTime() > a.getTime() ? a2 : a;
}
private boolean c() {
return this.c.f > System.currentTimeMillis();
}
boolean a() {
TwilightState twilightState = this.c;
if (c()) {
return twilightState.a;
}
Location b = b();
if (b != null) {
a(b);
return twilightState.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 i = Calendar.getInstance().get(11);
return i < 6 || i >= 22;
}
private Location a(String str) {
try {
if (this.b.isProviderEnabled(str)) {
return this.b.getLastKnownLocation(str);
}
return null;
} catch (Exception e) {
Log.d("TwilightManager", "Failed to get last known location", e);
return null;
}
}
private void a(Location location) {
long j;
TwilightState twilightState = this.c;
long currentTimeMillis = System.currentTimeMillis();
TwilightCalculator a = TwilightCalculator.a();
a.a(currentTimeMillis - 86400000, location.getLatitude(), location.getLongitude());
long j2 = a.a;
a.a(currentTimeMillis, location.getLatitude(), location.getLongitude());
boolean z = a.c == 1;
long j3 = a.b;
long j4 = a.a;
boolean z2 = z;
a.a(86400000 + currentTimeMillis, location.getLatitude(), location.getLongitude());
long j5 = a.b;
if (j3 == -1 || j4 == -1) {
j = 43200000 + currentTimeMillis;
} else {
j = (currentTimeMillis > j4 ? 0 + j5 : currentTimeMillis > j3 ? 0 + j4 : 0 + j3) + 60000;
}
twilightState.a = z2;
twilightState.b = j2;
twilightState.c = j3;
twilightState.d = j4;
twilightState.e = j5;
twilightState.f = j;
}
}