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 
     38     private final Handler mHandler = new Handler() {
     39         @Override
     40         public void handleMessage(Message msg) {
     41             switch (msg.what) {
     42                 case MSG_SET_TTY_MODE_RESPONSE: {
     43                     Log.v(TtyManager.this, "got setTtyMode response");
     44                     AsyncResult ar = (AsyncResult) msg.obj;
     45                     if (ar.exception != null) {
     46                         Log.d(TtyManager.this, "setTTYMode exception: %s", ar.exception);
     47                     }
     48                     mPhone.queryTTYMode(obtainMessage(MSG_GET_TTY_MODE_RESPONSE));
     49                     break;
     50                 }
     51                 case MSG_GET_TTY_MODE_RESPONSE: {
     52                     Log.v(TtyManager.this, "got queryTTYMode response");
     53                     AsyncResult ar = (AsyncResult) msg.obj;
     54                     if (ar.exception != null) {
     55                         Log.d(TtyManager.this, "queryTTYMode exception: %s", ar.exception);
     56                     } else {
     57                         int ttyMode = phoneModeToTelecomMode(((int[]) ar.result)[0]);
     58                         if (ttyMode != mTtyMode) {
     59                             Log.d(TtyManager.this, "setting TTY mode failed, attempted %d, got: %d",
     60                                     mTtyMode, ttyMode);
     61                         } else {
     62                             Log.d(TtyManager.this, "setting TTY mode to %d succeeded", ttyMode);
     63                         }
     64                     }
     65                     break;
     66                 }
     67             }
     68         }
     69     };
     70 
     71     TtyManager(Context context, Phone phone) {
     72         mPhone = phone;
     73 
     74         IntentFilter intentFilter = new IntentFilter(
     75                 TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED);
     76         context.registerReceiver(mReceiver, intentFilter);
     77 
     78         int ttyMode = TelecomManager.TTY_MODE_OFF;
     79         TelecomManager telecomManager = TelecomManager.from(context);
     80         if (telecomManager != null) {
     81             ttyMode = telecomManager.getCurrentTtyMode();
     82         }
     83         updateTtyMode(ttyMode);
     84     }
     85 
     86     private void updateTtyMode(int ttyMode) {
     87         Log.v(this, "updateTtyMode %d -> %d", mTtyMode, ttyMode);
     88         mTtyMode = ttyMode;
     89         mPhone.setTTYMode(telecomModeToPhoneMode(ttyMode),
     90                 mHandler.obtainMessage(MSG_SET_TTY_MODE_RESPONSE));
     91     }
     92 
     93     private final class TtyBroadcastReceiver extends BroadcastReceiver {
     94         @Override
     95         public void onReceive(Context context, Intent intent) {
     96             String action = intent.getAction();
     97             Log.v(TtyManager.this, "onReceive, action: %s", action);
     98             if (action.equals(TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED)) {
     99                 int ttyMode = intent.getIntExtra(
    100                         TelecomManager.EXTRA_CURRENT_TTY_MODE, TelecomManager.TTY_MODE_OFF);
    101                 updateTtyMode(ttyMode);
    102             }
    103         }
    104     }
    105 
    106     private static int telecomModeToPhoneMode(int telecomMode) {
    107         switch (telecomMode) {
    108             case TelecomManager.TTY_MODE_FULL:
    109                 return Phone.TTY_MODE_FULL;
    110             case TelecomManager.TTY_MODE_VCO:
    111                 return Phone.TTY_MODE_VCO;
    112             case TelecomManager.TTY_MODE_HCO:
    113                 return Phone.TTY_MODE_HCO;
    114             case TelecomManager.TTY_MODE_OFF:
    115             default:
    116                 return Phone.TTY_MODE_OFF;
    117         }
    118     }
    119 
    120     private static int phoneModeToTelecomMode(int phoneMode) {
    121         switch (phoneMode) {
    122             case Phone.TTY_MODE_FULL:
    123                 return TelecomManager.TTY_MODE_FULL;
    124             case Phone.TTY_MODE_VCO:
    125                 return TelecomManager.TTY_MODE_VCO;
    126             case Phone.TTY_MODE_HCO:
    127                 return TelecomManager.TTY_MODE_HCO;
    128             case Phone.TTY_MODE_OFF:
    129             default:
    130                 return TelecomManager.TTY_MODE_OFF;
    131         }
    132     }
    133 }
    134