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

Commit 4018ce82 authored by Sam Mortimer's avatar Sam Mortimer Committed by Steve Kondik
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 46eb2c5e
Loading
Loading
Loading
Loading
+25 −1
Original line number Diff line number Diff line
@@ -1321,13 +1321,18 @@ public class AppOpsManager {
        private final long mTime;
        private final long mRejectTime;
        private final int mDuration;
        private final int mAllowedCount;
        private final int mIgnoredCount;

        public OpEntry(int op, int mode, long time, long rejectTime, int duration) {
        public OpEntry(int op, int mode, long time, long rejectTime, int duration,
                int allowedCount, int ignoredCount) {
            mOp = op;
            mMode = mode;
            mTime = time;
            mRejectTime = rejectTime;
            mDuration = duration;
            mAllowedCount = allowedCount;
            mIgnoredCount = ignoredCount;
        }

        public int getOp() {
@@ -1354,6 +1359,14 @@ public class AppOpsManager {
            return mDuration == -1 ? (int)(System.currentTimeMillis()-mTime) : mDuration;
        }

        public int getAllowedCount() {
            return mAllowedCount;
        }

        public int getIgnoredCount() {
            return mIgnoredCount;
        }

        @Override
        public int describeContents() {
            return 0;
@@ -1366,6 +1379,8 @@ public class AppOpsManager {
            dest.writeLong(mTime);
            dest.writeLong(mRejectTime);
            dest.writeInt(mDuration);
            dest.writeInt(mAllowedCount);
            dest.writeInt(mIgnoredCount);
        }

        OpEntry(Parcel source) {
@@ -1374,6 +1389,8 @@ public class AppOpsManager {
            mTime = source.readLong();
            mRejectTime = source.readLong();
            mDuration = source.readInt();
            mAllowedCount = source.readInt();
            mIgnoredCount = source.readInt();
        }

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

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

    // AppOps accounting
    void resetCounters();
}
+50 −2
Original line number Diff line number Diff line
@@ -143,6 +143,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;
@@ -315,7 +317,8 @@ public class AppOpsService extends IAppOpsService.Stub {
            for (int j=0; j<pkgOps.size(); j++) {
                Op curOp = pkgOps.valueAt(j);
                resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
                        curOp.rejectTime, curOp.duration));
                        curOp.rejectTime, curOp.duration,
                        curOp.allowedCount, curOp.ignoredCount));
            }
        } else {
            for (int j=0; j<ops.length; j++) {
@@ -325,7 +328,8 @@ public class AppOpsService extends IAppOpsService.Stub {
                        resOps = new ArrayList<AppOpsManager.OpEntry>();
                    }
                    resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
                            curOp.rejectTime, curOp.duration));
                            curOp.rejectTime, curOp.duration,
                            curOp.allowedCount, curOp.ignoredCount));
                }
            }
        }
@@ -684,6 +688,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) {
@@ -701,6 +706,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)
@@ -708,6 +714,7 @@ public class AppOpsService extends IAppOpsService.Stub {
                            + uid + " package " + packageName);
                op.time = System.currentTimeMillis();
                op.rejectTime = 0;
                op.allowedCount++;
                return AppOpsManager.MODE_ALLOWED;
            } else {
                if (Looper.myLooper() == mLooper) {
@@ -743,6 +750,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);
@@ -755,6 +763,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)
@@ -764,6 +773,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) {
@@ -1127,6 +1137,14 @@ public class AppOpsService extends IAppOpsService.Stub {
                if (dur != null) {
                    op.duration = Integer.parseInt(dur);
                }
                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);
                }
                HashMap<String, Ops> pkgOps = mUidOps.get(uid);
                if (pkgOps == null) {
                    pkgOps = new HashMap<String, Ops>();
@@ -1213,6 +1231,14 @@ public class AppOpsService extends IAppOpsService.Stub {
                            if (dur != 0) {
                                out.attribute(null, "d", Integer.toString(dur));
                            }
                            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");
@@ -1595,4 +1621,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<mUidOps.size(); i++) {
                HashMap<String, Ops> packages = mUidOps.valueAt(i);
                for (Map.Entry<String, Ops> ent : packages.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
            scheduleWriteNowLocked();
        }
    }
}