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.content.Context;
20 import android.os.BatteryProperty;
21 import android.os.IBatteryPropertiesRegistrar;
22 import android.os.RemoteException;
23 import android.os.ServiceManager;
24 import com.android.internal.app.IBatteryStats;
25 
26 /**
27  * The BatteryManager class contains strings and constants used for values
28  * in the {@link android.content.Intent#ACTION_BATTERY_CHANGED} Intent, and
29  * provides a method for querying battery and charging properties.
30  */
31 public class BatteryManager {
32     /**
33      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
34      * integer containing the current status constant.
35      */
36     public static final String EXTRA_STATUS = "status";
37 
38     /**
39      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
40      * integer containing the current health constant.
41      */
42     public static final String EXTRA_HEALTH = "health";
43 
44     /**
45      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
46      * boolean indicating whether a battery is present.
47      */
48     public static final String EXTRA_PRESENT = "present";
49 
50     /**
51      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
52      * integer field containing the current battery level, from 0 to
53      * {@link #EXTRA_SCALE}.
54      */
55     public static final String EXTRA_LEVEL = "level";
56 
57     /**
58      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
59      * integer containing the maximum battery level.
60      */
61     public static final String EXTRA_SCALE = "scale";
62 
63     /**
64      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
65      * integer containing the resource ID of a small status bar icon
66      * indicating the current battery state.
67      */
68     public static final String EXTRA_ICON_SMALL = "icon-small";
69 
70     /**
71      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
72      * integer indicating whether the device is plugged in to a power
73      * source; 0 means it is on battery, other constants are different
74      * types of power sources.
75      */
76     public static final String EXTRA_PLUGGED = "plugged";
77 
78     /**
79      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
80      * integer containing the current battery voltage level.
81      */
82     public static final String EXTRA_VOLTAGE = "voltage";
83 
84     /**
85      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
86      * integer containing the current battery temperature.
87      */
88     public static final String EXTRA_TEMPERATURE = "temperature";
89 
90     /**
91      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
92      * String describing the technology of the current battery.
93      */
94     public static final String EXTRA_TECHNOLOGY = "technology";
95 
96     /**
97      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
98      * Int value set to nonzero if an unsupported charger is attached
99      * to the device.
100      * {@hide}
101      */
102     public static final String EXTRA_INVALID_CHARGER = "invalid_charger";
103 
104     /**
105      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
106      * Int value set to the maximum charging current supported by the charger in micro amperes.
107      * {@hide}
108      */
109     public static final String EXTRA_MAX_CHARGING_CURRENT = "max_charging_current";
110 
111     /**
112      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
113      * Int value set to the maximum charging voltage supported by the charger in micro volts.
114      * {@hide}
115      */
116     public static final String EXTRA_MAX_CHARGING_VOLTAGE = "max_charging_voltage";
117 
118     /**
119      * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
120      * integer containing the charge counter present in the battery.
121      * {@hide}
122      */
123      public static final String EXTRA_CHARGE_COUNTER = "charge_counter";
124 
125     // values for "status" field in the ACTION_BATTERY_CHANGED Intent
126     public static final int BATTERY_STATUS_UNKNOWN = 1;
127     public static final int BATTERY_STATUS_CHARGING = 2;
128     public static final int BATTERY_STATUS_DISCHARGING = 3;
129     public static final int BATTERY_STATUS_NOT_CHARGING = 4;
130     public static final int BATTERY_STATUS_FULL = 5;
131 
132     // values for "health" field in the ACTION_BATTERY_CHANGED Intent
133     public static final int BATTERY_HEALTH_UNKNOWN = 1;
134     public static final int BATTERY_HEALTH_GOOD = 2;
135     public static final int BATTERY_HEALTH_OVERHEAT = 3;
136     public static final int BATTERY_HEALTH_DEAD = 4;
137     public static final int BATTERY_HEALTH_OVER_VOLTAGE = 5;
138     public static final int BATTERY_HEALTH_UNSPECIFIED_FAILURE = 6;
139     public static final int BATTERY_HEALTH_COLD = 7;
140 
141     // values of the "plugged" field in the ACTION_BATTERY_CHANGED intent.
142     // These must be powers of 2.
143     /** Power source is an AC charger. */
144     public static final int BATTERY_PLUGGED_AC = 1;
145     /** Power source is a USB port. */
146     public static final int BATTERY_PLUGGED_USB = 2;
147     /** Power source is wireless. */
148     public static final int BATTERY_PLUGGED_WIRELESS = 4;
149 
150     /** @hide */
151     public static final int BATTERY_PLUGGED_ANY =
152             BATTERY_PLUGGED_AC | BATTERY_PLUGGED_USB | BATTERY_PLUGGED_WIRELESS;
153 
154     /**
155      * Sent when the device's battery has started charging (or has reached full charge
156      * and the device is on power).  This is a good time to do work that you would like to
157      * avoid doing while on battery (that is to avoid draining the user's battery due to
158      * things they don't care enough about).
159      *
160      * This is paired with {@link #ACTION_DISCHARGING}.  The current state can always
161      * be retrieved with {@link #isCharging()}.
162      */
163     public static final String ACTION_CHARGING = "android.os.action.CHARGING";
164 
165     /**
166      * Sent when the device's battery may be discharging, so apps should avoid doing
167      * extraneous work that would cause it to discharge faster.
168      *
169      * This is paired with {@link #ACTION_CHARGING}.  The current state can always
170      * be retrieved with {@link #isCharging()}.
171      */
172     public static final String ACTION_DISCHARGING = "android.os.action.DISCHARGING";
173 
174     /*
175      * Battery property identifiers.  These must match the values in
176      * frameworks/native/include/batteryservice/BatteryService.h
177      */
178     /** Battery capacity in microampere-hours, as an integer. */
179     public static final int BATTERY_PROPERTY_CHARGE_COUNTER = 1;
180 
181     /**
182      * Instantaneous battery current in microamperes, as an integer.  Positive
183      * values indicate net current entering the battery from a charge source,
184      * negative values indicate net current discharging from the battery.
185      */
186     public static final int BATTERY_PROPERTY_CURRENT_NOW = 2;
187 
188     /**
189      * Average battery current in microamperes, as an integer.  Positive
190      * values indicate net current entering the battery from a charge source,
191      * negative values indicate net current discharging from the battery.
192      * The time period over which the average is computed may depend on the
193      * fuel gauge hardware and its configuration.
194      */
195     public static final int BATTERY_PROPERTY_CURRENT_AVERAGE = 3;
196 
197     /**
198      * Remaining battery capacity as an integer percentage of total capacity
199      * (with no fractional part).
200      */
201     public static final int BATTERY_PROPERTY_CAPACITY = 4;
202 
203     /**
204      * Battery remaining energy in nanowatt-hours, as a long integer.
205      */
206     public static final int BATTERY_PROPERTY_ENERGY_COUNTER = 5;
207 
208     private final IBatteryStats mBatteryStats;
209     private final IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar;
210 
211     /**
212      * @removed Was previously made visible by accident.
213      */
BatteryManager()214     public BatteryManager() {
215         mBatteryStats = IBatteryStats.Stub.asInterface(
216                 ServiceManager.getService(BatteryStats.SERVICE_NAME));
217         mBatteryPropertiesRegistrar = IBatteryPropertiesRegistrar.Stub.asInterface(
218                 ServiceManager.getService("batteryproperties"));
219     }
220 
221     /**
222      * Return true if the battery is currently considered to be charging.  This means that
223      * the device is plugged in and is supplying sufficient power that the battery level is
224      * going up (or the battery is fully charged).  Changes in this state are matched by
225      * broadcasts of {@link #ACTION_CHARGING} and {@link #ACTION_DISCHARGING}.
226      */
isCharging()227     public boolean isCharging() {
228         try {
229             return mBatteryStats.isCharging();
230         } catch (RemoteException e) {
231             throw e.rethrowFromSystemServer();
232         }
233     }
234 
235     /**
236      * Query a battery property from the batteryproperties service.
237      *
238      * Returns the requested value, or Long.MIN_VALUE if property not
239      * supported on this system or on other error.
240      */
queryProperty(int id)241     private long queryProperty(int id) {
242         long ret;
243 
244         if (mBatteryPropertiesRegistrar == null) {
245             return Long.MIN_VALUE;
246         }
247 
248         try {
249             BatteryProperty prop = new BatteryProperty();
250 
251             if (mBatteryPropertiesRegistrar.getProperty(id, prop) == 0)
252                 ret = prop.getLong();
253             else
254                 ret = Long.MIN_VALUE;
255         } catch (RemoteException e) {
256             throw e.rethrowFromSystemServer();
257         }
258 
259         return ret;
260     }
261 
262     /**
263      * Return the value of a battery property of integer type.  If the
264      * platform does not provide the property queried, this value will
265      * be Integer.MIN_VALUE.
266      *
267      * @param id identifier of the requested property
268      *
269      * @return the property value, or Integer.MIN_VALUE if not supported.
270      */
getIntProperty(int id)271     public int getIntProperty(int id) {
272         return (int)queryProperty(id);
273     }
274 
275     /**
276      * Return the value of a battery property of long type If the
277      * platform does not provide the property queried, this value will
278      * be Long.MIN_VALUE.
279      *
280      * @param id identifier of the requested property
281      *
282      * @return the property value, or Long.MIN_VALUE if not supported.
283      */
getLongProperty(int id)284     public long getLongProperty(int id) {
285         return queryProperty(id);
286     }
287 }
288