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.services.telephony;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.os.AsyncResult;
24import android.os.Handler;
25import android.os.Message;
26import android.provider.Settings;
27import android.telecom.TelecomManager;
28
29import com.android.internal.telephony.Phone;
30
31final class TtyManager {
32    private final static int MSG_SET_TTY_MODE_RESPONSE = 1;
33    private final static int MSG_GET_TTY_MODE_RESPONSE = 2;
34
35    private final TtyBroadcastReceiver mReceiver = new TtyBroadcastReceiver();
36    private final Phone mPhone;
37    private int mTtyMode;
38    private int mUiTtyMode = -1;
39
40    private final Handler mHandler = new Handler() {
41        @Override
42        public void handleMessage(Message msg) {
43            switch (msg.what) {
44                case MSG_SET_TTY_MODE_RESPONSE: {
45                    Log.v(TtyManager.this, "got setTtyMode response");
46                    AsyncResult ar = (AsyncResult) msg.obj;
47                    if (ar.exception != null) {
48                        Log.d(TtyManager.this, "setTTYMode exception: %s", ar.exception);
49                    }
50                    mPhone.queryTTYMode(obtainMessage(MSG_GET_TTY_MODE_RESPONSE));
51                    break;
52                }
53                case MSG_GET_TTY_MODE_RESPONSE: {
54                    Log.v(TtyManager.this, "got queryTTYMode response");
55                    AsyncResult ar = (AsyncResult) msg.obj;
56                    if (ar.exception != null) {
57                        Log.d(TtyManager.this, "queryTTYMode exception: %s", ar.exception);
58                    } else {
59                        int ttyMode = phoneModeToTelecomMode(((int[]) ar.result)[0]);
60                        if (ttyMode != mTtyMode) {
61                            Log.d(TtyManager.this, "setting TTY mode failed, attempted %d, got: %d",
62                                    mTtyMode, ttyMode);
63                        } else {
64                            Log.d(TtyManager.this, "setting TTY mode to %d succeeded", ttyMode);
65                        }
66                    }
67                    break;
68                }
69            }
70        }
71    };
72
73    TtyManager(Context context, Phone phone) {
74        mPhone = phone;
75
76        IntentFilter intentFilter = new IntentFilter(
77                TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED);
78        intentFilter.addAction(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED);
79        context.registerReceiver(mReceiver, intentFilter);
80
81        int ttyMode = TelecomManager.TTY_MODE_OFF;
82        TelecomManager telecomManager = TelecomManager.from(context);
83        if (telecomManager != null) {
84            ttyMode = telecomManager.getCurrentTtyMode();
85        }
86        updateTtyMode(ttyMode);
87        //Get preferred TTY mode from data base as UI Tty mode is always user preferred Tty mode.
88        ttyMode = Settings.Secure.getInt(context.getContentResolver(),
89                Settings.Secure.PREFERRED_TTY_MODE, TelecomManager.TTY_MODE_OFF);
90        updateUiTtyMode(ttyMode);
91    }
92
93    private void updateTtyMode(int ttyMode) {
94        Log.v(this, "updateTtyMode %d -> %d", mTtyMode, ttyMode);
95        mTtyMode = ttyMode;
96        mPhone.setTTYMode(telecomModeToPhoneMode(ttyMode),
97                mHandler.obtainMessage(MSG_SET_TTY_MODE_RESPONSE));
98    }
99
100    private void updateUiTtyMode(int ttyMode) {
101        Log.i(this, "updateUiTtyMode %d -> %d", mUiTtyMode, ttyMode);
102        if(mUiTtyMode != ttyMode) {
103            mUiTtyMode = ttyMode;
104            mPhone.setUiTTYMode(telecomModeToPhoneMode(ttyMode), null);
105        } else {
106           Log.i(this, "ui tty mode didnt change");
107        }
108    }
109
110    private final class TtyBroadcastReceiver extends BroadcastReceiver {
111        @Override
112        public void onReceive(Context context, Intent intent) {
113            String action = intent.getAction();
114            Log.v(TtyManager.this, "onReceive, action: %s", action);
115            if (action.equals(TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED)) {
116                int ttyMode = intent.getIntExtra(
117                        TelecomManager.EXTRA_CURRENT_TTY_MODE, TelecomManager.TTY_MODE_OFF);
118                updateTtyMode(ttyMode);
119            } else if (action.equals(TelecomManager.ACTION_TTY_PREFERRED_MODE_CHANGED)) {
120                int newPreferredTtyMode = intent.getIntExtra(
121                        TelecomManager.EXTRA_TTY_PREFERRED_MODE, TelecomManager.TTY_MODE_OFF);
122                updateUiTtyMode(newPreferredTtyMode);
123            }
124        }
125    }
126
127    private static int telecomModeToPhoneMode(int telecomMode) {
128        switch (telecomMode) {
129            // AT command only has 0 and 1, so mapping VCO
130            // and HCO to FULL
131            case TelecomManager.TTY_MODE_FULL:
132            case TelecomManager.TTY_MODE_VCO:
133            case TelecomManager.TTY_MODE_HCO:
134                return Phone.TTY_MODE_FULL;
135            default:
136                return Phone.TTY_MODE_OFF;
137        }
138    }
139
140    private static int phoneModeToTelecomMode(int phoneMode) {
141        switch (phoneMode) {
142            case Phone.TTY_MODE_FULL:
143                return TelecomManager.TTY_MODE_FULL;
144            case Phone.TTY_MODE_VCO:
145                return TelecomManager.TTY_MODE_VCO;
146            case Phone.TTY_MODE_HCO:
147                return TelecomManager.TTY_MODE_HCO;
148            case Phone.TTY_MODE_OFF:
149            default:
150                return TelecomManager.TTY_MODE_OFF;
151        }
152    }
153}
154