CB-8384 Network status change support on Windows

This commit is contained in:
sgrebnov 2015-01-30 16:10:59 +03:00
parent d8a714757f
commit 9710603feb

View File

@ -24,11 +24,6 @@
var cordova = require('cordova'); var cordova = require('cordova');
var Connection = require('./Connection'); var Connection = require('./Connection');
module.exports = {
getConnectionInfo:function(win,fail,args)
{
console.log("NetworkStatusProxy::getConnectionInfo");
var winNetConn = Windows.Networking.Connectivity; var winNetConn = Windows.Networking.Connectivity;
var networkInfo = winNetConn.NetworkInformation; var networkInfo = winNetConn.NetworkInformation;
var networkCostInfo = winNetConn.NetworkCostType; var networkCostInfo = winNetConn.NetworkCostType;
@ -36,10 +31,14 @@ module.exports = {
var networkAuthenticationInfo = winNetConn.NetworkAuthenticationType; var networkAuthenticationInfo = winNetConn.NetworkAuthenticationType;
var networkEncryptionInfo = winNetConn.NetworkEncryptionType; var networkEncryptionInfo = winNetConn.NetworkEncryptionType;
var connectionType; function getCurrrentConnectionType() {
var profile = networkInfo.getInternetConnectionProfile();
if (!profile) {
return Connection.NONE;
}
var profile = Windows.Networking.Connectivity.NetworkInformation.getInternetConnectionProfile();
if(profile) {
var conLevel = profile.getNetworkConnectivityLevel(); var conLevel = profile.getNetworkConnectivityLevel();
var interfaceType = profile.networkAdapter.ianaInterfaceType; var interfaceType = profile.networkAdapter.ianaInterfaceType;
@ -47,9 +46,11 @@ module.exports = {
// localAccess (airplane mode as an example) or constrainedInternetAccess mean there is no access to the internet available // localAccess (airplane mode as an example) or constrainedInternetAccess mean there is no access to the internet available
// https://msdn.microsoft.com/library/windows/apps/windows.networking.connectivity.networkconnectivitylevel.aspx // https://msdn.microsoft.com/library/windows/apps/windows.networking.connectivity.networkconnectivitylevel.aspx
if (conLevel != Windows.Networking.Connectivity.NetworkConnectivityLevel.internetAccess) { if (conLevel != Windows.Networking.Connectivity.NetworkConnectivityLevel.internetAccess) {
connectionType = Connection.NONE; return Connection.NONE;
} }
else {
var connectionType;
switch (interfaceType) { switch (interfaceType) {
case 71: case 71:
connectionType = Connection.WIFI; connectionType = Connection.WIFI;
@ -65,26 +66,23 @@ module.exports = {
connectionType = Connection.UNKNOWN; connectionType = Connection.UNKNOWN;
break; break;
} }
}
}
// FYI
//Connection.UNKNOWN 'Unknown connection';
//Connection.ETHERNET 'Ethernet connection';
//Connection.WIFI 'WiFi connection';
//Connection.CELL_2G 'Cell 2G connection';
//Connection.CELL_3G 'Cell 3G connection';
//Connection.CELL_4G 'Cell 4G connection';
//Connection.NONE 'No network connection';
setTimeout(function () { return connectionType;
if (connectionType) {
win(connectionType);
} else {
win(Connection.NONE);
}
},0);
} }
module.exports = {
getConnectionInfo:function(win,fail,args)
{
var reportConnectionInfoOnce = function () {
win(getCurrrentConnectionType(), { keepCallback: true });
}
// report current connection type
setTimeout(reportConnectionInfoOnce, 0);
// start traking future changes
networkInfo.addEventListener("networkstatuschanged", reportConnectionInfoOnce);
}
}; };
require("cordova/exec/proxy").add("NetworkStatus",module.exports); require("cordova/exec/proxy").add("NetworkStatus",module.exports);