1/*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "emergency_utils.h"
17
18#include "shortnumberinfo.h"
19
20#include "cellular_call_config.h"
21#include "mmi_code_utils.h"
22#include "module_service_utils.h"
23
24namespace OHOS {
25namespace Telephony {
26int32_t EmergencyUtils::IsEmergencyCall(int32_t slotId, const std::string &phoneNum, bool &enabled)
27{
28    if (phoneNum.empty()) {
29        TELEPHONY_LOGI("IsEmergencyCall return, phoneNum is empty.");
30        return TELEPHONY_ERR_ARGUMENT_INVALID;
31    }
32
33    if (phoneNum.find('@') != std::string::npos || phoneNum.find("%40") != std::string::npos) {
34        TELEPHONY_LOGI("IsEmergencyCall return, phoneNum is not emergency.");
35        return TELEPHONY_ERR_ARGUMENT_INVALID;
36    }
37
38    if (phoneNum.front() == '+') {
39        TELEPHONY_LOGI("IsEmergencyCall return, phoneNum is not emergency.");
40        return TELEPHONY_ERR_ARGUMENT_INVALID;
41    }
42
43    return IsEmergencyCallProcessing(slotId, phoneNum, enabled);
44}
45
46int32_t EmergencyUtils::IsEmergencyCallProcessing(int32_t slotId, const std::string &formatString, bool &enabled)
47{
48    enabled = true;
49    TELEPHONY_LOGD("IsEmergencyCallProcessing entry.");
50    CellularCallConfig config;
51    ModuleServiceUtils dependDataObtain;
52    std::string countryIsoCode = dependDataObtain.GetNetworkCountryCode(slotId);
53    std::vector<EmergencyCall> eccCallList = config.GetEccCallList(slotId);
54    std::string mcc = config.GetMcc(slotId);
55    if (eccCallList.empty()) {
56        TELEPHONY_LOGI("eccCallList is nullptr.");
57        std::vector<std::string> eccList = { "110", "120", "119", "122", "112", "000", "911", "08", "118", "999" };
58        if (std::any_of(eccList.begin(), eccList.end(), [&formatString](std::string eccNum) {
59            return eccNum == formatString;
60        })) {
61            return TELEPHONY_ERR_SUCCESS;
62        }
63    } else {
64        for (auto it = eccCallList.begin(); it != eccCallList.end(); it++) {
65            if (mcc == it->mcc && formatString == it->eccNum) {
66                TELEPHONY_LOGI("IsEmergencyCallProcessing, Complies with sim data.");
67                return TELEPHONY_ERR_SUCCESS;
68            }
69        }
70    }
71    if (config.NeedReadThirdParyLib() && !countryIsoCode.empty()) {
72        TELEPHONY_LOGD("IsEmergencyCallProcessing countryIsoCode is not empty");
73        i18n::phonenumbers::ShortNumberInfo shortNumberInfo;
74        transform(countryIsoCode.begin(), countryIsoCode.end(), countryIsoCode.begin(), ::toupper);
75        enabled = shortNumberInfo.IsEmergencyNumber(formatString, countryIsoCode);
76        return TELEPHONY_ERR_SUCCESS;
77    }
78    TELEPHONY_LOGI("IsEmergencyCallProcessing, not an emergency number.");
79    enabled = false;
80    return TELEPHONY_ERR_SUCCESS;
81}
82} // namespace Telephony
83} // namespace OHOS
84