feat: household TV companion with scoped pairing and direct LAN camera strip
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package cn.ucvl.zhaodisplay;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.view.PixelCopy;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
import android.text.InputType;
|
||||
import org.json.JSONObject;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.webkit.WebResourceRequest;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class MainActivity extends Activity {
|
||||
static volatile MainActivity active;
|
||||
WebView web;
|
||||
CameraStrip cameras;
|
||||
@Override public void onCreate(Bundle state) {
|
||||
super.onCreate(state);
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
immersive();
|
||||
web=new WebView(this);
|
||||
web.setBackgroundColor(0xfff1ede4);
|
||||
web.getSettings().setJavaScriptEnabled(true);
|
||||
web.getSettings().setDomStorageEnabled(true);
|
||||
web.getSettings().setAllowFileAccess(false);
|
||||
web.getSettings().setAllowContentAccess(false);
|
||||
web.getSettings().setMixedContentMode(0);
|
||||
web.setWebViewClient(new WebViewClient(){
|
||||
@Override public boolean shouldOverrideUrlLoading(WebView view,WebResourceRequest request){
|
||||
return !"https".equals(request.getUrl().getScheme()) || !"zhao.ucvl.cn".equals(request.getUrl().getHost()) || !request.getUrl().getPath().startsWith("/tv");
|
||||
}
|
||||
});
|
||||
LinearLayout layout=new LinearLayout(this);layout.setOrientation(LinearLayout.VERTICAL);
|
||||
layout.addView(web,new LinearLayout.LayoutParams(-1,0,88));
|
||||
cameras=new CameraStrip(this);layout.addView(cameras,new LinearLayout.LayoutParams(-1,0,12));
|
||||
setContentView(layout);
|
||||
configure(getIntent());
|
||||
}
|
||||
void configure(Intent intent){
|
||||
String code=intent.getStringExtra("pairing_code");
|
||||
if(code!=null&&code.matches("[0-9]{8}")){intent.removeExtra("pairing_code");pair(code);return;}
|
||||
String token=getSharedPreferences("display",MODE_PRIVATE).getString("token","");
|
||||
web.loadUrl("https://zhao.ucvl.cn/tv"+(token.isEmpty()?"":"#display="+token));
|
||||
if(!token.isEmpty())startForegroundService(new Intent(this,MonitorService.class));
|
||||
else pairDialog();
|
||||
}
|
||||
void pairDialog(){
|
||||
EditText code=new EditText(this);code.setInputType(InputType.TYPE_CLASS_NUMBER);code.setHint("8 位家庭屏配对码");
|
||||
code.setFilters(new android.text.InputFilter[]{new android.text.InputFilter.LengthFilter(8)});
|
||||
new AlertDialog.Builder(this).setTitle("绑定家庭中心").setMessage("在赵府智家中进入目标家庭 → 电视设备 → 电视管理 → 生成配对码。更换家庭前,请先由管理员移动这台设备的归属。")
|
||||
.setView(code).setNegativeButton("取消",null).setPositiveButton("绑定",(dialog,which)->pair(code.getText().toString().trim())).show();
|
||||
}
|
||||
void pair(String code){
|
||||
if(!code.matches("[0-9]{8}")){Toast.makeText(this,"请输入 8 位配对码",Toast.LENGTH_LONG).show();return;}
|
||||
new Thread(()->{
|
||||
try{
|
||||
JSONObject value=DisplayApi.call("/api/display/pair",null,new JSONObject().put("code",code));
|
||||
getSharedPreferences("display",MODE_PRIVATE).edit().putString("token",value.getString("token")).putString("nativeToken",value.getString("nativeToken")).apply();
|
||||
runOnUiThread(()->{configure(new Intent());cameras.refresh();Toast.makeText(this,"已绑定:"+value.optString("home"),Toast.LENGTH_LONG).show();});
|
||||
}catch(Exception e){runOnUiThread(()->Toast.makeText(this,"绑定未成功,请检查网络或重新生成配对码",Toast.LENGTH_LONG).show());}
|
||||
},"display-pair").start();
|
||||
}
|
||||
@Override public void onNewIntent(Intent intent){super.onNewIntent(intent);setIntent(intent);configure(intent);}
|
||||
@Override public void onResume(){super.onResume();active=this;immersive();if(cameras!=null)cameras.foreground(true);}
|
||||
@Override public void onPause(){if(active==this)active=null;if(cameras!=null)cameras.foreground(false);super.onPause();}
|
||||
@Override public void onDestroy(){if(active==this)active=null;if(cameras!=null)cameras.close();web.destroy();super.onDestroy();}
|
||||
@Override public void onWindowFocusChanged(boolean hasFocus){super.onWindowFocusChanged(hasFocus);if(hasFocus)immersive();}
|
||||
void immersive(){getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);}
|
||||
static void capture(Consumer<byte[]> callback){
|
||||
MainActivity a=active;
|
||||
if(a==null){callback.accept(null);return;}
|
||||
a.runOnUiThread(()->{
|
||||
if(active!=a){callback.accept(null);return;}
|
||||
try {
|
||||
// PixelCopy captures this app's actual window, never other applications.
|
||||
Bitmap image=Bitmap.createBitmap(a.getWindow().getDecorView().getWidth(),a.getWindow().getDecorView().getHeight(),Bitmap.Config.ARGB_8888);
|
||||
PixelCopy.request(a.getWindow(),image,result->{
|
||||
if(result!=PixelCopy.SUCCESS){image.recycle();callback.accept(null);return;}
|
||||
Bitmap small=Bitmap.createScaledBitmap(image,480,Math.max(1,Math.round(480f*image.getHeight()/image.getWidth())),true);
|
||||
ByteArrayOutputStream out=new ByteArrayOutputStream();small.compress(Bitmap.CompressFormat.JPEG,65,out);
|
||||
small.recycle();image.recycle();callback.accept(out.toByteArray());
|
||||
},new Handler(a.getMainLooper()));
|
||||
}catch(Exception|OutOfMemoryError e){callback.accept(null);}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user