Dialer: implement vvm_type_advvm (AT&T VVM)

Change-Id: If1ddf1fba719e876910943759b90d1a1fdd1593d
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 73e9c2a..677e757 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -63,6 +63,7 @@
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
     <uses-permission android:name="android.permission.BROADCAST_STICKY" />
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
+    <uses-permission android:name="android.permission.RECEIVE_SMS" />
     <uses-permission android:name="android.permission.SEND_SMS" />
     <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
     <uses-permission android:name="android.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS" />
@@ -658,6 +659,16 @@
         </receiver>
 
         <receiver
+            android:name="com.android.voicemail.impl.sms.AdvvmSmsReceiver"
+            android:exported="true"
+            android:directBootAware="true">
+            <intent-filter>
+                <action android:name="android.intent.action.DATA_SMS_RECEIVED"/>
+                <data android:host="localhost" android:port="5499" android:scheme="sms"/>
+            </intent-filter>
+        </receiver>
+
+        <receiver
             android:name="com.android.voicemail.impl.VoicemailClientReceiver"
             android:exported="false">
             <intent-filter>
diff --git a/java/com/android/dialer/util/PermissionsUtil.java b/java/com/android/dialer/util/PermissionsUtil.java
index 3844380..7db383f 100644
--- a/java/com/android/dialer/util/PermissionsUtil.java
+++ b/java/com/android/dialer/util/PermissionsUtil.java
@@ -26,6 +26,7 @@
 import static android.Manifest.permission.READ_CONTACTS;
 import static android.Manifest.permission.READ_PHONE_STATE;
 import static android.Manifest.permission.READ_VOICEMAIL;
+import static android.Manifest.permission.RECEIVE_SMS;
 import static android.Manifest.permission.SEND_SMS;
 import static android.Manifest.permission.WRITE_CALL_LOG;
 import static android.Manifest.permission.WRITE_CONTACTS;
@@ -70,6 +71,7 @@
               WRITE_CALL_LOG,
               READ_PHONE_STATE,
               MODIFY_PHONE_STATE,
+              RECEIVE_SMS,
               SEND_SMS,
               CALL_PHONE,
               ADD_VOICEMAIL,
@@ -138,6 +140,10 @@
     return hasPermission(context, permission.ADD_VOICEMAIL);
   }
 
+  public static boolean hasReceiveSmsPermissions(Context context) {
+    return hasPermission(context, permission.RECEIVE_SMS);
+  }
+
   public static boolean hasSendSmsPermissions(Context context) {
     return hasPermission(context, permission.SEND_SMS);
   }
diff --git a/java/com/android/voicemail/VisualVoicemailTypeExtensions.java b/java/com/android/voicemail/VisualVoicemailTypeExtensions.java
index 0564810..051f393 100644
--- a/java/com/android/voicemail/VisualVoicemailTypeExtensions.java
+++ b/java/com/android/voicemail/VisualVoicemailTypeExtensions.java
@@ -26,4 +26,6 @@
 
   // Protocol used by Verizon wireless
   public static final String VVM_TYPE_VVM3 = "vvm_type_vvm3";
+  // Protocol used by AT&T
+  public static final String VVM_TYPE_ADVVM = "vvm_type_advvm";
 }
diff --git a/java/com/android/voicemail/VoicemailPermissionHelper.java b/java/com/android/voicemail/VoicemailPermissionHelper.java
index b7cdbca..b8b0d90 100644
--- a/java/com/android/voicemail/VoicemailPermissionHelper.java
+++ b/java/com/android/voicemail/VoicemailPermissionHelper.java
@@ -37,6 +37,7 @@
     permission.WRITE_VOICEMAIL,
     permission.READ_VOICEMAIL,
     permission.READ_PHONE_STATE,
+    permission.RECEIVE_SMS,
     permission.SEND_SMS
   };
 
diff --git a/java/com/android/voicemail/impl/OmtpVvmCarrierConfigHelper.java b/java/com/android/voicemail/impl/OmtpVvmCarrierConfigHelper.java
index 8c91ebf..1224789 100644
--- a/java/com/android/voicemail/impl/OmtpVvmCarrierConfigHelper.java
+++ b/java/com/android/voicemail/impl/OmtpVvmCarrierConfigHelper.java
@@ -91,6 +91,7 @@
 
   public static final String KEY_VVM_CLIENT_PREFIX_STRING = "vvm_client_prefix_string";
   private static final String KEY_IGNORE_TRANSCRIPTION_BOOL = "vvm_ignore_transcription";
+  private static final String KEY_USE_DIRECT_TLS_CONNECTION_BOOL = "vvm_use_direct_tls_connection";
 
   private final Context context;
   private final PersistableBundle carrierConfig;
@@ -254,6 +255,10 @@
   /** @return Port to start a SSL IMAP connection directly. */
   public int getSslPort() {
     Assert.checkArgument(isValid());
+    if (useDirectTlsConnection()) {
+      VvmLog.i(TAG, "vvm_use_direct_tls_connection = true");
+      return 993;
+    }
     return (int) getValue(KEY_VVM_SSL_PORT_NUMBER_INT, 0);
   }
 
@@ -502,4 +507,12 @@
     Assert.checkArgument(isValid());
     return (boolean) getValue(KEY_IGNORE_TRANSCRIPTION_BOOL, false);
   }
