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

339 lines
14 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;
import com.xixun.util.PlayerInfo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
2024-01-24 20:17:59 +08:00
import java.io.IOException;
2023-11-09 08:37:59 +08:00
import java.nio.charset.StandardCharsets;
2024-01-24 20:17:59 +08:00
import java.util.ArrayList;
2023-11-09 08:37:59 +08:00
import java.util.Base64;
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;
public class Server extends Service {
@Override
public IBinder onBind(Intent intent) {
return binder;
}
PlayerInfo.Stub binder = new PlayerInfo.Stub() {
@Override
public String getProgramName() throws RemoteException {
2024-01-24 20:17:59 +08:00
Util.println("Server getProgramName ...");
2023-11-09 08:37:59 +08:00
return null;
}
@Override
public String getVersion() throws RemoteException {
return null;
}
@Override
public void setScreenWidth(int w) throws RemoteException {
}
@Override
public void setScreenHeight(int h) throws RemoteException {
}
@Override
public void taskScreenshot(String cmdId) throws RemoteException {
}
@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 {
}
@SuppressLint("ResourceType")
@Override
public String executeJosnCommand(String jsonstr) throws RemoteException {
2024-01-24 20:17:59 +08:00
Util.println("Server executeJsonCommand ..."+jsonstr);//{"_type":"DeleteTask","id":"652522a0e81d1e000009201a","sendTo":"yzd-player"}
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");
boolean is2 = false;
if(_type.equals("PlayXixunTask") || (is2 = _type.equals("PlayProgramTask"))) {
var preDownloadURL = json.str("preDownloadURL");
if(preDownloadURL==null && is2) preDownloadURL = "https://m2mled.net/file/download?id=";
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");
if(! url.startsWith("http")) {
if(preDownloadURL==null) continue;
url = preDownloadURL + filename;
}
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");
if(! url.startsWith("http")) {
if(preDownloadURL==null) continue;
url = preDownloadURL + filename;
}
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");
new Thread(()->{
Util.deleteFiles(finalProSize, hases);
for(var needDown : needDowns) {
int cnt = 0;
for(var src : needDown.srcs) {
try {
var in = new URLConn(src.url).in();
var fout = new FileOutputStream(Util.programDir+"/"+src.filename);
IOs.writeCloseIn(fout, in);
fout.flush();
fout.getFD().sync();
fout.close();
} catch (Exception e) {
Util.printStackTrace(e);
}
cnt++;
if(notificationURL!=null && cnt!=needDown.srcs.size()) {
try {
new URLConn(notificationURL).timeout(5000).writeJson(new JSMap(
"commandId", finalCommandId,
"taskItemId", needDown.prog,
"progress", cnt*100/needDown.srcs.size()).toStr()).read();
} catch (IOException e) {
Util.printStackTrace(e);
}
}
}
if(notificationURL!=null) {
try {
new URLConn(notificationURL).timeout(5000).writeJson(new JSMap(
"commandId", finalCommandId,
"taskItemId", needDown.prog,
"progress", 100).toStr()).read();
} catch (IOException e) {
Util.printStackTrace(e);
}
}
}
2024-01-26 21:35:38 +08:00
MainActivity.ins.runOnUiThread(() -> MainActivity.ins.initProg(jsonBytes));
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")) {
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(() -> {
MainActivity.ins.backView.cosImg = url==null ? null : BitmapFactory.decodeFile(Util.backImgFile);
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();
}
return new JSMap(
"_type", "Error",
"errorMessage", "Unknow Type",
"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",
"errorMessage", e.toString(),
"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 {
return 0;
}
@Override
public void playInsertTask(String pid) throws RemoteException {
}
@Override
public void stopInsertTask(String pid) throws RemoteException {
}
@Override
public String getProgramTask() throws RemoteException {
return null;
}
@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
}