B2G/gaia/apps/settings/js/wifi.js
// scan wifi networks and display them in the list
scan wifi networks and display them in the list
scan wifi networks and display them in the list
function scan() {
      if (scanning)
        return;
      if (!gWifiManager.enabled || document.mozHidden) {
        scanning = false;
        gScanPending = true;
        return;
      }
      scanning = true;
      var req = gWifiManager.getNetworks();
      req.onsuccess = function onScanSuccess() {
        var allNetworks = req.result;
        var networks = {};
        for (var i = 0; i < allNetworks.length; ++i) {
          var network = allNetworks[i];
          var key = network.ssid + '+' + network.capabilities.join('+');
          if (!networks[key] || network.connected) {
            networks[key] = network;
          } else {
            if (!networks[key].connected &&
                network.relSignalStrength > networks[key].relSignalStrength)
              networks[key] = network;
          }
        }
        var networkKeys = Object.getOwnPropertyNames(networks);
        clear(false);
        if (networkKeys.length) {
          networkKeys.sort(function(a, b) {
            return networks[b].relSignalStrength -
                networks[a].relSignalStrength;
          });
          for (var i = 0; i < networkKeys.length; i++) {
            var network = networks[networkKeys[i]];
            var listItem = newListItem(network, toggleNetwork);
            if (isConnected(network)) {
              list.insertBefore(listItem, infoItem.nextSibling);
            } else {
              list.insertBefore(listItem, scanItem);
            }
            index[networkKeys[i]] = listItem; 
          }
        } else {
          list.insertBefore(newExplanationItem('noNetworksFound'), scanItem);
        }
        list.dataset.state = 'ready';
        if (autoscan) {
          window.setTimeout(scan, scanRate);
        }
        scanning = false;
      };
      req.onerror = function onScanError(error) {
        scanning = false;
        window.setTimeout(scan, scanRate);
      };
    }
function scan ( ) { if ( scanning ) return ; if ( ! gWifiManager . enabled || document . mozHidden ) { scanning = false ; gScanPending = true ; return ; } scanning = true ; var req = gWifiManager . getNetworks ( ) ; req . onsuccess = function onScanSuccess ( ) { var allNetworks = req . result ; var networks = { } ; for ( var i = 0 ; i < allNetworks . length ; ++ i ) { var network = allNetworks [ i ] ; var key = network . ssid + '+' + network . capabilities . join ( '+' ) ; if ( ! networks [ key ] || network . connected ) { networks [ key ] = network ; } else { if ( ! networks [ key ] . connected && network . relSignalStrength > networks [ key ] . relSignalStrength ) networks [ key ] = network ; } } var networkKeys = Object . getOwnPropertyNames ( networks ) ; clear ( false ) ; if ( networkKeys . length ) { networkKeys . sort ( function ( a , b ) { return networks [ b ] . relSignalStrength - networks [ a ] . relSignalStrength ; } ) ; for ( var i = 0 ; i < networkKeys . length ; i ++ ) { var network = networks [ networkKeys [ i ] ] ; var listItem = newListItem ( network , toggleNetwork ) ; if ( isConnected ( network ) ) { list . insertBefore ( listItem , infoItem . nextSibling ) ; } else { list . insertBefore ( listItem , scanItem ) ; } index [ networkKeys [ i ] ] = listItem ; } } else { list . insertBefore ( newExplanationItem ( 'noNetworksFound' ) , scanItem ) ; } list . dataset . state = 'ready' ; if ( autoscan ) { window . setTimeout ( scan , scanRate ) ; } scanning = false ; } ; req . onerror = function onScanError ( error ) { scanning = false ; window . setTimeout ( scan , scanRate ) ; } ; }
scan wifi networks and display them in the list
scan wifi networks and display them in the list
// stop auto-scanning if wifi disabled or the app is hidden // use ssid + capabilities as a composited key // keep connected network first, or select the highest strength // display network list // sort networks by signal strength // add detected networks // put connected network on top of list // add composited key to index // display a "no networks found" message if necessary // display the "Search Again" button // auto-rescan if requested // always try again.