11767c5feSopenharmony_ci// Copyright (C) 2012 The Libphonenumber Authors 21767c5feSopenharmony_ci// 31767c5feSopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 41767c5feSopenharmony_ci// you may not use this file except in compliance with the License. 51767c5feSopenharmony_ci// You may obtain a copy of the License at 61767c5feSopenharmony_ci// 71767c5feSopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 81767c5feSopenharmony_ci// 91767c5feSopenharmony_ci// Unless required by applicable law or agreed to in writing, software 101767c5feSopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 111767c5feSopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 121767c5feSopenharmony_ci// See the License for the specific language governing permissions and 131767c5feSopenharmony_ci// limitations under the License. 141767c5feSopenharmony_ci 151767c5feSopenharmony_ci// Library for obtaining information about international short phone numbers, 161767c5feSopenharmony_ci// such as short codes and emergency numbers. Note most commercial short 171767c5feSopenharmony_ci// numbers are not handled here, but by the phonenumberutil. 181767c5feSopenharmony_ci 191767c5feSopenharmony_ci#ifndef I18N_PHONENUMBERS_SHORTNUMBERINFO_H_ 201767c5feSopenharmony_ci#define I18N_PHONENUMBERS_SHORTNUMBERINFO_H_ 211767c5feSopenharmony_ci 221767c5feSopenharmony_ci#include <list> 231767c5feSopenharmony_ci#include <map> 241767c5feSopenharmony_ci#include <set> 251767c5feSopenharmony_ci#include <string> 261767c5feSopenharmony_ci 271767c5feSopenharmony_ci#include "phonenumbers/base/basictypes.h" 281767c5feSopenharmony_ci#include "phonenumbers/base/memory/scoped_ptr.h" 291767c5feSopenharmony_ci 301767c5feSopenharmony_cinamespace i18n { 311767c5feSopenharmony_cinamespace phonenumbers { 321767c5feSopenharmony_ci 331767c5feSopenharmony_ciusing std::list; 341767c5feSopenharmony_ciusing std::map; 351767c5feSopenharmony_ciusing std::set; 361767c5feSopenharmony_ciusing std::string; 371767c5feSopenharmony_ci 381767c5feSopenharmony_ciclass MatcherApi; 391767c5feSopenharmony_ciclass PhoneMetadata; 401767c5feSopenharmony_ciclass PhoneNumber; 411767c5feSopenharmony_ciclass PhoneNumberUtil; 421767c5feSopenharmony_ci 431767c5feSopenharmony_ciclass ShortNumberInfo { 441767c5feSopenharmony_ci public: 451767c5feSopenharmony_ci ShortNumberInfo(); 461767c5feSopenharmony_ci 471767c5feSopenharmony_ci // This type is neither copyable nor movable. 481767c5feSopenharmony_ci ShortNumberInfo(const ShortNumberInfo&) = delete; 491767c5feSopenharmony_ci ShortNumberInfo& operator=(const ShortNumberInfo&) = delete; 501767c5feSopenharmony_ci 511767c5feSopenharmony_ci ~ShortNumberInfo(); 521767c5feSopenharmony_ci 531767c5feSopenharmony_ci // Cost categories of short numbers. 541767c5feSopenharmony_ci enum ShortNumberCost { 551767c5feSopenharmony_ci TOLL_FREE, 561767c5feSopenharmony_ci STANDARD_RATE, 571767c5feSopenharmony_ci PREMIUM_RATE, 581767c5feSopenharmony_ci UNKNOWN_COST 591767c5feSopenharmony_ci }; 601767c5feSopenharmony_ci 611767c5feSopenharmony_ci // Check whether a short number is a possible number when dialled from a 621767c5feSopenharmony_ci // region, given the number in the form of a string, and the region where the 631767c5feSopenharmony_ci // number is dialed from. This provides a more lenient check than 641767c5feSopenharmony_ci // IsValidShortNumberForRegion. 651767c5feSopenharmony_ci bool IsPossibleShortNumberForRegion( 661767c5feSopenharmony_ci const PhoneNumber& short_number, 671767c5feSopenharmony_ci const string& region_dialing_from) const; 681767c5feSopenharmony_ci 691767c5feSopenharmony_ci // Check whether a short number is a possible number. If a country calling 701767c5feSopenharmony_ci // code is shared by multiple regions, this returns true if it's possible in 711767c5feSopenharmony_ci // any of them. This provides a more lenient check than IsValidShortNumber. 721767c5feSopenharmony_ci // See IsPossibleShortNumberForRegion for details. 731767c5feSopenharmony_ci bool IsPossibleShortNumber(const PhoneNumber& number) const; 741767c5feSopenharmony_ci 751767c5feSopenharmony_ci // Tests whether a short number matches a valid pattern in a region. Note 761767c5feSopenharmony_ci // that this doesn't verify the number is actually in use, which is 771767c5feSopenharmony_ci // impossible to tell by just looking at the number itself. 781767c5feSopenharmony_ci bool IsValidShortNumberForRegion( 791767c5feSopenharmony_ci const PhoneNumber& short_number, 801767c5feSopenharmony_ci const string& region_dialing_from) const; 811767c5feSopenharmony_ci 821767c5feSopenharmony_ci // Tests whether a short number matches a valid pattern. If a country calling 831767c5feSopenharmony_ci // code is shared by multiple regions, this returns true if it's valid in any 841767c5feSopenharmony_ci // of them. Note that this doesn't verify the number is actually in use, 851767c5feSopenharmony_ci // which is impossible to tell by just looking at the number itself. See 861767c5feSopenharmony_ci // IsValidShortNumberForRegion for details. 871767c5feSopenharmony_ci bool IsValidShortNumber(const PhoneNumber& number) const; 881767c5feSopenharmony_ci 891767c5feSopenharmony_ci // Gets the expected cost category of a short number when dialled from a 901767c5feSopenharmony_ci // region (however, nothing is implied about its validity). If it is 911767c5feSopenharmony_ci // important that the number is valid, then its validity must first be 921767c5feSopenharmony_ci // checked using IsValidShortNumberForRegion. Note that emergency numbers are 931767c5feSopenharmony_ci // always considered toll-free. Example usage: 941767c5feSopenharmony_ci // 951767c5feSopenharmony_ci // PhoneNumber number; 961767c5feSopenharmony_ci // phone_util.Parse("110", "US", &number); 971767c5feSopenharmony_ci // ... 981767c5feSopenharmony_ci // string region_code("CA"); 991767c5feSopenharmony_ci // ShortNumberInfo short_info; 1001767c5feSopenharmony_ci // if (short_info.IsValidShortNumberForRegion(number, region_code)) { 1011767c5feSopenharmony_ci // ShortNumberInfo::ShortNumberCost cost = 1021767c5feSopenharmony_ci // short_info.GetExpectedCostForRegion(number, region_code); 1031767c5feSopenharmony_ci // // Do something with the cost information here. 1041767c5feSopenharmony_ci // } 1051767c5feSopenharmony_ci ShortNumberCost GetExpectedCostForRegion( 1061767c5feSopenharmony_ci const PhoneNumber& short_number, 1071767c5feSopenharmony_ci const string& region_dialing_from) const; 1081767c5feSopenharmony_ci 1091767c5feSopenharmony_ci // Gets the expected cost category of a short number (however, nothing is 1101767c5feSopenharmony_ci // implied about its validity). If the country calling code is unique to a 1111767c5feSopenharmony_ci // region, this method behaves exactly the same as GetExpectedCostForRegion. 1121767c5feSopenharmony_ci // However, if the country calling code is shared by multiple regions, then 1131767c5feSopenharmony_ci // it returns the highest cost in the sequence PREMIUM_RATE, UNKNOWN_COST, 1141767c5feSopenharmony_ci // STANDARD_RATE, TOLL_FREE. The reason for the position of UNKNOWN_COST in 1151767c5feSopenharmony_ci // this order is that if a number is UNKNOWN_COST in one region but 1161767c5feSopenharmony_ci // STANDARD_RATE or TOLL_FREE in another, its expected cost cannot be 1171767c5feSopenharmony_ci // estimated as one of the latter since it might be a PREMIUM_RATE number. 1181767c5feSopenharmony_ci // 1191767c5feSopenharmony_ci // For example, if a number is STANDARD_RATE in the US, but TOLL_FREE in 1201767c5feSopenharmony_ci // Canada, the expected cost returned by this method will be STANDARD_RATE, 1211767c5feSopenharmony_ci // since the NANPA countries share the same country calling code. 1221767c5feSopenharmony_ci // 1231767c5feSopenharmony_ci // Note: If the region from which the number is dialed is known, it is highly 1241767c5feSopenharmony_ci // preferable to call GetExpectedCostForRegion instead. 1251767c5feSopenharmony_ci ShortNumberCost GetExpectedCost(const PhoneNumber& number) const; 1261767c5feSopenharmony_ci 1271767c5feSopenharmony_ci // Gets a valid short number for the specified region. 1281767c5feSopenharmony_ci string GetExampleShortNumber(const string& region_code) const; 1291767c5feSopenharmony_ci 1301767c5feSopenharmony_ci // Gets a valid short number for the specified cost category. 1311767c5feSopenharmony_ci string GetExampleShortNumberForCost(const string& region_code, 1321767c5feSopenharmony_ci ShortNumberCost cost) const; 1331767c5feSopenharmony_ci 1341767c5feSopenharmony_ci // Returns true if the number might be used to connect to an emergency service 1351767c5feSopenharmony_ci // in the given region. 1361767c5feSopenharmony_ci // 1371767c5feSopenharmony_ci // This method takes into account cases where the number might contain 1381767c5feSopenharmony_ci // formatting, or might have additional digits appended (when it is okay to do 1391767c5feSopenharmony_ci // that in the region specified). 1401767c5feSopenharmony_ci bool ConnectsToEmergencyNumber(const string& number, 1411767c5feSopenharmony_ci const string& region_code) const; 1421767c5feSopenharmony_ci 1431767c5feSopenharmony_ci // Returns true if the number exactly matches an emergency service number in 1441767c5feSopenharmony_ci // the given region. 1451767c5feSopenharmony_ci // 1461767c5feSopenharmony_ci // This method takes into account cases where the number might contain 1471767c5feSopenharmony_ci // formatting, but doesn't allow additional digits to be appended. 1481767c5feSopenharmony_ci bool IsEmergencyNumber(const string& number, 1491767c5feSopenharmony_ci const string& region_code) const; 1501767c5feSopenharmony_ci 1511767c5feSopenharmony_ci // Given a valid short number, determines whether it is carrier-specific 1521767c5feSopenharmony_ci // (however, nothing is implied about its validity). Carrier-specific numbers 1531767c5feSopenharmony_ci // may connect to a different end-point, or not connect at all, depending on 1541767c5feSopenharmony_ci // the user's carrier. If it is important that the number is valid, then its 1551767c5feSopenharmony_ci // validity must first be checked using IsValidShortNumber or 1561767c5feSopenharmony_ci // IsValidShortNumberForRegion. 1571767c5feSopenharmony_ci bool IsCarrierSpecific(const PhoneNumber& number) const; 1581767c5feSopenharmony_ci 1591767c5feSopenharmony_ci // Given a valid short number, determines whether it is carrier-specific when 1601767c5feSopenharmony_ci // dialed from the given region (however, nothing is implied about its 1611767c5feSopenharmony_ci // validity). Carrier-specific numbers may connect to a different end-point, 1621767c5feSopenharmony_ci // or not connect at all, depending on the user's carrier. If it is important 1631767c5feSopenharmony_ci // that the number is valid, then its validity must first be checked using 1641767c5feSopenharmony_ci // IsValidShortNumber or IsValidShortNumberForRegion. Returns false if the 1651767c5feSopenharmony_ci // number doesn't match the region provided. 1661767c5feSopenharmony_ci bool IsCarrierSpecificForRegion( 1671767c5feSopenharmony_ci const PhoneNumber& number, 1681767c5feSopenharmony_ci const string& region_dialing_from) const; 1691767c5feSopenharmony_ci 1701767c5feSopenharmony_ci // Given a valid short number, determines whether it is an SMS service 1711767c5feSopenharmony_ci // (however, nothing is implied about its validity). An SMS service is where 1721767c5feSopenharmony_ci // the primary or only intended usage is to receive and/or send text messages 1731767c5feSopenharmony_ci // (SMSs). This includes MMS as MMS numbers downgrade to SMS if the other 1741767c5feSopenharmony_ci // party isn't MMS-capable. If it is important that the number is valid, then 1751767c5feSopenharmony_ci // its validity must first be checked using IsValidShortNumber or 1761767c5feSopenharmony_ci // IsValidShortNumberForRegion. Returns false if the number doesn't match the 1771767c5feSopenharmony_ci // region provided. 1781767c5feSopenharmony_ci bool IsSmsServiceForRegion( 1791767c5feSopenharmony_ci const PhoneNumber& number, 1801767c5feSopenharmony_ci const string& region_dialing_from) const; 1811767c5feSopenharmony_ci 1821767c5feSopenharmony_ci private: 1831767c5feSopenharmony_ci const PhoneNumberUtil& phone_util_; 1841767c5feSopenharmony_ci const scoped_ptr<const MatcherApi> matcher_api_; 1851767c5feSopenharmony_ci 1861767c5feSopenharmony_ci // A mapping from a RegionCode to the PhoneMetadata for that region. 1871767c5feSopenharmony_ci scoped_ptr<std::map<string, PhoneMetadata> > 1881767c5feSopenharmony_ci region_to_short_metadata_map_; 1891767c5feSopenharmony_ci 1901767c5feSopenharmony_ci // In these countries, if extra digits are added to an emergency number, it no 1911767c5feSopenharmony_ci // longer connects to the emergency service. 1921767c5feSopenharmony_ci scoped_ptr<std::set<string> > 1931767c5feSopenharmony_ci regions_where_emergency_numbers_must_be_exact_; 1941767c5feSopenharmony_ci 1951767c5feSopenharmony_ci const i18n::phonenumbers::PhoneMetadata* GetMetadataForRegion( 1961767c5feSopenharmony_ci const string& region_code) const; 1971767c5feSopenharmony_ci 1981767c5feSopenharmony_ci bool RegionDialingFromMatchesNumber(const PhoneNumber& number, 1991767c5feSopenharmony_ci const string& region_dialing_from) const; 2001767c5feSopenharmony_ci 2011767c5feSopenharmony_ci // Helper method to get the region code for a given phone number, from a list 2021767c5feSopenharmony_ci // of possible region codes. If the list contains more than one region, the 2031767c5feSopenharmony_ci // first region for which the number is valid is returned. 2041767c5feSopenharmony_ci void GetRegionCodeForShortNumberFromRegionList( 2051767c5feSopenharmony_ci const PhoneNumber& number, 2061767c5feSopenharmony_ci const list<string>& region_codes, 2071767c5feSopenharmony_ci string* region_code) const; 2081767c5feSopenharmony_ci 2091767c5feSopenharmony_ci bool MatchesEmergencyNumberHelper(const string& number, 2101767c5feSopenharmony_ci const string& region_code, 2111767c5feSopenharmony_ci bool allow_prefix_match) const; 2121767c5feSopenharmony_ci}; 2131767c5feSopenharmony_ci 2141767c5feSopenharmony_ci} // namespace phonenumbers 2151767c5feSopenharmony_ci} // namespace i18n 2161767c5feSopenharmony_ci 2171767c5feSopenharmony_ci#endif // I18N_PHONENUMBERS_SHORTNUMBERINFO_H_ 218