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 17package com.android.server.telecom; 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.telecom.Log; 27import android.telecom.TelecomManager; 28 29import com.android.internal.util.IndentingPrintWriter; 30 31// TODO: Needed for move to system service: import com.android.internal.R; 32 33final 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 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 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 63 int getCurrentTtyMode() { 64 return mCurrentTtyMode; 65 } 66 67 @Override 68 public void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn) { 69 Log.v(this, "onWiredHeadsetPluggedInChanged"); 70 updateCurrentTtyMode(); 71 } 72 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 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 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 */ 139 public void dump(IndentingPrintWriter pw) { 140 pw.println("mCurrentTtyMode: " + mCurrentTtyMode); 141 } 142} 143