mirror of
https://github.com/TonyJiangWJ/Auto.js.git
synced 2026-06-21 21:01:43 +08:00
add: module threads; read and write binary file supports
This commit is contained in:
parent
1413bf21a5
commit
6fb6e14369
@ -8,8 +8,8 @@ android {
|
||||
applicationId "com.stardust.scriptdroid"
|
||||
minSdkVersion 17
|
||||
targetSdkVersion 23
|
||||
versionCode 227
|
||||
versionName "3.0.0 Alpha27"
|
||||
versionCode 230
|
||||
versionName "3.0.0 Alpha30"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
multiDexEnabled true
|
||||
ndk {
|
||||
|
||||
@ -49,6 +49,7 @@ var __that__ = this;
|
||||
var Promise = require('promise.js');
|
||||
var JSON = require('__json2__.js');
|
||||
var util = require('__util__.js');
|
||||
var threads = __runtime__.threads;
|
||||
|
||||
var __asGlobal__ = function(obj, functions){
|
||||
var len = functions.length;
|
||||
|
||||
@ -17,7 +17,7 @@ module.exports = function(__runtime__, scope){
|
||||
}
|
||||
|
||||
engines.myEngine = function(){
|
||||
return scope.__engine__;
|
||||
return rtEngines.myEngine();
|
||||
}
|
||||
|
||||
engines.stopAll = rtEngines.stopAll.bind(rtEngines);
|
||||
|
||||
@ -32,6 +32,7 @@ public abstract class JavaScriptEngine extends ScriptEngine.AbstractScriptEngine
|
||||
throw new IllegalStateException("a runtime has been set");
|
||||
}
|
||||
mRuntime = runtime;
|
||||
mRuntime.engines.setCurrentEngine(this);
|
||||
put("__runtime__", runtime);
|
||||
}
|
||||
|
||||
|
||||
@ -144,7 +144,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
return importerTopLevel;
|
||||
}
|
||||
|
||||
protected Context createContext() {
|
||||
public Context createContext() {
|
||||
if (!ContextFactory.hasExplicitGlobal()) {
|
||||
ContextFactory.initGlobal(new InterruptibleAndroidContextFactory(new File(mAndroidContext.getCacheDir(), "classes")));
|
||||
}
|
||||
|
||||
@ -16,6 +16,7 @@ import com.stardust.autojs.runtime.api.Console;
|
||||
import com.stardust.autojs.runtime.api.Engines;
|
||||
import com.stardust.autojs.runtime.api.Events;
|
||||
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.core.accessibility.UiSelector;
|
||||
import com.stardust.autojs.runtime.api.Images;
|
||||
@ -146,6 +147,9 @@ public class ScriptRuntime {
|
||||
@ScriptVariable
|
||||
public final Engines engines;
|
||||
|
||||
@ScriptVariable
|
||||
public final Threads threads;
|
||||
|
||||
private Images images;
|
||||
|
||||
private static WeakReference<Context> applicationContext;
|
||||
@ -172,6 +176,7 @@ public class ScriptRuntime {
|
||||
}
|
||||
engines = new Engines(builder.mEngineService);
|
||||
dialogs = new Dialogs(app, mUiHandler, bridges);
|
||||
threads = new Threads(this);
|
||||
}
|
||||
|
||||
public void init() {
|
||||
@ -303,6 +308,7 @@ public class ScriptRuntime {
|
||||
}
|
||||
|
||||
public void onExit() {
|
||||
threads.shutDownAll();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
images.releaseScreenCapturer();
|
||||
}
|
||||
|
||||
@ -1,10 +1,13 @@
|
||||
package com.stardust.autojs.runtime.api;
|
||||
|
||||
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.ScriptExecution;
|
||||
import com.stardust.autojs.script.AutoFileSource;
|
||||
import com.stardust.autojs.script.JavaScriptFileSource;
|
||||
import com.stardust.autojs.script.JavaScriptSource;
|
||||
import com.stardust.autojs.script.StringScriptSource;
|
||||
|
||||
/**
|
||||
@ -14,6 +17,7 @@ import com.stardust.autojs.script.StringScriptSource;
|
||||
public class Engines {
|
||||
|
||||
private ScriptEngineService mEngineService;
|
||||
private ScriptEngine<JavaScriptSource> mScriptEngine;
|
||||
|
||||
public Engines(ScriptEngineService engineService) {
|
||||
mEngineService = engineService;
|
||||
@ -40,4 +44,11 @@ public class Engines {
|
||||
}
|
||||
|
||||
|
||||
public void setCurrentEngine(ScriptEngine<JavaScriptSource> engine) {
|
||||
mScriptEngine = engine;
|
||||
}
|
||||
|
||||
public ScriptEngine<JavaScriptSource> myEngine() {
|
||||
return mScriptEngine;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package com.stardust.pio;
|
||||
|
||||
import android.app.NativeActivity;
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.os.Environment;
|
||||
@ -10,10 +11,12 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Locale;
|
||||
|
||||
@ -127,6 +130,16 @@ public class PFiles {
|
||||
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) {
|
||||
InputStream is = context.getResources().openRawResource(rawId);
|
||||
return copyStream(is, path);
|
||||
@ -192,11 +205,54 @@ public class PFiles {
|
||||
public static void write(OutputStream outputStream, String text, String encoding) {
|
||||
try {
|
||||
outputStream.write(text.getBytes(encoding));
|
||||
outputStream.close();
|
||||
} catch (IOException 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) {
|
||||
try {
|
||||
return copyStream(new FileInputStream(pathFrom), pathTo);
|
||||
@ -357,7 +413,7 @@ public class PFiles {
|
||||
|
||||
public static String getSimplifiedPath(String path) {
|
||||
if (path.startsWith(Environment.getExternalStorageDirectory().getPath())) {
|
||||
return path.substring(Environment.getExternalStorageDirectory().getPath().length());
|
||||
return path.substring(Environment.getExternalStorageDirectory().getPath().length());
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
@ -5,6 +5,8 @@ import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/29.
|
||||
@ -29,4 +31,6 @@ public class PRandomAccessBinaryFile extends RandomAccessFile {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,10 +1,17 @@
|
||||
package com.stardust.pio;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/4/6.
|
||||
*/
|
||||
|
||||
public class PReadableBinaryFile extends PFiles {
|
||||
public class PReadableBinaryFile implements Closeable {
|
||||
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user