2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var _global = window || global || {};
19 var utils_ = xwalk.utils;
20 var type_ = utils_.type;
21 var converter_ = utils_.converter;
22 var validator_ = utils_.validator;
23 var types_ = validator_.Types;
24 var native_ = new xwalk.utils.NativeManager(extension);
26 function convertColorToInt(rgbaColor) {
27 var color = rgbaColor.length === 7 ? rgbaColor + 'ff' : rgbaColor;
28 var isLengthOk = color.length === 9;
29 var isHash = color.substr(0, 1) === '#';
30 var hex = '0123456789abcdefABCDEF';
32 var c = color.replace('#', '');
34 for (var i = 0; i < c.length; i++) {
35 if (hex.indexOf(c[i]) < 0) {
40 if (!isLengthOk || !isHash || !isHex) {
41 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'invalid value');
44 return parseInt('0x' + c);
47 var EditManager = function() {
51 EditManager.prototype.allow = function() {
55 EditManager.prototype.disallow = function() {
59 var _edit = new EditManager();
61 var NotificationType = {
65 var StatusNotificationType = {
67 THUMBNAIL: 'THUMBNAIL',
72 var UserNotificationType = {
74 THUMBNAIL: 'THUMBNAIL',
79 var NotificationProgressType = {
80 PERCENTAGE: 'PERCENTAGE',
84 var LEDCustomFlags = {
85 LED_CUSTOM_DUTY_ON: 'LED_CUSTOM_DUTY_ON',
86 LED_CUSTOM_DEFAULT: 'LED_CUSTOM_DEFAULT'
89 function NotificationManager() {}
91 NotificationManager.prototype.post = function(notification) {
92 var args = validator_.validateArgs(arguments, [
93 { name: 'notification', type: types_.PLATFORM_OBJECT, values: Notification }
96 if (args.notification instanceof tizen.StatusNotification) {
98 'DEPRECATION WARNING: StatusNotification is deprecated since Tizen 4.0. ' +
99 'Use UserNotification instead.'
104 //add marker for UserNotification implementation
105 newImpl: args.notification instanceof tizen.UserNotification,
106 notification: args.notification
109 var result = native_.callSync('NotificationManager_post', data);
111 if (native_.isFailure(result)) {
112 throw native_.getErrorObject(result);
116 var d = native_.getResultObject(result);
117 notification.id = d.id;
118 notification.postedTime = new Date(d.postedTime) || new Date();
119 notification.type = d.type || NotificationType.STATUS;
123 NotificationManager.prototype.update = function(notification) {
124 var args = validator_.validateArgs(arguments, [
125 { name: 'notification', type: types_.PLATFORM_OBJECT, values: Notification }
128 if (!arguments.length) {
129 throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
131 if (!args.notification.id) {
132 throw new WebAPIException(WebAPIException.UNKNOWN_ERR);
135 if (args.notification instanceof tizen.StatusNotification) {
137 'DEPRECATION WARNING: StatusNotification is deprecated since Tizen 4.0. ' +
138 'Use UserNotification instead.'
143 //add marker for UserNotification implementation
144 newImpl: args.notification instanceof tizen.UserNotification,
145 notification: args.notification
148 var result = native_.callSync('NotificationManager_update', data);
150 if (native_.isFailure(result)) {
151 throw native_.getErrorObject(result);
155 NotificationManager.prototype.remove = function(id) {
156 var args = validator_.validateArgs(arguments, [{ name: 'id', type: types_.STRING }]);
158 if (!arguments.length || !(parseInt(arguments[0]) > 0)) {
159 throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
166 var result = native_.callSync('NotificationManager_remove', data);
168 if (native_.isFailure(result)) {
169 throw native_.getErrorObject(result);
173 NotificationManager.prototype.removeAll = function() {
174 var result = native_.callSync('NotificationManager_removeAll', {});
176 if (native_.isFailure(result)) {
177 throw native_.getErrorObject(result);
181 NotificationManager.prototype.get = function(id) {
182 var args = validator_.validateArgs(arguments, [{ name: 'id', type: types_.STRING }]);
185 'DEPRECATION WARNING: get() is deprecated since Tizen 4.0. ' +
186 'Use getNotification() instead.'
189 if (!arguments.length) {
190 throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
197 var result = native_.callSync('NotificationManager_get', data);
199 if (native_.isFailure(result)) {
200 throw native_.getErrorObject(result);
203 var n = native_.getResultObject(result);
206 var returnObject = new StatusNotification(n.statusType, n.title, n);
212 NotificationManager.prototype.getNotification = function(id) {
213 var args = validator_.validateArgs(arguments, [{ name: 'id', type: types_.STRING }]);
215 if (!arguments.length) {
216 throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
220 //add marker for UserNotification implementation
225 var result = native_.callSync('NotificationManager_get', data);
227 if (native_.isFailure(result)) {
228 throw native_.getErrorObject(result);
231 var n = native_.getResultObject(result);
234 var returnObject = new UserNotification(n.userType, n.title, n);
240 NotificationManager.prototype.getAll = function() {
242 'DEPRECATION WARNING: getAll() is deprecated since Tizen 4.0. ' +
243 'Use getAllNotifications() instead.'
246 var result = native_.callSync('NotificationManager_getAll', {});
248 if (native_.isFailure(result)) {
249 throw native_.getErrorObject(result);
252 var n = native_.getResultObject(result);
253 var notifications = [];
256 for (var i = 0; i < n.length; i++) {
257 notifications.push(new StatusNotification(n[i].statusType, n[i].title, n[i]));
261 return notifications;
264 NotificationManager.prototype.getAllNotifications = function() {
265 var result = native_.callSync('NotificationManager_getAll', {
266 //add marker for UserNotification implementation
270 if (native_.isFailure(result)) {
271 throw native_.getErrorObject(result);
274 var n = native_.getResultObject(result);
275 var notifications = [];
278 for (var i = 0; i < n.length; i++) {
279 notifications.push(new UserNotification(n[i].userType, n[i].title, n[i]));
283 return notifications;
287 * Plays the custom effect of the service LED that is located to the front of a device.
289 * @param timeOn Number
290 * @param timeOff Number
291 * @param color String
294 NotificationManager.prototype.playLEDCustomEffect = function(
300 var args = validator_.validateArgs(arguments, [
301 { name: 'timeOn', type: types_.LONG },
302 { name: 'timeOff', type: types_.LONG },
303 { name: 'color', type: types_.STRING },
304 { name: 'flags', type: types_.ARRAY, values: types_.STRING }
307 for (var i = 0; i < args.flags.length; ++i) {
308 if (Object.keys(LEDCustomFlags).indexOf(args.flags[i]) < 0) {
309 throw new WebAPIException(
310 WebAPIException.INVALID_VALUES_ERR,
316 args.color = convertColorToInt(args.color);
317 var result = native_.callSync('NotificationManager_playLEDCustomEffect', args);
318 if (native_.isFailure(result)) {
319 throw native_.getErrorObject(result);
324 * Stops the custom effect of the service LED that is located to the front of a device.
326 NotificationManager.prototype.stopLEDCustomEffect = function() {
327 var result = native_.callSync('NotificationManager_stopLEDCustomEffect');
328 if (native_.isFailure(result)) {
329 throw native_.getErrorObject(result);
333 NotificationManager.prototype.saveNotificationAsTemplate = function(name, notification) {
334 var args = validator_.validateArgs(arguments, [
335 { name: 'name', type: types_.STRING },
336 { name: 'notification', type: types_.PLATFORM_OBJECT, values: Notification }
339 //add marker for UserNotification implementation
340 args.newImpl = args.notification instanceof tizen.UserNotification;
342 var result = native_.callSync('NotificationManager_saveNotificationAsTemplate', args);
344 if (native_.isFailure(result)) {
345 throw native_.getErrorObject(result);
349 NotificationManager.prototype.createNotificationFromTemplate = function(name) {
350 var args = validator_.validateArgs(arguments, [
351 { name: 'name', type: types_.STRING }
354 if (!arguments.length) {
355 throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
358 var result = native_.callSync(
359 'NotificationManager_createNotificationFromTemplate',
363 if (native_.isFailure(result)) {
364 throw native_.getErrorObject(result);
366 var n = native_.getResultObject(result);
369 var returnObject = new UserNotification(n.userType, n.title, n);
375 var _checkProgressValue = function(v, _progressType) {
377 (_progressType === NotificationProgressType.PERCENTAGE && (v >= 0 && v <= 100)) ||
378 (_progressType === NotificationProgressType.BYTE &&
379 converter_.toUnsignedLong(v) >= 0)
386 var _checkDetailInfo = function(v) {
387 if (type_.isNull(v)) {
390 if (!type_.isArray(v)) {
393 for (var i = 0; i < v.length; ++i) {
394 if (!(v[i] instanceof tizen.NotificationDetailInfo)) {
401 var _setDetailInfo = function(v) {
403 for (var i = 0; i < v.length; ++i) {
404 _d.push(new tizen.NotificationDetailInfo(v[i].mainText, v[i].subText || null));
409 var _checkStringArray = function(v) {
410 if (type_.isNull(v)) {
413 if (!type_.isArray(v)) {
416 for (var i = 0; i < v.length; ++i) {
417 if (!type_.isString(v[i])) {
424 var _isHex = function(v) {
427 v.substr(0, 1) === '#' &&
428 /^([0-9A-Fa-f]{2})+$/.test(v.substr(1, 7))
432 function NotificationInitDict(data) {
433 var _iconPath = null;
434 var _soundPath = null;
435 var _vibration = false;
436 var _appControl = null;
438 var _progressType = NotificationProgressType.PERCENTAGE;
439 var _progressValue = null;
441 var _subIconPath = null;
442 var _detailInfo = [];
443 var _ledColor = null;
444 var _ledOnPeriod = 0;
445 var _ledOffPeriod = 0;
446 var _backgroundImagePath = null;
447 var _thumbnails = [];
449 Object.defineProperties(this, {
455 _iconPath = type_.isString(v) || type_.isNull(v) ? v : _iconPath;
464 _soundPath = type_.isString(v) || type_.isNull(v) ? v : _soundPath;
473 _vibration = type_.isBoolean(v) ? v : _vibration;
484 ? new tizen.ApplicationControl(
491 : v instanceof tizen.ApplicationControl || type_.isNull(v)
503 (type_.isString(v) && !/\s/.test(v)) || type_.isNull(v) ? v : _appId;
509 return _progressType;
513 Object.keys(NotificationProgressType).indexOf(v) >= 0
521 if (null === _progressValue) {
522 return _progressValue;
525 return _progressType === NotificationProgressType.PERCENTAGE
526 ? _progressValue * 100
530 if (type_.isNull(v)) {
534 if (_checkProgressValue(v, _progressType)) {
536 _progressType === NotificationProgressType.PERCENTAGE
538 : converter_.toUnsignedLong(v);
548 _number = type_.isNumber(v) || type_.isNull(v) ? v : _number;
557 _subIconPath = type_.isString(v) || type_.isNull(v) ? v : _subIconPath;
569 : _checkDetailInfo(v)
581 (type_.isString(v) && _isHex(v)) || type_.isNull(v) ? v : _ledColor;
590 _ledOnPeriod = type_.isNumber(v) ? v : _ledOnPeriod;
596 return _ledOffPeriod;
599 _ledOffPeriod = type_.isNumber(v) ? v : _ledOffPeriod;
603 backgroundImagePath: {
605 return _backgroundImagePath;
608 _backgroundImagePath =
609 type_.isString(v) || type_.isNull(v) ? v : _backgroundImagePath;
618 _thumbnails = _checkStringArray(v) ? v : _thumbnails;
624 if (data instanceof _global.Object) {
625 for (var prop in data) {
626 if (this.hasOwnProperty(prop)) {
627 this[prop] = data[prop];
633 function Notification(data) {
635 var _type = NotificationType.STATUS;
640 Object.defineProperties(this, {
646 _id = _edit.canEdit && v ? v : _id;
655 _type = _edit.canEdit
656 ? converter_.toEnum(v, Object.keys(NotificationType), false)
666 _postedTime = _edit.canEdit && v ? new Date(v) : _postedTime;
675 _title = converter_.toString(v);
684 _content = type_.isString(v) || type_.isNull(v) ? v : _content;
690 if (data instanceof _global.Object) {
691 for (var prop in data) {
692 if (data.hasOwnProperty(prop) && this.hasOwnProperty(prop)) {
693 this[prop] = data[prop];
699 function StatusNotification(statusType, title, notificationInitDict) {
700 validator_.isConstructorCall(this, StatusNotification);
701 type_.isObject(notificationInitDict)
702 ? (notificationInitDict.title = title)
703 : (notificationInitDict = { title: title });
704 NotificationInitDict.call(this, notificationInitDict);
705 Notification.call(this, notificationInitDict);
708 'DEPRECATION WARNING: StatusNotification is deprecated since Tizen 4.0. ' +
709 'Use UserNotification instead.'
713 Object.keys(StatusNotificationType).indexOf(statusType) >= 0
715 : StatusNotificationType.SIMPLE;
717 Object.defineProperties(this, {
724 Object.keys(StatusNotificationType).indexOf(v) >= 0 && _edit.canEdit
733 StatusNotification.prototype = new Notification();
734 StatusNotification.prototype.constructor = StatusNotification;
736 function NotificationDetailInfo(mainText, subText) {
737 validator_.isConstructorCall(this, NotificationDetailInfo);
739 var _mainText = type_.isString(mainText) ? mainText : '';
740 var _subText = type_.isString(subText) ? subText : null;
742 Object.defineProperties(this, {
748 _mainText = type_.isString(v) ? v : _mainText;
757 _subText = type_.isString(v) ? v : _subText;
764 function TextContentsInitDict(data) {
766 var _progressType = NotificationProgressType.PERCENTAGE;
767 var _progressValue = null;
769 var _detailInfo = [];
770 var _buttonsTexts = [];
771 var _contentForOff = null;
773 Object.defineProperties(this, {
776 return _progressType;
780 Object.keys(NotificationProgressType).indexOf(v) >= 0
788 if (null === _progressValue) {
789 return _progressValue;
792 return _progressType === NotificationProgressType.PERCENTAGE
793 ? _progressValue * 100
797 if (type_.isNull(v)) {
801 if (_checkProgressValue(v, _progressType)) {
803 _progressType === NotificationProgressType.PERCENTAGE
805 : converter_.toUnsignedLong(v);
815 _number = type_.isNumber(v) || type_.isNull(v) ? v : _number;
827 : _checkDetailInfo(v)
835 return _buttonsTexts;
838 _buttonsTexts = _checkStringArray(v) ? v : _buttonsTexts;
844 return _contentForOff;
848 type_.isString(v) || type_.isNull(v) ? v : _contentForOff;
854 if (data instanceof _global.Object) {
855 this['progressType'] = data['progressType'];
856 for (var prop in data) {
857 if (this.hasOwnProperty(prop)) {
858 this[prop] = data[prop];
864 function ImagesInitDict(data) {
866 var _iconPath = null;
867 var _subIconPath = null;
868 var _indicatorIconPath = null;
869 var _lockScreenIconPath = null;
870 var _buttonIconPaths = [];
871 var _backgroundImagePath = null;
873 Object.defineProperties(this, {
879 _iconPath = type_.isString(v) || type_.isNull(v) ? v : _iconPath;
888 _subIconPath = type_.isString(v) || type_.isNull(v) ? v : _subIconPath;
894 return _indicatorIconPath;
898 type_.isString(v) || type_.isNull(v) ? v : _indicatorIconPath;
902 lockScreenIconPath: {
904 return _lockScreenIconPath;
907 _lockScreenIconPath =
908 type_.isString(v) || type_.isNull(v) ? v : _lockScreenIconPath;
914 return _buttonIconPaths;
917 _buttonIconPaths = _checkStringArray(v) ? v : _buttonIconPaths;
921 backgroundImagePath: {
923 return _backgroundImagePath;
926 _backgroundImagePath =
927 type_.isString(v) || type_.isNull(v) ? v : _backgroundImagePath;
933 if (data instanceof _global.Object) {
934 for (var prop in data) {
935 if (this.hasOwnProperty(prop)) {
936 this[prop] = data[prop];
942 function ThumbnailsInitDict(data) {
944 var _lockScreenThumbnailIconPath = null;
945 var _thumbnailIconPath = null;
946 var _thumbnails = [];
948 Object.defineProperties(this, {
949 lockScreenThumbnailIconPath: {
951 return _lockScreenThumbnailIconPath;
954 _lockScreenThumbnailIconPath =
955 type_.isString(v) || type_.isNull(v)
957 : _lockScreenThumbnailIconPath;
963 return _thumbnailIconPath;
967 type_.isString(v) || type_.isNull(v) ? v : _thumbnailIconPath;
976 _thumbnails = _checkStringArray(v) ? v : _thumbnails;
982 if (data instanceof _global.Object) {
983 for (var prop in data) {
984 if (this.hasOwnProperty(prop)) {
985 this[prop] = data[prop];
991 function ActionsInitDict(data) {
993 var _soundPath = null;
994 var _vibration = false;
995 var _appControl = null;
998 Object.defineProperties(this, {
1004 _soundPath = type_.isString(v) || type_.isNull(v) ? v : _soundPath;
1013 _vibration = type_.isBoolean(v) ? v : _vibration;
1024 ? new tizen.ApplicationControl(
1031 : v instanceof tizen.ApplicationControl || type_.isNull(v)
1043 (type_.isString(v) && !/\s/.test(v)) || type_.isNull(v) ? v : _appId;
1049 if (data instanceof _global.Object) {
1050 for (var prop in data) {
1051 if (this.hasOwnProperty(prop)) {
1052 this[prop] = data[prop];
1058 function GroupContentsInitDict(data) {
1060 var _groupTitle = null;
1061 var _groupContent = null;
1062 var _groupContentForOff = null;
1064 Object.defineProperties(this, {
1070 _groupTitle = type_.isString(v) || type_.isNull(v) ? v : _groupTitle;
1076 return _groupContent;
1079 _groupContent = type_.isString(v) || type_.isNull(v) ? v : _groupContent;
1083 groupContentForOff: {
1085 return _groupContentForOff;
1088 _groupContentForOff =
1089 type_.isString(v) || type_.isNull(v) ? v : _groupContentForOff;
1095 if (data instanceof _global.Object) {
1096 for (var prop in data) {
1097 if (this.hasOwnProperty(prop)) {
1098 this[prop] = data[prop];
1104 function LedsInitDict(data) {
1106 var _ledColor = null;
1107 var _ledOnPeriod = 0;
1108 var _ledOffPeriod = 0;
1110 Object.defineProperties(this, {
1117 (type_.isString(v) && _isHex(v)) || type_.isNull(v) ? v : _ledColor;
1123 return _ledOnPeriod;
1126 _ledOnPeriod = type_.isNumber(v) ? v : _ledOnPeriod;
1132 return _ledOffPeriod;
1135 _ledOffPeriod = type_.isNumber(v) ? v : _ledOffPeriod;
1141 if (data instanceof _global.Object) {
1142 for (var prop in data) {
1143 if (this.hasOwnProperty(prop)) {
1144 this[prop] = data[prop];
1150 function UserNotification(userType, title, notificationGroupedInitDict) {
1151 validator_.isConstructorCall(this, UserNotification);
1152 type_.isObject(notificationGroupedInitDict)
1153 ? (notificationGroupedInitDict.title = title)
1154 : (notificationGroupedInitDict = { title: title });
1155 UserNotificationInitDict.call(this, notificationGroupedInitDict);
1156 Notification.call(this, notificationGroupedInitDict);
1158 this.textContents = new TextContentsInitDict(
1159 notificationGroupedInitDict.textContents
1161 this.images = new ImagesInitDict(notificationGroupedInitDict.images);
1162 this.thumbnails = new ThumbnailsInitDict(notificationGroupedInitDict.thumbnails);
1163 this.actions = new ActionsInitDict(notificationGroupedInitDict.actions);
1164 this.leds = new LedsInitDict(notificationGroupedInitDict.leds);
1165 this.groupContents = new GroupContentsInitDict(
1166 notificationGroupedInitDict.groupContents
1170 Object.keys(UserNotificationType).indexOf(userType) >= 0
1172 : StatusNotificationType.SIMPLE;
1174 Object.defineProperties(this, {
1181 Object.keys(StatusNotificationType).indexOf(v) >= 0 && _edit.canEdit
1190 UserNotification.prototype = new Notification();
1191 UserNotification.prototype.constructor = UserNotification;
1193 function UserNotificationInitDict(data) {
1194 var _textContents = null;
1196 var _thumbnails = null;
1197 var _actions = null;
1198 var _groupContents = null;
1201 Object.defineProperties(this, {
1204 return _textContents;
1207 _textContents = type_.isObject(v) || type_.isNull(v) ? v : _textContents;
1216 _images = type_.isObject(v) || type_.isNull(v) ? v : _images;
1225 _thumbnails = type_.isObject(v) || type_.isNull(v) ? v : _thumbnails;
1234 _actions = type_.isObject(v) || type_.isNull(v) ? v : _actions;
1240 return _groupContents;
1244 type_.isObject(v) || type_.isNull(v) ? v : _groupContents;
1253 _leds = type_.isObject(v) || type_.isNull(v) ? v : _leds;
1259 if (data instanceof _global.Object) {
1260 for (var prop in data) {
1261 if (this.hasOwnProperty(prop)) {
1262 this[prop] = data[prop];
1268 exports = new NotificationManager();
1269 tizen.StatusNotification = StatusNotification;
1270 tizen.UserNotification = UserNotification;
1271 tizen.NotificationDetailInfo = NotificationDetailInfo;