// Invoked when device connects to a network void OnNetworkConnected(object sender, EventArgs e) { if (activePage != null) { Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType net = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType; MethodResult r = new MethodResult { Code = (int)NetworkManager.Instance.GetActiveNetwork(), Result = net.ToString() }; activePage.InvokeJavascript("__mowbly__.Network", "onConnect", r); } }
//no need to lock the variables if We do everithing in the completed event handler void networkWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { DetectOnlineStatus(); if ((detailedMode) || (net != (Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType)e.Result)) { //there is no need to get events all the time, just when really changing something or the DetailedMode is true if (net != (Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType)e.Result) RaiseNotify(OnLostNetworkType, new NetworkDetectorEventArgs(online, net)); net = (Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType)e.Result; Debug.WriteLine(" New NetType: " + net.ToString()); if (net == Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Ethernet) Debug.WriteLine("!!!!! Zune is Connected"); switch (net) { case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Ethernet: if (!isZuneConnected) { isZuneConnected = true; RaiseNotify(OnZuneConnected, new NetworkDetectorEventArgs(online, net)); } RaiseNotify(OnConnectedEthernet, new NetworkDetectorEventArgs(online, net)); break; case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Wireless80211: if (isZuneConnected) { isZuneConnected = false; RaiseNotify(OnZuneDisconnected, new NetworkDetectorEventArgs(online, net)); } RaiseNotify(OnConnectedWifi, new NetworkDetectorEventArgs(online, net)); break; case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.MobileBroadbandCdma: if (isZuneConnected) { isZuneConnected = false; RaiseNotify(OnZuneDisconnected, new NetworkDetectorEventArgs(online, net)); } RaiseNotify(OnConnectedBroadbandCdma, new NetworkDetectorEventArgs(online, net)); break; case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.MobileBroadbandGsm: if (isZuneConnected) { isZuneConnected = false; RaiseNotify(OnZuneDisconnected, new NetworkDetectorEventArgs(online, net)); } RaiseNotify(OnConnectedBroadbandGsm, new NetworkDetectorEventArgs(online, net)); break; case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.None: if (!online) { if (isZuneConnected) { /*if we lost all network connection and the Zune was present before, then we lost the Zune too. Normally when Zune is present and then we got a None NetType, the PC just lost the internet connection but not the Zune sync*/ isZuneConnected = false; RaiseNotify(OnZuneDisconnected, new NetworkDetectorEventArgs(online, net)); } } RaiseNotify(OnConnectedNone, new NetworkDetectorEventArgs(online, net)); break; case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.AsymmetricDsl: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Atm: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.BasicIsdn: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Ethernet3Megabit: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.FastEthernetFx: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.FastEthernetT: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Fddi: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.GenericModem: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.GigabitEthernet: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.HighPerformanceSerialBus: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.IPOverAtm: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Isdn: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Loopback: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.MultiRateSymmetricDsl: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Ppp: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.PrimaryIsdn: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.RateAdaptDsl: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Slip: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.SymmetricDsl: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.TokenRing: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Tunnel: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Unknown: case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.VeryHighSpeedDsl: default://theoretically we can't get here but better be prepared RaiseNotify(OnConnectedOther, new NetworkDetectorEventArgs(online, net)); break; } } for (int i = 0; i < requestQueue.Count; i++) { if (requestQueue.Peek() < System.DateTime.Now.Ticks) requestQueue.Dequeue(); //the requests before this moment just got answered //if other requests are coming right after this, they will be served inside the next networkWorker_RunWorkerCompleted } if (requestQueue.Count == 0) updateTimer.Stop(); if (IsInstantRequestPresent) //the user requested a single poll { RaiseNotify(OnAsyncGetNetworkTypeCompleted, new NetworkDetectorEventArgs(online, net)); IsInstantRequestPresent = false; } requestStatus = NetworkTypeRequestStatus.Ended; Debug.WriteLine("<<<<< GetNetType ended " + System.DateTime.Now.ToString("T") + " <<<<<"); }