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