Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / 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 CallPreferredVoiceSubscription
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         /// <since_tizen> 3 </since_tizen>
90         public static event EventHandler<StateEventArgs> StateChanged
91         {
92             add
93             {
94                 if (_stateChanged == null)
95                 {
96                     Interop.Telephony.TelephonyError error = Interop.Telephony.TelephonySetStateChangedCb(stateDelegate, IntPtr.Zero);
97                     if (error != TelephonyError.None)
98                     {
99                         Log.Error(LogTag, "Add StateChanged Failed with Error: " + error);
100                     }
101
102                     else
103                     {
104                         _stateChanged += value;
105                     }
106
107                 }
108             }
109
110             remove
111             {
112                 _stateChanged -= value;
113                 if (_stateChanged == null)
114                 {
115                     Interop.Telephony.TelephonyError error = Interop.Telephony.TelephonyUnsetStateChangedCb(stateDelegate);
116                     if (error != TelephonyError.None)
117                     {
118                         Log.Error(LogTag, "Remove StateChanged Failed with Error: " + error);
119                     }
120                 }
121             }
122         }
123
124         /// <summary>
125         /// Acquires the telephony state value.
126         /// </summary>
127         /// <since_tizen> 3 </since_tizen>
128         /// <value>
129         /// The state value of telephony.
130         /// </value>
131         public static State CurrentState
132         {
133             get
134             {
135                 State state = State.NotReady;
136                 TelephonyError error = Interop.Telephony.TelephonyGetState(out state);
137                 if (error != TelephonyError.None)
138                 {
139                     Tizen.Log.Error(Interop.Telephony.LogTag, "GetState Failed with Error " + error);
140                     return State.Unavailable;
141                 }
142
143                 return state;
144             }
145         }
146
147         /// <summary>
148         /// Acquires the Number of available handles to use the telephony API.
149         /// </summary>
150         /// <since_tizen> 3 </since_tizen>
151         /// <returns>
152         /// A List of Telephony handles.
153         /// You will get 2 SlotHandles in case of dual SIM device.
154         /// where,SlotHandle at Index '0' represents Primary SIM and Index '1' represents Secondary SIM.
155         /// </returns>
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 can will be generated in the following cases
160         /// 1. System is out of memory
161         /// 2. If the operation is not supported on device
162         /// 3. If the Operation Failed
163         /// </exception>
164         public static IEnumerable<SlotHandle> Init()
165         {
166             //DeInitialize Previous Handles if present
167             if (_isInitialized)
168             {
169                 Deinit();
170             }
171
172             TelephonyError err = Interop.Telephony.TelephonyInit(out _handleList);
173             if (err != TelephonyError.None)
174             {
175                 Exception e = ExceptionFactory.CreateException(err);
176                 // Check if error is Invalid Parameter then hide the error
177                 if (e is ArgumentException)
178                 {
179                     e = new InvalidOperationException("Internal Error Occured");
180                 }
181
182                 throw e;
183             }
184
185             int offset = 0;
186             for (int i = 0; i < _handleList.Count; i++)
187             {
188                 _telephonyHandle.Add(new SlotHandle(Marshal.ReadIntPtr(_handleList.HandleArrayPointer, offset)));
189                 offset += Marshal.SizeOf(_handleList.HandleArrayPointer);
190             }
191
192             _isInitialized = true;
193             //Tizen.Log.Info(Interop.Telephony.LogTag, "Returning the number of sims " + _handleList.Count);
194             return _telephonyHandle;
195         }
196
197         /// <summary>
198         /// Deinitializes the telephony handles.
199         /// </summary>
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 device
206         /// 2. If the Operation Failed
207         /// </exception>
208         public static void Deinit()
209         {
210             TelephonyError error = Interop.Telephony.TelephonyDeinit(ref _handleList);
211             if (error != TelephonyError.None)
212             {
213                 Exception e = ExceptionFactory.CreateException(error);
214                 // Check if error is Invalid Parameter then hide the error
215                 if (e is ArgumentException)
216                 {
217                     e = new InvalidOperationException("Internal Error Occured");
218                 }
219
220                 throw e;
221             }
222
223             _isInitialized = false;
224             _telephonyHandle.Clear();
225         }
226
227         internal static SlotHandle FindHandle(IntPtr handle)
228         {
229             SlotHandle temp = _telephonyHandle[0];
230             foreach (SlotHandle simHandle in _telephonyHandle)
231             {
232                 if (simHandle._handle == handle)
233                 {
234                     temp = simHandle;
235                 }
236             }
237
238             return temp;
239         }
240     }
241 }