-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryAppState.ts
48 lines (39 loc) · 1.15 KB
/
queryAppState.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type { AppId } from '@dlenroc/roku-ecp';
import type { Driver } from '../Driver.ts';
export async function queryAppState(
this: Driver,
appId: string
): Promise<0 | 1 | 2 | 3 | 4> {
const appState = await this.sdk.ecp
.queryAppState({ appId: appId as AppId })
// TODO: Replace fallback with a check based on the OS version
.catch(() => ({ status: 'FAILED' } as const));
if (appState.status === 'OK') {
// not running
if (appState.state === 'inactive') {
return 1;
}
// running in background or suspended
if (appState.state === 'background') {
return 2;
}
if (appState.state === 'active') {
const activeApp = await this.sdk.ecp.queryActiveApp();
// running in background
if (activeApp.screensaver) {
return 3;
}
// is running in foreground
return 4;
}
} else {
const activeApp = await this.sdk.ecp.queryActiveApp();
// running in foreground
if ('id' in activeApp.app && activeApp.app.id == appId) {
return 4;
}
}
const isInstalled = await this.isAppInstalled(appId);
// not installed/running
return +isInstalled as 0 | 1;
}