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 state.
30 /// Telephony state is not ready
34 /// Telephony state is ready
44 /// Enumeration for the preferred voice call subscription.
46 public enum CallPreferredVoiceSubscription
71 /// This Class provides API's to Initialize and Deinitialize the framework
72 /// it also provides API's to get the SlotHandle's 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 /// Event Handler to be invoked when the telephony state changes.
89 public static event EventHandler<StateEventArgs> StateChanged
93 if (_stateChanged == null)
95 Interop.Telephony.TelephonyError error = Interop.Telephony.TelephonySetStateChangedCb(stateDelegate, IntPtr.Zero);
96 if (error != TelephonyError.None)
98 Log.Error(LogTag, "Add StateChanged Failed with Error: " + error);
103 _stateChanged += value;
111 _stateChanged -= value;
112 if (_stateChanged == null)
114 Interop.Telephony.TelephonyError error = Interop.Telephony.TelephonyUnsetStateChangedCb(stateDelegate);
115 if (error != TelephonyError.None)
117 Log.Error(LogTag, "Remove StateChanged Failed with Error: " + error);
124 /// Acquires the telephony state value.
127 /// The state value of telephony.
129 public static State CurrentState
133 State state = State.NotReady;
134 TelephonyError error = Interop.Telephony.TelephonyGetState(out state);
135 if (error != TelephonyError.None)
137 Tizen.Log.Error(Interop.Telephony.LogTag, "GetState Failed with Error " + error);
138 return State.Unavailable;
146 /// Acquires the Number of available handles to use the telephony API.
149 /// A List of Telephony handles.
150 /// You will get 2 SlotHandles in case of dual SIM device.
151 /// where,SlotHandle at Index '0' represents Primary SIM and Index '1' represents Secondary SIM.
153 /// <exception cref="InvalidOperationException">
154 /// This Exception can will be generated in the following cases
155 /// 1. System is out of memory
156 /// 2. If the operation is not supported on device
157 /// 3. If the Operation Failed
159 public static IEnumerable<SlotHandle> Init()
161 //DeInitialize Previous Handles if present
167 TelephonyError err = Interop.Telephony.TelephonyInit(out _handleList);
168 if (err != TelephonyError.None)
170 Exception e = ExceptionFactory.CreateException(err);
171 // Check if error is Invalid Parameter then hide the error
172 if (e is ArgumentException)
174 e = new InvalidOperationException("Internal Error Occured");
181 for (int i = 0; i < _handleList.Count; i++)
183 _telephonyHandle.Add(new SlotHandle(Marshal.ReadIntPtr(_handleList.HandleArrayPointer, offset)));
184 offset += Marshal.SizeOf(_handleList.HandleArrayPointer);
187 _isInitialized = true;
188 //Tizen.Log.Info(Interop.Telephony.LogTag, "Returning the number of sims " + _handleList.Count);
189 return _telephonyHandle;
193 /// Deinitializes the telephony handles.
195 /// <exception cref="InvalidOperationException">
196 /// This Exception can be generated in the following cases
197 /// 1. If the operation is not supported on device
198 /// 2. If the Operation Failed
200 public static void Deinit()
202 TelephonyError error = Interop.Telephony.TelephonyDeinit(ref _handleList);
203 if (error != TelephonyError.None)
205 Exception e = ExceptionFactory.CreateException(error);
206 // Check if error is Invalid Parameter then hide the error
207 if (e is ArgumentException)
209 e = new InvalidOperationException("Internal Error Occured");
215 _isInitialized = false;
216 _telephonyHandle.Clear();
219 internal static SlotHandle FindHandle(IntPtr handle)
221 SlotHandle temp = _telephonyHandle[0];
222 foreach (SlotHandle simHandle in _telephonyHandle)
224 if (simHandle._handle == handle)