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 isAlarmAbsolutePeriodDeprecated = true;
26 var AlarmManager = function() {
27 Object.defineProperties(this, {
28 PERIOD_MINUTE: { value: 60, writable: false, enumerable: true },
29 PERIOD_HOUR: { value: 3600, writable: false, enumerable: true },
30 PERIOD_DAY: { value: 86400, writable: false, enumerable: true },
31 PERIOD_WEEK: { value: 604800, writable: false, enumerable: true }
35 var EditManager = function() {
39 EditManager.prototype.allow = function() {
43 EditManager.prototype.disallow = function() {
47 var _edit = new EditManager();
49 function InternalData_(data) {
50 if (!(this instanceof InternalData_)) {
51 return new InternalData_(data);
54 for (var key in data) {
55 if (data.hasOwnProperty(key)) {
56 this[key] = data[key];
61 function UpdateInternalData_(internal, data) {
62 var values = InternalData_(data);
64 for (var key in data) {
65 if (values.hasOwnProperty(key) && internal.hasOwnProperty(key)) {
66 internal[key] = values;
71 var LogManager = function() {
72 this.enableLog = true;
75 LogManager.prototype.allow = function() {
76 this.enableLog = true;
79 LogManager.prototype.disallow = function() {
80 this.enableLog = false;
83 var _warningLogs = new LogManager();
85 //class AlarmManager ////////////////////////////////////////////////////
86 AlarmManager.prototype.add = function() {
87 var args = AV.validateArgs(arguments, [
90 type: AV.Types.PLATFORM_OBJECT,
91 values: [tizen.AlarmRelative, tizen.AlarmAbsolute]
94 name: 'applicationId',
99 type: AV.Types.PLATFORM_OBJECT,
100 values: tizen.ApplicationControl,
108 if (args.alarm instanceof tizen.AlarmRelative) {
109 type = 'AlarmRelative';
110 } else if (args.alarm instanceof tizen.AlarmAbsolute) {
111 type = 'AlarmAbsolute';
112 seconds = args.alarm.date.getTime();
116 callArgs.alarm = args.alarm;
117 callArgs.applicationId = args.applicationId;
118 if (args.has.appControl) {
119 callArgs.appControl = args.appControl;
122 callArgs.type = type;
123 callArgs.seconds = Converter.toString(seconds);
124 callArgs.isPeriodSet = !T.isNullOrUndefined(args.alarm.period);
126 var result = native.callSync('AlarmManagerAdd', callArgs);
127 if (native.isFailure(result)) {
128 throw native.getErrorObject(result);
131 UpdateInternalData_(args.alarm, native.getResultObject(result));
136 AlarmManager.prototype.addAlarmNotification = function() {
137 var args = AV.validateArgs(arguments, [
140 type: AV.Types.PLATFORM_OBJECT,
141 values: [tizen.AlarmRelative, tizen.AlarmAbsolute]
144 name: 'notification',
145 type: AV.Types.PLATFORM_OBJECT,
146 values: [tizen.StatusNotification, tizen.UserNotification]
152 if (args.alarm instanceof tizen.AlarmRelative) {
153 type = 'AlarmRelative';
154 } else if (args.alarm instanceof tizen.AlarmAbsolute) {
155 type = 'AlarmAbsolute';
156 milliseconds = args.alarm.date.getTime();
160 callArgs.alarm = args.alarm;
161 callArgs.type = type;
162 callArgs.notification = args.notification;
163 callArgs.milliseconds = Converter.toString(milliseconds);
164 callArgs.isPeriodSet = !T.isNullOrUndefined(args.alarm.period);
166 //add marker for UserNotification implementation
167 callArgs.newImpl = callArgs.notification instanceof tizen.UserNotification;
169 var result = native.callSync('AlarmManagerAddAlarmNotification', callArgs);
170 if (native.isFailure(result)) {
171 throw native.getErrorObject(result);
174 UpdateInternalData_(args.alarm, native.getResultObject(result));
179 AlarmManager.prototype.remove = function() {
180 var args = AV.validateArgs(arguments, [
183 type: AV.Types.STRING
187 var result = native.callSync('AlarmManagerRemove', { id: Number(args.id) });
189 if (native.isFailure(result)) {
190 throw native.getErrorObject(result);
194 AlarmManager.prototype.removeAll = function() {
195 var result = native.callSync('AlarmManagerRemoveAll', {});
197 if (native.isFailure(result)) {
198 throw native.getErrorObject(result);
202 AlarmManager.prototype.get = function() {
203 var args = AV.validateArgs(arguments, [
206 type: AV.Types.STRING
210 var result = native.callSync('AlarmManagerGet', { id: Number(args.id) });
212 if (native.isFailure(result)) {
213 throw native.getErrorObject(result);
215 result = native.getResultObject(result);
218 _warningLogs.disallow();
219 if ('AlarmRelative' === result.type) {
220 alarm = new tizen.AlarmRelative(
223 InternalData_(result)
235 alarm = new tizen.AlarmAbsolute(date, result.second, InternalData_(result));
237 _warningLogs.allow();
242 function _prepareAppControl(noti) {
243 if (!noti || !noti.actions || !noti.actions.appControl) {
244 privUtils_.log('Do nothing - appControl is NOT present');
247 if (!T.isNullOrUndefined(noti.actions.appControl.operation)) {
248 noti.actions.appControl = new tizen.ApplicationControl(
249 noti.actions.appControl.operation,
250 noti.actions.appControl.uri,
251 noti.actions.appControl.mime,
252 noti.actions.appControl.category,
253 noti.actions.appControl.data,
254 noti.actions.appControl.launchMode
259 function _prepareDetailInfo(noti) {
260 if (!noti || !noti.textContents || !noti.textContents.detailInfo) {
261 privUtils_.log('Do nothing - detailInfo is NOT present');
264 var detailInfo = noti.textContents.detailInfo;
265 if (T.isArray(detailInfo)) {
267 for (var i = 0; i < detailInfo.length; ++i) {
269 new tizen.NotificationDetailInfo(
270 detailInfo[i].mainText,
271 detailInfo[i].subText || null
275 noti.textContents.detailInfo = _d;
279 AlarmManager.prototype.getAlarmNotification = function() {
280 var args = AV.validateArgs(arguments, [
283 type: AV.Types.STRING
287 var result = native.callSync('AlarmManagerGetAlarmNotification', {
291 if (native.isFailure(result)) {
292 throw native.getErrorObject(result);
294 var noti = native.getResultObject(result);
295 _prepareAppControl(noti);
296 _prepareDetailInfo(noti);
297 return new tizen.UserNotification(noti.userType, noti.title, noti);
301 AlarmManager.prototype.getAll = function() {
302 var result = native.callSync('AlarmManagerGetAll', {});
304 if (native.isFailure(result)) {
305 throw native.getErrorObject(result);
307 var data = native.getResultObject(result);
309 _warningLogs.disallow();
310 data.forEach(function(i) {
311 if ('AlarmRelative' === i.type) {
312 md.push(new tizen.AlarmRelative(i.delay, i.period, InternalData_(i)));
314 var date = new Date(i.year, i.month, i.day, i.hour, i.min, i.sec);
315 md.push(new tizen.AlarmAbsolute(date, i.second, InternalData_(i)));
318 _warningLogs.allow();
323 //class Alarm //////////////////////////////////////////////////////////
327 if (!T.isNullOrUndefined(id) && id instanceof InternalData_) {
335 Object.defineProperties(this, {
340 set: function(value) {
341 if (value instanceof InternalData_) {
342 _internal.id = value.id;
349 //class AlarmRelative //////////////////////////////////////////////////
351 tizen.AlarmRelative = function(delay, period, internal) {
352 AV.validateConstructorCall(this, tizen.AlarmRelative);
356 var m_delay = Converter.toLong(delay);
358 if (arguments.length >= 2) {
359 if (!T.isNullOrUndefined(period)) {
360 m_period = Converter.toLong(period, true);
364 Alarm.call(this, internal);
366 Object.defineProperties(this, {
372 if (_edit.canEdit && v) {
373 m_delay = Converter.toLong(v.delay);
383 if (_edit.canEdit && v) {
384 m_period = Converter.toLong(v.period);
392 tizen.AlarmRelative.prototype = new Alarm();
394 tizen.AlarmRelative.prototype.constructor = tizen.AlarmRelative;
396 tizen.AlarmRelative.prototype.getRemainingSeconds = function() {
397 var result = native.callSync('AlarmManagerGetRemainingSeconds', {
401 if (native.isFailure(result)) {
402 throw native.getErrorObject(result);
404 return Converter.toLong(native.getResultObject(result).seconds, true);
408 function makeDateConst(obj) {
409 privUtils_.log('Enter MakeConst');
410 obj.setDate = function() {};
411 obj.setFullYear = function() {};
412 obj.setHours = function() {};
413 obj.setMilliseconds = function() {};
414 obj.setMinutes = function() {};
415 obj.setMonth = function() {};
416 obj.setSeconds = function() {};
417 obj.setTime = function() {};
418 obj.setUTCDate = function() {};
419 obj.setUTCFullYear = function() {};
420 obj.setUTCHours = function() {};
421 obj.setUTCMilliseconds = function() {};
422 obj.setUTCMinutes = function() {};
423 obj.setUTCMonth = function() {};
424 obj.setUTCSeconds = function() {};
425 obj.setYear = function() {};
426 privUtils_.log('Leave MakeConst');
429 //class AlarmAbsolute //////////////////////////////////////////////////
431 tizen.AlarmAbsolute = function(date, second, internal) {
432 AV.validateConstructorCall(this, tizen.AlarmAbsolute);
438 if (T.isDate(date)) {
440 if (arguments.length >= 2) {
441 if (T.isArray(second)) {
442 m_daysOfWeek = second;
444 if (!T.isNullOrUndefined(second)) {
445 m_period = Converter.toLong(second);
446 if (_warningLogs.enableLog && isAlarmAbsolutePeriodDeprecated) {
448 'This Constructor is deprecated since Tizen 4.0.' +
449 ' Please consider using other constructors or other ' +
457 Alarm.call(this, internal);
459 m_period = undefined;
461 makeDateConst(m_date);
462 Object.defineProperties(this, {
468 if (_edit.canEdit && T.isDate(v.date)) {
476 if (_warningLogs.enableLog && isAlarmAbsolutePeriodDeprecated) {
478 'Since Tizen 4.0 constructor AlarmAbsolute(Date date, ' +
479 'long period) is deprecated, thus period attribute should ' +
486 if (_warningLogs.enableLog && isAlarmAbsolutePeriodDeprecated) {
488 'Since Tizen 4.0 constructor AlarmAbsolute(Date date, ' +
489 'long period) is deprecated, thus period attribute should ' +
494 if (_edit.canEdit && v) {
495 m_period = Converter.toLong(v.period);
505 if (_edit.canEdit && T.isArray(v.second)) {
506 m_daysOfWeek = v.second;
514 tizen.AlarmAbsolute.prototype = new Alarm();
516 tizen.AlarmAbsolute.prototype.constructor = tizen.AlarmAbsolute;
518 tizen.AlarmAbsolute.prototype.getNextScheduledDate = function() {
519 var result = native.callSync('AlarmManagerGetNextScheduledDate', {
523 if (native.isFailure(result)) {
524 throw native.getErrorObject(result);
526 var d = native.getResultObject(result);
527 if (T.isNull(d.year)) {
530 var date = new Date(d.year, d.month, d.day, d.hour, d.min, d.sec);
536 // Singleton to check on which profile plugins are executed
537 var ProfileAlarm = (function() {
538 var m_profile = null;
539 function ProfileAlarm() {
540 if (m_profile == null) {
541 m_profile = privUtils_.checkProfile();
543 Object.defineProperties(this, {
556 getInstance: function() {
557 if (instance == null) {
558 instance = new ProfileAlarm();
559 // Hide the constructor so the returned objected can't be new'd...
560 instance.constructor = null;
567 var _profile = ProfileAlarm.getInstance().profile;
569 // Notifications feature are available on mobile and wearable profile
570 if (_profile != 'mobile' && _profile != 'wearable') {
571 delete AlarmManager.prototype.addAlarmNotification;
572 delete AlarmManager.prototype.getAlarmNotification;
573 isAlarmAbsolutePeriodDeprecated = false;
576 //exports //////////////////////////////////////////////////////////////
577 exports = new AlarmManager();