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.Handler; 23 import android.os.HandlerExecutor; 24 import android.os.Looper; 25 import android.os.PersistableBundle; 26 import android.preference.Preference; 27 import android.preference.PreferenceFragment; 28 import android.preference.PreferenceScreen; 29 import android.preference.SwitchPreference; 30 import android.provider.Settings; 31 import android.telephony.AccessNetworkConstants; 32 import android.telephony.CarrierConfigManager; 33 import android.telephony.SubscriptionManager; 34 import android.telephony.TelephonyCallback; 35 import android.telephony.TelephonyManager; 36 import android.text.TextUtils; 37 import android.util.Log; 38 39 import com.android.ims.ImsManager; 40 import com.android.internal.telephony.Phone; 41 import com.android.internal.telephony.PhoneFactory; 42 import com.android.internal.telephony.SubscriptionController; 43 import com.android.phone.PhoneGlobals; 44 import com.android.phone.R; 45 46 import java.util.concurrent.Executor; 47 import java.util.concurrent.Executors; 48 import java.util.concurrent.LinkedBlockingQueue; 49 import java.util.concurrent.TimeUnit; 50 51 public class AccessibilitySettingsFragment extends PreferenceFragment { 52 private static final String LOG_TAG = AccessibilitySettingsFragment.class.getSimpleName(); 53 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2); 54 55 private static final String BUTTON_TTY_KEY = "button_tty_mode_key"; 56 private static final String BUTTON_HAC_KEY = "button_hac_key"; 57 private static final String BUTTON_RTT_KEY = "button_rtt_key"; 58 private static final String RTT_INFO_PREF = "button_rtt_more_information_key"; 59 60 private static final int WFC_QUERY_TIMEOUT_MILLIS = 20; 61 62 private final TelephonyCallback mTelephonyCallback = new AccessibilityTelephonyCallback(); 63 64 private final class AccessibilityTelephonyCallback extends TelephonyCallback implements 65 TelephonyCallback.CallStateListener { 66 @Override onCallStateChanged(int state)67 public void onCallStateChanged(int state) { 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.registerTelephonyCallback(new HandlerExecutor(new Handler(Looper.getMainLooper())), 152 mTelephonyCallback); 153 } 154 155 @Override onPause()156 public void onPause() { 157 super.onPause(); 158 TelephonyManager tm = 159 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 160 tm.unregisterTelephonyCallback(mTelephonyCallback); 161 } 162 163 @Override onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)164 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 165 if (preference == mButtonTty) { 166 return true; 167 } else if (preference == mButtonHac) { 168 int hac = mButtonHac.isChecked() 169 ? SettingsConstants.HAC_ENABLED : SettingsConstants.HAC_DISABLED; 170 // Update HAC value in Settings database. 171 Settings.System.putInt(mContext.getContentResolver(), Settings.System.HEARING_AID, hac); 172 173 // Update HAC Value in AudioManager. 174 mAudioManager.setParameters( 175 SettingsConstants.HAC_KEY + "=" + (hac == SettingsConstants.HAC_ENABLED 176 ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF)); 177 return true; 178 } else if (preference == mButtonRtt) { 179 Log.i(LOG_TAG, "RTT setting changed -- now " + mButtonRtt.isChecked()); 180 int rttMode = mButtonRtt.isChecked() ? 1 : 0; 181 Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 182 rttMode); 183 // Update RTT config with IMS Manager if the always-on carrier config isn't set to true. 184 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService( 185 Context.CARRIER_CONFIG_SERVICE); 186 for (int subId : SubscriptionController.getInstance().getActiveSubIdList(true)) { 187 if (!configManager.getConfigForSubId(subId).getBoolean( 188 CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL, false)) { 189 int phoneId = SubscriptionController.getInstance().getPhoneId(subId); 190 ImsManager imsManager = ImsManager.getInstance(getContext(), phoneId); 191 imsManager.setRttEnabled(mButtonRtt.isChecked()); 192 } 193 } 194 return true; 195 } 196 197 return false; 198 } 199 isVolteTtySupportedInAnySlot()200 private boolean isVolteTtySupportedInAnySlot() { 201 final Phone[] phones = PhoneFactory.getPhones(); 202 if (phones == null) { 203 if (DBG) Log.d(LOG_TAG, "isVolteTtySupportedInAnySlot: No phones found."); 204 return false; 205 } 206 207 CarrierConfigManager configManager = 208 (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE); 209 for (Phone phone : phones) { 210 // Check if this phone supports VoLTE. 211 ImsManager imsManager = ImsManager.getInstance(mContext, phone.getPhoneId()); 212 boolean volteEnabled = false; 213 if (imsManager != null) { 214 volteEnabled = imsManager.isVolteEnabledByPlatform(); 215 } 216 217 // Check if this phone suports VoLTE TTY. 218 boolean volteTtySupported = false; 219 PersistableBundle carrierConfig = configManager.getConfigForSubId(phone.getSubId()); 220 if (carrierConfig != null) { 221 volteTtySupported = carrierConfig.getBoolean( 222 CarrierConfigManager.KEY_CARRIER_VOLTE_TTY_SUPPORTED_BOOL); 223 } 224 225 if (volteEnabled && volteTtySupported) { 226 // VoLTE TTY is supported on this phone that also suports VoLTE. 227 return true; 228 } 229 } 230 // VoLTE TTY was not supported on any phone that also supports VoLTE. 231 return false; 232 } 233 isVideoCallOrConferenceInProgress()234 private boolean isVideoCallOrConferenceInProgress() { 235 final Phone[] phones = PhoneFactory.getPhones(); 236 if (phones == null) { 237 if (DBG) Log.d(LOG_TAG, "isVideoCallOrConferenceInProgress: No phones found."); 238 return false; 239 } 240 241 for (Phone phone : phones) { 242 if (phone.isImsVideoCallOrConferencePresent()) { 243 return true; 244 } 245 } 246 return false; 247 } 248 isOnWfc()249 private boolean isOnWfc() { 250 LinkedBlockingQueue<Integer> result = new LinkedBlockingQueue<>(1); 251 Executor executor = Executors.newSingleThreadExecutor(); 252 mContext.getSystemService(android.telephony.ims.ImsManager.class) 253 .getImsMmTelManager(SubscriptionManager.getDefaultSubscriptionId()) 254 .getRegistrationTransportType(executor, result::offer); 255 try { 256 Integer transportType = result.poll(WFC_QUERY_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 257 return transportType != null 258 && transportType == AccessNetworkConstants.TRANSPORT_TYPE_WLAN; 259 } catch (InterruptedException e) { 260 return false; 261 } 262 } 263 shouldShowRttSetting()264 private boolean shouldShowRttSetting() { 265 // Go through all the subs -- if we want to display the RTT setting for any of them, do 266 // display it. 267 for (int subId : SubscriptionController.getInstance().getActiveSubIdList(true)) { 268 if (PhoneGlobals.getInstance().phoneMgr.isRttSupported(subId)) { 269 return true; 270 } 271 } 272 return false; 273 } 274 275 /** 276 * Determines if the device supports TTY per carrier config. 277 * @return {@code true} if the carrier supports TTY, {@code false} otherwise. 278 */ isTtySupportedByCarrier()279 private boolean isTtySupportedByCarrier() { 280 CarrierConfigManager configManager = 281 (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE); 282 return configManager.getConfig().getBoolean( 283 CarrierConfigManager.KEY_TTY_SUPPORTED_BOOL); 284 } 285 286 /** 287 * Determines from carrier config whether to always allow RTT while roaming. 288 */ isCarrierAllowRttWhenRoaming(int subId)289 private boolean isCarrierAllowRttWhenRoaming(int subId) { 290 PersistableBundle b = 291 PhoneGlobals.getInstance().getCarrierConfigForSubId(subId); 292 return b.getBoolean(CarrierConfigManager.KEY_RTT_SUPPORTED_WHILE_ROAMING_BOOL); 293 } 294 } 295