feat: household recording destinations and verified Baidu segment archive

This commit is contained in:
Codex
2026-10-16 13:02:12 +08:00
parent 292321ea20
commit 38201492af
42 changed files with 1787 additions and 112 deletions
@@ -30,6 +30,7 @@ final class CameraStrip extends LinearLayout {
boolean stopped=false, foreground=false;
long lease=0,lastRotation=0;
static volatile String report="[]";
static volatile String networkReport="[]";
CameraStrip(MainActivity a){
super(a);app=a;setOrientation(VERTICAL);setBackgroundColor(0xffe8e5db);
vlc=new LibVLC(a,new ArrayList<>(Arrays.asList("--rtsp-tcp","--network-caching=400","--no-audio","--no-video-title-show","--quiet")));
@@ -52,9 +53,16 @@ final class CameraStrip extends LinearLayout {
try{
JSONObject value=DisplayApi.call("/api/display/cameras",token,null);
JSONArray found=value.getJSONArray("items");String sig=found.toString();
JSONArray targets=value.optJSONArray("networkTargets"),states=new JSONArray();
if(targets!=null)for(int i=0;i<targets.length();i++){
JSONObject d=targets.getJSONObject(i);String host=d.optString("host"),state="outside_lan";
if(sameNetwork(host)){state="unreachable";for(int port:new int[]{80,443})try(Socket socket=new Socket()){socket.connect(new InetSocketAddress(host,port),600);state="reachable";break;}catch(Exception ignored){}}
states.put(new JSONObject().put("id",d.optString("id")).put("state",state));
}
networkReport=states.toString();
ui.post(()->{if(stopped)return;lease=SystemClock.elapsedRealtime()+300000;title.setText("家中画面 · 局域网直连");if(!signature.equals(sig)){cameras=found;signature=sig;group=0;render();}});
}catch(DisplayApi.ApiError e){if(e.status==401||e.status==403)ui.post(()->{lease=0;clear();signature="";title.setText("展示授权已失效 · 请重新绑定家庭");});}
catch(Exception ignored){}
}catch(DisplayApi.ApiError e){networkReport="[]";if(e.status==401||e.status==403)ui.post(()->{lease=0;clear();signature="";title.setText("展示授权已失效 · 请重新绑定家庭");});}
catch(Exception ignored){networkReport="[]";}
}
void refresh(){signature="";clear();if(!worker.isShutdown())worker.execute(this::fetch);}
void render(){
@@ -74,7 +82,7 @@ final class CameraStrip extends LinearLayout {
final Runnable watch=new Runnable(){public void run(){
if(stopped)return;
long now=SystemClock.elapsedRealtime();
if(lease>0&&lease<now){clear();signature="";title.setText("授权连接暂时中断 · 等待同步");}
if(lease>0&&lease<now){clear();networkReport="[]";signature="";title.setText("授权连接暂时中断 · 等待同步");}
else if(foreground&&cameras.length()>3&&now-lastRotation>60000){group=(group+1)%((cameras.length()+2)/3);render();}
JSONArray states=new JSONArray();
for(Tile t:new ArrayList<>(tiles)){
@@ -124,7 +132,10 @@ final class CameraStrip extends LinearLayout {
p.getVLCVout().setVideoSurface(surface,null);p.getVLCVout().setWindowSize(texture.getWidth(),texture.getHeight());
p.getVLCVout().attachViews((vout,w,h,vw,vh,sn,sd)->fit(vw,vh));
p.setEventListener(event->{if(player!=p||closed)return;if(event.type==MediaPlayer.Event.EncounteredError||event.type==MediaPlayer.Event.EndReached)fail("直连未成功 · 自动重试","offline");});
Media media=new Media(vlc,Uri.parse(camera.getString("url")));media.setHWDecoderEnabled(true,false);media.addOption(":rtsp-tcp");media.addOption(":network-caching=400");media.addOption(":no-audio");p.setMedia(media);media.release();p.setVolume(0);p.play();
URI endpoint=new URI(uri.getScheme(),null,uri.getHost(),uri.getPort(),uri.getPath(),uri.getQuery(),null);
Media media=new Media(vlc,Uri.parse(endpoint.toASCIIString()));
String credentials=uri.getUserInfo();if(credentials!=null){String[] parts=credentials.split(":",2);media.addOption(":rtsp-user="+parts[0]);media.addOption(":rtsp-pwd="+(parts.length>1?parts[1]:""));}
media.setHWDecoderEnabled(true,false);media.addOption(":rtsp-tcp");media.addOption(":network-caching=400");media.addOption(":no-audio");p.setMedia(media);media.release();p.setVolume(0);p.play();
}catch(Exception e){fail("直连未成功 · 自动重试","offline");}
}
void fit(int w,int h){if(w<=0||h<=0||texture.getWidth()==0||texture.getHeight()==0)return;float scale=Math.min((float)texture.getWidth()/w,(float)texture.getHeight()/h);Matrix matrix=new Matrix();matrix.setScale(w*scale/texture.getWidth(),h*scale/texture.getHeight(),texture.getWidth()/2f,texture.getHeight()/2f);texture.setTransform(matrix);}
@@ -0,0 +1,54 @@
package cn.ucvl.zhaodisplay;
import android.app.*;
import android.os.*;
import android.content.*;
import android.content.pm.PackageManager;
import android.Manifest;
import android.widget.*;
import android.text.InputType;
import org.json.JSONObject;
/** Phone-only, explicit opt-in. The TV never requests location permission. */
public class LocationActivity extends Activity {
TextView status; EditText code; Button pair,start,stop; CheckBox consent;
android.content.SharedPreferences prefs;
final Handler ui=new Handler();
final Runnable refresh=new Runnable(){public void run(){render();ui.postDelayed(this,2000);}};
public void onCreate(Bundle state){
super.onCreate(state);prefs=getSharedPreferences("location",0);
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);
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);
pair=new Button(this);pair.setText("绑定我的位置共享");layout.addView(pair);pair.setOnClickListener(v->pair());
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);
}
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);
}
void pair(){
String value=code.getText().toString().trim();if(!value.matches("[0-9]{8}")){Toast.makeText(this,"请输入八位配对码",1).show();return;}
pair.setEnabled(false);
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();
runOnUiThread(()->{code.setText("");consent.setChecked(false);render();});
}catch(Exception e){runOnUiThread(()->{pair.setEnabled(true);Toast.makeText(this,"绑定未成功,请检查网络或重新获取配对码",1).show();});}},"location-pair").start();
}
void start(){
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"));
}
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();}
}
@@ -0,0 +1,81 @@
package cn.ucvl.zhaodisplay;
import android.app.*;
import android.content.*;
import android.os.*;
import android.location.*;
import android.content.pm.PackageManager;
import android.Manifest;
import org.json.JSONObject;
import java.util.concurrent.*;
/** Visible, bounded sharing session. No boot receiver starts location collection. */
public class LocationService extends Service implements LocationListener {
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);
NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.createNotificationChannel(new NotificationChannel("location","本人位置共享",NotificationManager.IMPORTANCE_LOW));
worker=Executors.newSingleThreadScheduledExecutor();
}
void notification(String text){
PendingIntent stop=PendingIntent.getService(this,1,new Intent(this,LocationService.class).setAction("STOP"),PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent open=PendingIntent.getActivity(this,1,new Intent(this,LocationActivity.class),PendingIntent.FLAG_IMMUTABLE|PendingIntent.FLAG_UPDATE_CURRENT);
Notification notice=new Notification.Builder(this,"location").setContentTitle("赵府 · 位置共享").setContentText(text).setSmallIcon(android.R.drawable.ic_menu_mylocation).setContentIntent(open).setOngoing(true).addAction(new Notification.Action.Builder(null,"停止共享",stop).build()).build();
if(android.os.Build.VERSION.SDK_INT>=29)startForeground(45,notice,stopping?0:android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);else startForeground(45,notice);
}
public int onStartCommand(Intent intent,int flags,int id){
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;
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;
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","")+" · 可随时停止");}});
worker.scheduleWithFixedDelay(this::upload,1,300,TimeUnit.SECONDS);
}catch(Exception e){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);}
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";
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",prefs.getString("token",""),body);
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;
try{manager.removeUpdates(this);}catch(Exception ignored){}
prefs.edit().putBoolean("active",false).putString("status","本机已停止定位,正在通知平台清除").apply();
notification("本机已停止定位 · 正在通知平台清除");
worker.scheduleWithFixedDelay(()->{try{
String token=prefs.getString("token","");
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);
}
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();}
}
@@ -35,6 +35,7 @@ public class MonitorService extends Service {
result.put("model",Build.MODEL);result.put("osVersion",Build.VERSION.RELEASE);result.put("appVersion","0.1.44");
result.put("foreground",MainActivity.active!=null);
result.put("cameraStates",new org.json.JSONArray(CameraStrip.report));
result.put("networkStates",new org.json.JSONArray(CameraStrip.networkReport));
result.put("screenOn",((PowerManager)getSystemService(POWER_SERVICE)).isInteractive());
ActivityManager.MemoryInfo memory=new ActivityManager.MemoryInfo();((ActivityManager)getSystemService(ACTIVITY_SERVICE)).getMemoryInfo(memory);
result.put("memoryTotalMB",memory.totalMem/1048576d);result.put("memoryAvailableMB",memory.availMem/1048576d);