Merge "Fix some VVM status issues" into mnc-dev
diff --git a/src/com/android/phone/VvmPhoneStateListener.java b/src/com/android/phone/VvmPhoneStateListener.java
index fb267cd..53eff79 100644
--- a/src/com/android/phone/VvmPhoneStateListener.java
+++ b/src/com/android/phone/VvmPhoneStateListener.java
@@ -98,6 +98,10 @@
                     mContext, OmtpVvmSyncService.SYNC_FULL_SYNC, mPhoneAccount,
                     true /* firstAttempt */));
 
+            if (!OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
+                return;
+            }
+
             VoicemailContract.Status.setStatus(mContext, mPhoneAccount,
                     VoicemailContract.Status.CONFIGURATION_STATE_OK,
                     VoicemailContract.Status.DATA_CHANNEL_STATE_NO_CONNECTION,
diff --git a/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java b/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java
index 1caefe6..4260a37 100644
--- a/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java
+++ b/src/com/android/phone/vvm/omtp/sms/OmtpMessageReceiver.java
@@ -23,6 +23,7 @@
 import android.telecom.PhoneAccountHandle;
 import android.telecom.Voicemail;
 import android.telephony.SmsMessage;
+import android.telephony.SubscriptionManager;
 import android.util.Log;
 
 import com.android.internal.telephony.PhoneConstants;
@@ -49,6 +50,11 @@
         mPhoneAccount = PhoneUtils.makePstnPhoneAccountHandle(
                 intent.getExtras().getInt(PhoneConstants.PHONE_KEY));
 
+        if (mPhoneAccount == null) {
+            Log.w(TAG, "Received message for null phone account");
+            return;
+        }
+
         if (!VisualVoicemailSettingsUtil.isVisualVoicemailEnabled(mContext, mPhoneAccount)) {
             Log.v(TAG, "Received vvm message for disabled vvm source.");
             return;
diff --git a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java
index 286dde3..7668dad 100644
--- a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java
+++ b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java
@@ -26,10 +26,10 @@
 import com.android.phone.PhoneUtils;
 import com.android.phone.VvmPhoneStateListener;
 
-import java.util.HashMap;
-import java.util.HashSet;
+import java.util.Collections;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * A singleton class designed to remember the active OMTP visual voicemail sources. Because a
@@ -69,8 +69,10 @@
             mSubscriptionManager = SubscriptionManager.from(context);
             mTelephonyManager = (TelephonyManager)
                     mContext.getSystemService(Context.TELEPHONY_SERVICE);
-            mActiveVvmSources = new HashSet<PhoneAccountHandle>();
-            mPhoneStateListenerMap = new HashMap<PhoneAccountHandle, PhoneStateListener>();
+            mActiveVvmSources = Collections.newSetFromMap(
+                    new ConcurrentHashMap<PhoneAccountHandle, Boolean>(8, 0.9f, 1));
+            mPhoneStateListenerMap =
+                    new ConcurrentHashMap<PhoneAccountHandle, PhoneStateListener>(8, 0.9f, 1);
         }
     }
 
@@ -89,6 +91,13 @@
                 removeSource(phoneAccount);
             }
         }
+
+        // Remove any orphaned phone state listeners as well.
+        for (PhoneAccountHandle phoneAccount : mPhoneStateListenerMap.keySet()) {
+            if (!PhoneUtils.isPhoneAccountActive(mSubscriptionManager, phoneAccount)) {
+                removePhoneStateListener(phoneAccount);
+            }
+        }
     }
 
     public void removeSource(Phone phone) {