From a7fcd500de29e2af290f752f5ac6bed9a9c376ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salvador=20de=20la=20Puente=20Gonz=C3=A1lez?= Date: Mon, 28 Jan 2013 04:49:31 -0800 Subject: [PATCH] Merge pull request #7784 from lodr/bug-833780-no-topup-error Bug 833780 - Adding a desktop notification when receiving a top up error Cause tef+ and r+ --- apps/costcontrol/js/app.js | 27 +++++++++++- apps/costcontrol/js/message_handler.js | 44 +++++++++++++++++-- apps/costcontrol/js/views/balance.js | 6 ++- .../locales/costcontrol.en-US.properties | 7 ++- apps/costcontrol/manifest.webapp | 7 +-- apps/costcontrol/message_handler.html | 3 ++ 6 files changed, 81 insertions(+), 13 deletions(-) diff --git a/apps/costcontrol/js/app.js b/apps/costcontrol/js/app.js index 51a2ee03a661..b03d0eac9e8d 100644 --- a/apps/costcontrol/js/app.js +++ b/apps/costcontrol/js/app.js @@ -92,7 +92,7 @@ var CostControlApp = (function() { }); }); - // Handle open sent by the user via the widget + // Handle 'open activity' sent by the user via the widget navigator.mozSetMessageHandler('activity', function _handleActivity(activityRequest) { var name = activityRequest.source.name; @@ -110,12 +110,37 @@ var CostControlApp = (function() { } ); + // When a notification is received + window.navigator.mozSetMessageHandler('notification', + function _onNotification(notification) { + debug('Notification received!'); + navigator.mozApps.getSelf().onsuccess = function _onAppReady(evt) { + var app = evt.target.result; + app.launch(); + + var type = notification.imageURL.split('?')[1]; + debug('Notification type:', type); + handleNotification(type); + }; + } + ); + updateUI(); ConfigManager.observe('plantype', updateUI, true); initialized = true; } + function handleNotification(type) { + // XXX: Probably more types coming. Let's leave this switch and remove the + // comment when more types added. + switch (type) { + case 'topUpError': + BalanceTab.topUpWithCode(true); + break; + } + } + var currentMode; function updateUI() { ConfigManager.requestSettings(function _onSettings(settings) { diff --git a/apps/costcontrol/js/message_handler.js b/apps/costcontrol/js/message_handler.js index 2af3eb62abf7..f1f5c96f8a42 100644 --- a/apps/costcontrol/js/message_handler.js +++ b/apps/costcontrol/js/message_handler.js @@ -30,13 +30,14 @@ } // Close if in standalone mode + var closing; function closeIfProceeds() { debug('Trying to close...'); if (inStandAloneMode()) { - setTimeout(function _close() { + closing = setTimeout(function _close() { window.close(); debug('Closing message handler'); - }, 500); + }, 1000); } } @@ -92,12 +93,40 @@ }); } + function sendIncorrectTopUpNotification(callback) { + // XXX: Hack hiding the message class in the icon URL + // Should use the tag element of the notification once the final spec + // lands: + // See: https://bugzilla.mozilla.org/show_bug.cgi?id=782211 + navigator.mozApps.getSelf().onsuccess = function _onAppReady(evt) { + var app = evt.target.result; + var iconURL = NotificationHelper.getIconURI(app); + + var goToTopUpCode; + if (!inStandAloneMode()) { + goToTopUpCode = function _goToTopUpCode() { + app.launch(); + window.parent.BalanceTab.topUpWithCode(true); + }; + } + + iconURL += '?topUpError'; + NotificationHelper.send(_('topup-incorrectcode-title2'), + _('topup-incorrectcode-message2'), iconURL, + goToTopUpCode); + + if (callback) + callback(); + }; + } + // Register in standalone or for application if (inStandAloneMode() || inApplicationMode()) { debug('Installing handlers'); // When receiving an SMS, recognize and parse window.navigator.mozSetMessageHandler('sms-received', function _onSMS(sms) { + clearTimeout(closing); ConfigManager.requestAll(function _onInfo(configuration, settings) { // Non expected SMS if (configuration.balance.senders.indexOf(sms.sender) === -1 && @@ -128,7 +157,8 @@ description = new RegExp(configuration.topup.incorrect_code_regexp); isError = !!sms.body.match(description); if (!isError) - console.warn('Impossible to parse TopUp confirmation message.'); + console.warn('Impossible to parse TopUp error message.'); + } } @@ -162,6 +192,7 @@ closeIfProceeds(); } ); + } else if (isConfirmation) { // Store SUCCESS for TopIp and sync navigator.mozAlarms.remove(settings.waitingForTopUp); @@ -175,6 +206,7 @@ closeIfProceeds(); } ); + } else if (isError) { // Store ERROR for TopUp and sync settings.errors['INCORRECT_TOPUP_CODE'] = true; @@ -186,7 +218,7 @@ debug('Balance up to date and stored'); debug('Trying to synchronize!'); localStorage['sync'] = 'errors#' + Math.random(); - closeIfProceeds(); + sendIncorrectTopUpNotification(closeIfProceeds); } ); } @@ -195,6 +227,7 @@ // Whan receiving an alarm, differenciate by type and act window.navigator.mozSetMessageHandler('alarm', function _onAlarm(alarm) { + clearTimeout(closing); switch (alarm.data.type) { case 'balanceTimeout': ConfigManager.requestSettings(function _onSettings(settings) { @@ -238,6 +271,7 @@ // Count a new SMS window.navigator.mozSetMessageHandler('sms-sent', function _onSMSSent(sms) { + clearTimeout(closing); ConfigManager.requestSettings(function _onSettings(settings) { debug('SMS sent!'); var manager = window.navigator.mozSms; @@ -257,6 +291,8 @@ // When a call ends window.navigator.mozSetMessageHandler('telephony-call-ended', function _onCall(tcall) { + clearTimeout(closing); + if (tcall.direction !== 'outgoing') return; diff --git a/apps/costcontrol/js/views/balance.js b/apps/costcontrol/js/views/balance.js index a46f5eb3d2ea..310516cc0919 100644 --- a/apps/costcontrol/js/views/balance.js +++ b/apps/costcontrol/js/views/balance.js @@ -144,8 +144,11 @@ var BalanceTab = (function() { } // On tapping Top Up with code - function topUpWithCode() { + function topUpWithCode(lastWasError) { vmanager.changeViewTo('topup-dialog'); + if (lastWasError) + setTopUpMode('incorrect_code'); + topUpCodeInput.focus(); } @@ -348,6 +351,7 @@ var BalanceTab = (function() { } return { + topUpWithCode: topUpWithCode, initialize: setupTab, finalize: finalize }; diff --git a/apps/costcontrol/locales/costcontrol.en-US.properties b/apps/costcontrol/locales/costcontrol.en-US.properties index 33ec3019f426..748a63b62e92 100644 --- a/apps/costcontrol/locales/costcontrol.en-US.properties +++ b/apps/costcontrol/locales/costcontrol.en-US.properties @@ -1,9 +1,8 @@ # cost control -# Note: the space between "Cost" and "Control" is non-break space in Unicode. -# Note: "Cost Control" is the widget name and should not be translated -topup-confirmation-title = Cost Control +# Note: "Usage" is the application name +topup-confirmation-title2 = Usage topup-confirmation-message = Top-up confirmed -topup-incorrectcode-title = Cost Control +topup-incorrectcode-title2 = Usage topup-incorrectcode-message2 = Incorrect code entered. Please try again. updating = Updating diff --git a/apps/costcontrol/manifest.webapp b/apps/costcontrol/manifest.webapp index bd489b5b95af..3af3add257f2 100644 --- a/apps/costcontrol/manifest.webapp +++ b/apps/costcontrol/manifest.webapp @@ -59,8 +59,9 @@ "orientation": "portrait-primary", "messages": [ { "sms-received": "/message_handler.html" }, - { "alarm": "/message_handler.html"}, - { "sms-sent": "/message_handler.html"}, - { "telephony-call-ended": "/message_handler.html"} + { "alarm": "/message_handler.html" }, + { "sms-sent": "/message_handler.html" }, + { "telephony-call-ended": "/message_handler.html" }, + { "notification": "/index.html" } ] } diff --git a/apps/costcontrol/message_handler.html b/apps/costcontrol/message_handler.html index 86e436fe6af0..a0f8fdbafcd8 100644 --- a/apps/costcontrol/message_handler.html +++ b/apps/costcontrol/message_handler.html @@ -3,7 +3,10 @@ Message handler + + +