blob: 263750aa9946f3d2eded9d8770d0d48e8c869a67 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.os;
18
Sandeep Patila21c4272016-11-10 14:02:45 -080019import android.hardware.health.V1_0.Constants;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070020import com.android.internal.app.IBatteryStats;
Todd Poynore35872d2013-12-10 11:57:21 -080021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022/**
23 * The BatteryManager class contains strings and constants used for values
Todd Poynore35872d2013-12-10 11:57:21 -080024 * in the {@link android.content.Intent#ACTION_BATTERY_CHANGED} Intent, and
25 * provides a method for querying battery and charging properties.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026 */
27public class BatteryManager {
Dianne Hackbornedd93162009-09-19 14:03:05 -070028 /**
29 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
30 * integer containing the current status constant.
31 */
32 public static final String EXTRA_STATUS = "status";
33
34 /**
35 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
36 * integer containing the current health constant.
37 */
38 public static final String EXTRA_HEALTH = "health";
39
40 /**
41 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
42 * boolean indicating whether a battery is present.
43 */
44 public static final String EXTRA_PRESENT = "present";
45
46 /**
47 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
48 * integer field containing the current battery level, from 0 to
49 * {@link #EXTRA_SCALE}.
50 */
51 public static final String EXTRA_LEVEL = "level";
52
53 /**
54 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
55 * integer containing the maximum battery level.
56 */
57 public static final String EXTRA_SCALE = "scale";
58
59 /**
60 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
61 * integer containing the resource ID of a small status bar icon
62 * indicating the current battery state.
63 */
64 public static final String EXTRA_ICON_SMALL = "icon-small";
65
66 /**
67 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
68 * integer indicating whether the device is plugged in to a power
69 * source; 0 means it is on battery, other constants are different
70 * types of power sources.
71 */
72 public static final String EXTRA_PLUGGED = "plugged";
73
74 /**
75 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
76 * integer containing the current battery voltage level.
77 */
78 public static final String EXTRA_VOLTAGE = "voltage";
79
80 /**
81 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
82 * integer containing the current battery temperature.
83 */
84 public static final String EXTRA_TEMPERATURE = "temperature";
85
86 /**
87 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
88 * String describing the technology of the current battery.
89 */
90 public static final String EXTRA_TECHNOLOGY = "technology";
Mike Lockwooddeff9c82010-09-04 10:29:17 -040091
92 /**
93 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
Joe Onorato4ca7f1e2010-10-27 15:32:23 -070094 * Int value set to nonzero if an unsupported charger is attached
Mike Lockwooddeff9c82010-09-04 10:29:17 -040095 * to the device.
96 * {@hide}
97 */
98 public static final String EXTRA_INVALID_CHARGER = "invalid_charger";
99
Adrian Roos7b043112015-07-10 13:00:33 -0700100 /**
101 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
102 * Int value set to the maximum charging current supported by the charger in micro amperes.
103 * {@hide}
104 */
105 public static final String EXTRA_MAX_CHARGING_CURRENT = "max_charging_current";
106
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700107 /**
108 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
109 * Int value set to the maximum charging voltage supported by the charger in micro volts.
110 * {@hide}
111 */
112 public static final String EXTRA_MAX_CHARGING_VOLTAGE = "max_charging_voltage";
113
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700114 /**
115 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
116 * integer containing the charge counter present in the battery.
117 * {@hide}
118 */
119 public static final String EXTRA_CHARGE_COUNTER = "charge_counter";
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 // values for "status" field in the ACTION_BATTERY_CHANGED Intent
Sandeep Patila21c4272016-11-10 14:02:45 -0800122 public static final int BATTERY_STATUS_UNKNOWN = Constants.BATTERY_STATUS_UNKNOWN;
123 public static final int BATTERY_STATUS_CHARGING = Constants.BATTERY_STATUS_CHARGING;
124 public static final int BATTERY_STATUS_DISCHARGING = Constants.BATTERY_STATUS_DISCHARGING;
125 public static final int BATTERY_STATUS_NOT_CHARGING = Constants.BATTERY_STATUS_NOT_CHARGING;
126 public static final int BATTERY_STATUS_FULL = Constants.BATTERY_STATUS_FULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
128 // values for "health" field in the ACTION_BATTERY_CHANGED Intent
Sandeep Patila21c4272016-11-10 14:02:45 -0800129 public static final int BATTERY_HEALTH_UNKNOWN = Constants.BATTERY_HEALTH_UNKNOWN;
130 public static final int BATTERY_HEALTH_GOOD = Constants.BATTERY_HEALTH_GOOD;
131 public static final int BATTERY_HEALTH_OVERHEAT = Constants.BATTERY_HEALTH_OVERHEAT;
132 public static final int BATTERY_HEALTH_DEAD = Constants.BATTERY_HEALTH_DEAD;
133 public static final int BATTERY_HEALTH_OVER_VOLTAGE = Constants.BATTERY_HEALTH_OVER_VOLTAGE;
134 public static final int BATTERY_HEALTH_UNSPECIFIED_FAILURE = Constants.BATTERY_HEALTH_UNSPECIFIED_FAILURE;
135 public static final int BATTERY_HEALTH_COLD = Constants.BATTERY_HEALTH_COLD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
137 // values of the "plugged" field in the ACTION_BATTERY_CHANGED intent.
138 // These must be powers of 2.
139 /** Power source is an AC charger. */
140 public static final int BATTERY_PLUGGED_AC = 1;
141 /** Power source is a USB port. */
142 public static final int BATTERY_PLUGGED_USB = 2;
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700143 /** Power source is wireless. */
144 public static final int BATTERY_PLUGGED_WIRELESS = 4;
Jeff Browna4d82042012-10-02 19:11:19 -0700145
146 /** @hide */
147 public static final int BATTERY_PLUGGED_ANY =
148 BATTERY_PLUGGED_AC | BATTERY_PLUGGED_USB | BATTERY_PLUGGED_WIRELESS;
Todd Poynore35872d2013-12-10 11:57:21 -0800149
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700150 /**
151 * Sent when the device's battery has started charging (or has reached full charge
152 * and the device is on power). This is a good time to do work that you would like to
153 * avoid doing while on battery (that is to avoid draining the user's battery due to
154 * things they don't care enough about).
155 *
156 * This is paired with {@link #ACTION_DISCHARGING}. The current state can always
157 * be retrieved with {@link #isCharging()}.
158 */
159 public static final String ACTION_CHARGING = "android.os.action.CHARGING";
160
161 /**
162 * Sent when the device's battery may be discharging, so apps should avoid doing
163 * extraneous work that would cause it to discharge faster.
164 *
165 * This is paired with {@link #ACTION_CHARGING}. The current state can always
166 * be retrieved with {@link #isCharging()}.
167 */
168 public static final String ACTION_DISCHARGING = "android.os.action.DISCHARGING";
169
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700170 /*
171 * Battery property identifiers. These must match the values in
172 * frameworks/native/include/batteryservice/BatteryService.h
173 */
174 /** Battery capacity in microampere-hours, as an integer. */
175 public static final int BATTERY_PROPERTY_CHARGE_COUNTER = 1;
176
177 /**
178 * Instantaneous battery current in microamperes, as an integer. Positive
179 * values indicate net current entering the battery from a charge source,
180 * negative values indicate net current discharging from the battery.
181 */
182 public static final int BATTERY_PROPERTY_CURRENT_NOW = 2;
183
184 /**
185 * Average battery current in microamperes, as an integer. Positive
186 * values indicate net current entering the battery from a charge source,
187 * negative values indicate net current discharging from the battery.
188 * The time period over which the average is computed may depend on the
189 * fuel gauge hardware and its configuration.
190 */
191 public static final int BATTERY_PROPERTY_CURRENT_AVERAGE = 3;
192
193 /**
194 * Remaining battery capacity as an integer percentage of total capacity
195 * (with no fractional part).
196 */
197 public static final int BATTERY_PROPERTY_CAPACITY = 4;
198
199 /**
200 * Battery remaining energy in nanowatt-hours, as a long integer.
201 */
202 public static final int BATTERY_PROPERTY_ENERGY_COUNTER = 5;
203
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700204 private final IBatteryStats mBatteryStats;
Jeff Brown6e539312015-02-24 18:53:21 -0800205 private final IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar;
206
207 /**
208 * @removed Was previously made visible by accident.
209 */
210 public BatteryManager() {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700211 mBatteryStats = IBatteryStats.Stub.asInterface(
212 ServiceManager.getService(BatteryStats.SERVICE_NAME));
Jeff Brown6e539312015-02-24 18:53:21 -0800213 mBatteryPropertiesRegistrar = IBatteryPropertiesRegistrar.Stub.asInterface(
214 ServiceManager.getService("batteryproperties"));
215 }
Todd Poynore35872d2013-12-10 11:57:21 -0800216
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600217 /** {@hide} */
218 public BatteryManager(IBatteryStats batteryStats,
219 IBatteryPropertiesRegistrar batteryPropertiesRegistrar) {
220 mBatteryStats = batteryStats;
221 mBatteryPropertiesRegistrar = batteryPropertiesRegistrar;
222 }
223
Todd Poynore35872d2013-12-10 11:57:21 -0800224 /**
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700225 * Return true if the battery is currently considered to be charging. This means that
226 * the device is plugged in and is supplying sufficient power that the battery level is
227 * going up (or the battery is fully charged). Changes in this state are matched by
228 * broadcasts of {@link #ACTION_CHARGING} and {@link #ACTION_DISCHARGING}.
229 */
230 public boolean isCharging() {
231 try {
232 return mBatteryStats.isCharging();
233 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700234 throw e.rethrowFromSystemServer();
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700235 }
236 }
237
238 /**
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700239 * Query a battery property from the batteryproperties service.
Todd Poynore35872d2013-12-10 11:57:21 -0800240 *
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700241 * Returns the requested value, or Long.MIN_VALUE if property not
242 * supported on this system or on other error.
Todd Poynore35872d2013-12-10 11:57:21 -0800243 */
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700244 private long queryProperty(int id) {
245 long ret;
246
Todd Poynore35872d2013-12-10 11:57:21 -0800247 if (mBatteryPropertiesRegistrar == null) {
Jeff Brown6e539312015-02-24 18:53:21 -0800248 return Long.MIN_VALUE;
Todd Poynore35872d2013-12-10 11:57:21 -0800249 }
250
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700251 try {
252 BatteryProperty prop = new BatteryProperty();
253
254 if (mBatteryPropertiesRegistrar.getProperty(id, prop) == 0)
255 ret = prop.getLong();
256 else
257 ret = Long.MIN_VALUE;
258 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700259 throw e.rethrowFromSystemServer();
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700260 }
261
262 return ret;
263 }
264
265 /**
266 * Return the value of a battery property of integer type. If the
267 * platform does not provide the property queried, this value will
268 * be Integer.MIN_VALUE.
269 *
270 * @param id identifier of the requested property
271 *
272 * @return the property value, or Integer.MIN_VALUE if not supported.
273 */
274 public int getIntProperty(int id) {
275 return (int)queryProperty(id);
276 }
277
278 /**
279 * Return the value of a battery property of long type If the
280 * platform does not provide the property queried, this value will
281 * be Long.MIN_VALUE.
282 *
283 * @param id identifier of the requested property
284 *
285 * @return the property value, or Long.MIN_VALUE if not supported.
286 */
287 public long getLongProperty(int id) {
288 return queryProperty(id);
Todd Poynore35872d2013-12-10 11:57:21 -0800289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290}