• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "phone_number_format.h"
16 
17 #include <dlfcn.h>
18 
19 #include "locale_config.h"
20 #include "locid.h"
21 #include "map"
22 #include "new"
23 #include "set"
24 #include "string"
25 #include "utility"
26 
27 namespace OHOS {
28 namespace Global {
29 namespace I18n {
30 const int RECV_CHAR_LEN = 100;
31 using i18n::phonenumbers::PhoneNumberUtil;
32 
PhoneNumberFormat(const std::string & countryTag,const std::map<std::string,std::string> & options)33 PhoneNumberFormat::PhoneNumberFormat(const std::string &countryTag,
34                                      const std::map<std::string, std::string> &options)
35 {
36     util = PhoneNumberUtil::GetInstance();
37     country = countryTag;
38     std::string type = "";
39     auto search = options.find("type");
40     if (search != options.end()) {
41         type = search->second;
42     }
43 
44     std::map<std::string, PhoneNumberUtil::PhoneNumberFormat> type2PhoneNumberFormat = {
45         {"E164", PhoneNumberUtil::PhoneNumberFormat::E164},
46         {"RFC3966", PhoneNumberUtil::PhoneNumberFormat::RFC3966},
47         {"INTERNATIONAL", PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL},
48         {"NATIONAL", PhoneNumberUtil::PhoneNumberFormat::NATIONAL}
49     };
50 
51     std::set<std::string> validType = {"E164", "RFC3966", "INTERNATIONAL", "NATIONAL"};
52     if (validType.find(type) != validType.end()) {
53         phoneNumberFormat = type2PhoneNumberFormat[type];
54     } else {
55         phoneNumberFormat = PhoneNumberUtil::PhoneNumberFormat::NATIONAL;
56     }
57 }
58 
~PhoneNumberFormat()59 PhoneNumberFormat::~PhoneNumberFormat()
60 {
61     if (g_dynamicHandler != NULL) {
62         dlclose(g_dynamicHandler);
63         g_dynamicHandler = nullptr;
64         g_func = nullptr;
65     }
66 }
67 
CreateInstance(const std::string & countryTag,const std::map<std::string,std::string> & options)68 std::unique_ptr<PhoneNumberFormat> PhoneNumberFormat::CreateInstance(const std::string &countryTag,
69     const std::map<std::string, std::string> &options)
70 {
71     std::unique_ptr<PhoneNumberFormat> phoneNumberFormat = std::make_unique<PhoneNumberFormat>(countryTag, options);
72     if (phoneNumberFormat->GetPhoneNumberUtil() == nullptr) {
73         return nullptr;
74     }
75     return phoneNumberFormat;
76 }
77 
GetPhoneNumberUtil()78 PhoneNumberUtil* PhoneNumberFormat::GetPhoneNumberUtil()
79 {
80     return util;
81 }
82 
isValidPhoneNumber(const std::string & number) const83 bool PhoneNumberFormat::isValidPhoneNumber(const std::string &number) const
84 {
85     i18n::phonenumbers::PhoneNumber phoneNumber;
86     PhoneNumberUtil::ErrorType type = util->Parse(number, country, &phoneNumber);
87     if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
88         return false;
89     }
90     return util->IsValidNumber(phoneNumber);
91 }
92 
format(const std::string & number) const93 std::string PhoneNumberFormat::format(const std::string &number) const
94 {
95     i18n::phonenumbers::PhoneNumber phoneNumber;
96     PhoneNumberUtil::ErrorType type = util->Parse(number, country, &phoneNumber);
97     if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
98         return "";
99     }
100     std::string formatted_number;
101     util->Format(phoneNumber, phoneNumberFormat, &formatted_number);
102     return formatted_number;
103 }
104 
getLocationName(const std::string & number,const std::string & locale)105 std::string PhoneNumberFormat::getLocationName(const std::string &number, const std::string &locale)
106 {
107     const char* error = NULL;
108     if (g_dynamicHandler == NULL) {
109 #ifndef SUPPORT_ASAN
110         const char* geocodingSO = "libgeocoding.z.so";
111 #else
112         const char* geocodingSO = "system/asan/lib64/libgeocoding.z.so";
113 #endif
114         g_dynamicHandler = dlopen(geocodingSO, RTLD_NOW);
115         error = dlerror();
116     }
117     if (error == nullptr && !g_func) {
118         g_func = reinterpret_cast<ExposeLocationName>(dlsym(g_dynamicHandler, "exposeLocationName"));
119         error = dlerror();
120     }
121     if (error != NULL) {
122         i18n::phonenumbers::PhoneNumber phoneNumber;
123         PhoneNumberUtil::ErrorType type = util->Parse(number, country, &phoneNumber);
124         if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
125             return "";
126         }
127         std::string regionCode;
128         util->GetRegionCodeForNumber(phoneNumber, &regionCode);
129         return LocaleConfig::GetDisplayRegion(regionCode, locale, false);
130     }
131     const char* numberStr = number.c_str();
132     const char* localeStr = locale.c_str();
133     char recvArr[RECV_CHAR_LEN];
134     g_func(numberStr, localeStr, recvArr);
135     std::string locName = recvArr;
136     return locName;
137 }
138 } // namespace I18n
139 } // namespace Global
140 } // namespace OHOS
141