This commit is contained in:
Gangphon 2024-06-06 22:11:01 +08:00
parent db4c46e7e9
commit d6917767b4
2 changed files with 39 additions and 19 deletions

View File

@ -11,7 +11,7 @@ android {
minSdk 21 minSdk 21
targetSdk 34 targetSdk 34
versionCode 1 versionCode 1
versionName "2.1.1" versionName "2.1.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }

View File

@ -20,8 +20,10 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.Socket; import java.net.Socket;
import java.net.SocketTimeoutException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -37,6 +39,8 @@ public class SocketThread extends Thread {
Socket socket; Socket socket;
InputStream in; InputStream in;
OutputStream out; OutputStream out;
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd HH:mm:ss");
public SocketThread(MainActivity main, Socket socket) { public SocketThread(MainActivity main, Socket socket) {
this.main = main; this.main = main;
this.socket = socket; this.socket = socket;
@ -48,22 +52,26 @@ public class SocketThread extends Thread {
Util.socketThreads.add(this); Util.socketThreads.add(this);
in = socket.getInputStream(); in = socket.getInputStream();
out = socket.getOutputStream(); out = socket.getOutputStream();
HashSet<String> hases = null; HashSet<String> existed = null;
ByteArrayOutputStream progJson = null; ByteArrayOutputStream progJson = null;
while(true) { while(true) {
var obj = JSMap.from(in); var obj = JSMap.from(in);
var _type = obj.stnn("_type"); var _type = obj.stnn("_type");
Util.println("_type: "+_type); Util.println("_type: "+_type);
if("consult".equals(_type)) { if("consult".equals(_type)) {
JSList<String> ids = obj.jslist("idList"); JSList<JSMap> files = obj.jslist("files");
hases = new HashSet<>(); if(files==null) new JSMap("_type", _type, "idList", obj.jslist("idList")).write(out);
if(ids!=null) for(int i=0; i<ids.size(); i++) { else {
if(new File(Util.programDir + "/" + ids.get(i)).exists()) ids.remove(i--); existed = new HashSet<>();
else hases.add(ids.get(i)); for(var fil : files) {
var name = fil.stnn("name");
var file = new File(Util.programDir + "/" + name);
if(file.isFile() && file.length()==fil.intg("size")) existed.add(name);
}
new JSMap("_type", _type, "existed", existed).write(out);
} }
new JSMap("_type", _type, "idList", ids).write(out);
} else if("proStart".equals(_type)) { } else if("proStart".equals(_type)) {
Util.deleteFiles(obj.intg("proSize"), hases); Util.deleteFiles(obj.intg("proSize"), existed);
} else if("fileStart".equals(_type)) { } else if("fileStart".equals(_type)) {
var size = obj.intg("size"); var size = obj.intg("size");
var name = obj.stnn("id"); var name = obj.stnn("id");
@ -73,10 +81,15 @@ public class SocketThread extends Thread {
IOs.writeCloseOut(progJson, in, size); IOs.writeCloseOut(progJson, in, size);
} else { } else {
var fOut = new FileOutputStream(Util.programDir + "/" + name); var fOut = new FileOutputStream(Util.programDir + "/" + name);
IOs.write(fOut, in, size); try {
fOut.flush(); IOs.write(fOut, in, size);
fOut.getFD().sync(); fOut.flush();
fOut.close(); fOut.getFD().sync();
fOut.close();
} catch(Throwable e) {
fOut.close();
new File(Util.programDir + "/" + name).delete();
}
} }
} else if("imgFileStart".equals(_type)) { } else if("imgFileStart".equals(_type)) {
var size = obj.intg("size"); var size = obj.intg("size");
@ -127,8 +140,8 @@ public class SocketThread extends Thread {
var Fmt = new SimpleDateFormat("yy-MM-dd HH:mm:ss.SSS"); var Fmt = new SimpleDateFormat("yy-MM-dd HH:mm:ss.SSS");
var dur = 0; var dur = 0;
if(main.progView!=null) for(var page : main.progView.pages) dur += page.tDur; if(main.progView!=null) for(var page : main.progView.pages) dur += page.tDur;
writer.append("ProgSend: ").append(Fmt.format(new File(Util.programDir + "/program").lastModified())).append(" ProgDur: ").append(String.valueOf(dur)); writer.append("ProgSend: ").append(Fmt.format(new File(Util.programDir + "/program").lastModified())).append(" ProgDur: ").append(String.valueOf(dur)).append("ms");
if(main.progView!=null) writer.append(" Pages: ").append(String.valueOf(main.progView.avas.size())).append(" / ").append(String.valueOf(main.progView.pages.size())); if(main.progView!=null) writer.append(" Size Avas: ").append(String.valueOf(main.progView.avas.size())).append(" Pages: ").append(String.valueOf(main.progView.pages.size()));
writer.append("\n"); writer.append("\n");
writer.append(" Launch: ").append(Fmt.format(main.launchMilli)).append("\n"); writer.append(" Launch: ").append(Fmt.format(main.launchMilli)).append("\n");
writer.append(" Sync: ").append(Fmt.format(main.syncMs)).append("\n"); writer.append(" Sync: ").append(Fmt.format(main.syncMs)).append("\n");
@ -207,20 +220,27 @@ public class SocketThread extends Thread {
if(files == null) return; if(files == null) return;
Arrays.sort(files, (f1, f2) -> (int) (f2.lastModified() - f1.lastModified())); Arrays.sort(files, (f1, f2) -> (int) (f2.lastModified() - f1.lastModified()));
var writer = new OutputStreamWriter(out); var writer = new OutputStreamWriter(out);
for(var file : files) writer.append(file.getName()).append(' ').append(String.valueOf(file.length())).append('\n'); for(var file : files) writer.append(fmt.format(new Date(file.lastModified()))).append(' ').append(file.getName()).append(' ').append(String.valueOf(file.length())).append('\n');
writer.append('\n'); writer.append('\n');
writer.flush(); writer.flush();
} else if("GetFile".equals(_type)) { } else if("GetFile".equals(_type)) {
var name = obj.str("name"); var name = obj.str("name");
if(name!=null) IOs.writeCloseIn(out, new FileInputStream(Util.programDir+"/"+name)); if(name==null) new JSMap("msg", "name is null").write(out);
else new JSMap("code", 1, "msg", "name is null").write(out); else {
var file = new File(Util.programDir+"/"+name);
if(! file.isFile()) new JSMap("msg", "file not exist").write(out);
else {
new JSMap("len", file.length()).write(out);
IOs.writeCloseIn(out, new FileInputStream(file));
}
}
} }
out.flush(); out.flush();
Util.println("cmd end"); Util.println("cmd end");
} }
} catch (Throwable e) { } catch (Throwable e) {
var emsg = e.getMessage(); var emsg = e.getMessage();
if(emsg!=null && ("Socket closed".equals(emsg) || emsg.endsWith("end-of-input"))) { if(e instanceof SocketTimeoutException || "Socket closed".equals(emsg) || (emsg!=null && emsg.endsWith("end-of-input"))) {
Util.println(emsg); Util.println(emsg);
return; return;
} }