+//
+// Open Service Platform
+// Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
+//
+// 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.
+//
+/**
+ * @file FTel_NetworkManagerImpl.cpp
+ * @brief This is the implementation file for _NetworkManagerImpl class.
+ */
+
+#include <stdlib.h>
+#include <telephony_network.h>
+#include <FTelITelephonyNetworkEventListener.h>
+#include <FTelITelephonyNetworkSettingListener.h>
+#include <FTelNetworkInfo.h>
+#include <FTelNetworkStatus.h>
+#include <FTelNetworkManager.h>
+#include <FSysSettingInfo.h>
+#include <FBaseUtilStringUtil.h>
+#include <FBaseSysLog.h>
+#include "FApp_AppInfo.h"
+#include "FTel_NetworkManagerImpl.h"
+#include "FTel_NetworkStatusImpl.h"
+#include "FTel_NetworkInfoImpl.h"
+#include "FTel_NetworkManagerEvent.h"
+#include "FTel_NetworkManagerEventArg.h"
+#include "FTel_NetworkSettingEvent.h"
+#include "FTel_NetworkSettingEventArg.h"
+#include "FTel_TelephonyUtility.h"
+#include "FTel_TelephonyIpcProxy.h"
+
+using namespace std;
+using namespace Tizen::System;
+using namespace Tizen::App;
+using namespace Tizen::Base;
+using namespace Tizen::Base::Utility;
+using namespace Tizen::Base::Collection;
+
+
+namespace Tizen { namespace Telephony
+{
+
+_NetworkManagerImpl::_NetworkManagerImpl(void)
+ : __pConnectionHandle(null)
+ , __pNetworkManagerEvent(null)
+ , __pNetworkSettingEvent(null)
+ , __pTelephonyServiceProxy(null)
+ , __pSettingListener(null)
+{
+}
+
+_NetworkManagerImpl::~_NetworkManagerImpl(void)
+{
+ if (__pNetworkManagerEvent != null)
+ {
+ network_info_unset_service_state_changed_cb();
+ network_info_unset_roaming_state_changed_cb();
+ connection_unset_type_changed_cb(__pConnectionHandle.get());
+ }
+}
+
+result
+_NetworkManagerImpl::Construct(ITelephonyNetworkEventListener* pListener)
+{
+ connection_h pConnHandle = null;
+ int err = connection_create(&pConnHandle);
+ SysLog(NID_TEL, "The return value of connection_create() is %d", err);
+ SysTryReturnResult(NID_TEL, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the connection handle.");
+
+ std::unique_ptr<void, _ConnectionDeleter> pConnectionHandle(pConnHandle);
+
+ if (pListener != null)
+ {
+ // Register data service state changed event
+ err = connection_set_type_changed_cb(pConnectionHandle.get(),(connection_type_changed_cb)_NetworkManagerImpl::OnDataServiceStateChangedCallback, this);
+ SysLog(NID_TEL, "The return value of connection_set_type_changed_cb() is 0x%x", err);
+ SysTryReturnResult(NID_TEL, err == 0, E_SYSTEM, "A system error has occurred. Failed to register the callback function to received the data service state changed event.");
+
+ // Register call service state changed event
+ err = network_info_set_service_state_changed_cb((network_info_service_state_changed_cb) _NetworkManagerImpl::OnCallServiceStateChangedCallback, this);
+ SysLog(NID_TEL, "The return value of network_info_set_service_state_changed_cb() is 0x%x", err);
+ SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM,
+ "A system error has occurred. Failed to register the callback function to received the call service state changed event.");
+
+ // Register roaming state changed event
+ err = network_info_set_roaming_state_changed_cb((network_info_roaming_state_changed_cb)_NetworkManagerImpl::OnRoamingStateChangedCallback, this);
+ SysLog(NID_TEL, "The return value of network_info_set_roaming_state_changed_cb() is 0x%x", err);
+ SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM,
+ "A system error has occurred. Failed to register the callback function to received the roaming state changed event.");
+
+ unique_ptr<_NetworkManagerEvent> pNetworkManagerEvent(new (std::nothrow) _NetworkManagerEvent);
+ SysTryReturnResult(NID_TEL, pNetworkManagerEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
+
+ result r = pNetworkManagerEvent->Construct();
+ SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
+ SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to construct of the _NetworkManagerEvent.");
+
+ pNetworkManagerEvent->AddListener(*pListener, true);
+
+ __pNetworkManagerEvent = move(pNetworkManagerEvent);
+
+ }
+
+ __pTelephonyServiceProxy = _TelephonyIpcProxy::GetInstance();
+
+ if (__pTelephonyServiceProxy == null)
+ {
+ SysLog(NID_TEL, "Creating an IPC instance to connect with the Connectivity service daemon has failed.");
+ }
+ else
+ {
+ __pTelephonyServiceProxy->SetNetworkManagerImpl(*this);
+ }
+
+ __pConnectionHandle = move(pConnectionHandle);
+
+ return E_SUCCESS;
+}
+
+result
+_NetworkManagerImpl::GetNetworkStatus(NetworkStatus& networkStatus) const
+{
+ int err = NETWORK_INFO_ERROR_NONE;
+ bool isRoaming = false;
+ bool isDataServiceAvailable = false;
+ bool isCallServiceAvailable = false;
+ connection_cellular_state_e dataServiceState = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
+
+ SysTryReturnResult(NID_TEL, IsNetworkAvailable(), E_NETWORK_UNAVAILABLE, "The operation has failed because the device is in the offline mode.");
+
+ // Get the isRoming
+ err = network_info_is_roaming(&isRoaming);
+ SysLog(NID_TEL, "The return value of network_info_is_roaming() is 0x%x and the roming state is %s", err, isRoaming ? "True" : "False");
+
+ SysTryReturnResult(NID_TEL, err != NETWORK_INFO_ERROR_OUT_OF_SERVICE, E_SERVICE_UNAVAILABLE, "The operation failed because the device is out of the coverage area or in the emergency mode.");
+ SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the roaming status.")
+
+ // Get the isCallServiceAvailable
+ SysTryReturnResult(NID_TEL, IsServiceAvailable(), E_SERVICE_UNAVAILABLE, "The operation failed because the device is out of the coverage area or in the emergency mode.")
+ isCallServiceAvailable = true;
+
+ // Get the isDataServiceAvailable
+ err = connection_get_cellular_state(__pConnectionHandle.get(), &dataServiceState);
+
+ SysLog(NID_TEL, "The return value of connection_get_cellular_state() is 0x%x and the dataServiceState is %d", err, dataServiceState);
+ SysTryReturnResult(NID_TEL, err == CONNECTION_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the data service status.");
+
+ if (dataServiceState == CONNECTION_CELLULAR_STATE_AVAILABLE || dataServiceState == CONNECTION_CELLULAR_STATE_CONNECTED)
+ {
+ isDataServiceAvailable = true;
+ }
+
+ _NetworkStatusImpl::GetInstance(networkStatus)->SetIsRoaming(isRoaming);
+ _NetworkStatusImpl::GetInstance(networkStatus)->SetIsCallServiceAvailable(isCallServiceAvailable);
+ _NetworkStatusImpl::GetInstance(networkStatus)->SetIsDataServiceAvailable(isDataServiceAvailable);
+
+ return E_SUCCESS;
+
+}
+
+result
+_NetworkManagerImpl::GetNetworkInfo(NetworkInfo& networkInfo) const
+{
+ result r = E_SUCCESS;
+
+ SysTryReturnResult(NID_TEL, IsNetworkAvailable(), E_NETWORK_UNAVAILABLE,
+ "The operation has failed because the device is in the offline mode.");
+ SysTryReturnResult(NID_TEL, IsServiceAvailable(), E_SERVICE_UNAVAILABLE,
+ "The operation failed because the device is out of the coverage area or in the emergency mode.");
+
+ int err = NETWORK_INFO_ERROR_NONE;
+ _ApiVersion apiVersion = _AppInfo::GetApiVersion();
+
+ // Get the cell ID
+ int cellId = -1;
+ err = network_info_get_cell_id(&cellId);
+ SysLog(NID_TEL, "The return value of network_info_get_cell_id() is %d and the cellId value is %d", err, cellId);
+ SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the cell ID.");
+
+ // Get the lac
+ int lac = -1;
+ err = network_info_get_lac(&lac);
+ SysLog(NID_TEL, "The return value of network_info_get_lac() is %d and the lac value is %d", err, lac);
+ SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the lac.");
+
+ char* pTemp = null;
+
+ // Get the operator name
+ err = network_info_get_provider_name(&pTemp);
+ SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the plmn.");
+ unique_ptr<char[],_CharDeleter> pOperatorName(pTemp);
+ String operatorName;
+ r = (StringUtil::Utf8ToString(pOperatorName.get(), operatorName));
+ SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
+ SysLog(NID_TEL, "The operatorname is %ls", operatorName.GetPointer());
+
+ // Get the mcc
+ err = network_info_get_mcc(&pTemp);
+ SysLog(NID_TEL, "The return value of network_info_get_mcc() is %d and the mcc value is %s", err, pTemp);
+ SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the mcc.");
+ unique_ptr<char[],_CharDeleter> pMcc(pTemp);
+
+ // Get the mnc
+ err = network_info_get_mnc(&pTemp);
+ SysLog(NID_TEL, "The return value of network_info_get_mnc() is %d and the mnc value is %s", err, pTemp);
+ SysTryReturnResult(NID_TEL, err == NETWORK_INFO_ERROR_NONE, E_SYSTEM, "A system error has occurred. Failed to get the mnc.");
+ unique_ptr<char[],_CharDeleter> pMnc(pTemp);
+
+ String plmn;
+ r = plmn.Append(pMcc.get());
+ SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to set plmn.");
+
+ r = plmn.Append(pMnc.get());
+ SysLog(NID_TEL, "The plmn value is %ls", plmn.GetPointer());
+ SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to set plmn.");
+
+ _NetworkInfoImpl::GetInstance(networkInfo)->SetCellId(cellId);
+ _NetworkInfoImpl::GetInstance(networkInfo)->SetLac(lac);
+ _NetworkInfoImpl::GetInstance(networkInfo)->SetOperatorName(operatorName);
+ _NetworkInfoImpl::GetInstance(networkInfo)->SetMcc(atoi(pMcc.get()));
+ _NetworkInfoImpl::GetInstance(networkInfo)->SetMnc(atoi(pMnc.get()));
+
+ if (apiVersion == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
+ {
+ _NetworkInfoImpl::GetInstance(networkInfo)->SetPlmn(operatorName);
+ }
+ else
+ {
+ _NetworkInfoImpl::GetInstance(networkInfo)->SetPlmn(plmn);
+ }
+
+ return E_SUCCESS;
+}
+
+result
+_NetworkManagerImpl::SetNetworkSettingListener(ITelephonyNetworkSettingListener* pListener)
+{
+ SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "A system error has occurred. IPC instance has not been constructed yet.");
+
+ result r = __pTelephonyServiceProxy->HasSystemPrivilege();
+ SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
+
+ if (__pSettingListener != null)
+ {
+ __pNetworkSettingEvent->RemoveListener(*__pSettingListener);
+ __pSettingListener = null;
+ }
+
+ // Creates event object
+ if (__pNetworkSettingEvent == null && pListener != null)
+ {
+ std::unique_ptr<_NetworkSettingEvent> pNetworkSettingEvent(new (std::nothrow) _NetworkSettingEvent());
+
+ SysTryReturnResult(NID_TEL, pNetworkSettingEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
+
+ r = pNetworkSettingEvent->Construct();
+ SysTryReturnResult(NID_TEL, r == E_SUCCESS, r, "Propagating.");
+
+ __pNetworkSettingEvent = move(pNetworkSettingEvent);
+ }
+
+ // Adds listener
+ if (pListener != null)
+ {
+ __pSettingListener = pListener;
+ r = __pNetworkSettingEvent->AddListener(*__pSettingListener, true);
+ }
+
+ if (r != E_SUCCESS)
+ {
+ __pNetworkSettingEvent.reset(null);
+ SysLogException(NID_TEL, r, "[%s] Propagating.", GetErrorMessage(r));
+ }
+
+ return r;
+}
+
+result
+_NetworkManagerImpl::GetNetworkSelectionMode(void)
+{
+ SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "A system error has occurred. IPC instance has not been constructed yet.");
+
+ result r = __pTelephonyServiceProxy->GetNetworkSelectionMode();
+ SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
+ SysTryReturnResult(NID_TEL, r != E_PRIVILEGE_DENIED, r, "Propagating.");
+ SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_OPERATION_FAILED, "Failed to get selection mode.");
+
+ return r;
+}
+
+result
+_NetworkManagerImpl::SelectNetwork(const NetworkInfo & networkInfo)
+{
+ SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "IPC instance has not been constructed yet.");
+
+ const _NetworkInfoImpl* pNetworkInfoImpl = _NetworkInfoImpl::GetInstance(networkInfo);
+ TelNetworkSystemType_t networkSystemType = _TelephonyUtility::ConvertNetworkType(pNetworkInfoImpl->GetNetworkType());
+
+ result r = __pTelephonyServiceProxy->SelectNetwork(pNetworkInfoImpl->GetPlmn(), (int)networkSystemType);
+ SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
+ SysTryReturnResult(NID_TEL, r != E_PRIVILEGE_DENIED, r, "Propagating.");
+ SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_OPERATION_FAILED, "Failed to select network manually.");
+
+ return r;
+}
+
+result
+_NetworkManagerImpl::SelectNetwork(void)
+{
+ SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "A system error has occurred. IPC instance has not been constructed yet.");
+
+ result r = __pTelephonyServiceProxy->SelectNetwork();
+ SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
+ SysTryReturnResult(NID_TEL, r != E_PRIVILEGE_DENIED, r, "Propagating.");
+ SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_OPERATION_FAILED, "Failed to select network automatically.");
+
+ return r;
+}
+
+result
+_NetworkManagerImpl::SearchNetwork(void)
+{
+ SysTryReturnResult(NID_TEL, __pTelephonyServiceProxy != null, E_SYSTEM, "A system error has occurred. IPC instance has not been constructed yet.");
+
+ result r = __pTelephonyServiceProxy->SearchNetwork();
+ SysTryReturnResult(NID_TEL, r != E_OUT_OF_MEMORY, r, "Propagating.");
+ SysTryReturnResult(NID_TEL, r != E_PRIVILEGE_DENIED, r, "Propagating.");
+ SysTryReturnResult(NID_TEL, r == E_SUCCESS, E_OPERATION_FAILED, "Failed to search network automatic.");
+
+ return r;
+}
+
+void
+_NetworkManagerImpl::OnRoamingStateChangedCallback(bool isRoaming, void* pUserData)
+{
+ SysLog(NID_TEL, "The callback has called and the roaming state is %s", isRoaming ? "True" : "False");
+ _NetworkManagerImpl* pNetworkManagerImpl = static_cast <_NetworkManagerImpl*>(pUserData);
+
+ pNetworkManagerImpl->OnTelephonyNetworkStatusChanged(_NETWORK_EVENT_ROAMING, isRoaming);
+
+ return;
+}
+
+void
+_NetworkManagerImpl::OnCallServiceStateChangedCallback(network_info_service_state_e serviceState, void* pUserData)
+{
+ SysLog(NID_TEL, "The callback has called and the service state is %d", serviceState);
+
+ _NetworkManagerImpl* pNetworkManagerImpl = static_cast <_NetworkManagerImpl*>(pUserData);
+
+ bool isAvailable = false;
+ if (serviceState == NETWORK_INFO_SERVICE_STATE_IN_SERVICE)
+ {
+ isAvailable = true;
+ }
+
+ pNetworkManagerImpl->OnTelephonyNetworkStatusChanged(_NETWORK_EVENT_CALL, isAvailable);
+
+ return;
+}
+
+void
+_NetworkManagerImpl::OnDataServiceStateChangedCallback(connection_type_e type, void* pUserData)
+{
+ if (type != CONNECTION_TYPE_CELLULAR)
+ {
+ SysLog(NID_TEL, "The connection type %d can be ignored", type);
+ return;
+ }
+
+ _NetworkManagerImpl* pNetworkManagerImpl = static_cast <_NetworkManagerImpl*>(pUserData);
+ bool isAvailable = false;
+
+ // Get the isDataServiceAvailable
+ connection_cellular_state_e dataServiceState = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
+ int err = connection_get_cellular_state(pNetworkManagerImpl->__pConnectionHandle.get(), &dataServiceState);
+ SysLog(NID_TEL, "The return value of connection_get_cellular_state() is 0x%x", err);
+ SysTryReturnVoidResult(NID_TEL, err == CONNECTION_ERROR_NONE, E_SYSTEM, "[%s] A system error has occurred. Failed to get the data service status.", GetErrorMessage(E_SYSTEM));
+
+ if (dataServiceState == CONNECTION_CELLULAR_STATE_AVAILABLE || dataServiceState == CONNECTION_CELLULAR_STATE_CONNECTED)
+ {
+ isAvailable = true;
+ }
+
+ pNetworkManagerImpl->OnTelephonyNetworkStatusChanged(_NETWORK_EVENT_DATA, isAvailable);
+
+ return;
+}
+
+void
+_NetworkManagerImpl::OnTelephonyNetworkStatusChanged(_NetworkEventType type, bool data)
+{
+ SysLog(NID_TEL, "The listener has called with %s state", GetStringOfNetworkEventType(type));
+
+ result r = E_SUCCESS;
+ NetworkStatus networkStatus;
+ r = GetNetworkStatus(networkStatus);
+ SysTryReturnVoidResult(NID_TEL, r == E_SUCCESS, r, "[%s] Failed to get network status", GetErrorMessage(r));
+
+ switch (type)
+ {
+ case _NETWORK_EVENT_CALL:
+ _NetworkStatusImpl::GetInstance(networkStatus)->SetIsCallServiceAvailable(data);
+ break;
+
+ case _NETWORK_EVENT_DATA:
+ _NetworkStatusImpl::GetInstance(networkStatus)->SetIsDataServiceAvailable(data);
+ break;
+
+ case _NETWORK_EVENT_ROAMING:
+ _NetworkStatusImpl::GetInstance(networkStatus)->SetIsRoaming(data);
+ break;
+
+ default:
+ break;
+
+ }
+
+ _NetworkManagerEventArg* pEventArg = new (std::nothrow)_NetworkManagerEventArg(networkStatus);
+ SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+ (void)__pNetworkManagerEvent->Fire(*pEventArg);
+}
+
+bool
+_NetworkManagerImpl::IsServiceAvailable(bool checkEmergency /* = false */)
+{
+ bool isAvailable = false;
+ int err = NETWORK_INFO_ERROR_NONE;
+ network_info_service_state_e state;
+
+ err = network_info_get_service_state(&state);
+ SysLog(NID_TEL, "The return value of network_info_get_service_state() is 0x%x and the state is %d and checkEmergency is %s",
+ err, state, checkEmergency ? "True" : "False");
+
+ if (!checkEmergency && state == NETWORK_INFO_SERVICE_STATE_IN_SERVICE)
+ {
+ isAvailable = true;
+ }
+ else if (checkEmergency && (state == NETWORK_INFO_SERVICE_STATE_IN_SERVICE ||
+ state == NETWORK_INFO_SERVICE_STATE_EMERGENCY_ONLY))
+ {
+ isAvailable = true;
+ }
+
+ return isAvailable;
+}
+
+bool
+_NetworkManagerImpl::IsNetworkAvailable(void)
+{
+ bool isFlightModeEnabled = false;
+ result r = SettingInfo::GetValue(L"http://tizen.org/setting/network.flight_mode", isFlightModeEnabled);
+ SysLog(NID_TEL, "isFlightModeEnabled is %s", isFlightModeEnabled ? "True":"False");
+ SysLog(NID_TEL, "The return value of SettingInfo::GetValue() is %s", GetErrorMessage(r));
+
+ if (r != E_SUCCESS)
+ {
+ return false;
+ }
+
+ return !isFlightModeEnabled;
+}
+
+const char*
+_NetworkManagerImpl::GetStringOfNetworkEventType(_NetworkEventType type) const
+{
+ static const char* pStateStr[] =
+ {
+ "CallServiceAvailable",
+ "DataServiceAvailable ",
+ "Roaming"
+ };
+ return pStateStr[type];
+}
+
+_NetworkManagerImpl*
+_NetworkManagerImpl::GetInstance(NetworkManager& networkManager)
+{
+ return networkManager.__pNetworkManagerImpl;
+}
+
+const _NetworkManagerImpl*
+_NetworkManagerImpl::GetInstance(const NetworkManager& networkManager)
+{
+ return networkManager.__pNetworkManagerImpl;
+}
+
+
+void
+_NetworkManagerImpl::OnTelephonyNetworkSelectionModeReceived(bool isManual, result r)
+{
+ SysLog(NID_TEL, "The mode is %s and the result is %s", isManual ? "Manual":"Automatic", GetErrorMessage(r));
+
+ if (__pNetworkSettingEvent != null)
+ {
+ _NetworkSettingEventArg* pEventArg = new (std::nothrow)_NetworkSettingEventArg(_NETWORK_EVENT_GET_SELECTION_MODE, isManual, r);
+ SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+ (void)__pNetworkSettingEvent->Fire(*pEventArg);
+ }
+}
+
+void
+_NetworkManagerImpl::OnTelephonyNetworkSelectionCompleted(result r)
+{
+ SysLog(NID_TEL, "The listener has called with the result %s", GetErrorMessage(r));
+
+ if (__pNetworkSettingEvent != null)
+ {
+ _NetworkSettingEventArg* pEventArg = new (std::nothrow)_NetworkSettingEventArg(_NETWORK_EVENT_SELECT_NETWORK, r);
+ SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+ (void)__pNetworkSettingEvent->Fire(*pEventArg);
+ }
+}
+
+void
+_NetworkManagerImpl::OnTelephonyNetworkSearchCompleted(String message, result r)
+{
+ SysLog(NID_TEL, "The listener has called with the result %s", GetErrorMessage(r));
+
+ if (__pNetworkSettingEvent != null)
+ {
+ IList* pNetworkInfoList = null;
+
+ if (r == E_SUCCESS)
+ {
+ pNetworkInfoList = ParsingMessageN(message);
+
+ if (pNetworkInfoList == null)
+ {
+ SysLog(NID_TEL, "ParsingMessageN() has failed ");
+ r = E_SYSTEM;
+ }
+ }
+
+ _NetworkSettingEventArg* pEventArg = new (std::nothrow)_NetworkSettingEventArg(_NETWORK_EVENT_SEARCH_COMPLETED, pNetworkInfoList, r);
+ SysTryReturnVoidResult(NID_TEL, pEventArg != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+ (void)__pNetworkSettingEvent->Fire(*pEventArg);
+ }
+}
+
+IList*
+_NetworkManagerImpl::ParsingMessageN(const String& message)
+{
+ result r = E_SUCCESS;
+ int currentIndex = 0;
+ String temp;
+
+ NetworkInfo* pNetworkInfo = null;
+
+ int networkInfoCount = 0;
+
+ r = message.SubString(0, 1, temp);
+ SysTryReturn(NID_TEL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));
+
+ temp.Trim();
+
+ r = Integer::Decode(temp, networkInfoCount);
+ SysTryReturn(NID_TEL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has occurred.", GetErrorMessage(E_SYSTEM));
+
+ ArrayList* pNetworkInfoList = null;
+
+ pNetworkInfoList = new (std::nothrow) ArrayList();
+ SysTryReturn(NID_TEL, pNetworkInfoList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+ r = pNetworkInfoList->Construct(networkInfoCount);
+ SysTryReturn(NID_TEL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
+
+ for (int i = 0 ; i < networkInfoCount ; i++)
+ {
+ pNetworkInfo = GenerateNetworkInfoN(message, currentIndex);
+
+ pNetworkInfoList->Add(*pNetworkInfo);
+ }
+
+ return pNetworkInfoList;
+
+}
+
+NetworkInfo*
+_NetworkManagerImpl::GenerateNetworkInfoN(const String& message, int& currentIndex)
+{
+ NetworkInfo* pNetworkInfo = null;
+ _NetworkInfoImpl* pNetworkInfoImpl = null;
+
+ int temp = 0;
+ String tempString;
+
+ pNetworkInfo = new (std::nothrow) NetworkInfo();
+ SysTryReturn(NID_TEL, pNetworkInfo != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+ pNetworkInfoImpl = _NetworkInfoImpl::GetInstance(*pNetworkInfo);
+
+ // PLMN
+ int mcc = -1;
+ int mnc = -1;
+ tempString = MessageWithLengthToString(message, currentIndex);
+ _TelephonyUtility::GetMccMncFromPlmn(tempString, mcc, mnc);
+ pNetworkInfoImpl->SetPlmn(tempString);
+ pNetworkInfoImpl->SetMcc(mcc);
+ pNetworkInfoImpl->SetMnc(mnc);
+ SysLog(NID_TEL, "The plmn value is %ls, the mcc value is %d and the mnc is %d",tempString.GetPointer(), mcc, mnc);
+
+ // Operator Name
+ tempString = MessageWithLengthToString(message, currentIndex);
+ pNetworkInfoImpl->SetOperatorName(tempString);
+ SysLog(NID_TEL, "The Operator Name is %ls", tempString.GetPointer());
+
+ // NetworkType
+ tempString = MessageToString(message, currentIndex);
+ Tizen::Base::Integer::Parse(tempString, temp);
+
+ _NetworkType networkType = _TelephonyUtility::ConvertNetworkType(static_cast<TelNetworkSystemType_t>(temp));
+ pNetworkInfoImpl->SetNetworkType(networkType);
+ SysLog(NID_TEL, "The NetworkType is %d", networkType);
+
+ return pNetworkInfo;
+}
+
+
+String
+_NetworkManagerImpl::MessageToString(const String& message, int& currentIndex)
+{
+ int nextIndex = 0;
+ String tempString;
+ result r = E_SUCCESS;
+
+ r = message.IndexOf(":", currentIndex, nextIndex);
+
+ if (r != E_SUCCESS)
+ {
+ SysLog(NID_TEL, "The result of message.IndexOf is %s", GetErrorMessage(r));
+ }
+
+ currentIndex = nextIndex + 1;
+
+ r = message.IndexOf(":", currentIndex, nextIndex);
+
+ if (r != E_SUCCESS)
+ {
+ SysLog(NID_TEL, "The result of message.IndexOf is %s", GetErrorMessage(r));
+ }
+
+ r = message.SubString(currentIndex, nextIndex-currentIndex, tempString);
+
+ if (r != E_SUCCESS)
+ {
+ SysLog(NID_TEL, "The result of message.SubString is %s", GetErrorMessage(r));
+ }
+
+ return tempString;
+}
+
+String
+_NetworkManagerImpl::MessageWithLengthToString(const String& message, int& currentIndex)
+{
+ // e.g. plmn or network name -> "05Olleh",
+ int nextIndex = 0;
+ String tempString("");
+ result r = E_SUCCESS;
+ int len = 0;
+
+ r = message.IndexOf(":", currentIndex, nextIndex);
+
+ if (r != E_SUCCESS)
+ {
+ SysLog(NID_TEL, "The result of message.IndexOf is %s", GetErrorMessage(r));
+ }
+
+ currentIndex = nextIndex + 1;
+
+ message.SubString(currentIndex, 2, tempString);
+ tempString.Trim();
+ currentIndex += 2;
+ Tizen::Base::Integer::Decode(tempString, static_cast<int&>(len));
+
+ if (len == 0)
+ {
+ tempString = "";
+ }
+ else
+ {
+ r = message.SubString(currentIndex, len, tempString);
+
+ if (r != E_SUCCESS)
+ {
+ SysLog(NID_TEL, "The result of message.IndexOf is %s", GetErrorMessage(r));
+ }
+ }
+
+ currentIndex += len;
+
+ return tempString;
+}
+
+}} // Tizen::Telephony