/*
自动连拍_2.0
说明:
※ 次数不能小于等于 1 次,只能为整数;
※ 间隔时间不能小于 5 秒;
※ 归类路径不能为文件和只读路径。
*/
auto();
console.show();
let fs, ID = device.getAndroidId();
const pn = getPackageName("相机"),
      S = storages.create("shoot" + pn + ID),
      DIR = "/sdcard/DCIM/Camera/",
      FS = files.listDir(DIR).join("\n");
if (!launchPackage(pn)) exit();
console.setPosition(0, 0);
console.setSize(device.width, device.height / 3);
if (!(S.contains("x") && S.contains("y"))) {
    toastLog("首次启动,请点击拍照按钮!");
    threads.start(function(){
        events.observeTouch();
        events.onTouch(function(p){
            log("点击:" + p.x + ", " + p.y);
            ID = new Array();
            ID[0] = setInterval(function(){
                fs = files.listDir(DIR).join("\n");
                if (fs.length > FS.length) {
                    log("记录坐标:%d, %d", p.x, p.y);
                    S.put("x", p.x);
                    S.put("y", p.y);
                }
            }, 500);"
            ID[1] = setTimeout(function(){
                clearInterval(ID[0]);
            }, 5 * 1000);
        });
    });
    let THREAD = threads.start(function(){
        while (!S.contains("x")) sleep(10);
        while (!S.contains("y")) sleep(10);
        clearInterval(ID[0]);
        clearTimeout(ID[1]);
        events.removeAllTouchListeners();        
        threads.shutDownAll();
    });
    THREAD.join();
    toastLog("请退出相机,重新启动脚本!");
    fs = files.listDir(DIR).join("\n");
    while (currentPackage() == pn) sleep(500);
    files.remove(DIR + nowFiles(fs));
    exit();
}
const x = S.get("x"),
      y = S.get("y");
log("读取坐标:%d, %d", x, y);
threads.start(function(){
    while (currentPackage() == pn) sleep(100);
    console.hide();
    exit();
});
let amount, interval;
while (!0) {
    amount = rawInput("请输入拍照张数:");
    if (amount > 1 && !isNaN(amount)) break;
}
while (!0){
    interval = rawInput("请输入间隔时间(秒):");
    if (interval >= 5 && !isNaN(interval)) break;
}
while (!0) {
    ID = rawInput("请输入归类路径:");
    if (ID && !files.isFile(ID)) break;
}
threads.shutDownAll();
if (ID.substr(-1) != "/") ID += "/";
files.createWithDirs(ID + ".");
log("拍照张数:" + amount);
log("间隔时间:" + interval + " 秒");
log("归类路径:" + ID);
if (!confirm("准备好了吗?")) {
    toastLog("已停止脚本!");
    exit();
}
device.keepScreenOn();
log("若要中途退出,请按返回键 <Back>");
sleep(100);
THREAD = threads.start(function(){
    for (let i = amount; i; i--) {
        log("进度:%d / %d", amount - i + 1, amount);
        click(x, y);
        if (i - 1) sleep(interval * 1000);
    }
});
threads.start(function(){
    events.observeKey();
    events.onKeyDown("back", function(){
        THREAD.interrupt();
    });
    events.on("exit", function(){
        device.cancelKeepingAwake();
    });
});
THREAD.join();
threads.shutDownAll();
toastLog("拍照结束!");
log("请退出相机,将开始归类!");
while (currentPackage() == pn) sleep(500);
fs = files.listDir(DIR).join("\n");
fs = nowFiles(fs);
log("开始归类照片!");
for (let i = 0; i < fs.length; i++) {
    log("进度:%d / %d\n%s", i + 1, fs.length, fs[i]);
    while(!files.move(DIR + fs[i], ID + fs[i]));
}
toastLog("照片归类完毕!");
device.cancelKeepingAwake();



function nowFiles(f) {
    const now = f.split("\n"),
          old = FS.split("\n");
    let c = new Array();
    for (let i = 0; i < now.length; i++) {
        let d = !0;
        for (let j = 0; j < old.length; j++) {
            if (now[i] == old[j]) {
                d = !d;
                break;
            }
        }
        if (d) c.push(now[i]);
    }
    return c;
}