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.server.telecom;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.media.AudioManager;
24 import android.os.UserHandle;
25 import android.provider.Settings;
26 import android.telecom.Log;
27 import android.telecom.TelecomManager;
28 
29 import com.android.internal.util.IndentingPrintWriter;
30 
31 // TODO: Needed for move to system service: import com.android.internal.R;
32 
33 final class TtyManager implements WiredHeadsetManager.Listener {
34     private final TtyBroadcastReceiver mReceiver = new TtyBroadcastReceiver();
35     private final Context mContext;
36     private final WiredHeadsetManager mWiredHeadsetManager;
37     private int mPreferredTtyMode = TelecomManager.TTY_MODE_OFF;
38     private int mCurrentTtyMode = TelecomManager.TTY_MODE_OFF;
39 
TtyManager(Context context, WiredHeadsetManager wiredHeadsetManager)40     TtyManager(Context context, WiredHeadsetManager wiredHeadsetManager) {
41         mContext = context;
42         mWiredHeadsetManager = wiredHeadsetManager;
43         mWiredHeadsetManager.addListener(this);
44 
45         mPreferredTtyMode = Settings.Secure.getInt(
46                 mContext.getContentResolver(),
47                 Settings.Secure.PREFERRED_TTY_MODE,
48                 TelecomManager.TTY_MODE_OFF);
49 
50         IntentFilter intentFilter = new IntentFilter(
51                 TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
52         mContext.registerReceiver(mReceiver, intentFilter);
53 
54         updateCurrentTtyMode();
55     }
56 
isTtySupported()57     boolean isTtySupported() {
58         boolean isEnabled = mContext.getResources().getBoolean(R.bool.tty_enabled);
59         Log.v(this, "isTtySupported: %b", isEnabled);
60         return isEnabled;
61     }
62 
getCurrentTtyMode()63     int getCurrentTtyMode() {
64         return mCurrentTtyMode;
65     }
66 
67     @Override
onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn)68     public void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn) {
69         Log.v(this, "onWiredHeadsetPluggedInChanged");
70         updateCurrentTtyMode();
71     }
72 
updateCurrentTtyMode()73     private void updateCurrentTtyMode() {
74         int newTtyMode = TelecomManager.TTY_MODE_OFF;
75         if (isTtySupported() && mWiredHeadsetManager.isPluggedIn()) {
76             newTtyMode = mPreferredTtyMode;
77         }
78         Log.v(this, "updateCurrentTtyMode, %d -> %d", mCurrentTtyMode, newTtyMode);
79 
80         if (mCurrentTtyMode != newTtyMode) {
81             mCurrentTtyMode = newTtyMode;
82             Intent ttyModeChanged = new Intent(TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED);
83             ttyModeChanged.putExtra(TelecomManager.EXTRA_CURRENT_TTY_MODE, mCurrentTtyMode);
84             mContext.sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
85 
86             updateAudioTtyMode();
87         }
88     }
89 
updateAudioTtyMode()90     private void updateAudioTtyMode() {
91         String audioTtyMode;
92         switch (mCurrentTtyMode) {
93             case TelecomManager.TTY_MODE_FULL:
94                 audioTtyMode = "tty_full";
95                 break;
96             case TelecomManager.TTY_MODE_VCO:
97                 audioTtyMode = "tty_vco";
98                 break;
99             case TelecomManager.TTY_MODE_HCO:
100                 audioTtyMode = "tty_hco";
101                 break;
102             case TelecomManager.TTY_MODE_OFF:
103             default:
104                 audioTtyMode = "tty_off";
105                 break;
106         }
107         Log.v(this, "updateAudioTtyMode, %s", audioTtyMode);
108 
109         AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
110         audioManager.setParameters("tty_mode=" + audioTtyMode);
111     }
112 
113     private final class TtyBroadcastReceiver extends BroadcastReceiver {
114         @Override
onReceive(Context context, Intent intent)115         public void onReceive(Context context, Intent intent) {
116             Log.startSession("TBR.oR");
117             try {
118                 String action = intent.getAction();
119                 Log.v(TtyManager.this, "onReceive, action: %s", action);
120                 if (action.equals(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED)) {
121                     int newPreferredTtyMode = intent.getIntExtra(
122                             TelecomManager.EXTRA_TTY_PREFERRED_MODE, TelecomManager.TTY_MODE_OFF);
123                     if (mPreferredTtyMode != newPreferredTtyMode) {
124                         mPreferredTtyMode = newPreferredTtyMode;
125                         updateCurrentTtyMode();
126                     }
127                 }
128             } finally {
129                 Log.endSession();
130             }
131         }
132     }
133 
134     /**
135      * Dumps the state of the {@link TtyManager}.
136      *
137      * @param pw The {@code IndentingPrintWriter} to write the state to.
138      */
dump(IndentingPrintWriter pw)139     public void dump(IndentingPrintWriter pw) {
140         pw.println("mCurrentTtyMode: " + mCurrentTtyMode);
141     }
142 }
143