Home | History | Annotate | Download | only in settings
      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.CheckBoxPreference;
     23 import android.preference.Preference;
     24 import android.preference.PreferenceFragment;
     25 import android.preference.PreferenceScreen;
     26 import android.provider.Settings;
     27 import android.telecom.TelecomManager;
     28 import android.telephony.CarrierConfigManager;
     29 import android.telephony.PhoneStateListener;
     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 import com.android.phone.settings.TtyModeListPreference;
     39 
     40 import java.util.List;
     41 
     42 public class AccessibilitySettingsFragment extends PreferenceFragment {
     43     private static final String LOG_TAG = AccessibilitySettingsFragment.class.getSimpleName();
     44     private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
     45 
     46     private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
     47     private static final String BUTTON_HAC_KEY = "button_hac_key";
     48 
     49     private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
     50         /**
     51          * Disable the TTY setting when in/out of a call (and if carrier doesn't
     52          * support VoLTE with TTY).
     53          * @see android.telephony.PhoneStateListener#onCallStateChanged(int,
     54          * java.lang.String)
     55          */
     56         @Override
     57         public void onCallStateChanged(int state, String incomingNumber) {
     58             if (DBG) Log.d(LOG_TAG, "PhoneStateListener.onCallStateChanged: state=" + state);
     59             Preference pref = getPreferenceScreen().findPreference(BUTTON_TTY_KEY);
     60             if (pref != null) {
     61                 final boolean isVolteTtySupported = ImsManager.isVolteEnabledByPlatform(mContext)
     62                         && getVolteTtySupported();
     63                 pref.setEnabled((isVolteTtySupported && !isVideoCallInProgress()) ||
     64                         (state == TelephonyManager.CALL_STATE_IDLE));
     65             }
     66         }
     67     };
     68 
     69     private Context mContext;
     70     private AudioManager mAudioManager;
     71 
     72     private TtyModeListPreference mButtonTty;
     73     private CheckBoxPreference mButtonHac;
     74 
     75     @Override
     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 = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
     87 
     88         if (PhoneGlobals.getInstance().phoneMgr.isTtyModeSupported()) {
     89             mButtonTty.init();
     90         } else {
     91             getPreferenceScreen().removePreference(mButtonTty);
     92             mButtonTty = null;
     93         }
     94 
     95         if (PhoneGlobals.getInstance().phoneMgr.isHearingAidCompatibilitySupported()) {
     96             int hac = Settings.System.getInt(mContext.getContentResolver(),
     97                     Settings.System.HEARING_AID, SettingsConstants.HAC_DISABLED);
     98             mButtonHac.setChecked(hac == SettingsConstants.HAC_ENABLED);
     99         } else {
    100             getPreferenceScreen().removePreference(mButtonHac);
    101             mButtonHac = null;
    102         }
    103     }
    104 
    105     @Override
    106     public void onResume() {
    107         super.onResume();
    108         TelephonyManager tm =
    109                 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
    110         tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    111     }
    112 
    113     @Override
    114     public void onPause() {
    115         super.onPause();
    116         TelephonyManager tm =
    117                 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
    118         tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
    119     }
    120 
    121     @Override
    122     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    123         if (preference == mButtonTty) {
    124             return true;
    125         } else if (preference == mButtonHac) {
    126             int hac = mButtonHac.isChecked()
    127                     ? SettingsConstants.HAC_ENABLED : SettingsConstants.HAC_DISABLED;
    128             // Update HAC value in Settings database.
    129             Settings.System.putInt(mContext.getContentResolver(), Settings.System.HEARING_AID, hac);
    130 
    131             // Update HAC Value in AudioManager.
    132             mAudioManager.setParameter(SettingsConstants.HAC_KEY,
    133                     hac == SettingsConstants.HAC_ENABLED
    134                             ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF);
    135             return true;
    136         }
    137         return false;
    138     }
    139 
    140     private boolean getVolteTtySupported() {
    141         CarrierConfigManager configManager =
    142                 (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
    143         return configManager.getConfig().getBoolean(
    144                 CarrierConfigManager.KEY_CARRIER_VOLTE_TTY_SUPPORTED_BOOL);
    145     }
    146 
    147     private boolean isVideoCallInProgress() {
    148         final Phone[] phones = PhoneFactory.getPhones();
    149         if (phones == null) {
    150             if (DBG) Log.d(LOG_TAG, "isVideoCallInProgress: No phones found. Return false");
    151             return false;
    152         }
    153 
    154         for (Phone phone : phones) {
    155             if (phone.isVideoCallPresent()) {
    156                 return true;
    157             }
    158         }
    159         return false;
    160     }
    161 }
    162