blob: a734719afa5dda4515894d9c9346de9bc2c7e0bb [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
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060019import android.annotation.SystemService;
20import android.content.Context;
Dianne Hackborn74ed6ec2017-11-21 16:33:44 -080021import android.content.Intent;
Sandeep Patila21c4272016-11-10 14:02:45 -080022import android.hardware.health.V1_0.Constants;
Yifan Hongb0f13522017-10-31 15:48:50 -070023
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070024import com.android.internal.app.IBatteryStats;
Todd Poynore35872d2013-12-10 11:57:21 -080025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026/**
27 * The BatteryManager class contains strings and constants used for values
Todd Poynore35872d2013-12-10 11:57:21 -080028 * in the {@link android.content.Intent#ACTION_BATTERY_CHANGED} Intent, and
29 * provides a method for querying battery and charging properties.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060031@SystemService(Context.BATTERY_SERVICE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032public class BatteryManager {
Dianne Hackbornedd93162009-09-19 14:03:05 -070033 /**
34 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
35 * integer containing the current status constant.
36 */
37 public static final String EXTRA_STATUS = "status";
Yifan Hongb0f13522017-10-31 15:48:50 -070038
Dianne Hackbornedd93162009-09-19 14:03:05 -070039 /**
40 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
41 * integer containing the current health constant.
42 */
43 public static final String EXTRA_HEALTH = "health";
Yifan Hongb0f13522017-10-31 15:48:50 -070044
Dianne Hackbornedd93162009-09-19 14:03:05 -070045 /**
46 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
47 * boolean indicating whether a battery is present.
48 */
49 public static final String EXTRA_PRESENT = "present";
Yifan Hongb0f13522017-10-31 15:48:50 -070050
Dianne Hackbornedd93162009-09-19 14:03:05 -070051 /**
52 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
53 * integer field containing the current battery level, from 0 to
54 * {@link #EXTRA_SCALE}.
55 */
56 public static final String EXTRA_LEVEL = "level";
Yifan Hongb0f13522017-10-31 15:48:50 -070057
Dianne Hackbornedd93162009-09-19 14:03:05 -070058 /**
59 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
Dianne Hackborn74ed6ec2017-11-21 16:33:44 -080060 * Boolean field indicating whether the battery is currently considered to be
61 * low, that is whether a {@link Intent#ACTION_BATTERY_LOW} broadcast
62 * has been sent.
63 */
64 public static final String EXTRA_BATTERY_LOW = "battery_low";
65
66 /**
67 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
Dianne Hackbornedd93162009-09-19 14:03:05 -070068 * integer containing the maximum battery level.
69 */
70 public static final String EXTRA_SCALE = "scale";
Yifan Hongb0f13522017-10-31 15:48:50 -070071
Dianne Hackbornedd93162009-09-19 14:03:05 -070072 /**
73 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
74 * integer containing the resource ID of a small status bar icon
75 * indicating the current battery state.
76 */
77 public static final String EXTRA_ICON_SMALL = "icon-small";
Yifan Hongb0f13522017-10-31 15:48:50 -070078
Dianne Hackbornedd93162009-09-19 14:03:05 -070079 /**
80 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
81 * integer indicating whether the device is plugged in to a power
82 * source; 0 means it is on battery, other constants are different
83 * types of power sources.
84 */
85 public static final String EXTRA_PLUGGED = "plugged";
Yifan Hongb0f13522017-10-31 15:48:50 -070086
Dianne Hackbornedd93162009-09-19 14:03:05 -070087 /**
88 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
89 * integer containing the current battery voltage level.
90 */
91 public static final String EXTRA_VOLTAGE = "voltage";
Yifan Hongb0f13522017-10-31 15:48:50 -070092
Dianne Hackbornedd93162009-09-19 14:03:05 -070093 /**
94 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
95 * integer containing the current battery temperature.
96 */
97 public static final String EXTRA_TEMPERATURE = "temperature";
Yifan Hongb0f13522017-10-31 15:48:50 -070098
Dianne Hackbornedd93162009-09-19 14:03:05 -070099 /**
100 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
101 * String describing the technology of the current battery.
102 */
103 public static final String EXTRA_TECHNOLOGY = "technology";
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400104
105 /**
106 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
Joe Onorato4ca7f1e2010-10-27 15:32:23 -0700107 * Int value set to nonzero if an unsupported charger is attached
Mike Lockwooddeff9c82010-09-04 10:29:17 -0400108 * to the device.
109 * {@hide}
110 */
111 public static final String EXTRA_INVALID_CHARGER = "invalid_charger";
112
Adrian Roos7b043112015-07-10 13:00:33 -0700113 /**
114 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
115 * Int value set to the maximum charging current supported by the charger in micro amperes.
116 * {@hide}
117 */
118 public static final String EXTRA_MAX_CHARGING_CURRENT = "max_charging_current";
119
Badhri Jagan Sridharanf92fcfe2015-10-27 13:59:34 -0700120 /**
121 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
122 * Int value set to the maximum charging voltage supported by the charger in micro volts.
123 * {@hide}
124 */
125 public static final String EXTRA_MAX_CHARGING_VOLTAGE = "max_charging_voltage";
126
Ruchi Kandoi6361e222016-04-07 11:28:30 -0700127 /**
128 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
129 * integer containing the charge counter present in the battery.
130 * {@hide}
131 */
132 public static final String EXTRA_CHARGE_COUNTER = "charge_counter";
133
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800134 /**
135 * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
136 * Current int sequence number of the update.
137 * {@hide}
138 */
139 public static final String EXTRA_SEQUENCE = "seq";
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 // values for "status" field in the ACTION_BATTERY_CHANGED Intent
Sandeep Patila21c4272016-11-10 14:02:45 -0800142 public static final int BATTERY_STATUS_UNKNOWN = Constants.BATTERY_STATUS_UNKNOWN;
143 public static final int BATTERY_STATUS_CHARGING = Constants.BATTERY_STATUS_CHARGING;
144 public static final int BATTERY_STATUS_DISCHARGING = Constants.BATTERY_STATUS_DISCHARGING;
145 public static final int BATTERY_STATUS_NOT_CHARGING = Constants.BATTERY_STATUS_NOT_CHARGING;
146 public static final int BATTERY_STATUS_FULL = Constants.BATTERY_STATUS_FULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
148 // values for "health" field in the ACTION_BATTERY_CHANGED Intent
Sandeep Patila21c4272016-11-10 14:02:45 -0800149 public static final int BATTERY_HEALTH_UNKNOWN = Constants.BATTERY_HEALTH_UNKNOWN;
150 public static final int BATTERY_HEALTH_GOOD = Constants.BATTERY_HEALTH_GOOD;
151 public static final int BATTERY_HEALTH_OVERHEAT = Constants.BATTERY_HEALTH_OVERHEAT;
152 public static final int BATTERY_HEALTH_DEAD = Constants.BATTERY_HEALTH_DEAD;
153 public static final int BATTERY_HEALTH_OVER_VOLTAGE = Constants.BATTERY_HEALTH_OVER_VOLTAGE;
154 public static final int BATTERY_HEALTH_UNSPECIFIED_FAILURE = Constants.BATTERY_HEALTH_UNSPECIFIED_FAILURE;
155 public static final int BATTERY_HEALTH_COLD = Constants.BATTERY_HEALTH_COLD;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
157 // values of the "plugged" field in the ACTION_BATTERY_CHANGED intent.
158 // These must be powers of 2.
159 /** Power source is an AC charger. */
Bookatz1a1b0462018-01-12 11:47:03 -0800160 public static final int BATTERY_PLUGGED_AC = OsProtoEnums.BATTERY_PLUGGED_AC; // = 1
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 /** Power source is a USB port. */
Bookatz1a1b0462018-01-12 11:47:03 -0800162 public static final int BATTERY_PLUGGED_USB = OsProtoEnums.BATTERY_PLUGGED_USB; // = 2
Brian Muramatsu37a37f42012-08-14 15:21:02 -0700163 /** Power source is wireless. */
Bookatz1a1b0462018-01-12 11:47:03 -0800164 public static final int BATTERY_PLUGGED_WIRELESS = OsProtoEnums.BATTERY_PLUGGED_WIRELESS; // = 4
Jeff Browna4d82042012-10-02 19:11:19 -0700165
166 /** @hide */
167 public static final int BATTERY_PLUGGED_ANY =
168 BATTERY_PLUGGED_AC | BATTERY_PLUGGED_USB | BATTERY_PLUGGED_WIRELESS;
Todd Poynore35872d2013-12-10 11:57:21 -0800169
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700170 /**
171 * Sent when the device's battery has started charging (or has reached full charge
172 * and the device is on power). This is a good time to do work that you would like to
173 * avoid doing while on battery (that is to avoid draining the user's battery due to
174 * things they don't care enough about).
175 *
176 * This is paired with {@link #ACTION_DISCHARGING}. The current state can always
177 * be retrieved with {@link #isCharging()}.
178 */
179 public static final String ACTION_CHARGING = "android.os.action.CHARGING";
180
181 /**
182 * Sent when the device's battery may be discharging, so apps should avoid doing
183 * extraneous work that would cause it to discharge faster.
184 *
185 * This is paired with {@link #ACTION_CHARGING}. The current state can always
186 * be retrieved with {@link #isCharging()}.
187 */
188 public static final String ACTION_DISCHARGING = "android.os.action.DISCHARGING";
189
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700190 /*
191 * Battery property identifiers. These must match the values in
192 * frameworks/native/include/batteryservice/BatteryService.h
193 */
194 /** Battery capacity in microampere-hours, as an integer. */
195 public static final int BATTERY_PROPERTY_CHARGE_COUNTER = 1;
196
197 /**
198 * Instantaneous battery current in microamperes, as an integer. Positive
199 * values indicate net current entering the battery from a charge source,
200 * negative values indicate net current discharging from the battery.
201 */
202 public static final int BATTERY_PROPERTY_CURRENT_NOW = 2;
203
204 /**
205 * Average battery current in microamperes, as an integer. Positive
206 * values indicate net current entering the battery from a charge source,
207 * negative values indicate net current discharging from the battery.
208 * The time period over which the average is computed may depend on the
209 * fuel gauge hardware and its configuration.
210 */
211 public static final int BATTERY_PROPERTY_CURRENT_AVERAGE = 3;
212
213 /**
214 * Remaining battery capacity as an integer percentage of total capacity
215 * (with no fractional part).
216 */
217 public static final int BATTERY_PROPERTY_CAPACITY = 4;
218
219 /**
220 * Battery remaining energy in nanowatt-hours, as a long integer.
221 */
222 public static final int BATTERY_PROPERTY_ENERGY_COUNTER = 5;
223
Jin Qianedf0a372017-02-03 11:08:37 -0800224 /**
225 * Battery charge status, from a BATTERY_STATUS_* value.
226 */
Jin Qian2e88d4b2017-02-28 10:57:17 -0800227 public static final int BATTERY_PROPERTY_STATUS = 6;
Jin Qianedf0a372017-02-03 11:08:37 -0800228
Yifan Hongb0f13522017-10-31 15:48:50 -0700229 private final Context mContext;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700230 private final IBatteryStats mBatteryStats;
Jeff Brown6e539312015-02-24 18:53:21 -0800231 private final IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar;
232
233 /**
234 * @removed Was previously made visible by accident.
235 */
236 public BatteryManager() {
Yifan Hongb0f13522017-10-31 15:48:50 -0700237 mContext = null;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700238 mBatteryStats = IBatteryStats.Stub.asInterface(
239 ServiceManager.getService(BatteryStats.SERVICE_NAME));
Jeff Brown6e539312015-02-24 18:53:21 -0800240 mBatteryPropertiesRegistrar = IBatteryPropertiesRegistrar.Stub.asInterface(
241 ServiceManager.getService("batteryproperties"));
242 }
Todd Poynore35872d2013-12-10 11:57:21 -0800243
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600244 /** {@hide} */
Yifan Hongb0f13522017-10-31 15:48:50 -0700245 public BatteryManager(Context context,
246 IBatteryStats batteryStats,
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600247 IBatteryPropertiesRegistrar batteryPropertiesRegistrar) {
Yifan Hongb0f13522017-10-31 15:48:50 -0700248 mContext = context;
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600249 mBatteryStats = batteryStats;
250 mBatteryPropertiesRegistrar = batteryPropertiesRegistrar;
251 }
252
Todd Poynore35872d2013-12-10 11:57:21 -0800253 /**
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700254 * Return true if the battery is currently considered to be charging. This means that
255 * the device is plugged in and is supplying sufficient power that the battery level is
256 * going up (or the battery is fully charged). Changes in this state are matched by
257 * broadcasts of {@link #ACTION_CHARGING} and {@link #ACTION_DISCHARGING}.
258 */
259 public boolean isCharging() {
260 try {
261 return mBatteryStats.isCharging();
262 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700263 throw e.rethrowFromSystemServer();
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700264 }
265 }
266
267 /**
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700268 * Query a battery property from the batteryproperties service.
Todd Poynore35872d2013-12-10 11:57:21 -0800269 *
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700270 * Returns the requested value, or Long.MIN_VALUE if property not
271 * supported on this system or on other error.
Todd Poynore35872d2013-12-10 11:57:21 -0800272 */
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700273 private long queryProperty(int id) {
274 long ret;
275
Todd Poynore35872d2013-12-10 11:57:21 -0800276 if (mBatteryPropertiesRegistrar == null) {
Jeff Brown6e539312015-02-24 18:53:21 -0800277 return Long.MIN_VALUE;
Todd Poynore35872d2013-12-10 11:57:21 -0800278 }
279
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700280 try {
281 BatteryProperty prop = new BatteryProperty();
282
283 if (mBatteryPropertiesRegistrar.getProperty(id, prop) == 0)
284 ret = prop.getLong();
285 else
286 ret = Long.MIN_VALUE;
287 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700288 throw e.rethrowFromSystemServer();
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700289 }
290
291 return ret;
292 }
293
294 /**
Yifan Hongb0f13522017-10-31 15:48:50 -0700295 * Return the value of a battery property of integer type.
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700296 *
297 * @param id identifier of the requested property
298 *
Yifan Hongb0f13522017-10-31 15:48:50 -0700299 * @return the property value. If the property is not supported or there is any other error,
300 * return (a) 0 if {@code targetSdkVersion < VERSION_CODES.P} or (b) Integer.MIN_VALUE
301 * if {@code targetSdkVersion >= VERSION_CODES.P}.
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700302 */
303 public int getIntProperty(int id) {
Yifan Hongb0f13522017-10-31 15:48:50 -0700304 long value = queryProperty(id);
305 if (value == Long.MIN_VALUE && mContext != null
306 && mContext.getApplicationInfo().targetSdkVersion
307 >= android.os.Build.VERSION_CODES.P) {
308 return Integer.MIN_VALUE;
309 }
310
311 return (int) value;
Todd Poynor5fde3ff2014-06-05 14:49:28 -0700312 }
313
314 /**
315 * Return the value of a battery property of long type If the
316 * platform does not provide the property queried, this value will
317 * be Long.MIN_VALUE.
318 *
319 * @param id identifier of the requested property
320 *
321 * @return the property value, or Long.MIN_VALUE if not supported.
322 */
323 public long getLongProperty(int id) {
324 return queryProperty(id);
Todd Poynore35872d2013-12-10 11:57:21 -0800325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326}