1 /* 2 * Copyright (C) 2019 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 19 import android.annotation.IntDef; 20 import android.annotation.IntRange; 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 import android.annotation.RequiresPermission; 24 import android.annotation.SystemApi; 25 import android.annotation.SystemService; 26 import android.annotation.TestApi; 27 import android.content.Context; 28 import android.net.NetworkStack; 29 import android.os.connectivity.CellularBatteryStats; 30 import android.os.connectivity.WifiBatteryStats; 31 import android.telephony.DataConnectionRealTimeInfo; 32 33 import com.android.internal.app.IBatteryStats; 34 35 import java.lang.annotation.Retention; 36 import java.lang.annotation.RetentionPolicy; 37 import java.util.List; 38 39 /** 40 * This class provides an API surface for internal system components to report events that are 41 * needed for battery usage/estimation and battery blaming for apps. 42 * 43 * Note: This internally uses the same {@link IBatteryStats} binder service as the public 44 * {@link BatteryManager}. 45 * @hide 46 */ 47 @SystemApi 48 @SystemService(Context.BATTERY_STATS_SERVICE) 49 public final class BatteryStatsManager { 50 /** 51 * Wifi states. 52 * 53 * @see #noteWifiState(int, String) 54 */ 55 /** 56 * Wifi fully off. 57 */ 58 public static final int WIFI_STATE_OFF = 0; 59 /** 60 * Wifi connectivity off, but scanning enabled. 61 */ 62 public static final int WIFI_STATE_OFF_SCANNING = 1; 63 /** 64 * Wifi on, but no saved infrastructure networks to connect to. 65 */ 66 public static final int WIFI_STATE_ON_NO_NETWORKS = 2; 67 /** 68 * Wifi on, but not connected to any infrastructure networks. 69 */ 70 public static final int WIFI_STATE_ON_DISCONNECTED = 3; 71 /** 72 * Wifi on and connected to a infrastructure network. 73 */ 74 public static final int WIFI_STATE_ON_CONNECTED_STA = 4; 75 /** 76 * Wifi on and connected to a P2P device, but no infrastructure connection to a network. 77 */ 78 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5; 79 /** 80 * Wifi on and connected to both a P2P device and infrastructure connection to a network. 81 */ 82 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6; 83 /** 84 * SoftAp/Hotspot turned on. 85 */ 86 public static final int WIFI_STATE_SOFT_AP = 7; 87 88 /** @hide */ 89 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP + 1; 90 91 /** @hide */ 92 @IntDef(prefix = { "WIFI_STATE_" }, value = { 93 WIFI_STATE_OFF, 94 WIFI_STATE_OFF_SCANNING, 95 WIFI_STATE_ON_NO_NETWORKS, 96 WIFI_STATE_ON_DISCONNECTED, 97 WIFI_STATE_ON_CONNECTED_STA, 98 WIFI_STATE_ON_CONNECTED_P2P, 99 WIFI_STATE_ON_CONNECTED_STA_P2P, 100 WIFI_STATE_SOFT_AP 101 }) 102 @Retention(RetentionPolicy.SOURCE) 103 public @interface WifiState {} 104 105 /** 106 * Wifi supplicant daemon states. 107 * 108 * @see android.net.wifi.SupplicantState for detailed description of states. 109 * @see #noteWifiSupplicantStateChanged(int) 110 */ 111 /** @see android.net.wifi.SupplicantState#INVALID */ 112 public static final int WIFI_SUPPL_STATE_INVALID = 0; 113 /** @see android.net.wifi.SupplicantState#DISCONNECTED*/ 114 public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1; 115 /** @see android.net.wifi.SupplicantState#INTERFACE_DISABLED */ 116 public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2; 117 /** @see android.net.wifi.SupplicantState#INACTIVE*/ 118 public static final int WIFI_SUPPL_STATE_INACTIVE = 3; 119 /** @see android.net.wifi.SupplicantState#SCANNING*/ 120 public static final int WIFI_SUPPL_STATE_SCANNING = 4; 121 /** @see android.net.wifi.SupplicantState#AUTHENTICATING */ 122 public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5; 123 /** @see android.net.wifi.SupplicantState#ASSOCIATING */ 124 public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6; 125 /** @see android.net.wifi.SupplicantState#ASSOCIATED */ 126 public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7; 127 /** @see android.net.wifi.SupplicantState#FOUR_WAY_HANDSHAKE */ 128 public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8; 129 /** @see android.net.wifi.SupplicantState#GROUP_HANDSHAKE */ 130 public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9; 131 /** @see android.net.wifi.SupplicantState#COMPLETED */ 132 public static final int WIFI_SUPPL_STATE_COMPLETED = 10; 133 /** @see android.net.wifi.SupplicantState#DORMANT */ 134 public static final int WIFI_SUPPL_STATE_DORMANT = 11; 135 /** @see android.net.wifi.SupplicantState#UNINITIALIZED */ 136 public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12; 137 138 /** @hide */ 139 public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED + 1; 140 141 /** @hide */ 142 @IntDef(prefix = { "WIFI_SUPPL_STATE_" }, value = { 143 WIFI_SUPPL_STATE_INVALID, 144 WIFI_SUPPL_STATE_DISCONNECTED, 145 WIFI_SUPPL_STATE_INTERFACE_DISABLED, 146 WIFI_SUPPL_STATE_INACTIVE, 147 WIFI_SUPPL_STATE_SCANNING, 148 WIFI_SUPPL_STATE_AUTHENTICATING, 149 WIFI_SUPPL_STATE_ASSOCIATING, 150 WIFI_SUPPL_STATE_ASSOCIATED, 151 WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE, 152 WIFI_SUPPL_STATE_GROUP_HANDSHAKE, 153 WIFI_SUPPL_STATE_COMPLETED, 154 WIFI_SUPPL_STATE_DORMANT, 155 WIFI_SUPPL_STATE_UNINITIALIZED, 156 }) 157 @Retention(RetentionPolicy.SOURCE) 158 public @interface WifiSupplState {} 159 160 161 private final IBatteryStats mBatteryStats; 162 163 /** @hide */ BatteryStatsManager(IBatteryStats batteryStats)164 public BatteryStatsManager(IBatteryStats batteryStats) { 165 mBatteryStats = batteryStats; 166 } 167 168 169 /** 170 * Returns BatteryUsageStats, which contains power attribution data on a per-subsystem 171 * and per-UID basis. 172 * 173 * @hide 174 */ 175 @RequiresPermission(android.Manifest.permission.BATTERY_STATS) 176 @NonNull getBatteryUsageStats()177 public BatteryUsageStats getBatteryUsageStats() { 178 return getBatteryUsageStats(BatteryUsageStatsQuery.DEFAULT); 179 } 180 181 /** 182 * Returns BatteryUsageStats, which contains power attribution data on a per-subsystem 183 * and per-UID basis. 184 * 185 * @hide 186 */ 187 @RequiresPermission(android.Manifest.permission.BATTERY_STATS) 188 @NonNull getBatteryUsageStats(BatteryUsageStatsQuery query)189 public BatteryUsageStats getBatteryUsageStats(BatteryUsageStatsQuery query) { 190 return getBatteryUsageStats(List.of(query)).get(0); 191 } 192 193 /** 194 * Returns BatteryUsageStats, which contains power attribution data on a per-subsystem 195 * and per-UID basis. 196 * 197 * @hide 198 */ 199 @RequiresPermission(android.Manifest.permission.BATTERY_STATS) 200 @NonNull getBatteryUsageStats(List<BatteryUsageStatsQuery> queries)201 public List<BatteryUsageStats> getBatteryUsageStats(List<BatteryUsageStatsQuery> queries) { 202 try { 203 return mBatteryStats.getBatteryUsageStats(queries); 204 } catch (RemoteException e) { 205 throw e.rethrowFromSystemServer(); 206 } 207 } 208 209 /** 210 * Indicates that the wifi connection RSSI has changed. 211 * 212 * @param newRssi The new RSSI value. 213 */ 214 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportWifiRssiChanged(@ntRangefrom = -127, to = 0) int newRssi)215 public void reportWifiRssiChanged(@IntRange(from = -127, to = 0) int newRssi) { 216 try { 217 mBatteryStats.noteWifiRssiChanged(newRssi); 218 } catch (RemoteException e) { 219 e.rethrowFromSystemServer(); 220 } 221 } 222 223 /** 224 * Indicates that wifi was toggled on. 225 */ 226 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportWifiOn()227 public void reportWifiOn() { 228 try { 229 mBatteryStats.noteWifiOn(); 230 } catch (RemoteException e) { 231 e.rethrowFromSystemServer(); 232 } 233 } 234 235 /** 236 * Indicates that wifi was toggled off. 237 */ 238 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportWifiOff()239 public void reportWifiOff() { 240 try { 241 mBatteryStats.noteWifiOff(); 242 } catch (RemoteException e) { 243 e.rethrowFromSystemServer(); 244 } 245 } 246 247 /** 248 * Indicates that wifi state has changed. 249 * 250 * @param newWifiState The new wifi State. 251 * @param accessPoint SSID of the network if wifi is connected to STA, else null. 252 */ 253 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportWifiState(@ifiState int newWifiState, @Nullable String accessPoint)254 public void reportWifiState(@WifiState int newWifiState, 255 @Nullable String accessPoint) { 256 try { 257 mBatteryStats.noteWifiState(newWifiState, accessPoint); 258 } catch (RemoteException e) { 259 e.rethrowFromSystemServer(); 260 } 261 } 262 263 /** 264 * Indicates that a new wifi scan has started. 265 * 266 * @param ws worksource (to be used for battery blaming). 267 */ 268 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportWifiScanStartedFromSource(@onNull WorkSource ws)269 public void reportWifiScanStartedFromSource(@NonNull WorkSource ws) { 270 try { 271 mBatteryStats.noteWifiScanStartedFromSource(ws); 272 } catch (RemoteException e) { 273 e.rethrowFromSystemServer(); 274 } 275 } 276 277 /** 278 * Indicates that an ongoing wifi scan has stopped. 279 * 280 * @param ws worksource (to be used for battery blaming). 281 */ 282 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportWifiScanStoppedFromSource(@onNull WorkSource ws)283 public void reportWifiScanStoppedFromSource(@NonNull WorkSource ws) { 284 try { 285 mBatteryStats.noteWifiScanStoppedFromSource(ws); 286 } catch (RemoteException e) { 287 e.rethrowFromSystemServer(); 288 } 289 } 290 291 /** 292 * Indicates that a new wifi batched scan has started. 293 * 294 * @param ws worksource (to be used for battery blaming). 295 * @param csph Channels scanned per hour. 296 */ 297 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportWifiBatchedScanStartedFromSource(@onNull WorkSource ws, @IntRange(from = 0) int csph)298 public void reportWifiBatchedScanStartedFromSource(@NonNull WorkSource ws, 299 @IntRange(from = 0) int csph) { 300 try { 301 mBatteryStats.noteWifiBatchedScanStartedFromSource(ws, csph); 302 } catch (RemoteException e) { 303 e.rethrowFromSystemServer(); 304 } 305 } 306 307 /** 308 * Indicates that an ongoing wifi batched scan has stopped. 309 * 310 * @param ws worksource (to be used for battery blaming). 311 */ 312 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportWifiBatchedScanStoppedFromSource(@onNull WorkSource ws)313 public void reportWifiBatchedScanStoppedFromSource(@NonNull WorkSource ws) { 314 try { 315 mBatteryStats.noteWifiBatchedScanStoppedFromSource(ws); 316 } catch (RemoteException e) { 317 e.rethrowFromSystemServer(); 318 } 319 } 320 321 /** 322 * Retrieves all the cellular related battery stats. 323 * 324 * @return Instance of {@link CellularBatteryStats}. 325 */ 326 @RequiresPermission(anyOf = { 327 android.Manifest.permission.BATTERY_STATS, 328 android.Manifest.permission.UPDATE_DEVICE_STATS}) getCellularBatteryStats()329 public @NonNull CellularBatteryStats getCellularBatteryStats() { 330 try { 331 return mBatteryStats.getCellularBatteryStats(); 332 } catch (RemoteException e) { 333 e.rethrowFromSystemServer(); 334 return null; 335 } 336 } 337 338 /** 339 * Retrieves all the wifi related battery stats. 340 * 341 * @return Instance of {@link WifiBatteryStats}. 342 */ 343 @RequiresPermission(anyOf = { 344 android.Manifest.permission.BATTERY_STATS, 345 android.Manifest.permission.UPDATE_DEVICE_STATS}) getWifiBatteryStats()346 public @NonNull WifiBatteryStats getWifiBatteryStats() { 347 try { 348 return mBatteryStats.getWifiBatteryStats(); 349 } catch (RemoteException e) { 350 e.rethrowFromSystemServer(); 351 return null; 352 } 353 } 354 355 /** 356 * Retrieves accumulate wake lock stats. 357 * 358 * @hide 359 */ 360 @RequiresPermission(android.Manifest.permission.BATTERY_STATS) 361 @NonNull getWakeLockStats()362 public WakeLockStats getWakeLockStats() { 363 try { 364 return mBatteryStats.getWakeLockStats(); 365 } catch (RemoteException e) { 366 throw e.rethrowFromSystemServer(); 367 } 368 } 369 370 /** 371 * Retrieves accumulated bluetooth stats. 372 * 373 * @hide 374 */ 375 @RequiresPermission(android.Manifest.permission.BATTERY_STATS) 376 @NonNull getBluetoothBatteryStats()377 public BluetoothBatteryStats getBluetoothBatteryStats() { 378 try { 379 return mBatteryStats.getBluetoothBatteryStats(); 380 } catch (RemoteException e) { 381 throw e.rethrowFromSystemServer(); 382 } 383 } 384 385 /** 386 * Indicates an app acquiring full wifi lock. 387 * 388 * @param ws worksource (to be used for battery blaming). 389 */ 390 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportFullWifiLockAcquiredFromSource(@onNull WorkSource ws)391 public void reportFullWifiLockAcquiredFromSource(@NonNull WorkSource ws) { 392 try { 393 mBatteryStats.noteFullWifiLockAcquiredFromSource(ws); 394 } catch (RemoteException e) { 395 e.rethrowFromSystemServer(); 396 } 397 } 398 399 /** 400 * Indicates an app releasing full wifi lock. 401 * 402 * @param ws worksource (to be used for battery blaming). 403 */ 404 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportFullWifiLockReleasedFromSource(@onNull WorkSource ws)405 public void reportFullWifiLockReleasedFromSource(@NonNull WorkSource ws) { 406 try { 407 mBatteryStats.noteFullWifiLockReleasedFromSource(ws); 408 } catch (RemoteException e) { 409 e.rethrowFromSystemServer(); 410 } 411 } 412 413 /** 414 * Indicates that supplicant state has changed. 415 * 416 * @param newSupplState The new Supplicant state. 417 * @param failedAuth Boolean indicating whether there was a connection failure due to 418 * authentication failure. 419 */ 420 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportWifiSupplicantStateChanged(@ifiSupplState int newSupplState, boolean failedAuth)421 public void reportWifiSupplicantStateChanged(@WifiSupplState int newSupplState, 422 boolean failedAuth) { 423 try { 424 mBatteryStats.noteWifiSupplicantStateChanged(newSupplState, failedAuth); 425 } catch (RemoteException e) { 426 e.rethrowFromSystemServer(); 427 } 428 } 429 430 /** 431 * Indicates that an app has acquired the wifi multicast lock. 432 * 433 * @param ws Worksource with the uid of the app that acquired the wifi lock (to be used for 434 * battery blaming). 435 */ 436 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportWifiMulticastEnabled(@onNull WorkSource ws)437 public void reportWifiMulticastEnabled(@NonNull WorkSource ws) { 438 try { 439 mBatteryStats.noteWifiMulticastEnabled(ws.getAttributionUid()); 440 } catch (RemoteException e) { 441 e.rethrowFromSystemServer(); 442 } 443 } 444 445 /** 446 * Indicates that an app has released the wifi multicast lock. 447 * 448 * @param ws Worksource with the uid of the app that released the wifi lock (to be used for 449 * battery blaming). 450 */ 451 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportWifiMulticastDisabled(@onNull WorkSource ws)452 public void reportWifiMulticastDisabled(@NonNull WorkSource ws) { 453 try { 454 mBatteryStats.noteWifiMulticastDisabled(ws.getAttributionUid()); 455 } catch (RemoteException e) { 456 e.rethrowFromSystemServer(); 457 } 458 } 459 460 /** 461 * Indicates that the radio power state has changed. 462 * 463 * @param isActive indicates if the mobile radio is powered. 464 * @param uid Uid of this event. For the active state it represents the uid that was responsible 465 * for waking the radio, or -1 if the system was responsible for waking the radio. 466 * For inactive state, the UID should always be -1. 467 */ 468 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportMobileRadioPowerState(boolean isActive, int uid)469 public void reportMobileRadioPowerState(boolean isActive, int uid) { 470 try { 471 mBatteryStats.noteMobileRadioPowerState(getDataConnectionPowerState(isActive), 472 SystemClock.elapsedRealtimeNanos(), uid); 473 } catch (RemoteException e) { 474 e.rethrowFromSystemServer(); 475 } 476 } 477 478 /** 479 * Indicates that the wifi power state has changed. 480 * 481 * @param isActive indicates if the wifi radio is powered. 482 * @param uid Uid of this event. For the active state it represents the uid that was responsible 483 * for waking the radio, or -1 if the system was responsible for waking the radio. 484 * For inactive state, the UID should always be -1. 485 */ 486 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportWifiRadioPowerState(boolean isActive, int uid)487 public void reportWifiRadioPowerState(boolean isActive, int uid) { 488 try { 489 mBatteryStats.noteWifiRadioPowerState(getDataConnectionPowerState(isActive), 490 SystemClock.elapsedRealtimeNanos(), uid); 491 } catch (RemoteException e) { 492 e.rethrowFromSystemServer(); 493 } 494 } 495 496 /** 497 * Notifies the battery stats of a new interface, and the transport types of the network that 498 * includes that interface. 499 * 500 * @param iface The interface of the network. 501 * @param transportTypes The transport type of the network {@link Transport}. 502 * @hide 503 */ 504 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) 505 @RequiresPermission(anyOf = { 506 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, 507 android.Manifest.permission.NETWORK_STACK}) reportNetworkInterfaceForTransports(@onNull String iface, @NonNull int[] transportTypes)508 public void reportNetworkInterfaceForTransports(@NonNull String iface, 509 @NonNull int[] transportTypes) throws RuntimeException { 510 try { 511 mBatteryStats.noteNetworkInterfaceForTransports(iface, transportTypes); 512 } catch (RemoteException e) { 513 e.rethrowFromSystemServer(); 514 } 515 } 516 517 /** 518 * Indicates that Bluetooth was toggled on. 519 * 520 * @param uid calling package uid 521 * @param reason why Bluetooth has been turned on 522 * @param packageName package responsible for this change 523 * @deprecated Bluetooth self report its state and no longer call this 524 */ 525 @Deprecated 526 @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) reportBluetoothOn(int uid, int reason, @NonNull String packageName)527 public void reportBluetoothOn(int uid, int reason, @NonNull String packageName) { 528 } 529 530 /** 531 * Indicates that Bluetooth was toggled off. 532 * 533 * @param uid calling package uid 534 * @param reason why Bluetooth has been turned on 535 * @param packageName package responsible for this change 536 * @deprecated Bluetooth self report its state and no longer call this 537 */ 538 @Deprecated 539 @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) reportBluetoothOff(int uid, int reason, @NonNull String packageName)540 public void reportBluetoothOff(int uid, int reason, @NonNull String packageName) { 541 } 542 543 /** 544 * Indicates that a new Bluetooth LE scan has started. 545 * 546 * @param ws worksource (to be used for battery blaming). 547 * @param isUnoptimized whether or not the scan has a filter. 548 */ 549 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportBleScanStarted(@onNull WorkSource ws, boolean isUnoptimized)550 public void reportBleScanStarted(@NonNull WorkSource ws, boolean isUnoptimized) { 551 try { 552 mBatteryStats.noteBleScanStarted(ws, isUnoptimized); 553 } catch (RemoteException e) { 554 e.rethrowFromSystemServer(); 555 } 556 } 557 558 /** 559 * Indicates that an ongoing Bluetooth LE scan has stopped. 560 * 561 * @param ws worksource (to be used for battery blaming). 562 * @param isUnoptimized whether or not the scan has a filter. 563 */ 564 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportBleScanStopped(@onNull WorkSource ws, boolean isUnoptimized)565 public void reportBleScanStopped(@NonNull WorkSource ws, boolean isUnoptimized) { 566 try { 567 mBatteryStats.noteBleScanStopped(ws, isUnoptimized); 568 } catch (RemoteException e) { 569 e.rethrowFromSystemServer(); 570 } 571 } 572 573 /** 574 * Indicates that Bluetooth LE has been reset. 575 */ 576 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportBleScanReset()577 public void reportBleScanReset() { 578 try { 579 mBatteryStats.noteBleScanReset(); 580 } catch (RemoteException e) { 581 e.rethrowFromSystemServer(); 582 } 583 } 584 585 /** 586 * Indicates that Bluetooth LE scan has received new results. 587 * 588 * @param ws worksource (to be used for battery blaming). 589 * @param numNewResults number of results received since last update. 590 */ 591 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) reportBleScanResults(@onNull WorkSource ws, int numNewResults)592 public void reportBleScanResults(@NonNull WorkSource ws, int numNewResults) { 593 try { 594 mBatteryStats.noteBleScanResults(ws, numNewResults); 595 } catch (RemoteException e) { 596 e.rethrowFromSystemServer(); 597 } 598 } 599 getDataConnectionPowerState(boolean isActive)600 private static int getDataConnectionPowerState(boolean isActive) { 601 // TODO: DataConnectionRealTimeInfo is under telephony package but the constants are used 602 // for both Wifi and mobile. It would make more sense to separate the constants to a 603 // generic class or move it to generic package. 604 return isActive ? DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH 605 : DataConnectionRealTimeInfo.DC_POWER_STATE_LOW; 606 } 607 608 /** 609 * Sets battery AC charger to enabled/disabled, and freezes the battery state. 610 * @hide 611 */ 612 @TestApi 613 @RequiresPermission(android.Manifest.permission.DEVICE_POWER) setChargerAcOnline(boolean online, boolean forceUpdate)614 public void setChargerAcOnline(boolean online, boolean forceUpdate) { 615 try { 616 mBatteryStats.setChargerAcOnline(online, forceUpdate); 617 } catch (RemoteException e) { 618 e.rethrowFromSystemServer(); 619 } 620 } 621 622 /** 623 * Sets battery level, and freezes the battery state. 624 * @hide 625 */ 626 @TestApi 627 @RequiresPermission(android.Manifest.permission.DEVICE_POWER) setBatteryLevel(int level, boolean forceUpdate)628 public void setBatteryLevel(int level, boolean forceUpdate) { 629 try { 630 mBatteryStats.setBatteryLevel(level, forceUpdate); 631 } catch (RemoteException e) { 632 e.rethrowFromSystemServer(); 633 } 634 } 635 636 /** 637 * Unplugs battery, and freezes the battery state. 638 * @hide 639 */ 640 @TestApi 641 @RequiresPermission(android.Manifest.permission.DEVICE_POWER) unplugBattery(boolean forceUpdate)642 public void unplugBattery(boolean forceUpdate) { 643 try { 644 mBatteryStats.unplugBattery(forceUpdate); 645 } catch (RemoteException e) { 646 e.rethrowFromSystemServer(); 647 } 648 } 649 650 /** 651 * Unfreezes battery state, returning to current hardware values. 652 * @hide 653 */ 654 @TestApi 655 @RequiresPermission(android.Manifest.permission.DEVICE_POWER) resetBattery(boolean forceUpdate)656 public void resetBattery(boolean forceUpdate) { 657 try { 658 mBatteryStats.resetBattery(forceUpdate); 659 } catch (RemoteException e) { 660 e.rethrowFromSystemServer(); 661 } 662 } 663 664 /** 665 * Suspend charging even if plugged in. 666 * @hide 667 */ 668 @TestApi 669 @RequiresPermission(android.Manifest.permission.DEVICE_POWER) suspendBatteryInput()670 public void suspendBatteryInput() { 671 try { 672 mBatteryStats.suspendBatteryInput(); 673 } catch (RemoteException e) { 674 e.rethrowFromSystemServer(); 675 } 676 } 677 }