Separate system accounts and household people, add generation directory and consented member location sharing
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.ucvl.zhaodisplay" android:versionCode="1" android:versionName="0.1.44">
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.ucvl.zhaodisplay" android:versionCode="2" android:versionName="0.1.44">
|
||||
<uses-sdk android:minSdkVersion="26" android:targetSdkVersion="30"/>
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.json.JSONObject;
|
||||
|
||||
/** Phone-only, explicit opt-in. The TV never requests location permission. */
|
||||
public class LocationActivity extends Activity {
|
||||
static volatile boolean pairing=false;
|
||||
TextView status; EditText code; Button pair,start,stop; CheckBox consent;
|
||||
android.content.SharedPreferences prefs;
|
||||
final Handler ui=new Handler();
|
||||
@@ -20,11 +21,11 @@ public class LocationActivity extends Activity {
|
||||
LinearLayout layout=new LinearLayout(this);layout.setOrientation(1);int p=(int)(24*getResources().getDisplayMetrics().density);layout.setPadding(p,p,p,p);layout.setBackgroundColor(0xfff5f2e9);
|
||||
ScrollView scroll=new ScrollView(this);scroll.addView(layout);setContentView(scroll);
|
||||
TextView heading=new TextView(this);heading.setText("家人位置共享");heading.setTextSize(28);layout.addView(heading);
|
||||
TextView help=new TextView(this);help.setText("使用本人的赵府智家账号,进入所属家庭 → 场所总览 → 家人位置,生成配对码。\n\n开启后,约每 5 分钟向本家庭成员和家庭电视共享最新位置。仅记录当前点,不保存行踪。通知栏会持续显示共享状态,可随时停止。系统省电或断网可能延迟更新。");help.setTextSize(16);help.setPadding(0,p,0,p);layout.addView(help);
|
||||
TextView help=new TextView(this);help.setText("不需要注册或登录。请家人从平台的家庭成员中选择你的档案,生成手机配对码。\n\n配对后核对你的姓名、家庭及允许查看的人。只有你同意并开启共享,才约每 5 分钟上传最新位置。通知栏可随时停止;不保存轨迹。断网或省电可能延迟更新。");help.setTextSize(16);help.setPadding(0,p,0,p);layout.addView(help);
|
||||
status=new TextView(this);status.setTextSize(17);layout.addView(status);
|
||||
code=new EditText(this);code.setHint("输入本人账号生成的八位配对码");code.setInputType(InputType.TYPE_CLASS_NUMBER);code.setFilters(new android.text.InputFilter[]{new android.text.InputFilter.LengthFilter(8)});layout.addView(code);
|
||||
code=new EditText(this);code.setHint("输入家人为你生成的八位配对码");code.setInputType(InputType.TYPE_CLASS_NUMBER);code.setFilters(new android.text.InputFilter[]{new android.text.InputFilter.LengthFilter(8)});layout.addView(code);
|
||||
pair=new Button(this);pair.setText("绑定我的位置共享");layout.addView(pair);pair.setOnClickListener(v->pair());
|
||||
consent=new CheckBox(this);consent.setText("我同意在下方显示的有效期内,将位置共享给此家庭成员及家庭电视。停止后不再采集。 ");layout.addView(consent);
|
||||
consent=new CheckBox(this);consent.setText("我已核对上方的本人姓名、家庭、查看名单及有效期,同意开启位置共享。关闭后停止采集并清除位置。");layout.addView(consent);
|
||||
start=new Button(this);start.setText("同意并开启定位");layout.addView(start);start.setOnClickListener(v->start());
|
||||
stop=new Button(this);stop.setText("停止共享并清除位置");layout.addView(stop);stop.setOnClickListener(v->{startService(new Intent(this,LocationService.class).setAction("STOP"));consent.setChecked(false);});
|
||||
render();ui.post(refresh);
|
||||
@@ -32,22 +33,28 @@ public class LocationActivity extends Activity {
|
||||
void render(){
|
||||
boolean paired=!prefs.getString("token","").isEmpty(),active=prefs.getBoolean("active",false);
|
||||
long expires=prefs.getLong("expires",0);
|
||||
status.setText("家庭:"+prefs.getString("home","尚未绑定")+"\n"+(expires>0?"有效期至 "+java.text.DateFormat.getDateTimeInstance().format(new java.util.Date(expires))+"\n":"")+prefs.getString("status","共享关闭"));
|
||||
start.setEnabled(paired&&!active);stop.setEnabled(paired);pair.setEnabled(!active);
|
||||
status.setText("成员:"+prefs.getString("memberName","尚未绑定")+"\n允许查看:"+prefs.getString("recipients","无")+"\n家庭:"+prefs.getString("home","尚未绑定")+"\n"+(expires>0?"有效期至 "+java.text.DateFormat.getDateTimeInstance().format(new java.util.Date(expires))+"\n":"")+prefs.getString("status","共享关闭"));
|
||||
boolean busy=pairing||LocationService.busy;
|
||||
start.setEnabled(paired&&!active&&!busy);stop.setEnabled(paired&&!pairing);pair.setEnabled(!active&&!busy);consent.setEnabled(!active&&!busy);
|
||||
}
|
||||
void pair(){
|
||||
if(pairing||LocationService.busy||prefs.getBoolean("active",false))return;
|
||||
String value=code.getText().toString().trim();if(!value.matches("[0-9]{8}")){Toast.makeText(this,"请输入八位配对码",1).show();return;}
|
||||
pair.setEnabled(false);
|
||||
pairing=true;render();
|
||||
new Thread(()->{try{
|
||||
JSONObject response=DisplayApi.call("/api/location/pair",null,new JSONObject().put("code",value));
|
||||
prefs.edit().putString("token",response.getString("token")).putString("home",response.getString("home")).putLong("expires",(long)(response.getDouble("expires")*1000)).putBoolean("active",false).putString("status","已绑定,尚未开启定位").apply();
|
||||
JSONObject response=DisplayApi.call("/api/location/pair",null,new JSONObject().put("code",value).put("consentProtocol",2));
|
||||
StringBuilder viewers=new StringBuilder();for(String type:new String[]{"viewers","displays"}){org.json.JSONArray list=response.getJSONArray(type);for(int i=0;i<list.length();i++){if(viewers.length()>0)viewers.append("、");viewers.append(list.getJSONObject(i).getString("name"));if(type.equals("displays"))viewers.append("(公共显示终端)");}}
|
||||
prefs.edit().putString("memberName",response.getString("memberName")).putString("receipt",response.getString("receipt")).putString("recipients",viewers.toString()).putString("token",response.getString("token")).putString("home",response.getString("home")).putLong("expires",(long)(response.getDouble("expires")*1000)).putBoolean("active",false).putString("status","已绑定,尚未开启定位").apply();
|
||||
runOnUiThread(()->{code.setText("");consent.setChecked(false);render();});
|
||||
}catch(Exception e){runOnUiThread(()->{pair.setEnabled(true);Toast.makeText(this,"绑定未成功,请检查网络或重新获取配对码",1).show();});}},"location-pair").start();
|
||||
}catch(Exception e){runOnUiThread(()->Toast.makeText(this,"绑定未成功,请检查网络或重新获取配对码",1).show());}finally{pairing=false;runOnUiThread(this::render);}},"location-pair").start();
|
||||
}
|
||||
void start(){
|
||||
if(pairing||LocationService.busy||prefs.getBoolean("active",false))return;
|
||||
if(!consent.isChecked()){Toast.makeText(this,"请本人阅读并勾选共享说明",1).show();return;}
|
||||
if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED&&checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},45);return;}
|
||||
startForegroundService(new Intent(this,LocationService.class).setAction("START"));
|
||||
LocationService.busy=true;render();
|
||||
try{startForegroundService(new Intent(this,LocationService.class).setAction("START"));}
|
||||
catch(Exception e){LocationService.busy=false;render();Toast.makeText(this,"无法启动共享,请保持本页面在前台后重试",1).show();}
|
||||
}
|
||||
public void onRequestPermissionsResult(int request,String[] permissions,int[] results){super.onRequestPermissionsResult(request,permissions,results);if(request==45){if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)==0||checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)==0)start();else Toast.makeText(this,"未开启定位权限,不采集位置",1).show();}}
|
||||
public void onDestroy(){ui.removeCallbacksAndMessages(null);super.onDestroy();}
|
||||
|
||||
@@ -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