• 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 <regex>
19 
20 #include "asyoutypeformatter.h"
21 #include "call_ability_report_proxy.h"
22 #include "call_manager_errors.h"
23 #include "call_voice_assistant_manager.h"
24 #include "cellular_call_connection.h"
25 #include "cellular_data_client.h"
26 #include "core_service_client.h"
27 #include "number_identity_data_base_helper.h"
28 #include "parameters.h"
29 #include "phonenumbers/phonenumber.pb.h"
30 #include "telephony_log_wrapper.h"
31 #include "telephony_types.h"
32 
33 namespace OHOS {
34 namespace Telephony {
35 const int32_t MAX_LENGTH_SHORT_CODE = 2;
36 
CallNumberUtils()37 CallNumberUtils::CallNumberUtils() {}
38 
~CallNumberUtils()39 CallNumberUtils::~CallNumberUtils() {}
40 
FormatPhoneNumber(const std::string & phoneNumber,const std::string & countryCode,std::string & formatNumber)41 int32_t CallNumberUtils::FormatPhoneNumber(
42     const std::string &phoneNumber, const std::string &countryCode, std::string &formatNumber)
43 {
44     if (phoneNumber.empty()) {
45         TELEPHONY_LOGE("phoneNumber is nullptr!");
46         return TELEPHONY_ERR_ARGUMENT_INVALID;
47     }
48     if (phoneNumber.front() == '#' || phoneNumber.front() == '*') {
49         formatNumber = phoneNumber;
50         return TELEPHONY_SUCCESS;
51     }
52     i18n::phonenumbers::PhoneNumberUtil *phoneUtils = i18n::phonenumbers::PhoneNumberUtil::GetInstance();
53     if (phoneUtils == nullptr) {
54         TELEPHONY_LOGE("phoneUtils is nullptr");
55         return TELEPHONY_ERR_LOCAL_PTR_NULL;
56     }
57     std::string tmpCode = countryCode;
58     transform(tmpCode.begin(), tmpCode.end(), tmpCode.begin(), ::toupper);
59     i18n::phonenumbers::PhoneNumber parseResult;
60     phoneUtils->ParseAndKeepRawInput(phoneNumber, tmpCode, &parseResult);
61     phoneUtils->FormatInOriginalFormat(parseResult, tmpCode, &formatNumber);
62     if (formatNumber.empty() || formatNumber == "0") {
63         formatNumber = "";
64     }
65     return TELEPHONY_SUCCESS;
66 }
67 
FormatPhoneNumberToE164(const std::string phoneNumber,const std::string countryCode,std::string & formatNumber)68 int32_t CallNumberUtils::FormatPhoneNumberToE164(
69     const std::string phoneNumber, const std::string countryCode, std::string &formatNumber)
70 {
71     return FormatNumberBase(phoneNumber, countryCode, i18n::phonenumbers::PhoneNumberUtil::E164, formatNumber);
72 }
73 
FormatPhoneNumberToNational(const std::string phoneNumber,const std::string countryCode,std::string & formatNumber)74 int32_t CallNumberUtils::FormatPhoneNumberToNational(
75     const std::string phoneNumber, const std::string countryCode, std::string &formatNumber)
76 {
77     int32_t ret = FormatNumberBase(phoneNumber, countryCode,
78         i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::NATIONAL, formatNumber);
79     ProcessSpace(formatNumber);
80     return ret;
81 }
82 
FormatPhoneNumberToInternational(const std::string phoneNumber,const std::string countryCode,std::string & formatNumber)83 int32_t CallNumberUtils::FormatPhoneNumberToInternational(
84     const std::string phoneNumber, const std::string countryCode, std::string &formatNumber)
85 {
86     int32_t ret = FormatNumberBase(phoneNumber, countryCode,
87         i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL, formatNumber);
88     ProcessSpace(formatNumber);
89     return ret;
90 }
91 
FormatNumberBase(const std::string phoneNumber,std::string countryCode,const i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat formatInfo,std::string & formatNumber)92 int32_t CallNumberUtils::FormatNumberBase(const std::string phoneNumber, std::string countryCode,
93     const i18n::phonenumbers::PhoneNumberUtil::PhoneNumberFormat formatInfo, std::string &formatNumber)
94 {
95     if (phoneNumber.empty()) {
96         TELEPHONY_LOGE("phoneNumber is nullptr!");
97         return TELEPHONY_ERR_ARGUMENT_INVALID;
98     }
99     i18n::phonenumbers::PhoneNumberUtil *phoneUtils = i18n::phonenumbers::PhoneNumberUtil::GetInstance();
100     if (phoneUtils == nullptr) {
101         TELEPHONY_LOGE("phoneUtils is nullptr");
102         return TELEPHONY_ERR_LOCAL_PTR_NULL;
103     }
104     transform(countryCode.begin(), countryCode.end(), countryCode.begin(), ::toupper);
105     i18n::phonenumbers::PhoneNumber parseResult;
106     phoneUtils->Parse(phoneNumber, countryCode, &parseResult);
107     if (phoneUtils->IsValidNumber(parseResult) || HasBCPhoneNumber(phoneNumber)) {
108         phoneUtils->Format(parseResult, formatInfo, &formatNumber);
109     }
110     return TELEPHONY_SUCCESS;
111 }
112 
FormatPhoneNumberAsYouType(const std::string & phoneNumber,const std::string & countryCode,std::string & formatNumber)113 int32_t CallNumberUtils::FormatPhoneNumberAsYouType(
114     const std::string &phoneNumber, const std::string &countryCode, std::string &formatNumber)
115 {
116     if (phoneNumber.empty()) {
117         TELEPHONY_LOGE("phoneNumber is nullptr!");
118         return TELEPHONY_ERR_ARGUMENT_INVALID;
119     }
120     if (phoneNumber.front() == '#' || phoneNumber.front() == '*') {
121         formatNumber = phoneNumber;
122         return TELEPHONY_SUCCESS;
123     }
124     i18n::phonenumbers::PhoneNumberUtil *phoneUtils = i18n::phonenumbers::PhoneNumberUtil::GetInstance();
125     if (phoneUtils == nullptr) {
126         TELEPHONY_LOGE("phoneUtils is nullptr");
127         return TELEPHONY_ERR_LOCAL_PTR_NULL;
128     }
129     std::string tmpCode = countryCode;
130     transform(tmpCode.begin(), tmpCode.end(), tmpCode.begin(), ::toupper);
131     std::unique_ptr<i18n::phonenumbers::AsYouTypeFormatter> formatter(phoneUtils->GetAsYouTypeFormatter(tmpCode));
132     if (formatter == nullptr) {
133         TELEPHONY_LOGE("formatter is nullptr");
134         return TELEPHONY_ERR_LOCAL_PTR_NULL;
135     }
136     formatter->Clear();
137     std::string result;
138     for (size_t i = 0; i < phoneNumber.length(); i++) {
139         char c = phoneNumber.at(i);
140         formatNumber = formatter->InputDigit(c, &result);
141     }
142     if (formatNumber.empty() || formatNumber == "0") {
143         formatNumber = "";
144     }
145     return TELEPHONY_SUCCESS;
146 }
147 
ProcessSpace(std::string & number)148 void CallNumberUtils::ProcessSpace(std::string &number)
149 {
150     std::string word;
151     std::stringstream streamNum(number);
152     std::string store;
153     while (streamNum >> word) {
154         store += word;
155     }
156     number = store;
157 }
158 
CheckNumberIsEmergency(const std::string & phoneNumber,const int32_t slotId,bool & enabled)159 int32_t CallNumberUtils::CheckNumberIsEmergency(const std::string &phoneNumber, const int32_t slotId, bool &enabled)
160 {
161     return DelayedSingleton<CellularCallConnection>::GetInstance()->IsEmergencyPhoneNumber(
162         phoneNumber, slotId, enabled);
163 }
164 
IsCarrierVtConfig(const int32_t slotId,bool & enabled)165 int32_t CallNumberUtils::IsCarrierVtConfig(const int32_t slotId, bool &enabled)
166 {
167     return DelayedSingleton<CellularCallConnection>::GetInstance()->GetCarrierVtConfig(
168         slotId, enabled);
169 }
170 
IsValidSlotId(int32_t slotId) const171 bool CallNumberUtils::IsValidSlotId(int32_t slotId) const
172 {
173     if (SIM_SLOT_COUNT == HAS_A_SLOT) {
174         return slotId == SIM_SLOT_0;
175     }
176     if (SIM_SLOT_COUNT == HAS_TWO_SLOT) {
177         if (slotId == SIM_SLOT_0 || slotId == SIM_SLOT_1) {
178             return true;
179         }
180     }
181     return false;
182 }
183 
IsMMICode(std::string & number)184 bool CallNumberUtils::IsMMICode(std::string &number)
185 {
186     if (number.empty()) {
187         TELEPHONY_LOGE("number is empty.");
188         return false;
189     }
190     if (IsShortCode(number)) {
191         TELEPHONY_LOGI("number is shortCode.");
192         return true;
193     }
194     if (RegexMatchMmi(number)) {
195         if (!mmiData_.serviceCode.empty() && !mmiData_.dialString.empty() &&
196             (mmiData_.actionString == "*" || mmiData_.actionString == "#")) {
197             number = mmiData_.dialString;
198             TELEPHONY_LOGI("change number for dial");
199             return false;
200         }
201         return true;
202     }
203 
204     if ((number.front() == '*' || number.front() == '#') && number.back() == '#') {
205         TELEPHONY_LOGI("number start with * or # and end with #");
206         return true;
207     }
208 
209     return false;
210 }
211 
RegexMatchMmi(const std::string & number)212 bool CallNumberUtils::RegexMatchMmi(const std::string &number)
213 {
214     std::string symbols =
215         "((\\*|#|\\*#|\\*\\*|##)(\\d{2,3})(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*))?)?)?)?#)(.*)";
216     std::regex pattern(symbols);
217     std::smatch results;
218     if (regex_match(number, results, pattern)) {
219         TELEPHONY_LOGI("regex_match true");
220        /**
221         * The following sequence of functions shall be used for the control of Supplementary Services:
222         *  SELECT:	Entry of the procedure information (may be a digit or a sequence of characters).
223         *  SEND: Transmission of the information to the network.
224         *  INDICATION:	Call progress indications.
225         */
226         int32_t action = 2;
227         // 3GPP TS 22.030 V4.0.0 (2001-03)  6.5.2 Structure of the MMI
228         // This structure consists of the following parts:
229         //     Service Code, SC( (2 or 3 digits)
230         //     Supplementary Information, SI (variable length).
231         int32_t serviceCode = 3;
232         int32_t dialingNumber = 12;
233         mmiData_.actionString = results.str(action);
234         mmiData_.serviceCode = results.str(serviceCode);
235         mmiData_.dialString = results.str(dialingNumber);
236         return true;
237     }
238     return false;
239 }
240 
RemoveSeparatorsPhoneNumber(const std::string & phoneString)241 std::string CallNumberUtils::RemoveSeparatorsPhoneNumber(const std::string &phoneString)
242 {
243     std::string newString;
244     if (phoneString.empty()) {
245         TELEPHONY_LOGE("RemoveSeparatorsPhoneNumber return, phoneStr is empty.");
246         return newString;
247     }
248     for (char c : phoneString) {
249         if ((c >= '0' && c <= '9') || c == '*' || c == '#' || c == '+' || c == 'N' || c == ',' || c == ';') {
250             newString += c;
251         }
252     }
253 
254     return newString;
255 }
256 
RemovePostDialPhoneNumber(const std::string & phoneString)257 std::string CallNumberUtils::RemovePostDialPhoneNumber(const std::string &phoneString)
258 {
259     std::string newString = "";
260     if (phoneString.empty()) {
261         TELEPHONY_LOGE("RemovePostDialPhoneNumber return, phoneStr is empty.");
262         return newString;
263     }
264     for (char c : phoneString) {
265         if ((c >= '0' && c <= '9') || c == '*' || c == '#' || c == '+' || c == 'N') {
266             newString += c;
267         } else if (c == ',' || c == ';') {
268             break;
269         }
270     }
271 
272     return newString;
273 }
274 
HasAlphabetInPhoneNum(const std::string & inputValue)275 bool CallNumberUtils::HasAlphabetInPhoneNum(const std::string &inputValue)
276 {
277     if (inputValue.empty()) {
278         TELEPHONY_LOGE("HasAlphabetInPhoneNum return, input is empty.");
279         return true;
280     }
281     for (char c : inputValue) {
282         if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))) {
283             TELEPHONY_LOGE("The Phone Number contains letter");
284             return true;
285         }
286     }
287     TELEPHONY_LOGI("The Phone Number is valid");
288     return false;
289 }
290 
HasBCPhoneNumber(const std::string & phoneNumber)291 bool CallNumberUtils::HasBCPhoneNumber(const std::string &phoneNumber)
292 {
293     int32_t phoneNumberStart = 0;
294     int32_t phoneNumberStartLength = 3;
295     size_t bCNumberLength = 11;
296     std::string bCNumberStart = "192";
297     if (phoneNumber.length() == bCNumberLength &&
298         phoneNumber.substr(phoneNumberStart, phoneNumberStartLength) == bCNumberStart) {
299         return true;
300     }
301     return false;
302 }
303 
SelectAccountId(int32_t slotId,AppExecFwk::PacMap & extras)304 bool CallNumberUtils::SelectAccountId(int32_t slotId, AppExecFwk::PacMap &extras)
305 {
306     if (IsValidSlotId(slotId)) {
307         return true;
308     }
309     int32_t defaultVoiceSlotId = DelayedRefSingleton<CoreServiceClient>::GetInstance().GetDefaultVoiceSlotId();
310     if (defaultVoiceSlotId != TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL && IsValidSlotId(defaultVoiceSlotId)) {
311         extras.PutIntValue("accountId", defaultVoiceSlotId);
312         TELEPHONY_LOGI("select accountId to defaultVoiceSlotId = %{public}d", defaultVoiceSlotId);
313         return true;
314     }
315 #ifdef CELLULAR_DATA_SUPPORT
316     int32_t defaultDataSlotId = DelayedRefSingleton<CellularDataClient>::GetInstance().GetDefaultCellularDataSlotId();
317     if (defaultDataSlotId != TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL && IsValidSlotId(defaultDataSlotId)) {
318         extras.PutIntValue("accountId", defaultDataSlotId);
319         TELEPHONY_LOGI("select accountId to defaultDataSlotId = %{public}d", defaultDataSlotId);
320         return true;
321     }
322 #endif
323     return false;
324 }
325 
QueryNumberLocationInfo(std::string & numberLocation,std::string accountNumber)326 int32_t CallNumberUtils::QueryNumberLocationInfo(std::string &numberLocation, std::string accountNumber)
327 {
328     TELEPHONY_LOGW("QueryNumberLocationInfo");
329     if (accountNumber == "") {
330         TELEPHONY_LOGE("accountNumber is null");
331         return TELEPHONY_ERR_ARGUMENT_INVALID;
332     }
333     std::shared_ptr<NumberIdentityDataBaseHelper> callDataPtr =
334         DelayedSingleton<NumberIdentityDataBaseHelper>::GetInstance();
335     if (callDataPtr == nullptr) {
336         TELEPHONY_LOGE("callDataPtr is nullptr!");
337         return TELEPHONY_ERR_LOCAL_PTR_NULL;
338     }
339 
340     DataShare::DataSharePredicates predicates;
341     std::vector<std::string> phoneNumber;
342     phoneNumber.push_back(accountNumber);
343     predicates.SetWhereArgs(phoneNumber);
344     bool ret = callDataPtr->Query(numberLocation, predicates);
345     if (!ret) {
346         TELEPHONY_LOGE("Query number location database fail!");
347         return TELEPHONY_ERR_DATABASE_READ_FAIL;
348     }
349     return TELEPHONY_SUCCESS;
350 }
351 
NumberLocationUpdate(const sptr<CallBase> & callObjectPtr)352 void CallNumberUtils::NumberLocationUpdate(const sptr<CallBase> &callObjectPtr)
353 {
354     CallAttributeInfo info;
355     callObjectPtr->GetCallAttributeBaseInfo(info);
356     TELEPHONY_LOGW("NumberLocationUpdate, callId[%{public}d]", info.callId);
357     std::string numberLocation = callObjectPtr->GetNumberLocation();
358     int32_t ret = QueryNumberLocationInfo(numberLocation, callObjectPtr->GetAccountNumber());
359     if (ret != TELEPHONY_SUCCESS) {
360         return;
361     }
362     sptr<CallBase> call = callObjectPtr;
363     if (info.callState == TelCallState::CALL_STATUS_DIALING) {
364         call = CallObjectManager::GetOneCallObject(info.callId);
365         if (call == nullptr) {
366             TELEPHONY_LOGE("call is nullptr");
367             call = callObjectPtr;
368         }
369     }
370     call->SetNumberLocation(numberLocation);
371     CallVoiceAssistantManager::GetInstance()->UpdateNumberLocation(numberLocation, info.callId);
372     if (!CallObjectManager::IsCallExist(info.callId)) {
373         TELEPHONY_LOGE("call is not exist");
374         return;
375     }
376     if (numberLocation != "" && numberLocation != "default") {
377         TELEPHONY_LOGW("need report call info of numberLocation");
378         call->GetCallAttributeInfo(info);
379         DelayedSingleton<CallAbilityReportProxy>::GetInstance()->ReportCallStateInfo(info);
380     }
381 }
382 
YellowPageAndMarkUpdate(const sptr<CallBase> & callObjectPtr)383 void CallNumberUtils::YellowPageAndMarkUpdate(const sptr<CallBase> &callObjectPtr)
384 {
385     if (OHOS::system::GetParameter("const.global.region", "CN") != "CN") {
386         TELEPHONY_LOGI("not the chinese version, no need to query markinfo.");
387         return;
388     }
389     CallAttributeInfo info;
390     callObjectPtr->GetCallAttributeInfo(info);
391     TELEPHONY_LOGI("YellowPageAndMarkUpdate, callId[%{public}d]", info.callId);
392     NumberMarkInfo numberMarkInfo;
393     int32_t ret = QueryYellowPageAndMarkInfo(numberMarkInfo, callObjectPtr->GetAccountNumber());
394     if (ret != TELEPHONY_SUCCESS) {
395         TELEPHONY_LOGE("QueryYellowPageAndMarkInfo fail!");
396         return;
397     }
398     sptr<CallBase> call = callObjectPtr;
399     if (info.callState == TelCallState::CALL_STATUS_DIALING) {
400         call = CallObjectManager::GetOneCallObject(info.callId);
401         if (call == nullptr) {
402             TELEPHONY_LOGE("call is nullptr");
403             return;
404         }
405     }
406     call->SetNumberMarkInfo(numberMarkInfo);
407     if (!CallObjectManager::IsCallExist(info.callId)) {
408         TELEPHONY_LOGE("call is not exist");
409         return;
410     }
411     TELEPHONY_LOGI("markType: %{public}d, isEcc: %{public}d",
412         static_cast<int32_t>(numberMarkInfo.markType), info.isEcc);
413     if (numberMarkInfo.markType > MarkType::MARK_TYPE_NONE || info.isEcc) {
414         call->GetCallAttributeInfo(info);
415         DelayedSingleton<CallAbilityReportProxy>::GetInstance()->ReportCallStateInfo(info);
416     }
417 }
418 
QueryYellowPageAndMarkInfo(NumberMarkInfo & numberMarkInfo,std::string accountNumber)419 int32_t CallNumberUtils::QueryYellowPageAndMarkInfo(NumberMarkInfo &numberMarkInfo, std::string accountNumber)
420 {
421     TELEPHONY_LOGW("QueryYellowPageAndMarkInfo");
422     if (accountNumber == "") {
423         TELEPHONY_LOGE("accountNumber is null");
424         return TELEPHONY_ERR_ARGUMENT_INVALID;
425     }
426     std::shared_ptr<NumberIdentityDataBaseHelper> callDataPtr =
427         DelayedSingleton<NumberIdentityDataBaseHelper>::GetInstance();
428     if (callDataPtr == nullptr) {
429         TELEPHONY_LOGE("callDataPtr is nullptr!");
430         return TELEPHONY_ERR_LOCAL_PTR_NULL;
431     }
432 
433     DataShare::DataSharePredicates predicates;
434     std::vector<std::string> phoneNumber;
435     phoneNumber.push_back(accountNumber);
436     predicates.SetWhereArgs(phoneNumber);
437     bool ret = callDataPtr->QueryYellowPageAndMark(numberMarkInfo, predicates);
438     if (!ret) {
439         TELEPHONY_LOGE("Query yellow page and mark fail!");
440         return TELEPHONY_ERR_DATABASE_READ_FAIL;
441     }
442     return TELEPHONY_SUCCESS;
443 }
444 
IsShortCode(const std::string & number)445 bool CallNumberUtils::IsShortCode(const std::string &number)
446 {
447     if (CallObjectManager::HasCellularCallExist()) {
448         return IsShortCodeWithCellularCall(number);
449     }
450     return IsShortCodeWithoutCellularCall(number);
451 }
452 
IsShortCodeWithoutCellularCall(const std::string & number)453 bool CallNumberUtils::IsShortCodeWithoutCellularCall(const std::string &number)
454 {
455     if (number.length() > MAX_LENGTH_SHORT_CODE) {
456         return false;
457     }
458     if (number[0] == '1' && std::isdigit(number[1])) {
459         return false;
460     }
461     return true;
462 }
463 
IsShortCodeWithCellularCall(const std::string & number)464 bool CallNumberUtils::IsShortCodeWithCellularCall(const std::string &number)
465 {
466     if (number.length() < 1 || number.length() > MAX_LENGTH_SHORT_CODE) {
467         return false;
468     }
469     return true;
470 }
471 } // namespace Telephony
472 } // namespace OHOS
473