Home | History | Annotate | Download | only in telephony
      1 /*
      2  * Copyright (C) 2016 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.googlecode.android_scripting.facade.telephony;
     18 import com.android.ims.ImsConfig;
     19 import com.android.internal.telephony.RILConstants;
     20 import com.googlecode.android_scripting.Log;
     21 import android.telecom.TelecomManager;
     22 import android.telephony.DataConnectionRealTimeInfo;
     23 import android.telephony.PreciseCallState;
     24 import android.telephony.ServiceState;
     25 import android.telephony.TelephonyManager;
     26 import android.telephony.VoLteServiceState;
     27 
     28 /**
     29  * Telephony utility functions
     30  */
     31 public class TelephonyUtils {
     32 
     33     public static String getWfcModeString(int mode) {
     34        switch(mode) {
     35            case ImsConfig.WfcModeFeatureValueConstants.WIFI_PREFERRED:
     36                return TelephonyConstants.WFC_MODE_WIFI_PREFERRED;
     37            case ImsConfig.WfcModeFeatureValueConstants.CELLULAR_PREFERRED:
     38                return TelephonyConstants.WFC_MODE_CELLULAR_PREFERRED;
     39            case ImsConfig.WfcModeFeatureValueConstants.WIFI_ONLY:
     40                return TelephonyConstants.WFC_MODE_WIFI_ONLY;
     41        }
     42         Log.d("getWfcModeStringfromInt error. int: " + mode);
     43         return TelephonyConstants.WFC_MODE_UNKNOWN;
     44     }
     45 
     46     public static String getTtyModeString(int mode) {
     47         switch (mode) {
     48             case TelecomManager.TTY_MODE_FULL:
     49                 return TelephonyConstants.TTY_MODE_FULL;
     50             case TelecomManager.TTY_MODE_HCO:
     51                 return TelephonyConstants.TTY_MODE_HCO;
     52             case TelecomManager.TTY_MODE_OFF:
     53                 return TelephonyConstants.TTY_MODE_OFF;
     54             case TelecomManager.TTY_MODE_VCO:
     55                 return TelephonyConstants.TTY_MODE_VCO;
     56         }
     57         Log.d("getTtyModeString error. int: " + mode);
     58         return null;
     59     }
     60 
     61     public static String getPhoneTypeString(int type) {
     62         switch (type) {
     63             case TelephonyManager.PHONE_TYPE_GSM:
     64                 return TelephonyConstants.PHONE_TYPE_GSM;
     65             case TelephonyManager.PHONE_TYPE_NONE:
     66                 return TelephonyConstants.PHONE_TYPE_NONE;
     67             case TelephonyManager.PHONE_TYPE_CDMA:
     68                 return TelephonyConstants.PHONE_TYPE_CDMA;
     69             case TelephonyManager.PHONE_TYPE_SIP:
     70                 return TelephonyConstants.PHONE_TYPE_SIP;
     71         }
     72         Log.d("getPhoneTypeString error. int: " + type);
     73         return null;
     74     }
     75 
     76     public static String getSimStateString(int state) {
     77         switch (state) {
     78             case TelephonyManager.SIM_STATE_UNKNOWN:
     79                 return TelephonyConstants.SIM_STATE_UNKNOWN;
     80             case TelephonyManager.SIM_STATE_ABSENT:
     81                 return TelephonyConstants.SIM_STATE_ABSENT;
     82             case TelephonyManager.SIM_STATE_PIN_REQUIRED:
     83                 return TelephonyConstants.SIM_STATE_PIN_REQUIRED;
     84             case TelephonyManager.SIM_STATE_PUK_REQUIRED:
     85                 return TelephonyConstants.SIM_STATE_PUK_REQUIRED;
     86             case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
     87                 return TelephonyConstants.SIM_STATE_NETWORK_LOCKED;
     88             case TelephonyManager.SIM_STATE_READY:
     89                 return TelephonyConstants.SIM_STATE_READY;
     90             case TelephonyManager.SIM_STATE_NOT_READY:
     91                 return TelephonyConstants.SIM_STATE_NOT_READY;
     92             case TelephonyManager.SIM_STATE_PERM_DISABLED:
     93                 return TelephonyConstants.SIM_STATE_PERM_DISABLED;
     94             case TelephonyManager.SIM_STATE_CARD_IO_ERROR:
     95                 return TelephonyConstants.SIM_STATE_CARD_IO_ERROR;
     96         }
     97         Log.d("getSimStateString error. int: " + state);
     98         return TelephonyConstants.SIM_STATE_UNKNOWN;
     99     }
    100 
    101     public static String formatIncomingNumber(String incomingNumber) {
    102         String mIncomingNumber = null;
    103         int len = 0;
    104         if (incomingNumber != null) {
    105             len = incomingNumber.length();
    106         }
    107         if (len > 0) {
    108             /**
    109              * Currently this incomingNumber modification is specific for
    110              * US numbers.
    111              */
    112             if ((12 == len) && ('+' == incomingNumber.charAt(0))) {
    113                 mIncomingNumber = incomingNumber.substring(1);
    114             } else if (10 == len) {
    115                 mIncomingNumber = '1' + incomingNumber;
    116             } else {
    117                 mIncomingNumber = incomingNumber;
    118             }
    119         }
    120         return mIncomingNumber;
    121     }
    122 
    123     public static String getTelephonyCallStateString(int callState) {
    124         switch (callState) {
    125             case TelephonyManager.CALL_STATE_IDLE:
    126                 return TelephonyConstants.TELEPHONY_STATE_IDLE;
    127             case TelephonyManager.CALL_STATE_OFFHOOK:
    128                 return TelephonyConstants.TELEPHONY_STATE_OFFHOOK;
    129             case TelephonyManager.CALL_STATE_RINGING:
    130                 return TelephonyConstants.TELEPHONY_STATE_RINGING;
    131         }
    132         Log.d("getTelephonyCallStateString error. int: " + callState);
    133         return TelephonyConstants.TELEPHONY_STATE_UNKNOWN;
    134     }
    135 
    136     public static String getPreciseCallStateString(int state) {
    137         switch (state) {
    138             case PreciseCallState.PRECISE_CALL_STATE_ACTIVE:
    139                 return TelephonyConstants.PRECISE_CALL_STATE_ACTIVE;
    140             case PreciseCallState.PRECISE_CALL_STATE_HOLDING:
    141                 return TelephonyConstants.PRECISE_CALL_STATE_HOLDING;
    142             case PreciseCallState.PRECISE_CALL_STATE_DIALING:
    143                 return TelephonyConstants.PRECISE_CALL_STATE_DIALING;
    144             case PreciseCallState.PRECISE_CALL_STATE_ALERTING:
    145                 return TelephonyConstants.PRECISE_CALL_STATE_ALERTING;
    146             case PreciseCallState.PRECISE_CALL_STATE_INCOMING:
    147                 return TelephonyConstants.PRECISE_CALL_STATE_INCOMING;
    148             case PreciseCallState.PRECISE_CALL_STATE_WAITING:
    149                 return TelephonyConstants.PRECISE_CALL_STATE_WAITING;
    150             case PreciseCallState.PRECISE_CALL_STATE_DISCONNECTED:
    151                 return TelephonyConstants.PRECISE_CALL_STATE_DISCONNECTED;
    152             case PreciseCallState.PRECISE_CALL_STATE_DISCONNECTING:
    153                 return TelephonyConstants.PRECISE_CALL_STATE_DISCONNECTING;
    154             case PreciseCallState.PRECISE_CALL_STATE_IDLE:
    155                 return TelephonyConstants.PRECISE_CALL_STATE_IDLE;
    156             case PreciseCallState.PRECISE_CALL_STATE_NOT_VALID:
    157                 return TelephonyConstants.PRECISE_CALL_STATE_INVALID;
    158         }
    159         Log.d("getPreciseCallStateString error. int: " + state);
    160         return TelephonyConstants.PRECISE_CALL_STATE_UNKNOWN;
    161     }
    162 
    163     public static String getDcPowerStateString(int state) {
    164         if (state == DataConnectionRealTimeInfo.DC_POWER_STATE_LOW) {
    165             return TelephonyConstants.DC_POWER_STATE_LOW;
    166         } else if (state == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH) {
    167             return TelephonyConstants.DC_POWER_STATE_HIGH;
    168         } else if (state == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM) {
    169             return TelephonyConstants.DC_POWER_STATE_MEDIUM;
    170         } else {
    171             return TelephonyConstants.DC_POWER_STATE_UNKNOWN;
    172         }
    173     }
    174 
    175     public static String getDataConnectionStateString(int state) {
    176         switch (state) {
    177             case TelephonyManager.DATA_DISCONNECTED:
    178                 return TelephonyConstants.DATA_STATE_DISCONNECTED;
    179             case TelephonyManager.DATA_CONNECTING:
    180                 return TelephonyConstants.DATA_STATE_CONNECTING;
    181             case TelephonyManager.DATA_CONNECTED:
    182                 return TelephonyConstants.DATA_STATE_CONNECTED;
    183             case TelephonyManager.DATA_SUSPENDED:
    184                 return TelephonyConstants.DATA_STATE_SUSPENDED;
    185             case TelephonyManager.DATA_UNKNOWN:
    186                 return TelephonyConstants.DATA_STATE_UNKNOWN;
    187         }
    188         Log.d("getDataConnectionStateString error. int: " + state);
    189         return TelephonyConstants.DATA_STATE_UNKNOWN;
    190     }
    191 
    192     public static int getNetworkModeIntfromString(String networkMode) {
    193         switch (networkMode) {
    194             case TelephonyConstants.NETWORK_MODE_WCDMA_PREF:
    195                 return RILConstants.NETWORK_MODE_WCDMA_PREF;
    196             case TelephonyConstants.NETWORK_MODE_GSM_ONLY:
    197                 return RILConstants.NETWORK_MODE_GSM_ONLY;
    198             case TelephonyConstants.NETWORK_MODE_WCDMA_ONLY:
    199                 return RILConstants.NETWORK_MODE_WCDMA_ONLY;
    200             case TelephonyConstants.NETWORK_MODE_GSM_UMTS:
    201                 return RILConstants.NETWORK_MODE_GSM_UMTS;
    202             case TelephonyConstants.NETWORK_MODE_CDMA:
    203                 return RILConstants.NETWORK_MODE_CDMA;
    204             case TelephonyConstants.NETWORK_MODE_CDMA_NO_EVDO:
    205                 return RILConstants.NETWORK_MODE_CDMA_NO_EVDO;
    206             case TelephonyConstants.NETWORK_MODE_EVDO_NO_CDMA:
    207                 return RILConstants.NETWORK_MODE_EVDO_NO_CDMA;
    208             case TelephonyConstants.NETWORK_MODE_GLOBAL:
    209                 return RILConstants.NETWORK_MODE_GLOBAL;
    210             case TelephonyConstants.NETWORK_MODE_LTE_CDMA_EVDO:
    211                 return RILConstants.NETWORK_MODE_LTE_CDMA_EVDO;
    212             case TelephonyConstants.NETWORK_MODE_LTE_GSM_WCDMA:
    213                 return RILConstants.NETWORK_MODE_LTE_GSM_WCDMA;
    214             case TelephonyConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
    215                 return RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA;
    216             case TelephonyConstants.NETWORK_MODE_LTE_ONLY:
    217                 return RILConstants.NETWORK_MODE_LTE_ONLY;
    218             case TelephonyConstants.NETWORK_MODE_LTE_WCDMA:
    219                 return RILConstants.NETWORK_MODE_LTE_WCDMA;
    220             case TelephonyConstants.NETWORK_MODE_TDSCDMA_ONLY:
    221                 return RILConstants.NETWORK_MODE_TDSCDMA_ONLY;
    222             case TelephonyConstants.NETWORK_MODE_TDSCDMA_WCDMA:
    223                 return RILConstants.NETWORK_MODE_TDSCDMA_WCDMA;
    224             case TelephonyConstants.NETWORK_MODE_LTE_TDSCDMA:
    225                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA;
    226             case TelephonyConstants.NETWORK_MODE_TDSCDMA_GSM:
    227                 return RILConstants.NETWORK_MODE_TDSCDMA_GSM;
    228             case TelephonyConstants.NETWORK_MODE_LTE_TDSCDMA_GSM:
    229                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM;
    230             case TelephonyConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA:
    231                 return RILConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA;
    232             case TelephonyConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA:
    233                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA;
    234             case TelephonyConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA:
    235                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA;
    236             case TelephonyConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
    237                 return RILConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
    238             case TelephonyConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
    239                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
    240         }
    241         Log.d("getNetworkModeIntfromString error. String: " + networkMode);
    242         return RILConstants.RIL_ERRNO_INVALID_RESPONSE;
    243     }
    244 
    245     public static String getNetworkModeStringfromInt(int networkMode) {
    246         switch (networkMode) {
    247             case RILConstants.NETWORK_MODE_WCDMA_PREF:
    248                 return TelephonyConstants.NETWORK_MODE_WCDMA_PREF;
    249             case RILConstants.NETWORK_MODE_GSM_ONLY:
    250                 return TelephonyConstants.NETWORK_MODE_GSM_ONLY;
    251             case RILConstants.NETWORK_MODE_WCDMA_ONLY:
    252                 return TelephonyConstants.NETWORK_MODE_WCDMA_ONLY;
    253             case RILConstants.NETWORK_MODE_GSM_UMTS:
    254                 return TelephonyConstants.NETWORK_MODE_GSM_UMTS;
    255             case RILConstants.NETWORK_MODE_CDMA:
    256                 return TelephonyConstants.NETWORK_MODE_CDMA;
    257             case RILConstants.NETWORK_MODE_CDMA_NO_EVDO:
    258                 return TelephonyConstants.NETWORK_MODE_CDMA_NO_EVDO;
    259             case RILConstants.NETWORK_MODE_EVDO_NO_CDMA:
    260                 return TelephonyConstants.NETWORK_MODE_EVDO_NO_CDMA;
    261             case RILConstants.NETWORK_MODE_GLOBAL:
    262                 return TelephonyConstants.NETWORK_MODE_GLOBAL;
    263             case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO:
    264                 return TelephonyConstants.NETWORK_MODE_LTE_CDMA_EVDO;
    265             case RILConstants.NETWORK_MODE_LTE_GSM_WCDMA:
    266                 return TelephonyConstants.NETWORK_MODE_LTE_GSM_WCDMA;
    267             case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
    268                 return TelephonyConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA;
    269             case RILConstants.NETWORK_MODE_LTE_ONLY:
    270                 return TelephonyConstants.NETWORK_MODE_LTE_ONLY;
    271             case RILConstants.NETWORK_MODE_LTE_WCDMA:
    272                 return TelephonyConstants.NETWORK_MODE_LTE_WCDMA;
    273             case RILConstants.NETWORK_MODE_TDSCDMA_ONLY:
    274                 return TelephonyConstants.NETWORK_MODE_TDSCDMA_ONLY;
    275             case RILConstants.NETWORK_MODE_TDSCDMA_WCDMA:
    276                 return TelephonyConstants.NETWORK_MODE_TDSCDMA_WCDMA;
    277             case RILConstants.NETWORK_MODE_LTE_TDSCDMA:
    278                 return TelephonyConstants.NETWORK_MODE_LTE_TDSCDMA;
    279             case RILConstants.NETWORK_MODE_TDSCDMA_GSM:
    280                 return TelephonyConstants.NETWORK_MODE_TDSCDMA_GSM;
    281             case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM:
    282                 return TelephonyConstants.NETWORK_MODE_LTE_TDSCDMA_GSM;
    283             case RILConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA:
    284                 return TelephonyConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA;
    285             case RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA:
    286                 return TelephonyConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA;
    287             case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA:
    288                 return TelephonyConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA;
    289             case RILConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
    290                 return TelephonyConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
    291             case RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
    292                 return TelephonyConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
    293         }
    294         Log.d("getNetworkModeStringfromInt error. Int: " + networkMode);
    295         return TelephonyConstants.NETWORK_MODE_INVALID;
    296     }
    297 
    298     public static String getNetworkTypeString(int type) {
    299         switch(type) {
    300             case TelephonyManager.NETWORK_TYPE_GPRS:
    301                 return TelephonyConstants.RAT_GPRS;
    302             case TelephonyManager.NETWORK_TYPE_EDGE:
    303                 return TelephonyConstants.RAT_EDGE;
    304             case TelephonyManager.NETWORK_TYPE_UMTS:
    305                 return TelephonyConstants.RAT_UMTS;
    306             case TelephonyManager.NETWORK_TYPE_HSDPA:
    307                 return TelephonyConstants.RAT_HSDPA;
    308             case TelephonyManager.NETWORK_TYPE_HSUPA:
    309                 return TelephonyConstants.RAT_HSUPA;
    310             case TelephonyManager.NETWORK_TYPE_HSPA:
    311                 return TelephonyConstants.RAT_HSPA;
    312             case TelephonyManager.NETWORK_TYPE_CDMA:
    313                 return TelephonyConstants.RAT_CDMA;
    314             case TelephonyManager.NETWORK_TYPE_1xRTT:
    315                 return TelephonyConstants.RAT_1XRTT;
    316             case TelephonyManager.NETWORK_TYPE_EVDO_0:
    317                 return TelephonyConstants.RAT_EVDO_0;
    318             case TelephonyManager.NETWORK_TYPE_EVDO_A:
    319                 return TelephonyConstants.RAT_EVDO_A;
    320             case TelephonyManager.NETWORK_TYPE_EVDO_B:
    321                 return TelephonyConstants.RAT_EVDO_B;
    322             case TelephonyManager.NETWORK_TYPE_EHRPD:
    323                 return TelephonyConstants.RAT_EHRPD;
    324             case TelephonyManager.NETWORK_TYPE_LTE:
    325                 return TelephonyConstants.RAT_LTE;
    326             case TelephonyManager.NETWORK_TYPE_HSPAP:
    327                 return TelephonyConstants.RAT_HSPAP;
    328             case TelephonyManager.NETWORK_TYPE_GSM:
    329                 return TelephonyConstants.RAT_GSM;
    330             case TelephonyManager. NETWORK_TYPE_TD_SCDMA:
    331                 return TelephonyConstants.RAT_TD_SCDMA;
    332             case TelephonyManager.NETWORK_TYPE_IWLAN:
    333                 return TelephonyConstants.RAT_IWLAN;
    334             case TelephonyManager.NETWORK_TYPE_IDEN:
    335                 return TelephonyConstants.RAT_IDEN;
    336             case TelephonyManager.NETWORK_TYPE_LTE_CA:
    337                 return TelephonyConstants.RAT_LTE_CA;
    338         }
    339         return TelephonyConstants.RAT_UNKNOWN;
    340     }
    341 
    342     public static String getNetworkStateString(int state) {
    343         switch(state) {
    344             case ServiceState.STATE_EMERGENCY_ONLY:
    345                 return TelephonyConstants.SERVICE_STATE_EMERGENCY_ONLY;
    346             case ServiceState.STATE_IN_SERVICE:
    347                 return TelephonyConstants.SERVICE_STATE_IN_SERVICE;
    348             case ServiceState.STATE_OUT_OF_SERVICE:
    349                 return TelephonyConstants.SERVICE_STATE_OUT_OF_SERVICE;
    350             case ServiceState.STATE_POWER_OFF:
    351                 return TelephonyConstants.SERVICE_STATE_POWER_OFF;
    352             default:
    353                 return TelephonyConstants.SERVICE_STATE_UNKNOWN;
    354         }
    355    }
    356 
    357     public static String getSrvccStateString(int srvccState) {
    358         switch (srvccState) {
    359             case VoLteServiceState.HANDOVER_STARTED:
    360                 return TelephonyConstants.VOLTE_SERVICE_STATE_HANDOVER_STARTED;
    361             case VoLteServiceState.HANDOVER_COMPLETED:
    362                 return TelephonyConstants.VOLTE_SERVICE_STATE_HANDOVER_COMPLETED;
    363             case VoLteServiceState.HANDOVER_FAILED:
    364                 return TelephonyConstants.VOLTE_SERVICE_STATE_HANDOVER_FAILED;
    365             case VoLteServiceState.HANDOVER_CANCELED:
    366                 return TelephonyConstants.VOLTE_SERVICE_STATE_HANDOVER_CANCELED;
    367             default:
    368                 Log.e(String.format("getSrvccStateString():"
    369                         + "unknown state %d", srvccState));
    370                 return TelephonyConstants.VOLTE_SERVICE_STATE_HANDOVER_UNKNOWN;
    371         }
    372     };
    373 }
    374