Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.PhonenumberUtils / Tizen.PhonenumberUtils / PhonenumberUtils.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18
19 namespace Tizen.PhonenumberUtils
20 {
21     /// <summary>
22     /// The PhonenumberUtils class provides methods for parsing, formatting and normalizing phone numbers.
23     /// </summary>
24     public class PhonenumberUtils : IDisposable
25     {
26         private bool disposed = false;
27
28         public PhonenumberUtils()
29         {
30             int ret;
31
32             ret = Interop.PhonenumberUtils.Connect();
33             if (ret != (int)PhonenumberUtilsError.None)
34             {
35                 Log.Error(Globals.LogTag, "Failed to connect, Error - " + (PhonenumberUtilsError)ret);
36                 PhonenumberUtilsErrorFactory.ThrowPhonenumberUtilsException(ret);
37             }
38         }
39
40         ~PhonenumberUtils()
41         {
42             Dispose(false);
43         }
44
45         /// <summary>
46         /// Releases all resources used by the PhonenumberUtils.
47         /// It should be called after finished using of the object.
48         /// </summary>
49         public void Dispose()
50         {
51             Dispose(true);
52             GC.SuppressFinalize(this);
53         }
54
55         private void Dispose(bool disposing)
56         {
57             if (disposed)
58                 return;
59
60             // Free unmanaged objects
61             int ret;
62
63             ret = Interop.PhonenumberUtils.Disconnect();
64             if (ret != (int)PhonenumberUtilsError.None)
65                 Log.Error(Globals.LogTag, "Failed to disconnect, Error - " + (PhonenumberUtilsError)ret);
66
67             disposed = true;
68         }
69
70         /// <summary>
71         /// Gets the location string from number, region, and language.
72         /// </summary>
73         /// <param name="number">The number</param>
74         /// <param name="region">The region of number</param>
75         /// <param name="language">The language of location</param>
76         /// <returns>The location string</returns>
77         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
78         /// <exception cref="NotSupportedException">Thrown when phonenumber-utils is not supported</exception>
79         /// <exception cref="ArgumentException">Thrown when input coordinates are invalid</exception>
80         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
81         public string GetLocationFromNumber(string number, Region region, Language language)
82         {
83             int ret;
84             string result;
85
86             ret = Interop.PhonenumberUtils.GetLocationFromNumber(number, (int)region, (int)language, out result);
87             if (ret != (int)PhonenumberUtilsError.None)
88             {
89                 Log.Error(Globals.LogTag, "Failed to get location, Error - " + (PhonenumberUtilsError)ret);
90                 PhonenumberUtilsErrorFactory.ThrowPhonenumberUtilsException(ret);
91             }
92
93             return result;
94         }
95
96         /// <summary>
97         /// Gets the formatted number.
98         /// </summary>
99         /// <param name="number">The number</param>
100         /// <param name="region">The region of number</param>
101         /// <returns>The formatted number string</returns>
102         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
103         /// <exception cref="NotSupportedException">Thrown when phonenumber-utils is not supported</exception>
104         /// <exception cref="ArgumentException">Thrown when input coordinates are invalid</exception>
105         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
106         public string GetFormattedNumber(string number, Region region)
107         {
108             int ret;
109             string result;
110
111             ret = Interop.PhonenumberUtils.GetFormmatedNumber(number, (int)region, out result);
112             if (ret != (int)PhonenumberUtilsError.None)
113             {
114                 Log.Error(Globals.LogTag, "Failed to get formatted number, Error - " + (PhonenumberUtilsError)ret);
115                 PhonenumberUtilsErrorFactory.ThrowPhonenumberUtilsException(ret);
116             }
117
118             return result;
119         }
120
121         /// <summary>
122         /// Gets the normalized number.
123         /// </summary>
124         /// <param name="number">The number</param>
125         /// <returns>The normalized number</returns>
126         /// <privilege>http://tizen.org/privilege/telephony</privilege>
127         /// <feature>http://tizen.org/feature/network.telephony</feature>
128         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
129         /// <exception cref="NotSupportedException">Thrown when phonenumber-utils is not supported</exception>
130         /// <exception cref="ArgumentException">Thrown when input coordinates are invalid</exception>
131         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
132         /// <exception cref="UnauthorizedAccessException">Thrown when application does not have proper privileges</exception>
133         /// <remarks>
134         /// Normalized number starts with plus('+') and country code, and excludes the separators such as dash or space.
135         /// It is a format of E.164 standard including the country code based on current network.
136         /// </remarks>
137         public string GetNormalizedNumber(string number)
138         {
139             int ret;
140             string result;
141
142             ret = Interop.PhonenumberUtils.GetNormailizedNumber(number, out result);
143             if (ret != (int)PhonenumberUtilsError.None)
144             {
145                 Log.Error(Globals.LogTag, "Failed to get normalized number, Error - " + (PhonenumberUtilsError)ret);
146                 PhonenumberUtilsErrorFactory.ThrowPhonenumberUtilsException(ret);
147             }
148
149             return result;
150         }
151     }
152 }