|
| 1 | +package com.stardust.auojs.inrt.pluginclient; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.text.TextUtils; |
| 5 | +import android.util.Log; |
| 6 | + |
| 7 | +import com.google.gson.JsonElement; |
| 8 | +import com.google.gson.JsonNull; |
| 9 | +import com.google.gson.JsonObject; |
| 10 | +import com.stardust.app.GlobalAppContext; |
| 11 | +import com.stardust.auojs.inrt.autojs.AutoJs; |
| 12 | +import com.stardust.autojs.execution.ExecutionConfig; |
| 13 | +import com.stardust.autojs.execution.ScriptExecution; |
| 14 | +import com.stardust.autojs.project.ProjectLauncher; |
| 15 | +import com.stardust.autojs.script.ScriptSource; |
| 16 | +import com.stardust.autojs.script.StringScriptSource; |
| 17 | +import com.stardust.io.Zip; |
| 18 | +import com.stardust.pio.PFiles; |
| 19 | +import com.stardust.util.MD5; |
| 20 | + |
| 21 | +import com.stardust.auojs.inrt.Pref; |
| 22 | +import com.stardust.auojs.inrt.R; |
| 23 | + |
| 24 | +import java.io.ByteArrayInputStream; |
| 25 | +import java.io.File; |
| 26 | +import java.io.FileInputStream; |
| 27 | +import java.io.FileNotFoundException; |
| 28 | +import java.io.FileOutputStream; |
| 29 | +import java.util.HashMap; |
| 30 | + |
| 31 | +import io.reactivex.Observable; |
| 32 | +import io.reactivex.android.schedulers.AndroidSchedulers; |
| 33 | +import io.reactivex.schedulers.Schedulers; |
| 34 | + |
| 35 | +/** |
| 36 | + * Created by Stardust on 2017/5/11. |
| 37 | + */ |
| 38 | + |
| 39 | +public class DevPluginResponseHandler implements Handler { |
| 40 | + |
| 41 | + |
| 42 | + private Router mRouter = new Router.RootRouter("type") |
| 43 | + .handler("command", new Router("command") |
| 44 | + .handler("run", data -> { |
| 45 | + String script = data.get("script").getAsString(); |
| 46 | + String name = getName(data); |
| 47 | + String id = data.get("id").getAsString(); |
| 48 | + runScript(id, name, script); |
| 49 | + return true; |
| 50 | + }) |
| 51 | + .handler("stop", data -> { |
| 52 | + String id = data.get("id").getAsString(); |
| 53 | + stopScript(id); |
| 54 | + return true; |
| 55 | + }) |
| 56 | + .handler("rerun", data -> { |
| 57 | + String id = data.get("id").getAsString(); |
| 58 | + String script = data.get("script").getAsString(); |
| 59 | + String name = getName(data); |
| 60 | + stopScript(id); |
| 61 | + runScript(id, name, script); |
| 62 | + return true; |
| 63 | + }) |
| 64 | + .handler("stopAll", data -> { |
| 65 | + // AutoJs.getInstance().getScriptEngineService().stopAllAndToast(); |
| 66 | + return true; |
| 67 | + })); |
| 68 | + |
| 69 | + |
| 70 | + private HashMap<String, ScriptExecution> mScriptExecutions = new HashMap<>(); |
| 71 | + private final File mCacheDir; |
| 72 | + |
| 73 | + public DevPluginResponseHandler(File cacheDir) { |
| 74 | + mCacheDir = cacheDir; |
| 75 | + if (cacheDir.exists()) { |
| 76 | + if (cacheDir.isDirectory()) { |
| 77 | + PFiles.deleteFilesOfDir(cacheDir); |
| 78 | + } else { |
| 79 | + cacheDir.delete(); |
| 80 | + cacheDir.mkdirs(); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean handle(JsonObject data) { |
| 87 | + Log.d("-------", "runScript: "); |
| 88 | + return mRouter.handle(data); |
| 89 | + } |
| 90 | + |
| 91 | + public Observable<File> handleBytes(JsonObject data, JsonWebSocket.Bytes bytes) { |
| 92 | + String id = data.get("data").getAsJsonObject().get("id").getAsString(); |
| 93 | + String idMd5 = MD5.md5(id); |
| 94 | + return Observable.fromCallable(() -> { |
| 95 | + File dir = new File(mCacheDir, idMd5); |
| 96 | + Zip.unzip(new ByteArrayInputStream(bytes.byteString.toByteArray()), dir); |
| 97 | + return dir; |
| 98 | + }) |
| 99 | + .subscribeOn(Schedulers.io()); |
| 100 | + } |
| 101 | + |
| 102 | + private void runScript(String viewId, String name, String script) { |
| 103 | + StringScriptSource scriptSource = new StringScriptSource(name,script); |
| 104 | + ExecutionConfig config = new ExecutionConfig(); |
| 105 | + config.setWorkingDirectory(Pref.INSTANCE.getScriptDirPath()); |
| 106 | + AutoJs.Companion.getInstance().getScriptEngineService().execute(scriptSource, new ExecutionConfig()); |
| 107 | + // mScriptExecutions.put(viewId, Scripts.INSTANCE.run(new StringScriptSource("[remote]" + name, script))); |
| 108 | + } |
| 109 | + |
| 110 | + private void stopScript(String viewId) { |
| 111 | + ScriptExecution execution = mScriptExecutions.get(viewId); |
| 112 | + if (execution != null) { |
| 113 | + execution.getEngine().forceStop(); |
| 114 | + mScriptExecutions.remove(viewId); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private String getName(JsonObject data) { |
| 119 | + JsonElement element = data.get("name"); |
| 120 | + if (element instanceof JsonNull) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + return element.getAsString(); |
| 124 | + } |
| 125 | + |
| 126 | + private void copyDir(File fromDir, File toDir) throws FileNotFoundException { |
| 127 | + toDir.mkdirs(); |
| 128 | + File[] files = fromDir.listFiles(); |
| 129 | + if (files == null || files.length == 0) { |
| 130 | + return; |
| 131 | + } |
| 132 | + for (File file : files) { |
| 133 | + if (file.isDirectory()) { |
| 134 | + copyDir(file, new File(toDir, file.getName())); |
| 135 | + } else { |
| 136 | + FileOutputStream fos = new FileOutputStream(new File(toDir, file.getName())); |
| 137 | + PFiles.write(new FileInputStream(file), fos, true); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments