Disable MWI suppression if VVM cannot be activated

MWI is the traditional voicemail notification which should be suppressed
if visual voicemail is active. NotificationMgr.updateMwi() currently
checks the CONFIGURATION_STATE to suppress the MWI, but there are several
issues. b/31229016 is a bug that when the device boots the configuration
state will be cleared and the MWI for voicemail that arrives when the
device is offline will be cleared, even if the account cannot be
activated. A full solution will be adding a setMwiEnabled() method and
stop checking the configuration state, but that is too risky at this
moment (ag/1467545). This is a temporary workaround to shut down the
configuration state check if visual voicemail cannot be activated.

+ Don't clear MWI when signal is restored. Checking the notification
  state to hide MWI is obsolete in ag/1333776

Change-Id: I03b7e5cde2aa7ad4c1a31526eaf63d6832717e0a
Fixes: 31229016
diff --git a/src/com/android/phone/NotificationMgr.java b/src/com/android/phone/NotificationMgr.java
index 9504f34..d40b08e 100644
--- a/src/com/android/phone/NotificationMgr.java
+++ b/src/com/android/phone/NotificationMgr.java
@@ -47,11 +47,13 @@
 import android.util.ArrayMap;
 import android.util.Log;
 import android.widget.Toast;
+
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.TelephonyCapabilities;
 import com.android.phone.settings.VoicemailNotificationSettingsUtil;
 import com.android.phone.settings.VoicemailSettingsActivity;
 import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
+
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
@@ -72,6 +74,9 @@
     // Do not check in with VDBG = true, since that may write PII to the system log.
     private static final boolean VDBG = false;
 
+    private static final String MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX =
+            "mwi_should_check_vvm_configuration_state_";
+
     // notification types
     static final int MMI_NOTIFICATION = 1;
     static final int NETWORK_SELECTION_NOTIFICATION = 2;
@@ -201,6 +206,27 @@
         }
     }
 
+    public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
+        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
+            Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
+                    + subId);
+            return;
+        }
+
+        PreferenceManager.getDefaultSharedPreferences(mContext).edit()
+                .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
+                .apply();
+    }
+
+    private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
+        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
+            Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
+            return true;
+        }
+        return PreferenceManager
+                .getDefaultSharedPreferences(mContext)
+                .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
+    }
     /**
      * Updates the message waiting indicator (voicemail) notification.
      *
@@ -226,7 +252,7 @@
         }
 
         Phone phone = PhoneGlobals.getPhone(subId);
-        if (visible && phone != null) {
+        if (visible && phone != null && shouldCheckVisualVoicemailConfigurationForMwi(subId)) {
             VoicemailStatusQueryHelper queryHelper = new VoicemailStatusQueryHelper(mContext);
             PhoneAccountHandle phoneAccount = PhoneUtils.makePstnPhoneAccountHandle(phone);
             if (queryHelper.isVoicemailSourceConfigured(phoneAccount)) {
diff --git a/src/com/android/phone/PhoneGlobals.java b/src/com/android/phone/PhoneGlobals.java
index 2d87e78..14a451f 100644
--- a/src/com/android/phone/PhoneGlobals.java
+++ b/src/com/android/phone/PhoneGlobals.java
@@ -45,6 +45,7 @@
 import android.telephony.SubscriptionManager;
 import android.util.Log;
 import android.widget.Toast;
+
 import com.android.internal.telephony.Call;
 import com.android.internal.telephony.CallManager;
 import com.android.internal.telephony.IccCardConstants;
@@ -856,4 +857,23 @@
             phone.setVoiceMessageCount(0);
         }
     }
+
+    /**
+     * Enables or disables the visual voicemail check for message waiting indicator. Default value
+     * is true. MWI is the traditional voicemail notification which should be suppressed if visual
+     * voicemail is active. {@link NotificationMgr#updateMwi(int, boolean, boolean)} currently
+     * checks the {@link android.provider.VoicemailContract.Status#CONFIGURATION_STATE} to suppress
+     * the MWI, but there are several issues. b/31229016 is a bug that when the device boots the
+     * configuration state will be cleared and the MWI for voicemail that arrives when the device
+     * is offline will be cleared, even if the account cannot be activated. A full solution will be
+     * adding a setMwiEnabled() method and stop checking the configuration state, but that is too
+     * risky at this moment. This is a temporary workaround to shut down the configuration state
+     * check if visual voicemail cannot be activated.
+     * <p>TODO(twyen): implement the setMwiEnabled() mentioned above.
+     *
+     * @param subId the account to set the enabled state
+     */
+    public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
+        notificationMgr.setShouldCheckVisualVoicemailConfigurationForMwi(subId, enabled);
+    }
 }
diff --git a/src/com/android/phone/vvm/omtp/ActivationTask.java b/src/com/android/phone/vvm/omtp/ActivationTask.java
index e1fea4d..3a9893a 100644
--- a/src/com/android/phone/vvm/omtp/ActivationTask.java
+++ b/src/com/android/phone/vvm/omtp/ActivationTask.java
@@ -28,6 +28,7 @@
 import android.telecom.PhoneAccountHandle;
 import android.telephony.ServiceState;
 import android.telephony.TelephonyManager;
