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 /** 423 * Granted by {@link com.android.server.pm.PackageArchiver} to the installer responsible 424 * for unarchiving an app. 425 * 426 * @hide 427 */ 428 public static final int REASON_PACKAGE_UNARCHIVE = 328; 429 430 /** 431 * Tile onClick event 432 * @hide 433 */ 434 public static final int REASON_TILE_ONCLICK = 329; 435 436 /** @hide The app requests out-out. */ 437 public static final int REASON_OPT_OUT_REQUESTED = 1000; 438 439 /** 440 * The list of BG-FGS-Launch and temp-allow-list reason code. 441 * @hide 442 */ 443 @IntDef(flag = true, prefix = { "REASON_" }, value = { 444 // BG-FGS-Launch reasons. 445 REASON_DENIED, 446 REASON_UNKNOWN, 447 REASON_OTHER, 448 REASON_PROC_STATE_PERSISTENT, 449 REASON_PROC_STATE_PERSISTENT_UI, 450 REASON_PROC_STATE_TOP, 451 REASON_PROC_STATE_BTOP, 452 REASON_PROC_STATE_FGS, 453 REASON_PROC_STATE_BFGS, 454 REASON_UID_VISIBLE, 455 REASON_SYSTEM_UID, 456 REASON_ACTIVITY_STARTER, 457 REASON_START_ACTIVITY_FLAG, 458 REASON_FGS_BINDING, 459 REASON_DEVICE_OWNER, 460 REASON_PROFILE_OWNER, 461 REASON_COMPANION_DEVICE_MANAGER, 462 REASON_BACKGROUND_ACTIVITY_PERMISSION, 463 REASON_BACKGROUND_FGS_PERMISSION, 464 REASON_INSTR_BACKGROUND_ACTIVITY_PERMISSION, 465 REASON_INSTR_BACKGROUND_FGS_PERMISSION, 466 REASON_SYSTEM_ALERT_WINDOW_PERMISSION, 467 REASON_DEVICE_DEMO_MODE, 468 REASON_ALLOWLISTED_PACKAGE, 469 REASON_APPOP, 470 REASON_ACTIVITY_VISIBILITY_GRACE_PERIOD, 471 REASON_OP_ACTIVATE_VPN, 472 REASON_OP_ACTIVATE_PLATFORM_VPN, 473 REASON_CURRENT_INPUT_METHOD, 474 REASON_TEMP_ALLOWED_WHILE_IN_USE, 475 // temp and system allow list reasons. 476 REASON_GEOFENCING, 477 REASON_PUSH_MESSAGING, 478 REASON_PUSH_MESSAGING_OVER_QUOTA, 479 REASON_ACTIVITY_RECOGNITION, 480 REASON_ACCOUNT_TRANSFER, 481 REASON_PUSH_MESSAGING_DEFERRABLE, 482 REASON_BOOT_COMPLETED, 483 REASON_PRE_BOOT_COMPLETED, 484 REASON_LOCKED_BOOT_COMPLETED, 485 REASON_BLUETOOTH_BROADCAST, 486 REASON_TIMEZONE_CHANGED, 487 REASON_TIME_CHANGED, 488 REASON_LOCALE_CHANGED, 489 REASON_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED, 490 REASON_REFRESH_SAFETY_SOURCES, 491 REASON_SYSTEM_ALLOW_LISTED, 492 REASON_ALARM_MANAGER_ALARM_CLOCK, 493 REASON_ALARM_MANAGER_WHILE_IDLE, 494 REASON_SERVICE_LAUNCH, 495 REASON_KEY_CHAIN, 496 REASON_PACKAGE_VERIFIER, 497 REASON_SYNC_MANAGER, 498 REASON_DOMAIN_VERIFICATION_V1, 499 REASON_DOMAIN_VERIFICATION_V2, 500 REASON_VPN, 501 REASON_NOTIFICATION_SERVICE, 502 REASON_PACKAGE_REPLACED, 503 REASON_LOCATION_PROVIDER, 504 REASON_MEDIA_BUTTON, 505 REASON_EVENT_SMS, 506 REASON_EVENT_MMS, 507 REASON_SHELL, 508 REASON_MEDIA_SESSION_CALLBACK, 509 REASON_ROLE_DIALER, 510 REASON_ROLE_EMERGENCY, 511 REASON_SYSTEM_MODULE, 512 REASON_CARRIER_PRIVILEGED_APP, 513 REASON_DPO_PROTECTED_APP, 514 REASON_DISALLOW_APPS_CONTROL, 515 REASON_ACTIVE_DEVICE_ADMIN, 516 REASON_MEDIA_NOTIFICATION_TRANSFER, 517 REASON_PACKAGE_INSTALLER, 518 REASON_SYSTEM_EXEMPT_APP_OP, 519 REASON_PACKAGE_UNARCHIVE, 520 REASON_TILE_ONCLICK, 521 REASON_OPT_OUT_REQUESTED, 522 }) 523 @Retention(RetentionPolicy.SOURCE) 524 public @interface ReasonCode {} 525 526 /** 527 * @hide 528 */ PowerExemptionManager(@onNull Context context)529 public PowerExemptionManager(@NonNull Context context) { 530 mContext = context; 531 mService = context.getSystemService(DeviceIdleManager.class).getService(); 532 } 533 534 /** 535 * Add the specified package to the permanent power save allow list. 536 * 537 * @hide 538 */ 539 @RequiresPermission(android.Manifest.permission.DEVICE_POWER) addToPermanentAllowList(@onNull String packageName)540 public void addToPermanentAllowList(@NonNull String packageName) { 541 addToPermanentAllowList(Collections.singletonList(packageName)); 542 } 543 544 /** 545 * Add the specified packages to the permanent power save allow list. 546 * 547 * @hide 548 */ 549 @RequiresPermission(android.Manifest.permission.DEVICE_POWER) addToPermanentAllowList(@onNull List<String> packageNames)550 public void addToPermanentAllowList(@NonNull List<String> packageNames) { 551 try { 552 mService.addPowerSaveWhitelistApps(packageNames); 553 } catch (RemoteException e) { 554 throw e.rethrowFromSystemServer(); 555 } 556 } 557 558 /** 559 * Get a list of app IDs of app that are allow-listed. This does not include temporarily 560 * allow-listed apps. 561 * 562 * @param includingIdle Set to true if the app should be allow-listed from device idle as well 563 * as other power save restrictions 564 * @hide 565 */ 566 @NonNull 567 @RequiresPermission(android.Manifest.permission.DEVICE_POWER) getAllowListedAppIds(boolean includingIdle)568 public int[] getAllowListedAppIds(boolean includingIdle) { 569 try { 570 if (includingIdle) { 571 return mService.getAppIdWhitelist(); 572 } else { 573 return mService.getAppIdWhitelistExceptIdle(); 574 } 575 } catch (RemoteException e) { 576 throw e.rethrowFromSystemServer(); 577 } 578 } 579 580 /** 581 * Returns true if the app is allow-listed from power save restrictions. This does not include 582 * temporarily allow-listed apps. 583 * 584 * @param includingIdle Set to true if the app should be allow-listed from device 585 * idle as well as other power save restrictions 586 * @hide 587 */ isAllowListed(@onNull String packageName, boolean includingIdle)588 public boolean isAllowListed(@NonNull String packageName, boolean includingIdle) { 589 try { 590 if (includingIdle) { 591 return mService.isPowerSaveWhitelistApp(packageName); 592 } else { 593 return mService.isPowerSaveWhitelistExceptIdleApp(packageName); 594 } 595 } catch (RemoteException e) { 596 throw e.rethrowFromSystemServer(); 597 } 598 } 599 600 /** 601 * Remove an app from the permanent power save allow list. Only apps that were added via 602 * {@link #addToPermanentAllowList(String)} or {@link #addToPermanentAllowList(List)} will be 603 * removed. Apps allow-listed by default by the system cannot be removed. 604 * 605 * @param packageName The app to remove from the allow list 606 * @hide 607 */ 608 @RequiresPermission(android.Manifest.permission.DEVICE_POWER) removeFromPermanentAllowList(@onNull String packageName)609 public void removeFromPermanentAllowList(@NonNull String packageName) { 610 try { 611 mService.removePowerSaveWhitelistApp(packageName); 612 } catch (RemoteException e) { 613 throw e.rethrowFromSystemServer(); 614 } 615 } 616 617 /** 618 * Add an app to the temporary allow list for a short amount of time. 619 * 620 * @param packageName The package to add to the temp allow list 621 * @param durationMs How long to keep the app on the temp allow list for (in milliseconds) 622 * @param reasonCode one of {@link ReasonCode}, use {@link #REASON_UNKNOWN} if not sure. 623 * @param reason a optional human readable reason string, could be null or empty string. 624 */ 625 @UserHandleAware 626 @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) addToTemporaryAllowList(@onNull String packageName, @ReasonCode int reasonCode, @Nullable String reason, long durationMs)627 public void addToTemporaryAllowList(@NonNull String packageName, @ReasonCode int reasonCode, 628 @Nullable String reason, long durationMs) { 629 try { 630 mService.addPowerSaveTempWhitelistApp(packageName, durationMs, mContext.getUserId(), 631 reasonCode, reason); 632 } catch (RemoteException e) { 633 throw e.rethrowFromSystemServer(); 634 } 635 } 636 637 /** 638 * Add an app to the temporary allow list for a short amount of time for a specific reason. 639 * The temporary allow list is kept separately from the permanent allow list and apps are 640 * automatically removed from the temporary allow list after a predetermined amount of time. 641 * 642 * @param packageName The package to add to the temp allow list 643 * @param event The reason to add the app to the temp allow list 644 * @param reasonCode one of {@link ReasonCode}, use {@link #REASON_UNKNOWN} if not sure. 645 * @param reason A human-readable reason explaining why the app is temp allow-listed. Only 646 * used for logging purposes. Could be null or empty string. 647 * @return The duration (in milliseconds) that the app is allow-listed for 648 */ 649 @UserHandleAware 650 @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) addToTemporaryAllowListForEvent(@onNull String packageName, @ReasonCode int reasonCode, @Nullable String reason, @AllowListEvent int event)651 public long addToTemporaryAllowListForEvent(@NonNull String packageName, 652 @ReasonCode int reasonCode, @Nullable String reason, @AllowListEvent int event) { 653 try { 654 switch (event) { 655 case EVENT_MMS: 656 return mService.addPowerSaveTempWhitelistAppForMms( 657 packageName, mContext.getUserId(), reasonCode, reason); 658 case EVENT_SMS: 659 return mService.addPowerSaveTempWhitelistAppForSms( 660 packageName, mContext.getUserId(), reasonCode, reason); 661 case EVENT_UNSPECIFIED: 662 default: 663 return mService.whitelistAppTemporarily( 664 packageName, mContext.getUserId(), reasonCode, reason); 665 } 666 } catch (RemoteException e) { 667 throw e.rethrowFromSystemServer(); 668 } 669 } 670 671 /** 672 * @hide 673 */ getReasonCodeFromProcState(int procState)674 public static @ReasonCode int getReasonCodeFromProcState(int procState) { 675 if (procState <= PROCESS_STATE_PERSISTENT) { 676 return REASON_PROC_STATE_PERSISTENT; 677 } else if (procState <= PROCESS_STATE_PERSISTENT_UI) { 678 return REASON_PROC_STATE_PERSISTENT_UI; 679 } else if (procState <= PROCESS_STATE_TOP) { 680 return REASON_PROC_STATE_TOP; 681 } else if (procState <= PROCESS_STATE_BOUND_TOP) { 682 return REASON_PROC_STATE_BTOP; 683 } else if (procState <= PROCESS_STATE_FOREGROUND_SERVICE) { 684 return REASON_PROC_STATE_FGS; 685 } else if (procState <= PROCESS_STATE_BOUND_FOREGROUND_SERVICE) { 686 return REASON_PROC_STATE_BFGS; 687 } else { 688 return REASON_DENIED; 689 } 690 } 691 692 /** 693 * @hide 694 * @return the reason code mapped to statsd for the AppBackgroundRestrictionsInfo atom. 695 */ getExemptionReasonForStatsd(@easonCode int reasonCode)696 public static int getExemptionReasonForStatsd(@ReasonCode int reasonCode) { 697 switch (reasonCode) { 698 case REASON_SYSTEM_UID: 699 return AppBackgroundRestrictionsInfo.REASON_SYSTEM_UID; 700 case REASON_ALLOWLISTED_PACKAGE: 701 return AppBackgroundRestrictionsInfo.REASON_ALLOWLISTED_PACKAGE; 702 case REASON_COMPANION_DEVICE_MANAGER: 703 return AppBackgroundRestrictionsInfo.REASON_COMPANION_DEVICE_MANAGER; 704 case REASON_DEVICE_DEMO_MODE: 705 return AppBackgroundRestrictionsInfo.REASON_DEVICE_DEMO_MODE; 706 case REASON_DEVICE_OWNER: 707 return AppBackgroundRestrictionsInfo.REASON_DEVICE_OWNER; 708 case REASON_PROFILE_OWNER: 709 return AppBackgroundRestrictionsInfo.REASON_PROFILE_OWNER; 710 case REASON_PROC_STATE_PERSISTENT: 711 return AppBackgroundRestrictionsInfo.REASON_PROC_STATE_PERSISTENT; 712 case REASON_PROC_STATE_PERSISTENT_UI: 713 return AppBackgroundRestrictionsInfo.REASON_PROC_STATE_PERSISTENT_UI; 714 case REASON_OP_ACTIVATE_VPN: 715 return AppBackgroundRestrictionsInfo.REASON_OP_ACTIVATE_VPN; 716 case REASON_OP_ACTIVATE_PLATFORM_VPN: 717 return AppBackgroundRestrictionsInfo.REASON_OP_ACTIVATE_PLATFORM_VPN; 718 case REASON_SYSTEM_MODULE: 719 return AppBackgroundRestrictionsInfo.REASON_SYSTEM_MODULE; 720 case REASON_CARRIER_PRIVILEGED_APP: 721 return AppBackgroundRestrictionsInfo.REASON_CARRIER_PRIVILEGED_APP; 722 case REASON_SYSTEM_ALLOW_LISTED: 723 return AppBackgroundRestrictionsInfo.REASON_SYSTEM_ALLOW_LISTED; 724 case REASON_ROLE_DIALER: 725 return AppBackgroundRestrictionsInfo.REASON_ROLE_DIALER; 726 case REASON_ROLE_EMERGENCY: 727 return AppBackgroundRestrictionsInfo.REASON_ROLE_EMERGENCY; 728 case REASON_DPO_PROTECTED_APP: 729 return AppBackgroundRestrictionsInfo.REASON_DPO_PROTECTED_APP; 730 case REASON_DISALLOW_APPS_CONTROL: 731 return AppBackgroundRestrictionsInfo.REASON_DISALLOW_APPS_CONTROL; 732 case REASON_ACTIVE_DEVICE_ADMIN: 733 return AppBackgroundRestrictionsInfo.REASON_ACTIVE_DEVICE_ADMIN; 734 default: 735 return AppBackgroundRestrictionsInfo.REASON_DENIED; 736 } 737 } 738 739 /** 740 * Return string name of the integer reason code. 741 * @hide 742 * @param reasonCode 743 * @return string name of the reason code. 744 */ reasonCodeToString(@easonCode int reasonCode)745 public static String reasonCodeToString(@ReasonCode int reasonCode) { 746 switch (reasonCode) { 747 case REASON_DENIED: 748 return "DENIED"; 749 case REASON_UNKNOWN: 750 return "UNKNOWN"; 751 case REASON_OTHER: 752 return "OTHER"; 753 case REASON_PROC_STATE_PERSISTENT: 754 return "PROC_STATE_PERSISTENT"; 755 case REASON_PROC_STATE_PERSISTENT_UI: 756 return "PROC_STATE_PERSISTENT_UI"; 757 case REASON_PROC_STATE_TOP: 758 return "PROC_STATE_TOP"; 759 case REASON_PROC_STATE_BTOP: 760 return "PROC_STATE_BTOP"; 761 case REASON_PROC_STATE_FGS: 762 return "PROC_STATE_FGS"; 763 case REASON_PROC_STATE_BFGS: 764 return "PROC_STATE_BFGS"; 765 case REASON_UID_VISIBLE: 766 return "UID_VISIBLE"; 767 case REASON_SYSTEM_UID: 768 return "SYSTEM_UID"; 769 case REASON_ACTIVITY_STARTER: 770 return "ACTIVITY_STARTER"; 771 case REASON_START_ACTIVITY_FLAG: 772 return "START_ACTIVITY_FLAG"; 773 case REASON_FGS_BINDING: 774 return "FGS_BINDING"; 775 case REASON_DEVICE_OWNER: 776 return "DEVICE_OWNER"; 777 case REASON_PROFILE_OWNER: 778 return "PROFILE_OWNER"; 779 case REASON_COMPANION_DEVICE_MANAGER: 780 return "COMPANION_DEVICE_MANAGER"; 781 case REASON_BACKGROUND_ACTIVITY_PERMISSION: 782 return "BACKGROUND_ACTIVITY_PERMISSION"; 783 case REASON_BACKGROUND_FGS_PERMISSION: 784 return "BACKGROUND_FGS_PERMISSION"; 785 case REASON_INSTR_BACKGROUND_ACTIVITY_PERMISSION: 786 return "INSTR_BACKGROUND_ACTIVITY_PERMISSION"; 787 case REASON_INSTR_BACKGROUND_FGS_PERMISSION: 788 return "INSTR_BACKGROUND_FGS_PERMISSION"; 789 case REASON_SYSTEM_ALERT_WINDOW_PERMISSION: 790 return "SYSTEM_ALERT_WINDOW_PERMISSION"; 791 case REASON_DEVICE_DEMO_MODE: 792 return "DEVICE_DEMO_MODE"; 793 case REASON_ALLOWLISTED_PACKAGE: 794 return "ALLOWLISTED_PACKAGE"; 795 case REASON_APPOP: 796 return "APPOP"; 797 case REASON_ACTIVITY_VISIBILITY_GRACE_PERIOD: 798 return "ACTIVITY_VISIBILITY_GRACE_PERIOD"; 799 case REASON_OP_ACTIVATE_VPN: 800 return "OP_ACTIVATE_VPN"; 801 case REASON_OP_ACTIVATE_PLATFORM_VPN: 802 return "OP_ACTIVATE_PLATFORM_VPN"; 803 case REASON_CURRENT_INPUT_METHOD: 804 return "CURRENT_INPUT_METHOD"; 805 case REASON_TEMP_ALLOWED_WHILE_IN_USE: 806 return "TEMP_ALLOWED_WHILE_IN_USE"; 807 case REASON_GEOFENCING: 808 return "GEOFENCING"; 809 case REASON_PUSH_MESSAGING: 810 return "PUSH_MESSAGING"; 811 case REASON_PUSH_MESSAGING_OVER_QUOTA: 812 return "PUSH_MESSAGING_OVER_QUOTA"; 813 case REASON_ACTIVITY_RECOGNITION: 814 return "ACTIVITY_RECOGNITION"; 815 case REASON_ACCOUNT_TRANSFER: 816 return "REASON_ACCOUNT_TRANSFER"; 817 case REASON_PUSH_MESSAGING_DEFERRABLE: 818 return "PUSH_MESSAGING_DEFERRABLE"; 819 case REASON_BOOT_COMPLETED: 820 return "BOOT_COMPLETED"; 821 case REASON_PRE_BOOT_COMPLETED: 822 return "PRE_BOOT_COMPLETED"; 823 case REASON_LOCKED_BOOT_COMPLETED: 824 return "LOCKED_BOOT_COMPLETED"; 825 case REASON_BLUETOOTH_BROADCAST: 826 return "BLUETOOTH_BROADCAST"; 827 case REASON_TIMEZONE_CHANGED: 828 return "TIMEZONE_CHANGED"; 829 case REASON_TIME_CHANGED: 830 return "TIME_CHANGED"; 831 case REASON_LOCALE_CHANGED: 832 return "LOCALE_CHANGED"; 833 case REASON_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED: 834 return "REASON_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED"; 835 case REASON_REFRESH_SAFETY_SOURCES: 836 return "REASON_REFRESH_SAFETY_SOURCES"; 837 case REASON_SYSTEM_ALLOW_LISTED: 838 return "SYSTEM_ALLOW_LISTED"; 839 case REASON_ALARM_MANAGER_ALARM_CLOCK: 840 return "ALARM_MANAGER_ALARM_CLOCK"; 841 case REASON_ALARM_MANAGER_WHILE_IDLE: 842 return "ALARM_MANAGER_WHILE_IDLE"; 843 case REASON_SERVICE_LAUNCH: 844 return "SERVICE_LAUNCH"; 845 case REASON_KEY_CHAIN: 846 return "KEY_CHAIN"; 847 case REASON_PACKAGE_VERIFIER: 848 return "PACKAGE_VERIFIER"; 849 case REASON_SYNC_MANAGER: 850 return "SYNC_MANAGER"; 851 case REASON_DOMAIN_VERIFICATION_V1: 852 return "DOMAIN_VERIFICATION_V1"; 853 case REASON_DOMAIN_VERIFICATION_V2: 854 return "DOMAIN_VERIFICATION_V2"; 855 case REASON_VPN: 856 return "VPN"; 857 case REASON_NOTIFICATION_SERVICE: 858 return "NOTIFICATION_SERVICE"; 859 case REASON_PACKAGE_REPLACED: 860 return "PACKAGE_REPLACED"; 861 case REASON_LOCATION_PROVIDER: 862 return "LOCATION_PROVIDER"; 863 case REASON_MEDIA_BUTTON: 864 return "MEDIA_BUTTON"; 865 case REASON_EVENT_SMS: 866 return "EVENT_SMS"; 867 case REASON_EVENT_MMS: 868 return "EVENT_MMS"; 869 case REASON_SHELL: 870 return "SHELL"; 871 case REASON_MEDIA_SESSION_CALLBACK: 872 return "MEDIA_SESSION_CALLBACK"; 873 case REASON_ROLE_DIALER: 874 return "ROLE_DIALER"; 875 case REASON_ROLE_EMERGENCY: 876 return "ROLE_EMERGENCY"; 877 case REASON_SYSTEM_MODULE: 878 return "SYSTEM_MODULE"; 879 case REASON_CARRIER_PRIVILEGED_APP: 880 return "CARRIER_PRIVILEGED_APP"; 881 case REASON_DPO_PROTECTED_APP: 882 return "DPO_PROTECTED_APP"; 883 case REASON_DISALLOW_APPS_CONTROL: 884 return "DISALLOW_APPS_CONTROL"; 885 case REASON_ACTIVE_DEVICE_ADMIN: 886 return "ACTIVE_DEVICE_ADMIN"; 887 case REASON_OPT_OUT_REQUESTED: 888 return "REASON_OPT_OUT_REQUESTED"; 889 case REASON_MEDIA_NOTIFICATION_TRANSFER: 890 return "REASON_MEDIA_NOTIFICATION_TRANSFER"; 891 case REASON_PACKAGE_INSTALLER: 892 return "REASON_PACKAGE_INSTALLER"; 893 default: 894 return "(unknown:" + reasonCode + ")"; 895 } 896 } 897 } 898