mobile/android/modules/Notifications.jsm

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 "use strict"
michael@0 5
michael@0 6 let Cc = Components.classes;
michael@0 7 let Ci = Components.interfaces;
michael@0 8
michael@0 9 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 10
michael@0 11 this.EXPORTED_SYMBOLS = ["Notifications"];
michael@0 12
michael@0 13 function log(msg) {
michael@0 14 // Services.console.logStringMessage(msg);
michael@0 15 }
michael@0 16
michael@0 17 var _notificationsMap = {};
michael@0 18
michael@0 19 function Notification(aId, aOptions) {
michael@0 20 this._id = aId;
michael@0 21 this._when = (new Date).getTime();
michael@0 22 this.fillWithOptions(aOptions);
michael@0 23 }
michael@0 24
michael@0 25 Notification.prototype = {
michael@0 26 fillWithOptions: function(aOptions) {
michael@0 27 if ("icon" in aOptions && aOptions.icon != null)
michael@0 28 this._icon = aOptions.icon;
michael@0 29 else
michael@0 30 throw "Notification icon is mandatory";
michael@0 31
michael@0 32 if ("title" in aOptions && aOptions.title != null)
michael@0 33 this._title = aOptions.title;
michael@0 34 else
michael@0 35 throw "Notification title is mandatory";
michael@0 36
michael@0 37 if ("message" in aOptions && aOptions.message != null)
michael@0 38 this._message = aOptions.message;
michael@0 39 else
michael@0 40 this._message = null;
michael@0 41
michael@0 42 if ("priority" in aOptions && aOptions.priority != null)
michael@0 43 this._priority = aOptions.priority;
michael@0 44
michael@0 45 if ("buttons" in aOptions && aOptions.buttons != null) {
michael@0 46 if (aOptions.buttons.length > 3)
michael@0 47 throw "Too many buttons provided. The max number is 3";
michael@0 48
michael@0 49 this._buttons = {};
michael@0 50 for (let i = 0; i < aOptions.buttons.length; i++) {
michael@0 51 let button_id = aOptions.buttons[i].buttonId;
michael@0 52 this._buttons[button_id] = aOptions.buttons[i];
michael@0 53 }
michael@0 54 } else {
michael@0 55 this._buttons = null;
michael@0 56 }
michael@0 57
michael@0 58 if ("ongoing" in aOptions && aOptions.ongoing != null)
michael@0 59 this._ongoing = aOptions.ongoing;
michael@0 60 else
michael@0 61 this._ongoing = false;
michael@0 62
michael@0 63 if ("progress" in aOptions && aOptions.progress != null)
michael@0 64 this._progress = aOptions.progress;
michael@0 65 else
michael@0 66 this._progress = null;
michael@0 67
michael@0 68 if ("onCancel" in aOptions && aOptions.onCancel != null)
michael@0 69 this._onCancel = aOptions.onCancel;
michael@0 70 else
michael@0 71 this._onCancel = null;
michael@0 72
michael@0 73 if ("onClick" in aOptions && aOptions.onClick != null)
michael@0 74 this._onClick = aOptions.onClick;
michael@0 75 else
michael@0 76 this._onClick = null;
michael@0 77
michael@0 78 if ("cookie" in aOptions && aOptions.cookie != null)
michael@0 79 this._cookie = aOptions.cookie;
michael@0 80 else
michael@0 81 this._cookie = null;
michael@0 82
michael@0 83 if ("persistent" in aOptions && aOptions.persistent != null)
michael@0 84 this._persistent = aOptions.persistent;
michael@0 85 else
michael@0 86 this._persistent = false;
michael@0 87 },
michael@0 88
michael@0 89 show: function() {
michael@0 90 let msg = {
michael@0 91 type: "Notification:Show",
michael@0 92 id: this._id,
michael@0 93 title: this._title,
michael@0 94 smallIcon: this._icon,
michael@0 95 ongoing: this._ongoing,
michael@0 96 when: this._when,
michael@0 97 persistent: this._persistent
michael@0 98 };
michael@0 99
michael@0 100 if (this._message)
michael@0 101 msg.text = this._message;
michael@0 102
michael@0 103 if (this._progress) {
michael@0 104 msg.progress_value = this._progress;
michael@0 105 msg.progress_max = 100;
michael@0 106 msg.progress_indeterminate = false;
michael@0 107 } else if (Number.isNaN(this._progress)) {
michael@0 108 msg.progress_value = 0;
michael@0 109 msg.progress_max = 0;
michael@0 110 msg.progress_indeterminate = true;
michael@0 111 }
michael@0 112
michael@0 113 if (this._priority)
michael@0 114 msg.priority = this._priority;
michael@0 115
michael@0 116 if (this._buttons) {
michael@0 117 msg.actions = [];
michael@0 118 let buttonName;
michael@0 119 for (buttonName in this._buttons) {
michael@0 120 let button = this._buttons[buttonName];
michael@0 121 let obj = {
michael@0 122 buttonId: button.buttonId,
michael@0 123 title : button.title,
michael@0 124 icon : button.icon
michael@0 125 };
michael@0 126 msg.actions.push(obj);
michael@0 127 }
michael@0 128 }
michael@0 129
michael@0 130 if (this._light)
michael@0 131 msg.light = this._light;
michael@0 132
michael@0 133 Services.androidBridge.handleGeckoMessage(msg);
michael@0 134 return this;
michael@0 135 },
michael@0 136
michael@0 137 cancel: function() {
michael@0 138 let msg = {
michael@0 139 type: "Notification:Hide",
michael@0 140 id: this._id
michael@0 141 };
michael@0 142 Services.androidBridge.handleGeckoMessage(msg);
michael@0 143 }
michael@0 144 }
michael@0 145
michael@0 146 var Notifications = {
michael@0 147 _initObserver: function() {
michael@0 148 if (!this._observerAdded) {
michael@0 149 Services.obs.addObserver(this, "Notification:Event", true);
michael@0 150 this._observerAdded = true;
michael@0 151 }
michael@0 152 },
michael@0 153
michael@0 154 get idService() {
michael@0 155 delete this.idService;
michael@0 156 return this.idService = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
michael@0 157 },
michael@0 158
michael@0 159 create: function notif_notify(aOptions) {
michael@0 160 this._initObserver();
michael@0 161 let id = this.idService.generateUUID().toString();
michael@0 162 let notification = new Notification(id, aOptions);
michael@0 163 _notificationsMap[id] = notification;
michael@0 164 notification.show();
michael@0 165 return id;
michael@0 166 },
michael@0 167
michael@0 168 update: function notif_update(aId, aOptions) {
michael@0 169 let notification = _notificationsMap[aId];
michael@0 170 if (!notification)
michael@0 171 throw "Unknown notification id";
michael@0 172 notification.fillWithOptions(aOptions);
michael@0 173 notification.show();
michael@0 174 },
michael@0 175
michael@0 176 cancel: function notif_cancel(aId) {
michael@0 177 let notification = _notificationsMap[aId];
michael@0 178 if (notification)
michael@0 179 notification.cancel();
michael@0 180 },
michael@0 181
michael@0 182 observe: function notif_observe(aSubject, aTopic, aData) {
michael@0 183 let data = JSON.parse(aData);
michael@0 184 let id = data.id;
michael@0 185 let notification = _notificationsMap[id];
michael@0 186 if (!notification) {
michael@0 187 Services.console.logStringMessage("Notifications.jsm observe: received unknown event id " + id);
michael@0 188 return;
michael@0 189 }
michael@0 190
michael@0 191 switch (data.eventType) {
michael@0 192 case "notification-clicked":
michael@0 193 if (notification._onClick)
michael@0 194 notification._onClick(id, notification._cookie);
michael@0 195 break;
michael@0 196 case "notification-button-clicked": {
michael@0 197 if (!notification._buttons) {
michael@0 198 Services.console.logStringMessage("Notifications.jsm: received button clicked event but no buttons are available");
michael@0 199 break;
michael@0 200 }
michael@0 201
michael@0 202 let button = notification._buttons[data.buttonId];
michael@0 203 if (button)
michael@0 204 button.onClicked(id, notification._cookie);
michael@0 205 }
michael@0 206 break;
michael@0 207 case "notification-cleared":
michael@0 208 case "notification-closed":
michael@0 209 if (notification._onCancel)
michael@0 210 notification._onCancel(id, notification._cookie);
michael@0 211 delete _notificationsMap[id]; // since the notification was dismissed, we no longer need to hold a reference.
michael@0 212 break;
michael@0 213 }
michael@0 214 },
michael@0 215
michael@0 216 QueryInterface: function (aIID) {
michael@0 217 if (!aIID.equals(Ci.nsISupports) &&
michael@0 218 !aIID.equals(Ci.nsIObserver) &&
michael@0 219 !aIID.equals(Ci.nsISupportsWeakReference))
michael@0 220 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 221 return this;
michael@0 222 }
michael@0 223 }

mercurial