新增 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.Build;
import android.os.Looper; import android.os.Looper;
import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewCompat;
import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.CompoundButton; import android.widget.CompoundButton;
@@ -119,10 +120,19 @@ public class ViewPrototype {
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
private boolean registerEvent(String eventName) { private boolean registerEvent(String eventName) {
switch (eventName) { switch (eventName) {
case "touch_down":
case "touch_up":
case "touch": { case "touch": {
mView.setOnTouchListener((v, event) -> { mView.setOnTouchListener((v, event) -> {
BaseEvent e = new BaseEvent(mScope, event, event.getClass()); BaseEvent e = new BaseEvent(mScope, event, event.getClass());
//Log.d(LOG_TAG, "this = " + NativeView.this + ", emitter = " + mEventEmitter + ", view = " + mView); //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); emit("touch", e, v);
return e.isConsumed(); return e.isConsumed();
}); });
@@ -143,7 +153,12 @@ public class ViewPrototype {
case "key": { case "key": {
mView.setOnKeyListener((v, keyCode, event) -> { mView.setOnKeyListener((v, keyCode, event) -> {
BaseEvent e = new BaseEvent(mScope, event, event.getClass()); 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(); return e.isConsumed();
}); });
} }

View File

@@ -100,7 +100,9 @@ public class XmlConverter {
.handler("paddingRight", new AttributeHandler.DimenHandler("paddingRight")) .handler("paddingRight", new AttributeHandler.DimenHandler("paddingRight"))
.handler("paddingTop", new AttributeHandler.DimenHandler("paddingTop")) .handler("paddingTop", new AttributeHandler.DimenHandler("paddingTop"))
.handler("paddingBottom", new AttributeHandler.DimenHandler("paddingBottom")) .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 { public static String convertToAndroidLayout(String xml) throws IOException, SAXException, ParserConfigurationException {
return convertToAndroidLayout(new InputSource(new StringReader(xml))); 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 { public class RunnableScriptExecution extends ScriptExecution.AbstractScriptExecution implements Runnable {
private static final String TAG = "RunnableJSExecution"; private static final String TAG = "RunnableJSExecution";
private ScriptEngine mScriptEngine; private ScriptEngine mScriptEngine;
private ScriptEngineManager mScriptEngineManager; private ScriptEngineManager mScriptEngineManager;
@@ -39,16 +40,25 @@ public class RunnableScriptExecution extends ScriptExecution.AbstractScriptExecu
try { try {
prepare(engine); prepare(engine);
Object r = doExecution(engine); Object r = doExecution(engine);
Exception uncaughtException = engine.getUncaughtException();
if (uncaughtException != null) {
onException(engine, uncaughtException);
return null;
}
getListener().onSuccess(this, r); getListener().onSuccess(this, r);
return r; return r;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); onException(engine, e);
getListener().onException(this, e); return null;
} finally { } finally {
Log.d(TAG, "Engine destroy"); Log.d(TAG, "Engine destroy");
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) { private void prepare(ScriptEngine engine) {

View File

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

View File

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

View File

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