index.uts 1.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
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
import Context from "android.content.Context";
import BatteryManager from "android.os.BatteryManager";
import { UTSAndroid } from "io.dcloud.uts";

import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoFail, GetBatteryInfoOptions } from '../interface.uts'

export const getBatteryInfo : GetBatteryInfo = function (options) {
    const context = UTSAndroid.getAppContext();
    if (context != null) {
        const manager = context.getSystemService(
            Context.BATTERY_SERVICE
        ) as BatteryManager;
        const level = manager.getIntProperty(
            BatteryManager.BATTERY_PROPERTY_CAPACITY
        );
        const res : GetBatteryInfoSuccess = {
            errCode: 0,
            errSubject: "uni-getBatteryInfo",
            errMsg: 'getBatteryInfo:ok',
            level,
            isCharging: manager.isCharging()
        }
        options.success?.(res)
        options.complete?.(res)
    } else {
        const res : GetBatteryInfoFail = {
            errSubject: "uni-getBatteryInfo",
            errCode: 1001,
            errMsg: 'getBatteryInfo:fail getAppContext is null',
            cause: null
        }
        options.fail?.(res)
        options.complete?.(res)
    }
}