/* * 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; using System.Threading.Tasks; using System.Runtime.InteropServices; using System.Collections.Generic; namespace Tizen.Tapi { /// /// A class which manages sim phonebook record information. /// public class Phonebook { private IntPtr _handle = IntPtr.Zero; private Dictionary _callbackMap = new Dictionary(); private int _requestId = 0; private Phonebook() { } /// /// Gets the instance of Phonebook class. /// /// An instance of TapiHandle obtained from InitTapi in TapiManager API. /// Thrown when handle is passed as null. public Phonebook(TapiHandle handle) { if (handle == null) { throw new ArgumentNullException("Handle is null"); } _handle = handle._handle; } /// /// Gets the current inserted SIM phonebook init status, available phonebook list, and first valid index in case of FDN, ADN, and 3G phonebook. /// /// An instance of SimPhonebookStatus containing init status and phonebook list information. /// http://tizen.org/feature/network.telephony /// http://tizen.org/privilege/telephony /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public SimPhonebookStatus GetPhonebookInitInfo() { SimPhonebookStatusStruct pbStatus; int ret = Interop.Tapi.Phonebook.GetPhonebookInitInfo(_handle, out int isInitCompleted, out SimPhonebookListStruct pbList); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to get phonebook init info, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony"); } pbStatus.IsInitCompleted = isInitCompleted; pbStatus.PbList = pbList; return PhonebookStructConversions.ConvertSimPhonebookStatusStruct(pbStatus); } /// /// Gets the number of used records and total records of a specific SIM phonebook type. /// /// The different storage types to be selected in the SIM. /// A task containing an instance of PhonebookStorageInfo. /// http://tizen.org/feature/network.telephony /// http://tizen.org/privilege/telephony /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task GetPhonebookStorage(PhonebookType type) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)PhonebookAccessResult.Success) { Log.Error(TapiUtility.LogTag, "Error occurs during getting phone book storage: " + (PhonebookAccessResult)result); task.SetException(new InvalidOperationException("Error occurs during getting phone book storage, " + (PhonebookAccessResult)result)); return; } PhonebookStorageInfoStruct info = Marshal.PtrToStructure(data); task.SetResult(PhonebookStructConversions.ConvertPhonebookStorageStruct(info)); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; int ret = Interop.Tapi.Phonebook.GetPhonebookStorage(_handle, type, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to get phonebook storage info, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony"); } return task.Task; } /// /// Gets the max text length and max number length supported by the SIM phone book elementary file. /// /// The different storage types to be selected in the SIM. /// A task containing an instance of PhonebookMetaInfo. /// http://tizen.org/feature/network.telephony /// http://tizen.org/privilege/telephony /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task GetPhonebookMetaInfo(PhonebookType type) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)PhonebookAccessResult.Success) { Log.Error(TapiUtility.LogTag, "Error occurs during getting phone book meta info: " + (PhonebookAccessResult)result); task.SetException(new InvalidOperationException("Error occurs during getting phone book meta info, " + (PhonebookAccessResult)result)); return; } PhonebookMetaInfoStruct info = Marshal.PtrToStructure(data); task.SetResult(PhonebookStructConversions.ConvertPhonebookMetaInfoStruct(info)); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; int ret = Interop.Tapi.Phonebook.GetPhonebookMetaInfo(_handle, type, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to get phonebook meta info, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony"); } return task.Task; } /// /// Gets SIM 3G phonebook supported EFs like ANR, SNE, GRP, EMAIL and the corresponding EFs max text length, number length, and size. /// /// A task containing an instance of PhonebookMetaInfo3G. /// http://tizen.org/feature/network.telephony /// http://tizen.org/privilege/telephony /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task GetPhonebookMetaInfo3G() { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)PhonebookAccessResult.Success) { Log.Error(TapiUtility.LogTag, "Error occurs during getting 3G phone book meta info: " + (PhonebookAccessResult)result); task.SetException(new InvalidOperationException("Error occurs during getting 3G phone book meta info, " + (PhonebookAccessResult)result)); return; } PhonebookMetaInfo3GStruct metaInfo = Marshal.PtrToStructure(data); task.SetResult(PhonebookStructConversions.ConvertPhonebookMetaInfo3GStruct(metaInfo)); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; int ret = Interop.Tapi.Phonebook.GetPhonebookMetaInfo3G(_handle, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to get 3G phonebook meta info, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony"); } return task.Task; } /// /// Reads SIM phone book entry information from the given storage type and index. /// /// The different storage types to be selected in the SIM. /// The index for accessing the SIM data. /// A task containing an instance of PhonebookRecord. /// http://tizen.org/feature/network.telephony /// http://tizen.org/privilege/telephony /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task ReadPhonebookRecord(PhonebookType type, ushort index) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)PhonebookAccessResult.Success) { Log.Error(TapiUtility.LogTag, "Error occurs during reading phone book record: " + (PhonebookAccessResult)result); task.SetException(new InvalidOperationException("Error occurs during reading phone book record, " + (PhonebookAccessResult)result)); return; } PhonebookRecordStruct record = Marshal.PtrToStructure(data); task.SetResult(PhonebookStructConversions.ConvertPhonebookRecordStruct(record)); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; if (index == 0) { throw new ArgumentException("Index should not be zero"); } int ret = Interop.Tapi.Phonebook.ReadPhonebookRecord(_handle, type, index, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to read phonebook record, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony"); } return task.Task; } /// /// Adds or edits SIM phone book record entry information. /// /// The phonebook data to be updated or added. /// A task indicating whether the updation is done or not. /// http://tizen.org/feature/network.telephony /// platform /// http://tizen.org/privilege/telephony.admin /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when record is passed as null. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task UpdatePhonebookRecord(PhonebookRecord record) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)PhonebookAccessResult.Success) { Log.Error(TapiUtility.LogTag, "Error occurs during updation of phone book record: " + (PhonebookAccessResult)result); task.SetException(new InvalidOperationException("Error occurs during updation of phone book record, " + (PhonebookAccessResult)result)); return; } task.SetResult(true); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; if (record == null) { throw new ArgumentNullException("Phonebook record is null"); } if (record.Index == 0) { throw new ArgumentException("Index in phonebook record is zero"); } PhonebookRecordStruct recordStruct = PhonebookClassConversions.ConvertPhonebookrecord(record); int ret = Interop.Tapi.Phonebook.UpdatePhonebookRecord(_handle, ref recordStruct, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to update phonebook record, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin"); } return task.Task; } /// /// Deletes a SIM phonebook record. /// /// The different storage types to be selected in the SIM. /// The index of the record to be deleted. /// A task indicating whether deletion is done or not. /// http://tizen.org/feature/network.telephony /// platform /// http://tizen.org/privilege/telephony.admin /// Thrown when telephony feature is not supported. /// Thrown when privilege access is denied. /// Thrown when it is failed due to invalid parameter. /// Thrown when it is failed due to invalid operation. public Task DeletePhonebookRecord(PhonebookType type, ushort index) { TaskCompletionSource task = new TaskCompletionSource(); IntPtr id = (IntPtr)_requestId++; _callbackMap[id] = (handle, result, data, key) => { Task taskResult = new Task(() => { if (result != (int)PhonebookAccessResult.Success) { Log.Error(TapiUtility.LogTag, "Error occurs during deletion of phone book record: " + (PhonebookAccessResult)result); task.SetException(new InvalidOperationException("Error occurs during deletion of phone book record, " + (PhonebookAccessResult)result)); return; } task.SetResult(true); }); taskResult.Start(); taskResult.Wait(); _callbackMap.Remove(key); }; if (index == 0) { throw new ArgumentException("Index of the record is zero"); } int ret = Interop.Tapi.Phonebook.DeletePhonebookRecord(_handle, type, index, _callbackMap[id], id); if (ret != (int)TapiError.Success) { Log.Error(TapiUtility.LogTag, "Failed to delete phonebook record, Error: " + (TapiError)ret); TapiUtility.ThrowTapiException(ret, _handle, "http://tizen.org/privilege/telephony.admin"); } return task.Task; } } }