• 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 using ExposeLocationName = void (*)(const char*, const char*, char*);
33 void* g_dynamicHandler;
34 ExposeLocationName g_func;
35 
PhoneNumberFormat(const std::string & countryTag,const std::map<std::string,std::string> & options)36 PhoneNumberFormat::PhoneNumberFormat(const std::string &countryTag,
37                                      const std::map<std::string, std::string> &options)
38 {
39     util = PhoneNumberUtil::GetInstance();
40     country = countryTag;
41     std::string type = "";
42     auto search = options.find("type");
43     if (search != options.end()) {
44         type = search->second;
45     }
46 
47     std::map<std::string, PhoneNumberUtil::PhoneNumberFormat> type2PhoneNumberFormat = {
48         {"E164", PhoneNumberUtil::PhoneNumberFormat::E164},
49         {"RFC3966", PhoneNumberUtil::PhoneNumberFormat::RFC3966},
50         {"INTERNATIONAL", PhoneNumberUtil::PhoneNumberFormat::INTERNATIONAL},
51         {"NATIONAL", PhoneNumberUtil::PhoneNumberFormat::NATIONAL}
52     };
53 
54     std::set<std::string> validType = {"E164", "RFC3966", "INTERNATIONAL", "NATIONAL"};
55     if (validType.find(type) != validType.end()) {
56         phoneNumberFormat = type2PhoneNumberFormat[type];
57     } else {
58         phoneNumberFormat = PhoneNumberUtil::PhoneNumberFormat::NATIONAL;
59     }
60 }
61 
~PhoneNumberFormat()62 PhoneNumberFormat::~PhoneNumberFormat()
63 {
64     if (g_dynamicHandler != NULL) {
65         dlclose(g_dynamicHandler);
66         g_dynamicHandler = nullptr;
67         g_func = nullptr;
68     }
69 }
70 
CreateInstance(const std::string & countryTag,const std::map<std::string,std::string> & options)71 std::unique_ptr<PhoneNumberFormat> PhoneNumberFormat::CreateInstance(const std::string &countryTag,
72     const std::map<std::string, std::string> &options)
73 {
74     std::unique_ptr<PhoneNumberFormat> phoneNumberFormat = std::make_unique<PhoneNumberFormat>(countryTag, options);
75     if (phoneNumberFormat->GetPhoneNumberUtil() == nullptr) {
76         return nullptr;
77     }
78     return phoneNumberFormat;
79 }
80 
GetPhoneNumberUtil()81 PhoneNumberUtil* PhoneNumberFormat::GetPhoneNumberUtil()
82 {
83     return util;
84 }
85 
isValidPhoneNumber(const std::string & number) const86 bool PhoneNumberFormat::isValidPhoneNumber(const std::string &number) const
87 {
88     i18n::phonenumbers::PhoneNumber phoneNumber;
89     PhoneNumberUtil::ErrorType type = util->Parse(number, country, &phoneNumber);
90     if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
91         return false;
92     }
93     return util->IsValidNumber(phoneNumber);
94 }
95 
format(const std::string & number) const96 std::string PhoneNumberFormat::format(const std::string &number) const
97 {
98     i18n::phonenumbers::PhoneNumber phoneNumber;
99     PhoneNumberUtil::ErrorType type = util->Parse(number, country, &phoneNumber);
100     if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
101         return "";
102     }
103     std::string formatted_number;
104     util->Format(phoneNumber, phoneNumberFormat, &formatted_number);
105     return formatted_number;
106 }
107 
getLocationName(const std::string & number,const std::string & locale) const108 std::string PhoneNumberFormat::getLocationName(const std::string &number, const std::string &locale) const
109 {
110     const char* error = NULL;
111     if (g_dynamicHandler == NULL) {
112         const char* geocodingSO = "libgeocoding.z.so";
113         g_dynamicHandler = dlopen(geocodingSO, RTLD_NOW);
114         dlerror();
115     }
116     if (!g_func) {
117         g_func = (ExposeLocationName)dlsym(g_dynamicHandler, "exposeLocationName");
118     }
119     error = dlerror();
120     if (error != NULL) {
121         i18n::phonenumbers::PhoneNumber phoneNumber;
122         PhoneNumberUtil::ErrorType type = util->Parse(number, country, &phoneNumber);
123         if (type != PhoneNumberUtil::ErrorType::NO_PARSING_ERROR) {
124             return "";
125         }
126         std::string regionCode;
127         util->GetRegionCodeForNumber(phoneNumber, &regionCode);
128         return LocaleConfig::GetDisplayRegion(regionCode, locale, false);
129     }
130     const char* numberStr = number.c_str();
131     const char* localeStr = locale.c_str();
132     char recvArr[RECV_CHAR_LEN];
133     g_func(numberStr, localeStr, recvArr);
134     std::string locName = recvArr;
135     return locName;
136 }
137 } // namespace I18n
138 } // namespace Global
139 } // namespace OHOS
140