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 
126             boolean shouldDisableBecauseRoamingOffWfc = isRoaming && !isOnWfc();
127             if (shouldDisableBecauseRoamingOffWfc) {
128                 mButtonRtt.setSummary(TextUtils.concat(getText(R.string.rtt_mode_summary), "\n",
129                         getText(R.string.no_rtt_when_roaming)));
130             }
131             boolean rttOn = Settings.Secure.getInt(
132                     mContext.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
133             mButtonRtt.setChecked(rttOn);
134         } else {
135             getPreferenceScreen().removePreference(mButtonRtt);
136             Preference rttInfoPref = findPreference(RTT_INFO_PREF);
137             getPreferenceScreen().removePreference(rttInfoPref);
138             mButtonRtt = null;
139         }
140     }
141 
142     @Override
onResume()143     public void onResume() {
144         super.onResume();
145         TelephonyManager tm =
146                 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
147         tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
148     }
149 
150     @Override
onPause()151     public void onPause() {
152         super.onPause();
153         TelephonyManager tm =
154                 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
155         tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
156     }
157 
158     @Override
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)159     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
160         if (preference == mButtonTty) {
161             return true;
162         } else if (preference == mButtonHac) {
163             int hac = mButtonHac.isChecked()
164                     ? SettingsConstants.HAC_ENABLED : SettingsConstants.HAC_DISABLED;
165             // Update HAC value in Settings database.
166             Settings.System.putInt(mContext.getContentResolver(), Settings.System.HEARING_AID, hac);
167 
168             // Update HAC Value in AudioManager.
169             mAudioManager.setParameters(
170                     SettingsConstants.HAC_KEY + "=" + (hac == SettingsConstants.HAC_ENABLED
171                             ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF));
172             return true;
173         } else if (preference == mButtonRtt) {
174             Log.i(LOG_TAG, "RTT setting changed -- now " + mButtonRtt.isChecked());
175             int rttMode = mButtonRtt.isChecked() ? 1 : 0;
176             Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.RTT_CALLING_MODE,
177                     rttMode);
178             // Update RTT config with IMS Manager if the always-on carrier config isn't set to true.
179             CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
180                             Context.CARRIER_CONFIG_SERVICE);
181             for (int subId : SubscriptionController.getInstance().getActiveSubIdList(true)) {
182                 if (!configManager.getConfigForSubId(subId).getBoolean(
183                         CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL, false)) {
184                     int phoneId = SubscriptionController.getInstance().getPhoneId(subId);
185                     ImsManager imsManager = ImsManager.getInstance(getContext(), phoneId);
186                     imsManager.setRttEnabled(mButtonRtt.isChecked());
187                 }
188             }
189             return true;
190         }
191 
192         return false;
193     }
194 
isVolteTtySupportedInAnySlot()195     private boolean isVolteTtySupportedInAnySlot() {
196         final Phone[] phones = PhoneFactory.getPhones();
197         if (phones == null) {
198             if (DBG) Log.d(LOG_TAG, "isVolteTtySupportedInAnySlot: No phones found.");
199             return false;
200         }
201 
202         CarrierConfigManager configManager =
203                 (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
204         for (Phone phone : phones) {
205             // Check if this phone supports VoLTE.
206             ImsManager imsManager = ImsManager.getInstance(mContext, phone.getPhoneId());
207             boolean volteEnabled = false;
208             if (imsManager != null) {
209                 volteEnabled = imsManager.isVolteEnabledByPlatform();
210             }
211 
212             // Check if this phone suports VoLTE TTY.
213             boolean volteTtySupported = false;
214             PersistableBundle carrierConfig = configManager.getConfigForSubId(phone.getSubId());
215             if (carrierConfig != null) {
216                 volteTtySupported = carrierConfig.getBoolean(
217                         CarrierConfigManager.KEY_CARRIER_VOLTE_TTY_SUPPORTED_BOOL);
218             }
219 
220             if (volteEnabled && volteTtySupported) {
221                 // VoLTE TTY is supported on this phone that also suports VoLTE.
222                 return true;
223             }
224         }
225         // VoLTE TTY was not supported on any phone that also supports VoLTE.
226         return false;
227     }
228 
isVideoCallOrConferenceInProgress()229     private boolean isVideoCallOrConferenceInProgress() {
230         final Phone[] phones = PhoneFactory.getPhones();
231         if (phones == null) {
232             if (DBG) Log.d(LOG_TAG, "isVideoCallOrConferenceInProgress: No phones found.");
233             return false;
234         }
235 
236         for (Phone phone : phones) {
237             if (phone.isImsVideoCallOrConferencePresent()) {
238                 return true;
239             }
240         }
241         return false;
242     }
243 
isOnWfc()244     private boolean isOnWfc() {
245         LinkedBlockingQueue<Integer> result = new LinkedBlockingQueue<>(1);
246         Executor executor = Executors.newSingleThreadExecutor();
247         mContext.getSystemService(android.telephony.ims.ImsManager.class)
248                 .getImsMmTelManager(SubscriptionManager.getDefaultSubscriptionId())
249                 .getRegistrationTransportType(executor, result::offer);
250         try {
251             Integer transportType = result.poll(WFC_QUERY_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
252             return transportType != null
253                     && transportType == AccessNetworkConstants.TRANSPORT_TYPE_WLAN;
254         } catch (InterruptedException e) {
255             return false;
256         }
257     }
258 
shouldShowRttSetting()259     private boolean shouldShowRttSetting() {
260         // Go through all the subs -- if we want to display the RTT setting for any of them, do
261         // display it.
262         for (int subId : SubscriptionController.getInstance().getActiveSubIdList(true)) {
263             if (PhoneGlobals.getInstance().phoneMgr.isRttSupported(subId)) {
264                 return true;
265             }
266         }
267         return false;
268     }
269 
270     /**
271      * Determines if the device supports TTY per carrier config.
272      * @return {@code true} if the carrier supports TTY, {@code false} otherwise.
273      */
isTtySupportedByCarrier()274     private boolean isTtySupportedByCarrier() {
275         CarrierConfigManager configManager =
276                 (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
277         return configManager.getConfig().getBoolean(
278                 CarrierConfigManager.KEY_TTY_SUPPORTED_BOOL);
279     }
280 }
281