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// Author: Patrick Mezard
161767c5feSopenharmony_ci
171767c5feSopenharmony_ci#ifndef I18N_PHONENUMBERS_GEOCODING_PHONENUMBER_OFFLINE_GEOCODER_H_
181767c5feSopenharmony_ci#define I18N_PHONENUMBERS_GEOCODING_PHONENUMBER_OFFLINE_GEOCODER_H_
191767c5feSopenharmony_ci
201767c5feSopenharmony_ci#include <map>
211767c5feSopenharmony_ci#include <string>
221767c5feSopenharmony_ci
231767c5feSopenharmony_ci#include <unicode/locid.h>  // NOLINT(build/include_order)
241767c5feSopenharmony_ci
251767c5feSopenharmony_ci#include "phonenumbers/base/basictypes.h"
261767c5feSopenharmony_ci#include "phonenumbers/base/memory/scoped_ptr.h"
271767c5feSopenharmony_ci
281767c5feSopenharmony_cinamespace i18n {
291767c5feSopenharmony_cinamespace phonenumbers {
301767c5feSopenharmony_ci
311767c5feSopenharmony_ciusing std::map;
321767c5feSopenharmony_ciusing std::string;
331767c5feSopenharmony_ci
341767c5feSopenharmony_ciclass AreaCodeMap;
351767c5feSopenharmony_ciclass MappingFileProvider;
361767c5feSopenharmony_ciclass PhoneNumber;
371767c5feSopenharmony_ciclass PhoneNumberUtil;
381767c5feSopenharmony_cistruct CountryLanguages;
391767c5feSopenharmony_cistruct PrefixDescriptions;
401767c5feSopenharmony_citypedef icu::Locale Locale;
411767c5feSopenharmony_ci
421767c5feSopenharmony_ci// An offline geocoder which provides geographical information related to a
431767c5feSopenharmony_ci// phone number.
441767c5feSopenharmony_ciclass PhoneNumberOfflineGeocoder {
451767c5feSopenharmony_ci private:
461767c5feSopenharmony_ci  typedef map<string, const AreaCodeMap*> AreaCodeMaps;
471767c5feSopenharmony_ci
481767c5feSopenharmony_ci public:
491767c5feSopenharmony_ci  typedef const CountryLanguages* (*country_languages_getter)(int index);
501767c5feSopenharmony_ci  typedef const PrefixDescriptions* (*prefix_descriptions_getter)(int index);
511767c5feSopenharmony_ci
521767c5feSopenharmony_ci  PhoneNumberOfflineGeocoder();
531767c5feSopenharmony_ci
541767c5feSopenharmony_ci  // For tests
551767c5feSopenharmony_ci  PhoneNumberOfflineGeocoder(
561767c5feSopenharmony_ci      const int* country_calling_codes,
571767c5feSopenharmony_ci      int country_calling_codes_size,
581767c5feSopenharmony_ci      country_languages_getter get_country_languages,
591767c5feSopenharmony_ci      const char** prefix_language_code_pairs,
601767c5feSopenharmony_ci      int prefix_language_code_pairs_size,
611767c5feSopenharmony_ci      prefix_descriptions_getter get_prefix_descriptions);
621767c5feSopenharmony_ci
631767c5feSopenharmony_ci  // This type is neither copyable nor movable.
641767c5feSopenharmony_ci  PhoneNumberOfflineGeocoder(const PhoneNumberOfflineGeocoder&) = delete;
651767c5feSopenharmony_ci  PhoneNumberOfflineGeocoder& operator=(const PhoneNumberOfflineGeocoder&) =
661767c5feSopenharmony_ci      delete;
671767c5feSopenharmony_ci
681767c5feSopenharmony_ci  virtual ~PhoneNumberOfflineGeocoder();
691767c5feSopenharmony_ci
701767c5feSopenharmony_ci  // Returns a text description for the given phone number, in the language
711767c5feSopenharmony_ci  // provided. The description might consist of the name of the country where
721767c5feSopenharmony_ci  // the phone number is from, or the name of the geographical area the phone
731767c5feSopenharmony_ci  // number is from if more detailed information is available. Returns an empty
741767c5feSopenharmony_ci  // string if the number could come from multiple countries, or the country
751767c5feSopenharmony_ci  // code is in fact invalid.
761767c5feSopenharmony_ci  //
771767c5feSopenharmony_ci  // This method assumes the validity of the number passed in has already been
781767c5feSopenharmony_ci  // checked, and that the number is suitable for geocoding. We consider
791767c5feSopenharmony_ci  // fixed-line and mobile numbers possible candidates for geocoding.
801767c5feSopenharmony_ci  string GetDescriptionForValidNumber(const PhoneNumber& number,
811767c5feSopenharmony_ci                                      const Locale& language) const;
821767c5feSopenharmony_ci
831767c5feSopenharmony_ci  // As per GetDescriptionForValidNumber(PhoneNumber, Locale) but also considers
841767c5feSopenharmony_ci  // the region of the user. If the phone number is from the same region as the
851767c5feSopenharmony_ci  // user, only a lower-level description will be returned, if one exists.
861767c5feSopenharmony_ci  // Otherwise, the phone number's region will be returned, with optionally some
871767c5feSopenharmony_ci  // more detailed information.
881767c5feSopenharmony_ci  //
891767c5feSopenharmony_ci  // For example, for a user from the region "US" (United States), we would show
901767c5feSopenharmony_ci  // "Mountain View, CA" for a particular number, omitting the United States
911767c5feSopenharmony_ci  // from the description. For a user from the United Kingdom (region "GB"), for
921767c5feSopenharmony_ci  // the same number we may show "Mountain View, CA, United States" or even just
931767c5feSopenharmony_ci  // "United States".
941767c5feSopenharmony_ci  //
951767c5feSopenharmony_ci  // This method assumes the validity of the number passed in has already been
961767c5feSopenharmony_ci  // checked, and that the number is suitable for geocoding. We consider
971767c5feSopenharmony_ci  // fixed-line and mobile numbers possible candidates for geocoding.
981767c5feSopenharmony_ci  //
991767c5feSopenharmony_ci  // user_region is the region code for a given user. This region will be
1001767c5feSopenharmony_ci  // omitted from the description if the phone number comes from this region. It
1011767c5feSopenharmony_ci  // should be a two-letter uppercase CLDR region code.
1021767c5feSopenharmony_ci  string GetDescriptionForValidNumber(const PhoneNumber& number,
1031767c5feSopenharmony_ci      const Locale& language, const string& user_region) const;
1041767c5feSopenharmony_ci
1051767c5feSopenharmony_ci  // As per GetDescriptionForValidNumber(PhoneNumber, Locale) but explicitly
1061767c5feSopenharmony_ci  // checks the validity of the number passed in.
1071767c5feSopenharmony_ci  string GetDescriptionForNumber(const PhoneNumber& number,
1081767c5feSopenharmony_ci                                 const Locale& locale) const;
1091767c5feSopenharmony_ci
1101767c5feSopenharmony_ci  // As per GetDescriptionForValidNumber(PhoneNumber, Locale, String) but
1111767c5feSopenharmony_ci  // explicitly checks the validity of the number passed in.
1121767c5feSopenharmony_ci  string GetDescriptionForNumber(const PhoneNumber& number,
1131767c5feSopenharmony_ci      const Locale& language, const string& user_region) const;
1141767c5feSopenharmony_ci
1151767c5feSopenharmony_ci private:
1161767c5feSopenharmony_ci  void Init(const int* country_calling_codes,
1171767c5feSopenharmony_ci            int country_calling_codes_size,
1181767c5feSopenharmony_ci            country_languages_getter get_country_languages,
1191767c5feSopenharmony_ci            const char** prefix_language_code_pairs,
1201767c5feSopenharmony_ci            int prefix_language_code_pairs_size,
1211767c5feSopenharmony_ci            prefix_descriptions_getter get_prefix_descriptions);
1221767c5feSopenharmony_ci
1231767c5feSopenharmony_ci  const AreaCodeMap* LoadAreaCodeMapFromFile(
1241767c5feSopenharmony_ci      const string& filename) const;
1251767c5feSopenharmony_ci
1261767c5feSopenharmony_ci  const AreaCodeMap* GetPhonePrefixDescriptions(
1271767c5feSopenharmony_ci      int prefix, const string& language, const string& script,
1281767c5feSopenharmony_ci      const string& region) const;
1291767c5feSopenharmony_ci
1301767c5feSopenharmony_ci  // Returns the customary display name in the given language for the given
1311767c5feSopenharmony_ci  // region.
1321767c5feSopenharmony_ci  string GetRegionDisplayName(const string* region_code,
1331767c5feSopenharmony_ci                              const Locale& language) const;
1341767c5feSopenharmony_ci
1351767c5feSopenharmony_ci  // Returns the customary display name in the given language for the given
1361767c5feSopenharmony_ci  // territory the phone number is from.
1371767c5feSopenharmony_ci  string GetCountryNameForNumber(const PhoneNumber& number,
1381767c5feSopenharmony_ci                                 const Locale& language) const;
1391767c5feSopenharmony_ci
1401767c5feSopenharmony_ci  // Returns an area-level text description in the given language for the given
1411767c5feSopenharmony_ci  // phone number, or an empty string.
1421767c5feSopenharmony_ci  // lang is a two or three-letter lowercase ISO language code as defined by ISO
1431767c5feSopenharmony_ci  // 639. Note that where two different language codes exist (e.g. 'he' and 'iw'
1441767c5feSopenharmony_ci  // for Hebrew) we use the one that Java/Android canonicalized on ('iw' in this
1451767c5feSopenharmony_ci  // case).
1461767c5feSopenharmony_ci  // script is a four-letter titlecase (the first letter is uppercase and the
1471767c5feSopenharmony_ci  // rest of the letters are lowercase) ISO script code as defined in ISO 15924.
1481767c5feSopenharmony_ci  // region should be a two-letter uppercase ISO country code as defined by ISO
1491767c5feSopenharmony_ci  // 3166-1.
1501767c5feSopenharmony_ci  const char* GetAreaDescription(const PhoneNumber& number, const string& lang,
1511767c5feSopenharmony_ci                                 const string& script,
1521767c5feSopenharmony_ci                                 const string& region) const;
1531767c5feSopenharmony_ci
1541767c5feSopenharmony_ci  bool MayFallBackToEnglish(const string& lang) const;
1551767c5feSopenharmony_ci
1561767c5feSopenharmony_ci private:
1571767c5feSopenharmony_ci  const PhoneNumberUtil* phone_util_;
1581767c5feSopenharmony_ci  // The MappingFileProvider knows for which combination of country calling code
1591767c5feSopenharmony_ci  // and language a phone prefix mapping file is available in the file system,
1601767c5feSopenharmony_ci  // so that a file can be loaded when needed.
1611767c5feSopenharmony_ci  scoped_ptr<const MappingFileProvider> provider_;
1621767c5feSopenharmony_ci
1631767c5feSopenharmony_ci  const char** prefix_language_code_pairs_;
1641767c5feSopenharmony_ci  int prefix_language_code_pairs_size_;
1651767c5feSopenharmony_ci  prefix_descriptions_getter get_prefix_descriptions_;
1661767c5feSopenharmony_ci
1671767c5feSopenharmony_ci  // A mapping from country calling codes languages pairs to the corresponding
1681767c5feSopenharmony_ci  // phone prefix map that has been loaded.
1691767c5feSopenharmony_ci  mutable AreaCodeMaps available_maps_;
1701767c5feSopenharmony_ci};
1711767c5feSopenharmony_ci
1721767c5feSopenharmony_ci}  // namespace phonenumbers
1731767c5feSopenharmony_ci}  // namespace i18n
1741767c5feSopenharmony_ci
1751767c5feSopenharmony_ci#endif /* I18N_PHONENUMBERS_GEOCODING_PHONENUMBER_OFFLINE_GEOCODER_H_ */
176