+
 import com.android.phone.Assert;
 import com.android.phone.PhoneGlobals;
 import com.android.phone.VoicemailStatus;
@@ -40,6 +41,7 @@
 import com.android.phone.vvm.omtp.sync.OmtpVvmSyncService;
 import com.android.phone.vvm.omtp.sync.SyncTask;
 import com.android.phone.vvm.omtp.utils.PhoneAccountHandleConverter;
+
 import java.io.IOException;
 import java.util.HashSet;
 import java.util.Set;
@@ -237,6 +239,7 @@
             } else {
                 VvmLog.i(TAG, "Subscriber not ready but provisioning is not supported");
                 helper.handleEvent(status, OmtpEvents.CONFIG_SERVICE_NOT_AVAILABLE);
+                PhoneGlobals.getInstance().setShouldCheckVisualVoicemailConfigurationForMwi(subId, false);
             }
         }
     }
@@ -258,8 +261,10 @@
             vvmSourceManager.addSource(phone);
 
             SyncTask.start(context, phone, OmtpVvmSyncService.SYNC_FULL_SYNC);
-            // Remove the message waiting indicator, which is a stick notification fo traditional
+            // Remove the message waiting indicator, which is a sticky notification for traditional
             // voicemails.
+            PhoneGlobals.getInstance()
+                    .setShouldCheckVisualVoicemailConfigurationForMwi(subId, true);
             PhoneGlobals.getInstance().clearMwiIndicator(subId);
         } else {
             VvmLog.e(TAG, "Visual voicemail not available for subscriber.");
diff --git a/src/com/android/phone/vvm/omtp/VvmPhoneStateListener.java b/src/com/android/phone/vvm/omtp/VvmPhoneStateListener.java
index 94e616c..7e2472b 100644
--- a/src/com/android/phone/vvm/omtp/VvmPhoneStateListener.java
+++ b/src/com/android/phone/vvm/omtp/VvmPhoneStateListener.java
@@ -20,7 +20,7 @@
 import android.telephony.PhoneStateListener;
 import android.telephony.ServiceState;
 import android.telephony.SubscriptionManager;
-import com.android.phone.PhoneGlobals;
+
 import com.android.phone.PhoneUtils;
 import com.android.phone.VoicemailStatus;
 import com.android.phone.vvm.omtp.sync.OmtpVvmSourceManager;
@@ -75,7 +75,6 @@
                             .v(TAG, "Notifications channel is active for " + subId);
                     helper.handleEvent(VoicemailStatus.edit(mContext, mPhoneAccount),
                         OmtpEvents.NOTIFICATION_IN_SERVICE);
-                    PhoneGlobals.getInstance().clearMwiIndicator(subId);
                 }
             }
 
diff --git a/src/com/android/phone/vvm/omtp/protocol/Vvm3Protocol.java b/src/com/android/phone/vvm/omtp/protocol/Vvm3Protocol.java
index 72044e2..93e8fb9 100644
--- a/src/com/android/phone/vvm/omtp/protocol/Vvm3Protocol.java
+++ b/src/com/android/phone/vvm/omtp/protocol/Vvm3Protocol.java
@@ -24,6 +24,8 @@
 import android.telecom.PhoneAccountHandle;
 import android.telephony.SmsManager;
 import android.text.TextUtils;
+
+import com.android.phone.PhoneGlobals;
 import com.android.phone.VoicemailStatus;
 import com.android.phone.common.mail.MessagingException;
 import com.android.phone.settings.VisualVoicemailSettingsUtil;
@@ -42,6 +44,7 @@
 import com.android.phone.vvm.omtp.sync.VvmNetworkRequest;
 import com.android.phone.vvm.omtp.sync.VvmNetworkRequest.NetworkWrapper;
 import com.android.phone.vvm.omtp.sync.VvmNetworkRequest.RequestFailedException;
+
 import java.io.IOException;
 import java.security.SecureRandom;
 import java.util.Locale;
@@ -116,6 +119,8 @@
                 new Vvm3Subscriber(task, phoneAccountHandle, config, status, data).subscribe();
             } else {
                 config.handleEvent(status, OmtpEvents.VVM3_SUBSCRIBER_UNKNOWN);
+                PhoneGlobals.getInstance().setShouldCheckVisualVoicemailConfigurationForMwi(task.getSubId(),
+                        false);
             }
         } else if (OmtpConstants.SUBSCRIBER_NEW.equals(message.getProvisioningStatus())) {
             VvmLog.i(TAG, "setting up new user");
@@ -129,9 +134,13 @@
             VvmLog.i(TAG, "User provisioned but not activated, disabling VVM");
             VisualVoicemailSettingsUtil
                     .setEnabled(config.getContext(), phoneAccountHandle, false);
+            PhoneGlobals.getInstance().setShouldCheckVisualVoicemailConfigurationForMwi(task.getSubId(),
+                    false);
         } else if (OmtpConstants.SUBSCRIBER_BLOCKED.equals(message.getProvisioningStatus())) {
             VvmLog.i(TAG, "User blocked");
             config.handleEvent(status, OmtpEvents.VVM3_SUBSCRIBER_BLOCKED);
+            PhoneGlobals.getInstance().setShouldCheckVisualVoicemailConfigurationForMwi(task.getSubId(),
+                    false);
         }
     }