-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathdeviceidle.js
43 lines (39 loc) · 1.46 KB
/
deviceidle.js
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
import {errors} from 'appium/driver';
import _ from 'lodash';
const SUPPORTED_ACTIONS = ['whitelistAdd', 'whitelistRemove'];
/**
* This is a wrapper to 'adb shell dumpsys deviceidle' interface.
* Read https://www.protechtraining.com/blog/post/diving-into-android-m-doze-875
* for more details.
*
* @param {'whitelistAdd' | 'whitelistRemove'} action The action name to execute
* @param {string} [packages] Either a single package or multiple packages to add or remove from the idle whitelist
* @returns {Promise<void>}
*/
export async function mobileDeviceidle(action, packages) {
if (!(_.isString(packages) || _.isArray(packages))) {
throw new errors.InvalidArgumentError(`packages argument must be a string or an array`);
}
/** @type {string[]} */
const packagesArr = _.isArray(packages) ? packages : [packages];
/** @type {string[]} */
const commonArgs = ['dumpsys', 'deviceidle', 'whitelist'];
/** @type {(x: string) => string[]} */
let argsGenerator;
switch (action) {
case SUPPORTED_ACTIONS[0]:
argsGenerator = (pkg) => [...commonArgs, `+${pkg}`];
break;
case SUPPORTED_ACTIONS[1]:
argsGenerator = (pkg) => [...commonArgs, `-${pkg}`];
break;
default:
throw new errors.InvalidArgumentError(
`action must be one of ${JSON.stringify(SUPPORTED_ACTIONS)}. Got '${action}' instead`,
);
}
await this.adb.shellChunks(argsGenerator, packagesArr);
}
/**
* @typedef {import('appium-adb').ADB} ADB
*/