Separate system accounts and household people, add generation directory and consented member location sharing
This commit is contained in:
@@ -11,11 +11,15 @@ import java.util.concurrent.*;
|
||||
|
||||
/** Visible, bounded sharing session. No boot receiver starts location collection. */
|
||||
public class LocationService extends Service implements LocationListener {
|
||||
static volatile boolean busy=false;
|
||||
String sessionToken="";
|
||||
volatile boolean destroyed=false;
|
||||
ScheduledExecutorService worker;LocationManager manager;android.content.SharedPreferences prefs;
|
||||
volatile Location latest;volatile boolean active=false,stopping=false,starting=false;
|
||||
final Handler ui=new Handler();
|
||||
public void onCreate(){
|
||||
super.onCreate();prefs=getSharedPreferences("location",0);manager=(LocationManager)getSystemService(LOCATION_SERVICE);
|
||||
sessionToken=prefs.getString("token","");
|
||||
NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
||||
nm.createNotificationChannel(new NotificationChannel("location","本人位置共享",NotificationManager.IMPORTANCE_LOW));
|
||||
worker=Executors.newSingleThreadScheduledExecutor();
|
||||
@@ -30,15 +34,16 @@ public class LocationService extends Service implements LocationListener {
|
||||
if(intent!=null&&"STOP".equals(intent.getAction())){stopSharing();return START_NOT_STICKY;}
|
||||
if(intent==null){stopSelf();return START_NOT_STICKY;}
|
||||
if(active||stopping||starting)return START_NOT_STICKY;
|
||||
starting=true;
|
||||
starting=true;busy=true;
|
||||
final String receipt=prefs.getString("receipt","");
|
||||
notification("正在确认本人的共享授权");
|
||||
worker.execute(()->{try{
|
||||
JSONObject r=DisplayApi.call("/api/location/update",prefs.getString("token",""),new JSONObject().put("action","start").put("consent",true));
|
||||
if(stopping)return;
|
||||
JSONObject r=DisplayApi.call("/api/location/update",sessionToken,new JSONObject().put("action","start").put("consent",true).put("receipt",receipt));
|
||||
if(stopping||destroyed)return;
|
||||
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("home","")+" · 可随时停止");}});
|
||||
ui.post(()->{if(!stopping){listen();notification("共享给 "+prefs.getString("recipients","")+" · 可随时停止");}});
|
||||
worker.scheduleWithFixedDelay(this::upload,1,300,TimeUnit.SECONDS);
|
||||
}catch(Exception e){prefs.edit().putBoolean("active",false).putString("status","未开启:请检查网络、配对有效期").apply();stopSelf();}});
|
||||
}catch(Exception e){if(!destroyed){prefs.edit().putBoolean("active",false).putString("status","未开启:请检查网络、配对有效期").apply();stopSelf();}}});
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
void listen(){
|
||||
@@ -55,27 +60,29 @@ public class LocationService extends Service implements LocationListener {
|
||||
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",prefs.getString("token",""),body);
|
||||
body.put("signal",signal);DisplayApi.call("/api/location/update",sessionToken,body);
|
||||
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();}
|
||||
}
|
||||
void stopSharing(){
|
||||
if(stopping)return;stopping=true;active=false;latest=null;
|
||||
if(stopping)return;stopping=true;busy=true;active=false;latest=null;
|
||||
try{manager.removeUpdates(this);}catch(Exception ignored){}
|
||||
prefs.edit().putBoolean("active",false).putString("status","本机已停止定位,正在通知平台清除").apply();
|
||||
notification("本机已停止定位 · 正在通知平台清除");
|
||||
worker.scheduleWithFixedDelay(()->{try{
|
||||
String token=prefs.getString("token","");
|
||||
String token=sessionToken;
|
||||
if(!token.isEmpty())try{DisplayApi.call("/api/location/update",token,new JSONObject().put("action","stop"));}catch(DisplayApi.ApiError e){if(e.status!=401&&e.status!=403)throw e;}
|
||||
prefs.edit().remove("token").putBoolean("active",false).putString("status","共享关闭,平台已清除当前位置").apply();stopSelf();
|
||||
}catch(Exception e){prefs.edit().putString("status","本机已停止,断网期间平台可能显示最后位置;联网后清除,也可在平台停止共享").apply();}},0,30,TimeUnit.SECONDS);
|
||||
if(destroyed)return;
|
||||
prefs.edit().putBoolean("active",false).putString("status","共享关闭,平台已清除当前位置").apply();stopSelf();
|
||||
}catch(Exception e){prefs.edit().putString("status","本机已停止,联网后清除平台位置,旧坐标最长 15 分钟后隐藏,也可在平台解除授权").apply();}},0,30,TimeUnit.SECONDS);
|
||||
}
|
||||
public void onLocationChanged(Location value){if(active&&!stopping)latest=new Location(value);}
|
||||
public void onStatusChanged(String provider,int status,Bundle extras){}
|
||||
public void onProviderEnabled(String provider){}
|
||||
public void onProviderDisabled(String provider){}
|
||||
public IBinder onBind(Intent intent){return null;}
|
||||
public void onDestroy(){active=false;latest=null;try{manager.removeUpdates(this);}catch(Exception ignored){}if(worker!=null)worker.shutdownNow();prefs.edit().putBoolean("active",false).apply();super.onDestroy();}
|
||||
public void onDestroy(){destroyed=true;active=false;latest=null;try{manager.removeUpdates(this);}catch(Exception ignored){}if(worker!=null)worker.shutdownNow();prefs.edit().putBoolean("active",false).apply();busy=false;super.onDestroy();}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user