Fix for GSM-8bit decoding error

Bug: 253538769
Test: atest android.telephony.cts.VisualVoicemailServiceTest

Change-Id: Iad422dbe730ab8d318537d60439210ef8e7d37aa
Merged-In: Iad422dbe730ab8d318537d60439210ef8e7d37aa
diff --git a/src/java/com/android/internal/telephony/VisualVoicemailSmsFilter.java b/src/java/com/android/internal/telephony/VisualVoicemailSmsFilter.java
index 5bbe8b4..1934136 100644
--- a/src/java/com/android/internal/telephony/VisualVoicemailSmsFilter.java
+++ b/src/java/com/android/internal/telephony/VisualVoicemailSmsFilter.java
@@ -15,6 +15,8 @@
  */
 package com.android.internal.telephony;
 
+import static com.android.internal.telephony.SmsConstants.ENCODING_8BIT;
+
 import android.annotation.Nullable;
 import android.content.ComponentName;
 import android.content.Context;
@@ -294,7 +296,18 @@
                 result.firstMessage = message;
             }
             String body = message.getMessageBody();
-            if (body == null && message.getUserData() != null) {
+
+            /*
+             * For visual voice mail SMS message, UTF-8 is used by default
+             * {@link com.android.internal.telephony.SmsController#sendVisualVoicemailSmsForSubscriber}
+             *
+             * If config_sms_decode_gsm_8bit_data is enabled, GSM-8bit will be used to decode the
+             * received message. However, the message is most likely encoded with UTF-8. Therefore,
+             * we need to retry decoding the received message with UTF-8.
+             */
+            if ((body == null || message.getReceivedEncodingType() == ENCODING_8BIT)
+                    && message.getUserData() != null) {
+                Log.d(TAG, "getFullMessage decode using UTF-8");
                 // Attempt to interpret the user data as UTF-8. UTF-8 string over data SMS using
                 // 8BIT data coding scheme is our recommended way to send VVM SMS and is used in CTS
                 // Tests. The OMTP visual voicemail specification does not specify the SMS type and
@@ -303,7 +316,8 @@
                 try {
                     body = decoder.decode(byteBuffer).toString();
                 } catch (CharacterCodingException e) {
-                    // User data is not decode-able as UTF-8. Ignoring.
+                    Log.e(TAG, "getFullMessage: got CharacterCodingException"
+                            + " when decoding with UTF-8, e = " + e);
                     return null;
                 }
             }