Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
12 lines
No EOL
6.4 KiB
JavaScript
12 lines
No EOL
6.4 KiB
JavaScript
|
|
define(['require','modules/settings_cache','shared/wifi_helper'],function(require){'use strict';var SettingsCache=require('modules/settings_cache');var WifiHelper=require('shared/wifi_helper');var wifiManager=WifiHelper.getWifiManager();var settings=navigator.mozSettings;var _currentNetwork=wifiManager&&wifiManager.connection&&wifiManager.connection.network;var WifiContext={_wifiEnabledListeners:[],_wifiDisabledListeners:[],_wifiStatusChangeListeners:[],_wifiStatusTextChangeListeners:[],_wifiConnectionInfoUpdateListeners:[],_wifiNetworkForgottenListeners:[],_wifiWrongPasswordListeners:[],_wifiStatusText:{id:null},_macAddress:'',_init:function(){if(!wifiManager){return;}
|
|
settings.createLock().set({'wifi.enabled':wifiManager.enabled});this._bindEvents();this._updateWifi();},_bindEvents:function(){var self=this;var _updateWifi=this._updateWifi.bind(this);var _updateNetworkStatus=this._updateNetworkStatus.bind(this);this._wifiEnabledListeners.push(_updateWifi,_updateNetworkStatus);this._wifiDisabledListeners.push(_updateWifi);this._wifiStatusChangeListeners.push(_updateWifi,_updateNetworkStatus);if(wifiManager){wifiManager.onenabled=function(event){self._wifiEnabled(event);self._wifiStatusTextChange();};wifiManager.ondisabled=function(event){self._wifiDisabled(event);self._wifiStatusTextChange();};wifiManager.onstatuschange=function(event){self._wifiStatusChange.call(self,event);self._wifiStatusTextChange();};wifiManager.onconnectioninfoupdate=self._wifiConnectionInfoUpdate.bind(self);}},_updateWifi:function(event){var self=this;if(!wifiManager){return;}
|
|
if(!this._macAddress&&settings){SettingsCache.getSettings(function(results){self._macAddress=results['deviceinfo.mac'];});}
|
|
if(wifiManager.enabled){this._storeMacAddress();}
|
|
this._updateWifiStatusText(event);},_updateWifiStatusText:function(event){if(wifiManager.enabled){var network=(event&&event.network)?event.network:wifiManager.connection.network;var status=event&&event.status?event.status:wifiManager.connection.status;var networkProp=network?{ssid:network.ssid}:null;this._wifiStatusText={id:'full-status-'+status,args:networkProp};}else{this._wifiStatusText={id:'disabled'};}},_updateNetworkStatus:function(event){if(!_currentNetwork){return;}
|
|
if(event&&event.network&&_currentNetwork.ssid!=event.network.ssid){return;}
|
|
var networkStatus=event?event.status:wifiManager.connection.status;if(networkStatus==='connectingfailed'){var isCurrentNetworkKnown=_currentNetwork.known;delete _currentNetwork.password;this.forgetNetwork(_currentNetwork);if(isCurrentNetworkKnown===false){this._wifiWrongPassword();}}},_wifiEnabled:function(event){var self=this;this._syncWifiEnabled(true,function(){self._wifiEnabledListeners.forEach(function(listener){listener(event);});});},_wifiDisabled:function(event){var self=this;this._syncWifiEnabled(false,function(){self._wifiDisabledListeners.forEach(function(listener){listener(event);});});},_wifiStatusChange:function(event){this._wifiStatusChangeListeners.forEach(function(listener){listener(event);});},_wifiStatusTextChange:function(){this._wifiStatusTextChangeListeners.forEach(function(listener){listener();});},_wifiWrongPassword:function(){this._wifiWrongPasswordListeners.forEach(function(listener){listener();});},_wifiConnectionInfoUpdate:function(event){this._wifiConnectionInfoUpdateListeners.forEach(function(listener){listener(event);});},_wifiNetworkForgotten:function(event){this._wifiNetworkForgottenListeners.forEach(function(listener){listener(event);});},_storeMacAddress:function(){if(!wifiManager){return;}
|
|
if(settings&&wifiManager.macAddress&&wifiManager.macAddress!==this._macAddress&&wifiManager.macAddress!=='undefined'){this._macAddress=wifiManager.macAddress;settings.createLock().set({'deviceinfo.mac':this._macAddress});}},_syncWifiEnabled:function(enabled,callback){SettingsCache.getSettings(function(results){var wifiEnabled=results['wifi.enabled'];if(wifiEnabled!==enabled){settings.createLock().set({'wifi.enabled':enabled});}
|
|
callback();});},_removeEventListener:function(listeners,callback){var index=listeners.indexOf(callback);if(index>=0){listeners.splice(index,1);}},associateNetwork:function(network,cb){cb=cb||function(){};var request=wifiManager.associate(network);var done=function(){if(!request.error){_currentNetwork=network;}
|
|
cb(request.error);};request.onsuccess=done;request.onerror=done;},forgetNetwork:function(network,cb){cb=cb||function(){};var request=wifiManager.forget(network);var done=function(){if(!request.error){_currentNetwork=null;WifiContext._wifiNetworkForgotten({network:network});}
|
|
cb(request.error);};request.onsuccess=done;request.onerror=done;}};WifiContext._init();return{addEventListener:function(eventName,callback){if(eventName==='wifiEnabled'){WifiContext._wifiEnabledListeners.push(callback);}else if(eventName==='wifiDisabled'){WifiContext._wifiDisabledListeners.push(callback);}else if(eventName==='wifiStatusChange'){WifiContext._wifiStatusChangeListeners.push(callback);}else if(eventName==='wifiStatusTextChange'){WifiContext._wifiStatusTextChangeListeners.push(callback);}else if(eventName==='wifiWrongPassword'){WifiContext._wifiWrongPasswordListeners.push(callback);}else if(eventName==='wifiConnectionInfoUpdate'){WifiContext._wifiConnectionInfoUpdateListeners.push(callback);}else if(eventName==='wifiNetworkForgotten'){WifiContext._wifiNetworkForgottenListeners.push(callback);}},removeEventListener:function(eventName,callback){if(eventName==='wifiEnabled'){WifiContext._removeEventListener(WifiContext._wifiEnabledListeners,callback);}else if(eventName==='wifiDisabled'){WifiContext._removeEventListener(WifiContext._wifiDisabledListeners,callback);}else if(eventName==='wifiStatusChange'){WifiContext._removeEventListener(WifiContext._wifiStatusChangeListeners,callback);}else if(eventName==='wifiStatusTextChange'){WifiContext._removeEventListener(WifiContext._wifiStatusTextChangeListeners,callback);}else if(eventName==='wifiWrongPassword'){WifiContext._removeEventListener(WifiContext._wifiWrongPasswordListeners,callback);}else if(eventName==='wifiConnectionInfoUpdate'){WifiContext._removeEventListener(WifiContext._wifiConnectionInfoUpdateListeners,callback);}else if(eventName==='wifiNetworkForgotten'){WifiContext._removeEventListener(WifiContext._wifiNetworkForgottenListeners,callback);}},get wifiStatusText(){return WifiContext._wifiStatusText;},get currentNetwork(){return _currentNetwork;},forgetNetwork:WifiContext.forgetNetwork,associateNetwork:WifiContext.associateNetwork};}); |