Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7784 from lodr/bug-833780-no-topup-error
Browse files Browse the repository at this point in the history
Bug 833780 - Adding a desktop notification when receiving a top up error
Cause tef+ and r+
  • Loading branch information
delapuente authored and jhford committed Jan 28, 2013
1 parent f69840d commit a7fcd50
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
27 changes: 26 additions & 1 deletion apps/costcontrol/js/app.js
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
44 changes: 40 additions & 4 deletions apps/costcontrol/js/message_handler.js
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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.');

}

}
Expand Down Expand Up @@ -162,6 +192,7 @@
closeIfProceeds();
}
);

} else if (isConfirmation) {
// Store SUCCESS for TopIp and sync
navigator.mozAlarms.remove(settings.waitingForTopUp);
Expand All @@ -175,6 +206,7 @@
closeIfProceeds();
}
);

} else if (isError) {
// Store ERROR for TopUp and sync
settings.errors['INCORRECT_TOPUP_CODE'] = true;
Expand All @@ -186,7 +218,7 @@
debug('Balance up to date and stored');
debug('Trying to synchronize!');
localStorage['sync'] = 'errors#' + Math.random();
closeIfProceeds();
sendIncorrectTopUpNotification(closeIfProceeds);
}
);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -257,6 +291,8 @@
// When a call ends
window.navigator.mozSetMessageHandler('telephony-call-ended',
function _onCall(tcall) {
clearTimeout(closing);

if (tcall.direction !== 'outgoing')
return;

Expand Down
6 changes: 5 additions & 1 deletion apps/costcontrol/js/views/balance.js
Expand Up @@ -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();
}

Expand Down Expand Up @@ -348,6 +351,7 @@ var BalanceTab = (function() {
}

return {
topUpWithCode: topUpWithCode,
initialize: setupTab,
finalize: finalize
};
Expand Down
7 changes: 3 additions & 4 deletions 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
Expand Down
7 changes: 4 additions & 3 deletions apps/costcontrol/manifest.webapp
Expand Up @@ -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" }
]
}
3 changes: 3 additions & 0 deletions apps/costcontrol/message_handler.html
Expand Up @@ -3,7 +3,10 @@
<head>
<meta charset="utf-8">
<title>Message handler</title>
<link rel="resource" type="application/l10n" href="locales/locales.ini">
<script type="text/javascript" src="shared/js/l10n.js"></script>
<script type="text/javascript" src="shared/js/async_storage.js"></script>
<script type="text/javascript" src="shared/js/notification_helper.js"></script>
<script type="text/javascript" src="js/utils/debug.js"></script>
<script type="text/javascript" src="js/utils/toolkit.js"></script>
<script type="text/javascript" src="js/common.js"></script>
Expand Down

0 comments on commit a7fcd50

Please sign in to comment.