/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; namespace Tizen.PhonenumberUtils { /// /// The PhonenumberUtils class provides methods for parsing, formatting and normalizing phone numbers. /// public class PhonenumberUtils : IDisposable { private bool disposed = false; public PhonenumberUtils() { int ret; ret = Interop.PhonenumberUtils.Connect(); if (ret != (int)PhonenumberUtilsError.None) { Log.Error(Globals.LogTag, "Failed to connect, Error - " + (PhonenumberUtilsError)ret); PhonenumberUtilsErrorFactory.ThrowPhonenumberUtilsException(ret); } } ~PhonenumberUtils() { Dispose(false); } /// /// Releases all resources used by the PhonenumberUtils. /// It should be called after finished using of the object. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (disposed) return; // Free unmanaged objects int ret; ret = Interop.PhonenumberUtils.Disconnect(); if (ret != (int)PhonenumberUtilsError.None) Log.Error(Globals.LogTag, "Failed to disconnect, Error - " + (PhonenumberUtilsError)ret); disposed = true; } /// /// Gets the location string from number, region, and language. /// /// The number /// The region of number /// The language of location /// The location string /// Thrown when method failed due to invalid operation /// Thrown when phonenumber-utils is not supported /// Thrown when input coordinates are invalid /// Thrown when failed due to out of memory public string GetLocationFromNumber(string number, Region region, Language language) { int ret; string result; ret = Interop.PhonenumberUtils.GetLocationFromNumber(number, (int)region, (int)language, out result); if (ret != (int)PhonenumberUtilsError.None) { Log.Error(Globals.LogTag, "Failed to get location, Error - " + (PhonenumberUtilsError)ret); PhonenumberUtilsErrorFactory.ThrowPhonenumberUtilsException(ret); } return result; } /// /// Gets the formatted number. /// /// The number /// The region of number /// The formatted number string /// Thrown when method failed due to invalid operation /// Thrown when phonenumber-utils is not supported /// Thrown when input coordinates are invalid /// Thrown when failed due to out of memory public string GetFormattedNumber(string number, Region region) { int ret; string result; ret = Interop.PhonenumberUtils.GetFormmatedNumber(number, (int)region, out result); if (ret != (int)PhonenumberUtilsError.None) { Log.Error(Globals.LogTag, "Failed to get formatted number, Error - " + (PhonenumberUtilsError)ret); PhonenumberUtilsErrorFactory.ThrowPhonenumberUtilsException(ret); } return result; } /// /// Gets the normalized number. /// /// The number /// The normalized number /// http://tizen.org/privilege/telephony /// http://tizen.org/feature/network.telephony /// Thrown when method failed due to invalid operation /// Thrown when phonenumber-utils is not supported /// Thrown when input coordinates are invalid /// Thrown when failed due to out of memory /// Thrown when application does not have proper privileges /// /// Normalized number starts with plus('+') and country code, and excludes the separators such as dash or space. /// It is a format of E.164 standard including the country code based on current network. /// public string GetNormalizedNumber(string number) { int ret; string result; ret = Interop.PhonenumberUtils.GetNormailizedNumber(number, out result); if (ret != (int)PhonenumberUtilsError.None) { Log.Error(Globals.LogTag, "Failed to get normalized number, Error - " + (PhonenumberUtilsError)ret); PhonenumberUtilsErrorFactory.ThrowPhonenumberUtilsException(ret); } return result; } } }