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