新增 view事件touch_down, touch_up, touch_move, key_down, key_up

修复 某些情况下脚本无声退出的问题(没有任何报错信息控制台打印脚本退出)
修复 某些情况下调用exit时ui不退出或子线程不退出的问题
This commit is contained in:
hyb1996 2018-10-16 15:57:08 +08:00
parent 4dcb19e4fe
commit fc05252540
6 changed files with 51 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Looper;
import android.support.v4.view.ViewCompat;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CompoundButton;
@ -119,10 +120,19 @@ public class ViewPrototype {
@SuppressLint("ClickableViewAccessibility")
private boolean registerEvent(String eventName) {
switch (eventName) {
case "touch_down":
case "touch_up":
case "touch": {
mView.setOnTouchListener((v, event) -> {
BaseEvent e = new BaseEvent(mScope, event, event.getClass());
//Log.d(LOG_TAG, "this = " + NativeView.this + ", emitter = " + mEventEmitter + ", view = " + mView);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
emit("touch_down", e, v);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
emit("touch_up", e, v);
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
emit("touch_move", e, v);
}
emit("touch", e, v);
return e.isConsumed();
});
@ -143,7 +153,12 @@ public class ViewPrototype {
case "key": {
mView.setOnKeyListener((v, keyCode, event) -> {
BaseEvent e = new BaseEvent(mScope, event, event.getClass());
emit("key", e, keyCode, v);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
emit("key_down", keyCode, e, v);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
emit("key_up", keyCode, e, v);
}
emit("key", keyCode, e, v);
return e.isConsumed();
});
}

View File

@ -100,7 +100,9 @@ public class XmlConverter {
.handler("paddingRight", new AttributeHandler.DimenHandler("paddingRight"))
.handler("paddingTop", new AttributeHandler.DimenHandler("paddingTop"))
.handler("paddingBottom", new AttributeHandler.DimenHandler("paddingBottom"))
.defaultHandler(new AttributeHandler.MappedAttributeHandler());
.defaultHandler(new AttributeHandler.MappedAttributeHandler()
.mapName("align", "layout_gravity")
);
public static String convertToAndroidLayout(String xml) throws IOException, SAXException, ParserConfigurationException {
return convertToAndroidLayout(new InputSource(new StringReader(xml)));

View File

@ -14,6 +14,7 @@ import com.stardust.lang.ThreadCompat;
public class RunnableScriptExecution extends ScriptExecution.AbstractScriptExecution implements Runnable {
private static final String TAG = "RunnableJSExecution";
private ScriptEngine mScriptEngine;
private ScriptEngineManager mScriptEngineManager;
@ -39,16 +40,25 @@ public class RunnableScriptExecution extends ScriptExecution.AbstractScriptExecu
try {
prepare(engine);
Object r = doExecution(engine);
Exception uncaughtException = engine.getUncaughtException();
if (uncaughtException != null) {
onException(engine, uncaughtException);
return null;
}
getListener().onSuccess(this, r);
return r;
} catch (Exception e) {
e.printStackTrace();
getListener().onException(this, e);
onException(engine, e);
return null;
} finally {
Log.d(TAG, "Engine destroy");
engine.destroy();
}
return null;
}
protected void onException(ScriptEngine engine, Exception e) {
Log.w(TAG, "onException: engine = " + engine, e);
getListener().onException(this, e);
}
private void prepare(ScriptEngine engine) {

View File

@ -77,7 +77,6 @@ public class ScriptRuntime {
private static final String TAG = "ScriptRuntime";
public static class Builder {
private UiHandler mUiHandler;
private Console mConsole;
@ -352,6 +351,8 @@ public class ScriptRuntime {
public void exit() {
mThread.interrupt();
engines.myEngine().forceStop();
threads.exit();
if (Looper.myLooper() != Looper.getMainLooper()) {
throw new ScriptInterruptedException();
}
@ -359,9 +360,7 @@ public class ScriptRuntime {
public void exit(Exception e) {
engines.myEngine().uncaughtException(e);
if (Looper.myLooper() != Looper.getMainLooper()) {
throw new ScriptException(e);
}
exit();
}
@Deprecated
@ -434,7 +433,7 @@ public class ScriptRuntime {
return mProperties.remove(key);
}
public static String getStackTrace(Throwable e, boolean printJavaStackTrace){
public static String getStackTrace(Throwable e, boolean printJavaStackTrace) {
StringBuilder scriptTrace = new StringBuilder();
if (e instanceof RhinoException) {
RhinoException rhinoException = (RhinoException) e;
@ -443,9 +442,9 @@ public class ScriptRuntime {
element.renderV8Style(scriptTrace);
scriptTrace.append("\n");
}
if(printJavaStackTrace){
if (printJavaStackTrace) {
scriptTrace.append("- - - - - - - - - - -\n");
}else {
} else {
return scriptTrace.toString();
}
}

View File

@ -23,6 +23,7 @@ public class Threads {
private final Thread mMainThread;
private MainThreadProxy mMainThreadProxy;
private int mSpawnCount = 0;
private boolean mExit = false;
public Threads(ScriptRuntime runtime) {
mRuntime = runtime;
@ -44,11 +45,14 @@ public class Threads {
public TimerThread start(Runnable runnable) {
TimerThread thread = createThread(runnable);
synchronized (mThreads) {
if (mExit) {
throw new IllegalStateException("script exiting");
}
mThreads.add(thread);
thread.setName(mMainThread.getName() + " (Spawn-" + mSpawnCount + ")");
mSpawnCount++;
thread.start();
}
thread.start();
return thread;
}
@ -92,6 +96,12 @@ public class Threads {
}
}
public void exit() {
synchronized (mThreads) {
shutDownAll();
mExit = true;
}
}
public boolean hasRunningThreads() {
synchronized (mThreads) {

View File

@ -1,6 +1,6 @@
{
"appVersionCode": 431,
"appVersionName": "4.0.4 Alpha2",
"appVersionCode": 432,
"appVersionName": "4.0.4 Alpha3",
"target": 28,
"mini": 17,
"compile": 28,