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.os.PersistableBundle; 23 import android.preference.Preference; 24 import android.preference.PreferenceFragment; 25 import android.preference.PreferenceScreen; 26 import android.preference.SwitchPreference; 27 import android.provider.Settings; 28 import android.telephony.AccessNetworkConstants; 29 import android.telephony.CarrierConfigManager; 30 import android.telephony.PhoneStateListener; 31 import android.telephony.SubscriptionManager; 32 import android.telephony.TelephonyManager; 33 import android.text.TextUtils; 34 import android.util.Log; 35 36 import com.android.ims.ImsManager; 37 import com.android.internal.telephony.Phone; 38 import com.android.internal.telephony.PhoneFactory; 39 import com.android.internal.telephony.SubscriptionController; 40 import com.android.phone.PhoneGlobals; 41 import com.android.phone.R; 42 43 import java.util.concurrent.Executor; 44 import java.util.concurrent.Executors; 45 import java.util.concurrent.LinkedBlockingQueue; 46 import java.util.concurrent.TimeUnit; 47 48 public class AccessibilitySettingsFragment extends PreferenceFragment { 49 private static final String LOG_TAG = AccessibilitySettingsFragment.class.getSimpleName(); 50 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2); 51 52 private static final String BUTTON_TTY_KEY = "button_tty_mode_key"; 53 private static final String BUTTON_HAC_KEY = "button_hac_key"; 54 private static final String BUTTON_RTT_KEY = "button_rtt_key"; 55 private static final String RTT_INFO_PREF = "button_rtt_more_information_key"; 56 57 private static final int WFC_QUERY_TIMEOUT_MILLIS = 20; 58 59 private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() { 60 /** 61 * Disable the TTY setting when in/out of a call (and if carrier doesn't 62 * support VoLTE with TTY). 63 * @see android.telephony.PhoneStateListener#onCallStateChanged(int, 64 * java.lang.String) 65 */ 66 @Override 67 public void onCallStateChanged(int state, String incomingNumber) { 68 if (DBG) Log.d(LOG_TAG, "PhoneStateListener.onCallStateChanged: state=" + state); 69 Preference pref = getPreferenceScreen().findPreference(BUTTON_TTY_KEY); 70 if (pref != null) { 71 // Use TelephonyManager#getCallState instead of 'state' parameter because 72 // needs to check the current state of all phone calls to 73 // support multi sim configuration. 74 TelephonyManager telephonyManager = 75 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 76 final boolean isVolteTtySupported = isVolteTtySupportedInAnySlot(); 77 pref.setEnabled((isVolteTtySupported && !isVideoCallOrConferenceInProgress()) 78 || (telephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE)); 79 } 80 } 81 }; 82 83 private Context mContext; 84 private AudioManager mAudioManager; 85 86 private TtyModeListPreference mButtonTty; 87 private SwitchPreference mButtonHac; 88 private SwitchPreference mButtonRtt; 89 90 @Override onCreate(Bundle savedInstanceState)91 public void onCreate(Bundle savedInstanceState) { 92 super.onCreate(savedInstanceState); 93 94 mContext = getActivity().getApplicationContext(); 95 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); 96 97 addPreferencesFromResource(R.xml.accessibility_settings); 98 99 mButtonTty = (TtyModeListPreference) findPreference( 100 getResources().getString(R.string.tty_mode_key)); 101 mButtonHac = (SwitchPreference) findPreference(BUTTON_HAC_KEY); 102 mButtonRtt = (SwitchPreference) findPreference(BUTTON_RTT_KEY); 103 104 if (PhoneGlobals.getInstance().phoneMgr.isTtyModeSupported() && isTtySupportedByCarrier()) { 105 mButtonTty.init(); 106 } else { 107 getPreferenceScreen().removePreference(mButtonTty); 108 mButtonTty = null; 109 } 110 111 if (PhoneGlobals.getInstance().phoneMgr.isHearingAidCompatibilitySupported()) { 112 int hac = Settings.System.getInt(mContext.getContentResolver(), 113 Settings.System.HEARING_AID, SettingsConstants.HAC_DISABLED); 114 mButtonHac.setChecked(hac == SettingsConstants.HAC_ENABLED); 115 } else { 116 getPreferenceScreen().removePreference(mButtonHac); 117 mButtonHac = null; 118 } 119 120 if (shouldShowRttSetting()) { 121 TelephonyManager tm = 122 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 123 boolean isRoaming = tm.isNetworkRoaming( 124 SubscriptionManager.getDefaultVoiceSubscriptionId()); 125 boolean alwaysAllowWhileRoaming = isCarrierAllowRttWhenRoaming( 126 SubscriptionManager.getDefaultVoiceSubscriptionId()); 127 128 boolean shouldDisableBecauseRoamingOffWfc = 129 (isRoaming && !isOnWfc()) && !alwaysAllowWhileRoaming; 130 131 if (shouldDisableBecauseRoamingOffWfc) { 132 mButtonRtt.setSummary(TextUtils.concat(getText(R.string.rtt_mode_summary), "\n", 133 getText(R.string.no_rtt_when_roaming))); 134 } 135 boolean rttOn = Settings.Secure.getInt( 136 mContext.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0; 137 mButtonRtt.setChecked(rttOn); 138 } else { 139 getPreferenceScreen().removePreference(mButtonRtt); 140 Preference rttInfoPref = findPreference(RTT_INFO_PREF); 141 getPreferenceScreen().removePreference(rttInfoPref); 142 mButtonRtt = null; 143 } 144 } 145 146 @Override onResume()147 public void onResume() { 148 super.onResume(); 149 TelephonyManager tm = 150 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 151 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); 152 } 153 154 @Override onPause()155 public void onPause() { 156 super.onPause(); 157 TelephonyManager tm = 158 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 159 tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE); 160 } 161 162 @Override onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)163 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 164 if (preference == mButtonTty) { 165 return true; 166 } else if (preference == mButtonHac) { 167 int hac = mButtonHac.isChecked() 168 ? SettingsConstants.HAC_ENABLED : SettingsConstants.HAC_DISABLED; 169 // Update HAC value in Settings database. 170 Settings.System.putInt(mContext.getContentResolver(), Settings.System.HEARING_AID, hac); 171 172 // Update HAC Value in AudioManager. 173 mAudioManager.setParameters( 174 SettingsConstants.HAC_KEY + "=" + (hac == SettingsConstants.HAC_ENABLED 175 ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF)); 176 return true; 177 } else if (preference == mButtonRtt) { 178 Log.i(LOG_TAG, "RTT setting changed -- now " + mButtonRtt.isChecked()); 179 int rttMode = mButtonRtt.isChecked() ? 1 : 0; 180 Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 181 rttMode); 182 // Update RTT config with IMS Manager if the always-on carrier config isn't set to true. 183 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService( 184 Context.CARRIER_CONFIG_SERVICE); 185 for (int subId : SubscriptionController.getInstance().getActiveSubIdList(true)) { 186 if (!configManager.getConfigForSubId(subId).getBoolean( 187 CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL, false)) { 188 int phoneId = SubscriptionController.getInstance().getPhoneId(subId); 189 ImsManager imsManager = ImsManager.getInstance(getContext(), phoneId); 190 imsManager.setRttEnabled(mButtonRtt.isChecked()); 191 } 192 } 193 return true; 194 } 195 196 return false; 197 } 198 isVolteTtySupportedInAnySlot()199 private boolean isVolteTtySupportedInAnySlot() { 200 final Phone[] phones = PhoneFactory.getPhones(); 201 if (phones == null) { 202 if (DBG) Log.d(LOG_TAG, "isVolteTtySupportedInAnySlot: No phones found."); 203 return false; 204 } 205 206 CarrierConfigManager configManager = 207 (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE); 208 for (Phone phone : phones) { 209 // Check if this phone supports VoLTE. 210 ImsManager imsManager = ImsManager.getInstance(mContext, phone.getPhoneId()); 211 boolean volteEnabled = false; 212 if (imsManager != null) { 213 volteEnabled = imsManager.isVolteEnabledByPlatform(); 214 } 215 216 // Check if this phone suports VoLTE TTY. 217 boolean volteTtySupported = false; 218 PersistableBundle carrierConfig = configManager.getConfigForSubId(phone.getSubId()); 219 if (carrierConfig != null) { 220 volteTtySupported = carrierConfig.getBoolean( 221 CarrierConfigManager.KEY_CARRIER_VOLTE_TTY_SUPPORTED_BOOL); 222 } 223 224 if (volteEnabled && volteTtySupported) { 225 // VoLTE TTY is supported on this phone that also suports VoLTE. 226 return true; 227 } 228 } 229 // VoLTE TTY was not supported on any phone that also supports VoLTE. 230 return false; 231 } 232 isVideoCallOrConferenceInProgress()233 private boolean isVideoCallOrConferenceInProgress() { 234 final Phone[] phones = PhoneFactory.getPhones(); 235 if (phones == null) { 236 if (DBG) Log.d(LOG_TAG, "isVideoCallOrConferenceInProgress: No phones found."); 237 return false; 238 } 239 240 for (Phone phone : phones) { 241 if (phone.isImsVideoCallOrConferencePresent()) { 242 return true; 243 } 244 } 245 return false; 246 } 247 isOnWfc()248 private boolean isOnWfc() { 249 LinkedBlockingQueue<Integer> result = new LinkedBlockingQueue<>(1); 250 Executor executor = Executors.newSingleThreadExecutor(); 251 mContext.getSystemService(android.telephony.ims.ImsManager.class) 252 .getImsMmTelManager(SubscriptionManager.getDefaultSubscriptionId()) 253 .getRegistrationTransportType(executor, result::offer); 254 try { 255 Integer transportType = result.poll(WFC_QUERY_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 256 return transportType != null 257 && transportType == AccessNetworkConstants.TRANSPORT_TYPE_WLAN; 258 } catch (InterruptedException e) { 259 return false; 260 } 261 } 262 shouldShowRttSetting()263 private boolean shouldShowRttSetting() { 264 // Go through all the subs -- if we want to display the RTT setting for any of them, do 265 // display it. 266 for (int subId : SubscriptionController.getInstance().getActiveSubIdList(true)) { 267 if (PhoneGlobals.getInstance().phoneMgr.isRttSupported(subId)) { 268 return true; 269 } 270 } 271 return false; 272 } 273 274 /** 275 * Determines if the device supports TTY per carrier config. 276 * @return {@code true} if the carrier supports TTY, {@code false} otherwise. 277 */ isTtySupportedByCarrier()278 private boolean isTtySupportedByCarrier() { 279 CarrierConfigManager configManager = 280 (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE); 281 return configManager.getConfig().getBoolean( 282 CarrierConfigManager.KEY_TTY_SUPPORTED_BOOL); 283 } 284 285 /** 286 * Determines from carrier config whether to always allow RTT while roaming. 287 */ isCarrierAllowRttWhenRoaming(int subId)288 private boolean isCarrierAllowRttWhenRoaming(int subId) { 289 PersistableBundle b = 290 PhoneGlobals.getInstance().getCarrierConfigForSubId(subId); 291 return b.getBoolean(CarrierConfigManager.KEY_RTT_SUPPORTED_WHILE_ROAMING_BOOL); 292 } 293 } 294