fix: timer for ui thread is null in non-ui script
This commit is contained in:
2
.idea/modules.xml
generated
2
.idea/modules.xml
generated
@@ -2,8 +2,8 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectModuleManager">
|
<component name="ProjectModuleManager">
|
||||||
<modules>
|
<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://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://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$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
|
||||||
<module fileurl="file://$PROJECT_DIR$/autojs/autojs.iml" filepath="$PROJECT_DIR$/autojs/autojs.iml" />
|
<module fileurl="file://$PROJECT_DIR$/autojs/autojs.iml" filepath="$PROJECT_DIR$/autojs/autojs.iml" />
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.stardust.autojs.core.looper;
|
package com.stardust.autojs.core.looper;
|
||||||
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.SparseArray;
|
import android.util.SparseArray;
|
||||||
@@ -27,15 +28,18 @@ public class Timer {
|
|||||||
mBridges = bridges;
|
mBridges = bridges;
|
||||||
mMaxCallbackMillisForAllThread = maxCallbackMillisForAllThread;
|
mMaxCallbackMillisForAllThread = maxCallbackMillisForAllThread;
|
||||||
mHandler = new Handler();
|
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) {
|
public int setTimeout(final Object callback, final long delay, final Object... args) {
|
||||||
Log.d(LOG_TAG, "setTimeout: handler = " + mHandler);
|
|
||||||
mCallbackMaxId++;
|
mCallbackMaxId++;
|
||||||
final int id = mCallbackMaxId;
|
final int id = mCallbackMaxId;
|
||||||
Runnable r = () -> {
|
Runnable r = () -> {
|
||||||
Log.d(LOG_TAG, "callFunction: handler = " + mHandler);
|
|
||||||
mBridges.callFunction(callback, null, args);
|
mBridges.callFunction(callback, null, args);
|
||||||
mHandlerCallbacks.remove(id);
|
mHandlerCallbacks.remove(id);
|
||||||
};
|
};
|
||||||
@@ -105,8 +109,6 @@ public class Timer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasPendingCallbacks() {
|
public boolean hasPendingCallbacks() {
|
||||||
Log.d(LOG_TAG, "[thread]hasPendingCallbacks:" + (mMaxCallbackUptimeMillis > SystemClock.uptimeMillis()));
|
|
||||||
Log.d(LOG_TAG, "mMaxCallbackUptimeMillisForAllThreads:" + mMaxCallbackUptimeMillis);
|
|
||||||
return mMaxCallbackUptimeMillis > SystemClock.uptimeMillis();
|
return mMaxCallbackUptimeMillis > SystemClock.uptimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.stardust.autojs.runtime.api;
|
package com.stardust.autojs.runtime.api;
|
||||||
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.SparseArray;
|
import android.util.SparseArray;
|
||||||
@@ -21,10 +22,12 @@ public class Timers {
|
|||||||
private VolatileBox<Long> mMaxCallbackUptimeMillisForAllThreads = new VolatileBox<>(0L);
|
private VolatileBox<Long> mMaxCallbackUptimeMillisForAllThreads = new VolatileBox<>(0L);
|
||||||
private Threads mThreads;
|
private Threads mThreads;
|
||||||
private Timer mMainTimer;
|
private Timer mMainTimer;
|
||||||
|
private Timer mUiTimer;
|
||||||
|
|
||||||
|
|
||||||
public Timers(ScriptBridges bridges, Threads threads) {
|
public Timers(ScriptBridges bridges, Threads threads) {
|
||||||
mMainTimer = new Timer(bridges, mMaxCallbackUptimeMillisForAllThreads);
|
mMainTimer = new Timer(bridges, mMaxCallbackUptimeMillisForAllThreads);
|
||||||
|
mUiTimer = new Timer(bridges, mMaxCallbackUptimeMillisForAllThreads, Looper.getMainLooper());
|
||||||
mThreads = threads;
|
mThreads = threads;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +47,11 @@ public class Timers {
|
|||||||
if (thread == mThreads.getMainThread()) {
|
if (thread == mThreads.getMainThread()) {
|
||||||
return mMainTimer;
|
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) {
|
public int setTimeout(Object callback, long delay, Object... args) {
|
||||||
|
|||||||
Reference in New Issue
Block a user