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;
55             buttonTtyMode = Integer.valueOf((String) objValue).intValue();
56             int settingsTtyMode = android.provider.Settings.Secure.getInt(
57                     getContext().getContentResolver(),
58                     Settings.Secure.PREFERRED_TTY_MODE,
59                     TelecomManager.TTY_MODE_OFF);
60             if (DBG) log("handleTTYChange: requesting set TTY mode enable (TTY) to" +
61                     Integer.toString(buttonTtyMode));
62 
63             if (buttonTtyMode != settingsTtyMode) {
64                 switch(buttonTtyMode) {
65                     case TelecomManager.TTY_MODE_OFF:
66                     case TelecomManager.TTY_MODE_FULL:
67                     case TelecomManager.TTY_MODE_HCO:
68                     case TelecomManager.TTY_MODE_VCO:
69                         Settings.Secure.putInt(
70                                 getContext().getContentResolver(),
71                                 Settings.Secure.PREFERRED_TTY_MODE,
72                                 buttonTtyMode);
73                         break;
74                     default:
75                         buttonTtyMode = TelecomManager.TTY_MODE_OFF;
76                 }
77 
78                 setValue(Integer.toString(buttonTtyMode));
79                 updatePreferredTtyModeSummary(buttonTtyMode);
80                 Intent ttyModeChanged =
81                         new Intent(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
82                 ttyModeChanged.putExtra(TelecomManager.EXTRA_TTY_PREFERRED_MODE, buttonTtyMode);
83                 getContext().sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
84             }
85         }
86         return true;
87     }
88 
updatePreferredTtyModeSummary(int TtyMode)89     private void updatePreferredTtyModeSummary(int TtyMode) {
90         String [] txts = getContext().getResources().getStringArray(R.array.tty_mode_entries);
91         switch(TtyMode) {
92             case TelecomManager.TTY_MODE_OFF:
93             case TelecomManager.TTY_MODE_HCO:
94             case TelecomManager.TTY_MODE_VCO:
95             case TelecomManager.TTY_MODE_FULL:
96                 setSummary(txts[TtyMode]);
97                 break;
98             default:
99                 setEnabled(false);
100                 setSummary(txts[TelecomManager.TTY_MODE_OFF]);
101                 break;
102         }
103     }
104 
log(String msg)105     private static void log(String msg) {
106         Log.d(LOG_TAG, msg);
107     }
108 }
109