Fix potential crash when displaying MMI error result.

Newly added code made the incorrect assumption that an MMI result would
come back as an empty string if not supplied.
We've seen crashes in the field due to a null message string coming in.

Test: Code inspection / compilation.
Test: Wrote unit tests to check null handling in these cases.
Fixes: 191617795
Change-Id: I57d0227fd725f3ae19a2ce7290afc670bd88fd93
diff --git a/src/java/com/android/internal/telephony/gsm/GsmMmiCode.java b/src/java/com/android/internal/telephony/gsm/GsmMmiCode.java
index d2f0387..dec2468 100644
--- a/src/java/com/android/internal/telephony/gsm/GsmMmiCode.java
+++ b/src/java/com/android/internal/telephony/gsm/GsmMmiCode.java
@@ -1228,7 +1228,7 @@
     onUssdFinishedError() {
         if (mState == State.PENDING) {
             mState = State.FAILED;
-            if (mMessage.length() == 0) {
+            if (TextUtils.isEmpty(mMessage)) {
                 mMessage = mContext.getText(com.android.internal.R.string.mmiError);
             }
             Rlog.d(LOG_TAG, "onUssdFinishedError");
diff --git a/src/java/com/android/internal/telephony/imsphone/ImsPhoneMmiCode.java b/src/java/com/android/internal/telephony/imsphone/ImsPhoneMmiCode.java
index 87790a3..359079d 100644
--- a/src/java/com/android/internal/telephony/imsphone/ImsPhoneMmiCode.java
+++ b/src/java/com/android/internal/telephony/imsphone/ImsPhoneMmiCode.java
@@ -346,8 +346,8 @@
         return dialString;
     }
 
-    static ImsPhoneMmiCode
-    newNetworkInitiatedUssd(String ussdMessage, boolean isUssdRequest, ImsPhone phone) {
+    public static ImsPhoneMmiCode newNetworkInitiatedUssd(String ussdMessage,
+            boolean isUssdRequest, ImsPhone phone) {
         ImsPhoneMmiCode ret;
 
         ret = new ImsPhoneMmiCode(phone);
@@ -1194,12 +1194,10 @@
      *
      * The radio has reset, and this is still pending
      */
-
-    void
-    onUssdFinishedError() {
+    public void onUssdFinishedError() {
         if (mState == State.PENDING) {
             mState = State.FAILED;
-            if (mMessage.length() == 0) {
+            if (TextUtils.isEmpty(mMessage)) {
                 mMessage = mContext.getText(com.android.internal.R.string.mmiError);
             }
             Rlog.d(LOG_TAG, "onUssdFinishedError: mmi=" + this);
diff --git a/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmMmiCodeTest.java b/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmMmiCodeTest.java
index 08d5b13..ee4f6e5 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmMmiCodeTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/gsm/GsmMmiCodeTest.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony.gsm;
 
+import static junit.framework.Assert.fail;
+
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.Mockito.doReturn;
@@ -95,6 +97,16 @@
         assertTrue(mGsmMmiCode == null);
     }
 
+    @Test
+    public void testNoCrashOnEmptyMessage() {
+        GsmMmiCode mmi = GsmMmiCode.newNetworkInitiatedUssd(null, true, mGsmCdmaPhoneUT, null);
+        try {
+            mmi.onUssdFinishedError();
+        } catch (Exception e) {
+            fail("Shouldn't crash!!!");
+        }
+    }
+
     private void setCarrierSupportsCallerIdVerticalServiceCodesCarrierConfig() {
         final PersistableBundle bundle = new PersistableBundle();
         bundle.putBoolean(CarrierConfigManager
diff --git a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneMmiCodeTest.java b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneMmiCodeTest.java
index 220e7ce..aee46b7 100644
--- a/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneMmiCodeTest.java
+++ b/tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneMmiCodeTest.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.telephony.imsphone;
 
+import static junit.framework.Assert.fail;
+
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.Mockito.doReturn;
@@ -101,6 +103,16 @@
         assertTrue(mImsPhoneMmiCode == null);
     }
 
+    @Test
+    public void testNoCrashOnEmptyMessage() {
+        ImsPhoneMmiCode mmi = ImsPhoneMmiCode.newNetworkInitiatedUssd(null, true, mImsPhoneUT);
+        try {
+            mmi.onUssdFinishedError();
+        } catch (Exception e) {
+            fail("Shouldn't crash!!!");
+        }
+    }
+
     private void setCarrierSupportsCallerIdVerticalServiceCodesCarrierConfig() {
         final PersistableBundle bundle = new PersistableBundle();
         bundle.putBoolean(CarrierConfigManager