Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Tapi / Tizen.Tapi / Phonebook.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 using System.Threading.Tasks;
19 using System.Runtime.InteropServices;
20 using System.Collections.Generic;
21
22 namespace Tizen.Tapi
23 {
24     /// <summary>
25     /// A class which manages sim phonebook record information.
26     /// </summary>
27     public class Phonebook
28     {
29         private IntPtr _handle = IntPtr.Zero;
30         private Dictionary<IntPtr, Interop.Tapi.TapiResponseCallback> _callbackMap = new Dictionary<IntPtr, Interop.Tapi.TapiResponseCallback>();
31         private int _requestId = 0;
32
33         private Phonebook()
34         {
35         }
36
37         /// <summary>
38         /// Gets the instance of Phonebook class.
39         /// </summary>
40         /// <param name="handle">An instance of TapiHandle obtained from InitTapi in TapiManager API.</param>
41         /// <exception cref="ArgumentNullException">Thrown when handle is passed as null.</exception>
42         public Phonebook(TapiHandle handle)
43         {
44             if (handle == null)
45             {
46                 throw new ArgumentNullException("Handle is null");
47             }
48
49             _handle = handle._handle;
50         }
51
52         /// <summary>
53         /// Gets the current inserted SIM phonebook init status, available phonebook list, and first valid index in case of FDN, ADN, and 3G phonebook.
54         /// </summary>
55         /// <returns>An instance of SimPhonebookStatus containing init status and phonebook list information.</returns>
56         /// <feature>http://tizen.org/feature/network.telephony</feature>
57         /// <privilege>http://tizen.org/privilege/telephony</privilege>
58         /// <exception cref="NotSupportedException">Thrown when telephony feature is not supported.</exception>
59         /// <exception cref="UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
60         /// <exception cref="ArgumentException">Thrown when it is failed due to invalid parameter.</exception>
61         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation.</exception>
62         public SimPhonebookStatus GetPhonebookInitInfo()
63         {
64             SimPhonebookStatusStruct pbStatus;
65             int ret = Interop.Tapi.Phonebook.GetPhonebookInitInfo(_handle, out int isInitCompleted, out SimPhonebookListStruct pbList);
66             if (ret != (int)TapiError.Success)
67             {
68                 Log.Error(TapiUtility.LogTag, "Failed to get phonebook init info, Error: " + (TapiError)ret);
69                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
70             }
71
72             pbStatus.IsInitCompleted = isInitCompleted;
73             pbStatus.PbList = pbList;
74             return PhonebookStructConversions.ConvertSimPhonebookStatusStruct(pbStatus);
75         }
76
77         /// <summary>
78         /// Gets the number of used records and total records of a specific SIM phonebook type.
79         /// </summary>
80         /// <param name="type">The different storage types to be selected in the SIM.</param>
81         /// <returns>A task containing an instance of PhonebookStorageInfo.</returns>
82         /// <feature>http://tizen.org/feature/network.telephony</feature>
83         /// <privilege>http://tizen.org/privilege/telephony</privilege>
84         /// <exception cref="NotSupportedException">Thrown when telephony feature is not supported.</exception>
85         /// <exception cref="UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
86         /// <exception cref="ArgumentException">Thrown when it is failed due to invalid parameter.</exception>
87         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation.</exception>
88         public Task<PhonebookStorageInfo> GetPhonebookStorage(PhonebookType type)
89         {
90             TaskCompletionSource<PhonebookStorageInfo> task = new TaskCompletionSource<PhonebookStorageInfo>();
91             IntPtr id = (IntPtr)_requestId++;
92             _callbackMap[id] = (handle, result, data, key) =>
93             {
94                 Task taskResult = new Task(() =>
95                 {
96                     if (result != (int)PhonebookAccessResult.Success)
97                     {
98                         Log.Error(TapiUtility.LogTag, "Error occurs during getting phone book storage: " + (PhonebookAccessResult)result);
99                         task.SetException(new InvalidOperationException("Error occurs during getting phone book storage, " + (PhonebookAccessResult)result));
100                         return;
101                     }
102
103                     PhonebookStorageInfoStruct info = Marshal.PtrToStructure<PhonebookStorageInfoStruct>(data);
104                     task.SetResult(PhonebookStructConversions.ConvertPhonebookStorageStruct(info));
105                 });
106                 taskResult.Start();
107                 taskResult.Wait();
108                 _callbackMap.Remove(key);
109             };
110
111             int ret = Interop.Tapi.Phonebook.GetPhonebookStorage(_handle, type, _callbackMap[id], id);
112             if (ret != (int)TapiError.Success)
113             {
114                 Log.Error(TapiUtility.LogTag, "Failed to get phonebook storage info, Error: " + (TapiError)ret);
115                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
116             }
117
118             return task.Task;
119         }
120
121         /// <summary>
122         /// Gets the max text length and max number length supported by the SIM phone book elementary file.
123         /// </summary>
124         /// <param name="type">The different storage types to be selected in the SIM.</param>
125         /// <returns>A task containing an instance of PhonebookMetaInfo.</returns>
126         /// <feature>http://tizen.org/feature/network.telephony</feature>
127         /// <privilege>http://tizen.org/privilege/telephony</privilege>
128         /// <exception cref="NotSupportedException">Thrown when telephony feature is not supported.</exception>
129         /// <exception cref="UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
130         /// <exception cref="ArgumentException">Thrown when it is failed due to invalid parameter.</exception>
131         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation.</exception>
132         public Task<PhonebookMetaInfo> GetPhonebookMetaInfo(PhonebookType type)
133         {
134             TaskCompletionSource<PhonebookMetaInfo> task = new TaskCompletionSource<PhonebookMetaInfo>();
135             IntPtr id = (IntPtr)_requestId++;
136             _callbackMap[id] = (handle, result, data, key) =>
137             {
138                 Task taskResult = new Task(() =>
139                 {
140                     if (result != (int)PhonebookAccessResult.Success)
141                     {
142                         Log.Error(TapiUtility.LogTag, "Error occurs during getting phone book meta info: " + (PhonebookAccessResult)result);
143                         task.SetException(new InvalidOperationException("Error occurs during getting phone book meta info, " + (PhonebookAccessResult)result));
144                         return;
145                     }
146
147                     PhonebookMetaInfoStruct info = Marshal.PtrToStructure<PhonebookMetaInfoStruct>(data);
148                     task.SetResult(PhonebookStructConversions.ConvertPhonebookMetaInfoStruct(info));
149                 });
150                 taskResult.Start();
151                 taskResult.Wait();
152                 _callbackMap.Remove(key);
153             };
154
155             int ret = Interop.Tapi.Phonebook.GetPhonebookMetaInfo(_handle, type, _callbackMap[id], id);
156             if (ret != (int)TapiError.Success)
157             {
158                 Log.Error(TapiUtility.LogTag, "Failed to get phonebook meta info, Error: " + (TapiError)ret);
159                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
160             }
161
162             return task.Task;
163         }
164
165         /// <summary>
166         /// Gets SIM 3G phonebook supported EFs like ANR, SNE, GRP, EMAIL and the corresponding EFs max text length, number length, and size.
167         /// </summary>
168         /// <returns>A task containing an instance of PhonebookMetaInfo3G.</returns>
169         /// <feature>http://tizen.org/feature/network.telephony</feature>
170         /// <privilege>http://tizen.org/privilege/telephony</privilege>
171         /// <exception cref="NotSupportedException">Thrown when telephony feature is not supported.</exception>
172         /// <exception cref="UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
173         /// <exception cref="ArgumentException">Thrown when it is failed due to invalid parameter.</exception>
174         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation.</exception>
175         public Task<PhonebookMetaInfo3G> GetPhonebookMetaInfo3G()
176         {
177             TaskCompletionSource<PhonebookMetaInfo3G> task = new TaskCompletionSource<PhonebookMetaInfo3G>();
178             IntPtr id = (IntPtr)_requestId++;
179             _callbackMap[id] = (handle, result, data, key) =>
180             {
181                 Task taskResult = new Task(() =>
182                 {
183                     if (result != (int)PhonebookAccessResult.Success)
184                     {
185                         Log.Error(TapiUtility.LogTag, "Error occurs during getting 3G phone book meta info: " + (PhonebookAccessResult)result);
186                         task.SetException(new InvalidOperationException("Error occurs during getting 3G phone book meta info, " + (PhonebookAccessResult)result));
187                         return;
188                     }
189
190                     PhonebookMetaInfo3GStruct metaInfo = Marshal.PtrToStructure<PhonebookMetaInfo3GStruct>(data);
191                     task.SetResult(PhonebookStructConversions.ConvertPhonebookMetaInfo3GStruct(metaInfo));
192                 });
193                 taskResult.Start();
194                 taskResult.Wait();
195                 _callbackMap.Remove(key);
196             };
197
198             int ret = Interop.Tapi.Phonebook.GetPhonebookMetaInfo3G(_handle, _callbackMap[id], id);
199             if (ret != (int)TapiError.Success)
200             {
201                 Log.Error(TapiUtility.LogTag, "Failed to get 3G phonebook meta info, Error: " + (TapiError)ret);
202                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
203             }
204
205             return task.Task;
206         }
207
208         /// <summary>
209         /// Reads SIM phone book entry information from the given storage type and index.
210         /// </summary>
211         /// <param name="type">The different storage types to be selected in the SIM.</param>
212         /// <param name="index">The index for accessing the SIM data.</param>
213         /// <returns>A task containing an instance of PhonebookRecord.</returns>
214         /// <feature>http://tizen.org/feature/network.telephony</feature>
215         /// <privilege>http://tizen.org/privilege/telephony</privilege>
216         /// <exception cref="NotSupportedException">Thrown when telephony feature is not supported.</exception>
217         /// <exception cref="UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
218         /// <exception cref="ArgumentException">Thrown when it is failed due to invalid parameter.</exception>
219         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation.</exception>
220         public Task<PhonebookRecord> ReadPhonebookRecord(PhonebookType type, ushort index)
221         {
222             TaskCompletionSource<PhonebookRecord> task = new TaskCompletionSource<PhonebookRecord>();
223             IntPtr id = (IntPtr)_requestId++;
224             _callbackMap[id] = (handle, result, data, key) =>
225             {
226                 Task taskResult = new Task(() =>
227                 {
228                     if (result != (int)PhonebookAccessResult.Success)
229                     {
230                         Log.Error(TapiUtility.LogTag, "Error occurs during reading phone book record: " + (PhonebookAccessResult)result);
231                         task.SetException(new InvalidOperationException("Error occurs during reading phone book record, " + (PhonebookAccessResult)result));
232                         return;
233                     }
234
235                     PhonebookRecordStruct record = Marshal.PtrToStructure<PhonebookRecordStruct>(data);
236                     task.SetResult(PhonebookStructConversions.ConvertPhonebookRecordStruct(record));
237                 });
238                 taskResult.Start();
239                 taskResult.Wait();
240                 _callbackMap.Remove(key);
241             };
242
243             if (index == 0)
244             {
245                 throw new ArgumentException("Index should not be zero");
246             }
247
248             int ret = Interop.Tapi.Phonebook.ReadPhonebookRecord(_handle, type, index, _callbackMap[id], id);
249             if (ret != (int)TapiError.Success)
250             {
251                 Log.Error(TapiUtility.LogTag, "Failed to read phonebook record, Error: " + (TapiError)ret);
252                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
253             }
254
255             return task.Task;
256         }
257
258         /// <summary>
259         /// Adds or edits SIM phone book record entry information.
260         /// </summary>
261         /// <param name="record">The phonebook data to be updated or added.</param>
262         /// <returns>A task indicating whether the updation is done or not.</returns>
263         /// <feature>http://tizen.org/feature/network.telephony</feature>
264         /// <privlevel>platform</privlevel>
265         /// <privilege>http://tizen.org/privilege/telephony.admin</privilege>
266         /// <exception cref="NotSupportedException">Thrown when telephony feature is not supported.</exception>
267         /// <exception cref="UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
268         /// <exception cref="ArgumentNullException">Thrown when record is passed as null.</exception>
269         /// <exception cref="ArgumentException">Thrown when it is failed due to invalid parameter.</exception>
270         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation.</exception>
271         public Task<bool> UpdatePhonebookRecord(PhonebookRecord record)
272         {
273             TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
274             IntPtr id = (IntPtr)_requestId++;
275             _callbackMap[id] = (handle, result, data, key) =>
276             {
277                 Task taskResult = new Task(() =>
278                 {
279                     if (result != (int)PhonebookAccessResult.Success)
280                     {
281                         Log.Error(TapiUtility.LogTag, "Error occurs during updation of phone book record: " + (PhonebookAccessResult)result);
282                         task.SetException(new InvalidOperationException("Error occurs during updation of phone book record, " + (PhonebookAccessResult)result));
283                         return;
284                     }
285
286                     task.SetResult(true);
287                 });
288                 taskResult.Start();
289                 taskResult.Wait();
290                 _callbackMap.Remove(key);
291             };
292
293             if (record == null)
294             {
295                 throw new ArgumentNullException("Phonebook record is null");
296             }
297
298             if (record.Index == 0)
299             {
300                 throw new ArgumentException("Index in phonebook record is zero");
301             }
302
303             PhonebookRecordStruct recordStruct = PhonebookClassConversions.ConvertPhonebookrecord(record);
304             int ret = Interop.Tapi.Phonebook.UpdatePhonebookRecord(_handle, ref recordStruct, _callbackMap[id], id);
305             if (ret != (int)TapiError.Success)
306             {
307                 Log.Error(TapiUtility.LogTag, "Failed to update phonebook record, Error: " + (TapiError)ret);
308                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");
309             }
310
311             return task.Task;
312         }
313
314         /// <summary>
315         /// Deletes a SIM phonebook record.
316         /// </summary>
317         /// <param name="type">The different storage types to be selected in the SIM.</param>
318         /// <param name="index">The index of the record to be deleted.</param>
319         /// <returns>A task indicating whether deletion is done or not.</returns>
320         /// <feature>http://tizen.org/feature/network.telephony</feature>
321         /// <privlevel>platform</privlevel>
322         /// <privilege>http://tizen.org/privilege/telephony.admin</privilege>
323         /// <exception cref="NotSupportedException">Thrown when telephony feature is not supported.</exception>
324         /// <exception cref="UnauthorizedAccessException">Thrown when privilege access is denied.</exception>
325         /// <exception cref="ArgumentException">Thrown when it is failed due to invalid parameter.</exception>
326         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation.</exception>
327         public Task<bool> DeletePhonebookRecord(PhonebookType type, ushort index)
328         {
329             TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
330             IntPtr id = (IntPtr)_requestId++;
331             _callbackMap[id] = (handle, result, data, key) =>
332             {
333                 Task taskResult = new Task(() =>
334                 {
335                     if (result != (int)PhonebookAccessResult.Success)
336                     {
337                         Log.Error(TapiUtility.LogTag, "Error occurs during deletion of phone book record: " + (PhonebookAccessResult)result);
338                         task.SetException(new InvalidOperationException("Error occurs during deletion of phone book record, " + (PhonebookAccessResult)result));
339                         return;
340                     }
341
342                     task.SetResult(true);
343                 });
344                 taskResult.Start();
345                 taskResult.Wait();
346                 _callbackMap.Remove(key);
347             };
348
349             if (index == 0)
350             {
351                 throw new ArgumentException("Index of the record is zero");
352             }
353
354             int ret = Interop.Tapi.Phonebook.DeletePhonebookRecord(_handle, type, index, _callbackMap[id], id);
355             if (ret != (int)TapiError.Success)
356             {
357                 Log.Error(TapiUtility.LogTag, "Failed to delete phonebook record, Error: " + (TapiError)ret);
358                 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");
359             }
360
361             return task.Task;
362         }
363     }
364 }