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

Commit 5dfe36b8 authored by Nan Wu's avatar Nan Wu Committed by Android (Google) Code Review
Browse files

Merge "Block service bound by foreground app to launch background activity by default"

parents 2875b9e4 32d95deb
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10071,6 +10071,7 @@ package android.content {
    field public static final String BATTERY_SERVICE = "batterymanager";
    field public static final int BIND_ABOVE_CLIENT = 8; // 0x8
    field public static final int BIND_ADJUST_WITH_ACTIVITY = 128; // 0x80
    field public static final int BIND_ALLOW_ACTIVITY_STARTS = 512; // 0x200
    field public static final int BIND_ALLOW_OOM_MANAGEMENT = 16; // 0x10
    field public static final int BIND_AUTO_CREATE = 1; // 0x1
    field public static final int BIND_DEBUG_UNBIND = 2; // 0x2
+10 −0
Original line number Diff line number Diff line
@@ -278,6 +278,7 @@ public abstract class Context {
            BIND_IMPORTANT,
            BIND_ADJUST_WITH_ACTIVITY,
            BIND_NOT_PERCEPTIBLE,
            BIND_ALLOW_ACTIVITY_STARTS,
            BIND_INCLUDE_CAPABILITIES,
            BIND_SHARED_ISOLATED_PROCESS
    })
@@ -381,6 +382,15 @@ public abstract class Context {
     */
    public static final int BIND_NOT_PERCEPTIBLE = 0x00000100;

    /**
     * Flag for {@link #bindService}: If binding from an app that is visible, the bound service is
     * allowed to start an activity from background. This was the default behavior before SDK
     * version {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE}. Since then, the default
     * behavior changed to disallow the bound service to start a background activity even if the app
     * bound to it is in foreground, unless this flag is specified when binding.
     */
    public static final int BIND_ALLOW_ACTIVITY_STARTS = 0X000000200;

    /**
     * Flag for {@link #bindService}: If binding from an app that has specific capabilities
     * due to its foreground state such as an activity or foreground service, then this flag will
+1 −0
Original line number Diff line number Diff line
@@ -543,6 +543,7 @@ message ConnectionRecordProto {
        DEAD = 15;
        NOT_PERCEPTIBLE = 16;
        INCLUDE_CAPABILITIES = 17;
        ALLOW_ACTIVITY_STARTS = 18;
    }
    repeated Flag flags = 3;
    optional string service_name = 4;
+5 −0
Original line number Diff line number Diff line
@@ -77,6 +77,7 @@ final class ConnectionRecord {
            Context.BIND_NOT_VISIBLE,
            Context.BIND_NOT_PERCEPTIBLE,
            Context.BIND_INCLUDE_CAPABILITIES,
            Context.BIND_ALLOW_ACTIVITY_STARTS,
    };
    private static final int[] BIND_PROTO_ENUMS = new int[] {
            ConnectionRecordProto.AUTO_CREATE,
@@ -96,6 +97,7 @@ final class ConnectionRecord {
            ConnectionRecordProto.NOT_VISIBLE,
            ConnectionRecordProto.NOT_PERCEPTIBLE,
            ConnectionRecordProto.INCLUDE_CAPABILITIES,
            ConnectionRecordProto.ALLOW_ACTIVITY_STARTS,
    };

    void dump(PrintWriter pw, String prefix) {
@@ -237,6 +239,9 @@ final class ConnectionRecord {
        if ((flags & Context.BIND_NOT_PERCEPTIBLE) != 0) {
            sb.append("!PRCP ");
        }
        if ((flags & Context.BIND_ALLOW_ACTIVITY_STARTS) != 0) {
            sb.append("BALF ");
        }
        if ((flags & Context.BIND_INCLUDE_CAPABILITIES) != 0) {
            sb.append("CAPS ");
        }
+15 −8
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.util.ArrayMap;
import android.util.ArraySet;

import com.android.internal.annotations.GuardedBy;
import com.android.server.wm.WindowProcessController;

import java.io.PrintWriter;
import java.util.ArrayList;
@@ -506,19 +507,21 @@ final class ProcessServiceRecord {
        return mConnections.size();
    }

    void addBoundClientUid(int clientUid) {
    void addBoundClientUid(int clientUid, String clientPackageName, int bindFlags) {
        mBoundClientUids.add(clientUid);
        mApp.getWindowProcessController().setBoundClientUids(mBoundClientUids);
        mApp.getWindowProcessController()
                .addBoundClientUid(clientUid, clientPackageName, bindFlags);
    }

    void updateBoundClientUids() {
        if (mServices.isEmpty()) {
        clearBoundClientUids();
        if (mServices.isEmpty()) {
            return;
        }
        // grab a set of clientUids of all mConnections of all services
        final ArraySet<Integer> boundClientUids = new ArraySet<>();
        final int serviceCount = mServices.size();
        WindowProcessController controller = mApp.getWindowProcessController();
        for (int j = 0; j < serviceCount; j++) {
            final ArrayMap<IBinder, ArrayList<ConnectionRecord>> conns =
                    mServices.valueAt(j).getConnections();
@@ -526,12 +529,13 @@ final class ProcessServiceRecord {
            for (int conni = 0; conni < size; conni++) {
                ArrayList<ConnectionRecord> c = conns.valueAt(conni);
                for (int i = 0; i < c.size(); i++) {
                    boundClientUids.add(c.get(i).clientUid);
                    ConnectionRecord cr = c.get(i);
                    boundClientUids.add(cr.clientUid);
                    controller.addBoundClientUid(cr.clientUid, cr.clientPackageName, cr.flags);
                }
            }
        }
        mBoundClientUids = boundClientUids;
        mApp.getWindowProcessController().setBoundClientUids(mBoundClientUids);
    }

    void addBoundClientUidsOfNewService(ServiceRecord sr) {
@@ -542,15 +546,18 @@ final class ProcessServiceRecord {
        for (int conni = conns.size() - 1; conni >= 0; conni--) {
            ArrayList<ConnectionRecord> c = conns.valueAt(conni);
            for (int i = 0; i < c.size(); i++) {
                mBoundClientUids.add(c.get(i).clientUid);
                ConnectionRecord cr = c.get(i);
                mBoundClientUids.add(cr.clientUid);
                mApp.getWindowProcessController()
                        .addBoundClientUid(cr.clientUid, cr.clientPackageName, cr.flags);

            }
        }
        mApp.getWindowProcessController().setBoundClientUids(mBoundClientUids);
    }

    void clearBoundClientUids() {
        mBoundClientUids.clear();
        mApp.getWindowProcessController().setBoundClientUids(mBoundClientUids);
        mApp.getWindowProcessController().clearBoundClientUids();
    }

    @GuardedBy("mService")
Loading