You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2012/04/24 02:24:15 UTC
wp7 commit: [CB-338] fixing firing online and offline events
Updated Branches:
refs/heads/master 06dc6357e -> 98f29e826
[CB-338] fixing firing online and offline events
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/commit/98f29e82
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/tree/98f29e82
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/diff/98f29e82
Branch: refs/heads/master
Commit: 98f29e82603332c78f93db1829e6d55e5e78dfce
Parents: 06dc635
Author: filmaj <ma...@gmail.com>
Authored: Mon Apr 23 17:23:59 2012 -0700
Committer: filmaj <ma...@gmail.com>
Committed: Mon Apr 23 17:23:59 2012 -0700
----------------------------------------------------------------------
framework/Cordova/Commands/NetworkStatus.cs | 102 +++++++++++++++-------
1 files changed, 72 insertions(+), 30 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/98f29e82/framework/Cordova/Commands/NetworkStatus.cs
----------------------------------------------------------------------
diff --git a/framework/Cordova/Commands/NetworkStatus.cs b/framework/Cordova/Commands/NetworkStatus.cs
index 4718c7b..851e227 100644
--- a/framework/Cordova/Commands/NetworkStatus.cs
+++ b/framework/Cordova/Commands/NetworkStatus.cs
@@ -13,7 +13,9 @@
*/
using System;
+using System.Diagnostics;
using System.Net;
+using System.Net.NetworkInformation;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
@@ -42,48 +44,88 @@ namespace WP7CordovaClassLib.Cordova.Commands
const string CELL = "cellular";
- public void getConnectionInfo(string empty)
+ public NetworkStatus()
+ : base()
{
- //DeviceNetworkInformation.NetworkAvailabilityChanged += new EventHandler<NetworkNotificationEventArgs>(DeviceNetworkInformation_NetworkAvailabilityChanged);
-
- DispatchCommandResult(new PluginResult(PluginResult.Status.OK, checkConnectionType()));
+ DeviceNetworkInformation.NetworkAvailabilityChanged += new EventHandler<NetworkNotificationEventArgs>(ChangeDetected);
}
- //void DeviceNetworkInformation_NetworkAvailabilityChanged(object sender, NetworkNotificationEventArgs e)
- //{
- // throw new NotImplementedException();
- //}
-
- private string checkConnectionType()
+ public void getConnectionInfo(string empty)
{
+ // Use the GetIsNetworkAvailable method to quickly determine if we have a connection or not
+ // Otherwise, resolving a DNS name with no connection takes a while.
+ if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
+ {
+ // We have to try to resolve a host name to get the specific subtype of network available.
+ // Kind of a shitty API here MSFT
+ DeviceNetworkInformation.ResolveHostNameAsync(
+ new DnsEndPoint("microsoft.com", 80),
+ new NameResolutionCallback(nrr =>
+ {
+ if (nrr.NetworkErrorCode == NetworkError.Success)
+ {
+ updateConnectionType(checkConnectionType(nrr.NetworkInterface.InterfaceSubtype));
+ }
+ else
+ {
+ updateConnectionType(NONE);
+ }
+ }
+ ),
+ null
+ );
+ }
+ else
+ {
+ updateConnectionType(NONE);
+ }
+ }
- if (DeviceNetworkInformation.IsNetworkAvailable)
+ private string checkConnectionType(NetworkInterfaceSubType type)
+ {
+ switch (type)
{
- if (DeviceNetworkInformation.IsWiFiEnabled)
- {
+ case NetworkInterfaceSubType.Cellular_1XRTT: //cell
+ case NetworkInterfaceSubType.Cellular_GPRS: //cell
+ return CELL;
+ case NetworkInterfaceSubType.Cellular_EDGE: //2
+ return CELL_2G;
+ case NetworkInterfaceSubType.Cellular_3G:
+ case NetworkInterfaceSubType.Cellular_EVDO: //3
+ case NetworkInterfaceSubType.Cellular_EVDV: //3
+ case NetworkInterfaceSubType.Cellular_HSPA: //3
+ return CELL_3G;
+ case NetworkInterfaceSubType.WiFi:
return WIFI;
- }
- else
- {
- if (DeviceNetworkInformation.IsCellularDataEnabled)
- {
- // WP7 doesn't let us determine which type of cell data network
- // DeviceNetworkInformation.CellularMobileOperator
- return CELL;
- }
- else
- {
- return UNKNOWN;
- }
- }
+ case NetworkInterfaceSubType.Unknown:
+ case NetworkInterfaceSubType.Desktop_PassThru:
+ default:
+ return UNKNOWN;
}
- else
+ }
+
+ void ChangeDetected(object sender, NetworkNotificationEventArgs e)
+ {
+ switch (e.NotificationType)
{
- return NONE;
+ case NetworkNotificationType.InterfaceConnected:
+ updateConnectionType(checkConnectionType(e.NetworkInterface.InterfaceSubtype));
+ break;
+ case NetworkNotificationType.InterfaceDisconnected:
+ updateConnectionType(NONE);
+ break;
+ default:
+ break;
}
}
-
+ private void updateConnectionType(string type)
+ {
+ // This should also implicitly fire offline/online events as that is handled on the JS side
+ PluginResult result = new PluginResult(PluginResult.Status.OK, type);
+ result.KeepCallback = true;
+ DispatchCommandResult(result);
+ }
}
}