Android 剩余可用时长的计算公式

1. 剩余可用时长 BatteryStatsImpl.computeBatteryTimeRemaining 函数

http://androidxref.com/9.0.0_r3/xref/frameworks/base/core/java/com/android/internal/os/BatteryStatsImpl.java

**公式:剩余电池可用时长 = 每消耗1%的电量的平均时间 * 当前电量值 **

原理:统计出每次放电的电量变化的时间,获得每消耗1%的电量的平均时间,再根据当前电量值,从而得到预计可以时长

    @Override
    public long computeBatteryTimeRemaining(long curTime) {
        // 当前为充电状态,则无剩余可用时长
        if (!mOnBattery) {
            return -1;
        }
        // 无放电电池信息,则无剩余可用时长
        if (mDischargeStepTracker.mNumStepDurations < 1) {
            return -1;
        }
        // 无每消耗1%的电量的平均时间,则无剩余可用时长
        long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
        if (msPerLevel <= 0) {
            return -1;
        }
        // 剩余电池可用时长 = 每消耗1%的电量的平均时间 * 当前电量值 
        return (msPerLevel * mCurrentBatteryLevel) * 1000;
    }

1.1 是否充电状态 - mOnBattery

    boolean mOnBattery;
    
    // This should probably be exposed in the API, though it's not critical
    public static final int BATTERY_PLUGGED_NONE = OsProtoEnums.BATTERY_PLUGGED_NONE; // = 0
    
    // Plug states, primarily used by android/os/BatteryManager.java.
    enum BatteryPluggedStateEnum {
        // Note that NONE is not in BatteryManager.java's constants.
        BATTERY_PLUGGED_NONE = 0;
        // Power source is an AC charger.
        BATTERY_PLUGGED_AC = 1;
        // Power source is a USB port.
        BATTERY_PLUGGED_USB = 2;
        // Power source is wireless.
        BATTERY_PLUGGED_WIRELESS = 4;
    }
    
    public static boolean isOnBattery(int plugType, int status) {
        // 当前未充电 && 当前电池状态不为未知状态
        return plugType == BATTERY_PLUGGED_NONE && status != BatteryManager.BATTERY_STATUS_UNKNOWN;
    }
    
    @GuardedBy("this")
    public void setBatteryStateLocked(final int status, final int health, final int plugType,
            final int level, /* not final */ int temp, final int volt, final int chargeUAh,
            final int chargeFullUAh) {
        final boolean onBattery = isOnBattery(plugType, status);
        ...
        setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
        ...
    }
    
    @GuardedBy("this")
    protected void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime,
            final boolean onBattery, final int oldStatus, final int level, final int chargeUAh) {
        ...
        if (onBattery) {
            mOnBattery = mOnBatteryInternal = true;
        } else {
            mOnBattery = mOnBatteryInternal = false;
        }
        ...
    }

1.2 放电电池信息条数 mDischargeStepTracker.mNumStepDurations

查看 batterystats-daily.xml 电池信息条数,例如记录不同时间的灭屏、亮屏、省电模式、idle模式、充放电等信息

    // 读取每日电池信息
    public void readDailyStatsLocked() {
        ...
        // 电池每日使用信息
        // 路径: /data/system/batterystats-daily.xml
        
        // 0x 00          00           00    ffffffffff
        // 0x 00[modMode] 00[initMode] level 时间(duration)
        // initMode和modMode 中:
        // 00(灭屏 f) 01(亮屏 o) 02(doze d) 03(doze supend z)
        // 04(省电模式 p) 08(idle模式 i)
        mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
        stream = mDailyFile.openRead();
        parser.setInput(stream, StandardCharsets.UTF_8.name());
        ...
        readDailyItemsLocked(parser);
        ...
    }

    void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
            String tag)
            throws NumberFormatException, XmlPullParserException, IOException {
        ...
            if ("s".equals(tagName)) {
                if (i < num) {
                    String valueAttr = parser.getAttributeValue(null, "v");
                    if (valueAttr != null) {
                        // 例如 0x020362ffffffff00L:zD-62-ffffffff00
                        // 表示当前为doze supend,上一次为 doze,电量为 0x62, 时间为ffffffff00
                        steps.decodeEntryAt(i, valueAttr);
                        i++;
                    }
                }
            }
        ...
        // 每日电池信息
        steps.mNumStepDurations = i;
        ...
    }

1.3 每消耗1%的电量的平均时间 mDischargeStepTracker.computeTimePerLevel()

    public static final long STEP_LEVEL_TIME_MASK = 0x000000ffffffffffL;

    // 求充电或放电的平均时间
    public long computeTimePerLevel() {
        final long[] steps = mStepDurations;
        // 电池信息的总条数
        final int numSteps = mNumStepDurations;

        // For now we'll do a simple average across all steps.
        if (numSteps <= 0) {
            return -1;
        }
        long total = 0;
        for (int i=0; i<numSteps; i++) {
            // total += 0x0203629876543210 & 0x000000ffffffffffL
            // 充电或放电信息的总时间 += 充电或放电的单条时间
            total += steps[i] & STEP_LEVEL_TIME_MASK;
        }
        // 总时间 / 总记录数 
        // 因为为 mDischargeStepTracker.computeTimePerLevel,故这里计算出每消耗1%的电量的平均时间
        return total / numSteps;
    }
    

查看下 mStepDurations 的作用

    public static final int STEP_LEVEL_LEVEL_SHIFT = 40;
    public static final long STEP_LEVEL_LEVEL_MASK = 0x0000ff0000000000L;

    // 例如 电池信息格式 zD-62-9876543210
    public void decodeEntryAt(int index, String value) {
        // 获取电池电量
        // 例如 value = zD-62-9876543210,得到 level = 0x62,即 98% 的电量
        out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
        long duration = 0;
        while (i < N && (c=value.charAt(i)) != '-') {
            i++;
            duration <<= 4;
            if (c >= '0' && c <= '9') {
                duration += c - '0';
            } else if (c >= 'a' && c <= 'f') {
                duration += c - 'a' + 10;
            } else if (c >= 'A' && c <= 'F') {
                duration += c - 'A' + 10;
            }
        }
        // zD-62-9876543210 对应 0x0203629876543210
        mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
    }
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值