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

Commit e35872da authored by Todd Poynor's avatar Todd Poynor
Browse files

BatteryManager: Add API and service for battery property retrieval

Add service "batterymanager" and method getProperty to retrieve
battery properties.  This is a public API.

Make BatteryProperty public.  Cleanups for public-facing API.

Change-Id: I3637d131aabe4811dff40661728d5353eaf854c4
parent 771cd657
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -6464,6 +6464,7 @@ package android.content {
    field public static final java.lang.String ALARM_SERVICE = "alarm";
    field public static final java.lang.String APP_OPS_SERVICE = "appops";
    field public static final java.lang.String AUDIO_SERVICE = "audio";
    field public static final java.lang.String BATTERY_SERVICE = "batterymanager";
    field public static final int BIND_ABOVE_CLIENT = 8; // 0x8
    field public static final int BIND_ADJUST_WITH_ACTIVITY = 128; // 0x80
    field public static final int BIND_ALLOW_OOM_MANAGEMENT = 16; // 0x10
@@ -18974,6 +18975,7 @@ package android.os {
  public class BatteryManager {
    ctor public BatteryManager();
    method public android.os.BatteryProperty getProperty(int) throws android.os.RemoteException;
    field public static final int BATTERY_HEALTH_COLD = 7; // 0x7
    field public static final int BATTERY_HEALTH_DEAD = 4; // 0x4
    field public static final int BATTERY_HEALTH_GOOD = 2; // 0x2
@@ -19001,6 +19003,18 @@ package android.os {
    field public static final java.lang.String EXTRA_VOLTAGE = "voltage";
  }
  public class BatteryProperty implements android.os.Parcelable {
    method public int describeContents();
    method public int getInt();
    method public void readFromParcel(android.os.Parcel);
    method public void writeToParcel(android.os.Parcel, int);
    field public static final int CAPACITY = 4; // 0x4
    field public static final int CHARGE_COUNTER = 1; // 0x1
    field public static final android.os.Parcelable.Creator CREATOR;
    field public static final int CURRENT_AVERAGE = 3; // 0x3
    field public static final int CURRENT_NOW = 2; // 0x2
  }
  public class Binder implements android.os.IBinder {
    ctor public Binder();
    method public void attachInterface(android.os.IInterface, java.lang.String);
+6 −0
Original line number Diff line number Diff line
@@ -82,6 +82,7 @@ import android.net.wifi.hotspot.WifiHotspotManager;
import android.net.wifi.p2p.IWifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager;
import android.nfc.NfcManager;
import android.os.BatteryManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.Debug;
@@ -404,6 +405,11 @@ class ContextImpl extends Context {
                    return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName());
                }});

        registerService(BATTERY_SERVICE, new ServiceFetcher() {
                public Object createService(ContextImpl ctx) {
                    return new BatteryManager();
                }});

        registerService(NFC_SERVICE, new ServiceFetcher() {
                public Object createService(ContextImpl ctx) {
                    return new NfcManager(ctx);
+13 −0
Original line number Diff line number Diff line
@@ -2009,6 +2009,7 @@ public abstract class Context {
            CAMERA_SERVICE,
            PRINT_SERVICE,
            MEDIA_SESSION_SERVICE,
            BATTERY_SERVICE,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface ServiceName {}
@@ -2060,6 +2061,8 @@ public abstract class Context {
     * <dd> An {@link android.app.UiModeManager} for controlling UI modes.
     * <dt> {@link #DOWNLOAD_SERVICE} ("download")
     * <dd> A {@link android.app.DownloadManager} for requesting HTTP downloads
     * <dt> {@link #BATTERY_SERVICE} ("batterymanager")
     * <dd> A {@link android.os.Battery} for managing battery state
     * </dl>
     *
     * <p>Note:  System services obtained via this API may be closely associated with
@@ -2113,6 +2116,8 @@ public abstract class Context {
     * @see android.app.UiModeManager
     * @see #DOWNLOAD_SERVICE
     * @see android.app.DownloadManager
     * @see #BATTERY_SERVICE
     * @see android.os.BatteryManager
     */
    public abstract Object getSystemService(@ServiceName @NonNull String name);

@@ -2479,6 +2484,14 @@ public abstract class Context {
     */
    public static final String DOWNLOAD_SERVICE = "download";

    /**
     * Use with {@link #getSystemService} to retrieve a
     * {@link android.os.BatteryManager} for managing battery state.
     *
     * @see #getSystemService
     */
    public static final String BATTERY_SERVICE = "batterymanager";

    /**
     * Use with {@link #getSystemService} to retrieve a
     * {@link android.nfc.NfcManager} for using NFC.
+33 −1
Original line number Diff line number Diff line
@@ -16,9 +16,15 @@

package android.os;

import android.os.BatteryProperty;
import android.os.IBatteryPropertiesRegistrar;
import android.os.RemoteException;
import android.os.ServiceManager;

/**
 * The BatteryManager class contains strings and constants used for values
 * in the {@link android.content.Intent#ACTION_BATTERY_CHANGED} Intent.
 * in the {@link android.content.Intent#ACTION_BATTERY_CHANGED} Intent, and
 * provides a method for querying battery and charging properties.
 */
public class BatteryManager {
    /**
@@ -121,4 +127,30 @@ public class BatteryManager {
    /** @hide */
    public static final int BATTERY_PLUGGED_ANY =
            BATTERY_PLUGGED_AC | BATTERY_PLUGGED_USB | BATTERY_PLUGGED_WIRELESS;

    private IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar;

    /**
     * Return the requested battery property.
     *
     * @param id identifier from {@link BatteryProperty} of the requested property
     * @return a {@link BatteryProperty} object that returns the property value, or null on error
     */
    public BatteryProperty getProperty(int id) throws RemoteException {
        if (mBatteryPropertiesRegistrar == null) {
            IBinder b = ServiceManager.getService("batteryproperties");
            mBatteryPropertiesRegistrar =
                IBatteryPropertiesRegistrar.Stub.asInterface(b);

            if (mBatteryPropertiesRegistrar == null)
                return null;
        }

        BatteryProperty prop = new BatteryProperty(Integer.MIN_VALUE);
        if ((mBatteryPropertiesRegistrar.getProperty(id, prop) == 0) &&
            (prop.getInt() != Integer.MIN_VALUE))
            return prop;
        else
            return null;
    }
}
+54 −9
Original line number Diff line number Diff line
@@ -19,22 +19,67 @@ import android.os.Parcel;
import android.os.Parcelable;

/**
 * {@hide}
 * Battery properties that may be queried using
 * {@link BatteryManager#getProperty
 * BatteryManager.getProperty()}
 */
public class BatteryProperty implements Parcelable {
    /*
     * Battery property identifiers.  These must match the values in
     * frameworks/native/include/batteryservice/BatteryService.h
     */
    public static final int BATTERY_PROP_CHARGE_COUNTER = 1;
    public static final int BATTERY_PROP_CURRENT_NOW = 2;
    public static final int BATTERY_PROP_CURRENT_AVG = 3;
    public static final int BATTERY_PROP_CAPACITY = 4;
    /** Battery capacity in microampere-hours, as an integer. */
    public static final int CHARGE_COUNTER = 1;

    public int valueInt;
    /**
     * Instantaneous battery current in microamperes, as an integer.  Positive
     * values indicate net current entering the battery from a charge source,
     * negative values indicate net current discharging from the battery.
     */
    public static final int CURRENT_NOW = 2;

    /**
     * Average battery current in microamperes, as an integer.  Positive
     * values indicate net current entering the battery from a charge source,
     * negative values indicate net current discharging from the battery.
     * The time period over which the average is computed may depend on the
     * fuel gauge hardware and its configuration.
     */
    public static final int CURRENT_AVERAGE = 3;

    /**
     * Remaining battery capacity as an integer percentage of total capacity
     * (with no fractional part).
     */
    public static final int CAPACITY = 4;

    private int mValueInt;

    /**
     * @hide
     */
    public BatteryProperty(int value) {
        mValueInt = value;
    }

    /**
     * @hide
     */
    public BatteryProperty() {
        valueInt = Integer.MIN_VALUE;
        mValueInt = Integer.MIN_VALUE;
    }

    /**
     * Return the value of a property of integer type previously queried
     * via {@link BatteryManager#getProperty
     * BatteryManager.getProperty()}.  If the platform does
     * not provide the property queried, this value will be
     * Integer.MIN_VALUE.
     *
     * @return The queried property value, or Integer.MIN_VALUE if not supported.
     */
    public int getInt() {
        return mValueInt;
    }

    /*
@@ -47,11 +92,11 @@ public class BatteryProperty implements Parcelable {
    }

    public void readFromParcel(Parcel p) {
        valueInt = p.readInt();
        mValueInt = p.readInt();
    }

    public void writeToParcel(Parcel p, int flags) {
        p.writeInt(valueInt);
        p.writeInt(mValueInt);
    }

    public static final Parcelable.Creator<BatteryProperty> CREATOR