o.java 源代码
package com.moat.analytics.mobile.inm;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import com.x8zs.plugin.android.net.http.Headers;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class o implements LocationListener {
private static o f19969a;
private ScheduledExecutorService f19970b;
private ScheduledFuture<?> f19971c;
private ScheduledFuture<?> f19972d;
private LocationManager f19973e;
private boolean f19974f;
private Location f19975g;
private boolean f19976h;
private o() {
try {
boolean z6 = ((k) MoatAnalytics.getInstance()).f19946c;
this.f19974f = z6;
if (z6) {
p.a(3, "LocationManager", this, "Moat location services disabled");
return;
}
this.f19970b = Executors.newScheduledThreadPool(1);
LocationManager locationManager = (LocationManager) a.a().getSystemService(Headers.LOCATION);
this.f19973e = locationManager;
if (locationManager.getAllProviders().size() == 0) {
p.a(3, "LocationManager", this, "Device has no location providers");
} else {
e();
}
} catch (Exception e6) {
m.a(e6);
}
}
public static o a() {
if (f19969a == null) {
f19969a = new o();
}
return f19969a;
}
private static float b(Location location) {
return (float) ((System.currentTimeMillis() - location.getTime()) / 1000);
}
public void e() {
try {
if (!this.f19974f && this.f19973e != null) {
if (this.f19976h) {
p.a(3, "LocationManager", this, "already updating location");
}
p.a(3, "LocationManager", this, "starting location fetch");
Location b6 = b(this.f19975g, f());
this.f19975g = b6;
if (b6 != null) {
p.a(3, "LocationManager", this, "Have a valid location, won't fetch = " + this.f19975g.toString());
k();
return;
}
g();
}
} catch (Exception e6) {
m.a(e6);
}
}
private Location f() {
Location location = null;
try {
boolean l6 = l();
boolean m6 = m();
if (l6 && m6) {
location = b(this.f19973e.getLastKnownLocation("gps"), this.f19973e.getLastKnownLocation("network"));
} else if (l6) {
location = this.f19973e.getLastKnownLocation("gps");
} else if (m6) {
location = this.f19973e.getLastKnownLocation("network");
}
} catch (SecurityException e6) {
m.a(e6);
}
return location;
}
private void g() {
try {
if (!this.f19976h) {
p.a(3, "LocationManager", this, "Attempting to start update");
if (l()) {
p.a(3, "LocationManager", this, "start updating gps location");
this.f19973e.requestLocationUpdates("gps", 0L, 0.0f, this, Looper.getMainLooper());
this.f19976h = true;
}
if (m()) {
p.a(3, "LocationManager", this, "start updating network location");
this.f19973e.requestLocationUpdates("network", 0L, 0.0f, this, Looper.getMainLooper());
this.f19976h = true;
}
if (this.f19976h) {
i();
this.f19972d = this.f19970b.schedule(new Runnable() {
@Override
public void run() {
try {
p.a(3, "LocationManager", this, "fetchTimedOut");
o.this.a(true);
} catch (Exception e6) {
m.a(e6);
}
}
}, 60L, TimeUnit.SECONDS);
}
}
} catch (SecurityException e6) {
m.a(e6);
}
}
private void h() {
LocationManager locationManager;
try {
p.a(3, "LocationManager", this, "Stopping to update location");
if (n() && (locationManager = this.f19973e) != null) {
locationManager.removeUpdates(this);
this.f19976h = false;
}
} catch (SecurityException e6) {
m.a(e6);
}
}
private void i() {
ScheduledFuture<?> scheduledFuture = this.f19972d;
if (scheduledFuture != null && !scheduledFuture.isCancelled()) {
this.f19972d.cancel(true);
this.f19972d = null;
}
}
private void j() {
ScheduledFuture<?> scheduledFuture = this.f19971c;
if (scheduledFuture != null && !scheduledFuture.isCancelled()) {
this.f19971c.cancel(true);
this.f19971c = null;
}
}
private void k() {
p.a(3, "LocationManager", this, "Resetting fetch timer");
j();
Location location = this.f19975g;
float f6 = 600.0f;
if (location != null) {
f6 = Math.max(600.0f - b(location), 0.0f);
}
this.f19971c = this.f19970b.schedule(new Runnable() {
@Override
public void run() {
try {
p.a(3, "LocationManager", this, "fetchTimerCompleted");
o.this.e();
} catch (Exception e6) {
m.a(e6);
}
}
}, f6, TimeUnit.SECONDS);
}
private boolean l() {
if (a("android.permission.ACCESS_FINE_LOCATION") && this.f19973e.getProvider("gps") != null && this.f19973e.isProviderEnabled("gps")) {
return true;
}
return false;
}
private boolean m() {
if (n() && this.f19973e.getProvider("network") != null && this.f19973e.isProviderEnabled("network")) {
return true;
}
return false;
}
private static boolean n() {
if (!a("android.permission.ACCESS_FINE_LOCATION") && !a("android.permission.ACCESS_COARSE_LOCATION")) {
return false;
}
return true;
}
public void c() {
e();
}
public void d() {
a(false);
}
@Override
public void onLocationChanged(Location location) {
try {
p.a(3, "LocationManager", this, "Received an updated location = " + location.toString());
float b6 = b(location);
if (location.hasAccuracy() && location.getAccuracy() <= 100.0f && b6 < 600.0f) {
this.f19975g = b(this.f19975g, location);
p.a(3, "LocationManager", this, "fetchCompleted");
a(true);
}
} catch (Exception e6) {
m.a(e6);
}
}
@Override
public void onProviderDisabled(String str) {
}
@Override
public void onProviderEnabled(String str) {
}
@Override
public void onStatusChanged(String str, int i6, Bundle bundle) {
}
@Nullable
public Location b() {
if (this.f19974f || this.f19973e == null) {
return null;
}
return this.f19975g;
}
private static Location b(Location location, Location location2) {
boolean a7 = a(location);
boolean a8 = a(location2);
if (a7) {
return (a8 && location.getAccuracy() >= location.getAccuracy()) ? location2 : location;
}
if (a8) {
return location2;
}
return null;
}
public void a(boolean z6) {
try {
p.a(3, "LocationManager", this, "stopping location fetch");
h();
i();
if (z6) {
k();
} else {
j();
}
} catch (Exception e6) {
m.a(e6);
}
}
private static boolean a(Location location) {
if (location == null) {
return false;
}
return !(location.getLatitude() == 0.0d && location.getLongitude() == 0.0d) && location.getAccuracy() >= 0.0f && b(location) < 600.0f;
}
public static boolean a(Location location, Location location2) {
if (location == location2) {
return true;
}
return (location == null || location2 == null || location.getTime() != location2.getTime()) ? false : true;
}
private static boolean a(String str) {
return ContextCompat.checkSelfPermission(a.a().getApplicationContext(), str) == 0;
}
}