Source: js/cell_broadcast_system.js

  1. 'use strict';
  2. /* global CarrierInfoNotifier */
  3. /* global MobileOperator */
  4. /* global Service */
  5. (function(exports) {
  6. /**
  7. * CellBroadcastSystem
  8. * @class CellBroadcastSystem
  9. * @requires CarrierInfoNotifier
  10. * @requires MobileOperator
  11. */
  12. function CellBroadcastSystem() {}
  13. CellBroadcastSystem.prototype = {
  14. name: 'CellBroadcastSystem',
  15. /**
  16. * Whether or not the cellbroadcast setting is enabled or disabled.
  17. * @memberof CellBroadcastSystem.prototype
  18. * @type {Array}
  19. */
  20. _settingsDisabled: [],
  21. /**
  22. * The cell broadcast settings key.
  23. * @memberof CellBroadcastSystem.prototype
  24. * @type {String}
  25. */
  26. _settingsKey: 'ril.cellbroadcast.disabled',
  27. /**
  28. * Starts listening for events and settings changes.
  29. * @memberof CellBroadcastSystem.prototype
  30. */
  31. start: function cbs_start() {
  32. var self = this;
  33. if (navigator && navigator.mozCellBroadcast) {
  34. navigator.mozCellBroadcast.onreceived = this.show.bind(this);
  35. }
  36. var settings = window.navigator.mozSettings;
  37. var req = settings.createLock().get(this._settingsKey);
  38. req.onsuccess = function() {
  39. self._settingsDisabled = req.result[self._settingsKey];
  40. };
  41. settings.addObserver(
  42. this._settingsKey, this.settingsChangedHandler.bind(this));
  43. Service.register('show', this);
  44. },
  45. /**
  46. * Called when the cellbroadcast setting is changed.
  47. * @memberof CellBroadcastSystem.prototype
  48. */
  49. settingsChangedHandler: function cbs_settingsChangedHandler(event) {
  50. this._settingsDisabled = event.settingValue;
  51. if (this._hasCBSDisabled()) {
  52. var evt = new CustomEvent('cellbroadcastmsgchanged', { detail: null });
  53. window.dispatchEvent(evt);
  54. }
  55. },
  56. /**
  57. * Shows the cell broadcast notification.
  58. * @memberof CellBroadcastSystem.prototype
  59. */
  60. show: function cbs_show(event) {
  61. var msg = event.message;
  62. var serviceId = msg.serviceId || 0;
  63. var conn = window.navigator.mozMobileConnections[serviceId];
  64. var id = msg.messageId;
  65. var cdmaCategory = msg.cdmaServiceCategory;
  66. // Early return CMAS messsage and let network alert app handle it. Please
  67. // ref http://www.etsi.org/deliver/etsi_ts/123000_123099/123041/
  68. // 11.06.00_60/ts_123041v110600p.pdf, chapter 9.4.1.2.2 Message identifier
  69. // for GSM and http://www.3gpp2.org/public_html/specs/
  70. // C.R1001-G_v1.0_Param_Administration.pdf for CDMA.
  71. // GSM Message id from range 4370 to 4399(1112 hex to 112f hex) and
  72. // CDMA service category from range 4096 to 4351(1000 hex to 10ff hex)
  73. // should be CMAS and network alert will display detail information.
  74. var isGSM = cdmaCategory === null;
  75. var isGSMCmas = isGSM && (id >= 4370 && id < 4400);
  76. var isCDMACmas = !isGSM &&
  77. (cdmaCategory >= 0x1000 && cdmaCategory <= 0x10FF);
  78. if (isGSMCmas || isCDMACmas) {
  79. return;
  80. }
  81. if (conn && conn.voice && conn.voice.network &&
  82. conn.voice.network.mcc === MobileOperator.BRAZIL_MCC &&
  83. id === MobileOperator.BRAZIL_CELLBROADCAST_CHANNEL) {
  84. var evt = new CustomEvent('cellbroadcastmsgchanged',
  85. { detail: msg.body });
  86. window.dispatchEvent(evt);
  87. return;
  88. }
  89. var body = msg.body;
  90. // XXX: 'undefined' test until bug-1021177 lands
  91. if (msg.etws && (!body || (body == 'undefined'))) {
  92. body = navigator.mozL10n.get('cb-etws-warningType-' +
  93. (msg.etws.warningType ? msg.etws.warningType : 'other'));
  94. }
  95. CarrierInfoNotifier.show(body,
  96. navigator.mozL10n.get('cb-channel', { channel: id }));
  97. },
  98. /**
  99. * To make sure there is any CBS pref is disabled
  100. * @memberof CellBroadcastSystem.prototype
  101. */
  102. _hasCBSDisabled: function cbs__getDisabledCBSIndex() {
  103. var index =
  104. this._settingsDisabled.findIndex(disabled => (disabled === true));
  105. return (index >= 0);
  106. }
  107. };
  108. exports.CellBroadcastSystem = CellBroadcastSystem;
  109. }(window));