• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.content.Intent;
21 import android.os.PersistableBundle;
22 import android.os.UserHandle;
23 import android.preference.ListPreference;
24 import android.preference.Preference;
25 import android.provider.Settings;
26 import android.provider.Settings.Secure;
27 import android.telecom.TelecomManager;
28 import android.telephony.CarrierConfigManager;
29 import android.telephony.SubscriptionManager;
30 import android.util.AttributeSet;
31 import android.util.Log;
32 
33 import com.android.phone.PhoneGlobals;
34 import com.android.phone.R;
35 
36 import java.util.Arrays;
37 
38 public class TtyModeListPreference extends ListPreference
39         implements Preference.OnPreferenceChangeListener {
40     private static final String LOG_TAG = TtyModeListPreference.class.getSimpleName();
41     private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
42 
TtyModeListPreference(Context context, AttributeSet attrs)43     public TtyModeListPreference(Context context, AttributeSet attrs) {
44         super(context, attrs);
45     }
46 
init()47     public void init() {
48         setOnPreferenceChangeListener(this);
49 
50         int settingsTtyMode = Settings.Secure.getInt(getContext().getContentResolver(),
51                 Settings.Secure.PREFERRED_TTY_MODE,
52                 TelecomManager.TTY_MODE_OFF);
53         if (shouldHideHcoAndVco()) {
54             setEntries(Arrays.copyOfRange(
55                     getContext().getResources().getTextArray(R.array.tty_mode_entries), 0, 2));
56             setEntryValues(Arrays.copyOfRange(
57                     getContext().getResources().getTextArray(R.array.tty_mode_values), 0, 2));
58             if (settingsTtyMode == TelecomManager.TTY_MODE_HCO
59                     || settingsTtyMode == TelecomManager.TTY_MODE_VCO) {
60                 // If the persisted setting is HCO or VCO, set it to full here
61                 settingsTtyMode = TelecomManager.TTY_MODE_FULL;
62                 Settings.Secure.putInt(getContext().getContentResolver(), Secure.PREFERRED_TTY_MODE,
63                         settingsTtyMode);
64             }
65         }
66         setValue(Integer.toString(settingsTtyMode));
67         updatePreferredTtyModeSummary(settingsTtyMode);
68     }
69 
70     @Override
onPreferenceChange(Preference preference, Object objValue)71     public boolean onPreferenceChange(Preference preference, Object objValue) {
72         if (preference == this) {
73             int buttonTtyMode = Integer.parseInt((String) objValue);
74             int settingsTtyMode = android.provider.Settings.Secure.getInt(
75                     getContext().getContentResolver(),
76                     Settings.Secure.PREFERRED_TTY_MODE,
77                     TelecomManager.TTY_MODE_OFF);
78             if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
79                     Integer.toString(buttonTtyMode));
80 
81             if (buttonTtyMode != settingsTtyMode) {
82                 switch(buttonTtyMode) {
83                     case TelecomManager.TTY_MODE_OFF:
84                     case TelecomManager.TTY_MODE_FULL:
85                     case TelecomManager.TTY_MODE_HCO:
86                     case TelecomManager.TTY_MODE_VCO:
87                         Settings.Secure.putInt(
88                                 getContext().getContentResolver(),
89                                 Settings.Secure.PREFERRED_TTY_MODE,
90                                 buttonTtyMode);
91                         break;
92                     default:
93                         buttonTtyMode = TelecomManager.TTY_MODE_OFF;
94                 }
95 
96                 setValue(Integer.toString(buttonTtyMode));
97                 updatePreferredTtyModeSummary(buttonTtyMode);
98                 Intent ttyModeChanged =
99                         new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
100                 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
101                 getContext().sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
102             }
103         }
104         return true;
105     }
106 
updatePreferredTtyModeSummary(int TtyMode)107     private void updatePreferredTtyModeSummary(int TtyMode) {
108         String [] txts = getContext().getResources().getStringArray(R.array.tty_mode_entries);
109         switch(TtyMode) {
110             case TelecomManager.TTY_MODE_OFF:
111             case TelecomManager.TTY_MODE_HCO:
112             case TelecomManager.TTY_MODE_VCO:
113             case TelecomManager.TTY_MODE_FULL:
114                 setSummary(txts[TtyMode]);
115                 break;
116             default:
117                 setEnabled(false);
118                 setSummary(txts[TelecomManager.TTY_MODE_OFF]);
119                 break;
120         }
121     }
122 
shouldHideHcoAndVco()123     private boolean shouldHideHcoAndVco() {
124         CarrierConfigManager carrierConfigManager =
125                 getContext().getSystemService(CarrierConfigManager.class);
126         PersistableBundle config = carrierConfigManager.getConfigForSubId(
127                 SubscriptionManager.getDefaultVoiceSubscriptionId());
128         boolean carrierShouldHideHcoVco = config != null && config.getBoolean(
129                 CarrierConfigManager.KEY_HIDE_TTY_HCO_VCO_WITH_RTT_BOOL, false);
130         boolean isRttSupported = PhoneGlobals.getInstance().phoneMgr.isRttSupported(
131                 SubscriptionManager.getDefaultVoiceSubscriptionId());
132         return carrierShouldHideHcoVco && isRttSupported;
133     }
134 
log(String msg)135     private static void log(String msg) {
136         Log.d(LOG_TAG, msg);
137     }
138 }
139