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

Commit e91f3e7e authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

Fix issue #27776639: Background Check: Conn_Change...

...shouldnt be received when app is awake

(Really any implicit broadcast.)

Fix up a few things so we are more strict when not in lenient
mode.

Change-Id: I3c711525787e07ea7c604d0f9bc123e02448fa68
parent fe77fdf8
Loading
Loading
Loading
Loading
+88 −18
Original line number Original line Diff line number Diff line
@@ -25,7 +25,6 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Iterator;
@@ -1645,7 +1644,9 @@ public class AppOpsService extends IAppOpsService.Stub {
        int userId = UserHandle.USER_SYSTEM;
        int userId = UserHandle.USER_SYSTEM;
        String packageName;
        String packageName;
        String opStr;
        String opStr;
        String modeStr;
        int op;
        int op;
        int mode;
        int packageUid;
        int packageUid;


        Shell(IAppOpsService iface, AppOpsService internal) {
        Shell(IAppOpsService iface, AppOpsService internal) {
@@ -1681,6 +1682,59 @@ public class AppOpsService extends IAppOpsService.Stub {
            }
            }
        }
        }


        int strModeToMode(String modeStr, PrintWriter err) {
            switch (modeStr) {
                case "allow":
                    return AppOpsManager.MODE_ALLOWED;
                case "deny":
                    return AppOpsManager.MODE_ERRORED;
                case "ignore":
                    return AppOpsManager.MODE_IGNORED;
                case "default":
                    return AppOpsManager.MODE_DEFAULT;
            }
            try {
                return Integer.parseInt(modeStr);
            } catch (NumberFormatException e) {
            }
            err.println("Error: Mode " + modeStr + " is not valid");
            return -1;
        }

        int parseUserOpMode(int defMode, PrintWriter err) throws RemoteException {
            userId = UserHandle.USER_CURRENT;
            opStr = null;
            modeStr = null;
            for (String argument; (argument = getNextArg()) != null;) {
                if ("--user".equals(argument)) {
                    userId = UserHandle.parseUserArg(getNextArgRequired());
                } else {
                    if (opStr == null) {
                        opStr = argument;
                    } else if (modeStr == null) {
                        modeStr = argument;
                        break;
                    }
                }
            }
            if (opStr == null) {
                err.println("Error: Operation not specified.");
                return -1;
            }
            op = strOpToOp(opStr, err);
            if (op < 0) {
                return -1;
            }
            if (modeStr != null) {
                if ((mode=strModeToMode(modeStr, err)) < 0) {
                    return -1;
                }
            } else {
                mode = defMode;
            }
            return 0;
        }

        int parseUserPackageOp(boolean reqOp, PrintWriter err) throws RemoteException {
        int parseUserPackageOp(boolean reqOp, PrintWriter err) throws RemoteException {
            userId = UserHandle.USER_CURRENT;
            userId = UserHandle.USER_CURRENT;
            packageName = null;
            packageName = null;
@@ -1742,6 +1796,8 @@ public class AppOpsService extends IAppOpsService.Stub {
        pw.println("    Set the mode for a particular application and operation.");
        pw.println("    Set the mode for a particular application and operation.");
        pw.println("  get [--user <USER_ID>] <PACKAGE> [<OP>]");
        pw.println("  get [--user <USER_ID>] <PACKAGE> [<OP>]");
        pw.println("    Return the mode for a particular application and optional operation.");
        pw.println("    Return the mode for a particular application and optional operation.");
        pw.println("  query-op [--user <USER_ID>] <OP> [<MODE>]");
        pw.println("    Print all packages that currently have the given op in the given mode.");
        pw.println("  reset [--user <USER_ID>] [<PACKAGE>]");
        pw.println("  reset [--user <USER_ID>] [<PACKAGE>]");
        pw.println("    Reset the given application or all applications to default modes.");
        pw.println("    Reset the given application or all applications to default modes.");
        pw.println("  write-settings");
        pw.println("  write-settings");
@@ -1775,22 +1831,8 @@ public class AppOpsService extends IAppOpsService.Stub {
                        return -1;
                        return -1;
                    }
                    }


                    final int mode;
                    final int mode = shell.strModeToMode(modeStr, err);
                    switch (modeStr) {
                    if (mode < 0) {
                        case "allow":
                            mode = AppOpsManager.MODE_ALLOWED;
                            break;
                        case "deny":
                            mode = AppOpsManager.MODE_ERRORED;
                            break;
                        case "ignore":
                            mode = AppOpsManager.MODE_IGNORED;
                            break;
                        case "default":
                            mode = AppOpsManager.MODE_DEFAULT;
                            break;
                        default:
                            err.println("Error: Mode " + modeStr + " is not valid,");
                        return -1;
                        return -1;
                    }
                    }


@@ -1856,6 +1898,34 @@ public class AppOpsService extends IAppOpsService.Stub {
                    }
                    }
                    return 0;
                    return 0;
                }
                }
                case "query-op": {
                    int res = shell.parseUserOpMode(AppOpsManager.MODE_IGNORED, err);
                    if (res < 0) {
                        return res;
                    }
                    List<AppOpsManager.PackageOps> ops = shell.mInterface.getPackagesForOps(
                            new int[] {shell.op});
                    if (ops == null || ops.size() <= 0) {
                        pw.println("No operations.");
                        return 0;
                    }
                    for (int i=0; i<ops.size(); i++) {
                        final AppOpsManager.PackageOps pkg = ops.get(i);
                        boolean hasMatch = false;
                        final List<AppOpsManager.OpEntry> entries = ops.get(i).getOps();
                        for (int j=0; j<entries.size(); j++) {
                            AppOpsManager.OpEntry ent = entries.get(j);
                            if (ent.getOp() == shell.op && ent.getMode() == shell.mode) {
                                hasMatch = true;
                                break;
                            }
                        }
                        if (hasMatch) {
                            pw.println(pkg.getPackageName());
                        }
                    }
                    return 0;
                }
                case "reset": {
                case "reset": {
                    String packageName = null;
                    String packageName = null;
                    int userId = UserHandle.USER_CURRENT;
                    int userId = UserHandle.USER_CURRENT;
+1 −2
Original line number Original line Diff line number Diff line
@@ -61,7 +61,6 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.content.pm.ServiceInfo;
@@ -1279,7 +1278,7 @@ public final class ActiveServices {
                        // Before going further -- if this app is not allowed to run in the
                        // Before going further -- if this app is not allowed to run in the
                        // background, then at this point we aren't going to let it period.
                        // background, then at this point we aren't going to let it period.
                        final int allowed = mAm.checkAllowBackgroundLocked(
                        final int allowed = mAm.checkAllowBackgroundLocked(
                                sInfo.applicationInfo.uid, sInfo.packageName, callingPid);
                                sInfo.applicationInfo.uid, sInfo.packageName, callingPid, true);
                        if (allowed != ActivityManager.APP_START_MODE_NORMAL) {
                        if (allowed != ActivityManager.APP_START_MODE_NORMAL) {
                            Slog.w(TAG, "Background execution not allowed: service "
                            Slog.w(TAG, "Background execution not allowed: service "
                                    + service + " to " + name.flattenToShortString()
                                    + service + " to " + name.flattenToShortString()
+4 −3
Original line number Original line Diff line number Diff line
@@ -7535,14 +7535,15 @@ public final class ActivityManagerService extends ActivityManagerNative
    public int getAppStartMode(int uid, String packageName) {
    public int getAppStartMode(int uid, String packageName) {
        synchronized (this) {
        synchronized (this) {
            return checkAllowBackgroundLocked(uid, packageName, -1);
            return checkAllowBackgroundLocked(uid, packageName, -1, true);
        }
        }
    }
    }
    int checkAllowBackgroundLocked(int uid, String packageName, int callingPid) {
    int checkAllowBackgroundLocked(int uid, String packageName, int callingPid,
            boolean allowWhenForeground) {
        UidRecord uidRec = mActiveUids.get(uid);
        UidRecord uidRec = mActiveUids.get(uid);
        if (!mLenientBackgroundCheck) {
        if (!mLenientBackgroundCheck) {
            if (uidRec == null
            if (!allowWhenForeground || uidRec == null
                    || uidRec.curProcState >= ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND) {
                    || uidRec.curProcState >= ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND) {
                if (mAppOpsService.noteOperation(AppOpsManager.OP_RUN_IN_BACKGROUND, uid,
                if (mAppOpsService.noteOperation(AppOpsManager.OP_RUN_IN_BACKGROUND, uid,
                        packageName) != AppOpsManager.MODE_ALLOWED) {
                        packageName) != AppOpsManager.MODE_ALLOWED) {
+8 −9
Original line number Original line Diff line number Diff line
@@ -50,7 +50,6 @@ import android.util.EventLog;
import android.util.Slog;
import android.util.Slog;
import android.util.TimeUtils;
import android.util.TimeUtils;
import com.android.server.DeviceIdleController;
import com.android.server.DeviceIdleController;
import com.android.server.LocalServices;


import static com.android.server.am.ActivityManagerDebugConfig.*;
import static com.android.server.am.ActivityManagerDebugConfig.*;


@@ -563,7 +562,7 @@ public final class BroadcastQueue {
        }
        }
        if (!skip) {
        if (!skip) {
            final int allowed = mService.checkAllowBackgroundLocked(filter.receiverList.uid,
            final int allowed = mService.checkAllowBackgroundLocked(filter.receiverList.uid,
                    filter.packageName, -1);
                    filter.packageName, -1, true);
            if (allowed == ActivityManager.APP_START_MODE_DISABLED) {
            if (allowed == ActivityManager.APP_START_MODE_DISABLED) {
                Slog.w(TAG, "Background execution not allowed: receiving "
                Slog.w(TAG, "Background execution not allowed: receiving "
                        + r.intent
                        + r.intent
@@ -1102,21 +1101,21 @@ public final class BroadcastQueue {


            if (!skip) {
            if (!skip) {
                final int allowed = mService.checkAllowBackgroundLocked(
                final int allowed = mService.checkAllowBackgroundLocked(
                        info.activityInfo.applicationInfo.uid, info.activityInfo.packageName, -1);
                        info.activityInfo.applicationInfo.uid, info.activityInfo.packageName, -1,
                        false);
                if (allowed != ActivityManager.APP_START_MODE_NORMAL) {
                if (allowed != ActivityManager.APP_START_MODE_NORMAL) {
                    // We won't allow this receiver to be launched if the app has been
                    // We won't allow this receiver to be launched if the app has been
                    // completely disabled from launches, or it is delayed and the broadcast
                    // completely disabled from launches, or it was not explicitly sent
                    // was not explicitly sent to it and this would result in a new process
                    // to it and the app is in a state that should not receive it
                    // for it being created.
                    // (depending on how checkAllowBackgroundLocked has determined that).
                    if (allowed == ActivityManager.APP_START_MODE_DISABLED) {
                    if (allowed == ActivityManager.APP_START_MODE_DISABLED) {
                        Slog.w(TAG, "Background execution disabled: receiving "
                        Slog.w(TAG, "Background execution disabled: receiving "
                                + r.intent + " to "
                                + r.intent + " to "
                                + component.flattenToShortString());
                                + component.flattenToShortString());
                        skip = true;
                        skip = true;
                    }
                    } else if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0)
                    if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0)
                            || (r.intent.getComponent() == null
                            || (r.intent.getComponent() == null
                                && r.intent.getPackage() == null && app == null
                                && r.intent.getPackage() == null
                                && ((r.intent.getFlags()
                                && ((r.intent.getFlags()
                                        & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0))) {
                                        & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0))) {
                        Slog.w(TAG, "Background execution not allowed: receiving "
                        Slog.w(TAG, "Background execution not allowed: receiving "