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