Release 0.1.45: member phone locations and people-first family navigation
This commit is contained in:
@@ -16,6 +16,7 @@ public class LocationService extends Service implements LocationListener {
|
||||
volatile boolean destroyed=false;
|
||||
ScheduledExecutorService worker;LocationManager manager;android.content.SharedPreferences prefs;
|
||||
volatile Location latest;volatile boolean active=false,stopping=false,starting=false;
|
||||
long lastAttempt=0;boolean sentFix=false;int intervalSeconds=300;String lastSignal="";
|
||||
final Handler ui=new Handler();
|
||||
public void onCreate(){
|
||||
super.onCreate();prefs=getSharedPreferences("location",0);manager=(LocationManager)getSystemService(LOCATION_SERVICE);
|
||||
@@ -40,33 +41,44 @@ public class LocationService extends Service implements LocationListener {
|
||||
worker.execute(()->{try{
|
||||
JSONObject r=DisplayApi.call("/api/location/update",sessionToken,new JSONObject().put("action","start").put("consent",true).put("receipt",receipt));
|
||||
if(stopping||destroyed)return;
|
||||
intervalSeconds=LocationCadence.seconds(r.optInt("intervalSeconds",300));
|
||||
active=true;prefs.edit().putBoolean("active",true).putLong("expires",(long)(r.getDouble("expires")*1000)).putString("status","共享中,等待定位").apply();
|
||||
ui.post(()->{if(!stopping){listen();notification("共享给 "+prefs.getString("recipients","")+" · 可随时停止");}});
|
||||
worker.scheduleWithFixedDelay(this::upload,1,300,TimeUnit.SECONDS);
|
||||
worker.scheduleWithFixedDelay(this::upload,1,10,TimeUnit.SECONDS);
|
||||
}catch(Exception e){if(!destroyed){prefs.edit().putBoolean("active",false).putString("status","未开启:请检查网络、配对有效期").apply();stopSelf();}}});
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
void listen(){
|
||||
if(!active||stopping)return;
|
||||
try{for(String provider:new String[]{LocationManager.GPS_PROVIDER,LocationManager.NETWORK_PROVIDER})if(manager.getAllProviders().contains(provider))manager.requestLocationUpdates(provider,300000,0,this);}
|
||||
try{for(String provider:new String[]{LocationManager.GPS_PROVIDER,LocationManager.NETWORK_PROVIDER})if(manager.getAllProviders().contains(provider))manager.requestLocationUpdates(provider,intervalSeconds*1000L,0,this);}
|
||||
catch(SecurityException e){prefs.edit().putString("status","定位权限未开启").apply();}
|
||||
}
|
||||
void upload(){
|
||||
if(!active||stopping)return;
|
||||
if(System.currentTimeMillis()>prefs.getLong("expires",0)){ui.post(this::stopSharing);return;}
|
||||
try{
|
||||
String signal=checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED||checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED?manager.isLocationEnabled()?"waiting":"location_disabled":"permission_denied";
|
||||
String signal=checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED||checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED?locationEnabled(manager)?"waiting":"location_disabled":"permission_denied";
|
||||
// Permission/setting loss must clear the server fix without waiting five minutes.
|
||||
boolean safetyChange=(!"waiting".equals(signal)&&!signal.equals(lastSignal));
|
||||
if(!LocationCadence.due(SystemClock.elapsedRealtime(),lastAttempt,intervalSeconds,safetyChange||latest!=null&&!sentFix))return;
|
||||
lastAttempt=SystemClock.elapsedRealtime();
|
||||
JSONObject body=new JSONObject().put("action","update");Location fix=latest;
|
||||
if(fix!=null&&"waiting".equals(signal)&&SystemClock.elapsedRealtimeNanos()-fix.getElapsedRealtimeNanos()<900000000000L){
|
||||
body.put("position",new JSONObject().put("latitude",fix.getLatitude()).put("longitude",fix.getLongitude()).put("accuracy",fix.getAccuracy()).put("at",fix.getTime()/1000d));signal="available";
|
||||
}
|
||||
body.put("signal",signal);DisplayApi.call("/api/location/update",sessionToken,body);
|
||||
lastSignal=signal;
|
||||
if("available".equals(signal))sentFix=true;
|
||||
if(stopping||destroyed)return;
|
||||
String message="available".equals(signal)?"共享中 · 最近更新 "+java.text.DateFormat.getTimeInstance().format(new java.util.Date()):"共享中 · 等待定位,请检查手机定位和权限";
|
||||
prefs.edit().putString("status",message).apply();
|
||||
}catch(DisplayApi.ApiError e){if(e.status==401||e.status==403)ui.post(this::stopSharing);else prefs.edit().putString("status","网络未同步,稍后自动重试").apply();}
|
||||
catch(Exception e){prefs.edit().putString("status","网络未同步,稍后自动重试").apply();}
|
||||
}
|
||||
static boolean locationEnabled(LocationManager manager){
|
||||
if(Build.VERSION.SDK_INT>=28)return manager.isLocationEnabled();
|
||||
return manager.isProviderEnabled(LocationManager.GPS_PROVIDER)||manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
|
||||
}
|
||||
void stopSharing(){
|
||||
if(stopping)return;stopping=true;busy=true;active=false;latest=null;
|
||||
try{manager.removeUpdates(this);}catch(Exception ignored){}
|
||||
|
||||
Reference in New Issue
Block a user