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.UserHandle;
22 import android.preference.ListPreference;
23 import android.preference.Preference;
24 import android.provider.Settings;
25 import android.telecom.TelecomManager;
26 import android.util.AttributeSet;
27 import android.util.Log;
28 
29 import com.android.phone.PhoneGlobals;
30 import com.android.phone.R;
31 
32 public class TtyModeListPreference extends ListPreference
33         implements Preference.OnPreferenceChangeListener {
34     private static final String LOG_TAG = TtyModeListPreference.class.getSimpleName();
35     private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
36 
TtyModeListPreference(Context context, AttributeSet attrs)37     public TtyModeListPreference(Context context, AttributeSet attrs) {
38         super(context, attrs);
39     }
40 
init()41     public void init() {
42         setOnPreferenceChangeListener(this);
43 
44         int settingsTtyMode = Settings.Secure.getInt(getContext().getContentResolver(),
45                 Settings.Secure.PREFERRED_TTY_MODE,
46                 TelecomManager.TTY_MODE_OFF);
47         setValue(Integer.toString(settingsTtyMode));
48         updatePreferredTtyModeSummary(settingsTtyMode);
49     }
50 
51     @Override
onPreferenceChange(Preference preference, Object objValue)52     public boolean onPreferenceChange(Preference preference, Object objValue) {
53         if (preference == this) {
54             int buttonTtyMode = Integer.parseInt((String) objValue);
55             int settingsTtyMode = android.provider.Settings.Secure.getInt(
56                     getContext().getContentResolver(),
57                     Settings.Secure.PREFERRED_TTY_MODE,
58                     TelecomManager.TTY_MODE_OFF);
59             if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
60                     Integer.toString(buttonTtyMode));
61 
62             if (buttonTtyMode != settingsTtyMode) {
63                 switch(buttonTtyMode) {
64                     case TelecomManager.TTY_MODE_OFF:
65                     case TelecomManager.TTY_MODE_FULL:
66                     case TelecomManager.TTY_MODE_HCO:
67                     case TelecomManager.TTY_MODE_VCO:
68                         Settings.Secure.putInt(
69                                 getContext().getContentResolver(),
70                                 Settings.Secure.PREFERRED_TTY_MODE,
71                                 buttonTtyMode);
72                         break;
73                     default:
74                         buttonTtyMode = TelecomManager.TTY_MODE_OFF;
75                 }
76 
77                 setValue(Integer.toString(buttonTtyMode));
78                 updatePreferredTtyModeSummary(buttonTtyMode);
79                 Intent ttyModeChanged =
80                         new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
81                 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
82                 getContext().sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
83             }
84         }
85         return true;
86     }
87 
updatePreferredTtyModeSummary(int TtyMode)88     private void updatePreferredTtyModeSummary(int TtyMode) {
89         String [] txts = getContext().getResources().getStringArray(R.array.tty_mode_entries);
90         switch(TtyMode) {
91             case TelecomManager.TTY_MODE_OFF:
92             case TelecomManager.TTY_MODE_HCO:
93             case TelecomManager.TTY_MODE_VCO:
94             case TelecomManager.TTY_MODE_FULL:
95                 setSummary(txts[TtyMode]);
96                 break;
97             default:
98                 setEnabled(false);
99                 setSummary(txts[TelecomManager.TTY_MODE_OFF]);
100                 break;
101         }
102     }
103 
log(String msg)104     private static void log(String msg) {
105         Log.d(LOG_TAG, msg);
106     }
107 }
108