Change License under the Apache License.
[platform/core/csapi/telephony.git] / Tizen.Telephony / Tizen.Telephony / Telephony.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.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using static Interop.Telephony;
21
22 namespace Tizen.Telephony
23 {
24     /// <summary>
25     /// Enumeration for the telephony state.
26     /// </summary>
27     public enum State
28     {
29         /// <summary>
30         /// Telephony state is not ready
31         /// </summary>
32         NotReady,
33         /// <summary>
34         /// Telephony state is ready
35         /// </summary>
36         Ready,
37         /// <summary>
38         /// Unavailable
39         /// </summary>
40         Unavailable
41     };
42
43     /// <summary>
44     /// Enumeration for the preferred voice call subscription.
45     /// </summary>
46     public enum CallPreferredVoiceSubsubscription
47     {
48         /// <summary>
49         /// Unknown status
50         /// </summary>
51         Unknown = -1,
52         /// <summary>
53         /// Current network
54         /// </summary>
55         CurrentNetwork = 0,
56         /// <summary>
57         /// ASK Always
58         /// </summary>
59         AskAlways,
60         /// <summary>
61         /// SIM 1
62         /// </summary>
63         Sim1,
64         /// <summary>
65         /// SIM 2
66         /// </summary>
67         Sim2
68     };
69
70     /// <summary>
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.
73     /// </summary>
74     public static class Manager
75     {
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)
81         {
82             StateEventArgs args = new StateEventArgs(state);
83             _stateChanged?.Invoke(null, args);
84         };
85
86         /// <summary>
87         /// Event Handler to be invoked when the telephony state changes.
88         /// </summary>
89         public static event EventHandler<StateEventArgs> StateChanged
90         {
91             add
92             {
93                 if (_stateChanged == null)
94                 {
95                     Interop.Telephony.TelephonyError error = Interop.Telephony.TelephonySetStateChangedCb(stateDelegate, IntPtr.Zero);
96                     if (error != TelephonyError.None)
97                     {
98                         Log.Error(LogTag, "Add StateChanged Failed with Error: " + error);
99                     }
100
101                     else
102                     {
103                         _stateChanged += value;
104                     }
105
106                 }
107             }
108
109             remove
110             {
111                 _stateChanged -= value;
112                 if (_stateChanged == null)
113                 {
114                     Interop.Telephony.TelephonyError error = Interop.Telephony.TelephonyUnsetStateChangedCb(stateDelegate);
115                     if (error != TelephonyError.None)
116                     {
117                         Log.Error(LogTag, "Remove StateChanged Failed with Error: " + error);
118                     }
119                 }
120             }
121         }
122
123         /// <summary>
124         /// Acquires the telephony state value.
125         /// </summary>
126         /// <returns>
127         /// The state value of telephony.
128         /// </returns>
129         public static State CurrentState
130         {
131             get
132             {
133                 State state = State.NotReady;
134                 TelephonyError error = Interop.Telephony.TelephonyGetState(out state);
135                 if (error != TelephonyError.None)
136                 {
137                     Tizen.Log.Error(Interop.Telephony.LogTag, "GetState Failed with Error " + error);
138                     return State.Unavailable;
139                 }
140
141                 return state;
142             }
143         }
144
145         /// <summary>
146         /// Acquires the Number of available handles to use the telephony API.
147         /// </summary>
148         /// <returns>
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.
152         /// </returns>
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
158         /// </exception>
159         public static List<SlotHandle> Init()
160         {
161             //DeInitialize Previous Handles if present
162             if (_isInitialized)
163             {
164                 Deinit();
165             }
166
167             TelephonyError err = Interop.Telephony.TelephonyInit(out _handleList);
168             if (err != TelephonyError.None)
169             {
170                 Exception e = ExceptionFactory.CreateException(err);
171                 // Check if error is Invalid Parameter then hide the error
172                 if (e is ArgumentException)
173                 {
174                     e = new InvalidOperationException("Internal Error Occured");
175                 }
176
177                 throw e;
178             }
179
180             int offset = 0;
181             for (int i = 0; i < _handleList.Count; i++)
182             {
183                 _telephonyHandle.Add(new SlotHandle(Marshal.ReadIntPtr(_handleList.HandleArrayPointer, offset)));
184                 offset += Marshal.SizeOf(_handleList.HandleArrayPointer);
185             }
186
187             _isInitialized = true;
188             //Tizen.Log.Info(Interop.Telephony.LogTag, "Returning the number of sims " + _handleList.Count);
189             return _telephonyHandle;
190         }
191
192         /// <summary>
193         /// Deinitializes the telephony handles.
194         /// </summary>
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
199         /// </exception>
200         public static void Deinit()
201         {
202             TelephonyError error = Interop.Telephony.TelephonyDeinit(ref _handleList);
203             if (error != TelephonyError.None)
204             {
205                 Exception e = ExceptionFactory.CreateException(error);
206                 // Check if error is Invalid Parameter then hide the error
207                 if (e is ArgumentException)
208                 {
209                     e = new InvalidOperationException("Internal Error Occured");
210                 }
211
212                 throw e;
213             }
214
215             _isInitialized = false;
216             _telephonyHandle.Clear();
217         }
218
219         internal static SlotHandle FindHandle(IntPtr handle)
220         {
221             SlotHandle temp = _telephonyHandle[0];
222             foreach (SlotHandle simHandle in _telephonyHandle)
223             {
224                 if (simHandle._handle == handle)
225                 {
226                     temp = simHandle;
227                 }
228             }
229
230             return temp;
231         }
232     }
233 }