2 * Copyright (c) 2011 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.
17 * @file roaming_agent.cpp
18 * @author Pawel Sikorski (p.sikorski@samsung.com)
19 * @author Lukasz Marek (l.marek@samsung.com)
21 * @brief roaming agent
24 #include "roaming_agent.h"
25 #include <PopupInvoker.h>
26 #include <global_logic.h>
27 #include <global_model.h>
28 #include <dpl/singleton_impl.h>
29 #include <dpl/log/log.h>
30 #include <dpl/utils/wrt_global_settings.h>
33 IMPLEMENT_SINGLETON(RoamingAgent)
36 const char* CONNECTING_ROAMING_NETWORK = "Connecting to Roaming Network";
37 const char* CONNECTING_HOME_NETWORK = "Connecting to Home Network";
38 const char* CONNECTION_WARNING =
39 "Your terminal is going online.<br>"
40 "Be aware that data charges might apply!<br>"
41 "Do you want to continue?";
43 const char* KEEP_CONNECTION_WARNING =
44 "Notwork type changed.<br>"
45 "Do you want to keep active connections?";
48 RoamingAgent::RoamingAgent()
50 if (GlobalSettings::TestModeEnabled()) {
53 //setting say if we are using roaming network or home network
55 if (vconf_get_int(VCONFKEY_TELEPHONY_SVC_ROAM, &result) != 0) {
56 LogError("Cannot get current roaming status");
58 bool type = (result == VCONFKEY_TELEPHONY_SVC_ROAM_ON);
59 m_networkType = type ? ROAMING : HOME;
60 LogInfo("Network type is " << (type ? "ROAMING" : "HOME"));
62 //setting says if roaming is enabled at all
63 if (vconf_get_bool(VCONFKEY_SETAPPL_STATE_DATA_ROAMING_BOOL,
65 LogError("Cannot get current network setting");
67 m_roamingNetworkEnabled = result;
68 LogInfo("Roaming is " << (result ? "ENABLED" : "DISABLED"));
71 GlobalModel *globalModel =
72 GlobalLogicSingleton::Instance().GetGlobalModel();
74 //read current home network access setting
75 SaveNetworkAccessMode(HOME, globalModel->HomeNetworkAccess.Get());
76 //read current roaming network access setting
77 SaveNetworkAccessMode(ROAMING, globalModel->RoamingNetworkAccess.Get());
79 LogInfo("Roaming network setting " << m_networkOption.Option(ROAMING));
80 LogInfo("Home network network setting " << m_networkOption.Option(HOME));
82 globalModel->HomeNetworkAccess.AddListener(DPL::MakeDelegate(this,
85 OnHomeNetworkAccessModeChanged));
86 globalModel->RoamingNetworkAccess.AddListener(DPL::MakeDelegate(this,
90 OnRoamingNetworkAccessModeChanged));
92 // current network is roaming or local
93 if (vconf_notify_key_changed(
94 VCONFKEY_SETAPPL_STATE_DATA_ROAMING_BOOL,
95 vConfChagedCallback, this) < 0)
97 LogError("Cannot add vconf callback [" <<
98 VCONFKEY_SETAPPL_STATE_DATA_ROAMING_BOOL << "]");
101 if (vconf_notify_key_changed(
102 VCONFKEY_TELEPHONY_SVC_ROAM,
103 vConfChagedCallback, this) < 0)
105 LogError("Cannot add vconf callback [" <<
106 VCONFKEY_TELEPHONY_SVC_ROAM << "]");
109 if (vconf_notify_key_changed(
110 VCONFKEY_NETWORK_WIFI_STATE,
111 vConfChagedCallback, this) < 0)
113 LogError("Cannot add vconf callback [" <<
114 VCONFKEY_NETWORK_WIFI_STATE << "]");
118 RoamingAgent::~RoamingAgent()
120 if (GlobalSettings::TestModeEnabled()) {
124 GlobalModel *globalModel =
125 GlobalLogicSingleton::Instance().GetGlobalModel();
126 globalModel->HomeNetworkAccess.RemoveListener(DPL::MakeDelegate(this,
130 OnHomeNetworkAccessModeChanged));
131 globalModel->RoamingNetworkAccess.RemoveListener(DPL::MakeDelegate(this,
135 OnRoamingNetworkAccessModeChanged));
138 bool RoamingAgent::CheckAccess()
140 if (GlobalSettings::TestModeEnabled()) {
144 //Don't allow to use network when global phone setting doesn't allow it
145 if (m_networkType == ROAMING && !m_roamingNetworkEnabled) {
146 LogInfo("global phone setting turned off roaming usage");
150 return AskUser(CONNECTION_WARNING);
153 bool RoamingAgent::AskUser(const std::string &label)
155 // If current network uses wifi, pass the roaming check
156 int wifiState = VCONFKEY_NETWORK_WIFI_OFF;
157 if (vconf_get_int(VCONFKEY_NETWORK_WIFI_STATE, &wifiState) < 0) {
158 LogError("Fail to get wifi state");
160 if (VCONFKEY_NETWORK_WIFI_CONNECTED == wifiState) {
161 LogInfo("Current network uses WIFI");
166 NetworkOptionType &option = m_networkOption.Option(m_networkType);
170 LogInfo("Network option: never connect");
173 LogInfo("Network option: connect automatically");
176 LogInfo("Network option: always ask");
177 if (!(m_userAnswer.Answer(m_networkType).IsNull())) {
178 return *m_userAnswer.Answer(m_networkType);
180 //create popup and ask user
182 std::string title(m_networkType == ROAMING ?
183 CONNECTING_ROAMING_NETWORK :
184 CONNECTING_HOME_NETWORK);
186 bool response = Wrt::PopupInvoker().askYesNo(title, label);
188 LogDebug("Answer: " << response);
190 m_userAnswer.Answer(m_networkType) = response;
197 void RoamingAgent::Disconnect()
199 if (m_optionalDisconnectCallback.IsNull()) {
200 LogWarning("No disconnect callback set. Ignoring...");
202 LogInfo("disconnecting connections");
203 (*m_optionalDisconnectCallback)();
207 void RoamingAgent::HandleDisconnection()
209 if (!AskUser(KEEP_CONNECTION_WARNING)) {
214 void RoamingAgent::NetworkAccessModeChanged(NetworkType networkType,
215 GlobalModel::NetworkAccessMode mode)
217 if (GlobalSettings::TestModeEnabled()) {
220 SaveNetworkAccessMode(networkType, mode);
221 HandleDisconnection();
224 void RoamingAgent::vConfChagedCallback(keynode_t *keyNode, void *data)
226 LogInfo("vConfChagedCallback ");
227 RoamingAgent *roamAgent = static_cast<RoamingAgent *>(data);
228 if (GlobalSettings::TestModeEnabled()) {
232 char *key = vconf_keynode_get_name(keyNode);
234 LogWarning("vconf key is null.");
237 std::string keyString = key;
239 // current network is roaming or local
240 if (VCONFKEY_SETAPPL_STATE_DATA_ROAMING_BOOL == keyString) {
241 int value = vconf_keynode_get_bool(keyNode);
243 LogInfo("Network is now " << (value ? "ENABLED" : "DISABLED"));
244 if (roamAgent->m_roamingNetworkEnabled != value) {
245 roamAgent->m_roamingNetworkEnabled = value;
246 roamAgent->HandleDisconnection();
251 // allow data connection in the roaming network
252 if (VCONFKEY_TELEPHONY_SVC_ROAM == keyString) {
253 int isRoaming = VCONFKEY_TELEPHONY_SVC_ROAM_OFF;
254 if(vconf_get_int(VCONFKEY_TELEPHONY_SVC_ROAM, &isRoaming) != 0) {
255 LogPedantic("Cannot get current roaming status");
258 NetworkType networkType =
259 (isRoaming == VCONFKEY_TELEPHONY_SVC_ROAM_ON) ? ROAMING : HOME;
261 if (networkType != roamAgent->m_networkType) {
262 roamAgent->m_networkType = networkType;
263 LogInfo("Network type changed into " <<
264 (isRoaming ? "Roaming" : "Home network"));
265 roamAgent->HandleDisconnection();
272 if (VCONFKEY_NETWORK_WIFI_STATE == keyString) {
273 roamAgent->HandleDisconnection();
276 Assert("Unknown key callback is called");
280 void RoamingAgent::SaveNetworkAccessMode(NetworkType networkType,
281 GlobalModel::NetworkAccessMode mode)
283 LogDebug("SaveNetworkAccessMode");
285 case GlobalModel::NEVER_CONNECT:
286 m_networkOption.Option(networkType) = NEVER_CONNECT;
288 case GlobalModel::CONNECT_AUTOMATICALLY:
289 m_networkOption.Option(networkType) = CONNECT_AUTO;
291 case GlobalModel::ALWAYS_ASK:
292 m_networkOption.Option(networkType) = ALWAYS_ASK;
295 LogWarning("Invalid network access, using default");
296 m_networkOption.Option(networkType) = ALWAYS_ASK;
301 void RoamingAgent::OnHomeNetworkAccessModeChanged(
302 const DPL::Event::PropertyEvent<GlobalModel::NetworkAccessMode> &event)
304 LogInfo("home network access type: " << event.value);
305 NetworkAccessModeChanged(HOME, event.value);
308 void RoamingAgent::OnRoamingNetworkAccessModeChanged(
309 const DPL::Event::PropertyEvent<GlobalModel::NetworkAccessMode> &event)
311 LogInfo("roaming network access type: " << event.value);
312 NetworkAccessModeChanged(ROAMING, event.value);