Merge branch 'dev' of https://github.com/hyb1996/NoRootScriptDroid into dev
# Conflicts: # .idea/caches/build_file_checksums.ser
This commit is contained in:
@@ -155,7 +155,7 @@ module.exports = function(__runtime__, scope){
|
||||
builder.input(wrapNonNullString(properties.inputHint), wrapNonNullString(properties.inputPrefill),
|
||||
function(dialog, input){
|
||||
input = input.toString();
|
||||
builder.emit("input_change", dialog, input);
|
||||
builder.dialog.emit("input_change", builder.dialog, input);
|
||||
})
|
||||
.alwaysCallInputCallback();
|
||||
}
|
||||
@@ -163,23 +163,23 @@ module.exports = function(__runtime__, scope){
|
||||
var itemsSelectMode = properties.itemsSelectMode;
|
||||
if(itemsSelectMode == undefined || itemsSelectMode == 'select'){
|
||||
builder.itemsCallback(function(dialog, view, position, text){
|
||||
dialog.emit("item_select", position, text.toString(), dialog);
|
||||
builder.dialog.emit("item_select", position, text.toString(), builder.dialog);
|
||||
});
|
||||
}else if(itemsSelectMode == 'single'){
|
||||
builder.itemsCallbackSingleChoice(properties.itemsSelectedIndex == undefined ? -1 : properties.itemsSelectedIndex,
|
||||
function(dialog, view, which, text){
|
||||
dialog.emit("single_choice", which, text.toString(), dialog);
|
||||
builder.dialog.emit("single_choice", which, text.toString(), builder.dialog);
|
||||
return true;
|
||||
});
|
||||
}else if(itemsSelectMode == 'multi'){
|
||||
builder.itemsCallbackMultiChoice(properties.itemsSelectedIndex == undefined ? null : properties.itemsSelectedIndex,
|
||||
function(dialog, view, indices, texts){
|
||||
dialog.emit("multi_choice", toJsArray(indices, (l, i)=> parseInt(l.get(i)),
|
||||
toJsArray(texts, (l, i)=> l.get(i).toString())), dialog);
|
||||
builder.dialog.emit("multi_choice", toJsArray(indices, (l, i)=> parseInt(l.get(i)),
|
||||
toJsArray(texts, (l, i)=> l.get(i).toString())), builder.dialog);
|
||||
return true;
|
||||
});
|
||||
}else{
|
||||
throw new Error("unknown itemsSelecteMode " + itemsSelectMode);
|
||||
throw new Error("unknown itemsSelectMode " + itemsSelectMode);
|
||||
}
|
||||
}
|
||||
if(properties.progress != undefined){
|
||||
@@ -191,9 +191,17 @@ module.exports = function(__runtime__, scope){
|
||||
if(properties.checkBoxPrompt != undefined || properties.checkBoxChecked != undefined){
|
||||
builder.checkBoxPrompt(wrapNonNullString(properties.checkBoxPrompt), !!properties.checkBoxChecked,
|
||||
function(view, checked){
|
||||
builder.emit("check", checked, builder.getDialog());
|
||||
builder.getDialog().emit("check", checked, builder.getDialog());
|
||||
});
|
||||
}
|
||||
if(properties.customView != undefined) {
|
||||
let customView = properties.customView;
|
||||
if(typeof(customView) == 'xml' || typeof(customView) == 'string') {
|
||||
customView = ui.run(() => ui.inflate(customView));
|
||||
}
|
||||
let wrapInScrollView = (properties.wrapInScrollView === undefined) ? true : properties.wrapInScrollView;
|
||||
builder.customView(customView, wrapInScrollView);
|
||||
}
|
||||
}
|
||||
|
||||
function wrapNonNullString(str){
|
||||
|
||||
@@ -37,7 +37,7 @@ module.exports = function(__runtime__, scope){
|
||||
}
|
||||
config.delay = c.delay || 0;
|
||||
config.interval = c.interval || 0;
|
||||
config.loopTimes = c.loopTimes || 1;
|
||||
config.loopTimes = (c.loopTimes === undefined)? 1 : c.loopTimes;
|
||||
if(c.arguments){
|
||||
var arguments = c.arguments;
|
||||
for(var key in arguments){
|
||||
|
||||
@@ -11,7 +11,7 @@ module.exports = function (runtime, global) {
|
||||
ui.__defineGetter__("emitter", ()=> activity ? activity.getEventEmitter() : null);
|
||||
|
||||
ui.layout = function (xml) {
|
||||
if(!activity){
|
||||
if(typeof(activity) == 'undefined'){
|
||||
throw new Error("需要在ui模式下运行才能使用该函数");
|
||||
}
|
||||
runtime.ui.layoutInflater.setContext(activity);
|
||||
@@ -24,15 +24,18 @@ module.exports = function (runtime, global) {
|
||||
}
|
||||
|
||||
ui.inflate = function(xml, parent, attachToParent){
|
||||
if(!activity){
|
||||
throw new Error("需要在ui模式下运行才能使用该函数");
|
||||
}
|
||||
if(typeof(xml) == 'xml'){
|
||||
xml = xml.toXMLString();
|
||||
}
|
||||
parent = parent || null;
|
||||
attachToParent = !!attachToParent;
|
||||
runtime.ui.layoutInflater.setContext(activity);
|
||||
let ctx;
|
||||
if(typeof(activity) == 'undefined') {
|
||||
ctx = new android.view.ContextThemeWrapper(context, com.stardust.autojs.R.style.ScriptTheme);
|
||||
} else {
|
||||
ctx = activity;
|
||||
}
|
||||
runtime.ui.layoutInflater.setContext(ctx);
|
||||
return runtime.ui.layoutInflater.inflate(xml.toString(), parent, attachToParent);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,188 +0,0 @@
|
||||
package com.stardust.autojs.core.graphics;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
import com.stardust.autojs.core.eventloop.EventEmitter;
|
||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/3/16.
|
||||
*/
|
||||
|
||||
@SuppressLint("ViewConstructor")
|
||||
public class ScriptCanvasView extends SurfaceView implements SurfaceHolder.Callback {
|
||||
|
||||
private static final String LOG_TAG = "ScriptCanvasView";
|
||||
private volatile boolean mDrawing = true;
|
||||
private EventEmitter mEventEmitter;
|
||||
private final SurfaceHolder mHolder;
|
||||
private ExecutorService mDrawingThreadPool;
|
||||
private ScriptRuntime mScriptRuntime;
|
||||
private volatile long mTimePerDraw = 1000 / 30;
|
||||
|
||||
public ScriptCanvasView(Context context, ScriptRuntime scriptRuntime) {
|
||||
super(context);
|
||||
mScriptRuntime = scriptRuntime;
|
||||
mEventEmitter = new EventEmitter(mScriptRuntime.bridges);
|
||||
mHolder = getHolder();
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
public void setMaxFps(int maxFps) {
|
||||
if (maxFps <= 0) {
|
||||
mTimePerDraw = 0;
|
||||
} else {
|
||||
mTimePerDraw = 100 / maxFps;
|
||||
}
|
||||
}
|
||||
|
||||
private void init() {
|
||||
mHolder.addCallback(this);
|
||||
setZOrderOnTop(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
performDraw();
|
||||
Log.d(LOG_TAG, "surfaceCreated: " + this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
||||
|
||||
}
|
||||
|
||||
private synchronized void performDraw() {
|
||||
if (mDrawingThreadPool != null)
|
||||
return;
|
||||
mDrawingThreadPool = Executors.newCachedThreadPool();
|
||||
mDrawingThreadPool.execute(() -> {
|
||||
Canvas canvas = null;
|
||||
SurfaceHolder holder = getHolder();
|
||||
long time = SystemClock.uptimeMillis();
|
||||
ScriptCanvas scriptCanvas = new ScriptCanvas();
|
||||
try {
|
||||
while (mDrawing) {
|
||||
canvas = holder.lockCanvas();
|
||||
scriptCanvas.setCanvas(canvas);
|
||||
scriptCanvas.drawColor(Color.WHITE);
|
||||
emit("draw", scriptCanvas, ScriptCanvasView.this);
|
||||
holder.unlockCanvasAndPost(canvas);
|
||||
canvas = null;
|
||||
long dt = mTimePerDraw - (SystemClock.uptimeMillis() - time);
|
||||
if (dt > 0) {
|
||||
sleep(dt);
|
||||
}
|
||||
time = SystemClock.uptimeMillis();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
mScriptRuntime.exit(e);
|
||||
mDrawing = false;
|
||||
} finally {
|
||||
if (canvas != null) {
|
||||
holder.unlockCanvasAndPost(canvas);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void sleep(long dt) {
|
||||
try {
|
||||
Thread.sleep(dt);
|
||||
} catch (InterruptedException e) {
|
||||
throw new ScriptInterruptedException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onWindowVisibilityChanged(int visibility) {
|
||||
Log.d(LOG_TAG, "onWindowVisibilityChanged: " + this + ": visibility=" + visibility + ", mDrawingThreadPool=" + mDrawingThreadPool);
|
||||
if (visibility == VISIBLE) {
|
||||
mDrawing = true;
|
||||
} else {
|
||||
mDrawing = false;
|
||||
}
|
||||
super.onWindowVisibilityChanged(visibility);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void surfaceDestroyed(SurfaceHolder holder) {
|
||||
mDrawing = false;
|
||||
mDrawingThreadPool.shutdown();
|
||||
mDrawingThreadPool = null;
|
||||
Log.d(LOG_TAG, "surfaceDestroyed: " + this);
|
||||
}
|
||||
|
||||
public EventEmitter once(String eventName, Object listener) {
|
||||
return mEventEmitter.once(eventName, listener);
|
||||
}
|
||||
|
||||
public EventEmitter on(String eventName, Object listener) {
|
||||
return mEventEmitter.on(eventName, listener);
|
||||
}
|
||||
|
||||
public EventEmitter addListener(String eventName, Object listener) {
|
||||
return mEventEmitter.addListener(eventName, listener);
|
||||
}
|
||||
|
||||
public boolean emit(String eventName, Object... args) {
|
||||
return mEventEmitter.emit(eventName, args);
|
||||
}
|
||||
|
||||
public String[] eventNames() {
|
||||
return mEventEmitter.eventNames();
|
||||
}
|
||||
|
||||
public int listenerCount(String eventName) {
|
||||
return mEventEmitter.listenerCount(eventName);
|
||||
}
|
||||
|
||||
public Object[] listeners(String eventName) {
|
||||
return mEventEmitter.listeners(eventName);
|
||||
}
|
||||
|
||||
public EventEmitter prependListener(String eventName, Object listener) {
|
||||
return mEventEmitter.prependListener(eventName, listener);
|
||||
}
|
||||
|
||||
public EventEmitter prependOnceListener(String eventName, Object listener) {
|
||||
return mEventEmitter.prependOnceListener(eventName, listener);
|
||||
}
|
||||
|
||||
public EventEmitter removeAllListeners() {
|
||||
return mEventEmitter.removeAllListeners();
|
||||
}
|
||||
|
||||
public EventEmitter removeAllListeners(String eventName) {
|
||||
return mEventEmitter.removeAllListeners(eventName);
|
||||
}
|
||||
|
||||
public EventEmitter removeListener(String eventName, Object listener) {
|
||||
return mEventEmitter.removeListener(eventName, listener);
|
||||
}
|
||||
|
||||
public EventEmitter setMaxListeners(int n) {
|
||||
return mEventEmitter.setMaxListeners(n);
|
||||
}
|
||||
|
||||
public int getMaxListeners() {
|
||||
return mEventEmitter.getMaxListeners();
|
||||
}
|
||||
|
||||
public static int defaultMaxListeners() {
|
||||
return EventEmitter.defaultMaxListeners();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
package com.stardust.autojs.core.graphics
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.SurfaceTexture
|
||||
import android.os.SystemClock
|
||||
import android.util.Log
|
||||
import android.view.TextureView
|
||||
import android.view.View
|
||||
import com.stardust.autojs.core.eventloop.EventEmitter
|
||||
import com.stardust.autojs.runtime.ScriptRuntime
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException
|
||||
import com.stardust.ext.ifNull
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/3/16.
|
||||
*/
|
||||
|
||||
@SuppressLint("ViewConstructor")
|
||||
class ScriptCanvasView(context: Context, private val mScriptRuntime: ScriptRuntime) : TextureView(context), TextureView.SurfaceTextureListener {
|
||||
@Volatile
|
||||
private var mDrawing = true
|
||||
private val mEventEmitter: EventEmitter = EventEmitter(mScriptRuntime.bridges)
|
||||
private var mDrawingThreadPool: ExecutorService? = null
|
||||
@Volatile
|
||||
private var mTimePerDraw = (1000 / 30).toLong()
|
||||
|
||||
val maxListeners: Int
|
||||
get() = mEventEmitter.maxListeners
|
||||
|
||||
init {
|
||||
surfaceTextureListener = this
|
||||
}
|
||||
|
||||
fun setMaxFps(maxFps: Int) {
|
||||
mTimePerDraw = if (maxFps <= 0) {
|
||||
0
|
||||
} else {
|
||||
(100 / maxFps).toLong()
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun performDraw() {
|
||||
::mDrawingThreadPool.ifNull {
|
||||
Executors.newCachedThreadPool()
|
||||
}.run {
|
||||
execute {
|
||||
var canvas: Canvas? = null
|
||||
var time = SystemClock.uptimeMillis()
|
||||
val scriptCanvas = ScriptCanvas()
|
||||
try {
|
||||
while (mDrawing) {
|
||||
canvas = lockCanvas()
|
||||
scriptCanvas.setCanvas(canvas)
|
||||
// scriptCanvas.drawColor(Color.WHITE);
|
||||
emit("draw", scriptCanvas, this@ScriptCanvasView)
|
||||
unlockCanvasAndPost(canvas)
|
||||
canvas = null
|
||||
val dt = mTimePerDraw - (SystemClock.uptimeMillis() - time)
|
||||
if (dt > 0) {
|
||||
sleep(dt)
|
||||
}
|
||||
time = SystemClock.uptimeMillis()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
mScriptRuntime.exit(e)
|
||||
mDrawing = false
|
||||
} finally {
|
||||
if (canvas != null) {
|
||||
unlockCanvasAndPost(canvas)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun sleep(dt: Long) {
|
||||
try {
|
||||
Thread.sleep(dt)
|
||||
} catch (e: InterruptedException) {
|
||||
throw ScriptInterruptedException(e)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun onWindowVisibilityChanged(visibility: Int) {
|
||||
Log.d(LOG_TAG, "onWindowVisibilityChanged: " + this + ": visibility=" + visibility + ", mDrawingThreadPool=" + mDrawingThreadPool)
|
||||
val oldDrawing = mDrawing
|
||||
mDrawing = visibility == View.VISIBLE
|
||||
if (!oldDrawing && mDrawing) {
|
||||
performDraw()
|
||||
}
|
||||
super.onWindowVisibilityChanged(visibility)
|
||||
}
|
||||
|
||||
fun once(eventName: String, listener: Any): EventEmitter {
|
||||
return mEventEmitter.once(eventName, listener)
|
||||
}
|
||||
|
||||
fun on(eventName: String, listener: Any): EventEmitter {
|
||||
return mEventEmitter.on(eventName, listener)
|
||||
}
|
||||
|
||||
fun addListener(eventName: String, listener: Any): EventEmitter {
|
||||
return mEventEmitter.addListener(eventName, listener)
|
||||
}
|
||||
|
||||
fun emit(eventName: String, vararg args: Any): Boolean {
|
||||
return mEventEmitter.emit(eventName, *args)
|
||||
}
|
||||
|
||||
fun eventNames(): Array<String> {
|
||||
return mEventEmitter.eventNames()
|
||||
}
|
||||
|
||||
fun listenerCount(eventName: String): Int {
|
||||
return mEventEmitter.listenerCount(eventName)
|
||||
}
|
||||
|
||||
fun listeners(eventName: String): Array<Any> {
|
||||
return mEventEmitter.listeners(eventName)
|
||||
}
|
||||
|
||||
fun prependListener(eventName: String, listener: Any): EventEmitter {
|
||||
return mEventEmitter.prependListener(eventName, listener)
|
||||
}
|
||||
|
||||
fun prependOnceListener(eventName: String, listener: Any): EventEmitter {
|
||||
return mEventEmitter.prependOnceListener(eventName, listener)
|
||||
}
|
||||
|
||||
fun removeAllListeners(): EventEmitter {
|
||||
return mEventEmitter.removeAllListeners()
|
||||
}
|
||||
|
||||
fun removeAllListeners(eventName: String): EventEmitter {
|
||||
return mEventEmitter.removeAllListeners(eventName)
|
||||
}
|
||||
|
||||
fun removeListener(eventName: String, listener: Any): EventEmitter {
|
||||
return mEventEmitter.removeListener(eventName, listener)
|
||||
}
|
||||
|
||||
fun setMaxListeners(n: Int): EventEmitter {
|
||||
return mEventEmitter.setMaxListeners(n)
|
||||
}
|
||||
|
||||
override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
|
||||
performDraw()
|
||||
Log.d(LOG_TAG, "onSurfaceTextureAvailable: ${this}, width = $width, height = $height")
|
||||
}
|
||||
|
||||
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {}
|
||||
|
||||
override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
|
||||
mDrawing = false
|
||||
mDrawingThreadPool?.shutdown()
|
||||
Log.d(LOG_TAG, "onSurfaceTextureDestroyed: ${this}")
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val LOG_TAG = "ScriptCanvasView"
|
||||
|
||||
fun defaultMaxListeners(): Int {
|
||||
return EventEmitter.defaultMaxListeners()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.stardust.autojs.core.http;
|
||||
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -19,13 +20,24 @@ public class MutableOkHttp extends OkHttpClient {
|
||||
private long mTimeout = 30 * 1000;
|
||||
private Interceptor mRetryInterceptor = chain -> {
|
||||
Request request = chain.request();
|
||||
Response response = chain.proceed(request);
|
||||
Response response = null;
|
||||
int tryCount = 0;
|
||||
while (!response.isSuccessful() && tryCount < getMaxRetries()) {
|
||||
do {
|
||||
boolean succeed;
|
||||
try {
|
||||
response = chain.proceed(request);
|
||||
succeed = response.isSuccessful();
|
||||
} catch (SocketTimeoutException e) {
|
||||
succeed = false;
|
||||
if (tryCount >= getMaxRetries()) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
if (succeed || tryCount >= getMaxRetries()) {
|
||||
return response;
|
||||
}
|
||||
tryCount++;
|
||||
response = chain.proceed(request);
|
||||
}
|
||||
return response;
|
||||
} while (true);
|
||||
};
|
||||
|
||||
public MutableOkHttp() {
|
||||
@@ -64,6 +76,7 @@ public class MutableOkHttp extends OkHttpClient {
|
||||
|
||||
|
||||
public void setTimeout(long timeout) {
|
||||
mTimeout = timeout;
|
||||
muteClient();
|
||||
}
|
||||
|
||||
|
||||
@@ -17,10 +17,13 @@ import org.mozilla.javascript.Context;
|
||||
import java.util.HashSet;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/7/29.
|
||||
*/
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public class Loopers implements MessageQueue.IdleHandler {
|
||||
|
||||
private static final String LOG_TAG = "Loopers";
|
||||
@@ -32,9 +35,27 @@ public class Loopers implements MessageQueue.IdleHandler {
|
||||
private static final Runnable EMPTY_RUNNABLE = () -> {
|
||||
};
|
||||
|
||||
private volatile ThreadLocal<Boolean> waitWhenIdle = new ThreadLocal<>();
|
||||
private volatile ThreadLocal<HashSet<Integer>> waitIds = new ThreadLocal<>();
|
||||
private volatile ThreadLocal<Integer> maxWaitId = new ThreadLocal<>();
|
||||
private volatile ThreadLocal<Boolean> waitWhenIdle = new ThreadLocal<Boolean>() {
|
||||
@Nullable
|
||||
@Override
|
||||
protected Boolean initialValue() {
|
||||
return Looper.myLooper() == Looper.getMainLooper();
|
||||
}
|
||||
};
|
||||
private volatile ThreadLocal<HashSet<Integer>> waitIds = new ThreadLocal<HashSet<Integer>>() {
|
||||
@Nullable
|
||||
@Override
|
||||
protected HashSet<Integer> initialValue() {
|
||||
return new HashSet<>();
|
||||
}
|
||||
};
|
||||
private volatile ThreadLocal<Integer> maxWaitId = new ThreadLocal<Integer>() {
|
||||
@Nullable
|
||||
@Override
|
||||
protected Integer initialValue() {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
private volatile ThreadLocal<CopyOnWriteArrayList<LooperQuitHandler>> looperQuitHandlers = new ThreadLocal<>();
|
||||
private volatile Looper mServantLooper;
|
||||
private Timers mTimers;
|
||||
@@ -126,7 +147,7 @@ public class Loopers implements MessageQueue.IdleHandler {
|
||||
return mServantLooper;
|
||||
}
|
||||
|
||||
public void quitServantLooper() {
|
||||
private void quitServantLooper() {
|
||||
if (mServantLooper == null)
|
||||
return;
|
||||
mServantLooper.quit();
|
||||
@@ -134,12 +155,14 @@ public class Loopers implements MessageQueue.IdleHandler {
|
||||
|
||||
public int waitWhenIdle() {
|
||||
int id = maxWaitId.get();
|
||||
Log.d(LOG_TAG, "waitWhenIdle: " + id);
|
||||
maxWaitId.set(id + 1);
|
||||
waitIds.get().add(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
public void doNotWaitWhenIdle(int waitId) {
|
||||
Log.d(LOG_TAG, "doNotWaitWhenIdle: " + waitId);
|
||||
waitIds.get().remove(waitId);
|
||||
}
|
||||
|
||||
@@ -181,9 +204,6 @@ public class Loopers implements MessageQueue.IdleHandler {
|
||||
if (Looper.myLooper() == null)
|
||||
LooperHelper.prepare();
|
||||
Looper.myQueue().addIdleHandler(this);
|
||||
waitWhenIdle.set(Looper.myLooper() == Looper.getMainLooper());
|
||||
waitIds.set(new HashSet<>());
|
||||
maxWaitId.set(0);
|
||||
}
|
||||
|
||||
public void notifyThreadExit(TimerThread thread) {
|
||||
|
||||
@@ -89,6 +89,10 @@ public class Timer {
|
||||
}
|
||||
}
|
||||
|
||||
public void post(Runnable r) {
|
||||
|
||||
}
|
||||
|
||||
public boolean clearInterval(int id) {
|
||||
return clearCallback(id);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ public class JsDialogBuilder extends MaterialDialog.Builder {
|
||||
super(context);
|
||||
mTimer = runtime.timers.getTimerForCurrentThread();
|
||||
mLoopers = runtime.loopers;
|
||||
mEmitter = new EventEmitter(runtime.bridges, mTimer);
|
||||
mEmitter = new EventEmitter(runtime.bridges);
|
||||
mUiHandler = runtime.uiHandler;
|
||||
setUpEvents();
|
||||
}
|
||||
@@ -70,7 +70,6 @@ public class JsDialogBuilder extends MaterialDialog.Builder {
|
||||
|
||||
public void onShowCalled() {
|
||||
mTimer.postDelayed(() -> mWaitId = mLoopers.waitWhenIdle(), 0);
|
||||
|
||||
}
|
||||
|
||||
public JsDialog getDialog() {
|
||||
|
||||
@@ -5,18 +5,13 @@ import android.os.Handler;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
|
||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||
import com.stardust.autojs.runtime.api.AbstractShell;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.autojs.runtime.ScriptRuntime;
|
||||
import com.stardust.lang.ThreadCompat;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import jackpal.androidterm.ShellTermSession;
|
||||
import jackpal.androidterm.emulatorview.TermSession;
|
||||
@@ -68,9 +63,11 @@ public class Shell extends AbstractShell {
|
||||
private volatile TermSession mTermSession;
|
||||
private final Object mInitLock = new Object();
|
||||
private final Object mExitLock = new Object();
|
||||
private final Object mCommandOutputLock = new Object();
|
||||
private volatile RuntimeException mInitException;
|
||||
private volatile boolean mInitialized = false;
|
||||
private volatile boolean mWaitingExit = false;
|
||||
private volatile String mCommandOutput = null;
|
||||
private final boolean mShouldReadOutput;
|
||||
private Callback mCallback;
|
||||
|
||||
@@ -114,6 +111,18 @@ public class Shell extends AbstractShell {
|
||||
mTermSession.write(command + "\n");
|
||||
}
|
||||
|
||||
public String execAndWaitFor(String command) {
|
||||
exec(command);
|
||||
synchronized (mCommandOutputLock) {
|
||||
try {
|
||||
mCommandOutputLock.wait();
|
||||
return mCommandOutput;
|
||||
} catch (InterruptedException e) {
|
||||
throw new ScriptInterruptedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setCallback(Callback callback) {
|
||||
mCallback = callback;
|
||||
}
|
||||
@@ -131,13 +140,13 @@ public class Shell extends AbstractShell {
|
||||
checkInitException();
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}else {
|
||||
} else {
|
||||
logDebug("ensureInitialized: init");
|
||||
}
|
||||
}
|
||||
|
||||
private void logDebug(String log){
|
||||
if(DEBUG){
|
||||
private void logDebug(String log) {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, log);
|
||||
}
|
||||
}
|
||||
@@ -150,7 +159,7 @@ public class Shell extends AbstractShell {
|
||||
|
||||
private void waitInitialization() {
|
||||
synchronized (mInitLock) {
|
||||
if(mInitialized){
|
||||
if (mInitialized) {
|
||||
return;
|
||||
}
|
||||
logDebug("waitInitialization: enter");
|
||||
@@ -204,33 +213,11 @@ public class Shell extends AbstractShell {
|
||||
|
||||
private class MyShellTermSession extends ShellTermSession {
|
||||
|
||||
private BufferedReader mBufferedReader;
|
||||
private OutputStream mOutputStream;
|
||||
private Thread mReadingThread;
|
||||
private StringBuilder mStringBuffer = new StringBuilder();
|
||||
private ArrayList<String> mCommandOutputs = new ArrayList<>();
|
||||
|
||||
public MyShellTermSession(TermSettings settings, String initialCommand) throws IOException {
|
||||
super(settings, initialCommand);
|
||||
PipedInputStream pipedInputStream = new PipedInputStream(8192);
|
||||
mBufferedReader = new BufferedReader(new InputStreamReader(pipedInputStream));
|
||||
mOutputStream = new PipedOutputStream(pipedInputStream);
|
||||
if (mShouldReadOutput) {
|
||||
startReadingThread();
|
||||
}
|
||||
}
|
||||
|
||||
private void startReadingThread() {
|
||||
mReadingThread = new ThreadCompat(() -> {
|
||||
String line;
|
||||
try {
|
||||
while (!Thread.currentThread().isInterrupted()
|
||||
&& (line = mBufferedReader.readLine()) != null) {
|
||||
onNewLine(line);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
mReadingThread.start();
|
||||
}
|
||||
|
||||
private void onNewLine(String line) {
|
||||
@@ -239,6 +226,8 @@ public class Shell extends AbstractShell {
|
||||
if (!isRoot() && line.endsWith(" $ sh")) {
|
||||
notifyInitialized();
|
||||
}
|
||||
} else {
|
||||
mCommandOutputs.add(line);
|
||||
}
|
||||
if (mCallback != null) {
|
||||
mCallback.onNewLine(line);
|
||||
@@ -248,28 +237,56 @@ public class Shell extends AbstractShell {
|
||||
}
|
||||
}
|
||||
|
||||
private void onOutput(String str){
|
||||
private void onCommandOutput(ArrayList<String> output) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 1; i < output.size(); i++) {
|
||||
result.append(output.get(i));
|
||||
if (i < output.size() - 1) {
|
||||
result.append("\n");
|
||||
}
|
||||
}
|
||||
logDebug("onCommandOutput: lines = " + output + ", output = " + result);
|
||||
synchronized (mCommandOutputLock) {
|
||||
mCommandOutput = result.toString();
|
||||
mCommandOutputLock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
private void onOutput(String str) {
|
||||
logDebug("onOutput: " + str);
|
||||
if (!mInitialized) {
|
||||
if (isRoot() && str.endsWith(":/ # ")) {
|
||||
notifyInitialized();
|
||||
}
|
||||
}
|
||||
int start = 0;
|
||||
int i;
|
||||
while (true) {
|
||||
i = str.indexOf("\n", start);
|
||||
if (i > 0) {
|
||||
onNewLine((mStringBuffer.toString() + str.substring(0, i - 1)).trim());
|
||||
mStringBuffer.delete(0, mStringBuffer.length());
|
||||
} else {
|
||||
if (start <= str.length() - 1) {
|
||||
mStringBuffer.append(str.substring(start));
|
||||
}
|
||||
break;
|
||||
}
|
||||
start = i + 1;
|
||||
}
|
||||
if (str.endsWith(" # ") || str.endsWith(" $ ")) {
|
||||
onCommandOutput(mCommandOutputs);
|
||||
mCommandOutputs.clear();
|
||||
}
|
||||
if (mCallback != null) {
|
||||
mCallback.onOutput(str);
|
||||
mCallback.onOutput(str.replace("\r", ""));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void processInput(byte[] data, int offset, int count) {
|
||||
try {
|
||||
onOutput(new String(data, offset, count));
|
||||
mOutputStream.write(data, offset, count);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
finish();
|
||||
}
|
||||
onOutput(new String(data, offset, count));
|
||||
}
|
||||
|
||||
private void notifyExit() {
|
||||
@@ -299,21 +316,6 @@ public class Shell extends AbstractShell {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
super.finish();
|
||||
if (!mShouldReadOutput)
|
||||
return;
|
||||
if(mReadingThread != null){
|
||||
mReadingThread.interrupt();
|
||||
}
|
||||
try {
|
||||
mBufferedReader.close();
|
||||
mOutputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Co
|
||||
runtime.topLevelScope = mScriptable
|
||||
}
|
||||
|
||||
public override fun doExecution(source: JavaScriptSource): Any {
|
||||
public override fun doExecution(source: JavaScriptSource): Any? {
|
||||
var reader = source.nonNullScriptReader
|
||||
try {
|
||||
reader = preprocess(reader)
|
||||
|
||||
@@ -12,7 +12,6 @@ import com.stardust.util.ScreenMetrics;
|
||||
|
||||
public abstract class AbstractShell {
|
||||
|
||||
|
||||
public static class Result {
|
||||
public int code = -1;
|
||||
public String error;
|
||||
|
||||
@@ -144,7 +144,6 @@ public class Dialogs {
|
||||
}
|
||||
return new JsDialogBuilder(context, mRuntime)
|
||||
.theme(Theme.LIGHT);
|
||||
|
||||
}
|
||||
|
||||
public class NonUiDialogs {
|
||||
|
||||
Reference in New Issue
Block a user