This commit is contained in:
Gangphon 2024-01-26 21:35:38 +08:00
parent df9c61aa55
commit ce66727d0f
2 changed files with 80 additions and 21 deletions

View File

@ -35,6 +35,7 @@ import com.xixun.joey.aidlset.CardService;
import net.lingala.zip4j.ZipFile;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -50,6 +51,7 @@ import java.util.HashSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import gnph.util.Chsets;
import gnph.util.IOs;
import gnph.util.JSList;
import gnph.util.JSMap;
@ -153,11 +155,23 @@ public class MainActivity extends ComponentActivity implements Choreographer.Fra
try {
var zip = new ZipFile(path+"/program.zip");
if(zip.isEncrypted()) zip.setPassword(pass);
var json = JSMap.fromClose(zip.getInputStream(zip.getFileHeader("program")));
var isIns = json.bool("isIns");
long size = 0;
ByteArrayOutputStream progJson = null;
var headers = zip.getFileHeaders();
for(var header : headers) size += header.getUncompressedSize();
for(var header : headers) {
size += header.getUncompressedSize();
if("program".equals(header.getFileName())) {
progJson = new ByteArrayOutputStream();
IOs.writeClose(progJson, zip.getInputStream(header));
}
}
if(progJson==null) {
Util.println("No program File");
runOnUiThread(() -> {
Util.makeText(MainActivity.this, "No program File").show();
});
return;
}
if(size==0) {
Util.println("zip size is 0");
runOnUiThread(() -> {
@ -166,11 +180,12 @@ public class MainActivity extends ComponentActivity implements Choreographer.Fra
return;
}
Util.deleteFiles(size, null);
for(var header : headers) zip.extractFile(header, Util.programDir);
for(var header : headers) if(! "program".equals(header.getFileName())) zip.extractFile(header, Util.programDir);
var json = progJson.toByteArray();
runOnUiThread(() -> {
Util.println("Import Succeed");
Util.makeText(MainActivity.this, "Import Succeed").show();
initProg();
initProg(json);
});
} catch (Exception e) {
Util.printStackTrace(e);
@ -304,6 +319,45 @@ public class MainActivity extends ComponentActivity implements Choreographer.Fra
Util.printStackTrace(e);
}
}
public void initProg(byte[] json) {
try {
Util.println("\nParse Prog Json");
var task = JSMap.from(json).jsmap("task");
if(task==null) {
state = 7;
Util.println(" Error: task==null\n");
Util.println(new String(json, Chsets.UTF8));
return;
}
var view = new Prog(task, this);
if(view.getChildCount()==0) {
state = 7;
Util.println(" Error: ChildCount==0\n");
Util.println(new String(json, Chsets.UTF8));
return;
}
if(progView!=null) progView.release();
progView = view;
setContentView(progView);
var fOut = new FileOutputStream(Util.programDir + "/program");
fOut.write(json);
fOut.flush();
fOut.getFD().sync();
fOut.close();
state = 5;
Util.println("Init Sync");
syncProg((System.currentTimeMillis()+999)/1000*1000);
if(canAdd) {
choreographer.postFrameCallback(this);
canAdd = false;
}
} catch (Throwable e) {
state = 7;
Util.makeText(this, Util.toStr(e)).show();
Util.printStackTrace(e);
Util.println(new String(json, Chsets.UTF8));
}
}
Choreographer choreographer = Choreographer.getInstance();
int curAva, curTimes = 1;
@ -337,7 +391,7 @@ public class MainActivity extends ComponentActivity implements Choreographer.Fra
if(Util.buf.length()>1000000) Util.buf.replace(0, 100000, "");
Util.println("isDiff: "+isDiff+" endMs: "+lastPage.endMilli+" millis:"+milli);
syncProg(isDiff ? milli : lastPage.endMilli);
Util.println("after. curAva: "+curAva+" endMs: "+lastPage.endMilli);
Util.println("after. curAva: "+curAva+" endMs: "+page(curAva).endMilli);
choreographer.postFrameCallback(this);
canAdd = false;
return;
@ -345,7 +399,7 @@ public class MainActivity extends ComponentActivity implements Choreographer.Fra
}
}
page(curAva).setMillis(lastPage.endMilli);
Util.println("curAva: "+curAva+" endMs: "+lastPage.endMilli);
Util.println("curAva: "+curAva+" endMs: "+page(curAva).endMilli);
} else {
for(var layer : lastPage.layers) {
for(var src : layer.srcs) {
@ -393,7 +447,7 @@ public class MainActivity extends ComponentActivity implements Choreographer.Fra
} while(curAva < progView.avas.size() && start<=milli);
start -= page(--curAva).tDur;
syncMs = milli;
Util.println("Sync. milli: "+milli+" start: "+start+" diff: "+(milli - start));
Util.println("Sync. dur: "+dur+" milli: "+milli+" start: "+start+" diff: "+(milli - start));
}
page(curAva).setMillis(start);
if(state != 6) {
@ -420,6 +474,7 @@ public class MainActivity extends ComponentActivity implements Choreographer.Fra
in = socket.getInputStream();
out = socket.getOutputStream();
HashSet<String> hases = null;
ByteArrayOutputStream progJson = null;
while(true) {
var obj = JSMap.from(in);
var _type = obj.stnn("_type");
@ -438,11 +493,16 @@ public class MainActivity extends ComponentActivity implements Choreographer.Fra
var size = obj.intg("size");
var name = obj.stnn("id");
Util.println(" size: " + size + " name: " + name);
var fout = new FileOutputStream(Util.programDir + "/" + name);
IOs.write(fout, in, size);
fout.flush();
fout.getFD().sync();
fout.close();
if(name.equals("program")) {
progJson = new ByteArrayOutputStream();
IOs.writeCloseOut(progJson, in, size);
} else {
var fOut = new FileOutputStream(Util.programDir + "/" + name);
IOs.write(fOut, in, size);
fOut.flush();
fOut.getFD().sync();
fOut.close();
}
} else if("imgFileStart".equals(_type)) {
var size = obj.intg("size");
var fout = new FileOutputStream(Util.backImgFile);
@ -457,8 +517,12 @@ public class MainActivity extends ComponentActivity implements Choreographer.Fra
} else if("imgFileEnd".equals(_type)) {
new JSMap("success", true).write(out);
} else if("proEnd".equals(_type)) {
new JSMap("success", true).write(out);
runOnUiThread(this::initProg);
new JSMap("success", progJson!=null).write(out);
if(progJson!=null) {
var json = progJson.toByteArray();
progJson = null;
runOnUiThread(() -> initProg(json));
}
} else if("DelPrograms".equals(_type)) {
var latch = new CountDownLatch(1);
var ok = new AtomicBoolean(false);

View File

@ -204,12 +204,7 @@ public class Server extends Service {
}
}
}
try {
IOs.writeClose(new FileOutputStream(Util.programDir+"/program"), jsonBytes);
MainActivity.ins.runOnUiThread(MainActivity.ins::initProg);
} catch (Exception e) {
Util.printStackTrace(e);
}
MainActivity.ins.runOnUiThread(() -> MainActivity.ins.initProg(jsonBytes));
}).start();
return new JSMap(
"_type", "Success",