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

Commit 43eded91 authored by Sam Mortimer's avatar Sam Mortimer Committed by Roman Birg
Browse files

[2/2] Framework AppOps: Add per Op allowed and ignored counters

Port from cm-10.2 and cm-11.0

Author: Sam Mortimer <sam@mortimer.me.uk>
Date:   Wed Oct 2 22:06:42 2013 -0700
[2/2] AppOps: Add per Op allowed and ignored counters
Change-Id: Ifb3d25e17dbab082b816a0a655b4796a83af336b

Change-Id: Ifb3d25e17dbab082b816a0a655b4796a83af336b
parent 3683adb6
Loading
Loading
Loading
Loading
+24 −1
Original line number Diff line number Diff line
@@ -1289,9 +1289,11 @@ public class AppOpsManager {
        private final int mDuration;
        private final int mProxyUid;
        private final String mProxyPackageName;
        private final int mAllowedCount;
        private final int mIgnoredCount;

        public OpEntry(int op, int mode, long time, long rejectTime, int duration,
                int proxyUid, String proxyPackage) {
                       int proxyUid, String proxyPackage, int allowedCount, int ignoredCount) {
            mOp = op;
            mMode = mode;
            mTime = time;
@@ -1299,6 +1301,8 @@ public class AppOpsManager {
            mDuration = duration;
            mProxyUid = proxyUid;
            mProxyPackageName = proxyPackage;
            mAllowedCount = allowedCount;
            mIgnoredCount = ignoredCount;
        }

        public int getOp() {
@@ -1333,6 +1337,14 @@ public class AppOpsManager {
            return mProxyPackageName;
        }

        public int getAllowedCount() {
            return mAllowedCount;
        }

        public int getIgnoredCount() {
            return mIgnoredCount;
        }

        @Override
        public int describeContents() {
            return 0;
@@ -1347,6 +1359,8 @@ public class AppOpsManager {
            dest.writeInt(mDuration);
            dest.writeInt(mProxyUid);
            dest.writeString(mProxyPackageName);
            dest.writeInt(mAllowedCount);
            dest.writeInt(mIgnoredCount);
        }

        OpEntry(Parcel source) {
@@ -1357,6 +1371,8 @@ public class AppOpsManager {
            mDuration = source.readInt();
            mProxyUid = source.readInt();
            mProxyPackageName = source.readString();
            mAllowedCount = source.readInt();
            mIgnoredCount = source.readInt();
        }

        public static final Creator<OpEntry> CREATOR = new Creator<OpEntry>() {
@@ -1993,4 +2009,11 @@ public class AppOpsManager {
        }
    }

    /** @hide */
    public void resetCounters() {
        try {
            mService.resetCounters();
        } catch (RemoteException e) {
        }
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -51,4 +51,7 @@ interface IAppOpsService {
    // Privacy guard methods
    boolean getPrivacyGuardSettingForPackage(int uid, String packageName);
    void setPrivacyGuardSettingForPackage(int uid, String packageName, boolean state);

    // AppOps accounting
    void resetCounters();
}
+48 −3
Original line number Diff line number Diff line
@@ -174,6 +174,8 @@ public class AppOpsService extends IAppOpsService.Stub {
        public int startOpCount;
        public PermissionDialogReqQueue dialogReqQueue;
        final ArrayList<IBinder> clientTokens;
        public int allowedCount;
        public int ignoredCount;

        public Op(int _uid, String _packageName, int _op, int _mode) {
            uid = _uid;
@@ -402,7 +404,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                Op curOp = pkgOps.valueAt(j);
                resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
                        curOp.rejectTime, curOp.duration, curOp.proxyUid,
                        curOp.proxyPackageName));
                        curOp.proxyPackageName, curOp.allowedCount, curOp.ignoredCount));
            }
        } else {
            for (int j=0; j<ops.length; j++) {
@@ -413,7 +415,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                    }
                    resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
                            curOp.rejectTime, curOp.duration, curOp.proxyUid,
                            curOp.proxyPackageName));
                            curOp.proxyPackageName, curOp.allowedCount, curOp.ignoredCount));
                }
            }
        }
