Use checkOp instead noteOp for location permission checks.

Also don't report location access in getLastLocation() unless there
exists a location that can be delivered to the client.

Bug: 123533751
Bug: 122748680
Test: Manual
Change-Id: I62897cefbfb35422a076c9a5cd0e1d0df62a39b1
diff --git a/services/core/java/com/android/server/LocationManagerService.java b/services/core/java/com/android/server/LocationManagerService.java
index 1165b09..ba40192 100644
--- a/services/core/java/com/android/server/LocationManagerService.java
+++ b/services/core/java/com/android/server/LocationManagerService.java
@@ -1369,7 +1369,7 @@
                 }
             } else {
                 if (!allowMonitoring
-                        || mAppOps.noteOpNoThrow(op, mCallerIdentity.mUid,
+                        || mAppOps.checkOpNoThrow(op, mCallerIdentity.mUid,
                         mCallerIdentity.mPackageName) != AppOpsManager.MODE_ALLOWED) {
                     mAppOps.finishOp(op, mCallerIdentity.mUid, mCallerIdentity.mPackageName);
                     return false;
@@ -1904,7 +1904,7 @@
             int allowedResolutionLevel) {
         int op = resolutionLevelToOp(allowedResolutionLevel);
         if (op >= 0) {
-            if (mAppOps.noteOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
+            if (mAppOps.checkOp(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) {
                 return false;
             }
         }
@@ -2549,13 +2549,6 @@
                     return null;
                 }
 
-                if (!reportLocationAccessNoThrow(pid, uid, packageName, allowedResolutionLevel)) {
-                    if (D) {
-                        Log.d(TAG, "not returning last loc for no op app: "
-                                + packageName);
-                    }
-                    return null;
-                }
 
                 // Figure out the provider. Either its explicitly request (deprecated API's),
                 // or use the fused provider
@@ -2599,16 +2592,27 @@
                     return null;
                 }
 
+                Location lastLocation = null;
                 if (allowedResolutionLevel < RESOLUTION_LEVEL_FINE) {
                     Location noGPSLocation = location.getExtraLocation(
                             Location.EXTRA_NO_GPS_LOCATION);
                     if (noGPSLocation != null) {
-                        return new Location(mLocationFudger.getOrCreate(noGPSLocation));
+                        lastLocation = new Location(mLocationFudger.getOrCreate(noGPSLocation));
                     }
                 } else {
-                    return new Location(location);
+                    lastLocation = new Location(location);
                 }
-                return null;
+                // Don't report location access if there is no last location to deliver.
+                if (lastLocation != null) {
+                    if (!reportLocationAccessNoThrow(
+                            pid, uid, packageName, allowedResolutionLevel)) {
+                        if (D) {
+                            Log.d(TAG, "not returning last loc for no op app: " + packageName);
+                        }
+                        lastLocation =  null;
+                    }
+                }
+                return lastLocation;
             } finally {
                 Binder.restoreCallingIdentity(identity);
             }
@@ -3249,7 +3253,7 @@
     // Mock Providers
 
     private boolean canCallerAccessMockLocation(String opPackageName) {
-        return mAppOps.noteOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(),
+        return mAppOps.checkOp(AppOpsManager.OP_MOCK_LOCATION, Binder.getCallingUid(),
                 opPackageName) == AppOpsManager.MODE_ALLOWED;
     }