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

Commit 20982c69 authored by Sam Mortimer's avatar Sam Mortimer Committed by Roman Birg
Browse files

AppOps: track op persistence by name instead of id

On XML write, include the op name.  On XML read, map
the name back to op id (if it exists).

Persistent AppOp state now follows the op name instead
numeric id.  This allows upgrades between versions of
android that have different code<>name mappings.

Change-Id: Iffc02095b6838a34ff999f8db51c2c6739dd0db8
parent 77ce8f84
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -914,6 +914,8 @@ public class AppOpsManager {
     */
    private static HashMap<String, Integer> sPermToOp = new HashMap<>();

    private static HashMap<String, Integer> sNameToOp = new HashMap<String, Integer>();

    static {
        if (sOpToSwitch.length != _NUM_OP) {
            throw new IllegalStateException("sOpToSwitch length " + sOpToSwitch.length
@@ -957,6 +959,9 @@ public class AppOpsManager {
                sPermToOp.put(sOpPerms[i], i);
            }
        }
        for (int i=0; i<_NUM_OP; i++) {
            sNameToOp.put(sOpNames[i], i);
        }
    }

    /**
@@ -988,6 +993,15 @@ public class AppOpsManager {
        throw new IllegalArgumentException("Unknown operation string: " + op);
    }

    /**
     * Map a non-localized name for the operation back to the Op number
     * @hide
     */
    public static int nameToOp(String name) {
        Integer val = sNameToOp.get(name);
        return val != null ? val : OP_NONE;
    }

    /**
     * Retrieve the permission associated with an operation, or null if there is not one.
     * @hide
+15 −1
Original line number Diff line number Diff line
@@ -1388,7 +1388,20 @@ public class AppOpsService extends IAppOpsService.Stub {

            String tagName = parser.getName();
            if (tagName.equals("op")) {
                Op op = new Op(uid, pkgName, Integer.parseInt(parser.getAttributeValue(null, "n")));
                int code = Integer
                        .parseInt(parser.getAttributeValue(null, "n"));
                // use op name string if it exists
                String codeNameStr = parser.getAttributeValue(null, "ns");
                if (codeNameStr != null) {
                    // returns OP_NONE if it could not be mapped
                    code = AppOpsManager.nameToOp(codeNameStr);
                }
                // skip op codes that are out of bounds
                if (code == AppOpsManager.OP_NONE
                        || code >= AppOpsManager._NUM_OP) {
                    continue;
                }
                Op op = new Op(uid, pkgName, code);
                String mode = parser.getAttributeValue(null, "m");
                if (mode != null) {
                    op.mode = Integer.parseInt(mode);
@@ -1500,6 +1513,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                            AppOpsManager.OpEntry op = ops.get(j);
                            out.startTag(null, "op");
                            out.attribute(null, "n", Integer.toString(op.getOp()));
                            out.attribute(null, "ns", AppOpsManager.opToName(op.getOp()));
                            if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) {
                                out.attribute(null, "m", Integer.toString(op.getMode()));
                            }