@@ -987,6 +989,7 @@ public class AppOpsService extends IAppOpsService.Stub {
            }
            Op op = getOpLocked(ops, code, true);
            if (isOpRestricted(uid, code, packageName)) {
                op.ignoredCount++;
                return AppOpsManager.MODE_IGNORED;
            }
            if (op.duration == -1) {
@@ -1014,6 +1017,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                            + " for code " + switchCode + " (" + code
                            + ") uid " + uid + " package " + packageName);
                op.rejectTime = System.currentTimeMillis();
                op.ignoredCount++;
                return switchOp.mode;
            } else if (switchOp.mode == AppOpsManager.MODE_ALLOWED) {
                if (DEBUG) Log.d(TAG, "noteOperation: allowing code " + code + " uid " + uid
@@ -1022,6 +1026,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                op.rejectTime = 0;
                op.proxyUid = proxyUid;
                op.proxyPackageName = proxyPackageName;
                op.allowedCount++;
                return AppOpsManager.MODE_ALLOWED;

            } else {
@@ -1058,6 +1063,7 @@ public class AppOpsService extends IAppOpsService.Stub {
            }
            Op op = getOpLocked(ops, code, true);
            if (isOpRestricted(uid, code, packageName)) {
                op.ignoredCount++;
                return AppOpsManager.MODE_IGNORED;
            }
            final int switchCode = AppOpsManager.opToSwitch(code);
@@ -1080,6 +1086,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                            + " for code " + switchCode + " (" + code
                            + ") uid " + uid + " package " + packageName);
                op.rejectTime = System.currentTimeMillis();
                op.ignoredCount++;
                return switchOp.mode;
            } else if (switchOp.mode == AppOpsManager.MODE_ALLOWED) {
                if (DEBUG)
@@ -1089,6 +1096,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                    op.time = System.currentTimeMillis();
                    op.rejectTime = 0;
                    op.duration = -1;
                    op.allowedCount++;
                }
                op.nesting++;
                if (client.mStartedOps != null) {
@@ -1515,7 +1523,14 @@ public class AppOpsService extends IAppOpsService.Stub {
                if (proxyPackageName != null) {
                    op.proxyPackageName = proxyPackageName;
                }

                String allowed = parser.getAttributeValue(null, "ac");
                if (allowed != null) {
                    op.allowedCount = Integer.parseInt(allowed);
                }
                String ignored = parser.getAttributeValue(null, "ic");
                if (ignored != null) {
                    op.ignoredCount = Integer.parseInt(ignored);
                }
                UidState uidState = getUidStateLocked(uid, true);
                if (uidState.pkgOps == null) {
                    uidState.pkgOps = new ArrayMap<>();
@@ -1630,6 +1645,14 @@ public class AppOpsService extends IAppOpsService.Stub {
                            if (proxyPackageName != null) {
                                out.attribute(null, "pp", proxyPackageName);
                            }
                            int allowed = op.getAllowedCount();
                            if (allowed != 0) {
                                out.attribute(null, "ac", Integer.toString(allowed));
                            }
                            int ignored = op.getIgnoredCount();
                            if (ignored != 0) {
                                out.attribute(null, "ic", Integer.toString(ignored));
                            }
                            out.endTag(null, "op");
                        }
                        out.endTag(null, "uid");
@@ -2092,4 +2115,26 @@ public class AppOpsService extends IAppOpsService.Stub {
                    ? AppOpsManager.MODE_ASK : AppOpsManager.MODE_ALLOWED);
        }
    }

    @Override
    public void resetCounters() {
        mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
                Binder.getCallingPid(), Binder.getCallingUid(), null);
        synchronized (this) {
            for (int i=0; i<mUidStates.size(); i++) {
                final UidState uidState = mUidStates.valueAt(i);
                for (Map.Entry<String, Ops> ent : uidState.pkgOps.entrySet()) {
                    String packageName = ent.getKey();
                    Ops pkgOps = ent.getValue();
                    for (int j=0; j<pkgOps.size(); j++) {
                        Op curOp = pkgOps.valueAt(j);
                        curOp.allowedCount = 0;
                        curOp.ignoredCount = 0;
                    }
                }
            }
            // ensure the counter reset persists
            scheduleWriteLocked();
        }
    }
}