blob: 0545666ca743b8bfa4b339b12101fc4bb7b2114e [file] [log] [blame]
Roshan Pius848513e2019-10-11 13:44:00 -07001/*
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
17package android.os;
18
Roshan Pius9e1e0fc2019-12-10 15:20:08 -080019import android.annotation.IntDef;
Roshan Pius848513e2019-10-11 13:44:00 -070020import android.annotation.IntRange;
21import android.annotation.NonNull;
22import android.annotation.Nullable;
23import android.annotation.RequiresPermission;
24import android.annotation.SystemApi;
25import android.annotation.SystemService;
26import android.content.Context;
SongFerngWang8a5307c2019-11-04 22:17:47 +080027import android.os.connectivity.CellularBatteryStats;
Roshan Pius848513e2019-10-11 13:44:00 -070028import android.os.connectivity.WifiBatteryStats;
29
30import com.android.internal.app.IBatteryStats;
31
Roshan Pius9e1e0fc2019-12-10 15:20:08 -080032import java.lang.annotation.Retention;
33import java.lang.annotation.RetentionPolicy;
34
Roshan Pius848513e2019-10-11 13:44:00 -070035/**
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)
45public class BatteryStatsManager {
Roshan Pius9e1e0fc2019-12-10 15:20:08 -080046 /**
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
Roshan Pius848513e2019-10-11 13:44:00 -0700156 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 noteWifiRssiChanged(@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 noteWifiOn() {
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 noteWifiOff() {
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)
Roshan Pius9e1e0fc2019-12-10 15:20:08 -0800208 public void noteWifiState(@WifiState int newWifiState,
Roshan Pius848513e2019-10-11 13:44:00 -0700209 @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 noteWifiScanStartedFromSource(@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 noteWifiScanStoppedFromSource(@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 noteWifiBatchedScanStartedFromSource(@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 noteWifiBatchedScanStoppedFromSource(@NonNull WorkSource ws) {
268 try {
269 mBatteryStats.noteWifiBatchedScanStoppedFromSource(ws);
270 } catch (RemoteException e) {
271 e.rethrowFromSystemServer();
272 }
273 }
274
275 /**
SongFerngWang8a5307c2019-11-04 22:17:47 +0800276 * 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 /**
Roshan Pius848513e2019-10-11 13:44:00 -0700291 * 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 noteFullWifiLockAcquiredFromSource(@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 noteFullWifiLockReleasedFromSource(@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)
Roshan Pius9e1e0fc2019-12-10 15:20:08 -0800341 public void noteWifiSupplicantStateChanged(@WifiSupplState int newSupplState,
Roshan Pius848513e2019-10-11 13:44:00 -0700342 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 uid UID of the app that acquired the wifi lock (to be used for battery blaming).
354 */
355 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
356 public void noteWifiMulticastEnabled(int uid) {
357 try {
358 mBatteryStats.noteWifiMulticastEnabled(uid);
359 } catch (RemoteException e) {
360 e.rethrowFromSystemServer();
361 }
362 }
363
364 /**
365 * Indicates that an app has released the wifi multicast lock.
366 *
367 * @param uid UID of the app that released the wifi lock (to be used for battery blaming).
368 */
369 @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
370 public void noteWifiMulticastDisabled(int uid) {
371 try {
372 mBatteryStats.noteWifiMulticastDisabled(uid);
373 } catch (RemoteException e) {
374 e.rethrowFromSystemServer();
375 }
376 }
377}