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.subscription.SubscriptionManagerService; 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 int[] activeSubIds = SubscriptionManagerService.getInstance().getActiveSubIdList(true); 187 188 for (int subId : activeSubIds) { 189 if (!configManager.getConfigForSubId(subId).getBoolean( 190 CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL, false)) { 191 int phoneId = SubscriptionManager.getPhoneId(subId); 192 ImsManager imsManager = ImsManager.getInstance(getContext(), phoneId); 193 imsManager.setRttEnabled(mButtonRtt.isChecked()); 194 } 195 } 196 return true; 197 } 198 199 return false; 200 } 201 isVolteTtySupportedInAnySlot()202 private boolean isVolteTtySupportedInAnySlot() { 203 final Phone[] phones = PhoneFactory.getPhones(); 204 if (phones == null) { 205 if (DBG) Log.d(LOG_TAG, "isVolteTtySupportedInAnySlot: No phones found."); 206 return false; 207 } 208 209 CarrierConfigManager configManager = 210 (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE); 211 for (Phone phone : phones) { 212 // Check if this phone supports VoLTE. 213 ImsManager imsManager = ImsManager.getInstance(mContext, phone.getPhoneId()); 214 boolean volteEnabled = false; 215 if (imsManager != null) { 216 volteEnabled = imsManager.isVolteEnabledByPlatform(); 217 } 218 219 // Check if this phone suports VoLTE TTY. 220 boolean volteTtySupported = false; 221 PersistableBundle carrierConfig = configManager.getConfigForSubId(phone.getSubId()); 222 if (carrierConfig != null) { 223 volteTtySupported = carrierConfig.getBoolean( 224 CarrierConfigManager.KEY_CARRIER_VOLTE_TTY_SUPPORTED_BOOL); 225 } 226 227 if (volteEnabled && volteTtySupported) { 228 // VoLTE TTY is supported on this phone that also suports VoLTE. 229 return true; 230 } 231 } 232 // VoLTE TTY was not supported on any phone that also supports VoLTE. 233 return false; 234 } 235 isVideoCallOrConferenceInProgress()236 private boolean isVideoCallOrConferenceInProgress() { 237 final Phone[] phones = PhoneFactory.getPhones(); 238 if (phones == null) { 239 if (DBG) Log.d(LOG_TAG, "isVideoCallOrConferenceInProgress: No phones found."); 240 return false; 241 } 242 243 for (Phone phone : phones) { 244 if (phone.isImsVideoCallOrConferencePresent()) { 245 return true; 246 } 247 } 248 return false; 249 } 250 isOnWfc()251 private boolean isOnWfc() { 252 LinkedBlockingQueue<Integer> result = new LinkedBlockingQueue<>(1); 253 Executor executor = Executors.newSingleThreadExecutor(); 254 mContext.getSystemService(android.telephony.ims.ImsManager.class) 255 .getImsMmTelManager(SubscriptionManager.getDefaultSubscriptionId()) 256 .getRegistrationTransportType(executor, result::offer); 257 try { 258 Integer transportType = result.poll(WFC_QUERY_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); 259 return transportType != null 260 && transportType == AccessNetworkConstants.TRANSPORT_TYPE_WLAN; 261 } catch (InterruptedException e) { 262 return false; 263 } 264 } 265 shouldShowRttSetting()266 private boolean shouldShowRttSetting() { 267 // Go through all the subs -- if we want to display the RTT setting for any of them, do 268 // display it. 269 for (int subId : SubscriptionManagerService.getInstance().getActiveSubIdList(true)) { 270 if (PhoneGlobals.getInstance().phoneMgr.isRttSupported(subId)) { 271 return true; 272 } 273 } 274 return false; 275 } 276 277 /** 278 * Determines if the device supports TTY per carrier config. 279 * @return {@code true} if the carrier supports TTY, {@code false} otherwise. 280 */ isTtySupportedByCarrier()281 private boolean isTtySupportedByCarrier() { 282 CarrierConfigManager configManager = 283 (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE); 284 return configManager.getConfig().getBoolean( 285 CarrierConfigManager.KEY_TTY_SUPPORTED_BOOL); 286 } 287 288 /** 289 * Determines from carrier config whether to always allow RTT while roaming. 290 */ isCarrierAllowRttWhenRoaming(int subId)291 private boolean isCarrierAllowRttWhenRoaming(int subId) { 292 PersistableBundle b = 293 PhoneGlobals.getInstance().getCarrierConfigForSubId(subId); 294 return b.getBoolean(CarrierConfigManager.KEY_RTT_SUPPORTED_WHILE_ROAMING_BOOL); 295 } 296 } 297