Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 20f42909 authored by Hui Yu's avatar Hui Yu
Browse files

BG-FGS-launch restriction exemptions.

1. Add a new permission START_FOREGROUND_SERVICES_FROM_BACKGROUND.
2. Add a new bind flag BIND_ALLOW_FOREGROUND_SERVICE_STARTS_FROM_BACKGROUND
3. BroadcastOptions.setTemporaryAppWhitelistDuration() is currently
protected with CHANGE_DEVICE_IDLE_TEMP_WHITELIST permission, also open
it for START_ACTIVITIES_FROM_BACKGROUND and
START_FOREGROUND_SERVICES_FROM_BACKGROUND permission.
4. Exempt SYSTEM_ALERT_WINDOW permission.
5. if Context.startForegroundService() or Service.startForeground() is
restricted by BG-FGS-launch restriction, and app's targetSdkVersion is S
and above, throw a IllegalStateException.

Bug: 171305836
Test: atest cts/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java#testFgsBindingFlagFGS
atest cts/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java#testFgsBindingFlagActivity
atest cts/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java#testFgsStartSystemAlertWindow
atest cts/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java#testFgsStartFromBGException

Change-Id: Iff3ed65e174a8406d4d6045cda42bdde6cecf30d
parent dfde1bd0
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -228,6 +228,7 @@ package android {
    field public static final String SHOW_KEYGUARD_MESSAGE = "android.permission.SHOW_KEYGUARD_MESSAGE";
    field public static final String SHUTDOWN = "android.permission.SHUTDOWN";
    field public static final String START_ACTIVITIES_FROM_BACKGROUND = "android.permission.START_ACTIVITIES_FROM_BACKGROUND";
    field public static final String START_FOREGROUND_SERVICES_FROM_BACKGROUND = "android.permission.START_FOREGROUND_SERVICES_FROM_BACKGROUND";
    field public static final String STATUS_BAR_SERVICE = "android.permission.STATUS_BAR_SERVICE";
    field public static final String STOP_APP_SWITCHES = "android.permission.STOP_APP_SWITCHES";
    field public static final String SUBSTITUTE_NOTIFICATION_APP_NAME = "android.permission.SUBSTITUTE_NOTIFICATION_APP_NAME";
@@ -603,7 +604,7 @@ package android.app {
    method public static android.app.BroadcastOptions makeBasic();
    method @RequiresPermission(android.Manifest.permission.START_ACTIVITIES_FROM_BACKGROUND) public void setBackgroundActivityStartsAllowed(boolean);
    method public void setDontSendToRestrictedApps(boolean);
    method @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) public void setTemporaryAppWhitelistDuration(long);
    method @RequiresPermission(anyOf={android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST, android.Manifest.permission.START_ACTIVITIES_FROM_BACKGROUND, android.Manifest.permission.START_FOREGROUND_SERVICES_FROM_BACKGROUND}) public void setTemporaryAppWhitelistDuration(long);
    method public android.os.Bundle toBundle();
  }
@@ -1764,6 +1765,7 @@ package android.content {
    field public static final String BACKUP_SERVICE = "backup";
    field public static final String BATTERY_STATS_SERVICE = "batterystats";
    field public static final int BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS = 1048576; // 0x100000
    field public static final int BIND_ALLOW_FOREGROUND_SERVICE_STARTS_FROM_BACKGROUND = 262144; // 0x40000
    field public static final String BUGREPORT_SERVICE = "bugreport";
    field public static final String CONTENT_SUGGESTIONS_SERVICE = "content_suggestions";
    field public static final String CONTEXTHUB_SERVICE = "contexthub";
+3 −1
Original line number Diff line number Diff line
@@ -90,7 +90,9 @@ public class BroadcastOptions {
     * power allowlist when this broadcast is being delivered to it.
     * @param duration The duration in milliseconds; 0 means to not place on allowlist.
     */
    @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST)
    @RequiresPermission(anyOf = {android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST,
            android.Manifest.permission.START_ACTIVITIES_FROM_BACKGROUND,
            android.Manifest.permission.START_FOREGROUND_SERVICES_FROM_BACKGROUND})
    public void setTemporaryAppWhitelistDuration(long duration) {
        mTemporaryAppWhitelistDuration = duration;
    }
+4 −0
Original line number Diff line number Diff line
@@ -697,6 +697,10 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
     * service element of manifest file. The value of attribute
     * {@link android.R.attr#foregroundServiceType} can be multiple flags ORed together.</p>
     *
     * @throws IllegalStateException If the app targeting API is
     * {@link android.os.Build.VERSION_CODES#S} or later, and the service is restricted from
     * becoming foreground service due to background restriction.
     *
     * @param id The identifier for this notification as per
     * {@link NotificationManager#notify(int, Notification)
     * NotificationManager.notify(int, Notification)}; must not be 0.
+14 −0
Original line number Diff line number Diff line
@@ -366,6 +366,16 @@ public abstract class Context {
    /***********    Public flags above this line ***********/
    /***********    Hidden flags below this line ***********/

    /**
     * Flag for {@link #bindService}: allow background foreground service starts from the bound
     * service's process.
     * This flag is only respected if the caller is holding
     * {@link android.Manifest.permission#START_FOREGROUND_SERVICES_FROM_BACKGROUND}.
     * @hide
     */
    @SystemApi
    public static final int BIND_ALLOW_FOREGROUND_SERVICE_STARTS_FROM_BACKGROUND = 0x00040000;

    /**
     * Flag for {@link #bindService}: This flag is intended to be used only by the system to adjust
     * the scheduling policy for IMEs (and any other out-of-process user-visible components that
@@ -3107,6 +3117,10 @@ public abstract class Context {
     * @throws SecurityException If the caller does not have permission to access the service
     * or the service can not be found.
     *
     * @throws IllegalStateException If the caller app's targeting API is
     * {@link android.os.Build.VERSION_CODES#S} or later, and the foreground service is restricted
     * from start due to background restriction.
     *
     * @see #stopService
     * @see android.app.Service#startForeground(int, android.app.Notification)
     */
+4 −0
Original line number Diff line number Diff line
@@ -2557,6 +2557,10 @@
    <permission android:name="android.permission.START_ACTIVITIES_FROM_BACKGROUND"
        android:protectionLevel="signature|privileged|vendorPrivileged|oem|verifier" />

    <!-- @SystemApi @hide Allows an application to start foreground services from background -->
    <permission android:name="android.permission.START_FOREGROUND_SERVICES_FROM_BACKGROUND"
                android:protectionLevel="signature|privileged|vendorPrivileged|oem|verifier" />

    <!-- @SystemApi Must be required by activities that handle the intent action
         {@link Intent#ACTION_SEND_SHOW_SUSPENDED_APP_DETAILS}. This is for use by apps that
         hold {@link Manifest.permission#SUSPEND_APPS} to interact with the system.
Loading