Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
40 lines
No EOL
9.6 KiB
JavaScript
40 lines
No EOL
9.6 KiB
JavaScript
;'use strict';var CostControlApp=(function(){var costcontrol,initialized=false,hashFromLastRun,hashFromNotification;var vmanager;function waitForSIMReady(callback){SimManager.requestDataSimIcc(function _onIccId(dataSim){var dataSimIcc=dataSim.icc;var cardState=dataSimIcc.cardState;if(cardState!=='ready'){showNonReadyScreen(cardState);debug('SIM not ready:',cardState);dataSimIcc.oncardstatechange=function(){waitForSIMReady(callback);};}else{hideNotReadyScreen();debug('SIM ready. ICCID:',dataSim.iccId);dataSimIcc.oncardstatechange=undefined;callback&&callback();}},function _errorNoSim(){console.warn('Error when trying to get the ICC, SIM not detected.');LazyLoader.load(['/shared/js/airplane_mode_helper.js'],function(){AirplaneModeHelper.ready(function(){var fakeState=null;if(AirplaneModeHelper.getStatus()==='enabled'){console.warn('The airplaneMode is enabled.');fakeState='airplaneMode';window.addEventListener('airplaneModeDisabled',function _onAirplanemodeDisabled(evt){if(evt.detail&&evt.detail.serviceId==='data'){waitForSIMReady(startApp);}});}
|
|
showNonReadyScreen(fakeState);});});});}
|
|
var nonReadyScreen;function showNonReadyScreen(cardState){if(isApplicationLocalized){realshowNonReadyScreen(cardState);}else{window.addEventListener('localized',function _onlocalized(){window.removeEventListener('localized',_onlocalized);realshowNonReadyScreen(cardState);});}
|
|
function realshowNonReadyScreen(messageId){debug('Showing non-ready screen.');if(!nonReadyScreen){nonReadyScreen=new NonReadyScreen(document.getElementById('non-ready-screen'));}
|
|
nonReadyScreen.updateForState(cardState);vmanager.changeViewTo(nonReadyScreen.id);}}
|
|
function hideNotReadyScreen(status){debug('Hiding non-ready screen.');if(vmanager.getCurrentView()==='non-ready-screen'){vmanager.closeCurrentView();}
|
|
return;}
|
|
var tabmanager,settingsVManager;function _onHashChange(evt){var parser=document.createElement('a');parser.href=evt.oldURL;var oldHash=parser.hash.split('#');parser.href=evt.newURL;var newHash=parser.hash.split('#');if(newHash.length>3){console.error('Cost Control bad URL schema');return;}
|
|
debug('URL schema before normalizing:',newHash);var normalized=false;if(!newHash[1]&&oldHash[1]){newHash[1]=oldHash[1];normalized=true;}
|
|
if(newHash.length===3&&newHash[2]===''){if(oldHash.length===3){newHash[2]=oldHash[2];}else{newHash=newHash.slice(0,2);}
|
|
normalized=true;}
|
|
if(normalized){debug('URL schema after normalization:',newHash);window.location.hash=newHash.join('#');return;}
|
|
if(newHash[1]){tabmanager.changeViewTo(newHash[1]);}
|
|
if(newHash.length<3){vmanager.closeCurrentView();}else{vmanager.changeViewTo(newHash[2],'#tabpanel');}}
|
|
function setupCardHandler(){tabmanager=new ViewManager(['balance-tab','telephony-tab',{id:'datausage-tab',tab:'right'}]);settingsVManager=new ViewManager();window.addEventListener('hashchange',_onHashChange);}
|
|
function loadMessageHandler(){var messageHandlerFrame=document.getElementById('message-handler');var thereIsNextReset=ConfigManager.option('nextReset');var alreadyLoaded=messageHandlerFrame.src.contains('message_handler.html');if(alreadyLoaded&&thereIsNextReset){setNextReset(ConfigManager.option('nextReset'));return;}
|
|
if(thereIsNextReset){window.addEventListener('messagehandlerready',function _setNextReset(){window.removeEventListener('messagehandlerready',_setNextReset);setNextReset(ConfigManager.option('nextReset'));});}
|
|
messageHandlerFrame.src='message_handler.html';}
|
|
function _onDataSimChange(){CostControl.reset();ConfigManager.setConfig(null);SimManager.requestDataSimIcc(startApp);}
|
|
function startApp(callback){if(!ConfigManager.supportCustomizeMode){SimManager.requestDataSimIcc(function(dataSim){ConfigManager.requestSettings(dataSim.iccId,function(settings){if(settings.trackingPeriod==='custom'){ConfigManager.setOption({trackingPeriod:'never'});asyncStorage.getItem('nextResetAlarm',function(id){debug('Current nextResetAlarm',id+'.',id?'Removing.':'');if(id){navigator.mozAlarms.remove(id);}
|
|
asyncStorage.setItem('nextResetAlarm',null,function(){ConfigManager.setOption({nextReset:null});});});}});});}
|
|
if(SimManager.isMultiSim()){window.addEventListener('dataSlotChange',_onDataSimChange);}
|
|
CostControl.getInstance(function _onCostControlReady(instance){if(ConfigManager.option('fte')){startFTE();return;}
|
|
loadMessageHandler();costcontrol=instance;if(!initialized){setupApp(callback);}else{loadSettings();updateUI(callback);}});}
|
|
var isApplicationLocalized=false;window.addEventListener('localized',function _onLocalize(){isApplicationLocalized=true;if(initialized){updateUI();loadSettings();}});function setupApp(callback){setupCardHandler();var settingsButtons=document.querySelectorAll('.settings-button');Array.prototype.forEach.call(settingsButtons,function _eachSettingsButton(button){button.addEventListener('click',function _showSettings(){window.location.hash='##settings-view';});});navigator.mozSetMessageHandler('activity',function _handleActivity(activityRequest){var name=activityRequest.source.name;switch(name){case'costcontrol/balance':window.location.hash='#balance-tab';break;case'costcontrol/telephony':window.location.hash='#telephony-tab';break;case'costcontrol/data_usage':window.location.hash='#datausage-tab';break;}});window.navigator.mozSetMessageHandler('notification',function _onNotification(notification){if(!notification.clicked){return;}
|
|
debug('Notification was clicked!');navigator.mozApps.getSelf().onsuccess=function _onAppReady(evt){var app=evt.target.result;app.launch();var type=notification.data;debug('Notification type:',type);handleNotification(type);};});document.addEventListener('visibilitychange',function _onVisibilityChange(evt){if(!document.hidden&&initialized){waitForSIMReady();}});updateUI(callback);ConfigManager.observe('plantype',updateUI,true);initialized=true;loadSettings();}
|
|
function loadSettings(){window.performance.mark('loadSettingsStart');document.getElementById('settings-view-placeholder').src='settings.html';}
|
|
function handleNotification(type){switch(type){case'topUpError':BalanceTab.topUpWithCode(true);break;case'lowBalance':case'zeroBalance':window.location.hash='#balance-tab';hashFromNotification='#balance-tab';break;case'dataUsage':hashFromNotification='#datausage-tab';tabmanager.changeViewTo('datausage-tab');break;}}
|
|
var currentMode;function updateUI(callback){SimManager.requestDataSimIcc(function(dataSim){ConfigManager.requestSettings(dataSim.iccId,function _onSettings(settings){var mode=ConfigManager.getApplicationMode();var newHash=window.location.hash;var tabs=document.getElementById('tabs');var dataUsageTab=document.getElementById('datausage-tab');debug('App UI mode: ',mode);if(mode!==currentMode){currentMode=mode;if(mode==='DATA_USAGE_ONLY'){tabs.hidden=true;dataUsageTab.classList.add('standalone');newHash='#datausage-tab';}else{dataUsageTab.classList.remove('standalone');tabs.hidden=false;document.getElementById('balance-tab-filter').hidden=(mode!=='PREPAID');document.getElementById('telephony-tab-filter').hidden=(mode!=='POSTPAID');if(!isDataUsageTabShown()){newHash=(mode==='PREPAID')?'#balance-tab#':'#telephony-tab#';}}
|
|
window.location.hash=hashFromNotification||hashFromLastRun||newHash;hashFromLastRun=hashFromNotification=null;requestAnimationFrame(function continueLoading(){document.getElementById('main').classList.remove('non-ready');if(mode==='PREPAID'){if(typeof TelephonyTab!=='undefined'){TelephonyTab.finalize();}
|
|
if(typeof BalanceTab!=='undefined'){BalanceTab.initialize();}}else if(mode==='POSTPAID'){if(typeof BalanceTab!=='undefined'){BalanceTab.finalize();}
|
|
if(typeof TelephonyTab!=='undefined'){TelephonyTab.initialize();}}});}
|
|
(typeof callback==='function')&&callback();});});}
|
|
function isDataUsageTabShown(){return window.location.hash.split('#')[1]==='datausage-tab';}
|
|
function onFteFinished(e){if(e.origin!==Common.COST_CONTROL_APP){return;}
|
|
var type=e.data.type;if(type==='fte_finished'){window.removeEventListener('message',onFteFinished);document.getElementById('splash_section').hidden='true';ConfigManager.requestAll(function(){startApp(Common.closeFTE);});}}
|
|
function startFTE(){window.addEventListener('message',onFteFinished);var mode=ConfigManager.getApplicationMode();Common.startFTE(mode);}
|
|
function initApp(){hashFromLastRun=window.location.hash;window.location.hash='';vmanager=new ViewManager();waitForSIMReady(startApp);}
|
|
return{init:function(){var SCRIPTS_NEEDED=['js/utils/debug.js','js/common.js','js/views/NonReadyScreen.js','js/utils/toolkit.js','js/view_manager.js'];if(!window.navigator.mozMobileConnections||!window.navigator.mozIccManager||!window.navigator.mozNetworkStats){LazyLoader.load(SCRIPTS_NEEDED,function _showError(){vmanager=new ViewManager();showNonReadyScreen(null);});}else{SCRIPTS_NEEDED=['js/sim_manager.js','js/utils/debug.js','shared/js/date_time_helper.js','js/utils/chart.js','js/utils/formatting.js','js/utils/toolkit.js','js/settings/networkUsageAlarm.js','js/common.js','js/costcontrol.js','js/config/config_manager.js','js/views/NonReadyScreen.js','js/view_manager.js'];LazyLoader.load(SCRIPTS_NEEDED,initApp);}
|
|
if(navigator.mozAudioChannelManager){navigator.mozAudioChannelManager.volumeControlChannel='notification';}},reset:function(){costcontrol=null;initialized=false;vmanager=null;tabmanager=null;settingsVManager=null;currentMode=null;isApplicationLocalized=false;window.removeEventListener('dataSlotChange',_onDataSimChange);window.removeEventListener('hashchange',_onHashChange);window.removeEventListener('message',onFteFinished);window.location.hash='';nonReadyScreen=null;},showBalanceTab:function _showBalanceTab(){window.location.hash='#balance-tab';},showDataUsageTab:function _showDataUsageTab(){window.location.hash='#datausage-tab';},showAppDetailView:function _showAppDetailView(){window.location.hash='##appdetail-view';},_onHashChange:_onHashChange};}()); |