Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.os;
     18 
     19 import android.os.BatteryProperty;
     20 import android.os.IBatteryPropertiesRegistrar;
     21 import android.os.RemoteException;
     22 import android.os.ServiceManager;
     23 
     24 /**
     25  * The BatteryManager class contains strings and constants used for values
     26  * in the {@link android.content.Intent#ACTION_BATTERY_CHANGED} Intent, and
     27  * provides a method for querying battery and charging properties.
     28  */
     29 public class BatteryManager {
     30     /**
     31      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     32      * integer containing the current status constant.
     33      */
     34     public static final String EXTRA_STATUS = "status";
     35 
     36     /**
     37      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     38      * integer containing the current health constant.
     39      */
     40     public static final String EXTRA_HEALTH = "health";
     41 
     42     /**
     43      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     44      * boolean indicating whether a battery is present.
     45      */
     46     public static final String EXTRA_PRESENT = "present";
     47 
     48     /**
     49      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     50      * integer field containing the current battery level, from 0 to
     51      * {@link #EXTRA_SCALE}.
     52      */
     53     public static final String EXTRA_LEVEL = "level";
     54 
     55     /**
     56      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     57      * integer containing the maximum battery level.
     58      */
     59     public static final String EXTRA_SCALE = "scale";
     60 
     61     /**
     62      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     63      * integer containing the resource ID of a small status bar icon
     64      * indicating the current battery state.
     65      */
     66     public static final String EXTRA_ICON_SMALL = "icon-small";
     67 
     68     /**
     69      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     70      * integer indicating whether the device is plugged in to a power
     71      * source; 0 means it is on battery, other constants are different
     72      * types of power sources.
     73      */
     74     public static final String EXTRA_PLUGGED = "plugged";
     75 
     76     /**
     77      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     78      * integer containing the current battery voltage level.
     79      */
     80     public static final String EXTRA_VOLTAGE = "voltage";
     81 
     82     /**
     83      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     84      * integer containing the current battery temperature.
     85      */
     86     public static final String EXTRA_TEMPERATURE = "temperature";
     87 
     88     /**
     89      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     90      * String describing the technology of the current battery.
     91      */
     92     public static final String EXTRA_TECHNOLOGY = "technology";
     93 
     94     /**
     95      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
     96      * Int value set to nonzero if an unsupported charger is attached
     97      * to the device.
     98      * {@hide}
     99      */
    100     public static final String EXTRA_INVALID_CHARGER = "invalid_charger";
    101 
    102     // values for "status" field in the ACTION_BATTERY_CHANGED Intent
    103     public static final int BATTERY_STATUS_UNKNOWN = 1;
    104     public static final int BATTERY_STATUS_CHARGING = 2;
    105     public static final int BATTERY_STATUS_DISCHARGING = 3;
    106     public static final int BATTERY_STATUS_NOT_CHARGING = 4;
    107     public static final int BATTERY_STATUS_FULL = 5;
    108 
    109     // values for "health" field in the ACTION_BATTERY_CHANGED Intent
    110     public static final int BATTERY_HEALTH_UNKNOWN = 1;
    111     public static final int BATTERY_HEALTH_GOOD = 2;
    112     public static final int BATTERY_HEALTH_OVERHEAT = 3;
    113     public static final int BATTERY_HEALTH_DEAD = 4;
    114     public static final int BATTERY_HEALTH_OVER_VOLTAGE = 5;
    115     public static final int BATTERY_HEALTH_UNSPECIFIED_FAILURE = 6;
    116     public static final int BATTERY_HEALTH_COLD = 7;
    117 
    118     // values of the "plugged" field in the ACTION_BATTERY_CHANGED intent.
    119     // These must be powers of 2.
    120     /** Power source is an AC charger. */
    121     public static final int BATTERY_PLUGGED_AC = 1;
    122     /** Power source is a USB port. */
    123     public static final int BATTERY_PLUGGED_USB = 2;
    124     /** Power source is wireless. */
    125     public static final int BATTERY_PLUGGED_WIRELESS = 4;
    126 
    127     /** @hide */
    128     public static final int BATTERY_PLUGGED_ANY =
    129             BATTERY_PLUGGED_AC | BATTERY_PLUGGED_USB | BATTERY_PLUGGED_WIRELESS;
    130 
    131     /*
    132      * Battery property identifiers.  These must match the values in
    133      * frameworks/native/include/batteryservice/BatteryService.h
    134      */
    135     /** Battery capacity in microampere-hours, as an integer. */
    136     public static final int BATTERY_PROPERTY_CHARGE_COUNTER = 1;
    137 
    138     /**
    139      * Instantaneous battery current in microamperes, as an integer.  Positive
    140      * values indicate net current entering the battery from a charge source,
    141      * negative values indicate net current discharging from the battery.
    142      */
    143     public static final int BATTERY_PROPERTY_CURRENT_NOW = 2;
    144 
    145     /**
    146      * Average battery current in microamperes, as an integer.  Positive
    147      * values indicate net current entering the battery from a charge source,
    148      * negative values indicate net current discharging from the battery.
    149      * The time period over which the average is computed may depend on the
    150      * fuel gauge hardware and its configuration.
    151      */
    152     public static final int BATTERY_PROPERTY_CURRENT_AVERAGE = 3;
    153 
    154     /**
    155      * Remaining battery capacity as an integer percentage of total capacity
    156      * (with no fractional part).
    157      */
    158     public static final int BATTERY_PROPERTY_CAPACITY = 4;
    159 
    160     /**
    161      * Battery remaining energy in nanowatt-hours, as a long integer.
    162      */
    163     public static final int BATTERY_PROPERTY_ENERGY_COUNTER = 5;
    164 
    165     private IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar;
    166 
    167     /**
    168      * Query a battery property from the batteryproperties service.
    169      *
    170      * Returns the requested value, or Long.MIN_VALUE if property not
    171      * supported on this system or on other error.
    172      */
    173     private long queryProperty(int id) {
    174         long ret;
    175 
    176         if (mBatteryPropertiesRegistrar == null) {
    177             IBinder b = ServiceManager.getService("batteryproperties");
    178             mBatteryPropertiesRegistrar =
    179                 IBatteryPropertiesRegistrar.Stub.asInterface(b);
    180 
    181             if (mBatteryPropertiesRegistrar == null)
    182                 return Long.MIN_VALUE;
    183         }
    184 
    185         try {
    186             BatteryProperty prop = new BatteryProperty();
    187 
    188             if (mBatteryPropertiesRegistrar.getProperty(id, prop) == 0)
    189                 ret = prop.getLong();
    190             else
    191                 ret = Long.MIN_VALUE;
    192         } catch (RemoteException e) {
    193             ret = Long.MIN_VALUE;
    194         }
    195 
    196         return ret;
    197     }
    198 
    199     /**
    200      * Return the value of a battery property of integer type.  If the
    201      * platform does not provide the property queried, this value will
    202      * be Integer.MIN_VALUE.
    203      *
    204      * @param id identifier of the requested property
    205      *
    206      * @return the property value, or Integer.MIN_VALUE if not supported.
    207      */
    208     public int getIntProperty(int id) {
    209         return (int)queryProperty(id);
    210     }
    211 
    212     /**
    213      * Return the value of a battery property of long type If the
    214      * platform does not provide the property queried, this value will
    215      * be Long.MIN_VALUE.
    216      *
    217      * @param id identifier of the requested property
    218      *
    219      * @return the property value, or Long.MIN_VALUE if not supported.
    220      */
    221     public long getLongProperty(int id) {
    222         return queryProperty(id);
    223     }
    224 }
    225