@dderevjanik/termux-api
Version:
This library allows you to interact with your Android device from Node.js using termux-api
54 lines (50 loc) • 1.25 kB
text/typescript
import { exec } from "child_process";
export interface CallLog {}
export interface CallLogOptions {
/**
* limit in retrieved call log
* @default 10
*/
limit?: number;
/**
* offset in call log
* @default 0
*/
offset?: number;
}
// TODO: Finish return data & type
/**
* List call log history
*
* **Requires permission:** `android.permission.READ_CALL_LOG`
*/
export async function callLog(options: CallLogOptions = {}): Promise<void> {
const opts: CallLogOptions = {
limit: 10,
offset: 0,
...options,
};
return new Promise<void>((resolve, reject) => {
const command = `termux-call-log -l ${opts.limit} -o ${opts.offset}`;
exec(command, (error, stdout, stderr) => {
if (error) {
return reject(`Error: ${error.message}`);
}
if (stderr) {
return reject(`Error: ${stderr}`);
}
const output = stdout.trim();
if (output.length === 0) {
// <https://github.com/termux/termux-api/issues/434>
return reject(`Error: No call logs found`);
}
try {
const callLogs = JSON.parse(output);
resolve(callLogs);
} catch (e: any) {
reject(`Error: ${e.message}`);
return;
}
});
});
}