fix: app crash when a timer callback throws exception on main thread; feat: umeng ads

This commit is contained in:
hyb1996
2018-09-15 14:04:01 +08:00
parent 02c64cd7b1
commit a3ae309742
15 changed files with 129 additions and 161 deletions

View File

@@ -3,10 +3,9 @@ package com.stardust.autojs.core.looper;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
import android.util.SparseArray;
import com.stardust.autojs.runtime.ScriptBridges;
import com.stardust.autojs.runtime.ScriptRuntime;
import com.stardust.concurrent.VolatileBox;
/**
@@ -19,19 +18,19 @@ public class Timer {
private SparseArray<Runnable> mHandlerCallbacks = new SparseArray<>();
private int mCallbackMaxId = 0;
private ScriptBridges mBridges;
private ScriptRuntime mRuntime;
private Handler mHandler;
private long mMaxCallbackUptimeMillis = 0;
private final VolatileBox<Long> mMaxCallbackMillisForAllThread;
public Timer(ScriptBridges bridges, VolatileBox<Long> maxCallbackMillisForAllThread) {
mBridges = bridges;
public Timer(ScriptRuntime runtime, VolatileBox<Long> maxCallbackMillisForAllThread) {
mRuntime = runtime;
mMaxCallbackMillisForAllThread = maxCallbackMillisForAllThread;
mHandler = new Handler();
}
public Timer(ScriptBridges bridges, VolatileBox<Long> maxCallbackMillisForAllThread, Looper looper) {
mBridges = bridges;
public Timer(ScriptRuntime runtime, VolatileBox<Long> maxCallbackMillisForAllThread, Looper looper) {
mRuntime = runtime;
mMaxCallbackMillisForAllThread = maxCallbackMillisForAllThread;
mHandler = new Handler(looper);
}
@@ -40,7 +39,7 @@ public class Timer {
mCallbackMaxId++;
final int id = mCallbackMaxId;
Runnable r = () -> {
mBridges.callFunction(callback, null, args);
callFunction(callback, null, args);
mHandlerCallbacks.remove(id);
};
mHandlerCallbacks.put(id, r);
@@ -48,6 +47,18 @@ public class Timer {
return id;
}
private void callFunction(Object callback, Object thiz, Object[] args) {
if(Looper.myLooper() == Looper.getMainLooper()){
try {
mRuntime.bridges.callFunction(callback, thiz, args);
}catch (Exception e){
mRuntime.exit(e);
}
}else {
mRuntime.bridges.callFunction(callback, thiz, args);
}
}
public boolean clearTimeout(int id) {
return clearCallback(id);
}
@@ -60,7 +71,7 @@ public class Timer {
public void run() {
if (mHandlerCallbacks.get(id) == null)
return;
mBridges.callFunction(listener, null, args);
callFunction(listener, null, args);
postDelayed(this, interval);
}
};
@@ -86,7 +97,7 @@ public class Timer {
mCallbackMaxId++;
final int id = mCallbackMaxId;
Runnable r = () -> {
mBridges.callFunction(listener, null, args);
callFunction(listener, null, args);
mHandlerCallbacks.remove(id);
};
mHandlerCallbacks.put(id, r);

View File

@@ -41,7 +41,7 @@ public class TimerThread extends ThreadCompat {
@Override
public void run() {
mRuntime.loopers.prepare();
mTimer = new Timer(mRuntime.bridges, mMaxCallbackUptimeMillisForAllThreads);
mTimer = new Timer(mRuntime, mMaxCallbackUptimeMillisForAllThreads);
sTimerMap.put(Thread.currentThread(), mTimer);
notifyRunning();
new Handler().post(mTarget);

View File

@@ -222,7 +222,7 @@ public class ScriptRuntime {
if (loopers != null)
throw new IllegalStateException("already initialized");
threads = new Threads(this);
timers = new Timers(bridges, threads);
timers = new Timers(this);
loopers = new Loopers(this);
events = new Events(uiHandler.getContext(), accessibilityBridge, this);
mThread = Thread.currentThread();

View File

@@ -9,6 +9,7 @@ import android.util.SparseArray;
import com.stardust.autojs.core.looper.Timer;
import com.stardust.autojs.core.looper.TimerThread;
import com.stardust.autojs.runtime.ScriptBridges;
import com.stardust.autojs.runtime.ScriptRuntime;
import com.stardust.concurrent.VolatileBox;
/**
@@ -25,10 +26,10 @@ public class Timers {
private Timer mUiTimer;
public Timers(ScriptBridges bridges, Threads threads) {
mMainTimer = new Timer(bridges, mMaxCallbackUptimeMillisForAllThreads);
mUiTimer = new Timer(bridges, mMaxCallbackUptimeMillisForAllThreads, Looper.getMainLooper());
mThreads = threads;
public Timers(ScriptRuntime runtime) {
mMainTimer = new Timer(runtime, mMaxCallbackUptimeMillisForAllThreads);
mUiTimer = new Timer(runtime, mMaxCallbackUptimeMillisForAllThreads, Looper.getMainLooper());
mThreads = runtime.threads;
}
public Timer getMainTimer() {

View File

@@ -12,6 +12,22 @@
<item>OncePerDay</item>
</string-array>
<string-array name="ad_types_keys">
<item>随机</item>
<item>新闻</item>
<item>社交</item>
<item>动漫</item>
<item>游戏</item>
</string-array>
<string-array name="ad_types_values">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
<string-array name="root_record_out_file_type_keys">
<item>可编辑的js文件</item>