fix: app crash when longClick throws exception
This commit is contained in:
@@ -110,7 +110,7 @@ public class Drawables {
|
||||
} else if(value.startsWith("data:")) {
|
||||
loadDataInto(view, value);
|
||||
}else {
|
||||
view.setImageDrawable(com.stardust.autojs.core.ui.inflater.util.Drawables.parse(view, value));
|
||||
view.setImageDrawable(Drawables.parse(view, value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,9 +4,7 @@ import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
import com.stardust.autojs.BuildConfig;
|
||||
import com.stardust.autojs.execution.ScriptExecutionListener;
|
||||
import com.stardust.autojs.rhino.AndroidContextFactory;
|
||||
import com.stardust.autojs.rhino.RhinoAndroidHelper;
|
||||
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
|
||||
import com.stardust.autojs.script.JavaScriptSource;
|
||||
import com.stardust.autojs.script.StringScriptSource;
|
||||
@@ -16,8 +14,6 @@ import com.stardust.pio.UncheckedIOException;
|
||||
|
||||
import org.mozilla.javascript.Callable;
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.ContextFactory;
|
||||
import org.mozilla.javascript.ErrorReporter;
|
||||
import org.mozilla.javascript.ImporterTopLevel;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
import org.mozilla.javascript.ScriptableObject;
|
||||
@@ -48,7 +44,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
private Scriptable mScriptable;
|
||||
private Thread mThread;
|
||||
private android.content.Context mAndroidContext;
|
||||
private Thread.UncaughtExceptionHandler mUiThreadExceptionHandler;
|
||||
private Thread.UncaughtExceptionHandler mUncaughtExceptionHandler;
|
||||
|
||||
public RhinoJavaScriptEngine(android.content.Context context) {
|
||||
mAndroidContext = context;
|
||||
@@ -165,8 +161,12 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
context.setWrapFactory(new WrapFactory());
|
||||
}
|
||||
|
||||
public void setUiThreadExceptionHandler(Thread.UncaughtExceptionHandler uiThreadExceptionHandler) {
|
||||
mUiThreadExceptionHandler = uiThreadExceptionHandler;
|
||||
public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uiThreadExceptionHandler) {
|
||||
mUncaughtExceptionHandler = uiThreadExceptionHandler;
|
||||
}
|
||||
|
||||
public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() {
|
||||
return mUncaughtExceptionHandler;
|
||||
}
|
||||
|
||||
private class WrapFactory extends org.mozilla.javascript.WrapFactory {
|
||||
@@ -212,7 +212,7 @@ public class RhinoJavaScriptEngine extends JavaScriptEngine {
|
||||
try {
|
||||
return super.doTopCall(callable, cx, scope, thisObj, args);
|
||||
} catch (Exception e) {
|
||||
mUiThreadExceptionHandler.uncaughtException(Thread.currentThread(), e);
|
||||
mUncaughtExceptionHandler.uncaughtException(Thread.currentThread(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public class LoopedBasedJavaScriptExecution extends RunnableScriptExecution {
|
||||
long delay = getConfig().delay;
|
||||
sleep(delay);
|
||||
final LoopBasedJavaScriptEngine javaScriptEngine = (LoopBasedJavaScriptEngine) engine;
|
||||
javaScriptEngine.setUiThreadExceptionHandler((t, e) -> {
|
||||
javaScriptEngine.setUncaughtExceptionHandler((t, e) -> {
|
||||
javaScriptEngine.forceStop();
|
||||
getListener().onException(this, (Exception) e);
|
||||
});
|
||||
|
||||
@@ -49,7 +49,7 @@ public class ScriptExecuteActivity extends AppCompatActivity implements Thread.U
|
||||
mScriptSource = mScriptExecution.getSource();
|
||||
mScriptEngine = mScriptExecution.getEngine();
|
||||
mExecutionListener = mScriptExecution.getListener();
|
||||
((RhinoJavaScriptEngine) mScriptEngine).setUiThreadExceptionHandler((t, e) -> onException((Exception) e));
|
||||
((RhinoJavaScriptEngine) mScriptEngine).setUncaughtExceptionHandler((t, e) -> onException((Exception) e));
|
||||
runScript();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.stardust.autojs.ScriptEngineService;
|
||||
import com.stardust.autojs.annotation.ScriptVariable;
|
||||
import com.stardust.autojs.core.accessibility.AccessibilityBridge;
|
||||
import com.stardust.autojs.core.image.Colors;
|
||||
import com.stardust.autojs.engine.RhinoJavaScriptEngine;
|
||||
import com.stardust.autojs.engine.ScriptEngine;
|
||||
import com.stardust.autojs.rhino.AndroidClassLoader;
|
||||
import com.stardust.autojs.runtime.api.AbstractShell;
|
||||
@@ -35,6 +36,7 @@ import com.stardust.lang.ThreadCompat;
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
import com.stardust.util.ClipboardUtil;
|
||||
import com.stardust.autojs.core.util.ProcessShell;
|
||||
import com.stardust.util.Objects;
|
||||
import com.stardust.util.ScreenMetrics;
|
||||
import com.stardust.util.SdkVersionUtil;
|
||||
import com.stardust.util.Supplier;
|
||||
@@ -42,6 +44,7 @@ import com.stardust.util.UiHandler;
|
||||
import com.stardust.view.accessibility.AccessibilityInfoProvider;
|
||||
|
||||
import org.mozilla.javascript.ContextFactory;
|
||||
import org.mozilla.javascript.RhinoException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -305,6 +308,25 @@ public class ScriptRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
public void exit(Object obj) throws Throwable {
|
||||
mThread.interrupt();
|
||||
if (!(obj instanceof Throwable)) {
|
||||
console.error(obj);
|
||||
return;
|
||||
}
|
||||
Throwable e = (Exception) obj;
|
||||
if (Looper.myLooper() != Looper.getMainLooper()) {
|
||||
throw e;
|
||||
} else {
|
||||
Thread.UncaughtExceptionHandler handler = ((RhinoJavaScriptEngine) engines.myEngine()).getUncaughtExceptionHandler();
|
||||
if (handler != null) {
|
||||
handler.uncaughtException(Thread.currentThread(), e);
|
||||
} else {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void stop() {
|
||||
exit();
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.stardust.util.ViewUtil;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/12/5.
|
||||
@@ -30,7 +31,7 @@ public class Floaty {
|
||||
private JsLayoutInflater mJsLayoutInflater;
|
||||
private Context mContext;
|
||||
private UiHandler mUiHandler;
|
||||
private Set<JsFloatyWindow> mWindows = new HashSet<>();
|
||||
private Set<JsFloatyWindow> mWindows = new CopyOnWriteArraySet<>();
|
||||
private ScriptRuntime mRuntime;
|
||||
|
||||
public Floaty(UiHandler uiHandler, UI ui, ScriptRuntime runtime) {
|
||||
|
||||
Reference in New Issue
Block a user