RTT initialization bug fixes

Add a setting toggle to enable/disable RTT
Allow calls to be initiated with RTT enabled (add plumbing)
Refactor to match the change to dial()

Bug: 71873552
Test: manual
Change-Id: I79b415751b7f1495a3cd603b83f8c4df4389c4c8
Merged-In: I79b415751b7f1495a3cd603b83f8c4df4389c4c8
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 8c450a0..72be3b2 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1165,6 +1165,11 @@
     <string name="hac_mode_title">Hearing aids</string>
     <string name="hac_mode_summary">Turn on hearing aid compatibility</string>
 
+    <!-- RTT settings: used to turn on/off Real-Time Text, an accessibility feature similar to
+         TTY that allows users to send text over a phone call. -->
+    <string name="rtt_mode_title">RTT</string>
+    <string name="rtt_mode_summary">Turn RTT on or off</string>
+
     <!-- Service option entries.  -->
     <string-array name="tty_mode_entries">
         <item>TTY Off</item>
diff --git a/res/xml/accessibility_settings.xml b/res/xml/accessibility_settings.xml
index 2c007f6..e674e74 100644
--- a/res/xml/accessibility_settings.xml
+++ b/res/xml/accessibility_settings.xml
@@ -32,4 +32,10 @@
         android:persistent="true"
         android:summary="@string/hac_mode_summary"/>
 
+    <SwitchPreference
+        android:key="button_rtt_key"
+        android:title="@string/rtt_mode_title"
+        android:persistent="true"
+        android:summary="@string/rtt_mode_summary"/>
+
 </PreferenceScreen>
diff --git a/sip/src/com/android/services/telephony/sip/SipConnectionService.java b/sip/src/com/android/services/telephony/sip/SipConnectionService.java
index a5f48d3..27be9b4 100644
--- a/sip/src/com/android/services/telephony/sip/SipConnectionService.java
+++ b/sip/src/com/android/services/telephony/sip/SipConnectionService.java
@@ -37,6 +37,7 @@
 
 import com.android.internal.telephony.CallStateException;
 import com.android.internal.telephony.PhoneFactory;
+import com.android.internal.telephony.PhoneInternalInterface;
 import com.android.internal.telephony.sip.SipPhone;
 import com.android.services.telephony.DisconnectCauseUtil;
 
@@ -258,7 +259,9 @@
 
         try {
             com.android.internal.telephony.Connection originalConnection =
-                    phone.dial(number, request.getVideoState());
+                    phone.dial(number, new PhoneInternalInterface.DialArgs.Builder<>()
+                            .setVideoState(request.getVideoState())
+                            .build());
             return originalConnection;
         } catch (CallStateException e) {
             log("startCallWithPhone, exception: " + e);
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index f10a609..cb45eb0 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -3265,6 +3265,14 @@
         return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
     }
 
