OSDN Git Service

New API to get app op information about a single package.
authorDianne Hackborn <hackbod@google.com>
Sat, 19 Jan 2013 02:36:09 +0000 (18:36 -0800)
committerDianne Hackborn <hackbod@google.com>
Sat, 19 Jan 2013 02:36:09 +0000 (18:36 -0800)
Change-Id: I986453d9bb4161da467fb820b12502464e936483

core/java/android/app/AppOpsManager.java
core/java/com/android/internal/app/IAppOpsService.aidl
services/java/com/android/server/AppOpsService.java

index 679c91d..5d24d69 100644 (file)
@@ -187,6 +187,14 @@ public class AppOpsManager {
         return null;
     }
 
+    public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName, int[] ops) {
+        try {
+            return mService.getOpsForPackage(uid, packageName, ops);
+        } catch (RemoteException e) {
+        }
+        return null;
+    }
+
     public int checkOp(int op, int uid, String packageName) {
         try {
             int mode = mService.checkOperation(op, uid, packageName);
index 827dba6..a4eb4c5 100644 (file)
@@ -20,6 +20,7 @@ import android.app.AppOpsManager;
 
 interface IAppOpsService {
     List<AppOpsManager.PackageOps> getPackagesForOps(in int[] ops);
+    List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName, in int[] ops);
     int checkOperation(int code, int uid, String packageName);
     int noteOperation(int code, int uid, String packageName);
     int startOperation(int code, int uid, String packageName);
index 1712806..3d9ddae 100644 (file)
@@ -127,6 +127,30 @@ public class AppOpsService extends IAppOpsService.Stub {
         }
     }
 
+    private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) {
+        ArrayList<AppOpsManager.OpEntry> resOps = null;
+        if (ops == null) {
+            resOps = new ArrayList<AppOpsManager.OpEntry>();
+            for (int j=0; j<pkgOps.size(); j++) {
+                Op curOp = pkgOps.valueAt(j);
+                resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.time,
+                        curOp.duration));
+            }
+        } else {
+            for (int j=0; j<ops.length; j++) {
+                Op curOp = pkgOps.get(ops[j]);
+                if (curOp != null) {
+                    if (resOps == null) {
+                        resOps = new ArrayList<AppOpsManager.OpEntry>();
+                    }
+                    resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.time,
+                            curOp.duration));
+                }
+            }
+        }
+        return resOps;
+    }
+
     @Override
     public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) {
         mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
@@ -136,26 +160,7 @@ public class AppOpsService extends IAppOpsService.Stub {
             for (int i=0; i<mUidOps.size(); i++) {
                 HashMap<String, Ops> packages = mUidOps.valueAt(i);
                 for (Ops pkgOps : packages.values()) {
-                    ArrayList<AppOpsManager.OpEntry> resOps = null;
-                    if (ops == null) {
-                        resOps = new ArrayList<AppOpsManager.OpEntry>();
-                        for (int j=0; j<pkgOps.size(); j++) {
-                            Op curOp = pkgOps.valueAt(j);
-                            resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.time,
-                                    curOp.duration));
-                        }
-                    } else {
-                        for (int j=0; j<ops.length; j++) {
-                            Op curOp = pkgOps.get(ops[j]);
-                            if (curOp != null) {
-                                if (resOps == null) {
-                                    resOps = new ArrayList<AppOpsManager.OpEntry>();
-                                }
-                                resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.time,
-                                        curOp.duration));
-                            }
-                        }
-                    }
+                    ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
                     if (resOps != null) {
                         if (res == null) {
                             res = new ArrayList<AppOpsManager.PackageOps>();
@@ -171,6 +176,28 @@ public class AppOpsService extends IAppOpsService.Stub {
     }
 
     @Override
+    public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName,
+            int[] ops) {
+        mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
+                Binder.getCallingPid(), Binder.getCallingUid(), null);
+        synchronized (this) {
+            Ops pkgOps = getOpsLocked(uid, packageName, false);
+            if (pkgOps == null) {
+                return null;
+            }
+            ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
+            if (resOps == null) {
+                return null;
+            }
+            ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>();
+            AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
+                    pkgOps.packageName, pkgOps.uid, resOps);
+            res.add(resPackage);
+            return res;
+        }
+    }
+
+    @Override
     public int checkOperation(int code, int uid, String packageName) {
         uid = handleIncomingUid(uid);
         synchronized (this) {
@@ -252,7 +279,7 @@ public class AppOpsService extends IAppOpsService.Stub {
         return uid;
     }
 
-    private Op getOpLocked(int code, int uid, String packageName, boolean edit) {
+    private Ops getOpsLocked(int uid, String packageName, boolean edit) {
         HashMap<String, Ops> pkgOps = mUidOps.get(uid);
         if (pkgOps == null) {
             if (!edit) {
@@ -289,6 +316,14 @@ public class AppOpsService extends IAppOpsService.Stub {
             ops = new Ops(packageName, uid);
             pkgOps.put(packageName, ops);
         }
+        return ops;
+    }
+
+    private Op getOpLocked(int code, int uid, String packageName, boolean edit) {
+        Ops ops = getOpsLocked(uid, packageName, edit);
+        if (ops == null) {
+            return null;
+        }
         Op op = ops.get(code);
         if (op == null) {
             if (!edit) {