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 #ifndef OHOS_GLOBAL_I18N_PHONE_NUMBER_FORMAT_H
16 #define OHOS_GLOBAL_I18N_PHONE_NUMBER_FORMAT_H
17 
18 #include <map>
19 #include <set>
20 #include <string>
21 #include <mutex>
22 #include "memory"
23 #include "phonenumbers/phonenumberutil.h"
24 #include "phonenumbers/asyoutypeformatter.h"
25 
26 namespace OHOS {
27 namespace Global {
28 namespace I18n {
29 using i18n::phonenumbers::PhoneNumberUtil;
30 using i18n::phonenumbers::AsYouTypeFormatter;
31 using ExposeLocationName = int (*)(const char*, const char*, char*, const int);
32 using GetBlockedRegionName = bool (*)(const char*, const char*);
33 using GetBlockedPhoneLocationName = int (*)(const char*, const char*, char*);
34 using GetReplacedPhoneLocationName = void (*)(const char*, const char*, char*);
35 
36 class PhoneNumberFormat {
37 public:
38     PhoneNumberFormat(const std::string &countryTag, const std::map<std::string, std::string> &options);
39     virtual ~PhoneNumberFormat();
40     bool isValidPhoneNumber(const std::string &number) const;
41     std::string format(const std::string &number);
42     static std::unique_ptr<PhoneNumberFormat> CreateInstance(const std::string &countryTag,
43                                                              const std::map<std::string, std::string> &options);
44     std::string getLocationName(const std::string &number, const std::string &locale);
45     std::string getPhoneLocationName(const std::string& number, const std::string& phoneLocale,
46         const std::string& displayLocale, const std::string& regionCode);
47     virtual bool getBlockedRegionName(const std::string& regionCode, const std::string& language);
48     virtual std::string getCityName(const std::string& language, const std::string& phonenumber,
49         const std::string& locationName);
50     static void CloseDynamicHandler();
51 
52 private:
53     PhoneNumberUtil* GetPhoneNumberUtil();
54     void OpenHandler();
55     std::string GetAsYouTypeFormatResult(const std::string &number);
56     std::string FormatAllInputNumber(const std::string &originalNumber, std::string &replacedNumber);
57     PhoneNumberUtil *util;
58     std::unique_ptr<AsYouTypeFormatter> formatter = nullptr;
59     std::string country;
60     PhoneNumberUtil::PhoneNumberFormat phoneNumberFormat;
61     static void* dynamicHandler;
62     static std::mutex phoneMutex;
63     static std::mutex AS_YOU_TYPE_FORMAT_MUTEX;
64     static size_t MAX_NUMBER_LENGTH;
65     std::string lastFormatNumber;
66     bool withOptions = false;
67     static std::map<char, char> VALID_PHONE_NUMBER_CHARS;
68 };
69 } // namespace I18n
70 } // namespace Global
71 } // namespace OHOS
72 #endif
73