+    public boolean isRttSupported() {
+        boolean isCarrierSupported =
+                mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
+        boolean isDeviceSupported =
+                mPhone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
+        return isCarrierSupported && isDeviceSupported;
+    }
+
     /**
      * Returns the unique device ID of phone, for example, the IMEI for
      * GSM and the MEID for CDMA phones. Return null if device ID is not available.
diff --git a/src/com/android/phone/settings/AccessibilitySettingsFragment.java b/src/com/android/phone/settings/AccessibilitySettingsFragment.java
index af7d4bf..7ede23a 100644
--- a/src/com/android/phone/settings/AccessibilitySettingsFragment.java
+++ b/src/com/android/phone/settings/AccessibilitySettingsFragment.java
@@ -24,8 +24,10 @@
 import android.preference.PreferenceFragment;
 import android.preference.PreferenceScreen;
 import android.provider.Settings;
+import android.telecom.TelecomManager;
 import android.telephony.CarrierConfigManager;
 import android.telephony.PhoneStateListener;
+import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 import android.util.Log;
 
@@ -41,6 +43,7 @@
 
     private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
     private static final String BUTTON_HAC_KEY = "button_hac_key";
+    private static final String BUTTON_RTT_KEY = "button_rtt_key";
 
     private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
         /**
@@ -67,6 +70,7 @@
 
     private TtyModeListPreference mButtonTty;
     private SwitchPreference mButtonHac;
+    private SwitchPreference mButtonRtt;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -80,6 +84,7 @@
         mButtonTty = (TtyModeListPreference) findPreference(
                 getResources().getString(R.string.tty_mode_key));
         mButtonHac = (SwitchPreference) findPreference(BUTTON_HAC_KEY);
+        mButtonRtt = (SwitchPreference) findPreference(BUTTON_RTT_KEY);
 
         if (PhoneGlobals.getInstance().phoneMgr.isTtyModeSupported()) {
             mButtonTty.init();
@@ -96,6 +101,19 @@
             getPreferenceScreen().removePreference(mButtonHac);
             mButtonHac = null;
         }
+
+        if (PhoneGlobals.getInstance().phoneMgr.isRttSupported()) {
+            // TODO: this is going to be a on/off switch for now. Ask UX about how to integrate
+            // this settings with TTY
+            boolean rttOn = Settings.System.getInt(
+                    mContext.getContentResolver(), Settings.System.RTT_CALLING_MODE,
+                    TelecomManager.TTY_MODE_OFF)
+                    != TelecomManager.TTY_MODE_OFF;
+            mButtonRtt.setChecked(rttOn);
+        } else {
+            getPreferenceScreen().removePreference(mButtonRtt);
+            mButtonRtt = null;
+        }
     }
 
     @Override
@@ -129,7 +147,19 @@
                     hac == SettingsConstants.HAC_ENABLED
                             ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF);
             return true;
+        } else if (preference == mButtonRtt) {
+            Log.i(LOG_TAG, "RTT setting changed -- now " + mButtonRtt.isChecked());
+            int rttMode = mButtonRtt.isChecked()
+                    ? TelecomManager.TTY_MODE_FULL : TelecomManager.TTY_MODE_OFF;
+            Settings.System.putInt(mContext.getContentResolver(), Settings.System.RTT_CALLING_MODE,
+                    rttMode);
+            // Update RTT config with IMS Manager
+            ImsManager imsManager = ImsManager.getInstance(getContext(),
+                    SubscriptionManager.getDefaultVoicePhoneId());
+            imsManager.setRttEnabled(mButtonRtt.isChecked());
+            return true;
         }
+
         return false;
     }
 
diff --git a/src/com/android/services/telephony/TelecomAccountRegistry.java b/src/com/android/services/telephony/TelecomAccountRegistry.java
index 17c6049..3fb8d32 100644
--- a/src/com/android/services/telephony/TelecomAccountRegistry.java
+++ b/src/com/android/services/telephony/TelecomAccountRegistry.java
@@ -234,9 +234,7 @@
                 extras.putBoolean(PhoneAccount.EXTRA_PLAY_CALL_RECORDING_TONE, true);
             }
 
-            boolean isDeviceRttSupported = mContext.getResources().getBoolean(
-                    R.bool.config_support_rtt);
-            if (isDeviceRttSupported && isCarrierRttSupported()) {
+            if (PhoneGlobals.getInstance().phoneMgr.isRttSupported()) {
                 capabilities |= PhoneAccount.CAPABILITY_RTT;
             }
 
@@ -405,12 +403,6 @@
                     b.getBoolean(CarrierConfigManager.KEY_SUPPORT_VIDEO_CONFERENCE_CALL_BOOL);
         }
 
-        private boolean isCarrierRttSupported() {
-            PersistableBundle b =
-                    PhoneGlobals.getInstance().getCarrierConfigForSubId(mPhone.getSubId());
-            return b != null && b.getBoolean(CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
-        }
-
         /**
          * Determines from carrier config whether merging of wifi calls is allowed when VoWIFI is
          * turned off.
diff --git a/src/com/android/services/telephony/TelephonyConnection.java b/src/com/android/services/telephony/TelephonyConnection.java
index 67e9739..b27baed 100644
--- a/src/com/android/services/telephony/TelephonyConnection.java
+++ b/src/com/android/services/telephony/TelephonyConnection.java
@@ -559,6 +559,16 @@
                     DisconnectCause.toString(cause));
             mHandler.obtainMessage(MSG_DISCONNECT).sendToTarget();
         }
+
+        @Override
+        public void onRttInitiated() {
+            sendRttInitiationSuccess();
+        }
+
+        @Override
+        public void onRttTerminated() {
+            sendRttSessionRemotelyTerminated();
+        }
     };
 
     protected com.android.internal.telephony.Connection mOriginalConnection;
@@ -567,6 +577,7 @@
     private boolean mIsStateOverridden = false;
     private Call.State mOriginalConnectionState = Call.State.IDLE;
     private Call.State mConnectionOverriddenState = Call.State.IDLE;
+    private RttTextStream mRttTextStream = null;
 
     private boolean mWasImsConnection;
 
@@ -992,6 +1003,8 @@
                 mIsCdmaVoicePrivacyEnabled);
         newProperties = changeBitmask(newProperties, PROPERTY_ASSISTED_DIALING_USED,
                 mIsUsingAssistedDialing);
+        newProperties = changeBitmask(newProperties, PROPERTY_IS_RTT,
+                (getConnectionProperties() & PROPERTY_IS_RTT) != 0);
 
         if (getConnectionProperties() != newProperties) {
             setConnectionProperties(newProperties);
@@ -1811,6 +1824,14 @@
         return false;
     }
 
+    public void setRttTextStream(RttTextStream s) {
+        mRttTextStream = s;
+    }
+
+    public RttTextStream getRttTextStream() {
+        return mRttTextStream;
+    }
+
     /**
      * For video calls, sets whether this connection supports pausing the outgoing video for the
      * call using the {@link android.telecom.VideoProfile#STATE_PAUSED} VideoState.
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index 8584d42..2022733 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -650,6 +650,7 @@
         connection.setAddress(handle, PhoneConstants.PRESENTATION_ALLOWED);
         connection.setInitializing();
         connection.setVideoState(request.getVideoState());
+        connection.setRttTextStream(request.getRttTextStream());
 
         return connection;
     }
@@ -988,7 +989,11 @@
         com.android.internal.telephony.Connection originalConnection = null;
         try {
             if (phone != null) {
-                originalConnection = phone.dial(number, null, videoState, extras);
+                originalConnection = phone.dial(number, new ImsPhone.ImsDialArgs.Builder()
+                        .setVideoState(videoState)
+                        .setIntentExtras(extras)
+                        .setRttTextStream(connection.getRttTextStream())
+                        .build());
             }
         } catch (CallStateException e) {
             Log.e(this, e, "placeOutgoingConnection, phone.dial exception: " + e);
diff --git a/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java b/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
index b4c93ee..3bd2716 100644
--- a/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
+++ b/tests/src/com/android/services/telephony/TelephonyConnectionServiceTest.java
@@ -539,7 +539,7 @@
         // was redialed on the same slot
         assertEquals(0, c.getNotifyPhoneAccountChangedCount());
         try {
-            verify(slot0Phone).dial(anyString(), any(), anyInt(), any());
+            verify(slot0Phone).dial(anyString(), any());
         } catch (CallStateException e) {
             // This shouldn't happen
             fail();
@@ -571,7 +571,7 @@
         // was never redialed
         assertEquals(0, c.getNotifyPhoneAccountChangedCount());
         try {
-            verify(slot0Phone, never()).dial(anyString(), any(), anyInt(), any());
+            verify(slot0Phone, never()).dial(anyString(), any());
         } catch (CallStateException e) {
             // This shouldn't happen
             fail();
@@ -611,7 +611,7 @@
         // redialed on another slot
         assertEquals(1, c.getNotifyPhoneAccountChangedCount());
         try {
-            verify(slot1Phone).dial(anyString(), any(), anyInt(), any());
+            verify(slot1Phone).dial(anyString(), any());
         } catch (CallStateException e) {
             // This shouldn't happen
             fail();
@@ -649,7 +649,7 @@
         // redialed on another slot
         assertEquals(1, c.getNotifyPhoneAccountChangedCount());
         try {
-            verify(slot1Phone).dial(anyString(), any(), anyInt(), any());
+            verify(slot1Phone).dial(anyString(), any());
         } catch (CallStateException e) {
             // This shouldn't happen
             fail();
@@ -700,8 +700,8 @@
         // redialed on another slot
         assertEquals(2, c.getNotifyPhoneAccountChangedCount());
         try {
-            verify(slot0Phone).dial(anyString(), any(), anyInt(), any());
-            verify(slot1Phone).dial(anyString(), any(), anyInt(), any());
+            verify(slot0Phone).dial(anyString(), any());
+            verify(slot1Phone).dial(anyString(), any());
         } catch (CallStateException e) {
             // This shouldn't happen
             fail();
@@ -750,8 +750,8 @@
         // redialed on another slot
         assertEquals(1, c.getNotifyPhoneAccountChangedCount());
         try {
-            verify(slot1Phone).dial(anyString(), any(), anyInt(), any());
-            verify(slot0Phone, never()).dial(anyString(), any(), anyInt(), any());
+            verify(slot1Phone).dial(anyString(), any());
+            verify(slot0Phone, never()).dial(anyString(), any());
         } catch (CallStateException e) {
             // This shouldn't happen
             fail();
@@ -850,7 +850,7 @@
 
     private void setPhonesDialConnection(Phone phone, Connection c) {
         try {
-            when(phone.dial(anyString(), anyInt())).thenReturn(c);
+            when(phone.dial(anyString(), any())).thenReturn(c);
         } catch (CallStateException e) {
             // this shouldn't happen
             fail();