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 T = xwalk.utils.type;
18 var Converter = xwalk.utils.converter;
19 var AV = xwalk.utils.validator;
20 var privUtils_ = xwalk.utils;
22 var native = new xwalk.utils.NativeManager(extension);
24 var AlarmManager = function () {
25 Object.defineProperties(this, {
26 PERIOD_MINUTE: { value: 60, writable: false, enumerable: true},
27 PERIOD_HOUR: { value: 3600, writable: false, enumerable: true},
28 PERIOD_DAY: { value: 86400, writable: false, enumerable: true},
29 PERIOD_WEEK: { value: 604800, writable: false, enumerable: true},
33 var EditManager = function() {
37 EditManager.prototype.allow = function() {
41 EditManager.prototype.disallow = function() {
45 var _edit = new EditManager();
47 function InternalData_(data) {
48 if (!(this instanceof InternalData_)) {
49 return new InternalData_(data);
52 for(var key in data) {
53 if (data.hasOwnProperty(key)) {
54 this[key] = data[key];
59 function UpdateInternalData_(internal, data) {
60 var values = InternalData_(data);
62 for(var key in data) {
63 if (values.hasOwnProperty(key) && internal.hasOwnProperty(key)) {
64 internal[key] = values;
69 var LogManager = function() {
70 this.enableLog = true;
73 LogManager.prototype.allow = function() {
74 this.enableLog = true;
77 LogManager.prototype.disallow = function() {
78 this.enableLog = false;
81 var _warningLogs = new LogManager();
83 //class AlarmManager ////////////////////////////////////////////////////
84 AlarmManager.prototype.add = function () {
85 var args = AV.validateMethod(arguments, [
88 type : AV.Types.PLATFORM_OBJECT,
89 values : [tizen.AlarmRelative, tizen.AlarmAbsolute]
92 name : 'applicationId',
93 type : AV.Types.STRING,
97 type : AV.Types.PLATFORM_OBJECT,
98 values : tizen.ApplicationControl,
104 var type = null, seconds = 0;
105 if (args.alarm instanceof tizen.AlarmRelative) {
106 type = 'AlarmRelative';
107 } else if (args.alarm instanceof tizen.AlarmAbsolute) {
108 type = 'AlarmAbsolute';
109 seconds = args.alarm.date.getTime();
113 callArgs.alarm = args.alarm;
114 callArgs.applicationId = args.applicationId;
115 if (args.has.appControl) {
116 callArgs.appControl = args.appControl;
119 callArgs.type = type;
120 callArgs.seconds = Converter.toString(seconds);
121 callArgs.isPeriodSet = !T.isNullOrUndefined(args.alarm.period);
123 var result = native.callSync('AlarmManager_add', callArgs);
124 if (native.isFailure(result)) {
125 throw native.getErrorObject(result);
128 UpdateInternalData_(args.alarm, native.getResultObject(result));
133 AlarmManager.prototype.addAlarmNotification = function() {
134 var args = AV.validateMethod(arguments, [
137 type: AV.Types.PLATFORM_OBJECT,
138 values: [tizen.AlarmRelative, tizen.AlarmAbsolute]
140 name: 'notification',
141 type: AV.Types.PLATFORM_OBJECT,
142 values: [tizen.StatusNotification, tizen.UserNotification]
145 var type = null, milliseconds = 0;
146 if (args.alarm instanceof tizen.AlarmRelative) {
147 type = 'AlarmRelative';
148 } else if (args.alarm instanceof tizen.AlarmAbsolute) {
149 type = 'AlarmAbsolute';
150 milliseconds = args.alarm.date.getTime();
154 callArgs.alarm = args.alarm;
155 callArgs.type = type;
156 callArgs.notification = args.notification;
157 callArgs.milliseconds = Converter.toString(milliseconds);
158 callArgs.isPeriodSet = !T.isNullOrUndefined(args.alarm.period);
160 //add marker for UserNotification implementation
161 callArgs.newImpl = (callArgs.notification instanceof tizen.UserNotification);
163 var result = native.callSync('AlarmManager_addAlarmNotification', callArgs);
164 if (native.isFailure(result)) {
165 throw native.getErrorObject(result);
169 UpdateInternalData_(args.alarm, native.getResultObject(result));
174 AlarmManager.prototype.remove = function () {
175 var args = AV.validateMethod(arguments, [
178 type : AV.Types.STRING,
182 var result = native.callSync('AlarmManager_remove', {id: Number(args.id)});
184 if (native.isFailure(result)) {
185 throw native.getErrorObject(result);
189 AlarmManager.prototype.removeAll = function () {
190 var result = native.callSync('AlarmManager_removeAll', {});
192 if (native.isFailure(result)) {
193 throw native.getErrorObject(result);
197 AlarmManager.prototype.get = function () {
198 var args = AV.validateMethod(arguments, [
201 type : AV.Types.STRING,
205 var result = native.callSync('AlarmManager_get', {id: Number(args.id)});
207 if (native.isFailure(result)) {
208 throw native.getErrorObject(result);
210 result = native.getResultObject(result);
213 _warningLogs.disallow();
214 if ('AlarmRelative' === result.type) {
215 alarm = new tizen.AlarmRelative(result.delay, result.period, InternalData_(result));
217 var date = new Date(result.year, result.month, result.day,
218 result.hour, result.min, result.sec);
220 alarm = new tizen.AlarmAbsolute(date, result.second, InternalData_(result));
222 _warningLogs.allow();
227 function _prepareAppControl(noti) {
228 if (!noti || !noti.actions || !noti.actions.appControl) {
229 privUtils_.log("Do nothing - appControl is NOT present");
232 if (!T.isNullOrUndefined(noti.actions.appControl.operation)) {
233 noti.actions.appControl = new tizen.ApplicationControl(
234 noti.actions.appControl.operation,
235 noti.actions.appControl.uri,
236 noti.actions.appControl.mime,
237 noti.actions.appControl.category,
238 noti.actions.appControl.data,
239 noti.actions.appControl.launchMode);
243 function _prepareDetailInfo(noti) {
244 if (!noti || !noti.textContents || !noti.textContents.detailInfo) {
245 console.log("Do nothing - detailInfo is NOT present");
248 var detailInfo = noti.textContents.detailInfo;
249 if (T.isArray(detailInfo)) {
251 for (var i = 0; i < detailInfo.length; ++i) {
252 _d.push(new tizen.NotificationDetailInfo(detailInfo[i].mainText,
253 detailInfo[i].subText || null));
255 noti.textContents.detailInfo = _d;
259 AlarmManager.prototype.getAlarmNotification = function () {
260 var args = AV.validateMethod(arguments, [
263 type : AV.Types.STRING,
267 var result = native.callSync('AlarmManager_getAlarmNotification', {id: Number(args.id)});
269 if (native.isFailure(result)) {
270 throw native.getErrorObject(result);
272 var noti = native.getResultObject(result);
273 _prepareAppControl(noti);
274 _prepareDetailInfo(noti);
275 return new tizen.UserNotification(noti.userType, noti.title, noti);
279 AlarmManager.prototype.getAll = function () {
280 var result = native.callSync('AlarmManager_getAll', {});
282 if (native.isFailure(result)) {
283 throw native.getErrorObject(result);
285 var data = native.getResultObject(result);
287 _warningLogs.disallow();
288 data.forEach(function (i) {
289 if ('AlarmRelative'=== i.type) {
290 md.push(new tizen.AlarmRelative(i.delay, i.period, InternalData_(i)));
292 var date = new Date(i.year, i.month, i.day,
293 i.hour, i.min, i.sec);
294 md.push(new tizen.AlarmAbsolute(date, i.second, InternalData_(i)));
297 _warningLogs.allow();
302 //class Alarm //////////////////////////////////////////////////////////
306 if (!T.isNullOrUndefined(id) && id instanceof InternalData_) {
314 Object.defineProperties(this, {
316 get: function () {return _internal.id;},
317 set: function (value) {
318 if (value instanceof InternalData_) {
319 _internal.id = value.id;
326 //class AlarmRelative //////////////////////////////////////////////////
328 tizen.AlarmRelative = function(delay, period, internal) {
329 AV.validateConstructorCall(this, tizen.AlarmRelative);
333 var m_delay = Converter.toLong(delay);
335 if (arguments.length >= 2) {
336 if(!T.isNullOrUndefined(period)){
337 m_period = Converter.toLong(period, true);
341 Alarm.call(this, internal);
343 Object.defineProperties(this, {
349 if (_edit.canEdit && v) {
350 m_delay = Converter.toLong(v.delay);
360 if (_edit.canEdit && v) {
361 m_period = Converter.toLong(v.period);
369 tizen.AlarmRelative.prototype = new Alarm();
371 tizen.AlarmRelative.prototype.constructor = tizen.AlarmRelative;
373 tizen.AlarmRelative.prototype.getRemainingSeconds = function () {
374 var result = native.callSync('AlarmRelative_getRemainingSeconds', {id: Number(this.id)});
376 if (native.isFailure(result)) {
377 throw native.getErrorObject(result);
379 return Converter.toLong(native.getResultObject(result).seconds, true);
383 function makeDateConst(obj) {
384 privUtils_.log('Enter MakeConst');
385 obj.setDate = function() {};
386 obj.setFullYear = function() {};
387 obj.setHours = function() {};
388 obj.setMilliseconds = function() {};
389 obj.setMinutes = function() {};
390 obj.setMonth = function() {};
391 obj.setSeconds = function() {};
392 obj.setTime = function() {};
393 obj.setUTCDate = function() {};
394 obj.setUTCFullYear = function() {};
395 obj.setUTCHours = function() {};
396 obj.setUTCMilliseconds = function() {};
397 obj.setUTCMinutes = function() {};
398 obj.setUTCMonth = function() {};
399 obj.setUTCSeconds = function() {};
400 obj.setYear = function() {};
401 privUtils_.log('Leave MakeConst');
404 //class AlarmAbsolute //////////////////////////////////////////////////
406 tizen.AlarmAbsolute = function(date, second, internal) {
407 AV.validateConstructorCall(this, tizen.AlarmAbsolute);
409 var m_period = null, m_daysOfWeek = [], m_date;
411 if (T.isDate(date)) {
413 if (arguments.length >= 2) {
414 if(T.isArray(second)){
415 m_daysOfWeek = second;
417 if(!T.isNullOrUndefined(second)){
418 m_period = Converter.toLong(second);
419 if(_warningLogs.enableLog){
420 privUtils_.warn("This Constructor is deprecated since Tizen 4.0." +
421 " Please consider using other constructors or other type of an alarm.");
427 Alarm.call(this, internal);
429 m_period = undefined;
431 makeDateConst(m_date);
432 Object.defineProperties(this, {
438 if (_edit.canEdit && T.isDate(v.date)) {
446 if(_warningLogs.enableLog){
447 privUtils_.warn('Since Tizen 4.0 constructor AlarmAbsolute(Date date, long period) ' +
448 'is deprecated, thus period attribute should not be used.');
453 if(_warningLogs.enableLog){
454 privUtils_.warn('Since Tizen 4.0 constructor AlarmAbsolute(Date date, long period) ' +
455 'is deprecated, thus period attribute should not be used.');
458 if (_edit.canEdit && v) {
459 m_period = Converter.toLong(v.period);
469 if (_edit.canEdit && T.isArray(v.second)) {
470 m_daysOfWeek = v.second;
478 tizen.AlarmAbsolute.prototype = new Alarm();
480 tizen.AlarmAbsolute.prototype.constructor = tizen.AlarmAbsolute;
482 tizen.AlarmAbsolute.prototype.getNextScheduledDate = function () {
483 var result = native.callSync('AlarmAbsolute_getNextScheduledDate', {id: Number(this.id)});
485 if (native.isFailure(result)) {
486 throw native.getErrorObject(result);
488 var d = native.getResultObject(result);
489 if (T.isNull(d.year)) {
492 var date = new Date(d.year, d.month, d.day, d.hour, d.min, d.sec);
498 // Singleton to check on which profile plugins are executed
499 var ProfileAlarm = (function() {
500 var m_profile = null;
501 function ProfileAlarm() {
502 if (m_profile == null) {
503 m_profile = privUtils_.checkProfile();
505 Object.defineProperties(this, {
518 getInstance: function() {
519 if (instance == null) {
520 instance = new ProfileAlarm();
521 // Hide the constructor so the returned objected can't be new'd...
522 instance.constructor = null;
529 var _profile = ProfileAlarm.getInstance().profile;
531 // Notifications feature are available on mobile and wearable profile
532 if (_profile != 'mobile' && _profile != 'wearable') {
533 delete AlarmManager.prototype.addAlarmNotification;
534 delete AlarmManager.prototype.getAlarmNotification;
537 //exports //////////////////////////////////////////////////////////////
538 exports = new AlarmManager();