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