add: module threads; read and write binary file supports

This commit is contained in:
hyb1996
2017-12-03 14:13:34 +08:00
parent 1413bf21a5
commit 6fb6e14369
11 changed files with 172 additions and 6 deletions

View File

@@ -8,8 +8,8 @@ android {
applicationId "com.stardust.scriptdroid" applicationId "com.stardust.scriptdroid"
minSdkVersion 17 minSdkVersion 17
targetSdkVersion 23 targetSdkVersion 23
versionCode 227 versionCode 230
versionName "3.0.0 Alpha27" versionName "3.0.0 Alpha30"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true multiDexEnabled true
ndk { ndk {

View File

@@ -49,6 +49,7 @@ var __that__ = this;
var Promise = require('promise.js'); var Promise = require('promise.js');
var JSON = require('__json2__.js'); var JSON = require('__json2__.js');
var util = require('__util__.js'); var util = require('__util__.js');
var threads = __runtime__.threads;
var __asGlobal__ = function(obj, functions){ var __asGlobal__ = function(obj, functions){
var len = functions.length; var len = functions.length;

View File

@@ -17,7 +17,7 @@ module.exports = function(__runtime__, scope){
} }
engines.myEngine = function(){ engines.myEngine = function(){
return scope.__engine__; return rtEngines.myEngine();
} }
engines.stopAll = rtEngines.stopAll.bind(rtEngines); engines.stopAll = rtEngines.stopAll.bind(rtEngines);

View File

@@ -32,6 +32,7 @@ public abstract class JavaScriptEngine extends ScriptEngine.AbstractScriptEngine
throw new IllegalStateException("a runtime has been set"); throw new IllegalStateException("a runtime has been set");
} }
mRuntime = runtime; mRuntime = runtime;
mRuntime.engines.setCurrentEngine(this);
put("__runtime__", runtime); put("__runtime__", runtime);
} }

View File

@@ -144,7 +144,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
return importerTopLevel; return importerTopLevel;
} }
protected Context createContext() { public Context createContext() {
if (!ContextFactory.hasExplicitGlobal()) { if (!ContextFactory.hasExplicitGlobal()) {
ContextFactory.initGlobal(new InterruptibleAndroidContextFactory(new File(mAndroidContext.getCacheDir(), "classes"))); ContextFactory.initGlobal(new InterruptibleAndroidContextFactory(new File(mAndroidContext.getCacheDir(), "classes")));
} }

View File

@@ -16,6 +16,7 @@ import com.stardust.autojs.runtime.api.Console;
import com.stardust.autojs.runtime.api.Engines; import com.stardust.autojs.runtime.api.Engines;
import com.stardust.autojs.runtime.api.Events; import com.stardust.autojs.runtime.api.Events;
import com.stardust.autojs.runtime.api.Loopers; import com.stardust.autojs.runtime.api.Loopers;
import com.stardust.autojs.runtime.api.Threads;
import com.stardust.autojs.runtime.api.Timers; import com.stardust.autojs.runtime.api.Timers;
import com.stardust.autojs.core.accessibility.UiSelector; import com.stardust.autojs.core.accessibility.UiSelector;
import com.stardust.autojs.runtime.api.Images; import com.stardust.autojs.runtime.api.Images;
@@ -146,6 +147,9 @@ public class ScriptRuntime {
@ScriptVariable @ScriptVariable
public final Engines engines; public final Engines engines;
@ScriptVariable
public final Threads threads;
private Images images; private Images images;
private static WeakReference<Context> applicationContext; private static WeakReference<Context> applicationContext;
@@ -172,6 +176,7 @@ public class ScriptRuntime {
} }
engines = new Engines(builder.mEngineService); engines = new Engines(builder.mEngineService);
dialogs = new Dialogs(app, mUiHandler, bridges); dialogs = new Dialogs(app, mUiHandler, bridges);
threads = new Threads(this);
} }
public void init() { public void init() {
@@ -303,6 +308,7 @@ public class ScriptRuntime {
} }
public void onExit() { public void onExit() {
threads.shutDownAll();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
images.releaseScreenCapturer(); images.releaseScreenCapturer();
} }

View File

@@ -1,10 +1,13 @@
package com.stardust.autojs.runtime.api; package com.stardust.autojs.runtime.api;
import com.stardust.autojs.ScriptEngineService; import com.stardust.autojs.ScriptEngineService;
import com.stardust.autojs.engine.JavaScriptEngine;
import com.stardust.autojs.engine.ScriptEngine;
import com.stardust.autojs.execution.ExecutionConfig; import com.stardust.autojs.execution.ExecutionConfig;
import com.stardust.autojs.execution.ScriptExecution; import com.stardust.autojs.execution.ScriptExecution;
import com.stardust.autojs.script.AutoFileSource; import com.stardust.autojs.script.AutoFileSource;
import com.stardust.autojs.script.JavaScriptFileSource; import com.stardust.autojs.script.JavaScriptFileSource;
import com.stardust.autojs.script.JavaScriptSource;
import com.stardust.autojs.script.StringScriptSource; import com.stardust.autojs.script.StringScriptSource;
/** /**
@@ -14,6 +17,7 @@ import com.stardust.autojs.script.StringScriptSource;
public class Engines { public class Engines {
private ScriptEngineService mEngineService; private ScriptEngineService mEngineService;
private ScriptEngine<JavaScriptSource> mScriptEngine;
public Engines(ScriptEngineService engineService) { public Engines(ScriptEngineService engineService) {
mEngineService = engineService; mEngineService = engineService;
@@ -40,4 +44,11 @@ public class Engines {
} }
public void setCurrentEngine(ScriptEngine<JavaScriptSource> engine) {
mScriptEngine = engine;
}
public ScriptEngine<JavaScriptSource> myEngine() {
return mScriptEngine;
}
} }

View File

@@ -0,0 +1,80 @@
package com.stardust.autojs.runtime.api;
import com.stardust.autojs.engine.RhinoJavaScriptEngine;
import com.stardust.autojs.runtime.ScriptRuntime;
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
import com.stardust.concurrent.VolatileBox;
import com.stardust.concurrent.VolatileDispose;
import com.stardust.lang.ThreadCompat;
import com.stardust.pio.PFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by Stardust on 2017/12/3.
*/
public class Threads {
private List<ThreadCompat> mThreads = new ArrayList<>();
private ScriptRuntime mScriptRuntime;
public Threads(ScriptRuntime scriptRuntime) {
mScriptRuntime = scriptRuntime;
}
public void start(Runnable runnable) {
ThreadCompat threadCompat = new ThreadCompat(() -> {
try {
((RhinoJavaScriptEngine) mScriptRuntime.engines.myEngine()).createContext();
runnable.run();
} catch (Exception e) {
if (!ScriptInterruptedException.causedByInterrupted(e)) {
mScriptRuntime.console.error(e);
}
}
});
mThreads.add(threadCompat);
threadCompat.start();
}
public VolatileBox variable() {
return new VolatileBox();
}
public VolatileDispose disposable() {
return new VolatileDispose();
}
public List list() {
return Collections.synchronizedList(new ArrayList<>());
}
public Set set() {
return new ConcurrentSkipListSet();
}
public Map map() {
return new ConcurrentHashMap();
}
public AtomicInteger atomicInt() {
return new AtomicInteger();
}
public void shutDownAll() {
for (ThreadCompat threadCompat : mThreads) {
threadCompat.interrupt();
}
mThreads.clear();
}
}

View File

@@ -1,5 +1,6 @@
package com.stardust.pio; package com.stardust.pio;
import android.app.NativeActivity;
import android.content.Context; import android.content.Context;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.os.Environment; import android.os.Environment;
@@ -10,10 +11,12 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Locale; import java.util.Locale;
@@ -127,6 +130,16 @@ public class PFiles {
return read(inputStream, "utf-8"); return read(inputStream, "utf-8");
} }
public static byte[] readBytes(InputStream is) {
try {
byte[] bytes = new byte[is.available()];
is.read(bytes);
return bytes;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static boolean copyRaw(Context context, int rawId, String path) { public static boolean copyRaw(Context context, int rawId, String path) {
InputStream is = context.getResources().openRawResource(rawId); InputStream is = context.getResources().openRawResource(rawId);
return copyStream(is, path); return copyStream(is, path);
@@ -192,11 +205,54 @@ public class PFiles {
public static void write(OutputStream outputStream, String text, String encoding) { public static void write(OutputStream outputStream, String text, String encoding) {
try { try {
outputStream.write(text.getBytes(encoding)); outputStream.write(text.getBytes(encoding));
outputStream.close();
} catch (IOException e) { } catch (IOException e) {
throw new UncheckedIOException(e); throw new UncheckedIOException(e);
} }
} }
public static void append(String path, String text) {
try {
write(new FileOutputStream(path, true), text);
} catch (FileNotFoundException e) {
throw new UncheckedIOException(e);
}
}
public static void append(String path, String text, String encoding) {
try {
write(new FileOutputStream(path, true), text, encoding);
} catch (FileNotFoundException e) {
throw new UncheckedIOException(e);
}
}
public static void writeBytes(OutputStream outputStream, byte[] bytes) {
try {
outputStream.write(bytes);
outputStream.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static void appendBytes(String path, byte[] bytes) {
try {
writeBytes(new FileOutputStream(path, true), bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static void writeBytes(String path, byte[] bytes) {
try {
writeBytes(new FileOutputStream(path), bytes);
} catch (FileNotFoundException e) {
throw new UncheckedIOException(e);
}
}
public static boolean copy(String pathFrom, String pathTo) { public static boolean copy(String pathFrom, String pathTo) {
try { try {
return copyStream(new FileInputStream(pathFrom), pathTo); return copyStream(new FileInputStream(pathFrom), pathTo);
@@ -357,7 +413,7 @@ public class PFiles {
public static String getSimplifiedPath(String path) { public static String getSimplifiedPath(String path) {
if (path.startsWith(Environment.getExternalStorageDirectory().getPath())) { if (path.startsWith(Environment.getExternalStorageDirectory().getPath())) {
return path.substring(Environment.getExternalStorageDirectory().getPath().length()); return path.substring(Environment.getExternalStorageDirectory().getPath().length());
} }
return path; return path;
} }

View File

@@ -5,6 +5,8 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
/** /**
* Created by Stardust on 2017/4/29. * Created by Stardust on 2017/4/29.
@@ -29,4 +31,6 @@ public class PRandomAccessBinaryFile extends RandomAccessFile {
} }

View File

@@ -1,10 +1,17 @@
package com.stardust.pio; package com.stardust.pio;
import java.io.Closeable;
import java.io.IOException;
/** /**
* Created by Stardust on 2017/4/6. * Created by Stardust on 2017/4/6.
*/ */
public class PReadableBinaryFile extends PFiles { public class PReadableBinaryFile implements Closeable {
@Override
public void close() throws IOException {
}
} }