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