Disable some settings if TTY over VoLTE not supported

- Disable TTY mode modification mid-call if VoLTE supported
- Disable Enhanced 4G modification if TTY is ON and TTY is
  not supported over VoLTE
- Show Enhanced 4G as OFF if TTY is ON and TTY is not supported
  over VoLTE
- Refactoring

Bug: 18593974
Change-Id: I15fba8df9daae1ee8bd013a4ff1b105fcf91b722
diff --git a/src/com/android/phone/CallFeaturesSetting.java b/src/com/android/phone/CallFeaturesSetting.java
index fbaddbd..a97f5d0 100644
--- a/src/com/android/phone/CallFeaturesSetting.java
+++ b/src/com/android/phone/CallFeaturesSetting.java
@@ -41,6 +41,7 @@
 import android.provider.Settings;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomManager;
+import android.telephony.PhoneStateListener;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.util.Log;
@@ -278,6 +279,13 @@
     public void onPause() {
         super.onPause();
         mForeground = false;
+
+        if (ImsManager.isVolteEnabledByPlatform(this) &&
+                !mPhone.getContext().getResources().getBoolean(
+                        com.android.internal.R.bool.config_carrier_volte_tty_supported)) {
+            TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
+            tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
+        }
     }
 
     /**
@@ -298,6 +306,23 @@
      */
     private boolean mSetupVoicemail = false;
 
+    private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
+        /**
+         * Enable/disable the TTY setting when in/out of a call (and if carrier doesn't
+         * support VoLTE with TTY).
+         * @see android.telephony.PhoneStateListener#onCallStateChanged(int,
+         * java.lang.String)
+         */
+        @Override
+        public void onCallStateChanged(int state, String incomingNumber) {
+            if (DBG) log("PhoneStateListener.onCallStateChanged: state=" + state);
+            Preference pref = getPreferenceScreen().findPreference(BUTTON_TTY_KEY);
+            if (pref != null) {
+                pref.setEnabled(state == TelephonyManager.CALL_STATE_IDLE);
+            }
+        }
+    };
+
     /*
      * Click Listeners, handle click based on objects attached to UI.
      */
@@ -1300,6 +1325,13 @@
         } else {
             prefSet.removePreference(mEnableVideoCalling);
         }
+
+        if (ImsManager.isVolteEnabledByPlatform(this) &&
+                !mPhone.getContext().getResources().getBoolean(
+                        com.android.internal.R.bool.config_carrier_volte_tty_supported)) {
+            TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
+            tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
+        }
     }
 
     @Override