local-notification-util.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (c) 2013-2015 by appPlant UG. All rights reserved.
  3. *
  4. * @APPPLANT_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apache License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPPLANT_LICENSE_HEADER_END@
  22. */
  23. var exec = require('cordova/exec'),
  24. channel = require('cordova/channel');
  25. /***********
  26. * MEMBERS *
  27. ***********/
  28. // Default values
  29. exports._defaults = {
  30. text: '',
  31. title: '',
  32. sound: 'res://platform_default',
  33. badge: 0,
  34. id: "0",
  35. data: undefined,
  36. every: undefined,
  37. at: undefined
  38. };
  39. // listener
  40. exports._listener = {};
  41. /********
  42. * UTIL *
  43. ********/
  44. /**
  45. * Merge platform specific properties into the default ones.
  46. *
  47. * @return {Object}
  48. * The default properties for the platform
  49. */
  50. exports.applyPlatformSpecificOptions = function () {
  51. var defaults = this._defaults;
  52. switch (device.platform) {
  53. case 'Android':
  54. defaults.icon = 'res://icon';
  55. defaults.smallIcon = 'res://ic_popup_reminder';
  56. defaults.ongoing = false;
  57. defaults.autoClear = true;
  58. defaults.led = 'FFFFFF';
  59. break;
  60. }
  61. return defaults;
  62. };
  63. /**
  64. * Merge custom properties with the default values.
  65. *
  66. * @param {Object} options
  67. * Set of custom values
  68. *
  69. * @retrun {Object}
  70. * The merged property list
  71. */
  72. exports.mergeWithDefaults = function (options) {
  73. var defaults = this.getDefaults();
  74. options.at = this.getValueFor(options, 'at', 'firstAt', 'date');
  75. options.text = this.getValueFor(options, 'text', 'message');
  76. options.data = this.getValueFor(options, 'data', 'json');
  77. if (defaults.hasOwnProperty('autoClear')) {
  78. options.autoClear = this.getValueFor(options, 'autoClear', 'autoCancel');
  79. }
  80. if (options.autoClear !== true && options.ongoing) {
  81. options.autoClear = false;
  82. }
  83. if (options.at === undefined || options.at === null) {
  84. options.at = new Date();
  85. }
  86. for (var key in defaults) {
  87. if (options[key] === null || options[key] === undefined) {
  88. if (options.hasOwnProperty(key) && ['data','sound'].indexOf(key) > -1) {
  89. options[key] = undefined;
  90. } else {
  91. options[key] = defaults[key];
  92. }
  93. }
  94. }
  95. for (key in options) {
  96. if (!defaults.hasOwnProperty(key)) {
  97. delete options[key];
  98. console.warn('Unknown property: ' + key);
  99. }
  100. }
  101. return options;
  102. };
  103. /**
  104. * Convert the passed values to their required type.
  105. *
  106. * @param {Object} options
  107. * Set of custom values
  108. *
  109. * @retrun {Object}
  110. * The converted property list
  111. */
  112. exports.convertProperties = function (options) {
  113. if (options.id) {
  114. if (isNaN(options.id)) {
  115. options.id = this.getDefaults().id;
  116. } else {
  117. options.id = options.id.toString();
  118. }
  119. }
  120. if (options.title) {
  121. options.title = options.title.toString();
  122. }
  123. if (options.text) {
  124. options.text = options.text.toString();
  125. }
  126. if (options.badge) {
  127. if (isNaN(options.badge)) {
  128. options.badge = this.getDefaults().badge;
  129. } else {
  130. options.badge = Number(options.badge);
  131. }
  132. }
  133. if (typeof options.at == 'object') {
  134. options.at = options.at.getTime();
  135. }
  136. options.at = Math.round(options.at/1000);
  137. if (typeof options.data == 'object') {
  138. options.data = JSON.stringify(options.data);
  139. }
  140. return options;
  141. };
  142. /**
  143. * Create callback, which will be executed within a specific scope.
  144. *
  145. * @param {Function} callbackFn
  146. * The callback function
  147. * @param {Object} scope
  148. * The scope for the function
  149. *
  150. * @return {Function}
  151. * The new callback function
  152. */
  153. exports.createCallbackFn = function (callbackFn, scope) {
  154. if (typeof callbackFn != 'function')
  155. return;
  156. return function () {
  157. callbackFn.apply(scope || this, arguments);
  158. };
  159. };
  160. /**
  161. * Convert the IDs to Strings.
  162. *
  163. * @param {String/Number[]} ids
  164. *
  165. * @return Array of Strings
  166. */
  167. exports.convertIds = function (ids) {
  168. var convertedIds = [];
  169. for (var i = 0; i < ids.length; i++) {
  170. convertedIds.push(ids[i].toString());
  171. }
  172. return convertedIds;
  173. };
  174. /**
  175. * First found value for the given keys.
  176. *
  177. * @param {Object} options
  178. * Object with key-value properties
  179. * @param {String[]} keys*
  180. * Key list
  181. */
  182. exports.getValueFor = function (options) {
  183. var keys = Array.apply(null, arguments).slice(1);
  184. for (var i = 0; i < keys.length; i++) {
  185. var key = keys[i];
  186. if (options.hasOwnProperty(key)) {
  187. return options[key];
  188. }
  189. }
  190. };
  191. /**
  192. * Fire event with given arguments.
  193. *
  194. * @param {String} event
  195. * The event's name
  196. * @param {args*}
  197. * The callback's arguments
  198. */
  199. exports.fireEvent = function (event) {
  200. var args = Array.apply(null, arguments).slice(1),
  201. listener = this._listener[event];
  202. if (!listener)
  203. return;
  204. for (var i = 0; i < listener.length; i++) {
  205. var fn = listener[i][0],
  206. scope = listener[i][1];
  207. fn.apply(scope, args);
  208. }
  209. };
  210. /**
  211. * Execute the native counterpart.
  212. *
  213. * @param {String} action
  214. * The name of the action
  215. * @param args[]
  216. * Array of arguments
  217. * @param {Function} callback
  218. * The callback function
  219. * @param {Object} scope
  220. * The scope for the function
  221. */
  222. exports.exec = function (action, args, callback, scope) {
  223. var fn = this.createCallbackFn(callback, scope),
  224. params = [];
  225. if (Array.isArray(args)) {
  226. params = args;
  227. } else if (args) {
  228. params.push(args);
  229. }
  230. exec(fn, null, 'LocalNotification', action, params);
  231. };
  232. /*********
  233. * HOOKS *
  234. *********/
  235. // Called after 'deviceready' event
  236. channel.deviceready.subscribe(function () {
  237. // Device is ready now, the listeners are registered
  238. // and all queued events can be executed.
  239. exec(null, null, 'LocalNotification', 'deviceready', []);
  240. });
  241. // Called before 'deviceready' event
  242. channel.onCordovaReady.subscribe(function () {
  243. // Device plugin is ready now
  244. channel.onCordovaInfoReady.subscribe(function () {
  245. // Merge platform specifics into defaults
  246. exports.applyPlatformSpecificOptions();
  247. });
  248. });