1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.phone.settings; 18 19 import android.content.Context; 20 import android.media.AudioManager; 21 import android.os.Bundle; 22 import android.preference.Preference; 23 import android.preference.PreferenceFragment; 24 import android.preference.PreferenceScreen; 25 import android.preference.SwitchPreference; 26 import android.provider.Settings; 27 import android.telephony.CarrierConfigManager; 28 import android.telephony.PhoneStateListener; 29 import android.telephony.SubscriptionManager; 30 import android.telephony.TelephonyManager; 31 import android.util.Log; 32 33 import com.android.ims.ImsManager; 34 import com.android.internal.telephony.Phone; 35 import com.android.internal.telephony.PhoneFactory; 36 import com.android.phone.PhoneGlobals; 37 import com.android.phone.R; 38 39 public class AccessibilitySettingsFragment extends PreferenceFragment { 40 private static final String LOG_TAG = AccessibilitySettingsFragment.class.getSimpleName(); 41 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2); 42 43 private static final String BUTTON_TTY_KEY = "button_tty_mode_key"; 44 private static final String BUTTON_HAC_KEY = "button_hac_key"; 45 private static final String BUTTON_RTT_KEY = "button_rtt_key"; 46 private static final String RTT_INFO_PREF = "button_rtt_more_information_key"; 47 48 private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() { 49 /** 50 * Disable the TTY setting when in/out of a call (and if carrier doesn't 51 * support VoLTE with TTY). 52 * @see android.telephony.PhoneStateListener#onCallStateChanged(int, 53 * java.lang.String) 54 */ 55 @Override 56 public void onCallStateChanged(int state, String incomingNumber) { 57 if (DBG) Log.d(LOG_TAG, "PhoneStateListener.onCallStateChanged: state=" + state); 58 Preference pref = getPreferenceScreen().findPreference(BUTTON_TTY_KEY); 59 if (pref != null) { 60 final boolean isVolteTtySupported = ImsManager.isVolteEnabledByPlatform(mContext) 61 && getVolteTtySupported(); 62 pref.setEnabled((isVolteTtySupported && !isVideoCallOrConferenceInProgress()) || 63 (state == TelephonyManager.CALL_STATE_IDLE)); 64 } 65 } 66 }; 67 68 private Context mContext; 69 private AudioManager mAudioManager; 70 71 private TtyModeListPreference mButtonTty; 72 private SwitchPreference mButtonHac; 73 private SwitchPreference mButtonRtt; 74 75 @Override onCreate(Bundle savedInstanceState)76 public void onCreate(Bundle savedInstanceState) { 77 super.onCreate(savedInstanceState); 78 79 mContext = getActivity().getApplicationContext(); 80 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); 81 82 addPreferencesFromResource(R.xml.accessibility_settings); 83 84 mButtonTty = (TtyModeListPreference) findPreference( 85 getResources().getString(R.string.tty_mode_key)); 86 mButtonHac = (SwitchPreference) findPreference(BUTTON_HAC_KEY); 87 mButtonRtt = (SwitchPreference) findPreference(BUTTON_RTT_KEY); 88 89 if (PhoneGlobals.getInstance().phoneMgr.isTtyModeSupported()) { 90 mButtonTty.init(); 91 } else { 92 getPreferenceScreen().removePreference(mButtonTty); 93 mButtonTty = null; 94 } 95 96 if (PhoneGlobals.getInstance().phoneMgr.isHearingAidCompatibilitySupported()) { 97 int hac = Settings.System.getInt(mContext.getContentResolver(), 98 Settings.System.HEARING_AID, SettingsConstants.HAC_DISABLED); 99 mButtonHac.setChecked(hac == SettingsConstants.HAC_ENABLED); 100 } else { 101 getPreferenceScreen().removePreference(mButtonHac); 102 mButtonHac = null; 103 } 104 105 if (PhoneGlobals.getInstance().phoneMgr.isRttSupported()) { 106 // TODO: this is going to be a on/off switch for now. Ask UX about how to integrate 107 // this settings with TTY 108 boolean rttOn = Settings.Secure.getInt( 109 mContext.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0; 110 mButtonRtt.setChecked(rttOn); 111 } else { 112 getPreferenceScreen().removePreference(mButtonRtt); 113 Preference rttInfoPref = findPreference(RTT_INFO_PREF); 114 getPreferenceScreen().removePreference(rttInfoPref); 115 mButtonRtt = null; 116 } 117 } 118 119 @Override onResume()120 public void onResume() { 121 super.onResume(); 122 TelephonyManager tm = 123 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 124 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); 125 } 126 127 @Override onPause()128 public void onPause() { 129 super.onPause(); 130 TelephonyManager tm = 131 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 132 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE); 133 } 134 135 @Override onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)136 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 137 if (preference == mButtonTty) { 138 return true; 139 } else if (preference == mButtonHac) { 140 int hac = mButtonHac.isChecked() 141 ? SettingsConstants.HAC_ENABLED : SettingsConstants.HAC_DISABLED; 142 // Update HAC value in Settings database. 143 Settings.System.putInt(mContext.getContentResolver(), Settings.System.HEARING_AID, hac); 144 145 // Update HAC Value in AudioManager. 146 mAudioManager.setParameter(SettingsConstants.HAC_KEY, 147 hac == SettingsConstants.HAC_ENABLED 148 ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF); 149 return true; 150 } else if (preference == mButtonRtt) { 151 Log.i(LOG_TAG, "RTT setting changed -- now " + mButtonRtt.isChecked()); 152 int rttMode = mButtonRtt.isChecked() ? 1 : 0; 153 Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 154 rttMode); 155 // Update RTT config with IMS Manager 156 ImsManager imsManager = ImsManager.getInstance(getContext(), 157 SubscriptionManager.getDefaultVoicePhoneId()); 158 imsManager.setRttEnabled(mButtonRtt.isChecked()); 159 return true; 160 } 161 162 return false; 163 } 164 getVolteTtySupported()165 private boolean getVolteTtySupported() { 166 CarrierConfigManager configManager = 167 (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE); 168 return configManager.getConfig().getBoolean( 169 CarrierConfigManager.KEY_CARRIER_VOLTE_TTY_SUPPORTED_BOOL); 170 } 171 isVideoCallOrConferenceInProgress()172 private boolean isVideoCallOrConferenceInProgress() { 173 final Phone[] phones = PhoneFactory.getPhones(); 174 if (phones == null) { 175 if (DBG) Log.d(LOG_TAG, "isVideoCallOrConferenceInProgress: No phones found."); 176 return false; 177 } 178 179 for (Phone phone : phones) { 180 if (phone.isImsVideoCallOrConferencePresent()) { 181 return true; 182 } 183 } 184 return false; 185 } 186 } 187