Debian Codegraph @

apex/jobscheduler/framework/java/android/os/PowerExemptionManager.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 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 static android.app.ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
20 import static android.app.ActivityManager.PROCESS_STATE_BOUND_TOP;
21 import static android.app.ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
22 import static android.app.ActivityManager.PROCESS_STATE_PERSISTENT;
23 import static android.app.ActivityManager.PROCESS_STATE_PERSISTENT_UI;
24 import static android.app.ActivityManager.PROCESS_STATE_TOP;
25
26 import android.annotation.IntDef;
27 import android.annotation.NonNull;
28 import android.annotation.Nullable;
29 import android.annotation.RequiresPermission;
30 import android.annotation.SystemApi;
31 import android.annotation.SystemService;
32 import android.annotation.UserHandleAware;
33 import android.content.Context;
34
35 import java.lang.annotation.Retention;
36 import java.lang.annotation.RetentionPolicy;
37 import java.util.Collections;
38 import java.util.List;
39
40 /**
41 * Interface to access and modify the permanent and temporary power save allow list. The two lists
42 * are kept separately. Apps placed on the permanent allow list are only removed via an explicit
43 * {@link #removeFromPermanentAllowList(String)} call. Apps allow-listed by default by the system
44 * cannot be removed. Apps placed on the temporary allow list are removed from that allow list after
45 * a predetermined amount of time.
46 *
47 * @hide
48 */
49 @SystemApi
50 @SystemService(Context.POWER_EXEMPTION_SERVICE)
51 public class PowerExemptionManager {
52 private final Context mContext;
53 // Proxy to DeviceIdleController for now
54 // TODO: migrate to PowerExemptionController
55 private final IDeviceIdleController mService;
56
57 /**
58 * Indicates that an unforeseen event has occurred and the app should be allow-listed to handle
59 * it.
60 */
61 public static final int EVENT_UNSPECIFIED = 0;
62
63 /**
64 * Indicates that an SMS event has occurred and the app should be allow-listed to handle it.
65 */
66 public static final int EVENT_SMS = 1;
67
68 /**
69 * Indicates that an MMS event has occurred and the app should be allow-listed to handle it.
70 */
71 public static final int EVENT_MMS = 2;
72
73 /**
74 * @hide
75 */
76 @Retention(RetentionPolicy.SOURCE)
77 @IntDef(prefix = {"EVENT_"}, value = {
78 EVENT_UNSPECIFIED,
79 EVENT_SMS,
80 EVENT_MMS,
81 })
82 public @interface AllowListEvent {
83 }
84
85 /**
86 * Does not place the app on any temporary allow list. Nullifies the previous call to
87 * {@link android.app.BroadcastOptions#setTemporaryAppAllowlist(long, int, int, String)}.
88 * Note: this will not remove the receiver app from the temp allow list.
89 */
90 public static final int TEMPORARY_ALLOW_LIST_TYPE_NONE = -1;
91 /**
92 * Allow the temp allow list behavior, plus allow foreground service start from background.
93 */
94 public static final int TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED = 0;
95 /**
96 * Only allow the temp allow list behavior, not allow foreground service start from background.
97 */
98 public static final int TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED = 1;
99
100 /**
101 * The list of temp allow list types.
102 * @hide
103 */
104 @IntDef(flag = true, prefix = { "TEMPORARY_ALLOW_LIST_TYPE_" }, value = {
105 TEMPORARY_ALLOW_LIST_TYPE_NONE,
106 TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED,
107 TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED,
108 })
109 @Retention(RetentionPolicy.SOURCE)
110 public @interface TempAllowListType {}
111
112 /* Reason codes for BG-FGS-launch. */
113 /**
114 * BG-FGS-launch is denied.
115 * @hide
116 */
117 public static final int REASON_DENIED = -1;
118
119 /* Reason code range 0-9 are reserved for default reasons */
120 /**
121 * The default reason code if reason is unknown.
122 */
123 public static final int REASON_UNKNOWN = 0;
124 /**
125 * Use REASON_OTHER if there is no better choice.
126 */
127 public static final int REASON_OTHER = 1;
128
129 /* Reason code range 10-49 are reserved for BG-FGS-launch allowed proc states */
130 /** @hide */
131 public static final int REASON_PROC_STATE_PERSISTENT = 10;
132 /** @hide */
133 public static final int REASON_PROC_STATE_PERSISTENT_UI = 11;
134 /** @hide */
135 public static final int REASON_PROC_STATE_TOP = 12;
136 /** @hide */
137 public static final int REASON_PROC_STATE_BTOP = 13;
138 /** @hide */
139 public static final int REASON_PROC_STATE_FGS = 14;
140 /** @hide */
141 public static final int REASON_PROC_STATE_BFGS = 15;
142
143 /* Reason code range 50-99 are reserved for BG-FGS-launch allowed reasons */
144 /** @hide */
145 public static final int REASON_UID_VISIBLE = 50;
146 /** @hide */
147 public static final int REASON_SYSTEM_UID = 51;
148 /** @hide */
149 public static final int REASON_ACTIVITY_STARTER = 52;
150 /** @hide */
151 public static final int REASON_START_ACTIVITY_FLAG = 53;
152 /** @hide */
153 public static final int REASON_FGS_BINDING = 54;
154 /** @hide */
155 public static final int REASON_DEVICE_OWNER = 55;
156 /** @hide */
157 public static final int REASON_PROFILE_OWNER = 56;
158 /** @hide */
159 public static final int REASON_COMPANION_DEVICE_MANAGER = 57;
160 /**
161 * START_ACTIVITIES_FROM_BACKGROUND permission.
162 * @hide
163 */
164 public static final int REASON_BACKGROUND_ACTIVITY_PERMISSION = 58;
165 /**
166 * START_FOREGROUND_SERVICES_FROM_BACKGROUND permission.
167 * @hide
168 */
169 public static final int REASON_BACKGROUND_FGS_PERMISSION = 59;
170 /** @hide */
171 public static final int REASON_INSTR_BACKGROUND_ACTIVITY_PERMISSION = 60;
172 /** @hide */
173 public static final int REASON_INSTR_BACKGROUND_FGS_PERMISSION = 61;
174 /** @hide */
175 public static final int REASON_SYSTEM_ALERT_WINDOW_PERMISSION = 62;
176 /** @hide */
177 public static final int REASON_DEVICE_DEMO_MODE = 63;
178 /** @hide */
179 public static final int REASON_ALLOWLISTED_PACKAGE = 65;
180 /** @hide */
181 public static final int REASON_APPOP = 66;
182 /** @hide */
183 public static final int REASON_ACTIVITY_VISIBILITY_GRACE_PERIOD = 67;
184 /** @hide */
185 public static final int REASON_OP_ACTIVATE_VPN = 68;
186 /** @hide */
187 public static final int REASON_OP_ACTIVATE_PLATFORM_VPN = 69;
188 /**
189 * Temporarily allowed to have FGS while-in-use permissions.
190 * @hide
191 */
192 public static final int REASON_TEMP_ALLOWED_WHILE_IN_USE = 70;
193 /** @hide */
194 public static final int REASON_CURRENT_INPUT_METHOD = 71;
195
196 /* BG-FGS-launch is allowed by temp-allow-list or system-allow-list.
197 Reason code for temp and system allow list starts here.
198 Reason code range 100-199 are reserved for public reasons. */
199 /**
200 * Set temp-allow-list for location geofence purpose.
201 */
202 public static final int REASON_GEOFENCING = 100;
203 /**
204 * Set temp-allow-list for server push messaging.
205 */
206 public static final int REASON_PUSH_MESSAGING = 101;
207 /**
208 * Set temp-allow-list for server push messaging over the quota.
209 */
210 public static final int REASON_PUSH_MESSAGING_OVER_QUOTA = 102;
211 /**
212 * Set temp-allow-list for activity recognition.
213 */
214 public static final int REASON_ACTIVITY_RECOGNITION = 103;
215 /**
216 * Set temp-allow-list for transferring accounts between users.
217 */
218 public static final int REASON_ACCOUNT_TRANSFER = 104;
219
220 /* Reason code range 200-299 are reserved for broadcast actions */
221 /**
222 * Broadcast ACTION_BOOT_COMPLETED.
223 * @hide
224 */
225 public static final int REASON_BOOT_COMPLETED = 200;
226 /**
227 * Broadcast ACTION_PRE_BOOT_COMPLETED.
228 * @hide
229 */
230 public static final int REASON_PRE_BOOT_COMPLETED = 201;
231 /**
232 * Broadcast ACTION_LOCKED_BOOT_COMPLETED.
233 * @hide
234 */
235 public static final int REASON_LOCKED_BOOT_COMPLETED = 202;
236 /**
237 * All Bluetooth broadcasts.
238 */
239 public static final int REASON_BLUETOOTH_BROADCAST = 203;
240 /**
241 * Broadcast {@link android.content.Intent#ACTION_TIMEZONE_CHANGED}
242 * @hide
243 */
244 public static final int REASON_TIMEZONE_CHANGED = 204;
245 /**
246 * Broadcast {@link android.content.Intent#ACTION_TIME_CHANGED}
247 * @hide
248 */
249 public static final int REASON_TIME_CHANGED = 205;
250 /**
251 * Broadcast {@link android.content.Intent#ACTION_LOCALE_CHANGED}
252 * @hide
253 */
254 public static final int REASON_LOCALE_CHANGED = 206;
255 /**
256 * Broadcast
257 * {@link android.app.AlarmManager#ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED}
258 * @hide
259 */
260 public static final int REASON_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED = 207;
261 /**
262 * Broadcast {@link android.safetycenter.SafetyCenterManager#ACTION_REFRESH_SAFETY_SOURCES}.
263 */
264 public static final int REASON_REFRESH_SAFETY_SOURCES = 208;
265
266 /* Reason code range 300-399 are reserved for other internal reasons */
267 /**
268 * Device idle system allow list, including EXCEPT-IDLE
269 * @hide
270 */
271 public static final int REASON_SYSTEM_ALLOW_LISTED = 300;
272 /** @hide */
273 public static final int REASON_ALARM_MANAGER_ALARM_CLOCK = 301;
274 /**
275 * AlarmManagerService.
276 * @hide
277 */
278 public static final int REASON_ALARM_MANAGER_WHILE_IDLE = 302;
279 /**
280 * ActiveServices.
281 * @hide
282 */
283 public static final int REASON_SERVICE_LAUNCH = 303;
284 /**
285 * KeyChainSystemService.
286 * @hide
287 */
288 public static final int REASON_KEY_CHAIN = 304;
289 /**
290 * PackageManagerService.
291 * @hide
292 */
293 public static final int REASON_PACKAGE_VERIFIER = 305;
294 /**
295 * SyncManager.
296 * @hide
297 */
298 public static final int REASON_SYNC_MANAGER = 306;
299 /**
300 * DomainVerificationProxyV1.
301 * @hide
302 */
303 public static final int REASON_DOMAIN_VERIFICATION_V1 = 307;
304 /**
305 * DomainVerificationProxyV2.
306 * @hide
307 */
308 public static final int REASON_DOMAIN_VERIFICATION_V2 = 308;
309 /** @hide */
310 public static final int REASON_VPN = 309;
311 /**
312 * NotificationManagerService.
313 * @hide
314 */
315 public static final int REASON_NOTIFICATION_SERVICE = 310;
316 /**
317 * Broadcast ACTION_MY_PACKAGE_REPLACED.
318 * @hide
319 */
320 public static final int REASON_PACKAGE_REPLACED = 311;
321 /**
322 * LocationProvider.
323 * @hide
324 */
325 @SystemApi
326 public static final int REASON_LOCATION_PROVIDER = 312;
327 /**
328 * MediaButtonReceiver.
329 * @hide
330 */
331 public static final int REASON_MEDIA_BUTTON = 313;
332 /**
333 * InboundSmsHandler.
334 * @hide
335 */
336 public static final int REASON_EVENT_SMS = 314;
337 /**
338 * InboundSmsHandler.
339 * @hide
340 */
341 public static final int REASON_EVENT_MMS = 315;
342 /**
343 * Shell app.
344 * @hide
345 */
346 public static final int REASON_SHELL = 316;
347 /**
348 * Media session callbacks.
349 * @hide
350 */
351 public static final int REASON_MEDIA_SESSION_CALLBACK = 317;
352 /**
353 * Dialer app.
354 * @hide
355 */
356 public static final int REASON_ROLE_DIALER = 318;
357 /**
358 * Emergency app.
359 * @hide
360 */
361 public static final int REASON_ROLE_EMERGENCY = 319;
362 /**
363 * System Module.
364 * @hide
365 */
366 public static final int REASON_SYSTEM_MODULE = 320;
367 /**
368 * Carrier privileged app.
369 * @hide
370 */
371 public static final int REASON_CARRIER_PRIVILEGED_APP = 321;
372 /**
373 * Device/Profile owner protected apps.
374 * @hide
375 */
376 public static final int REASON_DPO_PROTECTED_APP = 322;
377 /**
378 * Apps control is disallowed for the user.
379 * @hide
380 */
381 public static final int REASON_DISALLOW_APPS_CONTROL = 323;
382 /**
383 * Active device admin package.
384 * @hide
385 */
386 public static final int REASON_ACTIVE_DEVICE_ADMIN = 324;
387
388 /**
389 * Media notification re-generate during transferring.
390 * @hide
391 */
392 public static final int REASON_MEDIA_NOTIFICATION_TRANSFER = 325;
393
394 /** @hide The app requests out-out. */
395 public static final int REASON_OPT_OUT_REQUESTED = 1000;
396
397 /**
398 * The list of BG-FGS-Launch and temp-allow-list reason code.
399 * @hide
400 */
401 @IntDef(flag = true, prefix = { "REASON_" }, value = {
402 // BG-FGS-Launch reasons.
403 REASON_DENIED,
404 REASON_UNKNOWN,
405 REASON_OTHER,
406 REASON_PROC_STATE_PERSISTENT,
407 REASON_PROC_STATE_PERSISTENT_UI,
408 REASON_PROC_STATE_TOP,
409 REASON_PROC_STATE_BTOP,
410 REASON_PROC_STATE_FGS,
411 REASON_PROC_STATE_BFGS,
412 REASON_UID_VISIBLE,
413 REASON_SYSTEM_UID,
414 REASON_ACTIVITY_STARTER,
415 REASON_START_ACTIVITY_FLAG,
416 REASON_FGS_BINDING,
417 REASON_DEVICE_OWNER,
418 REASON_PROFILE_OWNER,
419 REASON_COMPANION_DEVICE_MANAGER,
420 REASON_BACKGROUND_ACTIVITY_PERMISSION,
421 REASON_BACKGROUND_FGS_PERMISSION,
422 REASON_INSTR_BACKGROUND_ACTIVITY_PERMISSION,
423 REASON_INSTR_BACKGROUND_FGS_PERMISSION,
424 REASON_SYSTEM_ALERT_WINDOW_PERMISSION,
425 REASON_DEVICE_DEMO_MODE,
426 REASON_ALLOWLISTED_PACKAGE,
427 REASON_APPOP,
428 REASON_ACTIVITY_VISIBILITY_GRACE_PERIOD,
429 REASON_OP_ACTIVATE_VPN,
430 REASON_OP_ACTIVATE_PLATFORM_VPN,
431 REASON_CURRENT_INPUT_METHOD,
432 REASON_TEMP_ALLOWED_WHILE_IN_USE,
433 // temp and system allow list reasons.
434 REASON_GEOFENCING,
435 REASON_PUSH_MESSAGING,
436 REASON_PUSH_MESSAGING_OVER_QUOTA,
437 REASON_ACTIVITY_RECOGNITION,
438 REASON_ACCOUNT_TRANSFER,
439 REASON_BOOT_COMPLETED,
440 REASON_PRE_BOOT_COMPLETED,
441 REASON_LOCKED_BOOT_COMPLETED,
442 REASON_BLUETOOTH_BROADCAST,
443 REASON_TIMEZONE_CHANGED,
444 REASON_TIME_CHANGED,
445 REASON_LOCALE_CHANGED,
446 REASON_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED,
447 REASON_REFRESH_SAFETY_SOURCES,
448 REASON_SYSTEM_ALLOW_LISTED,
449 REASON_ALARM_MANAGER_ALARM_CLOCK,
450 REASON_ALARM_MANAGER_WHILE_IDLE,
451 REASON_SERVICE_LAUNCH,
452 REASON_KEY_CHAIN,
453 REASON_PACKAGE_VERIFIER,
454 REASON_SYNC_MANAGER,
455 REASON_DOMAIN_VERIFICATION_V1,
456 REASON_DOMAIN_VERIFICATION_V2,
457 REASON_VPN,
458 REASON_NOTIFICATION_SERVICE,
459 REASON_PACKAGE_REPLACED,
460 REASON_LOCATION_PROVIDER,
461 REASON_MEDIA_BUTTON,
462 REASON_EVENT_SMS,
463 REASON_EVENT_MMS,
464 REASON_SHELL,
465 REASON_MEDIA_SESSION_CALLBACK,
466 REASON_ROLE_DIALER,
467 REASON_ROLE_EMERGENCY,
468 REASON_SYSTEM_MODULE,
469 REASON_CARRIER_PRIVILEGED_APP,
470 REASON_OPT_OUT_REQUESTED,
471 REASON_DPO_PROTECTED_APP,
472 REASON_DISALLOW_APPS_CONTROL,
473 REASON_ACTIVE_DEVICE_ADMIN,
474 REASON_MEDIA_NOTIFICATION_TRANSFER,
475 })
476 @Retention(RetentionPolicy.SOURCE)
477 public @interface ReasonCode {}
478
479 /**
480 * @hide
481 */
482 public PowerExemptionManager(@NonNull Context context) {
483 mContext = context;
484 mService = context.getSystemService(DeviceIdleManager.class).getService();
485 }
486
487 /**
488 * Add the specified package to the permanent power save allow list.
489 *
490 * @hide
491 */
492 @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
493 public void addToPermanentAllowList(@NonNull String packageName) {
494 addToPermanentAllowList(Collections.singletonList(packageName));
495 }
496
497 /**
498 * Add the specified packages to the permanent power save allow list.
499 *
500 * @hide
501 */
502 @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
503 public void addToPermanentAllowList(@NonNull List<String> packageNames) {
504 try {
505 mService.addPowerSaveWhitelistApps(packageNames);
506 } catch (RemoteException e) {
507 throw e.rethrowFromSystemServer();
508 }
509 }
510
511 /**
512 * Get a list of app IDs of app that are allow-listed. This does not include temporarily
513 * allow-listed apps.
514 *
515 * @param includingIdle Set to true if the app should be allow-listed from device idle as well
516 * as other power save restrictions
517 * @hide
518 */
519 @NonNull
520 @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
521 public int[] getAllowListedAppIds(boolean includingIdle) {
522 try {
523 if (includingIdle) {
524 return mService.getAppIdWhitelist();
525 } else {
526 return mService.getAppIdWhitelistExceptIdle();
527 }
528 } catch (RemoteException e) {
529 throw e.rethrowFromSystemServer();
530 }
531 }
532
533 /**
534 * Returns true if the app is allow-listed from power save restrictions. This does not include
535 * temporarily allow-listed apps.
536 *
537 * @param includingIdle Set to true if the app should be allow-listed from device
538 * idle as well as other power save restrictions
539 * @hide
540 */
541 public boolean isAllowListed(@NonNull String packageName, boolean includingIdle) {
542 try {
543 if (includingIdle) {
544 return mService.isPowerSaveWhitelistApp(packageName);
545 } else {
546 return mService.isPowerSaveWhitelistExceptIdleApp(packageName);
547 }
548 } catch (RemoteException e) {
549 throw e.rethrowFromSystemServer();
550 }
551 }
552
553 /**
554 * Remove an app from the permanent power save allow list. Only apps that were added via
555 * {@link #addToPermanentAllowList(String)} or {@link #addToPermanentAllowList(List)} will be
556 * removed. Apps allow-listed by default by the system cannot be removed.
557 *
558 * @param packageName The app to remove from the allow list
559 * @hide
560 */
561 @RequiresPermission(android.Manifest.permission.DEVICE_POWER)
562 public void removeFromPermanentAllowList(@NonNull String packageName) {
563 try {
564 mService.removePowerSaveWhitelistApp(packageName);
565 } catch (RemoteException e) {
566 throw e.rethrowFromSystemServer();
567 }
568 }
569
570 /**
571 * Add an app to the temporary allow list for a short amount of time.
572 *
573 * @param packageName The package to add to the temp allow list
574 * @param durationMs How long to keep the app on the temp allow list for (in milliseconds)
575 * @param reasonCode one of {@link ReasonCode}, use {@link #REASON_UNKNOWN} if not sure.
576 * @param reason a optional human readable reason string, could be null or empty string.
577 */
578 @UserHandleAware
579 @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST)
580 public void addToTemporaryAllowList(@NonNull String packageName, @ReasonCode int reasonCode,
581 @Nullable String reason, long durationMs) {
582 try {
583 mService.addPowerSaveTempWhitelistApp(packageName, durationMs, mContext.getUserId(),
584 reasonCode, reason);
585 } catch (RemoteException e) {
586 throw e.rethrowFromSystemServer();
587 }
588 }
589
590 /**
591 * Add an app to the temporary allow list for a short amount of time for a specific reason.
592 * The temporary allow list is kept separately from the permanent allow list and apps are
593 * automatically removed from the temporary allow list after a predetermined amount of time.
594 *
595 * @param packageName The package to add to the temp allow list
596 * @param event The reason to add the app to the temp allow list
597 * @param reasonCode one of {@link ReasonCode}, use {@link #REASON_UNKNOWN} if not sure.
598 * @param reason A human-readable reason explaining why the app is temp allow-listed. Only
599 * used for logging purposes. Could be null or empty string.
600 * @return The duration (in milliseconds) that the app is allow-listed for
601 */
602 @UserHandleAware
603 @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST)
604 public long addToTemporaryAllowListForEvent(@NonNull String packageName,
605 @ReasonCode int reasonCode, @Nullable String reason, @AllowListEvent int event) {
606 try {
607 switch (event) {
608 case EVENT_MMS:
609 return mService.addPowerSaveTempWhitelistAppForMms(
610 packageName, mContext.getUserId(), reasonCode, reason);
611 case EVENT_SMS:
612 return mService.addPowerSaveTempWhitelistAppForSms(
613 packageName, mContext.getUserId(), reasonCode, reason);
614 case EVENT_UNSPECIFIED:
615 default:
616 return mService.whitelistAppTemporarily(
617 packageName, mContext.getUserId(), reasonCode, reason);
618 }
619 } catch (RemoteException e) {
620 throw e.rethrowFromSystemServer();
621 }
622 }
623
624 /**
625 * @hide
626 */
627 public static @ReasonCode int getReasonCodeFromProcState(int procState) {
628 if (procState <= PROCESS_STATE_PERSISTENT) {
629 return REASON_PROC_STATE_PERSISTENT;
630 } else if (procState <= PROCESS_STATE_PERSISTENT_UI) {
631 return REASON_PROC_STATE_PERSISTENT_UI;
632 } else if (procState <= PROCESS_STATE_TOP) {
633 return REASON_PROC_STATE_TOP;
634 } else if (procState <= PROCESS_STATE_BOUND_TOP) {
635 return REASON_PROC_STATE_BTOP;
636 } else if (procState <= PROCESS_STATE_FOREGROUND_SERVICE) {
637 return REASON_PROC_STATE_FGS;
638 } else if (procState <= PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
639 return REASON_PROC_STATE_BFGS;
640 } else {
641 return REASON_DENIED;
642 }
643 }
644
645 /**
646 * @hide
647 * @return the reason code mapped to statsd for the AppBackgroundRestrictionsInfo atom.
648 */
649 public static int getExemptionReasonForStatsd(@ReasonCode int reasonCode) {
650 switch (reasonCode) {
651 case REASON_SYSTEM_UID:
652 return AppBackgroundRestrictionsInfo.REASON_SYSTEM_UID;
653 case REASON_ALLOWLISTED_PACKAGE:
654 return AppBackgroundRestrictionsInfo.REASON_ALLOWLISTED_PACKAGE;
655 case REASON_COMPANION_DEVICE_MANAGER:
656 return AppBackgroundRestrictionsInfo.REASON_COMPANION_DEVICE_MANAGER;
657 case REASON_DEVICE_DEMO_MODE:
658 return AppBackgroundRestrictionsInfo.REASON_DEVICE_DEMO_MODE;
659 case REASON_DEVICE_OWNER:
660 return AppBackgroundRestrictionsInfo.REASON_DEVICE_OWNER;
661 case REASON_PROFILE_OWNER:
662 return AppBackgroundRestrictionsInfo.REASON_PROFILE_OWNER;
663 case REASON_PROC_STATE_PERSISTENT:
664 return AppBackgroundRestrictionsInfo.REASON_PROC_STATE_PERSISTENT;
665 case REASON_PROC_STATE_PERSISTENT_UI:
666 return AppBackgroundRestrictionsInfo.REASON_PROC_STATE_PERSISTENT_UI;
667 case REASON_OP_ACTIVATE_VPN:
668 return AppBackgroundRestrictionsInfo.REASON_OP_ACTIVATE_VPN;
669 case REASON_OP_ACTIVATE_PLATFORM_VPN:
670 return AppBackgroundRestrictionsInfo.REASON_OP_ACTIVATE_PLATFORM_VPN;
671 case REASON_SYSTEM_MODULE:
672 return AppBackgroundRestrictionsInfo.REASON_SYSTEM_MODULE;
673 case REASON_CARRIER_PRIVILEGED_APP:
674 return AppBackgroundRestrictionsInfo.REASON_CARRIER_PRIVILEGED_APP;
675 case REASON_SYSTEM_ALLOW_LISTED:
676 return AppBackgroundRestrictionsInfo.REASON_SYSTEM_ALLOW_LISTED;
677 case REASON_ROLE_DIALER:
678 return AppBackgroundRestrictionsInfo.REASON_ROLE_DIALER;
679 case REASON_ROLE_EMERGENCY:
680 return AppBackgroundRestrictionsInfo.REASON_ROLE_EMERGENCY;
681 case REASON_DPO_PROTECTED_APP:
682 return AppBackgroundRestrictionsInfo.REASON_DPO_PROTECTED_APP;
683 case REASON_DISALLOW_APPS_CONTROL:
684 return AppBackgroundRestrictionsInfo.REASON_DISALLOW_APPS_CONTROL;
685 case REASON_ACTIVE_DEVICE_ADMIN:
686 return AppBackgroundRestrictionsInfo.REASON_ACTIVE_DEVICE_ADMIN;
687 default:
688 return AppBackgroundRestrictionsInfo.REASON_DENIED;
689 }
690 }
691
692 /**
693 * Return string name of the integer reason code.
694 * @hide
695 * @param reasonCode
696 * @return string name of the reason code.
697 */
698 public static String reasonCodeToString(@ReasonCode int reasonCode) {
699 switch (reasonCode) {
700 case REASON_DENIED:
701 return "DENIED";
702 case REASON_UNKNOWN:
703 return "UNKNOWN";
704 case REASON_OTHER:
705 return "OTHER";
706 case REASON_PROC_STATE_PERSISTENT:
707 return "PROC_STATE_PERSISTENT";
708 case REASON_PROC_STATE_PERSISTENT_UI:
709 return "PROC_STATE_PERSISTENT_UI";
710 case REASON_PROC_STATE_TOP:
711 return "PROC_STATE_TOP";
712 case REASON_PROC_STATE_BTOP:
713 return "PROC_STATE_BTOP";
714 case REASON_PROC_STATE_FGS:
715 return "PROC_STATE_FGS";
716 case REASON_PROC_STATE_BFGS:
717 return "PROC_STATE_BFGS";
718 case REASON_UID_VISIBLE:
719 return "UID_VISIBLE";
720 case REASON_SYSTEM_UID:
721 return "SYSTEM_UID";
722 case REASON_ACTIVITY_STARTER:
723 return "ACTIVITY_STARTER";
724 case REASON_START_ACTIVITY_FLAG:
725 return "START_ACTIVITY_FLAG";
726 case REASON_FGS_BINDING:
727 return "FGS_BINDING";
728 case REASON_DEVICE_OWNER:
729 return "DEVICE_OWNER";
730 case REASON_PROFILE_OWNER:
731 return "PROFILE_OWNER";
732 case REASON_COMPANION_DEVICE_MANAGER:
733 return "COMPANION_DEVICE_MANAGER";
734 case REASON_BACKGROUND_ACTIVITY_PERMISSION:
735 return "BACKGROUND_ACTIVITY_PERMISSION";
736 case REASON_BACKGROUND_FGS_PERMISSION:
737 return "BACKGROUND_FGS_PERMISSION";
738 case REASON_INSTR_BACKGROUND_ACTIVITY_PERMISSION:
739 return "INSTR_BACKGROUND_ACTIVITY_PERMISSION";
740 case REASON_INSTR_BACKGROUND_FGS_PERMISSION:
741 return "INSTR_BACKGROUND_FGS_PERMISSION";
742 case REASON_SYSTEM_ALERT_WINDOW_PERMISSION:
743 return "SYSTEM_ALERT_WINDOW_PERMISSION";
744 case REASON_DEVICE_DEMO_MODE:
745 return "DEVICE_DEMO_MODE";
746 case REASON_ALLOWLISTED_PACKAGE:
747 return "ALLOWLISTED_PACKAGE";
748 case REASON_APPOP:
749 return "APPOP";
750 case REASON_ACTIVITY_VISIBILITY_GRACE_PERIOD:
751 return "ACTIVITY_VISIBILITY_GRACE_PERIOD";
752 case REASON_OP_ACTIVATE_VPN:
753 return "OP_ACTIVATE_VPN";
754 case REASON_OP_ACTIVATE_PLATFORM_VPN:
755 return "OP_ACTIVATE_PLATFORM_VPN";
756 case REASON_CURRENT_INPUT_METHOD:
757 return "CURRENT_INPUT_METHOD";
758 case REASON_TEMP_ALLOWED_WHILE_IN_USE:
759 return "TEMP_ALLOWED_WHILE_IN_USE";
760 case REASON_GEOFENCING:
761 return "GEOFENCING";
762 case REASON_PUSH_MESSAGING:
763 return "PUSH_MESSAGING";
764 case REASON_PUSH_MESSAGING_OVER_QUOTA:
765 return "PUSH_MESSAGING_OVER_QUOTA";
766 case REASON_ACTIVITY_RECOGNITION:
767 return "ACTIVITY_RECOGNITION";
768 case REASON_ACCOUNT_TRANSFER:
769 return "REASON_ACCOUNT_TRANSFER";
770 case REASON_BOOT_COMPLETED:
771 return "BOOT_COMPLETED";
772 case REASON_PRE_BOOT_COMPLETED:
773 return "PRE_BOOT_COMPLETED";
774 case REASON_LOCKED_BOOT_COMPLETED:
775 return "LOCKED_BOOT_COMPLETED";
776 case REASON_BLUETOOTH_BROADCAST:
777 return "BLUETOOTH_BROADCAST";
778 case REASON_TIMEZONE_CHANGED:
779 return "TIMEZONE_CHANGED";
780 case REASON_TIME_CHANGED:
781 return "TIME_CHANGED";
782 case REASON_LOCALE_CHANGED:
783 return "LOCALE_CHANGED";
784 case REASON_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED:
785 return "REASON_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED";
786 case REASON_REFRESH_SAFETY_SOURCES:
787 return "REASON_REFRESH_SAFETY_SOURCES";
788 case REASON_SYSTEM_ALLOW_LISTED:
789 return "SYSTEM_ALLOW_LISTED";
790 case REASON_ALARM_MANAGER_ALARM_CLOCK:
791 return "ALARM_MANAGER_ALARM_CLOCK";
792 case REASON_ALARM_MANAGER_WHILE_IDLE:
793 return "ALARM_MANAGER_WHILE_IDLE";
794 case REASON_SERVICE_LAUNCH:
795 return "SERVICE_LAUNCH";
796 case REASON_KEY_CHAIN:
797 return "KEY_CHAIN";
798 case REASON_PACKAGE_VERIFIER:
799 return "PACKAGE_VERIFIER";
800 case REASON_SYNC_MANAGER:
801 return "SYNC_MANAGER";
802 case REASON_DOMAIN_VERIFICATION_V1:
803 return "DOMAIN_VERIFICATION_V1";
804 case REASON_DOMAIN_VERIFICATION_V2:
805 return "DOMAIN_VERIFICATION_V2";
806 case REASON_VPN:
807 return "VPN";
808 case REASON_NOTIFICATION_SERVICE:
809 return "NOTIFICATION_SERVICE";
810 case REASON_PACKAGE_REPLACED:
811 return "PACKAGE_REPLACED";
812 case REASON_LOCATION_PROVIDER:
813 return "LOCATION_PROVIDER";
814 case REASON_MEDIA_BUTTON:
815 return "MEDIA_BUTTON";
816 case REASON_EVENT_SMS:
817 return "EVENT_SMS";
818 case REASON_EVENT_MMS:
819 return "EVENT_MMS";
820 case REASON_SHELL:
821 return "SHELL";
822 case REASON_MEDIA_SESSION_CALLBACK:
823 return "MEDIA_SESSION_CALLBACK";
824 case REASON_ROLE_DIALER:
825 return "ROLE_DIALER";
826 case REASON_ROLE_EMERGENCY:
827 return "ROLE_EMERGENCY";
828 case REASON_SYSTEM_MODULE:
829 return "SYSTEM_MODULE";
830 case REASON_CARRIER_PRIVILEGED_APP:
831 return "CARRIER_PRIVILEGED_APP";
832 case REASON_DPO_PROTECTED_APP:
833 return "DPO_PROTECTED_APP";
834 case REASON_DISALLOW_APPS_CONTROL:
835 return "DISALLOW_APPS_CONTROL";
836 case REASON_ACTIVE_DEVICE_ADMIN:
837 return "ACTIVE_DEVICE_ADMIN";
838 case REASON_OPT_OUT_REQUESTED:
839 return "REASON_OPT_OUT_REQUESTED";
840 case REASON_MEDIA_NOTIFICATION_TRANSFER:
841 return "REASON_MEDIA_NOTIFICATION_TRANSFER";
842 default:
843 return "(unknown:" + reasonCode + ")";
844 }
845 }
846 }