Throw if location permission not held

Test: cts-tradefed run cts-dev -m CtsPermissionTestCases
              -t android.permission.cts.NoLocationPermissionTest

bug:62899976

Change-Id: I5291841af84be63deaf4db806392e1c7ce670be5
diff --git a/src/com/android/phone/LocationAccessPolicy.java b/src/com/android/phone/LocationAccessPolicy.java
index 9f863a3..6f2a5ec 100644
--- a/src/com/android/phone/LocationAccessPolicy.java
+++ b/src/com/android/phone/LocationAccessPolicy.java
@@ -45,18 +45,27 @@
      *
      * @param pkgName Package name of the application requesting access
      * @param uid The uid of the package
+     * @param message Message to add to the exception if no location permission
      * @return boolean true or false if permissions is granted
      */
     static boolean canAccessCellLocation(@NonNull Context context, @NonNull String pkgName,
-            int uid) throws SecurityException {
+            int uid, String message) throws SecurityException {
         context.getSystemService(AppOpsManager.class).checkPackage(uid, pkgName);
         // We always require the location permission and also require the
         // location mode to be on for non-legacy apps. Legacy apps are
         // required to be in the foreground to at least mitigate the case
         // where a legacy app the user is not using tracks their location.
-        if (!hasUidLocationPermission(context, pkgName, uid)
-                || (!isLocationModeEnabled(context, UserHandle.getUserId(uid)))
-                        && !isLegacyForeground(context, pkgName)) {
+
+        // Grating ACCESS_FINE_LOCATION to an app automatically grants it ACCESS_COARSE_LOCATION.
+        context.enforceCallingOrSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION, message);
+        final int opCode = AppOpsManager.permissionToOpCode(
+                Manifest.permission.ACCESS_COARSE_LOCATION);
+        if (opCode != AppOpsManager.OP_NONE && context.getSystemService(AppOpsManager.class)
+                .noteOp(opCode, uid, pkgName) != AppOpsManager.MODE_ALLOWED) {
+            return false;
+        }
+        if (!isLocationModeEnabled(context, UserHandle.getUserId(uid))
+                && !isLegacyForeground(context, pkgName)) {
             return false;
         }
         // If the user or profile is current, permission is granted.
@@ -103,21 +112,6 @@
                 == PackageManager.PERMISSION_GRANTED;
     }
 
-    private static boolean hasUidLocationPermission(@NonNull Context context,
-            @NonNull String pkgName, int uid) {
-        // Grating ACCESS_FINE_LOCATION to an app automatically grants it ACCESS_COARSE_LOCATION.
-        if ((context.checkCallingOrSelfPermission(
-                Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
-            final int opCode = AppOpsManager.permissionToOpCode(
-                    Manifest.permission.ACCESS_COARSE_LOCATION);
-            if (opCode != AppOpsManager.OP_NONE) {
-                return context.getSystemService(AppOpsManager.class).noteOp(opCode, uid, pkgName)
-                        == AppOpsManager.MODE_ALLOWED;
-            }
-        }
-        return false;
-    }
-
     private static boolean isCurrentProfile(@NonNull Context context, int uid) {
         final int currentUser = ActivityManager.getCurrentUser();
         final int callingUserId = UserHandle.getUserId(uid);
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 204afd3..59b7684 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -1641,7 +1641,7 @@
     @Override
     public Bundle getCellLocation(String callingPackage) {
         if (!LocationAccessPolicy.canAccessCellLocation(mPhone.getContext(),
-                callingPackage, Binder.getCallingUid())) {
+                callingPackage, Binder.getCallingUid(), "getCellLocation")) {
             return null;
         }
 
@@ -1709,7 +1709,7 @@
     @SuppressWarnings("unchecked")
     public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
         if (!LocationAccessPolicy.canAccessCellLocation(mPhone.getContext(),
-                callingPackage, Binder.getCallingUid())) {
+                callingPackage, Binder.getCallingUid(), "getNeighboringCellInfo")) {
             return null;
         }
 
@@ -1737,7 +1737,7 @@
     @Override
     public List<CellInfo> getAllCellInfo(String callingPackage) {
         if (!LocationAccessPolicy.canAccessCellLocation(mPhone.getContext(),
-                callingPackage, Binder.getCallingUid())) {
+                callingPackage, Binder.getCallingUid(), "getAllCellInfo")) {
             return null;
         }