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.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using static Interop.Telephony;
22 namespace Tizen.Telephony
25 /// Enumeration for the telephony states.
30 /// The telephony state is not ready.
34 /// The telephony state is ready.
44 /// Enumeration for the preferred voice call subscription.
46 public enum CallPreferredVoiceSubscription
71 /// This class provides APIs to initialize and deinitialize the framework.
72 /// It also provides APIs to get the SlotHandles, which can then be used to get other Network/Sim/Call/Modem information.
74 public static class Manager
76 internal static List<SlotHandle> _telephonyHandle = new List<SlotHandle>();
77 private static HandleList _handleList;
78 private static bool _isInitialized = false;
79 private static event EventHandler<StateEventArgs> _stateChanged;
80 private static StateChangedCallback stateDelegate = delegate(State state, IntPtr userData)
82 StateEventArgs args = new StateEventArgs(state);
83 _stateChanged?.Invoke(null, args);
87 /// The event handler to be invoked when the telephony state changes.
89 /// <since_tizen> 3 </since_tizen>
90 public static event EventHandler<StateEventArgs> StateChanged
94 if (_stateChanged == null)
96 Interop.Telephony.TelephonyError error = Interop.Telephony.TelephonySetStateChangedCb(stateDelegate, IntPtr.Zero);
97 if (error != TelephonyError.None)
99 Log.Error(LogTag, "Add StateChanged Failed with Error: " + error);
104 _stateChanged += value;
112 _stateChanged -= value;
113 if (_stateChanged == null)
115 Interop.Telephony.TelephonyError error = Interop.Telephony.TelephonyUnsetStateChangedCb(stateDelegate);
116 if (error != TelephonyError.None)
118 Log.Error(LogTag, "Remove StateChanged Failed with Error: " + error);
125 /// Acquires the telephony state value.
127 /// <since_tizen> 3 </since_tizen>
129 /// The state value of telephony.
131 public static State CurrentState
135 State state = State.NotReady;
136 TelephonyError error = Interop.Telephony.TelephonyGetState(out state);
137 if (error != TelephonyError.None)
139 Tizen.Log.Error(Interop.Telephony.LogTag, "GetState Failed with Error " + error);
140 return State.Unavailable;
148 /// Acquires the number of available handles to use the telephony API.
150 /// <since_tizen> 3 </since_tizen>
152 /// A list of telephony handles.
153 /// You will get 2 SlotHandles in case of the dual SIM device.
154 /// Where, SlotHandle at Index '0' represents the primary SIM and Index '1' represents the secondary SIM.
156 /// <feature>http://tizen.org/feature/network.telephony</feature>
157 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
158 /// <exception cref="InvalidOperationException">
159 /// This exception will be generated in the following cases:
160 /// 1. The system is out of memory.
161 /// 2. If the operation is not supported on the device.
162 /// 3. If the operation failed.
164 public static IEnumerable<SlotHandle> Init()
166 //DeInitialize Previous Handles if present
172 TelephonyError err = Interop.Telephony.TelephonyInit(out _handleList);
173 if (err != TelephonyError.None)
175 Exception e = ExceptionFactory.CreateException(err);
176 // Check if error is Invalid Parameter then hide the error
177 if (e is ArgumentException)
179 e = new InvalidOperationException("Internal Error Occured");
186 for (int i = 0; i < _handleList.Count; i++)
188 _telephonyHandle.Add(new SlotHandle(Marshal.ReadIntPtr(_handleList.HandleArrayPointer, offset)));
189 offset += Marshal.SizeOf(_handleList.HandleArrayPointer);
192 _isInitialized = true;
193 //Tizen.Log.Info(Interop.Telephony.LogTag, "Returning the number of sims " + _handleList.Count);
194 return _telephonyHandle;
198 /// Deinitializes the telephony handles.
200 /// <since_tizen> 3 </since_tizen>
201 /// <feature>http://tizen.org/feature/network.telephony</feature>
202 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
203 /// <exception cref="InvalidOperationException">
204 /// This exception can be generated in the following cases:
205 /// 1. If the operation is not supported on the device.
206 /// 2. If the operation failed.
208 public static void Deinit()
210 TelephonyError error = Interop.Telephony.TelephonyDeinit(ref _handleList);
211 if (error != TelephonyError.None)
213 Exception e = ExceptionFactory.CreateException(error);
214 // Check if error is Invalid Parameter then hide the error
215 if (e is ArgumentException)
217 e = new InvalidOperationException("Internal Error Occured");
223 _isInitialized = false;
224 _telephonyHandle.Clear();
227 internal static SlotHandle FindHandle(IntPtr handle)
229 SlotHandle temp = _telephonyHandle[0];
230 foreach (SlotHandle simHandle in _telephonyHandle)
232 if (simHandle._handle == handle)