blob: c94cd30e7299795f5197d57067ccef7e1d81f73e [file] [log] [blame]
Sailesh Nepalb88795a2014-07-15 14:53:27 -07001/*
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
17package com.android.telecomm;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.media.AudioManager;
24import android.os.UserHandle;
25import android.provider.Settings;
26import android.telecomm.TelecommConstants;
27
28final class TtyManager implements WiredHeadsetManager.Listener {
29 private final TtyBroadcastReceiver mReceiver = new TtyBroadcastReceiver();
30 private final Context mContext;
31 private final WiredHeadsetManager mWiredHeadsetManager;
32 private int mPreferredTtyMode = TelecommConstants.TTY_MODE_OFF;
33 private int mCurrentTtyMode = TelecommConstants.TTY_MODE_OFF;
34
35 TtyManager(Context context, WiredHeadsetManager wiredHeadsetManager) {
36 mContext = context;
37 mWiredHeadsetManager = wiredHeadsetManager;
38 mWiredHeadsetManager.addListener(this);
39
40 mPreferredTtyMode = Settings.Secure.getInt(
41 mContext.getContentResolver(),
42 Settings.Secure.PREFERRED_TTY_MODE,
43 TelecommConstants.TTY_MODE_OFF);
44
45 IntentFilter intentFilter = new IntentFilter(
46 TelecommConstants.ACTION_TTY_PREFERRED_MODE_CHANGED);
47 mContext.registerReceiver(mReceiver, intentFilter);
48
49 updateCurrentTtyMode();
50 }
51
52 boolean isTtySupported() {
53 boolean isEnabled = mContext.getResources().getBoolean(R.bool.tty_enabled);
54 Log.v(this, "isTtySupported: " + isEnabled);
55 return isEnabled;
56 }
57
58 int getCurrentTtyMode() {
59 return mCurrentTtyMode;
60 }
61
62 @Override
63 public void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn) {
64 Log.v(this, "onWiredHeadsetPluggedInChanged");
65 updateCurrentTtyMode();
66 }
67
68 private void updateCurrentTtyMode() {
69 int newTtyMode = TelecommConstants.TTY_MODE_OFF;
70 if (isTtySupported() && mWiredHeadsetManager.isPluggedIn()) {
71 newTtyMode = mPreferredTtyMode;
72 }
73 Log.v(this, "updateCurrentTtyMode, %d -> %d", mCurrentTtyMode, newTtyMode);
74
75 if (mCurrentTtyMode != newTtyMode) {
76 mCurrentTtyMode = newTtyMode;
77 Intent ttyModeChanged = new Intent(TelecommConstants.ACTION_CURRENT_TTY_MODE_CHANGED);
78 ttyModeChanged.putExtra(TelecommConstants.EXTRA_CURRENT_TTY_MODE, mCurrentTtyMode);
79 mContext.sendBroadcastAsUser(ttyModeChanged, UserHandle.ALL);
80
81 updateAudioTtyMode();
82 }
83 }
84
85 private void updateAudioTtyMode() {
86 String audioTtyMode;
87 switch (mCurrentTtyMode) {
88 case TelecommConstants.TTY_MODE_FULL:
89 audioTtyMode = "tty_full";
90 break;
91 case TelecommConstants.TTY_MODE_VCO:
92 audioTtyMode = "tty_vco";
93 break;
94 case TelecommConstants.TTY_MODE_HCO:
95 audioTtyMode = "tty_hco";
96 break;
97 case TelecommConstants.TTY_MODE_OFF:
98 default:
99 audioTtyMode = "tty_off";
100 break;
101 }
102 Log.v(this, "updateAudioTtyMode, %s", audioTtyMode);
103
104 AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
105 audioManager.setParameters("tty_mode=" + audioTtyMode);
106 }
107
108 private final class TtyBroadcastReceiver extends BroadcastReceiver {
109 @Override
110 public void onReceive(Context context, Intent intent) {
111 String action = intent.getAction();
112 Log.v(TtyManager.this, "onReceive, action: %s", action);
113 if (action.equals(TelecommConstants.ACTION_TTY_PREFERRED_MODE_CHANGED)) {
114 int newPreferredTtyMode = intent.getIntExtra(
115 TelecommConstants.EXTRA_TTY_PREFERRED_MODE, TelecommConstants.TTY_MODE_OFF);
116 if (mPreferredTtyMode != newPreferredTtyMode) {
117 mPreferredTtyMode = newPreferredTtyMode;
118 updateCurrentTtyMode();
119 }
120 }
121 }
122 }
123}