More work on App Ops service.

Implemented reading and writing state to retain information
across boots, API to retrieve state from it, improved location
manager interaction to monitor both coarse and fine access
and only note operations when location data is being delivered
back to app (not when it is just registering to get the data at
some time in the future).

Also implement tracking of read/write ops on contacts and the
call log.  This involved tweaking the content provider protocol
to pass over the name of the calling package, and some
infrastructure in the ContentProvider transport to note incoming
calls with the app ops service.  The contacts provider and call
log provider turn this on for themselves.

This also implements some of the mechanics of being able to ignore
incoming provider calls...  all that is left are some new APIs for
the real content provider implementation to be involved with
providing the correct behavior for query() (return an empty
cursor with the right columns) and insert() (need to figure out
what URI to return).

Change-Id: I36ebbcd63dee58264a480f3d3786891ca7cbdb4c
diff --git a/services/java/com/android/server/LocationManagerService.java b/services/java/com/android/server/LocationManagerService.java
index 946ed78..b351fc2 100644
--- a/services/java/com/android/server/LocationManagerService.java
+++ b/services/java/com/android/server/LocationManagerService.java
@@ -529,8 +529,7 @@
         }
 
         public boolean callLocationChangedLocked(Location location) {
-            if (mAppOps.noteOpNoThrow(AppOpsManager.OP_LOCATION, mUid, mPackageName)
-                    != AppOpsManager.MODE_ALLOWED) {
+            if (!reportLocationAccessNoThrow(mUid, mPackageName, mAllowedResolutionLevel)) {
                 return true;
             }
             if (mListener != null) {
@@ -803,6 +802,36 @@
         }
     }
 
+    boolean reportLocationAccessNoThrow(int uid, String packageName, int allowedResolutionLevel) {
+        int op;
+        if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
+            if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
+                op = AppOpsManager.OP_COARSE_LOCATION;
+            } else {
+                op = AppOpsManager.OP_FINE_LOCATION;
+            }
+            if (mAppOps.noteOpNoThrow(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    boolean checkLocationAccess(int uid, String packageName, int allowedResolutionLevel) {
+        int op;
+        if (allowedResolutionLevel != RESOLUTION_LEVEL_NONE) {
+            if (allowedResolutionLevel == RESOLUTION_LEVEL_COARSE) {
+                op = AppOpsManager.OP_COARSE_LOCATION;
+            } else {
+                op = AppOpsManager.OP_FINE_LOCATION;
+            }
+            if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     /**
      * Returns all providers by name, including passive, but excluding
      * fused, also including ones that are not permitted to
@@ -1199,7 +1228,7 @@
         try {
             // We don't check for MODE_IGNORED here; we will do that when we go to deliver
             // a location.
-            mAppOps.noteOp(AppOpsManager.OP_LOCATION, uid, packageName);
+            checkLocationAccess(uid, packageName, allowedResolutionLevel);
             Receiver recevier = checkListenerOrIntent(listener, intent, pid, uid, packageName);
 
             synchronized (mLock) {
@@ -1311,8 +1340,7 @@
         final int uid = Binder.getCallingUid();
         final long identity = Binder.clearCallingIdentity();
         try {
-            if (mAppOps.noteOp(AppOpsManager.OP_LOCATION, uid, packageName)
-                    != AppOpsManager.MODE_ALLOWED) {
+            if (!reportLocationAccessNoThrow(uid, packageName, allowedResolutionLevel)) {
                 return null;
             }
             
@@ -1403,14 +1431,14 @@
         if (mGpsStatusProvider == null) {
             return false;
         }
-        checkResolutionLevelIsSufficientForProviderUse(getCallerAllowedResolutionLevel(),
+        int allowedResolutionLevel = getCallerAllowedResolutionLevel();
+        checkResolutionLevelIsSufficientForProviderUse(allowedResolutionLevel,
                 LocationManager.GPS_PROVIDER);
 
         final int uid = Binder.getCallingUid();
         final long ident = Binder.clearCallingIdentity();
         try {
-            if (mAppOps.noteOp(AppOpsManager.OP_LOCATION, uid, packageName)
-                    != AppOpsManager.MODE_ALLOWED) {
+            if (checkLocationAccess(uid, packageName, allowedResolutionLevel)) {
                 return false;
             }
         } finally {