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/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);