Allow initial voicemail status setup without signal

During device shutdown the VVM status might be wiped as the SIM will be
perceived as removed, setting the configuration state as "not
configured". During this state dialer will not show the VVM tab. The tab
will be restored when activation is ran because the configuration state
is set back to OK or ACTIVATING.

During previous optimization, the activation will not be queued at all
if the phone does not have signal. In this case after a shut down the
user will not see the voicemail tab again until signal is restore and
activation is ran.

In this CL, VVM activation will be queued even if there are no signal
the activation task will do the initial status table setup to bring
back the VVM tab,then check for signal and abort. Notification channel
state is wiped during the setup, but the "no signal" will be restored
if the task is aborted due to no signal.

Change-Id: Ib7c376b0a208fc14452ece0f5fad84b136f97d45
Fixes: 31903828
diff --git a/src/com/android/phone/vvm/omtp/ActivationTask.java b/src/com/android/phone/vvm/omtp/ActivationTask.java
index 3a9893a..72f1f54 100644
--- a/src/com/android/phone/vvm/omtp/ActivationTask.java
+++ b/src/com/android/phone/vvm/omtp/ActivationTask.java
@@ -24,7 +24,6 @@
 import android.os.Bundle;
 import android.provider.Settings;
 import android.provider.Settings.Global;
-import android.provider.VoicemailContract.Status;
 import android.telecom.PhoneAccountHandle;
 import android.telephony.ServiceState;
 import android.telephony.TelephonyManager;
@@ -101,13 +100,6 @@
             return;
         }
 
-        // Check for signal before activating. The event often happen while boot and the
-        // network is not connected yet. Launching activation will likely to cause the SMS
-        // sending to fail and waste unnecessary time waiting for time out.
-        if (!hasSignal(context, subId)) {
-            VvmLog.i(TAG, "Activation requested while not in service, rejecting");
-        }
-
         Intent intent = BaseTask.createIntent(context, ActivationTask.class, subId);
         if (messageData != null) {
             intent.putExtra(EXTRA_MESSAGE_DATA_BUNDLE, messageData);
@@ -140,12 +132,6 @@
             return;
         }
 
-        if (!hasSignal(getContext(), subId)) {
-            VvmLog.i(TAG, "Service lost during activation, aborting");
-            // Don't retry, a new activation will be started after the signal returned.
-            return;
-        }
-
         OmtpVvmCarrierConfigHelper helper = new OmtpVvmCarrierConfigHelper(getContext(), subId);
         if (!helper.isValid()) {
             VvmLog.i(TAG, "VVM not supported on subId " + subId);
@@ -167,6 +153,7 @@
 
         if (!OmtpVvmSourceManager.getInstance(getContext())
                 .isVvmSourceRegistered(phoneAccountHandle)) {
+            // This account has not been activated before during the lifetime of this boot.
             VisualVoicemailPreferences preferences = new VisualVoicemailPreferences(getContext(),
                     phoneAccountHandle);
             if (preferences.getString(OmtpConstants.SERVER_ADDRESS, null) == null) {
@@ -178,12 +165,20 @@
             } else {
                 // The account has been activated on this device before. Pretend it is already
                 // activated. If there are any activation error it will overwrite this status.
-                VoicemailStatus.edit(getContext(), phoneAccountHandle)
-                        .setConfigurationState(Status.CONFIGURATION_STATE_OK)
-                        .apply();
+                helper.handleEvent(VoicemailStatus.edit(getContext(), phoneAccountHandle),
+                        OmtpEvents.CONFIG_ACTIVATING_SUBSEQUENT);
             }
 
         }
+        if (!hasSignal(getContext(), subId)) {
+            VvmLog.i(TAG, "Service lost during activation, aborting");
+            // Restore the "NO SIGNAL" state since it will be overwritten by the CONFIG_ACTIVATING
+            // event.
+            helper.handleEvent(VoicemailStatus.edit(getContext(), phoneAccountHandle),
+                    OmtpEvents.NOTIFICATION_SERVICE_LOST);
+            // Don't retry, a new activation will be started after the signal returned.
+            return;
+        }
 
         helper.activateSmsFilter();
         VoicemailStatus.Editor status = mRetryPolicy.getVoicemailStatusEditor();
diff --git a/src/com/android/phone/vvm/omtp/DefaultOmtpEventHandler.java b/src/com/android/phone/vvm/omtp/DefaultOmtpEventHandler.java
index 88943c9..9fad094 100644
--- a/src/com/android/phone/vvm/omtp/DefaultOmtpEventHandler.java
+++ b/src/com/android/phone/vvm/omtp/DefaultOmtpEventHandler.java
@@ -19,6 +19,7 @@
 import android.content.Context;
 import android.provider.VoicemailContract;
 import android.provider.VoicemailContract.Status;
+
 import com.android.phone.VoicemailStatus;
 import com.android.phone.vvm.omtp.OmtpEvents.Type;
 
@@ -61,8 +62,14 @@
                 // for this activation.
                 status
                         .setConfigurationState(Status.CONFIGURATION_STATE_CONFIGURING)
-                        .setDataChannelState(Status.DATA_CHANNEL_STATE_OK)
-                        .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_OK).apply();
+                        .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_OK)
+                        .setDataChannelState(Status.DATA_CHANNEL_STATE_OK).apply();
+                break;
+            case CONFIG_ACTIVATING_SUBSEQUENT:
+                status
+                        .setConfigurationState(Status.CONFIGURATION_STATE_OK)
+                        .setNotificationChannelState(Status.NOTIFICATION_CHANNEL_STATE_OK)
+                        .setDataChannelState(Status.DATA_CHANNEL_STATE_OK).apply();
                 break;
             case CONFIG_SERVICE_NOT_AVAILABLE:
                 status
diff --git a/src/com/android/phone/vvm/omtp/OmtpEvents.java b/src/com/android/phone/vvm/omtp/OmtpEvents.java
index 648e0d0..7a89418 100644
--- a/src/com/android/phone/vvm/omtp/OmtpEvents.java
+++ b/src/com/android/phone/vvm/omtp/OmtpEvents.java
@@ -17,6 +17,7 @@
 package com.android.phone.vvm.omtp;
 
 import android.annotation.IntDef;
+
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 
@@ -34,6 +35,8 @@
     // The voicemail PIN is replaced with a generated PIN, user should change it.
     CONFIG_DEFAULT_PIN_REPLACED(Type.CONFIGURATION, true),
     CONFIG_ACTIVATING(Type.CONFIGURATION, true),
+    // There are already activation records, this is only a book-keeping activation.
+    CONFIG_ACTIVATING_SUBSEQUENT(Type.CONFIGURATION, true),
     CONFIG_STATUS_SMS_TIME_OUT(Type.CONFIGURATION),
     CONFIG_SERVICE_NOT_AVAILABLE(Type.CONFIGURATION),