2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Threading.Tasks;
19 using System.Runtime.InteropServices;
20 using System.Collections.Generic;
25 /// A class which manages sim phonebook record information.
27 public class Phonebook
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;
38 /// Gets the instance of Phonebook class.
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)
46 throw new ArgumentNullException("Handle is null");
49 _handle = handle._handle;
53 /// Gets the current inserted SIM phonebook init status, available phonebook list, and first valid index in case of FDN, ADN, and 3G phonebook.
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()
64 SimPhonebookStatusStruct pbStatus;
65 int ret = Interop.Tapi.Phonebook.GetPhonebookInitInfo(_handle, out int isInitCompleted, out SimPhonebookListStruct pbList);
66 if (ret != (int)TapiError.Success)
68 Log.Error(TapiUtility.LogTag, "Failed to get phonebook init info, Error: " + (TapiError)ret);
69 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
72 pbStatus.IsInitCompleted = isInitCompleted;
73 pbStatus.PbList = pbList;
74 return PhonebookStructConversions.ConvertSimPhonebookStatusStruct(pbStatus);
78 /// Gets the number of used records and total records of a specific SIM phonebook type.
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)
90 TaskCompletionSource<PhonebookStorageInfo> task = new TaskCompletionSource<PhonebookStorageInfo>();
91 IntPtr id = (IntPtr)_requestId++;
92 _callbackMap[id] = (handle, result, data, key) =>
94 Task taskResult = new Task(() =>
96 if (result != (int)PhonebookAccessResult.Success)
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));
103 PhonebookStorageInfoStruct info = Marshal.PtrToStructure<PhonebookStorageInfoStruct>(data);
104 task.SetResult(PhonebookStructConversions.ConvertPhonebookStorageStruct(info));
108 _callbackMap.Remove(key);
111 int ret = Interop.Tapi.Phonebook.GetPhonebookStorage(_handle, type, _callbackMap[id], id);
112 if (ret != (int)TapiError.Success)
114 Log.Error(TapiUtility.LogTag, "Failed to get phonebook storage info, Error: " + (TapiError)ret);
115 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
122 /// Gets the max text length and max number length supported by the SIM phone book elementary file.
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)
134 TaskCompletionSource<PhonebookMetaInfo> task = new TaskCompletionSource<PhonebookMetaInfo>();
135 IntPtr id = (IntPtr)_requestId++;
136 _callbackMap[id] = (handle, result, data, key) =>
138 Task taskResult = new Task(() =>
140 if (result != (int)PhonebookAccessResult.Success)
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));
147 PhonebookMetaInfoStruct info = Marshal.PtrToStructure<PhonebookMetaInfoStruct>(data);
148 task.SetResult(PhonebookStructConversions.ConvertPhonebookMetaInfoStruct(info));
152 _callbackMap.Remove(key);
155 int ret = Interop.Tapi.Phonebook.GetPhonebookMetaInfo(_handle, type, _callbackMap[id], id);
156 if (ret != (int)TapiError.Success)
158 Log.Error(TapiUtility.LogTag, "Failed to get phonebook meta info, Error: " + (TapiError)ret);
159 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
166 /// Gets SIM 3G phonebook supported EFs like ANR, SNE, GRP, EMAIL and the corresponding EFs max text length, number length, and size.
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()
177 TaskCompletionSource<PhonebookMetaInfo3G> task = new TaskCompletionSource<PhonebookMetaInfo3G>();
178 IntPtr id = (IntPtr)_requestId++;
179 _callbackMap[id] = (handle, result, data, key) =>
181 Task taskResult = new Task(() =>
183 if (result != (int)PhonebookAccessResult.Success)
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));
190 PhonebookMetaInfo3GStruct metaInfo = Marshal.PtrToStructure<PhonebookMetaInfo3GStruct>(data);
191 task.SetResult(PhonebookStructConversions.ConvertPhonebookMetaInfo3GStruct(metaInfo));
195 _callbackMap.Remove(key);
198 int ret = Interop.Tapi.Phonebook.GetPhonebookMetaInfo3G(_handle, _callbackMap[id], id);
199 if (ret != (int)TapiError.Success)
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");
209 /// Reads SIM phone book entry information from the given storage type and index.
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)
222 TaskCompletionSource<PhonebookRecord> task = new TaskCompletionSource<PhonebookRecord>();
223 IntPtr id = (IntPtr)_requestId++;
224 _callbackMap[id] = (handle, result, data, key) =>
226 Task taskResult = new Task(() =>
228 if (result != (int)PhonebookAccessResult.Success)
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));
235 PhonebookRecordStruct record = Marshal.PtrToStructure<PhonebookRecordStruct>(data);
236 task.SetResult(PhonebookStructConversions.ConvertPhonebookRecordStruct(record));
240 _callbackMap.Remove(key);
245 throw new ArgumentException("Index should not be zero");
248 int ret = Interop.Tapi.Phonebook.ReadPhonebookRecord(_handle, type, index, _callbackMap[id], id);
249 if (ret != (int)TapiError.Success)
251 Log.Error(TapiUtility.LogTag, "Failed to read phonebook record, Error: " + (TapiError)ret);
252 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony");
259 /// Adds or edits SIM phone book record entry information.
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)
273 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
274 IntPtr id = (IntPtr)_requestId++;
275 _callbackMap[id] = (handle, result, data, key) =>
277 Task taskResult = new Task(() =>
279 if (result != (int)PhonebookAccessResult.Success)
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));
286 task.SetResult(true);
290 _callbackMap.Remove(key);
295 throw new ArgumentNullException("Phonebook record is null");
298 if (record.Index == 0)
300 throw new ArgumentException("Index in phonebook record is zero");
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)
307 Log.Error(TapiUtility.LogTag, "Failed to update phonebook record, Error: " + (TapiError)ret);
308 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");
315 /// Deletes a SIM phonebook record.
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)
329 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
330 IntPtr id = (IntPtr)_requestId++;
331 _callbackMap[id] = (handle, result, data, key) =>
333 Task taskResult = new Task(() =>
335 if (result != (int)PhonebookAccessResult.Success)
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));
342 task.SetResult(true);
346 _callbackMap.Remove(key);
351 throw new ArgumentException("Index of the record is zero");
354 int ret = Interop.Tapi.Phonebook.DeletePhonebookRecord(_handle, type, index, _callbackMap[id], id);
355 if (ret != (int)TapiError.Success)
357 Log.Error(TapiUtility.LogTag, "Failed to delete phonebook record, Error: " + (TapiError)ret);
358 TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin");