fix: timer for ui thread is null in non-ui script

This commit is contained in:
hyb1996 2017-12-29 00:35:22 +08:00
parent ee0c7f4916
commit c12814769f
3 changed files with 16 additions and 7 deletions

View File

@ -2,8 +2,8 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Auto.js.iml" filepath="$PROJECT_DIR$/Auto.js.iml" />
<module fileurl="file://E:\YiBin\AndroidStudioProjects\NoRootScriptDroid\Auto.js.iml" filepath="E:\YiBin\AndroidStudioProjects\NoRootScriptDroid\Auto.js.iml" />
<module fileurl="file://$PROJECT_DIR$/NoRootScriptDroid.iml" filepath="$PROJECT_DIR$/NoRootScriptDroid.iml" />
<module fileurl="file://C:\Users\Stardust\Documents\AndroidProjects\Auto.js\NoRootScriptDroid.iml" filepath="C:\Users\Stardust\Documents\AndroidProjects\Auto.js\NoRootScriptDroid.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/autojs/autojs.iml" filepath="$PROJECT_DIR$/autojs/autojs.iml" />

View File

@ -1,6 +1,7 @@
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;
@ -27,15 +28,18 @@ public class Timer {
mBridges = bridges;
mMaxCallbackMillisForAllThread = maxCallbackMillisForAllThread;
mHandler = new Handler();
Log.d(LOG_TAG, "Timer: handler = " + mHandler + ", thread = " + Thread.currentThread());
}
public Timer(ScriptBridges bridges, VolatileBox<Long> maxCallbackMillisForAllThread, Looper looper) {
mBridges = bridges;
mMaxCallbackMillisForAllThread = maxCallbackMillisForAllThread;
mHandler = new Handler(looper);
}
public int setTimeout(final Object callback, final long delay, final Object... args) {
Log.d(LOG_TAG, "setTimeout: handler = " + mHandler);
mCallbackMaxId++;
final int id = mCallbackMaxId;
Runnable r = () -> {
Log.d(LOG_TAG, "callFunction: handler = " + mHandler);
mBridges.callFunction(callback, null, args);
mHandlerCallbacks.remove(id);
};
@ -105,8 +109,6 @@ public class Timer {
}
public boolean hasPendingCallbacks() {
Log.d(LOG_TAG, "[thread]hasPendingCallbacks:" + (mMaxCallbackUptimeMillis > SystemClock.uptimeMillis()));
Log.d(LOG_TAG, "mMaxCallbackUptimeMillisForAllThreads:" + mMaxCallbackUptimeMillis);
return mMaxCallbackUptimeMillis > SystemClock.uptimeMillis();
}

View File

@ -1,6 +1,7 @@
package com.stardust.autojs.runtime.api;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
import android.util.SparseArray;
@ -21,10 +22,12 @@ public class Timers {
private VolatileBox<Long> mMaxCallbackUptimeMillisForAllThreads = new VolatileBox<>(0L);
private Threads mThreads;
private Timer mMainTimer;
private Timer mUiTimer;
public Timers(ScriptBridges bridges, Threads threads) {
mMainTimer = new Timer(bridges, mMaxCallbackUptimeMillisForAllThreads);
mUiTimer = new Timer(bridges, mMaxCallbackUptimeMillisForAllThreads, Looper.getMainLooper());
mThreads = threads;
}
@ -44,7 +47,11 @@ public class Timers {
if (thread == mThreads.getMainThread()) {
return mMainTimer;
}
return TimerThread.getTimerForThread(thread);
Timer timer = TimerThread.getTimerForThread(thread);
if (timer == null && Looper.myLooper() == Looper.getMainLooper()) {
return mUiTimer;
}
return timer;
}
public int setTimeout(Object callback, long delay, Object... args) {