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 #30869 from rexboy7/1179199-notification-iac
Browse files Browse the repository at this point in the history
Bug 1179199 - [Stingray][Notification] Establish IAC with smart-system. r=johnhu, yifan
  • Loading branch information
rexboy7 committed Jul 16, 2015
2 parents f209a2f + 6353137 commit 8bca5aa
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 28 deletions.
37 changes: 34 additions & 3 deletions tv_apps/notification/js/notification_app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
/* globals NotificationApp */
'use strict';

window.addEventListener('iac-notification-message', function(evt) {
(function(exports) {

function NotificationApp() {
window.addEventListener('iac-notification-message', this);

// XXX: Testing purpose. Please remove it once real notification sending
// service is done.
/* jshint nonew: false */
new Notification('Notification test', {
'body': evt.detail
'body': 'test content'
});
});
}

NotificationApp.prototype = {
handleEvent: function (evt) {
switch(evt.type) {
case 'iac-notification-message':
var detail = JSON.parse(evt.detail);

switch(detail.type) {
case 'desktop-notification':
// XXX: Testing purpose. Please remove it once real notification
// sending service is done.
console.log(detail.title);
console.log(detail.text);
break;

case 'desktop-notification-close':
break;
}
}
}
};


exports.NotificationApp = NotificationApp;
})(window);

window.notificationApp = new NotificationApp();
84 changes: 72 additions & 12 deletions tv_apps/smart-system/js/notification_connector.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,81 @@
/* globals NotificationConnector */
/* globals NotificationConnector, Promise */
'use strict';

(function(exports) {

var NotificationConnector = function() {
navigator.mozApps.getSelf().onsuccess = function(evt) {
var app = evt.target.result;
app.connect('notification-message').then(function onConnAcceptet(ports) {
ports.forEach(function(port) {
// XXX: Testing purpose. Please remove it once real notification sending
// service is done.
port.postMessage('hello');
});
}, function onConnRejected(reason) {
console.log('rejected');
window.addEventListener('notification-message', this);
window.addEventListener('desktop-notification-resend', this);
this.connect();
};

NotificationConnector.prototype = {
connect: function() {
var self = this;
if (this._port) {
return Promise.resolve(this._port);
}

return new Promise(function(resolve, reject) {
navigator.mozApps.getSelf().onsuccess = function(evt) {
var app = evt.target.result;
app.connect('notification-message').then(function onAccepted(ports) {
if (ports.length !== 1) {
console.error('notification service: more than one app found?');
reject();
return;
}
self._port = ports[0];
self._port.onmessage = self.onIACMessage.bind(this);
resolve(ports[0]);

}, function onConnRejected(reason) {
console.log('Warning: couldn\'t open notification service.');
reject();
});
};
});
};
},

send: function(detail) {
if (this._port) {
this._port.postMessage(JSON.stringify(detail));
return;
}
this.connect().then(function(port) {
port.postMessage(JSON.stringify(detail));
});
},

handleEvent: function(evt) {
switch(evt.type) {
case 'notification-message':
this.send(evt.detail);
break;

case 'desktop-notification-resend':
this.send({
type: 'desktop-notification-resend',
number: evt.detail.number
});
break;
}
},

onIACMessage: function(message) {
var detail = JSON.parse(message.data);
switch(detail.type) {
case 'close-notification':
console.error('Received close message event:' + detail.id);
break;
case 'click-notification':
console.error('Received click message event' + detail.id);
break;
case 'added-notification':
console.error('Received add message event' + detail.id);
break;
}
}
};

exports.NotificationConnector = NotificationConnector;
Expand Down
27 changes: 14 additions & 13 deletions tv_apps/smart-system/js/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ var NotificationScreen = {
this.removeNotification(detail.id);
break;
}

// Send event for notification connector.
this._sendEvent('notification-message', detail.type);
break;
case 'ftuopen':
this.toaster.removeEventListener('click', this);
Expand All @@ -72,18 +75,14 @@ var NotificationScreen = {

clickNotification: function ns_clickNotification(id) {
// event for gecko
var event = document.createEvent('CustomEvent');
event.initCustomEvent('mozContentNotificationEvent', true, true, {
this._sendEvent('mozContentNotificationEvent', {
type: 'desktop-notification-click',
id: id
});
window.dispatchEvent(event);
// event for gaia
window.dispatchEvent(new CustomEvent('notification-clicked', {
detail: {
id: id
}
}));
this._sendEvent('notification-clicked', {
id: id
});
},

showToast: function ns_showToast(notification) {
Expand Down Expand Up @@ -142,12 +141,10 @@ var NotificationScreen = {
};

// Tell gecko that we already show the notification.
var event = document.createEvent('CustomEvent');
event.initCustomEvent('mozContentNotificationEvent', true, true, {
this._sendEvent('mozContentNotificationEvent', {
type: 'desktop-notification-show',
id: notification.id
});
window.dispatchEvent(event);

// XXX: we still need to know if the notification is silent in the future.
// We turn the screen on if needed in order to let
Expand All @@ -172,11 +169,15 @@ var NotificationScreen = {
},

removeNotification: function ns_removeNotification(notificationId) {
var event = document.createEvent('CustomEvent');
event.initCustomEvent('mozContentNotificationEvent', true, true, {
this._sendEvent('mozContentNotificationEvent', {
type: 'desktop-notification-close',
id: notificationId
});
},

_sendEvent: function ns_sendEvent(name, detail) {
var event = document.createEvent('CustomEvent');
event.initCustomEvent(name, true, true, detail);
window.dispatchEvent(event);
}
};
Expand Down

0 comments on commit 8bca5aa

Please sign in to comment.