feat(android): runOnMain, postFrameCallback & removeFrameCallback (#9943)

This commit is contained in:
Osei Fortune
2022-07-13 18:21:04 -04:00
committed by GitHub
parent 6f48b4bef3
commit 49343cb9b4
2 changed files with 61 additions and 19 deletions

View File

@@ -1,20 +1,33 @@
import * as definition from './fps-native';
import { Device } from '../platform';
export class FPSCallback implements definition.FPSCallback {
private impl: android.view.Choreographer.FrameCallback;
private impl: android.view.Choreographer.FrameCallback | ((nanos: number) => void);
private onFrame: (currentTimeMillis: number) => void;
public running: boolean;
sdkVersion: number;
constructor(onFrame: (currentTimeMillis: number) => void) {
this.running = false;
this.onFrame = onFrame;
this.impl = new android.view.Choreographer.FrameCallback({
doFrame: (nanos: number) => {
this.sdkVersion = parseInt(Device.sdkVersion);
if (this.sdkVersion >= 24 && this._isNativeFramesSupported()) {
this.impl = (nanos: number) => {
this.handleFrame(nanos);
},
});
};
} else {
this.impl = new android.view.Choreographer.FrameCallback({
doFrame: (nanos: number) => {
this.handleFrame(nanos);
},
});
}
}
private _isNativeFramesSupported() {
return typeof (<any>global).__postFrameCallback === 'function' && typeof (<any>global).__removeFrameCallback === 'function';
}
public start() {
@@ -22,7 +35,12 @@ export class FPSCallback implements definition.FPSCallback {
return;
}
android.view.Choreographer.getInstance().postFrameCallback(this.impl);
if (this.sdkVersion >= 24 && this._isNativeFramesSupported()) {
(global as any).__postFrameCallback(this.impl);
} else {
android.view.Choreographer.getInstance().postFrameCallback(this.impl as any);
}
this.running = true;
}
@@ -31,7 +49,12 @@ export class FPSCallback implements definition.FPSCallback {
return;
}
android.view.Choreographer.getInstance().removeFrameCallback(this.impl);
if (this.sdkVersion >= 24 && this._isNativeFramesSupported()) {
(global as any).__removeFrameCallback(this.impl);
} else {
android.view.Choreographer.getInstance().removeFrameCallback(this.impl as any);
}
this.running = false;
}
@@ -43,6 +66,11 @@ export class FPSCallback implements definition.FPSCallback {
// divide by 1 000 000 since the parameter is in nanoseconds
this.onFrame(nanos / 1000000);
// add the FrameCallback instance again since it is automatically removed from the Choreographer
android.view.Choreographer.getInstance().postFrameCallback(this.impl);
if (this.sdkVersion >= 24 && this._isNativeFramesSupported()) {
(global as any).__postFrameCallback(this.impl);
} else {
android.view.Choreographer.getInstance().postFrameCallback(this.impl as any);
}
}
}

View File

@@ -1,9 +1,18 @@
import { android as ad } from '../application';
export function dispatchToMainThread(func: () => void) {
new android.os.Handler(android.os.Looper.getMainLooper()).post(
new java.lang.Runnable({
run: func,
})
);
const runOnMainThread = (global as any).__runOnMainThread;
if (runOnMainThread) {
runOnMainThread(() => {
func();
});
} else {
new android.os.Handler(android.os.Looper.getMainLooper()).post(
new java.lang.Runnable({
run: func,
})
);
}
}
export function isMainThread(): boolean {
@@ -11,9 +20,14 @@ export function isMainThread(): boolean {
}
export function dispatchToUIThread(func: () => void) {
return function (func) {
if (func) {
func();
}
};
const activity: androidx.appcompat.app.AppCompatActivity = ad.foregroundActivity || ad.startActivity;
if (activity && func) {
activity.runOnUiThread(
new java.lang.Runnable({
run() {
func();
},
})
);
}
}