Android/XixunPlayer/app/src/main/java/com/xixun/xixunplayer/AIDLService.java

481 lines
22 KiB
Java
Raw Normal View History

2023-11-09 08:37:59 +08:00
package com.xixun.xixunplayer;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.os.RemoteException;
2024-05-11 21:12:39 +08:00
import com.xixun.command.reply.TaskProgressReply;
2023-11-09 08:37:59 +08:00
import com.xixun.util.PlayerInfo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
2025-12-23 16:40:05 +08:00
import java.io.OutputStreamWriter;
2023-11-09 08:37:59 +08:00
import java.nio.charset.StandardCharsets;
2025-12-23 16:40:05 +08:00
import java.text.SimpleDateFormat;
2024-01-24 20:17:59 +08:00
import java.util.ArrayList;
2025-12-23 16:40:05 +08:00
import java.util.Arrays;
2023-11-09 08:37:59 +08:00
import java.util.Base64;
2025-12-23 16:40:05 +08:00
import java.util.Date;
2024-01-24 20:17:59 +08:00
import java.util.HashSet;
2023-11-09 08:37:59 +08:00
import gnph.util.IOs;
2024-01-24 20:17:59 +08:00
import gnph.util.JSList;
2023-11-09 08:37:59 +08:00
import gnph.util.JSMap;
import gnph.util.URLConn;
2025-07-28 19:14:00 +08:00
public class AIDLService extends Service {
2023-11-09 08:37:59 +08:00
@Override
public IBinder onBind(Intent intent) {
return binder;
}
PlayerInfo.Stub binder = new PlayerInfo.Stub() {
@Override
public String getProgramName() throws RemoteException {
2024-03-14 20:50:46 +08:00
try {
2024-07-04 17:45:56 +08:00
String name = null;
2024-08-22 17:37:46 +08:00
var ins = MainActivity.ins;
if(ins!=null && ! ins.avas.isEmpty()) name = ins.avas.get(ins.curAva).name;
2024-03-14 20:50:46 +08:00
Util.println("Server getProgramName. <-"+name);
return name;
} catch (Exception e) {
Util.printStackTrace(e);
return null;
}
2023-11-09 08:37:59 +08:00
}
@Override
public String getVersion() throws RemoteException {
2024-11-11 18:22:45 +08:00
Util.println("Server getVersion. <-"+null);
2023-11-09 08:37:59 +08:00
return null;
}
@Override
2024-11-11 18:22:45 +08:00
public void setScreenWidth(int width) throws RemoteException {
Util.println("Server setScreenWidth. ->"+width);
2023-11-09 08:37:59 +08:00
}
@Override
2024-11-11 18:22:45 +08:00
public void setScreenHeight(int height) throws RemoteException {
Util.println("Server setScreenHeight. ->"+height);
2023-11-09 08:37:59 +08:00
}
@Override
public void taskScreenshot(String cmdId) throws RemoteException {
2024-11-11 18:22:45 +08:00
Util.println("Server taskScreenshot. ->"+cmdId);
2023-11-09 08:37:59 +08:00
}
@Override
public void setExternalTemperature(float t) throws RemoteException {
}
@Override
public void setInternalTemperature(float t) throws RemoteException {
}
@Override
public void setHumidity(float h) throws RemoteException {
}
@Override
public boolean forcePlayProgram(String pid) throws RemoteException {
return false;
}
@Override
public boolean finishForcePlay() throws RemoteException {
return false;
}
@Override
public String getCurProgramId() throws RemoteException {
2024-01-24 20:17:59 +08:00
Util.println("Server getCurProgramId ...");
2023-11-09 08:37:59 +08:00
return null;
}
@Override
public void setUSBProgramPwd(String pwd) throws RemoteException {
}
2025-12-23 16:40:05 +08:00
SimpleDateFormat fmt = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
2023-11-09 08:37:59 +08:00
@SuppressLint("ResourceType")
@Override
public String executeJosnCommand(String jsonstr) throws RemoteException {
2025-07-28 19:14:00 +08:00
Util.println("AIDL executeJsonCommand ..."+jsonstr);//{"_type":"DeleteTask","id":"652522a0e81d1e000009201a","sendTo":"yzd-player"}
2024-01-24 20:17:59 +08:00
String commandId = null;
2023-11-09 08:37:59 +08:00
try {
2024-01-24 20:17:59 +08:00
var jsonBytes = jsonstr.getBytes(StandardCharsets.UTF_8);
var json = JSMap.from(jsonBytes);
2023-11-09 08:37:59 +08:00
var _type = json.stnn("_type");
2024-01-24 20:17:59 +08:00
commandId = json.stnn("id");
2024-03-12 18:28:10 +08:00
if(_type.equals("PlayXixunTask") || _type.equals("PlayProgramTask")) {
2024-01-24 20:17:59 +08:00
var preDownloadURL = json.str("preDownloadURL");
2025-07-28 19:14:00 +08:00
if(preDownloadURL==null) {
Util.println(" serverURL "+Util.serverURL);
preDownloadURL = Util.serverURL+"file/download?id=";
}
2024-01-24 20:17:59 +08:00
var task = json.jsmap("task");
JSList<JSMap> jpages = task.jslist("items");
int proSize = 0;
var needDowns = new ArrayList<NeedDowns>();
var hases = new HashSet<String>();
for(var pageMap : jpages) {
var _program = pageMap.jsmap("_program");
if(_program==null) continue;
proSize += _program.intg("totalSize");
var needDown = new NeedDowns();
needDown.prog = pageMap.stnn("_id");
needDowns.add(needDown);
JSList<JSMap> layers = _program.jslist("layers");
if(layers==null || layers.isEmpty()) continue;
for(int ll=layers.size()-1; ll>=0; ll--) {
var layer = new Prog.Layer();
JSList<JSMap> sources = layers.get(ll).jslist("sources");
// var border = layers.get(ll).jsmap("border");
// if(border!=null) {
// }
for(var source : sources) {
var type = source.stnn("_type");
if(type.equals("Video") || type.equals("Audio") || type.equals("Image") || type.startsWith("DigitalClock") || type.equals("Timer") || type.startsWith("Environ")) {
var filename = source.str("id");
if(filename==null) continue;
var url = source.stnn("url");
2024-03-26 15:15:57 +08:00
if(! url.startsWith("http")) url = preDownloadURL + filename;
2024-01-24 20:17:59 +08:00
var file = new File(Util.programDir+"/"+filename);
if(file.exists() && file.length() > 0) {
proSize -= file.length();
hases.add(filename);
} else needDown.srcs.add(new Src(filename, url));
2024-02-02 18:09:54 +08:00
} else if(type.startsWith("MultiPng") || type.equals("SplitText")) {
2024-01-24 20:17:59 +08:00
JSList<JSMap> imgs = source.jslist("arrayPics");
if(imgs.isEmpty()) continue;
for(var img : imgs) {
var filename = img.str("id");
if(filename==null) continue;
var url = img.stnn("url");
2024-03-26 15:15:57 +08:00
if(! url.startsWith("http")) url = preDownloadURL + filename;
2024-01-24 20:17:59 +08:00
var file = new File(Util.programDir+"/"+filename);
if(file.exists() && file.length() > 0) {
proSize -= file.length();
hases.add(filename);
} else needDown.srcs.add(new Src(filename, url));
}
}
}
}
}
int finalProSize = proSize;
String finalCommandId = commandId;
var notificationURL = json.str("notificationURL");
2025-07-28 19:14:00 +08:00
var downMs = System.currentTimeMillis();
Util.downId = downMs;
2024-01-24 20:17:59 +08:00
new Thread(()->{
Util.deleteFiles(finalProSize, hases);
for(var needDown : needDowns) {
int cnt = 0;
for(var src : needDown.srcs) {
2025-07-28 19:14:00 +08:00
for(int t=0; ; t++) {
try {
Util.println("download "+src.url);
var conn = new URLConn(src.url);
if(t>0) {
var len = new File(Util.programDir+"/"+src.filename).length();
conn.setHeader("Range", "bytes="+len+"-");
2025-12-23 16:40:05 +08:00
Util.println(" Range: bytes="+len+"-");
2025-07-28 19:14:00 +08:00
}
var input = conn.in();
var code = conn.hconn().getResponseCode();
2025-12-23 16:40:05 +08:00
Util.println(" "+code+" - "+conn.hconn().getResponseMessage());
if(code==200 || code==206) {
var fOut = new FileOutputStream(Util.programDir+"/"+src.filename, code==206);
IOs.writeCloseIn(fOut, input);
fOut.flush();
fOut.getFD().sync();
fOut.close();
break;
} else {
Util.println(" body "+IOs.readStrClose(input));
Util.println(" size: "+new File(Util.programDir+"/"+src.filename).length()+" "+src.filename);
}
2025-07-28 19:14:00 +08:00
} catch (Throwable e) {
Util.println(Util.toStr(e));
}
if(t>=7200 || Util.downId!=downMs) return;
try {
Thread.sleep(5000);
} catch (Exception ignored) {}
2024-01-24 20:17:59 +08:00
}
2025-07-28 19:14:00 +08:00
2024-01-24 20:17:59 +08:00
cnt++;
2024-05-11 21:12:39 +08:00
var progress = cnt*100/needDown.srcs.size();
if(cnt != needDown.srcs.size()) {
if(notificationURL==null) {
var intent = new Intent("xixun.intent.action.REPLY");
intent.putExtra("reply", new TaskProgressReply(finalCommandId, needDown.prog, progress, 500, 3));
2025-07-28 19:14:00 +08:00
sendBroadcast(intent);
2024-05-11 21:12:39 +08:00
} else {
try {
new URLConn(notificationURL).timeout(5000).writeJson(new JSMap(
"commandId", finalCommandId,
"taskItemId", needDown.prog,
"progress", progress).toStr()).read();
2025-07-28 19:14:00 +08:00
} catch (Throwable e) {
2024-05-11 21:12:39 +08:00
Util.printStackTrace(e);
}
2024-01-24 20:17:59 +08:00
}
}
}
2024-05-11 21:12:39 +08:00
if(notificationURL==null) {
var intent = new Intent("xixun.intent.action.REPLY");
intent.putExtra("reply", new TaskProgressReply(finalCommandId, needDown.prog, 100, 400, 0));
2025-07-28 19:14:00 +08:00
sendBroadcast(intent);
2024-05-11 21:12:39 +08:00
} else {
2024-01-24 20:17:59 +08:00
try {
new URLConn(notificationURL).timeout(5000).writeJson(new JSMap(
"commandId", finalCommandId,
"taskItemId", needDown.prog,
"progress", 100).toStr()).read();
2025-07-28 19:14:00 +08:00
} catch (Throwable e) {
2024-01-24 20:17:59 +08:00
Util.printStackTrace(e);
}
}
}
2025-07-28 19:14:00 +08:00
if(Util.downId!=downMs) return;
var acti = MainActivity.ins;
if(acti!=null) acti.runOnUiThread(() -> acti.initProg(jsonBytes));
else {
try {
var fOut = new FileOutputStream(Util.programDir + "/program");
fOut.write(jsonBytes);
2025-12-23 16:40:05 +08:00
var spaces = " ".getBytes();
for(int i=0;i<1000; i++) fOut.write(spaces);
2025-07-28 19:14:00 +08:00
fOut.flush();
fOut.getFD().sync();
fOut.close();
} catch (Throwable e) {
Util.printStackTrace(e);
}
var intent = new Intent(AIDLService.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
AIDLService.this.startActivity(intent);
}
var intent = new Intent("com.xixun.AccessibilityService");
intent.putExtra("newProgram", "platform");
sendBroadcast(intent);
2024-01-24 20:17:59 +08:00
}).start();
2023-12-01 16:17:06 +08:00
return new JSMap(
2024-01-24 20:17:59 +08:00
"_type", "Success",
2023-12-01 16:17:06 +08:00
"result", "received command",
"cardId", Util.getCardId(),
2024-01-24 20:17:59 +08:00
"commandId", commandId
2023-12-01 16:17:06 +08:00
).toString();
} else if(_type.equals("DeleteTask")) {
2025-07-28 19:14:00 +08:00
Util.downId = 0;
2023-11-09 08:37:59 +08:00
MainActivity.ins.runOnUiThread(() -> MainActivity.ins.delProgFile());
return new JSMap(
"_type", "DataCallback",
"result", "received command",
"cardId", Util.getCardId(),
2024-01-24 20:17:59 +08:00
"commandId", commandId
2023-11-09 08:37:59 +08:00
).toString();
} else if(_type.equals("PlayerStateCommand")) {
var descs = Util.getState(MainActivity.ins.state);
return new JSMap(
"_type", "Success",
"cardId", Util.getCardId(),
2024-01-24 20:17:59 +08:00
"commandId", commandId,
2023-11-09 08:37:59 +08:00
"code", MainActivity.ins.state,
"des_en", descs[0],
"des", descs[1]
).toString();
} else if(_type.equals("SetPlayerBackground")) {
var url = json.str("url");
if(url==null) new File(Util.backImgFile).delete();
2024-03-05 16:50:07 +08:00
else {
var fout = new FileOutputStream(Util.backImgFile);
IOs.write(fout, new URLConn(url).in());
fout.flush();
fout.getFD().sync();
fout.close();
}
2023-11-09 08:37:59 +08:00
MainActivity.ins.runOnUiThread(() -> {
2024-05-11 21:12:39 +08:00
MainActivity.ins.backView.cosImg = url==null ? null : BitmapFactory.decodeFile(Util.backImgFile, Util.noScaled);
2023-11-09 08:37:59 +08:00
MainActivity.ins.backView.invalidate();
});
return new JSMap(
"_type", "Success",
"cardId", Util.getCardId(),
2024-01-24 20:17:59 +08:00
"commandId", commandId
2023-11-09 08:37:59 +08:00
).toString();
} else if(_type.equals("GetPlayerBackground")) {
var backImg = new File(Util.backImgFile);
var img = Base64.getEncoder().encodeToString(IOs.readBytesClose(backImg.exists() ? new FileInputStream(backImg) : MainActivity.ins.getResources().openRawResource(R.drawable.back)));
return new JSMap(
"_type", "DataCallback",
"cardId", Util.getCardId(),
2024-01-24 20:17:59 +08:00
"commandId", commandId,
2023-11-09 08:37:59 +08:00
"img", img
).toString();
2024-05-09 20:30:22 +08:00
} else if(_type.equalsIgnoreCase("getProgramTask")) {
var task = JSMap.fromClose(new FileInputStream(Util.programDir+"/program")).jsmap("task");
return new JSMap(
"_type", "ProgramTaskCallback",
"cardId", Util.getCardId(),
"commandId", commandId,
"task", task
).toString();
2025-07-28 19:14:00 +08:00
} else if(_type.equalsIgnoreCase("UploadPlayLogs")) {
var url = json.str("url");
Util.logOn = url!=null;
Util.cfg.put("logUploadUrl", url);
Util.cfg.put("logUploadInterval", json.intg("interval", 60));
try (var fOut = new FileOutputStream(Util.programDir + "/cfg")) {
Util.cfg.write(fOut);
fOut.flush();
fOut.getFD().sync();
}
return new JSMap(
"_type", "Success",
"cardId", Util.getCardId(),
"commandId", commandId
).toString();
2025-12-23 16:40:05 +08:00
} else if(_type.equalsIgnoreCase("GetLog")) {
return new JSMap(
"_type", "Success",
"cardId", Util.getCardId(),
"commandId", commandId,
"text", Util.buf.toString()
).toString();
} else if(_type.equalsIgnoreCase("ListProgFiles")) {
var files = new File(Util.programDir).listFiles();
var writer = new StringBuilder();
if(files!=null) {
Arrays.sort(files, (f1, f2) -> Long.signum(f2.lastModified() - f1.lastModified()));
for(var file : files) writer.append(fmt.format(new Date(file.lastModified()))).append(' ').append(file.getName()).append(' ').append(String.valueOf(file.length())).append('\n');
}
return new JSMap(
"_type", "Success",
"cardId", Util.getCardId(),
"commandId", commandId,
"text", writer.toString()
).toString();
} else if("GetJson".equalsIgnoreCase(_type)) {
var inse = new File(Util.programDir+"/insert");
var files = new File(Util.programDir).listFiles();
var writer = new StringBuilder();
if(inse.isFile()) {
var text = IOs.readStrClose(new FileInputStream(inse)).trim();
if(files!=null) for(var file : files) if(! "program".equals(file.getName())) text = text.replace("\""+file.getName()+"\"", "\""+file.getName()+"\"/*"+file.length()+" "+fmt.format(new Date(file.lastModified()))+"*/");
writer.append("insert:\n");
writer.append(text);
if(! text.endsWith("\n")) writer.append("\n");
}
var prog = new File(Util.programDir+"/program");
if(! prog.isFile()) writer.append("'program' file not exist");
else {
var text = IOs.readStrClose(new FileInputStream(prog)).trim();
if(files!=null) for(var file : files) if(! "program".equals(file.getName())) text = text.replace("\""+file.getName()+"\"", "\""+file.getName()+"\"/*"+file.length()+" "+fmt.format(new Date(file.lastModified()))+"*/");
writer.append("\nprogram:\n");
writer.append(text);
}
return new JSMap(
"_type", "Success",
"cardId", Util.getCardId(),
"commandId", commandId,
"text", writer.toString()
).toString();
2023-11-09 08:37:59 +08:00
}
return new JSMap(
"_type", "Error",
2024-05-09 20:30:22 +08:00
"errorMessage", "Unknown Type: "+_type,
2023-11-09 08:37:59 +08:00
"cardId", Util.getCardId(),
2024-01-24 20:17:59 +08:00
"commandId", commandId
2023-11-09 08:37:59 +08:00
).toString();
} catch (Exception e) {
2024-01-24 20:17:59 +08:00
Util.printStackTrace(e);
2023-11-09 08:37:59 +08:00
return new JSMap(
"_type", "Error",
2024-05-09 20:30:22 +08:00
"errorMessage", Util.toStr(e),
2023-11-09 08:37:59 +08:00
"cardId", Util.getCardId(),
2024-01-24 20:17:59 +08:00
"commandId", commandId
2023-11-09 08:37:59 +08:00
).toString();
}
}
@Override
public void pausePlayer(boolean b) throws RemoteException {
}
@Override
public boolean isPause() throws RemoteException {
return false;
}
@Override
public boolean clearTasks() throws RemoteException {
2023-12-01 16:17:06 +08:00
MainActivity.ins.runOnUiThread(() -> MainActivity.ins.delProgFile());
2023-11-09 08:37:59 +08:00
return true;
}
@Override
public int countOfPrograms(int type) throws RemoteException {
2024-07-04 17:45:56 +08:00
var cnt = MainActivity.ins!=null && MainActivity.ins.progView!=null ? MainActivity.ins.progView.pages.size() : 0;
Util.println("Server countOfPrograms. <-"+cnt);
return cnt;
2023-11-09 08:37:59 +08:00
}
@Override
public void playInsertTask(String pid) throws RemoteException {
}
@Override
public void stopInsertTask(String pid) throws RemoteException {
}
@Override
public String getProgramTask() throws RemoteException {
2024-03-26 15:15:57 +08:00
try {
2025-12-23 16:40:05 +08:00
return IOs.readStrClose(new FileInputStream(Util.programDir+"/program")).trim();
2024-03-26 15:15:57 +08:00
} catch (Exception e) {
Util.printStackTrace(e);
return Util.toStr(e);
}
2023-11-09 08:37:59 +08:00
}
@Override
public void setUploadLogUrl(String playLog) throws RemoteException {
}
@Override
public String getUploadLogUrl() throws RemoteException {
return null;
}
};
2024-01-24 20:17:59 +08:00
static class Src {
String filename;
String url;
public Src(String filename, String url) {
this.filename = filename;
this.url = url;
}
}
static class NeedDowns {
String prog;
ArrayList<Src> srcs = new ArrayList<>();
}
2023-11-09 08:37:59 +08:00
}