• 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 #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 
33 class PhoneNumberFormat {
34 public:
35     PhoneNumberFormat(const std::string &countryTag, const std::map<std::string, std::string> &options);
36     virtual ~PhoneNumberFormat();
37     bool isValidPhoneNumber(const std::string &number) const;
38     std::string format(const std::string &number);
39     static std::unique_ptr<PhoneNumberFormat> CreateInstance(const std::string &countryTag,
40                                                              const std::map<std::string, std::string> &options);
41     std::string getLocationName(const std::string &number, const std::string &locale);
42     std::string getPhoneLocationName(const std::string& number, const std::string& phoneLocale,
43         const std::string& displayLocale);
44     virtual bool getBlockedRegionName(const std::string& regionCode, const std::string& language);
45     virtual std::string getCityName(const std::string& language, const std::string& phonenumber,
46         const std::string& locationName);
47     static void CloseDynamicHandler();
48 
49 private:
50     PhoneNumberUtil* GetPhoneNumberUtil();
51     void OpenHandler();
52     std::string GetAsYouTypeFormatResult(const std::string &number);
53     std::string FormatAllInputNumber(const std::string &originalNumber, std::string &replacedNumber);
54     PhoneNumberUtil *util;
55     std::unique_ptr<AsYouTypeFormatter> formatter = nullptr;
56     std::string country;
57     PhoneNumberUtil::PhoneNumberFormat phoneNumberFormat;
58     static void* dynamicHandler;
59     static std::mutex phoneMutex;
60     static std::mutex AS_YOU_TYPE_FORMAT_MUTEX;
61     static size_t MAX_NUMBER_LENGTH;
62     std::string lastFormatNumber;
63     bool withOptions = false;
64     static std::map<char, char> VALID_PHONE_NUMBER_CHARS;
65 };
66 } // namespace I18n
67 } // namespace Global
68 } // namespace OHOS
69 #endif
70