Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9614aa9c authored by Jack Wu's avatar Jack Wu Committed by Gerrit Code Review
Browse files

Merge changes from topic "batteryStateOfHealth"

* changes:
  BatteryManager: stateOfHealth is a property
  BatteryManager: Add new battery intent and property
parents 1d09d644 f25f8106
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -30910,6 +30910,8 @@ package android.os {
    field public static final int BATTERY_STATUS_NOT_CHARGING = 4; // 0x4
    field public static final int BATTERY_STATUS_UNKNOWN = 1; // 0x1
    field public static final String EXTRA_BATTERY_LOW = "battery_low";
    field public static final String EXTRA_CHARGING_STATUS = "android.os.extra.CHARGING_STATUS";
    field public static final String EXTRA_CYCLE_COUNT = "android.os.extra.CYCLE_COUNT";
    field public static final String EXTRA_HEALTH = "health";
    field public static final String EXTRA_ICON_SMALL = "icon-small";
    field public static final String EXTRA_LEVEL = "level";
+8 −0
Original line number Diff line number Diff line
@@ -9152,6 +9152,14 @@ package android.os {
  public class BatteryManager {
    method @RequiresPermission(android.Manifest.permission.POWER_SAVER) public boolean setChargingStateUpdateDelayMillis(int);
    field @RequiresPermission(android.Manifest.permission.BATTERY_STATS) public static final int BATTERY_PROPERTY_CHARGING_POLICY = 9; // 0x9
    field @RequiresPermission(android.Manifest.permission.BATTERY_STATS) public static final int BATTERY_PROPERTY_FIRST_USAGE_DATE = 8; // 0x8
    field @RequiresPermission(android.Manifest.permission.BATTERY_STATS) public static final int BATTERY_PROPERTY_MANUFACTURING_DATE = 7; // 0x7
    field @RequiresPermission(android.Manifest.permission.BATTERY_STATS) public static final int BATTERY_PROPERTY_STATE_OF_HEALTH = 10; // 0xa
    field public static final int CHARGING_POLICY_ADAPTIVE_AC = 3; // 0x3
    field public static final int CHARGING_POLICY_ADAPTIVE_AON = 2; // 0x2
    field public static final int CHARGING_POLICY_ADAPTIVE_LONGLIFE = 4; // 0x4
    field public static final int CHARGING_POLICY_DEFAULT = 1; // 0x1
    field public static final String EXTRA_EVENTS = "android.os.extra.EVENTS";
    field public static final String EXTRA_EVENT_TIMESTAMP = "android.os.extra.EVENT_TIMESTAMP";
  }
+111 −1
Original line number Diff line number Diff line
@@ -146,6 +146,18 @@ public class BatteryManager {
     */
    public static final String EXTRA_SEQUENCE = "seq";

    /**
     * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     * Int value representing the battery charging cycle count.
     */
    public static final String EXTRA_CYCLE_COUNT = "android.os.extra.CYCLE_COUNT";

    /**
     * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     * Int value representing the battery charging status.
     */
    public static final String EXTRA_CHARGING_STATUS = "android.os.extra.CHARGING_STATUS";

    /**
     * Extra for {@link android.content.Intent#ACTION_BATTERY_LEVEL_CHANGED}:
     * Contains list of Bundles representing battery events
@@ -190,6 +202,35 @@ public class BatteryManager {
    /** Power source is dock. */
    public static final int BATTERY_PLUGGED_DOCK = OsProtoEnums.BATTERY_PLUGGED_DOCK; // = 8

    // values for "charge policy" property
    /**
     * Default policy (e.g. normal).
     * @hide
     */
    @SystemApi
    public static final int CHARGING_POLICY_DEFAULT = OsProtoEnums.CHARGING_POLICY_DEFAULT; // = 1
    /**
     * Optimized for battery health using static thresholds (e.g stop at 80%).
     * @hide
     */
    @SystemApi
    public static final int CHARGING_POLICY_ADAPTIVE_AON =
                                            OsProtoEnums.CHARGING_POLICY_ADAPTIVE_AON; // = 2
    /**
     * Optimized for battery health using adaptive thresholds.
     * @hide
     */
    @SystemApi
    public static final int CHARGING_POLICY_ADAPTIVE_AC =
                                            OsProtoEnums.CHARGING_POLICY_ADAPTIVE_AC; // = 3
    /**
     * Optimized for battery health, devices always connected to power.
     * @hide
     */
    @SystemApi
    public static final int CHARGING_POLICY_ADAPTIVE_LONGLIFE =
                                            OsProtoEnums.CHARGING_POLICY_ADAPTIVE_LONGLIFE; // = 4

    /** @hide */
    public static final int BATTERY_PLUGGED_ANY =
            BATTERY_PLUGGED_AC | BATTERY_PLUGGED_USB | BATTERY_PLUGGED_WIRELESS
@@ -254,6 +295,76 @@ public class BatteryManager {
     */
    public static final int BATTERY_PROPERTY_STATUS = 6;

    /**
     * Battery manufacturing date is reported in epoch. The 0 timepoint
     * begins at midnight Coordinated Universal Time (UTC) on January 1, 1970.
     * It is a long integer in seconds.
     *
     * <p class="note">
     * The sender must hold the {@link android.Manifest.permission#BATTERY_STATS} permission.
     *
     * Example: <code>
     *  // The value returned from the API can be used to create a Date, used
     *  // to set the time on a calendar and coverted to a string.
     *  import java.util.Date;
     *
     *  mBatteryManager = mContext.getSystemService(BatteryManager.class);
     *  final long manufacturingDate =
     *      mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_MANUFACTURING_DATE);
     *  Date date = new Date(manufacturingDate);
     *  Calendar calendar = Calendar.getInstance();
     *  calendar.setTime(date);
     * // Convert to yyyy-MM-dd HH:mm:ss format string
     *  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     *  String dateString = sdf.format(date);
     * </code>
     * @hide
     */
    @RequiresPermission(permission.BATTERY_STATS)
    @SystemApi
    public static final int BATTERY_PROPERTY_MANUFACTURING_DATE = 7;

    /**
     * The date of first usage is reported in epoch. The 0 timepoint
     * begins at midnight Coordinated Universal Time (UTC) on January 1, 1970.
     * It is a long integer in seconds.
     *
     * <p class="note">
     * The sender must hold the {@link android.Manifest.permission#BATTERY_STATS} permission.
     *
     * {@link BATTERY_PROPERTY_MANUFACTURING_DATE for sample code}
     * @hide
     */
    @RequiresPermission(permission.BATTERY_STATS)
    @SystemApi
    public static final int BATTERY_PROPERTY_FIRST_USAGE_DATE = 8;

    /**
     * Battery charging policy from a CHARGING_POLICY_* value..
     *
     * <p class="note">
     * The sender must hold the {@link android.Manifest.permission#BATTERY_STATS} permission.
     *
     * @hide
     */
    @RequiresPermission(permission.BATTERY_STATS)
    @SystemApi
    public static final int BATTERY_PROPERTY_CHARGING_POLICY = 9;

    /**
     *
     * Percentage representing the measured battery state of health (remaining
     * estimated full charge capacity relative to the rated capacity in %).
     *
     * <p class="note">
     * The sender must hold the {@link android.Manifest.permission#BATTERY_STATS} permission.
     *
     * @hide
     */
    @RequiresPermission(permission.BATTERY_STATS)
    @SystemApi
    public static final int BATTERY_PROPERTY_STATE_OF_HEALTH = 10;

    private final Context mContext;
    private final IBatteryStats mBatteryStats;
    private final IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar;
@@ -307,7 +418,6 @@ public class BatteryManager {

        try {
            BatteryProperty prop = new BatteryProperty();

            if (mBatteryPropertiesRegistrar.getProperty(id, prop) == 0)
                ret = prop.getLong();
            else
+22 −3
Original line number Diff line number Diff line
@@ -149,6 +149,8 @@ public final class BatteryService extends SystemService {
    private int mLastMaxChargingCurrent;
    private int mLastMaxChargingVoltage;
    private int mLastChargeCounter;
    private int mLastBatteryCycleCount;
    private int mLastCharingState;

    private int mSequence = 1;

@@ -503,7 +505,9 @@ public final class BatteryService extends SystemService {
                        || mHealthInfo.maxChargingCurrentMicroamps != mLastMaxChargingCurrent
                        || mHealthInfo.maxChargingVoltageMicrovolts != mLastMaxChargingVoltage
                        || mHealthInfo.batteryChargeCounterUah != mLastChargeCounter
                        || mInvalidCharger != mLastInvalidCharger)) {
                        || mInvalidCharger != mLastInvalidCharger
                        || mHealthInfo.batteryCycleCount != mLastBatteryCycleCount
                        || mHealthInfo.chargingState != mLastCharingState)) {

            if (mPlugType != mLastPlugType) {
                if (mLastPlugType == BATTERY_PLUGGED_NONE) {
@@ -677,6 +681,8 @@ public final class BatteryService extends SystemService {
            mLastChargeCounter = mHealthInfo.batteryChargeCounterUah;
            mLastBatteryLevelCritical = mBatteryLevelCritical;
            mLastInvalidCharger = mInvalidCharger;
            mLastBatteryCycleCount = mHealthInfo.batteryCycleCount;
            mLastCharingState = mHealthInfo.chargingState;
        }
    }

@@ -708,6 +714,8 @@ public final class BatteryService extends SystemService {
                BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE,
                mHealthInfo.maxChargingVoltageMicrovolts);
        intent.putExtra(BatteryManager.EXTRA_CHARGE_COUNTER, mHealthInfo.batteryChargeCounterUah);
        intent.putExtra(BatteryManager.EXTRA_CYCLE_COUNT, mHealthInfo.batteryCycleCount);
        intent.putExtra(BatteryManager.EXTRA_CHARGING_STATUS, mHealthInfo.chargingState);
        if (DEBUG) {
            Slog.d(TAG, "Sending ACTION_BATTERY_CHANGED. scale:" + BATTERY_SCALE
                    + ", info:" + mHealthInfo.toString());
@@ -731,6 +739,8 @@ public final class BatteryService extends SystemService {
        event.putInt(BatteryManager.EXTRA_TEMPERATURE, mHealthInfo.batteryTemperatureTenthsCelsius);
        event.putInt(BatteryManager.EXTRA_CHARGE_COUNTER, mHealthInfo.batteryChargeCounterUah);
        event.putLong(BatteryManager.EXTRA_EVENT_TIMESTAMP, now);
        event.putInt(BatteryManager.EXTRA_CYCLE_COUNT, mHealthInfo.batteryCycleCount);
        event.putInt(BatteryManager.EXTRA_CHARGING_STATUS, mHealthInfo.chargingState);

        boolean queueWasEmpty = mBatteryLevelsEventQueue.isEmpty();
        mBatteryLevelsEventQueue.add(event);
@@ -1241,11 +1251,20 @@ public final class BatteryService extends SystemService {
        }
    }

    // Reduced IBatteryPropertiesRegistrar that only implements getProperty for usage
    // in BatteryManager.
    // Reduced IBatteryPropertiesRegistrar that implements getProperty for usage
    // in BatteryManager and enforce permissions.
    private final class BatteryPropertiesRegistrar extends IBatteryPropertiesRegistrar.Stub {
        @Override
        public int getProperty(int id, final BatteryProperty prop) throws RemoteException {
            switch (id) {
                case BatteryManager.BATTERY_PROPERTY_MANUFACTURING_DATE:
                case BatteryManager.BATTERY_PROPERTY_FIRST_USAGE_DATE:
                case BatteryManager.BATTERY_PROPERTY_CHARGING_POLICY:
                case BatteryManager.BATTERY_PROPERTY_STATE_OF_HEALTH:
                    mContext.enforceCallingPermission(
                            android.Manifest.permission.BATTERY_STATS, null);
                    break;
            }
            return mHealthServiceWrapper.getProperty(id, prop);
        }
        @Override
+17 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.health;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.hardware.health.BatteryHealthData;
import android.hardware.health.HealthInfo;
import android.hardware.health.IHealth;
import android.os.BatteryManager;
@@ -113,6 +114,7 @@ class HealthServiceWrapperAidl extends HealthServiceWrapper {
    private int getPropertyInternal(int id, BatteryProperty prop) throws RemoteException {
        IHealth service = mLastService.get();
        if (service == null) throw new RemoteException("no health service");
        BatteryHealthData healthData;
        try {
            switch (id) {
                case BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER:
@@ -133,6 +135,21 @@ class HealthServiceWrapperAidl extends HealthServiceWrapper {
                case BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER:
                    prop.setLong(service.getEnergyCounterNwh());
                    break;
                case BatteryManager.BATTERY_PROPERTY_MANUFACTURING_DATE:
                    healthData = service.getBatteryHealthData();
                    prop.setLong(healthData.batteryManufacturingDateSeconds);
                    break;
                case BatteryManager.BATTERY_PROPERTY_FIRST_USAGE_DATE:
                    healthData = service.getBatteryHealthData();
                    prop.setLong(healthData.batteryFirstUsageSeconds);
                    break;
                case BatteryManager.BATTERY_PROPERTY_CHARGING_POLICY:
                    prop.setLong(service.getChargingPolicy());
                    break;
                case BatteryManager.BATTERY_PROPERTY_STATE_OF_HEALTH:
                    healthData = service.getBatteryHealthData();
                    prop.setLong(healthData.batteryStateOfHealth);
                    break;
            }
        } catch (UnsupportedOperationException e) {
            // Leave prop untouched.