Tizen 2.0 Release
[samples/web/ExercisePlanner.git] / js / app.onAlarm.js
1 /*jslint devel:true*/
2 /*global ExercisePlanner:false, tizen:false, Audio:false*/
3 /**
4  * These method are using when alarm is call.
5  */
6 (function () {
7         "use strict";
8         ExercisePlanner.prototype.findCurrentAlarm = function () {
9                 var currentTimeInMinutes = parseInt(this.applicationStartTime.getTime() / 1000, 10),
10                         listOfAlarms = tizen.alarm.getAll();
11
12                 return listOfAlarms.filter(function (item) {
13                         // alarm relative has not date property;
14                         if (!item.date) {
15                                 return false;
16                         }
17
18                         // +40/-10 seconds tolerance;
19                         if (parseInt(item.date.getTime() / 1000, 10) < (currentTimeInMinutes + 40)
20                                         && parseInt(item.date.getTime() / 1000, 10) > (currentTimeInMinutes - 10)) {
21                                 return true;
22                         }
23                 });
24         };
25
26         ExercisePlanner.prototype.wait = function () {
27                 // lastAlert -> change +1min
28                 var snozeTime = 1, currentAlarm = this.currentAlarm, newDate = new Date(), alarm;
29
30                 if (currentAlarm) {
31                         newDate.setMinutes(newDate.getMinutes() + snozeTime);
32                         // period value must be set so application started by alert will know current alert;
33                         alarm = new tizen.AlarmAbsolute(newDate, tizen.alarm.PERIOD_WEEK * 10);
34                         tizen.alarm.add(alarm, this.selfId);
35                         // -- remove old snooze alarm
36                         if (currentAlarm.period === tizen.alarm.PERIOD_WEEK * 10) {
37                                 tizen.alarm.remove(currentAlarm.id);
38                         }
39                         this.stopMusic();
40                 }
41
42                 this.exit();
43                 // or tizen.application.hide();
44         };
45
46         ExercisePlanner.prototype.ok = function () {
47                 this.exit();
48         };
49
50         ExercisePlanner.prototype.startMusic = function () {
51
52                 if (!this.audioOfAlert) {
53                         this.audioOfAlert = new Audio();
54                 }
55                 this.audioOfAlert.src = 'WebContent/Runner.mp3';
56                 this.audioOfAlert.load();
57                 this.audioOfAlert.play();
58         };
59
60         ExercisePlanner.prototype.stopMusic = function () {
61                 if (this.audioOfAlert) {
62                         this.audioOfAlert.pause();
63                 }
64         };
65 }());
66