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
17package com.android.phone.settings;
18
19import android.content.Context;
20import android.media.AudioManager;
21import android.os.Bundle;
22import android.preference.CheckBoxPreference;
23import android.preference.Preference;
24import android.preference.PreferenceFragment;
25import android.preference.PreferenceScreen;
26import android.provider.Settings;
27import android.telecom.TelecomManager;
28import android.telephony.CarrierConfigManager;
29import android.telephony.PhoneStateListener;
30import android.telephony.TelephonyManager;
31import android.util.Log;
32
33import com.android.ims.ImsManager;
34import com.android.phone.PhoneGlobals;
35import com.android.phone.R;
36import com.android.phone.settings.TtyModeListPreference;
37
38public class AccessibilitySettingsFragment extends PreferenceFragment {
39    private static final String LOG_TAG = AccessibilitySettingsFragment.class.getSimpleName();
40    private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
41
42    private static final String BUTTON_TTY_KEY = "button_tty_mode_key";
43    private static final String BUTTON_HAC_KEY = "button_hac_key";
44
45    private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
46        /**
47         * Disable the TTY setting when in/out of a call (and if carrier doesn't
48         * support VoLTE with TTY).
49         * @see android.telephony.PhoneStateListener#onCallStateChanged(int,
50         * java.lang.String)
51         */
52        @Override
53        public void onCallStateChanged(int state, String incomingNumber) {
54            if (DBG) Log.d(LOG_TAG, "PhoneStateListener.onCallStateChanged: state=" + state);
55            Preference pref = getPreferenceScreen().findPreference(BUTTON_TTY_KEY);
56            if (pref != null) {
57                pref.setEnabled(state == TelephonyManager.CALL_STATE_IDLE);
58            }
59        }
60    };
61
62    private Context mContext;
63    private AudioManager mAudioManager;
64
65    private TtyModeListPreference mButtonTty;
66    private CheckBoxPreference mButtonHac;
67
68    @Override
69    public void onCreate(Bundle savedInstanceState) {
70        super.onCreate(savedInstanceState);
71
72        mContext = getActivity().getApplicationContext();
73        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
74
75        addPreferencesFromResource(R.xml.accessibility_settings);
76
77        mButtonTty = (TtyModeListPreference) findPreference(
78                getResources().getString(R.string.tty_mode_key));
79        mButtonHac = (CheckBoxPreference) findPreference(BUTTON_HAC_KEY);
80
81        if (PhoneGlobals.getInstance().phoneMgr.isTtyModeSupported()) {
82            mButtonTty.init();
83        } else {
84            getPreferenceScreen().removePreference(mButtonTty);
85            mButtonTty = null;
86        }
87
88        if (PhoneGlobals.getInstance().phoneMgr.isHearingAidCompatibilitySupported()) {
89            int hac = Settings.System.getInt(mContext.getContentResolver(),
90                    Settings.System.HEARING_AID, SettingsConstants.HAC_DISABLED);
91            mButtonHac.setChecked(hac == SettingsConstants.HAC_ENABLED);
92        } else {
93            getPreferenceScreen().removePreference(mButtonHac);
94            mButtonHac = null;
95        }
96    }
97
98    @Override
99    public void onResume() {
100        super.onResume();
101
102        if (ImsManager.isVolteEnabledByPlatform(mContext) && !getVolteTtySupported()) {
103            TelephonyManager tm =
104                    (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
105            tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
106        }
107    }
108
109    @Override
110    public void onPause() {
111        super.onPause();
112
113        if (ImsManager.isVolteEnabledByPlatform(mContext) && !getVolteTtySupported()) {
114            TelephonyManager tm =
115                    (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
116            tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
117        }
118    }
119
120    @Override
121    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
122        if (preference == mButtonTty) {
123            return true;
124        } else if (preference == mButtonHac) {
125            int hac = mButtonHac.isChecked()
126                    ? SettingsConstants.HAC_ENABLED : SettingsConstants.HAC_DISABLED;
127            // Update HAC value in Settings database.
128            Settings.System.putInt(mContext.getContentResolver(), Settings.System.HEARING_AID, hac);
129
130            // Update HAC Value in AudioManager.
131            mAudioManager.setParameter(SettingsConstants.HAC_KEY,
132                    hac == SettingsConstants.HAC_ENABLED
133                            ? SettingsConstants.HAC_VAL_ON : SettingsConstants.HAC_VAL_OFF);
134            return true;
135        }
136        return false;
137    }
138
139    private boolean getVolteTtySupported() {
140        CarrierConfigManager configManager =
141                (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
142        return configManager.getConfig().getBoolean(
143                CarrierConfigManager.KEY_CARRIER_VOLTE_TTY_SUPPORTED_BOOL);
144    }
145}
146