Review PhonenumberUtils API cs files
[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 the methods for parsing, formatting, and normalizing the phone numbers.
23     /// </summary>
24     /// <since_tizen> 3 </since_tizen>
25     public class PhonenumberUtils : IDisposable
26     {
27         private bool disposed = false;
28
29         /// <summary>
30         /// Creates a PhonenumberUtils.
31         /// </summary>
32         /// <feature>http://tizen.org/feature/network.telephony</feature>
33         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
34         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
35         /// <since_tizen> 3 </since_tizen>
36         public PhonenumberUtils()
37         {
38             int ret;
39
40             ret = Interop.PhonenumberUtils.Connect();
41             if (ret != (int)PhonenumberUtilsError.None)
42             {
43                 Log.Error(Globals.LogTag, "Failed to connect, Error - " + (PhonenumberUtilsError)ret);
44                 PhonenumberUtilsErrorFactory.ThrowPhonenumberUtilsException(ret);
45             }
46         }
47
48         /// <summary>
49         /// The destructor.
50         /// </summary>
51         /// <since_tizen> 4 </since_tizen>
52         ~PhonenumberUtils()
53         {
54             Dispose(false);
55         }
56
57         /// <summary>
58         /// Releases all the resources used by the PhonenumberUtils.
59         /// It should be called after it has finished using the object.
60         /// </summary>
61         /// <since_tizen> 3 </since_tizen>
62         public void Dispose()
63         {
64             Dispose(true);
65             GC.SuppressFinalize(this);
66         }
67
68         private void Dispose(bool disposing)
69         {
70             if (disposed)
71                 return;
72
73             // Free unmanaged objects
74             int ret;
75
76             ret = Interop.PhonenumberUtils.Disconnect();
77             if (ret != (int)PhonenumberUtilsError.None)
78                 Log.Error(Globals.LogTag, "Failed to disconnect, Error - " + (PhonenumberUtilsError)ret);
79
80             disposed = true;
81         }
82
83         /// <summary>
84         /// Gets the location string from the number, region, and language.
85         /// </summary>
86         /// <param name="number">The number.</param>
87         /// <param name="region">The region of number.</param>
88         /// <param name="language">The language of location.</param>
89         /// <returns>The location string.</returns>
90         /// <feature>http://tizen.org/feature/network.telephony</feature>
91         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
92         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
93         /// <exception cref="ArgumentException">Thrown when the input coordinates are invalid.</exception>
94         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory.</exception>
95         /// <since_tizen> 3 </since_tizen>
96         public string GetLocationFromNumber(string number, Region region, Language language)
97         {
98             int ret;
99             string result;
100
101             ret = Interop.PhonenumberUtils.GetLocationFromNumber(number, (int)region, (int)language, out result);
102             if (ret != (int)PhonenumberUtilsError.None)
103             {
104                 Log.Error(Globals.LogTag, "Failed to get location, Error - " + (PhonenumberUtilsError)ret);
105                 PhonenumberUtilsErrorFactory.ThrowPhonenumberUtilsException(ret);
106             }
107
108             return result;
109         }
110
111         /// <summary>
112         /// Gets the formatted number.
113         /// </summary>
114         /// <param name="number">The number.</param>
115         /// <param name="region">The region of number.</param>
116         /// <returns>The formatted number string.</returns>
117         /// <feature>http://tizen.org/feature/network.telephony</feature>
118         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
119         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
120         /// <exception cref="ArgumentException">Thrown when the input coordinates are invalid.</exception>
121         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory.</exception>
122         /// <since_tizen> 3 </since_tizen>
123         public string GetFormattedNumber(string number, Region region)
124         {
125             int ret;
126             string result;
127
128             ret = Interop.PhonenumberUtils.GetFormmatedNumber(number, (int)region, out result);
129             if (ret != (int)PhonenumberUtilsError.None)
130             {
131                 Log.Error(Globals.LogTag, "Failed to get formatted number, Error - " + (PhonenumberUtilsError)ret);
132                 PhonenumberUtilsErrorFactory.ThrowPhonenumberUtilsException(ret);
133             }
134
135             return result;
136         }
137
138         /// <summary>
139         /// Gets the normalized number.
140         /// </summary>
141         /// <param name="number">The number.</param>
142         /// <returns>The normalized number.</returns>
143         /// <privilege>http://tizen.org/privilege/telephony</privilege>
144         /// <feature>http://tizen.org/feature/network.telephony</feature>
145         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
146         /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
147         /// <exception cref="ArgumentException">Thrown when the input coordinates are invalid.</exception>
148         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory.</exception>
149         /// <exception cref="UnauthorizedAccessException">Thrown when the application does not have proper privileges.</exception>
150         /// <remarks>
151         /// Normalized number starts with plus('+') and country code, and excludes the separators such as dash or space.
152         /// It is a format of the E.164 standard including the country code based on the current network.
153         /// </remarks>
154         /// <since_tizen> 3 </since_tizen>
155         public string GetNormalizedNumber(string number)
156         {
157             int ret;
158             string result;
159
160             ret = Interop.PhonenumberUtils.GetNormailizedNumber(number, out result);
161             if (ret != (int)PhonenumberUtilsError.None)
162             {
163                 Log.Error(Globals.LogTag, "Failed to get normalized number, Error - " + (PhonenumberUtilsError)ret);
164                 PhonenumberUtilsErrorFactory.ThrowPhonenumberUtilsException(ret);
165             }
166
167             return result;
168         }
169     }
170 }