Merge "Add target package to VisualVoicemailFilterSettings."
diff --git a/src/com/android/phone/vvm/RemoteVvmTaskManager.java b/src/com/android/phone/vvm/RemoteVvmTaskManager.java
index 4fc8c57..3a18c7e 100644
--- a/src/com/android/phone/vvm/RemoteVvmTaskManager.java
+++ b/src/com/android/phone/vvm/RemoteVvmTaskManager.java
@@ -69,6 +69,8 @@
             "com.android.phone.vvm.ACTION_VISUAL_VOICEMAIL_SERVICE_EVENT";
     private static final String EXTRA_WHAT = "what";
 
+    private static final String EXTRA_TARGET_PACKAGE = "target_package";
+
     // TODO(twyen): track task individually to have time outs.
     private int mTaskReferenceCount;
 
@@ -79,7 +81,7 @@
      */
     private Messenger mMessenger;
 
-    public static void startCellServiceConnected(Context context,
+    static void startCellServiceConnected(Context context,
             PhoneAccountHandle phoneAccountHandle) {
         Intent intent = new Intent(ACTION_START_CELL_SERVICE_CONNECTED, null, context,
                 RemoteVvmTaskManager.class);
@@ -87,28 +89,46 @@
         context.startService(intent);
     }
 
-    public static void startSmsReceived(Context context, VisualVoicemailSms sms) {
+    static void startSmsReceived(Context context, VisualVoicemailSms sms,
+            String targetPackage) {
         Intent intent = new Intent(ACTION_START_SMS_RECEIVED, null, context,
                 RemoteVvmTaskManager.class);
         intent.putExtra(VisualVoicemailService.DATA_PHONE_ACCOUNT_HANDLE,
                 sms.getPhoneAccountHandle());
         intent.putExtra(VisualVoicemailService.DATA_SMS, sms);
+        intent.putExtra(EXTRA_TARGET_PACKAGE, targetPackage);
         context.startService(intent);
     }
 
-    public static void startSimRemoved(Context context, PhoneAccountHandle phoneAccountHandle) {
+    static void startSimRemoved(Context context, PhoneAccountHandle phoneAccountHandle) {
         Intent intent = new Intent(ACTION_START_SIM_REMOVED, null, context,
                 RemoteVvmTaskManager.class);
         intent.putExtra(VisualVoicemailService.DATA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
         context.startService(intent);
     }
 
-    public static boolean hasRemoteService(Context context, int subId) {
-        return getRemotePackage(context, subId) != null;
+    static boolean hasRemoteService(Context context, int subId, String targetPackage) {
+        return getRemotePackage(context, subId, targetPackage) != null;
     }
 
+    /**
+     * Return the {@link ComponentName} of the {@link VisualVoicemailService} which is active (the
+     * current default dialer), or {@code null} if no implementation is found.
+     */
     @Nullable
     public static ComponentName getRemotePackage(Context context, int subId) {
+        return getRemotePackage(context, subId, null);
+    }
+
+    /**
+     * Return the {@link ComponentName} of the {@link VisualVoicemailService} which is active (the
+     * current default dialer), or {@code null} if no implementation is found.
+     *
+     * @param targetPackage the package that should be the active VisualVociemailService
+     */
+    @Nullable
+    public static ComponentName getRemotePackage(Context context, int subId,
+            @Nullable String targetPackage) {
         ComponentName broadcastPackage = getBroadcastPackage(context);
         if (broadcastPackage != null) {
             return broadcastPackage;
@@ -122,7 +142,8 @@
         PersistableBundle carrierConfig = context
                 .getSystemService(CarrierConfigManager.class).getConfigForSubId(subId);
         packages.add(
-                carrierConfig.getString(CarrierConfigManager.KEY_CARRIER_VVM_PACKAGE_NAME_STRING));
+                carrierConfig
+                        .getString(CarrierConfigManager.KEY_CARRIER_VVM_PACKAGE_NAME_STRING));
         String[] vvmPackages = carrierConfig
                 .getStringArray(CarrierConfigManager.KEY_CARRIER_VVM_PACKAGE_NAME_STRING_ARRAY);
         if (vvmPackages != null && vvmPackages.length > 0) {
@@ -132,6 +153,7 @@
         }
         packages.add(context.getResources().getString(R.string.system_visual_voicemail_client));
         packages.add(telecomManager.getSystemDialerPackage());
+
         for (String packageName : packages) {
             if (TextUtils.isEmpty(packageName)) {
                 continue;
@@ -153,7 +175,10 @@
                         + " does not enforce BIND_VISUAL_VOICEMAIL_SERVICE, ignoring");
                 continue;
             }
-
+            if (targetPackage != null && !TextUtils.equals(packageName, targetPackage)) {
+                VvmLog.w(TAG, "target package " + targetPackage
+                        + " is no longer the active VisualVoicemailService, ignoring");
+            }
             return info.getComponentInfo().getComponentName();
 
         }
@@ -203,7 +228,8 @@
         PhoneAccountHandle phoneAccountHandle = intent.getExtras()
                 .getParcelable(VisualVoicemailService.DATA_PHONE_ACCOUNT_HANDLE);
         int subId = PhoneAccountHandleConverter.toSubId(phoneAccountHandle);
-        ComponentName remotePackage = getRemotePackage(this, subId);
+        ComponentName remotePackage = getRemotePackage(this, subId,
+                intent.getStringExtra(EXTRA_TARGET_PACKAGE));
         if (remotePackage == null) {
             VvmLog.i(TAG, "No service to handle " + intent.getAction() + ", ignoring");
             checkReference();
diff --git a/src/com/android/phone/vvm/VisualVoicemailSmsFilterConfig.java b/src/com/android/phone/vvm/VisualVoicemailSmsFilterConfig.java
index 058f18e..ecd1f0f 100644
--- a/src/com/android/phone/vvm/VisualVoicemailSmsFilterConfig.java
+++ b/src/com/android/phone/vvm/VisualVoicemailSmsFilterConfig.java
@@ -92,6 +92,7 @@
                         VisualVoicemailSmsFilterSettings.DEFAULT_ORIGINATING_NUMBERS))
                 .setDestinationPort(reader.getInt(DESTINATION_PORT_KEY,
                         VisualVoicemailSmsFilterSettings.DEFAULT_DESTINATION_PORT))
+                .setPackageName(packageName)
                 .build();
     }
 
diff --git a/src/com/android/phone/vvm/VvmSmsReceiver.java b/src/com/android/phone/vvm/VvmSmsReceiver.java
index 4bffa97..8265e50 100644
--- a/src/com/android/phone/vvm/VvmSmsReceiver.java
+++ b/src/com/android/phone/vvm/VvmSmsReceiver.java
@@ -36,7 +36,6 @@
     public void onReceive(Context context, Intent intent) {
         VisualVoicemailSms sms = intent.getExtras()
                 .getParcelable(VoicemailContract.EXTRA_VOICEMAIL_SMS);
-
         if (sms.getPhoneAccountHandle() == null) {
             // This should never happen
             VvmLog.e(TAG, "Received message for null phone account");
@@ -49,11 +48,12 @@
             return;
         }
 
-        if (RemoteVvmTaskManager.hasRemoteService(context, subId)) {
+        String targetPackage = intent.getExtras().getString(VoicemailContract.EXTRA_TARGET_PACKAGE);
+        if (RemoteVvmTaskManager.hasRemoteService(context, subId, targetPackage)) {
             VvmLog.i(TAG, "Sending SMS received event to remote service");
-            RemoteVvmTaskManager.startSmsReceived(context, sms);
+            RemoteVvmTaskManager.startSmsReceived(context, sms, targetPackage);
         } else {
-            VvmLog.w(TAG, "Sending SMS received event to remote service");
-        };
+            VvmLog.w(TAG, "No remote service to handle SMS received event");
+        }
     }
 }