开始玩一玩安桌的代码

前面的已经忘光了

/home/x/work/lineageOS/system/core/healthd/BatteryMonitor.cpp

电池管理单元,BatteryService会调用,得到底层数据

数据从内核文件传出来

可以在 bool BatteryMonitor::update(void) 函数中修改电池数据,模拟电池消耗 
/*
//伪装电量 66%
props.batteryLevel = 66;
//伪装当前电压
props.batteryVoltage = ( (4400 - 3700)/100 * props.batteryLevel) + 3700;
//非充电状态
props.chargerUsbOnline=false;
*/
void BatteryMonitor::init(struct healthd_config *hc) {
    String8 path;
    char pval[PROPERTY_VALUE_MAX];

    mHealthdConfig = hc;
    DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
    if (dir == NULL) {
        KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
    } else {
        struct dirent* entry;

        while ((entry = readdir(dir))) {
            const char* name = entry->d_name;

            if (!strcmp(name, ".") || !strcmp(name, ".."))
                continue;

            // Look for "type" file in each subdirectory
            path.clear();
            path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
            switch(readPowerSupplyType(path)) {
            case ANDROID_POWER_SUPPLY_TYPE_AC:
            case ANDROID_POWER_SUPPLY_TYPE_USB:
            case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
            case ANDROID_POWER_SUPPLY_TYPE_DOCK_AC:
                path.clear();
                path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
                if (access(path.string(), R_OK) == 0)
                    mChargerNames.add(String8(name));
                break;

            case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
                mBatteryDevicePresent = true;

                if (mHealthdConfig->batteryStatusPath.isEmpty()) {
                    path.clear();
                    path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
                                      name);
                    if (access(path, R_OK) == 0)
                        mHealthdConfig->batteryStatusPath = path;
                }

                if (mHealthdConfig->batteryHealthPath.isEmpty()) {
                    path.clear();
                    path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
                                      name);
                    if (access(path, R_OK) == 0)
                        mHealthdConfig->batteryHealthPath = path;
                }

                if (mHealthdConfig->batteryPresentPath.isEmpty()) {
                    path.clear();
                    path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
                                      name);
                    if (access(path, R_OK) == 0)
                        mHealthdConfig->batteryPresentPath = path;
                }

                if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
                    path.clear();
                    path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
                                      name);
                    if (access(path, R_OK) == 0)
                        mHealthdConfig->batteryCapacityPath = path;
                }

                if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
                    path.clear();
                    path.appendFormat("%s/%s/voltage_now",
                                      POWER_SUPPLY_SYSFS_PATH, name);
                    if (access(path, R_OK) == 0) {
                        mHealthdConfig->batteryVoltagePath = path;
                    } else {
                        path.clear();
                        path.appendFormat("%s/%s/batt_vol",
                                          POWER_SUPPLY_SYSFS_PATH, name);
                        if (access(path, R_OK) == 0)
                            mHealthdConfig->batteryVoltagePath = path;
                    }
                }

                if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
                    path.clear();
                    path.appendFormat("%s/%s/charge_full",
                                      POWER_SUPPLY_SYSFS_PATH, name);
                    if (access(path, R_OK) == 0)
                        mHealthdConfig->batteryFullChargePath = path;
                }

                if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
                    path.clear();
                    path.appendFormat("%s/%s/current_now",
                                      POWER_SUPPLY_SYSFS_PATH, name);
                    if (access(path, R_OK) == 0)
                        mHealthdConfig->batteryCurrentNowPath = path;
                }

                if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
                    path.clear();
                    path.appendFormat("%s/%s/cycle_count",
                                      POWER_SUPPLY_SYSFS_PATH, name);
                    if (access(path, R_OK) == 0)
                        mHealthdConfig->batteryCycleCountPath = path;
                }

                if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
                    path.clear();
                    path.appendFormat("%s/%s/current_avg",
                                      POWER_SUPPLY_SYSFS_PATH, name);
                    if (access(path, R_OK) == 0)
                        mHealthdConfig->batteryCurrentAvgPath = path;
                }

                if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
                    path.clear();
                    path.appendFormat("%s/%s/charge_counter",
                                      POWER_SUPPLY_SYSFS_PATH, name);
                    if (access(path, R_OK) == 0)
                        mHealthdConfig->batteryChargeCounterPath = path;
                }

                if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
                    path.clear();
                    path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
                                      name);
                    if (access(path, R_OK) == 0) {
                        mHealthdConfig->batteryTemperaturePath = path;
                    } else {
                        path.clear();
                        path.appendFormat("%s/%s/batt_temp",
                                          POWER_SUPPLY_SYSFS_PATH, name);
                        if (access(path, R_OK) == 0)
                            mHealthdConfig->batteryTemperaturePath = path;
                    }
                }

                if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
                    path.clear();
                    path.appendFormat("%s/%s/technology",
                                      POWER_SUPPLY_SYSFS_PATH, name);
                    if (access(path, R_OK) == 0)
                        mHealthdConfig->batteryTechnologyPath = path;
                }

                break;

            case ANDROID_POWER_SUPPLY_TYPE_DOCK_BATTERY:
                if (mHealthdConfig->dockBatterySupported) {
                    mDockBatteryDevicePresent = true;

                    if (mHealthdConfig->dockBatteryStatusPath.isEmpty()) {
                        path.clear();
                        path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
                                          name);
                        if (access(path, R_OK) == 0)
                            mHealthdConfig->dockBatteryStatusPath = path;
                    }

                    if (mHealthdConfig->dockBatteryHealthPath.isEmpty()) {
                        path.clear();
                        path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
                                          name);
                        if (access(path, R_OK) == 0)
                            mHealthdConfig->dockBatteryHealthPath = path;
                    }

                    if (mHealthdConfig->dockBatteryPresentPath.isEmpty()) {
                        path.clear();
                        path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
                                          name);
                        if (access(path, R_OK) == 0)
                            mHealthdConfig->dockBatteryPresentPath = path;
                    }

                    if (mHealthdConfig->dockBatteryCapacityPath.isEmpty()) {
                        path.clear();
                        path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
                                          name);
                        if (access(path, R_OK) == 0)
                            mHealthdConfig->dockBatteryCapacityPath = path;
                    }

                    if (mHealthdConfig->dockBatteryVoltagePath.isEmpty()) {
                        path.clear();
                        path.appendFormat("%s/%s/voltage_now",
                                          POWER_SUPPLY_SYSFS_PATH, name);
                        if (access(path, R_OK) == 0) {
                            mHealthdConfig->dockBatteryVoltagePath = path;
                        } else {
                            path.clear();
                            path.appendFormat("%s/%s/batt_vol",
                                              POWER_SUPPLY_SYSFS_PATH, name);
                            if (access(path, R_OK) == 0)
                                mHealthdConfig->dockBatteryVoltagePath = path;
                        }
                    }

                    if (mHealthdConfig->dockBatteryCurrentNowPath.isEmpty()) {
                        path.clear();
                        path.appendFormat("%s/%s/current_now",
                                          POWER_SUPPLY_SYSFS_PATH, name);
                        if (access(path, R_OK) == 0)
                            mHealthdConfig->dockBatteryCurrentNowPath = path;
                    }

                    if (mHealthdConfig->dockBatteryCurrentAvgPath.isEmpty()) {
                        path.clear();
                        path.appendFormat("%s/%s/current_avg",
                                          POWER_SUPPLY_SYSFS_PATH, name);
                        if (access(path, R_OK) == 0)
                            mHealthdConfig->dockBatteryCurrentAvgPath = path;
                    }

                    if (mHealthdConfig->dockBatteryChargeCounterPath.isEmpty()) {
                        path.clear();
                        path.appendFormat("%s/%s/charge_counter",
                                          POWER_SUPPLY_SYSFS_PATH, name);
                        if (access(path, R_OK) == 0)
                            mHealthdConfig->dockBatteryChargeCounterPath = path;
                    }

                    if (mHealthdConfig->dockBatteryTemperaturePath.isEmpty()) {
                        path.clear();
                        path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
                                          name);
                        if (access(path, R_OK) == 0) {
                            mHealthdConfig->dockBatteryTemperaturePath = path;
                        } else {
                            path.clear();
                            path.appendFormat("%s/%s/batt_temp",
                                              POWER_SUPPLY_SYSFS_PATH, name);
                            if (access(path, R_OK) == 0)
                                mHealthdConfig->dockBatteryTemperaturePath = path;
                        }
                    }

                    if (mHealthdConfig->dockBatteryTechnologyPath.isEmpty()) {
                        path.clear();
                        path.appendFormat("%s/%s/technology",
                                          POWER_SUPPLY_SYSFS_PATH, name);
                        if (access(path, R_OK) == 0)
                            mHealthdConfig->dockBatteryTechnologyPath = path;
                    }
                }

                break;

            case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
                break;
            }
        }
        closedir(dir);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值