/* * 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.Collections.Generic; using System.Runtime.InteropServices; using static Interop.Telephony; namespace Tizen.Telephony { /// /// Enumeration for the telephony states. /// public enum State { /// /// The telephony state is not ready. /// NotReady, /// /// The telephony state is ready. /// Ready, /// /// Unavailable. /// Unavailable }; /// /// Enumeration for the preferred voice call subscription. /// public enum CallPreferredVoiceSubscription { /// /// Unknown status. /// Unknown = -1, /// /// Current network. /// CurrentNetwork = 0, /// /// Ask Always. /// AskAlways, /// /// SIM 1. /// Sim1, /// /// SIM 2. /// Sim2 }; /// /// This class provides APIs to initialize and deinitialize the framework. /// It also provides APIs to get the SlotHandles, which can then be used to get other Network/Sim/Call/Modem information. /// public static class Manager { internal static List _telephonyHandle = new List(); private static HandleList _handleList; private static bool _isInitialized = false; private static event EventHandler _stateChanged; private static StateChangedCallback stateDelegate = delegate(State state, IntPtr userData) { StateEventArgs args = new StateEventArgs(state); _stateChanged?.Invoke(null, args); }; /// /// The event handler to be invoked when the telephony state changes. /// /// 3 public static event EventHandler StateChanged { add { if (_stateChanged == null) { Interop.Telephony.TelephonyError error = Interop.Telephony.TelephonySetStateChangedCb(stateDelegate, IntPtr.Zero); if (error != TelephonyError.None) { Log.Error(LogTag, "Add StateChanged Failed with Error: " + error); } else { _stateChanged += value; } } } remove { _stateChanged -= value; if (_stateChanged == null) { Interop.Telephony.TelephonyError error = Interop.Telephony.TelephonyUnsetStateChangedCb(stateDelegate); if (error != TelephonyError.None) { Log.Error(LogTag, "Remove StateChanged Failed with Error: " + error); } } } } /// /// Acquires the telephony state value. /// /// 3 /// /// The state value of telephony. /// public static State CurrentState { get { State state = State.NotReady; TelephonyError error = Interop.Telephony.TelephonyGetState(out state); if (error != TelephonyError.None) { Tizen.Log.Error(Interop.Telephony.LogTag, "GetState Failed with Error " + error); return State.Unavailable; } return state; } } /// /// Acquires the number of available handles to use the telephony API. /// /// 3 /// /// A list of telephony handles. /// You will get 2 SlotHandles in case of the dual SIM device. /// Where, SlotHandle at Index '0' represents the primary SIM and Index '1' represents the secondary SIM. /// /// http://tizen.org/feature/network.telephony /// The required feature is not supported. /// /// This exception will be generated in the following cases: /// 1. The system is out of memory. /// 2. If the operation is not supported on the device. /// 3. If the operation failed. /// public static IEnumerable Init() { //DeInitialize Previous Handles if present if (_isInitialized) { Deinit(); } TelephonyError err = Interop.Telephony.TelephonyInit(out _handleList); if (err != TelephonyError.None) { Exception e = ExceptionFactory.CreateException(err); // Check if error is Invalid Parameter then hide the error if (e is ArgumentException) { e = new InvalidOperationException("Internal Error Occured"); } throw e; } int offset = 0; for (int i = 0; i < _handleList.Count; i++) { _telephonyHandle.Add(new SlotHandle(Marshal.ReadIntPtr(_handleList.HandleArrayPointer, offset))); offset += Marshal.SizeOf(_handleList.HandleArrayPointer); } _isInitialized = true; //Tizen.Log.Info(Interop.Telephony.LogTag, "Returning the number of sims " + _handleList.Count); return _telephonyHandle; } /// /// Deinitializes the telephony handles. /// /// 3 /// http://tizen.org/feature/network.telephony /// The required feature is not supported. /// /// This exception can be generated in the following cases: /// 1. If the operation is not supported on the device. /// 2. If the operation failed. /// public static void Deinit() { TelephonyError error = Interop.Telephony.TelephonyDeinit(ref _handleList); if (error != TelephonyError.None) { Exception e = ExceptionFactory.CreateException(error); // Check if error is Invalid Parameter then hide the error if (e is ArgumentException) { e = new InvalidOperationException("Internal Error Occured"); } throw e; } _isInitialized = false; _telephonyHandle.Clear(); } internal static SlotHandle FindHandle(IntPtr handle) { SlotHandle temp = _telephonyHandle[0]; foreach (SlotHandle simHandle in _telephonyHandle) { if (simHandle._handle == handle) { temp = simHandle; } } return temp; } } }