From fc05252540f4c42f7b66428680e9211d1cbbd350 Mon Sep 17 00:00:00 2001 From: hyb1996 <946994919@qq.com> Date: Tue, 16 Oct 2018 15:57:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20view=E4=BA=8B=E4=BB=B6touc?= =?UTF-8?q?h=5Fdown,=20touch=5Fup,=20touch=5Fmove,=20key=5Fdown,=20key=5Fu?= =?UTF-8?q?p=20=E4=BF=AE=E5=A4=8D=20=E6=9F=90=E4=BA=9B=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E8=84=9A=E6=9C=AC=E6=97=A0=E5=A3=B0=E9=80=80=E5=87=BA?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=88=E6=B2=A1=E6=9C=89=E4=BB=BB?= =?UTF-8?q?=E4=BD=95=E6=8A=A5=E9=94=99=E4=BF=A1=E6=81=AF=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=8F=B0=E6=89=93=E5=8D=B0=E8=84=9A=E6=9C=AC=E9=80=80=E5=87=BA?= =?UTF-8?q?=EF=BC=89=20=E4=BF=AE=E5=A4=8D=20=E6=9F=90=E4=BA=9B=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8B=E8=B0=83=E7=94=A8exit=E6=97=B6ui=E4=B8=8D?= =?UTF-8?q?=E9=80=80=E5=87=BA=E6=88=96=E5=AD=90=E7=BA=BF=E7=A8=8B=E4=B8=8D?= =?UTF-8?q?=E9=80=80=E5=87=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/ui/nativeview/ViewPrototype.java | 17 ++++++++++++++++- .../autojs/core/ui/xml/XmlConverter.java | 4 +++- .../execution/RunnableScriptExecution.java | 16 +++++++++++++--- .../stardust/autojs/runtime/ScriptRuntime.java | 13 ++++++------- .../stardust/autojs/runtime/api/Threads.java | 12 +++++++++++- project-versions.json | 4 ++-- 6 files changed, 51 insertions(+), 15 deletions(-) diff --git a/autojs/src/main/java/com/stardust/autojs/core/ui/nativeview/ViewPrototype.java b/autojs/src/main/java/com/stardust/autojs/core/ui/nativeview/ViewPrototype.java index a8e3ff4f..3d2ded89 100644 --- a/autojs/src/main/java/com/stardust/autojs/core/ui/nativeview/ViewPrototype.java +++ b/autojs/src/main/java/com/stardust/autojs/core/ui/nativeview/ViewPrototype.java @@ -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(); }); } diff --git a/autojs/src/main/java/com/stardust/autojs/core/ui/xml/XmlConverter.java b/autojs/src/main/java/com/stardust/autojs/core/ui/xml/XmlConverter.java index b3366a2c..c1b71815 100644 --- a/autojs/src/main/java/com/stardust/autojs/core/ui/xml/XmlConverter.java +++ b/autojs/src/main/java/com/stardust/autojs/core/ui/xml/XmlConverter.java @@ -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))); diff --git a/autojs/src/main/java/com/stardust/autojs/execution/RunnableScriptExecution.java b/autojs/src/main/java/com/stardust/autojs/execution/RunnableScriptExecution.java index ce87eebc..29863f28 100644 --- a/autojs/src/main/java/com/stardust/autojs/execution/RunnableScriptExecution.java +++ b/autojs/src/main/java/com/stardust/autojs/execution/RunnableScriptExecution.java @@ -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) { diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/ScriptRuntime.java b/autojs/src/main/java/com/stardust/autojs/runtime/ScriptRuntime.java index 8d1c5537..57cb8029 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/ScriptRuntime.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/ScriptRuntime.java @@ -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(); } } diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/api/Threads.java b/autojs/src/main/java/com/stardust/autojs/runtime/api/Threads.java index b05df963..4b58d07a 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/api/Threads.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/api/Threads.java @@ -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) { diff --git a/project-versions.json b/project-versions.json index e21a0e93..57b48bdf 100644 --- a/project-versions.json +++ b/project-versions.json @@ -1,6 +1,6 @@ { - "appVersionCode": 431, - "appVersionName": "4.0.4 Alpha2", + "appVersionCode": 432, + "appVersionName": "4.0.4 Alpha3", "target": 28, "mini": 17, "compile": 28,