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

Commit f235d0b5 authored by Amith Yamasani's avatar Amith Yamasani
Browse files

Avoid elevating apps to high proc states on binding

For service and provider bindings from TOP and
FOREGROUND_SERVICE apps, don't elevate bound apps
to above BOUND_FOREGROUND_SERVICE.

For service bindings, it is possible to explicitly
request the binding to match the foreground app
such that the bound app can get similar privileges
of foreground permissions.

For instance, when a foreground service has a location
type, providers it binds to don't automatically get the
location privilege. On the other hand, sometimes apps
showing UI want to treat their dependencies also as if
they are showing UI.

This change does not affect the oom_adj calculation,
only the proc state calculation for bound processes.

New BIND_INCLUDE_CAPABILITIES flag can be used to restore old
behavior for bound services.

Introduces a new state PROCESS_STATE_BOUND_TOP

Bug: 128337543
Test: atest CtsAppTestCases:ActivityManagerProcessStateTest

Change-Id: I13733e7f43a78903299254bc110cd8f7a8db4c40
parent 825c9333
Loading
Loading
Loading
Loading
+23 −17
Original line number Original line Diff line number Diff line
@@ -513,71 +513,75 @@ public class ActivityManager {
    /** @hide Process is hosting a foreground service with location type. */
    /** @hide Process is hosting a foreground service with location type. */
    public static final int PROCESS_STATE_FOREGROUND_SERVICE_LOCATION = 3;
    public static final int PROCESS_STATE_FOREGROUND_SERVICE_LOCATION = 3;


    /** @hide Process is bound to a TOP app. This is ranked below SERVICE_LOCATION so that
     * it doesn't get the capability of location access while-in-use. */
    public static final int PROCESS_STATE_BOUND_TOP = 4;

    /** @hide Process is hosting a foreground service. */
    /** @hide Process is hosting a foreground service. */
    @UnsupportedAppUsage
    @UnsupportedAppUsage
    public static final int PROCESS_STATE_FOREGROUND_SERVICE = 4;
    public static final int PROCESS_STATE_FOREGROUND_SERVICE = 5;


    /** @hide Process is hosting a foreground service due to a system binding. */
    /** @hide Process is hosting a foreground service due to a system binding. */
    @UnsupportedAppUsage
    @UnsupportedAppUsage
    public static final int PROCESS_STATE_BOUND_FOREGROUND_SERVICE = 5;
    public static final int PROCESS_STATE_BOUND_FOREGROUND_SERVICE = 6;


    /** @hide Process is important to the user, and something they are aware of. */
    /** @hide Process is important to the user, and something they are aware of. */
    public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 6;
    public static final int PROCESS_STATE_IMPORTANT_FOREGROUND = 7;


    /** @hide Process is important to the user, but not something they are aware of. */
    /** @hide Process is important to the user, but not something they are aware of. */
    @UnsupportedAppUsage
    @UnsupportedAppUsage
    public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 7;
    public static final int PROCESS_STATE_IMPORTANT_BACKGROUND = 8;


    /** @hide Process is in the background transient so we will try to keep running. */
    /** @hide Process is in the background transient so we will try to keep running. */
    public static final int PROCESS_STATE_TRANSIENT_BACKGROUND = 8;
    public static final int PROCESS_STATE_TRANSIENT_BACKGROUND = 9;


    /** @hide Process is in the background running a backup/restore operation. */
    /** @hide Process is in the background running a backup/restore operation. */
    public static final int PROCESS_STATE_BACKUP = 9;
    public static final int PROCESS_STATE_BACKUP = 10;


    /** @hide Process is in the background running a service.  Unlike oom_adj, this level
    /** @hide Process is in the background running a service.  Unlike oom_adj, this level
     * is used for both the normal running in background state and the executing
     * is used for both the normal running in background state and the executing
     * operations state. */
     * operations state. */
    @UnsupportedAppUsage
    @UnsupportedAppUsage
    public static final int PROCESS_STATE_SERVICE = 10;
    public static final int PROCESS_STATE_SERVICE = 11;


    /** @hide Process is in the background running a receiver.   Note that from the
    /** @hide Process is in the background running a receiver.   Note that from the
     * perspective of oom_adj, receivers run at a higher foreground level, but for our
     * perspective of oom_adj, receivers run at a higher foreground level, but for our
     * prioritization here that is not necessary and putting them below services means
     * prioritization here that is not necessary and putting them below services means
     * many fewer changes in some process states as they receive broadcasts. */
     * many fewer changes in some process states as they receive broadcasts. */
    @UnsupportedAppUsage
    @UnsupportedAppUsage
    public static final int PROCESS_STATE_RECEIVER = 11;
    public static final int PROCESS_STATE_RECEIVER = 12;


    /** @hide Same as {@link #PROCESS_STATE_TOP} but while device is sleeping. */
    /** @hide Same as {@link #PROCESS_STATE_TOP} but while device is sleeping. */
    public static final int PROCESS_STATE_TOP_SLEEPING = 12;
    public static final int PROCESS_STATE_TOP_SLEEPING = 13;


    /** @hide Process is in the background, but it can't restore its state so we want
    /** @hide Process is in the background, but it can't restore its state so we want
     * to try to avoid killing it. */
     * to try to avoid killing it. */
    public static final int PROCESS_STATE_HEAVY_WEIGHT = 13;
    public static final int PROCESS_STATE_HEAVY_WEIGHT = 14;


    /** @hide Process is in the background but hosts the home activity. */
    /** @hide Process is in the background but hosts the home activity. */
    @UnsupportedAppUsage
    @UnsupportedAppUsage
    public static final int PROCESS_STATE_HOME = 14;
    public static final int PROCESS_STATE_HOME = 15;


    /** @hide Process is in the background but hosts the last shown activity. */
    /** @hide Process is in the background but hosts the last shown activity. */
    public static final int PROCESS_STATE_LAST_ACTIVITY = 15;
    public static final int PROCESS_STATE_LAST_ACTIVITY = 16;


    /** @hide Process is being cached for later use and contains activities. */
    /** @hide Process is being cached for later use and contains activities. */
    @UnsupportedAppUsage
    @UnsupportedAppUsage
    public static final int PROCESS_STATE_CACHED_ACTIVITY = 16;
    public static final int PROCESS_STATE_CACHED_ACTIVITY = 17;


    /** @hide Process is being cached for later use and is a client of another cached
    /** @hide Process is being cached for later use and is a client of another cached
     * process that contains activities. */
     * process that contains activities. */
    public static final int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 17;
    public static final int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 18;


    /** @hide Process is being cached for later use and has an activity that corresponds
    /** @hide Process is being cached for later use and has an activity that corresponds
     * to an existing recent task. */
     * to an existing recent task. */
    public static final int PROCESS_STATE_CACHED_RECENT = 18;
    public static final int PROCESS_STATE_CACHED_RECENT = 19;


    /** @hide Process is being cached for later use and is empty. */
    /** @hide Process is being cached for later use and is empty. */
    public static final int PROCESS_STATE_CACHED_EMPTY = 19;
    public static final int PROCESS_STATE_CACHED_EMPTY = 20;


    /** @hide Process does not exist. */
    /** @hide Process does not exist. */
    public static final int PROCESS_STATE_NONEXISTENT = 20;
    public static final int PROCESS_STATE_NONEXISTENT = 21;


    // NOTE: If PROCESS_STATEs are added, then new fields must be added
    // NOTE: If PROCESS_STATEs are added, then new fields must be added
    // to frameworks/base/core/proto/android/app/enums.proto and the following method must
    // to frameworks/base/core/proto/android/app/enums.proto and the following method must
@@ -602,6 +606,8 @@ public class ActivityManager {
                return AppProtoEnums.PROCESS_STATE_PERSISTENT_UI;
                return AppProtoEnums.PROCESS_STATE_PERSISTENT_UI;
            case PROCESS_STATE_TOP:
            case PROCESS_STATE_TOP:
                return AppProtoEnums.PROCESS_STATE_TOP;
                return AppProtoEnums.PROCESS_STATE_TOP;
            case PROCESS_STATE_BOUND_TOP:
                return AppProtoEnums.PROCESS_STATE_BOUND_TOP;
            case PROCESS_STATE_FOREGROUND_SERVICE_LOCATION:
            case PROCESS_STATE_FOREGROUND_SERVICE_LOCATION:
            case PROCESS_STATE_FOREGROUND_SERVICE:
            case PROCESS_STATE_FOREGROUND_SERVICE:
                return AppProtoEnums.PROCESS_STATE_FOREGROUND_SERVICE;
                return AppProtoEnums.PROCESS_STATE_FOREGROUND_SERVICE;
+1 −1
Original line number Original line Diff line number Diff line
@@ -427,7 +427,7 @@ public abstract class Context {
     * invisible background activities.  This will impact the number of
     * invisible background activities.  This will impact the number of
     * recent activities the user can switch between without having them
     * recent activities the user can switch between without having them
     * restart.  There is no guarantee this will be respected, as the system
     * restart.  There is no guarantee this will be respected, as the system
     * tries to balance such requests from one app vs. the importantance of
     * tries to balance such requests from one app vs. the importance of
     * keeping other apps around.
     * keeping other apps around.
     */
     */
    public static final int BIND_VISIBLE = 0x10000000;
    public static final int BIND_VISIBLE = 0x10000000;
+3 −1
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package android.os;
package android.os;


import static android.app.ActivityManager.PROCESS_STATE_BOUND_TOP;
import static android.app.ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE_LOCATION;
import static android.app.ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE_LOCATION;


import android.annotation.UnsupportedAppUsage;
import android.annotation.UnsupportedAppUsage;
@@ -859,7 +860,8 @@ public abstract class BatteryStats implements Parcelable {
         */
         */
        public static final int[] CRITICAL_PROC_STATES = {
        public static final int[] CRITICAL_PROC_STATES = {
                PROCESS_STATE_TOP,
                PROCESS_STATE_TOP,
                PROCESS_STATE_FOREGROUND_SERVICE_LOCATION, PROCESS_STATE_FOREGROUND_SERVICE,
                PROCESS_STATE_FOREGROUND_SERVICE_LOCATION,
                PROCESS_STATE_BOUND_TOP, PROCESS_STATE_FOREGROUND_SERVICE,
                PROCESS_STATE_FOREGROUND
                PROCESS_STATE_FOREGROUND
        };
        };


+1 −0
Original line number Original line Diff line number Diff line
@@ -79,6 +79,7 @@ public final class ProcessState {
        STATE_TOP,                      // ActivityManager.PROCESS_STATE_TOP
        STATE_TOP,                      // ActivityManager.PROCESS_STATE_TOP
        STATE_IMPORTANT_FOREGROUND,     // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE_LOCATION
        STATE_IMPORTANT_FOREGROUND,     // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE_LOCATION
        STATE_IMPORTANT_FOREGROUND,     // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE
        STATE_IMPORTANT_FOREGROUND,     // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE
        STATE_IMPORTANT_FOREGROUND,     // ActivityManager.PROCESS_STATE_BOUND_TOP
        STATE_IMPORTANT_FOREGROUND,     // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE
        STATE_IMPORTANT_FOREGROUND,     // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE
        STATE_IMPORTANT_FOREGROUND,     // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
        STATE_IMPORTANT_FOREGROUND,     // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
        STATE_IMPORTANT_BACKGROUND,     // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND
        STATE_IMPORTANT_BACKGROUND,     // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND
+4 −1
Original line number Original line Diff line number Diff line
@@ -38,6 +38,7 @@ enum AppTransitionReasonEnum {
}
}


// ActivityManager.java PROCESS_STATEs
// ActivityManager.java PROCESS_STATEs
// Next tag: 1021
enum ProcessStateEnum {
enum ProcessStateEnum {
    // Unlike the ActivityManager PROCESS_STATE values, the ordering and numerical values
    // Unlike the ActivityManager PROCESS_STATE values, the ordering and numerical values
    // here are completely fixed and arbitrary. Order is irrelevant.
    // here are completely fixed and arbitrary. Order is irrelevant.
@@ -56,9 +57,11 @@ enum ProcessStateEnum {
    // Process is hosting the current top activities. Note that this covers
    // Process is hosting the current top activities. Note that this covers
    // all activities that are visible to the user.
    // all activities that are visible to the user.
    PROCESS_STATE_TOP = 1002;
    PROCESS_STATE_TOP = 1002;
    // Process is bound to a TOP app.
    PROCESS_STATE_BOUND_TOP = 1020;
    // Process is hosting a foreground service.
    // Process is hosting a foreground service.
    PROCESS_STATE_FOREGROUND_SERVICE = 1003;
    PROCESS_STATE_FOREGROUND_SERVICE = 1003;
    // Process is hosting a foreground service due to a system binding.
    // Process is hosting a service bound by the system or another foreground app.
    PROCESS_STATE_BOUND_FOREGROUND_SERVICE = 1004;
    PROCESS_STATE_BOUND_FOREGROUND_SERVICE = 1004;
    // Process is important to the user, and something they are aware of.
    // Process is important to the user, and something they are aware of.
    PROCESS_STATE_IMPORTANT_FOREGROUND = 1005;
    PROCESS_STATE_IMPORTANT_FOREGROUND = 1005;
Loading