Reissue MWI dismissal when restoring cellular signal.

A race condition can happen if a voicemail message is received when the
signal is down. When the signal comes up again, the phone state listener
for visual voicemail will update the voicemail status table to indicate
that visual voicemail is available, at the same time, the MWI updates
with the new voicemail. Issue an update to the MWI after the voicemail
status table is updated.
- Move VvmPhoneStateListener to com.android.phone because
  NotificationMgr is only visible to the package

Bug: 22375837
Change-Id: I89b10be981da958427ec708bac3962aae04f2018
diff --git a/src/com/android/phone/vvm/omtp/sync/VvmPhoneStateListener.java b/src/com/android/phone/VvmPhoneStateListener.java
similarity index 70%
rename from src/com/android/phone/vvm/omtp/sync/VvmPhoneStateListener.java
rename to src/com/android/phone/VvmPhoneStateListener.java
index 84449d7..d2d84b2 100644
--- a/src/com/android/phone/vvm/omtp/sync/VvmPhoneStateListener.java
+++ b/src/com/android/phone/VvmPhoneStateListener.java
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License
  */
-package com.android.phone.vvm.omtp.sync;
+package com.android.phone;
 
 import android.content.Context;
 import android.content.Intent;
@@ -22,8 +22,10 @@
 import android.telephony.PhoneStateListener;
 import android.telephony.ServiceState;
 
-import com.android.phone.PhoneUtils;
 import com.android.phone.vvm.omtp.OmtpVvmCarrierConfigHelper;
+import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
+import com.android.phone.vvm.omtp.sync.OmtpVvmSyncService;
+import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
 
 /**
  * Check if service is lost and indicate this in the voicemail status.
@@ -49,15 +51,22 @@
                             VoicemailContract.Status.CONFIGURATION_STATE_OK,
                             VoicemailContract.Status.DATA_CHANNEL_STATE_OK,
                             VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE_OK);
-                    // Run a full sync in case something was missed while signal was down.
-                    Intent serviceIntent = OmtpVvmSyncService.getSyncIntent(
-                            mContext, OmtpVvmSyncService.SYNC_FULL_SYNC, mPhoneAccount,
-                            true /* firstAttempt */);
-                    mContext.startService(serviceIntent);
+                    PhoneGlobals.getInstance().notificationMgr.updateMwi(
+                            PhoneUtils.getSubIdForPhoneAccountHandle(mPhoneAccount), false);
                 }
             }
 
-            if (!OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
+            if (OmtpVvmSourceManager.getInstance(mContext).isVvmSourceRegistered(mPhoneAccount)) {
+                // If the source is already registered, run a full sync in case something was missed
+                // while signal was down.
+                Intent serviceIntent = OmtpVvmSyncService.getSyncIntent(
+                        mContext, OmtpVvmSyncService.SYNC_FULL_SYNC, mPhoneAccount,
+                        true /* firstAttempt */);
+                mContext.startService(serviceIntent);
+            } else {
+                // Otherwise initiate an activation because this means that an OMTP source was
+                // recognized but either the activation text was not successfully sent or a response
+                // was not received.
                 OmtpVvmCarrierConfigHelper carrierConfigHelper = new OmtpVvmCarrierConfigHelper(
                         mContext, PhoneUtils.getSubIdForPhoneAccountHandle(mPhoneAccount));
                 carrierConfigHelper.startActivation();
diff --git a/src/com/android/phone/settings/VoicemailSettingsActivity.java b/src/com/android/phone/settings/VoicemailSettingsActivity.java
index fdc5bf3..bb8c6fe 100644
--- a/src/com/android/phone/settings/VoicemailSettingsActivity.java
+++ b/src/com/android/phone/settings/VoicemailSettingsActivity.java
@@ -392,6 +392,7 @@
             boolean isEnabled = (Boolean) objValue;
             VisualVoicemailSettingsUtil.setVisualVoicemailEnabled(mPhone, isEnabled, true);
             if (isEnabled) {
+                OmtpVvmSourceManager.getInstance(mPhone.getContext()).addPhoneStateListener(mPhone);
                 mOmtpVvmCarrierConfigHelper.startActivation();
             } else {
                 OmtpVvmSourceManager.getInstance(mPhone.getContext()).removeSource(mPhone);
diff --git a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java
index 63e9fee..286dde3 100644
--- a/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java
+++ b/src/com/android/phone/vvm/omtp/sync/OmtpVvmSourceManager.java
@@ -24,6 +24,7 @@
 
 import com.android.internal.telephony.Phone;
 import com.android.phone.PhoneUtils;
+import com.android.phone.VvmPhoneStateListener;
 
 import java.util.HashMap;
 import java.util.HashSet;
@@ -104,6 +105,10 @@
         OmtpVvmSyncService.cancelAllRetries(mContext, phoneAccount);
     }
 
+    public void addPhoneStateListener(Phone phone) {
+        addPhoneStateListener(PhoneUtils.makePstnPhoneAccountHandle(phone));
+    }
+
     public void addPhoneStateListener(PhoneAccountHandle phoneAccount) {
         if (!mPhoneStateListenerMap.containsKey(phoneAccount)) {
             VvmPhoneStateListener phoneStateListener = new VvmPhoneStateListener(mContext,