Alan Viverette | 3da604b | 2020-06-10 18:34:39 +0000 | [diff] [blame] | 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.content.Context; |
| 27 | import android.os.connectivity.CellularBatteryStats; |
| 28 | import android.os.connectivity.WifiBatteryStats; |
| 29 | |
| 30 | import com.android.internal.app.IBatteryStats; |
| 31 | |
| 32 | import java.lang.annotation.Retention; |
| 33 | import java.lang.annotation.RetentionPolicy; |
| 34 | |
| 35 | /** |
| 36 | * This class provides an API surface for internal system components to report events that are |
| 37 | * needed for battery usage/estimation and battery blaming for apps. |
| 38 | * |
| 39 | * Note: This internally uses the same {@link IBatteryStats} binder service as the public |
| 40 | * {@link BatteryManager}. |
| 41 | * @hide |
| 42 | */ |
| 43 | @SystemApi |
| 44 | @SystemService(Context.BATTERY_STATS_SERVICE) |
| 45 | public final class BatteryStatsManager { |
| 46 | /** |
| 47 | * Wifi states. |
| 48 | * |
| 49 | * @see #noteWifiState(int, String) |
| 50 | */ |
| 51 | /** |
| 52 | * Wifi fully off. |
| 53 | */ |
| 54 | public static final int WIFI_STATE_OFF = 0; |
| 55 | /** |
| 56 | * Wifi connectivity off, but scanning enabled. |
| 57 | */ |
| 58 | public static final int WIFI_STATE_OFF_SCANNING = 1; |
| 59 | /** |
| 60 | * Wifi on, but no saved infrastructure networks to connect to. |
| 61 | */ |
| 62 | public static final int WIFI_STATE_ON_NO_NETWORKS = 2; |
| 63 | /** |
| 64 | * Wifi on, but not connected to any infrastructure networks. |
| 65 | */ |
| 66 | public static final int WIFI_STATE_ON_DISCONNECTED = 3; |
| 67 | /** |
| 68 | * Wifi on and connected to a infrastructure network. |
| 69 | */ |
| 70 | public static final int WIFI_STATE_ON_CONNECTED_STA = 4; |
| 71 | /** |
| 72 | * Wifi on and connected to a P2P device, but no infrastructure connection to a network. |
| 73 | */ |
| 74 | public static final int WIFI_STATE_ON_CONNECTED_P2P = 5; |
| 75 | /** |
| 76 | * Wifi on and connected to both a P2P device and infrastructure connection to a network. |
| 77 | */ |
| 78 | public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6; |
| 79 | /** |
| 80 | * SoftAp/Hotspot turned on. |
| 81 | */ |
| 82 | public static final int WIFI_STATE_SOFT_AP = 7; |
| 83 | |
| 84 | /** @hide */ |
| 85 | public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP + 1; |
| 86 | |
| 87 | /** @hide */ |
| 88 | @IntDef(flag = true, prefix = { "WIFI_STATE_" }, value = { |
| 89 | WIFI_STATE_OFF, |
| 90 | WIFI_STATE_OFF_SCANNING, |
| 91 | WIFI_STATE_ON_NO_NETWORKS, |
| 92 | WIFI_STATE_ON_DISCONNECTED, |
| 93 | WIFI_STATE_ON_CONNECTED_STA, |
| 94 | WIFI_STATE_ON_CONNECTED_P2P, |
| 95 | WIFI_STATE_ON_CONNECTED_STA_P2P, |
| 96 | WIFI_STATE_SOFT_AP |
| 97 | }) |
| 98 | @Retention(RetentionPolicy.SOURCE) |
| 99 | public @interface WifiState {} |
| 100 | |
| 101 | /** |
| 102 | * Wifi supplicant daemon states. |
| 103 | * |
| 104 | * @see android.net.wifi.SupplicantState for detailed description of states. |
| 105 | * @see #noteWifiSupplicantStateChanged(int) |
| 106 | */ |
| 107 | /** @see android.net.wifi.SupplicantState#INVALID */ |
| 108 | public static final int WIFI_SUPPL_STATE_INVALID = 0; |
| 109 | /** @see android.net.wifi.SupplicantState#DISCONNECTED*/ |
| 110 | public static final int WIFI_SUPPL_STATE_DISCONNECTED = 1; |
| 111 | /** @see android.net.wifi.SupplicantState#INTERFACE_DISABLED */ |
| 112 | public static final int WIFI_SUPPL_STATE_INTERFACE_DISABLED = 2; |
| 113 | /** @see android.net.wifi.SupplicantState#INACTIVE*/ |
| 114 | public static final int WIFI_SUPPL_STATE_INACTIVE = 3; |
| 115 | /** @see android.net.wifi.SupplicantState#SCANNING*/ |
| 116 | public static final int WIFI_SUPPL_STATE_SCANNING = 4; |
| 117 | /** @see android.net.wifi.SupplicantState#AUTHENTICATING */ |
| 118 | public static final int WIFI_SUPPL_STATE_AUTHENTICATING = 5; |
| 119 | /** @see android.net.wifi.SupplicantState#ASSOCIATING */ |
| 120 | public static final int WIFI_SUPPL_STATE_ASSOCIATING = 6; |
| 121 | /** @see android.net.wifi.SupplicantState#ASSOCIATED */ |
| 122 | public static final int WIFI_SUPPL_STATE_ASSOCIATED = 7; |
| 123 | /** @see android.net.wifi.SupplicantState#FOUR_WAY_HANDSHAKE */ |
| 124 | public static final int WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE = 8; |
| 125 | /** @see android.net.wifi.SupplicantState#GROUP_HANDSHAKE */ |
| 126 | public static final int WIFI_SUPPL_STATE_GROUP_HANDSHAKE = 9; |
| 127 | /** @see android.net.wifi.SupplicantState#COMPLETED */ |
| 128 | public static final int WIFI_SUPPL_STATE_COMPLETED = 10; |
| 129 | /** @see android.net.wifi.SupplicantState#DORMANT */ |
| 130 | public static final int WIFI_SUPPL_STATE_DORMANT = 11; |
| 131 | /** @see android.net.wifi.SupplicantState#UNINITIALIZED */ |
| 132 | public static final int WIFI_SUPPL_STATE_UNINITIALIZED = 12; |
| 133 | |
| 134 | /** @hide */ |
| 135 | public static final int NUM_WIFI_SUPPL_STATES = WIFI_SUPPL_STATE_UNINITIALIZED + 1; |
| 136 | |
| 137 | /** @hide */ |
| 138 | @IntDef(flag = true, prefix = { "WIFI_SUPPL_STATE_" }, value = { |
| 139 | WIFI_SUPPL_STATE_INVALID, |
| 140 | WIFI_SUPPL_STATE_DISCONNECTED, |
| 141 | WIFI_SUPPL_STATE_INTERFACE_DISABLED, |
| 142 | WIFI_SUPPL_STATE_INACTIVE, |
| 143 | WIFI_SUPPL_STATE_SCANNING, |
| 144 | WIFI_SUPPL_STATE_AUTHENTICATING, |
| 145 | WIFI_SUPPL_STATE_ASSOCIATING, |
| 146 | WIFI_SUPPL_STATE_ASSOCIATED, |
| 147 | WIFI_SUPPL_STATE_FOUR_WAY_HANDSHAKE, |
| 148 | WIFI_SUPPL_STATE_GROUP_HANDSHAKE, |
| 149 | WIFI_SUPPL_STATE_COMPLETED, |
| 150 | WIFI_SUPPL_STATE_DORMANT, |
| 151 | WIFI_SUPPL_STATE_UNINITIALIZED, |
| 152 | }) |
| 153 | @Retention(RetentionPolicy.SOURCE) |
| 154 | public @interface WifiSupplState {} |
| 155 | |
| 156 | private final IBatteryStats mBatteryStats; |
| 157 | |
| 158 | /** @hide */ |
| 159 | public BatteryStatsManager(IBatteryStats batteryStats) { |
| 160 | mBatteryStats = batteryStats; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Indicates that the wifi connection RSSI has changed. |
| 165 | * |
| 166 | * @param newRssi The new RSSI value. |
| 167 | */ |
| 168 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 169 | public void reportWifiRssiChanged(@IntRange(from = -127, to = 0) int newRssi) { |
| 170 | try { |
| 171 | mBatteryStats.noteWifiRssiChanged(newRssi); |
| 172 | } catch (RemoteException e) { |
| 173 | e.rethrowFromSystemServer(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Indicates that wifi was toggled on. |
| 179 | */ |
| 180 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 181 | public void reportWifiOn() { |
| 182 | try { |
| 183 | mBatteryStats.noteWifiOn(); |
| 184 | } catch (RemoteException e) { |
| 185 | e.rethrowFromSystemServer(); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Indicates that wifi was toggled off. |
| 191 | */ |
| 192 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 193 | public void reportWifiOff() { |
| 194 | try { |
| 195 | mBatteryStats.noteWifiOff(); |
| 196 | } catch (RemoteException e) { |
| 197 | e.rethrowFromSystemServer(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Indicates that wifi state has changed. |
| 203 | * |
| 204 | * @param newWifiState The new wifi State. |
| 205 | * @param accessPoint SSID of the network if wifi is connected to STA, else null. |
| 206 | */ |
| 207 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 208 | public void reportWifiState(@WifiState int newWifiState, |
| 209 | @Nullable String accessPoint) { |
| 210 | try { |
| 211 | mBatteryStats.noteWifiState(newWifiState, accessPoint); |
| 212 | } catch (RemoteException e) { |
| 213 | e.rethrowFromSystemServer(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Indicates that a new wifi scan has started. |
| 219 | * |
| 220 | * @param ws Worksource (to be used for battery blaming). |
| 221 | */ |
| 222 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 223 | public void reportWifiScanStartedFromSource(@NonNull WorkSource ws) { |
| 224 | try { |
| 225 | mBatteryStats.noteWifiScanStartedFromSource(ws); |
| 226 | } catch (RemoteException e) { |
| 227 | e.rethrowFromSystemServer(); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Indicates that an ongoing wifi scan has stopped. |
| 233 | * |
| 234 | * @param ws Worksource (to be used for battery blaming). |
| 235 | */ |
| 236 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 237 | public void reportWifiScanStoppedFromSource(@NonNull WorkSource ws) { |
| 238 | try { |
| 239 | mBatteryStats.noteWifiScanStoppedFromSource(ws); |
| 240 | } catch (RemoteException e) { |
| 241 | e.rethrowFromSystemServer(); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Indicates that a new wifi batched scan has started. |
| 247 | * |
| 248 | * @param ws Worksource (to be used for battery blaming). |
| 249 | * @param csph Channels scanned per hour. |
| 250 | */ |
| 251 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 252 | public void reportWifiBatchedScanStartedFromSource(@NonNull WorkSource ws, |
| 253 | @IntRange(from = 0) int csph) { |
| 254 | try { |
| 255 | mBatteryStats.noteWifiBatchedScanStartedFromSource(ws, csph); |
| 256 | } catch (RemoteException e) { |
| 257 | e.rethrowFromSystemServer(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Indicates that an ongoing wifi batched scan has stopped. |
| 263 | * |
| 264 | * @param ws Worksource (to be used for battery blaming). |
| 265 | */ |
| 266 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 267 | public void reportWifiBatchedScanStoppedFromSource(@NonNull WorkSource ws) { |
| 268 | try { |
| 269 | mBatteryStats.noteWifiBatchedScanStoppedFromSource(ws); |
| 270 | } catch (RemoteException e) { |
| 271 | e.rethrowFromSystemServer(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Retrieves all the cellular related battery stats. |
| 277 | * |
| 278 | * @return Instance of {@link CellularBatteryStats}. |
| 279 | */ |
| 280 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 281 | public @NonNull CellularBatteryStats getCellularBatteryStats() { |
| 282 | try { |
| 283 | return mBatteryStats.getCellularBatteryStats(); |
| 284 | } catch (RemoteException e) { |
| 285 | e.rethrowFromSystemServer(); |
| 286 | return null; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Retrieves all the wifi related battery stats. |
| 292 | * |
| 293 | * @return Instance of {@link WifiBatteryStats}. |
| 294 | */ |
| 295 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 296 | public @NonNull WifiBatteryStats getWifiBatteryStats() { |
| 297 | try { |
| 298 | return mBatteryStats.getWifiBatteryStats(); |
| 299 | } catch (RemoteException e) { |
| 300 | e.rethrowFromSystemServer(); |
| 301 | return null; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Indicates an app acquiring full wifi lock. |
| 307 | * |
| 308 | * @param ws Worksource (to be used for battery blaming). |
| 309 | */ |
| 310 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 311 | public void reportFullWifiLockAcquiredFromSource(@NonNull WorkSource ws) { |
| 312 | try { |
| 313 | mBatteryStats.noteFullWifiLockAcquiredFromSource(ws); |
| 314 | } catch (RemoteException e) { |
| 315 | e.rethrowFromSystemServer(); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Indicates an app releasing full wifi lock. |
| 321 | * |
| 322 | * @param ws Worksource (to be used for battery blaming). |
| 323 | */ |
| 324 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 325 | public void reportFullWifiLockReleasedFromSource(@NonNull WorkSource ws) { |
| 326 | try { |
| 327 | mBatteryStats.noteFullWifiLockReleasedFromSource(ws); |
| 328 | } catch (RemoteException e) { |
| 329 | e.rethrowFromSystemServer(); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Indicates that supplicant state has changed. |
| 335 | * |
| 336 | * @param newSupplState The new Supplicant state. |
| 337 | * @param failedAuth Boolean indicating whether there was a connection failure due to |
| 338 | * authentication failure. |
| 339 | */ |
| 340 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 341 | public void reportWifiSupplicantStateChanged(@WifiSupplState int newSupplState, |
| 342 | boolean failedAuth) { |
| 343 | try { |
| 344 | mBatteryStats.noteWifiSupplicantStateChanged(newSupplState, failedAuth); |
| 345 | } catch (RemoteException e) { |
| 346 | e.rethrowFromSystemServer(); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Indicates that an app has acquired the wifi multicast lock. |
| 352 | * |
| 353 | * @param ws Worksource with the uid of the app that acquired the wifi lock (to be used for |
| 354 | * battery blaming). |
| 355 | */ |
| 356 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 357 | public void reportWifiMulticastEnabled(@NonNull WorkSource ws) { |
| 358 | try { |
| 359 | mBatteryStats.noteWifiMulticastEnabled(ws.getAttributionUid()); |
| 360 | } catch (RemoteException e) { |
| 361 | e.rethrowFromSystemServer(); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Indicates that an app has released the wifi multicast lock. |
| 367 | * |
| 368 | * @param ws Worksource with the uid of the app that released the wifi lock (to be used for |
| 369 | * battery blaming). |
| 370 | */ |
| 371 | @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS) |
| 372 | public void reportWifiMulticastDisabled(@NonNull WorkSource ws) { |
| 373 | try { |
| 374 | mBatteryStats.noteWifiMulticastDisabled(ws.getAttributionUid()); |
| 375 | } catch (RemoteException e) { |
| 376 | e.rethrowFromSystemServer(); |
| 377 | } |
| 378 | } |
| 379 | } |