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

Commit da6d8814 authored by Sam Mortimer's avatar Sam Mortimer Committed by Gerrit Code Review
Browse files

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

Change-Id: Ifb3d25e17dbab082b816a0a655b4796a83af336b
parent 2a099881
Loading
Loading
Loading
Loading
+25 −1
Original line number Diff line number Diff line
@@ -381,13 +381,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() {
@@ -414,6 +419,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;
@@ -426,6 +439,8 @@ public class AppOpsManager {
            dest.writeLong(mTime);
            dest.writeLong(mRejectTime);
            dest.writeInt(mDuration);
            dest.writeInt(mAllowedCount);
            dest.writeInt(mIgnoredCount);
        }

        OpEntry(Parcel source) {
@@ -434,6 +449,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>() {
@@ -626,4 +643,11 @@ public class AppOpsManager {
        } catch (RemoteException e) {
        }
    }

    public void resetCounters() {
        try {
            mService.resetCounters();
        } catch (RemoteException e) {
        }
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ interface IAppOpsService {
    List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName, in int[] ops);
    void setMode(int code, int uid, String packageName, int mode);
    void resetAllModes();
    void resetCounters();

    // Privacy guard methods
    boolean getPrivacyGuardSettingForPackage(int uid, String packageName);
+46 −2
Original line number Diff line number Diff line
@@ -133,6 +133,8 @@ public class AppOpsService extends IAppOpsService.Stub {
        public int nesting;
        public int tempNesting;
        public PermissionDialogResult dialogResult;
        public int allowedCount;
        public int ignoredCount;

        public Op(int _op, boolean strict) {
            op = _op;
@@ -285,7 +287,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++) {
@@ -295,7 +298,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));
                }
            }
        }
@@ -563,11 +567,13 @@ public class AppOpsService extends IAppOpsService.Stub {
                if (DEBUG) Log.d(TAG, "recordOperation: reject #" + mode + " for code "
                        + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
                op.rejectTime = System.currentTimeMillis();
                op.ignoredCount++;
            } else if (mode == AppOpsManager.MODE_ALLOWED) {
                if (DEBUG) Log.d(TAG, "recordOperation: allowing code " + code + " uid " + uid
                        + " package " + packageName);
                op.time = System.currentTimeMillis();
                op.rejectTime = 0;
                op.allowedCount++;
            }
        }
    }
@@ -1080,6 +1086,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>();
@@ -1149,6 +1163,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");
@@ -1231,4 +1253,26 @@ public class AppOpsService extends IAppOpsService.Stub {
                    ? AppOpsManager.MODE_IGNORED : 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();
    }
}