Enforce VisualVoicemailService to require BIND_VISUAL_VOICEMAIL_SERVICE

Also fixes NPE in enforceVisualVoicemailPackage()

Change-Id: I6a9a6ea33fc939cf0926e9d5ec4beeb0f312e24c
Fixes: 35854031
Test: CTS - VisualVoicemailServiceTest
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 5cd1d85..d2e635e 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -3537,8 +3537,12 @@
      * @throws SecurityException if the caller is not the visual voicemail package.
      */
     private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
-        String vvmPackage = RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId)
-                .getPackageName();
+        ComponentName componentName =
+                RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId);
+        if(componentName == null) {
+            throw new SecurityException("Caller not current active visual voicemail package[null]");
+        }
+        String vvmPackage = componentName.getPackageName();
         if (!callingPackage.equals(vvmPackage)) {
             throw new SecurityException("Caller not current active visual voicemail package[" +
                     vvmPackage + "]");
diff --git a/src/com/android/phone/vvm/RemoteVvmTaskManager.java b/src/com/android/phone/vvm/RemoteVvmTaskManager.java
index f48fc7e..ca971d1 100644
--- a/src/com/android/phone/vvm/RemoteVvmTaskManager.java
+++ b/src/com/android/phone/vvm/RemoteVvmTaskManager.java
@@ -131,9 +131,22 @@
             bindIntent.setPackage(packageName);
             ResolveInfo info = context.getPackageManager()
                     .resolveService(bindIntent, PackageManager.MATCH_ALL);
-            if (info != null) {
-                return info.getComponentInfo().getComponentName();
+            if (info == null) {
+                continue;
             }
+            if(info.serviceInfo == null){
+                VvmLog.w(TAG,
+                        "Component " + info.getComponentInfo() + " is not a service, ignoring");
+                continue;
+            }
+            if (!android.Manifest.permission.BIND_VISUAL_VOICEMAIL_SERVICE
+                    .equals(info.serviceInfo.permission)) {
+                VvmLog.w(TAG, "package " + info.serviceInfo.packageName
+                        + " does not enforce BIND_VISUAL_VOICEMAIL_SERVICE, ignoring");
+                continue;
+            }
+
+            return info.getComponentInfo().getComponentName();
 
         }
         return null;