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
diff --git a/api/current.txt b/api/current.txt
index 2274ea0..c718881 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -6464,6 +6464,7 @@
     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 @@
 
   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 @@
     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);
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java
index 1c02102..f444680 100644
--- a/core/java/android/app/ContextImpl.java
+++ b/core/java/android/app/ContextImpl.java
@@ -82,6 +82,7 @@
 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 @@
                     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);
diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java
index ed0cc23..decd59c 100644
--- a/core/java/android/content/Context.java
+++ b/core/java/android/content/Context.java
@@ -2009,6 +2009,7 @@
             CAMERA_SERVICE,
             PRINT_SERVICE,
             MEDIA_SESSION_SERVICE,
+            BATTERY_SERVICE,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface ServiceName {}
@@ -2060,6 +2061,8 @@
      * <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 @@
      * @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);
 
@@ -2481,6 +2486,14 @@
 
     /**
      * 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.
      *
      * @see #getSystemService
diff --git a/core/java/android/os/BatteryManager.java b/core/java/android/os/BatteryManager.java
index 2e38960..f339e52 100644
--- a/core/java/android/os/BatteryManager.java
+++ b/core/java/android/os/BatteryManager.java
@@ -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 @@
     /** @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;
+    }
 }
diff --git a/core/java/android/os/BatteryProperty.java b/core/java/android/os/BatteryProperty.java
index 76b0dc4..ec73952 100644
--- a/core/java/android/os/BatteryProperty.java
+++ b/core/java/android/os/BatteryProperty.java
@@ -19,22 +19,67 @@
 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 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