+
+  /**
+   * Allow forcing direct TLS, default to false.
+   */
+  public boolean useDirectTlsConnection() {
+    Assert.checkArgument(isValid());
+    return (boolean) getValue(KEY_USE_DIRECT_TLS_CONNECTION_BOOL, false);
+  }
 }
diff --git a/java/com/android/voicemail/impl/imap/ImapHelper.java b/java/com/android/voicemail/impl/imap/ImapHelper.java
index 95e7bbb..955eba9 100644
--- a/java/com/android/voicemail/impl/imap/ImapHelper.java
+++ b/java/com/android/voicemail/impl/imap/ImapHelper.java
@@ -125,6 +125,10 @@
       int port = Integer.parseInt(prefs.getString(OmtpConstants.IMAP_PORT, null));
       int auth = ImapStore.FLAG_NONE;
 
+      if (serverName != null) {
+        serverName = serverName.replaceFirst("[12]:", "");
+      }
+
       int sslPort = this.config.getSslPort();
       if (sslPort != 0) {
         port = sslPort;
diff --git a/java/com/android/voicemail/impl/mail/store/ImapConnection.java b/java/com/android/voicemail/impl/mail/store/ImapConnection.java
index 4a4debb..8b1ee83 100644
--- a/java/com/android/voicemail/impl/mail/store/ImapConnection.java
+++ b/java/com/android/voicemail/impl/mail/store/ImapConnection.java
@@ -171,6 +171,9 @@
 
   /** Attempts to convert the connection into secure connection. */
   private void maybeDoStartTls() throws IOException, MessagingException {
+    if (imapStore.getImapHelper().getConfig().useDirectTlsConnection()) {
+      return;
+    }
     // STARTTLS is required in the OMTP standard but not every implementation support it.
     // Make sure the server does have this capability
     if (hasCapability(ImapConstants.CAPABILITY_STARTTLS)) {
diff --git a/java/com/android/voicemail/impl/protocol/VisualVoicemailProtocolFactory.java b/java/com/android/voicemail/impl/protocol/VisualVoicemailProtocolFactory.java
index 7f635f1..57f1e1f 100644
--- a/java/com/android/voicemail/impl/protocol/VisualVoicemailProtocolFactory.java
+++ b/java/com/android/voicemail/impl/protocol/VisualVoicemailProtocolFactory.java
@@ -39,6 +39,7 @@
       case TelephonyManager.VVM_TYPE_CVVM:
         return new CvvmProtocol();
       case VisualVoicemailTypeExtensions.VVM_TYPE_VVM3:
+      case VisualVoicemailTypeExtensions.VVM_TYPE_ADVVM:
         return new Vvm3Protocol();
       default:
         VvmLog.e(TAG, "Unexpected visual voicemail type: " + type);
diff --git a/java/com/android/voicemail/impl/sms/AdvvmSmsReceiver.java b/java/com/android/voicemail/impl/sms/AdvvmSmsReceiver.java
new file mode 100644
index 0000000..384b8ce
--- /dev/null
+++ b/java/com/android/voicemail/impl/sms/AdvvmSmsReceiver.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2024 The LineageOS Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+package com.android.voicemail.impl.sms;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.provider.Telephony;
+import android.telephony.SmsMessage;
+import com.android.dialer.telecom.TelecomUtil;
+import android.telecom.PhoneAccount;
+import android.telecom.PhoneAccountHandle;
+import com.android.voicemail.impl.sync.SyncTask;
+
+/** Receive data SMS messages for the ADVVM protocol used by AT&T and others. */
+public class AdvvmSmsReceiver extends BroadcastReceiver {
+
+  private static final String TAG = "AdvvmSmsReceiver";
+
+  @Override
+  public void onReceive(Context context, Intent intent) {
+    SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
+    for (SmsMessage msg: messages) {
+      // in order to query parameters we need to have a `scheme://`
+      Uri bodyUri = Uri.parse("advvm://" + msg.getMessageBody());
+      // TODO: once AT&T's protocol is better understood, we probably will need
+      // to handle the actual parameters of this. For now, just trigger a sync
+      // since that is apparently the only time we will receive these messages
+      // currently.
+      PhoneAccountHandle phone =
+          TelecomUtil.getDefaultOutgoingPhoneAccount(context, PhoneAccount.SCHEME_VOICEMAIL);
+      SyncTask.start(context, phone);
+    }
+  }
+}
diff --git a/privapp_whitelist_com.android.dialer-ext.xml b/privapp_whitelist_com.android.dialer-ext.xml
index 814e88e..c13d9c7 100644
--- a/privapp_whitelist_com.android.dialer-ext.xml
+++ b/privapp_whitelist_com.android.dialer-ext.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-     Copyright (C) 2019-2023 The LineageOS Project
+     Copyright (C) 2019-2024 The LineageOS Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -19,5 +19,6 @@
     <privapp-permissions package="com.android.dialer">
         <permission name="android.permission.CAPTURE_AUDIO_OUTPUT"/>
         <permission name="android.permission.BROADCAST_CLOSE_SYSTEM_DIALOGS"/>
+        <permission name="android.permission.RECEIVE_SMS"/>
     </privapp-permissions>
 </permissions>