Android 6.0 BatteryManager分析

来源:互联网 发布:python 遍历字符串 编辑:程序博客网 时间:2024/06/05 18:59

BatteryManager.java

BatteryManager

构造函数

    public BatteryManager() {        mBatteryStats = IBatteryStats.Stub.asInterface(                ServiceManager.getService(BatteryStats.SERVICE_NAME));        mBatteryPropertiesRegistrar = IBatteryPropertiesRegistrar.Stub.asInterface(                ServiceManager.getService("batteryproperties"));//healthd中add到service manager中的    }


queryProperty

/**     * Query a battery property from the batteryproperties service.     *     * Returns the requested value, or Long.MIN_VALUE if property not     * supported on this system or on other error.     */    private long queryProperty(int id) {        long ret;        if (mBatteryPropertiesRegistrar == null) {            return Long.MIN_VALUE;        }        try {            BatteryProperty prop = new BatteryProperty();            if (mBatteryPropertiesRegistrar.getProperty(id, prop) == 0)                ret = prop.getLong();            else                ret = Long.MIN_VALUE;        } catch (RemoteException e) {            ret = Long.MIN_VALUE;        }        return ret;    }


healthd_mode_android.cpp

void healthd_mode_android_init(struct healthd_config* /*config*/) {    ProcessState::self()->setThreadPoolMaxThreadCount(0);    IPCThreadState::self()->disableBackgroundScheduling(true);    IPCThreadState::self()->setupPolling(&gBinderFd);    if (gBinderFd >= 0) {        if (healthd_register_event(gBinderFd, binder_event))            KLOG_ERROR(LOG_TAG,                       "Register for binder events failed\n");    }    gBatteryPropertiesRegistrar = new BatteryPropertiesRegistrar();    gBatteryPropertiesRegistrar->publish();}

getProperty

status_t BatteryPropertiesRegistrar::getProperty(int id, struct BatteryProperty *val) {    return healthd_get_property(id, val);}


BatteryMonitor.cpp

getProperty

status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {    status_t ret = BAD_VALUE;    val->valueInt64 = LONG_MIN;    switch(id) {    case BATTERY_PROP_CHARGE_COUNTER:        if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {            val->valueInt64 =                getIntField(mHealthdConfig->batteryChargeCounterPath);            ret = NO_ERROR;        } else {            ret = NAME_NOT_FOUND;        }        break;    case BATTERY_PROP_CURRENT_NOW:        if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {            val->valueInt64 =                getIntField(mHealthdConfig->batteryCurrentNowPath);            ret = NO_ERROR;        } else {            ret = NAME_NOT_FOUND;        }        break;    case BATTERY_PROP_CURRENT_AVG:        if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {            val->valueInt64 =                getIntField(mHealthdConfig->batteryCurrentAvgPath);            ret = NO_ERROR;        } else {            ret = NAME_NOT_FOUND;        }        break;    case BATTERY_PROP_CAPACITY:        if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {            val->valueInt64 =                getIntField(mHealthdConfig->batteryCapacityPath);            ret = NO_ERROR;        } else {            ret = NAME_NOT_FOUND;        }        break;    case BATTERY_PROP_ENERGY_COUNTER:        if (mHealthdConfig->energyCounter) {            ret = mHealthdConfig->energyCounter(&val->valueInt64);        } else {            ret = NAME_NOT_FOUND;        }        break;    default:        break;    }    return ret;}


0 0
原创粉丝点击