Move getNeighboringCellInfo() SDK Level Check

Move the SDK level check to inside the phone process
for safety. This CL also adds a (hopefully) reusable
function to get the target SDK level for a package.

Bug: 117520186
Test: atest CtsPermissionTestCasesSdk28
Test: manual with SL4A at targetSdk=27,28
Change-Id: Ib68aa5cfc8a4f6c5c93b2b0e2ddfe6709be6cea8
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 8866d30..4be94f4 100755
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -25,6 +25,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.ComponentInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
@@ -1807,10 +1808,25 @@
         }
     }
 
+    /**
+     * Returns the target SDK version number for a given package name.
+     *
+     * @return target SDK if the package is found or INT_MAX.
+     */
+    private int getTargetSdk(String packageName) {
+        try {
+            final ApplicationInfo ai =
+                    mPhone.getContext().getPackageManager().getApplicationInfo(packageName, 0);
+            if (ai != null) return ai.targetSdkVersion;
+        } catch (PackageManager.NameNotFoundException unexpected) {
+        }
+        return Integer.MAX_VALUE;
+    }
+
     @Override
     @SuppressWarnings("unchecked")
-    public List<NeighboringCellInfo>
-            getNeighboringCellInfo(String callingPackage, int targetSdk) {
+    public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
+        final int targetSdk = getTargetSdk(callingPackage);
         if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
             throw new SecurityException(
                     "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");