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 | |
Sandeep Patil | a21c427 | 2016-11-10 14:02:45 -0800 | [diff] [blame] | 19 | import android.hardware.health.V1_0.Constants; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 20 | import com.android.internal.app.IBatteryStats; |
Todd Poynor | e35872d | 2013-12-10 11:57:21 -0800 | [diff] [blame] | 21 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | /** |
| 23 | * The BatteryManager class contains strings and constants used for values |
Todd Poynor | e35872d | 2013-12-10 11:57:21 -0800 | [diff] [blame] | 24 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | */ |
| 27 | public class BatteryManager { |
Dianne Hackborn | edd9316 | 2009-09-19 14:03:05 -0700 | [diff] [blame] | 28 | /** |
| 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 Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}: |
Joe Onorato | 4ca7f1e | 2010-10-27 15:32:23 -0700 | [diff] [blame] | 94 | * Int value set to nonzero if an unsupported charger is attached |
Mike Lockwood | deff9c8 | 2010-09-04 10:29:17 -0400 | [diff] [blame] | 95 | * to the device. |
| 96 | * {@hide} |
| 97 | */ |
| 98 | public static final String EXTRA_INVALID_CHARGER = "invalid_charger"; |
| 99 | |
Adrian Roos | 7b04311 | 2015-07-10 13:00:33 -0700 | [diff] [blame] | 100 | /** |
| 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 Sridharan | f92fcfe | 2015-10-27 13:59:34 -0700 | [diff] [blame] | 107 | /** |
| 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 Kandoi | 6361e22 | 2016-04-07 11:28:30 -0700 | [diff] [blame] | 114 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | // values for "status" field in the ACTION_BATTERY_CHANGED Intent |
Sandeep Patil | a21c427 | 2016-11-10 14:02:45 -0800 | [diff] [blame] | 122 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | |
| 128 | // values for "health" field in the ACTION_BATTERY_CHANGED Intent |
Sandeep Patil | a21c427 | 2016-11-10 14:02:45 -0800 | [diff] [blame] | 129 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | |
| 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 Muramatsu | 37a37f4 | 2012-08-14 15:21:02 -0700 | [diff] [blame] | 143 | /** Power source is wireless. */ |
| 144 | public static final int BATTERY_PLUGGED_WIRELESS = 4; |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 145 | |
| 146 | /** @hide */ |
| 147 | public static final int BATTERY_PLUGGED_ANY = |
| 148 | BATTERY_PLUGGED_AC | BATTERY_PLUGGED_USB | BATTERY_PLUGGED_WIRELESS; |
Todd Poynor | e35872d | 2013-12-10 11:57:21 -0800 | [diff] [blame] | 149 | |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 150 | /** |
| 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 Poynor | 5fde3ff | 2014-06-05 14:49:28 -0700 | [diff] [blame] | 170 | /* |
| 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 Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 204 | private final IBatteryStats mBatteryStats; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 205 | private final IBatteryPropertiesRegistrar mBatteryPropertiesRegistrar; |
| 206 | |
| 207 | /** |
| 208 | * @removed Was previously made visible by accident. |
| 209 | */ |
| 210 | public BatteryManager() { |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 211 | mBatteryStats = IBatteryStats.Stub.asInterface( |
| 212 | ServiceManager.getService(BatteryStats.SERVICE_NAME)); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 213 | mBatteryPropertiesRegistrar = IBatteryPropertiesRegistrar.Stub.asInterface( |
| 214 | ServiceManager.getService("batteryproperties")); |
| 215 | } |
Todd Poynor | e35872d | 2013-12-10 11:57:21 -0800 | [diff] [blame] | 216 | |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 217 | /** {@hide} */ |
| 218 | public BatteryManager(IBatteryStats batteryStats, |
| 219 | IBatteryPropertiesRegistrar batteryPropertiesRegistrar) { |
| 220 | mBatteryStats = batteryStats; |
| 221 | mBatteryPropertiesRegistrar = batteryPropertiesRegistrar; |
| 222 | } |
| 223 | |
Todd Poynor | e35872d | 2013-12-10 11:57:21 -0800 | [diff] [blame] | 224 | /** |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 225 | * 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 Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 234 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
Todd Poynor | 5fde3ff | 2014-06-05 14:49:28 -0700 | [diff] [blame] | 239 | * Query a battery property from the batteryproperties service. |
Todd Poynor | e35872d | 2013-12-10 11:57:21 -0800 | [diff] [blame] | 240 | * |
Todd Poynor | 5fde3ff | 2014-06-05 14:49:28 -0700 | [diff] [blame] | 241 | * Returns the requested value, or Long.MIN_VALUE if property not |
| 242 | * supported on this system or on other error. |
Todd Poynor | e35872d | 2013-12-10 11:57:21 -0800 | [diff] [blame] | 243 | */ |
Todd Poynor | 5fde3ff | 2014-06-05 14:49:28 -0700 | [diff] [blame] | 244 | private long queryProperty(int id) { |
| 245 | long ret; |
| 246 | |
Todd Poynor | e35872d | 2013-12-10 11:57:21 -0800 | [diff] [blame] | 247 | if (mBatteryPropertiesRegistrar == null) { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 248 | return Long.MIN_VALUE; |
Todd Poynor | e35872d | 2013-12-10 11:57:21 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Todd Poynor | 5fde3ff | 2014-06-05 14:49:28 -0700 | [diff] [blame] | 251 | 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 Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 259 | throw e.rethrowFromSystemServer(); |
Todd Poynor | 5fde3ff | 2014-06-05 14:49:28 -0700 | [diff] [blame] | 260 | } |
| 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 Poynor | e35872d | 2013-12-10 11:57:21 -0800 | [diff] [blame] | 289 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | } |