• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "call_number_utils.h"
17 
18 #include "phonenumbers/phonenumber.pb.h"
19 
20 #include "telephony_log_wrapper.h"
21 #include "telephony_types.h"
22 
23 #include "call_manager_errors.h"
24 #include "cellular_call_connection.h"
25 
26 namespace OHOS {
27 namespace Telephony {
CallNumberUtils()28 CallNumberUtils::CallNumberUtils() : phoneUtils_(i18n::phonenumbers::PhoneNumberUtil::GetInstance()) {}
29 
~CallNumberUtils()30 CallNumberUtils::~CallNumberUtils() {}
31 
FormatPhoneNumber(const std::string & phoneNumber,const std::string & countryCode,std::string & formatNumber)32 int32_t CallNumberUtils::FormatPhoneNumber(
33     const std::string &phoneNumber, const std::string &countryCode, std::string &formatNumber)
34 {
35     if (phoneNumber.empty()) {
36         TELEPHONY_LOGE("phoneNumber is nullptr!");
37         return CALL_ERR_PHONE_NUMBER_EMPTY;
38     }
39     if (phoneNumber.front() == '#' || phoneNumber.front() == '*') {
40         formatNumber = phoneNumber;
41         return TELEPHONY_SUCCESS;
42     }
43     std::string tmpCode = countryCode;
44     transform(tmpCode.begin(), tmpCode.end(), tmpCode.begin(), ::toupper);
45     i18n::phonenumbers::PhoneNumber parseResult;
46     if (phoneUtils_ == nullptr) {
47         TELEPHONY_LOGE("phoneUtils_ is nullptr");
48         return TELEPHONY_ERR_LOCAL_PTR_NULL;
49     }
50     phoneUtils_->ParseAndKeepRawInput(phoneNumber, tmpCode, &parseResult);
51     phoneUtils_->FormatInOriginalFormat(parseResult, tmpCode, &formatNumber);
52     if (formatNumber.empty() || formatNumber == "0" || phoneNumber == formatNumber) {
53         TELEPHONY_LOGE("FormatPhoneNumber failed!");
54         return CALL_ERR_FORMAT_PHONE_NUMBER_FAILED;
55     }
56     return TELEPHONY_SUCCESS;
57 }
58 
FormatPhoneNumberToE164(const std::string phoneNumber,const std::string countryCode,std::string & formatNumber)59 int32_t CallNumberUtils::FormatPhoneNumberToE164(
60     const std::string phoneNumber, const std::string countryCode, std::string &formatNumber)
61 {
62     return FormatNumberBase(phoneNumber, countryCode, i18n::phonenumbers::PhoneNumberUtil::E164, formatNumber);
63 }
64 
FormatNumberBase(const std::string phoneNumber,std::string countryCode,const i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat formatInfo,std::string & formatNumber)65 int32_t CallNumberUtils::FormatNumberBase(const std::string phoneNumber, std::string countryCode,
66     const i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat formatInfo, std::string &formatNumber)
67 {
68     if (phoneNumber.empty()) {
69         TELEPHONY_LOGE("phoneNumber is nullptr!");
70         return CALL_ERR_PHONE_NUMBER_EMPTY;
71     }
72     transform(countryCode.begin(), countryCode.end(), countryCode.begin(), ::toupper);
73     i18n::phonenumbers::PhoneNumber parseResult;
74     if (phoneUtils_ == nullptr) {
75         TELEPHONY_LOGE("phoneUtils_ is nullptr");
76         return TELEPHONY_ERR_LOCAL_PTR_NULL;
77     }
78     phoneUtils_->Parse(phoneNumber, countryCode, &parseResult);
79     if (phoneUtils_->IsValidNumber(parseResult)) {
80         phoneUtils_->Format(parseResult, formatInfo, &formatNumber);
81     }
82     if (formatNumber.empty() || formatNumber == "0" || phoneNumber == formatNumber) {
83         TELEPHONY_LOGE("FormatPhoneNumber failed!");
84         return CALL_ERR_FORMAT_PHONE_NUMBER_FAILED;
85     }
86     return TELEPHONY_SUCCESS;
87 }
88 
CheckNumberIsEmergency(const std::string & phoneNumber,const int32_t slotId,int32_t & errorCode)89 bool CallNumberUtils::CheckNumberIsEmergency(
90     const std::string &phoneNumber, const int32_t slotId, int32_t &errorCode)
91 {
92     int isEcc = DelayedSingleton<CellularCallConnection>::GetInstance()->IsEmergencyPhoneNumber(
93         phoneNumber, slotId, errorCode);
94     return (isEcc != 0);
95 }
96 
IsValidSlotId(int32_t slotId) const97 bool CallNumberUtils::IsValidSlotId(int32_t slotId) const
98 {
99     if (SIM_SLOT_COUNT == HAS_A_SLOT) {
100         return slotId == SIM_SLOT_0;
101     }
102     if (SIM_SLOT_COUNT == HAS_TWO_SLOT) {
103         if (slotId == SIM_SLOT_0 || slotId == SIM_SLOT_1) {
104             return true;
105         }
106     }
107     return false;
108 }
109 } // namespace Telephony
110 } // namespace OHOS
111